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