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_CALIB_STATUS
, ATLAS_REG_CALIB_STATUS
),
69 regmap_reg_range(ATLAS_REG_TEMP_DATA
, ATLAS_REG_TEMP_DATA
+ 4),
70 regmap_reg_range(ATLAS_REG_PH_DATA
, ATLAS_REG_PH_DATA
+ 4),
73 static const struct regmap_access_table atlas_volatile_table
= {
74 .yes_ranges
= atlas_volatile_ranges
,
75 .n_yes_ranges
= ARRAY_SIZE(atlas_volatile_ranges
),
78 static const struct regmap_config atlas_regmap_config
= {
79 .name
= ATLAS_REGMAP_NAME
,
84 .volatile_table
= &atlas_volatile_table
,
85 .max_register
= ATLAS_REG_PH_DATA
+ 4,
86 .cache_type
= REGCACHE_FLAT
,
89 static const struct iio_chan_spec atlas_channels
[] = {
93 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
102 IIO_CHAN_SOFT_TIMESTAMP(1),
105 .address
= ATLAS_REG_TEMP_DATA
,
106 .info_mask_separate
=
107 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
113 static int atlas_set_powermode(struct atlas_data
*data
, int on
)
115 return regmap_write(data
->regmap
, ATLAS_REG_PWR_CONTROL
, on
);
118 static int atlas_set_interrupt(struct atlas_data
*data
, bool state
)
120 return regmap_update_bits(data
->regmap
, ATLAS_REG_INT_CONTROL
,
121 ATLAS_REG_INT_CONTROL_EN
,
122 state
? ATLAS_REG_INT_CONTROL_EN
: 0);
125 static int atlas_buffer_postenable(struct iio_dev
*indio_dev
)
127 struct atlas_data
*data
= iio_priv(indio_dev
);
130 ret
= iio_triggered_buffer_postenable(indio_dev
);
134 ret
= pm_runtime_get_sync(&data
->client
->dev
);
136 pm_runtime_put_noidle(&data
->client
->dev
);
140 return atlas_set_interrupt(data
, true);
143 static int atlas_buffer_predisable(struct iio_dev
*indio_dev
)
145 struct atlas_data
*data
= iio_priv(indio_dev
);
148 ret
= iio_triggered_buffer_predisable(indio_dev
);
152 ret
= atlas_set_interrupt(data
, false);
156 pm_runtime_mark_last_busy(&data
->client
->dev
);
157 return pm_runtime_put_autosuspend(&data
->client
->dev
);
160 static const struct iio_trigger_ops atlas_interrupt_trigger_ops
= {
161 .owner
= THIS_MODULE
,
164 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops
= {
165 .postenable
= atlas_buffer_postenable
,
166 .predisable
= atlas_buffer_predisable
,
169 static void atlas_work_handler(struct irq_work
*work
)
171 struct atlas_data
*data
= container_of(work
, struct atlas_data
, work
);
173 iio_trigger_poll(data
->trig
);
176 static irqreturn_t
atlas_trigger_handler(int irq
, void *private)
178 struct iio_poll_func
*pf
= private;
179 struct iio_dev
*indio_dev
= pf
->indio_dev
;
180 struct atlas_data
*data
= iio_priv(indio_dev
);
183 ret
= i2c_smbus_read_i2c_block_data(data
->client
, ATLAS_REG_PH_DATA
,
184 sizeof(data
->buffer
[0]), (u8
*) &data
->buffer
);
187 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
190 iio_trigger_notify_done(indio_dev
->trig
);
195 static irqreturn_t
atlas_interrupt_handler(int irq
, void *private)
197 struct iio_dev
*indio_dev
= private;
198 struct atlas_data
*data
= iio_priv(indio_dev
);
200 irq_work_queue(&data
->work
);
205 static int atlas_read_ph_measurement(struct atlas_data
*data
, __be32
*val
)
207 struct device
*dev
= &data
->client
->dev
;
208 int suspended
= pm_runtime_suspended(dev
);
211 ret
= pm_runtime_get_sync(dev
);
213 pm_runtime_put_noidle(dev
);
218 usleep_range(ATLAS_PH_INT_TIME_IN_US
,
219 ATLAS_PH_INT_TIME_IN_US
+ 100000);
221 ret
= regmap_bulk_read(data
->regmap
, ATLAS_REG_PH_DATA
,
222 (u8
*) val
, sizeof(*val
));
224 pm_runtime_mark_last_busy(dev
);
225 pm_runtime_put_autosuspend(dev
);
230 static int atlas_read_raw(struct iio_dev
*indio_dev
,
231 struct iio_chan_spec
const *chan
,
232 int *val
, int *val2
, long mask
)
234 struct atlas_data
*data
= iio_priv(indio_dev
);
237 case IIO_CHAN_INFO_RAW
: {
241 switch (chan
->type
) {
243 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
244 (u8
*) ®
, sizeof(reg
));
247 mutex_lock(&indio_dev
->mlock
);
249 if (iio_buffer_enabled(indio_dev
))
252 ret
= atlas_read_ph_measurement(data
, ®
);
254 mutex_unlock(&indio_dev
->mlock
);
261 *val
= be32_to_cpu(reg
);
266 case IIO_CHAN_INFO_SCALE
:
267 switch (chan
->type
) {
273 *val
= 1; /* 0.001 */
279 return IIO_VAL_FRACTIONAL
;
285 static int atlas_write_raw(struct iio_dev
*indio_dev
,
286 struct iio_chan_spec
const *chan
,
287 int val
, int val2
, long mask
)
289 struct atlas_data
*data
= iio_priv(indio_dev
);
290 __be32 reg
= cpu_to_be32(val
);
292 if (val2
!= 0 || val
< 0 || val
> 20000)
295 if (mask
!= IIO_CHAN_INFO_RAW
|| chan
->type
!= IIO_TEMP
)
298 return regmap_bulk_write(data
->regmap
, chan
->address
,
302 static const struct iio_info atlas_info
= {
303 .driver_module
= THIS_MODULE
,
304 .read_raw
= atlas_read_raw
,
305 .write_raw
= atlas_write_raw
,
308 static int atlas_check_calibration(struct atlas_data
*data
)
310 struct device
*dev
= &data
->client
->dev
;
314 ret
= regmap_read(data
->regmap
, ATLAS_REG_CALIB_STATUS
, &val
);
318 if (!(val
& ATLAS_REG_CALIB_STATUS_MASK
)) {
319 dev_warn(dev
, "device has not been calibrated\n");
323 if (!(val
& ATLAS_REG_CALIB_STATUS_LOW
))
324 dev_warn(dev
, "device missing low point calibration\n");
326 if (!(val
& ATLAS_REG_CALIB_STATUS_MID
))
327 dev_warn(dev
, "device missing mid point calibration\n");
329 if (!(val
& ATLAS_REG_CALIB_STATUS_HIGH
))
330 dev_warn(dev
, "device missing high point calibration\n");
335 static int atlas_probe(struct i2c_client
*client
,
336 const struct i2c_device_id
*id
)
338 struct atlas_data
*data
;
339 struct iio_trigger
*trig
;
340 struct iio_dev
*indio_dev
;
343 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
347 indio_dev
->info
= &atlas_info
;
348 indio_dev
->name
= ATLAS_DRV_NAME
;
349 indio_dev
->channels
= atlas_channels
;
350 indio_dev
->num_channels
= ARRAY_SIZE(atlas_channels
);
351 indio_dev
->modes
= INDIO_BUFFER_SOFTWARE
| INDIO_DIRECT_MODE
;
352 indio_dev
->dev
.parent
= &client
->dev
;
354 trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-dev%d",
355 indio_dev
->name
, indio_dev
->id
);
360 data
= iio_priv(indio_dev
);
361 data
->client
= client
;
363 trig
->dev
.parent
= indio_dev
->dev
.parent
;
364 trig
->ops
= &atlas_interrupt_trigger_ops
;
365 iio_trigger_set_drvdata(trig
, indio_dev
);
367 i2c_set_clientdata(client
, indio_dev
);
369 data
->regmap
= devm_regmap_init_i2c(client
, &atlas_regmap_config
);
370 if (IS_ERR(data
->regmap
)) {
371 dev_err(&client
->dev
, "regmap initialization failed\n");
372 return PTR_ERR(data
->regmap
);
375 ret
= pm_runtime_set_active(&client
->dev
);
379 if (client
->irq
<= 0) {
380 dev_err(&client
->dev
, "no valid irq defined\n");
384 ret
= atlas_check_calibration(data
);
388 ret
= iio_trigger_register(trig
);
390 dev_err(&client
->dev
, "failed to register trigger\n");
394 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
395 &atlas_trigger_handler
, &atlas_buffer_setup_ops
);
397 dev_err(&client
->dev
, "cannot setup iio trigger\n");
398 goto unregister_trigger
;
401 init_irq_work(&data
->work
, atlas_work_handler
);
403 /* interrupt pin toggles on new conversion */
404 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
405 NULL
, atlas_interrupt_handler
,
406 IRQF_TRIGGER_RISING
|
407 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
411 dev_err(&client
->dev
, "request irq (%d) failed\n", client
->irq
);
412 goto unregister_buffer
;
415 ret
= atlas_set_powermode(data
, 1);
417 dev_err(&client
->dev
, "cannot power device on");
418 goto unregister_buffer
;
421 pm_runtime_enable(&client
->dev
);
422 pm_runtime_set_autosuspend_delay(&client
->dev
, 2500);
423 pm_runtime_use_autosuspend(&client
->dev
);
425 ret
= iio_device_register(indio_dev
);
427 dev_err(&client
->dev
, "unable to register device\n");
434 pm_runtime_disable(&client
->dev
);
435 atlas_set_powermode(data
, 0);
438 iio_triggered_buffer_cleanup(indio_dev
);
441 iio_trigger_unregister(data
->trig
);
446 static int atlas_remove(struct i2c_client
*client
)
448 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
449 struct atlas_data
*data
= iio_priv(indio_dev
);
451 iio_device_unregister(indio_dev
);
452 iio_triggered_buffer_cleanup(indio_dev
);
453 iio_trigger_unregister(data
->trig
);
455 pm_runtime_disable(&client
->dev
);
456 pm_runtime_set_suspended(&client
->dev
);
457 pm_runtime_put_noidle(&client
->dev
);
459 return atlas_set_powermode(data
, 0);
463 static int atlas_runtime_suspend(struct device
*dev
)
465 struct atlas_data
*data
=
466 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
468 return atlas_set_powermode(data
, 0);
471 static int atlas_runtime_resume(struct device
*dev
)
473 struct atlas_data
*data
=
474 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
476 return atlas_set_powermode(data
, 1);
480 static const struct dev_pm_ops atlas_pm_ops
= {
481 SET_RUNTIME_PM_OPS(atlas_runtime_suspend
,
482 atlas_runtime_resume
, NULL
)
485 static const struct i2c_device_id atlas_id
[] = {
486 { "atlas-ph-sm", 0 },
489 MODULE_DEVICE_TABLE(i2c
, atlas_id
);
491 static const struct of_device_id atlas_dt_ids
[] = {
492 { .compatible
= "atlas,ph-sm" },
495 MODULE_DEVICE_TABLE(of
, atlas_dt_ids
);
497 static struct i2c_driver atlas_driver
= {
499 .name
= ATLAS_DRV_NAME
,
500 .of_match_table
= of_match_ptr(atlas_dt_ids
),
503 .probe
= atlas_probe
,
504 .remove
= atlas_remove
,
505 .id_table
= atlas_id
,
507 module_i2c_driver(atlas_driver
);
509 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
510 MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
511 MODULE_LICENSE("GPL");