blob: 62b37cd8fb56d840c66b819df65a48ea56461c61 [file] [log] [blame]
Matt Ranostay27dec002016-01-26 18:34:31 -08001/*
2 * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor
3 *
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
5 *
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.
10 *
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.
15 */
16
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>
34
35#define ATLAS_REGMAP_NAME "atlas_ph_regmap"
36#define ATLAS_DRV_NAME "atlas_ph"
37
38#define ATLAS_REG_DEV_TYPE 0x00
39#define ATLAS_REG_DEV_VERSION 0x01
40
41#define ATLAS_REG_INT_CONTROL 0x04
42#define ATLAS_REG_INT_CONTROL_EN BIT(3)
43
44#define ATLAS_REG_PWR_CONTROL 0x06
45
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)
51
52#define ATLAS_REG_TEMP_DATA 0x0e
53#define ATLAS_REG_PH_DATA 0x16
54
55#define ATLAS_PH_INT_TIME_IN_US 450000
56
57struct atlas_data {
58 struct i2c_client *client;
59 struct iio_trigger *trig;
60 struct regmap *regmap;
61 struct irq_work work;
62
63 __be32 buffer[4]; /* 32-bit pH data + 32-bit pad + 64-bit timestamp */
64};
65
66static const struct regmap_range atlas_volatile_ranges[] = {
67 regmap_reg_range(ATLAS_REG_INT_CONTROL, ATLAS_REG_INT_CONTROL),
Matt Ranostay27dec002016-01-26 18:34:31 -080068 regmap_reg_range(ATLAS_REG_PH_DATA, ATLAS_REG_PH_DATA + 4),
69};
70
71static const struct regmap_access_table atlas_volatile_table = {
72 .yes_ranges = atlas_volatile_ranges,
73 .n_yes_ranges = ARRAY_SIZE(atlas_volatile_ranges),
74};
75
76static const struct regmap_config atlas_regmap_config = {
77 .name = ATLAS_REGMAP_NAME,
78
79 .reg_bits = 8,
80 .val_bits = 8,
81
82 .volatile_table = &atlas_volatile_table,
83 .max_register = ATLAS_REG_PH_DATA + 4,
Matt Ranostay0d43b342016-02-13 17:20:42 -080084 .cache_type = REGCACHE_RBTREE,
Matt Ranostay27dec002016-01-26 18:34:31 -080085};
86
87static const struct iio_chan_spec atlas_channels[] = {
88 {
89 .type = IIO_PH,
90 .info_mask_separate =
91 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
92 .scan_index = 0,
93 .scan_type = {
94 .sign = 'u',
95 .realbits = 32,
96 .storagebits = 32,
97 .endianness = IIO_BE,
98 },
99 },
100 IIO_CHAN_SOFT_TIMESTAMP(1),
101 {
102 .type = IIO_TEMP,
103 .address = ATLAS_REG_TEMP_DATA,
104 .info_mask_separate =
105 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
106 .output = 1,
107 .scan_index = -1
108 },
109};
110
111static int atlas_set_powermode(struct atlas_data *data, int on)
112{
113 return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on);
114}
115
116static int atlas_set_interrupt(struct atlas_data *data, bool state)
117{
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);
121}
122
123static int atlas_buffer_postenable(struct iio_dev *indio_dev)
124{
125 struct atlas_data *data = iio_priv(indio_dev);
126 int ret;
127
128 ret = iio_triggered_buffer_postenable(indio_dev);
129 if (ret)
130 return ret;
131
132 ret = pm_runtime_get_sync(&data->client->dev);
133 if (ret < 0) {
134 pm_runtime_put_noidle(&data->client->dev);
135 return ret;
136 }
137
138 return atlas_set_interrupt(data, true);
139}
140
141static int atlas_buffer_predisable(struct iio_dev *indio_dev)
142{
143 struct atlas_data *data = iio_priv(indio_dev);
144 int ret;
145
146 ret = iio_triggered_buffer_predisable(indio_dev);
147 if (ret)
148 return ret;
149
150 ret = atlas_set_interrupt(data, false);
151 if (ret)
152 return ret;
153
154 pm_runtime_mark_last_busy(&data->client->dev);
155 return pm_runtime_put_autosuspend(&data->client->dev);
156}
157
158static const struct iio_trigger_ops atlas_interrupt_trigger_ops = {
159 .owner = THIS_MODULE,
160};
161
162static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = {
163 .postenable = atlas_buffer_postenable,
164 .predisable = atlas_buffer_predisable,
165};
166
167static void atlas_work_handler(struct irq_work *work)
168{
169 struct atlas_data *data = container_of(work, struct atlas_data, work);
170
171 iio_trigger_poll(data->trig);
172}
173
174static irqreturn_t atlas_trigger_handler(int irq, void *private)
175{
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);
179 int ret;
180
Matt Ranostay98e55e92016-02-14 18:59:07 -0800181 ret = regmap_bulk_read(data->regmap, ATLAS_REG_PH_DATA,
182 (u8 *) &data->buffer, sizeof(data->buffer[0]));
Matt Ranostay27dec002016-01-26 18:34:31 -0800183
Matt Ranostay98e55e92016-02-14 18:59:07 -0800184 if (!ret)
Matt Ranostay27dec002016-01-26 18:34:31 -0800185 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
186 iio_get_time_ns());
187
188 iio_trigger_notify_done(indio_dev->trig);
189
190 return IRQ_HANDLED;
191}
192
193static irqreturn_t atlas_interrupt_handler(int irq, void *private)
194{
195 struct iio_dev *indio_dev = private;
196 struct atlas_data *data = iio_priv(indio_dev);
197
198 irq_work_queue(&data->work);
199
200 return IRQ_HANDLED;
201}
202
203static int atlas_read_ph_measurement(struct atlas_data *data, __be32 *val)
204{
205 struct device *dev = &data->client->dev;
206 int suspended = pm_runtime_suspended(dev);
207 int ret;
208
209 ret = pm_runtime_get_sync(dev);
210 if (ret < 0) {
211 pm_runtime_put_noidle(dev);
212 return ret;
213 }
214
215 if (suspended)
216 usleep_range(ATLAS_PH_INT_TIME_IN_US,
217 ATLAS_PH_INT_TIME_IN_US + 100000);
218
219 ret = regmap_bulk_read(data->regmap, ATLAS_REG_PH_DATA,
220 (u8 *) val, sizeof(*val));
221
222 pm_runtime_mark_last_busy(dev);
223 pm_runtime_put_autosuspend(dev);
224
225 return ret;
226}
227
228static int atlas_read_raw(struct iio_dev *indio_dev,
229 struct iio_chan_spec const *chan,
230 int *val, int *val2, long mask)
231{
232 struct atlas_data *data = iio_priv(indio_dev);
233
234 switch (mask) {
235 case IIO_CHAN_INFO_RAW: {
236 int ret;
237 __be32 reg;
238
239 switch (chan->type) {
240 case IIO_TEMP:
241 ret = regmap_bulk_read(data->regmap, chan->address,
242 (u8 *) &reg, sizeof(reg));
243 break;
244 case IIO_PH:
245 mutex_lock(&indio_dev->mlock);
246
247 if (iio_buffer_enabled(indio_dev))
248 ret = -EBUSY;
249 else
250 ret = atlas_read_ph_measurement(data, &reg);
251
252 mutex_unlock(&indio_dev->mlock);
253 break;
254 default:
255 ret = -EINVAL;
256 }
257
258 if (!ret) {
259 *val = be32_to_cpu(reg);
260 ret = IIO_VAL_INT;
261 }
262 return ret;
263 }
264 case IIO_CHAN_INFO_SCALE:
265 switch (chan->type) {
266 case IIO_TEMP:
267 *val = 1; /* 0.01 */
268 *val2 = 100;
269 break;
270 case IIO_PH:
271 *val = 1; /* 0.001 */
272 *val2 = 1000;
273 break;
274 default:
275 return -EINVAL;
276 }
277 return IIO_VAL_FRACTIONAL;
278 }
279
280 return -EINVAL;
281}
282
283static int atlas_write_raw(struct iio_dev *indio_dev,
284 struct iio_chan_spec const *chan,
285 int val, int val2, long mask)
286{
287 struct atlas_data *data = iio_priv(indio_dev);
288 __be32 reg = cpu_to_be32(val);
289
290 if (val2 != 0 || val < 0 || val > 20000)
291 return -EINVAL;
292
293 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
294 return -EINVAL;
295
296 return regmap_bulk_write(data->regmap, chan->address,
297 &reg, sizeof(reg));
298}
299
300static const struct iio_info atlas_info = {
301 .driver_module = THIS_MODULE,
302 .read_raw = atlas_read_raw,
303 .write_raw = atlas_write_raw,
304};
305
306static int atlas_check_calibration(struct atlas_data *data)
307{
308 struct device *dev = &data->client->dev;
309 int ret;
310 unsigned int val;
311
312 ret = regmap_read(data->regmap, ATLAS_REG_CALIB_STATUS, &val);
313 if (ret)
314 return ret;
315
316 if (!(val & ATLAS_REG_CALIB_STATUS_MASK)) {
317 dev_warn(dev, "device has not been calibrated\n");
318 return 0;
319 }
320
321 if (!(val & ATLAS_REG_CALIB_STATUS_LOW))
322 dev_warn(dev, "device missing low point calibration\n");
323
324 if (!(val & ATLAS_REG_CALIB_STATUS_MID))
325 dev_warn(dev, "device missing mid point calibration\n");
326
327 if (!(val & ATLAS_REG_CALIB_STATUS_HIGH))
328 dev_warn(dev, "device missing high point calibration\n");
329
330 return 0;
331};
332
333static int atlas_probe(struct i2c_client *client,
334 const struct i2c_device_id *id)
335{
336 struct atlas_data *data;
337 struct iio_trigger *trig;
338 struct iio_dev *indio_dev;
339 int ret;
340
341 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
342 if (!indio_dev)
343 return -ENOMEM;
344
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;
351
352 trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
353 indio_dev->name, indio_dev->id);
354
355 if (!trig)
356 return -ENOMEM;
357
358 data = iio_priv(indio_dev);
359 data->client = client;
360 data->trig = trig;
361 trig->dev.parent = indio_dev->dev.parent;
362 trig->ops = &atlas_interrupt_trigger_ops;
363 iio_trigger_set_drvdata(trig, indio_dev);
364
365 i2c_set_clientdata(client, indio_dev);
366
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);
371 }
372
373 ret = pm_runtime_set_active(&client->dev);
374 if (ret)
375 return ret;
376
377 if (client->irq <= 0) {
378 dev_err(&client->dev, "no valid irq defined\n");
379 return -EINVAL;
380 }
381
382 ret = atlas_check_calibration(data);
383 if (ret)
384 return ret;
385
386 ret = iio_trigger_register(trig);
387 if (ret) {
388 dev_err(&client->dev, "failed to register trigger\n");
389 return ret;
390 }
391
392 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
393 &atlas_trigger_handler, &atlas_buffer_setup_ops);
394 if (ret) {
395 dev_err(&client->dev, "cannot setup iio trigger\n");
396 goto unregister_trigger;
397 }
398
399 init_irq_work(&data->work, atlas_work_handler);
400
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,
406 "atlas_irq",
407 indio_dev);
408 if (ret) {
409 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
410 goto unregister_buffer;
411 }
412
413 ret = atlas_set_powermode(data, 1);
414 if (ret) {
415 dev_err(&client->dev, "cannot power device on");
416 goto unregister_buffer;
417 }
418
419 pm_runtime_enable(&client->dev);
420 pm_runtime_set_autosuspend_delay(&client->dev, 2500);
421 pm_runtime_use_autosuspend(&client->dev);
422
423 ret = iio_device_register(indio_dev);
424 if (ret) {
425 dev_err(&client->dev, "unable to register device\n");
426 goto unregister_pm;
427 }
428
429 return 0;
430
431unregister_pm:
432 pm_runtime_disable(&client->dev);
433 atlas_set_powermode(data, 0);
434
435unregister_buffer:
436 iio_triggered_buffer_cleanup(indio_dev);
437
438unregister_trigger:
439 iio_trigger_unregister(data->trig);
440
441 return ret;
442}
443
444static int atlas_remove(struct i2c_client *client)
445{
446 struct iio_dev *indio_dev = i2c_get_clientdata(client);
447 struct atlas_data *data = iio_priv(indio_dev);
448
449 iio_device_unregister(indio_dev);
450 iio_triggered_buffer_cleanup(indio_dev);
451 iio_trigger_unregister(data->trig);
452
453 pm_runtime_disable(&client->dev);
454 pm_runtime_set_suspended(&client->dev);
455 pm_runtime_put_noidle(&client->dev);
456
457 return atlas_set_powermode(data, 0);
458}
459
460#ifdef CONFIG_PM
461static int atlas_runtime_suspend(struct device *dev)
462{
463 struct atlas_data *data =
464 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
465
466 return atlas_set_powermode(data, 0);
467}
468
469static int atlas_runtime_resume(struct device *dev)
470{
471 struct atlas_data *data =
472 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
473
474 return atlas_set_powermode(data, 1);
475}
476#endif
477
478static const struct dev_pm_ops atlas_pm_ops = {
479 SET_RUNTIME_PM_OPS(atlas_runtime_suspend,
480 atlas_runtime_resume, NULL)
481};
482
483static const struct i2c_device_id atlas_id[] = {
484 { "atlas-ph-sm", 0 },
485 {}
486};
487MODULE_DEVICE_TABLE(i2c, atlas_id);
488
489static const struct of_device_id atlas_dt_ids[] = {
490 { .compatible = "atlas,ph-sm" },
491 { }
492};
493MODULE_DEVICE_TABLE(of, atlas_dt_ids);
494
495static struct i2c_driver atlas_driver = {
496 .driver = {
497 .name = ATLAS_DRV_NAME,
498 .of_match_table = of_match_ptr(atlas_dt_ids),
499 .pm = &atlas_pm_ops,
500 },
501 .probe = atlas_probe,
502 .remove = atlas_remove,
503 .id_table = atlas_id,
504};
505module_i2c_driver(atlas_driver);
506
507MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
508MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor");
509MODULE_LICENSE("GPL");