dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / light / veml6030.c
blobaa25b87fca8f7da3a0a68d2e6fc298ccc4a99e9e
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * VEML6030 Ambient Light Sensor
5 * Copyright (c) 2019, Rishi Gupta <gupt21@gmail.com>
7 * Datasheet: https://www.vishay.com/docs/84366/veml6030.pdf
8 * Appnote-84367: https://www.vishay.com/docs/84367/designingveml6030.pdf
9 */
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/err.h>
14 #include <linux/regmap.h>
15 #include <linux/interrupt.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/events.h>
21 /* Device registers */
22 #define VEML6030_REG_ALS_CONF 0x00
23 #define VEML6030_REG_ALS_WH 0x01
24 #define VEML6030_REG_ALS_WL 0x02
25 #define VEML6030_REG_ALS_PSM 0x03
26 #define VEML6030_REG_ALS_DATA 0x04
27 #define VEML6030_REG_WH_DATA 0x05
28 #define VEML6030_REG_ALS_INT 0x06
30 /* Bit masks for specific functionality */
31 #define VEML6030_ALS_IT GENMASK(9, 6)
32 #define VEML6030_PSM GENMASK(2, 1)
33 #define VEML6030_ALS_PERS GENMASK(5, 4)
34 #define VEML6030_ALS_GAIN GENMASK(12, 11)
35 #define VEML6030_PSM_EN BIT(0)
36 #define VEML6030_INT_TH_LOW BIT(15)
37 #define VEML6030_INT_TH_HIGH BIT(14)
38 #define VEML6030_ALS_INT_EN BIT(1)
39 #define VEML6030_ALS_SD BIT(0)
42 * The resolution depends on both gain and integration time. The
43 * cur_resolution stores one of the resolution mentioned in the
44 * table during startup and gets updated whenever integration time
45 * or gain is changed.
47 * Table 'resolution and maximum detection range' in appnote 84367
48 * is visualized as a 2D array. The cur_gain stores index of gain
49 * in this table (0-3) while the cur_integration_time holds index
50 * of integration time (0-5).
52 struct veml6030_data {
53 struct i2c_client *client;
54 struct regmap *regmap;
55 int cur_resolution;
56 int cur_gain;
57 int cur_integration_time;
60 /* Integration time available in seconds */
61 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
62 "0.025 0.05 0.1 0.2 0.4 0.8");
65 * Scale is 1/gain. Value 0.125 is ALS gain x (1/8), 0.25 is
66 * ALS gain x (1/4), 1.0 = ALS gain x 1 and 2.0 is ALS gain x 2.
68 static IIO_CONST_ATTR(in_illuminance_scale_available,
69 "0.125 0.25 1.0 2.0");
71 static struct attribute *veml6030_attributes[] = {
72 &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
73 &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
74 NULL
77 static const struct attribute_group veml6030_attr_group = {
78 .attrs = veml6030_attributes,
82 * Persistence = 1/2/4/8 x integration time
83 * Minimum time for which light readings must stay above configured
84 * threshold to assert the interrupt.
86 static const char * const period_values[] = {
87 "0.1 0.2 0.4 0.8",
88 "0.2 0.4 0.8 1.6",
89 "0.4 0.8 1.6 3.2",
90 "0.8 1.6 3.2 6.4",
91 "0.05 0.1 0.2 0.4",
92 "0.025 0.050 0.1 0.2"
96 * Return list of valid period values in seconds corresponding to
97 * the currently active integration time.
99 static ssize_t in_illuminance_period_available_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
102 int ret, reg, x;
103 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
104 struct veml6030_data *data = iio_priv(indio_dev);
106 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, &reg);
107 if (ret) {
108 dev_err(&data->client->dev,
109 "can't read als conf register %d\n", ret);
110 return ret;
113 ret = ((reg >> 6) & 0xF);
114 switch (ret) {
115 case 0:
116 case 1:
117 case 2:
118 case 3:
119 x = ret;
120 break;
121 case 8:
122 x = 4;
123 break;
124 case 12:
125 x = 5;
126 break;
127 default:
128 return -EINVAL;
131 return snprintf(buf, PAGE_SIZE, "%s\n", period_values[x]);
134 static IIO_DEVICE_ATTR_RO(in_illuminance_period_available, 0);
136 static struct attribute *veml6030_event_attributes[] = {
137 &iio_dev_attr_in_illuminance_period_available.dev_attr.attr,
138 NULL
141 static const struct attribute_group veml6030_event_attr_group = {
142 .attrs = veml6030_event_attributes,
145 static int veml6030_als_pwr_on(struct veml6030_data *data)
147 return regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
148 VEML6030_ALS_SD, 0);
151 static int veml6030_als_shut_down(struct veml6030_data *data)
153 return regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
154 VEML6030_ALS_SD, 1);
157 static void veml6030_als_shut_down_action(void *data)
159 veml6030_als_shut_down(data);
162 static const struct iio_event_spec veml6030_event_spec[] = {
164 .type = IIO_EV_TYPE_THRESH,
165 .dir = IIO_EV_DIR_RISING,
166 .mask_separate = BIT(IIO_EV_INFO_VALUE),
167 }, {
168 .type = IIO_EV_TYPE_THRESH,
169 .dir = IIO_EV_DIR_FALLING,
170 .mask_separate = BIT(IIO_EV_INFO_VALUE),
171 }, {
172 .type = IIO_EV_TYPE_THRESH,
173 .dir = IIO_EV_DIR_EITHER,
174 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
175 BIT(IIO_EV_INFO_ENABLE),
179 /* Channel number */
180 enum veml6030_chan {
181 CH_ALS,
182 CH_WHITE,
185 static const struct iio_chan_spec veml6030_channels[] = {
187 .type = IIO_LIGHT,
188 .channel = CH_ALS,
189 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
190 BIT(IIO_CHAN_INFO_PROCESSED) |
191 BIT(IIO_CHAN_INFO_INT_TIME) |
192 BIT(IIO_CHAN_INFO_SCALE),
193 .event_spec = veml6030_event_spec,
194 .num_event_specs = ARRAY_SIZE(veml6030_event_spec),
197 .type = IIO_INTENSITY,
198 .channel = CH_WHITE,
199 .modified = 1,
200 .channel2 = IIO_MOD_LIGHT_BOTH,
201 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
202 BIT(IIO_CHAN_INFO_PROCESSED),
206 static const struct regmap_config veml6030_regmap_config = {
207 .name = "veml6030_regmap",
208 .reg_bits = 8,
209 .val_bits = 16,
210 .max_register = VEML6030_REG_ALS_INT,
211 .val_format_endian = REGMAP_ENDIAN_LITTLE,
214 static int veml6030_get_intgrn_tm(struct iio_dev *indio_dev,
215 int *val, int *val2)
217 int ret, reg;
218 struct veml6030_data *data = iio_priv(indio_dev);
220 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, &reg);
221 if (ret) {
222 dev_err(&data->client->dev,
223 "can't read als conf register %d\n", ret);
224 return ret;
227 switch ((reg >> 6) & 0xF) {
228 case 0:
229 *val2 = 100000;
230 break;
231 case 1:
232 *val2 = 200000;
233 break;
234 case 2:
235 *val2 = 400000;
236 break;
237 case 3:
238 *val2 = 800000;
239 break;
240 case 8:
241 *val2 = 50000;
242 break;
243 case 12:
244 *val2 = 25000;
245 break;
246 default:
247 return -EINVAL;
250 *val = 0;
251 return IIO_VAL_INT_PLUS_MICRO;
254 static int veml6030_set_intgrn_tm(struct iio_dev *indio_dev,
255 int val, int val2)
257 int ret, new_int_time, int_idx;
258 struct veml6030_data *data = iio_priv(indio_dev);
260 if (val)
261 return -EINVAL;
263 switch (val2) {
264 case 25000:
265 new_int_time = 0x300;
266 int_idx = 5;
267 break;
268 case 50000:
269 new_int_time = 0x200;
270 int_idx = 4;
271 break;
272 case 100000:
273 new_int_time = 0x00;
274 int_idx = 3;
275 break;
276 case 200000:
277 new_int_time = 0x40;
278 int_idx = 2;
279 break;
280 case 400000:
281 new_int_time = 0x80;
282 int_idx = 1;
283 break;
284 case 800000:
285 new_int_time = 0xC0;
286 int_idx = 0;
287 break;
288 default:
289 return -EINVAL;
292 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
293 VEML6030_ALS_IT, new_int_time);
294 if (ret) {
295 dev_err(&data->client->dev,
296 "can't update als integration time %d\n", ret);
297 return ret;
301 * Cache current integration time and update resolution. For every
302 * increase in integration time to next level, resolution is halved
303 * and vice-versa.
305 if (data->cur_integration_time < int_idx)
306 data->cur_resolution <<= int_idx - data->cur_integration_time;
307 else if (data->cur_integration_time > int_idx)
308 data->cur_resolution >>= data->cur_integration_time - int_idx;
310 data->cur_integration_time = int_idx;
312 return ret;
315 static int veml6030_read_persistence(struct iio_dev *indio_dev,
316 int *val, int *val2)
318 int ret, reg, period, x, y;
319 struct veml6030_data *data = iio_priv(indio_dev);
321 ret = veml6030_get_intgrn_tm(indio_dev, &x, &y);
322 if (ret < 0)
323 return ret;
325 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, &reg);
326 if (ret) {
327 dev_err(&data->client->dev,
328 "can't read als conf register %d\n", ret);
331 /* integration time multiplied by 1/2/4/8 */
332 period = y * (1 << ((reg >> 4) & 0x03));
334 *val = period / 1000000;
335 *val2 = period % 1000000;
337 return IIO_VAL_INT_PLUS_MICRO;
340 static int veml6030_write_persistence(struct iio_dev *indio_dev,
341 int val, int val2)
343 int ret, period, x, y;
344 struct veml6030_data *data = iio_priv(indio_dev);
346 ret = veml6030_get_intgrn_tm(indio_dev, &x, &y);
347 if (ret < 0)
348 return ret;
350 if (!val) {
351 period = val2 / y;
352 } else {
353 if ((val == 1) && (val2 == 600000))
354 period = 1600000 / y;
355 else if ((val == 3) && (val2 == 200000))
356 period = 3200000 / y;
357 else if ((val == 6) && (val2 == 400000))
358 period = 6400000 / y;
359 else
360 period = -1;
363 if (period <= 0 || period > 8 || hweight8(period) != 1)
364 return -EINVAL;
366 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
367 VEML6030_ALS_PERS, (ffs(period) - 1) << 4);
368 if (ret)
369 dev_err(&data->client->dev,
370 "can't set persistence value %d\n", ret);
372 return ret;
375 static int veml6030_set_als_gain(struct iio_dev *indio_dev,
376 int val, int val2)
378 int ret, new_gain, gain_idx;
379 struct veml6030_data *data = iio_priv(indio_dev);
381 if (val == 0 && val2 == 125000) {
382 new_gain = 0x1000; /* 0x02 << 11 */
383 gain_idx = 3;
384 } else if (val == 0 && val2 == 250000) {
385 new_gain = 0x1800;
386 gain_idx = 2;
387 } else if (val == 1 && val2 == 0) {
388 new_gain = 0x00;
389 gain_idx = 1;
390 } else if (val == 2 && val2 == 0) {
391 new_gain = 0x800;
392 gain_idx = 0;
393 } else {
394 return -EINVAL;
397 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
398 VEML6030_ALS_GAIN, new_gain);
399 if (ret) {
400 dev_err(&data->client->dev,
401 "can't set als gain %d\n", ret);
402 return ret;
406 * Cache currently set gain & update resolution. For every
407 * increase in the gain to next level, resolution is halved
408 * and vice-versa.
410 if (data->cur_gain < gain_idx)
411 data->cur_resolution <<= gain_idx - data->cur_gain;
412 else if (data->cur_gain > gain_idx)
413 data->cur_resolution >>= data->cur_gain - gain_idx;
415 data->cur_gain = gain_idx;
417 return ret;
420 static int veml6030_get_als_gain(struct iio_dev *indio_dev,
421 int *val, int *val2)
423 int ret, reg;
424 struct veml6030_data *data = iio_priv(indio_dev);
426 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, &reg);
427 if (ret) {
428 dev_err(&data->client->dev,
429 "can't read als conf register %d\n", ret);
430 return ret;
433 switch ((reg >> 11) & 0x03) {
434 case 0:
435 *val = 1;
436 *val2 = 0;
437 break;
438 case 1:
439 *val = 2;
440 *val2 = 0;
441 break;
442 case 2:
443 *val = 0;
444 *val2 = 125000;
445 break;
446 case 3:
447 *val = 0;
448 *val2 = 250000;
449 break;
450 default:
451 return -EINVAL;
454 return IIO_VAL_INT_PLUS_MICRO;
457 static int veml6030_read_thresh(struct iio_dev *indio_dev,
458 int *val, int *val2, int dir)
460 int ret, reg;
461 struct veml6030_data *data = iio_priv(indio_dev);
463 if (dir == IIO_EV_DIR_RISING)
464 ret = regmap_read(data->regmap, VEML6030_REG_ALS_WH, &reg);
465 else
466 ret = regmap_read(data->regmap, VEML6030_REG_ALS_WL, &reg);
467 if (ret) {
468 dev_err(&data->client->dev,
469 "can't read als threshold value %d\n", ret);
470 return ret;
473 *val = reg & 0xffff;
474 return IIO_VAL_INT;
477 static int veml6030_write_thresh(struct iio_dev *indio_dev,
478 int val, int val2, int dir)
480 int ret;
481 struct veml6030_data *data = iio_priv(indio_dev);
483 if (val > 0xFFFF || val < 0 || val2)
484 return -EINVAL;
486 if (dir == IIO_EV_DIR_RISING) {
487 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WH, val);
488 if (ret)
489 dev_err(&data->client->dev,
490 "can't set high threshold %d\n", ret);
491 } else {
492 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WL, val);
493 if (ret)
494 dev_err(&data->client->dev,
495 "can't set low threshold %d\n", ret);
498 return ret;
502 * Provide both raw as well as light reading in lux.
503 * light (in lux) = resolution * raw reading
505 static int veml6030_read_raw(struct iio_dev *indio_dev,
506 struct iio_chan_spec const *chan, int *val,
507 int *val2, long mask)
509 int ret, reg;
510 struct veml6030_data *data = iio_priv(indio_dev);
511 struct regmap *regmap = data->regmap;
512 struct device *dev = &data->client->dev;
514 switch (mask) {
515 case IIO_CHAN_INFO_RAW:
516 case IIO_CHAN_INFO_PROCESSED:
517 switch (chan->type) {
518 case IIO_LIGHT:
519 ret = regmap_read(regmap, VEML6030_REG_ALS_DATA, &reg);
520 if (ret < 0) {
521 dev_err(dev, "can't read als data %d\n", ret);
522 return ret;
524 if (mask == IIO_CHAN_INFO_PROCESSED) {
525 *val = (reg * data->cur_resolution) / 10000;
526 *val2 = (reg * data->cur_resolution) % 10000;
527 return IIO_VAL_INT_PLUS_MICRO;
529 *val = reg;
530 return IIO_VAL_INT;
531 case IIO_INTENSITY:
532 ret = regmap_read(regmap, VEML6030_REG_WH_DATA, &reg);
533 if (ret < 0) {
534 dev_err(dev, "can't read white data %d\n", ret);
535 return ret;
537 if (mask == IIO_CHAN_INFO_PROCESSED) {
538 *val = (reg * data->cur_resolution) / 10000;
539 *val2 = (reg * data->cur_resolution) % 10000;
540 return IIO_VAL_INT_PLUS_MICRO;
542 *val = reg;
543 return IIO_VAL_INT;
544 default:
545 return -EINVAL;
547 case IIO_CHAN_INFO_INT_TIME:
548 if (chan->type == IIO_LIGHT)
549 return veml6030_get_intgrn_tm(indio_dev, val, val2);
550 return -EINVAL;
551 case IIO_CHAN_INFO_SCALE:
552 if (chan->type == IIO_LIGHT)
553 return veml6030_get_als_gain(indio_dev, val, val2);
554 return -EINVAL;
555 default:
556 return -EINVAL;
560 static int veml6030_write_raw(struct iio_dev *indio_dev,
561 struct iio_chan_spec const *chan,
562 int val, int val2, long mask)
564 switch (mask) {
565 case IIO_CHAN_INFO_INT_TIME:
566 switch (chan->type) {
567 case IIO_LIGHT:
568 return veml6030_set_intgrn_tm(indio_dev, val, val2);
569 default:
570 return -EINVAL;
572 case IIO_CHAN_INFO_SCALE:
573 switch (chan->type) {
574 case IIO_LIGHT:
575 return veml6030_set_als_gain(indio_dev, val, val2);
576 default:
577 return -EINVAL;
579 default:
580 return -EINVAL;
584 static int veml6030_read_event_val(struct iio_dev *indio_dev,
585 const struct iio_chan_spec *chan, enum iio_event_type type,
586 enum iio_event_direction dir, enum iio_event_info info,
587 int *val, int *val2)
589 switch (info) {
590 case IIO_EV_INFO_VALUE:
591 switch (dir) {
592 case IIO_EV_DIR_RISING:
593 case IIO_EV_DIR_FALLING:
594 return veml6030_read_thresh(indio_dev, val, val2, dir);
595 default:
596 return -EINVAL;
598 break;
599 case IIO_EV_INFO_PERIOD:
600 return veml6030_read_persistence(indio_dev, val, val2);
601 default:
602 return -EINVAL;
606 static int veml6030_write_event_val(struct iio_dev *indio_dev,
607 const struct iio_chan_spec *chan, enum iio_event_type type,
608 enum iio_event_direction dir, enum iio_event_info info,
609 int val, int val2)
611 switch (info) {
612 case IIO_EV_INFO_VALUE:
613 return veml6030_write_thresh(indio_dev, val, val2, dir);
614 case IIO_EV_INFO_PERIOD:
615 return veml6030_write_persistence(indio_dev, val, val2);
616 default:
617 return -EINVAL;
621 static int veml6030_read_interrupt_config(struct iio_dev *indio_dev,
622 const struct iio_chan_spec *chan, enum iio_event_type type,
623 enum iio_event_direction dir)
625 int ret, reg;
626 struct veml6030_data *data = iio_priv(indio_dev);
628 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, &reg);
629 if (ret) {
630 dev_err(&data->client->dev,
631 "can't read als conf register %d\n", ret);
632 return ret;
635 if (reg & VEML6030_ALS_INT_EN)
636 return 1;
637 else
638 return 0;
642 * Sensor should not be measuring light when interrupt is configured.
643 * Therefore correct sequence to configure interrupt functionality is:
644 * shut down -> enable/disable interrupt -> power on
646 * state = 1 enables interrupt, state = 0 disables interrupt
648 static int veml6030_write_interrupt_config(struct iio_dev *indio_dev,
649 const struct iio_chan_spec *chan, enum iio_event_type type,
650 enum iio_event_direction dir, int state)
652 int ret;
653 struct veml6030_data *data = iio_priv(indio_dev);
655 if (state < 0 || state > 1)
656 return -EINVAL;
658 ret = veml6030_als_shut_down(data);
659 if (ret < 0) {
660 dev_err(&data->client->dev,
661 "can't disable als to configure interrupt %d\n", ret);
662 return ret;
665 /* enable interrupt + power on */
666 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
667 VEML6030_ALS_INT_EN | VEML6030_ALS_SD, state << 1);
668 if (ret)
669 dev_err(&data->client->dev,
670 "can't enable interrupt & poweron als %d\n", ret);
672 return ret;
675 static const struct iio_info veml6030_info = {
676 .read_raw = veml6030_read_raw,
677 .write_raw = veml6030_write_raw,
678 .read_event_value = veml6030_read_event_val,
679 .write_event_value = veml6030_write_event_val,
680 .read_event_config = veml6030_read_interrupt_config,
681 .write_event_config = veml6030_write_interrupt_config,
682 .attrs = &veml6030_attr_group,
683 .event_attrs = &veml6030_event_attr_group,
686 static const struct iio_info veml6030_info_no_irq = {
687 .read_raw = veml6030_read_raw,
688 .write_raw = veml6030_write_raw,
689 .attrs = &veml6030_attr_group,
692 static irqreturn_t veml6030_event_handler(int irq, void *private)
694 int ret, reg, evtdir;
695 struct iio_dev *indio_dev = private;
696 struct veml6030_data *data = iio_priv(indio_dev);
698 ret = regmap_read(data->regmap, VEML6030_REG_ALS_INT, &reg);
699 if (ret) {
700 dev_err(&data->client->dev,
701 "can't read als interrupt register %d\n", ret);
702 return IRQ_HANDLED;
705 /* Spurious interrupt handling */
706 if (!(reg & (VEML6030_INT_TH_HIGH | VEML6030_INT_TH_LOW)))
707 return IRQ_NONE;
709 if (reg & VEML6030_INT_TH_HIGH)
710 evtdir = IIO_EV_DIR_RISING;
711 else
712 evtdir = IIO_EV_DIR_FALLING;
714 iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
715 0, IIO_EV_TYPE_THRESH, evtdir),
716 iio_get_time_ns(indio_dev));
718 return IRQ_HANDLED;
722 * Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2,
723 * persistence to 1 x integration time and the threshold
724 * interrupt disabled by default. First shutdown the sensor,
725 * update registers and then power on the sensor.
727 static int veml6030_hw_init(struct iio_dev *indio_dev)
729 int ret, val;
730 struct veml6030_data *data = iio_priv(indio_dev);
731 struct i2c_client *client = data->client;
733 ret = veml6030_als_shut_down(data);
734 if (ret) {
735 dev_err(&client->dev, "can't shutdown als %d\n", ret);
736 return ret;
739 ret = regmap_write(data->regmap, VEML6030_REG_ALS_CONF, 0x1001);
740 if (ret) {
741 dev_err(&client->dev, "can't setup als configs %d\n", ret);
742 return ret;
745 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_PSM,
746 VEML6030_PSM | VEML6030_PSM_EN, 0x03);
747 if (ret) {
748 dev_err(&client->dev, "can't setup default PSM %d\n", ret);
749 return ret;
752 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WH, 0xFFFF);
753 if (ret) {
754 dev_err(&client->dev, "can't setup high threshold %d\n", ret);
755 return ret;
758 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WL, 0x0000);
759 if (ret) {
760 dev_err(&client->dev, "can't setup low threshold %d\n", ret);
761 return ret;
764 ret = veml6030_als_pwr_on(data);
765 if (ret) {
766 dev_err(&client->dev, "can't poweron als %d\n", ret);
767 return ret;
770 /* Wait 4 ms to let processor & oscillator start correctly */
771 usleep_range(4000, 4002);
773 /* Clear stale interrupt status bits if any during start */
774 ret = regmap_read(data->regmap, VEML6030_REG_ALS_INT, &val);
775 if (ret < 0) {
776 dev_err(&client->dev,
777 "can't clear als interrupt status %d\n", ret);
778 return ret;
781 /* Cache currently active measurement parameters */
782 data->cur_gain = 3;
783 data->cur_resolution = 4608;
784 data->cur_integration_time = 3;
786 return ret;
789 static int veml6030_probe(struct i2c_client *client,
790 const struct i2c_device_id *id)
792 int ret;
793 struct veml6030_data *data;
794 struct iio_dev *indio_dev;
795 struct regmap *regmap;
797 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
798 dev_err(&client->dev, "i2c adapter doesn't support plain i2c\n");
799 return -EOPNOTSUPP;
802 regmap = devm_regmap_init_i2c(client, &veml6030_regmap_config);
803 if (IS_ERR(regmap)) {
804 dev_err(&client->dev, "can't setup regmap\n");
805 return PTR_ERR(regmap);
808 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
809 if (!indio_dev)
810 return -ENOMEM;
812 data = iio_priv(indio_dev);
813 i2c_set_clientdata(client, indio_dev);
814 data->client = client;
815 data->regmap = regmap;
817 indio_dev->dev.parent = &client->dev;
818 indio_dev->name = "veml6030";
819 indio_dev->channels = veml6030_channels;
820 indio_dev->num_channels = ARRAY_SIZE(veml6030_channels);
821 indio_dev->modes = INDIO_DIRECT_MODE;
823 if (client->irq) {
824 ret = devm_request_threaded_irq(&client->dev, client->irq,
825 NULL, veml6030_event_handler,
826 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
827 "veml6030", indio_dev);
828 if (ret < 0) {
829 dev_err(&client->dev,
830 "irq %d request failed\n", client->irq);
831 return ret;
833 indio_dev->info = &veml6030_info;
834 } else {
835 indio_dev->info = &veml6030_info_no_irq;
838 ret = veml6030_hw_init(indio_dev);
839 if (ret < 0)
840 return ret;
842 ret = devm_add_action_or_reset(&client->dev,
843 veml6030_als_shut_down_action, data);
844 if (ret < 0)
845 return ret;
847 return devm_iio_device_register(&client->dev, indio_dev);
850 static int __maybe_unused veml6030_runtime_suspend(struct device *dev)
852 int ret;
853 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
854 struct veml6030_data *data = iio_priv(indio_dev);
856 ret = veml6030_als_shut_down(data);
857 if (ret < 0)
858 dev_err(&data->client->dev, "can't suspend als %d\n", ret);
860 return ret;
863 static int __maybe_unused veml6030_runtime_resume(struct device *dev)
865 int ret;
866 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
867 struct veml6030_data *data = iio_priv(indio_dev);
869 ret = veml6030_als_pwr_on(data);
870 if (ret < 0)
871 dev_err(&data->client->dev, "can't resume als %d\n", ret);
873 return ret;
876 static const struct dev_pm_ops veml6030_pm_ops = {
877 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
878 pm_runtime_force_resume)
879 SET_RUNTIME_PM_OPS(veml6030_runtime_suspend,
880 veml6030_runtime_resume, NULL)
883 static const struct of_device_id veml6030_of_match[] = {
884 { .compatible = "vishay,veml6030" },
887 MODULE_DEVICE_TABLE(of, veml6030_of_match);
889 static const struct i2c_device_id veml6030_id[] = {
890 { "veml6030", 0 },
893 MODULE_DEVICE_TABLE(i2c, veml6030_id);
895 static struct i2c_driver veml6030_driver = {
896 .driver = {
897 .name = "veml6030",
898 .of_match_table = veml6030_of_match,
899 .pm = &veml6030_pm_ops,
901 .probe = veml6030_probe,
902 .id_table = veml6030_id,
904 module_i2c_driver(veml6030_driver);
906 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
907 MODULE_DESCRIPTION("VEML6030 Ambient Light Sensor");
908 MODULE_LICENSE("GPL v2");