xref: /openbmc/linux/drivers/iio/adc/ltc2497.c (revision f59a3ee6)
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 #include <linux/property.h>
16 
17 #include <asm/unaligned.h>
18 
19 #include "ltc2497.h"
20 
21 enum ltc2497_chip_type {
22 	TYPE_LTC2497,
23 	TYPE_LTC2499,
24 };
25 
26 struct ltc2497_driverdata {
27 	/* this must be the first member */
28 	struct ltc2497core_driverdata common_ddata;
29 	struct i2c_client *client;
30 	u32 recv_size;
31 	u32 sub_lsb;
32 	/*
33 	 * DMA (thus cache coherency maintenance) may require the
34 	 * transfer buffers to live in their own cache lines.
35 	 */
36 	union {
37 		__be32 d32;
38 		u8 d8[3];
39 	} data __aligned(IIO_DMA_MINALIGN);
40 };
41 
42 static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
43 				      u8 address, int *val)
44 {
45 	struct ltc2497_driverdata *st =
46 		container_of(ddata, struct ltc2497_driverdata, common_ddata);
47 	int ret;
48 
49 	if (val) {
50 		if (st->recv_size == 3)
51 			ret = i2c_master_recv(st->client, (char *)&st->data.d8,
52 					      st->recv_size);
53 		else
54 			ret = i2c_master_recv(st->client, (char *)&st->data.d32,
55 					      st->recv_size);
56 		if (ret < 0) {
57 			dev_err(&st->client->dev, "i2c_master_recv failed\n");
58 			return ret;
59 		}
60 
61 		/*
62 		 * The data format is 16/24 bit 2s complement, but with an upper sign bit on the
63 		 * resolution + 1 position, which is set for positive values only. Given this
64 		 * bit's value, subtracting BIT(resolution + 1) from the ADC's result is
65 		 * equivalent to a sign extension.
66 		 */
67 		if (st->recv_size == 3) {
68 			*val = (get_unaligned_be24(st->data.d8) >> st->sub_lsb)
69 				- BIT(ddata->chip_info->resolution + 1);
70 		} else {
71 			*val = (be32_to_cpu(st->data.d32) >> st->sub_lsb)
72 				- BIT(ddata->chip_info->resolution + 1);
73 		}
74 
75 		/*
76 		 * The part started a new conversion at the end of the above i2c
77 		 * transfer, so if the address didn't change since the last call
78 		 * everything is fine and we can return early.
79 		 * If not (which should only happen when some sort of bulk
80 		 * conversion is implemented) we have to program the new
81 		 * address. Note that this probably fails as the conversion that
82 		 * was triggered above is like not complete yet and the two
83 		 * operations have to be done in a single transfer.
84 		 */
85 		if (ddata->addr_prev == address)
86 			return 0;
87 	}
88 
89 	ret = i2c_smbus_write_byte(st->client,
90 				   LTC2497_ENABLE | address);
91 	if (ret)
92 		dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
93 			ERR_PTR(ret));
94 	return ret;
95 }
96 
97 static int ltc2497_probe(struct i2c_client *client,
98 			 const struct i2c_device_id *id)
99 {
100 	const struct ltc2497_chip_info *chip_info;
101 	struct iio_dev *indio_dev;
102 	struct ltc2497_driverdata *st;
103 	struct device *dev = &client->dev;
104 	u32 resolution;
105 
106 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
107 				     I2C_FUNC_SMBUS_WRITE_BYTE))
108 		return -EOPNOTSUPP;
109 
110 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
111 	if (!indio_dev)
112 		return -ENOMEM;
113 
114 	st = iio_priv(indio_dev);
115 	i2c_set_clientdata(client, indio_dev);
116 	st->client = client;
117 	st->common_ddata.result_and_measure = ltc2497_result_and_measure;
118 
119 	chip_info = device_get_match_data(dev);
120 	if (!chip_info)
121 		chip_info = (const struct ltc2497_chip_info *)id->driver_data;
122 	st->common_ddata.chip_info = chip_info;
123 
124 	resolution = chip_info->resolution;
125 	st->sub_lsb = 31 - (resolution + 1);
126 	st->recv_size = BITS_TO_BYTES(resolution) + 1;
127 
128 	return ltc2497core_probe(dev, indio_dev);
129 }
130 
131 static void ltc2497_remove(struct i2c_client *client)
132 {
133 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
134 
135 	ltc2497core_remove(indio_dev);
136 }
137 
138 static const struct ltc2497_chip_info ltc2497_info[] = {
139 	[TYPE_LTC2497] = {
140 		.resolution = 16,
141 		.name = NULL,
142 	},
143 	[TYPE_LTC2499] = {
144 		.resolution = 24,
145 		.name = "ltc2499",
146 	},
147 };
148 
149 static const struct i2c_device_id ltc2497_id[] = {
150 	{ "ltc2497", (kernel_ulong_t)&ltc2497_info[TYPE_LTC2497] },
151 	{ "ltc2499", (kernel_ulong_t)&ltc2497_info[TYPE_LTC2499] },
152 	{ }
153 };
154 MODULE_DEVICE_TABLE(i2c, ltc2497_id);
155 
156 static const struct of_device_id ltc2497_of_match[] = {
157 	{ .compatible = "lltc,ltc2497", .data = &ltc2497_info[TYPE_LTC2497] },
158 	{ .compatible = "lltc,ltc2499", .data = &ltc2497_info[TYPE_LTC2499] },
159 	{},
160 };
161 MODULE_DEVICE_TABLE(of, ltc2497_of_match);
162 
163 static struct i2c_driver ltc2497_driver = {
164 	.driver = {
165 		.name = "ltc2497",
166 		.of_match_table = ltc2497_of_match,
167 	},
168 	.probe = ltc2497_probe,
169 	.remove = ltc2497_remove,
170 	.id_table = ltc2497_id,
171 };
172 module_i2c_driver(ltc2497_driver);
173 
174 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
175 MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
176 MODULE_LICENSE("GPL v2");
177