2 * lmp91000.c - Support for Texas Instruments digital potentiostats
4 * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
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.
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.
16 * TODO: bias voltage + polarity control, and multiple chip support
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/delay.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>
31 #define LMP91000_REG_LOCK 0x01
32 #define LMP91000_REG_TIACN 0x10
33 #define LMP91000_REG_TIACN_GAIN_SHIFT 2
35 #define LMP91000_REG_REFCN 0x11
36 #define LMP91000_REG_REFCN_EXT_REF 0x20
37 #define LMP91000_REG_REFCN_50_ZERO 0x80
39 #define LMP91000_REG_MODECN 0x12
40 #define LMP91000_REG_MODECN_3LEAD 0x03
41 #define LMP91000_REG_MODECN_TEMP 0x07
43 #define LMP91000_DRV_NAME "lmp91000"
45 static const int lmp91000_tia_gain
[] = { 0, 2750, 3500, 7000, 14000, 35000,
48 static const int lmp91000_rload
[] = { 10, 33, 50, 100 };
50 #define LMP91000_TEMP_BASE -40
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 };
67 static const struct regmap_config lmp91000_regmap_config
= {
72 struct lmp91000_data
{
73 struct regmap
*regmap
;
76 struct iio_trigger
*trig
;
77 struct iio_cb_buffer
*cb_buffer
;
78 struct iio_channel
*adc_chan
;
80 struct completion completion
;
83 u32 buffer
[4]; /* 64-bit data + 64-bit timestamp */
86 static const struct iio_chan_spec lmp91000_channels
[] = {
87 { /* chemical channel mV */
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
),
101 IIO_CHAN_SOFT_TIMESTAMP(1),
102 { /* temperature channel mV */
105 .address
= LMP91000_REG_MODECN_TEMP
,
106 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
111 static int lmp91000_read(struct lmp91000_data
*data
, int channel
, int *val
)
115 ret
= regmap_read(data
->regmap
, LMP91000_REG_MODECN
, &state
);
119 ret
= regmap_write(data
->regmap
, LMP91000_REG_MODECN
, channel
);
123 /* delay till first temperature reading is complete */
124 if ((state
!= channel
) && (channel
== LMP91000_REG_MODECN_TEMP
))
125 usleep_range(3000, 4000);
127 data
->chan_select
= channel
!= LMP91000_REG_MODECN_3LEAD
;
129 iio_trigger_poll_chained(data
->trig
);
131 ret
= wait_for_completion_timeout(&data
->completion
, HZ
);
132 reinit_completion(&data
->completion
);
137 *val
= data
->buffer
[data
->chan_select
];
142 static irqreturn_t
lmp91000_buffer_handler(int irq
, void *private)
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
);
149 memset(data
->buffer
, 0, sizeof(data
->buffer
));
151 ret
= lmp91000_read(data
, LMP91000_REG_MODECN_3LEAD
, &val
);
153 data
->buffer
[0] = val
;
154 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
155 iio_get_time_ns(indio_dev
));
158 iio_trigger_notify_done(indio_dev
->trig
);
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
)
167 struct lmp91000_data
*data
= iio_priv(indio_dev
);
170 case IIO_CHAN_INFO_RAW
:
171 case IIO_CHAN_INFO_PROCESSED
: {
172 int ret
= iio_channel_start_all_cb(data
->cb_buffer
);
177 ret
= lmp91000_read(data
, chan
->address
, val
);
179 iio_channel_stop_all_cb(data
->cb_buffer
);
184 if (mask
== IIO_CHAN_INFO_PROCESSED
) {
187 ret
= iio_convert_raw_to_processed(data
->adc_chan
,
192 for (i
= 0; i
< ARRAY_SIZE(lmp91000_temp_lut
); i
++)
193 if (lmp91000_temp_lut
[i
] < tmp
)
196 *val
= (LMP91000_TEMP_BASE
+ i
) * 1000;
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
);
209 static const struct iio_info lmp91000_info
= {
210 .read_raw
= lmp91000_read_raw
,
213 static int lmp91000_read_config(struct lmp91000_data
*data
)
215 struct device
*dev
= data
->dev
;
216 struct device_node
*np
= dev
->of_node
;
217 unsigned int reg
, val
;
220 ret
= of_property_read_u32(np
, "ti,tia-gain-ohm", &val
);
222 if (of_property_read_bool(np
, "ti,external-tia-resistor"))
225 dev_err(dev
, "no ti,tia-gain-ohm defined");
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
;
240 dev_err(dev
, "invalid ti,tia-gain-ohm %d\n", val
);
244 ret
= of_property_read_u32(np
, "ti,rload-ohm", &val
);
247 dev_info(dev
, "no ti,rload-ohm defined, default to %d\n", val
);
251 for (i
= 0; i
< ARRAY_SIZE(lmp91000_rload
); i
++) {
252 if (lmp91000_rload
[i
] == val
) {
260 dev_err(dev
, "invalid ti,rload-ohm %d\n", val
);
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);
273 static int lmp91000_buffer_cb(const void *val
, void *private)
275 struct iio_dev
*indio_dev
= private;
276 struct lmp91000_data
*data
= iio_priv(indio_dev
);
278 data
->buffer
[data
->chan_select
] = *((int *)val
);
279 complete_all(&data
->completion
);
284 static const struct iio_trigger_ops lmp91000_trigger_ops
= {
288 static int lmp91000_buffer_preenable(struct iio_dev
*indio_dev
)
290 struct lmp91000_data
*data
= iio_priv(indio_dev
);
292 return iio_channel_start_all_cb(data
->cb_buffer
);
295 static int lmp91000_buffer_predisable(struct iio_dev
*indio_dev
)
297 struct lmp91000_data
*data
= iio_priv(indio_dev
);
299 iio_channel_stop_all_cb(data
->cb_buffer
);
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
,
310 static int lmp91000_probe(struct i2c_client
*client
,
311 const struct i2c_device_id
*id
)
313 struct device
*dev
= &client
->dev
;
314 struct lmp91000_data
*data
;
315 struct iio_dev
*indio_dev
;
318 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
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
);
330 data
= iio_priv(indio_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
);
338 data
->trig
= devm_iio_trigger_alloc(data
->dev
, "%s-mux%d",
339 indio_dev
->name
, indio_dev
->id
);
341 dev_err(dev
, "cannot allocate iio trigger.\n");
345 data
->trig
->ops
= &lmp91000_trigger_ops
;
346 data
->trig
->dev
.parent
= dev
;
347 init_completion(&data
->completion
);
349 ret
= lmp91000_read_config(data
);
353 ret
= iio_trigger_set_immutable(iio_channel_cb_get_iio_dev(data
->cb_buffer
),
356 dev_err(dev
, "cannot set immutable trigger.\n");
360 ret
= iio_trigger_register(data
->trig
);
362 dev_err(dev
, "cannot register iio trigger.\n");
366 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
367 &lmp91000_buffer_handler
,
368 &lmp91000_buffer_setup_ops
);
370 goto error_unreg_trigger
;
372 data
->cb_buffer
= iio_channel_get_all_cb(dev
, &lmp91000_buffer_cb
,
375 if (IS_ERR(data
->cb_buffer
)) {
376 if (PTR_ERR(data
->cb_buffer
) == -ENODEV
)
379 ret
= PTR_ERR(data
->cb_buffer
);
381 goto error_unreg_buffer
;
384 data
->adc_chan
= iio_channel_cb_get_channels(data
->cb_buffer
);
386 ret
= iio_device_register(indio_dev
);
388 goto error_unreg_cb_buffer
;
392 error_unreg_cb_buffer
:
393 iio_channel_release_all_cb(data
->cb_buffer
);
396 iio_triggered_buffer_cleanup(indio_dev
);
399 iio_trigger_unregister(data
->trig
);
404 static int lmp91000_remove(struct i2c_client
*client
)
406 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
407 struct lmp91000_data
*data
= iio_priv(indio_dev
);
409 iio_device_unregister(indio_dev
);
411 iio_channel_stop_all_cb(data
->cb_buffer
);
412 iio_channel_release_all_cb(data
->cb_buffer
);
414 iio_triggered_buffer_cleanup(indio_dev
);
415 iio_trigger_unregister(data
->trig
);
420 static const struct of_device_id lmp91000_of_match
[] = {
421 { .compatible
= "ti,lmp91000", },
424 MODULE_DEVICE_TABLE(of
, lmp91000_of_match
);
426 static const struct i2c_device_id lmp91000_id
[] = {
430 MODULE_DEVICE_TABLE(i2c
, lmp91000_id
);
432 static struct i2c_driver lmp91000_driver
= {
434 .name
= LMP91000_DRV_NAME
,
435 .of_match_table
= of_match_ptr(lmp91000_of_match
),
437 .probe
= lmp91000_probe
,
438 .remove
= lmp91000_remove
,
439 .id_table
= lmp91000_id
,
441 module_i2c_driver(lmp91000_driver
);
443 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
444 MODULE_DESCRIPTION("LMP91000 digital potentiostat");
445 MODULE_LICENSE("GPL");