blob: d447879498516d9f385bf37070f8ff0f3fc10f23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2001-2004 Aurelien Jarno <aurelien@aurel32.net>
Jean Delvarefb4504f2009-03-30 21:46:43 +02003 Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 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>
Ingo Molnar5c085d32006-01-18 23:16:04 +010025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/* Addresses to scan */
Jean Delvare2cdddeb2008-01-27 18:14:47 +010028static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* Insmod parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33static int input_mode;
34module_param(input_mode, int, 0);
35MODULE_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
Jean Delvarefb4504f2009-03-30 21:46:43 +020043 7 6 5 4 3 2 1 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 | 0 |AOEF| AIP | 0 |AINC| AICH | */
45
46/* Analog Output Enable Flag (analog output active if 1) */
47#define PCF8591_CONTROL_AOEF 0x40
Jean Delvarefb4504f2009-03-30 21:46:43 +020048
49/* Analog Input Programming
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 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
Jean Delvarefb4504f2009-03-30 21:46:43 +020060 0x00 = channel 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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
73struct pcf8591_data {
Ingo Molnar5c085d32006-01-18 23:16:04 +010074 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 u8 control;
77 u8 aout;
78};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static void pcf8591_init_client(struct i2c_client *client);
81static int pcf8591_read_channel(struct device *dev, int channel);
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* following are the sysfs callback functions */
84#define show_in_channel(channel) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -040085static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{ \
87 return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
88} \
89static DEVICE_ATTR(in##channel##_input, S_IRUGO, \
90 show_in##channel##_input, NULL);
91
92show_in_channel(0);
93show_in_channel(1);
94show_in_channel(2);
95show_in_channel(3);
96
Yani Ioannoua5099cf2005-05-17 06:42:25 -040097static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
100 return sprintf(buf, "%d\n", data->aout * 10);
101}
102
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400103static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
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
Jean Delvarefb4504f2009-03-30 21:46:43 +0200116static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 show_out0_ouput, set_out0_output);
118
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400119static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
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
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400125static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
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
Ingo Molnar5c085d32006-01-18 23:16:04 +0100131 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (val)
133 data->control |= PCF8591_CONTROL_AOEF;
134 else
135 data->control &= ~PCF8591_CONTROL_AOEF;
136 i2c_smbus_write_byte(client, data->control);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100137 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return count;
139}
140
Jean Delvarefb4504f2009-03-30 21:46:43 +0200141static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 show_out0_enable, set_out0_enable);
143
Jean Delvare7d9db672006-09-03 22:20:24 +0200144static 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
152static const struct attribute_group pcf8591_attr_group = {
153 .attrs = pcf8591_attributes,
154};
155
156static struct attribute *pcf8591_attributes_opt[] = {
157 &dev_attr_in2_input.attr,
158 &dev_attr_in3_input.attr,
159 NULL
160};
161
162static const struct attribute_group pcf8591_attr_group_opt = {
163 .attrs = pcf8591_attributes_opt,
164};
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
167 * Real code
168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200170/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100171static int pcf8591_detect(struct i2c_client *client,
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200172 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200174 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
177 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200178 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200180 /* 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
188static int pcf8591_probe(struct i2c_client *client,
189 const struct i2c_device_id *id)
190{
191 struct pcf8591_data *data;
192 int err;
193
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200194 if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 err = -ENOMEM;
196 goto exit;
197 }
Jean Delvarefb4504f2009-03-30 21:46:43 +0200198
Jean Delvaref741f672008-07-14 22:38:36 +0200199 i2c_set_clientdata(client, data);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100200 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Initialize the PCF8591 chip */
Jean Delvaref741f672008-07-14 22:38:36 +0200203 pcf8591_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* Register sysfs hooks */
Jean Delvaref741f672008-07-14 22:38:36 +0200206 err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
Jean Delvare7d9db672006-09-03 22:20:24 +0200207 if (err)
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200208 goto exit_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* Register input2 if not in "two differential inputs" mode */
Jean Delvare7d9db672006-09-03 22:20:24 +0200211 if (input_mode != 3) {
Jean Delvaref741f672008-07-14 22:38:36 +0200212 if ((err = device_create_file(&client->dev,
Jean Delvare7d9db672006-09-03 22:20:24 +0200213 &dev_attr_in2_input)))
214 goto exit_sysfs_remove;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jean Delvare7d9db672006-09-03 22:20:24 +0200217 /* Register input3 only in "four single ended inputs" mode */
218 if (input_mode == 0) {
Jean Delvaref741f672008-07-14 22:38:36 +0200219 if ((err = device_create_file(&client->dev,
Jean Delvare7d9db672006-09-03 22:20:24 +0200220 &dev_attr_in3_input)))
221 goto exit_sysfs_remove;
222 }
223
224 return 0;
225
226exit_sysfs_remove:
Jean Delvaref741f672008-07-14 22:38:36 +0200227 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
228 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229exit_kfree:
230 kfree(data);
231exit:
232 return err;
233}
234
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200235static int pcf8591_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Jean Delvare7d9db672006-09-03 22:20:24 +0200237 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
238 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 kfree(i2c_get_clientdata(client));
240 return 0;
241}
242
243/* Called when we have found a new PCF8591. */
244static 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);
Jean Delvarefb4504f2009-03-30 21:46:43 +0200251
252 /* The first byte transmitted contains the conversion code of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 previous read cycle. FLUSH IT! */
254 i2c_smbus_read_byte(client);
255}
256
257static 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
Ingo Molnar5c085d32006-01-18 23:16:04 +0100263 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
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);
Jean Delvarefb4504f2009-03-30 21:46:43 +0200269
270 /* The first byte transmitted contains the conversion code of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 the previous read cycle. FLUSH IT! */
272 i2c_smbus_read_byte(client);
273 }
274 value = i2c_smbus_read_byte(client);
275
Ingo Molnar5c085d32006-01-18 23:16:04 +0100276 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
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
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200285static const struct i2c_device_id pcf8591_id[] = {
286 { "pcf8591", 0 },
287 { }
288};
289MODULE_DEVICE_TABLE(i2c, pcf8591_id);
290
291static 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,
Jean Delvarec3813d62009-12-14 21:17:25 +0100301 .address_list = normal_i2c,
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200302};
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static 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
314static void __exit pcf8591_exit(void)
315{
316 i2c_del_driver(&pcf8591_driver);
317}
318
319MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
320MODULE_DESCRIPTION("PCF8591 driver");
321MODULE_LICENSE("GPL");
322
323module_init(pcf8591_init);
324module_exit(pcf8591_exit);