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> |
Javier Martinez Canillas | c172d22 | 2017-03-15 01:45:00 -0300 | [diff] [blame] | 18 | #include <linux/of_device.h> |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 19 | #include <linux/init.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/regmap.h> |
| 22 | #include <linux/pm_runtime.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/delay.h> |
| 25 | |
Wolfram Sang | 9010624c | 2017-05-21 22:34:39 +0200 | [diff] [blame] | 26 | #include <linux/platform_data/ads1015.h> |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 27 | |
| 28 | #include <linux/iio/iio.h> |
| 29 | #include <linux/iio/types.h> |
| 30 | #include <linux/iio/sysfs.h> |
| 31 | #include <linux/iio/buffer.h> |
| 32 | #include <linux/iio/triggered_buffer.h> |
| 33 | #include <linux/iio/trigger_consumer.h> |
| 34 | |
| 35 | #define ADS1015_DRV_NAME "ads1015" |
| 36 | |
| 37 | #define ADS1015_CONV_REG 0x00 |
| 38 | #define ADS1015_CFG_REG 0x01 |
| 39 | |
| 40 | #define ADS1015_CFG_DR_SHIFT 5 |
| 41 | #define ADS1015_CFG_MOD_SHIFT 8 |
| 42 | #define ADS1015_CFG_PGA_SHIFT 9 |
| 43 | #define ADS1015_CFG_MUX_SHIFT 12 |
| 44 | |
| 45 | #define ADS1015_CFG_DR_MASK GENMASK(7, 5) |
| 46 | #define ADS1015_CFG_MOD_MASK BIT(8) |
| 47 | #define ADS1015_CFG_PGA_MASK GENMASK(11, 9) |
| 48 | #define ADS1015_CFG_MUX_MASK GENMASK(14, 12) |
| 49 | |
| 50 | /* device operating modes */ |
| 51 | #define ADS1015_CONTINUOUS 0 |
| 52 | #define ADS1015_SINGLESHOT 1 |
| 53 | |
| 54 | #define ADS1015_SLEEP_DELAY_MS 2000 |
| 55 | #define ADS1015_DEFAULT_PGA 2 |
| 56 | #define ADS1015_DEFAULT_DATA_RATE 4 |
| 57 | #define ADS1015_DEFAULT_CHAN 0 |
| 58 | |
Javier Martinez Canillas | c172d22 | 2017-03-15 01:45:00 -0300 | [diff] [blame] | 59 | enum chip_ids { |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 60 | ADS1015, |
| 61 | ADS1115, |
| 62 | }; |
| 63 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 64 | enum ads1015_channels { |
| 65 | ADS1015_AIN0_AIN1 = 0, |
| 66 | ADS1015_AIN0_AIN3, |
| 67 | ADS1015_AIN1_AIN3, |
| 68 | ADS1015_AIN2_AIN3, |
| 69 | ADS1015_AIN0, |
| 70 | ADS1015_AIN1, |
| 71 | ADS1015_AIN2, |
| 72 | ADS1015_AIN3, |
| 73 | ADS1015_TIMESTAMP, |
| 74 | }; |
| 75 | |
| 76 | static const unsigned int ads1015_data_rate[] = { |
| 77 | 128, 250, 490, 920, 1600, 2400, 3300, 3300 |
| 78 | }; |
| 79 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 80 | static const unsigned int ads1115_data_rate[] = { |
| 81 | 8, 16, 32, 64, 128, 250, 475, 860 |
| 82 | }; |
| 83 | |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 84 | /* |
| 85 | * Translation from PGA bits to full-scale positive and negative input voltage |
| 86 | * range in mV |
| 87 | */ |
| 88 | static int ads1015_fullscale_range[] = { |
| 89 | 6144, 4096, 2048, 1024, 512, 256, 256, 256 |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | #define ADS1015_V_CHAN(_chan, _addr) { \ |
| 93 | .type = IIO_VOLTAGE, \ |
| 94 | .indexed = 1, \ |
| 95 | .address = _addr, \ |
| 96 | .channel = _chan, \ |
| 97 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 98 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 99 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 100 | .scan_index = _addr, \ |
| 101 | .scan_type = { \ |
| 102 | .sign = 's', \ |
| 103 | .realbits = 12, \ |
| 104 | .storagebits = 16, \ |
| 105 | .shift = 4, \ |
| 106 | .endianness = IIO_CPU, \ |
| 107 | }, \ |
Matt Ranostay | 8ac8aa6 | 2016-05-17 15:02:03 -0700 | [diff] [blame] | 108 | .datasheet_name = "AIN"#_chan, \ |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | #define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \ |
| 112 | .type = IIO_VOLTAGE, \ |
| 113 | .differential = 1, \ |
| 114 | .indexed = 1, \ |
| 115 | .address = _addr, \ |
| 116 | .channel = _chan, \ |
| 117 | .channel2 = _chan2, \ |
| 118 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 119 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 120 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 121 | .scan_index = _addr, \ |
| 122 | .scan_type = { \ |
| 123 | .sign = 's', \ |
| 124 | .realbits = 12, \ |
| 125 | .storagebits = 16, \ |
| 126 | .shift = 4, \ |
| 127 | .endianness = IIO_CPU, \ |
| 128 | }, \ |
Matt Ranostay | 8ac8aa6 | 2016-05-17 15:02:03 -0700 | [diff] [blame] | 129 | .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \ |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 132 | #define ADS1115_V_CHAN(_chan, _addr) { \ |
| 133 | .type = IIO_VOLTAGE, \ |
| 134 | .indexed = 1, \ |
| 135 | .address = _addr, \ |
| 136 | .channel = _chan, \ |
| 137 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 138 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 139 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 140 | .scan_index = _addr, \ |
| 141 | .scan_type = { \ |
| 142 | .sign = 's', \ |
| 143 | .realbits = 16, \ |
| 144 | .storagebits = 16, \ |
| 145 | .endianness = IIO_CPU, \ |
| 146 | }, \ |
Matt Ranostay | 8ac8aa6 | 2016-05-17 15:02:03 -0700 | [diff] [blame] | 147 | .datasheet_name = "AIN"#_chan, \ |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | #define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) { \ |
| 151 | .type = IIO_VOLTAGE, \ |
| 152 | .differential = 1, \ |
| 153 | .indexed = 1, \ |
| 154 | .address = _addr, \ |
| 155 | .channel = _chan, \ |
| 156 | .channel2 = _chan2, \ |
| 157 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 158 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 159 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 160 | .scan_index = _addr, \ |
| 161 | .scan_type = { \ |
| 162 | .sign = 's', \ |
| 163 | .realbits = 16, \ |
| 164 | .storagebits = 16, \ |
| 165 | .endianness = IIO_CPU, \ |
| 166 | }, \ |
Matt Ranostay | 8ac8aa6 | 2016-05-17 15:02:03 -0700 | [diff] [blame] | 167 | .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \ |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 170 | struct ads1015_data { |
| 171 | struct regmap *regmap; |
| 172 | /* |
| 173 | * Protects ADC ops, e.g: concurrent sysfs/buffered |
| 174 | * data reads, configuration updates |
| 175 | */ |
| 176 | struct mutex lock; |
| 177 | struct ads1015_channel_data channel_data[ADS1015_CHANNELS]; |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 178 | |
| 179 | unsigned int *data_rate; |
Akinobu Mita | 73e3e3f | 2017-07-21 00:24:20 +0900 | [diff] [blame] | 180 | /* |
| 181 | * Set to true when the ADC is switched to the continuous-conversion |
| 182 | * mode and exits from a power-down state. This flag is used to avoid |
| 183 | * getting the stale result from the conversion register. |
| 184 | */ |
| 185 | bool conv_invalid; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | static bool ads1015_is_writeable_reg(struct device *dev, unsigned int reg) |
| 189 | { |
| 190 | return (reg == ADS1015_CFG_REG); |
| 191 | } |
| 192 | |
| 193 | static const struct regmap_config ads1015_regmap_config = { |
| 194 | .reg_bits = 8, |
| 195 | .val_bits = 16, |
| 196 | .max_register = ADS1015_CFG_REG, |
| 197 | .writeable_reg = ads1015_is_writeable_reg, |
| 198 | }; |
| 199 | |
| 200 | static const struct iio_chan_spec ads1015_channels[] = { |
| 201 | ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1), |
| 202 | ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3), |
| 203 | ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3), |
| 204 | ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3), |
| 205 | ADS1015_V_CHAN(0, ADS1015_AIN0), |
| 206 | ADS1015_V_CHAN(1, ADS1015_AIN1), |
| 207 | ADS1015_V_CHAN(2, ADS1015_AIN2), |
| 208 | ADS1015_V_CHAN(3, ADS1015_AIN3), |
| 209 | IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP), |
| 210 | }; |
| 211 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 212 | static const struct iio_chan_spec ads1115_channels[] = { |
| 213 | ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1), |
| 214 | ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3), |
| 215 | ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3), |
| 216 | ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3), |
| 217 | ADS1115_V_CHAN(0, ADS1015_AIN0), |
| 218 | ADS1115_V_CHAN(1, ADS1015_AIN1), |
| 219 | ADS1115_V_CHAN(2, ADS1015_AIN2), |
| 220 | ADS1115_V_CHAN(3, ADS1015_AIN3), |
| 221 | IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP), |
| 222 | }; |
| 223 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 224 | static int ads1015_set_power_state(struct ads1015_data *data, bool on) |
| 225 | { |
| 226 | int ret; |
| 227 | struct device *dev = regmap_get_device(data->regmap); |
| 228 | |
| 229 | if (on) { |
| 230 | ret = pm_runtime_get_sync(dev); |
| 231 | if (ret < 0) |
| 232 | pm_runtime_put_noidle(dev); |
| 233 | } else { |
| 234 | pm_runtime_mark_last_busy(dev); |
| 235 | ret = pm_runtime_put_autosuspend(dev); |
| 236 | } |
| 237 | |
Akinobu Mita | a6fe5e5 | 2017-07-21 00:24:21 +0900 | [diff] [blame] | 238 | return ret < 0 ? ret : 0; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static |
| 242 | int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val) |
| 243 | { |
| 244 | int ret, pga, dr, conv_time; |
Akinobu Mita | 4744d4e | 2017-07-21 00:24:22 +0900 | [diff] [blame] | 245 | unsigned int old, mask, cfg; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 246 | |
| 247 | if (chan < 0 || chan >= ADS1015_CHANNELS) |
| 248 | return -EINVAL; |
| 249 | |
Akinobu Mita | 4744d4e | 2017-07-21 00:24:22 +0900 | [diff] [blame] | 250 | ret = regmap_read(data->regmap, ADS1015_CFG_REG, &old); |
| 251 | if (ret) |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 252 | return ret; |
| 253 | |
Akinobu Mita | 4744d4e | 2017-07-21 00:24:22 +0900 | [diff] [blame] | 254 | pga = data->channel_data[chan].pga; |
| 255 | dr = data->channel_data[chan].data_rate; |
| 256 | mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK | |
| 257 | ADS1015_CFG_DR_MASK; |
| 258 | cfg = chan << ADS1015_CFG_MUX_SHIFT | pga << ADS1015_CFG_PGA_SHIFT | |
| 259 | dr << ADS1015_CFG_DR_SHIFT; |
| 260 | |
| 261 | cfg = (old & ~mask) | (cfg & mask); |
| 262 | |
| 263 | ret = regmap_write(data->regmap, ADS1015_CFG_REG, cfg); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
| 267 | if (old != cfg || data->conv_invalid) { |
| 268 | int dr_old = (old & ADS1015_CFG_DR_MASK) >> |
| 269 | ADS1015_CFG_DR_SHIFT; |
| 270 | |
| 271 | conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr_old]); |
| 272 | conv_time += DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 273 | usleep_range(conv_time, conv_time + 1); |
Akinobu Mita | 73e3e3f | 2017-07-21 00:24:20 +0900 | [diff] [blame] | 274 | data->conv_invalid = false; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | return regmap_read(data->regmap, ADS1015_CONV_REG, val); |
| 278 | } |
| 279 | |
| 280 | static irqreturn_t ads1015_trigger_handler(int irq, void *p) |
| 281 | { |
| 282 | struct iio_poll_func *pf = p; |
| 283 | struct iio_dev *indio_dev = pf->indio_dev; |
| 284 | struct ads1015_data *data = iio_priv(indio_dev); |
| 285 | s16 buf[8]; /* 1x s16 ADC val + 3x s16 padding + 4x s16 timestamp */ |
| 286 | int chan, ret, res; |
| 287 | |
| 288 | memset(buf, 0, sizeof(buf)); |
| 289 | |
| 290 | mutex_lock(&data->lock); |
| 291 | chan = find_first_bit(indio_dev->active_scan_mask, |
| 292 | indio_dev->masklength); |
| 293 | ret = ads1015_get_adc_result(data, chan, &res); |
| 294 | if (ret < 0) { |
| 295 | mutex_unlock(&data->lock); |
| 296 | goto err; |
| 297 | } |
| 298 | |
| 299 | buf[0] = res; |
| 300 | mutex_unlock(&data->lock); |
| 301 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 302 | iio_push_to_buffers_with_timestamp(indio_dev, buf, |
| 303 | iio_get_time_ns(indio_dev)); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 304 | |
| 305 | err: |
| 306 | iio_trigger_notify_done(indio_dev->trig); |
| 307 | |
| 308 | return IRQ_HANDLED; |
| 309 | } |
| 310 | |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 311 | static int ads1015_set_scale(struct ads1015_data *data, |
| 312 | struct iio_chan_spec const *chan, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 313 | int scale, int uscale) |
| 314 | { |
Akinobu Mita | 56b57a9 | 2017-07-21 00:24:23 +0900 | [diff] [blame^] | 315 | int i; |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 316 | int fullscale = div_s64((scale * 1000000LL + uscale) << |
| 317 | (chan->scan_type.realbits - 1), 1000000); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 318 | |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 319 | for (i = 0; i < ARRAY_SIZE(ads1015_fullscale_range); i++) { |
| 320 | if (ads1015_fullscale_range[i] == fullscale) { |
Akinobu Mita | 56b57a9 | 2017-07-21 00:24:23 +0900 | [diff] [blame^] | 321 | data->channel_data[chan->address].pga = i; |
| 322 | return 0; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 323 | } |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 324 | } |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 325 | |
Akinobu Mita | 56b57a9 | 2017-07-21 00:24:23 +0900 | [diff] [blame^] | 326 | return -EINVAL; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static int ads1015_set_data_rate(struct ads1015_data *data, int chan, int rate) |
| 330 | { |
Akinobu Mita | 0d106b7 | 2017-07-21 00:24:17 +0900 | [diff] [blame] | 331 | int i; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 332 | |
Akinobu Mita | 0d106b7 | 2017-07-21 00:24:17 +0900 | [diff] [blame] | 333 | for (i = 0; i < ARRAY_SIZE(ads1015_data_rate); i++) { |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 334 | if (data->data_rate[i] == rate) { |
Akinobu Mita | 0d106b7 | 2017-07-21 00:24:17 +0900 | [diff] [blame] | 335 | data->channel_data[chan].data_rate = i; |
| 336 | return 0; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 337 | } |
Akinobu Mita | 0d106b7 | 2017-07-21 00:24:17 +0900 | [diff] [blame] | 338 | } |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 339 | |
Akinobu Mita | 0d106b7 | 2017-07-21 00:24:17 +0900 | [diff] [blame] | 340 | return -EINVAL; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 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; |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 382 | *val = ads1015_fullscale_range[idx]; |
| 383 | *val2 = chan->scan_type.realbits - 1; |
| 384 | ret = IIO_VAL_FRACTIONAL_LOG2; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 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: |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 411 | ret = ads1015_set_scale(data, chan, val, val2); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 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 | |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 443 | static IIO_CONST_ATTR_NAMED(ads1015_scale_available, scale_available, |
| 444 | "3 2 1 0.5 0.25 0.125"); |
| 445 | static IIO_CONST_ATTR_NAMED(ads1115_scale_available, scale_available, |
| 446 | "0.1875 0.125 0.0625 0.03125 0.015625 0.007813"); |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 447 | |
| 448 | static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available, |
| 449 | sampling_frequency_available, "128 250 490 920 1600 2400 3300"); |
| 450 | static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available, |
| 451 | sampling_frequency_available, "8 16 32 64 128 250 475 860"); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 452 | |
| 453 | static struct attribute *ads1015_attributes[] = { |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 454 | &iio_const_attr_ads1015_scale_available.dev_attr.attr, |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 455 | &iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 456 | NULL, |
| 457 | }; |
| 458 | |
| 459 | static const struct attribute_group ads1015_attribute_group = { |
| 460 | .attrs = ads1015_attributes, |
| 461 | }; |
| 462 | |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 463 | static struct attribute *ads1115_attributes[] = { |
Akinobu Mita | 8d0e8e7 | 2017-07-21 00:24:18 +0900 | [diff] [blame] | 464 | &iio_const_attr_ads1115_scale_available.dev_attr.attr, |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 465 | &iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr, |
| 466 | NULL, |
| 467 | }; |
| 468 | |
| 469 | static const struct attribute_group ads1115_attribute_group = { |
| 470 | .attrs = ads1115_attributes, |
| 471 | }; |
| 472 | |
Bhumika Goyal | 99a22f0 | 2017-01-21 22:33:00 +0530 | [diff] [blame] | 473 | static const struct iio_info ads1015_info = { |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 474 | .driver_module = THIS_MODULE, |
| 475 | .read_raw = ads1015_read_raw, |
| 476 | .write_raw = ads1015_write_raw, |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 477 | .attrs = &ads1015_attribute_group, |
| 478 | }; |
| 479 | |
Bhumika Goyal | 99a22f0 | 2017-01-21 22:33:00 +0530 | [diff] [blame] | 480 | static const struct iio_info ads1115_info = { |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 481 | .driver_module = THIS_MODULE, |
| 482 | .read_raw = ads1015_read_raw, |
| 483 | .write_raw = ads1015_write_raw, |
| 484 | .attrs = &ads1115_attribute_group, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | #ifdef CONFIG_OF |
| 488 | static int ads1015_get_channels_config_of(struct i2c_client *client) |
| 489 | { |
Giorgio Dal Molin | 522caeb | 2016-08-16 20:43:37 +0200 | [diff] [blame] | 490 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 491 | struct ads1015_data *data = iio_priv(indio_dev); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 492 | struct device_node *node; |
| 493 | |
| 494 | if (!client->dev.of_node || |
| 495 | !of_get_next_child(client->dev.of_node, NULL)) |
| 496 | return -EINVAL; |
| 497 | |
| 498 | for_each_child_of_node(client->dev.of_node, node) { |
| 499 | u32 pval; |
| 500 | unsigned int channel; |
| 501 | unsigned int pga = ADS1015_DEFAULT_PGA; |
| 502 | unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE; |
| 503 | |
| 504 | if (of_property_read_u32(node, "reg", &pval)) { |
Rob Herring | 3921db4 | 2017-07-18 16:43:08 -0500 | [diff] [blame] | 505 | dev_err(&client->dev, "invalid reg on %pOF\n", |
| 506 | node); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 507 | continue; |
| 508 | } |
| 509 | |
| 510 | channel = pval; |
| 511 | if (channel >= ADS1015_CHANNELS) { |
| 512 | dev_err(&client->dev, |
Rob Herring | 3921db4 | 2017-07-18 16:43:08 -0500 | [diff] [blame] | 513 | "invalid channel index %d on %pOF\n", |
| 514 | channel, node); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 515 | continue; |
| 516 | } |
| 517 | |
| 518 | if (!of_property_read_u32(node, "ti,gain", &pval)) { |
| 519 | pga = pval; |
| 520 | if (pga > 6) { |
Rob Herring | 3921db4 | 2017-07-18 16:43:08 -0500 | [diff] [blame] | 521 | dev_err(&client->dev, "invalid gain on %pOF\n", |
| 522 | node); |
Wei Yongjun | 943bbe7 | 2016-08-26 14:31:50 +0000 | [diff] [blame] | 523 | of_node_put(node); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 524 | return -EINVAL; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (!of_property_read_u32(node, "ti,datarate", &pval)) { |
| 529 | data_rate = pval; |
| 530 | if (data_rate > 7) { |
| 531 | dev_err(&client->dev, |
Rob Herring | 3921db4 | 2017-07-18 16:43:08 -0500 | [diff] [blame] | 532 | "invalid data_rate on %pOF\n", |
| 533 | node); |
Wei Yongjun | 943bbe7 | 2016-08-26 14:31:50 +0000 | [diff] [blame] | 534 | of_node_put(node); |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 535 | return -EINVAL; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | data->channel_data[channel].pga = pga; |
| 540 | data->channel_data[channel].data_rate = data_rate; |
| 541 | } |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | #endif |
| 546 | |
| 547 | static void ads1015_get_channels_config(struct i2c_client *client) |
| 548 | { |
| 549 | unsigned int k; |
| 550 | |
| 551 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 552 | struct ads1015_data *data = iio_priv(indio_dev); |
| 553 | struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev); |
| 554 | |
| 555 | /* prefer platform data */ |
| 556 | if (pdata) { |
| 557 | memcpy(data->channel_data, pdata->channel_data, |
| 558 | sizeof(data->channel_data)); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | #ifdef CONFIG_OF |
| 563 | if (!ads1015_get_channels_config_of(client)) |
| 564 | return; |
| 565 | #endif |
| 566 | /* fallback on default configuration */ |
| 567 | for (k = 0; k < ADS1015_CHANNELS; ++k) { |
| 568 | data->channel_data[k].pga = ADS1015_DEFAULT_PGA; |
| 569 | data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | static int ads1015_probe(struct i2c_client *client, |
| 574 | const struct i2c_device_id *id) |
| 575 | { |
| 576 | struct iio_dev *indio_dev; |
| 577 | struct ads1015_data *data; |
| 578 | int ret; |
Javier Martinez Canillas | c172d22 | 2017-03-15 01:45:00 -0300 | [diff] [blame] | 579 | enum chip_ids chip; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 580 | |
| 581 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 582 | if (!indio_dev) |
| 583 | return -ENOMEM; |
| 584 | |
| 585 | data = iio_priv(indio_dev); |
| 586 | i2c_set_clientdata(client, indio_dev); |
| 587 | |
| 588 | mutex_init(&data->lock); |
| 589 | |
| 590 | indio_dev->dev.parent = &client->dev; |
Matt Ranostay | f5241db | 2016-06-30 19:33:50 -0700 | [diff] [blame] | 591 | indio_dev->dev.of_node = client->dev.of_node; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 592 | indio_dev->name = ADS1015_DRV_NAME; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 593 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 594 | |
Javier Martinez Canillas | c172d22 | 2017-03-15 01:45:00 -0300 | [diff] [blame] | 595 | if (client->dev.of_node) |
| 596 | chip = (enum chip_ids)of_device_get_match_data(&client->dev); |
| 597 | else |
| 598 | chip = id->driver_data; |
| 599 | switch (chip) { |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 600 | case ADS1015: |
| 601 | indio_dev->channels = ads1015_channels; |
| 602 | indio_dev->num_channels = ARRAY_SIZE(ads1015_channels); |
| 603 | indio_dev->info = &ads1015_info; |
| 604 | data->data_rate = (unsigned int *) &ads1015_data_rate; |
| 605 | break; |
| 606 | case ADS1115: |
| 607 | indio_dev->channels = ads1115_channels; |
| 608 | indio_dev->num_channels = ARRAY_SIZE(ads1115_channels); |
| 609 | indio_dev->info = &ads1115_info; |
| 610 | data->data_rate = (unsigned int *) &ads1115_data_rate; |
| 611 | break; |
| 612 | } |
| 613 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 614 | /* we need to keep this ABI the same as used by hwmon ADS1015 driver */ |
| 615 | ads1015_get_channels_config(client); |
| 616 | |
| 617 | data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config); |
| 618 | if (IS_ERR(data->regmap)) { |
| 619 | dev_err(&client->dev, "Failed to allocate register map\n"); |
| 620 | return PTR_ERR(data->regmap); |
| 621 | } |
| 622 | |
| 623 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 624 | ads1015_trigger_handler, |
| 625 | &ads1015_buffer_setup_ops); |
| 626 | if (ret < 0) { |
| 627 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); |
| 628 | return ret; |
| 629 | } |
Akinobu Mita | e8245c6 | 2017-07-21 00:24:19 +0900 | [diff] [blame] | 630 | |
| 631 | ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
| 632 | ADS1015_CFG_MOD_MASK, |
| 633 | ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT); |
| 634 | if (ret) |
| 635 | return ret; |
| 636 | |
Akinobu Mita | 73e3e3f | 2017-07-21 00:24:20 +0900 | [diff] [blame] | 637 | data->conv_invalid = true; |
| 638 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 639 | ret = pm_runtime_set_active(&client->dev); |
| 640 | if (ret) |
| 641 | goto err_buffer_cleanup; |
| 642 | pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS); |
| 643 | pm_runtime_use_autosuspend(&client->dev); |
| 644 | pm_runtime_enable(&client->dev); |
| 645 | |
| 646 | ret = iio_device_register(indio_dev); |
| 647 | if (ret < 0) { |
| 648 | dev_err(&client->dev, "Failed to register IIO device\n"); |
| 649 | goto err_buffer_cleanup; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | |
| 654 | err_buffer_cleanup: |
| 655 | iio_triggered_buffer_cleanup(indio_dev); |
| 656 | |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static int ads1015_remove(struct i2c_client *client) |
| 661 | { |
| 662 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 663 | struct ads1015_data *data = iio_priv(indio_dev); |
| 664 | |
| 665 | iio_device_unregister(indio_dev); |
| 666 | |
| 667 | pm_runtime_disable(&client->dev); |
| 668 | pm_runtime_set_suspended(&client->dev); |
| 669 | pm_runtime_put_noidle(&client->dev); |
| 670 | |
| 671 | iio_triggered_buffer_cleanup(indio_dev); |
| 672 | |
| 673 | /* power down single shot mode */ |
| 674 | return regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
| 675 | ADS1015_CFG_MOD_MASK, |
| 676 | ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT); |
| 677 | } |
| 678 | |
| 679 | #ifdef CONFIG_PM |
| 680 | static int ads1015_runtime_suspend(struct device *dev) |
| 681 | { |
| 682 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 683 | struct ads1015_data *data = iio_priv(indio_dev); |
| 684 | |
| 685 | return regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
| 686 | ADS1015_CFG_MOD_MASK, |
| 687 | ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT); |
| 688 | } |
| 689 | |
| 690 | static int ads1015_runtime_resume(struct device *dev) |
| 691 | { |
| 692 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 693 | struct ads1015_data *data = iio_priv(indio_dev); |
Akinobu Mita | 73e3e3f | 2017-07-21 00:24:20 +0900 | [diff] [blame] | 694 | int ret; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 695 | |
Akinobu Mita | 73e3e3f | 2017-07-21 00:24:20 +0900 | [diff] [blame] | 696 | ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 697 | ADS1015_CFG_MOD_MASK, |
| 698 | ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT); |
Akinobu Mita | 73e3e3f | 2017-07-21 00:24:20 +0900 | [diff] [blame] | 699 | if (!ret) |
| 700 | data->conv_invalid = true; |
| 701 | |
| 702 | return ret; |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 703 | } |
| 704 | #endif |
| 705 | |
| 706 | static const struct dev_pm_ops ads1015_pm_ops = { |
| 707 | SET_RUNTIME_PM_OPS(ads1015_runtime_suspend, |
| 708 | ads1015_runtime_resume, NULL) |
| 709 | }; |
| 710 | |
| 711 | static const struct i2c_device_id ads1015_id[] = { |
Matt Ranostay | ba35f11 | 2016-05-15 22:18:46 -0700 | [diff] [blame] | 712 | {"ads1015", ADS1015}, |
| 713 | {"ads1115", ADS1115}, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 714 | {} |
| 715 | }; |
| 716 | MODULE_DEVICE_TABLE(i2c, ads1015_id); |
| 717 | |
Javier Martinez Canillas | c172d22 | 2017-03-15 01:45:00 -0300 | [diff] [blame] | 718 | static const struct of_device_id ads1015_of_match[] = { |
| 719 | { |
| 720 | .compatible = "ti,ads1015", |
| 721 | .data = (void *)ADS1015 |
| 722 | }, |
| 723 | { |
| 724 | .compatible = "ti,ads1115", |
| 725 | .data = (void *)ADS1115 |
| 726 | }, |
| 727 | {} |
| 728 | }; |
| 729 | MODULE_DEVICE_TABLE(of, ads1015_of_match); |
| 730 | |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 731 | static struct i2c_driver ads1015_driver = { |
| 732 | .driver = { |
| 733 | .name = ADS1015_DRV_NAME, |
Javier Martinez Canillas | c172d22 | 2017-03-15 01:45:00 -0300 | [diff] [blame] | 734 | .of_match_table = ads1015_of_match, |
Daniel Baluta | ecc24e7 | 2016-02-11 15:49:54 +0200 | [diff] [blame] | 735 | .pm = &ads1015_pm_ops, |
| 736 | }, |
| 737 | .probe = ads1015_probe, |
| 738 | .remove = ads1015_remove, |
| 739 | .id_table = ads1015_id, |
| 740 | }; |
| 741 | |
| 742 | module_i2c_driver(ads1015_driver); |
| 743 | |
| 744 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); |
| 745 | MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver"); |
| 746 | MODULE_LICENSE("GPL v2"); |