Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 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 | |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 48 | /* Data rates in samples per second */ |
| 49 | static const unsigned int data_rate_table[8] = { |
| 50 | 128, 250, 490, 920, 1600, 2400, 3300, 3300 }; |
| 51 | |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 52 | #define ADS1015_DEFAULT_CHANNELS 0xff |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 53 | #define ADS1015_DEFAULT_PGA 2 |
| 54 | #define ADS1015_DEFAULT_DATA_RATE 4 |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 55 | |
| 56 | struct ads1015_data { |
| 57 | struct device *hwmon_dev; |
| 58 | struct mutex update_lock; /* mutex protect updates */ |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 59 | struct ads1015_channel_data channel_data[ADS1015_CHANNELS]; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
Guenter Roeck | 1196573 | 2012-04-09 13:53:00 -0400 | [diff] [blame] | 62 | static int ads1015_read_adc(struct i2c_client *client, unsigned int channel) |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 63 | { |
| 64 | u16 config; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 65 | struct ads1015_data *data = i2c_get_clientdata(client); |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 66 | unsigned int pga = data->channel_data[channel].pga; |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 67 | unsigned int data_rate = data->channel_data[channel].data_rate; |
| 68 | unsigned int conversion_time_ms; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 69 | int res; |
| 70 | |
| 71 | mutex_lock(&data->update_lock); |
| 72 | |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 73 | /* get channel parameters */ |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 74 | res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 75 | if (res < 0) |
| 76 | goto err_unlock; |
| 77 | config = res; |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 78 | conversion_time_ms = DIV_ROUND_UP(1000, data_rate_table[data_rate]); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 79 | |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 80 | /* setup and start single conversion */ |
| 81 | config &= 0x001f; |
| 82 | config |= (1 << 15) | (1 << 8); |
| 83 | config |= (channel & 0x0007) << 12; |
| 84 | config |= (pga & 0x0007) << 9; |
| 85 | config |= (data_rate & 0x0007) << 5; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 86 | |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 87 | res = i2c_smbus_write_word_swapped(client, ADS1015_CONFIG, config); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 88 | if (res < 0) |
| 89 | goto err_unlock; |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 90 | |
| 91 | /* wait until conversion finished */ |
| 92 | msleep(conversion_time_ms); |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 93 | res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG); |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 94 | if (res < 0) |
| 95 | goto err_unlock; |
| 96 | config = res; |
| 97 | if (!(config & (1 << 15))) { |
| 98 | /* conversion not finished in time */ |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 99 | res = -EIO; |
| 100 | goto err_unlock; |
| 101 | } |
| 102 | |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 103 | res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 104 | |
| 105 | err_unlock: |
| 106 | mutex_unlock(&data->update_lock); |
| 107 | return res; |
| 108 | } |
| 109 | |
Guenter Roeck | 1196573 | 2012-04-09 13:53:00 -0400 | [diff] [blame] | 110 | static int ads1015_reg_to_mv(struct i2c_client *client, unsigned int channel, |
| 111 | s16 reg) |
| 112 | { |
| 113 | struct ads1015_data *data = i2c_get_clientdata(client); |
| 114 | unsigned int pga = data->channel_data[channel].pga; |
| 115 | int fullscale = fullscale_table[pga]; |
| 116 | |
| 117 | return DIV_ROUND_CLOSEST(reg * fullscale, 0x7ff0); |
| 118 | } |
| 119 | |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 120 | /* sysfs callback function */ |
| 121 | static ssize_t show_in(struct device *dev, struct device_attribute *da, |
| 122 | char *buf) |
| 123 | { |
| 124 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 125 | struct i2c_client *client = to_i2c_client(dev); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 126 | int res; |
Guenter Roeck | 1196573 | 2012-04-09 13:53:00 -0400 | [diff] [blame] | 127 | int index = attr->index; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 128 | |
Guenter Roeck | 1196573 | 2012-04-09 13:53:00 -0400 | [diff] [blame] | 129 | res = ads1015_read_adc(client, index); |
| 130 | if (res < 0) |
| 131 | return res; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 132 | |
Guenter Roeck | 1196573 | 2012-04-09 13:53:00 -0400 | [diff] [blame] | 133 | return sprintf(buf, "%d\n", ads1015_reg_to_mv(client, index, res)); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Jean Delvare | fdf241a | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 136 | static const struct sensor_device_attribute ads1015_in[] = { |
| 137 | SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0), |
| 138 | SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1), |
| 139 | SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2), |
| 140 | SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3), |
| 141 | SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4), |
| 142 | SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5), |
| 143 | SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6), |
| 144 | SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7), |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | /* |
| 148 | * Driver interface |
| 149 | */ |
| 150 | |
| 151 | static int ads1015_remove(struct i2c_client *client) |
| 152 | { |
| 153 | struct ads1015_data *data = i2c_get_clientdata(client); |
Jean Delvare | fdf241a | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 154 | int k; |
| 155 | |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 156 | hwmon_device_unregister(data->hwmon_dev); |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 157 | for (k = 0; k < ADS1015_CHANNELS; ++k) |
Jean Delvare | fdf241a | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 158 | device_remove_file(&client->dev, &ads1015_in[k].dev_attr); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 159 | kfree(data); |
| 160 | return 0; |
| 161 | } |
| 162 | |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 163 | #ifdef CONFIG_OF |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 164 | static int ads1015_get_channels_config_of(struct i2c_client *client) |
| 165 | { |
| 166 | struct ads1015_data *data = i2c_get_clientdata(client); |
| 167 | struct device_node *node; |
| 168 | |
| 169 | if (!client->dev.of_node |
| 170 | || !of_get_next_child(client->dev.of_node, NULL)) |
| 171 | return -EINVAL; |
| 172 | |
| 173 | for_each_child_of_node(client->dev.of_node, node) { |
| 174 | const __be32 *property; |
| 175 | int len; |
| 176 | unsigned int channel; |
| 177 | unsigned int pga = ADS1015_DEFAULT_PGA; |
| 178 | unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE; |
| 179 | |
| 180 | property = of_get_property(node, "reg", &len); |
| 181 | if (!property || len != sizeof(int)) { |
| 182 | dev_err(&client->dev, "invalid reg on %s\n", |
| 183 | node->full_name); |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | channel = be32_to_cpup(property); |
| 188 | if (channel > ADS1015_CHANNELS) { |
| 189 | dev_err(&client->dev, |
| 190 | "invalid channel index %d on %s\n", |
| 191 | channel, node->full_name); |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | property = of_get_property(node, "ti,gain", &len); |
| 196 | if (property && len == sizeof(int)) { |
| 197 | pga = be32_to_cpup(property); |
| 198 | if (pga > 6) { |
| 199 | dev_err(&client->dev, |
| 200 | "invalid gain on %s\n", |
| 201 | node->full_name); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | property = of_get_property(node, "ti,datarate", &len); |
| 206 | if (property && len == sizeof(int)) { |
| 207 | data_rate = be32_to_cpup(property); |
| 208 | if (data_rate > 7) { |
| 209 | dev_err(&client->dev, |
| 210 | "invalid data_rate on %s\n", |
| 211 | node->full_name); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | data->channel_data[channel].enabled = true; |
| 216 | data->channel_data[channel].pga = pga; |
| 217 | data->channel_data[channel].data_rate = data_rate; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 222 | #endif |
| 223 | |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 224 | static void ads1015_get_channels_config(struct i2c_client *client) |
| 225 | { |
| 226 | unsigned int k; |
| 227 | struct ads1015_data *data = i2c_get_clientdata(client); |
| 228 | struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev); |
| 229 | |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 230 | /* prefer platform data */ |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 231 | if (pdata) { |
| 232 | memcpy(data->channel_data, pdata->channel_data, |
| 233 | sizeof(data->channel_data)); |
| 234 | return; |
| 235 | } |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 236 | |
| 237 | #ifdef CONFIG_OF |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 238 | if (!ads1015_get_channels_config_of(client)) |
| 239 | return; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 240 | #endif |
| 241 | |
| 242 | /* fallback on default configuration */ |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 243 | for (k = 0; k < ADS1015_CHANNELS; ++k) { |
| 244 | data->channel_data[k].enabled = true; |
| 245 | data->channel_data[k].pga = ADS1015_DEFAULT_PGA; |
| 246 | data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE; |
| 247 | } |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static int ads1015_probe(struct i2c_client *client, |
| 251 | const struct i2c_device_id *id) |
| 252 | { |
| 253 | struct ads1015_data *data; |
| 254 | int err; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 255 | unsigned int k; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 256 | |
| 257 | data = kzalloc(sizeof(struct ads1015_data), GFP_KERNEL); |
| 258 | if (!data) { |
| 259 | err = -ENOMEM; |
| 260 | goto exit; |
| 261 | } |
| 262 | |
| 263 | i2c_set_clientdata(client, data); |
| 264 | mutex_init(&data->update_lock); |
| 265 | |
| 266 | /* build sysfs attribute group */ |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 267 | ads1015_get_channels_config(client); |
| 268 | for (k = 0; k < ADS1015_CHANNELS; ++k) { |
| 269 | if (!data->channel_data[k].enabled) |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 270 | continue; |
Jean Delvare | fdf241a | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 271 | err = device_create_file(&client->dev, &ads1015_in[k].dev_attr); |
| 272 | if (err) |
Guenter Roeck | 363434b | 2012-02-22 08:13:52 -0800 | [diff] [blame] | 273 | goto exit_remove; |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 274 | } |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 275 | |
| 276 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 277 | if (IS_ERR(data->hwmon_dev)) { |
| 278 | err = PTR_ERR(data->hwmon_dev); |
| 279 | goto exit_remove; |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | |
| 284 | exit_remove: |
Dirk Eibach | c004686 | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 285 | for (k = 0; k < ADS1015_CHANNELS; ++k) |
Jean Delvare | fdf241a | 2011-03-21 17:59:37 +0100 | [diff] [blame] | 286 | device_remove_file(&client->dev, &ads1015_in[k].dev_attr); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 287 | kfree(data); |
| 288 | exit: |
| 289 | return err; |
| 290 | } |
| 291 | |
| 292 | static const struct i2c_device_id ads1015_id[] = { |
| 293 | { "ads1015", 0 }, |
| 294 | { } |
| 295 | }; |
| 296 | MODULE_DEVICE_TABLE(i2c, ads1015_id); |
| 297 | |
| 298 | static struct i2c_driver ads1015_driver = { |
| 299 | .driver = { |
| 300 | .name = "ads1015", |
| 301 | }, |
| 302 | .probe = ads1015_probe, |
| 303 | .remove = ads1015_remove, |
| 304 | .id_table = ads1015_id, |
| 305 | }; |
| 306 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 307 | module_i2c_driver(ads1015_driver); |
Dirk Eibach | 8c22a8f | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 308 | |
| 309 | MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>"); |
| 310 | MODULE_DESCRIPTION("ADS1015 driver"); |
| 311 | MODULE_LICENSE("GPL"); |