i2c: Drop I2C_CLIENT_INSMOD_1
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / pcf8591.c
blobd447879498516d9f385bf37070f8ff0f3fc10f23
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>
27 /* Addresses to scan */
28 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
29 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
31 /* Insmod parameters */
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");
42 /* The PCF8591 control byte
43 7 6 5 4 3 2 1 0
44 | 0 |AOEF| AIP | 0 |AINC| AICH | */
46 /* Analog Output Enable Flag (analog output active if 1) */
47 #define PCF8591_CONTROL_AOEF 0x40
49 /* Analog Input Programming
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
56 /* Autoincrement Flag (switch on if 1) */
57 #define PCF8591_CONTROL_AINC 0x04
59 /* Channel selection
60 0x00 = channel 0
61 0x01 = channel 1
62 0x02 = channel 2
63 0x03 = channel 3 */
64 #define PCF8591_CONTROL_AICH_MASK 0x03
66 /* Initial values */
67 #define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF)
68 #define PCF8591_INIT_AOUT 0 /* DAC out = 0 */
70 /* Conversions */
71 #define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg))
73 struct pcf8591_data {
74 struct mutex update_lock;
76 u8 control;
77 u8 aout;
80 static void pcf8591_init_client(struct i2c_client *client);
81 static int pcf8591_read_channel(struct device *dev, int channel);
83 /* following are the sysfs callback functions */
84 #define show_in_channel(channel) \
85 static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \
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);
92 show_in_channel(0);
93 show_in_channel(1);
94 show_in_channel(2);
95 show_in_channel(3);
97 static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
99 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
100 return sprintf(buf, "%d\n", data->aout * 10);
103 static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
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;
113 return -EINVAL;
116 static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
117 show_out0_ouput, set_out0_output);
119 static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
121 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
122 return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
125 static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
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);
131 mutex_lock(&data->update_lock);
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);
137 mutex_unlock(&data->update_lock);
138 return count;
141 static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
142 show_out0_enable, set_out0_enable);
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
152 static const struct attribute_group pcf8591_attr_group = {
153 .attrs = pcf8591_attributes,
156 static struct attribute *pcf8591_attributes_opt[] = {
157 &dev_attr_in2_input.attr,
158 &dev_attr_in3_input.attr,
159 NULL
162 static const struct attribute_group pcf8591_attr_group_opt = {
163 .attrs = pcf8591_attributes_opt,
167 * Real code
170 /* Return 0 if detection is successful, -ENODEV otherwise */
171 static int pcf8591_detect(struct i2c_client *client,
172 struct i2c_board_info *info)
174 struct i2c_adapter *adapter = client->adapter;
176 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
177 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
178 return -ENODEV;
180 /* Now, we would do the remaining detection. But the PCF8591 is plainly
181 impossible to detect! Stupid chip. */
183 strlcpy(info->type, "pcf8591", I2C_NAME_SIZE);
185 return 0;
188 static int pcf8591_probe(struct i2c_client *client,
189 const struct i2c_device_id *id)
191 struct pcf8591_data *data;
192 int err;
194 if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
195 err = -ENOMEM;
196 goto exit;
199 i2c_set_clientdata(client, data);
200 mutex_init(&data->update_lock);
202 /* Initialize the PCF8591 chip */
203 pcf8591_init_client(client);
205 /* Register sysfs hooks */
206 err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
207 if (err)
208 goto exit_kfree;
210 /* Register input2 if not in "two differential inputs" mode */
211 if (input_mode != 3) {
212 if ((err = device_create_file(&client->dev,
213 &dev_attr_in2_input)))
214 goto exit_sysfs_remove;
217 /* Register input3 only in "four single ended inputs" mode */
218 if (input_mode == 0) {
219 if ((err = device_create_file(&client->dev,
220 &dev_attr_in3_input)))
221 goto exit_sysfs_remove;
224 return 0;
226 exit_sysfs_remove:
227 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
228 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
229 exit_kfree:
230 kfree(data);
231 exit:
232 return err;
235 static int pcf8591_remove(struct i2c_client *client)
237 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
238 sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
239 kfree(i2c_get_clientdata(client));
240 return 0;
243 /* Called when we have found a new PCF8591. */
244 static void pcf8591_init_client(struct i2c_client *client)
246 struct pcf8591_data *data = i2c_get_clientdata(client);
247 data->control = PCF8591_INIT_CONTROL;
248 data->aout = PCF8591_INIT_AOUT;
250 i2c_smbus_write_byte_data(client, data->control, data->aout);
252 /* The first byte transmitted contains the conversion code of the
253 previous read cycle. FLUSH IT! */
254 i2c_smbus_read_byte(client);
257 static int pcf8591_read_channel(struct device *dev, int channel)
259 u8 value;
260 struct i2c_client *client = to_i2c_client(dev);
261 struct pcf8591_data *data = i2c_get_clientdata(client);
263 mutex_lock(&data->update_lock);
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);
270 /* The first byte transmitted contains the conversion code of
271 the previous read cycle. FLUSH IT! */
272 i2c_smbus_read_byte(client);
274 value = i2c_smbus_read_byte(client);
276 mutex_unlock(&data->update_lock);
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);
285 static const struct i2c_device_id pcf8591_id[] = {
286 { "pcf8591", 0 },
289 MODULE_DEVICE_TABLE(i2c, pcf8591_id);
291 static struct i2c_driver pcf8591_driver = {
292 .driver = {
293 .name = "pcf8591",
295 .probe = pcf8591_probe,
296 .remove = pcf8591_remove,
297 .id_table = pcf8591_id,
299 .class = I2C_CLASS_HWMON, /* Nearest choice */
300 .detect = pcf8591_detect,
301 .address_list = normal_i2c,
304 static int __init pcf8591_init(void)
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;
311 return i2c_add_driver(&pcf8591_driver);
314 static void __exit pcf8591_exit(void)
316 i2c_del_driver(&pcf8591_driver);
319 MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
320 MODULE_DESCRIPTION("PCF8591 driver");
321 MODULE_LICENSE("GPL");
323 module_init(pcf8591_init);
324 module_exit(pcf8591_exit);