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