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/mod_devicetable.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_RTD_DATA 0x0e
58 #define ATLAS_REG_PH_TEMP_DATA 0x0e
59 #define ATLAS_REG_PH_DATA 0x16
61 #define ATLAS_REG_EC_PROBE 0x08
62 #define ATLAS_REG_EC_TEMP_DATA 0x10
63 #define ATLAS_REG_EC_DATA 0x18
64 #define ATLAS_REG_TDS_DATA 0x1c
65 #define ATLAS_REG_PSS_DATA 0x20
67 #define ATLAS_REG_ORP_CALIB_STATUS 0x0d
68 #define ATLAS_REG_ORP_DATA 0x0e
70 #define ATLAS_REG_DO_TEMP_DATA 0x12
71 #define ATLAS_REG_DO_DATA 0x22
73 #define ATLAS_PH_INT_TIME_IN_MS 450
74 #define ATLAS_EC_INT_TIME_IN_MS 650
75 #define ATLAS_ORP_INT_TIME_IN_MS 450
76 #define ATLAS_DO_INT_TIME_IN_MS 450
77 #define ATLAS_RTD_INT_TIME_IN_MS 450
88 struct i2c_client
*client
;
89 struct iio_trigger
*trig
;
90 struct atlas_device
*chip
;
91 struct regmap
*regmap
;
93 unsigned int interrupt_enabled
;
95 __be32 buffer
[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
98 static const struct regmap_config atlas_regmap_config
= {
99 .name
= ATLAS_REGMAP_NAME
,
104 static int atlas_buffer_num_channels(const struct iio_chan_spec
*spec
)
108 for (; spec
->type
!= IIO_TIMESTAMP
; spec
++)
114 static const struct iio_chan_spec atlas_ph_channels
[] = {
117 .address
= ATLAS_REG_PH_DATA
,
118 .info_mask_separate
=
119 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
125 .endianness
= IIO_BE
,
128 IIO_CHAN_SOFT_TIMESTAMP(1),
131 .address
= ATLAS_REG_PH_TEMP_DATA
,
132 .info_mask_separate
=
133 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
139 #define ATLAS_CONCENTRATION_CHANNEL(_idx, _addr) \
141 .type = IIO_CONCENTRATION, \
145 .info_mask_separate = \
146 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
147 .scan_index = _idx + 1, \
152 .endianness = IIO_BE, \
156 static const struct iio_chan_spec atlas_ec_channels
[] = {
158 .type
= IIO_ELECTRICALCONDUCTIVITY
,
159 .address
= ATLAS_REG_EC_DATA
,
160 .info_mask_separate
=
161 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
167 .endianness
= IIO_BE
,
170 ATLAS_CONCENTRATION_CHANNEL(0, ATLAS_REG_TDS_DATA
),
171 ATLAS_CONCENTRATION_CHANNEL(1, ATLAS_REG_PSS_DATA
),
172 IIO_CHAN_SOFT_TIMESTAMP(3),
175 .address
= ATLAS_REG_EC_TEMP_DATA
,
176 .info_mask_separate
=
177 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
183 static const struct iio_chan_spec atlas_orp_channels
[] = {
186 .address
= ATLAS_REG_ORP_DATA
,
187 .info_mask_separate
=
188 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
194 .endianness
= IIO_BE
,
197 IIO_CHAN_SOFT_TIMESTAMP(1),
200 static const struct iio_chan_spec atlas_do_channels
[] = {
202 .type
= IIO_CONCENTRATION
,
203 .address
= ATLAS_REG_DO_DATA
,
204 .info_mask_separate
=
205 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
211 .endianness
= IIO_BE
,
214 IIO_CHAN_SOFT_TIMESTAMP(1),
217 .address
= ATLAS_REG_DO_TEMP_DATA
,
218 .info_mask_separate
=
219 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
225 static const struct iio_chan_spec atlas_rtd_channels
[] = {
228 .address
= ATLAS_REG_RTD_DATA
,
229 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
235 .endianness
= IIO_BE
,
238 IIO_CHAN_SOFT_TIMESTAMP(1),
241 static int atlas_check_ph_calibration(struct atlas_data
*data
)
243 struct device
*dev
= &data
->client
->dev
;
247 ret
= regmap_read(data
->regmap
, ATLAS_REG_PH_CALIB_STATUS
, &val
);
251 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_MASK
)) {
252 dev_warn(dev
, "device has not been calibrated\n");
256 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_LOW
))
257 dev_warn(dev
, "device missing low point calibration\n");
259 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_MID
))
260 dev_warn(dev
, "device missing mid point calibration\n");
262 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_HIGH
))
263 dev_warn(dev
, "device missing high point calibration\n");
268 static int atlas_check_ec_calibration(struct atlas_data
*data
)
270 struct device
*dev
= &data
->client
->dev
;
275 ret
= regmap_bulk_read(data
->regmap
, ATLAS_REG_EC_PROBE
, &rval
, 2);
279 val
= be16_to_cpu(rval
);
280 dev_info(dev
, "probe set to K = %d.%.2d", val
/ 100, val
% 100);
282 ret
= regmap_read(data
->regmap
, ATLAS_REG_EC_CALIB_STATUS
, &val
);
286 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_MASK
)) {
287 dev_warn(dev
, "device has not been calibrated\n");
291 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_DRY
))
292 dev_warn(dev
, "device missing dry point calibration\n");
294 if (val
& ATLAS_REG_EC_CALIB_STATUS_SINGLE
) {
295 dev_warn(dev
, "device using single point calibration\n");
297 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_LOW
))
298 dev_warn(dev
, "device missing low point calibration\n");
300 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_HIGH
))
301 dev_warn(dev
, "device missing high point calibration\n");
307 static int atlas_check_orp_calibration(struct atlas_data
*data
)
309 struct device
*dev
= &data
->client
->dev
;
313 ret
= regmap_read(data
->regmap
, ATLAS_REG_ORP_CALIB_STATUS
, &val
);
318 dev_warn(dev
, "device has not been calibrated\n");
323 static int atlas_check_do_calibration(struct atlas_data
*data
)
325 struct device
*dev
= &data
->client
->dev
;
329 ret
= regmap_read(data
->regmap
, ATLAS_REG_DO_CALIB_STATUS
, &val
);
333 if (!(val
& ATLAS_REG_DO_CALIB_STATUS_MASK
)) {
334 dev_warn(dev
, "device has not been calibrated\n");
338 if (!(val
& ATLAS_REG_DO_CALIB_STATUS_PRESSURE
))
339 dev_warn(dev
, "device missing atmospheric pressure calibration\n");
341 if (!(val
& ATLAS_REG_DO_CALIB_STATUS_DO
))
342 dev_warn(dev
, "device missing dissolved oxygen calibration\n");
347 struct atlas_device
{
348 const struct iio_chan_spec
*channels
;
352 int (*calibration
)(struct atlas_data
*data
);
356 static struct atlas_device atlas_devices
[] = {
358 .channels
= atlas_ph_channels
,
360 .data_reg
= ATLAS_REG_PH_DATA
,
361 .calibration
= &atlas_check_ph_calibration
,
362 .delay
= ATLAS_PH_INT_TIME_IN_MS
,
365 .channels
= atlas_ec_channels
,
367 .data_reg
= ATLAS_REG_EC_DATA
,
368 .calibration
= &atlas_check_ec_calibration
,
369 .delay
= ATLAS_EC_INT_TIME_IN_MS
,
372 .channels
= atlas_orp_channels
,
374 .data_reg
= ATLAS_REG_ORP_DATA
,
375 .calibration
= &atlas_check_orp_calibration
,
376 .delay
= ATLAS_ORP_INT_TIME_IN_MS
,
379 .channels
= atlas_do_channels
,
381 .data_reg
= ATLAS_REG_DO_DATA
,
382 .calibration
= &atlas_check_do_calibration
,
383 .delay
= ATLAS_DO_INT_TIME_IN_MS
,
386 .channels
= atlas_rtd_channels
,
388 .data_reg
= ATLAS_REG_RTD_DATA
,
389 .delay
= ATLAS_RTD_INT_TIME_IN_MS
,
393 static int atlas_set_powermode(struct atlas_data
*data
, int on
)
395 return regmap_write(data
->regmap
, ATLAS_REG_PWR_CONTROL
, on
);
398 static int atlas_set_interrupt(struct atlas_data
*data
, bool state
)
400 if (!data
->interrupt_enabled
)
403 return regmap_update_bits(data
->regmap
, ATLAS_REG_INT_CONTROL
,
404 ATLAS_REG_INT_CONTROL_EN
,
405 state
? ATLAS_REG_INT_CONTROL_EN
: 0);
408 static int atlas_buffer_postenable(struct iio_dev
*indio_dev
)
410 struct atlas_data
*data
= iio_priv(indio_dev
);
413 ret
= pm_runtime_get_sync(&data
->client
->dev
);
415 pm_runtime_put_noidle(&data
->client
->dev
);
419 return atlas_set_interrupt(data
, true);
422 static int atlas_buffer_predisable(struct iio_dev
*indio_dev
)
424 struct atlas_data
*data
= iio_priv(indio_dev
);
427 ret
= atlas_set_interrupt(data
, false);
431 pm_runtime_mark_last_busy(&data
->client
->dev
);
432 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
439 static const struct iio_trigger_ops atlas_interrupt_trigger_ops
= {
442 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops
= {
443 .postenable
= atlas_buffer_postenable
,
444 .predisable
= atlas_buffer_predisable
,
447 static void atlas_work_handler(struct irq_work
*work
)
449 struct atlas_data
*data
= container_of(work
, struct atlas_data
, work
);
451 iio_trigger_poll(data
->trig
);
454 static irqreturn_t
atlas_trigger_handler(int irq
, void *private)
456 struct iio_poll_func
*pf
= private;
457 struct iio_dev
*indio_dev
= pf
->indio_dev
;
458 struct atlas_data
*data
= iio_priv(indio_dev
);
459 int channels
= atlas_buffer_num_channels(data
->chip
->channels
);
462 ret
= regmap_bulk_read(data
->regmap
, data
->chip
->data_reg
,
463 &data
->buffer
, sizeof(__be32
) * channels
);
466 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
467 iio_get_time_ns(indio_dev
));
469 iio_trigger_notify_done(indio_dev
->trig
);
474 static irqreturn_t
atlas_interrupt_handler(int irq
, void *private)
476 struct iio_dev
*indio_dev
= private;
477 struct atlas_data
*data
= iio_priv(indio_dev
);
479 irq_work_queue(&data
->work
);
484 static int atlas_read_measurement(struct atlas_data
*data
, int reg
, __be32
*val
)
486 struct device
*dev
= &data
->client
->dev
;
487 int suspended
= pm_runtime_suspended(dev
);
490 ret
= pm_runtime_get_sync(dev
);
492 pm_runtime_put_noidle(dev
);
497 msleep(data
->chip
->delay
);
499 ret
= regmap_bulk_read(data
->regmap
, reg
, val
, sizeof(*val
));
501 pm_runtime_mark_last_busy(dev
);
502 pm_runtime_put_autosuspend(dev
);
507 static int atlas_read_raw(struct iio_dev
*indio_dev
,
508 struct iio_chan_spec
const *chan
,
509 int *val
, int *val2
, long mask
)
511 struct atlas_data
*data
= iio_priv(indio_dev
);
514 case IIO_CHAN_INFO_PROCESSED
:
515 case IIO_CHAN_INFO_RAW
: {
519 switch (chan
->type
) {
521 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
525 case IIO_CONCENTRATION
:
526 case IIO_ELECTRICALCONDUCTIVITY
:
528 ret
= iio_device_claim_direct_mode(indio_dev
);
532 ret
= atlas_read_measurement(data
, chan
->address
, ®
);
534 iio_device_release_direct_mode(indio_dev
);
541 *val
= be32_to_cpu(reg
);
546 case IIO_CHAN_INFO_SCALE
:
547 switch (chan
->type
) {
552 *val
= 1; /* 0.001 */
555 case IIO_ELECTRICALCONDUCTIVITY
:
556 *val
= 1; /* 0.00001 */
559 case IIO_CONCENTRATION
:
560 *val
= 0; /* 0.000000001 */
562 return IIO_VAL_INT_PLUS_NANO
;
570 return IIO_VAL_FRACTIONAL
;
576 static int atlas_write_raw(struct iio_dev
*indio_dev
,
577 struct iio_chan_spec
const *chan
,
578 int val
, int val2
, long mask
)
580 struct atlas_data
*data
= iio_priv(indio_dev
);
581 __be32 reg
= cpu_to_be32(val
/ 10);
583 if (val2
!= 0 || val
< 0 || val
> 20000)
586 if (mask
!= IIO_CHAN_INFO_RAW
|| chan
->type
!= IIO_TEMP
)
589 return regmap_bulk_write(data
->regmap
, chan
->address
,
593 static const struct iio_info atlas_info
= {
594 .read_raw
= atlas_read_raw
,
595 .write_raw
= atlas_write_raw
,
598 static const struct i2c_device_id atlas_id
[] = {
599 { "atlas-ph-sm", ATLAS_PH_SM
},
600 { "atlas-ec-sm", ATLAS_EC_SM
},
601 { "atlas-orp-sm", ATLAS_ORP_SM
},
602 { "atlas-do-sm", ATLAS_DO_SM
},
603 { "atlas-rtd-sm", ATLAS_RTD_SM
},
606 MODULE_DEVICE_TABLE(i2c
, atlas_id
);
608 static const struct of_device_id atlas_dt_ids
[] = {
609 { .compatible
= "atlas,ph-sm", .data
= (void *)ATLAS_PH_SM
, },
610 { .compatible
= "atlas,ec-sm", .data
= (void *)ATLAS_EC_SM
, },
611 { .compatible
= "atlas,orp-sm", .data
= (void *)ATLAS_ORP_SM
, },
612 { .compatible
= "atlas,do-sm", .data
= (void *)ATLAS_DO_SM
, },
613 { .compatible
= "atlas,rtd-sm", .data
= (void *)ATLAS_RTD_SM
, },
616 MODULE_DEVICE_TABLE(of
, atlas_dt_ids
);
618 static int atlas_probe(struct i2c_client
*client
,
619 const struct i2c_device_id
*id
)
621 struct atlas_data
*data
;
622 struct atlas_device
*chip
;
623 struct iio_trigger
*trig
;
624 struct iio_dev
*indio_dev
;
627 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
631 if (!dev_fwnode(&client
->dev
))
632 chip
= &atlas_devices
[id
->driver_data
];
634 chip
= &atlas_devices
[(unsigned long)device_get_match_data(&client
->dev
)];
636 indio_dev
->info
= &atlas_info
;
637 indio_dev
->name
= ATLAS_DRV_NAME
;
638 indio_dev
->channels
= chip
->channels
;
639 indio_dev
->num_channels
= chip
->num_channels
;
640 indio_dev
->modes
= INDIO_BUFFER_SOFTWARE
| INDIO_DIRECT_MODE
;
642 trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-dev%d",
643 indio_dev
->name
, indio_dev
->id
);
648 data
= iio_priv(indio_dev
);
649 data
->client
= client
;
652 trig
->dev
.parent
= indio_dev
->dev
.parent
;
653 trig
->ops
= &atlas_interrupt_trigger_ops
;
654 iio_trigger_set_drvdata(trig
, indio_dev
);
656 i2c_set_clientdata(client
, indio_dev
);
658 data
->regmap
= devm_regmap_init_i2c(client
, &atlas_regmap_config
);
659 if (IS_ERR(data
->regmap
)) {
660 dev_err(&client
->dev
, "regmap initialization failed\n");
661 return PTR_ERR(data
->regmap
);
664 ret
= pm_runtime_set_active(&client
->dev
);
668 ret
= chip
->calibration(data
);
672 ret
= iio_trigger_register(trig
);
674 dev_err(&client
->dev
, "failed to register trigger\n");
678 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
679 &atlas_trigger_handler
, &atlas_buffer_setup_ops
);
681 dev_err(&client
->dev
, "cannot setup iio trigger\n");
682 goto unregister_trigger
;
685 init_irq_work(&data
->work
, atlas_work_handler
);
687 if (client
->irq
> 0) {
688 /* interrupt pin toggles on new conversion */
689 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
690 NULL
, atlas_interrupt_handler
,
691 IRQF_TRIGGER_RISING
|
692 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
697 dev_warn(&client
->dev
,
698 "request irq (%d) failed\n", client
->irq
);
700 data
->interrupt_enabled
= 1;
703 ret
= atlas_set_powermode(data
, 1);
705 dev_err(&client
->dev
, "cannot power device on");
706 goto unregister_buffer
;
709 pm_runtime_enable(&client
->dev
);
710 pm_runtime_set_autosuspend_delay(&client
->dev
, 2500);
711 pm_runtime_use_autosuspend(&client
->dev
);
713 ret
= iio_device_register(indio_dev
);
715 dev_err(&client
->dev
, "unable to register device\n");
722 pm_runtime_disable(&client
->dev
);
723 atlas_set_powermode(data
, 0);
726 iio_triggered_buffer_cleanup(indio_dev
);
729 iio_trigger_unregister(data
->trig
);
734 static int atlas_remove(struct i2c_client
*client
)
736 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
737 struct atlas_data
*data
= iio_priv(indio_dev
);
739 iio_device_unregister(indio_dev
);
740 iio_triggered_buffer_cleanup(indio_dev
);
741 iio_trigger_unregister(data
->trig
);
743 pm_runtime_disable(&client
->dev
);
744 pm_runtime_set_suspended(&client
->dev
);
745 pm_runtime_put_noidle(&client
->dev
);
747 return atlas_set_powermode(data
, 0);
751 static int atlas_runtime_suspend(struct device
*dev
)
753 struct atlas_data
*data
=
754 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
756 return atlas_set_powermode(data
, 0);
759 static int atlas_runtime_resume(struct device
*dev
)
761 struct atlas_data
*data
=
762 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
764 return atlas_set_powermode(data
, 1);
768 static const struct dev_pm_ops atlas_pm_ops
= {
769 SET_RUNTIME_PM_OPS(atlas_runtime_suspend
,
770 atlas_runtime_resume
, NULL
)
773 static struct i2c_driver atlas_driver
= {
775 .name
= ATLAS_DRV_NAME
,
776 .of_match_table
= atlas_dt_ids
,
779 .probe
= atlas_probe
,
780 .remove
= atlas_remove
,
781 .id_table
= atlas_id
,
783 module_i2c_driver(atlas_driver
);
785 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
786 MODULE_DESCRIPTION("Atlas Scientific SM sensors");
787 MODULE_LICENSE("GPL");