ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / i2c / chips / pcf8591.c
1 /*
2     pcf8591.c - Part of lm_sensors, Linux kernel modules for hardware
3                 monitoring
4     Copyright (C) 2001-2004 Aurelien Jarno <aurelien@aurel32.net>
5     Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with 
6     the help of Jean Delvare <khali@linux-fr.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-sensor.h>
28
29 /* Addresses to scan */
30 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
31 static unsigned short normal_i2c_range[] = { 0x48, 0x4f, I2C_CLIENT_END };
32 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
33 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
34
35 /* Insmod parameters */
36 SENSORS_INSMOD_1(pcf8591);
37
38 static int input_mode;
39 MODULE_PARM(input_mode, "i");
40 MODULE_PARM_DESC(input_mode,
41         "Analog input mode:\n"
42         " 0 = four single ended inputs\n"
43         " 1 = three differential inputs\n"
44         " 2 = single ended and differential mixed\n"
45         " 3 = two differential inputs\n");
46
47 /* The PCF8591 control byte
48       7    6    5    4    3    2    1    0  
49    |  0 |AOEF|   AIP   |  0 |AINC|  AICH   | */
50
51 /* Analog Output Enable Flag (analog output active if 1) */
52 #define PCF8591_CONTROL_AOEF            0x40
53                                         
54 /* Analog Input Programming 
55    0x00 = four single ended inputs
56    0x10 = three differential inputs
57    0x20 = single ended and differential mixed
58    0x30 = two differential inputs */
59 #define PCF8591_CONTROL_AIP_MASK        0x30
60
61 /* Autoincrement Flag (switch on if 1) */
62 #define PCF8591_CONTROL_AINC            0x04
63
64 /* Channel selection
65    0x00 = channel 0 
66    0x01 = channel 1
67    0x02 = channel 2
68    0x03 = channel 3 */
69 #define PCF8591_CONTROL_AICH_MASK       0x03
70
71 /* Initial values */
72 #define PCF8591_INIT_CONTROL    ((input_mode << 4) | PCF8591_CONTROL_AOEF)
73 #define PCF8591_INIT_AOUT       0       /* DAC out = 0 */
74
75 /* Conversions */
76 #define REG_TO_SIGNED(reg)      (((reg) & 0x80)?((reg) - 256):(reg))
77
78 struct pcf8591_data {
79         struct i2c_client client;
80         struct semaphore update_lock;
81
82         u8 control;
83         u8 aout;
84 };
85
86 static int pcf8591_attach_adapter(struct i2c_adapter *adapter);
87 static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind);
88 static int pcf8591_detach_client(struct i2c_client *client);
89 static void pcf8591_init_client(struct i2c_client *client);
90 static int pcf8591_read_channel(struct device *dev, int channel);
91
92 /* This is the driver that will be inserted */
93 static struct i2c_driver pcf8591_driver = {
94         .owner          = THIS_MODULE,
95         .name           = "pcf8591",
96         .id             = I2C_DRIVERID_PCF8591,
97         .flags          = I2C_DF_NOTIFY,
98         .attach_adapter = pcf8591_attach_adapter,
99         .detach_client  = pcf8591_detach_client,
100 };
101
102 static int pcf8591_id = 0;
103
104 /* following are the sysfs callback functions */
105 #define show_in_channel(channel)                                        \
106 static ssize_t show_in##channel##_input(struct device *dev, char *buf)  \
107 {                                                                       \
108         return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
109 }                                                                       \
110 static DEVICE_ATTR(in##channel##_input, S_IRUGO,                        \
111                    show_in##channel##_input, NULL);
112
113 show_in_channel(0);
114 show_in_channel(1);
115 show_in_channel(2);
116 show_in_channel(3);
117
118 static ssize_t show_out0_ouput(struct device *dev, char *buf)
119 {
120         struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
121         return sprintf(buf, "%d\n", data->aout * 10);
122 }
123
124 static ssize_t set_out0_output(struct device *dev, const char *buf, size_t count)
125 {
126         unsigned int value;
127         struct i2c_client *client = to_i2c_client(dev);
128         struct pcf8591_data *data = i2c_get_clientdata(client);
129         if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) {
130                 data->aout = value;
131                 i2c_smbus_write_byte_data(client, data->control, data->aout);
132                 return count;
133         }
134         return -EINVAL;
135 }
136
137 static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO, 
138                    show_out0_ouput, set_out0_output);
139
140 static ssize_t show_out0_enable(struct device *dev, char *buf)
141 {
142         struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
143         return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
144 }
145
146 static ssize_t set_out0_enable(struct device *dev, const char *buf, size_t count)
147 {
148         struct i2c_client *client = to_i2c_client(dev);
149         struct pcf8591_data *data = i2c_get_clientdata(client);
150         if (simple_strtoul(buf, NULL, 10))
151                 data->control |= PCF8591_CONTROL_AOEF;
152         else
153                 data->control &= ~PCF8591_CONTROL_AOEF;
154         i2c_smbus_write_byte(client, data->control);
155         return count;
156 }
157
158 static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO, 
159                    show_out0_enable, set_out0_enable);
160
161 /*
162  * Real code
163  */
164 static int pcf8591_attach_adapter(struct i2c_adapter *adapter)
165 {
166         return i2c_detect(adapter, &addr_data, pcf8591_detect);
167 }
168
169 /* This function is called by i2c_detect */
170 int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind)
171 {
172         struct i2c_client *new_client;
173         struct pcf8591_data *data;
174         int err = 0;
175
176         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
177                                      | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
178                 goto exit;
179
180         /* OK. For now, we presume we have a valid client. We now create the
181            client structure, even though we cannot fill it completely yet. */
182         if (!(data = kmalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
183                 err = -ENOMEM;
184                 goto exit;
185         }
186         memset(data, 0, sizeof(struct pcf8591_data));
187         
188         new_client = &data->client;
189         i2c_set_clientdata(new_client, data);
190         new_client->addr = address;
191         new_client->adapter = adapter;
192         new_client->driver = &pcf8591_driver;
193         new_client->flags = 0;
194
195         /* Now, we would do the remaining detection. But the PCF8591 is plainly
196            impossible to detect! Stupid chip. */
197
198         /* Determine the chip type - only one kind supported! */
199         if (kind <= 0)
200                 kind = pcf8591;
201
202         /* Fill in the remaining client fields and put it into the global 
203            list */
204         strlcpy(new_client->name, "pcf8591", I2C_NAME_SIZE);
205
206         new_client->id = pcf8591_id++;
207         init_MUTEX(&data->update_lock);
208
209         /* Tell the I2C layer a new client has arrived */
210         if ((err = i2c_attach_client(new_client)))
211                 goto exit_kfree;
212
213         /* Initialize the PCF8591 chip */
214         pcf8591_init_client(new_client);
215
216         /* Register sysfs hooks */
217         device_create_file(&new_client->dev, &dev_attr_out0_enable);
218         device_create_file(&new_client->dev, &dev_attr_out0_output);
219         device_create_file(&new_client->dev, &dev_attr_in0_input);
220         device_create_file(&new_client->dev, &dev_attr_in1_input);
221
222         /* Register input2 if not in "two differential inputs" mode */
223         if (input_mode != 3 )
224                 device_create_file(&new_client->dev, &dev_attr_in2_input);
225                 
226         /* Register input3 only in "four single ended inputs" mode */
227         if (input_mode == 0)
228                 device_create_file(&new_client->dev, &dev_attr_in3_input);
229         
230         return 0;
231         
232         /* OK, this is not exactly good programming practice, usually. But it is
233            very code-efficient in this case. */
234
235 exit_kfree:
236         kfree(data);
237 exit:
238         return err;
239 }
240
241 static int pcf8591_detach_client(struct i2c_client *client)
242 {
243         int err;
244
245         if ((err = i2c_detach_client(client))) {
246                 dev_err(&client->dev,
247                         "Client deregistration failed, client not detached.\n");
248                 return err;
249         }
250
251         kfree(i2c_get_clientdata(client));
252         return 0;
253 }
254
255 /* Called when we have found a new PCF8591. */
256 static void pcf8591_init_client(struct i2c_client *client)
257 {
258         struct pcf8591_data *data = i2c_get_clientdata(client);
259         data->control = PCF8591_INIT_CONTROL;
260         data->aout = PCF8591_INIT_AOUT;
261
262         i2c_smbus_write_byte_data(client, data->control, data->aout);
263         
264         /* The first byte transmitted contains the conversion code of the 
265            previous read cycle. FLUSH IT! */
266         i2c_smbus_read_byte(client);
267 }
268
269 static int pcf8591_read_channel(struct device *dev, int channel)
270 {
271         u8 value;
272         struct i2c_client *client = to_i2c_client(dev);
273         struct pcf8591_data *data = i2c_get_clientdata(client);
274
275         down(&data->update_lock);
276
277         if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
278                 data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
279                               | channel;
280                 i2c_smbus_write_byte(client, data->control);
281         
282                 /* The first byte transmitted contains the conversion code of 
283                    the previous read cycle. FLUSH IT! */
284                 i2c_smbus_read_byte(client);
285         }
286         value = i2c_smbus_read_byte(client);
287
288         up(&data->update_lock);
289
290         if ((channel == 2 && input_mode == 2) ||
291             (channel != 3 && (input_mode == 1 || input_mode == 3)))
292                 return (10 * REG_TO_SIGNED(value));
293         else
294                 return (10 * value);
295 }
296
297 static int __init pcf8591_init(void)
298 {
299         if (input_mode < 0 || input_mode > 3) {
300                 printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n",
301                        input_mode);
302                 input_mode = 0;
303         }
304         return i2c_add_driver(&pcf8591_driver);
305 }
306
307 static void __exit pcf8591_exit(void)
308 {
309         i2c_del_driver(&pcf8591_driver);
310 }
311
312 MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
313 MODULE_DESCRIPTION("PCF8591 driver");
314 MODULE_LICENSE("GPL");
315
316 module_init(pcf8591_init);
317 module_exit(pcf8591_exit);