• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/hwmon/
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>
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>
25#include <linux/mutex.h>
26
27/* Addresses to scan */
28static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
29					0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
30
31/* Insmod parameters */
32
33static int input_mode;
34module_param(input_mode, int, 0);
35MODULE_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");
41
42/* The PCF8591 control byte
43      7    6    5    4    3    2    1    0
44   |  0 |AOEF|   AIP   |  0 |AINC|  AICH   | */
45
46/* Analog Output Enable Flag (analog output active if 1) */
47#define PCF8591_CONTROL_AOEF		0x40
48
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
55
56/* Autoincrement Flag (switch on if 1) */
57#define PCF8591_CONTROL_AINC		0x04
58
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
65
66/* Initial values */
67#define PCF8591_INIT_CONTROL	((input_mode << 4) | PCF8591_CONTROL_AOEF)
68#define PCF8591_INIT_AOUT	0	/* DAC out = 0 */
69
70/* Conversions */
71#define REG_TO_SIGNED(reg)	(((reg) & 0x80)?((reg) - 256):(reg))
72
73struct pcf8591_data {
74	struct mutex update_lock;
75
76	u8 control;
77	u8 aout;
78};
79
80static void pcf8591_init_client(struct i2c_client *client);
81static int pcf8591_read_channel(struct device *dev, int channel);
82
83/* following are the sysfs callback functions */
84#define show_in_channel(channel)					\
85static 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}									\
89static DEVICE_ATTR(in##channel##_input, S_IRUGO,			\
90		   show_in##channel##_input, NULL);
91
92show_in_channel(0);
93show_in_channel(1);
94show_in_channel(2);
95show_in_channel(3);
96
97static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
98{
99	struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
100	return sprintf(buf, "%d\n", data->aout * 10);
101}
102
103static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
104{
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;
112	}
113	return -EINVAL;
114}
115
116static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
117		   show_out0_ouput, set_out0_output);
118
119static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
120{
121	struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
122	return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
123}
124
125static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
126{
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);
130
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;
139}
140
141static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
142		   show_out0_enable, set_out0_enable);
143
144static 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
150};
151
152static const struct attribute_group pcf8591_attr_group = {
153	.attrs = pcf8591_attributes,
154};
155
156static struct attribute *pcf8591_attributes_opt[] = {
157	&dev_attr_in2_input.attr,
158	&dev_attr_in3_input.attr,
159	NULL
160};
161
162static const struct attribute_group pcf8591_attr_group_opt = {
163	.attrs = pcf8591_attributes_opt,
164};
165
166/*
167 * Real code
168 */
169
170/* Return 0 if detection is successful, -ENODEV otherwise */
171static int pcf8591_detect(struct i2c_client *client,
172			  struct i2c_board_info *info)
173{
174	struct i2c_adapter *adapter = client->adapter;
175
176	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
177				     | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
178		return -ENODEV;
179
180	/* Now, we would do the remaining detection. But the PCF8591 is plainly
181	   impossible to detect! Stupid chip. */
182
183	strlcpy(info->type, "pcf8591", I2C_NAME_SIZE);
184
185	return 0;
186}
187
188static int pcf8591_probe(struct i2c_client *client,
189			 const struct i2c_device_id *id)
190{
191	struct pcf8591_data *data;
192	int err;
193
194	if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
195		err = -ENOMEM;
196		goto exit;
197	}
198
199	i2c_set_clientdata(client, data);
200	mutex_init(&data->update_lock);
201
202	/* Initialize the PCF8591 chip */
203	pcf8591_init_client(client);
204
205	/* Register sysfs hooks */
206	err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
207	if (err)
208		goto exit_kfree;
209
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;
215	}
216
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;
222	}
223
224	return 0;
225
226exit_sysfs_remove:
227	sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
228	sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
229exit_kfree:
230	kfree(data);
231exit:
232	return err;
233}
234
235static int pcf8591_remove(struct i2c_client *client)
236{
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;
241}
242
243/* Called when we have found a new PCF8591. */
244static void pcf8591_init_client(struct i2c_client *client)
245{
246	struct pcf8591_data *data = i2c_get_clientdata(client);
247	data->control = PCF8591_INIT_CONTROL;
248	data->aout = PCF8591_INIT_AOUT;
249
250	i2c_smbus_write_byte_data(client, data->control, data->aout);
251
252	/* The first byte transmitted contains the conversion code of the
253	   previous read cycle. FLUSH IT! */
254	i2c_smbus_read_byte(client);
255}
256
257static int pcf8591_read_channel(struct device *dev, int channel)
258{
259	u8 value;
260	struct i2c_client *client = to_i2c_client(dev);
261	struct pcf8591_data *data = i2c_get_clientdata(client);
262
263	mutex_lock(&data->update_lock);
264
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);
269
270		/* The first byte transmitted contains the conversion code of
271		   the previous read cycle. FLUSH IT! */
272		i2c_smbus_read_byte(client);
273	}
274	value = i2c_smbus_read_byte(client);
275
276	mutex_unlock(&data->update_lock);
277
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);
283}
284
285static const struct i2c_device_id pcf8591_id[] = {
286	{ "pcf8591", 0 },
287	{ }
288};
289MODULE_DEVICE_TABLE(i2c, pcf8591_id);
290
291static struct i2c_driver pcf8591_driver = {
292	.driver = {
293		.name	= "pcf8591",
294	},
295	.probe		= pcf8591_probe,
296	.remove		= pcf8591_remove,
297	.id_table	= pcf8591_id,
298
299	.class		= I2C_CLASS_HWMON,	/* Nearest choice */
300	.detect		= pcf8591_detect,
301	.address_list	= normal_i2c,
302};
303
304static int __init pcf8591_init(void)
305{
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;
310	}
311	return i2c_add_driver(&pcf8591_driver);
312}
313
314static void __exit pcf8591_exit(void)
315{
316	i2c_del_driver(&pcf8591_driver);
317}
318
319MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
320MODULE_DESCRIPTION("PCF8591 driver");
321MODULE_LICENSE("GPL");
322
323module_init(pcf8591_init);
324module_exit(pcf8591_exit);
325