1 // SPDX-License-Identifier: GPL-2.0+
3 * atlas-sensor.c - Support for Atlas Scientific OEM SM sensors
5 * Copyright (C) 2015-2019 Konsulko Group
6 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/err.h>
15 #include <linux/irq.h>
16 #include <linux/irq_work.h>
17 #include <linux/i2c.h>
18 #include <linux/of_device.h>
19 #include <linux/regmap.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/pm_runtime.h>
27 #define ATLAS_REGMAP_NAME "atlas_regmap"
28 #define ATLAS_DRV_NAME "atlas"
30 #define ATLAS_REG_DEV_TYPE 0x00
31 #define ATLAS_REG_DEV_VERSION 0x01
33 #define ATLAS_REG_INT_CONTROL 0x04
34 #define ATLAS_REG_INT_CONTROL_EN BIT(3)
36 #define ATLAS_REG_PWR_CONTROL 0x06
38 #define ATLAS_REG_PH_CALIB_STATUS 0x0d
39 #define ATLAS_REG_PH_CALIB_STATUS_MASK 0x07
40 #define ATLAS_REG_PH_CALIB_STATUS_LOW BIT(0)
41 #define ATLAS_REG_PH_CALIB_STATUS_MID BIT(1)
42 #define ATLAS_REG_PH_CALIB_STATUS_HIGH BIT(2)
44 #define ATLAS_REG_EC_CALIB_STATUS 0x0f
45 #define ATLAS_REG_EC_CALIB_STATUS_MASK 0x0f
46 #define ATLAS_REG_EC_CALIB_STATUS_DRY BIT(0)
47 #define ATLAS_REG_EC_CALIB_STATUS_SINGLE BIT(1)
48 #define ATLAS_REG_EC_CALIB_STATUS_LOW BIT(2)
49 #define ATLAS_REG_EC_CALIB_STATUS_HIGH BIT(3)
51 #define ATLAS_REG_DO_CALIB_STATUS 0x09
52 #define ATLAS_REG_DO_CALIB_STATUS_MASK 0x03
53 #define ATLAS_REG_DO_CALIB_STATUS_PRESSURE BIT(0)
54 #define ATLAS_REG_DO_CALIB_STATUS_DO BIT(1)
56 #define ATLAS_REG_PH_TEMP_DATA 0x0e
57 #define ATLAS_REG_PH_DATA 0x16
59 #define ATLAS_REG_EC_PROBE 0x08
60 #define ATLAS_REG_EC_TEMP_DATA 0x10
61 #define ATLAS_REG_EC_DATA 0x18
62 #define ATLAS_REG_TDS_DATA 0x1c
63 #define ATLAS_REG_PSS_DATA 0x20
65 #define ATLAS_REG_ORP_CALIB_STATUS 0x0d
66 #define ATLAS_REG_ORP_DATA 0x0e
68 #define ATLAS_REG_DO_TEMP_DATA 0x12
69 #define ATLAS_REG_DO_DATA 0x22
71 #define ATLAS_PH_INT_TIME_IN_MS 450
72 #define ATLAS_EC_INT_TIME_IN_MS 650
73 #define ATLAS_ORP_INT_TIME_IN_MS 450
74 #define ATLAS_DO_INT_TIME_IN_MS 450
84 struct i2c_client
*client
;
85 struct iio_trigger
*trig
;
86 struct atlas_device
*chip
;
87 struct regmap
*regmap
;
89 unsigned int interrupt_enabled
;
91 __be32 buffer
[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
94 static const struct regmap_config atlas_regmap_config
= {
95 .name
= ATLAS_REGMAP_NAME
,
100 static int atlas_buffer_num_channels(const struct iio_chan_spec
*spec
)
104 for (; spec
->type
!= IIO_TIMESTAMP
; spec
++)
110 static const struct iio_chan_spec atlas_ph_channels
[] = {
113 .address
= ATLAS_REG_PH_DATA
,
114 .info_mask_separate
=
115 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
121 .endianness
= IIO_BE
,
124 IIO_CHAN_SOFT_TIMESTAMP(1),
127 .address
= ATLAS_REG_PH_TEMP_DATA
,
128 .info_mask_separate
=
129 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
135 #define ATLAS_CONCENTRATION_CHANNEL(_idx, _addr) \
137 .type = IIO_CONCENTRATION, \
141 .info_mask_separate = \
142 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
143 .scan_index = _idx + 1, \
148 .endianness = IIO_BE, \
152 static const struct iio_chan_spec atlas_ec_channels
[] = {
154 .type
= IIO_ELECTRICALCONDUCTIVITY
,
155 .address
= ATLAS_REG_EC_DATA
,
156 .info_mask_separate
=
157 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
163 .endianness
= IIO_BE
,
166 ATLAS_CONCENTRATION_CHANNEL(0, ATLAS_REG_TDS_DATA
),
167 ATLAS_CONCENTRATION_CHANNEL(1, ATLAS_REG_PSS_DATA
),
168 IIO_CHAN_SOFT_TIMESTAMP(3),
171 .address
= ATLAS_REG_EC_TEMP_DATA
,
172 .info_mask_separate
=
173 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
179 static const struct iio_chan_spec atlas_orp_channels
[] = {
182 .address
= ATLAS_REG_ORP_DATA
,
183 .info_mask_separate
=
184 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
190 .endianness
= IIO_BE
,
193 IIO_CHAN_SOFT_TIMESTAMP(1),
196 static const struct iio_chan_spec atlas_do_channels
[] = {
198 .type
= IIO_CONCENTRATION
,
199 .address
= ATLAS_REG_DO_DATA
,
200 .info_mask_separate
=
201 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
207 .endianness
= IIO_BE
,
210 IIO_CHAN_SOFT_TIMESTAMP(1),
213 .address
= ATLAS_REG_DO_TEMP_DATA
,
214 .info_mask_separate
=
215 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
221 static int atlas_check_ph_calibration(struct atlas_data
*data
)
223 struct device
*dev
= &data
->client
->dev
;
227 ret
= regmap_read(data
->regmap
, ATLAS_REG_PH_CALIB_STATUS
, &val
);
231 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_MASK
)) {
232 dev_warn(dev
, "device has not been calibrated\n");
236 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_LOW
))
237 dev_warn(dev
, "device missing low point calibration\n");
239 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_MID
))
240 dev_warn(dev
, "device missing mid point calibration\n");
242 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_HIGH
))
243 dev_warn(dev
, "device missing high point calibration\n");
248 static int atlas_check_ec_calibration(struct atlas_data
*data
)
250 struct device
*dev
= &data
->client
->dev
;
255 ret
= regmap_bulk_read(data
->regmap
, ATLAS_REG_EC_PROBE
, &rval
, 2);
259 val
= be16_to_cpu(rval
);
260 dev_info(dev
, "probe set to K = %d.%.2d", val
/ 100, val
% 100);
262 ret
= regmap_read(data
->regmap
, ATLAS_REG_EC_CALIB_STATUS
, &val
);
266 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_MASK
)) {
267 dev_warn(dev
, "device has not been calibrated\n");
271 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_DRY
))
272 dev_warn(dev
, "device missing dry point calibration\n");
274 if (val
& ATLAS_REG_EC_CALIB_STATUS_SINGLE
) {
275 dev_warn(dev
, "device using single point calibration\n");
277 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_LOW
))
278 dev_warn(dev
, "device missing low point calibration\n");
280 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_HIGH
))
281 dev_warn(dev
, "device missing high point calibration\n");
287 static int atlas_check_orp_calibration(struct atlas_data
*data
)
289 struct device
*dev
= &data
->client
->dev
;
293 ret
= regmap_read(data
->regmap
, ATLAS_REG_ORP_CALIB_STATUS
, &val
);
298 dev_warn(dev
, "device has not been calibrated\n");
303 static int atlas_check_do_calibration(struct atlas_data
*data
)
305 struct device
*dev
= &data
->client
->dev
;
309 ret
= regmap_read(data
->regmap
, ATLAS_REG_DO_CALIB_STATUS
, &val
);
313 if (!(val
& ATLAS_REG_DO_CALIB_STATUS_MASK
)) {
314 dev_warn(dev
, "device has not been calibrated\n");
318 if (!(val
& ATLAS_REG_DO_CALIB_STATUS_PRESSURE
))
319 dev_warn(dev
, "device missing atmospheric pressure calibration\n");
321 if (!(val
& ATLAS_REG_DO_CALIB_STATUS_DO
))
322 dev_warn(dev
, "device missing dissolved oxygen calibration\n");
327 struct atlas_device
{
328 const struct iio_chan_spec
*channels
;
332 int (*calibration
)(struct atlas_data
*data
);
336 static struct atlas_device atlas_devices
[] = {
338 .channels
= atlas_ph_channels
,
340 .data_reg
= ATLAS_REG_PH_DATA
,
341 .calibration
= &atlas_check_ph_calibration
,
342 .delay
= ATLAS_PH_INT_TIME_IN_MS
,
345 .channels
= atlas_ec_channels
,
347 .data_reg
= ATLAS_REG_EC_DATA
,
348 .calibration
= &atlas_check_ec_calibration
,
349 .delay
= ATLAS_EC_INT_TIME_IN_MS
,
352 .channels
= atlas_orp_channels
,
354 .data_reg
= ATLAS_REG_ORP_DATA
,
355 .calibration
= &atlas_check_orp_calibration
,
356 .delay
= ATLAS_ORP_INT_TIME_IN_MS
,
359 .channels
= atlas_do_channels
,
361 .data_reg
= ATLAS_REG_DO_DATA
,
362 .calibration
= &atlas_check_do_calibration
,
363 .delay
= ATLAS_DO_INT_TIME_IN_MS
,
367 static int atlas_set_powermode(struct atlas_data
*data
, int on
)
369 return regmap_write(data
->regmap
, ATLAS_REG_PWR_CONTROL
, on
);
372 static int atlas_set_interrupt(struct atlas_data
*data
, bool state
)
374 if (!data
->interrupt_enabled
)
377 return regmap_update_bits(data
->regmap
, ATLAS_REG_INT_CONTROL
,
378 ATLAS_REG_INT_CONTROL_EN
,
379 state
? ATLAS_REG_INT_CONTROL_EN
: 0);
382 static int atlas_buffer_postenable(struct iio_dev
*indio_dev
)
384 struct atlas_data
*data
= iio_priv(indio_dev
);
387 ret
= iio_triggered_buffer_postenable(indio_dev
);
391 ret
= pm_runtime_get_sync(&data
->client
->dev
);
393 pm_runtime_put_noidle(&data
->client
->dev
);
397 return atlas_set_interrupt(data
, true);
400 static int atlas_buffer_predisable(struct iio_dev
*indio_dev
)
402 struct atlas_data
*data
= iio_priv(indio_dev
);
405 ret
= atlas_set_interrupt(data
, false);
409 pm_runtime_mark_last_busy(&data
->client
->dev
);
410 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
414 return iio_triggered_buffer_predisable(indio_dev
);
417 static const struct iio_trigger_ops atlas_interrupt_trigger_ops
= {
420 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops
= {
421 .postenable
= atlas_buffer_postenable
,
422 .predisable
= atlas_buffer_predisable
,
425 static void atlas_work_handler(struct irq_work
*work
)
427 struct atlas_data
*data
= container_of(work
, struct atlas_data
, work
);
429 iio_trigger_poll(data
->trig
);
432 static irqreturn_t
atlas_trigger_handler(int irq
, void *private)
434 struct iio_poll_func
*pf
= private;
435 struct iio_dev
*indio_dev
= pf
->indio_dev
;
436 struct atlas_data
*data
= iio_priv(indio_dev
);
437 int channels
= atlas_buffer_num_channels(data
->chip
->channels
);
440 ret
= regmap_bulk_read(data
->regmap
, data
->chip
->data_reg
,
441 (u8
*) &data
->buffer
,
442 sizeof(__be32
) * channels
);
445 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
446 iio_get_time_ns(indio_dev
));
448 iio_trigger_notify_done(indio_dev
->trig
);
453 static irqreturn_t
atlas_interrupt_handler(int irq
, void *private)
455 struct iio_dev
*indio_dev
= private;
456 struct atlas_data
*data
= iio_priv(indio_dev
);
458 irq_work_queue(&data
->work
);
463 static int atlas_read_measurement(struct atlas_data
*data
, int reg
, __be32
*val
)
465 struct device
*dev
= &data
->client
->dev
;
466 int suspended
= pm_runtime_suspended(dev
);
469 ret
= pm_runtime_get_sync(dev
);
471 pm_runtime_put_noidle(dev
);
476 msleep(data
->chip
->delay
);
478 ret
= regmap_bulk_read(data
->regmap
, reg
, (u8
*) val
, sizeof(*val
));
480 pm_runtime_mark_last_busy(dev
);
481 pm_runtime_put_autosuspend(dev
);
486 static int atlas_read_raw(struct iio_dev
*indio_dev
,
487 struct iio_chan_spec
const *chan
,
488 int *val
, int *val2
, long mask
)
490 struct atlas_data
*data
= iio_priv(indio_dev
);
493 case IIO_CHAN_INFO_RAW
: {
497 switch (chan
->type
) {
499 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
500 (u8
*) ®
, sizeof(reg
));
503 case IIO_CONCENTRATION
:
504 case IIO_ELECTRICALCONDUCTIVITY
:
506 ret
= iio_device_claim_direct_mode(indio_dev
);
510 ret
= atlas_read_measurement(data
, chan
->address
, ®
);
512 iio_device_release_direct_mode(indio_dev
);
519 *val
= be32_to_cpu(reg
);
524 case IIO_CHAN_INFO_SCALE
:
525 switch (chan
->type
) {
530 *val
= 1; /* 0.001 */
533 case IIO_ELECTRICALCONDUCTIVITY
:
534 *val
= 1; /* 0.00001 */
537 case IIO_CONCENTRATION
:
538 *val
= 0; /* 0.000000001 */
540 return IIO_VAL_INT_PLUS_NANO
;
548 return IIO_VAL_FRACTIONAL
;
554 static int atlas_write_raw(struct iio_dev
*indio_dev
,
555 struct iio_chan_spec
const *chan
,
556 int val
, int val2
, long mask
)
558 struct atlas_data
*data
= iio_priv(indio_dev
);
559 __be32 reg
= cpu_to_be32(val
/ 10);
561 if (val2
!= 0 || val
< 0 || val
> 20000)
564 if (mask
!= IIO_CHAN_INFO_RAW
|| chan
->type
!= IIO_TEMP
)
567 return regmap_bulk_write(data
->regmap
, chan
->address
,
571 static const struct iio_info atlas_info
= {
572 .read_raw
= atlas_read_raw
,
573 .write_raw
= atlas_write_raw
,
576 static const struct i2c_device_id atlas_id
[] = {
577 { "atlas-ph-sm", ATLAS_PH_SM
},
578 { "atlas-ec-sm", ATLAS_EC_SM
},
579 { "atlas-orp-sm", ATLAS_ORP_SM
},
580 { "atlas-do-sm", ATLAS_DO_SM
},
583 MODULE_DEVICE_TABLE(i2c
, atlas_id
);
585 static const struct of_device_id atlas_dt_ids
[] = {
586 { .compatible
= "atlas,ph-sm", .data
= (void *)ATLAS_PH_SM
, },
587 { .compatible
= "atlas,ec-sm", .data
= (void *)ATLAS_EC_SM
, },
588 { .compatible
= "atlas,orp-sm", .data
= (void *)ATLAS_ORP_SM
, },
589 { .compatible
= "atlas,do-sm", .data
= (void *)ATLAS_DO_SM
, },
592 MODULE_DEVICE_TABLE(of
, atlas_dt_ids
);
594 static int atlas_probe(struct i2c_client
*client
,
595 const struct i2c_device_id
*id
)
597 struct atlas_data
*data
;
598 struct atlas_device
*chip
;
599 const struct of_device_id
*of_id
;
600 struct iio_trigger
*trig
;
601 struct iio_dev
*indio_dev
;
604 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
608 of_id
= of_match_device(atlas_dt_ids
, &client
->dev
);
610 chip
= &atlas_devices
[id
->driver_data
];
612 chip
= &atlas_devices
[(unsigned long)of_id
->data
];
614 indio_dev
->info
= &atlas_info
;
615 indio_dev
->name
= ATLAS_DRV_NAME
;
616 indio_dev
->channels
= chip
->channels
;
617 indio_dev
->num_channels
= chip
->num_channels
;
618 indio_dev
->modes
= INDIO_BUFFER_SOFTWARE
| INDIO_DIRECT_MODE
;
619 indio_dev
->dev
.parent
= &client
->dev
;
621 trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-dev%d",
622 indio_dev
->name
, indio_dev
->id
);
627 data
= iio_priv(indio_dev
);
628 data
->client
= client
;
631 trig
->dev
.parent
= indio_dev
->dev
.parent
;
632 trig
->ops
= &atlas_interrupt_trigger_ops
;
633 iio_trigger_set_drvdata(trig
, indio_dev
);
635 i2c_set_clientdata(client
, indio_dev
);
637 data
->regmap
= devm_regmap_init_i2c(client
, &atlas_regmap_config
);
638 if (IS_ERR(data
->regmap
)) {
639 dev_err(&client
->dev
, "regmap initialization failed\n");
640 return PTR_ERR(data
->regmap
);
643 ret
= pm_runtime_set_active(&client
->dev
);
647 ret
= chip
->calibration(data
);
651 ret
= iio_trigger_register(trig
);
653 dev_err(&client
->dev
, "failed to register trigger\n");
657 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
658 &atlas_trigger_handler
, &atlas_buffer_setup_ops
);
660 dev_err(&client
->dev
, "cannot setup iio trigger\n");
661 goto unregister_trigger
;
664 init_irq_work(&data
->work
, atlas_work_handler
);
666 if (client
->irq
> 0) {
667 /* interrupt pin toggles on new conversion */
668 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
669 NULL
, atlas_interrupt_handler
,
670 IRQF_TRIGGER_RISING
|
671 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
676 dev_warn(&client
->dev
,
677 "request irq (%d) failed\n", client
->irq
);
679 data
->interrupt_enabled
= 1;
682 ret
= atlas_set_powermode(data
, 1);
684 dev_err(&client
->dev
, "cannot power device on");
685 goto unregister_buffer
;
688 pm_runtime_enable(&client
->dev
);
689 pm_runtime_set_autosuspend_delay(&client
->dev
, 2500);
690 pm_runtime_use_autosuspend(&client
->dev
);
692 ret
= iio_device_register(indio_dev
);
694 dev_err(&client
->dev
, "unable to register device\n");
701 pm_runtime_disable(&client
->dev
);
702 atlas_set_powermode(data
, 0);
705 iio_triggered_buffer_cleanup(indio_dev
);
708 iio_trigger_unregister(data
->trig
);
713 static int atlas_remove(struct i2c_client
*client
)
715 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
716 struct atlas_data
*data
= iio_priv(indio_dev
);
718 iio_device_unregister(indio_dev
);
719 iio_triggered_buffer_cleanup(indio_dev
);
720 iio_trigger_unregister(data
->trig
);
722 pm_runtime_disable(&client
->dev
);
723 pm_runtime_set_suspended(&client
->dev
);
724 pm_runtime_put_noidle(&client
->dev
);
726 return atlas_set_powermode(data
, 0);
730 static int atlas_runtime_suspend(struct device
*dev
)
732 struct atlas_data
*data
=
733 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
735 return atlas_set_powermode(data
, 0);
738 static int atlas_runtime_resume(struct device
*dev
)
740 struct atlas_data
*data
=
741 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
743 return atlas_set_powermode(data
, 1);
747 static const struct dev_pm_ops atlas_pm_ops
= {
748 SET_RUNTIME_PM_OPS(atlas_runtime_suspend
,
749 atlas_runtime_resume
, NULL
)
752 static struct i2c_driver atlas_driver
= {
754 .name
= ATLAS_DRV_NAME
,
755 .of_match_table
= of_match_ptr(atlas_dt_ids
),
758 .probe
= atlas_probe
,
759 .remove
= atlas_remove
,
760 .id_table
= atlas_id
,
762 module_i2c_driver(atlas_driver
);
764 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
765 MODULE_DESCRIPTION("Atlas Scientific SM sensors");
766 MODULE_LICENSE("GPL");