Merge branch 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[linux-2.6-block.git] / drivers / iio / chemical / atlas-ph-sensor.c
CommitLineData
27dec00e
MR
1/*
2 * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor
3 *
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
5 *
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.
10 *
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.
15 */
16
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>
7103b99b 27#include <linux/of_device.h>
27dec00e
MR
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>
35
36#define ATLAS_REGMAP_NAME "atlas_ph_regmap"
37#define ATLAS_DRV_NAME "atlas_ph"
38
39#define ATLAS_REG_DEV_TYPE 0x00
40#define ATLAS_REG_DEV_VERSION 0x01
41
42#define ATLAS_REG_INT_CONTROL 0x04
43#define ATLAS_REG_INT_CONTROL_EN BIT(3)
44
45#define ATLAS_REG_PWR_CONTROL 0x06
46
7103b99b
MR
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)
27dec00e 52
e8dd92bf
MR
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)
59
7103b99b 60#define ATLAS_REG_PH_TEMP_DATA 0x0e
27dec00e
MR
61#define ATLAS_REG_PH_DATA 0x16
62
e8dd92bf
MR
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
68
ce08cc98
MR
69#define ATLAS_REG_ORP_CALIB_STATUS 0x0d
70#define ATLAS_REG_ORP_DATA 0x0e
71
27dec00e 72#define ATLAS_PH_INT_TIME_IN_US 450000
e8dd92bf 73#define ATLAS_EC_INT_TIME_IN_US 650000
ce08cc98 74#define ATLAS_ORP_INT_TIME_IN_US 450000
27dec00e 75
7103b99b
MR
76enum {
77 ATLAS_PH_SM,
e8dd92bf 78 ATLAS_EC_SM,
ce08cc98 79 ATLAS_ORP_SM,
7103b99b
MR
80};
81
27dec00e
MR
82struct atlas_data {
83 struct i2c_client *client;
84 struct iio_trigger *trig;
7103b99b 85 struct atlas_device *chip;
27dec00e
MR
86 struct regmap *regmap;
87 struct irq_work work;
88
e8dd92bf 89 __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
27dec00e
MR
90};
91
27dec00e
MR
92static const struct regmap_config atlas_regmap_config = {
93 .name = ATLAS_REGMAP_NAME,
27dec00e
MR
94 .reg_bits = 8,
95 .val_bits = 8,
27dec00e
MR
96};
97
7103b99b 98static const struct iio_chan_spec atlas_ph_channels[] = {
27dec00e
MR
99 {
100 .type = IIO_PH,
7103b99b 101 .address = ATLAS_REG_PH_DATA,
27dec00e
MR
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,
110 },
111 },
112 IIO_CHAN_SOFT_TIMESTAMP(1),
113 {
114 .type = IIO_TEMP,
7103b99b 115 .address = ATLAS_REG_PH_TEMP_DATA,
27dec00e
MR
116 .info_mask_separate =
117 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
118 .output = 1,
119 .scan_index = -1
120 },
121};
122
e8dd92bf
MR
123#define ATLAS_EC_CHANNEL(_idx, _addr) \
124 {\
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 }, \
138 }
139
140static const struct iio_chan_spec atlas_ec_channels[] = {
141 {
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,
152 },
153 },
154 ATLAS_EC_CHANNEL(0, ATLAS_REG_TDS_DATA),
155 ATLAS_EC_CHANNEL(1, ATLAS_REG_PSS_DATA),
156 IIO_CHAN_SOFT_TIMESTAMP(3),
157 {
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
164 },
165};
166
ce08cc98
MR
167static const struct iio_chan_spec atlas_orp_channels[] = {
168 {
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,
179 },
180 },
181 IIO_CHAN_SOFT_TIMESTAMP(1),
182};
183
7103b99b
MR
184static int atlas_check_ph_calibration(struct atlas_data *data)
185{
186 struct device *dev = &data->client->dev;
187 int ret;
188 unsigned int val;
189
190 ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val);
191 if (ret)
192 return ret;
193
194 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) {
195 dev_warn(dev, "device has not been calibrated\n");
196 return 0;
197 }
198
199 if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW))
200 dev_warn(dev, "device missing low point calibration\n");
201
202 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID))
203 dev_warn(dev, "device missing mid point calibration\n");
204
205 if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH))
206 dev_warn(dev, "device missing high point calibration\n");
207
208 return 0;
209}
210
e8dd92bf
MR
211static int atlas_check_ec_calibration(struct atlas_data *data)
212{
213 struct device *dev = &data->client->dev;
214 int ret;
215 unsigned int val;
d1fe85ec 216 __be16 rval;
e8dd92bf 217
d1fe85ec 218 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2);
e8dd92bf
MR
219 if (ret)
220 return ret;
221
d1fe85ec
SB
222 val = be16_to_cpu(rval);
223 dev_info(dev, "probe set to K = %d.%.2d", val / 100, val % 100);
e8dd92bf
MR
224
225 ret = regmap_read(data->regmap, ATLAS_REG_EC_CALIB_STATUS, &val);
226 if (ret)
227 return ret;
228
229 if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) {
230 dev_warn(dev, "device has not been calibrated\n");
231 return 0;
232 }
233
234 if (!(val & ATLAS_REG_EC_CALIB_STATUS_DRY))
235 dev_warn(dev, "device missing dry point calibration\n");
236
237 if (val & ATLAS_REG_EC_CALIB_STATUS_SINGLE) {
238 dev_warn(dev, "device using single point calibration\n");
239 } else {
240 if (!(val & ATLAS_REG_EC_CALIB_STATUS_LOW))
241 dev_warn(dev, "device missing low point calibration\n");
242
243 if (!(val & ATLAS_REG_EC_CALIB_STATUS_HIGH))
244 dev_warn(dev, "device missing high point calibration\n");
245 }
246
247 return 0;
248}
249
ce08cc98
MR
250static int atlas_check_orp_calibration(struct atlas_data *data)
251{
252 struct device *dev = &data->client->dev;
253 int ret;
254 unsigned int val;
255
256 ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val);
257 if (ret)
258 return ret;
259
260 if (!val)
261 dev_warn(dev, "device has not been calibrated\n");
262
263 return 0;
264};
265
7103b99b
MR
266struct atlas_device {
267 const struct iio_chan_spec *channels;
268 int num_channels;
269 int data_reg;
270
271 int (*calibration)(struct atlas_data *data);
272 int delay;
273};
274
275static struct atlas_device atlas_devices[] = {
276 [ATLAS_PH_SM] = {
277 .channels = atlas_ph_channels,
278 .num_channels = 3,
279 .data_reg = ATLAS_REG_PH_DATA,
280 .calibration = &atlas_check_ph_calibration,
281 .delay = ATLAS_PH_INT_TIME_IN_US,
282 },
e8dd92bf
MR
283 [ATLAS_EC_SM] = {
284 .channels = atlas_ec_channels,
285 .num_channels = 5,
286 .data_reg = ATLAS_REG_EC_DATA,
287 .calibration = &atlas_check_ec_calibration,
288 .delay = ATLAS_EC_INT_TIME_IN_US,
289 },
ce08cc98
MR
290 [ATLAS_ORP_SM] = {
291 .channels = atlas_orp_channels,
292 .num_channels = 2,
293 .data_reg = ATLAS_REG_ORP_DATA,
294 .calibration = &atlas_check_orp_calibration,
295 .delay = ATLAS_ORP_INT_TIME_IN_US,
296 },
7103b99b
MR
297};
298
27dec00e
MR
299static int atlas_set_powermode(struct atlas_data *data, int on)
300{
301 return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
302}
303
304static int atlas_set_interrupt(struct atlas_data *data, bool state)
305{
306 return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL,
307 ATLAS_REG_INT_CONTROL_EN,
308 state ? ATLAS_REG_INT_CONTROL_EN : 0);
309}
310
311static int atlas_buffer_postenable(struct iio_dev *indio_dev)
312{
313 struct atlas_data *data = iio_priv(indio_dev);
314 int ret;
315
316 ret = iio_triggered_buffer_postenable(indio_dev);
317 if (ret)
318 return ret;
319
320 ret = pm_runtime_get_sync(&data->client->dev);
321 if (ret < 0) {
322 pm_runtime_put_noidle(&data->client->dev);
323 return ret;
324 }
325
326 return atlas_set_interrupt(data, true);
327}
328
329static int atlas_buffer_predisable(struct iio_dev *indio_dev)
330{
331 struct atlas_data *data = iio_priv(indio_dev);
332 int ret;
333
334 ret = iio_triggered_buffer_predisable(indio_dev);
335 if (ret)
336 return ret;
337
338 ret = atlas_set_interrupt(data, false);
339 if (ret)
340 return ret;
341
342 pm_runtime_mark_last_busy(&data->client->dev);
343 return pm_runtime_put_autosuspend(&data->client->dev);
344}
345
346static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
27dec00e
MR
347};
348
349static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
350 .postenable = atlas_buffer_postenable,
351 .predisable = atlas_buffer_predisable,
352};
353
354static void atlas_work_handler(struct irq_work *work)
355{
356 struct atlas_data *data = container_of(work, struct atlas_data, work);
357
358 iio_trigger_poll(data->trig);
359}
360
361static irqreturn_t atlas_trigger_handler(int irq, void *private)
362{
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;
367
7103b99b
MR
368 ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
369 (u8 *) &data->buffer,
370 sizeof(__be32) * (data->chip->num_channels - 2));
27dec00e 371
98e55e93 372 if (!ret)
27dec00e 373 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
bc2b7dab 374 iio_get_time_ns(indio_dev));
27dec00e
MR
375
376 iio_trigger_notify_done(indio_dev->trig);
377
378 return IRQ_HANDLED;
379}
380
381static irqreturn_t atlas_interrupt_handler(int irq, void *private)
382{
383 struct iio_dev *indio_dev = private;
384 struct atlas_data *data = iio_priv(indio_dev);
385
386 irq_work_queue(&data->work);
387
388 return IRQ_HANDLED;
389}
390
7103b99b 391static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
27dec00e
MR
392{
393 struct device *dev = &data->client->dev;
394 int suspended = pm_runtime_suspended(dev);
395 int ret;
396
397 ret = pm_runtime_get_sync(dev);
398 if (ret < 0) {
399 pm_runtime_put_noidle(dev);
400 return ret;
401 }
402
403 if (suspended)
7103b99b 404 usleep_range(data->chip->delay, data->chip->delay + 100000);
27dec00e 405
7103b99b 406 ret = regmap_bulk_read(data->regmap, reg, (u8 *) val, sizeof(*val));
27dec00e
MR
407
408 pm_runtime_mark_last_busy(dev);
409 pm_runtime_put_autosuspend(dev);
410
411 return ret;
412}
413
414static int atlas_read_raw(struct iio_dev *indio_dev,
415 struct iio_chan_spec const *chan,
416 int *val, int *val2, long mask)
417{
418 struct atlas_data *data = iio_priv(indio_dev);
419
420 switch (mask) {
421 case IIO_CHAN_INFO_RAW: {
422 int ret;
423 __be32 reg;
424
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:
e8dd92bf
MR
431 case IIO_CONCENTRATION:
432 case IIO_ELECTRICALCONDUCTIVITY:
ce08cc98 433 case IIO_VOLTAGE:
b015b3e3
MR
434 ret = iio_device_claim_direct_mode(indio_dev);
435 if (ret)
436 return ret;
27dec00e 437
b015b3e3 438 ret = atlas_read_measurement(data, chan->address, &reg);
27dec00e 439
b015b3e3 440 iio_device_release_direct_mode(indio_dev);
27dec00e
MR
441 break;
442 default:
443 ret = -EINVAL;
444 }
445
446 if (!ret) {
447 *val = be32_to_cpu(reg);
448 ret = IIO_VAL_INT;
449 }
450 return ret;
451 }
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;
e8dd92bf
MR
462 case IIO_ELECTRICALCONDUCTIVITY:
463 *val = 1; /* 0.00001 */
ca64d4bc 464 *val2 = 100000;
e8dd92bf
MR
465 break;
466 case IIO_CONCENTRATION:
467 *val = 0; /* 0.000000001 */
468 *val2 = 1000;
469 return IIO_VAL_INT_PLUS_NANO;
ce08cc98
MR
470 case IIO_VOLTAGE:
471 *val = 1; /* 0.1 */
472 *val2 = 10;
473 break;
27dec00e
MR
474 default:
475 return -EINVAL;
476 }
477 return IIO_VAL_FRACTIONAL;
478 }
479
480 return -EINVAL;
481}
482
483static int atlas_write_raw(struct iio_dev *indio_dev,
484 struct iio_chan_spec const *chan,
485 int val, int val2, long mask)
486{
487 struct atlas_data *data = iio_priv(indio_dev);
488 __be32 reg = cpu_to_be32(val);
489
490 if (val2 != 0 || val < 0 || val > 20000)
491 return -EINVAL;
492
493 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
494 return -EINVAL;
495
496 return regmap_bulk_write(data->regmap, chan->address,
497 &reg, sizeof(reg));
498}
499
500static const struct iio_info atlas_info = {
27dec00e
MR
501 .read_raw = atlas_read_raw,
502 .write_raw = atlas_write_raw,
503};
504
7103b99b
MR
505static const struct i2c_device_id atlas_id[] = {
506 { "atlas-ph-sm", ATLAS_PH_SM},
e8dd92bf 507 { "atlas-ec-sm", ATLAS_EC_SM},
ce08cc98 508 { "atlas-orp-sm", ATLAS_ORP_SM},
7103b99b
MR
509 {}
510};
511MODULE_DEVICE_TABLE(i2c, atlas_id);
27dec00e 512
7103b99b
MR
513static const struct of_device_id atlas_dt_ids[] = {
514 { .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, },
e8dd92bf 515 { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, },
ce08cc98 516 { .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, },
7103b99b 517 { }
27dec00e 518};
7103b99b 519MODULE_DEVICE_TABLE(of, atlas_dt_ids);
27dec00e
MR
520
521static int atlas_probe(struct i2c_client *client,
522 const struct i2c_device_id *id)
523{
524 struct atlas_data *data;
7103b99b
MR
525 struct atlas_device *chip;
526 const struct of_device_id *of_id;
27dec00e
MR
527 struct iio_trigger *trig;
528 struct iio_dev *indio_dev;
529 int ret;
530
531 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
532 if (!indio_dev)
533 return -ENOMEM;
534
7103b99b
MR
535 of_id = of_match_device(atlas_dt_ids, &client->dev);
536 if (!of_id)
537 chip = &atlas_devices[id->driver_data];
538 else
539 chip = &atlas_devices[(unsigned long)of_id->data];
540
27dec00e
MR
541 indio_dev->info = &atlas_info;
542 indio_dev->name = ATLAS_DRV_NAME;
7103b99b
MR
543 indio_dev->channels = chip->channels;
544 indio_dev->num_channels = chip->num_channels;
27dec00e
MR
545 indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
546 indio_dev->dev.parent = &client->dev;
547
548 trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
549 indio_dev->name, indio_dev->id);
550
551 if (!trig)
552 return -ENOMEM;
553
554 data = iio_priv(indio_dev);
555 data->client = client;
556 data->trig = trig;
7103b99b 557 data->chip = chip;
27dec00e
MR
558 trig->dev.parent = indio_dev->dev.parent;
559 trig->ops = &atlas_interrupt_trigger_ops;
560 iio_trigger_set_drvdata(trig, indio_dev);
561
562 i2c_set_clientdata(client, indio_dev);
563
564 data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config);
565 if (IS_ERR(data->regmap)) {
566 dev_err(&client->dev, "regmap initialization failed\n");
567 return PTR_ERR(data->regmap);
568 }
569
570 ret = pm_runtime_set_active(&client->dev);
571 if (ret)
572 return ret;
573
574 if (client->irq <= 0) {
575 dev_err(&client->dev, "no valid irq defined\n");
576 return -EINVAL;
577 }
578
7103b99b 579 ret = chip->calibration(data);
27dec00e
MR
580 if (ret)
581 return ret;
582
583 ret = iio_trigger_register(trig);
584 if (ret) {
585 dev_err(&client->dev, "failed to register trigger\n");
586 return ret;
587 }
588
589 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
590 &atlas_trigger_handler, &atlas_buffer_setup_ops);
591 if (ret) {
592 dev_err(&client->dev, "cannot setup iio trigger\n");
593 goto unregister_trigger;
594 }
595
596 init_irq_work(&data->work, atlas_work_handler);
597
598 /* interrupt pin toggles on new conversion */
599 ret = devm_request_threaded_irq(&client->dev, client->irq,
600 NULL, atlas_interrupt_handler,
601 IRQF_TRIGGER_RISING |
602 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
603 "atlas_irq",
604 indio_dev);
605 if (ret) {
606 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
607 goto unregister_buffer;
608 }
609
610 ret = atlas_set_powermode(data, 1);
611 if (ret) {
612 dev_err(&client->dev, "cannot power device on");
613 goto unregister_buffer;
614 }
615
616 pm_runtime_enable(&client->dev);
617 pm_runtime_set_autosuspend_delay(&client->dev, 2500);
618 pm_runtime_use_autosuspend(&client->dev);
619
620 ret = iio_device_register(indio_dev);
621 if (ret) {
622 dev_err(&client->dev, "unable to register device\n");
623 goto unregister_pm;
624 }
625
626 return 0;
627
628unregister_pm:
629 pm_runtime_disable(&client->dev);
630 atlas_set_powermode(data, 0);
631
632unregister_buffer:
633 iio_triggered_buffer_cleanup(indio_dev);
634
635unregister_trigger:
636 iio_trigger_unregister(data->trig);
637
638 return ret;
639}
640
641static int atlas_remove(struct i2c_client *client)
642{
643 struct iio_dev *indio_dev = i2c_get_clientdata(client);
644 struct atlas_data *data = iio_priv(indio_dev);
645
646 iio_device_unregister(indio_dev);
647 iio_triggered_buffer_cleanup(indio_dev);
648 iio_trigger_unregister(data->trig);
649
650 pm_runtime_disable(&client->dev);
651 pm_runtime_set_suspended(&client->dev);
652 pm_runtime_put_noidle(&client->dev);
653
654 return atlas_set_powermode(data, 0);
655}
656
657#ifdef CONFIG_PM
658static int atlas_runtime_suspend(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, 0);
664}
665
666static int atlas_runtime_resume(struct device *dev)
667{
668 struct atlas_data *data =
669 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
670
671 return atlas_set_powermode(data, 1);
672}
673#endif
674
675static const struct dev_pm_ops atlas_pm_ops = {
676 SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
677 atlas_runtime_resume, NULL)
678};
679
27dec00e
MR
680static struct i2c_driver atlas_driver = {
681 .driver = {
682 .name = ATLAS_DRV_NAME,
683 .of_match_table = of_match_ptr(atlas_dt_ids),
684 .pm = &atlas_pm_ops,
685 },
686 .probe = atlas_probe,
687 .remove = atlas_remove,
688 .id_table = atlas_id,
689};
690module_i2c_driver(atlas_driver);
691
692MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
693MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
694MODULE_LICENSE("GPL");