b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* TI ADS124S0X chip family driver |
| 3 | * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ |
| 4 | */ |
| 5 | |
| 6 | #include <linux/err.h> |
| 7 | #include <linux/delay.h> |
| 8 | #include <linux/device.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/of_gpio.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/sysfs.h> |
| 15 | |
| 16 | #include <linux/gpio/consumer.h> |
| 17 | #include <linux/spi/spi.h> |
| 18 | |
| 19 | #include <linux/iio/iio.h> |
| 20 | #include <linux/iio/buffer.h> |
| 21 | #include <linux/iio/trigger_consumer.h> |
| 22 | #include <linux/iio/triggered_buffer.h> |
| 23 | #include <linux/iio/sysfs.h> |
| 24 | |
| 25 | /* Commands */ |
| 26 | #define ADS124S08_CMD_NOP 0x00 |
| 27 | #define ADS124S08_CMD_WAKEUP 0x02 |
| 28 | #define ADS124S08_CMD_PWRDWN 0x04 |
| 29 | #define ADS124S08_CMD_RESET 0x06 |
| 30 | #define ADS124S08_CMD_START 0x08 |
| 31 | #define ADS124S08_CMD_STOP 0x0a |
| 32 | #define ADS124S08_CMD_SYOCAL 0x16 |
| 33 | #define ADS124S08_CMD_SYGCAL 0x17 |
| 34 | #define ADS124S08_CMD_SFOCAL 0x19 |
| 35 | #define ADS124S08_CMD_RDATA 0x12 |
| 36 | #define ADS124S08_CMD_RREG 0x20 |
| 37 | #define ADS124S08_CMD_WREG 0x40 |
| 38 | |
| 39 | /* Registers */ |
| 40 | #define ADS124S08_ID_REG 0x00 |
| 41 | #define ADS124S08_STATUS 0x01 |
| 42 | #define ADS124S08_INPUT_MUX 0x02 |
| 43 | #define ADS124S08_PGA 0x03 |
| 44 | #define ADS124S08_DATA_RATE 0x04 |
| 45 | #define ADS124S08_REF 0x05 |
| 46 | #define ADS124S08_IDACMAG 0x06 |
| 47 | #define ADS124S08_IDACMUX 0x07 |
| 48 | #define ADS124S08_VBIAS 0x08 |
| 49 | #define ADS124S08_SYS 0x09 |
| 50 | #define ADS124S08_OFCAL0 0x0a |
| 51 | #define ADS124S08_OFCAL1 0x0b |
| 52 | #define ADS124S08_OFCAL2 0x0c |
| 53 | #define ADS124S08_FSCAL0 0x0d |
| 54 | #define ADS124S08_FSCAL1 0x0e |
| 55 | #define ADS124S08_FSCAL2 0x0f |
| 56 | #define ADS124S08_GPIODAT 0x10 |
| 57 | #define ADS124S08_GPIOCON 0x11 |
| 58 | |
| 59 | /* ADS124S0x common channels */ |
| 60 | #define ADS124S08_AIN0 0x00 |
| 61 | #define ADS124S08_AIN1 0x01 |
| 62 | #define ADS124S08_AIN2 0x02 |
| 63 | #define ADS124S08_AIN3 0x03 |
| 64 | #define ADS124S08_AIN4 0x04 |
| 65 | #define ADS124S08_AIN5 0x05 |
| 66 | #define ADS124S08_AINCOM 0x0c |
| 67 | /* ADS124S08 only channels */ |
| 68 | #define ADS124S08_AIN6 0x06 |
| 69 | #define ADS124S08_AIN7 0x07 |
| 70 | #define ADS124S08_AIN8 0x08 |
| 71 | #define ADS124S08_AIN9 0x09 |
| 72 | #define ADS124S08_AIN10 0x0a |
| 73 | #define ADS124S08_AIN11 0x0b |
| 74 | #define ADS124S08_MAX_CHANNELS 12 |
| 75 | |
| 76 | #define ADS124S08_POS_MUX_SHIFT 0x04 |
| 77 | #define ADS124S08_INT_REF 0x09 |
| 78 | |
| 79 | #define ADS124S08_START_REG_MASK 0x1f |
| 80 | #define ADS124S08_NUM_BYTES_MASK 0x1f |
| 81 | |
| 82 | #define ADS124S08_START_CONV 0x01 |
| 83 | #define ADS124S08_STOP_CONV 0x00 |
| 84 | |
| 85 | enum ads124s_id { |
| 86 | ADS124S08_ID, |
| 87 | ADS124S06_ID, |
| 88 | }; |
| 89 | |
| 90 | struct ads124s_chip_info { |
| 91 | const struct iio_chan_spec *channels; |
| 92 | unsigned int num_channels; |
| 93 | }; |
| 94 | |
| 95 | struct ads124s_private { |
| 96 | const struct ads124s_chip_info *chip_info; |
| 97 | struct gpio_desc *reset_gpio; |
| 98 | struct spi_device *spi; |
| 99 | struct mutex lock; |
| 100 | /* |
| 101 | * Used to correctly align data. |
| 102 | * Ensure timestamp is naturally aligned. |
| 103 | * Note that the full buffer length may not be needed if not |
| 104 | * all channels are enabled, as long as the alignment of the |
| 105 | * timestamp is maintained. |
| 106 | */ |
| 107 | u32 buffer[ADS124S08_MAX_CHANNELS + sizeof(s64)/sizeof(u32)] __aligned(8); |
| 108 | u8 data[5] ____cacheline_aligned; |
| 109 | }; |
| 110 | |
| 111 | #define ADS124S08_CHAN(index) \ |
| 112 | { \ |
| 113 | .type = IIO_VOLTAGE, \ |
| 114 | .indexed = 1, \ |
| 115 | .channel = index, \ |
| 116 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 117 | .scan_index = index, \ |
| 118 | .scan_type = { \ |
| 119 | .sign = 'u', \ |
| 120 | .realbits = 32, \ |
| 121 | .storagebits = 32, \ |
| 122 | }, \ |
| 123 | } |
| 124 | |
| 125 | static const struct iio_chan_spec ads124s06_channels[] = { |
| 126 | ADS124S08_CHAN(0), |
| 127 | ADS124S08_CHAN(1), |
| 128 | ADS124S08_CHAN(2), |
| 129 | ADS124S08_CHAN(3), |
| 130 | ADS124S08_CHAN(4), |
| 131 | ADS124S08_CHAN(5), |
| 132 | }; |
| 133 | |
| 134 | static const struct iio_chan_spec ads124s08_channels[] = { |
| 135 | ADS124S08_CHAN(0), |
| 136 | ADS124S08_CHAN(1), |
| 137 | ADS124S08_CHAN(2), |
| 138 | ADS124S08_CHAN(3), |
| 139 | ADS124S08_CHAN(4), |
| 140 | ADS124S08_CHAN(5), |
| 141 | ADS124S08_CHAN(6), |
| 142 | ADS124S08_CHAN(7), |
| 143 | ADS124S08_CHAN(8), |
| 144 | ADS124S08_CHAN(9), |
| 145 | ADS124S08_CHAN(10), |
| 146 | ADS124S08_CHAN(11), |
| 147 | }; |
| 148 | |
| 149 | static const struct ads124s_chip_info ads124s_chip_info_tbl[] = { |
| 150 | [ADS124S08_ID] = { |
| 151 | .channels = ads124s08_channels, |
| 152 | .num_channels = ARRAY_SIZE(ads124s08_channels), |
| 153 | }, |
| 154 | [ADS124S06_ID] = { |
| 155 | .channels = ads124s06_channels, |
| 156 | .num_channels = ARRAY_SIZE(ads124s06_channels), |
| 157 | }, |
| 158 | }; |
| 159 | |
| 160 | static int ads124s_write_cmd(struct iio_dev *indio_dev, u8 command) |
| 161 | { |
| 162 | struct ads124s_private *priv = iio_priv(indio_dev); |
| 163 | |
| 164 | priv->data[0] = command; |
| 165 | |
| 166 | return spi_write(priv->spi, &priv->data[0], 1); |
| 167 | } |
| 168 | |
| 169 | static int ads124s_write_reg(struct iio_dev *indio_dev, u8 reg, u8 data) |
| 170 | { |
| 171 | struct ads124s_private *priv = iio_priv(indio_dev); |
| 172 | |
| 173 | priv->data[0] = ADS124S08_CMD_WREG | reg; |
| 174 | priv->data[1] = 0x0; |
| 175 | priv->data[2] = data; |
| 176 | |
| 177 | return spi_write(priv->spi, &priv->data[0], 3); |
| 178 | } |
| 179 | |
| 180 | static int ads124s_reset(struct iio_dev *indio_dev) |
| 181 | { |
| 182 | struct ads124s_private *priv = iio_priv(indio_dev); |
| 183 | |
| 184 | if (priv->reset_gpio) { |
| 185 | gpiod_set_value_cansleep(priv->reset_gpio, 0); |
| 186 | udelay(200); |
| 187 | gpiod_set_value_cansleep(priv->reset_gpio, 1); |
| 188 | } else { |
| 189 | return ads124s_write_cmd(indio_dev, ADS124S08_CMD_RESET); |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | }; |
| 194 | |
| 195 | static int ads124s_read(struct iio_dev *indio_dev, unsigned int chan) |
| 196 | { |
| 197 | struct ads124s_private *priv = iio_priv(indio_dev); |
| 198 | int ret; |
| 199 | u32 tmp; |
| 200 | struct spi_transfer t[] = { |
| 201 | { |
| 202 | .tx_buf = &priv->data[0], |
| 203 | .len = 4, |
| 204 | .cs_change = 1, |
| 205 | }, { |
| 206 | .tx_buf = &priv->data[1], |
| 207 | .rx_buf = &priv->data[1], |
| 208 | .len = 4, |
| 209 | }, |
| 210 | }; |
| 211 | |
| 212 | priv->data[0] = ADS124S08_CMD_RDATA; |
| 213 | memset(&priv->data[1], ADS124S08_CMD_NOP, sizeof(priv->data) - 1); |
| 214 | |
| 215 | ret = spi_sync_transfer(priv->spi, t, ARRAY_SIZE(t)); |
| 216 | if (ret < 0) |
| 217 | return ret; |
| 218 | |
| 219 | tmp = priv->data[2] << 16 | priv->data[3] << 8 | priv->data[4]; |
| 220 | |
| 221 | return tmp; |
| 222 | } |
| 223 | |
| 224 | static int ads124s_read_raw(struct iio_dev *indio_dev, |
| 225 | struct iio_chan_spec const *chan, |
| 226 | int *val, int *val2, long m) |
| 227 | { |
| 228 | struct ads124s_private *priv = iio_priv(indio_dev); |
| 229 | int ret; |
| 230 | |
| 231 | mutex_lock(&priv->lock); |
| 232 | switch (m) { |
| 233 | case IIO_CHAN_INFO_RAW: |
| 234 | ret = ads124s_write_reg(indio_dev, ADS124S08_INPUT_MUX, |
| 235 | chan->channel); |
| 236 | if (ret) { |
| 237 | dev_err(&priv->spi->dev, "Set ADC CH failed\n"); |
| 238 | goto out; |
| 239 | } |
| 240 | |
| 241 | ret = ads124s_write_cmd(indio_dev, ADS124S08_START_CONV); |
| 242 | if (ret) { |
| 243 | dev_err(&priv->spi->dev, "Start conversions failed\n"); |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | ret = ads124s_read(indio_dev, chan->channel); |
| 248 | if (ret < 0) { |
| 249 | dev_err(&priv->spi->dev, "Read ADC failed\n"); |
| 250 | goto out; |
| 251 | } |
| 252 | |
| 253 | *val = ret; |
| 254 | |
| 255 | ret = ads124s_write_cmd(indio_dev, ADS124S08_STOP_CONV); |
| 256 | if (ret) { |
| 257 | dev_err(&priv->spi->dev, "Stop conversions failed\n"); |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | ret = IIO_VAL_INT; |
| 262 | break; |
| 263 | default: |
| 264 | ret = -EINVAL; |
| 265 | break; |
| 266 | } |
| 267 | out: |
| 268 | mutex_unlock(&priv->lock); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | static const struct iio_info ads124s_info = { |
| 273 | .read_raw = &ads124s_read_raw, |
| 274 | }; |
| 275 | |
| 276 | static irqreturn_t ads124s_trigger_handler(int irq, void *p) |
| 277 | { |
| 278 | struct iio_poll_func *pf = p; |
| 279 | struct iio_dev *indio_dev = pf->indio_dev; |
| 280 | struct ads124s_private *priv = iio_priv(indio_dev); |
| 281 | int scan_index, j = 0; |
| 282 | int ret; |
| 283 | |
| 284 | for_each_set_bit(scan_index, indio_dev->active_scan_mask, |
| 285 | indio_dev->masklength) { |
| 286 | ret = ads124s_write_reg(indio_dev, ADS124S08_INPUT_MUX, |
| 287 | scan_index); |
| 288 | if (ret) |
| 289 | dev_err(&priv->spi->dev, "Set ADC CH failed\n"); |
| 290 | |
| 291 | ret = ads124s_write_cmd(indio_dev, ADS124S08_START_CONV); |
| 292 | if (ret) |
| 293 | dev_err(&priv->spi->dev, "Start ADC conversions failed\n"); |
| 294 | |
| 295 | priv->buffer[j] = ads124s_read(indio_dev, scan_index); |
| 296 | ret = ads124s_write_cmd(indio_dev, ADS124S08_STOP_CONV); |
| 297 | if (ret) |
| 298 | dev_err(&priv->spi->dev, "Stop ADC conversions failed\n"); |
| 299 | |
| 300 | j++; |
| 301 | } |
| 302 | |
| 303 | iio_push_to_buffers_with_timestamp(indio_dev, priv->buffer, |
| 304 | pf->timestamp); |
| 305 | |
| 306 | iio_trigger_notify_done(indio_dev->trig); |
| 307 | |
| 308 | return IRQ_HANDLED; |
| 309 | } |
| 310 | |
| 311 | static int ads124s_probe(struct spi_device *spi) |
| 312 | { |
| 313 | struct ads124s_private *ads124s_priv; |
| 314 | struct iio_dev *indio_dev; |
| 315 | const struct spi_device_id *spi_id = spi_get_device_id(spi); |
| 316 | int ret; |
| 317 | |
| 318 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*ads124s_priv)); |
| 319 | if (indio_dev == NULL) |
| 320 | return -ENOMEM; |
| 321 | |
| 322 | ads124s_priv = iio_priv(indio_dev); |
| 323 | |
| 324 | ads124s_priv->reset_gpio = devm_gpiod_get_optional(&spi->dev, |
| 325 | "reset", GPIOD_OUT_LOW); |
| 326 | if (IS_ERR(ads124s_priv->reset_gpio)) |
| 327 | dev_info(&spi->dev, "Reset GPIO not defined\n"); |
| 328 | |
| 329 | ads124s_priv->chip_info = &ads124s_chip_info_tbl[spi_id->driver_data]; |
| 330 | |
| 331 | spi_set_drvdata(spi, indio_dev); |
| 332 | |
| 333 | ads124s_priv->spi = spi; |
| 334 | |
| 335 | indio_dev->name = spi_id->name; |
| 336 | indio_dev->dev.parent = &spi->dev; |
| 337 | indio_dev->dev.of_node = spi->dev.of_node; |
| 338 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 339 | indio_dev->channels = ads124s_priv->chip_info->channels; |
| 340 | indio_dev->num_channels = ads124s_priv->chip_info->num_channels; |
| 341 | indio_dev->info = &ads124s_info; |
| 342 | |
| 343 | mutex_init(&ads124s_priv->lock); |
| 344 | |
| 345 | ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL, |
| 346 | ads124s_trigger_handler, NULL); |
| 347 | if (ret) { |
| 348 | dev_err(&spi->dev, "iio triggered buffer setup failed\n"); |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | ads124s_reset(indio_dev); |
| 353 | |
| 354 | return devm_iio_device_register(&spi->dev, indio_dev); |
| 355 | } |
| 356 | |
| 357 | static const struct spi_device_id ads124s_id[] = { |
| 358 | { "ads124s06", ADS124S06_ID }, |
| 359 | { "ads124s08", ADS124S08_ID }, |
| 360 | { } |
| 361 | }; |
| 362 | MODULE_DEVICE_TABLE(spi, ads124s_id); |
| 363 | |
| 364 | static const struct of_device_id ads124s_of_table[] = { |
| 365 | { .compatible = "ti,ads124s06" }, |
| 366 | { .compatible = "ti,ads124s08" }, |
| 367 | { }, |
| 368 | }; |
| 369 | MODULE_DEVICE_TABLE(of, ads124s_of_table); |
| 370 | |
| 371 | static struct spi_driver ads124s_driver = { |
| 372 | .driver = { |
| 373 | .name = "ads124s08", |
| 374 | .of_match_table = ads124s_of_table, |
| 375 | }, |
| 376 | .probe = ads124s_probe, |
| 377 | .id_table = ads124s_id, |
| 378 | }; |
| 379 | module_spi_driver(ads124s_driver); |
| 380 | |
| 381 | MODULE_AUTHOR("Dan Murphy <dmuprhy@ti.com>"); |
| 382 | MODULE_DESCRIPTION("TI TI_ADS12S0X ADC"); |
| 383 | MODULE_LICENSE("GPL v2"); |