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 .driver_module = THIS_MODULE, 211 .read_raw = lmp91000_read_raw, 212 }; 213 214 static int lmp91000_read_config(struct lmp91000_data *data) 215 { 216 struct device *dev = data->dev; 217 struct device_node *np = dev->of_node; 218 unsigned int reg, val; 219 int i, ret; 220 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; 228 } 229 } 230 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; 237 } 238 } 239 240 if (ret) { 241 dev_err(dev, "invalid ti,tia-gain-ohm %d\n", val); 242 return ret; 243 } 244 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); 249 } 250 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; 257 } 258 } 259 260 if (ret) { 261 dev_err(dev, "invalid ti,rload-ohm %d\n", val); 262 return ret; 263 } 264 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); 270 271 return 0; 272 } 273 274 static int lmp91000_buffer_cb(const void *val, void *private) 275 { 276 struct iio_dev *indio_dev = private; 277 struct lmp91000_data *data = iio_priv(indio_dev); 278 279 data->buffer[data->chan_select] = *((int *)val); 280 complete_all(&data->completion); 281 282 return 0; 283 } 284 285 static const struct iio_trigger_ops lmp91000_trigger_ops = { 286 .owner = THIS_MODULE, 287 }; 288 289 290 static int lmp91000_buffer_preenable(struct iio_dev *indio_dev) 291 { 292 struct lmp91000_data *data = iio_priv(indio_dev); 293 294 return iio_channel_start_all_cb(data->cb_buffer); 295 } 296 297 static int lmp91000_buffer_predisable(struct iio_dev *indio_dev) 298 { 299 struct lmp91000_data *data = iio_priv(indio_dev); 300 301 iio_channel_stop_all_cb(data->cb_buffer); 302 303 return 0; 304 } 305 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, 310 }; 311 312 static int lmp91000_probe(struct i2c_client *client, 313 const struct i2c_device_id *id) 314 { 315 struct device *dev = &client->dev; 316 struct lmp91000_data *data; 317 struct iio_dev *indio_dev; 318 int ret; 319 320 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 321 if (!indio_dev) 322 return -ENOMEM; 323 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); 330 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); 337 } 338 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; 344 } 345 346 data->trig->ops = &lmp91000_trigger_ops; 347 data->trig->dev.parent = dev; 348 init_completion(&data->completion); 349 350 ret = lmp91000_read_config(data); 351 if (ret) 352 return ret; 353 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; 359 } 360 361 ret = iio_trigger_register(data->trig); 362 if (ret) { 363 dev_err(dev, "cannot register iio trigger.\n"); 364 return ret; 365 } 366 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; 372 373 data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb, 374 indio_dev); 375 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); 381 382 goto error_unreg_buffer; 383 } 384 385 data->adc_chan = iio_channel_cb_get_channels(data->cb_buffer); 386 387 ret = iio_device_register(indio_dev); 388 if (ret) 389 goto error_unreg_cb_buffer; 390 391 return 0; 392 393 error_unreg_cb_buffer: 394 iio_channel_release_all_cb(data->cb_buffer); 395 396 error_unreg_buffer: 397 iio_triggered_buffer_cleanup(indio_dev); 398 399 error_unreg_trigger: 400 iio_trigger_unregister(data->trig); 401 402 return ret; 403 } 404 405 static int lmp91000_remove(struct i2c_client *client) 406 { 407 struct iio_dev *indio_dev = i2c_get_clientdata(client); 408 struct lmp91000_data *data = iio_priv(indio_dev); 409 410 iio_device_unregister(indio_dev); 411 412 iio_channel_stop_all_cb(data->cb_buffer); 413 iio_channel_release_all_cb(data->cb_buffer); 414 415 iio_triggered_buffer_cleanup(indio_dev); 416 iio_trigger_unregister(data->trig); 417 418 return 0; 419 } 420 421 static const struct of_device_id lmp91000_of_match[] = { 422 { .compatible = "ti,lmp91000", }, 423 { }, 424 }; 425 MODULE_DEVICE_TABLE(of, lmp91000_of_match); 426 427 static const struct i2c_device_id lmp91000_id[] = { 428 { "lmp91000", 0 }, 429 {} 430 }; 431 MODULE_DEVICE_TABLE(i2c, lmp91000_id); 432 433 static struct i2c_driver lmp91000_driver = { 434 .driver = { 435 .name = LMP91000_DRV_NAME, 436 .of_match_table = of_match_ptr(lmp91000_of_match), 437 }, 438 .probe = lmp91000_probe, 439 .remove = lmp91000_remove, 440 .id_table = lmp91000_id, 441 }; 442 module_i2c_driver(lmp91000_driver); 443 444 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>"); 445 MODULE_DESCRIPTION("LMP91000 digital potentiostat"); 446 MODULE_LICENSE("GPL"); 447