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>
36 #include <linux/i2c/ads1015.h>
38 /* ADS1015 registers */
40 ADS1015_CONVERSION = 0,
44 /* PGA fullscale voltages in mV */
45 static const unsigned int fullscale_table[8] = {
46 6144, 4096, 2048, 1024, 512, 256, 256, 256 };
48 #define ADS1015_CONFIG_CHANNELS 8
49 #define ADS1015_DEFAULT_CHANNELS 0xff
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;
58 static s32 ads1015_read_reg(struct i2c_client *client, unsigned int reg)
60 s32 data = i2c_smbus_read_word_data(client, reg);
62 return (data < 0) ? data : swab16(data);
65 static s32 ads1015_write_reg(struct i2c_client *client, unsigned int reg,
68 return i2c_smbus_write_word_data(client, reg, swab16(val));
71 static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
79 struct ads1015_data *data = i2c_get_clientdata(client);
82 mutex_lock(&data->update_lock);
84 /* get fullscale voltage */
85 res = ads1015_read_reg(client, ADS1015_CONFIG);
89 pga = (config >> 9) & 0x0007;
90 fullscale = fullscale_table[pga];
92 /* set channel and start single conversion */
93 config &= ~(0x0007 << 12);
94 config |= (1 << 15) | (1 << 8) | (channel & 0x0007) << 12;
96 /* wait until conversion finished */
97 res = ads1015_write_reg(client, ADS1015_CONFIG, config);
100 for (k = 0; k < 5; ++k) {
102 res = ads1015_read_reg(client, ADS1015_CONFIG);
106 if (config & (1 << 15))
114 res = ads1015_read_reg(client, ADS1015_CONVERSION);
119 mutex_unlock(&data->update_lock);
121 *value = DIV_ROUND_CLOSEST(conversion * fullscale, 0x7ff0);
126 mutex_unlock(&data->update_lock);
130 /* sysfs callback function */
131 static ssize_t show_in(struct device *dev, struct device_attribute *da,
134 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
135 struct i2c_client *client = to_i2c_client(dev);
139 res = ads1015_read_value(client, attr->index, &in);
141 return (res < 0) ? res : sprintf(buf, "%d\n", in);
144 #define in_reg(offset)\
145 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in,\
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,
172 static int ads1015_remove(struct i2c_client *client)
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);
181 static unsigned int ads1015_get_exported_channels(struct i2c_client *client)
183 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
185 struct device_node *np = client->dev.of_node;
186 const __be32 *of_channels;
187 int of_channels_size;
190 /* prefer platform data */
192 return pdata->exported_channels;
196 of_channels = of_get_property(np, "exported-channels",
198 if (of_channels && (of_channels_size == sizeof(*of_channels)))
199 return be32_to_cpup(of_channels);
202 /* fallback on default configuration */
203 return ADS1015_DEFAULT_CHANNELS;
206 static int ads1015_probe(struct i2c_client *client,
207 const struct i2c_device_id *id)
209 struct ads1015_data *data;
211 unsigned int exported_channels;
215 data = kzalloc(sizeof(struct ads1015_data), GFP_KERNEL);
221 i2c_set_clientdata(client, data);
222 mutex_init(&data->update_lock);
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)))
230 data->attr_table[n++] = all_attributes[k];
232 err = sysfs_create_group(&client->dev.kobj, &data->attr_group);
236 data->hwmon_dev = hwmon_device_register(&client->dev);
237 if (IS_ERR(data->hwmon_dev)) {
238 err = PTR_ERR(data->hwmon_dev);
245 sysfs_remove_group(&client->dev.kobj, &data->attr_group);
252 static const struct i2c_device_id ads1015_id[] = {
256 MODULE_DEVICE_TABLE(i2c, ads1015_id);
258 static struct i2c_driver ads1015_driver = {
262 .probe = ads1015_probe,
263 .remove = ads1015_remove,
264 .id_table = ads1015_id,
267 static int __init sensors_ads1015_init(void)
269 return i2c_add_driver(&ads1015_driver);
272 static void __exit sensors_ads1015_exit(void)
274 i2c_del_driver(&ads1015_driver);
277 MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
278 MODULE_DESCRIPTION("ADS1015 driver");
279 MODULE_LICENSE("GPL");
281 module_init(sensors_ads1015_init);
282 module_exit(sensors_ads1015_exit);