1 // SPDX-License-Identifier: GPL-2.0+
3 * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor
5 * Copyright (C) 2015-2018 Matt Ranostay
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/gpio.h>
18 #include <linux/i2c.h>
19 #include <linux/of_device.h>
20 #include <linux/regmap.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/pm_runtime.h>
28 #define ATLAS_REGMAP_NAME "atlas_ph_regmap"
29 #define ATLAS_DRV_NAME "atlas_ph"
31 #define ATLAS_REG_DEV_TYPE 0x00
32 #define ATLAS_REG_DEV_VERSION 0x01
34 #define ATLAS_REG_INT_CONTROL 0x04
35 #define ATLAS_REG_INT_CONTROL_EN BIT(3)
37 #define ATLAS_REG_PWR_CONTROL 0x06
39 #define ATLAS_REG_PH_CALIB_STATUS 0x0d
40 #define ATLAS_REG_PH_CALIB_STATUS_MASK 0x07
41 #define ATLAS_REG_PH_CALIB_STATUS_LOW BIT(0)
42 #define ATLAS_REG_PH_CALIB_STATUS_MID BIT(1)
43 #define ATLAS_REG_PH_CALIB_STATUS_HIGH BIT(2)
45 #define ATLAS_REG_EC_CALIB_STATUS 0x0f
46 #define ATLAS_REG_EC_CALIB_STATUS_MASK 0x0f
47 #define ATLAS_REG_EC_CALIB_STATUS_DRY BIT(0)
48 #define ATLAS_REG_EC_CALIB_STATUS_SINGLE BIT(1)
49 #define ATLAS_REG_EC_CALIB_STATUS_LOW BIT(2)
50 #define ATLAS_REG_EC_CALIB_STATUS_HIGH BIT(3)
52 #define ATLAS_REG_PH_TEMP_DATA 0x0e
53 #define ATLAS_REG_PH_DATA 0x16
55 #define ATLAS_REG_EC_PROBE 0x08
56 #define ATLAS_REG_EC_TEMP_DATA 0x10
57 #define ATLAS_REG_EC_DATA 0x18
58 #define ATLAS_REG_TDS_DATA 0x1c
59 #define ATLAS_REG_PSS_DATA 0x20
61 #define ATLAS_REG_ORP_CALIB_STATUS 0x0d
62 #define ATLAS_REG_ORP_DATA 0x0e
64 #define ATLAS_PH_INT_TIME_IN_MS 450
65 #define ATLAS_EC_INT_TIME_IN_MS 650
66 #define ATLAS_ORP_INT_TIME_IN_MS 450
75 struct i2c_client
*client
;
76 struct iio_trigger
*trig
;
77 struct atlas_device
*chip
;
78 struct regmap
*regmap
;
81 __be32 buffer
[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
84 static const struct regmap_config atlas_regmap_config
= {
85 .name
= ATLAS_REGMAP_NAME
,
90 static const struct iio_chan_spec atlas_ph_channels
[] = {
93 .address
= ATLAS_REG_PH_DATA
,
95 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
101 .endianness
= IIO_BE
,
104 IIO_CHAN_SOFT_TIMESTAMP(1),
107 .address
= ATLAS_REG_PH_TEMP_DATA
,
108 .info_mask_separate
=
109 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
115 #define ATLAS_EC_CHANNEL(_idx, _addr) \
117 .type = IIO_CONCENTRATION, \
121 .info_mask_separate = \
122 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
123 .scan_index = _idx + 1, \
128 .endianness = IIO_BE, \
132 static const struct iio_chan_spec atlas_ec_channels
[] = {
134 .type
= IIO_ELECTRICALCONDUCTIVITY
,
135 .address
= ATLAS_REG_EC_DATA
,
136 .info_mask_separate
=
137 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
143 .endianness
= IIO_BE
,
146 ATLAS_EC_CHANNEL(0, ATLAS_REG_TDS_DATA
),
147 ATLAS_EC_CHANNEL(1, ATLAS_REG_PSS_DATA
),
148 IIO_CHAN_SOFT_TIMESTAMP(3),
151 .address
= ATLAS_REG_EC_TEMP_DATA
,
152 .info_mask_separate
=
153 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
159 static const struct iio_chan_spec atlas_orp_channels
[] = {
162 .address
= ATLAS_REG_ORP_DATA
,
163 .info_mask_separate
=
164 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
170 .endianness
= IIO_BE
,
173 IIO_CHAN_SOFT_TIMESTAMP(1),
176 static int atlas_check_ph_calibration(struct atlas_data
*data
)
178 struct device
*dev
= &data
->client
->dev
;
182 ret
= regmap_read(data
->regmap
, ATLAS_REG_PH_CALIB_STATUS
, &val
);
186 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_MASK
)) {
187 dev_warn(dev
, "device has not been calibrated\n");
191 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_LOW
))
192 dev_warn(dev
, "device missing low point calibration\n");
194 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_MID
))
195 dev_warn(dev
, "device missing mid point calibration\n");
197 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_HIGH
))
198 dev_warn(dev
, "device missing high point calibration\n");
203 static int atlas_check_ec_calibration(struct atlas_data
*data
)
205 struct device
*dev
= &data
->client
->dev
;
210 ret
= regmap_bulk_read(data
->regmap
, ATLAS_REG_EC_PROBE
, &rval
, 2);
214 val
= be16_to_cpu(rval
);
215 dev_info(dev
, "probe set to K = %d.%.2d", val
/ 100, val
% 100);
217 ret
= regmap_read(data
->regmap
, ATLAS_REG_EC_CALIB_STATUS
, &val
);
221 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_MASK
)) {
222 dev_warn(dev
, "device has not been calibrated\n");
226 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_DRY
))
227 dev_warn(dev
, "device missing dry point calibration\n");
229 if (val
& ATLAS_REG_EC_CALIB_STATUS_SINGLE
) {
230 dev_warn(dev
, "device using single point calibration\n");
232 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_LOW
))
233 dev_warn(dev
, "device missing low point calibration\n");
235 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_HIGH
))
236 dev_warn(dev
, "device missing high point calibration\n");
242 static int atlas_check_orp_calibration(struct atlas_data
*data
)
244 struct device
*dev
= &data
->client
->dev
;
248 ret
= regmap_read(data
->regmap
, ATLAS_REG_ORP_CALIB_STATUS
, &val
);
253 dev_warn(dev
, "device has not been calibrated\n");
258 struct atlas_device
{
259 const struct iio_chan_spec
*channels
;
263 int (*calibration
)(struct atlas_data
*data
);
267 static struct atlas_device atlas_devices
[] = {
269 .channels
= atlas_ph_channels
,
271 .data_reg
= ATLAS_REG_PH_DATA
,
272 .calibration
= &atlas_check_ph_calibration
,
273 .delay
= ATLAS_PH_INT_TIME_IN_MS
,
276 .channels
= atlas_ec_channels
,
278 .data_reg
= ATLAS_REG_EC_DATA
,
279 .calibration
= &atlas_check_ec_calibration
,
280 .delay
= ATLAS_EC_INT_TIME_IN_MS
,
283 .channels
= atlas_orp_channels
,
285 .data_reg
= ATLAS_REG_ORP_DATA
,
286 .calibration
= &atlas_check_orp_calibration
,
287 .delay
= ATLAS_ORP_INT_TIME_IN_MS
,
291 static int atlas_set_powermode(struct atlas_data
*data
, int on
)
293 return regmap_write(data
->regmap
, ATLAS_REG_PWR_CONTROL
, on
);
296 static int atlas_set_interrupt(struct atlas_data
*data
, bool state
)
298 return regmap_update_bits(data
->regmap
, ATLAS_REG_INT_CONTROL
,
299 ATLAS_REG_INT_CONTROL_EN
,
300 state
? ATLAS_REG_INT_CONTROL_EN
: 0);
303 static int atlas_buffer_postenable(struct iio_dev
*indio_dev
)
305 struct atlas_data
*data
= iio_priv(indio_dev
);
308 ret
= iio_triggered_buffer_postenable(indio_dev
);
312 ret
= pm_runtime_get_sync(&data
->client
->dev
);
314 pm_runtime_put_noidle(&data
->client
->dev
);
318 return atlas_set_interrupt(data
, true);
321 static int atlas_buffer_predisable(struct iio_dev
*indio_dev
)
323 struct atlas_data
*data
= iio_priv(indio_dev
);
326 ret
= iio_triggered_buffer_predisable(indio_dev
);
330 ret
= atlas_set_interrupt(data
, false);
334 pm_runtime_mark_last_busy(&data
->client
->dev
);
335 return pm_runtime_put_autosuspend(&data
->client
->dev
);
338 static const struct iio_trigger_ops atlas_interrupt_trigger_ops
= {
341 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops
= {
342 .postenable
= atlas_buffer_postenable
,
343 .predisable
= atlas_buffer_predisable
,
346 static void atlas_work_handler(struct irq_work
*work
)
348 struct atlas_data
*data
= container_of(work
, struct atlas_data
, work
);
350 iio_trigger_poll(data
->trig
);
353 static irqreturn_t
atlas_trigger_handler(int irq
, void *private)
355 struct iio_poll_func
*pf
= private;
356 struct iio_dev
*indio_dev
= pf
->indio_dev
;
357 struct atlas_data
*data
= iio_priv(indio_dev
);
360 ret
= regmap_bulk_read(data
->regmap
, data
->chip
->data_reg
,
361 (u8
*) &data
->buffer
,
362 sizeof(__be32
) * (data
->chip
->num_channels
- 2));
365 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
366 iio_get_time_ns(indio_dev
));
368 iio_trigger_notify_done(indio_dev
->trig
);
373 static irqreturn_t
atlas_interrupt_handler(int irq
, void *private)
375 struct iio_dev
*indio_dev
= private;
376 struct atlas_data
*data
= iio_priv(indio_dev
);
378 irq_work_queue(&data
->work
);
383 static int atlas_read_measurement(struct atlas_data
*data
, int reg
, __be32
*val
)
385 struct device
*dev
= &data
->client
->dev
;
386 int suspended
= pm_runtime_suspended(dev
);
389 ret
= pm_runtime_get_sync(dev
);
391 pm_runtime_put_noidle(dev
);
396 msleep(data
->chip
->delay
);
398 ret
= regmap_bulk_read(data
->regmap
, reg
, (u8
*) val
, sizeof(*val
));
400 pm_runtime_mark_last_busy(dev
);
401 pm_runtime_put_autosuspend(dev
);
406 static int atlas_read_raw(struct iio_dev
*indio_dev
,
407 struct iio_chan_spec
const *chan
,
408 int *val
, int *val2
, long mask
)
410 struct atlas_data
*data
= iio_priv(indio_dev
);
413 case IIO_CHAN_INFO_RAW
: {
417 switch (chan
->type
) {
419 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
420 (u8
*) ®
, sizeof(reg
));
423 case IIO_CONCENTRATION
:
424 case IIO_ELECTRICALCONDUCTIVITY
:
426 ret
= iio_device_claim_direct_mode(indio_dev
);
430 ret
= atlas_read_measurement(data
, chan
->address
, ®
);
432 iio_device_release_direct_mode(indio_dev
);
439 *val
= be32_to_cpu(reg
);
444 case IIO_CHAN_INFO_SCALE
:
445 switch (chan
->type
) {
451 *val
= 1; /* 0.001 */
454 case IIO_ELECTRICALCONDUCTIVITY
:
455 *val
= 1; /* 0.00001 */
458 case IIO_CONCENTRATION
:
459 *val
= 0; /* 0.000000001 */
461 return IIO_VAL_INT_PLUS_NANO
;
469 return IIO_VAL_FRACTIONAL
;
475 static int atlas_write_raw(struct iio_dev
*indio_dev
,
476 struct iio_chan_spec
const *chan
,
477 int val
, int val2
, long mask
)
479 struct atlas_data
*data
= iio_priv(indio_dev
);
480 __be32 reg
= cpu_to_be32(val
);
482 if (val2
!= 0 || val
< 0 || val
> 20000)
485 if (mask
!= IIO_CHAN_INFO_RAW
|| chan
->type
!= IIO_TEMP
)
488 return regmap_bulk_write(data
->regmap
, chan
->address
,
492 static const struct iio_info atlas_info
= {
493 .read_raw
= atlas_read_raw
,
494 .write_raw
= atlas_write_raw
,
497 static const struct i2c_device_id atlas_id
[] = {
498 { "atlas-ph-sm", ATLAS_PH_SM
},
499 { "atlas-ec-sm", ATLAS_EC_SM
},
500 { "atlas-orp-sm", ATLAS_ORP_SM
},
503 MODULE_DEVICE_TABLE(i2c
, atlas_id
);
505 static const struct of_device_id atlas_dt_ids
[] = {
506 { .compatible
= "atlas,ph-sm", .data
= (void *)ATLAS_PH_SM
, },
507 { .compatible
= "atlas,ec-sm", .data
= (void *)ATLAS_EC_SM
, },
508 { .compatible
= "atlas,orp-sm", .data
= (void *)ATLAS_ORP_SM
, },
511 MODULE_DEVICE_TABLE(of
, atlas_dt_ids
);
513 static int atlas_probe(struct i2c_client
*client
,
514 const struct i2c_device_id
*id
)
516 struct atlas_data
*data
;
517 struct atlas_device
*chip
;
518 const struct of_device_id
*of_id
;
519 struct iio_trigger
*trig
;
520 struct iio_dev
*indio_dev
;
523 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
527 of_id
= of_match_device(atlas_dt_ids
, &client
->dev
);
529 chip
= &atlas_devices
[id
->driver_data
];
531 chip
= &atlas_devices
[(unsigned long)of_id
->data
];
533 indio_dev
->info
= &atlas_info
;
534 indio_dev
->name
= ATLAS_DRV_NAME
;
535 indio_dev
->channels
= chip
->channels
;
536 indio_dev
->num_channels
= chip
->num_channels
;
537 indio_dev
->modes
= INDIO_BUFFER_SOFTWARE
| INDIO_DIRECT_MODE
;
538 indio_dev
->dev
.parent
= &client
->dev
;
540 trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-dev%d",
541 indio_dev
->name
, indio_dev
->id
);
546 data
= iio_priv(indio_dev
);
547 data
->client
= client
;
550 trig
->dev
.parent
= indio_dev
->dev
.parent
;
551 trig
->ops
= &atlas_interrupt_trigger_ops
;
552 iio_trigger_set_drvdata(trig
, indio_dev
);
554 i2c_set_clientdata(client
, indio_dev
);
556 data
->regmap
= devm_regmap_init_i2c(client
, &atlas_regmap_config
);
557 if (IS_ERR(data
->regmap
)) {
558 dev_err(&client
->dev
, "regmap initialization failed\n");
559 return PTR_ERR(data
->regmap
);
562 ret
= pm_runtime_set_active(&client
->dev
);
566 if (client
->irq
<= 0) {
567 dev_err(&client
->dev
, "no valid irq defined\n");
571 ret
= chip
->calibration(data
);
575 ret
= iio_trigger_register(trig
);
577 dev_err(&client
->dev
, "failed to register trigger\n");
581 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
582 &atlas_trigger_handler
, &atlas_buffer_setup_ops
);
584 dev_err(&client
->dev
, "cannot setup iio trigger\n");
585 goto unregister_trigger
;
588 init_irq_work(&data
->work
, atlas_work_handler
);
590 /* interrupt pin toggles on new conversion */
591 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
592 NULL
, atlas_interrupt_handler
,
593 IRQF_TRIGGER_RISING
|
594 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
598 dev_err(&client
->dev
, "request irq (%d) failed\n", client
->irq
);
599 goto unregister_buffer
;
602 ret
= atlas_set_powermode(data
, 1);
604 dev_err(&client
->dev
, "cannot power device on");
605 goto unregister_buffer
;
608 pm_runtime_enable(&client
->dev
);
609 pm_runtime_set_autosuspend_delay(&client
->dev
, 2500);
610 pm_runtime_use_autosuspend(&client
->dev
);
612 ret
= iio_device_register(indio_dev
);
614 dev_err(&client
->dev
, "unable to register device\n");
621 pm_runtime_disable(&client
->dev
);
622 atlas_set_powermode(data
, 0);
625 iio_triggered_buffer_cleanup(indio_dev
);
628 iio_trigger_unregister(data
->trig
);
633 static int atlas_remove(struct i2c_client
*client
)
635 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
636 struct atlas_data
*data
= iio_priv(indio_dev
);
638 iio_device_unregister(indio_dev
);
639 iio_triggered_buffer_cleanup(indio_dev
);
640 iio_trigger_unregister(data
->trig
);
642 pm_runtime_disable(&client
->dev
);
643 pm_runtime_set_suspended(&client
->dev
);
644 pm_runtime_put_noidle(&client
->dev
);
646 return atlas_set_powermode(data
, 0);
650 static int atlas_runtime_suspend(struct device
*dev
)
652 struct atlas_data
*data
=
653 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
655 return atlas_set_powermode(data
, 0);
658 static int atlas_runtime_resume(struct device
*dev
)
660 struct atlas_data
*data
=
661 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
663 return atlas_set_powermode(data
, 1);
667 static const struct dev_pm_ops atlas_pm_ops
= {
668 SET_RUNTIME_PM_OPS(atlas_runtime_suspend
,
669 atlas_runtime_resume
, NULL
)
672 static struct i2c_driver atlas_driver
= {
674 .name
= ATLAS_DRV_NAME
,
675 .of_match_table
= of_match_ptr(atlas_dt_ids
),
678 .probe
= atlas_probe
,
679 .remove
= atlas_remove
,
680 .id_table
= atlas_id
,
682 module_i2c_driver(atlas_driver
);
684 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
685 MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
686 MODULE_LICENSE("GPL");