Blame view

drivers/hwmon/pcf8591.c 8.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
      Copyright (C) 2001-2004 Aurelien Jarno <aurelien@aurel32.net>
fb4504fe8   Jean Delvare   Move the pcf8591 ...
3
      Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
      the help of Jean Delvare <khali@linux-fr.org>
  
      This program is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation; either version 2 of the License, or
      (at your option) any later version.
  
      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
  
      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
2caec1343   Joe Perches   hwmon: (pcf8591) ...
20
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/i2c.h>
5c085d369   Ingo Molnar   [PATCH] i2c: Sema...
25
  #include <linux/mutex.h>
4275fcd65   Jean Delvare   hwmon: (pcf8591) ...
26
27
  #include <linux/err.h>
  #include <linux/hwmon.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  /* Insmod parameters */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
  static int input_mode;
  module_param(input_mode, int, 0);
  MODULE_PARM_DESC(input_mode,
  	"Analog input mode:
  "
  	" 0 = four single ended inputs
  "
  	" 1 = three differential inputs
  "
  	" 2 = single ended and differential mixed
  "
  	" 3 = two differential inputs
  ");
  
  /* The PCF8591 control byte
fb4504fe8   Jean Delvare   Move the pcf8591 ...
46
        7    6    5    4    3    2    1    0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
49
50
     |  0 |AOEF|   AIP   |  0 |AINC|  AICH   | */
  
  /* Analog Output Enable Flag (analog output active if 1) */
  #define PCF8591_CONTROL_AOEF		0x40
fb4504fe8   Jean Delvare   Move the pcf8591 ...
51
52
  
  /* Analog Input Programming
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
54
55
56
57
58
59
60
61
62
     0x00 = four single ended inputs
     0x10 = three differential inputs
     0x20 = single ended and differential mixed
     0x30 = two differential inputs */
  #define PCF8591_CONTROL_AIP_MASK	0x30
  
  /* Autoincrement Flag (switch on if 1) */
  #define PCF8591_CONTROL_AINC		0x04
  
  /* Channel selection
fb4504fe8   Jean Delvare   Move the pcf8591 ...
63
     0x00 = channel 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
67
68
69
70
71
72
73
74
75
76
     0x01 = channel 1
     0x02 = channel 2
     0x03 = channel 3 */
  #define PCF8591_CONTROL_AICH_MASK	0x03
  
  /* Initial values */
  #define PCF8591_INIT_CONTROL	((input_mode << 4) | PCF8591_CONTROL_AOEF)
  #define PCF8591_INIT_AOUT	0	/* DAC out = 0 */
  
  /* Conversions */
  #define REG_TO_SIGNED(reg)	(((reg) & 0x80)?((reg) - 256):(reg))
  
  struct pcf8591_data {
4275fcd65   Jean Delvare   hwmon: (pcf8591) ...
77
  	struct device *hwmon_dev;
5c085d369   Ingo Molnar   [PATCH] i2c: Sema...
78
  	struct mutex update_lock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
81
82
  
  	u8 control;
  	u8 aout;
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
84
  static void pcf8591_init_client(struct i2c_client *client);
  static int pcf8591_read_channel(struct device *dev, int channel);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
  /* following are the sysfs callback functions */
  #define show_in_channel(channel)					\
a5099cfc2   Yani Ioannou   [PATCH] Driver Co...
87
  static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf)	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
92
93
94
95
96
97
98
  {									\
  	return sprintf(buf, "%d
  ", pcf8591_read_channel(dev, channel));\
  }									\
  static DEVICE_ATTR(in##channel##_input, S_IRUGO,			\
  		   show_in##channel##_input, NULL);
  
  show_in_channel(0);
  show_in_channel(1);
  show_in_channel(2);
  show_in_channel(3);
a5099cfc2   Yani Ioannou   [PATCH] Driver Co...
99
  static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
103
104
  {
  	struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  	return sprintf(buf, "%d
  ", data->aout * 10);
  }
a5099cfc2   Yani Ioannou   [PATCH] Driver Co...
105
  static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
107
108
109
110
111
112
113
114
115
116
  {
  	unsigned int value;
  	struct i2c_client *client = to_i2c_client(dev);
  	struct pcf8591_data *data = i2c_get_clientdata(client);
  	if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) {
  		data->aout = value;
  		i2c_smbus_write_byte_data(client, data->control, data->aout);
  		return count;
  	}
  	return -EINVAL;
  }
fb4504fe8   Jean Delvare   Move the pcf8591 ...
117
  static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
  		   show_out0_ouput, set_out0_output);
a5099cfc2   Yani Ioannou   [PATCH] Driver Co...
119
  static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
123
124
  {
  	struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  	return sprintf(buf, "%u
  ", !(!(data->control & PCF8591_CONTROL_AOEF)));
  }
a5099cfc2   Yani Ioannou   [PATCH] Driver Co...
125
  static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
129
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct pcf8591_data *data = i2c_get_clientdata(client);
  	unsigned long val = simple_strtoul(buf, NULL, 10);
5c085d369   Ingo Molnar   [PATCH] i2c: Sema...
130
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
132
133
134
135
  	if (val)
  		data->control |= PCF8591_CONTROL_AOEF;
  	else
  		data->control &= ~PCF8591_CONTROL_AOEF;
  	i2c_smbus_write_byte(client, data->control);
5c085d369   Ingo Molnar   [PATCH] i2c: Sema...
136
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
  	return count;
  }
fb4504fe8   Jean Delvare   Move the pcf8591 ...
139
  static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
  		   show_out0_enable, set_out0_enable);
7d9db67fe   Jean Delvare   i2c: __must_check...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  static struct attribute *pcf8591_attributes[] = {
  	&dev_attr_out0_enable.attr,
  	&dev_attr_out0_output.attr,
  	&dev_attr_in0_input.attr,
  	&dev_attr_in1_input.attr,
  	NULL
  };
  
  static const struct attribute_group pcf8591_attr_group = {
  	.attrs = pcf8591_attributes,
  };
  
  static struct attribute *pcf8591_attributes_opt[] = {
  	&dev_attr_in2_input.attr,
  	&dev_attr_in3_input.attr,
  	NULL
  };
  
  static const struct attribute_group pcf8591_attr_group_opt = {
  	.attrs = pcf8591_attributes_opt,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
163
164
  /*
   * Real code
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165

8b77e6ac4   Jean Delvare   i2c: Convert the ...
166
167
168
169
170
  static int pcf8591_probe(struct i2c_client *client,
  			 const struct i2c_device_id *id)
  {
  	struct pcf8591_data *data;
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171

5263ebb51   Deepak Saxena   [PATCH] i2c: kzal...
172
  	if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
175
  		err = -ENOMEM;
  		goto exit;
  	}
fb4504fe8   Jean Delvare   Move the pcf8591 ...
176

f741f6732   Jean Delvare   i2c: Clean up old...
177
  	i2c_set_clientdata(client, data);
5c085d369   Ingo Molnar   [PATCH] i2c: Sema...
178
  	mutex_init(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
  	/* Initialize the PCF8591 chip */
f741f6732   Jean Delvare   i2c: Clean up old...
181
  	pcf8591_init_client(client);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
  
  	/* Register sysfs hooks */
f741f6732   Jean Delvare   i2c: Clean up old...
184
  	err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
7d9db67fe   Jean Delvare   i2c: __must_check...
185
  	if (err)
8b77e6ac4   Jean Delvare   i2c: Convert the ...
186
  		goto exit_kfree;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
  
  	/* Register input2 if not in "two differential inputs" mode */
7d9db67fe   Jean Delvare   i2c: __must_check...
189
  	if (input_mode != 3) {
f741f6732   Jean Delvare   i2c: Clean up old...
190
  		if ((err = device_create_file(&client->dev,
7d9db67fe   Jean Delvare   i2c: __must_check...
191
192
193
  					      &dev_attr_in2_input)))
  			goto exit_sysfs_remove;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
  	/* Register input3 only in "four single ended inputs" mode */
7d9db67fe   Jean Delvare   i2c: __must_check...
195
  	if (input_mode == 0) {
f741f6732   Jean Delvare   i2c: Clean up old...
196
  		if ((err = device_create_file(&client->dev,
7d9db67fe   Jean Delvare   i2c: __must_check...
197
198
199
  					      &dev_attr_in3_input)))
  			goto exit_sysfs_remove;
  	}
4275fcd65   Jean Delvare   hwmon: (pcf8591) ...
200
201
202
203
204
  	data->hwmon_dev = hwmon_device_register(&client->dev);
  	if (IS_ERR(data->hwmon_dev)) {
  		err = PTR_ERR(data->hwmon_dev);
  		goto exit_sysfs_remove;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206

7d9db67fe   Jean Delvare   i2c: __must_check...
207
  exit_sysfs_remove:
f741f6732   Jean Delvare   i2c: Clean up old...
208
209
  	sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  	sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
212
213
214
  exit_kfree:
  	kfree(data);
  exit:
  	return err;
  }
8b77e6ac4   Jean Delvare   i2c: Convert the ...
215
  static int pcf8591_remove(struct i2c_client *client)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  {
4275fcd65   Jean Delvare   hwmon: (pcf8591) ...
217
218
219
  	struct pcf8591_data *data = i2c_get_clientdata(client);
  
  	hwmon_device_unregister(data->hwmon_dev);
7d9db67fe   Jean Delvare   i2c: __must_check...
220
221
  	sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  	sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222
223
224
225
226
227
228
229
230
231
232
233
  	kfree(i2c_get_clientdata(client));
  	return 0;
  }
  
  /* Called when we have found a new PCF8591. */
  static void pcf8591_init_client(struct i2c_client *client)
  {
  	struct pcf8591_data *data = i2c_get_clientdata(client);
  	data->control = PCF8591_INIT_CONTROL;
  	data->aout = PCF8591_INIT_AOUT;
  
  	i2c_smbus_write_byte_data(client, data->control, data->aout);
fb4504fe8   Jean Delvare   Move the pcf8591 ...
234
235
  
  	/* The first byte transmitted contains the conversion code of the
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
237
238
239
240
241
242
243
244
  	   previous read cycle. FLUSH IT! */
  	i2c_smbus_read_byte(client);
  }
  
  static int pcf8591_read_channel(struct device *dev, int channel)
  {
  	u8 value;
  	struct i2c_client *client = to_i2c_client(dev);
  	struct pcf8591_data *data = i2c_get_clientdata(client);
5c085d369   Ingo Molnar   [PATCH] i2c: Sema...
245
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
247
248
249
250
  
  	if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  		data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  			      | channel;
  		i2c_smbus_write_byte(client, data->control);
fb4504fe8   Jean Delvare   Move the pcf8591 ...
251
252
  
  		/* The first byte transmitted contains the conversion code of
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
  		   the previous read cycle. FLUSH IT! */
  		i2c_smbus_read_byte(client);
  	}
  	value = i2c_smbus_read_byte(client);
5c085d369   Ingo Molnar   [PATCH] i2c: Sema...
257
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
260
261
262
263
264
  
  	if ((channel == 2 && input_mode == 2) ||
  	    (channel != 3 && (input_mode == 1 || input_mode == 3)))
  		return (10 * REG_TO_SIGNED(value));
  	else
  		return (10 * value);
  }
8b77e6ac4   Jean Delvare   i2c: Convert the ...
265
266
267
268
269
270
271
272
273
274
275
276
277
  static const struct i2c_device_id pcf8591_id[] = {
  	{ "pcf8591", 0 },
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, pcf8591_id);
  
  static struct i2c_driver pcf8591_driver = {
  	.driver = {
  		.name	= "pcf8591",
  	},
  	.probe		= pcf8591_probe,
  	.remove		= pcf8591_remove,
  	.id_table	= pcf8591_id,
8b77e6ac4   Jean Delvare   i2c: Convert the ...
278
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
  static int __init pcf8591_init(void)
  {
  	if (input_mode < 0 || input_mode > 3) {
2caec1343   Joe Perches   hwmon: (pcf8591) ...
282
283
  		pr_warn("invalid input_mode (%d)
  ", input_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  		input_mode = 0;
  	}
  	return i2c_add_driver(&pcf8591_driver);
  }
  
  static void __exit pcf8591_exit(void)
  {
  	i2c_del_driver(&pcf8591_driver);
  }
  
  MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  MODULE_DESCRIPTION("PCF8591 driver");
  MODULE_LICENSE("GPL");
  
  module_init(pcf8591_init);
  module_exit(pcf8591_exit);