2 * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor
4 * Copyright (C) 2015 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.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/mutex.h>
22 #include <linux/err.h>
23 #include <linux/irq.h>
24 #include <linux/irq_work.h>
25 #include <linux/gpio.h>
26 #include <linux/i2c.h>
27 #include <linux/of_device.h>
28 #include <linux/regmap.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/iio/trigger.h>
32 #include <linux/iio/trigger_consumer.h>
33 #include <linux/iio/triggered_buffer.h>
34 #include <linux/pm_runtime.h>
36 #define ATLAS_REGMAP_NAME "atlas_ph_regmap"
37 #define ATLAS_DRV_NAME "atlas_ph"
39 #define ATLAS_REG_DEV_TYPE 0x00
40 #define ATLAS_REG_DEV_VERSION 0x01
42 #define ATLAS_REG_INT_CONTROL 0x04
43 #define ATLAS_REG_INT_CONTROL_EN BIT(3)
45 #define ATLAS_REG_PWR_CONTROL 0x06
47 #define ATLAS_REG_PH_CALIB_STATUS 0x0d
48 #define ATLAS_REG_PH_CALIB_STATUS_MASK 0x07
49 #define ATLAS_REG_PH_CALIB_STATUS_LOW BIT(0)
50 #define ATLAS_REG_PH_CALIB_STATUS_MID BIT(1)
51 #define ATLAS_REG_PH_CALIB_STATUS_HIGH BIT(2)
53 #define ATLAS_REG_EC_CALIB_STATUS 0x0f
54 #define ATLAS_REG_EC_CALIB_STATUS_MASK 0x0f
55 #define ATLAS_REG_EC_CALIB_STATUS_DRY BIT(0)
56 #define ATLAS_REG_EC_CALIB_STATUS_SINGLE BIT(1)
57 #define ATLAS_REG_EC_CALIB_STATUS_LOW BIT(2)
58 #define ATLAS_REG_EC_CALIB_STATUS_HIGH BIT(3)
60 #define ATLAS_REG_PH_TEMP_DATA 0x0e
61 #define ATLAS_REG_PH_DATA 0x16
63 #define ATLAS_REG_EC_PROBE 0x08
64 #define ATLAS_REG_EC_TEMP_DATA 0x10
65 #define ATLAS_REG_EC_DATA 0x18
66 #define ATLAS_REG_TDS_DATA 0x1c
67 #define ATLAS_REG_PSS_DATA 0x20
69 #define ATLAS_PH_INT_TIME_IN_US 450000
70 #define ATLAS_EC_INT_TIME_IN_US 650000
78 struct i2c_client *client;
79 struct iio_trigger *trig;
80 struct atlas_device *chip;
81 struct regmap *regmap;
84 __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
87 static const struct regmap_range atlas_volatile_ranges[] = {
88 regmap_reg_range(ATLAS_REG_INT_CONTROL, ATLAS_REG_INT_CONTROL),
89 regmap_reg_range(ATLAS_REG_PH_DATA, ATLAS_REG_PH_DATA + 4),
90 regmap_reg_range(ATLAS_REG_EC_DATA, ATLAS_REG_PSS_DATA + 4),
93 static const struct regmap_access_table atlas_volatile_table = {
94 .yes_ranges = atlas_volatile_ranges,
95 .n_yes_ranges = ARRAY_SIZE(atlas_volatile_ranges),
98 static const struct regmap_config atlas_regmap_config = {
99 .name = ATLAS_REGMAP_NAME,
104 .volatile_table = &atlas_volatile_table,
105 .max_register = ATLAS_REG_PSS_DATA + 4,
106 .cache_type = REGCACHE_RBTREE,
109 static const struct iio_chan_spec atlas_ph_channels[] = {
112 .address = ATLAS_REG_PH_DATA,
113 .info_mask_separate =
114 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
120 .endianness = IIO_BE,
123 IIO_CHAN_SOFT_TIMESTAMP(1),
126 .address = ATLAS_REG_PH_TEMP_DATA,
127 .info_mask_separate =
128 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
134 #define ATLAS_EC_CHANNEL(_idx, _addr) \
136 .type = IIO_CONCENTRATION, \
140 .info_mask_separate = \
141 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
142 .scan_index = _idx + 1, \
147 .endianness = IIO_BE, \
151 static const struct iio_chan_spec atlas_ec_channels[] = {
153 .type = IIO_ELECTRICALCONDUCTIVITY,
154 .address = ATLAS_REG_EC_DATA,
155 .info_mask_separate =
156 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
162 .endianness = IIO_BE,
165 ATLAS_EC_CHANNEL(0, ATLAS_REG_TDS_DATA),
166 ATLAS_EC_CHANNEL(1, ATLAS_REG_PSS_DATA),
167 IIO_CHAN_SOFT_TIMESTAMP(3),
170 .address = ATLAS_REG_EC_TEMP_DATA,
171 .info_mask_separate =
172 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
178 static int atlas_check_ph_calibration(struct atlas_data *data)
180 struct device *dev = &data->client->dev;
184 ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val);
188 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) {
189 dev_warn(dev, "device has not been calibrated\n");
193 if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW))
194 dev_warn(dev, "device missing low point calibration\n");
196 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID))
197 dev_warn(dev, "device missing mid point calibration\n");
199 if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH))
200 dev_warn(dev, "device missing high point calibration\n");
205 static int atlas_check_ec_calibration(struct atlas_data *data)
207 struct device *dev = &data->client->dev;
211 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &val, 2);
215 dev_info(dev, "probe set to K = %d.%.2d", be16_to_cpu(val) / 100,
216 be16_to_cpu(val) % 100);
218 ret = regmap_read(data->regmap, ATLAS_REG_EC_CALIB_STATUS, &val);
222 if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) {
223 dev_warn(dev, "device has not been calibrated\n");
227 if (!(val & ATLAS_REG_EC_CALIB_STATUS_DRY))
228 dev_warn(dev, "device missing dry point calibration\n");
230 if (val & ATLAS_REG_EC_CALIB_STATUS_SINGLE) {
231 dev_warn(dev, "device using single point calibration\n");
233 if (!(val & ATLAS_REG_EC_CALIB_STATUS_LOW))
234 dev_warn(dev, "device missing low point calibration\n");
236 if (!(val & ATLAS_REG_EC_CALIB_STATUS_HIGH))
237 dev_warn(dev, "device missing high point calibration\n");
243 struct atlas_device {
244 const struct iio_chan_spec *channels;
248 int (*calibration)(struct atlas_data *data);
252 static struct atlas_device atlas_devices[] = {
254 .channels = atlas_ph_channels,
256 .data_reg = ATLAS_REG_PH_DATA,
257 .calibration = &atlas_check_ph_calibration,
258 .delay = ATLAS_PH_INT_TIME_IN_US,
261 .channels = atlas_ec_channels,
263 .data_reg = ATLAS_REG_EC_DATA,
264 .calibration = &atlas_check_ec_calibration,
265 .delay = ATLAS_EC_INT_TIME_IN_US,
270 static int atlas_set_powermode(struct atlas_data *data, int on)
272 return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
275 static int atlas_set_interrupt(struct atlas_data *data, bool state)
277 return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL,
278 ATLAS_REG_INT_CONTROL_EN,
279 state ? ATLAS_REG_INT_CONTROL_EN : 0);
282 static int atlas_buffer_postenable(struct iio_dev *indio_dev)
284 struct atlas_data *data = iio_priv(indio_dev);
287 ret = iio_triggered_buffer_postenable(indio_dev);
291 ret = pm_runtime_get_sync(&data->client->dev);
293 pm_runtime_put_noidle(&data->client->dev);
297 return atlas_set_interrupt(data, true);
300 static int atlas_buffer_predisable(struct iio_dev *indio_dev)
302 struct atlas_data *data = iio_priv(indio_dev);
305 ret = iio_triggered_buffer_predisable(indio_dev);
309 ret = atlas_set_interrupt(data, false);
313 pm_runtime_mark_last_busy(&data->client->dev);
314 return pm_runtime_put_autosuspend(&data->client->dev);
317 static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
318 .owner = THIS_MODULE,
321 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
322 .postenable = atlas_buffer_postenable,
323 .predisable = atlas_buffer_predisable,
326 static void atlas_work_handler(struct irq_work *work)
328 struct atlas_data *data = container_of(work, struct atlas_data, work);
330 iio_trigger_poll(data->trig);
333 static irqreturn_t atlas_trigger_handler(int irq, void *private)
335 struct iio_poll_func *pf = private;
336 struct iio_dev *indio_dev = pf->indio_dev;
337 struct atlas_data *data = iio_priv(indio_dev);
340 ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
341 (u8 *) &data->buffer,
342 sizeof(__be32) * (data->chip->num_channels - 2));
345 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
346 iio_get_time_ns(indio_dev));
348 iio_trigger_notify_done(indio_dev->trig);
353 static irqreturn_t atlas_interrupt_handler(int irq, void *private)
355 struct iio_dev *indio_dev = private;
356 struct atlas_data *data = iio_priv(indio_dev);
358 irq_work_queue(&data->work);
363 static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
365 struct device *dev = &data->client->dev;
366 int suspended = pm_runtime_suspended(dev);
369 ret = pm_runtime_get_sync(dev);
371 pm_runtime_put_noidle(dev);
376 usleep_range(data->chip->delay, data->chip->delay + 100000);
378 ret = regmap_bulk_read(data->regmap, reg, (u8 *) val, sizeof(*val));
380 pm_runtime_mark_last_busy(dev);
381 pm_runtime_put_autosuspend(dev);
386 static int atlas_read_raw(struct iio_dev *indio_dev,
387 struct iio_chan_spec const *chan,
388 int *val, int *val2, long mask)
390 struct atlas_data *data = iio_priv(indio_dev);
393 case IIO_CHAN_INFO_RAW: {
397 switch (chan->type) {
399 ret = regmap_bulk_read(data->regmap, chan->address,
400 (u8 *) ®, sizeof(reg));
403 case IIO_CONCENTRATION:
404 case IIO_ELECTRICALCONDUCTIVITY:
405 mutex_lock(&indio_dev->mlock);
407 if (iio_buffer_enabled(indio_dev))
410 ret = atlas_read_measurement(data,
411 chan->address, ®);
413 mutex_unlock(&indio_dev->mlock);
420 *val = be32_to_cpu(reg);
425 case IIO_CHAN_INFO_SCALE:
426 switch (chan->type) {
432 *val = 1; /* 0.001 */
435 case IIO_ELECTRICALCONDUCTIVITY:
436 *val = 1; /* 0.00001 */
439 case IIO_CONCENTRATION:
440 *val = 0; /* 0.000000001 */
442 return IIO_VAL_INT_PLUS_NANO;
446 return IIO_VAL_FRACTIONAL;
452 static int atlas_write_raw(struct iio_dev *indio_dev,
453 struct iio_chan_spec const *chan,
454 int val, int val2, long mask)
456 struct atlas_data *data = iio_priv(indio_dev);
457 __be32 reg = cpu_to_be32(val);
459 if (val2 != 0 || val < 0 || val > 20000)
462 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
465 return regmap_bulk_write(data->regmap, chan->address,
469 static const struct iio_info atlas_info = {
470 .driver_module = THIS_MODULE,
471 .read_raw = atlas_read_raw,
472 .write_raw = atlas_write_raw,
475 static const struct i2c_device_id atlas_id[] = {
476 { "atlas-ph-sm", ATLAS_PH_SM},
477 { "atlas-ec-sm", ATLAS_EC_SM},
480 MODULE_DEVICE_TABLE(i2c, atlas_id);
482 static const struct of_device_id atlas_dt_ids[] = {
483 { .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, },
484 { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, },
487 MODULE_DEVICE_TABLE(of, atlas_dt_ids);
489 static int atlas_probe(struct i2c_client *client,
490 const struct i2c_device_id *id)
492 struct atlas_data *data;
493 struct atlas_device *chip;
494 const struct of_device_id *of_id;
495 struct iio_trigger *trig;
496 struct iio_dev *indio_dev;
499 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
503 of_id = of_match_device(atlas_dt_ids, &client->dev);
505 chip = &atlas_devices[id->driver_data];
507 chip = &atlas_devices[(unsigned long)of_id->data];
509 indio_dev->info = &atlas_info;
510 indio_dev->name = ATLAS_DRV_NAME;
511 indio_dev->channels = chip->channels;
512 indio_dev->num_channels = chip->num_channels;
513 indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
514 indio_dev->dev.parent = &client->dev;
516 trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
517 indio_dev->name, indio_dev->id);
522 data = iio_priv(indio_dev);
523 data->client = client;
526 trig->dev.parent = indio_dev->dev.parent;
527 trig->ops = &atlas_interrupt_trigger_ops;
528 iio_trigger_set_drvdata(trig, indio_dev);
530 i2c_set_clientdata(client, indio_dev);
532 data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config);
533 if (IS_ERR(data->regmap)) {
534 dev_err(&client->dev, "regmap initialization failed\n");
535 return PTR_ERR(data->regmap);
538 ret = pm_runtime_set_active(&client->dev);
542 if (client->irq <= 0) {
543 dev_err(&client->dev, "no valid irq defined\n");
547 ret = chip->calibration(data);
551 ret = iio_trigger_register(trig);
553 dev_err(&client->dev, "failed to register trigger\n");
557 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
558 &atlas_trigger_handler, &atlas_buffer_setup_ops);
560 dev_err(&client->dev, "cannot setup iio trigger\n");
561 goto unregister_trigger;
564 init_irq_work(&data->work, atlas_work_handler);
566 /* interrupt pin toggles on new conversion */
567 ret = devm_request_threaded_irq(&client->dev, client->irq,
568 NULL, atlas_interrupt_handler,
569 IRQF_TRIGGER_RISING |
570 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
574 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
575 goto unregister_buffer;
578 ret = atlas_set_powermode(data, 1);
580 dev_err(&client->dev, "cannot power device on");
581 goto unregister_buffer;
584 pm_runtime_enable(&client->dev);
585 pm_runtime_set_autosuspend_delay(&client->dev, 2500);
586 pm_runtime_use_autosuspend(&client->dev);
588 ret = iio_device_register(indio_dev);
590 dev_err(&client->dev, "unable to register device\n");
597 pm_runtime_disable(&client->dev);
598 atlas_set_powermode(data, 0);
601 iio_triggered_buffer_cleanup(indio_dev);
604 iio_trigger_unregister(data->trig);
609 static int atlas_remove(struct i2c_client *client)
611 struct iio_dev *indio_dev = i2c_get_clientdata(client);
612 struct atlas_data *data = iio_priv(indio_dev);
614 iio_device_unregister(indio_dev);
615 iio_triggered_buffer_cleanup(indio_dev);
616 iio_trigger_unregister(data->trig);
618 pm_runtime_disable(&client->dev);
619 pm_runtime_set_suspended(&client->dev);
620 pm_runtime_put_noidle(&client->dev);
622 return atlas_set_powermode(data, 0);
626 static int atlas_runtime_suspend(struct device *dev)
628 struct atlas_data *data =
629 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
631 return atlas_set_powermode(data, 0);
634 static int atlas_runtime_resume(struct device *dev)
636 struct atlas_data *data =
637 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
639 return atlas_set_powermode(data, 1);
643 static const struct dev_pm_ops atlas_pm_ops = {
644 SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
645 atlas_runtime_resume, NULL)
648 static struct i2c_driver atlas_driver = {
650 .name = ATLAS_DRV_NAME,
651 .of_match_table = of_match_ptr(atlas_dt_ids),
654 .probe = atlas_probe,
655 .remove = atlas_remove,
656 .id_table = atlas_id,
658 module_i2c_driver(atlas_driver);
660 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
661 MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
662 MODULE_LICENSE("GPL");