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 .driver_module
= THIS_MODULE
,
211 .read_raw
= lmp91000_read_raw
,
214 static int lmp91000_read_config(struct lmp91000_data
*data
)
216 struct device
*dev
= data
->dev
;
217 struct device_node
*np
= dev
->of_node
;
218 unsigned int reg
, val
;
221 ret
= of_property_read_u32(np
, "ti,tia-gain-ohm", &val
);
223 if (of_property_read_bool(np
, "ti,external-tia-resistor"))
226 dev_err(dev
, "no ti,tia-gain-ohm defined");
232 for (i
= 0; i
< ARRAY_SIZE(lmp91000_tia_gain
); i
++) {
233 if (lmp91000_tia_gain
[i
] == val
) {
234 reg
= i
<< LMP91000_REG_TIACN_GAIN_SHIFT
;
241 dev_err(dev
, "invalid ti,tia-gain-ohm %d\n", val
);
245 ret
= of_property_read_u32(np
, "ti,rload-ohm", &val
);
248 dev_info(dev
, "no ti,rload-ohm defined, default to %d\n", val
);
252 for (i
= 0; i
< ARRAY_SIZE(lmp91000_rload
); i
++) {
253 if (lmp91000_rload
[i
] == val
) {
261 dev_err(dev
, "invalid ti,rload-ohm %d\n", val
);
265 regmap_write(data
->regmap
, LMP91000_REG_LOCK
, 0);
266 regmap_write(data
->regmap
, LMP91000_REG_TIACN
, reg
);
267 regmap_write(data
->regmap
, LMP91000_REG_REFCN
, LMP91000_REG_REFCN_EXT_REF
268 | LMP91000_REG_REFCN_50_ZERO
);
269 regmap_write(data
->regmap
, LMP91000_REG_LOCK
, 1);
274 static int lmp91000_buffer_cb(const void *val
, void *private)
276 struct iio_dev
*indio_dev
= private;
277 struct lmp91000_data
*data
= iio_priv(indio_dev
);
279 data
->buffer
[data
->chan_select
] = *((int *)val
);
280 complete_all(&data
->completion
);
285 static const struct iio_trigger_ops lmp91000_trigger_ops
= {
286 .owner
= THIS_MODULE
,
290 static int lmp91000_buffer_preenable(struct iio_dev
*indio_dev
)
292 struct lmp91000_data
*data
= iio_priv(indio_dev
);
294 return iio_channel_start_all_cb(data
->cb_buffer
);
297 static int lmp91000_buffer_predisable(struct iio_dev
*indio_dev
)
299 struct lmp91000_data
*data
= iio_priv(indio_dev
);
301 iio_channel_stop_all_cb(data
->cb_buffer
);
306 static const struct iio_buffer_setup_ops lmp91000_buffer_setup_ops
= {
307 .preenable
= lmp91000_buffer_preenable
,
308 .postenable
= iio_triggered_buffer_postenable
,
309 .predisable
= lmp91000_buffer_predisable
,
312 static int lmp91000_probe(struct i2c_client
*client
,
313 const struct i2c_device_id
*id
)
315 struct device
*dev
= &client
->dev
;
316 struct lmp91000_data
*data
;
317 struct iio_dev
*indio_dev
;
320 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
324 indio_dev
->info
= &lmp91000_info
;
325 indio_dev
->channels
= lmp91000_channels
;
326 indio_dev
->num_channels
= ARRAY_SIZE(lmp91000_channels
);
327 indio_dev
->name
= LMP91000_DRV_NAME
;
328 indio_dev
->modes
= INDIO_DIRECT_MODE
;
329 i2c_set_clientdata(client
, indio_dev
);
331 data
= iio_priv(indio_dev
);
333 data
->regmap
= devm_regmap_init_i2c(client
, &lmp91000_regmap_config
);
334 if (IS_ERR(data
->regmap
)) {
335 dev_err(dev
, "regmap initialization failed.\n");
336 return PTR_ERR(data
->regmap
);
339 data
->trig
= devm_iio_trigger_alloc(data
->dev
, "%s-mux%d",
340 indio_dev
->name
, indio_dev
->id
);
342 dev_err(dev
, "cannot allocate iio trigger.\n");
346 data
->trig
->ops
= &lmp91000_trigger_ops
;
347 data
->trig
->dev
.parent
= dev
;
348 init_completion(&data
->completion
);
350 ret
= lmp91000_read_config(data
);
354 ret
= iio_trigger_set_immutable(iio_channel_cb_get_iio_dev(data
->cb_buffer
),
357 dev_err(dev
, "cannot set immutable trigger.\n");
361 ret
= iio_trigger_register(data
->trig
);
363 dev_err(dev
, "cannot register iio trigger.\n");
367 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
368 &lmp91000_buffer_handler
,
369 &lmp91000_buffer_setup_ops
);
371 goto error_unreg_trigger
;
373 data
->cb_buffer
= iio_channel_get_all_cb(dev
, &lmp91000_buffer_cb
,
376 if (IS_ERR(data
->cb_buffer
)) {
377 if (PTR_ERR(data
->cb_buffer
) == -ENODEV
)
380 ret
= PTR_ERR(data
->cb_buffer
);
382 goto error_unreg_buffer
;
385 data
->adc_chan
= iio_channel_cb_get_channels(data
->cb_buffer
);
387 ret
= iio_device_register(indio_dev
);
389 goto error_unreg_cb_buffer
;
393 error_unreg_cb_buffer
:
394 iio_channel_release_all_cb(data
->cb_buffer
);
397 iio_triggered_buffer_cleanup(indio_dev
);
400 iio_trigger_unregister(data
->trig
);
405 static int lmp91000_remove(struct i2c_client
*client
)
407 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
408 struct lmp91000_data
*data
= iio_priv(indio_dev
);
410 iio_device_unregister(indio_dev
);
412 iio_channel_stop_all_cb(data
->cb_buffer
);
413 iio_channel_release_all_cb(data
->cb_buffer
);
415 iio_triggered_buffer_cleanup(indio_dev
);
416 iio_trigger_unregister(data
->trig
);
421 static const struct of_device_id lmp91000_of_match
[] = {
422 { .compatible
= "ti,lmp91000", },
425 MODULE_DEVICE_TABLE(of
, lmp91000_of_match
);
427 static const struct i2c_device_id lmp91000_id
[] = {
431 MODULE_DEVICE_TABLE(i2c
, lmp91000_id
);
433 static struct i2c_driver lmp91000_driver
= {
435 .name
= LMP91000_DRV_NAME
,
436 .of_match_table
= of_match_ptr(lmp91000_of_match
),
438 .probe
= lmp91000_probe
,
439 .remove
= lmp91000_remove
,
440 .id_table
= lmp91000_id
,
442 module_i2c_driver(lmp91000_driver
);
444 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
445 MODULE_DESCRIPTION("LMP91000 digital potentiostat");
446 MODULE_LICENSE("GPL");