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