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