1
2
3
4
5
6
7
8
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>
27
28#define ATLAS_REGMAP_NAME "atlas_ph_regmap"
29#define ATLAS_DRV_NAME "atlas_ph"
30
31#define ATLAS_REG_DEV_TYPE 0x00
32#define ATLAS_REG_DEV_VERSION 0x01
33
34#define ATLAS_REG_INT_CONTROL 0x04
35#define ATLAS_REG_INT_CONTROL_EN BIT(3)
36
37#define ATLAS_REG_PWR_CONTROL 0x06
38
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)
44
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)
51
52#define ATLAS_REG_PH_TEMP_DATA 0x0e
53#define ATLAS_REG_PH_DATA 0x16
54
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
60
61#define ATLAS_REG_ORP_CALIB_STATUS 0x0d
62#define ATLAS_REG_ORP_DATA 0x0e
63
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
67
68enum {
69 ATLAS_PH_SM,
70 ATLAS_EC_SM,
71 ATLAS_ORP_SM,
72};
73
74struct atlas_data {
75 struct i2c_client *client;
76 struct iio_trigger *trig;
77 struct atlas_device *chip;
78 struct regmap *regmap;
79 struct irq_work work;
80
81 __be32 buffer[6];
82};
83
84static const struct regmap_config atlas_regmap_config = {
85 .name = ATLAS_REGMAP_NAME,
86 .reg_bits = 8,
87 .val_bits = 8,
88};
89
90static const struct iio_chan_spec atlas_ph_channels[] = {
91 {
92 .type = IIO_PH,
93 .address = ATLAS_REG_PH_DATA,
94 .info_mask_separate =
95 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
96 .scan_index = 0,
97 .scan_type = {
98 .sign = 'u',
99 .realbits = 32,
100 .storagebits = 32,
101 .endianness = IIO_BE,
102 },
103 },
104 IIO_CHAN_SOFT_TIMESTAMP(1),
105 {
106 .type = IIO_TEMP,
107 .address = ATLAS_REG_PH_TEMP_DATA,
108 .info_mask_separate =
109 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
110 .output = 1,
111 .scan_index = -1
112 },
113};
114
115#define ATLAS_EC_CHANNEL(_idx, _addr) \
116 {\
117 .type = IIO_CONCENTRATION, \
118 .indexed = 1, \
119 .channel = _idx, \
120 .address = _addr, \
121 .info_mask_separate = \
122 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
123 .scan_index = _idx + 1, \
124 .scan_type = { \
125 .sign = 'u', \
126 .realbits = 32, \
127 .storagebits = 32, \
128 .endianness = IIO_BE, \
129 }, \
130 }
131
132static const struct iio_chan_spec atlas_ec_channels[] = {
133 {
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),
138 .scan_index = 0,
139 .scan_type = {
140 .sign = 'u',
141 .realbits = 32,
142 .storagebits = 32,
143 .endianness = IIO_BE,
144 },
145 },
146 ATLAS_EC_CHANNEL(0, ATLAS_REG_TDS_DATA),
147 ATLAS_EC_CHANNEL(1, ATLAS_REG_PSS_DATA),
148 IIO_CHAN_SOFT_TIMESTAMP(3),
149 {
150 .type = IIO_TEMP,
151 .address = ATLAS_REG_EC_TEMP_DATA,
152 .info_mask_separate =
153 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
154 .output = 1,
155 .scan_index = -1
156 },
157};
158
159static const struct iio_chan_spec atlas_orp_channels[] = {
160 {
161 .type = IIO_VOLTAGE,
162 .address = ATLAS_REG_ORP_DATA,
163 .info_mask_separate =
164 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
165 .scan_index = 0,
166 .scan_type = {
167 .sign = 's',
168 .realbits = 32,
169 .storagebits = 32,
170 .endianness = IIO_BE,
171 },
172 },
173 IIO_CHAN_SOFT_TIMESTAMP(1),
174};
175
176static int atlas_check_ph_calibration(struct atlas_data *data)
177{
178 struct device *dev = &data->client->dev;
179 int ret;
180 unsigned int val;
181
182 ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val);
183 if (ret)
184 return ret;
185
186 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) {
187 dev_warn(dev, "device has not been calibrated\n");
188 return 0;
189 }
190
191 if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW))
192 dev_warn(dev, "device missing low point calibration\n");
193
194 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID))
195 dev_warn(dev, "device missing mid point calibration\n");
196
197 if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH))
198 dev_warn(dev, "device missing high point calibration\n");
199
200 return 0;
201}
202
203static int atlas_check_ec_calibration(struct atlas_data *data)
204{
205 struct device *dev = &data->client->dev;
206 int ret;
207 unsigned int val;
208 __be16 rval;
209
210 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2);
211 if (ret)
212 return ret;
213
214 val = be16_to_cpu(rval);
215 dev_info(dev, "probe set to K = %d.%.2d", val / 100, val % 100);
216
217 ret = regmap_read(data->regmap, ATLAS_REG_EC_CALIB_STATUS, &val);
218 if (ret)
219 return ret;
220
221 if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) {
222 dev_warn(dev, "device has not been calibrated\n");
223 return 0;
224 }
225
226 if (!(val & ATLAS_REG_EC_CALIB_STATUS_DRY))
227 dev_warn(dev, "device missing dry point calibration\n");
228
229 if (val & ATLAS_REG_EC_CALIB_STATUS_SINGLE) {
230 dev_warn(dev, "device using single point calibration\n");
231 } else {
232 if (!(val & ATLAS_REG_EC_CALIB_STATUS_LOW))
233 dev_warn(dev, "device missing low point calibration\n");
234
235 if (!(val & ATLAS_REG_EC_CALIB_STATUS_HIGH))
236 dev_warn(dev, "device missing high point calibration\n");
237 }
238
239 return 0;
240}
241
242static int atlas_check_orp_calibration(struct atlas_data *data)
243{
244 struct device *dev = &data->client->dev;
245 int ret;
246 unsigned int val;
247
248 ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val);
249 if (ret)
250 return ret;
251
252 if (!val)
253 dev_warn(dev, "device has not been calibrated\n");
254
255 return 0;
256};
257
258struct atlas_device {
259 const struct iio_chan_spec *channels;
260 int num_channels;
261 int data_reg;
262
263 int (*calibration)(struct atlas_data *data);
264 int delay;
265};
266
267static struct atlas_device atlas_devices[] = {
268 [ATLAS_PH_SM] = {
269 .channels = atlas_ph_channels,
270 .num_channels = 3,
271 .data_reg = ATLAS_REG_PH_DATA,
272 .calibration = &atlas_check_ph_calibration,
273 .delay = ATLAS_PH_INT_TIME_IN_MS,
274 },
275 [ATLAS_EC_SM] = {
276 .channels = atlas_ec_channels,
277 .num_channels = 5,
278 .data_reg = ATLAS_REG_EC_DATA,
279 .calibration = &atlas_check_ec_calibration,
280 .delay = ATLAS_EC_INT_TIME_IN_MS,
281 },
282 [ATLAS_ORP_SM] = {
283 .channels = atlas_orp_channels,
284 .num_channels = 2,
285 .data_reg = ATLAS_REG_ORP_DATA,
286 .calibration = &atlas_check_orp_calibration,
287 .delay = ATLAS_ORP_INT_TIME_IN_MS,
288 },
289};
290
291static int atlas_set_powermode(struct atlas_data *data, int on)
292{
293 return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
294}
295
296static int atlas_set_interrupt(struct atlas_data *data, bool state)
297{
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);
301}
302
303static int atlas_buffer_postenable(struct iio_dev *indio_dev)
304{
305 struct atlas_data *data = iio_priv(indio_dev);
306 int ret;
307
308 ret = iio_triggered_buffer_postenable(indio_dev);
309 if (ret)
310 return ret;
311
312 ret = pm_runtime_get_sync(&data->client->dev);
313 if (ret < 0) {
314 pm_runtime_put_noidle(&data->client->dev);
315 return ret;
316 }
317
318 return atlas_set_interrupt(data, true);
319}
320
321static int atlas_buffer_predisable(struct iio_dev *indio_dev)
322{
323 struct atlas_data *data = iio_priv(indio_dev);
324 int ret;
325
326 ret = iio_triggered_buffer_predisable(indio_dev);
327 if (ret)
328 return ret;
329
330 ret = atlas_set_interrupt(data, false);
331 if (ret)
332 return ret;
333
334 pm_runtime_mark_last_busy(&data->client->dev);
335 return pm_runtime_put_autosuspend(&data->client->dev);
336}
337
338static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
339};
340
341static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
342 .postenable = atlas_buffer_postenable,
343 .predisable = atlas_buffer_predisable,
344};
345
346static void atlas_work_handler(struct irq_work *work)
347{
348 struct atlas_data *data = container_of(work, struct atlas_data, work);
349
350 iio_trigger_poll(data->trig);
351}
352
353static irqreturn_t atlas_trigger_handler(int irq, void *private)
354{
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);
358 int ret;
359
360 ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
361 (u8 *) &data->buffer,
362 sizeof(__be32) * (data->chip->num_channels - 2));
363
364 if (!ret)
365 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
366 iio_get_time_ns(indio_dev));
367
368 iio_trigger_notify_done(indio_dev->trig);
369
370 return IRQ_HANDLED;
371}
372
373static irqreturn_t atlas_interrupt_handler(int irq, void *private)
374{
375 struct iio_dev *indio_dev = private;
376 struct atlas_data *data = iio_priv(indio_dev);
377
378 irq_work_queue(&data->work);
379
380 return IRQ_HANDLED;
381}
382
383static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
384{
385 struct device *dev = &data->client->dev;
386 int suspended = pm_runtime_suspended(dev);
387 int ret;
388
389 ret = pm_runtime_get_sync(dev);
390 if (ret < 0) {
391 pm_runtime_put_noidle(dev);
392 return ret;
393 }
394
395 if (suspended)
396 msleep(data->chip->delay);
397
398 ret = regmap_bulk_read(data->regmap, reg, (u8 *) val, sizeof(*val));
399
400 pm_runtime_mark_last_busy(dev);
401 pm_runtime_put_autosuspend(dev);
402
403 return ret;
404}
405
406static int atlas_read_raw(struct iio_dev *indio_dev,
407 struct iio_chan_spec const *chan,
408 int *val, int *val2, long mask)
409{
410 struct atlas_data *data = iio_priv(indio_dev);
411
412 switch (mask) {
413 case IIO_CHAN_INFO_RAW: {
414 int ret;
415 __be32 reg;
416
417 switch (chan->type) {
418 case IIO_TEMP:
419 ret = regmap_bulk_read(data->regmap, chan->address,
420 (u8 *) ®, sizeof(reg));
421 break;
422 case IIO_PH:
423 case IIO_CONCENTRATION:
424 case IIO_ELECTRICALCONDUCTIVITY:
425 case IIO_VOLTAGE:
426 ret = iio_device_claim_direct_mode(indio_dev);
427 if (ret)
428 return ret;
429
430 ret = atlas_read_measurement(data, chan->address, ®);
431
432 iio_device_release_direct_mode(indio_dev);
433 break;
434 default:
435 ret = -EINVAL;
436 }
437
438 if (!ret) {
439 *val = be32_to_cpu(reg);
440 ret = IIO_VAL_INT;
441 }
442 return ret;
443 }
444 case IIO_CHAN_INFO_SCALE:
445 switch (chan->type) {
446 case IIO_TEMP:
447 *val = 1;
448 *val2 = 100;
449 break;
450 case IIO_PH:
451 *val = 1;
452 *val2 = 1000;
453 break;
454 case IIO_ELECTRICALCONDUCTIVITY:
455 *val = 1;
456 *val2 = 100000;
457 break;
458 case IIO_CONCENTRATION:
459 *val = 0;
460 *val2 = 1000;
461 return IIO_VAL_INT_PLUS_NANO;
462 case IIO_VOLTAGE:
463 *val = 1;
464 *val2 = 10;
465 break;
466 default:
467 return -EINVAL;
468 }
469 return IIO_VAL_FRACTIONAL;
470 }
471
472 return -EINVAL;
473}
474
475static int atlas_write_raw(struct iio_dev *indio_dev,
476 struct iio_chan_spec const *chan,
477 int val, int val2, long mask)
478{
479 struct atlas_data *data = iio_priv(indio_dev);
480 __be32 reg = cpu_to_be32(val);
481
482 if (val2 != 0 || val < 0 || val > 20000)
483 return -EINVAL;
484
485 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
486 return -EINVAL;
487
488 return regmap_bulk_write(data->regmap, chan->address,
489 ®, sizeof(reg));
490}
491
492static const struct iio_info atlas_info = {
493 .read_raw = atlas_read_raw,
494 .write_raw = atlas_write_raw,
495};
496
497static 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},
501 {}
502};
503MODULE_DEVICE_TABLE(i2c, atlas_id);
504
505static 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, },
509 { }
510};
511MODULE_DEVICE_TABLE(of, atlas_dt_ids);
512
513static int atlas_probe(struct i2c_client *client,
514 const struct i2c_device_id *id)
515{
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;
521 int ret;
522
523 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
524 if (!indio_dev)
525 return -ENOMEM;
526
527 of_id = of_match_device(atlas_dt_ids, &client->dev);
528 if (!of_id)
529 chip = &atlas_devices[id->driver_data];
530 else
531 chip = &atlas_devices[(unsigned long)of_id->data];
532
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;
539
540 trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
541 indio_dev->name, indio_dev->id);
542
543 if (!trig)
544 return -ENOMEM;
545
546 data = iio_priv(indio_dev);
547 data->client = client;
548 data->trig = trig;
549 data->chip = chip;
550 trig->dev.parent = indio_dev->dev.parent;
551 trig->ops = &atlas_interrupt_trigger_ops;
552 iio_trigger_set_drvdata(trig, indio_dev);
553
554 i2c_set_clientdata(client, indio_dev);
555
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);
560 }
561
562 ret = pm_runtime_set_active(&client->dev);
563 if (ret)
564 return ret;
565
566 if (client->irq <= 0) {
567 dev_err(&client->dev, "no valid irq defined\n");
568 return -EINVAL;
569 }
570
571 ret = chip->calibration(data);
572 if (ret)
573 return ret;
574
575 ret = iio_trigger_register(trig);
576 if (ret) {
577 dev_err(&client->dev, "failed to register trigger\n");
578 return ret;
579 }
580
581 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
582 &atlas_trigger_handler, &atlas_buffer_setup_ops);
583 if (ret) {
584 dev_err(&client->dev, "cannot setup iio trigger\n");
585 goto unregister_trigger;
586 }
587
588 init_irq_work(&data->work, atlas_work_handler);
589
590
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,
595 "atlas_irq",
596 indio_dev);
597 if (ret) {
598 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
599 goto unregister_buffer;
600 }
601
602 ret = atlas_set_powermode(data, 1);
603 if (ret) {
604 dev_err(&client->dev, "cannot power device on");
605 goto unregister_buffer;
606 }
607
608 pm_runtime_enable(&client->dev);
609 pm_runtime_set_autosuspend_delay(&client->dev, 2500);
610 pm_runtime_use_autosuspend(&client->dev);
611
612 ret = iio_device_register(indio_dev);
613 if (ret) {
614 dev_err(&client->dev, "unable to register device\n");
615 goto unregister_pm;
616 }
617
618 return 0;
619
620unregister_pm:
621 pm_runtime_disable(&client->dev);
622 atlas_set_powermode(data, 0);
623
624unregister_buffer:
625 iio_triggered_buffer_cleanup(indio_dev);
626
627unregister_trigger:
628 iio_trigger_unregister(data->trig);
629
630 return ret;
631}
632
633static int atlas_remove(struct i2c_client *client)
634{
635 struct iio_dev *indio_dev = i2c_get_clientdata(client);
636 struct atlas_data *data = iio_priv(indio_dev);
637
638 iio_device_unregister(indio_dev);
639 iio_triggered_buffer_cleanup(indio_dev);
640 iio_trigger_unregister(data->trig);
641
642 pm_runtime_disable(&client->dev);
643 pm_runtime_set_suspended(&client->dev);
644 pm_runtime_put_noidle(&client->dev);
645
646 return atlas_set_powermode(data, 0);
647}
648
649#ifdef CONFIG_PM
650static int atlas_runtime_suspend(struct device *dev)
651{
652 struct atlas_data *data =
653 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
654
655 return atlas_set_powermode(data, 0);
656}
657
658static int atlas_runtime_resume(struct device *dev)
659{
660 struct atlas_data *data =
661 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
662
663 return atlas_set_powermode(data, 1);
664}
665#endif
666
667static const struct dev_pm_ops atlas_pm_ops = {
668 SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
669 atlas_runtime_resume, NULL)
670};
671
672static struct i2c_driver atlas_driver = {
673 .driver = {
674 .name = ATLAS_DRV_NAME,
675 .of_match_table = of_match_ptr(atlas_dt_ids),
676 .pm = &atlas_pm_ops,
677 },
678 .probe = atlas_probe,
679 .remove = atlas_remove,
680 .id_table = atlas_id,
681};
682module_i2c_driver(atlas_driver);
683
684MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
685MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
686MODULE_LICENSE("GPL");
687