diff options
-rw-r--r-- | Documentation/devicetree/bindings/hwmon/ads1015.txt | 29 | ||||
-rw-r--r-- | Documentation/hwmon/ads1015 | 67 | ||||
-rw-r--r-- | MAINTAINERS | 8 | ||||
-rw-r--r-- | drivers/hwmon/Kconfig | 10 | ||||
-rw-r--r-- | drivers/hwmon/Makefile | 1 | ||||
-rw-r--r-- | drivers/hwmon/ads1015.c | 282 | ||||
-rw-r--r-- | include/linux/i2c/ads1015.h | 28 |
7 files changed, 425 insertions, 0 deletions
diff --git a/Documentation/devicetree/bindings/hwmon/ads1015.txt b/Documentation/devicetree/bindings/hwmon/ads1015.txt new file mode 100644 index 000000000000..0f30616384c5 --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/ads1015.txt | |||
@@ -0,0 +1,29 @@ | |||
1 | ADS1015 (I2C) | ||
2 | |||
3 | This device is a 12-bit A-D converter with 4 inputs. | ||
4 | |||
5 | The inputs can be used single ended or in certain differential combinations. | ||
6 | |||
7 | For configuration all possible combinations are mapped to 8 channels: | ||
8 | 0: Voltage over AIN0 and AIN1. | ||
9 | 1: Voltage over AIN0 and AIN3. | ||
10 | 2: Voltage over AIN1 and AIN3. | ||
11 | 3: Voltage over AIN2 and AIN3. | ||
12 | 4: Voltage over AIN0 and GND. | ||
13 | 5: Voltage over AIN1 and GND. | ||
14 | 6: Voltage over AIN2 and GND. | ||
15 | 7: Voltage over AIN3 and GND. | ||
16 | |||
17 | Optional properties: | ||
18 | |||
19 | - exported-channels : exported_channels is a bitmask that specifies which | ||
20 | channels should be accessable by the user. | ||
21 | |||
22 | Example: | ||
23 | ads1015@49 { | ||
24 | compatible = "ti,ads1015"; | ||
25 | reg = <0x49>; | ||
26 | exported-channels = <0x14>; | ||
27 | }; | ||
28 | |||
29 | In this example only channel 2 and 4 would be accessable by the user. | ||
diff --git a/Documentation/hwmon/ads1015 b/Documentation/hwmon/ads1015 new file mode 100644 index 000000000000..56ee7977b1a8 --- /dev/null +++ b/Documentation/hwmon/ads1015 | |||
@@ -0,0 +1,67 @@ | |||
1 | Kernel driver ads1015 | ||
2 | ===================== | ||
3 | |||
4 | Supported chips: | ||
5 | * Texas Instruments ADS1015 | ||
6 | Prefix: 'ads1015' | ||
7 | Datasheet: Publicly available at the Texas Instruments website : | ||
8 | http://focus.ti.com/lit/ds/symlink/ads1015.pdf | ||
9 | |||
10 | Authors: | ||
11 | Dirk Eibach, Guntermann & Drunck GmbH <eibach@gdsys.de> | ||
12 | |||
13 | Description | ||
14 | ----------- | ||
15 | |||
16 | This driver implements support for the Texas Instruments ADS1015. | ||
17 | |||
18 | This device is a 12-bit A-D converter with 4 inputs. | ||
19 | |||
20 | The inputs can be used single ended or in certain differential combinations. | ||
21 | |||
22 | The inputs can be exported to 8 sysfs input files in0_input - in7_input: | ||
23 | in0: Voltage over AIN0 and AIN1. | ||
24 | in1: Voltage over AIN0 and AIN3. | ||
25 | in2: Voltage over AIN1 and AIN3. | ||
26 | in3: Voltage over AIN2 and AIN3. | ||
27 | in4: Voltage over AIN0 and GND. | ||
28 | in5: Voltage over AIN1 and GND. | ||
29 | in6: Voltage over AIN2 and GND. | ||
30 | in7: Voltage over AIN3 and GND. | ||
31 | |||
32 | Which inputs are exported can be configured using platform data or devicetree. | ||
33 | |||
34 | By default all inputs are exported. | ||
35 | |||
36 | Platform Data | ||
37 | ------------- | ||
38 | |||
39 | In linux/i2c/ads1015.h platform data is defined as: | ||
40 | |||
41 | struct ads1015_platform_data { | ||
42 | unsigned int exported_channels; | ||
43 | }; | ||
44 | |||
45 | exported_channels is a bitmask that specifies which inputs should be exported. | ||
46 | |||
47 | Example: | ||
48 | struct ads1015_platform_data data = { | ||
49 | .exported_channels = (1 << 2) | (1 << 4) | ||
50 | }; | ||
51 | |||
52 | In this case only in2_input and in4_input would be created. | ||
53 | |||
54 | Devicetree | ||
55 | ---------- | ||
56 | |||
57 | The ads1015 node may have an "exported-channels" property. | ||
58 | exported_channels is a bitmask that specifies which inputs should be exported. | ||
59 | |||
60 | Example: | ||
61 | ads1015@49 { | ||
62 | compatible = "ti,ads1015"; | ||
63 | reg = <0x49>; | ||
64 | exported-channels = < 0x14 >; | ||
65 | }; | ||
66 | |||
67 | In this case only in2_input and in4_input would be created. | ||
diff --git a/MAINTAINERS b/MAINTAINERS index b8a123f7e810..38077a656820 100644 --- a/MAINTAINERS +++ b/MAINTAINERS | |||
@@ -365,6 +365,14 @@ W: http://wiki-analog.com/ADP8860 | |||
365 | S: Supported | 365 | S: Supported |
366 | F: drivers/video/backlight/adp8860_bl.c | 366 | F: drivers/video/backlight/adp8860_bl.c |
367 | 367 | ||
368 | ADS1015 HARDWARE MONITOR DRIVER | ||
369 | M: Dirk Eibach <eibach@gdsys.de> | ||
370 | L: lm-sensors@lm-sensors.org | ||
371 | S: Maintained | ||
372 | F: Documentation/hwmon/ads1015 | ||
373 | F: drivers/hwmon/ads1015.c | ||
374 | F: include/linux/i2c/ads1015.h | ||
375 | |||
368 | ADT746X FAN DRIVER | 376 | ADT746X FAN DRIVER |
369 | M: Colin Leroy <colin@colino.net> | 377 | M: Colin Leroy <colin@colino.net> |
370 | S: Maintained | 378 | S: Maintained |
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 6fad9f082f67..e4bd13b3cd8b 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig | |||
@@ -968,6 +968,16 @@ config SENSORS_SCH5627 | |||
968 | This driver can also be built as a module. If so, the module | 968 | This driver can also be built as a module. If so, the module |
969 | will be called sch5627. | 969 | will be called sch5627. |
970 | 970 | ||
971 | config SENSORS_ADS1015 | ||
972 | tristate "Texas Instruments ADS1015" | ||
973 | depends on I2C | ||
974 | help | ||
975 | If you say yes here you get support for Texas Instruments ADS1015 | ||
976 | 12-bit 4-input ADC device. | ||
977 | |||
978 | This driver can also be built as a module. If so, the module | ||
979 | will be called ads1015. | ||
980 | |||
971 | config SENSORS_ADS7828 | 981 | config SENSORS_ADS7828 |
972 | tristate "Texas Instruments ADS7828" | 982 | tristate "Texas Instruments ADS7828" |
973 | depends on I2C | 983 | depends on I2C |
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index 4d0122c52219..54ca5939d028 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile | |||
@@ -29,6 +29,7 @@ obj-$(CONFIG_SENSORS_ADM1026) += adm1026.o | |||
29 | obj-$(CONFIG_SENSORS_ADM1029) += adm1029.o | 29 | obj-$(CONFIG_SENSORS_ADM1029) += adm1029.o |
30 | obj-$(CONFIG_SENSORS_ADM1031) += adm1031.o | 30 | obj-$(CONFIG_SENSORS_ADM1031) += adm1031.o |
31 | obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o | 31 | obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o |
32 | obj-$(CONFIG_SENSORS_ADS1015) += ads1015.o | ||
32 | obj-$(CONFIG_SENSORS_ADS7828) += ads7828.o | 33 | obj-$(CONFIG_SENSORS_ADS7828) += ads7828.o |
33 | obj-$(CONFIG_SENSORS_ADS7871) += ads7871.o | 34 | obj-$(CONFIG_SENSORS_ADS7871) += ads7871.o |
34 | obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o | 35 | obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o |
diff --git a/drivers/hwmon/ads1015.c b/drivers/hwmon/ads1015.c new file mode 100644 index 000000000000..9e1585c3152e --- /dev/null +++ b/drivers/hwmon/ads1015.c | |||
@@ -0,0 +1,282 @@ | |||
1 | /* | ||
2 | * ads1015.c - lm_sensors driver for ads1015 12-bit 4-input ADC | ||
3 | * (C) Copyright 2010 | ||
4 | * Dirk Eibach, Guntermann & Drunck GmbH <eibach@gdsys.de> | ||
5 | * | ||
6 | * Based on the ads7828 driver by Steve Hardy. | ||
7 | * | ||
8 | * Datasheet available at: http://focus.ti.com/lit/ds/symlink/ads1015.pdf | ||
9 | * | ||
10 | * This program is free software; you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License as published by | ||
12 | * the Free Software Foundation; either version 2 of the License, or | ||
13 | * (at your option) any later version. | ||
14 | * | ||
15 | * This program is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | * GNU General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU General Public License | ||
21 | * along with this program; if not, write to the Free Software | ||
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
23 | */ | ||
24 | |||
25 | #include <linux/module.h> | ||
26 | #include <linux/init.h> | ||
27 | #include <linux/slab.h> | ||
28 | #include <linux/delay.h> | ||
29 | #include <linux/i2c.h> | ||
30 | #include <linux/hwmon.h> | ||
31 | #include <linux/hwmon-sysfs.h> | ||
32 | #include <linux/err.h> | ||
33 | #include <linux/mutex.h> | ||
34 | #include <linux/of.h> | ||
35 | |||
36 | #include <linux/i2c/ads1015.h> | ||
37 | |||
38 | /* ADS1015 registers */ | ||
39 | enum { | ||
40 | ADS1015_CONVERSION = 0, | ||
41 | ADS1015_CONFIG = 1, | ||
42 | }; | ||
43 | |||
44 | /* PGA fullscale voltages in mV */ | ||
45 | static const unsigned int fullscale_table[8] = { | ||
46 | 6144, 4096, 2048, 1024, 512, 256, 256, 256 }; | ||
47 | |||
48 | #define ADS1015_CONFIG_CHANNELS 8 | ||
49 | #define ADS1015_DEFAULT_CHANNELS 0xff | ||
50 | |||
51 | struct ads1015_data { | ||
52 | struct device *hwmon_dev; | ||
53 | struct mutex update_lock; /* mutex protect updates */ | ||
54 | struct attribute *attr_table[ADS1015_CONFIG_CHANNELS + 1]; | ||
55 | struct attribute_group attr_group; | ||
56 | }; | ||
57 | |||
58 | static s32 ads1015_read_reg(struct i2c_client *client, unsigned int reg) | ||
59 | { | ||
60 | s32 data = i2c_smbus_read_word_data(client, reg); | ||
61 | |||
62 | return (data < 0) ? data : swab16(data); | ||
63 | } | ||
64 | |||
65 | static s32 ads1015_write_reg(struct i2c_client *client, unsigned int reg, | ||
66 | u16 val) | ||
67 | { | ||
68 | return i2c_smbus_write_word_data(client, reg, swab16(val)); | ||
69 | } | ||
70 | |||
71 | static int ads1015_read_value(struct i2c_client *client, unsigned int channel, | ||
72 | int *value) | ||
73 | { | ||
74 | u16 config; | ||
75 | s16 conversion; | ||
76 | unsigned int pga; | ||
77 | int fullscale; | ||
78 | unsigned int k; | ||
79 | struct ads1015_data *data = i2c_get_clientdata(client); | ||
80 | int res; | ||
81 | |||
82 | mutex_lock(&data->update_lock); | ||
83 | |||
84 | /* get fullscale voltage */ | ||
85 | res = ads1015_read_reg(client, ADS1015_CONFIG); | ||
86 | if (res < 0) | ||
87 | goto err_unlock; | ||
88 | config = res; | ||
89 | pga = (config >> 9) & 0x0007; | ||
90 | fullscale = fullscale_table[pga]; | ||
91 | |||
92 | /* set channel and start single conversion */ | ||
93 | config &= ~(0x0007 << 12); | ||
94 | config |= (1 << 15) | (1 << 8) | (channel & 0x0007) << 12; | ||
95 | |||
96 | /* wait until conversion finished */ | ||
97 | res = ads1015_write_reg(client, ADS1015_CONFIG, config); | ||
98 | if (res < 0) | ||
99 | goto err_unlock; | ||
100 | for (k = 0; k < 5; ++k) { | ||
101 | msleep(1); | ||
102 | res = ads1015_read_reg(client, ADS1015_CONFIG); | ||
103 | if (res < 0) | ||
104 | goto err_unlock; | ||
105 | config = res; | ||
106 | if (config & (1 << 15)) | ||
107 | break; | ||
108 | } | ||
109 | if (k == 5) { | ||
110 | res = -EIO; | ||
111 | goto err_unlock; | ||
112 | } | ||
113 | |||
114 | res = ads1015_read_reg(client, ADS1015_CONVERSION); | ||
115 | if (res < 0) | ||
116 | goto err_unlock; | ||
117 | conversion = res; | ||
118 | |||
119 | mutex_unlock(&data->update_lock); | ||
120 | |||
121 | *value = DIV_ROUND_CLOSEST(conversion * fullscale, 0x7ff0); | ||
122 | |||
123 | return 0; | ||
124 | |||
125 | err_unlock: | ||
126 | mutex_unlock(&data->update_lock); | ||
127 | return res; | ||
128 | } | ||
129 | |||
130 | /* sysfs callback function */ | ||
131 | static ssize_t show_in(struct device *dev, struct device_attribute *da, | ||
132 | char *buf) | ||
133 | { | ||
134 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | ||
135 | struct i2c_client *client = to_i2c_client(dev); | ||
136 | int in; | ||
137 | int res; | ||
138 | |||
139 | res = ads1015_read_value(client, attr->index, &in); | ||
140 | |||
141 | return (res < 0) ? res : sprintf(buf, "%d\n", in); | ||
142 | } | ||
143 | |||
144 | #define in_reg(offset)\ | ||
145 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in,\ | ||
146 | NULL, offset) | ||
147 | |||
148 | in_reg(0); | ||
149 | in_reg(1); | ||
150 | in_reg(2); | ||
151 | in_reg(3); | ||
152 | in_reg(4); | ||
153 | in_reg(5); | ||
154 | in_reg(6); | ||
155 | in_reg(7); | ||
156 | |||
157 | static struct attribute *all_attributes[] = { | ||
158 | &sensor_dev_attr_in0_input.dev_attr.attr, | ||
159 | &sensor_dev_attr_in1_input.dev_attr.attr, | ||
160 | &sensor_dev_attr_in2_input.dev_attr.attr, | ||
161 | &sensor_dev_attr_in3_input.dev_attr.attr, | ||
162 | &sensor_dev_attr_in4_input.dev_attr.attr, | ||
163 | &sensor_dev_attr_in5_input.dev_attr.attr, | ||
164 | &sensor_dev_attr_in6_input.dev_attr.attr, | ||
165 | &sensor_dev_attr_in7_input.dev_attr.attr, | ||
166 | }; | ||
167 | |||
168 | /* | ||
169 | * Driver interface | ||
170 | */ | ||
171 | |||
172 | static int ads1015_remove(struct i2c_client *client) | ||
173 | { | ||
174 | struct ads1015_data *data = i2c_get_clientdata(client); | ||
175 | hwmon_device_unregister(data->hwmon_dev); | ||
176 | sysfs_remove_group(&client->dev.kobj, &data->attr_group); | ||
177 | kfree(data); | ||
178 | return 0; | ||
179 | } | ||
180 | |||
181 | static unsigned int ads1015_get_exported_channels(struct i2c_client *client) | ||
182 | { | ||
183 | struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev); | ||
184 | #ifdef CONFIG_OF | ||
185 | struct device_node *np = client->dev.of_node; | ||
186 | const __be32 *of_channels; | ||
187 | int of_channels_size; | ||
188 | #endif | ||
189 | |||
190 | /* prefer platform data */ | ||
191 | if (pdata) | ||
192 | return pdata->exported_channels; | ||
193 | |||
194 | #ifdef CONFIG_OF | ||
195 | /* fallback on OF */ | ||
196 | of_channels = of_get_property(np, "exported-channels", | ||
197 | &of_channels_size); | ||
198 | if (of_channels && (of_channels_size == sizeof(*of_channels))) | ||
199 | return be32_to_cpup(of_channels); | ||
200 | #endif | ||
201 | |||
202 | /* fallback on default configuration */ | ||
203 | return ADS1015_DEFAULT_CHANNELS; | ||
204 | } | ||
205 | |||
206 | static int ads1015_probe(struct i2c_client *client, | ||
207 | const struct i2c_device_id *id) | ||
208 | { | ||
209 | struct ads1015_data *data; | ||
210 | int err; | ||
211 | unsigned int exported_channels; | ||
212 | unsigned int k; | ||
213 | unsigned int n = 0; | ||
214 | |||
215 | data = kzalloc(sizeof(struct ads1015_data), GFP_KERNEL); | ||
216 | if (!data) { | ||
217 | err = -ENOMEM; | ||
218 | goto exit; | ||
219 | } | ||
220 | |||
221 | i2c_set_clientdata(client, data); | ||
222 | mutex_init(&data->update_lock); | ||
223 | |||
224 | /* build sysfs attribute group */ | ||
225 | data->attr_group.attrs = data->attr_table; | ||
226 | exported_channels = ads1015_get_exported_channels(client); | ||
227 | for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k) { | ||
228 | if (!(exported_channels & (1<<k))) | ||
229 | continue; | ||
230 | data->attr_table[n++] = all_attributes[k]; | ||
231 | } | ||
232 | err = sysfs_create_group(&client->dev.kobj, &data->attr_group); | ||
233 | if (err) | ||
234 | goto exit_free; | ||
235 | |||
236 | data->hwmon_dev = hwmon_device_register(&client->dev); | ||
237 | if (IS_ERR(data->hwmon_dev)) { | ||
238 | err = PTR_ERR(data->hwmon_dev); | ||
239 | goto exit_remove; | ||
240 | } | ||
241 | |||
242 | return 0; | ||
243 | |||
244 | exit_remove: | ||
245 | sysfs_remove_group(&client->dev.kobj, &data->attr_group); | ||
246 | exit_free: | ||
247 | kfree(data); | ||
248 | exit: | ||
249 | return err; | ||
250 | } | ||
251 | |||
252 | static const struct i2c_device_id ads1015_id[] = { | ||
253 | { "ads1015", 0 }, | ||
254 | { } | ||
255 | }; | ||
256 | MODULE_DEVICE_TABLE(i2c, ads1015_id); | ||
257 | |||
258 | static struct i2c_driver ads1015_driver = { | ||
259 | .driver = { | ||
260 | .name = "ads1015", | ||
261 | }, | ||
262 | .probe = ads1015_probe, | ||
263 | .remove = ads1015_remove, | ||
264 | .id_table = ads1015_id, | ||
265 | }; | ||
266 | |||
267 | static int __init sensors_ads1015_init(void) | ||
268 | { | ||
269 | return i2c_add_driver(&ads1015_driver); | ||
270 | } | ||
271 | |||
272 | static void __exit sensors_ads1015_exit(void) | ||
273 | { | ||
274 | i2c_del_driver(&ads1015_driver); | ||
275 | } | ||
276 | |||
277 | MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>"); | ||
278 | MODULE_DESCRIPTION("ADS1015 driver"); | ||
279 | MODULE_LICENSE("GPL"); | ||
280 | |||
281 | module_init(sensors_ads1015_init); | ||
282 | module_exit(sensors_ads1015_exit); | ||
diff --git a/include/linux/i2c/ads1015.h b/include/linux/i2c/ads1015.h new file mode 100644 index 000000000000..8541c6acfafd --- /dev/null +++ b/include/linux/i2c/ads1015.h | |||
@@ -0,0 +1,28 @@ | |||
1 | /* | ||
2 | * Platform Data for ADS1015 12-bit 4-input ADC | ||
3 | * (C) Copyright 2010 | ||
4 | * Dirk Eibach, Guntermann & Drunck GmbH <eibach@gdsys.de> | ||
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 | #ifndef LINUX_ADS1015_H | ||
22 | #define LINUX_ADS1015_H | ||
23 | |||
24 | struct ads1015_platform_data { | ||
25 | unsigned int exported_channels; | ||
26 | }; | ||
27 | |||
28 | #endif /* LINUX_ADS1015_H */ | ||