1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * ltc2497.c - Driver for Analog Devices/Linear Technology LTC2497 ADC 4 * 5 * Copyright (C) 2017 Analog Devices Inc. 6 * 7 * Datasheet: http://cds.linear.com/docs/en/datasheet/2497fd.pdf 8 */ 9 10#include <linux/i2c.h> 11#include <linux/iio/iio.h> 12#include <linux/iio/driver.h> 13#include <linux/module.h> 14#include <linux/mod_devicetable.h> 15 16#include "ltc2497.h" 17 18struct ltc2497_driverdata { 19 /* this must be the first member */ 20 struct ltc2497core_driverdata common_ddata; 21 struct i2c_client *client; 22 /* 23 * DMA (thus cache coherency maintenance) requires the 24 * transfer buffers to live in their own cache lines. 25 */ 26 __be32 buf ____cacheline_aligned; 27}; 28 29static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, 30 u8 address, int *val) 31{ 32 struct ltc2497_driverdata *st = 33 container_of(ddata, struct ltc2497_driverdata, common_ddata); 34 int ret; 35 36 if (val) { 37 ret = i2c_master_recv(st->client, (char *)&st->buf, 3); 38 if (ret < 0) { 39 dev_err(&st->client->dev, "i2c_master_recv failed\n"); 40 return ret; 41 } 42 43 *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17); 44 45 /* 46 * The part started a new conversion at the end of the above i2c 47 * transfer, so if the address didn't change since the last call 48 * everything is fine and we can return early. 49 * If not (which should only happen when some sort of bulk 50 * conversion is implemented) we have to program the new 51 * address. Note that this probably fails as the conversion that 52 * was triggered above is like not complete yet and the two 53 * operations have to be done in a single transfer. 54 */ 55 if (ddata->addr_prev == address) 56 return 0; 57 } 58 59 ret = i2c_smbus_write_byte(st->client, 60 LTC2497_ENABLE | address); 61 if (ret) 62 dev_err(&st->client->dev, "i2c transfer failed: %pe\n", 63 ERR_PTR(ret)); 64 return ret; 65} 66 67static int ltc2497_probe(struct i2c_client *client, 68 const struct i2c_device_id *id) 69{ 70 struct iio_dev *indio_dev; 71 struct ltc2497_driverdata *st; 72 struct device *dev = &client->dev; 73 74 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | 75 I2C_FUNC_SMBUS_WRITE_BYTE)) 76 return -EOPNOTSUPP; 77 78 indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 79 if (!indio_dev) 80 return -ENOMEM; 81 82 st = iio_priv(indio_dev); 83 i2c_set_clientdata(client, indio_dev); 84 st->client = client; 85 st->common_ddata.result_and_measure = ltc2497_result_and_measure; 86 87 return ltc2497core_probe(dev, indio_dev); 88} 89 90static int ltc2497_remove(struct i2c_client *client) 91{ 92 struct iio_dev *indio_dev = i2c_get_clientdata(client); 93 94 ltc2497core_remove(indio_dev); 95 96 return 0; 97} 98 99static const struct i2c_device_id ltc2497_id[] = { 100 { "ltc2497", 0 }, 101 { } 102}; 103MODULE_DEVICE_TABLE(i2c, ltc2497_id); 104 105static const struct of_device_id ltc2497_of_match[] = { 106 { .compatible = "lltc,ltc2497", }, 107 {}, 108}; 109MODULE_DEVICE_TABLE(of, ltc2497_of_match); 110 111static struct i2c_driver ltc2497_driver = { 112 .driver = { 113 .name = "ltc2497", 114 .of_match_table = ltc2497_of_match, 115 }, 116 .probe = ltc2497_probe, 117 .remove = ltc2497_remove, 118 .id_table = ltc2497_id, 119}; 120module_i2c_driver(ltc2497_driver); 121 122MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 123MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver"); 124MODULE_LICENSE("GPL v2"); 125