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_REG_ORP_CALIB_STATUS 0x0d
70 #define ATLAS_REG_ORP_DATA 0x0e
72 #define ATLAS_PH_INT_TIME_IN_US 450000
73 #define ATLAS_EC_INT_TIME_IN_US 650000
74 #define ATLAS_ORP_INT_TIME_IN_US 450000
83 struct i2c_client
*client
;
84 struct iio_trigger
*trig
;
85 struct atlas_device
*chip
;
86 struct regmap
*regmap
;
89 __be32 buffer
[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
92 static const struct regmap_config atlas_regmap_config
= {
93 .name
= ATLAS_REGMAP_NAME
,
98 static const struct iio_chan_spec atlas_ph_channels
[] = {
101 .address
= ATLAS_REG_PH_DATA
,
102 .info_mask_separate
=
103 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
109 .endianness
= IIO_BE
,
112 IIO_CHAN_SOFT_TIMESTAMP(1),
115 .address
= ATLAS_REG_PH_TEMP_DATA
,
116 .info_mask_separate
=
117 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
123 #define ATLAS_EC_CHANNEL(_idx, _addr) \
125 .type = IIO_CONCENTRATION, \
129 .info_mask_separate = \
130 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
131 .scan_index = _idx + 1, \
136 .endianness = IIO_BE, \
140 static const struct iio_chan_spec atlas_ec_channels
[] = {
142 .type
= IIO_ELECTRICALCONDUCTIVITY
,
143 .address
= ATLAS_REG_EC_DATA
,
144 .info_mask_separate
=
145 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
151 .endianness
= IIO_BE
,
154 ATLAS_EC_CHANNEL(0, ATLAS_REG_TDS_DATA
),
155 ATLAS_EC_CHANNEL(1, ATLAS_REG_PSS_DATA
),
156 IIO_CHAN_SOFT_TIMESTAMP(3),
159 .address
= ATLAS_REG_EC_TEMP_DATA
,
160 .info_mask_separate
=
161 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
167 static const struct iio_chan_spec atlas_orp_channels
[] = {
170 .address
= ATLAS_REG_ORP_DATA
,
171 .info_mask_separate
=
172 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
178 .endianness
= IIO_BE
,
181 IIO_CHAN_SOFT_TIMESTAMP(1),
184 static int atlas_check_ph_calibration(struct atlas_data
*data
)
186 struct device
*dev
= &data
->client
->dev
;
190 ret
= regmap_read(data
->regmap
, ATLAS_REG_PH_CALIB_STATUS
, &val
);
194 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_MASK
)) {
195 dev_warn(dev
, "device has not been calibrated\n");
199 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_LOW
))
200 dev_warn(dev
, "device missing low point calibration\n");
202 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_MID
))
203 dev_warn(dev
, "device missing mid point calibration\n");
205 if (!(val
& ATLAS_REG_PH_CALIB_STATUS_HIGH
))
206 dev_warn(dev
, "device missing high point calibration\n");
211 static int atlas_check_ec_calibration(struct atlas_data
*data
)
213 struct device
*dev
= &data
->client
->dev
;
217 ret
= regmap_bulk_read(data
->regmap
, ATLAS_REG_EC_PROBE
, &val
, 2);
221 dev_info(dev
, "probe set to K = %d.%.2d", be16_to_cpu(val
) / 100,
222 be16_to_cpu(val
) % 100);
224 ret
= regmap_read(data
->regmap
, ATLAS_REG_EC_CALIB_STATUS
, &val
);
228 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_MASK
)) {
229 dev_warn(dev
, "device has not been calibrated\n");
233 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_DRY
))
234 dev_warn(dev
, "device missing dry point calibration\n");
236 if (val
& ATLAS_REG_EC_CALIB_STATUS_SINGLE
) {
237 dev_warn(dev
, "device using single point calibration\n");
239 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_LOW
))
240 dev_warn(dev
, "device missing low point calibration\n");
242 if (!(val
& ATLAS_REG_EC_CALIB_STATUS_HIGH
))
243 dev_warn(dev
, "device missing high point calibration\n");
249 static int atlas_check_orp_calibration(struct atlas_data
*data
)
251 struct device
*dev
= &data
->client
->dev
;
255 ret
= regmap_read(data
->regmap
, ATLAS_REG_ORP_CALIB_STATUS
, &val
);
260 dev_warn(dev
, "device has not been calibrated\n");
265 struct atlas_device
{
266 const struct iio_chan_spec
*channels
;
270 int (*calibration
)(struct atlas_data
*data
);
274 static struct atlas_device atlas_devices
[] = {
276 .channels
= atlas_ph_channels
,
278 .data_reg
= ATLAS_REG_PH_DATA
,
279 .calibration
= &atlas_check_ph_calibration
,
280 .delay
= ATLAS_PH_INT_TIME_IN_US
,
283 .channels
= atlas_ec_channels
,
285 .data_reg
= ATLAS_REG_EC_DATA
,
286 .calibration
= &atlas_check_ec_calibration
,
287 .delay
= ATLAS_EC_INT_TIME_IN_US
,
290 .channels
= atlas_orp_channels
,
292 .data_reg
= ATLAS_REG_ORP_DATA
,
293 .calibration
= &atlas_check_orp_calibration
,
294 .delay
= ATLAS_ORP_INT_TIME_IN_US
,
298 static int atlas_set_powermode(struct atlas_data
*data
, int on
)
300 return regmap_write(data
->regmap
, ATLAS_REG_PWR_CONTROL
, on
);
303 static int atlas_set_interrupt(struct atlas_data
*data
, bool state
)
305 return regmap_update_bits(data
->regmap
, ATLAS_REG_INT_CONTROL
,
306 ATLAS_REG_INT_CONTROL_EN
,
307 state
? ATLAS_REG_INT_CONTROL_EN
: 0);
310 static int atlas_buffer_postenable(struct iio_dev
*indio_dev
)
312 struct atlas_data
*data
= iio_priv(indio_dev
);
315 ret
= iio_triggered_buffer_postenable(indio_dev
);
319 ret
= pm_runtime_get_sync(&data
->client
->dev
);
321 pm_runtime_put_noidle(&data
->client
->dev
);
325 return atlas_set_interrupt(data
, true);
328 static int atlas_buffer_predisable(struct iio_dev
*indio_dev
)
330 struct atlas_data
*data
= iio_priv(indio_dev
);
333 ret
= iio_triggered_buffer_predisable(indio_dev
);
337 ret
= atlas_set_interrupt(data
, false);
341 pm_runtime_mark_last_busy(&data
->client
->dev
);
342 return pm_runtime_put_autosuspend(&data
->client
->dev
);
345 static const struct iio_trigger_ops atlas_interrupt_trigger_ops
= {
346 .owner
= THIS_MODULE
,
349 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops
= {
350 .postenable
= atlas_buffer_postenable
,
351 .predisable
= atlas_buffer_predisable
,
354 static void atlas_work_handler(struct irq_work
*work
)
356 struct atlas_data
*data
= container_of(work
, struct atlas_data
, work
);
358 iio_trigger_poll(data
->trig
);
361 static irqreturn_t
atlas_trigger_handler(int irq
, void *private)
363 struct iio_poll_func
*pf
= private;
364 struct iio_dev
*indio_dev
= pf
->indio_dev
;
365 struct atlas_data
*data
= iio_priv(indio_dev
);
368 ret
= regmap_bulk_read(data
->regmap
, data
->chip
->data_reg
,
369 (u8
*) &data
->buffer
,
370 sizeof(__be32
) * (data
->chip
->num_channels
- 2));
373 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
374 iio_get_time_ns(indio_dev
));
376 iio_trigger_notify_done(indio_dev
->trig
);
381 static irqreturn_t
atlas_interrupt_handler(int irq
, void *private)
383 struct iio_dev
*indio_dev
= private;
384 struct atlas_data
*data
= iio_priv(indio_dev
);
386 irq_work_queue(&data
->work
);
391 static int atlas_read_measurement(struct atlas_data
*data
, int reg
, __be32
*val
)
393 struct device
*dev
= &data
->client
->dev
;
394 int suspended
= pm_runtime_suspended(dev
);
397 ret
= pm_runtime_get_sync(dev
);
399 pm_runtime_put_noidle(dev
);
404 usleep_range(data
->chip
->delay
, data
->chip
->delay
+ 100000);
406 ret
= regmap_bulk_read(data
->regmap
, reg
, (u8
*) val
, sizeof(*val
));
408 pm_runtime_mark_last_busy(dev
);
409 pm_runtime_put_autosuspend(dev
);
414 static int atlas_read_raw(struct iio_dev
*indio_dev
,
415 struct iio_chan_spec
const *chan
,
416 int *val
, int *val2
, long mask
)
418 struct atlas_data
*data
= iio_priv(indio_dev
);
421 case IIO_CHAN_INFO_RAW
: {
425 switch (chan
->type
) {
427 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
428 (u8
*) ®
, sizeof(reg
));
431 case IIO_CONCENTRATION
:
432 case IIO_ELECTRICALCONDUCTIVITY
:
434 ret
= iio_device_claim_direct_mode(indio_dev
);
438 ret
= atlas_read_measurement(data
, chan
->address
, ®
);
440 iio_device_release_direct_mode(indio_dev
);
447 *val
= be32_to_cpu(reg
);
452 case IIO_CHAN_INFO_SCALE
:
453 switch (chan
->type
) {
459 *val
= 1; /* 0.001 */
462 case IIO_ELECTRICALCONDUCTIVITY
:
463 *val
= 1; /* 0.00001 */
466 case IIO_CONCENTRATION
:
467 *val
= 0; /* 0.000000001 */
469 return IIO_VAL_INT_PLUS_NANO
;
477 return IIO_VAL_FRACTIONAL
;
483 static int atlas_write_raw(struct iio_dev
*indio_dev
,
484 struct iio_chan_spec
const *chan
,
485 int val
, int val2
, long mask
)
487 struct atlas_data
*data
= iio_priv(indio_dev
);
488 __be32 reg
= cpu_to_be32(val
);
490 if (val2
!= 0 || val
< 0 || val
> 20000)
493 if (mask
!= IIO_CHAN_INFO_RAW
|| chan
->type
!= IIO_TEMP
)
496 return regmap_bulk_write(data
->regmap
, chan
->address
,
500 static const struct iio_info atlas_info
= {
501 .driver_module
= THIS_MODULE
,
502 .read_raw
= atlas_read_raw
,
503 .write_raw
= atlas_write_raw
,
506 static const struct i2c_device_id atlas_id
[] = {
507 { "atlas-ph-sm", ATLAS_PH_SM
},
508 { "atlas-ec-sm", ATLAS_EC_SM
},
509 { "atlas-orp-sm", ATLAS_ORP_SM
},
512 MODULE_DEVICE_TABLE(i2c
, atlas_id
);
514 static const struct of_device_id atlas_dt_ids
[] = {
515 { .compatible
= "atlas,ph-sm", .data
= (void *)ATLAS_PH_SM
, },
516 { .compatible
= "atlas,ec-sm", .data
= (void *)ATLAS_EC_SM
, },
517 { .compatible
= "atlas,orp-sm", .data
= (void *)ATLAS_ORP_SM
, },
520 MODULE_DEVICE_TABLE(of
, atlas_dt_ids
);
522 static int atlas_probe(struct i2c_client
*client
,
523 const struct i2c_device_id
*id
)
525 struct atlas_data
*data
;
526 struct atlas_device
*chip
;
527 const struct of_device_id
*of_id
;
528 struct iio_trigger
*trig
;
529 struct iio_dev
*indio_dev
;
532 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
536 of_id
= of_match_device(atlas_dt_ids
, &client
->dev
);
538 chip
= &atlas_devices
[id
->driver_data
];
540 chip
= &atlas_devices
[(unsigned long)of_id
->data
];
542 indio_dev
->info
= &atlas_info
;
543 indio_dev
->name
= ATLAS_DRV_NAME
;
544 indio_dev
->channels
= chip
->channels
;
545 indio_dev
->num_channels
= chip
->num_channels
;
546 indio_dev
->modes
= INDIO_BUFFER_SOFTWARE
| INDIO_DIRECT_MODE
;
547 indio_dev
->dev
.parent
= &client
->dev
;
549 trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-dev%d",
550 indio_dev
->name
, indio_dev
->id
);
555 data
= iio_priv(indio_dev
);
556 data
->client
= client
;
559 trig
->dev
.parent
= indio_dev
->dev
.parent
;
560 trig
->ops
= &atlas_interrupt_trigger_ops
;
561 iio_trigger_set_drvdata(trig
, indio_dev
);
563 i2c_set_clientdata(client
, indio_dev
);
565 data
->regmap
= devm_regmap_init_i2c(client
, &atlas_regmap_config
);
566 if (IS_ERR(data
->regmap
)) {
567 dev_err(&client
->dev
, "regmap initialization failed\n");
568 return PTR_ERR(data
->regmap
);
571 ret
= pm_runtime_set_active(&client
->dev
);
575 if (client
->irq
<= 0) {
576 dev_err(&client
->dev
, "no valid irq defined\n");
580 ret
= chip
->calibration(data
);
584 ret
= iio_trigger_register(trig
);
586 dev_err(&client
->dev
, "failed to register trigger\n");
590 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
591 &atlas_trigger_handler
, &atlas_buffer_setup_ops
);
593 dev_err(&client
->dev
, "cannot setup iio trigger\n");
594 goto unregister_trigger
;
597 init_irq_work(&data
->work
, atlas_work_handler
);
599 /* interrupt pin toggles on new conversion */
600 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
601 NULL
, atlas_interrupt_handler
,
602 IRQF_TRIGGER_RISING
|
603 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
607 dev_err(&client
->dev
, "request irq (%d) failed\n", client
->irq
);
608 goto unregister_buffer
;
611 ret
= atlas_set_powermode(data
, 1);
613 dev_err(&client
->dev
, "cannot power device on");
614 goto unregister_buffer
;
617 pm_runtime_enable(&client
->dev
);
618 pm_runtime_set_autosuspend_delay(&client
->dev
, 2500);
619 pm_runtime_use_autosuspend(&client
->dev
);
621 ret
= iio_device_register(indio_dev
);
623 dev_err(&client
->dev
, "unable to register device\n");
630 pm_runtime_disable(&client
->dev
);
631 atlas_set_powermode(data
, 0);
634 iio_triggered_buffer_cleanup(indio_dev
);
637 iio_trigger_unregister(data
->trig
);
642 static int atlas_remove(struct i2c_client
*client
)
644 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
645 struct atlas_data
*data
= iio_priv(indio_dev
);
647 iio_device_unregister(indio_dev
);
648 iio_triggered_buffer_cleanup(indio_dev
);
649 iio_trigger_unregister(data
->trig
);
651 pm_runtime_disable(&client
->dev
);
652 pm_runtime_set_suspended(&client
->dev
);
653 pm_runtime_put_noidle(&client
->dev
);
655 return atlas_set_powermode(data
, 0);
659 static int atlas_runtime_suspend(struct device
*dev
)
661 struct atlas_data
*data
=
662 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
664 return atlas_set_powermode(data
, 0);
667 static int atlas_runtime_resume(struct device
*dev
)
669 struct atlas_data
*data
=
670 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
672 return atlas_set_powermode(data
, 1);
676 static const struct dev_pm_ops atlas_pm_ops
= {
677 SET_RUNTIME_PM_OPS(atlas_runtime_suspend
,
678 atlas_runtime_resume
, NULL
)
681 static struct i2c_driver atlas_driver
= {
683 .name
= ATLAS_DRV_NAME
,
684 .of_match_table
= of_match_ptr(atlas_dt_ids
),
687 .probe
= atlas_probe
,
688 .remove
= atlas_remove
,
689 .id_table
= atlas_id
,
691 module_i2c_driver(atlas_driver
);
693 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
694 MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
695 MODULE_LICENSE("GPL");