Debian Codegraph @

drivers/iio/adc/ti-ads7950.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Texas Instruments ADS7950 SPI ADC driver
4 *
5 * Copyright 2016 David Lechner <[email protected]>
6 *
7 * Based on iio/ad7923.c:
8 * Copyright 2011 Analog Devices Inc
9 * Copyright 2012 CS Systemes d'Information
10 *
11 * And also on hwmon/ads79xx.c
12 * Copyright (C) 2013 Texas Instruments Incorporated - https://www.ti.com/
13 * Nishanth Menon
14 */
15
16 #include <linux/acpi.h>
17 #include <linux/bitops.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <linux/spi/spi.h>
27
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/triggered_buffer.h>
33
34 /*
35 * In case of ACPI, we use the 5000 mV as default for the reference pin.
36 * Device tree users encode that via the vref-supply regulator.
37 */
38 #define TI_ADS7950_VA_MV_ACPI_DEFAULT 5000
39
40 #define TI_ADS7950_CR_GPIO BIT(14)
41 #define TI_ADS7950_CR_MANUAL BIT(12)
42 #define TI_ADS7950_CR_WRITE BIT(11)
43 #define TI_ADS7950_CR_CHAN(ch) ((ch) << 7)
44 #define TI_ADS7950_CR_RANGE_5V BIT(6)
45 #define TI_ADS7950_CR_GPIO_DATA BIT(4)
46
47 #define TI_ADS7950_MAX_CHAN 16
48 #define TI_ADS7950_NUM_GPIOS 4
49
50 /* val = value, dec = left shift, bits = number of bits of the mask */
51 #define TI_ADS7950_EXTRACT(val, dec, bits) \
52 (((val) >> (dec)) & ((1 << (bits)) - 1))
53
54 #define TI_ADS7950_MAN_CMD(cmd) (TI_ADS7950_CR_MANUAL | (cmd))
55 #define TI_ADS7950_GPIO_CMD(cmd) (TI_ADS7950_CR_GPIO | (cmd))
56
57 /* Manual mode configuration */
58 #define TI_ADS7950_MAN_CMD_SETTINGS(st) \
59 (TI_ADS7950_MAN_CMD(TI_ADS7950_CR_WRITE | st->cmd_settings_bitmask))
60 /* GPIO mode configuration */
61 #define TI_ADS7950_GPIO_CMD_SETTINGS(st) \
62 (TI_ADS7950_GPIO_CMD(st->gpio_cmd_settings_bitmask))
63
64 struct ti_ads7950_state {
65 struct spi_device *spi;
66 struct spi_transfer ring_xfer;
67 struct spi_transfer scan_single_xfer[3];
68 struct spi_message ring_msg;
69 struct spi_message scan_single_msg;
70
71 /* Lock to protect the spi xfer buffers */
72 struct mutex slock;
73 struct gpio_chip chip;
74
75 struct regulator *reg;
76 unsigned int vref_mv;
77
78 /*
79 * Bitmask of lower 7 bits used for configuration
80 * These bits only can be written when TI_ADS7950_CR_WRITE
81 * is set, otherwise it retains its original state.
82 * [0-3] GPIO signal
83 * [4] Set following frame to return GPIO signal values
84 * [5] Powers down device
85 * [6] Sets Vref range1(2.5v) or range2(5v)
86 *
87 * Bits present on Manual/Auto1/Auto2 commands
88 */
89 unsigned int cmd_settings_bitmask;
90
91 /*
92 * Bitmask of GPIO command
93 * [0-3] GPIO direction
94 * [4-6] Different GPIO alarm mode configurations
95 * [7] GPIO 2 as device range input
96 * [8] GPIO 3 as device power down input
97 * [9] Reset all registers
98 * [10-11] N/A
99 */
100 unsigned int gpio_cmd_settings_bitmask;
101
102 /*
103 * DMA (thus cache coherency maintenance) may require the
104 * transfer buffers to live in their own cache lines.
105 */
106 u16 rx_buf[TI_ADS7950_MAX_CHAN + 2] __aligned(IIO_DMA_MINALIGN);
107 u16 tx_buf[TI_ADS7950_MAX_CHAN + 2];
108 u16 single_tx;
109 u16 single_rx;
110
111 };
112
113 struct ti_ads7950_chip_info {
114 const struct iio_chan_spec *channels;
115 unsigned int num_channels;
116 };
117
118 enum ti_ads7950_id {
119 TI_ADS7950,
120 TI_ADS7951,
121 TI_ADS7952,
122 TI_ADS7953,
123 TI_ADS7954,
124 TI_ADS7955,
125 TI_ADS7956,
126 TI_ADS7957,
127 TI_ADS7958,
128 TI_ADS7959,
129 TI_ADS7960,
130 TI_ADS7961,
131 };
132
133 #define TI_ADS7950_V_CHAN(index, bits) \
134 { \
135 .type = IIO_VOLTAGE, \
136 .indexed = 1, \
137 .channel = index, \
138 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
139 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
140 .address = index, \
141 .datasheet_name = "CH##index", \
142 .scan_index = index, \
143 .scan_type = { \
144 .sign = 'u', \
145 .realbits = bits, \
146 .storagebits = 16, \
147 .shift = 12 - (bits), \
148 .endianness = IIO_CPU, \
149 }, \
150 }
151
152 #define DECLARE_TI_ADS7950_4_CHANNELS(name, bits) \
153 const struct iio_chan_spec name ## _channels[] = { \
154 TI_ADS7950_V_CHAN(0, bits), \
155 TI_ADS7950_V_CHAN(1, bits), \
156 TI_ADS7950_V_CHAN(2, bits), \
157 TI_ADS7950_V_CHAN(3, bits), \
158 IIO_CHAN_SOFT_TIMESTAMP(4), \
159 }
160
161 #define DECLARE_TI_ADS7950_8_CHANNELS(name, bits) \
162 const struct iio_chan_spec name ## _channels[] = { \
163 TI_ADS7950_V_CHAN(0, bits), \
164 TI_ADS7950_V_CHAN(1, bits), \
165 TI_ADS7950_V_CHAN(2, bits), \
166 TI_ADS7950_V_CHAN(3, bits), \
167 TI_ADS7950_V_CHAN(4, bits), \
168 TI_ADS7950_V_CHAN(5, bits), \
169 TI_ADS7950_V_CHAN(6, bits), \
170 TI_ADS7950_V_CHAN(7, bits), \
171 IIO_CHAN_SOFT_TIMESTAMP(8), \
172 }
173
174 #define DECLARE_TI_ADS7950_12_CHANNELS(name, bits) \
175 const struct iio_chan_spec name ## _channels[] = { \
176 TI_ADS7950_V_CHAN(0, bits), \
177 TI_ADS7950_V_CHAN(1, bits), \
178 TI_ADS7950_V_CHAN(2, bits), \
179 TI_ADS7950_V_CHAN(3, bits), \
180 TI_ADS7950_V_CHAN(4, bits), \
181 TI_ADS7950_V_CHAN(5, bits), \
182 TI_ADS7950_V_CHAN(6, bits), \
183 TI_ADS7950_V_CHAN(7, bits), \
184 TI_ADS7950_V_CHAN(8, bits), \
185 TI_ADS7950_V_CHAN(9, bits), \
186 TI_ADS7950_V_CHAN(10, bits), \
187 TI_ADS7950_V_CHAN(11, bits), \
188 IIO_CHAN_SOFT_TIMESTAMP(12), \
189 }
190
191 #define DECLARE_TI_ADS7950_16_CHANNELS(name, bits) \
192 const struct iio_chan_spec name ## _channels[] = { \
193 TI_ADS7950_V_CHAN(0, bits), \
194 TI_ADS7950_V_CHAN(1, bits), \
195 TI_ADS7950_V_CHAN(2, bits), \
196 TI_ADS7950_V_CHAN(3, bits), \
197 TI_ADS7950_V_CHAN(4, bits), \
198 TI_ADS7950_V_CHAN(5, bits), \
199 TI_ADS7950_V_CHAN(6, bits), \
200 TI_ADS7950_V_CHAN(7, bits), \
201 TI_ADS7950_V_CHAN(8, bits), \
202 TI_ADS7950_V_CHAN(9, bits), \
203 TI_ADS7950_V_CHAN(10, bits), \
204 TI_ADS7950_V_CHAN(11, bits), \
205 TI_ADS7950_V_CHAN(12, bits), \
206 TI_ADS7950_V_CHAN(13, bits), \
207 TI_ADS7950_V_CHAN(14, bits), \
208 TI_ADS7950_V_CHAN(15, bits), \
209 IIO_CHAN_SOFT_TIMESTAMP(16), \
210 }
211
212 static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7950, 12);
213 static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7951, 12);
214 static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7952, 12);
215 static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7953, 12);
216 static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7954, 10);
217 static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7955, 10);
218 static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7956, 10);
219 static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7957, 10);
220 static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7958, 8);
221 static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7959, 8);
222 static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7960, 8);
223 static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7961, 8);
224
225 static const struct ti_ads7950_chip_info ti_ads7950_chip_info[] = {
226 [TI_ADS7950] = {
227 .channels = ti_ads7950_channels,
228 .num_channels = ARRAY_SIZE(ti_ads7950_channels),
229 },
230 [TI_ADS7951] = {
231 .channels = ti_ads7951_channels,
232 .num_channels = ARRAY_SIZE(ti_ads7951_channels),
233 },
234 [TI_ADS7952] = {
235 .channels = ti_ads7952_channels,
236 .num_channels = ARRAY_SIZE(ti_ads7952_channels),
237 },
238 [TI_ADS7953] = {
239 .channels = ti_ads7953_channels,
240 .num_channels = ARRAY_SIZE(ti_ads7953_channels),
241 },
242 [TI_ADS7954] = {
243 .channels = ti_ads7954_channels,
244 .num_channels = ARRAY_SIZE(ti_ads7954_channels),
245 },
246 [TI_ADS7955] = {
247 .channels = ti_ads7955_channels,
248 .num_channels = ARRAY_SIZE(ti_ads7955_channels),
249 },
250 [TI_ADS7956] = {
251 .channels = ti_ads7956_channels,
252 .num_channels = ARRAY_SIZE(ti_ads7956_channels),
253 },
254 [TI_ADS7957] = {
255 .channels = ti_ads7957_channels,
256 .num_channels = ARRAY_SIZE(ti_ads7957_channels),
257 },
258 [TI_ADS7958] = {
259 .channels = ti_ads7958_channels,
260 .num_channels = ARRAY_SIZE(ti_ads7958_channels),
261 },
262 [TI_ADS7959] = {
263 .channels = ti_ads7959_channels,
264 .num_channels = ARRAY_SIZE(ti_ads7959_channels),
265 },
266 [TI_ADS7960] = {
267 .channels = ti_ads7960_channels,
268 .num_channels = ARRAY_SIZE(ti_ads7960_channels),
269 },
270 [TI_ADS7961] = {
271 .channels = ti_ads7961_channels,
272 .num_channels = ARRAY_SIZE(ti_ads7961_channels),
273 },
274 };
275
276 /*
277 * ti_ads7950_update_scan_mode() setup the spi transfer buffer for the new
278 * scan mask
279 */
280 static int ti_ads7950_update_scan_mode(struct iio_dev *indio_dev,
281 const unsigned long *active_scan_mask)
282 {
283 struct ti_ads7950_state *st = iio_priv(indio_dev);
284 int i, cmd, len;
285
286 len = 0;
287 for_each_set_bit(i, active_scan_mask, indio_dev->num_channels) {
288 cmd = TI_ADS7950_MAN_CMD(TI_ADS7950_CR_CHAN(i));
289 st->tx_buf[len++] = cmd;
290 }
291
292 /* Data for the 1st channel is not returned until the 3rd transfer */
293 st->tx_buf[len++] = 0;
294 st->tx_buf[len++] = 0;
295
296 st->ring_xfer.len = len * 2;
297
298 return 0;
299 }
300
301 static irqreturn_t ti_ads7950_trigger_handler(int irq, void *p)
302 {
303 struct iio_poll_func *pf = p;
304 struct iio_dev *indio_dev = pf->indio_dev;
305 struct ti_ads7950_state *st = iio_priv(indio_dev);
306 int ret;
307
308 mutex_lock(&st->slock);
309 ret = spi_sync(st->spi, &st->ring_msg);
310 if (ret < 0)
311 goto out;
312
313 iio_push_to_buffers_with_ts_unaligned(indio_dev, &st->rx_buf[2],
314 sizeof(*st->rx_buf) *
315 TI_ADS7950_MAX_CHAN,
316 iio_get_time_ns(indio_dev));
317
318 out:
319 mutex_unlock(&st->slock);
320 iio_trigger_notify_done(indio_dev->trig);
321
322 return IRQ_HANDLED;
323 }
324
325 static int ti_ads7950_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
326 {
327 struct ti_ads7950_state *st = iio_priv(indio_dev);
328 int ret, cmd;
329
330 mutex_lock(&st->slock);
331 cmd = TI_ADS7950_MAN_CMD(TI_ADS7950_CR_CHAN(ch));
332 st->single_tx = cmd;
333
334 ret = spi_sync(st->spi, &st->scan_single_msg);
335 if (ret)
336 goto out;
337
338 ret = st->single_rx;
339
340 out:
341 mutex_unlock(&st->slock);
342
343 return ret;
344 }
345
346 static int ti_ads7950_get_range(struct ti_ads7950_state *st)
347 {
348 int vref;
349
350 if (st->vref_mv) {
351 vref = st->vref_mv;
352 } else {
353 vref = regulator_get_voltage(st->reg);
354 if (vref < 0)
355 return vref;
356
357 vref /= 1000;
358 }
359
360 if (st->cmd_settings_bitmask & TI_ADS7950_CR_RANGE_5V)
361 vref *= 2;
362
363 return vref;
364 }
365
366 static int ti_ads7950_read_raw(struct iio_dev *indio_dev,
367 struct iio_chan_spec const *chan,
368 int *val, int *val2, long m)
369 {
370 struct ti_ads7950_state *st = iio_priv(indio_dev);
371 int ret;
372
373 switch (m) {
374 case IIO_CHAN_INFO_RAW:
375 ret = ti_ads7950_scan_direct(indio_dev, chan->address);
376 if (ret < 0)
377 return ret;
378
379 if (chan->address != TI_ADS7950_EXTRACT(ret, 12, 4))
380 return -EIO;
381
382 *val = TI_ADS7950_EXTRACT(ret, chan->scan_type.shift,
383 chan->scan_type.realbits);
384
385 return IIO_VAL_INT;
386 case IIO_CHAN_INFO_SCALE:
387 ret = ti_ads7950_get_range(st);
388 if (ret < 0)
389 return ret;
390
391 *val = ret;
392 *val2 = (1 << chan->scan_type.realbits) - 1;
393
394 return IIO_VAL_FRACTIONAL;
395 }
396
397 return -EINVAL;
398 }
399
400 static const struct iio_info ti_ads7950_info = {
401 .read_raw = &ti_ads7950_read_raw,
402 .update_scan_mode = ti_ads7950_update_scan_mode,
403 };
404
405 static int ti_ads7950_set(struct gpio_chip *chip, unsigned int offset,
406 int value)
407 {
408 struct ti_ads7950_state *st = gpiochip_get_data(chip);
409 int ret;
410
411 mutex_lock(&st->slock);
412
413 if (value)
414 st->cmd_settings_bitmask |= BIT(offset);
415 else
416 st->cmd_settings_bitmask &= ~BIT(offset);
417
418 st->single_tx = TI_ADS7950_MAN_CMD_SETTINGS(st);
419 ret = spi_sync(st->spi, &st->scan_single_msg);
420
421 mutex_unlock(&st->slock);
422
423 return ret;
424 }
425
426 static int ti_ads7950_get(struct gpio_chip *chip, unsigned int offset)
427 {
428 struct ti_ads7950_state *st = gpiochip_get_data(chip);
429 bool state;
430 int ret;
431
432 mutex_lock(&st->slock);
433
434 /* If set as output, return the output */
435 if (st->gpio_cmd_settings_bitmask & BIT(offset)) {
436 state = st->cmd_settings_bitmask & BIT(offset);
437 ret = 0;
438 goto out;
439 }
440
441 /* GPIO data bit sets SDO bits 12-15 to GPIO input */
442 st->cmd_settings_bitmask |= TI_ADS7950_CR_GPIO_DATA;
443 st->single_tx = TI_ADS7950_MAN_CMD_SETTINGS(st);
444 ret = spi_sync(st->spi, &st->scan_single_msg);
445 if (ret)
446 goto out;
447
448 state = (st->single_rx >> 12) & BIT(offset);
449
450 /* Revert back to original settings */
451 st->cmd_settings_bitmask &= ~TI_ADS7950_CR_GPIO_DATA;
452 st->single_tx = TI_ADS7950_MAN_CMD_SETTINGS(st);
453 ret = spi_sync(st->spi, &st->scan_single_msg);
454 if (ret)
455 goto out;
456
457 out:
458 mutex_unlock(&st->slock);
459
460 return ret ?: state;
461 }
462
463 static int ti_ads7950_get_direction(struct gpio_chip *chip,
464 unsigned int offset)
465 {
466 struct ti_ads7950_state *st = gpiochip_get_data(chip);
467
468 /* Bitmask is inverted from GPIO framework 0=input/1=output */
469 return !(st->gpio_cmd_settings_bitmask & BIT(offset));
470 }
471
472 static int _ti_ads7950_set_direction(struct gpio_chip *chip, int offset,
473 int input)
474 {
475 struct ti_ads7950_state *st = gpiochip_get_data(chip);
476 int ret = 0;
477
478 mutex_lock(&st->slock);
479
480 /* Only change direction if needed */
481 if (input && (st->gpio_cmd_settings_bitmask & BIT(offset)))
482 st->gpio_cmd_settings_bitmask &= ~BIT(offset);
483 else if (!input && !(st->gpio_cmd_settings_bitmask & BIT(offset)))
484 st->gpio_cmd_settings_bitmask |= BIT(offset);
485 else
486 goto out;
487
488 st->single_tx = TI_ADS7950_GPIO_CMD_SETTINGS(st);
489 ret = spi_sync(st->spi, &st->scan_single_msg);
490
491 out:
492 mutex_unlock(&st->slock);
493
494 return ret;
495 }
496
497 static int ti_ads7950_direction_input(struct gpio_chip *chip,
498 unsigned int offset)
499 {
500 return _ti_ads7950_set_direction(chip, offset, 1);
501 }
502
503 static int ti_ads7950_direction_output(struct gpio_chip *chip,
504 unsigned int offset, int value)
505 {
506 int ret;
507
508 ret = ti_ads7950_set(chip, offset, value);
509 if (ret)
510 return ret;
511
512 return _ti_ads7950_set_direction(chip, offset, 0);
513 }
514
515 static int ti_ads7950_init_hw(struct ti_ads7950_state *st)
516 {
517 int ret = 0;
518
519 mutex_lock(&st->slock);
520
521 /* Settings for Manual/Auto1/Auto2 commands */
522 /* Default to 5v ref */
523 st->cmd_settings_bitmask = TI_ADS7950_CR_RANGE_5V;
524 st->single_tx = TI_ADS7950_MAN_CMD_SETTINGS(st);
525 ret = spi_sync(st->spi, &st->scan_single_msg);
526 if (ret)
527 goto out;
528
529 /* Settings for GPIO command */
530 st->gpio_cmd_settings_bitmask = 0x0;
531 st->single_tx = TI_ADS7950_GPIO_CMD_SETTINGS(st);
532 ret = spi_sync(st->spi, &st->scan_single_msg);
533
534 out:
535 mutex_unlock(&st->slock);
536
537 return ret;
538 }
539
540 static int ti_ads7950_probe(struct spi_device *spi)
541 {
542 struct ti_ads7950_state *st;
543 struct iio_dev *indio_dev;
544 const struct ti_ads7950_chip_info *info;
545 int ret;
546
547 spi->bits_per_word = 16;
548 spi->mode |= SPI_CS_WORD;
549 ret = spi_setup(spi);
550 if (ret < 0) {
551 dev_err(&spi->dev, "Error in spi setup\n");
552 return ret;
553 }
554
555 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
556 if (!indio_dev)
557 return -ENOMEM;
558
559 st = iio_priv(indio_dev);
560
561 spi_set_drvdata(spi, indio_dev);
562
563 st->spi = spi;
564
565 info = &ti_ads7950_chip_info[spi_get_device_id(spi)->driver_data];
566
567 indio_dev->name = spi_get_device_id(spi)->name;
568 indio_dev->modes = INDIO_DIRECT_MODE;
569 indio_dev->channels = info->channels;
570 indio_dev->num_channels = info->num_channels;
571 indio_dev->info = &ti_ads7950_info;
572
573 /* build spi ring message */
574 spi_message_init(&st->ring_msg);
575
576 st->ring_xfer.tx_buf = &st->tx_buf[0];
577 st->ring_xfer.rx_buf = &st->rx_buf[0];
578 /* len will be set later */
579
580 spi_message_add_tail(&st->ring_xfer, &st->ring_msg);
581
582 /*
583 * Setup default message. The sample is read at the end of the first
584 * transfer, then it takes one full cycle to convert the sample and one
585 * more cycle to send the value. The conversion process is driven by
586 * the SPI clock, which is why we have 3 transfers. The middle one is
587 * just dummy data sent while the chip is converting the sample that
588 * was read at the end of the first transfer.
589 */
590
591 st->scan_single_xfer[0].tx_buf = &st->single_tx;
592 st->scan_single_xfer[0].len = 2;
593 st->scan_single_xfer[0].cs_change = 1;
594 st->scan_single_xfer[1].tx_buf = &st->single_tx;
595 st->scan_single_xfer[1].len = 2;
596 st->scan_single_xfer[1].cs_change = 1;
597 st->scan_single_xfer[2].rx_buf = &st->single_rx;
598 st->scan_single_xfer[2].len = 2;
599
600 spi_message_init_with_transfers(&st->scan_single_msg,
601 st->scan_single_xfer, 3);
602
603 /* Use hard coded value for reference voltage in ACPI case */
604 if (ACPI_COMPANION(&spi->dev))
605 st->vref_mv = TI_ADS7950_VA_MV_ACPI_DEFAULT;
606
607 mutex_init(&st->slock);
608
609 st->reg = devm_regulator_get(&spi->dev, "vref");
610 if (IS_ERR(st->reg)) {
611 ret = dev_err_probe(&spi->dev, PTR_ERR(st->reg),
612 "Failed to get regulator \"vref\"\n");
613 goto error_destroy_mutex;
614 }
615
616 ret = regulator_enable(st->reg);
617 if (ret) {
618 dev_err(&spi->dev, "Failed to enable regulator \"vref\"\n");
619 goto error_destroy_mutex;
620 }
621
622 ret = iio_triggered_buffer_setup(indio_dev, NULL,
623 &ti_ads7950_trigger_handler, NULL);
624 if (ret) {
625 dev_err(&spi->dev, "Failed to setup triggered buffer\n");
626 goto error_disable_reg;
627 }
628
629 ret = ti_ads7950_init_hw(st);
630 if (ret) {
631 dev_err(&spi->dev, "Failed to init adc chip\n");
632 goto error_cleanup_ring;
633 }
634
635 ret = iio_device_register(indio_dev);
636 if (ret) {
637 dev_err(&spi->dev, "Failed to register iio device\n");
638 goto error_cleanup_ring;
639 }
640
641 /* Add GPIO chip */
642 st->chip.label = dev_name(&st->spi->dev);
643 st->chip.parent = &st->spi->dev;
644 st->chip.owner = THIS_MODULE;
645 st->chip.can_sleep = true;
646 st->chip.base = -1;
647 st->chip.ngpio = TI_ADS7950_NUM_GPIOS;
648 st->chip.get_direction = ti_ads7950_get_direction;
649 st->chip.direction_input = ti_ads7950_direction_input;
650 st->chip.direction_output = ti_ads7950_direction_output;
651 st->chip.get = ti_ads7950_get;
652 st->chip.set = ti_ads7950_set;
653
654 ret = gpiochip_add_data(&st->chip, st);
655 if (ret) {
656 dev_err(&spi->dev, "Failed to init GPIOs\n");
657 goto error_iio_device;
658 }
659
660 return 0;
661
662 error_iio_device:
663 iio_device_unregister(indio_dev);
664 error_cleanup_ring:
665 iio_triggered_buffer_cleanup(indio_dev);
666 error_disable_reg:
667 regulator_disable(st->reg);
668 error_destroy_mutex:
669 mutex_destroy(&st->slock);
670
671 return ret;
672 }
673
674 static void ti_ads7950_remove(struct spi_device *spi)
675 {
676 struct iio_dev *indio_dev = spi_get_drvdata(spi);
677 struct ti_ads7950_state *st = iio_priv(indio_dev);
678
679 gpiochip_remove(&st->chip);
680 iio_device_unregister(indio_dev);
681 iio_triggered_buffer_cleanup(indio_dev);
682 regulator_disable(st->reg);
683 mutex_destroy(&st->slock);
684 }
685
686 static const struct spi_device_id ti_ads7950_id[] = {
687 { "ads7950", TI_ADS7950 },
688 { "ads7951", TI_ADS7951 },
689 { "ads7952", TI_ADS7952 },
690 { "ads7953", TI_ADS7953 },
691 { "ads7954", TI_ADS7954 },
692 { "ads7955", TI_ADS7955 },
693 { "ads7956", TI_ADS7956 },
694 { "ads7957", TI_ADS7957 },
695 { "ads7958", TI_ADS7958 },
696 { "ads7959", TI_ADS7959 },
697 { "ads7960", TI_ADS7960 },
698 { "ads7961", TI_ADS7961 },
699 { }
700 };
701 MODULE_DEVICE_TABLE(spi, ti_ads7950_id);
702
703 static const struct of_device_id ads7950_of_table[] = {
704 { .compatible = "ti,ads7950", .data = &ti_ads7950_chip_info[TI_ADS7950] },
705 { .compatible = "ti,ads7951", .data = &ti_ads7950_chip_info[TI_ADS7951] },
706 { .compatible = "ti,ads7952", .data = &ti_ads7950_chip_info[TI_ADS7952] },
707 { .compatible = "ti,ads7953", .data = &ti_ads7950_chip_info[TI_ADS7953] },
708 { .compatible = "ti,ads7954", .data = &ti_ads7950_chip_info[TI_ADS7954] },
709 { .compatible = "ti,ads7955", .data = &ti_ads7950_chip_info[TI_ADS7955] },
710 { .compatible = "ti,ads7956", .data = &ti_ads7950_chip_info[TI_ADS7956] },
711 { .compatible = "ti,ads7957", .data = &ti_ads7950_chip_info[TI_ADS7957] },
712 { .compatible = "ti,ads7958", .data = &ti_ads7950_chip_info[TI_ADS7958] },
713 { .compatible = "ti,ads7959", .data = &ti_ads7950_chip_info[TI_ADS7959] },
714 { .compatible = "ti,ads7960", .data = &ti_ads7950_chip_info[TI_ADS7960] },
715 { .compatible = "ti,ads7961", .data = &ti_ads7950_chip_info[TI_ADS7961] },
716 { }
717 };
718 MODULE_DEVICE_TABLE(of, ads7950_of_table);
719
720 static struct spi_driver ti_ads7950_driver = {
721 .driver = {
722 .name = "ads7950",
723 .of_match_table = ads7950_of_table,
724 },
725 .probe = ti_ads7950_probe,
726 .remove = ti_ads7950_remove,
727 .id_table = ti_ads7950_id,
728 };
729 module_spi_driver(ti_ads7950_driver);
730
731 MODULE_AUTHOR("David Lechner <[email protected]>");
732 MODULE_DESCRIPTION("TI TI_ADS7950 ADC");
733 MODULE_LICENSE("GPL v2");