b44: fix workarround for wap54g10
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / pcf8591.c
blobdc7259d698120d895aedc8b13d9c73c564c0e00b
1 /*
2 Copyright (C) 2001-2004 Aurelien Jarno <aurelien@aurel32.net>
3 Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
4 the help of Jean Delvare <khali@linux-fr.org>
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.
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.
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.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/i2c.h>
25 #include <linux/mutex.h>
26 #include <linux/err.h>
27 #include <linux/hwmon.h>
29 /* Insmod parameters */
31 static int input_mode;
32 module_param(input_mode, int, 0);
33 MODULE_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");
40 /* The PCF8591 control byte
41 7 6 5 4 3 2 1 0
42 | 0 |AOEF| AIP | 0 |AINC| AICH | */
44 /* Analog Output Enable Flag (analog output active if 1) */
45 #define PCF8591_CONTROL_AOEF 0x40
47 /* Analog Input Programming
48 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
54 /* Autoincrement Flag (switch on if 1) */
55 #define PCF8591_CONTROL_AINC 0x04
57 /* Channel selection
58 0x00 = channel 0
59 0x01 = channel 1
60 0x02 = channel 2
61 0x03 = channel 3 */
62 #define PCF8591_CONTROL_AICH_MASK 0x03
64 /* Initial values */
65 #define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF)
66 #define PCF8591_INIT_AOUT 0 /* DAC out = 0 */
68 /* Conversions */
69 #define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg))
71 struct pcf8591_data {
72 struct device *hwmon_dev;
73 struct mutex update_lock;
75 u8 control;
76 u8 aout;
79 static void pcf8591_init_client(struct i2c_client *client);
80 static int pcf8591_read_channel(struct device *dev, int channel);
82 /* following are the sysfs callback functions */
83 #define show_in_channel(channel) \
84 static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \
85 { \
86 return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
87 } \
88 static DEVICE_ATTR(in##channel##_input, S_IRUGO, \
89 show_in##channel##_input, NULL);
91 show_in_channel(0);
92 show_in_channel(1);
93 show_in_channel(2);
94 show_in_channel(3);
96 static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
98 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
99 return sprintf(buf, "%d\n", data->aout * 10);
102 static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
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;
112 return -EINVAL;
115 static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
116 show_out0_ouput, set_out0_output);
118 static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
120 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
121 return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
124 static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
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);
130 mutex_lock(&data->update_lock);
131 if (val)
132 data->control |= PCF8591_CONTROL_AOEF;
133 else
134 data->control &= ~PCF8591_CONTROL_AOEF;
135 i2c_smbus_write_byte(client, data->control);
136 mutex_unlock(&data->update_lock);
137 return count;
140 static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
141 show_out0_enable, set_out0_enable);
143 static 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
151 static const struct attribute_group pcf8591_attr_group = {
152 .attrs = pcf8591_attributes,
155 static struct attribute *pcf8591_attributes_opt[] = {
156 &dev_attr_in2_input.attr,
157 &dev_attr_in3_input.attr,
158 NULL
161 static const struct attribute_group pcf8591_attr_group_opt = {
162 .attrs = pcf8591_attributes_opt,
166 * Real code
169 static int pcf8591_probe(struct i2c_client *client,
170 const struct i2c_device_id *id)
172 struct pcf8591_data *data;
173 int err;
175 if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
176 err = -ENOMEM;
177 goto exit;
180 i2c_set_clientdata(client, data);
181 mutex_init(&data->update_lock);
183 /* Initialize the PCF8591 chip */
184 pcf8591_init_client(client);
186 /* Register sysfs hooks */
187 err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
188 if (err)
189 goto exit_kfree;
191 /* Register input2 if not in "two differential inputs" mode */
192 if (input_mode != 3) {
193 if ((err = device_create_file(&client->dev,
194 &dev_attr_in2_input)))
195 goto exit_sysfs_remove;
198 /* Register input3 only in "four single ended inputs" mode */
199 if (input_mode == 0) {
200 if ((err = device_create_file(&client->dev,
201 &dev_attr_in3_input)))
202 goto exit_sysfs_remove;
205 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;
211 return 0;
213 exit_sysfs_remove:
214 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
215 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
216 exit_kfree:
217 kfree(data);
218 exit:
219 return err;
222 static int pcf8591_remove(struct i2c_client *client)
224 struct pcf8591_data *data = i2c_get_clientdata(client);
226 hwmon_device_unregister(data->hwmon_dev);
227 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
228 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
229 kfree(i2c_get_clientdata(client));
230 return 0;
233 /* Called when we have found a new PCF8591. */
234 static void pcf8591_init_client(struct i2c_client *client)
236 struct pcf8591_data *data = i2c_get_clientdata(client);
237 data->control = PCF8591_INIT_CONTROL;
238 data->aout = PCF8591_INIT_AOUT;
240 i2c_smbus_write_byte_data(client, data->control, data->aout);
242 /* The first byte transmitted contains the conversion code of the
243 previous read cycle. FLUSH IT! */
244 i2c_smbus_read_byte(client);
247 static int pcf8591_read_channel(struct device *dev, int channel)
249 u8 value;
250 struct i2c_client *client = to_i2c_client(dev);
251 struct pcf8591_data *data = i2c_get_clientdata(client);
253 mutex_lock(&data->update_lock);
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);
260 /* The first byte transmitted contains the conversion code of
261 the previous read cycle. FLUSH IT! */
262 i2c_smbus_read_byte(client);
264 value = i2c_smbus_read_byte(client);
266 mutex_unlock(&data->update_lock);
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);
275 static const struct i2c_device_id pcf8591_id[] = {
276 { "pcf8591", 0 },
279 MODULE_DEVICE_TABLE(i2c, pcf8591_id);
281 static struct i2c_driver pcf8591_driver = {
282 .driver = {
283 .name = "pcf8591",
285 .probe = pcf8591_probe,
286 .remove = pcf8591_remove,
287 .id_table = pcf8591_id,
290 static int __init pcf8591_init(void)
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;
297 return i2c_add_driver(&pcf8591_driver);
300 static void __exit pcf8591_exit(void)
302 i2c_del_driver(&pcf8591_driver);
305 MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
306 MODULE_DESCRIPTION("PCF8591 driver");
307 MODULE_LICENSE("GPL");
309 module_init(pcf8591_init);
310 module_exit(pcf8591_exit);