Uwe Kleine-König | e4c5c4d | 2019-12-09 21:32:48 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * ltc2496.c - Driver for Analog Devices/Linear Technology LTC2496 ADC |
| 4 | * |
| 5 | * Based on ltc2497.c which has |
| 6 | * Copyright (C) 2017 Analog Devices Inc. |
| 7 | * |
| 8 | * Licensed under the GPL-2. |
| 9 | * |
| 10 | * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/2496fc.pdf |
| 11 | */ |
| 12 | |
| 13 | #include <linux/spi/spi.h> |
| 14 | #include <linux/iio/iio.h> |
| 15 | #include <linux/iio/driver.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | |
| 19 | #include "ltc2497.h" |
| 20 | |
| 21 | struct ltc2496_driverdata { |
| 22 | /* this must be the first member */ |
| 23 | struct ltc2497core_driverdata common_ddata; |
| 24 | struct spi_device *spi; |
| 25 | |
| 26 | /* |
| 27 | * DMA (thus cache coherency maintenance) requires the |
| 28 | * transfer buffers to live in their own cache lines. |
| 29 | */ |
| 30 | unsigned char rxbuf[3] ____cacheline_aligned; |
| 31 | unsigned char txbuf[3]; |
| 32 | }; |
| 33 | |
| 34 | static int ltc2496_result_and_measure(struct ltc2497core_driverdata *ddata, |
| 35 | u8 address, int *val) |
| 36 | { |
| 37 | struct ltc2496_driverdata *st = |
| 38 | container_of(ddata, struct ltc2496_driverdata, common_ddata); |
| 39 | struct spi_transfer t = { |
| 40 | .tx_buf = st->txbuf, |
| 41 | .rx_buf = st->rxbuf, |
| 42 | .len = sizeof(st->txbuf), |
| 43 | }; |
| 44 | int ret; |
| 45 | |
| 46 | st->txbuf[0] = LTC2497_ENABLE | address; |
| 47 | |
| 48 | ret = spi_sync_transfer(st->spi, &t, 1); |
| 49 | if (ret < 0) { |
| 50 | dev_err(&st->spi->dev, "spi_sync_transfer failed: %pe\n", |
| 51 | ERR_PTR(ret)); |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | if (val) |
| 56 | *val = ((st->rxbuf[0] & 0x3f) << 12 | |
| 57 | st->rxbuf[1] << 4 | st->rxbuf[2] >> 4) - |
| 58 | (1 << 17); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int ltc2496_probe(struct spi_device *spi) |
| 64 | { |
| 65 | struct iio_dev *indio_dev; |
| 66 | struct ltc2496_driverdata *st; |
| 67 | struct device *dev = &spi->dev; |
| 68 | |
| 69 | indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); |
| 70 | if (!indio_dev) |
| 71 | return -ENOMEM; |
| 72 | |
| 73 | st = iio_priv(indio_dev); |
| 74 | spi_set_drvdata(spi, indio_dev); |
| 75 | st->spi = spi; |
| 76 | st->common_ddata.result_and_measure = ltc2496_result_and_measure; |
| 77 | |
| 78 | return ltc2497core_probe(dev, indio_dev); |
| 79 | } |
| 80 | |
| 81 | static int ltc2496_remove(struct spi_device *spi) |
| 82 | { |
| 83 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 84 | |
| 85 | ltc2497core_remove(indio_dev); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static const struct of_device_id ltc2496_of_match[] = { |
| 91 | { .compatible = "lltc,ltc2496", }, |
| 92 | {}, |
| 93 | }; |
| 94 | MODULE_DEVICE_TABLE(of, ltc2496_of_match); |
| 95 | |
| 96 | static struct spi_driver ltc2496_driver = { |
| 97 | .driver = { |
| 98 | .name = "ltc2496", |
| 99 | .of_match_table = of_match_ptr(ltc2496_of_match), |
| 100 | }, |
| 101 | .probe = ltc2496_probe, |
| 102 | .remove = ltc2496_remove, |
| 103 | }; |
| 104 | module_spi_driver(ltc2496_driver); |
| 105 | |
| 106 | MODULE_AUTHOR("Uwe Kleine-König <u.kleine-könig@pengutronix.de>"); |
| 107 | MODULE_DESCRIPTION("Linear Technology LTC2496 ADC driver"); |
| 108 | MODULE_LICENSE("GPL v2"); |