Merge master.kernel.org:/pub/scm/linux/kernel/git/sridhar/lksctp-2.6
[sfrench/cifs-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
28 /* Addresses to scan */
29 static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
30                                         0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
31
32 /* Insmod parameters */
33 I2C_CLIENT_INSMOD_1(pcf8591);
34
35 static int input_mode;
36 module_param(input_mode, int, 0);
37 MODULE_PARM_DESC(input_mode,
38         "Analog input mode:\n"
39         " 0 = four single ended inputs\n"
40         " 1 = three differential inputs\n"
41         " 2 = single ended and differential mixed\n"
42         " 3 = two differential inputs\n");
43
44 /* The PCF8591 control byte
45       7    6    5    4    3    2    1    0  
46    |  0 |AOEF|   AIP   |  0 |AINC|  AICH   | */
47
48 /* Analog Output Enable Flag (analog output active if 1) */
49 #define PCF8591_CONTROL_AOEF            0x40
50                                         
51 /* Analog Input Programming 
52    0x00 = four single ended inputs
53    0x10 = three differential inputs
54    0x20 = single ended and differential mixed
55    0x30 = two differential inputs */
56 #define PCF8591_CONTROL_AIP_MASK        0x30
57
58 /* Autoincrement Flag (switch on if 1) */
59 #define PCF8591_CONTROL_AINC            0x04
60
61 /* Channel selection
62    0x00 = channel 0 
63    0x01 = channel 1
64    0x02 = channel 2
65    0x03 = channel 3 */
66 #define PCF8591_CONTROL_AICH_MASK       0x03
67
68 /* Initial values */
69 #define PCF8591_INIT_CONTROL    ((input_mode << 4) | PCF8591_CONTROL_AOEF)
70 #define PCF8591_INIT_AOUT       0       /* DAC out = 0 */
71
72 /* Conversions */
73 #define REG_TO_SIGNED(reg)      (((reg) & 0x80)?((reg) - 256):(reg))
74
75 struct pcf8591_data {
76         struct i2c_client client;
77         struct semaphore update_lock;
78
79         u8 control;
80         u8 aout;
81 };
82
83 static int pcf8591_attach_adapter(struct i2c_adapter *adapter);
84 static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind);
85 static int pcf8591_detach_client(struct i2c_client *client);
86 static void pcf8591_init_client(struct i2c_client *client);
87 static int pcf8591_read_channel(struct device *dev, int channel);
88
89 /* This is the driver that will be inserted */
90 static struct i2c_driver pcf8591_driver = {
91         .owner          = THIS_MODULE,
92         .name           = "pcf8591",
93         .id             = I2C_DRIVERID_PCF8591,
94         .flags          = I2C_DF_NOTIFY,
95         .attach_adapter = pcf8591_attach_adapter,
96         .detach_client  = pcf8591_detach_client,
97 };
98
99 /* following are the sysfs callback functions */
100 #define show_in_channel(channel)                                        \
101 static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf)   \
102 {                                                                       \
103         return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
104 }                                                                       \
105 static DEVICE_ATTR(in##channel##_input, S_IRUGO,                        \
106                    show_in##channel##_input, NULL);
107
108 show_in_channel(0);
109 show_in_channel(1);
110 show_in_channel(2);
111 show_in_channel(3);
112
113 static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
114 {
115         struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
116         return sprintf(buf, "%d\n", data->aout * 10);
117 }
118
119 static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
120 {
121         unsigned int value;
122         struct i2c_client *client = to_i2c_client(dev);
123         struct pcf8591_data *data = i2c_get_clientdata(client);
124         if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) {
125                 data->aout = value;
126                 i2c_smbus_write_byte_data(client, data->control, data->aout);
127                 return count;
128         }
129         return -EINVAL;
130 }
131
132 static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO, 
133                    show_out0_ouput, set_out0_output);
134
135 static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
136 {
137         struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
138         return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
139 }
140
141 static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
142 {
143         struct i2c_client *client = to_i2c_client(dev);
144         struct pcf8591_data *data = i2c_get_clientdata(client);
145         unsigned long val = simple_strtoul(buf, NULL, 10);
146
147         down(&data->update_lock);
148         if (val)
149                 data->control |= PCF8591_CONTROL_AOEF;
150         else
151                 data->control &= ~PCF8591_CONTROL_AOEF;
152         i2c_smbus_write_byte(client, data->control);
153         up(&data->update_lock);
154         return count;
155 }
156
157 static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO, 
158                    show_out0_enable, set_out0_enable);
159
160 /*
161  * Real code
162  */
163 static int pcf8591_attach_adapter(struct i2c_adapter *adapter)
164 {
165         return i2c_probe(adapter, &addr_data, pcf8591_detect);
166 }
167
168 /* This function is called by i2c_probe */
169 static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind)
170 {
171         struct i2c_client *new_client;
172         struct pcf8591_data *data;
173         int err = 0;
174
175         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
176                                      | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
177                 goto exit;
178
179         /* OK. For now, we presume we have a valid client. We now create the
180            client structure, even though we cannot fill it completely yet. */
181         if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
182                 err = -ENOMEM;
183                 goto exit;
184         }
185         
186         new_client = &data->client;
187         i2c_set_clientdata(new_client, data);
188         new_client->addr = address;
189         new_client->adapter = adapter;
190         new_client->driver = &pcf8591_driver;
191         new_client->flags = 0;
192
193         /* Now, we would do the remaining detection. But the PCF8591 is plainly
194            impossible to detect! Stupid chip. */
195
196         /* Determine the chip type - only one kind supported! */
197         if (kind <= 0)
198                 kind = pcf8591;
199
200         /* Fill in the remaining client fields and put it into the global 
201            list */
202         strlcpy(new_client->name, "pcf8591", I2C_NAME_SIZE);
203         init_MUTEX(&data->update_lock);
204
205         /* Tell the I2C layer a new client has arrived */
206         if ((err = i2c_attach_client(new_client)))
207                 goto exit_kfree;
208
209         /* Initialize the PCF8591 chip */
210         pcf8591_init_client(new_client);
211
212         /* Register sysfs hooks */
213         device_create_file(&new_client->dev, &dev_attr_out0_enable);
214         device_create_file(&new_client->dev, &dev_attr_out0_output);
215         device_create_file(&new_client->dev, &dev_attr_in0_input);
216         device_create_file(&new_client->dev, &dev_attr_in1_input);
217
218         /* Register input2 if not in "two differential inputs" mode */
219         if (input_mode != 3 )
220                 device_create_file(&new_client->dev, &dev_attr_in2_input);
221                 
222         /* Register input3 only in "four single ended inputs" mode */
223         if (input_mode == 0)
224                 device_create_file(&new_client->dev, &dev_attr_in3_input);
225         
226         return 0;
227         
228         /* OK, this is not exactly good programming practice, usually. But it is
229            very code-efficient in this case. */
230
231 exit_kfree:
232         kfree(data);
233 exit:
234         return err;
235 }
236
237 static int pcf8591_detach_client(struct i2c_client *client)
238 {
239         int err;
240
241         if ((err = i2c_detach_client(client)))
242                 return err;
243
244         kfree(i2c_get_clientdata(client));
245         return 0;
246 }
247
248 /* Called when we have found a new PCF8591. */
249 static void pcf8591_init_client(struct i2c_client *client)
250 {
251         struct pcf8591_data *data = i2c_get_clientdata(client);
252         data->control = PCF8591_INIT_CONTROL;
253         data->aout = PCF8591_INIT_AOUT;
254
255         i2c_smbus_write_byte_data(client, data->control, data->aout);
256         
257         /* The first byte transmitted contains the conversion code of the 
258            previous read cycle. FLUSH IT! */
259         i2c_smbus_read_byte(client);
260 }
261
262 static int pcf8591_read_channel(struct device *dev, int channel)
263 {
264         u8 value;
265         struct i2c_client *client = to_i2c_client(dev);
266         struct pcf8591_data *data = i2c_get_clientdata(client);
267
268         down(&data->update_lock);
269
270         if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
271                 data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
272                               | channel;
273                 i2c_smbus_write_byte(client, data->control);
274         
275                 /* The first byte transmitted contains the conversion code of 
276                    the previous read cycle. FLUSH IT! */
277                 i2c_smbus_read_byte(client);
278         }
279         value = i2c_smbus_read_byte(client);
280
281         up(&data->update_lock);
282
283         if ((channel == 2 && input_mode == 2) ||
284             (channel != 3 && (input_mode == 1 || input_mode == 3)))
285                 return (10 * REG_TO_SIGNED(value));
286         else
287                 return (10 * value);
288 }
289
290 static int __init pcf8591_init(void)
291 {
292         if (input_mode < 0 || input_mode > 3) {
293                 printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n",
294                        input_mode);
295                 input_mode = 0;
296         }
297         return i2c_add_driver(&pcf8591_driver);
298 }
299
300 static void __exit pcf8591_exit(void)
301 {
302         i2c_del_driver(&pcf8591_driver);
303 }
304
305 MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
306 MODULE_DESCRIPTION("PCF8591 driver");
307 MODULE_LICENSE("GPL");
308
309 module_init(pcf8591_init);
310 module_exit(pcf8591_exit);