Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ltc2496.c - Driver for Analog Devices/Linear Technology LTC2496 ADC
0004  *
0005  * Based on ltc2497.c which has
0006  * Copyright (C) 2017 Analog Devices Inc.
0007  *
0008  * Licensed under the GPL-2.
0009  *
0010  * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/2496fc.pdf
0011  */
0012 
0013 #include <linux/spi/spi.h>
0014 #include <linux/iio/iio.h>
0015 #include <linux/iio/driver.h>
0016 #include <linux/module.h>
0017 #include <linux/mod_devicetable.h>
0018 
0019 #include "ltc2497.h"
0020 
0021 struct ltc2496_driverdata {
0022     /* this must be the first member */
0023     struct ltc2497core_driverdata common_ddata;
0024     struct spi_device *spi;
0025 
0026     /*
0027      * DMA (thus cache coherency maintenance) may require the
0028      * transfer buffers to live in their own cache lines.
0029      */
0030     unsigned char rxbuf[3] __aligned(IIO_DMA_MINALIGN);
0031     unsigned char txbuf[3];
0032 };
0033 
0034 static int ltc2496_result_and_measure(struct ltc2497core_driverdata *ddata,
0035                       u8 address, int *val)
0036 {
0037     struct ltc2496_driverdata *st =
0038         container_of(ddata, struct ltc2496_driverdata, common_ddata);
0039     struct spi_transfer t = {
0040         .tx_buf = st->txbuf,
0041         .rx_buf = st->rxbuf,
0042         .len = sizeof(st->txbuf),
0043     };
0044     int ret;
0045 
0046     st->txbuf[0] = LTC2497_ENABLE | address;
0047 
0048     ret = spi_sync_transfer(st->spi, &t, 1);
0049     if (ret < 0)  {
0050         dev_err(&st->spi->dev, "spi_sync_transfer failed: %pe\n",
0051             ERR_PTR(ret));
0052         return ret;
0053     }
0054 
0055     if (val)
0056         *val = ((st->rxbuf[0] & 0x3f) << 12 |
0057             st->rxbuf[1] << 4 | st->rxbuf[2] >> 4) -
0058             (1 << 17);
0059 
0060     return 0;
0061 }
0062 
0063 static int ltc2496_probe(struct spi_device *spi)
0064 {
0065     struct iio_dev *indio_dev;
0066     struct ltc2496_driverdata *st;
0067     struct device *dev = &spi->dev;
0068 
0069     indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
0070     if (!indio_dev)
0071         return -ENOMEM;
0072 
0073     st = iio_priv(indio_dev);
0074     spi_set_drvdata(spi, indio_dev);
0075     st->spi = spi;
0076     st->common_ddata.result_and_measure = ltc2496_result_and_measure;
0077 
0078     return ltc2497core_probe(dev, indio_dev);
0079 }
0080 
0081 static void ltc2496_remove(struct spi_device *spi)
0082 {
0083     struct iio_dev *indio_dev = spi_get_drvdata(spi);
0084 
0085     ltc2497core_remove(indio_dev);
0086 }
0087 
0088 static const struct of_device_id ltc2496_of_match[] = {
0089     { .compatible = "lltc,ltc2496", },
0090     {},
0091 };
0092 MODULE_DEVICE_TABLE(of, ltc2496_of_match);
0093 
0094 static struct spi_driver ltc2496_driver = {
0095     .driver = {
0096         .name = "ltc2496",
0097         .of_match_table = ltc2496_of_match,
0098     },
0099     .probe = ltc2496_probe,
0100     .remove = ltc2496_remove,
0101 };
0102 module_spi_driver(ltc2496_driver);
0103 
0104 MODULE_AUTHOR("Uwe Kleine-König <u.kleine-könig@pengutronix.de>");
0105 MODULE_DESCRIPTION("Linear Technology LTC2496 ADC driver");
0106 MODULE_LICENSE("GPL v2");