blob: dc7259d698120d895aedc8b13d9c73c564c0e00b [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>
Jean Delvare4275fcd2010-10-28 20:31:49 +020026#include <linux/err.h>
27#include <linux/hwmon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Insmod parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31static int input_mode;
32module_param(input_mode, int, 0);
33MODULE_PARM_DESC(input_mode,
34 "Analog input mode:\n"
35 " 0 = four single ended inputs\n"
36 " 1 = three differential inputs\n"
37 " 2 = single ended and differential mixed\n"
38 " 3 = two differential inputs\n");
39
40/* The PCF8591 control byte
Jean Delvarefb4504f2009-03-30 21:46:43 +020041 7 6 5 4 3 2 1 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 | 0 |AOEF| AIP | 0 |AINC| AICH | */
43
44/* Analog Output Enable Flag (analog output active if 1) */
45#define PCF8591_CONTROL_AOEF 0x40
Jean Delvarefb4504f2009-03-30 21:46:43 +020046
47/* Analog Input Programming
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 0x00 = four single ended inputs
49 0x10 = three differential inputs
50 0x20 = single ended and differential mixed
51 0x30 = two differential inputs */
52#define PCF8591_CONTROL_AIP_MASK 0x30
53
54/* Autoincrement Flag (switch on if 1) */
55#define PCF8591_CONTROL_AINC 0x04
56
57/* Channel selection
Jean Delvarefb4504f2009-03-30 21:46:43 +020058 0x00 = channel 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 0x01 = channel 1
60 0x02 = channel 2
61 0x03 = channel 3 */
62#define PCF8591_CONTROL_AICH_MASK 0x03
63
64/* Initial values */
65#define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF)
66#define PCF8591_INIT_AOUT 0 /* DAC out = 0 */
67
68/* Conversions */
69#define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg))
70
71struct pcf8591_data {
Jean Delvare4275fcd2010-10-28 20:31:49 +020072 struct device *hwmon_dev;
Ingo Molnar5c085d32006-01-18 23:16:04 +010073 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 u8 control;
76 u8 aout;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static void pcf8591_init_client(struct i2c_client *client);
80static int pcf8591_read_channel(struct device *dev, int channel);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* following are the sysfs callback functions */
83#define show_in_channel(channel) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -040084static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{ \
86 return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
87} \
88static DEVICE_ATTR(in##channel##_input, S_IRUGO, \
89 show_in##channel##_input, NULL);
90
91show_in_channel(0);
92show_in_channel(1);
93show_in_channel(2);
94show_in_channel(3);
95
Yani Ioannoua5099cf2005-05-17 06:42:25 -040096static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
99 return sprintf(buf, "%d\n", data->aout * 10);
100}
101
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400102static 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 -0700103{
104 unsigned int value;
105 struct i2c_client *client = to_i2c_client(dev);
106 struct pcf8591_data *data = i2c_get_clientdata(client);
107 if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) {
108 data->aout = value;
109 i2c_smbus_write_byte_data(client, data->control, data->aout);
110 return count;
111 }
112 return -EINVAL;
113}
114
Jean Delvarefb4504f2009-03-30 21:46:43 +0200115static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 show_out0_ouput, set_out0_output);
117
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400118static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
121 return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
122}
123
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400124static 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 -0700125{
126 struct i2c_client *client = to_i2c_client(dev);
127 struct pcf8591_data *data = i2c_get_clientdata(client);
128 unsigned long val = simple_strtoul(buf, NULL, 10);
129
Ingo Molnar5c085d32006-01-18 23:16:04 +0100130 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (val)
132 data->control |= PCF8591_CONTROL_AOEF;
133 else
134 data->control &= ~PCF8591_CONTROL_AOEF;
135 i2c_smbus_write_byte(client, data->control);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100136 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return count;
138}
139
Jean Delvarefb4504f2009-03-30 21:46:43 +0200140static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 show_out0_enable, set_out0_enable);
142
Jean Delvare7d9db672006-09-03 22:20:24 +0200143static struct attribute *pcf8591_attributes[] = {
144 &dev_attr_out0_enable.attr,
145 &dev_attr_out0_output.attr,
146 &dev_attr_in0_input.attr,
147 &dev_attr_in1_input.attr,
148 NULL
149};
150
151static const struct attribute_group pcf8591_attr_group = {
152 .attrs = pcf8591_attributes,
153};
154
155static struct attribute *pcf8591_attributes_opt[] = {
156 &dev_attr_in2_input.attr,
157 &dev_attr_in3_input.attr,
158 NULL
159};
160
161static const struct attribute_group pcf8591_attr_group_opt = {
162 .attrs = pcf8591_attributes_opt,
163};
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
166 * Real code
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200169static int pcf8591_probe(struct i2c_client *client,
170 const struct i2c_device_id *id)
171{
172 struct pcf8591_data *data;
173 int err;
174
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200175 if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 err = -ENOMEM;
177 goto exit;
178 }
Jean Delvarefb4504f2009-03-30 21:46:43 +0200179
Jean Delvaref741f672008-07-14 22:38:36 +0200180 i2c_set_clientdata(client, data);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100181 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Initialize the PCF8591 chip */
Jean Delvaref741f672008-07-14 22:38:36 +0200184 pcf8591_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* Register sysfs hooks */
Jean Delvaref741f672008-07-14 22:38:36 +0200187 err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
Jean Delvare7d9db672006-09-03 22:20:24 +0200188 if (err)
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200189 goto exit_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* Register input2 if not in "two differential inputs" mode */
Jean Delvare7d9db672006-09-03 22:20:24 +0200192 if (input_mode != 3) {
Jean Delvaref741f672008-07-14 22:38:36 +0200193 if ((err = device_create_file(&client->dev,
Jean Delvare7d9db672006-09-03 22:20:24 +0200194 &dev_attr_in2_input)))
195 goto exit_sysfs_remove;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jean Delvare7d9db672006-09-03 22:20:24 +0200198 /* Register input3 only in "four single ended inputs" mode */
199 if (input_mode == 0) {
Jean Delvaref741f672008-07-14 22:38:36 +0200200 if ((err = device_create_file(&client->dev,
Jean Delvare7d9db672006-09-03 22:20:24 +0200201 &dev_attr_in3_input)))
202 goto exit_sysfs_remove;
203 }
204
Jean Delvare4275fcd2010-10-28 20:31:49 +0200205 data->hwmon_dev = hwmon_device_register(&client->dev);
206 if (IS_ERR(data->hwmon_dev)) {
207 err = PTR_ERR(data->hwmon_dev);
208 goto exit_sysfs_remove;
209 }
210
Jean Delvare7d9db672006-09-03 22:20:24 +0200211 return 0;
212
213exit_sysfs_remove:
Jean Delvaref741f672008-07-14 22:38:36 +0200214 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
215 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216exit_kfree:
217 kfree(data);
218exit:
219 return err;
220}
221
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200222static int pcf8591_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Jean Delvare4275fcd2010-10-28 20:31:49 +0200224 struct pcf8591_data *data = i2c_get_clientdata(client);
225
226 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare7d9db672006-09-03 22:20:24 +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 -0700229 kfree(i2c_get_clientdata(client));
230 return 0;
231}
232
233/* Called when we have found a new PCF8591. */
234static void pcf8591_init_client(struct i2c_client *client)
235{
236 struct pcf8591_data *data = i2c_get_clientdata(client);
237 data->control = PCF8591_INIT_CONTROL;
238 data->aout = PCF8591_INIT_AOUT;
239
240 i2c_smbus_write_byte_data(client, data->control, data->aout);
Jean Delvarefb4504f2009-03-30 21:46:43 +0200241
242 /* The first byte transmitted contains the conversion code of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 previous read cycle. FLUSH IT! */
244 i2c_smbus_read_byte(client);
245}
246
247static int pcf8591_read_channel(struct device *dev, int channel)
248{
249 u8 value;
250 struct i2c_client *client = to_i2c_client(dev);
251 struct pcf8591_data *data = i2c_get_clientdata(client);
252
Ingo Molnar5c085d32006-01-18 23:16:04 +0100253 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
256 data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
257 | channel;
258 i2c_smbus_write_byte(client, data->control);
Jean Delvarefb4504f2009-03-30 21:46:43 +0200259
260 /* The first byte transmitted contains the conversion code of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 the previous read cycle. FLUSH IT! */
262 i2c_smbus_read_byte(client);
263 }
264 value = i2c_smbus_read_byte(client);
265
Ingo Molnar5c085d32006-01-18 23:16:04 +0100266 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if ((channel == 2 && input_mode == 2) ||
269 (channel != 3 && (input_mode == 1 || input_mode == 3)))
270 return (10 * REG_TO_SIGNED(value));
271 else
272 return (10 * value);
273}
274
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200275static const struct i2c_device_id pcf8591_id[] = {
276 { "pcf8591", 0 },
277 { }
278};
279MODULE_DEVICE_TABLE(i2c, pcf8591_id);
280
281static struct i2c_driver pcf8591_driver = {
282 .driver = {
283 .name = "pcf8591",
284 },
285 .probe = pcf8591_probe,
286 .remove = pcf8591_remove,
287 .id_table = pcf8591_id,
Jean Delvare8b77e6a2008-07-16 19:30:06 +0200288};
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static 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
300static void __exit pcf8591_exit(void)
301{
302 i2c_del_driver(&pcf8591_driver);
303}
304
305MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
306MODULE_DESCRIPTION("PCF8591 driver");
307MODULE_LICENSE("GPL");
308
309module_init(pcf8591_init);
310module_exit(pcf8591_exit);