mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / iio / chemical / atlas-ph-sensor.c
blob62b37cd8fb56d840c66b819df65a48ea56461c61
1 /*
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
57 struct atlas_data {
58 struct i2c_client *client;
59 struct iio_trigger *trig;
60 struct regmap *regmap;
61 struct irq_work work;
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,
79 .reg_bits = 8,
80 .val_bits = 8,
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[] = {
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,
100 IIO_CHAN_SOFT_TIMESTAMP(1),
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
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);
126 int ret;
128 ret = iio_triggered_buffer_postenable(indio_dev);
129 if (ret)
130 return ret;
132 ret = pm_runtime_get_sync(&data->client->dev);
133 if (ret < 0) {
134 pm_runtime_put_noidle(&data->client->dev);
135 return ret;
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);
144 int ret;
146 ret = iio_triggered_buffer_predisable(indio_dev);
147 if (ret)
148 return ret;
150 ret = atlas_set_interrupt(data, false);
151 if (ret)
152 return ret;
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);
179 int ret;
181 ret = regmap_bulk_read(data->regmap, ATLAS_REG_PH_DATA,
182 (u8 *) &data->buffer, sizeof(data->buffer[0]));
184 if (!ret)
185 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
186 iio_get_time_ns());
188 iio_trigger_notify_done(indio_dev->trig);
190 return IRQ_HANDLED;
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);
200 return IRQ_HANDLED;
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);
207 int ret;
209 ret = pm_runtime_get_sync(dev);
210 if (ret < 0) {
211 pm_runtime_put_noidle(dev);
212 return ret;
215 if (suspended)
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);
225 return ret;
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);
234 switch (mask) {
235 case IIO_CHAN_INFO_RAW: {
236 int ret;
237 __be32 reg;
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);
247 if (iio_buffer_enabled(indio_dev))
248 ret = -EBUSY;
249 else
250 ret = atlas_read_ph_measurement(data, &reg);
252 mutex_unlock(&indio_dev->mlock);
253 break;
254 default:
255 ret = -EINVAL;
258 if (!ret) {
259 *val = be32_to_cpu(reg);
260 ret = IIO_VAL_INT;
262 return ret;
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;
277 return IIO_VAL_FRACTIONAL;
280 return -EINVAL;
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)
291 return -EINVAL;
293 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP)
294 return -EINVAL;
296 return regmap_bulk_write(data->regmap, chan->address,
297 &reg, sizeof(reg));
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;
309 int ret;
310 unsigned int val;
312 ret = regmap_read(data->regmap, ATLAS_REG_CALIB_STATUS, &val);
313 if (ret)
314 return ret;
316 if (!(val & ATLAS_REG_CALIB_STATUS_MASK)) {
317 dev_warn(dev, "device has not been calibrated\n");
318 return 0;
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");
330 return 0;
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;
339 int ret;
341 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
342 if (!indio_dev)
343 return -ENOMEM;
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);
355 if (!trig)
356 return -ENOMEM;
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);
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);
374 if (ret)
375 return ret;
377 if (client->irq <= 0) {
378 dev_err(&client->dev, "no valid irq defined\n");
379 return -EINVAL;
382 ret = atlas_check_calibration(data);
383 if (ret)
384 return ret;
386 ret = iio_trigger_register(trig);
387 if (ret) {
388 dev_err(&client->dev, "failed to register trigger\n");
389 return ret;
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;
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,
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;
413 ret = atlas_set_powermode(data, 1);
414 if (ret) {
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);
424 if (ret) {
425 dev_err(&client->dev, "unable to register device\n");
426 goto unregister_pm;
429 return 0;
431 unregister_pm:
432 pm_runtime_disable(&client->dev);
433 atlas_set_powermode(data, 0);
435 unregister_buffer:
436 iio_triggered_buffer_cleanup(indio_dev);
438 unregister_trigger:
439 iio_trigger_unregister(data->trig);
441 return ret;
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);
460 #ifdef CONFIG_PM
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);
476 #endif
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 = {
496 .driver = {
497 .name = ATLAS_DRV_NAME,
498 .of_match_table = of_match_ptr(atlas_dt_ids),
499 .pm = &atlas_pm_ops,
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");