Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / light / rpr0521.c
blobffe9ce798ea2d73b2cd952afb1ec8d0647876473
1 /*
2 * RPR-0521 ROHM Ambient Light and Proximity Sensor
4 * Copyright (c) 2015, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for RPR-0521RS (7-bit I2C slave address 0x38).
12 * TODO: illuminance channel
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/delay.h>
20 #include <linux/acpi.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/pm_runtime.h>
30 #define RPR0521_REG_SYSTEM_CTRL 0x40
31 #define RPR0521_REG_MODE_CTRL 0x41
32 #define RPR0521_REG_ALS_CTRL 0x42
33 #define RPR0521_REG_PXS_CTRL 0x43
34 #define RPR0521_REG_PXS_DATA 0x44 /* 16-bit, little endian */
35 #define RPR0521_REG_ALS_DATA0 0x46 /* 16-bit, little endian */
36 #define RPR0521_REG_ALS_DATA1 0x48 /* 16-bit, little endian */
37 #define RPR0521_REG_INTERRUPT 0x4A
38 #define RPR0521_REG_PS_OFFSET_LSB 0x53
39 #define RPR0521_REG_ID 0x92
41 #define RPR0521_MODE_ALS_MASK BIT(7)
42 #define RPR0521_MODE_PXS_MASK BIT(6)
43 #define RPR0521_MODE_MEAS_TIME_MASK GENMASK(3, 0)
44 #define RPR0521_ALS_DATA0_GAIN_MASK GENMASK(5, 4)
45 #define RPR0521_ALS_DATA0_GAIN_SHIFT 4
46 #define RPR0521_ALS_DATA1_GAIN_MASK GENMASK(3, 2)
47 #define RPR0521_ALS_DATA1_GAIN_SHIFT 2
48 #define RPR0521_PXS_GAIN_MASK GENMASK(5, 4)
49 #define RPR0521_PXS_GAIN_SHIFT 4
50 #define RPR0521_PXS_PERSISTENCE_MASK GENMASK(3, 0)
51 #define RPR0521_INTERRUPT_INT_TRIG_PS_MASK BIT(0)
52 #define RPR0521_INTERRUPT_INT_TRIG_ALS_MASK BIT(1)
53 #define RPR0521_INTERRUPT_INT_REASSERT_MASK BIT(3)
54 #define RPR0521_INTERRUPT_ALS_INT_STATUS_MASK BIT(6)
55 #define RPR0521_INTERRUPT_PS_INT_STATUS_MASK BIT(7)
57 #define RPR0521_MODE_ALS_ENABLE BIT(7)
58 #define RPR0521_MODE_ALS_DISABLE 0x00
59 #define RPR0521_MODE_PXS_ENABLE BIT(6)
60 #define RPR0521_MODE_PXS_DISABLE 0x00
61 #define RPR0521_PXS_PERSISTENCE_DRDY 0x00
63 #define RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE BIT(0)
64 #define RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE 0x00
65 #define RPR0521_INTERRUPT_INT_TRIG_ALS_ENABLE BIT(1)
66 #define RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE 0x00
67 #define RPR0521_INTERRUPT_INT_REASSERT_ENABLE BIT(3)
68 #define RPR0521_INTERRUPT_INT_REASSERT_DISABLE 0x00
70 #define RPR0521_MANUFACT_ID 0xE0
71 #define RPR0521_DEFAULT_MEAS_TIME 0x06 /* ALS - 100ms, PXS - 100ms */
73 #define RPR0521_DRV_NAME "RPR0521"
74 #define RPR0521_IRQ_NAME "rpr0521_event"
75 #define RPR0521_REGMAP_NAME "rpr0521_regmap"
77 #define RPR0521_SLEEP_DELAY_MS 2000
79 #define RPR0521_ALS_SCALE_AVAIL "0.007812 0.015625 0.5 1"
80 #define RPR0521_PXS_SCALE_AVAIL "0.125 0.5 1"
82 struct rpr0521_gain {
83 int scale;
84 int uscale;
87 static const struct rpr0521_gain rpr0521_als_gain[4] = {
88 {1, 0}, /* x1 */
89 {0, 500000}, /* x2 */
90 {0, 15625}, /* x64 */
91 {0, 7812}, /* x128 */
94 static const struct rpr0521_gain rpr0521_pxs_gain[3] = {
95 {1, 0}, /* x1 */
96 {0, 500000}, /* x2 */
97 {0, 125000}, /* x4 */
100 enum rpr0521_channel {
101 RPR0521_CHAN_PXS,
102 RPR0521_CHAN_ALS_DATA0,
103 RPR0521_CHAN_ALS_DATA1,
106 struct rpr0521_reg_desc {
107 u8 address;
108 u8 device_mask;
111 static const struct rpr0521_reg_desc rpr0521_data_reg[] = {
112 [RPR0521_CHAN_PXS] = {
113 .address = RPR0521_REG_PXS_DATA,
114 .device_mask = RPR0521_MODE_PXS_MASK,
116 [RPR0521_CHAN_ALS_DATA0] = {
117 .address = RPR0521_REG_ALS_DATA0,
118 .device_mask = RPR0521_MODE_ALS_MASK,
120 [RPR0521_CHAN_ALS_DATA1] = {
121 .address = RPR0521_REG_ALS_DATA1,
122 .device_mask = RPR0521_MODE_ALS_MASK,
126 static const struct rpr0521_gain_info {
127 u8 reg;
128 u8 mask;
129 u8 shift;
130 const struct rpr0521_gain *gain;
131 int size;
132 } rpr0521_gain[] = {
133 [RPR0521_CHAN_PXS] = {
134 .reg = RPR0521_REG_PXS_CTRL,
135 .mask = RPR0521_PXS_GAIN_MASK,
136 .shift = RPR0521_PXS_GAIN_SHIFT,
137 .gain = rpr0521_pxs_gain,
138 .size = ARRAY_SIZE(rpr0521_pxs_gain),
140 [RPR0521_CHAN_ALS_DATA0] = {
141 .reg = RPR0521_REG_ALS_CTRL,
142 .mask = RPR0521_ALS_DATA0_GAIN_MASK,
143 .shift = RPR0521_ALS_DATA0_GAIN_SHIFT,
144 .gain = rpr0521_als_gain,
145 .size = ARRAY_SIZE(rpr0521_als_gain),
147 [RPR0521_CHAN_ALS_DATA1] = {
148 .reg = RPR0521_REG_ALS_CTRL,
149 .mask = RPR0521_ALS_DATA1_GAIN_MASK,
150 .shift = RPR0521_ALS_DATA1_GAIN_SHIFT,
151 .gain = rpr0521_als_gain,
152 .size = ARRAY_SIZE(rpr0521_als_gain),
156 struct rpr0521_samp_freq {
157 int als_hz;
158 int als_uhz;
159 int pxs_hz;
160 int pxs_uhz;
163 static const struct rpr0521_samp_freq rpr0521_samp_freq_i[13] = {
164 /* {ALS, PXS}, W==currently writable option */
165 {0, 0, 0, 0}, /* W0000, 0=standby */
166 {0, 0, 100, 0}, /* 0001 */
167 {0, 0, 25, 0}, /* 0010 */
168 {0, 0, 10, 0}, /* 0011 */
169 {0, 0, 2, 500000}, /* 0100 */
170 {10, 0, 20, 0}, /* 0101 */
171 {10, 0, 10, 0}, /* W0110 */
172 {10, 0, 2, 500000}, /* 0111 */
173 {2, 500000, 20, 0}, /* 1000, measurement 100ms, sleep 300ms */
174 {2, 500000, 10, 0}, /* 1001, measurement 100ms, sleep 300ms */
175 {2, 500000, 0, 0}, /* 1010, high sensitivity mode */
176 {2, 500000, 2, 500000}, /* W1011, high sensitivity mode */
177 {20, 0, 20, 0} /* 1100, ALS_data x 0.5, see specification P.18 */
180 struct rpr0521_data {
181 struct i2c_client *client;
183 /* protect device params updates (e.g state, gain) */
184 struct mutex lock;
186 /* device active status */
187 bool als_dev_en;
188 bool pxs_dev_en;
190 struct iio_trigger *drdy_trigger0;
191 s64 irq_timestamp;
193 /* optimize runtime pm ops - enable/disable device only if needed */
194 bool als_ps_need_en;
195 bool pxs_ps_need_en;
196 bool als_need_dis;
197 bool pxs_need_dis;
199 struct regmap *regmap;
202 static IIO_CONST_ATTR(in_intensity_scale_available, RPR0521_ALS_SCALE_AVAIL);
203 static IIO_CONST_ATTR(in_proximity_scale_available, RPR0521_PXS_SCALE_AVAIL);
206 * Start with easy freq first, whole table of freq combinations is more
207 * complicated.
209 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("2.5 10");
211 static struct attribute *rpr0521_attributes[] = {
212 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
213 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
214 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
215 NULL,
218 static const struct attribute_group rpr0521_attribute_group = {
219 .attrs = rpr0521_attributes,
222 /* Order of the channel data in buffer */
223 enum rpr0521_scan_index_order {
224 RPR0521_CHAN_INDEX_PXS,
225 RPR0521_CHAN_INDEX_BOTH,
226 RPR0521_CHAN_INDEX_IR,
229 static const unsigned long rpr0521_available_scan_masks[] = {
230 BIT(RPR0521_CHAN_INDEX_PXS) | BIT(RPR0521_CHAN_INDEX_BOTH) |
231 BIT(RPR0521_CHAN_INDEX_IR),
235 static const struct iio_chan_spec rpr0521_channels[] = {
237 .type = IIO_PROXIMITY,
238 .address = RPR0521_CHAN_PXS,
239 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
240 BIT(IIO_CHAN_INFO_OFFSET) |
241 BIT(IIO_CHAN_INFO_SCALE),
242 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
243 .scan_index = RPR0521_CHAN_INDEX_PXS,
244 .scan_type = {
245 .sign = 'u',
246 .realbits = 16,
247 .storagebits = 16,
248 .endianness = IIO_LE,
252 .type = IIO_INTENSITY,
253 .modified = 1,
254 .address = RPR0521_CHAN_ALS_DATA0,
255 .channel2 = IIO_MOD_LIGHT_BOTH,
256 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
257 BIT(IIO_CHAN_INFO_SCALE),
258 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
259 .scan_index = RPR0521_CHAN_INDEX_BOTH,
260 .scan_type = {
261 .sign = 'u',
262 .realbits = 16,
263 .storagebits = 16,
264 .endianness = IIO_LE,
268 .type = IIO_INTENSITY,
269 .modified = 1,
270 .address = RPR0521_CHAN_ALS_DATA1,
271 .channel2 = IIO_MOD_LIGHT_IR,
272 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
273 BIT(IIO_CHAN_INFO_SCALE),
274 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
275 .scan_index = RPR0521_CHAN_INDEX_IR,
276 .scan_type = {
277 .sign = 'u',
278 .realbits = 16,
279 .storagebits = 16,
280 .endianness = IIO_LE,
285 static int rpr0521_als_enable(struct rpr0521_data *data, u8 status)
287 int ret;
289 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
290 RPR0521_MODE_ALS_MASK,
291 status);
292 if (ret < 0)
293 return ret;
295 if (status & RPR0521_MODE_ALS_MASK)
296 data->als_dev_en = true;
297 else
298 data->als_dev_en = false;
300 return 0;
303 static int rpr0521_pxs_enable(struct rpr0521_data *data, u8 status)
305 int ret;
307 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
308 RPR0521_MODE_PXS_MASK,
309 status);
310 if (ret < 0)
311 return ret;
313 if (status & RPR0521_MODE_PXS_MASK)
314 data->pxs_dev_en = true;
315 else
316 data->pxs_dev_en = false;
318 return 0;
322 * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
324 * @data: rpr0521 device private data
325 * @on: state to be set for devices in @device_mask
326 * @device_mask: bitmask specifying for which device we need to update @on state
328 * Calls for this function must be balanced so that each ON should have matching
329 * OFF. Otherwise pm usage_count gets out of sync.
331 static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
332 u8 device_mask)
334 #ifdef CONFIG_PM
335 int ret;
337 if (device_mask & RPR0521_MODE_ALS_MASK) {
338 data->als_ps_need_en = on;
339 data->als_need_dis = !on;
342 if (device_mask & RPR0521_MODE_PXS_MASK) {
343 data->pxs_ps_need_en = on;
344 data->pxs_need_dis = !on;
348 * On: _resume() is called only when we are suspended
349 * Off: _suspend() is called after delay if _resume() is not
350 * called before that.
351 * Note: If either measurement is re-enabled before _suspend(),
352 * both stay enabled until _suspend().
354 if (on) {
355 ret = pm_runtime_get_sync(&data->client->dev);
356 } else {
357 pm_runtime_mark_last_busy(&data->client->dev);
358 ret = pm_runtime_put_autosuspend(&data->client->dev);
360 if (ret < 0) {
361 dev_err(&data->client->dev,
362 "Failed: rpr0521_set_power_state for %d, ret %d\n",
363 on, ret);
364 if (on)
365 pm_runtime_put_noidle(&data->client->dev);
367 return ret;
370 if (on) {
371 /* If _resume() was not called, enable measurement now. */
372 if (data->als_ps_need_en) {
373 ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
374 if (ret)
375 return ret;
376 data->als_ps_need_en = false;
379 if (data->pxs_ps_need_en) {
380 ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
381 if (ret)
382 return ret;
383 data->pxs_ps_need_en = false;
386 #endif
387 return 0;
390 /* Interrupt register tells if this sensor caused the interrupt or not. */
391 static inline bool rpr0521_is_triggered(struct rpr0521_data *data)
393 int ret;
394 int reg;
396 ret = regmap_read(data->regmap, RPR0521_REG_INTERRUPT, &reg);
397 if (ret < 0)
398 return false; /* Reg read failed. */
399 if (reg &
400 (RPR0521_INTERRUPT_ALS_INT_STATUS_MASK |
401 RPR0521_INTERRUPT_PS_INT_STATUS_MASK))
402 return true;
403 else
404 return false; /* Int not from this sensor. */
407 /* IRQ to trigger handler */
408 static irqreturn_t rpr0521_drdy_irq_handler(int irq, void *private)
410 struct iio_dev *indio_dev = private;
411 struct rpr0521_data *data = iio_priv(indio_dev);
413 data->irq_timestamp = iio_get_time_ns(indio_dev);
415 * We need to wake the thread to read the interrupt reg. It
416 * is not possible to do that here because regmap_read takes a
417 * mutex.
420 return IRQ_WAKE_THREAD;
423 static irqreturn_t rpr0521_drdy_irq_thread(int irq, void *private)
425 struct iio_dev *indio_dev = private;
426 struct rpr0521_data *data = iio_priv(indio_dev);
428 if (rpr0521_is_triggered(data)) {
429 iio_trigger_poll_chained(data->drdy_trigger0);
430 return IRQ_HANDLED;
433 return IRQ_NONE;
436 static irqreturn_t rpr0521_trigger_consumer_store_time(int irq, void *p)
438 struct iio_poll_func *pf = p;
439 struct iio_dev *indio_dev = pf->indio_dev;
441 /* Other trigger polls store time here. */
442 if (!iio_trigger_using_own(indio_dev))
443 pf->timestamp = iio_get_time_ns(indio_dev);
445 return IRQ_WAKE_THREAD;
448 static irqreturn_t rpr0521_trigger_consumer_handler(int irq, void *p)
450 struct iio_poll_func *pf = p;
451 struct iio_dev *indio_dev = pf->indio_dev;
452 struct rpr0521_data *data = iio_priv(indio_dev);
453 int err;
455 u8 buffer[16]; /* 3 16-bit channels + padding + ts */
457 /* Use irq timestamp when reasonable. */
458 if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
459 pf->timestamp = data->irq_timestamp;
460 data->irq_timestamp = 0;
462 /* Other chained trigger polls get timestamp only here. */
463 if (!pf->timestamp)
464 pf->timestamp = iio_get_time_ns(indio_dev);
466 err = regmap_bulk_read(data->regmap, RPR0521_REG_PXS_DATA,
467 &buffer,
468 (3 * 2) + 1); /* 3 * 16-bit + (discarded) int clear reg. */
469 if (!err)
470 iio_push_to_buffers_with_timestamp(indio_dev,
471 buffer, pf->timestamp);
472 else
473 dev_err(&data->client->dev,
474 "Trigger consumer can't read from sensor.\n");
475 pf->timestamp = 0;
477 iio_trigger_notify_done(indio_dev->trig);
479 return IRQ_HANDLED;
482 static int rpr0521_write_int_enable(struct rpr0521_data *data)
484 int err;
486 /* Interrupt after each measurement */
487 err = regmap_update_bits(data->regmap, RPR0521_REG_PXS_CTRL,
488 RPR0521_PXS_PERSISTENCE_MASK,
489 RPR0521_PXS_PERSISTENCE_DRDY);
490 if (err) {
491 dev_err(&data->client->dev, "PS control reg write fail.\n");
492 return -EBUSY;
495 /* Ignore latch and mode because of drdy */
496 err = regmap_write(data->regmap, RPR0521_REG_INTERRUPT,
497 RPR0521_INTERRUPT_INT_REASSERT_DISABLE |
498 RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE |
499 RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE
501 if (err) {
502 dev_err(&data->client->dev, "Interrupt setup write fail.\n");
503 return -EBUSY;
506 return 0;
509 static int rpr0521_write_int_disable(struct rpr0521_data *data)
511 /* Don't care of clearing mode, assert and latch. */
512 return regmap_write(data->regmap, RPR0521_REG_INTERRUPT,
513 RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE |
514 RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE
519 * Trigger producer enable / disable. Note that there will be trigs only when
520 * measurement data is ready to be read.
522 static int rpr0521_pxs_drdy_set_state(struct iio_trigger *trigger,
523 bool enable_drdy)
525 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
526 struct rpr0521_data *data = iio_priv(indio_dev);
527 int err;
529 if (enable_drdy)
530 err = rpr0521_write_int_enable(data);
531 else
532 err = rpr0521_write_int_disable(data);
533 if (err)
534 dev_err(&data->client->dev, "rpr0521_pxs_drdy_set_state failed\n");
536 return err;
539 static const struct iio_trigger_ops rpr0521_trigger_ops = {
540 .set_trigger_state = rpr0521_pxs_drdy_set_state,
544 static int rpr0521_buffer_preenable(struct iio_dev *indio_dev)
546 int err;
547 struct rpr0521_data *data = iio_priv(indio_dev);
549 mutex_lock(&data->lock);
550 err = rpr0521_set_power_state(data, true,
551 (RPR0521_MODE_PXS_MASK | RPR0521_MODE_ALS_MASK));
552 mutex_unlock(&data->lock);
553 if (err)
554 dev_err(&data->client->dev, "_buffer_preenable fail\n");
556 return err;
559 static int rpr0521_buffer_postdisable(struct iio_dev *indio_dev)
561 int err;
562 struct rpr0521_data *data = iio_priv(indio_dev);
564 mutex_lock(&data->lock);
565 err = rpr0521_set_power_state(data, false,
566 (RPR0521_MODE_PXS_MASK | RPR0521_MODE_ALS_MASK));
567 mutex_unlock(&data->lock);
568 if (err)
569 dev_err(&data->client->dev, "_buffer_postdisable fail\n");
571 return err;
574 static const struct iio_buffer_setup_ops rpr0521_buffer_setup_ops = {
575 .preenable = rpr0521_buffer_preenable,
576 .postenable = iio_triggered_buffer_postenable,
577 .predisable = iio_triggered_buffer_predisable,
578 .postdisable = rpr0521_buffer_postdisable,
581 static int rpr0521_get_gain(struct rpr0521_data *data, int chan,
582 int *val, int *val2)
584 int ret, reg, idx;
586 ret = regmap_read(data->regmap, rpr0521_gain[chan].reg, &reg);
587 if (ret < 0)
588 return ret;
590 idx = (rpr0521_gain[chan].mask & reg) >> rpr0521_gain[chan].shift;
591 *val = rpr0521_gain[chan].gain[idx].scale;
592 *val2 = rpr0521_gain[chan].gain[idx].uscale;
594 return 0;
597 static int rpr0521_set_gain(struct rpr0521_data *data, int chan,
598 int val, int val2)
600 int i, idx = -EINVAL;
602 /* get gain index */
603 for (i = 0; i < rpr0521_gain[chan].size; i++)
604 if (val == rpr0521_gain[chan].gain[i].scale &&
605 val2 == rpr0521_gain[chan].gain[i].uscale) {
606 idx = i;
607 break;
610 if (idx < 0)
611 return idx;
613 return regmap_update_bits(data->regmap, rpr0521_gain[chan].reg,
614 rpr0521_gain[chan].mask,
615 idx << rpr0521_gain[chan].shift);
618 static int rpr0521_read_samp_freq(struct rpr0521_data *data,
619 enum iio_chan_type chan_type,
620 int *val, int *val2)
622 int reg, ret;
624 ret = regmap_read(data->regmap, RPR0521_REG_MODE_CTRL, &reg);
625 if (ret < 0)
626 return ret;
628 reg &= RPR0521_MODE_MEAS_TIME_MASK;
629 if (reg >= ARRAY_SIZE(rpr0521_samp_freq_i))
630 return -EINVAL;
632 switch (chan_type) {
633 case IIO_INTENSITY:
634 *val = rpr0521_samp_freq_i[reg].als_hz;
635 *val2 = rpr0521_samp_freq_i[reg].als_uhz;
636 return 0;
638 case IIO_PROXIMITY:
639 *val = rpr0521_samp_freq_i[reg].pxs_hz;
640 *val2 = rpr0521_samp_freq_i[reg].pxs_uhz;
641 return 0;
643 default:
644 return -EINVAL;
648 static int rpr0521_write_samp_freq_common(struct rpr0521_data *data,
649 enum iio_chan_type chan_type,
650 int val, int val2)
652 int i;
655 * Ignore channel
656 * both pxs and als are setup only to same freq because of simplicity
658 switch (val) {
659 case 0:
660 i = 0;
661 break;
663 case 2:
664 if (val2 != 500000)
665 return -EINVAL;
667 i = 11;
668 break;
670 case 10:
671 i = 6;
672 break;
674 default:
675 return -EINVAL;
678 return regmap_update_bits(data->regmap,
679 RPR0521_REG_MODE_CTRL,
680 RPR0521_MODE_MEAS_TIME_MASK,
684 static int rpr0521_read_ps_offset(struct rpr0521_data *data, int *offset)
686 int ret;
687 __le16 buffer;
689 ret = regmap_bulk_read(data->regmap,
690 RPR0521_REG_PS_OFFSET_LSB, &buffer, sizeof(buffer));
692 if (ret < 0) {
693 dev_err(&data->client->dev, "Failed to read PS OFFSET register\n");
694 return ret;
696 *offset = le16_to_cpu(buffer);
698 return ret;
701 static int rpr0521_write_ps_offset(struct rpr0521_data *data, int offset)
703 int ret;
704 __le16 buffer;
706 buffer = cpu_to_le16(offset & 0x3ff);
707 ret = regmap_raw_write(data->regmap,
708 RPR0521_REG_PS_OFFSET_LSB, &buffer, sizeof(buffer));
710 if (ret < 0) {
711 dev_err(&data->client->dev, "Failed to write PS OFFSET register\n");
712 return ret;
715 return ret;
718 static int rpr0521_read_raw(struct iio_dev *indio_dev,
719 struct iio_chan_spec const *chan, int *val,
720 int *val2, long mask)
722 struct rpr0521_data *data = iio_priv(indio_dev);
723 int ret;
724 int busy;
725 u8 device_mask;
726 __le16 raw_data;
728 switch (mask) {
729 case IIO_CHAN_INFO_RAW:
730 if (chan->type != IIO_INTENSITY && chan->type != IIO_PROXIMITY)
731 return -EINVAL;
733 busy = iio_device_claim_direct_mode(indio_dev);
734 if (busy)
735 return -EBUSY;
737 device_mask = rpr0521_data_reg[chan->address].device_mask;
739 mutex_lock(&data->lock);
740 ret = rpr0521_set_power_state(data, true, device_mask);
741 if (ret < 0)
742 goto rpr0521_read_raw_out;
744 ret = regmap_bulk_read(data->regmap,
745 rpr0521_data_reg[chan->address].address,
746 &raw_data, sizeof(raw_data));
747 if (ret < 0) {
748 rpr0521_set_power_state(data, false, device_mask);
749 goto rpr0521_read_raw_out;
752 ret = rpr0521_set_power_state(data, false, device_mask);
754 rpr0521_read_raw_out:
755 mutex_unlock(&data->lock);
756 iio_device_release_direct_mode(indio_dev);
757 if (ret < 0)
758 return ret;
760 *val = le16_to_cpu(raw_data);
762 return IIO_VAL_INT;
764 case IIO_CHAN_INFO_SCALE:
765 mutex_lock(&data->lock);
766 ret = rpr0521_get_gain(data, chan->address, val, val2);
767 mutex_unlock(&data->lock);
768 if (ret < 0)
769 return ret;
771 return IIO_VAL_INT_PLUS_MICRO;
773 case IIO_CHAN_INFO_SAMP_FREQ:
774 mutex_lock(&data->lock);
775 ret = rpr0521_read_samp_freq(data, chan->type, val, val2);
776 mutex_unlock(&data->lock);
777 if (ret < 0)
778 return ret;
780 return IIO_VAL_INT_PLUS_MICRO;
782 case IIO_CHAN_INFO_OFFSET:
783 mutex_lock(&data->lock);
784 ret = rpr0521_read_ps_offset(data, val);
785 mutex_unlock(&data->lock);
786 if (ret < 0)
787 return ret;
789 return IIO_VAL_INT;
791 default:
792 return -EINVAL;
796 static int rpr0521_write_raw(struct iio_dev *indio_dev,
797 struct iio_chan_spec const *chan, int val,
798 int val2, long mask)
800 struct rpr0521_data *data = iio_priv(indio_dev);
801 int ret;
803 switch (mask) {
804 case IIO_CHAN_INFO_SCALE:
805 mutex_lock(&data->lock);
806 ret = rpr0521_set_gain(data, chan->address, val, val2);
807 mutex_unlock(&data->lock);
809 return ret;
811 case IIO_CHAN_INFO_SAMP_FREQ:
812 mutex_lock(&data->lock);
813 ret = rpr0521_write_samp_freq_common(data, chan->type,
814 val, val2);
815 mutex_unlock(&data->lock);
817 return ret;
819 case IIO_CHAN_INFO_OFFSET:
820 mutex_lock(&data->lock);
821 ret = rpr0521_write_ps_offset(data, val);
822 mutex_unlock(&data->lock);
824 return ret;
826 default:
827 return -EINVAL;
831 static const struct iio_info rpr0521_info = {
832 .read_raw = rpr0521_read_raw,
833 .write_raw = rpr0521_write_raw,
834 .attrs = &rpr0521_attribute_group,
837 static int rpr0521_init(struct rpr0521_data *data)
839 int ret;
840 int id;
842 ret = regmap_read(data->regmap, RPR0521_REG_ID, &id);
843 if (ret < 0) {
844 dev_err(&data->client->dev, "Failed to read REG_ID register\n");
845 return ret;
848 if (id != RPR0521_MANUFACT_ID) {
849 dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
850 id, RPR0521_MANUFACT_ID);
851 return -ENODEV;
854 /* set default measurement time - 100 ms for both ALS and PS */
855 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
856 RPR0521_MODE_MEAS_TIME_MASK,
857 RPR0521_DEFAULT_MEAS_TIME);
858 if (ret) {
859 pr_err("regmap_update_bits returned %d\n", ret);
860 return ret;
863 #ifndef CONFIG_PM
864 ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
865 if (ret < 0)
866 return ret;
867 ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
868 if (ret < 0)
869 return ret;
870 #endif
872 data->irq_timestamp = 0;
874 return 0;
877 static int rpr0521_poweroff(struct rpr0521_data *data)
879 int ret;
880 int tmp;
882 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
883 RPR0521_MODE_ALS_MASK |
884 RPR0521_MODE_PXS_MASK,
885 RPR0521_MODE_ALS_DISABLE |
886 RPR0521_MODE_PXS_DISABLE);
887 if (ret < 0)
888 return ret;
890 data->als_dev_en = false;
891 data->pxs_dev_en = false;
894 * Int pin keeps state after power off. Set pin to high impedance
895 * mode to prevent power drain.
897 ret = regmap_read(data->regmap, RPR0521_REG_INTERRUPT, &tmp);
898 if (ret) {
899 dev_err(&data->client->dev, "Failed to reset int pin.\n");
900 return ret;
903 return 0;
906 static bool rpr0521_is_volatile_reg(struct device *dev, unsigned int reg)
908 switch (reg) {
909 case RPR0521_REG_MODE_CTRL:
910 case RPR0521_REG_ALS_CTRL:
911 case RPR0521_REG_PXS_CTRL:
912 return false;
913 default:
914 return true;
918 static const struct regmap_config rpr0521_regmap_config = {
919 .name = RPR0521_REGMAP_NAME,
921 .reg_bits = 8,
922 .val_bits = 8,
924 .max_register = RPR0521_REG_ID,
925 .cache_type = REGCACHE_RBTREE,
926 .volatile_reg = rpr0521_is_volatile_reg,
929 static int rpr0521_probe(struct i2c_client *client,
930 const struct i2c_device_id *id)
932 struct rpr0521_data *data;
933 struct iio_dev *indio_dev;
934 struct regmap *regmap;
935 int ret;
937 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
938 if (!indio_dev)
939 return -ENOMEM;
941 regmap = devm_regmap_init_i2c(client, &rpr0521_regmap_config);
942 if (IS_ERR(regmap)) {
943 dev_err(&client->dev, "regmap_init failed!\n");
944 return PTR_ERR(regmap);
947 data = iio_priv(indio_dev);
948 i2c_set_clientdata(client, indio_dev);
949 data->client = client;
950 data->regmap = regmap;
952 mutex_init(&data->lock);
954 indio_dev->dev.parent = &client->dev;
955 indio_dev->info = &rpr0521_info;
956 indio_dev->name = RPR0521_DRV_NAME;
957 indio_dev->channels = rpr0521_channels;
958 indio_dev->num_channels = ARRAY_SIZE(rpr0521_channels);
959 indio_dev->modes = INDIO_DIRECT_MODE;
961 ret = rpr0521_init(data);
962 if (ret < 0) {
963 dev_err(&client->dev, "rpr0521 chip init failed\n");
964 return ret;
967 ret = pm_runtime_set_active(&client->dev);
968 if (ret < 0)
969 goto err_poweroff;
971 pm_runtime_enable(&client->dev);
972 pm_runtime_set_autosuspend_delay(&client->dev, RPR0521_SLEEP_DELAY_MS);
973 pm_runtime_use_autosuspend(&client->dev);
976 * If sensor write/read is needed in _probe after _use_autosuspend,
977 * sensor needs to be _resumed first using rpr0521_set_power_state().
980 /* IRQ to trigger setup */
981 if (client->irq) {
982 /* Trigger0 producer setup */
983 data->drdy_trigger0 = devm_iio_trigger_alloc(
984 indio_dev->dev.parent,
985 "%s-dev%d", indio_dev->name, indio_dev->id);
986 if (!data->drdy_trigger0) {
987 ret = -ENOMEM;
988 goto err_pm_disable;
990 data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
991 data->drdy_trigger0->ops = &rpr0521_trigger_ops;
992 indio_dev->available_scan_masks = rpr0521_available_scan_masks;
993 iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
995 /* Ties irq to trigger producer handler. */
996 ret = devm_request_threaded_irq(&client->dev, client->irq,
997 rpr0521_drdy_irq_handler, rpr0521_drdy_irq_thread,
998 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
999 RPR0521_IRQ_NAME, indio_dev);
1000 if (ret < 0) {
1001 dev_err(&client->dev, "request irq %d for trigger0 failed\n",
1002 client->irq);
1003 goto err_pm_disable;
1006 ret = devm_iio_trigger_register(indio_dev->dev.parent,
1007 data->drdy_trigger0);
1008 if (ret) {
1009 dev_err(&client->dev, "iio trigger register failed\n");
1010 goto err_pm_disable;
1014 * Now whole pipe from physical interrupt (irq defined by
1015 * devicetree to device) to trigger0 output is set up.
1018 /* Trigger consumer setup */
1019 ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,
1020 indio_dev,
1021 rpr0521_trigger_consumer_store_time,
1022 rpr0521_trigger_consumer_handler,
1023 &rpr0521_buffer_setup_ops);
1024 if (ret < 0) {
1025 dev_err(&client->dev, "iio triggered buffer setup failed\n");
1026 goto err_pm_disable;
1030 ret = iio_device_register(indio_dev);
1031 if (ret)
1032 goto err_pm_disable;
1034 return 0;
1036 err_pm_disable:
1037 pm_runtime_disable(&client->dev);
1038 pm_runtime_set_suspended(&client->dev);
1039 pm_runtime_put_noidle(&client->dev);
1040 err_poweroff:
1041 rpr0521_poweroff(data);
1043 return ret;
1046 static int rpr0521_remove(struct i2c_client *client)
1048 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1050 iio_device_unregister(indio_dev);
1052 pm_runtime_disable(&client->dev);
1053 pm_runtime_set_suspended(&client->dev);
1054 pm_runtime_put_noidle(&client->dev);
1056 rpr0521_poweroff(iio_priv(indio_dev));
1058 return 0;
1061 #ifdef CONFIG_PM
1062 static int rpr0521_runtime_suspend(struct device *dev)
1064 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1065 struct rpr0521_data *data = iio_priv(indio_dev);
1066 int ret;
1068 mutex_lock(&data->lock);
1069 /* If measurements are enabled, enable them on resume */
1070 if (!data->als_need_dis)
1071 data->als_ps_need_en = data->als_dev_en;
1072 if (!data->pxs_need_dis)
1073 data->pxs_ps_need_en = data->pxs_dev_en;
1075 /* disable channels and sets {als,pxs}_dev_en to false */
1076 ret = rpr0521_poweroff(data);
1077 regcache_mark_dirty(data->regmap);
1078 mutex_unlock(&data->lock);
1080 return ret;
1083 static int rpr0521_runtime_resume(struct device *dev)
1085 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1086 struct rpr0521_data *data = iio_priv(indio_dev);
1087 int ret;
1089 regcache_sync(data->regmap);
1090 if (data->als_ps_need_en) {
1091 ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
1092 if (ret < 0)
1093 return ret;
1094 data->als_ps_need_en = false;
1097 if (data->pxs_ps_need_en) {
1098 ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
1099 if (ret < 0)
1100 return ret;
1101 data->pxs_ps_need_en = false;
1103 msleep(100); //wait for first measurement result
1105 return 0;
1107 #endif
1109 static const struct dev_pm_ops rpr0521_pm_ops = {
1110 SET_RUNTIME_PM_OPS(rpr0521_runtime_suspend,
1111 rpr0521_runtime_resume, NULL)
1114 static const struct acpi_device_id rpr0521_acpi_match[] = {
1115 {"RPR0521", 0},
1118 MODULE_DEVICE_TABLE(acpi, rpr0521_acpi_match);
1120 static const struct i2c_device_id rpr0521_id[] = {
1121 {"rpr0521", 0},
1125 MODULE_DEVICE_TABLE(i2c, rpr0521_id);
1127 static struct i2c_driver rpr0521_driver = {
1128 .driver = {
1129 .name = RPR0521_DRV_NAME,
1130 .pm = &rpr0521_pm_ops,
1131 .acpi_match_table = ACPI_PTR(rpr0521_acpi_match),
1133 .probe = rpr0521_probe,
1134 .remove = rpr0521_remove,
1135 .id_table = rpr0521_id,
1138 module_i2c_driver(rpr0521_driver);
1140 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1141 MODULE_DESCRIPTION("RPR0521 ROHM Ambient Light and Proximity Sensor driver");
1142 MODULE_LICENSE("GPL v2");