sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / iio / potentiostat / lmp91000.c
blobe22714365022c2d9252dcd7575a404591dd56143
1 /*
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>
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>
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,
46 120000, 350000 };
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 = {
68 .reg_bits = 8,
69 .val_bits = 8,
72 struct lmp91000_data {
73 struct regmap *regmap;
74 struct device *dev;
76 struct iio_trigger *trig;
77 struct iio_cb_buffer *cb_buffer;
78 struct iio_channel *adc_chan;
80 struct completion completion;
81 u8 chan_select;
83 u32 buffer[4]; /* 64-bit data + 64-bit timestamp */
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,
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,
111 static int lmp91000_read(struct lmp91000_data *data, int channel, int *val)
113 int state, ret;
115 ret = regmap_read(data->regmap, LMP91000_REG_MODECN, &state);
116 if (ret)
117 return -EINVAL;
119 ret = regmap_write(data->regmap, LMP91000_REG_MODECN, channel);
120 if (ret)
121 return -EINVAL;
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);
134 if (!ret)
135 return -ETIMEDOUT;
137 *val = data->buffer[data->chan_select];
139 return 0;
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);
147 int ret, val;
149 memset(data->buffer, 0, sizeof(data->buffer));
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));
158 iio_trigger_notify_done(indio_dev->trig);
160 return IRQ_HANDLED;
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);
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);
174 if (ret)
175 return ret;
177 ret = lmp91000_read(data, chan->address, val);
179 iio_channel_stop_all_cb(data->cb_buffer);
181 if (ret)
182 return ret;
184 if (mask == IIO_CHAN_INFO_PROCESSED) {
185 int tmp, i;
187 ret = iio_convert_raw_to_processed(data->adc_chan,
188 *val, &tmp, 1);
189 if (ret)
190 return ret;
192 for (i = 0; i < ARRAY_SIZE(lmp91000_temp_lut); i++)
193 if (lmp91000_temp_lut[i] < tmp)
194 break;
196 *val = (LMP91000_TEMP_BASE + i) * 1000;
198 return IIO_VAL_INT;
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);
206 return -EINVAL;
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;
219 int i, ret;
221 ret = of_property_read_u32(np, "ti,tia-gain-ohm", &val);
222 if (ret) {
223 if (of_property_read_bool(np, "ti,external-tia-resistor"))
224 val = 0;
225 else {
226 dev_err(dev, "no ti,tia-gain-ohm defined");
227 return ret;
231 ret = -EINVAL;
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;
235 ret = 0;
236 break;
240 if (ret) {
241 dev_err(dev, "invalid ti,tia-gain-ohm %d\n", val);
242 return ret;
245 ret = of_property_read_u32(np, "ti,rload-ohm", &val);
246 if (ret) {
247 val = 100;
248 dev_info(dev, "no ti,rload-ohm defined, default to %d\n", val);
251 ret = -EINVAL;
252 for (i = 0; i < ARRAY_SIZE(lmp91000_rload); i++) {
253 if (lmp91000_rload[i] == val) {
254 reg |= i;
255 ret = 0;
256 break;
260 if (ret) {
261 dev_err(dev, "invalid ti,rload-ohm %d\n", val);
262 return ret;
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);
271 return 0;
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);
282 return 0;
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);
303 return 0;
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;
318 int ret;
320 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
321 if (!indio_dev)
322 return -ENOMEM;
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);
332 data->dev = 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);
341 if (!data->trig) {
342 dev_err(dev, "cannot allocate iio trigger.\n");
343 return -ENOMEM;
346 data->trig->ops = &lmp91000_trigger_ops;
347 data->trig->dev.parent = dev;
348 init_completion(&data->completion);
350 ret = lmp91000_read_config(data);
351 if (ret)
352 return ret;
354 ret = iio_trigger_set_immutable(iio_channel_cb_get_iio_dev(data->cb_buffer),
355 data->trig);
356 if (ret) {
357 dev_err(dev, "cannot set immutable trigger.\n");
358 return ret;
361 ret = iio_trigger_register(data->trig);
362 if (ret) {
363 dev_err(dev, "cannot register iio trigger.\n");
364 return ret;
367 ret = iio_triggered_buffer_setup(indio_dev, NULL,
368 &lmp91000_buffer_handler,
369 &lmp91000_buffer_setup_ops);
370 if (ret)
371 goto error_unreg_trigger;
373 data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
374 indio_dev);
376 if (IS_ERR(data->cb_buffer)) {
377 if (PTR_ERR(data->cb_buffer) == -ENODEV)
378 ret = -EPROBE_DEFER;
379 else
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);
388 if (ret)
389 goto error_unreg_cb_buffer;
391 return 0;
393 error_unreg_cb_buffer:
394 iio_channel_release_all_cb(data->cb_buffer);
396 error_unreg_buffer:
397 iio_triggered_buffer_cleanup(indio_dev);
399 error_unreg_trigger:
400 iio_trigger_unregister(data->trig);
402 return ret;
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);
418 return 0;
421 static const struct of_device_id lmp91000_of_match[] = {
422 { .compatible = "ti,lmp91000", },
423 { },
425 MODULE_DEVICE_TABLE(of, lmp91000_of_match);
427 static const struct i2c_device_id lmp91000_id[] = {
428 { "lmp91000", 0 },
431 MODULE_DEVICE_TABLE(i2c, lmp91000_id);
433 static struct i2c_driver lmp91000_driver = {
434 .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");