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