perf python: Do not force closing original perf descriptor in evlist.get_pollfd()
[linux/fpc-iii.git] / drivers / iio / chemical / atlas-ph-sensor.c
bloba406ad31b096f6755121fede4b7a6e2de3a24342
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
7 */
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
68 enum {
69 ATLAS_PH_SM,
70 ATLAS_EC_SM,
71 ATLAS_ORP_SM,
74 struct 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;
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,
86 .reg_bits = 8,
87 .val_bits = 8,
90 static const struct iio_chan_spec atlas_ph_channels[] = {
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,
104 IIO_CHAN_SOFT_TIMESTAMP(1),
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
115 #define ATLAS_EC_CHANNEL(_idx, _addr) \
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 }, \
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),
138 .scan_index = 0,
139 .scan_type = {
140 .sign = 'u',
141 .realbits = 32,
142 .storagebits = 32,
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),
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
159 static const struct iio_chan_spec atlas_orp_channels[] = {
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,
173 IIO_CHAN_SOFT_TIMESTAMP(1),
176 static int atlas_check_ph_calibration(struct atlas_data *data)
178 struct device *dev = &data->client->dev;
179 int ret;
180 unsigned int val;
182 ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val);
183 if (ret)
184 return ret;
186 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) {
187 dev_warn(dev, "device has not been calibrated\n");
188 return 0;
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");
200 return 0;
203 static int atlas_check_ec_calibration(struct atlas_data *data)
205 struct device *dev = &data->client->dev;
206 int ret;
207 unsigned int val;
208 __be16 rval;
210 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2);
211 if (ret)
212 return ret;
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);
218 if (ret)
219 return ret;
221 if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) {
222 dev_warn(dev, "device has not been calibrated\n");
223 return 0;
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");
231 } else {
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");
239 return 0;
242 static int atlas_check_orp_calibration(struct atlas_data *data)
244 struct device *dev = &data->client->dev;
245 int ret;
246 unsigned int val;
248 ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val);
249 if (ret)
250 return ret;
252 if (!val)
253 dev_warn(dev, "device has not been calibrated\n");
255 return 0;
258 struct atlas_device {
259 const struct iio_chan_spec *channels;
260 int num_channels;
261 int data_reg;
263 int (*calibration)(struct atlas_data *data);
264 int delay;
267 static 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,
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,
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,
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);
306 int ret;
308 ret = iio_triggered_buffer_postenable(indio_dev);
309 if (ret)
310 return ret;
312 ret = pm_runtime_get_sync(&data->client->dev);
313 if (ret < 0) {
314 pm_runtime_put_noidle(&data->client->dev);
315 return ret;
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);
324 int ret;
326 ret = iio_triggered_buffer_predisable(indio_dev);
327 if (ret)
328 return ret;
330 ret = atlas_set_interrupt(data, false);
331 if (ret)
332 return ret;
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);
358 int ret;
360 ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
361 (u8 *) &data->buffer,
362 sizeof(__be32) * (data->chip->num_channels - 2));
364 if (!ret)
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);
370 return IRQ_HANDLED;
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);
380 return IRQ_HANDLED;
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);
387 int ret;
389 ret = pm_runtime_get_sync(dev);
390 if (ret < 0) {
391 pm_runtime_put_noidle(dev);
392 return ret;
395 if (suspended)
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);
403 return ret;
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);
412 switch (mask) {
413 case IIO_CHAN_INFO_RAW: {
414 int ret;
415 __be32 reg;
417 switch (chan->type) {
418 case IIO_TEMP:
419 ret = regmap_bulk_read(data->regmap, chan->address,
420 (u8 *) &reg, 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;
430 ret = atlas_read_measurement(data, chan->address, &reg);
432 iio_device_release_direct_mode(indio_dev);
433 break;
434 default:
435 ret = -EINVAL;
438 if (!ret) {
439 *val = be32_to_cpu(reg);
440 ret = IIO_VAL_INT;
442 return ret;
444 case IIO_CHAN_INFO_SCALE:
445 switch (chan->type) {
446 case IIO_TEMP:
447 *val = 1; /* 0.01 */
448 *val2 = 100;
449 break;
450 case IIO_PH:
451 *val = 1; /* 0.001 */
452 *val2 = 1000;
453 break;
454 case IIO_ELECTRICALCONDUCTIVITY:
455 *val = 1; /* 0.00001 */
456 *val2 = 100000;
457 break;
458 case IIO_CONCENTRATION:
459 *val = 0; /* 0.000000001 */
460 *val2 = 1000;
461 return IIO_VAL_INT_PLUS_NANO;
462 case IIO_VOLTAGE:
463 *val = 1; /* 0.1 */
464 *val2 = 10;
465 break;
466 default:
467 return -EINVAL;
469 return IIO_VAL_FRACTIONAL;
472 return -EINVAL;
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)
483 return -EINVAL;
485 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
486 return -EINVAL;
488 return regmap_bulk_write(data->regmap, chan->address,
489 &reg, sizeof(reg));
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;
521 int ret;
523 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
524 if (!indio_dev)
525 return -ENOMEM;
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];
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);
543 if (!trig)
544 return -ENOMEM;
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);
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);
563 if (ret)
564 return ret;
566 if (client->irq <= 0) {
567 dev_err(&client->dev, "no valid irq defined\n");
568 return -EINVAL;
571 ret = chip->calibration(data);
572 if (ret)
573 return ret;
575 ret = iio_trigger_register(trig);
576 if (ret) {
577 dev_err(&client->dev, "failed to register trigger\n");
578 return ret;
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;
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,
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;
602 ret = atlas_set_powermode(data, 1);
603 if (ret) {
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);
613 if (ret) {
614 dev_err(&client->dev, "unable to register device\n");
615 goto unregister_pm;
618 return 0;
620 unregister_pm:
621 pm_runtime_disable(&client->dev);
622 atlas_set_powermode(data, 0);
624 unregister_buffer:
625 iio_triggered_buffer_cleanup(indio_dev);
627 unregister_trigger:
628 iio_trigger_unregister(data->trig);
630 return ret;
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);
649 #ifdef CONFIG_PM
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);
665 #endif
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 = {
673 .driver = {
674 .name = ATLAS_DRV_NAME,
675 .of_match_table = of_match_ptr(atlas_dt_ids),
676 .pm = &atlas_pm_ops,
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");