| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Texas Instruments ADS1119 ADC driver. |
| 4 |
* |
| 5 |
* Copyright 2024 Toradex |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/bits.h> |
| 9 |
#include <linux/bitfield.h> |
| 10 |
#include <linux/completion.h> |
| 11 |
#include <linux/delay.h> |
| 12 |
#include <linux/dev_printk.h> |
| 13 |
#include <linux/err.h> |
| 14 |
#include <linux/gpio/consumer.h> |
| 15 |
#include <linux/interrupt.h> |
| 16 |
#include <linux/iopoll.h> |
| 17 |
#include <linux/i2c.h> |
| 18 |
#include <linux/kernel.h> |
| 19 |
#include <linux/math.h> |
| 20 |
#include <linux/module.h> |
| 21 |
#include <linux/pm_runtime.h> |
| 22 |
#include <linux/regulator/consumer.h> |
| 23 |
#include <linux/units.h> |
| 24 |
|
| 25 |
#include <linux/iio/iio.h> |
| 26 |
#include <linux/iio/buffer.h> |
| 27 |
#include <linux/iio/trigger.h> |
| 28 |
#include <linux/iio/triggered_buffer.h> |
| 29 |
#include <linux/iio/trigger_consumer.h> |
| 30 |
|
| 31 |
#define ADS1119_CMD_RESET 0x06 |
| 32 |
#define ADS1119_CMD_POWERDOWN 0x02 |
| 33 |
#define ADS1119_CMD_START_SYNC 0x08 |
| 34 |
#define ADS1119_CMD_RDATA 0x10 |
| 35 |
#define ADS1119_CMD_RREG_CONFIG 0x20 |
| 36 |
#define ADS1119_CMD_RREG_STATUS 0x24 |
| 37 |
#define ADS1119_CMD_WREG 0x40 |
| 38 |
|
| 39 |
#define ADS1119_CMD_RREG(reg) (0x20 | (reg) << 2) |
| 40 |
|
| 41 |
/* Config register */ |
| 42 |
#define ADS1119_REG_CONFIG 0x00 |
| 43 |
#define ADS1119_CONFIG_VREF_FIELD BIT(0) |
| 44 |
#define ADS1119_CONFIG_CM_FIELD BIT(1) |
| 45 |
#define ADS1119_CONFIG_DR_FIELD GENMASK(3, 2) |
| 46 |
#define ADS1119_CONFIG_GAIN_FIELD BIT(4) |
| 47 |
#define ADS1119_CONFIG_MUX_FIELD GENMASK(7, 5) |
| 48 |
|
| 49 |
#define ADS1119_VREF_INTERNAL 0 |
| 50 |
#define ADS1119_VREF_EXTERNAL 1 |
| 51 |
#define ADS1119_VREF_INTERNAL_VAL 2048000 |
| 52 |
|
| 53 |
#define ADS1119_CM_SINGLE 0 |
| 54 |
#define ADS1119_CM_CONTINUOUS 1 |
| 55 |
|
| 56 |
#define ADS1119_DR_20_SPS 0 |
| 57 |
#define ADS1119_DR_90_SPS 1 |
| 58 |
#define ADS1119_DR_330_SPS 2 |
| 59 |
#define ADS1119_DR_1000_SPS 3 |
| 60 |
|
| 61 |
#define ADS1119_GAIN_1 0 |
| 62 |
#define ADS1119_GAIN_4 1 |
| 63 |
|
| 64 |
#define ADS1119_MUX_AIN0_AIN1 0 |
| 65 |
#define ADS1119_MUX_AIN2_AIN3 1 |
| 66 |
#define ADS1119_MUX_AIN1_AIN2 2 |
| 67 |
#define ADS1119_MUX_AIN0 3 |
| 68 |
#define ADS1119_MUX_AIN1 4 |
| 69 |
#define ADS1119_MUX_AIN2 5 |
| 70 |
#define ADS1119_MUX_AIN3 6 |
| 71 |
#define ADS1119_MUX_SHORTED 7 |
| 72 |
|
| 73 |
/* Status register */ |
| 74 |
#define ADS1119_REG_STATUS 0x01 |
| 75 |
#define ADS1119_STATUS_DRDY_FIELD BIT(7) |
| 76 |
|
| 77 |
#define ADS1119_DEFAULT_GAIN 1 |
| 78 |
#define ADS1119_DEFAULT_DATARATE 20 |
| 79 |
|
| 80 |
#define ADS1119_SUSPEND_DELAY 2000 |
| 81 |
|
| 82 |
/* Timeout based on the minimum sample rate of 20 SPS (50000us) */ |
| 83 |
#define ADS1119_MAX_DRDY_TIMEOUT 85000 |
| 84 |
|
| 85 |
#define ADS1119_MAX_CHANNELS 7 |
| 86 |
#define ADS1119_MAX_SINGLE_CHANNELS 4 |
| 87 |
|
| 88 |
struct ads1119_channel_config { |
| 89 |
int gain; |
| 90 |
int datarate; |
| 91 |
int mux; |
| 92 |
}; |
| 93 |
|
| 94 |
struct ads1119_state { |
| 95 |
struct completion completion; |
| 96 |
struct i2c_client *client; |
| 97 |
struct gpio_desc *reset_gpio; |
| 98 |
struct iio_trigger *trig; |
| 99 |
struct ads1119_channel_config *channels_cfg; |
| 100 |
unsigned int num_channels_cfg; |
| 101 |
unsigned int cached_config; |
| 102 |
int vref_uV; |
| 103 |
}; |
| 104 |
|
| 105 |
static const char * const ads1119_power_supplies[] = { |
| 106 |
"avdd", "dvdd" |
| 107 |
}; |
| 108 |
|
| 109 |
static const int ads1119_available_datarates[] = { |
| 110 |
20, 90, 330, 1000, |
| 111 |
}; |
| 112 |
|
| 113 |
static const int ads1119_available_gains[] = { |
| 114 |
1, 1, |
| 115 |
1, 4, |
| 116 |
}; |
| 117 |
|
| 118 |
static int ads1119_upd_cfg_reg(struct ads1119_state *st, unsigned int fields, |
| 119 |
unsigned int val) |
| 120 |
{ |
| 121 |
unsigned int config = st->cached_config; |
| 122 |
int ret; |
| 123 |
|
| 124 |
config &= ~fields; |
| 125 |
config |= val; |
| 126 |
|
| 127 |
ret = i2c_smbus_write_byte_data(st->client, ADS1119_CMD_WREG, config); |
| 128 |
if (ret) |
| 129 |
return ret; |
| 130 |
|
| 131 |
st->cached_config = config; |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
static bool ads1119_data_ready(struct ads1119_state *st) |
| 137 |
{ |
| 138 |
int status; |
| 139 |
|
| 140 |
status = i2c_smbus_read_byte_data(st->client, ADS1119_CMD_RREG_STATUS); |
| 141 |
if (status < 0) |
| 142 |
return false; |
| 143 |
|
| 144 |
return FIELD_GET(ADS1119_STATUS_DRDY_FIELD, status); |
| 145 |
} |
| 146 |
|
| 147 |
static int ads1119_reset(struct ads1119_state *st) |
| 148 |
{ |
| 149 |
st->cached_config = 0; |
| 150 |
|
| 151 |
if (!st->reset_gpio) |
| 152 |
return i2c_smbus_write_byte(st->client, ADS1119_CMD_RESET); |
| 153 |
|
| 154 |
gpiod_set_value_cansleep(st->reset_gpio, 1); |
| 155 |
udelay(1); |
| 156 |
gpiod_set_value_cansleep(st->reset_gpio, 0); |
| 157 |
udelay(1); |
| 158 |
|
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
static int ads1119_set_conv_mode(struct ads1119_state *st, bool continuous) |
| 163 |
{ |
| 164 |
unsigned int mode; |
| 165 |
|
| 166 |
if (continuous) |
| 167 |
mode = ADS1119_CM_CONTINUOUS; |
| 168 |
else |
| 169 |
mode = ADS1119_CM_SINGLE; |
| 170 |
|
| 171 |
return ads1119_upd_cfg_reg(st, ADS1119_CONFIG_CM_FIELD, |
| 172 |
FIELD_PREP(ADS1119_CONFIG_CM_FIELD, mode)); |
| 173 |
} |
| 174 |
|
| 175 |
static int ads1119_get_hw_gain(int gain) |
| 176 |
{ |
| 177 |
if (gain == 4) |
| 178 |
return ADS1119_GAIN_4; |
| 179 |
else |
| 180 |
return ADS1119_GAIN_1; |
| 181 |
} |
| 182 |
|
| 183 |
static int ads1119_get_hw_datarate(int datarate) |
| 184 |
{ |
| 185 |
switch (datarate) { |
| 186 |
case 90: |
| 187 |
return ADS1119_DR_90_SPS; |
| 188 |
case 330: |
| 189 |
return ADS1119_DR_330_SPS; |
| 190 |
case 1000: |
| 191 |
return ADS1119_DR_1000_SPS; |
| 192 |
case 20: |
| 193 |
default: |
| 194 |
return ADS1119_DR_20_SPS; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
static int ads1119_configure_channel(struct ads1119_state *st, int mux, |
| 199 |
int gain, int datarate) |
| 200 |
{ |
| 201 |
int ret; |
| 202 |
|
| 203 |
ret = ads1119_upd_cfg_reg(st, ADS1119_CONFIG_MUX_FIELD, |
| 204 |
FIELD_PREP(ADS1119_CONFIG_MUX_FIELD, mux)); |
| 205 |
if (ret) |
| 206 |
return ret; |
| 207 |
|
| 208 |
ret = ads1119_upd_cfg_reg(st, ADS1119_CONFIG_GAIN_FIELD, |
| 209 |
FIELD_PREP(ADS1119_CONFIG_GAIN_FIELD, |
| 210 |
ads1119_get_hw_gain(gain))); |
| 211 |
if (ret) |
| 212 |
return ret; |
| 213 |
|
| 214 |
return ads1119_upd_cfg_reg(st, ADS1119_CONFIG_DR_FIELD, |
| 215 |
FIELD_PREP(ADS1119_CONFIG_DR_FIELD, |
| 216 |
ads1119_get_hw_datarate(datarate))); |
| 217 |
} |
| 218 |
|
| 219 |
static int ads1119_poll_data_ready(struct ads1119_state *st, |
| 220 |
struct iio_chan_spec const *chan) |
| 221 |
{ |
| 222 |
unsigned int datarate = st->channels_cfg[chan->address].datarate; |
| 223 |
unsigned long wait_time; |
| 224 |
bool data_ready; |
| 225 |
|
| 226 |
/* Poll 5 times more than the data rate */ |
| 227 |
wait_time = DIV_ROUND_CLOSEST(MICRO, 5 * datarate); |
| 228 |
|
| 229 |
return read_poll_timeout(ads1119_data_ready, data_ready, |
| 230 |
data_ready, wait_time, |
| 231 |
ADS1119_MAX_DRDY_TIMEOUT, false, st); |
| 232 |
} |
| 233 |
|
| 234 |
static int ads1119_read_data(struct ads1119_state *st, |
| 235 |
struct iio_chan_spec const *chan, |
| 236 |
unsigned int *val) |
| 237 |
{ |
| 238 |
unsigned int timeout; |
| 239 |
int ret = 0; |
| 240 |
|
| 241 |
timeout = msecs_to_jiffies(ADS1119_MAX_DRDY_TIMEOUT); |
| 242 |
|
| 243 |
if (!st->client->irq) { |
| 244 |
ret = ads1119_poll_data_ready(st, chan); |
| 245 |
if (ret) |
| 246 |
return ret; |
| 247 |
} else if (!wait_for_completion_timeout(&st->completion, timeout)) { |
| 248 |
return -ETIMEDOUT; |
| 249 |
} |
| 250 |
|
| 251 |
ret = i2c_smbus_read_word_swapped(st->client, ADS1119_CMD_RDATA); |
| 252 |
if (ret < 0) |
| 253 |
return ret; |
| 254 |
|
| 255 |
*val = ret; |
| 256 |
|
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
static int ads1119_single_conversion(struct ads1119_state *st, |
| 261 |
struct iio_chan_spec const *chan, |
| 262 |
int *val, |
| 263 |
bool calib_offset) |
| 264 |
{ |
| 265 |
struct device *dev = &st->client->dev; |
| 266 |
int mux = st->channels_cfg[chan->address].mux; |
| 267 |
int gain = st->channels_cfg[chan->address].gain; |
| 268 |
int datarate = st->channels_cfg[chan->address].datarate; |
| 269 |
unsigned int sample; |
| 270 |
int ret; |
| 271 |
|
| 272 |
if (calib_offset) |
| 273 |
mux = ADS1119_MUX_SHORTED; |
| 274 |
|
| 275 |
ret = pm_runtime_resume_and_get(dev); |
| 276 |
if (ret) |
| 277 |
return ret; |
| 278 |
|
| 279 |
ret = ads1119_configure_channel(st, mux, gain, datarate); |
| 280 |
if (ret) |
| 281 |
goto pdown; |
| 282 |
|
| 283 |
if (st->client->irq) |
| 284 |
reinit_completion(&st->completion); |
| 285 |
|
| 286 |
ret = i2c_smbus_write_byte(st->client, ADS1119_CMD_START_SYNC); |
| 287 |
if (ret) |
| 288 |
goto pdown; |
| 289 |
|
| 290 |
ret = ads1119_read_data(st, chan, &sample); |
| 291 |
if (ret) |
| 292 |
goto pdown; |
| 293 |
|
| 294 |
*val = sign_extend32(sample, chan->scan_type.realbits - 1); |
| 295 |
ret = IIO_VAL_INT; |
| 296 |
pdown: |
| 297 |
pm_runtime_put_autosuspend(dev); |
| 298 |
return ret; |
| 299 |
} |
| 300 |
|
| 301 |
static int ads1119_validate_datarate(struct ads1119_state *st, int datarate) |
| 302 |
{ |
| 303 |
switch (datarate) { |
| 304 |
case 20: |
| 305 |
case 90: |
| 306 |
case 330: |
| 307 |
case 1000: |
| 308 |
return datarate; |
| 309 |
default: |
| 310 |
return -EINVAL; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
static int ads1119_read_avail(struct iio_dev *indio_dev, |
| 315 |
struct iio_chan_spec const *chan, |
| 316 |
const int **vals, int *type, int *length, |
| 317 |
long mask) |
| 318 |
{ |
| 319 |
switch (mask) { |
| 320 |
case IIO_CHAN_INFO_SCALE: |
| 321 |
*type = IIO_VAL_FRACTIONAL; |
| 322 |
*vals = ads1119_available_gains; |
| 323 |
*length = ARRAY_SIZE(ads1119_available_gains); |
| 324 |
return IIO_AVAIL_LIST; |
| 325 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 326 |
*type = IIO_VAL_INT; |
| 327 |
*vals = ads1119_available_datarates; |
| 328 |
*length = ARRAY_SIZE(ads1119_available_datarates); |
| 329 |
return IIO_AVAIL_LIST; |
| 330 |
default: |
| 331 |
return -EINVAL; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
static int ads1119_read_raw(struct iio_dev *indio_dev, |
| 336 |
struct iio_chan_spec const *chan, int *val, |
| 337 |
int *val2, long mask) |
| 338 |
{ |
| 339 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 340 |
unsigned int index = chan->address; |
| 341 |
int ret; |
| 342 |
|
| 343 |
if (index >= st->num_channels_cfg) |
| 344 |
return -EINVAL; |
| 345 |
|
| 346 |
switch (mask) { |
| 347 |
case IIO_CHAN_INFO_RAW: |
| 348 |
if (!iio_device_claim_direct(indio_dev)) |
| 349 |
return -EBUSY; |
| 350 |
ret = ads1119_single_conversion(st, chan, val, false); |
| 351 |
iio_device_release_direct(indio_dev); |
| 352 |
return ret; |
| 353 |
case IIO_CHAN_INFO_OFFSET: |
| 354 |
if (!iio_device_claim_direct(indio_dev)) |
| 355 |
return -EBUSY; |
| 356 |
ret = ads1119_single_conversion(st, chan, val, true); |
| 357 |
iio_device_release_direct(indio_dev); |
| 358 |
return ret; |
| 359 |
case IIO_CHAN_INFO_SCALE: |
| 360 |
*val = st->vref_uV / 1000; |
| 361 |
*val /= st->channels_cfg[index].gain; |
| 362 |
*val2 = chan->scan_type.realbits - 1; |
| 363 |
return IIO_VAL_FRACTIONAL_LOG2; |
| 364 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 365 |
*val = st->channels_cfg[index].datarate; |
| 366 |
return IIO_VAL_INT; |
| 367 |
default: |
| 368 |
return -EINVAL; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
static int ads1119_write_raw(struct iio_dev *indio_dev, |
| 373 |
struct iio_chan_spec const *chan, int val, |
| 374 |
int val2, long mask) |
| 375 |
{ |
| 376 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 377 |
unsigned int index = chan->address; |
| 378 |
int ret; |
| 379 |
|
| 380 |
if (index >= st->num_channels_cfg) |
| 381 |
return -EINVAL; |
| 382 |
|
| 383 |
switch (mask) { |
| 384 |
case IIO_CHAN_INFO_SCALE: |
| 385 |
ret = MICRO / ((val * MICRO) + val2); |
| 386 |
if (ret != 1 && ret != 4) |
| 387 |
return -EINVAL; |
| 388 |
|
| 389 |
st->channels_cfg[index].gain = ret; |
| 390 |
return 0; |
| 391 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 392 |
ret = ads1119_validate_datarate(st, val); |
| 393 |
if (ret < 0) |
| 394 |
return ret; |
| 395 |
|
| 396 |
st->channels_cfg[index].datarate = ret; |
| 397 |
return 0; |
| 398 |
default: |
| 399 |
return -EINVAL; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
static int ads1119_debugfs_reg_access(struct iio_dev *indio_dev, |
| 404 |
unsigned int reg, unsigned int writeval, |
| 405 |
unsigned int *readval) |
| 406 |
{ |
| 407 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 408 |
int ret; |
| 409 |
|
| 410 |
if (reg > ADS1119_REG_STATUS) |
| 411 |
return -EINVAL; |
| 412 |
|
| 413 |
if (readval) { |
| 414 |
ret = i2c_smbus_read_byte_data(st->client, |
| 415 |
ADS1119_CMD_RREG(reg)); |
| 416 |
if (ret < 0) |
| 417 |
return ret; |
| 418 |
|
| 419 |
*readval = ret; |
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
if (reg > ADS1119_REG_CONFIG) |
| 424 |
return -EINVAL; |
| 425 |
|
| 426 |
return i2c_smbus_write_byte_data(st->client, ADS1119_CMD_WREG, |
| 427 |
writeval); |
| 428 |
} |
| 429 |
|
| 430 |
static const struct iio_info ads1119_info = { |
| 431 |
.read_avail = ads1119_read_avail, |
| 432 |
.read_raw = ads1119_read_raw, |
| 433 |
.write_raw = ads1119_write_raw, |
| 434 |
.debugfs_reg_access = ads1119_debugfs_reg_access, |
| 435 |
}; |
| 436 |
|
| 437 |
static int ads1119_triggered_buffer_preenable(struct iio_dev *indio_dev) |
| 438 |
{ |
| 439 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 440 |
struct device *dev = &st->client->dev; |
| 441 |
unsigned int index; |
| 442 |
int ret; |
| 443 |
|
| 444 |
index = find_first_bit(indio_dev->active_scan_mask, |
| 445 |
iio_get_masklength(indio_dev)); |
| 446 |
|
| 447 |
ret = ads1119_set_conv_mode(st, true); |
| 448 |
if (ret) |
| 449 |
return ret; |
| 450 |
|
| 451 |
ret = ads1119_configure_channel(st, |
| 452 |
st->channels_cfg[index].mux, |
| 453 |
st->channels_cfg[index].gain, |
| 454 |
st->channels_cfg[index].datarate); |
| 455 |
if (ret) |
| 456 |
return ret; |
| 457 |
|
| 458 |
ret = pm_runtime_resume_and_get(dev); |
| 459 |
if (ret) |
| 460 |
return ret; |
| 461 |
|
| 462 |
return i2c_smbus_write_byte(st->client, ADS1119_CMD_START_SYNC); |
| 463 |
} |
| 464 |
|
| 465 |
static int ads1119_triggered_buffer_postdisable(struct iio_dev *indio_dev) |
| 466 |
{ |
| 467 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 468 |
struct device *dev = &st->client->dev; |
| 469 |
int ret; |
| 470 |
|
| 471 |
ret = ads1119_set_conv_mode(st, false); |
| 472 |
if (ret) |
| 473 |
return ret; |
| 474 |
|
| 475 |
pm_runtime_put_autosuspend(dev); |
| 476 |
|
| 477 |
return 0; |
| 478 |
} |
| 479 |
|
| 480 |
static const struct iio_buffer_setup_ops ads1119_buffer_setup_ops = { |
| 481 |
.preenable = ads1119_triggered_buffer_preenable, |
| 482 |
.postdisable = ads1119_triggered_buffer_postdisable, |
| 483 |
.validate_scan_mask = &iio_validate_scan_mask_onehot, |
| 484 |
}; |
| 485 |
|
| 486 |
static const struct iio_trigger_ops ads1119_trigger_ops = { |
| 487 |
.validate_device = &iio_trigger_validate_own_device, |
| 488 |
}; |
| 489 |
|
| 490 |
static irqreturn_t ads1119_irq_handler(int irq, void *dev_id) |
| 491 |
{ |
| 492 |
struct iio_dev *indio_dev = dev_id; |
| 493 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 494 |
|
| 495 |
if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) |
| 496 |
iio_trigger_poll(indio_dev->trig); |
| 497 |
else |
| 498 |
complete(&st->completion); |
| 499 |
|
| 500 |
return IRQ_HANDLED; |
| 501 |
} |
| 502 |
|
| 503 |
static irqreturn_t ads1119_trigger_handler(int irq, void *private) |
| 504 |
{ |
| 505 |
struct iio_poll_func *pf = private; |
| 506 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 507 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 508 |
struct { |
| 509 |
s16 sample; |
| 510 |
aligned_s64 timestamp; |
| 511 |
} scan = { }; |
| 512 |
unsigned int index; |
| 513 |
int ret; |
| 514 |
|
| 515 |
if (!iio_trigger_using_own(indio_dev)) { |
| 516 |
index = find_first_bit(indio_dev->active_scan_mask, |
| 517 |
iio_get_masklength(indio_dev)); |
| 518 |
|
| 519 |
ret = ads1119_poll_data_ready(st, &indio_dev->channels[index]); |
| 520 |
if (ret) { |
| 521 |
dev_err(&st->client->dev, |
| 522 |
"Failed to poll data on trigger (%d)\n", ret); |
| 523 |
goto done; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
ret = i2c_smbus_read_word_swapped(st->client, ADS1119_CMD_RDATA); |
| 528 |
if (ret < 0) { |
| 529 |
dev_err(&st->client->dev, |
| 530 |
"Failed to read data on trigger (%d)\n", ret); |
| 531 |
goto done; |
| 532 |
} |
| 533 |
|
| 534 |
scan.sample = ret; |
| 535 |
|
| 536 |
iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), |
| 537 |
iio_get_time_ns(indio_dev)); |
| 538 |
done: |
| 539 |
iio_trigger_notify_done(indio_dev->trig); |
| 540 |
return IRQ_HANDLED; |
| 541 |
} |
| 542 |
|
| 543 |
static int ads1119_init(struct ads1119_state *st, bool vref_external) |
| 544 |
{ |
| 545 |
int ret; |
| 546 |
|
| 547 |
ret = ads1119_reset(st); |
| 548 |
if (ret) |
| 549 |
return ret; |
| 550 |
|
| 551 |
if (vref_external) |
| 552 |
return ads1119_upd_cfg_reg(st, |
| 553 |
ADS1119_CONFIG_VREF_FIELD, |
| 554 |
FIELD_PREP(ADS1119_CONFIG_VREF_FIELD, |
| 555 |
ADS1119_VREF_EXTERNAL)); |
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
static int ads1119_map_analog_inputs_mux(int ain_pos, int ain_neg, |
| 560 |
bool differential) |
| 561 |
{ |
| 562 |
if (ain_pos >= ADS1119_MAX_SINGLE_CHANNELS) |
| 563 |
return -EINVAL; |
| 564 |
|
| 565 |
if (!differential) |
| 566 |
return ADS1119_MUX_AIN0 + ain_pos; |
| 567 |
|
| 568 |
if (ain_pos == 0 && ain_neg == 1) |
| 569 |
return ADS1119_MUX_AIN0_AIN1; |
| 570 |
else if (ain_pos == 1 && ain_neg == 2) |
| 571 |
return ADS1119_MUX_AIN1_AIN2; |
| 572 |
else if (ain_pos == 2 && ain_neg == 3) |
| 573 |
return ADS1119_MUX_AIN2_AIN3; |
| 574 |
|
| 575 |
return -EINVAL; |
| 576 |
} |
| 577 |
|
| 578 |
static int ads1119_alloc_and_config_channels(struct iio_dev *indio_dev) |
| 579 |
{ |
| 580 |
const struct iio_chan_spec ads1119_channel = |
| 581 |
(const struct iio_chan_spec) { |
| 582 |
.type = IIO_VOLTAGE, |
| 583 |
.indexed = 1, |
| 584 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 585 |
BIT(IIO_CHAN_INFO_SCALE) | |
| 586 |
BIT(IIO_CHAN_INFO_OFFSET) | |
| 587 |
BIT(IIO_CHAN_INFO_SAMP_FREQ), |
| 588 |
.info_mask_shared_by_all_available = |
| 589 |
BIT(IIO_CHAN_INFO_SCALE) | |
| 590 |
BIT(IIO_CHAN_INFO_SAMP_FREQ), |
| 591 |
.scan_type = { |
| 592 |
.sign = 's', |
| 593 |
.realbits = 16, |
| 594 |
.storagebits = 16, |
| 595 |
.endianness = IIO_CPU, |
| 596 |
}, |
| 597 |
}; |
| 598 |
const struct iio_chan_spec ads1119_ts = IIO_CHAN_SOFT_TIMESTAMP(0); |
| 599 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 600 |
struct iio_chan_spec *iio_channels, *chan; |
| 601 |
struct device *dev = &st->client->dev; |
| 602 |
unsigned int num_channels, i; |
| 603 |
bool differential; |
| 604 |
u32 ain[2]; |
| 605 |
int ret; |
| 606 |
|
| 607 |
st->num_channels_cfg = device_get_child_node_count(dev); |
| 608 |
if (st->num_channels_cfg > ADS1119_MAX_CHANNELS) |
| 609 |
return dev_err_probe(dev, -EINVAL, |
| 610 |
"Too many channels %d, max is %d\n", |
| 611 |
st->num_channels_cfg, |
| 612 |
ADS1119_MAX_CHANNELS); |
| 613 |
|
| 614 |
st->channels_cfg = devm_kcalloc(dev, st->num_channels_cfg, |
| 615 |
sizeof(*st->channels_cfg), GFP_KERNEL); |
| 616 |
if (!st->channels_cfg) |
| 617 |
return -ENOMEM; |
| 618 |
|
| 619 |
/* Allocate one more iio channel for the timestamp */ |
| 620 |
num_channels = st->num_channels_cfg + 1; |
| 621 |
iio_channels = devm_kcalloc(dev, num_channels, sizeof(*iio_channels), |
| 622 |
GFP_KERNEL); |
| 623 |
if (!iio_channels) |
| 624 |
return -ENOMEM; |
| 625 |
|
| 626 |
i = 0; |
| 627 |
|
| 628 |
device_for_each_child_node_scoped(dev, child) { |
| 629 |
chan = &iio_channels[i]; |
| 630 |
|
| 631 |
differential = fwnode_property_present(child, "diff-channels"); |
| 632 |
if (differential) |
| 633 |
ret = fwnode_property_read_u32_array(child, |
| 634 |
"diff-channels", |
| 635 |
ain, 2); |
| 636 |
else |
| 637 |
ret = fwnode_property_read_u32(child, "single-channel", |
| 638 |
&ain[0]); |
| 639 |
|
| 640 |
if (ret) |
| 641 |
return dev_err_probe(dev, ret, |
| 642 |
"Failed to get channel property\n"); |
| 643 |
|
| 644 |
ret = ads1119_map_analog_inputs_mux(ain[0], ain[1], |
| 645 |
differential); |
| 646 |
if (ret < 0) |
| 647 |
return dev_err_probe(dev, ret, |
| 648 |
"Invalid channel value\n"); |
| 649 |
|
| 650 |
st->channels_cfg[i].mux = ret; |
| 651 |
st->channels_cfg[i].gain = ADS1119_DEFAULT_GAIN; |
| 652 |
st->channels_cfg[i].datarate = ADS1119_DEFAULT_DATARATE; |
| 653 |
|
| 654 |
*chan = ads1119_channel; |
| 655 |
chan->channel = ain[0]; |
| 656 |
chan->address = i; |
| 657 |
chan->scan_index = i; |
| 658 |
|
| 659 |
if (differential) { |
| 660 |
chan->channel2 = ain[1]; |
| 661 |
chan->differential = 1; |
| 662 |
} |
| 663 |
|
| 664 |
dev_dbg(dev, "channel: index %d, mux %d\n", i, |
| 665 |
st->channels_cfg[i].mux); |
| 666 |
|
| 667 |
i++; |
| 668 |
} |
| 669 |
|
| 670 |
iio_channels[i] = ads1119_ts; |
| 671 |
iio_channels[i].address = i; |
| 672 |
iio_channels[i].scan_index = i; |
| 673 |
|
| 674 |
indio_dev->channels = iio_channels; |
| 675 |
indio_dev->num_channels = num_channels; |
| 676 |
|
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
static void ads1119_powerdown(void *data) |
| 681 |
{ |
| 682 |
struct ads1119_state *st = data; |
| 683 |
|
| 684 |
i2c_smbus_write_byte(st->client, ADS1119_CMD_POWERDOWN); |
| 685 |
} |
| 686 |
|
| 687 |
static int ads1119_probe(struct i2c_client *client) |
| 688 |
{ |
| 689 |
struct iio_dev *indio_dev; |
| 690 |
struct ads1119_state *st; |
| 691 |
struct device *dev = &client->dev; |
| 692 |
bool vref_external = true; |
| 693 |
int ret; |
| 694 |
|
| 695 |
indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); |
| 696 |
if (!indio_dev) |
| 697 |
return -ENOMEM; |
| 698 |
|
| 699 |
st = iio_priv(indio_dev); |
| 700 |
st->client = client; |
| 701 |
|
| 702 |
indio_dev->name = "ads1119"; |
| 703 |
indio_dev->info = &ads1119_info; |
| 704 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 705 |
|
| 706 |
i2c_set_clientdata(client, indio_dev); |
| 707 |
|
| 708 |
ret = devm_regulator_bulk_get_enable(dev, |
| 709 |
ARRAY_SIZE(ads1119_power_supplies), |
| 710 |
ads1119_power_supplies); |
| 711 |
if (ret) |
| 712 |
return dev_err_probe(dev, ret, |
| 713 |
"Failed to get and enable supplies\n"); |
| 714 |
|
| 715 |
st->vref_uV = devm_regulator_get_enable_read_voltage(dev, "vref"); |
| 716 |
if (st->vref_uV == -ENODEV) { |
| 717 |
vref_external = false; |
| 718 |
st->vref_uV = ADS1119_VREF_INTERNAL_VAL; |
| 719 |
} else if (st->vref_uV < 0) { |
| 720 |
return dev_err_probe(dev, st->vref_uV, "Failed to get vref\n"); |
| 721 |
} |
| 722 |
|
| 723 |
st->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); |
| 724 |
if (IS_ERR(st->reset_gpio)) |
| 725 |
return dev_err_probe(dev, PTR_ERR(st->reset_gpio), |
| 726 |
"Failed to get reset gpio\n"); |
| 727 |
|
| 728 |
ret = ads1119_alloc_and_config_channels(indio_dev); |
| 729 |
if (ret) |
| 730 |
return ret; |
| 731 |
|
| 732 |
init_completion(&st->completion); |
| 733 |
|
| 734 |
ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, |
| 735 |
ads1119_trigger_handler, |
| 736 |
&ads1119_buffer_setup_ops); |
| 737 |
if (ret) |
| 738 |
return dev_err_probe(dev, ret, "Failed to setup IIO buffer\n"); |
| 739 |
|
| 740 |
if (client->irq > 0) { |
| 741 |
ret = devm_request_irq(dev, client->irq, ads1119_irq_handler, |
| 742 |
IRQF_NO_THREAD, "ads1119", indio_dev); |
| 743 |
if (ret) |
| 744 |
return dev_err_probe(dev, ret, |
| 745 |
"Failed to allocate irq\n"); |
| 746 |
|
| 747 |
st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", |
| 748 |
indio_dev->name, |
| 749 |
iio_device_id(indio_dev)); |
| 750 |
if (!st->trig) |
| 751 |
return -ENOMEM; |
| 752 |
|
| 753 |
st->trig->ops = &ads1119_trigger_ops; |
| 754 |
iio_trigger_set_drvdata(st->trig, indio_dev); |
| 755 |
|
| 756 |
ret = devm_iio_trigger_register(dev, st->trig); |
| 757 |
if (ret) |
| 758 |
return dev_err_probe(dev, ret, |
| 759 |
"Failed to register IIO trigger\n"); |
| 760 |
} |
| 761 |
|
| 762 |
ret = ads1119_init(st, vref_external); |
| 763 |
if (ret) |
| 764 |
return dev_err_probe(dev, ret, |
| 765 |
"Failed to initialize device\n"); |
| 766 |
|
| 767 |
pm_runtime_set_autosuspend_delay(dev, ADS1119_SUSPEND_DELAY); |
| 768 |
pm_runtime_use_autosuspend(dev); |
| 769 |
pm_runtime_mark_last_busy(dev); |
| 770 |
pm_runtime_set_active(dev); |
| 771 |
|
| 772 |
ret = devm_pm_runtime_enable(dev); |
| 773 |
if (ret) |
| 774 |
return dev_err_probe(dev, ret, "Failed to enable pm runtime\n"); |
| 775 |
|
| 776 |
ret = devm_add_action_or_reset(dev, ads1119_powerdown, st); |
| 777 |
if (ret) |
| 778 |
return ret; |
| 779 |
|
| 780 |
return devm_iio_device_register(dev, indio_dev); |
| 781 |
} |
| 782 |
|
| 783 |
static int ads1119_runtime_suspend(struct device *dev) |
| 784 |
{ |
| 785 |
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 786 |
struct ads1119_state *st = iio_priv(indio_dev); |
| 787 |
|
| 788 |
return i2c_smbus_write_byte(st->client, ADS1119_CMD_POWERDOWN); |
| 789 |
} |
| 790 |
|
| 791 |
/* |
| 792 |
* The ADS1119 does not require a resume function because it automatically |
| 793 |
* powers on after a reset. |
| 794 |
* After a power down command, the ADS1119 can still communicate but turns off |
| 795 |
* its analog parts. To resume from power down, the device will power up again |
| 796 |
* upon receiving a start/sync command. |
| 797 |
*/ |
| 798 |
static DEFINE_RUNTIME_DEV_PM_OPS(ads1119_pm_ops, ads1119_runtime_suspend, |
| 799 |
NULL, NULL); |
| 800 |
|
| 801 |
static const struct of_device_id __maybe_unused ads1119_of_match[] = { |
| 802 |
{ .compatible = "ti,ads1119" }, |
| 803 |
{ } |
| 804 |
}; |
| 805 |
MODULE_DEVICE_TABLE(of, ads1119_of_match); |
| 806 |
|
| 807 |
static const struct i2c_device_id ads1119_id[] = { |
| 808 |
{ "ads1119" }, |
| 809 |
{ } |
| 810 |
}; |
| 811 |
MODULE_DEVICE_TABLE(i2c, ads1119_id); |
| 812 |
|
| 813 |
static struct i2c_driver ads1119_driver = { |
| 814 |
.driver = { |
| 815 |
.name = "ads1119", |
| 816 |
.of_match_table = ads1119_of_match, |
| 817 |
.pm = pm_ptr(&ads1119_pm_ops), |
| 818 |
}, |
| 819 |
.probe = ads1119_probe, |
| 820 |
.id_table = ads1119_id, |
| 821 |
}; |
| 822 |
module_i2c_driver(ads1119_driver); |
| 823 |
|
| 824 |
MODULE_AUTHOR("João Paulo Gonçalves <[email protected]>"); |
| 825 |
MODULE_DESCRIPTION("Texas Instruments ADS1119 ADC Driver"); |
| 826 |
MODULE_LICENSE("GPL"); |