1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * atlas-sensor.c - Support for Atlas Scientific OEM SM sensors
4  *
5  * Copyright (C) 2015-2019 Konsulko Group
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/i2c.h>
18 #include <linux/of_device.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>
26 
27 #define ATLAS_REGMAP_NAME	"atlas_regmap"
28 #define ATLAS_DRV_NAME		"atlas"
29 
30 #define ATLAS_REG_DEV_TYPE		0x00
31 #define ATLAS_REG_DEV_VERSION		0x01
32 
33 #define ATLAS_REG_INT_CONTROL		0x04
34 #define ATLAS_REG_INT_CONTROL_EN	BIT(3)
35 
36 #define ATLAS_REG_PWR_CONTROL		0x06
37 
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)
43 
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)
50 
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)
55 
56 #define ATLAS_REG_RTD_DATA		0x0e
57 
58 #define ATLAS_REG_PH_TEMP_DATA		0x0e
59 #define ATLAS_REG_PH_DATA		0x16
60 
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
66 
67 #define ATLAS_REG_ORP_CALIB_STATUS	0x0d
68 #define ATLAS_REG_ORP_DATA		0x0e
69 
70 #define ATLAS_REG_DO_TEMP_DATA		0x12
71 #define ATLAS_REG_DO_DATA		0x22
72 
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
78 
79 enum {
80 	ATLAS_PH_SM,
81 	ATLAS_EC_SM,
82 	ATLAS_ORP_SM,
83 	ATLAS_DO_SM,
84 	ATLAS_RTD_SM,
85 };
86 
87 struct atlas_data {
88 	struct i2c_client *client;
89 	struct iio_trigger *trig;
90 	struct atlas_device *chip;
91 	struct regmap *regmap;
92 	struct irq_work work;
93 	unsigned int interrupt_enabled;
94 
95 	__be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
96 };
97 
98 static const struct regmap_config atlas_regmap_config = {
99 	.name = ATLAS_REGMAP_NAME,
100 	.reg_bits = 8,
101 	.val_bits = 8,
102 };
103 
104 static int atlas_buffer_num_channels(const struct iio_chan_spec *spec)
105 {
106 	int idx = 0;
107 
108 	for (; spec->type != IIO_TIMESTAMP; spec++)
109 		idx++;
110 
111 	return idx;
112 };
113 
114 static const struct iio_chan_spec atlas_ph_channels[] = {
115 	{
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,
126 		},
127 	},
128 	IIO_CHAN_SOFT_TIMESTAMP(1),
129 	{
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
136 	},
137 };
138 
139 #define ATLAS_CONCENTRATION_CHANNEL(_idx, _addr) \
140 	{\
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 		}, \
154 	}
155 
156 static const struct iio_chan_spec atlas_ec_channels[] = {
157 	{
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,
168 		},
169 	},
170 	ATLAS_CONCENTRATION_CHANNEL(0, ATLAS_REG_TDS_DATA),
171 	ATLAS_CONCENTRATION_CHANNEL(1, ATLAS_REG_PSS_DATA),
172 	IIO_CHAN_SOFT_TIMESTAMP(3),
173 	{
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
180 	},
181 };
182 
183 static const struct iio_chan_spec atlas_orp_channels[] = {
184 	{
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,
195 		},
196 	},
197 	IIO_CHAN_SOFT_TIMESTAMP(1),
198 };
199 
200 static const struct iio_chan_spec atlas_do_channels[] = {
201 	{
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,
212 		},
213 	},
214 	IIO_CHAN_SOFT_TIMESTAMP(1),
215 	{
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
222 	},
223 };
224 
225 static const struct iio_chan_spec atlas_rtd_channels[] = {
226 	{
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,
236 		},
237 	},
238 	IIO_CHAN_SOFT_TIMESTAMP(1),
239 };
240 
241 static int atlas_check_ph_calibration(struct atlas_data *data)
242 {
243 	struct device *dev = &data->client->dev;
244 	int ret;
245 	unsigned int val;
246 
247 	ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val);
248 	if (ret)
249 		return ret;
250 
251 	if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) {
252 		dev_warn(dev, "device has not been calibrated\n");
253 		return 0;
254 	}
255 
256 	if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW))
257 		dev_warn(dev, "device missing low point calibration\n");
258 
259 	if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID))
260 		dev_warn(dev, "device missing mid point calibration\n");
261 
262 	if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH))
263 		dev_warn(dev, "device missing high point calibration\n");
264 
265 	return 0;
266 }
267 
268 static int atlas_check_ec_calibration(struct atlas_data *data)
269 {
270 	struct device *dev = &data->client->dev;
271 	int ret;
272 	unsigned int val;
273 	__be16	rval;
274 
275 	ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2);
276 	if (ret)
277 		return ret;
278 
279 	val = be16_to_cpu(rval);
280 	dev_info(dev, "probe set to K = %d.%.2d", val / 100, val % 100);
281 
282 	ret = regmap_read(data->regmap, ATLAS_REG_EC_CALIB_STATUS, &val);
283 	if (ret)
284 		return ret;
285 
286 	if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) {
287 		dev_warn(dev, "device has not been calibrated\n");
288 		return 0;
289 	}
290 
291 	if (!(val & ATLAS_REG_EC_CALIB_STATUS_DRY))
292 		dev_warn(dev, "device missing dry point calibration\n");
293 
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");
299 
300 		if (!(val & ATLAS_REG_EC_CALIB_STATUS_HIGH))
301 			dev_warn(dev, "device missing high point calibration\n");
302 	}
303 
304 	return 0;
305 }
306 
307 static int atlas_check_orp_calibration(struct atlas_data *data)
308 {
309 	struct device *dev = &data->client->dev;
310 	int ret;
311 	unsigned int val;
312 
313 	ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val);
314 	if (ret)
315 		return ret;
316 
317 	if (!val)
318 		dev_warn(dev, "device has not been calibrated\n");
319 
320 	return 0;
321 }
322 
323 static int atlas_check_do_calibration(struct atlas_data *data)
324 {
325 	struct device *dev = &data->client->dev;
326 	int ret;
327 	unsigned int val;
328 
329 	ret = regmap_read(data->regmap, ATLAS_REG_DO_CALIB_STATUS, &val);
330 	if (ret)
331 		return ret;
332 
333 	if (!(val & ATLAS_REG_DO_CALIB_STATUS_MASK)) {
334 		dev_warn(dev, "device has not been calibrated\n");
335 		return 0;
336 	}
337 
338 	if (!(val & ATLAS_REG_DO_CALIB_STATUS_PRESSURE))
339 		dev_warn(dev, "device missing atmospheric pressure calibration\n");
340 
341 	if (!(val & ATLAS_REG_DO_CALIB_STATUS_DO))
342 		dev_warn(dev, "device missing dissolved oxygen calibration\n");
343 
344 	return 0;
345 }
346 
347 struct atlas_device {
348 	const struct iio_chan_spec *channels;
349 	int num_channels;
350 	int data_reg;
351 
352 	int (*calibration)(struct atlas_data *data);
353 	int delay;
354 };
355 
356 static 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,
363 	},
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,
370 	},
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,
377 	},
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,
384 	},
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,
390 	},
391 };
392 
393 static int atlas_set_powermode(struct atlas_data *data, int on)
394 {
395 	return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
396 }
397 
398 static int atlas_set_interrupt(struct atlas_data *data, bool state)
399 {
400 	if (!data->interrupt_enabled)
401 		return 0;
402 
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);
406 }
407 
408 static int atlas_buffer_postenable(struct iio_dev *indio_dev)
409 {
410 	struct atlas_data *data = iio_priv(indio_dev);
411 	int ret;
412 
413 	ret = iio_triggered_buffer_postenable(indio_dev);
414 	if (ret)
415 		return ret;
416 
417 	ret = pm_runtime_get_sync(&data->client->dev);
418 	if (ret < 0) {
419 		pm_runtime_put_noidle(&data->client->dev);
420 		return ret;
421 	}
422 
423 	return atlas_set_interrupt(data, true);
424 }
425 
426 static int atlas_buffer_predisable(struct iio_dev *indio_dev)
427 {
428 	struct atlas_data *data = iio_priv(indio_dev);
429 	int ret;
430 
431 	ret = atlas_set_interrupt(data, false);
432 	if (ret)
433 		return ret;
434 
435 	pm_runtime_mark_last_busy(&data->client->dev);
436 	ret = pm_runtime_put_autosuspend(&data->client->dev);
437 	if (ret)
438 		return ret;
439 
440 	return iio_triggered_buffer_predisable(indio_dev);
441 }
442 
443 static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
444 };
445 
446 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
447 	.postenable = atlas_buffer_postenable,
448 	.predisable = atlas_buffer_predisable,
449 };
450 
451 static void atlas_work_handler(struct irq_work *work)
452 {
453 	struct atlas_data *data = container_of(work, struct atlas_data, work);
454 
455 	iio_trigger_poll(data->trig);
456 }
457 
458 static irqreturn_t atlas_trigger_handler(int irq, void *private)
459 {
460 	struct iio_poll_func *pf = private;
461 	struct iio_dev *indio_dev = pf->indio_dev;
462 	struct atlas_data *data = iio_priv(indio_dev);
463 	int channels = atlas_buffer_num_channels(data->chip->channels);
464 	int ret;
465 
466 	ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
467 			      &data->buffer, sizeof(__be32) * channels);
468 
469 	if (!ret)
470 		iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
471 				iio_get_time_ns(indio_dev));
472 
473 	iio_trigger_notify_done(indio_dev->trig);
474 
475 	return IRQ_HANDLED;
476 }
477 
478 static irqreturn_t atlas_interrupt_handler(int irq, void *private)
479 {
480 	struct iio_dev *indio_dev = private;
481 	struct atlas_data *data = iio_priv(indio_dev);
482 
483 	irq_work_queue(&data->work);
484 
485 	return IRQ_HANDLED;
486 }
487 
488 static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
489 {
490 	struct device *dev = &data->client->dev;
491 	int suspended = pm_runtime_suspended(dev);
492 	int ret;
493 
494 	ret = pm_runtime_get_sync(dev);
495 	if (ret < 0) {
496 		pm_runtime_put_noidle(dev);
497 		return ret;
498 	}
499 
500 	if (suspended)
501 		msleep(data->chip->delay);
502 
503 	ret = regmap_bulk_read(data->regmap, reg, val, sizeof(*val));
504 
505 	pm_runtime_mark_last_busy(dev);
506 	pm_runtime_put_autosuspend(dev);
507 
508 	return ret;
509 }
510 
511 static int atlas_read_raw(struct iio_dev *indio_dev,
512 			  struct iio_chan_spec const *chan,
513 			  int *val, int *val2, long mask)
514 {
515 	struct atlas_data *data = iio_priv(indio_dev);
516 
517 	switch (mask) {
518 	case IIO_CHAN_INFO_PROCESSED:
519 	case IIO_CHAN_INFO_RAW: {
520 		int ret;
521 		__be32 reg;
522 
523 		switch (chan->type) {
524 		case IIO_TEMP:
525 			ret = regmap_bulk_read(data->regmap, chan->address,
526 					       &reg, sizeof(reg));
527 			break;
528 		case IIO_PH:
529 		case IIO_CONCENTRATION:
530 		case IIO_ELECTRICALCONDUCTIVITY:
531 		case IIO_VOLTAGE:
532 			ret = iio_device_claim_direct_mode(indio_dev);
533 			if (ret)
534 				return ret;
535 
536 			ret = atlas_read_measurement(data, chan->address, &reg);
537 
538 			iio_device_release_direct_mode(indio_dev);
539 			break;
540 		default:
541 			ret = -EINVAL;
542 		}
543 
544 		if (!ret) {
545 			*val = be32_to_cpu(reg);
546 			ret = IIO_VAL_INT;
547 		}
548 		return ret;
549 	}
550 	case IIO_CHAN_INFO_SCALE:
551 		switch (chan->type) {
552 		case IIO_TEMP:
553 			*val = 10;
554 			return IIO_VAL_INT;
555 		case IIO_PH:
556 			*val = 1; /* 0.001 */
557 			*val2 = 1000;
558 			break;
559 		case IIO_ELECTRICALCONDUCTIVITY:
560 			*val = 1; /* 0.00001 */
561 			*val2 = 100000;
562 			break;
563 		case IIO_CONCENTRATION:
564 			*val = 0; /* 0.000000001 */
565 			*val2 = 1000;
566 			return IIO_VAL_INT_PLUS_NANO;
567 		case IIO_VOLTAGE:
568 			*val = 1; /* 0.1 */
569 			*val2 = 10;
570 			break;
571 		default:
572 			return -EINVAL;
573 		}
574 		return IIO_VAL_FRACTIONAL;
575 	}
576 
577 	return -EINVAL;
578 }
579 
580 static int atlas_write_raw(struct iio_dev *indio_dev,
581 			   struct iio_chan_spec const *chan,
582 			   int val, int val2, long mask)
583 {
584 	struct atlas_data *data = iio_priv(indio_dev);
585 	__be32 reg = cpu_to_be32(val / 10);
586 
587 	if (val2 != 0 || val < 0 || val > 20000)
588 		return -EINVAL;
589 
590 	if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
591 		return -EINVAL;
592 
593 	return regmap_bulk_write(data->regmap, chan->address,
594 				 &reg, sizeof(reg));
595 }
596 
597 static const struct iio_info atlas_info = {
598 	.read_raw = atlas_read_raw,
599 	.write_raw = atlas_write_raw,
600 };
601 
602 static const struct i2c_device_id atlas_id[] = {
603 	{ "atlas-ph-sm", ATLAS_PH_SM},
604 	{ "atlas-ec-sm", ATLAS_EC_SM},
605 	{ "atlas-orp-sm", ATLAS_ORP_SM},
606 	{ "atlas-do-sm", ATLAS_DO_SM},
607 	{ "atlas-rtd-sm", ATLAS_RTD_SM},
608 	{}
609 };
610 MODULE_DEVICE_TABLE(i2c, atlas_id);
611 
612 static const struct of_device_id atlas_dt_ids[] = {
613 	{ .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, },
614 	{ .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, },
615 	{ .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, },
616 	{ .compatible = "atlas,do-sm", .data = (void *)ATLAS_DO_SM, },
617 	{ .compatible = "atlas,rtd-sm", .data = (void *)ATLAS_RTD_SM, },
618 	{ }
619 };
620 MODULE_DEVICE_TABLE(of, atlas_dt_ids);
621 
622 static int atlas_probe(struct i2c_client *client,
623 		       const struct i2c_device_id *id)
624 {
625 	struct atlas_data *data;
626 	struct atlas_device *chip;
627 	const struct of_device_id *of_id;
628 	struct iio_trigger *trig;
629 	struct iio_dev *indio_dev;
630 	int ret;
631 
632 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
633 	if (!indio_dev)
634 		return -ENOMEM;
635 
636 	of_id = of_match_device(atlas_dt_ids, &client->dev);
637 	if (!of_id)
638 		chip = &atlas_devices[id->driver_data];
639 	else
640 		chip = &atlas_devices[(unsigned long)of_id->data];
641 
642 	indio_dev->info = &atlas_info;
643 	indio_dev->name = ATLAS_DRV_NAME;
644 	indio_dev->channels = chip->channels;
645 	indio_dev->num_channels = chip->num_channels;
646 	indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
647 	indio_dev->dev.parent = &client->dev;
648 
649 	trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
650 				      indio_dev->name, indio_dev->id);
651 
652 	if (!trig)
653 		return -ENOMEM;
654 
655 	data = iio_priv(indio_dev);
656 	data->client = client;
657 	data->trig = trig;
658 	data->chip = chip;
659 	trig->dev.parent = indio_dev->dev.parent;
660 	trig->ops = &atlas_interrupt_trigger_ops;
661 	iio_trigger_set_drvdata(trig, indio_dev);
662 
663 	i2c_set_clientdata(client, indio_dev);
664 
665 	data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config);
666 	if (IS_ERR(data->regmap)) {
667 		dev_err(&client->dev, "regmap initialization failed\n");
668 		return PTR_ERR(data->regmap);
669 	}
670 
671 	ret = pm_runtime_set_active(&client->dev);
672 	if (ret)
673 		return ret;
674 
675 	ret = chip->calibration(data);
676 	if (ret)
677 		return ret;
678 
679 	ret = iio_trigger_register(trig);
680 	if (ret) {
681 		dev_err(&client->dev, "failed to register trigger\n");
682 		return ret;
683 	}
684 
685 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
686 		&atlas_trigger_handler, &atlas_buffer_setup_ops);
687 	if (ret) {
688 		dev_err(&client->dev, "cannot setup iio trigger\n");
689 		goto unregister_trigger;
690 	}
691 
692 	init_irq_work(&data->work, atlas_work_handler);
693 
694 	if (client->irq > 0) {
695 		/* interrupt pin toggles on new conversion */
696 		ret = devm_request_threaded_irq(&client->dev, client->irq,
697 				NULL, atlas_interrupt_handler,
698 				IRQF_TRIGGER_RISING |
699 				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
700 				"atlas_irq",
701 				indio_dev);
702 
703 		if (ret)
704 			dev_warn(&client->dev,
705 				"request irq (%d) failed\n", client->irq);
706 		else
707 			data->interrupt_enabled = 1;
708 	}
709 
710 	ret = atlas_set_powermode(data, 1);
711 	if (ret) {
712 		dev_err(&client->dev, "cannot power device on");
713 		goto unregister_buffer;
714 	}
715 
716 	pm_runtime_enable(&client->dev);
717 	pm_runtime_set_autosuspend_delay(&client->dev, 2500);
718 	pm_runtime_use_autosuspend(&client->dev);
719 
720 	ret = iio_device_register(indio_dev);
721 	if (ret) {
722 		dev_err(&client->dev, "unable to register device\n");
723 		goto unregister_pm;
724 	}
725 
726 	return 0;
727 
728 unregister_pm:
729 	pm_runtime_disable(&client->dev);
730 	atlas_set_powermode(data, 0);
731 
732 unregister_buffer:
733 	iio_triggered_buffer_cleanup(indio_dev);
734 
735 unregister_trigger:
736 	iio_trigger_unregister(data->trig);
737 
738 	return ret;
739 }
740 
741 static int atlas_remove(struct i2c_client *client)
742 {
743 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
744 	struct atlas_data *data = iio_priv(indio_dev);
745 
746 	iio_device_unregister(indio_dev);
747 	iio_triggered_buffer_cleanup(indio_dev);
748 	iio_trigger_unregister(data->trig);
749 
750 	pm_runtime_disable(&client->dev);
751 	pm_runtime_set_suspended(&client->dev);
752 	pm_runtime_put_noidle(&client->dev);
753 
754 	return atlas_set_powermode(data, 0);
755 }
756 
757 #ifdef CONFIG_PM
758 static int atlas_runtime_suspend(struct device *dev)
759 {
760 	struct atlas_data *data =
761 		     iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
762 
763 	return atlas_set_powermode(data, 0);
764 }
765 
766 static int atlas_runtime_resume(struct device *dev)
767 {
768 	struct atlas_data *data =
769 		     iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
770 
771 	return atlas_set_powermode(data, 1);
772 }
773 #endif
774 
775 static const struct dev_pm_ops atlas_pm_ops = {
776 	SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
777 			   atlas_runtime_resume, NULL)
778 };
779 
780 static struct i2c_driver atlas_driver = {
781 	.driver = {
782 		.name	= ATLAS_DRV_NAME,
783 		.of_match_table	= of_match_ptr(atlas_dt_ids),
784 		.pm	= &atlas_pm_ops,
785 	},
786 	.probe		= atlas_probe,
787 	.remove		= atlas_remove,
788 	.id_table	= atlas_id,
789 };
790 module_i2c_driver(atlas_driver);
791 
792 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
793 MODULE_DESCRIPTION("Atlas Scientific SM sensors");
794 MODULE_LICENSE("GPL");
795