1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor
4  *
5  * Copyright (C) 2015-2018 Matt Ranostay
6  * Author: Matt Ranostay <matt.ranostay@konsulko.com>
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 
68 enum {
69 	ATLAS_PH_SM,
70 	ATLAS_EC_SM,
71 	ATLAS_ORP_SM,
72 };
73 
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;
80 
81 	__be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
82 };
83 
84 static const struct regmap_config atlas_regmap_config = {
85 	.name = ATLAS_REGMAP_NAME,
86 	.reg_bits = 8,
87 	.val_bits = 8,
88 };
89 
90 static 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 
132 static 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 
159 static 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 
atlas_check_ph_calibration(struct atlas_data * data)176 static 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 
atlas_check_ec_calibration(struct atlas_data * data)203 static 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 
atlas_check_orp_calibration(struct atlas_data * data)242 static 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 
258 struct 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 
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,
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 
atlas_set_powermode(struct atlas_data * data,int on)291 static int atlas_set_powermode(struct atlas_data *data, int on)
292 {
293 	return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
294 }
295 
atlas_set_interrupt(struct atlas_data * data,bool state)296 static 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 
atlas_buffer_postenable(struct iio_dev * indio_dev)303 static 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 
atlas_buffer_predisable(struct iio_dev * indio_dev)321 static 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 
338 static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
339 };
340 
341 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
342 	.postenable = atlas_buffer_postenable,
343 	.predisable = atlas_buffer_predisable,
344 };
345 
atlas_work_handler(struct irq_work * work)346 static 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 
atlas_trigger_handler(int irq,void * private)353 static 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 
atlas_interrupt_handler(int irq,void * private)373 static 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 
atlas_read_measurement(struct atlas_data * data,int reg,__be32 * val)383 static 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 
atlas_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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)
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 *) &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;
429 
430 			ret = atlas_read_measurement(data, chan->address, &reg);
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 = 10;
448 			return IIO_VAL_INT;
449 		case IIO_PH:
450 			*val = 1; /* 0.001 */
451 			*val2 = 1000;
452 			break;
453 		case IIO_ELECTRICALCONDUCTIVITY:
454 			*val = 1; /* 0.00001 */
455 			*val2 = 100000;
456 			break;
457 		case IIO_CONCENTRATION:
458 			*val = 0; /* 0.000000001 */
459 			*val2 = 1000;
460 			return IIO_VAL_INT_PLUS_NANO;
461 		case IIO_VOLTAGE:
462 			*val = 1; /* 0.1 */
463 			*val2 = 10;
464 			break;
465 		default:
466 			return -EINVAL;
467 		}
468 		return IIO_VAL_FRACTIONAL;
469 	}
470 
471 	return -EINVAL;
472 }
473 
atlas_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)474 static int atlas_write_raw(struct iio_dev *indio_dev,
475 			   struct iio_chan_spec const *chan,
476 			   int val, int val2, long mask)
477 {
478 	struct atlas_data *data = iio_priv(indio_dev);
479 	__be32 reg = cpu_to_be32(val / 10);
480 
481 	if (val2 != 0 || val < 0 || val > 20000)
482 		return -EINVAL;
483 
484 	if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
485 		return -EINVAL;
486 
487 	return regmap_bulk_write(data->regmap, chan->address,
488 				 &reg, sizeof(reg));
489 }
490 
491 static const struct iio_info atlas_info = {
492 	.read_raw = atlas_read_raw,
493 	.write_raw = atlas_write_raw,
494 };
495 
496 static const struct i2c_device_id atlas_id[] = {
497 	{ "atlas-ph-sm", ATLAS_PH_SM},
498 	{ "atlas-ec-sm", ATLAS_EC_SM},
499 	{ "atlas-orp-sm", ATLAS_ORP_SM},
500 	{}
501 };
502 MODULE_DEVICE_TABLE(i2c, atlas_id);
503 
504 static const struct of_device_id atlas_dt_ids[] = {
505 	{ .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, },
506 	{ .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, },
507 	{ .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, },
508 	{ }
509 };
510 MODULE_DEVICE_TABLE(of, atlas_dt_ids);
511 
atlas_probe(struct i2c_client * client,const struct i2c_device_id * id)512 static int atlas_probe(struct i2c_client *client,
513 		       const struct i2c_device_id *id)
514 {
515 	struct atlas_data *data;
516 	struct atlas_device *chip;
517 	const struct of_device_id *of_id;
518 	struct iio_trigger *trig;
519 	struct iio_dev *indio_dev;
520 	int ret;
521 
522 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
523 	if (!indio_dev)
524 		return -ENOMEM;
525 
526 	of_id = of_match_device(atlas_dt_ids, &client->dev);
527 	if (!of_id)
528 		chip = &atlas_devices[id->driver_data];
529 	else
530 		chip = &atlas_devices[(unsigned long)of_id->data];
531 
532 	indio_dev->info = &atlas_info;
533 	indio_dev->name = ATLAS_DRV_NAME;
534 	indio_dev->channels = chip->channels;
535 	indio_dev->num_channels = chip->num_channels;
536 	indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
537 	indio_dev->dev.parent = &client->dev;
538 
539 	trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
540 				      indio_dev->name, indio_dev->id);
541 
542 	if (!trig)
543 		return -ENOMEM;
544 
545 	data = iio_priv(indio_dev);
546 	data->client = client;
547 	data->trig = trig;
548 	data->chip = chip;
549 	trig->dev.parent = indio_dev->dev.parent;
550 	trig->ops = &atlas_interrupt_trigger_ops;
551 	iio_trigger_set_drvdata(trig, indio_dev);
552 
553 	i2c_set_clientdata(client, indio_dev);
554 
555 	data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config);
556 	if (IS_ERR(data->regmap)) {
557 		dev_err(&client->dev, "regmap initialization failed\n");
558 		return PTR_ERR(data->regmap);
559 	}
560 
561 	ret = pm_runtime_set_active(&client->dev);
562 	if (ret)
563 		return ret;
564 
565 	if (client->irq <= 0) {
566 		dev_err(&client->dev, "no valid irq defined\n");
567 		return -EINVAL;
568 	}
569 
570 	ret = chip->calibration(data);
571 	if (ret)
572 		return ret;
573 
574 	ret = iio_trigger_register(trig);
575 	if (ret) {
576 		dev_err(&client->dev, "failed to register trigger\n");
577 		return ret;
578 	}
579 
580 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
581 		&atlas_trigger_handler, &atlas_buffer_setup_ops);
582 	if (ret) {
583 		dev_err(&client->dev, "cannot setup iio trigger\n");
584 		goto unregister_trigger;
585 	}
586 
587 	init_irq_work(&data->work, atlas_work_handler);
588 
589 	/* interrupt pin toggles on new conversion */
590 	ret = devm_request_threaded_irq(&client->dev, client->irq,
591 					NULL, atlas_interrupt_handler,
592 					IRQF_TRIGGER_RISING |
593 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
594 					"atlas_irq",
595 					indio_dev);
596 	if (ret) {
597 		dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
598 		goto unregister_buffer;
599 	}
600 
601 	ret = atlas_set_powermode(data, 1);
602 	if (ret) {
603 		dev_err(&client->dev, "cannot power device on");
604 		goto unregister_buffer;
605 	}
606 
607 	pm_runtime_enable(&client->dev);
608 	pm_runtime_set_autosuspend_delay(&client->dev, 2500);
609 	pm_runtime_use_autosuspend(&client->dev);
610 
611 	ret = iio_device_register(indio_dev);
612 	if (ret) {
613 		dev_err(&client->dev, "unable to register device\n");
614 		goto unregister_pm;
615 	}
616 
617 	return 0;
618 
619 unregister_pm:
620 	pm_runtime_disable(&client->dev);
621 	atlas_set_powermode(data, 0);
622 
623 unregister_buffer:
624 	iio_triggered_buffer_cleanup(indio_dev);
625 
626 unregister_trigger:
627 	iio_trigger_unregister(data->trig);
628 
629 	return ret;
630 }
631 
atlas_remove(struct i2c_client * client)632 static int atlas_remove(struct i2c_client *client)
633 {
634 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
635 	struct atlas_data *data = iio_priv(indio_dev);
636 
637 	iio_device_unregister(indio_dev);
638 	iio_triggered_buffer_cleanup(indio_dev);
639 	iio_trigger_unregister(data->trig);
640 
641 	pm_runtime_disable(&client->dev);
642 	pm_runtime_set_suspended(&client->dev);
643 	pm_runtime_put_noidle(&client->dev);
644 
645 	return atlas_set_powermode(data, 0);
646 }
647 
648 #ifdef CONFIG_PM
atlas_runtime_suspend(struct device * dev)649 static int atlas_runtime_suspend(struct device *dev)
650 {
651 	struct atlas_data *data =
652 		     iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
653 
654 	return atlas_set_powermode(data, 0);
655 }
656 
atlas_runtime_resume(struct device * dev)657 static int atlas_runtime_resume(struct device *dev)
658 {
659 	struct atlas_data *data =
660 		     iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
661 
662 	return atlas_set_powermode(data, 1);
663 }
664 #endif
665 
666 static const struct dev_pm_ops atlas_pm_ops = {
667 	SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
668 			   atlas_runtime_resume, NULL)
669 };
670 
671 static struct i2c_driver atlas_driver = {
672 	.driver = {
673 		.name	= ATLAS_DRV_NAME,
674 		.of_match_table	= of_match_ptr(atlas_dt_ids),
675 		.pm	= &atlas_pm_ops,
676 	},
677 	.probe		= atlas_probe,
678 	.remove		= atlas_remove,
679 	.id_table	= atlas_id,
680 };
681 module_i2c_driver(atlas_driver);
682 
683 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
684 MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
685 MODULE_LICENSE("GPL");
686