2 * ads1015.c - lm_sensors driver for ads1015 12-bit 4-input ADC
4 * Dirk Eibach, Guntermann & Drunck GmbH <eibach@gdsys.de>
6 * Based on the ads7828 driver by Steve Hardy.
8 * Datasheet available at: http://focus.ti.com/lit/ds/symlink/ads1015.pdf
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.
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.
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.
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_device.h>
37 #include <linux/platform_data/ads1015.h>
39 /* ADS1015 registers */
41 ADS1015_CONVERSION = 0,
45 /* PGA fullscale voltages in mV */
46 static const unsigned int fullscale_table[8] = {
47 6144, 4096, 2048, 1024, 512, 256, 256, 256 };
49 /* Data rates in samples per second */
50 static const unsigned int data_rate_table_1015[8] = {
51 128, 250, 490, 920, 1600, 2400, 3300, 3300
54 static const unsigned int data_rate_table_1115[8] = {
55 8, 16, 32, 64, 128, 250, 475, 860
58 #define ADS1015_DEFAULT_CHANNELS 0xff
59 #define ADS1015_DEFAULT_PGA 2
60 #define ADS1015_DEFAULT_DATA_RATE 4
68 struct device *hwmon_dev;
69 struct mutex update_lock; /* mutex protect updates */
70 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
71 enum ads1015_chips id;
74 static int ads1015_read_adc(struct i2c_client *client, unsigned int channel)
77 struct ads1015_data *data = i2c_get_clientdata(client);
78 unsigned int pga = data->channel_data[channel].pga;
79 unsigned int data_rate = data->channel_data[channel].data_rate;
80 unsigned int conversion_time_ms;
81 const unsigned int * const rate_table = data->id == ads1115 ?
82 data_rate_table_1115 : data_rate_table_1015;
85 mutex_lock(&data->update_lock);
87 /* get channel parameters */
88 res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
92 conversion_time_ms = DIV_ROUND_UP(1000, rate_table[data_rate]);
94 /* setup and start single conversion */
96 config |= (1 << 15) | (1 << 8);
97 config |= (channel & 0x0007) << 12;
98 config |= (pga & 0x0007) << 9;
99 config |= (data_rate & 0x0007) << 5;
101 res = i2c_smbus_write_word_swapped(client, ADS1015_CONFIG, config);
105 /* wait until conversion finished */
106 msleep(conversion_time_ms);
107 res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
111 if (!(config & (1 << 15))) {
112 /* conversion not finished in time */
117 res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION);
120 mutex_unlock(&data->update_lock);
124 static int ads1015_reg_to_mv(struct i2c_client *client, unsigned int channel,
127 struct ads1015_data *data = i2c_get_clientdata(client);
128 unsigned int pga = data->channel_data[channel].pga;
129 int fullscale = fullscale_table[pga];
130 const int mask = data->id == ads1115 ? 0x7fff : 0x7ff0;
132 return DIV_ROUND_CLOSEST(reg * fullscale, mask);
135 /* sysfs callback function */
136 static ssize_t show_in(struct device *dev, struct device_attribute *da,
139 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
140 struct i2c_client *client = to_i2c_client(dev);
142 int index = attr->index;
144 res = ads1015_read_adc(client, index);
148 return sprintf(buf, "%d\n", ads1015_reg_to_mv(client, index, res));
151 static const struct sensor_device_attribute ads1015_in[] = {
152 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
153 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
154 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
155 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
156 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
157 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
158 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
159 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
166 static int ads1015_remove(struct i2c_client *client)
168 struct ads1015_data *data = i2c_get_clientdata(client);
171 hwmon_device_unregister(data->hwmon_dev);
172 for (k = 0; k < ADS1015_CHANNELS; ++k)
173 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
178 static int ads1015_get_channels_config_of(struct i2c_client *client)
180 struct ads1015_data *data = i2c_get_clientdata(client);
181 struct device_node *node;
183 if (!client->dev.of_node
184 || !of_get_next_child(client->dev.of_node, NULL))
187 for_each_child_of_node(client->dev.of_node, node) {
189 unsigned int channel;
190 unsigned int pga = ADS1015_DEFAULT_PGA;
191 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
193 if (of_property_read_u32(node, "reg", &pval)) {
194 dev_err(&client->dev, "invalid reg on %pOF\n", node);
199 if (channel >= ADS1015_CHANNELS) {
200 dev_err(&client->dev,
201 "invalid channel index %d on %pOF\n",
206 if (!of_property_read_u32(node, "ti,gain", &pval)) {
209 dev_err(&client->dev, "invalid gain on %pOF\n",
215 if (!of_property_read_u32(node, "ti,datarate", &pval)) {
218 dev_err(&client->dev,
219 "invalid data_rate on %pOF\n", node);
224 data->channel_data[channel].enabled = true;
225 data->channel_data[channel].pga = pga;
226 data->channel_data[channel].data_rate = data_rate;
233 static void ads1015_get_channels_config(struct i2c_client *client)
236 struct ads1015_data *data = i2c_get_clientdata(client);
237 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
239 /* prefer platform data */
241 memcpy(data->channel_data, pdata->channel_data,
242 sizeof(data->channel_data));
247 if (!ads1015_get_channels_config_of(client))
251 /* fallback on default configuration */
252 for (k = 0; k < ADS1015_CHANNELS; ++k) {
253 data->channel_data[k].enabled = true;
254 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
255 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
259 static int ads1015_probe(struct i2c_client *client,
260 const struct i2c_device_id *id)
262 struct ads1015_data *data;
266 data = devm_kzalloc(&client->dev, sizeof(struct ads1015_data),
271 if (client->dev.of_node)
272 data->id = (enum ads1015_chips)
273 of_device_get_match_data(&client->dev);
275 data->id = id->driver_data;
276 i2c_set_clientdata(client, data);
277 mutex_init(&data->update_lock);
279 /* build sysfs attribute group */
280 ads1015_get_channels_config(client);
281 for (k = 0; k < ADS1015_CHANNELS; ++k) {
282 if (!data->channel_data[k].enabled)
284 err = device_create_file(&client->dev, &ads1015_in[k].dev_attr);
289 data->hwmon_dev = hwmon_device_register(&client->dev);
290 if (IS_ERR(data->hwmon_dev)) {
291 err = PTR_ERR(data->hwmon_dev);
298 for (k = 0; k < ADS1015_CHANNELS; ++k)
299 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
303 static const struct i2c_device_id ads1015_id[] = {
304 { "ads1015", ads1015},
305 { "ads1115", ads1115},
308 MODULE_DEVICE_TABLE(i2c, ads1015_id);
310 static const struct of_device_id ads1015_of_match[] = {
312 .compatible = "ti,ads1015",
313 .data = (void *)ads1015
316 .compatible = "ti,ads1115",
317 .data = (void *)ads1115
321 MODULE_DEVICE_TABLE(of, ads1015_of_match);
323 static struct i2c_driver ads1015_driver = {
326 .of_match_table = of_match_ptr(ads1015_of_match),
328 .probe = ads1015_probe,
329 .remove = ads1015_remove,
330 .id_table = ads1015_id,
333 module_i2c_driver(ads1015_driver);
335 MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
336 MODULE_DESCRIPTION("ADS1015 driver");
337 MODULE_LICENSE("GPL");