Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ADS1015 - Texas Instruments Analog-to-Digital Converter |
| 3 | * |
| 4 | * Copyright (c) 2016, Intel Corporation. |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of version 2 of |
| 7 | * the GNU General Public License. See the file COPYING in the main |
| 8 | * directory of this archive for more details. |
| 9 | * |
| 10 | * IIO driver for ADS1015 ADC 7-bit I2C slave address: |
| 11 | * * 0x48 - ADDR connected to Ground |
| 12 | * * 0x49 - ADDR connected to Vdd |
| 13 | * * 0x4A - ADDR connected to SDA |
| 14 | * * 0x4B - ADDR connected to SCL |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/regmap.h> |
| 21 | #include <linux/pm_runtime.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/delay.h> |
| 24 | |
| 25 | #include <linux/i2c/ads1015.h> |
| 26 | |
| 27 | #include <linux/iio/iio.h> |
| 28 | #include <linux/iio/types.h> |
| 29 | #include <linux/iio/sysfs.h> |
| 30 | #include <linux/iio/buffer.h> |
| 31 | #include <linux/iio/triggered_buffer.h> |
| 32 | #include <linux/iio/trigger_consumer.h> |
| 33 | |
| 34 | #define ADS1015_DRV_NAME "ads1015" |
| 35 | |
| 36 | #define ADS1015_CONV_REG 0x00 |
| 37 | #define ADS1015_CFG_REG 0x01 |
| 38 | |
| 39 | #define ADS1015_CFG_DR_SHIFT 5 |
| 40 | #define ADS1015_CFG_MOD_SHIFT 8 |
| 41 | #define ADS1015_CFG_PGA_SHIFT 9 |
| 42 | #define ADS1015_CFG_MUX_SHIFT 12 |
| 43 | |
| 44 | #define ADS1015_CFG_DR_MASK GENMASK(7, 5) |
| 45 | #define ADS1015_CFG_MOD_MASK BIT(8) |
| 46 | #define ADS1015_CFG_PGA_MASK GENMASK(11, 9) |
| 47 | #define ADS1015_CFG_MUX_MASK GENMASK(14, 12) |
| 48 | |
| 49 | /* device operating modes */ |
| 50 | #define ADS1015_CONTINUOUS 0 |
| 51 | #define ADS1015_SINGLESHOT 1 |
| 52 | |
| 53 | #define ADS1015_SLEEP_DELAY_MS 2000 |
| 54 | #define ADS1015_DEFAULT_PGA 2 |
| 55 | #define ADS1015_DEFAULT_DATA_RATE 4 |
| 56 | #define ADS1015_DEFAULT_CHAN 0 |
| 57 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 58 | enum { |
| 59 | ADS1015, |
| 60 | ADS1115, |
| 61 | }; |
| 62 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 63 | enum ads1015_channels { |
| 64 | ADS1015_AIN0_AIN1 = 0, |
| 65 | ADS1015_AIN0_AIN3, |
| 66 | ADS1015_AIN1_AIN3, |
| 67 | ADS1015_AIN2_AIN3, |
| 68 | ADS1015_AIN0, |
| 69 | ADS1015_AIN1, |
| 70 | ADS1015_AIN2, |
| 71 | ADS1015_AIN3, |
| 72 | ADS1015_TIMESTAMP, |
| 73 | }; |
| 74 | |
| 75 | static const unsigned int ads1015_data_rate[] = { |
| 76 | 128, 250, 490, 920, 1600, 2400, 3300, 3300 |
| 77 | }; |
| 78 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 79 | static const unsigned int ads1115_data_rate[] = { |
| 80 | 8, 16, 32, 64, 128, 250, 475, 860 |
| 81 | }; |
| 82 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 83 | static const struct { |
| 84 | int scale; |
| 85 | int uscale; |
| 86 | } ads1015_scale[] = { |
| 87 | {3, 0}, |
| 88 | {2, 0}, |
| 89 | {1, 0}, |
| 90 | {0, 500000}, |
| 91 | {0, 250000}, |
| 92 | {0, 125000}, |
| 93 | {0, 125000}, |
| 94 | {0, 125000}, |
| 95 | }; |
| 96 | |
| 97 | #define ADS1015_V_CHAN(_chan, _addr) { \ |
| 98 | .type = IIO_VOLTAGE, \ |
| 99 | .indexed = 1, \ |
| 100 | .address = _addr, \ |
| 101 | .channel = _chan, \ |
| 102 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 103 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 104 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 105 | .scan_index = _addr, \ |
| 106 | .scan_type = { \ |
| 107 | .sign = 's', \ |
| 108 | .realbits = 12, \ |
| 109 | .storagebits = 16, \ |
| 110 | .shift = 4, \ |
| 111 | .endianness = IIO_CPU, \ |
| 112 | }, \ |
| 113 | } |
| 114 | |
| 115 | #define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \ |
| 116 | .type = IIO_VOLTAGE, \ |
| 117 | .differential = 1, \ |
| 118 | .indexed = 1, \ |
| 119 | .address = _addr, \ |
| 120 | .channel = _chan, \ |
| 121 | .channel2 = _chan2, \ |
| 122 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 123 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 124 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 125 | .scan_index = _addr, \ |
| 126 | .scan_type = { \ |
| 127 | .sign = 's', \ |
| 128 | .realbits = 12, \ |
| 129 | .storagebits = 16, \ |
| 130 | .shift = 4, \ |
| 131 | .endianness = IIO_CPU, \ |
| 132 | }, \ |
| 133 | } |
| 134 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 135 | #define ADS1115_V_CHAN(_chan, _addr) { \ |
| 136 | .type = IIO_VOLTAGE, \ |
| 137 | .indexed = 1, \ |
| 138 | .address = _addr, \ |
| 139 | .channel = _chan, \ |
| 140 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 141 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 142 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 143 | .scan_index = _addr, \ |
| 144 | .scan_type = { \ |
| 145 | .sign = 's', \ |
| 146 | .realbits = 16, \ |
| 147 | .storagebits = 16, \ |
| 148 | .endianness = IIO_CPU, \ |
| 149 | }, \ |
| 150 | } |
| 151 | |
| 152 | #define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) { \ |
| 153 | .type = IIO_VOLTAGE, \ |
| 154 | .differential = 1, \ |
| 155 | .indexed = 1, \ |
| 156 | .address = _addr, \ |
| 157 | .channel = _chan, \ |
| 158 | .channel2 = _chan2, \ |
| 159 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 160 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 161 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 162 | .scan_index = _addr, \ |
| 163 | .scan_type = { \ |
| 164 | .sign = 's', \ |
| 165 | .realbits = 16, \ |
| 166 | .storagebits = 16, \ |
| 167 | .endianness = IIO_CPU, \ |
| 168 | }, \ |
| 169 | } |
| 170 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 171 | struct ads1015_data { |
| 172 | struct regmap *regmap; |
| 173 | /* |
| 174 | * Protects ADC ops, e.g: concurrent sysfs/buffered |
| 175 | * data reads, configuration updates |
| 176 | */ |
| 177 | struct mutex lock; |
| 178 | struct ads1015_channel_data channel_data[ADS1015_CHANNELS]; |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 179 | |
| 180 | unsigned int *data_rate; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | static bool ads1015_is_writeable_reg(struct device *dev, unsigned int reg) |
| 184 | { |
| 185 | return (reg == ADS1015_CFG_REG); |
| 186 | } |
| 187 | |
| 188 | static const struct regmap_config ads1015_regmap_config = { |
| 189 | .reg_bits = 8, |
| 190 | .val_bits = 16, |
| 191 | .max_register = ADS1015_CFG_REG, |
| 192 | .writeable_reg = ads1015_is_writeable_reg, |
| 193 | }; |
| 194 | |
| 195 | static const struct iio_chan_spec ads1015_channels[] = { |
| 196 | ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1), |
| 197 | ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3), |
| 198 | ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3), |
| 199 | ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3), |
| 200 | ADS1015_V_CHAN(0, ADS1015_AIN0), |
| 201 | ADS1015_V_CHAN(1, ADS1015_AIN1), |
| 202 | ADS1015_V_CHAN(2, ADS1015_AIN2), |
| 203 | ADS1015_V_CHAN(3, ADS1015_AIN3), |
| 204 | IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP), |
| 205 | }; |
| 206 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 207 | static const struct iio_chan_spec ads1115_channels[] = { |
| 208 | ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1), |
| 209 | ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3), |
| 210 | ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3), |
| 211 | ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3), |
| 212 | ADS1115_V_CHAN(0, ADS1015_AIN0), |
| 213 | ADS1115_V_CHAN(1, ADS1015_AIN1), |
| 214 | ADS1115_V_CHAN(2, ADS1015_AIN2), |
| 215 | ADS1115_V_CHAN(3, ADS1015_AIN3), |
| 216 | IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP), |
| 217 | }; |
| 218 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 219 | static int ads1015_set_power_state(struct ads1015_data *data, bool on) |
| 220 | { |
| 221 | int ret; |
| 222 | struct device *dev = regmap_get_device(data->regmap); |
| 223 | |
| 224 | if (on) { |
| 225 | ret = pm_runtime_get_sync(dev); |
| 226 | if (ret < 0) |
| 227 | pm_runtime_put_noidle(dev); |
| 228 | } else { |
| 229 | pm_runtime_mark_last_busy(dev); |
| 230 | ret = pm_runtime_put_autosuspend(dev); |
| 231 | } |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | static |
| 237 | int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val) |
| 238 | { |
| 239 | int ret, pga, dr, conv_time; |
| 240 | bool change; |
| 241 | |
| 242 | if (chan < 0 || chan >= ADS1015_CHANNELS) |
| 243 | return -EINVAL; |
| 244 | |
| 245 | pga = data->channel_data[chan].pga; |
| 246 | dr = data->channel_data[chan].data_rate; |
| 247 | |
| 248 | ret = regmap_update_bits_check(data->regmap, ADS1015_CFG_REG, |
| 249 | ADS1015_CFG_MUX_MASK | |
| 250 | ADS1015_CFG_PGA_MASK, |
| 251 | chan << ADS1015_CFG_MUX_SHIFT | |
| 252 | pga << ADS1015_CFG_PGA_SHIFT, |
| 253 | &change); |
| 254 | if (ret < 0) |
| 255 | return ret; |
| 256 | |
| 257 | if (change) { |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 258 | conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 259 | usleep_range(conv_time, conv_time + 1); |
| 260 | } |
| 261 | |
| 262 | return regmap_read(data->regmap, ADS1015_CONV_REG, val); |
| 263 | } |
| 264 | |
| 265 | static irqreturn_t ads1015_trigger_handler(int irq, void *p) |
| 266 | { |
| 267 | struct iio_poll_func *pf = p; |
| 268 | struct iio_dev *indio_dev = pf->indio_dev; |
| 269 | struct ads1015_data *data = iio_priv(indio_dev); |
| 270 | s16 buf[8]; /* 1x s16 ADC val + 3x s16 padding + 4x s16 timestamp */ |
| 271 | int chan, ret, res; |
| 272 | |
| 273 | memset(buf, 0, sizeof(buf)); |
| 274 | |
| 275 | mutex_lock(&data->lock); |
| 276 | chan = find_first_bit(indio_dev->active_scan_mask, |
| 277 | indio_dev->masklength); |
| 278 | ret = ads1015_get_adc_result(data, chan, &res); |
| 279 | if (ret < 0) { |
| 280 | mutex_unlock(&data->lock); |
| 281 | goto err; |
| 282 | } |
| 283 | |
| 284 | buf[0] = res; |
| 285 | mutex_unlock(&data->lock); |
| 286 | |
| 287 | iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); |
| 288 | |
| 289 | err: |
| 290 | iio_trigger_notify_done(indio_dev->trig); |
| 291 | |
| 292 | return IRQ_HANDLED; |
| 293 | } |
| 294 | |
| 295 | static int ads1015_set_scale(struct ads1015_data *data, int chan, |
| 296 | int scale, int uscale) |
| 297 | { |
| 298 | int i, ret, rindex = -1; |
| 299 | |
| 300 | for (i = 0; i < ARRAY_SIZE(ads1015_scale); i++) |
| 301 | if (ads1015_scale[i].scale == scale && |
| 302 | ads1015_scale[i].uscale == uscale) { |
| 303 | rindex = i; |
| 304 | break; |
| 305 | } |
| 306 | if (rindex < 0) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
| 310 | ADS1015_CFG_PGA_MASK, |
| 311 | rindex << ADS1015_CFG_PGA_SHIFT); |
| 312 | if (ret < 0) |
| 313 | return ret; |
| 314 | |
| 315 | data->channel_data[chan].pga = rindex; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int ads1015_set_data_rate(struct ads1015_data *data, int chan, int rate) |
| 321 | { |
| 322 | int i, ret, rindex = -1; |
| 323 | |
| 324 | for (i = 0; i < ARRAY_SIZE(ads1015_data_rate); i++) |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 325 | if (data->data_rate[i] == rate) { |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 326 | rindex = i; |
| 327 | break; |
| 328 | } |
| 329 | if (rindex < 0) |
| 330 | return -EINVAL; |
| 331 | |
| 332 | ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
| 333 | ADS1015_CFG_DR_MASK, |
| 334 | rindex << ADS1015_CFG_DR_SHIFT); |
| 335 | if (ret < 0) |
| 336 | return ret; |
| 337 | |
| 338 | data->channel_data[chan].data_rate = rindex; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int ads1015_read_raw(struct iio_dev *indio_dev, |
| 344 | struct iio_chan_spec const *chan, int *val, |
| 345 | int *val2, long mask) |
| 346 | { |
| 347 | int ret, idx; |
| 348 | struct ads1015_data *data = iio_priv(indio_dev); |
| 349 | |
| 350 | mutex_lock(&indio_dev->mlock); |
| 351 | mutex_lock(&data->lock); |
| 352 | switch (mask) { |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 353 | case IIO_CHAN_INFO_RAW: { |
| 354 | int shift = chan->scan_type.shift; |
| 355 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 356 | if (iio_buffer_enabled(indio_dev)) { |
| 357 | ret = -EBUSY; |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | ret = ads1015_set_power_state(data, true); |
| 362 | if (ret < 0) |
| 363 | break; |
| 364 | |
| 365 | ret = ads1015_get_adc_result(data, chan->address, val); |
| 366 | if (ret < 0) { |
| 367 | ads1015_set_power_state(data, false); |
| 368 | break; |
| 369 | } |
| 370 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 371 | *val = sign_extend32(*val >> shift, 15 - shift); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 372 | |
| 373 | ret = ads1015_set_power_state(data, false); |
| 374 | if (ret < 0) |
| 375 | break; |
| 376 | |
| 377 | ret = IIO_VAL_INT; |
| 378 | break; |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 379 | } |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 380 | case IIO_CHAN_INFO_SCALE: |
| 381 | idx = data->channel_data[chan->address].pga; |
| 382 | *val = ads1015_scale[idx].scale; |
| 383 | *val2 = ads1015_scale[idx].uscale; |
| 384 | ret = IIO_VAL_INT_PLUS_MICRO; |
| 385 | break; |
| 386 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 387 | idx = data->channel_data[chan->address].data_rate; |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 388 | *val = data->data_rate[idx]; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 389 | ret = IIO_VAL_INT; |
| 390 | break; |
| 391 | default: |
| 392 | ret = -EINVAL; |
| 393 | break; |
| 394 | } |
| 395 | mutex_unlock(&data->lock); |
| 396 | mutex_unlock(&indio_dev->mlock); |
| 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | static int ads1015_write_raw(struct iio_dev *indio_dev, |
| 402 | struct iio_chan_spec const *chan, int val, |
| 403 | int val2, long mask) |
| 404 | { |
| 405 | struct ads1015_data *data = iio_priv(indio_dev); |
| 406 | int ret; |
| 407 | |
| 408 | mutex_lock(&data->lock); |
| 409 | switch (mask) { |
| 410 | case IIO_CHAN_INFO_SCALE: |
| 411 | ret = ads1015_set_scale(data, chan->address, val, val2); |
| 412 | break; |
| 413 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 414 | ret = ads1015_set_data_rate(data, chan->address, val); |
| 415 | break; |
| 416 | default: |
| 417 | ret = -EINVAL; |
| 418 | break; |
| 419 | } |
| 420 | mutex_unlock(&data->lock); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static int ads1015_buffer_preenable(struct iio_dev *indio_dev) |
| 426 | { |
| 427 | return ads1015_set_power_state(iio_priv(indio_dev), true); |
| 428 | } |
| 429 | |
| 430 | static int ads1015_buffer_postdisable(struct iio_dev *indio_dev) |
| 431 | { |
| 432 | return ads1015_set_power_state(iio_priv(indio_dev), false); |
| 433 | } |
| 434 | |
| 435 | static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = { |
| 436 | .preenable = ads1015_buffer_preenable, |
| 437 | .postenable = iio_triggered_buffer_postenable, |
| 438 | .predisable = iio_triggered_buffer_predisable, |
| 439 | .postdisable = ads1015_buffer_postdisable, |
| 440 | .validate_scan_mask = &iio_validate_scan_mask_onehot, |
| 441 | }; |
| 442 | |
| 443 | static IIO_CONST_ATTR(scale_available, "3 2 1 0.5 0.25 0.125"); |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 444 | |
| 445 | static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available, |
| 446 | sampling_frequency_available, "128 250 490 920 1600 2400 3300"); |
| 447 | static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available, |
| 448 | sampling_frequency_available, "8 16 32 64 128 250 475 860"); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 449 | |
| 450 | static struct attribute *ads1015_attributes[] = { |
| 451 | &iio_const_attr_scale_available.dev_attr.attr, |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 452 | &iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 453 | NULL, |
| 454 | }; |
| 455 | |
| 456 | static const struct attribute_group ads1015_attribute_group = { |
| 457 | .attrs = ads1015_attributes, |
| 458 | }; |
| 459 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 460 | static struct attribute *ads1115_attributes[] = { |
| 461 | &iio_const_attr_scale_available.dev_attr.attr, |
| 462 | &iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr, |
| 463 | NULL, |
| 464 | }; |
| 465 | |
| 466 | static const struct attribute_group ads1115_attribute_group = { |
| 467 | .attrs = ads1115_attributes, |
| 468 | }; |
| 469 | |
| 470 | static struct iio_info ads1015_info = { |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 471 | .driver_module = THIS_MODULE, |
| 472 | .read_raw = ads1015_read_raw, |
| 473 | .write_raw = ads1015_write_raw, |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 474 | .attrs = &ads1015_attribute_group, |
| 475 | }; |
| 476 | |
| 477 | static struct iio_info ads1115_info = { |
| 478 | .driver_module = THIS_MODULE, |
| 479 | .read_raw = ads1015_read_raw, |
| 480 | .write_raw = ads1015_write_raw, |
| 481 | .attrs = &ads1115_attribute_group, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | #ifdef CONFIG_OF |
| 485 | static int ads1015_get_channels_config_of(struct i2c_client *client) |
| 486 | { |
| 487 | struct ads1015_data *data = i2c_get_clientdata(client); |
| 488 | struct device_node *node; |
| 489 | |
| 490 | if (!client->dev.of_node || |
| 491 | !of_get_next_child(client->dev.of_node, NULL)) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | for_each_child_of_node(client->dev.of_node, node) { |
| 495 | u32 pval; |
| 496 | unsigned int channel; |
| 497 | unsigned int pga = ADS1015_DEFAULT_PGA; |
| 498 | unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE; |
| 499 | |
| 500 | if (of_property_read_u32(node, "reg", &pval)) { |
| 501 | dev_err(&client->dev, "invalid reg on %s\n", |
| 502 | node->full_name); |
| 503 | continue; |
| 504 | } |
| 505 | |
| 506 | channel = pval; |
| 507 | if (channel >= ADS1015_CHANNELS) { |
| 508 | dev_err(&client->dev, |
| 509 | "invalid channel index %d on %s\n", |
| 510 | channel, node->full_name); |
| 511 | continue; |
| 512 | } |
| 513 | |
| 514 | if (!of_property_read_u32(node, "ti,gain", &pval)) { |
| 515 | pga = pval; |
| 516 | if (pga > 6) { |
| 517 | dev_err(&client->dev, "invalid gain on %s\n", |
| 518 | node->full_name); |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (!of_property_read_u32(node, "ti,datarate", &pval)) { |
| 524 | data_rate = pval; |
| 525 | if (data_rate > 7) { |
| 526 | dev_err(&client->dev, |
| 527 | "invalid data_rate on %s\n", |
| 528 | node->full_name); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | data->channel_data[channel].pga = pga; |
| 534 | data->channel_data[channel].data_rate = data_rate; |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | #endif |
| 540 | |
| 541 | static void ads1015_get_channels_config(struct i2c_client *client) |
| 542 | { |
| 543 | unsigned int k; |
| 544 | |
| 545 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 546 | struct ads1015_data *data = iio_priv(indio_dev); |
| 547 | struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev); |
| 548 | |
| 549 | /* prefer platform data */ |
| 550 | if (pdata) { |
| 551 | memcpy(data->channel_data, pdata->channel_data, |
| 552 | sizeof(data->channel_data)); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | #ifdef CONFIG_OF |
| 557 | if (!ads1015_get_channels_config_of(client)) |
| 558 | return; |
| 559 | #endif |
| 560 | /* fallback on default configuration */ |
| 561 | for (k = 0; k < ADS1015_CHANNELS; ++k) { |
| 562 | data->channel_data[k].pga = ADS1015_DEFAULT_PGA; |
| 563 | data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | static int ads1015_probe(struct i2c_client *client, |
| 568 | const struct i2c_device_id *id) |
| 569 | { |
| 570 | struct iio_dev *indio_dev; |
| 571 | struct ads1015_data *data; |
| 572 | int ret; |
| 573 | |
| 574 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 575 | if (!indio_dev) |
| 576 | return -ENOMEM; |
| 577 | |
| 578 | data = iio_priv(indio_dev); |
| 579 | i2c_set_clientdata(client, indio_dev); |
| 580 | |
| 581 | mutex_init(&data->lock); |
| 582 | |
| 583 | indio_dev->dev.parent = &client->dev; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 584 | indio_dev->name = ADS1015_DRV_NAME; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 585 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 586 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 587 | switch (id->driver_data) { |
| 588 | case ADS1015: |
| 589 | indio_dev->channels = ads1015_channels; |
| 590 | indio_dev->num_channels = ARRAY_SIZE(ads1015_channels); |
| 591 | indio_dev->info = &ads1015_info; |
| 592 | data->data_rate = (unsigned int *) &ads1015_data_rate; |
| 593 | break; |
| 594 | case ADS1115: |
| 595 | indio_dev->channels = ads1115_channels; |
| 596 | indio_dev->num_channels = ARRAY_SIZE(ads1115_channels); |
| 597 | indio_dev->info = &ads1115_info; |
| 598 | data->data_rate = (unsigned int *) &ads1115_data_rate; |
| 599 | break; |
| 600 | } |
| 601 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 602 | /* we need to keep this ABI the same as used by hwmon ADS1015 driver */ |
| 603 | ads1015_get_channels_config(client); |
| 604 | |
| 605 | data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config); |
| 606 | if (IS_ERR(data->regmap)) { |
| 607 | dev_err(&client->dev, "Failed to allocate register map\n"); |
| 608 | return PTR_ERR(data->regmap); |
| 609 | } |
| 610 | |
| 611 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 612 | ads1015_trigger_handler, |
| 613 | &ads1015_buffer_setup_ops); |
| 614 | if (ret < 0) { |
| 615 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); |
| 616 | return ret; |
| 617 | } |
| 618 | ret = pm_runtime_set_active(&client->dev); |
| 619 | if (ret) |
| 620 | goto err_buffer_cleanup; |
| 621 | pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS); |
| 622 | pm_runtime_use_autosuspend(&client->dev); |
| 623 | pm_runtime_enable(&client->dev); |
| 624 | |
| 625 | ret = iio_device_register(indio_dev); |
| 626 | if (ret < 0) { |
| 627 | dev_err(&client->dev, "Failed to register IIO device\n"); |
| 628 | goto err_buffer_cleanup; |
| 629 | } |
| 630 | |
| 631 | return 0; |
| 632 | |
| 633 | err_buffer_cleanup: |
| 634 | iio_triggered_buffer_cleanup(indio_dev); |
| 635 | |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static int ads1015_remove(struct i2c_client *client) |
| 640 | { |
| 641 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 642 | struct ads1015_data *data = iio_priv(indio_dev); |
| 643 | |
| 644 | iio_device_unregister(indio_dev); |
| 645 | |
| 646 | pm_runtime_disable(&client->dev); |
| 647 | pm_runtime_set_suspended(&client->dev); |
| 648 | pm_runtime_put_noidle(&client->dev); |
| 649 | |
| 650 | iio_triggered_buffer_cleanup(indio_dev); |
| 651 | |
| 652 | /* power down single shot mode */ |
| 653 | return regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
| 654 | ADS1015_CFG_MOD_MASK, |
| 655 | ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT); |
| 656 | } |
| 657 | |
| 658 | #ifdef CONFIG_PM |
| 659 | static int ads1015_runtime_suspend(struct device *dev) |
| 660 | { |
| 661 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 662 | struct ads1015_data *data = iio_priv(indio_dev); |
| 663 | |
| 664 | return regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
| 665 | ADS1015_CFG_MOD_MASK, |
| 666 | ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT); |
| 667 | } |
| 668 | |
| 669 | static int ads1015_runtime_resume(struct device *dev) |
| 670 | { |
| 671 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 672 | struct ads1015_data *data = iio_priv(indio_dev); |
| 673 | |
| 674 | return regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
| 675 | ADS1015_CFG_MOD_MASK, |
| 676 | ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT); |
| 677 | } |
| 678 | #endif |
| 679 | |
| 680 | static const struct dev_pm_ops ads1015_pm_ops = { |
| 681 | SET_RUNTIME_PM_OPS(ads1015_runtime_suspend, |
| 682 | ads1015_runtime_resume, NULL) |
| 683 | }; |
| 684 | |
| 685 | static const struct i2c_device_id ads1015_id[] = { |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame^] | 686 | {"ads1015", ADS1015}, |
| 687 | {"ads1115", ADS1115}, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 688 | {} |
| 689 | }; |
| 690 | MODULE_DEVICE_TABLE(i2c, ads1015_id); |
| 691 | |
| 692 | static struct i2c_driver ads1015_driver = { |
| 693 | .driver = { |
| 694 | .name = ADS1015_DRV_NAME, |
| 695 | .pm = &ads1015_pm_ops, |
| 696 | }, |
| 697 | .probe = ads1015_probe, |
| 698 | .remove = ads1015_remove, |
| 699 | .id_table = ads1015_id, |
| 700 | }; |
| 701 | |
| 702 | module_i2c_driver(ads1015_driver); |
| 703 | |
| 704 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); |
| 705 | MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver"); |
| 706 | MODULE_LICENSE("GPL v2"); |