Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / iio / chemical / atlas-sensor.c
blobbaf93e5e3ca798cbe36d01b401e9ed0f047b3a47
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * atlas-sensor.c - Support for Atlas Scientific OEM SM sensors
5 * Copyright (C) 2015-2019 Konsulko Group
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/i2c.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/regmap.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/pm_runtime.h>
27 #define ATLAS_REGMAP_NAME "atlas_regmap"
28 #define ATLAS_DRV_NAME "atlas"
30 #define ATLAS_REG_DEV_TYPE 0x00
31 #define ATLAS_REG_DEV_VERSION 0x01
33 #define ATLAS_REG_INT_CONTROL 0x04
34 #define ATLAS_REG_INT_CONTROL_EN BIT(3)
36 #define ATLAS_REG_PWR_CONTROL 0x06
38 #define ATLAS_REG_PH_CALIB_STATUS 0x0d
39 #define ATLAS_REG_PH_CALIB_STATUS_MASK 0x07
40 #define ATLAS_REG_PH_CALIB_STATUS_LOW BIT(0)
41 #define ATLAS_REG_PH_CALIB_STATUS_MID BIT(1)
42 #define ATLAS_REG_PH_CALIB_STATUS_HIGH BIT(2)
44 #define ATLAS_REG_EC_CALIB_STATUS 0x0f
45 #define ATLAS_REG_EC_CALIB_STATUS_MASK 0x0f
46 #define ATLAS_REG_EC_CALIB_STATUS_DRY BIT(0)
47 #define ATLAS_REG_EC_CALIB_STATUS_SINGLE BIT(1)
48 #define ATLAS_REG_EC_CALIB_STATUS_LOW BIT(2)
49 #define ATLAS_REG_EC_CALIB_STATUS_HIGH BIT(3)
51 #define ATLAS_REG_DO_CALIB_STATUS 0x09
52 #define ATLAS_REG_DO_CALIB_STATUS_MASK 0x03
53 #define ATLAS_REG_DO_CALIB_STATUS_PRESSURE BIT(0)
54 #define ATLAS_REG_DO_CALIB_STATUS_DO BIT(1)
56 #define ATLAS_REG_RTD_DATA 0x0e
58 #define ATLAS_REG_PH_TEMP_DATA 0x0e
59 #define ATLAS_REG_PH_DATA 0x16
61 #define ATLAS_REG_EC_PROBE 0x08
62 #define ATLAS_REG_EC_TEMP_DATA 0x10
63 #define ATLAS_REG_EC_DATA 0x18
64 #define ATLAS_REG_TDS_DATA 0x1c
65 #define ATLAS_REG_PSS_DATA 0x20
67 #define ATLAS_REG_ORP_CALIB_STATUS 0x0d
68 #define ATLAS_REG_ORP_DATA 0x0e
70 #define ATLAS_REG_DO_TEMP_DATA 0x12
71 #define ATLAS_REG_DO_DATA 0x22
73 #define ATLAS_PH_INT_TIME_IN_MS 450
74 #define ATLAS_EC_INT_TIME_IN_MS 650
75 #define ATLAS_ORP_INT_TIME_IN_MS 450
76 #define ATLAS_DO_INT_TIME_IN_MS 450
77 #define ATLAS_RTD_INT_TIME_IN_MS 450
79 enum {
80 ATLAS_PH_SM,
81 ATLAS_EC_SM,
82 ATLAS_ORP_SM,
83 ATLAS_DO_SM,
84 ATLAS_RTD_SM,
87 struct atlas_data {
88 struct i2c_client *client;
89 struct iio_trigger *trig;
90 const struct atlas_device *chip;
91 struct regmap *regmap;
92 struct irq_work work;
93 unsigned int interrupt_enabled;
94 /* 96-bit data + 32-bit pad + 64-bit timestamp */
95 __be32 buffer[6] __aligned(8);
98 static const struct regmap_config atlas_regmap_config = {
99 .name = ATLAS_REGMAP_NAME,
100 .reg_bits = 8,
101 .val_bits = 8,
104 static int atlas_buffer_num_channels(const struct iio_chan_spec *spec)
106 int idx = 0;
108 for (; spec->type != IIO_TIMESTAMP; spec++)
109 idx++;
111 return idx;
114 static const struct iio_chan_spec atlas_ph_channels[] = {
116 .type = IIO_PH,
117 .address = ATLAS_REG_PH_DATA,
118 .info_mask_separate =
119 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
120 .scan_index = 0,
121 .scan_type = {
122 .sign = 'u',
123 .realbits = 32,
124 .storagebits = 32,
125 .endianness = IIO_BE,
128 IIO_CHAN_SOFT_TIMESTAMP(1),
130 .type = IIO_TEMP,
131 .address = ATLAS_REG_PH_TEMP_DATA,
132 .info_mask_separate =
133 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
134 .output = 1,
135 .scan_index = -1
139 #define ATLAS_CONCENTRATION_CHANNEL(_idx, _addr) \
141 .type = IIO_CONCENTRATION, \
142 .indexed = 1, \
143 .channel = _idx, \
144 .address = _addr, \
145 .info_mask_separate = \
146 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
147 .scan_index = _idx + 1, \
148 .scan_type = { \
149 .sign = 'u', \
150 .realbits = 32, \
151 .storagebits = 32, \
152 .endianness = IIO_BE, \
153 }, \
156 static const struct iio_chan_spec atlas_ec_channels[] = {
158 .type = IIO_ELECTRICALCONDUCTIVITY,
159 .address = ATLAS_REG_EC_DATA,
160 .info_mask_separate =
161 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
162 .scan_index = 0,
163 .scan_type = {
164 .sign = 'u',
165 .realbits = 32,
166 .storagebits = 32,
167 .endianness = IIO_BE,
170 ATLAS_CONCENTRATION_CHANNEL(0, ATLAS_REG_TDS_DATA),
171 ATLAS_CONCENTRATION_CHANNEL(1, ATLAS_REG_PSS_DATA),
172 IIO_CHAN_SOFT_TIMESTAMP(3),
174 .type = IIO_TEMP,
175 .address = ATLAS_REG_EC_TEMP_DATA,
176 .info_mask_separate =
177 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
178 .output = 1,
179 .scan_index = -1
183 static const struct iio_chan_spec atlas_orp_channels[] = {
185 .type = IIO_VOLTAGE,
186 .address = ATLAS_REG_ORP_DATA,
187 .info_mask_separate =
188 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
189 .scan_index = 0,
190 .scan_type = {
191 .sign = 's',
192 .realbits = 32,
193 .storagebits = 32,
194 .endianness = IIO_BE,
197 IIO_CHAN_SOFT_TIMESTAMP(1),
200 static const struct iio_chan_spec atlas_do_channels[] = {
202 .type = IIO_CONCENTRATION,
203 .address = ATLAS_REG_DO_DATA,
204 .info_mask_separate =
205 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
206 .scan_index = 0,
207 .scan_type = {
208 .sign = 'u',
209 .realbits = 32,
210 .storagebits = 32,
211 .endianness = IIO_BE,
214 IIO_CHAN_SOFT_TIMESTAMP(1),
216 .type = IIO_TEMP,
217 .address = ATLAS_REG_DO_TEMP_DATA,
218 .info_mask_separate =
219 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
220 .output = 1,
221 .scan_index = -1
225 static const struct iio_chan_spec atlas_rtd_channels[] = {
227 .type = IIO_TEMP,
228 .address = ATLAS_REG_RTD_DATA,
229 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
230 .scan_index = 0,
231 .scan_type = {
232 .sign = 's',
233 .realbits = 32,
234 .storagebits = 32,
235 .endianness = IIO_BE,
238 IIO_CHAN_SOFT_TIMESTAMP(1),
241 static int atlas_check_ph_calibration(struct atlas_data *data)
243 struct device *dev = &data->client->dev;
244 int ret;
245 unsigned int val;
247 ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val);
248 if (ret)
249 return ret;
251 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) {
252 dev_warn(dev, "device has not been calibrated\n");
253 return 0;
256 if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW))
257 dev_warn(dev, "device missing low point calibration\n");
259 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID))
260 dev_warn(dev, "device missing mid point calibration\n");
262 if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH))
263 dev_warn(dev, "device missing high point calibration\n");
265 return 0;
268 static int atlas_check_ec_calibration(struct atlas_data *data)
270 struct device *dev = &data->client->dev;
271 int ret;
272 unsigned int val;
273 __be16 rval;
275 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2);
276 if (ret)
277 return ret;
279 val = be16_to_cpu(rval);
280 dev_info(dev, "probe set to K = %d.%.2d", val / 100, val % 100);
282 ret = regmap_read(data->regmap, ATLAS_REG_EC_CALIB_STATUS, &val);
283 if (ret)
284 return ret;
286 if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) {
287 dev_warn(dev, "device has not been calibrated\n");
288 return 0;
291 if (!(val & ATLAS_REG_EC_CALIB_STATUS_DRY))
292 dev_warn(dev, "device missing dry point calibration\n");
294 if (val & ATLAS_REG_EC_CALIB_STATUS_SINGLE) {
295 dev_warn(dev, "device using single point calibration\n");
296 } else {
297 if (!(val & ATLAS_REG_EC_CALIB_STATUS_LOW))
298 dev_warn(dev, "device missing low point calibration\n");
300 if (!(val & ATLAS_REG_EC_CALIB_STATUS_HIGH))
301 dev_warn(dev, "device missing high point calibration\n");
304 return 0;
307 static int atlas_check_orp_calibration(struct atlas_data *data)
309 struct device *dev = &data->client->dev;
310 int ret;
311 unsigned int val;
313 ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val);
314 if (ret)
315 return ret;
317 if (!val)
318 dev_warn(dev, "device has not been calibrated\n");
320 return 0;
323 static int atlas_check_do_calibration(struct atlas_data *data)
325 struct device *dev = &data->client->dev;
326 int ret;
327 unsigned int val;
329 ret = regmap_read(data->regmap, ATLAS_REG_DO_CALIB_STATUS, &val);
330 if (ret)
331 return ret;
333 if (!(val & ATLAS_REG_DO_CALIB_STATUS_MASK)) {
334 dev_warn(dev, "device has not been calibrated\n");
335 return 0;
338 if (!(val & ATLAS_REG_DO_CALIB_STATUS_PRESSURE))
339 dev_warn(dev, "device missing atmospheric pressure calibration\n");
341 if (!(val & ATLAS_REG_DO_CALIB_STATUS_DO))
342 dev_warn(dev, "device missing dissolved oxygen calibration\n");
344 return 0;
347 struct atlas_device {
348 const struct iio_chan_spec *channels;
349 int num_channels;
350 int data_reg;
352 int (*calibration)(struct atlas_data *data);
353 int delay;
356 static const struct atlas_device atlas_devices[] = {
357 [ATLAS_PH_SM] = {
358 .channels = atlas_ph_channels,
359 .num_channels = 3,
360 .data_reg = ATLAS_REG_PH_DATA,
361 .calibration = &atlas_check_ph_calibration,
362 .delay = ATLAS_PH_INT_TIME_IN_MS,
364 [ATLAS_EC_SM] = {
365 .channels = atlas_ec_channels,
366 .num_channels = 5,
367 .data_reg = ATLAS_REG_EC_DATA,
368 .calibration = &atlas_check_ec_calibration,
369 .delay = ATLAS_EC_INT_TIME_IN_MS,
371 [ATLAS_ORP_SM] = {
372 .channels = atlas_orp_channels,
373 .num_channels = 2,
374 .data_reg = ATLAS_REG_ORP_DATA,
375 .calibration = &atlas_check_orp_calibration,
376 .delay = ATLAS_ORP_INT_TIME_IN_MS,
378 [ATLAS_DO_SM] = {
379 .channels = atlas_do_channels,
380 .num_channels = 3,
381 .data_reg = ATLAS_REG_DO_DATA,
382 .calibration = &atlas_check_do_calibration,
383 .delay = ATLAS_DO_INT_TIME_IN_MS,
385 [ATLAS_RTD_SM] = {
386 .channels = atlas_rtd_channels,
387 .num_channels = 2,
388 .data_reg = ATLAS_REG_RTD_DATA,
389 .delay = ATLAS_RTD_INT_TIME_IN_MS,
393 static int atlas_set_powermode(struct atlas_data *data, int on)
395 return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
398 static int atlas_set_interrupt(struct atlas_data *data, bool state)
400 if (!data->interrupt_enabled)
401 return 0;
403 return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL,
404 ATLAS_REG_INT_CONTROL_EN,
405 state ? ATLAS_REG_INT_CONTROL_EN : 0);
408 static int atlas_buffer_postenable(struct iio_dev *indio_dev)
410 struct atlas_data *data = iio_priv(indio_dev);
411 int ret;
413 ret = pm_runtime_resume_and_get(&data->client->dev);
414 if (ret)
415 return ret;
417 return atlas_set_interrupt(data, true);
420 static int atlas_buffer_predisable(struct iio_dev *indio_dev)
422 struct atlas_data *data = iio_priv(indio_dev);
423 int ret;
425 ret = atlas_set_interrupt(data, false);
426 if (ret)
427 return ret;
429 pm_runtime_mark_last_busy(&data->client->dev);
430 ret = pm_runtime_put_autosuspend(&data->client->dev);
431 if (ret)
432 return ret;
434 return 0;
437 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
438 .postenable = atlas_buffer_postenable,
439 .predisable = atlas_buffer_predisable,
442 static void atlas_work_handler(struct irq_work *work)
444 struct atlas_data *data = container_of(work, struct atlas_data, work);
446 iio_trigger_poll(data->trig);
449 static irqreturn_t atlas_trigger_handler(int irq, void *private)
451 struct iio_poll_func *pf = private;
452 struct iio_dev *indio_dev = pf->indio_dev;
453 struct atlas_data *data = iio_priv(indio_dev);
454 int channels = atlas_buffer_num_channels(data->chip->channels);
455 int ret;
457 ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
458 &data->buffer, sizeof(__be32) * channels);
460 if (!ret)
461 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
462 iio_get_time_ns(indio_dev));
464 iio_trigger_notify_done(indio_dev->trig);
466 return IRQ_HANDLED;
469 static irqreturn_t atlas_interrupt_handler(int irq, void *private)
471 struct iio_dev *indio_dev = private;
472 struct atlas_data *data = iio_priv(indio_dev);
474 irq_work_queue(&data->work);
476 return IRQ_HANDLED;
479 static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
481 struct device *dev = &data->client->dev;
482 int suspended = pm_runtime_suspended(dev);
483 int ret;
485 ret = pm_runtime_resume_and_get(dev);
486 if (ret)
487 return ret;
489 if (suspended)
490 msleep(data->chip->delay);
492 ret = regmap_bulk_read(data->regmap, reg, val, sizeof(*val));
494 pm_runtime_mark_last_busy(dev);
495 pm_runtime_put_autosuspend(dev);
497 return ret;
500 static int atlas_read_raw(struct iio_dev *indio_dev,
501 struct iio_chan_spec const *chan,
502 int *val, int *val2, long mask)
504 struct atlas_data *data = iio_priv(indio_dev);
506 switch (mask) {
507 case IIO_CHAN_INFO_PROCESSED:
508 case IIO_CHAN_INFO_RAW: {
509 int ret;
510 __be32 reg;
512 switch (chan->type) {
513 case IIO_TEMP:
514 ret = regmap_bulk_read(data->regmap, chan->address,
515 &reg, sizeof(reg));
516 break;
517 case IIO_PH:
518 case IIO_CONCENTRATION:
519 case IIO_ELECTRICALCONDUCTIVITY:
520 case IIO_VOLTAGE:
521 ret = iio_device_claim_direct_mode(indio_dev);
522 if (ret)
523 return ret;
525 ret = atlas_read_measurement(data, chan->address, &reg);
527 iio_device_release_direct_mode(indio_dev);
528 break;
529 default:
530 ret = -EINVAL;
533 if (!ret) {
534 *val = be32_to_cpu(reg);
535 ret = IIO_VAL_INT;
537 return ret;
539 case IIO_CHAN_INFO_SCALE:
540 switch (chan->type) {
541 case IIO_TEMP:
542 *val = 10;
543 return IIO_VAL_INT;
544 case IIO_PH:
545 *val = 1; /* 0.001 */
546 *val2 = 1000;
547 break;
548 case IIO_ELECTRICALCONDUCTIVITY:
549 *val = 1; /* 0.00001 */
550 *val2 = 100000;
551 break;
552 case IIO_CONCENTRATION:
553 *val = 0; /* 0.000000001 */
554 *val2 = 1000;
555 return IIO_VAL_INT_PLUS_NANO;
556 case IIO_VOLTAGE:
557 *val = 1; /* 0.1 */
558 *val2 = 10;
559 break;
560 default:
561 return -EINVAL;
563 return IIO_VAL_FRACTIONAL;
566 return -EINVAL;
569 static int atlas_write_raw(struct iio_dev *indio_dev,
570 struct iio_chan_spec const *chan,
571 int val, int val2, long mask)
573 struct atlas_data *data = iio_priv(indio_dev);
574 __be32 reg = cpu_to_be32(val / 10);
576 if (val2 != 0 || val < 0 || val > 20000)
577 return -EINVAL;
579 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
580 return -EINVAL;
582 return regmap_bulk_write(data->regmap, chan->address,
583 &reg, sizeof(reg));
586 static const struct iio_info atlas_info = {
587 .read_raw = atlas_read_raw,
588 .write_raw = atlas_write_raw,
591 static const struct i2c_device_id atlas_id[] = {
592 { "atlas-ph-sm", (kernel_ulong_t)&atlas_devices[ATLAS_PH_SM] },
593 { "atlas-ec-sm", (kernel_ulong_t)&atlas_devices[ATLAS_EC_SM] },
594 { "atlas-orp-sm", (kernel_ulong_t)&atlas_devices[ATLAS_ORP_SM] },
595 { "atlas-do-sm", (kernel_ulong_t)&atlas_devices[ATLAS_DO_SM] },
596 { "atlas-rtd-sm", (kernel_ulong_t)&atlas_devices[ATLAS_RTD_SM] },
599 MODULE_DEVICE_TABLE(i2c, atlas_id);
601 static const struct of_device_id atlas_dt_ids[] = {
602 { .compatible = "atlas,ph-sm", .data = &atlas_devices[ATLAS_PH_SM] },
603 { .compatible = "atlas,ec-sm", .data = &atlas_devices[ATLAS_EC_SM] },
604 { .compatible = "atlas,orp-sm", .data = &atlas_devices[ATLAS_ORP_SM] },
605 { .compatible = "atlas,do-sm", .data = &atlas_devices[ATLAS_DO_SM] },
606 { .compatible = "atlas,rtd-sm", .data = &atlas_devices[ATLAS_RTD_SM] },
609 MODULE_DEVICE_TABLE(of, atlas_dt_ids);
611 static int atlas_probe(struct i2c_client *client)
613 struct atlas_data *data;
614 const struct atlas_device *chip;
615 struct iio_trigger *trig;
616 struct iio_dev *indio_dev;
617 int ret;
619 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
620 if (!indio_dev)
621 return -ENOMEM;
623 chip = i2c_get_match_data(client);
625 indio_dev->info = &atlas_info;
626 indio_dev->name = ATLAS_DRV_NAME;
627 indio_dev->channels = chip->channels;
628 indio_dev->num_channels = chip->num_channels;
629 indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
631 trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
632 indio_dev->name, iio_device_id(indio_dev));
634 if (!trig)
635 return -ENOMEM;
637 data = iio_priv(indio_dev);
638 data->client = client;
639 data->trig = trig;
640 data->chip = chip;
641 iio_trigger_set_drvdata(trig, indio_dev);
643 i2c_set_clientdata(client, indio_dev);
645 data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config);
646 if (IS_ERR(data->regmap)) {
647 dev_err(&client->dev, "regmap initialization failed\n");
648 return PTR_ERR(data->regmap);
651 ret = pm_runtime_set_active(&client->dev);
652 if (ret)
653 return ret;
655 ret = chip->calibration(data);
656 if (ret)
657 return ret;
659 ret = iio_trigger_register(trig);
660 if (ret) {
661 dev_err(&client->dev, "failed to register trigger\n");
662 return ret;
665 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
666 &atlas_trigger_handler, &atlas_buffer_setup_ops);
667 if (ret) {
668 dev_err(&client->dev, "cannot setup iio trigger\n");
669 goto unregister_trigger;
672 init_irq_work(&data->work, atlas_work_handler);
674 if (client->irq > 0) {
675 /* interrupt pin toggles on new conversion */
676 ret = devm_request_threaded_irq(&client->dev, client->irq,
677 NULL, atlas_interrupt_handler,
678 IRQF_TRIGGER_RISING |
679 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
680 "atlas_irq",
681 indio_dev);
683 if (ret)
684 dev_warn(&client->dev,
685 "request irq (%d) failed\n", client->irq);
686 else
687 data->interrupt_enabled = 1;
690 ret = atlas_set_powermode(data, 1);
691 if (ret) {
692 dev_err(&client->dev, "cannot power device on");
693 goto unregister_buffer;
696 pm_runtime_enable(&client->dev);
697 pm_runtime_set_autosuspend_delay(&client->dev, 2500);
698 pm_runtime_use_autosuspend(&client->dev);
700 ret = iio_device_register(indio_dev);
701 if (ret) {
702 dev_err(&client->dev, "unable to register device\n");
703 goto unregister_pm;
706 return 0;
708 unregister_pm:
709 pm_runtime_disable(&client->dev);
710 atlas_set_powermode(data, 0);
712 unregister_buffer:
713 iio_triggered_buffer_cleanup(indio_dev);
715 unregister_trigger:
716 iio_trigger_unregister(data->trig);
718 return ret;
721 static void atlas_remove(struct i2c_client *client)
723 struct iio_dev *indio_dev = i2c_get_clientdata(client);
724 struct atlas_data *data = iio_priv(indio_dev);
725 int ret;
727 iio_device_unregister(indio_dev);
728 iio_triggered_buffer_cleanup(indio_dev);
729 iio_trigger_unregister(data->trig);
731 pm_runtime_disable(&client->dev);
732 pm_runtime_set_suspended(&client->dev);
734 ret = atlas_set_powermode(data, 0);
735 if (ret)
736 dev_err(&client->dev, "Failed to power down device (%pe)\n",
737 ERR_PTR(ret));
740 static int atlas_runtime_suspend(struct device *dev)
742 struct atlas_data *data =
743 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
745 return atlas_set_powermode(data, 0);
748 static int atlas_runtime_resume(struct device *dev)
750 struct atlas_data *data =
751 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
753 return atlas_set_powermode(data, 1);
756 static const struct dev_pm_ops atlas_pm_ops = {
757 RUNTIME_PM_OPS(atlas_runtime_suspend, atlas_runtime_resume, NULL)
760 static struct i2c_driver atlas_driver = {
761 .driver = {
762 .name = ATLAS_DRV_NAME,
763 .of_match_table = atlas_dt_ids,
764 .pm = pm_ptr(&atlas_pm_ops),
766 .probe = atlas_probe,
767 .remove = atlas_remove,
768 .id_table = atlas_id,
770 module_i2c_driver(atlas_driver);
772 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
773 MODULE_DESCRIPTION("Atlas Scientific SM sensors");
774 MODULE_LICENSE("GPL");