staging: octeon: prevent poll during rx init
[linux/fpc-iii.git] / drivers / iio / chemical / atlas-ph-sensor.c
blob84fbff32b96d04eb90ba53d11573282e72911bd3
1 /*
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
76 enum {
77 ATLAS_PH_SM,
78 ATLAS_EC_SM,
79 ATLAS_ORP_SM,
82 struct atlas_data {
83 struct i2c_client *client;
84 struct iio_trigger *trig;
85 struct atlas_device *chip;
86 struct regmap *regmap;
87 struct irq_work work;
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,
94 .reg_bits = 8,
95 .val_bits = 8,
98 static const struct iio_chan_spec atlas_ph_channels[] = {
100 .type = IIO_PH,
101 .address = ATLAS_REG_PH_DATA,
102 .info_mask_separate =
103 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
104 .scan_index = 0,
105 .scan_type = {
106 .sign = 'u',
107 .realbits = 32,
108 .storagebits = 32,
109 .endianness = IIO_BE,
112 IIO_CHAN_SOFT_TIMESTAMP(1),
114 .type = IIO_TEMP,
115 .address = ATLAS_REG_PH_TEMP_DATA,
116 .info_mask_separate =
117 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
118 .output = 1,
119 .scan_index = -1
123 #define ATLAS_EC_CHANNEL(_idx, _addr) \
125 .type = IIO_CONCENTRATION, \
126 .indexed = 1, \
127 .channel = _idx, \
128 .address = _addr, \
129 .info_mask_separate = \
130 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
131 .scan_index = _idx + 1, \
132 .scan_type = { \
133 .sign = 'u', \
134 .realbits = 32, \
135 .storagebits = 32, \
136 .endianness = IIO_BE, \
137 }, \
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),
146 .scan_index = 0,
147 .scan_type = {
148 .sign = 'u',
149 .realbits = 32,
150 .storagebits = 32,
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),
158 .type = IIO_TEMP,
159 .address = ATLAS_REG_EC_TEMP_DATA,
160 .info_mask_separate =
161 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
162 .output = 1,
163 .scan_index = -1
167 static const struct iio_chan_spec atlas_orp_channels[] = {
169 .type = IIO_VOLTAGE,
170 .address = ATLAS_REG_ORP_DATA,
171 .info_mask_separate =
172 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
173 .scan_index = 0,
174 .scan_type = {
175 .sign = 's',
176 .realbits = 32,
177 .storagebits = 32,
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;
187 int ret;
188 unsigned int val;
190 ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val);
191 if (ret)
192 return ret;
194 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) {
195 dev_warn(dev, "device has not been calibrated\n");
196 return 0;
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");
208 return 0;
211 static int atlas_check_ec_calibration(struct atlas_data *data)
213 struct device *dev = &data->client->dev;
214 int ret;
215 unsigned int val;
217 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &val, 2);
218 if (ret)
219 return ret;
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);
225 if (ret)
226 return ret;
228 if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) {
229 dev_warn(dev, "device has not been calibrated\n");
230 return 0;
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");
238 } else {
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");
246 return 0;
249 static int atlas_check_orp_calibration(struct atlas_data *data)
251 struct device *dev = &data->client->dev;
252 int ret;
253 unsigned int val;
255 ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val);
256 if (ret)
257 return ret;
259 if (!val)
260 dev_warn(dev, "device has not been calibrated\n");
262 return 0;
265 struct atlas_device {
266 const struct iio_chan_spec *channels;
267 int num_channels;
268 int data_reg;
270 int (*calibration)(struct atlas_data *data);
271 int delay;
274 static struct atlas_device atlas_devices[] = {
275 [ATLAS_PH_SM] = {
276 .channels = atlas_ph_channels,
277 .num_channels = 3,
278 .data_reg = ATLAS_REG_PH_DATA,
279 .calibration = &atlas_check_ph_calibration,
280 .delay = ATLAS_PH_INT_TIME_IN_US,
282 [ATLAS_EC_SM] = {
283 .channels = atlas_ec_channels,
284 .num_channels = 5,
285 .data_reg = ATLAS_REG_EC_DATA,
286 .calibration = &atlas_check_ec_calibration,
287 .delay = ATLAS_EC_INT_TIME_IN_US,
289 [ATLAS_ORP_SM] = {
290 .channels = atlas_orp_channels,
291 .num_channels = 2,
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);
313 int ret;
315 ret = iio_triggered_buffer_postenable(indio_dev);
316 if (ret)
317 return ret;
319 ret = pm_runtime_get_sync(&data->client->dev);
320 if (ret < 0) {
321 pm_runtime_put_noidle(&data->client->dev);
322 return ret;
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);
331 int ret;
333 ret = iio_triggered_buffer_predisable(indio_dev);
334 if (ret)
335 return ret;
337 ret = atlas_set_interrupt(data, false);
338 if (ret)
339 return ret;
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);
366 int ret;
368 ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
369 (u8 *) &data->buffer,
370 sizeof(__be32) * (data->chip->num_channels - 2));
372 if (!ret)
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);
378 return IRQ_HANDLED;
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);
388 return IRQ_HANDLED;
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);
395 int ret;
397 ret = pm_runtime_get_sync(dev);
398 if (ret < 0) {
399 pm_runtime_put_noidle(dev);
400 return ret;
403 if (suspended)
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);
411 return ret;
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);
420 switch (mask) {
421 case IIO_CHAN_INFO_RAW: {
422 int ret;
423 __be32 reg;
425 switch (chan->type) {
426 case IIO_TEMP:
427 ret = regmap_bulk_read(data->regmap, chan->address,
428 (u8 *) &reg, sizeof(reg));
429 break;
430 case IIO_PH:
431 case IIO_CONCENTRATION:
432 case IIO_ELECTRICALCONDUCTIVITY:
433 case IIO_VOLTAGE:
434 ret = iio_device_claim_direct_mode(indio_dev);
435 if (ret)
436 return ret;
438 ret = atlas_read_measurement(data, chan->address, &reg);
440 iio_device_release_direct_mode(indio_dev);
441 break;
442 default:
443 ret = -EINVAL;
446 if (!ret) {
447 *val = be32_to_cpu(reg);
448 ret = IIO_VAL_INT;
450 return ret;
452 case IIO_CHAN_INFO_SCALE:
453 switch (chan->type) {
454 case IIO_TEMP:
455 *val = 1; /* 0.01 */
456 *val2 = 100;
457 break;
458 case IIO_PH:
459 *val = 1; /* 0.001 */
460 *val2 = 1000;
461 break;
462 case IIO_ELECTRICALCONDUCTIVITY:
463 *val = 1; /* 0.00001 */
464 *val = 100000;
465 break;
466 case IIO_CONCENTRATION:
467 *val = 0; /* 0.000000001 */
468 *val2 = 1000;
469 return IIO_VAL_INT_PLUS_NANO;
470 case IIO_VOLTAGE:
471 *val = 1; /* 0.1 */
472 *val2 = 10;
473 break;
474 default:
475 return -EINVAL;
477 return IIO_VAL_FRACTIONAL;
480 return -EINVAL;
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)
491 return -EINVAL;
493 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
494 return -EINVAL;
496 return regmap_bulk_write(data->regmap, chan->address,
497 &reg, sizeof(reg));
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;
530 int ret;
532 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
533 if (!indio_dev)
534 return -ENOMEM;
536 of_id = of_match_device(atlas_dt_ids, &client->dev);
537 if (!of_id)
538 chip = &atlas_devices[id->driver_data];
539 else
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);
552 if (!trig)
553 return -ENOMEM;
555 data = iio_priv(indio_dev);
556 data->client = client;
557 data->trig = trig;
558 data->chip = chip;
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);
572 if (ret)
573 return ret;
575 if (client->irq <= 0) {
576 dev_err(&client->dev, "no valid irq defined\n");
577 return -EINVAL;
580 ret = chip->calibration(data);
581 if (ret)
582 return ret;
584 ret = iio_trigger_register(trig);
585 if (ret) {
586 dev_err(&client->dev, "failed to register trigger\n");
587 return ret;
590 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
591 &atlas_trigger_handler, &atlas_buffer_setup_ops);
592 if (ret) {
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,
604 "atlas_irq",
605 indio_dev);
606 if (ret) {
607 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
608 goto unregister_buffer;
611 ret = atlas_set_powermode(data, 1);
612 if (ret) {
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);
622 if (ret) {
623 dev_err(&client->dev, "unable to register device\n");
624 goto unregister_pm;
627 return 0;
629 unregister_pm:
630 pm_runtime_disable(&client->dev);
631 atlas_set_powermode(data, 0);
633 unregister_buffer:
634 iio_triggered_buffer_cleanup(indio_dev);
636 unregister_trigger:
637 iio_trigger_unregister(data->trig);
639 return ret;
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);
658 #ifdef CONFIG_PM
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);
674 #endif
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 = {
682 .driver = {
683 .name = ATLAS_DRV_NAME,
684 .of_match_table = of_match_ptr(atlas_dt_ids),
685 .pm = &atlas_pm_ops,
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");