2 * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
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.
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.
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>
27 #include <linux/regmap.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/triggered_buffer.h>
33 #include <linux/pm_runtime.h>
35 #define ATLAS_REGMAP_NAME "atlas_ph_regmap"
36 #define ATLAS_DRV_NAME "atlas_ph"
38 #define ATLAS_REG_DEV_TYPE 0x00
39 #define ATLAS_REG_DEV_VERSION 0x01
41 #define ATLAS_REG_INT_CONTROL 0x04
42 #define ATLAS_REG_INT_CONTROL_EN BIT(3)
44 #define ATLAS_REG_PWR_CONTROL 0x06
46 #define ATLAS_REG_CALIB_STATUS 0x0d
47 #define ATLAS_REG_CALIB_STATUS_MASK 0x07
48 #define ATLAS_REG_CALIB_STATUS_LOW BIT(0)
49 #define ATLAS_REG_CALIB_STATUS_MID BIT(1)
50 #define ATLAS_REG_CALIB_STATUS_HIGH BIT(2)
52 #define ATLAS_REG_TEMP_DATA 0x0e
53 #define ATLAS_REG_PH_DATA 0x16
55 #define ATLAS_PH_INT_TIME_IN_US 450000
58 struct i2c_client
*client
;
59 struct iio_trigger
*trig
;
60 struct regmap
*regmap
;
63 __be32 buffer
[4]; /* 32-bit pH data + 32-bit pad + 64-bit timestamp */
66 static const struct regmap_range atlas_volatile_ranges
[] = {
67 regmap_reg_range(ATLAS_REG_INT_CONTROL
, ATLAS_REG_INT_CONTROL
),
68 regmap_reg_range(ATLAS_REG_PH_DATA
, ATLAS_REG_PH_DATA
+ 4),
71 static const struct regmap_access_table atlas_volatile_table
= {
72 .yes_ranges
= atlas_volatile_ranges
,
73 .n_yes_ranges
= ARRAY_SIZE(atlas_volatile_ranges
),
76 static const struct regmap_config atlas_regmap_config
= {
77 .name
= ATLAS_REGMAP_NAME
,
82 .volatile_table
= &atlas_volatile_table
,
83 .max_register
= ATLAS_REG_PH_DATA
+ 4,
84 .cache_type
= REGCACHE_RBTREE
,
87 static const struct iio_chan_spec atlas_channels
[] = {
91 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
100 IIO_CHAN_SOFT_TIMESTAMP(1),
103 .address
= ATLAS_REG_TEMP_DATA
,
104 .info_mask_separate
=
105 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
111 static int atlas_set_powermode(struct atlas_data
*data
, int on
)
113 return regmap_write(data
->regmap
, ATLAS_REG_PWR_CONTROL
, on
);
116 static int atlas_set_interrupt(struct atlas_data
*data
, bool state
)
118 return regmap_update_bits(data
->regmap
, ATLAS_REG_INT_CONTROL
,
119 ATLAS_REG_INT_CONTROL_EN
,
120 state
? ATLAS_REG_INT_CONTROL_EN
: 0);
123 static int atlas_buffer_postenable(struct iio_dev
*indio_dev
)
125 struct atlas_data
*data
= iio_priv(indio_dev
);
128 ret
= iio_triggered_buffer_postenable(indio_dev
);
132 ret
= pm_runtime_get_sync(&data
->client
->dev
);
134 pm_runtime_put_noidle(&data
->client
->dev
);
138 return atlas_set_interrupt(data
, true);
141 static int atlas_buffer_predisable(struct iio_dev
*indio_dev
)
143 struct atlas_data
*data
= iio_priv(indio_dev
);
146 ret
= iio_triggered_buffer_predisable(indio_dev
);
150 ret
= atlas_set_interrupt(data
, false);
154 pm_runtime_mark_last_busy(&data
->client
->dev
);
155 return pm_runtime_put_autosuspend(&data
->client
->dev
);
158 static const struct iio_trigger_ops atlas_interrupt_trigger_ops
= {
159 .owner
= THIS_MODULE
,
162 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops
= {
163 .postenable
= atlas_buffer_postenable
,
164 .predisable
= atlas_buffer_predisable
,
167 static void atlas_work_handler(struct irq_work
*work
)
169 struct atlas_data
*data
= container_of(work
, struct atlas_data
, work
);
171 iio_trigger_poll(data
->trig
);
174 static irqreturn_t
atlas_trigger_handler(int irq
, void *private)
176 struct iio_poll_func
*pf
= private;
177 struct iio_dev
*indio_dev
= pf
->indio_dev
;
178 struct atlas_data
*data
= iio_priv(indio_dev
);
181 ret
= regmap_bulk_read(data
->regmap
, ATLAS_REG_PH_DATA
,
182 (u8
*) &data
->buffer
, sizeof(data
->buffer
[0]));
185 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
188 iio_trigger_notify_done(indio_dev
->trig
);
193 static irqreturn_t
atlas_interrupt_handler(int irq
, void *private)
195 struct iio_dev
*indio_dev
= private;
196 struct atlas_data
*data
= iio_priv(indio_dev
);
198 irq_work_queue(&data
->work
);
203 static int atlas_read_ph_measurement(struct atlas_data
*data
, __be32
*val
)
205 struct device
*dev
= &data
->client
->dev
;
206 int suspended
= pm_runtime_suspended(dev
);
209 ret
= pm_runtime_get_sync(dev
);
211 pm_runtime_put_noidle(dev
);
216 usleep_range(ATLAS_PH_INT_TIME_IN_US
,
217 ATLAS_PH_INT_TIME_IN_US
+ 100000);
219 ret
= regmap_bulk_read(data
->regmap
, ATLAS_REG_PH_DATA
,
220 (u8
*) val
, sizeof(*val
));
222 pm_runtime_mark_last_busy(dev
);
223 pm_runtime_put_autosuspend(dev
);
228 static int atlas_read_raw(struct iio_dev
*indio_dev
,
229 struct iio_chan_spec
const *chan
,
230 int *val
, int *val2
, long mask
)
232 struct atlas_data
*data
= iio_priv(indio_dev
);
235 case IIO_CHAN_INFO_RAW
: {
239 switch (chan
->type
) {
241 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
242 (u8
*) ®
, sizeof(reg
));
245 mutex_lock(&indio_dev
->mlock
);
247 if (iio_buffer_enabled(indio_dev
))
250 ret
= atlas_read_ph_measurement(data
, ®
);
252 mutex_unlock(&indio_dev
->mlock
);
259 *val
= be32_to_cpu(reg
);
264 case IIO_CHAN_INFO_SCALE
:
265 switch (chan
->type
) {
271 *val
= 1; /* 0.001 */
277 return IIO_VAL_FRACTIONAL
;
283 static int atlas_write_raw(struct iio_dev
*indio_dev
,
284 struct iio_chan_spec
const *chan
,
285 int val
, int val2
, long mask
)
287 struct atlas_data
*data
= iio_priv(indio_dev
);
288 __be32 reg
= cpu_to_be32(val
);
290 if (val2
!= 0 || val
< 0 || val
> 20000)
293 if (mask
!= IIO_CHAN_INFO_RAW
|| chan
->type
!= IIO_TEMP
)
296 return regmap_bulk_write(data
->regmap
, chan
->address
,
300 static const struct iio_info atlas_info
= {
301 .driver_module
= THIS_MODULE
,
302 .read_raw
= atlas_read_raw
,
303 .write_raw
= atlas_write_raw
,
306 static int atlas_check_calibration(struct atlas_data
*data
)
308 struct device
*dev
= &data
->client
->dev
;
312 ret
= regmap_read(data
->regmap
, ATLAS_REG_CALIB_STATUS
, &val
);
316 if (!(val
& ATLAS_REG_CALIB_STATUS_MASK
)) {
317 dev_warn(dev
, "device has not been calibrated\n");
321 if (!(val
& ATLAS_REG_CALIB_STATUS_LOW
))
322 dev_warn(dev
, "device missing low point calibration\n");
324 if (!(val
& ATLAS_REG_CALIB_STATUS_MID
))
325 dev_warn(dev
, "device missing mid point calibration\n");
327 if (!(val
& ATLAS_REG_CALIB_STATUS_HIGH
))
328 dev_warn(dev
, "device missing high point calibration\n");
333 static int atlas_probe(struct i2c_client
*client
,
334 const struct i2c_device_id
*id
)
336 struct atlas_data
*data
;
337 struct iio_trigger
*trig
;
338 struct iio_dev
*indio_dev
;
341 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
345 indio_dev
->info
= &atlas_info
;
346 indio_dev
->name
= ATLAS_DRV_NAME
;
347 indio_dev
->channels
= atlas_channels
;
348 indio_dev
->num_channels
= ARRAY_SIZE(atlas_channels
);
349 indio_dev
->modes
= INDIO_BUFFER_SOFTWARE
| INDIO_DIRECT_MODE
;
350 indio_dev
->dev
.parent
= &client
->dev
;
352 trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-dev%d",
353 indio_dev
->name
, indio_dev
->id
);
358 data
= iio_priv(indio_dev
);
359 data
->client
= client
;
361 trig
->dev
.parent
= indio_dev
->dev
.parent
;
362 trig
->ops
= &atlas_interrupt_trigger_ops
;
363 iio_trigger_set_drvdata(trig
, indio_dev
);
365 i2c_set_clientdata(client
, indio_dev
);
367 data
->regmap
= devm_regmap_init_i2c(client
, &atlas_regmap_config
);
368 if (IS_ERR(data
->regmap
)) {
369 dev_err(&client
->dev
, "regmap initialization failed\n");
370 return PTR_ERR(data
->regmap
);
373 ret
= pm_runtime_set_active(&client
->dev
);
377 if (client
->irq
<= 0) {
378 dev_err(&client
->dev
, "no valid irq defined\n");
382 ret
= atlas_check_calibration(data
);
386 ret
= iio_trigger_register(trig
);
388 dev_err(&client
->dev
, "failed to register trigger\n");
392 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
393 &atlas_trigger_handler
, &atlas_buffer_setup_ops
);
395 dev_err(&client
->dev
, "cannot setup iio trigger\n");
396 goto unregister_trigger
;
399 init_irq_work(&data
->work
, atlas_work_handler
);
401 /* interrupt pin toggles on new conversion */
402 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
403 NULL
, atlas_interrupt_handler
,
404 IRQF_TRIGGER_RISING
|
405 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
409 dev_err(&client
->dev
, "request irq (%d) failed\n", client
->irq
);
410 goto unregister_buffer
;
413 ret
= atlas_set_powermode(data
, 1);
415 dev_err(&client
->dev
, "cannot power device on");
416 goto unregister_buffer
;
419 pm_runtime_enable(&client
->dev
);
420 pm_runtime_set_autosuspend_delay(&client
->dev
, 2500);
421 pm_runtime_use_autosuspend(&client
->dev
);
423 ret
= iio_device_register(indio_dev
);
425 dev_err(&client
->dev
, "unable to register device\n");
432 pm_runtime_disable(&client
->dev
);
433 atlas_set_powermode(data
, 0);
436 iio_triggered_buffer_cleanup(indio_dev
);
439 iio_trigger_unregister(data
->trig
);
444 static int atlas_remove(struct i2c_client
*client
)
446 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
447 struct atlas_data
*data
= iio_priv(indio_dev
);
449 iio_device_unregister(indio_dev
);
450 iio_triggered_buffer_cleanup(indio_dev
);
451 iio_trigger_unregister(data
->trig
);
453 pm_runtime_disable(&client
->dev
);
454 pm_runtime_set_suspended(&client
->dev
);
455 pm_runtime_put_noidle(&client
->dev
);
457 return atlas_set_powermode(data
, 0);
461 static int atlas_runtime_suspend(struct device
*dev
)
463 struct atlas_data
*data
=
464 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
466 return atlas_set_powermode(data
, 0);
469 static int atlas_runtime_resume(struct device
*dev
)
471 struct atlas_data
*data
=
472 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
474 return atlas_set_powermode(data
, 1);
478 static const struct dev_pm_ops atlas_pm_ops
= {
479 SET_RUNTIME_PM_OPS(atlas_runtime_suspend
,
480 atlas_runtime_resume
, NULL
)
483 static const struct i2c_device_id atlas_id
[] = {
484 { "atlas-ph-sm", 0 },
487 MODULE_DEVICE_TABLE(i2c
, atlas_id
);
489 static const struct of_device_id atlas_dt_ids
[] = {
490 { .compatible
= "atlas,ph-sm" },
493 MODULE_DEVICE_TABLE(of
, atlas_dt_ids
);
495 static struct i2c_driver atlas_driver
= {
497 .name
= ATLAS_DRV_NAME
,
498 .of_match_table
= of_match_ptr(atlas_dt_ids
),
501 .probe
= atlas_probe
,
502 .remove
= atlas_remove
,
503 .id_table
= atlas_id
,
505 module_i2c_driver(atlas_driver
);
507 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
508 MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
509 MODULE_LICENSE("GPL");