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