blob: 56ba6c82b501f186f9f34324b9306ff7f464f6f6 [file] [log] [blame]
Matt Ranostayd6ad8052018-02-17 21:36:46 -08001// SPDX-License-Identifier: GPL-2.0+
Matt Ranostay27dec002016-01-26 18:34:31 -08002/*
Matt Ranostay0d151902019-12-15 16:00:45 -08003 * atlas-sensor.c - Support for Atlas Scientific OEM SM sensors
Matt Ranostay27dec002016-01-26 18:34:31 -08004 *
Matt Ranostay0d151902019-12-15 16:00:45 -08005 * Copyright (C) 2015-2019 Konsulko Group
Matt Ranostayd6ad8052018-02-17 21:36:46 -08006 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
Matt Ranostay27dec002016-01-26 18:34:31 -08007 */
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>
Matt Ranostay27dec002016-01-26 18:34:31 -080017#include <linux/i2c.h>
Jonathan Camerona867e892020-09-10 18:32:29 +010018#include <linux/mod_devicetable.h>
Matt Ranostay27dec002016-01-26 18:34:31 -080019#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
Matt Ranostay0d151902019-12-15 16:00:45 -080027#define ATLAS_REGMAP_NAME "atlas_regmap"
28#define ATLAS_DRV_NAME "atlas"
Matt Ranostay27dec002016-01-26 18:34:31 -080029
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
Matt Ranostay7103b992016-05-24 21:29:18 -070038#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)
Matt Ranostay27dec002016-01-26 18:34:31 -080043
Matt Ranostaye8dd92b2016-05-24 21:29:20 -070044#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
Matt Ranostaya751b8e2020-02-17 19:42:38 -080051#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
Matt Ranostayb101c932020-04-25 10:25:20 +030056#define ATLAS_REG_RTD_DATA 0x0e
57
Matt Ranostay7103b992016-05-24 21:29:18 -070058#define ATLAS_REG_PH_TEMP_DATA 0x0e
Matt Ranostay27dec002016-01-26 18:34:31 -080059#define ATLAS_REG_PH_DATA 0x16
60
Matt Ranostaye8dd92b2016-05-24 21:29:20 -070061#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
Matt Ranostayce08cc92016-08-19 13:36:11 -070067#define ATLAS_REG_ORP_CALIB_STATUS 0x0d
68#define ATLAS_REG_ORP_DATA 0x0e
69
Matt Ranostaya751b8e2020-02-17 19:42:38 -080070#define ATLAS_REG_DO_TEMP_DATA 0x12
71#define ATLAS_REG_DO_DATA 0x22
72
Matt Ranostay02112262018-04-09 23:18:22 -070073#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
Matt Ranostaya751b8e2020-02-17 19:42:38 -080076#define ATLAS_DO_INT_TIME_IN_MS 450
Matt Ranostayb101c932020-04-25 10:25:20 +030077#define ATLAS_RTD_INT_TIME_IN_MS 450
Matt Ranostay27dec002016-01-26 18:34:31 -080078
Matt Ranostay7103b992016-05-24 21:29:18 -070079enum {
80 ATLAS_PH_SM,
Matt Ranostaye8dd92b2016-05-24 21:29:20 -070081 ATLAS_EC_SM,
Matt Ranostayce08cc92016-08-19 13:36:11 -070082 ATLAS_ORP_SM,
Matt Ranostaya751b8e2020-02-17 19:42:38 -080083 ATLAS_DO_SM,
Matt Ranostayb101c932020-04-25 10:25:20 +030084 ATLAS_RTD_SM,
Matt Ranostay7103b992016-05-24 21:29:18 -070085};
86
Matt Ranostay27dec002016-01-26 18:34:31 -080087struct atlas_data {
88 struct i2c_client *client;
89 struct iio_trigger *trig;
Matt Ranostay7103b992016-05-24 21:29:18 -070090 struct atlas_device *chip;
Matt Ranostay27dec002016-01-26 18:34:31 -080091 struct regmap *regmap;
92 struct irq_work work;
Matt Ranostayd4578dc2020-02-17 19:42:37 -080093 unsigned int interrupt_enabled;
Matt Ranostay27dec002016-01-26 18:34:31 -080094
Matt Ranostaye8dd92b2016-05-24 21:29:20 -070095 __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
Matt Ranostay27dec002016-01-26 18:34:31 -080096};
97
Matt Ranostay27dec002016-01-26 18:34:31 -080098static const struct regmap_config atlas_regmap_config = {
99 .name = ATLAS_REGMAP_NAME,
Matt Ranostay27dec002016-01-26 18:34:31 -0800100 .reg_bits = 8,
101 .val_bits = 8,
Matt Ranostay27dec002016-01-26 18:34:31 -0800102};
103
Matt Ranostay47668972020-01-03 17:29:08 -0800104static 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
Matt Ranostay7103b992016-05-24 21:29:18 -0700114static const struct iio_chan_spec atlas_ph_channels[] = {
Matt Ranostay27dec002016-01-26 18:34:31 -0800115 {
116 .type = IIO_PH,
Matt Ranostay7103b992016-05-24 21:29:18 -0700117 .address = ATLAS_REG_PH_DATA,
Matt Ranostay27dec002016-01-26 18:34:31 -0800118 .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,
Matt Ranostay7103b992016-05-24 21:29:18 -0700131 .address = ATLAS_REG_PH_TEMP_DATA,
Matt Ranostay27dec002016-01-26 18:34:31 -0800132 .info_mask_separate =
133 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
134 .output = 1,
135 .scan_index = -1
136 },
137};
138
Matt Ranostaya751b8e2020-02-17 19:42:38 -0800139#define ATLAS_CONCENTRATION_CHANNEL(_idx, _addr) \
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700140 {\
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
156static 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 },
Matt Ranostaya751b8e2020-02-17 19:42:38 -0800170 ATLAS_CONCENTRATION_CHANNEL(0, ATLAS_REG_TDS_DATA),
171 ATLAS_CONCENTRATION_CHANNEL(1, ATLAS_REG_PSS_DATA),
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700172 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
Matt Ranostayce08cc92016-08-19 13:36:11 -0700183static 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
Matt Ranostaya751b8e2020-02-17 19:42:38 -0800200static const struct iio_chan_spec atlas_do_channels[] = {
Matt Ranostay98bcead2020-04-20 02:28:47 +0300201 {
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 },
Matt Ranostaya751b8e2020-02-17 19:42:38 -0800214 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
Matt Ranostayb101c932020-04-25 10:25:20 +0300225static 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
Matt Ranostay7103b992016-05-24 21:29:18 -0700241static 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
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700268static int atlas_check_ec_calibration(struct atlas_data *data)
269{
270 struct device *dev = &data->client->dev;
271 int ret;
272 unsigned int val;
Sandhya Bankard1fe85e2016-09-25 00:46:21 +0530273 __be16 rval;
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700274
Sandhya Bankard1fe85e2016-09-25 00:46:21 +0530275 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2);
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700276 if (ret)
277 return ret;
278
Sandhya Bankard1fe85e2016-09-25 00:46:21 +0530279 val = be16_to_cpu(rval);
280 dev_info(dev, "probe set to K = %d.%.2d", val / 100, val % 100);
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700281
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
Matt Ranostayce08cc92016-08-19 13:36:11 -0700307static 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;
Matt Ranostaya751b8e2020-02-17 19:42:38 -0800321}
322
323static 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}
Matt Ranostayce08cc92016-08-19 13:36:11 -0700346
Matt Ranostay7103b992016-05-24 21:29:18 -0700347struct 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
356static 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,
Matt Ranostay02112262018-04-09 23:18:22 -0700362 .delay = ATLAS_PH_INT_TIME_IN_MS,
Matt Ranostay7103b992016-05-24 21:29:18 -0700363 },
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700364 [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,
Matt Ranostay02112262018-04-09 23:18:22 -0700369 .delay = ATLAS_EC_INT_TIME_IN_MS,
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700370 },
Matt Ranostayce08cc92016-08-19 13:36:11 -0700371 [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,
Matt Ranostay02112262018-04-09 23:18:22 -0700376 .delay = ATLAS_ORP_INT_TIME_IN_MS,
Matt Ranostayce08cc92016-08-19 13:36:11 -0700377 },
Matt Ranostaya751b8e2020-02-17 19:42:38 -0800378 [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 },
Matt Ranostayb101c932020-04-25 10:25:20 +0300385 [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 },
Matt Ranostay7103b992016-05-24 21:29:18 -0700391};
392
Matt Ranostay27dec002016-01-26 18:34:31 -0800393static int atlas_set_powermode(struct atlas_data *data, int on)
394{
395 return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
396}
397
398static int atlas_set_interrupt(struct atlas_data *data, bool state)
399{
Matt Ranostayd4578dc2020-02-17 19:42:37 -0800400 if (!data->interrupt_enabled)
401 return 0;
402
Matt Ranostay27dec002016-01-26 18:34:31 -0800403 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
408static int atlas_buffer_postenable(struct iio_dev *indio_dev)
409{
410 struct atlas_data *data = iio_priv(indio_dev);
411 int ret;
412
Matt Ranostay27dec002016-01-26 18:34:31 -0800413 ret = pm_runtime_get_sync(&data->client->dev);
414 if (ret < 0) {
415 pm_runtime_put_noidle(&data->client->dev);
416 return ret;
417 }
418
419 return atlas_set_interrupt(data, true);
420}
421
422static int atlas_buffer_predisable(struct iio_dev *indio_dev)
423{
424 struct atlas_data *data = iio_priv(indio_dev);
425 int ret;
426
Matt Ranostay27dec002016-01-26 18:34:31 -0800427 ret = atlas_set_interrupt(data, false);
428 if (ret)
429 return ret;
430
431 pm_runtime_mark_last_busy(&data->client->dev);
Alexandru Ardelean0c8a6e72019-09-20 10:31:22 +0300432 ret = pm_runtime_put_autosuspend(&data->client->dev);
433 if (ret)
434 return ret;
435
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +0300436 return 0;
Matt Ranostay27dec002016-01-26 18:34:31 -0800437}
438
439static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
Matt Ranostay27dec002016-01-26 18:34:31 -0800440};
441
442static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
443 .postenable = atlas_buffer_postenable,
444 .predisable = atlas_buffer_predisable,
445};
446
447static void atlas_work_handler(struct irq_work *work)
448{
449 struct atlas_data *data = container_of(work, struct atlas_data, work);
450
451 iio_trigger_poll(data->trig);
452}
453
454static irqreturn_t atlas_trigger_handler(int irq, void *private)
455{
456 struct iio_poll_func *pf = private;
457 struct iio_dev *indio_dev = pf->indio_dev;
458 struct atlas_data *data = iio_priv(indio_dev);
Matt Ranostay47668972020-01-03 17:29:08 -0800459 int channels = atlas_buffer_num_channels(data->chip->channels);
Matt Ranostay27dec002016-01-26 18:34:31 -0800460 int ret;
461
Matt Ranostay7103b992016-05-24 21:29:18 -0700462 ret = regmap_bulk_read(data->regmap, data->chip->data_reg,
Jonathan Cameron0f92afe2020-04-05 19:03:17 +0100463 &data->buffer, sizeof(__be32) * channels);
Matt Ranostay27dec002016-01-26 18:34:31 -0800464
Matt Ranostay98e55e92016-02-14 18:59:07 -0800465 if (!ret)
Matt Ranostay27dec002016-01-26 18:34:31 -0800466 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100467 iio_get_time_ns(indio_dev));
Matt Ranostay27dec002016-01-26 18:34:31 -0800468
469 iio_trigger_notify_done(indio_dev->trig);
470
471 return IRQ_HANDLED;
472}
473
474static irqreturn_t atlas_interrupt_handler(int irq, void *private)
475{
476 struct iio_dev *indio_dev = private;
477 struct atlas_data *data = iio_priv(indio_dev);
478
479 irq_work_queue(&data->work);
480
481 return IRQ_HANDLED;
482}
483
Matt Ranostay7103b992016-05-24 21:29:18 -0700484static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
Matt Ranostay27dec002016-01-26 18:34:31 -0800485{
486 struct device *dev = &data->client->dev;
487 int suspended = pm_runtime_suspended(dev);
488 int ret;
489
490 ret = pm_runtime_get_sync(dev);
491 if (ret < 0) {
492 pm_runtime_put_noidle(dev);
493 return ret;
494 }
495
496 if (suspended)
Matt Ranostay02112262018-04-09 23:18:22 -0700497 msleep(data->chip->delay);
Matt Ranostay27dec002016-01-26 18:34:31 -0800498
Jonathan Cameron0f92afe2020-04-05 19:03:17 +0100499 ret = regmap_bulk_read(data->regmap, reg, val, sizeof(*val));
Matt Ranostay27dec002016-01-26 18:34:31 -0800500
501 pm_runtime_mark_last_busy(dev);
502 pm_runtime_put_autosuspend(dev);
503
504 return ret;
505}
506
507static int atlas_read_raw(struct iio_dev *indio_dev,
508 struct iio_chan_spec const *chan,
509 int *val, int *val2, long mask)
510{
511 struct atlas_data *data = iio_priv(indio_dev);
512
513 switch (mask) {
Matt Ranostayb101c932020-04-25 10:25:20 +0300514 case IIO_CHAN_INFO_PROCESSED:
Matt Ranostay27dec002016-01-26 18:34:31 -0800515 case IIO_CHAN_INFO_RAW: {
516 int ret;
517 __be32 reg;
518
519 switch (chan->type) {
520 case IIO_TEMP:
521 ret = regmap_bulk_read(data->regmap, chan->address,
Jonathan Cameron0f92afe2020-04-05 19:03:17 +0100522 &reg, sizeof(reg));
Matt Ranostay27dec002016-01-26 18:34:31 -0800523 break;
524 case IIO_PH:
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700525 case IIO_CONCENTRATION:
526 case IIO_ELECTRICALCONDUCTIVITY:
Matt Ranostayce08cc92016-08-19 13:36:11 -0700527 case IIO_VOLTAGE:
Matt Ranostayb015b3e2016-08-19 13:36:09 -0700528 ret = iio_device_claim_direct_mode(indio_dev);
529 if (ret)
530 return ret;
Matt Ranostay27dec002016-01-26 18:34:31 -0800531
Matt Ranostayb015b3e2016-08-19 13:36:09 -0700532 ret = atlas_read_measurement(data, chan->address, &reg);
Matt Ranostay27dec002016-01-26 18:34:31 -0800533
Matt Ranostayb015b3e2016-08-19 13:36:09 -0700534 iio_device_release_direct_mode(indio_dev);
Matt Ranostay27dec002016-01-26 18:34:31 -0800535 break;
536 default:
537 ret = -EINVAL;
538 }
539
540 if (!ret) {
541 *val = be32_to_cpu(reg);
542 ret = IIO_VAL_INT;
543 }
544 return ret;
545 }
546 case IIO_CHAN_INFO_SCALE:
547 switch (chan->type) {
548 case IIO_TEMP:
Matt Ranostay08088312018-12-30 19:07:01 -0800549 *val = 10;
550 return IIO_VAL_INT;
Matt Ranostay27dec002016-01-26 18:34:31 -0800551 case IIO_PH:
552 *val = 1; /* 0.001 */
553 *val2 = 1000;
554 break;
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700555 case IIO_ELECTRICALCONDUCTIVITY:
556 *val = 1; /* 0.00001 */
Colin Ian Kingca64d4b2016-07-25 23:06:56 +0100557 *val2 = 100000;
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700558 break;
559 case IIO_CONCENTRATION:
560 *val = 0; /* 0.000000001 */
561 *val2 = 1000;
562 return IIO_VAL_INT_PLUS_NANO;
Matt Ranostayce08cc92016-08-19 13:36:11 -0700563 case IIO_VOLTAGE:
564 *val = 1; /* 0.1 */
565 *val2 = 10;
566 break;
Matt Ranostay27dec002016-01-26 18:34:31 -0800567 default:
568 return -EINVAL;
569 }
570 return IIO_VAL_FRACTIONAL;
571 }
572
573 return -EINVAL;
574}
575
576static int atlas_write_raw(struct iio_dev *indio_dev,
577 struct iio_chan_spec const *chan,
578 int val, int val2, long mask)
579{
580 struct atlas_data *data = iio_priv(indio_dev);
Matt Ranostay08088312018-12-30 19:07:01 -0800581 __be32 reg = cpu_to_be32(val / 10);
Matt Ranostay27dec002016-01-26 18:34:31 -0800582
583 if (val2 != 0 || val < 0 || val > 20000)
584 return -EINVAL;
585
586 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
587 return -EINVAL;
588
589 return regmap_bulk_write(data->regmap, chan->address,
590 &reg, sizeof(reg));
591}
592
593static const struct iio_info atlas_info = {
Matt Ranostay27dec002016-01-26 18:34:31 -0800594 .read_raw = atlas_read_raw,
595 .write_raw = atlas_write_raw,
596};
597
Matt Ranostay7103b992016-05-24 21:29:18 -0700598static const struct i2c_device_id atlas_id[] = {
599 { "atlas-ph-sm", ATLAS_PH_SM},
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700600 { "atlas-ec-sm", ATLAS_EC_SM},
Matt Ranostayce08cc92016-08-19 13:36:11 -0700601 { "atlas-orp-sm", ATLAS_ORP_SM},
Matt Ranostaya751b8e2020-02-17 19:42:38 -0800602 { "atlas-do-sm", ATLAS_DO_SM},
Matt Ranostayb101c932020-04-25 10:25:20 +0300603 { "atlas-rtd-sm", ATLAS_RTD_SM},
Matt Ranostay7103b992016-05-24 21:29:18 -0700604 {}
Matt Ranostay27dec002016-01-26 18:34:31 -0800605};
Matt Ranostay7103b992016-05-24 21:29:18 -0700606MODULE_DEVICE_TABLE(i2c, atlas_id);
607
608static const struct of_device_id atlas_dt_ids[] = {
609 { .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, },
Matt Ranostaye8dd92b2016-05-24 21:29:20 -0700610 { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, },
Matt Ranostayce08cc92016-08-19 13:36:11 -0700611 { .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, },
Matt Ranostaya751b8e2020-02-17 19:42:38 -0800612 { .compatible = "atlas,do-sm", .data = (void *)ATLAS_DO_SM, },
Matt Ranostayb101c932020-04-25 10:25:20 +0300613 { .compatible = "atlas,rtd-sm", .data = (void *)ATLAS_RTD_SM, },
Matt Ranostay7103b992016-05-24 21:29:18 -0700614 { }
615};
616MODULE_DEVICE_TABLE(of, atlas_dt_ids);
Matt Ranostay27dec002016-01-26 18:34:31 -0800617
618static int atlas_probe(struct i2c_client *client,
619 const struct i2c_device_id *id)
620{
621 struct atlas_data *data;
Matt Ranostay7103b992016-05-24 21:29:18 -0700622 struct atlas_device *chip;
Matt Ranostay27dec002016-01-26 18:34:31 -0800623 struct iio_trigger *trig;
624 struct iio_dev *indio_dev;
625 int ret;
626
627 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
628 if (!indio_dev)
629 return -ENOMEM;
630
Jonathan Camerona867e892020-09-10 18:32:29 +0100631 if (!dev_fwnode(&client->dev))
Matt Ranostay7103b992016-05-24 21:29:18 -0700632 chip = &atlas_devices[id->driver_data];
633 else
Jonathan Camerona867e892020-09-10 18:32:29 +0100634 chip = &atlas_devices[(unsigned long)device_get_match_data(&client->dev)];
Matt Ranostay7103b992016-05-24 21:29:18 -0700635
Matt Ranostay27dec002016-01-26 18:34:31 -0800636 indio_dev->info = &atlas_info;
637 indio_dev->name = ATLAS_DRV_NAME;
Matt Ranostay7103b992016-05-24 21:29:18 -0700638 indio_dev->channels = chip->channels;
639 indio_dev->num_channels = chip->num_channels;
Matt Ranostay27dec002016-01-26 18:34:31 -0800640 indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE;
Matt Ranostay27dec002016-01-26 18:34:31 -0800641
642 trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
643 indio_dev->name, indio_dev->id);
644
645 if (!trig)
646 return -ENOMEM;
647
648 data = iio_priv(indio_dev);
649 data->client = client;
650 data->trig = trig;
Matt Ranostay7103b992016-05-24 21:29:18 -0700651 data->chip = chip;
Matt Ranostay27dec002016-01-26 18:34:31 -0800652 trig->ops = &atlas_interrupt_trigger_ops;
653 iio_trigger_set_drvdata(trig, indio_dev);
654
655 i2c_set_clientdata(client, indio_dev);
656
657 data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config);
658 if (IS_ERR(data->regmap)) {
659 dev_err(&client->dev, "regmap initialization failed\n");
660 return PTR_ERR(data->regmap);
661 }
662
663 ret = pm_runtime_set_active(&client->dev);
664 if (ret)
665 return ret;
666
Matt Ranostay7103b992016-05-24 21:29:18 -0700667 ret = chip->calibration(data);
Matt Ranostay27dec002016-01-26 18:34:31 -0800668 if (ret)
669 return ret;
670
671 ret = iio_trigger_register(trig);
672 if (ret) {
673 dev_err(&client->dev, "failed to register trigger\n");
674 return ret;
675 }
676
677 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
678 &atlas_trigger_handler, &atlas_buffer_setup_ops);
679 if (ret) {
680 dev_err(&client->dev, "cannot setup iio trigger\n");
681 goto unregister_trigger;
682 }
683
684 init_irq_work(&data->work, atlas_work_handler);
685
Matt Ranostayd4578dc2020-02-17 19:42:37 -0800686 if (client->irq > 0) {
687 /* interrupt pin toggles on new conversion */
688 ret = devm_request_threaded_irq(&client->dev, client->irq,
689 NULL, atlas_interrupt_handler,
690 IRQF_TRIGGER_RISING |
691 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
692 "atlas_irq",
693 indio_dev);
694
695 if (ret)
696 dev_warn(&client->dev,
697 "request irq (%d) failed\n", client->irq);
698 else
699 data->interrupt_enabled = 1;
Matt Ranostay27dec002016-01-26 18:34:31 -0800700 }
701
702 ret = atlas_set_powermode(data, 1);
703 if (ret) {
704 dev_err(&client->dev, "cannot power device on");
705 goto unregister_buffer;
706 }
707
708 pm_runtime_enable(&client->dev);
709 pm_runtime_set_autosuspend_delay(&client->dev, 2500);
710 pm_runtime_use_autosuspend(&client->dev);
711
712 ret = iio_device_register(indio_dev);
713 if (ret) {
714 dev_err(&client->dev, "unable to register device\n");
715 goto unregister_pm;
716 }
717
718 return 0;
719
720unregister_pm:
721 pm_runtime_disable(&client->dev);
722 atlas_set_powermode(data, 0);
723
724unregister_buffer:
725 iio_triggered_buffer_cleanup(indio_dev);
726
727unregister_trigger:
728 iio_trigger_unregister(data->trig);
729
730 return ret;
731}
732
733static int atlas_remove(struct i2c_client *client)
734{
735 struct iio_dev *indio_dev = i2c_get_clientdata(client);
736 struct atlas_data *data = iio_priv(indio_dev);
737
738 iio_device_unregister(indio_dev);
739 iio_triggered_buffer_cleanup(indio_dev);
740 iio_trigger_unregister(data->trig);
741
742 pm_runtime_disable(&client->dev);
743 pm_runtime_set_suspended(&client->dev);
744 pm_runtime_put_noidle(&client->dev);
745
746 return atlas_set_powermode(data, 0);
747}
748
749#ifdef CONFIG_PM
750static int atlas_runtime_suspend(struct device *dev)
751{
752 struct atlas_data *data =
753 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
754
755 return atlas_set_powermode(data, 0);
756}
757
758static int atlas_runtime_resume(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, 1);
764}
765#endif
766
767static const struct dev_pm_ops atlas_pm_ops = {
768 SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
769 atlas_runtime_resume, NULL)
770};
771
Matt Ranostay27dec002016-01-26 18:34:31 -0800772static struct i2c_driver atlas_driver = {
773 .driver = {
774 .name = ATLAS_DRV_NAME,
Jonathan Camerona867e892020-09-10 18:32:29 +0100775 .of_match_table = atlas_dt_ids,
Matt Ranostay27dec002016-01-26 18:34:31 -0800776 .pm = &atlas_pm_ops,
777 },
778 .probe = atlas_probe,
779 .remove = atlas_remove,
780 .id_table = atlas_id,
781};
782module_i2c_driver(atlas_driver);
783
Matt Ranostayd6ad8052018-02-17 21:36:46 -0800784MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
Matt Ranostay0d151902019-12-15 16:00:45 -0800785MODULE_DESCRIPTION("Atlas Scientific SM sensors");
Matt Ranostay27dec002016-01-26 18:34:31 -0800786MODULE_LICENSE("GPL");