1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * lmp91000.c - Support for Texas Instruments digital potentiostats
4  *
5  * Copyright (C) 2016, 2018
6  * Author: Matt Ranostay <matt.ranostay@konsulko.com>
7  *
8  * TODO: bias voltage + polarity control, and multiple chip support
9  */
10 
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/delay.h>
14 #include <linux/of.h>
15 #include <linux/regmap.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/consumer.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/trigger_consumer.h>
21 #include <linux/iio/triggered_buffer.h>
22 
23 #define LMP91000_REG_LOCK		0x01
24 #define LMP91000_REG_TIACN		0x10
25 #define LMP91000_REG_TIACN_GAIN_SHIFT	2
26 
27 #define LMP91000_REG_REFCN		0x11
28 #define LMP91000_REG_REFCN_EXT_REF	0x20
29 #define LMP91000_REG_REFCN_50_ZERO	0x80
30 
31 #define LMP91000_REG_MODECN		0x12
32 #define LMP91000_REG_MODECN_3LEAD	0x03
33 #define LMP91000_REG_MODECN_TEMP	0x07
34 
35 #define LMP91000_DRV_NAME	"lmp91000"
36 
37 static const int lmp91000_tia_gain[] = { 0, 2750, 3500, 7000, 14000, 35000,
38 					 120000, 350000 };
39 
40 static const int lmp91000_rload[] = { 10, 33, 50, 100 };
41 
42 #define LMP91000_TEMP_BASE	-40
43 
44 static const u16 lmp91000_temp_lut[] = {
45 	1875, 1867, 1860, 1852, 1844, 1836, 1828, 1821, 1813, 1805,
46 	1797, 1789, 1782, 1774, 1766, 1758, 1750, 1742, 1734, 1727,
47 	1719, 1711, 1703, 1695, 1687, 1679, 1671, 1663, 1656, 1648,
48 	1640, 1632, 1624, 1616, 1608, 1600, 1592, 1584, 1576, 1568,
49 	1560, 1552, 1544, 1536, 1528, 1520, 1512, 1504, 1496, 1488,
50 	1480, 1472, 1464, 1456, 1448, 1440, 1432, 1424, 1415, 1407,
51 	1399, 1391, 1383, 1375, 1367, 1359, 1351, 1342, 1334, 1326,
52 	1318, 1310, 1302, 1293, 1285, 1277, 1269, 1261, 1253, 1244,
53 	1236, 1228, 1220, 1212, 1203, 1195, 1187, 1179, 1170, 1162,
54 	1154, 1146, 1137, 1129, 1121, 1112, 1104, 1096, 1087, 1079,
55 	1071, 1063, 1054, 1046, 1038, 1029, 1021, 1012, 1004,  996,
56 	 987,  979,  971,  962,  954,  945,  937,  929,  920,  912,
57 	 903,  895,  886,  878,  870,  861 };
58 
59 static const struct regmap_config lmp91000_regmap_config = {
60 	.reg_bits = 8,
61 	.val_bits = 8,
62 };
63 
64 struct lmp91000_data {
65 	struct regmap *regmap;
66 	struct device *dev;
67 
68 	struct iio_trigger *trig;
69 	struct iio_cb_buffer *cb_buffer;
70 	struct iio_channel *adc_chan;
71 
72 	struct completion completion;
73 	u8 chan_select;
74 
75 	u32 buffer[4]; /* 64-bit data + 64-bit timestamp */
76 };
77 
78 static const struct iio_chan_spec lmp91000_channels[] = {
79 	{ /* chemical channel mV */
80 		.type = IIO_VOLTAGE,
81 		.channel = 0,
82 		.address = LMP91000_REG_MODECN_3LEAD,
83 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
84 				      BIT(IIO_CHAN_INFO_OFFSET) |
85 				      BIT(IIO_CHAN_INFO_SCALE),
86 		.scan_index = 0,
87 		.scan_type = {
88 			.sign = 's',
89 			.realbits = 32,
90 			.storagebits = 32,
91 		},
92 	},
93 	IIO_CHAN_SOFT_TIMESTAMP(1),
94 	{ /* temperature channel mV */
95 		.type = IIO_TEMP,
96 		.channel = 1,
97 		.address = LMP91000_REG_MODECN_TEMP,
98 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
99 		.scan_index = -1,
100 	},
101 };
102 
103 static int lmp91000_read(struct lmp91000_data *data, int channel, int *val)
104 {
105 	int state, ret;
106 
107 	ret = regmap_read(data->regmap, LMP91000_REG_MODECN, &state);
108 	if (ret)
109 		return -EINVAL;
110 
111 	ret = regmap_write(data->regmap, LMP91000_REG_MODECN, channel);
112 	if (ret)
113 		return -EINVAL;
114 
115 	/* delay till first temperature reading is complete */
116 	if (state != channel && channel == LMP91000_REG_MODECN_TEMP)
117 		usleep_range(3000, 4000);
118 
119 	data->chan_select = channel != LMP91000_REG_MODECN_3LEAD;
120 
121 	iio_trigger_poll_chained(data->trig);
122 
123 	ret = wait_for_completion_timeout(&data->completion, HZ);
124 	reinit_completion(&data->completion);
125 
126 	if (!ret)
127 		return -ETIMEDOUT;
128 
129 	*val = data->buffer[data->chan_select];
130 
131 	return 0;
132 }
133 
134 static irqreturn_t lmp91000_buffer_handler(int irq, void *private)
135 {
136 	struct iio_poll_func *pf = private;
137 	struct iio_dev *indio_dev = pf->indio_dev;
138 	struct lmp91000_data *data = iio_priv(indio_dev);
139 	int ret, val;
140 
141 	memset(data->buffer, 0, sizeof(data->buffer));
142 
143 	ret = lmp91000_read(data, LMP91000_REG_MODECN_3LEAD, &val);
144 	if (!ret) {
145 		data->buffer[0] = val;
146 		iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
147 						   iio_get_time_ns(indio_dev));
148 	}
149 
150 	iio_trigger_notify_done(indio_dev->trig);
151 
152 	return IRQ_HANDLED;
153 }
154 
155 static int lmp91000_read_raw(struct iio_dev *indio_dev,
156 			     struct iio_chan_spec const *chan,
157 			     int *val, int *val2, long mask)
158 {
159 	struct lmp91000_data *data = iio_priv(indio_dev);
160 
161 	switch (mask) {
162 	case IIO_CHAN_INFO_RAW:
163 	case IIO_CHAN_INFO_PROCESSED: {
164 		int ret = iio_channel_start_all_cb(data->cb_buffer);
165 
166 		if (ret)
167 			return ret;
168 
169 		ret = lmp91000_read(data, chan->address, val);
170 
171 		iio_channel_stop_all_cb(data->cb_buffer);
172 
173 		if (ret)
174 			return ret;
175 
176 		if (mask == IIO_CHAN_INFO_PROCESSED) {
177 			int tmp, i;
178 
179 			ret = iio_convert_raw_to_processed(data->adc_chan,
180 							   *val, &tmp, 1);
181 			if (ret)
182 				return ret;
183 
184 			for (i = 0; i < ARRAY_SIZE(lmp91000_temp_lut); i++)
185 				if (lmp91000_temp_lut[i] < tmp)
186 					break;
187 
188 			*val = (LMP91000_TEMP_BASE + i) * 1000;
189 		}
190 		return IIO_VAL_INT;
191 	}
192 	case IIO_CHAN_INFO_OFFSET:
193 		return iio_read_channel_offset(data->adc_chan, val, val2);
194 	case IIO_CHAN_INFO_SCALE:
195 		return iio_read_channel_scale(data->adc_chan, val, val2);
196 	}
197 
198 	return -EINVAL;
199 }
200 
201 static const struct iio_info lmp91000_info = {
202 	.read_raw = lmp91000_read_raw,
203 };
204 
205 static int lmp91000_read_config(struct lmp91000_data *data)
206 {
207 	struct device *dev = data->dev;
208 	struct device_node *np = dev->of_node;
209 	unsigned int reg, val;
210 	int i, ret;
211 
212 	ret = of_property_read_u32(np, "ti,tia-gain-ohm", &val);
213 	if (ret) {
214 		if (!of_property_read_bool(np, "ti,external-tia-resistor")) {
215 			dev_err(dev, "no ti,tia-gain-ohm defined and external resistor not specified\n");
216 			return ret;
217 		}
218 		val = 0;
219 	}
220 
221 	ret = -EINVAL;
222 	for (i = 0; i < ARRAY_SIZE(lmp91000_tia_gain); i++) {
223 		if (lmp91000_tia_gain[i] == val) {
224 			reg = i << LMP91000_REG_TIACN_GAIN_SHIFT;
225 			ret = 0;
226 			break;
227 		}
228 	}
229 
230 	if (ret) {
231 		dev_err(dev, "invalid ti,tia-gain-ohm %d\n", val);
232 		return ret;
233 	}
234 
235 	ret = of_property_read_u32(np, "ti,rload-ohm", &val);
236 	if (ret) {
237 		val = 100;
238 		dev_info(dev, "no ti,rload-ohm defined, default to %d\n", val);
239 	}
240 
241 	ret = -EINVAL;
242 	for (i = 0; i < ARRAY_SIZE(lmp91000_rload); i++) {
243 		if (lmp91000_rload[i] == val) {
244 			reg |= i;
245 			ret = 0;
246 			break;
247 		}
248 	}
249 
250 	if (ret) {
251 		dev_err(dev, "invalid ti,rload-ohm %d\n", val);
252 		return ret;
253 	}
254 
255 	regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
256 	regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
257 	regmap_write(data->regmap, LMP91000_REG_REFCN,
258 		     LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
259 	regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
260 
261 	return 0;
262 }
263 
264 static int lmp91000_buffer_cb(const void *val, void *private)
265 {
266 	struct iio_dev *indio_dev = private;
267 	struct lmp91000_data *data = iio_priv(indio_dev);
268 
269 	data->buffer[data->chan_select] = *((int *)val);
270 	complete_all(&data->completion);
271 
272 	return 0;
273 }
274 
275 static const struct iio_trigger_ops lmp91000_trigger_ops = {
276 };
277 
278 static int lmp91000_buffer_postenable(struct iio_dev *indio_dev)
279 {
280 	struct lmp91000_data *data = iio_priv(indio_dev);
281 	int err;
282 
283 	err = iio_triggered_buffer_postenable(indio_dev);
284 	if (err)
285 		return err;
286 
287 	err = iio_channel_start_all_cb(data->cb_buffer);
288 	if (err)
289 		iio_triggered_buffer_predisable(indio_dev);
290 
291 	return err;
292 }
293 
294 static int lmp91000_buffer_predisable(struct iio_dev *indio_dev)
295 {
296 	struct lmp91000_data *data = iio_priv(indio_dev);
297 
298 	iio_channel_stop_all_cb(data->cb_buffer);
299 
300 	return iio_triggered_buffer_predisable(indio_dev);
301 }
302 
303 static const struct iio_buffer_setup_ops lmp91000_buffer_setup_ops = {
304 	.postenable = lmp91000_buffer_postenable,
305 	.predisable = lmp91000_buffer_predisable,
306 };
307 
308 static int lmp91000_probe(struct i2c_client *client,
309 			  const struct i2c_device_id *id)
310 {
311 	struct device *dev = &client->dev;
312 	struct lmp91000_data *data;
313 	struct iio_dev *indio_dev;
314 	int ret;
315 
316 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
317 	if (!indio_dev)
318 		return -ENOMEM;
319 
320 	indio_dev->info = &lmp91000_info;
321 	indio_dev->channels = lmp91000_channels;
322 	indio_dev->num_channels = ARRAY_SIZE(lmp91000_channels);
323 	indio_dev->name = LMP91000_DRV_NAME;
324 	indio_dev->dev.parent = &client->dev;
325 	indio_dev->modes = INDIO_DIRECT_MODE;
326 	i2c_set_clientdata(client, indio_dev);
327 
328 	data = iio_priv(indio_dev);
329 	data->dev = dev;
330 	data->regmap = devm_regmap_init_i2c(client, &lmp91000_regmap_config);
331 	if (IS_ERR(data->regmap)) {
332 		dev_err(dev, "regmap initialization failed.\n");
333 		return PTR_ERR(data->regmap);
334 	}
335 
336 	data->trig = devm_iio_trigger_alloc(data->dev, "%s-mux%d",
337 					    indio_dev->name, indio_dev->id);
338 	if (!data->trig) {
339 		dev_err(dev, "cannot allocate iio trigger.\n");
340 		return -ENOMEM;
341 	}
342 
343 	data->trig->ops = &lmp91000_trigger_ops;
344 	data->trig->dev.parent = dev;
345 	init_completion(&data->completion);
346 
347 	ret = lmp91000_read_config(data);
348 	if (ret)
349 		return ret;
350 
351 	ret = iio_trigger_set_immutable(iio_channel_cb_get_iio_dev(data->cb_buffer),
352 					data->trig);
353 	if (ret) {
354 		dev_err(dev, "cannot set immutable trigger.\n");
355 		return ret;
356 	}
357 
358 	ret = iio_trigger_register(data->trig);
359 	if (ret) {
360 		dev_err(dev, "cannot register iio trigger.\n");
361 		return ret;
362 	}
363 
364 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
365 					 &lmp91000_buffer_handler,
366 					 &lmp91000_buffer_setup_ops);
367 	if (ret)
368 		goto error_unreg_trigger;
369 
370 	data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
371 						 indio_dev);
372 
373 	if (IS_ERR(data->cb_buffer)) {
374 		if (PTR_ERR(data->cb_buffer) == -ENODEV)
375 			ret = -EPROBE_DEFER;
376 		else
377 			ret = PTR_ERR(data->cb_buffer);
378 
379 		goto error_unreg_buffer;
380 	}
381 
382 	data->adc_chan = iio_channel_cb_get_channels(data->cb_buffer);
383 
384 	ret = iio_device_register(indio_dev);
385 	if (ret)
386 		goto error_unreg_cb_buffer;
387 
388 	return 0;
389 
390 error_unreg_cb_buffer:
391 	iio_channel_release_all_cb(data->cb_buffer);
392 
393 error_unreg_buffer:
394 	iio_triggered_buffer_cleanup(indio_dev);
395 
396 error_unreg_trigger:
397 	iio_trigger_unregister(data->trig);
398 
399 	return ret;
400 }
401 
402 static int lmp91000_remove(struct i2c_client *client)
403 {
404 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
405 	struct lmp91000_data *data = iio_priv(indio_dev);
406 
407 	iio_device_unregister(indio_dev);
408 
409 	iio_channel_stop_all_cb(data->cb_buffer);
410 	iio_channel_release_all_cb(data->cb_buffer);
411 
412 	iio_triggered_buffer_cleanup(indio_dev);
413 	iio_trigger_unregister(data->trig);
414 
415 	return 0;
416 }
417 
418 static const struct of_device_id lmp91000_of_match[] = {
419 	{ .compatible = "ti,lmp91000", },
420 	{ .compatible = "ti,lmp91002", },
421 	{ },
422 };
423 MODULE_DEVICE_TABLE(of, lmp91000_of_match);
424 
425 static const struct i2c_device_id lmp91000_id[] = {
426 	{ "lmp91000", 0 },
427 	{ "lmp91002", 0 },
428 	{}
429 };
430 MODULE_DEVICE_TABLE(i2c, lmp91000_id);
431 
432 static struct i2c_driver lmp91000_driver = {
433 	.driver = {
434 		.name = LMP91000_DRV_NAME,
435 		.of_match_table = of_match_ptr(lmp91000_of_match),
436 	},
437 	.probe = lmp91000_probe,
438 	.remove = lmp91000_remove,
439 	.id_table = lmp91000_id,
440 };
441 module_i2c_driver(lmp91000_driver);
442 
443 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
444 MODULE_DESCRIPTION("LMP91000 digital potentiostat");
445 MODULE_LICENSE("GPL");
446