xref: /linux/drivers/iio/light/rpr0521.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * RPR-0521 ROHM Ambient Light and Proximity Sensor
4  *
5  * Copyright (c) 2015, Intel Corporation.
6  *
7  * IIO driver for RPR-0521RS (7-bit I2C slave address 0x38).
8  *
9  * TODO: illuminance channel
10  */
11 
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/init.h>
15 #include <linux/i2c.h>
16 #include <linux/regmap.h>
17 #include <linux/delay.h>
18 
19 #include <linux/iio/iio.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/pm_runtime.h>
26 
27 #define RPR0521_REG_SYSTEM_CTRL		0x40
28 #define RPR0521_REG_MODE_CTRL		0x41
29 #define RPR0521_REG_ALS_CTRL		0x42
30 #define RPR0521_REG_PXS_CTRL		0x43
31 #define RPR0521_REG_PXS_DATA		0x44 /* 16-bit, little endian */
32 #define RPR0521_REG_ALS_DATA0		0x46 /* 16-bit, little endian */
33 #define RPR0521_REG_ALS_DATA1		0x48 /* 16-bit, little endian */
34 #define RPR0521_REG_INTERRUPT		0x4A
35 #define RPR0521_REG_PS_OFFSET_LSB	0x53
36 #define RPR0521_REG_ID			0x92
37 
38 #define RPR0521_MODE_ALS_MASK		BIT(7)
39 #define RPR0521_MODE_PXS_MASK		BIT(6)
40 #define RPR0521_MODE_MEAS_TIME_MASK	GENMASK(3, 0)
41 #define RPR0521_ALS_DATA0_GAIN_MASK	GENMASK(5, 4)
42 #define RPR0521_ALS_DATA0_GAIN_SHIFT	4
43 #define RPR0521_ALS_DATA1_GAIN_MASK	GENMASK(3, 2)
44 #define RPR0521_ALS_DATA1_GAIN_SHIFT	2
45 #define RPR0521_PXS_GAIN_MASK		GENMASK(5, 4)
46 #define RPR0521_PXS_GAIN_SHIFT		4
47 #define RPR0521_PXS_PERSISTENCE_MASK	GENMASK(3, 0)
48 #define RPR0521_INTERRUPT_INT_TRIG_PS_MASK	BIT(0)
49 #define RPR0521_INTERRUPT_INT_TRIG_ALS_MASK	BIT(1)
50 #define RPR0521_INTERRUPT_INT_REASSERT_MASK	BIT(3)
51 #define RPR0521_INTERRUPT_ALS_INT_STATUS_MASK	BIT(6)
52 #define RPR0521_INTERRUPT_PS_INT_STATUS_MASK	BIT(7)
53 
54 #define RPR0521_MODE_ALS_ENABLE		BIT(7)
55 #define RPR0521_MODE_ALS_DISABLE	0x00
56 #define RPR0521_MODE_PXS_ENABLE		BIT(6)
57 #define RPR0521_MODE_PXS_DISABLE	0x00
58 #define RPR0521_PXS_PERSISTENCE_DRDY	0x00
59 
60 #define RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE	BIT(0)
61 #define RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE	0x00
62 #define RPR0521_INTERRUPT_INT_TRIG_ALS_ENABLE	BIT(1)
63 #define RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE	0x00
64 #define RPR0521_INTERRUPT_INT_REASSERT_ENABLE	BIT(3)
65 #define RPR0521_INTERRUPT_INT_REASSERT_DISABLE	0x00
66 
67 #define RPR0521_MANUFACT_ID		0xE0
68 #define RPR0521_DEFAULT_MEAS_TIME	0x06 /* ALS - 100ms, PXS - 100ms */
69 
70 #define RPR0521_DRV_NAME		"RPR0521"
71 #define RPR0521_IRQ_NAME		"rpr0521_event"
72 #define RPR0521_REGMAP_NAME		"rpr0521_regmap"
73 
74 #define RPR0521_SLEEP_DELAY_MS	2000
75 
76 #define RPR0521_ALS_SCALE_AVAIL "0.007812 0.015625 0.5 1"
77 #define RPR0521_PXS_SCALE_AVAIL "0.125 0.5 1"
78 
79 struct rpr0521_gain {
80 	int scale;
81 	int uscale;
82 };
83 
84 static const struct rpr0521_gain rpr0521_als_gain[4] = {
85 	{1, 0},		/* x1 */
86 	{0, 500000},	/* x2 */
87 	{0, 15625},	/* x64 */
88 	{0, 7812},	/* x128 */
89 };
90 
91 static const struct rpr0521_gain rpr0521_pxs_gain[3] = {
92 	{1, 0},		/* x1 */
93 	{0, 500000},	/* x2 */
94 	{0, 125000},	/* x4 */
95 };
96 
97 enum rpr0521_channel {
98 	RPR0521_CHAN_PXS,
99 	RPR0521_CHAN_ALS_DATA0,
100 	RPR0521_CHAN_ALS_DATA1,
101 };
102 
103 struct rpr0521_reg_desc {
104 	u8 address;
105 	u8 device_mask;
106 };
107 
108 static const struct rpr0521_reg_desc rpr0521_data_reg[] = {
109 	[RPR0521_CHAN_PXS]	= {
110 		.address	= RPR0521_REG_PXS_DATA,
111 		.device_mask	= RPR0521_MODE_PXS_MASK,
112 	},
113 	[RPR0521_CHAN_ALS_DATA0] = {
114 		.address	= RPR0521_REG_ALS_DATA0,
115 		.device_mask	= RPR0521_MODE_ALS_MASK,
116 	},
117 	[RPR0521_CHAN_ALS_DATA1] = {
118 		.address	= RPR0521_REG_ALS_DATA1,
119 		.device_mask	= RPR0521_MODE_ALS_MASK,
120 	},
121 };
122 
123 static const struct rpr0521_gain_info {
124 	u8 reg;
125 	u8 mask;
126 	u8 shift;
127 	const struct rpr0521_gain *gain;
128 	int size;
129 } rpr0521_gain[] = {
130 	[RPR0521_CHAN_PXS] = {
131 		.reg	= RPR0521_REG_PXS_CTRL,
132 		.mask	= RPR0521_PXS_GAIN_MASK,
133 		.shift	= RPR0521_PXS_GAIN_SHIFT,
134 		.gain	= rpr0521_pxs_gain,
135 		.size	= ARRAY_SIZE(rpr0521_pxs_gain),
136 	},
137 	[RPR0521_CHAN_ALS_DATA0] = {
138 		.reg	= RPR0521_REG_ALS_CTRL,
139 		.mask	= RPR0521_ALS_DATA0_GAIN_MASK,
140 		.shift	= RPR0521_ALS_DATA0_GAIN_SHIFT,
141 		.gain	= rpr0521_als_gain,
142 		.size	= ARRAY_SIZE(rpr0521_als_gain),
143 	},
144 	[RPR0521_CHAN_ALS_DATA1] = {
145 		.reg	= RPR0521_REG_ALS_CTRL,
146 		.mask	= RPR0521_ALS_DATA1_GAIN_MASK,
147 		.shift	= RPR0521_ALS_DATA1_GAIN_SHIFT,
148 		.gain	= rpr0521_als_gain,
149 		.size	= ARRAY_SIZE(rpr0521_als_gain),
150 	},
151 };
152 
153 struct rpr0521_samp_freq {
154 	int	als_hz;
155 	int	als_uhz;
156 	int	pxs_hz;
157 	int	pxs_uhz;
158 };
159 
160 static const struct rpr0521_samp_freq rpr0521_samp_freq_i[13] = {
161 /*	{ALS, PXS},		   W==currently writable option */
162 	{0, 0, 0, 0},		/* W0000, 0=standby */
163 	{0, 0, 100, 0},		/*  0001 */
164 	{0, 0, 25, 0},		/*  0010 */
165 	{0, 0, 10, 0},		/*  0011 */
166 	{0, 0, 2, 500000},	/*  0100 */
167 	{10, 0, 20, 0},		/*  0101 */
168 	{10, 0, 10, 0},		/* W0110 */
169 	{10, 0, 2, 500000},	/*  0111 */
170 	{2, 500000, 20, 0},	/*  1000, measurement 100ms, sleep 300ms */
171 	{2, 500000, 10, 0},	/*  1001, measurement 100ms, sleep 300ms */
172 	{2, 500000, 0, 0},	/*  1010, high sensitivity mode */
173 	{2, 500000, 2, 500000},	/* W1011, high sensitivity mode */
174 	{20, 0, 20, 0}	/* 1100, ALS_data x 0.5, see specification P.18 */
175 };
176 
177 struct rpr0521_data {
178 	struct i2c_client *client;
179 
180 	/* protect device params updates (e.g state, gain) */
181 	struct mutex lock;
182 
183 	/* device active status */
184 	bool als_dev_en;
185 	bool pxs_dev_en;
186 
187 	struct iio_trigger *drdy_trigger0;
188 	s64 irq_timestamp;
189 
190 	/* optimize runtime pm ops - enable/disable device only if needed */
191 	bool als_ps_need_en;
192 	bool pxs_ps_need_en;
193 	bool als_need_dis;
194 	bool pxs_need_dis;
195 
196 	struct regmap *regmap;
197 
198 	/*
199 	 * Ensure correct naturally aligned timestamp.
200 	 * Note that the read will put garbage data into
201 	 * the padding but this should not be a problem
202 	 */
203 	struct {
204 		__le16 channels[3];
205 		u8 garbage;
206 		s64 ts __aligned(8);
207 	} scan;
208 };
209 
210 static IIO_CONST_ATTR(in_intensity_scale_available, RPR0521_ALS_SCALE_AVAIL);
211 static IIO_CONST_ATTR(in_proximity_scale_available, RPR0521_PXS_SCALE_AVAIL);
212 
213 /*
214  * Start with easy freq first, whole table of freq combinations is more
215  * complicated.
216  */
217 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("2.5 10");
218 
219 static struct attribute *rpr0521_attributes[] = {
220 	&iio_const_attr_in_intensity_scale_available.dev_attr.attr,
221 	&iio_const_attr_in_proximity_scale_available.dev_attr.attr,
222 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
223 	NULL,
224 };
225 
226 static const struct attribute_group rpr0521_attribute_group = {
227 	.attrs = rpr0521_attributes,
228 };
229 
230 /* Order of the channel data in buffer */
231 enum rpr0521_scan_index_order {
232 	RPR0521_CHAN_INDEX_PXS,
233 	RPR0521_CHAN_INDEX_BOTH,
234 	RPR0521_CHAN_INDEX_IR,
235 };
236 
237 static const unsigned long rpr0521_available_scan_masks[] = {
238 	BIT(RPR0521_CHAN_INDEX_PXS) | BIT(RPR0521_CHAN_INDEX_BOTH) |
239 	BIT(RPR0521_CHAN_INDEX_IR),
240 	0
241 };
242 
243 static const struct iio_chan_spec rpr0521_channels[] = {
244 	{
245 		.type = IIO_PROXIMITY,
246 		.address = RPR0521_CHAN_PXS,
247 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
248 			BIT(IIO_CHAN_INFO_OFFSET) |
249 			BIT(IIO_CHAN_INFO_SCALE),
250 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
251 		.scan_index = RPR0521_CHAN_INDEX_PXS,
252 		.scan_type = {
253 			.sign = 'u',
254 			.realbits = 16,
255 			.storagebits = 16,
256 			.endianness = IIO_LE,
257 		},
258 	},
259 	{
260 		.type = IIO_INTENSITY,
261 		.modified = 1,
262 		.address = RPR0521_CHAN_ALS_DATA0,
263 		.channel2 = IIO_MOD_LIGHT_BOTH,
264 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
265 			BIT(IIO_CHAN_INFO_SCALE),
266 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
267 		.scan_index = RPR0521_CHAN_INDEX_BOTH,
268 		.scan_type = {
269 			.sign = 'u',
270 			.realbits = 16,
271 			.storagebits = 16,
272 			.endianness = IIO_LE,
273 		},
274 	},
275 	{
276 		.type = IIO_INTENSITY,
277 		.modified = 1,
278 		.address = RPR0521_CHAN_ALS_DATA1,
279 		.channel2 = IIO_MOD_LIGHT_IR,
280 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
281 			BIT(IIO_CHAN_INFO_SCALE),
282 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
283 		.scan_index = RPR0521_CHAN_INDEX_IR,
284 		.scan_type = {
285 			.sign = 'u',
286 			.realbits = 16,
287 			.storagebits = 16,
288 			.endianness = IIO_LE,
289 		},
290 	},
291 };
292 
293 static int rpr0521_als_enable(struct rpr0521_data *data, u8 status)
294 {
295 	int ret;
296 
297 	ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
298 				 RPR0521_MODE_ALS_MASK,
299 				 status);
300 	if (ret < 0)
301 		return ret;
302 
303 	if (status & RPR0521_MODE_ALS_MASK)
304 		data->als_dev_en = true;
305 	else
306 		data->als_dev_en = false;
307 
308 	return 0;
309 }
310 
311 static int rpr0521_pxs_enable(struct rpr0521_data *data, u8 status)
312 {
313 	int ret;
314 
315 	ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
316 				 RPR0521_MODE_PXS_MASK,
317 				 status);
318 	if (ret < 0)
319 		return ret;
320 
321 	if (status & RPR0521_MODE_PXS_MASK)
322 		data->pxs_dev_en = true;
323 	else
324 		data->pxs_dev_en = false;
325 
326 	return 0;
327 }
328 
329 /**
330  * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
331  *
332  * @data: rpr0521 device private data
333  * @on: state to be set for devices in @device_mask
334  * @device_mask: bitmask specifying for which device we need to update @on state
335  *
336  * Calls for this function must be balanced so that each ON should have matching
337  * OFF. Otherwise pm usage_count gets out of sync.
338  */
339 static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
340 				   u8 device_mask)
341 {
342 #ifdef CONFIG_PM
343 	int ret;
344 
345 	if (device_mask & RPR0521_MODE_ALS_MASK) {
346 		data->als_ps_need_en = on;
347 		data->als_need_dis = !on;
348 	}
349 
350 	if (device_mask & RPR0521_MODE_PXS_MASK) {
351 		data->pxs_ps_need_en = on;
352 		data->pxs_need_dis = !on;
353 	}
354 
355 	/*
356 	 * On: _resume() is called only when we are suspended
357 	 * Off: _suspend() is called after delay if _resume() is not
358 	 * called before that.
359 	 * Note: If either measurement is re-enabled before _suspend(),
360 	 * both stay enabled until _suspend().
361 	 */
362 	if (on) {
363 		ret = pm_runtime_resume_and_get(&data->client->dev);
364 	} else {
365 		pm_runtime_mark_last_busy(&data->client->dev);
366 		ret = pm_runtime_put_autosuspend(&data->client->dev);
367 	}
368 	if (ret < 0) {
369 		dev_err(&data->client->dev,
370 			"Failed: rpr0521_set_power_state for %d, ret %d\n",
371 			on, ret);
372 		return ret;
373 	}
374 
375 	if (on) {
376 		/* If _resume() was not called, enable measurement now. */
377 		if (data->als_ps_need_en) {
378 			ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
379 			if (ret)
380 				return ret;
381 			data->als_ps_need_en = false;
382 		}
383 
384 		if (data->pxs_ps_need_en) {
385 			ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
386 			if (ret)
387 				return ret;
388 			data->pxs_ps_need_en = false;
389 		}
390 	}
391 #endif
392 	return 0;
393 }
394 
395 /* Interrupt register tells if this sensor caused the interrupt or not. */
396 static inline bool rpr0521_is_triggered(struct rpr0521_data *data)
397 {
398 	int ret;
399 	int reg;
400 
401 	ret = regmap_read(data->regmap, RPR0521_REG_INTERRUPT, &reg);
402 	if (ret < 0)
403 		return false;   /* Reg read failed. */
404 	if (reg &
405 	    (RPR0521_INTERRUPT_ALS_INT_STATUS_MASK |
406 	    RPR0521_INTERRUPT_PS_INT_STATUS_MASK))
407 		return true;
408 	else
409 		return false;   /* Int not from this sensor. */
410 }
411 
412 /* IRQ to trigger handler */
413 static irqreturn_t rpr0521_drdy_irq_handler(int irq, void *private)
414 {
415 	struct iio_dev *indio_dev = private;
416 	struct rpr0521_data *data = iio_priv(indio_dev);
417 
418 	data->irq_timestamp = iio_get_time_ns(indio_dev);
419 	/*
420 	 * We need to wake the thread to read the interrupt reg. It
421 	 * is not possible to do that here because regmap_read takes a
422 	 * mutex.
423 	 */
424 
425 	return IRQ_WAKE_THREAD;
426 }
427 
428 static irqreturn_t rpr0521_drdy_irq_thread(int irq, void *private)
429 {
430 	struct iio_dev *indio_dev = private;
431 	struct rpr0521_data *data = iio_priv(indio_dev);
432 
433 	if (rpr0521_is_triggered(data)) {
434 		iio_trigger_poll_nested(data->drdy_trigger0);
435 		return IRQ_HANDLED;
436 	}
437 
438 	return IRQ_NONE;
439 }
440 
441 static irqreturn_t rpr0521_trigger_consumer_handler(int irq, void *p)
442 {
443 	struct iio_poll_func *pf = p;
444 	struct iio_dev *indio_dev = pf->indio_dev;
445 	struct rpr0521_data *data = iio_priv(indio_dev);
446 	int err;
447 
448 	/* Use irq timestamp when reasonable. */
449 	if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
450 		pf->timestamp = data->irq_timestamp;
451 		data->irq_timestamp = 0;
452 	}
453 	/* Other chained trigger polls get timestamp only here. */
454 	if (!pf->timestamp)
455 		pf->timestamp = iio_get_time_ns(indio_dev);
456 
457 	err = regmap_bulk_read(data->regmap, RPR0521_REG_PXS_DATA,
458 		data->scan.channels,
459 		(3 * 2) + 1);	/* 3 * 16-bit + (discarded) int clear reg. */
460 	if (!err)
461 		iio_push_to_buffers_with_timestamp(indio_dev,
462 						   &data->scan, pf->timestamp);
463 	else
464 		dev_err(&data->client->dev,
465 			"Trigger consumer can't read from sensor.\n");
466 	pf->timestamp = 0;
467 
468 	iio_trigger_notify_done(indio_dev->trig);
469 
470 	return IRQ_HANDLED;
471 }
472 
473 static int rpr0521_write_int_enable(struct rpr0521_data *data)
474 {
475 	int err;
476 
477 	/* Interrupt after each measurement */
478 	err = regmap_update_bits(data->regmap, RPR0521_REG_PXS_CTRL,
479 		RPR0521_PXS_PERSISTENCE_MASK,
480 		RPR0521_PXS_PERSISTENCE_DRDY);
481 	if (err) {
482 		dev_err(&data->client->dev, "PS control reg write fail.\n");
483 		return -EBUSY;
484 		}
485 
486 	/* Ignore latch and mode because of drdy */
487 	err = regmap_write(data->regmap, RPR0521_REG_INTERRUPT,
488 		RPR0521_INTERRUPT_INT_REASSERT_DISABLE |
489 		RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE |
490 		RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE
491 		);
492 	if (err) {
493 		dev_err(&data->client->dev, "Interrupt setup write fail.\n");
494 		return -EBUSY;
495 		}
496 
497 	return 0;
498 }
499 
500 static int rpr0521_write_int_disable(struct rpr0521_data *data)
501 {
502 	/* Don't care of clearing mode, assert and latch. */
503 	return regmap_write(data->regmap, RPR0521_REG_INTERRUPT,
504 				RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE |
505 				RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE
506 				);
507 }
508 
509 /*
510  * Trigger producer enable / disable. Note that there will be trigs only when
511  * measurement data is ready to be read.
512  */
513 static int rpr0521_pxs_drdy_set_state(struct iio_trigger *trigger,
514 	bool enable_drdy)
515 {
516 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
517 	struct rpr0521_data *data = iio_priv(indio_dev);
518 	int err;
519 
520 	if (enable_drdy)
521 		err = rpr0521_write_int_enable(data);
522 	else
523 		err = rpr0521_write_int_disable(data);
524 	if (err)
525 		dev_err(&data->client->dev, "rpr0521_pxs_drdy_set_state failed\n");
526 
527 	return err;
528 }
529 
530 static const struct iio_trigger_ops rpr0521_trigger_ops = {
531 	.set_trigger_state = rpr0521_pxs_drdy_set_state,
532 	};
533 
534 
535 static int rpr0521_buffer_preenable(struct iio_dev *indio_dev)
536 {
537 	int err;
538 	struct rpr0521_data *data = iio_priv(indio_dev);
539 
540 	mutex_lock(&data->lock);
541 	err = rpr0521_set_power_state(data, true,
542 		(RPR0521_MODE_PXS_MASK | RPR0521_MODE_ALS_MASK));
543 	mutex_unlock(&data->lock);
544 	if (err)
545 		dev_err(&data->client->dev, "_buffer_preenable fail\n");
546 
547 	return err;
548 }
549 
550 static int rpr0521_buffer_postdisable(struct iio_dev *indio_dev)
551 {
552 	int err;
553 	struct rpr0521_data *data = iio_priv(indio_dev);
554 
555 	mutex_lock(&data->lock);
556 	err = rpr0521_set_power_state(data, false,
557 		(RPR0521_MODE_PXS_MASK | RPR0521_MODE_ALS_MASK));
558 	mutex_unlock(&data->lock);
559 	if (err)
560 		dev_err(&data->client->dev, "_buffer_postdisable fail\n");
561 
562 	return err;
563 }
564 
565 static const struct iio_buffer_setup_ops rpr0521_buffer_setup_ops = {
566 	.preenable = rpr0521_buffer_preenable,
567 	.postdisable = rpr0521_buffer_postdisable,
568 };
569 
570 static int rpr0521_get_gain(struct rpr0521_data *data, int chan,
571 			    int *val, int *val2)
572 {
573 	int ret, reg, idx;
574 
575 	ret = regmap_read(data->regmap, rpr0521_gain[chan].reg, &reg);
576 	if (ret < 0)
577 		return ret;
578 
579 	idx = (rpr0521_gain[chan].mask & reg) >> rpr0521_gain[chan].shift;
580 	*val = rpr0521_gain[chan].gain[idx].scale;
581 	*val2 = rpr0521_gain[chan].gain[idx].uscale;
582 
583 	return 0;
584 }
585 
586 static int rpr0521_set_gain(struct rpr0521_data *data, int chan,
587 			    int val, int val2)
588 {
589 	int i, idx = -EINVAL;
590 
591 	/* get gain index */
592 	for (i = 0; i < rpr0521_gain[chan].size; i++)
593 		if (val == rpr0521_gain[chan].gain[i].scale &&
594 		    val2 == rpr0521_gain[chan].gain[i].uscale) {
595 			idx = i;
596 			break;
597 		}
598 
599 	if (idx < 0)
600 		return idx;
601 
602 	return regmap_update_bits(data->regmap, rpr0521_gain[chan].reg,
603 				  rpr0521_gain[chan].mask,
604 				  idx << rpr0521_gain[chan].shift);
605 }
606 
607 static int rpr0521_read_samp_freq(struct rpr0521_data *data,
608 				enum iio_chan_type chan_type,
609 			    int *val, int *val2)
610 {
611 	int reg, ret;
612 
613 	ret = regmap_read(data->regmap, RPR0521_REG_MODE_CTRL, &reg);
614 	if (ret < 0)
615 		return ret;
616 
617 	reg &= RPR0521_MODE_MEAS_TIME_MASK;
618 	if (reg >= ARRAY_SIZE(rpr0521_samp_freq_i))
619 		return -EINVAL;
620 
621 	switch (chan_type) {
622 	case IIO_INTENSITY:
623 		*val = rpr0521_samp_freq_i[reg].als_hz;
624 		*val2 = rpr0521_samp_freq_i[reg].als_uhz;
625 		return 0;
626 
627 	case IIO_PROXIMITY:
628 		*val = rpr0521_samp_freq_i[reg].pxs_hz;
629 		*val2 = rpr0521_samp_freq_i[reg].pxs_uhz;
630 		return 0;
631 
632 	default:
633 		return -EINVAL;
634 	}
635 }
636 
637 static int rpr0521_write_samp_freq_common(struct rpr0521_data *data,
638 				enum iio_chan_type chan_type,
639 				int val, int val2)
640 {
641 	int i;
642 
643 	/*
644 	 * Ignore channel
645 	 * both pxs and als are setup only to same freq because of simplicity
646 	 */
647 	switch (val) {
648 	case 0:
649 		i = 0;
650 		break;
651 
652 	case 2:
653 		if (val2 != 500000)
654 			return -EINVAL;
655 
656 		i = 11;
657 		break;
658 
659 	case 10:
660 		i = 6;
661 		break;
662 
663 	default:
664 		return -EINVAL;
665 	}
666 
667 	return regmap_update_bits(data->regmap,
668 		RPR0521_REG_MODE_CTRL,
669 		RPR0521_MODE_MEAS_TIME_MASK,
670 		i);
671 }
672 
673 static int rpr0521_read_ps_offset(struct rpr0521_data *data, int *offset)
674 {
675 	int ret;
676 	__le16 buffer;
677 
678 	ret = regmap_bulk_read(data->regmap,
679 		RPR0521_REG_PS_OFFSET_LSB, &buffer, sizeof(buffer));
680 
681 	if (ret < 0) {
682 		dev_err(&data->client->dev, "Failed to read PS OFFSET register\n");
683 		return ret;
684 	}
685 	*offset = le16_to_cpu(buffer);
686 
687 	return ret;
688 }
689 
690 static int rpr0521_write_ps_offset(struct rpr0521_data *data, int offset)
691 {
692 	int ret;
693 	__le16 buffer;
694 
695 	buffer = cpu_to_le16(offset & 0x3ff);
696 	ret = regmap_raw_write(data->regmap,
697 		RPR0521_REG_PS_OFFSET_LSB, &buffer, sizeof(buffer));
698 
699 	if (ret < 0) {
700 		dev_err(&data->client->dev, "Failed to write PS OFFSET register\n");
701 		return ret;
702 	}
703 
704 	return ret;
705 }
706 
707 static int rpr0521_read_raw(struct iio_dev *indio_dev,
708 			    struct iio_chan_spec const *chan, int *val,
709 			    int *val2, long mask)
710 {
711 	struct rpr0521_data *data = iio_priv(indio_dev);
712 	int ret;
713 	int busy;
714 	u8 device_mask;
715 	__le16 raw_data;
716 
717 	switch (mask) {
718 	case IIO_CHAN_INFO_RAW:
719 		if (chan->type != IIO_INTENSITY && chan->type != IIO_PROXIMITY)
720 			return -EINVAL;
721 
722 		busy = iio_device_claim_direct_mode(indio_dev);
723 		if (busy)
724 			return -EBUSY;
725 
726 		device_mask = rpr0521_data_reg[chan->address].device_mask;
727 
728 		mutex_lock(&data->lock);
729 		ret = rpr0521_set_power_state(data, true, device_mask);
730 		if (ret < 0)
731 			goto rpr0521_read_raw_out;
732 
733 		ret = regmap_bulk_read(data->regmap,
734 				       rpr0521_data_reg[chan->address].address,
735 				       &raw_data, sizeof(raw_data));
736 		if (ret < 0) {
737 			rpr0521_set_power_state(data, false, device_mask);
738 			goto rpr0521_read_raw_out;
739 		}
740 
741 		ret = rpr0521_set_power_state(data, false, device_mask);
742 
743 rpr0521_read_raw_out:
744 		mutex_unlock(&data->lock);
745 		iio_device_release_direct_mode(indio_dev);
746 		if (ret < 0)
747 			return ret;
748 
749 		*val = le16_to_cpu(raw_data);
750 
751 		return IIO_VAL_INT;
752 
753 	case IIO_CHAN_INFO_SCALE:
754 		mutex_lock(&data->lock);
755 		ret = rpr0521_get_gain(data, chan->address, val, val2);
756 		mutex_unlock(&data->lock);
757 		if (ret < 0)
758 			return ret;
759 
760 		return IIO_VAL_INT_PLUS_MICRO;
761 
762 	case IIO_CHAN_INFO_SAMP_FREQ:
763 		mutex_lock(&data->lock);
764 		ret = rpr0521_read_samp_freq(data, chan->type, val, val2);
765 		mutex_unlock(&data->lock);
766 		if (ret < 0)
767 			return ret;
768 
769 		return IIO_VAL_INT_PLUS_MICRO;
770 
771 	case IIO_CHAN_INFO_OFFSET:
772 		mutex_lock(&data->lock);
773 		ret = rpr0521_read_ps_offset(data, val);
774 		mutex_unlock(&data->lock);
775 		if (ret < 0)
776 			return ret;
777 
778 		return IIO_VAL_INT;
779 
780 	default:
781 		return -EINVAL;
782 	}
783 }
784 
785 static int rpr0521_write_raw(struct iio_dev *indio_dev,
786 			     struct iio_chan_spec const *chan, int val,
787 			     int val2, long mask)
788 {
789 	struct rpr0521_data *data = iio_priv(indio_dev);
790 	int ret;
791 
792 	switch (mask) {
793 	case IIO_CHAN_INFO_SCALE:
794 		mutex_lock(&data->lock);
795 		ret = rpr0521_set_gain(data, chan->address, val, val2);
796 		mutex_unlock(&data->lock);
797 
798 		return ret;
799 
800 	case IIO_CHAN_INFO_SAMP_FREQ:
801 		mutex_lock(&data->lock);
802 		ret = rpr0521_write_samp_freq_common(data, chan->type,
803 						     val, val2);
804 		mutex_unlock(&data->lock);
805 
806 		return ret;
807 
808 	case IIO_CHAN_INFO_OFFSET:
809 		mutex_lock(&data->lock);
810 		ret = rpr0521_write_ps_offset(data, val);
811 		mutex_unlock(&data->lock);
812 
813 		return ret;
814 
815 	default:
816 		return -EINVAL;
817 	}
818 }
819 
820 static const struct iio_info rpr0521_info = {
821 	.read_raw	= rpr0521_read_raw,
822 	.write_raw	= rpr0521_write_raw,
823 	.attrs		= &rpr0521_attribute_group,
824 };
825 
826 static int rpr0521_init(struct rpr0521_data *data)
827 {
828 	int ret;
829 	int id;
830 
831 	ret = regmap_read(data->regmap, RPR0521_REG_ID, &id);
832 	if (ret < 0) {
833 		dev_err(&data->client->dev, "Failed to read REG_ID register\n");
834 		return ret;
835 	}
836 
837 	if (id != RPR0521_MANUFACT_ID) {
838 		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
839 			id, RPR0521_MANUFACT_ID);
840 		return -ENODEV;
841 	}
842 
843 	/* set default measurement time - 100 ms for both ALS and PS */
844 	ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
845 				 RPR0521_MODE_MEAS_TIME_MASK,
846 				 RPR0521_DEFAULT_MEAS_TIME);
847 	if (ret) {
848 		pr_err("regmap_update_bits returned %d\n", ret);
849 		return ret;
850 	}
851 
852 #ifndef CONFIG_PM
853 	ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
854 	if (ret < 0)
855 		return ret;
856 	ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
857 	if (ret < 0)
858 		return ret;
859 #endif
860 
861 	data->irq_timestamp = 0;
862 
863 	return 0;
864 }
865 
866 static int rpr0521_poweroff(struct rpr0521_data *data)
867 {
868 	int ret;
869 	int tmp;
870 
871 	ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
872 				 RPR0521_MODE_ALS_MASK |
873 				 RPR0521_MODE_PXS_MASK,
874 				 RPR0521_MODE_ALS_DISABLE |
875 				 RPR0521_MODE_PXS_DISABLE);
876 	if (ret < 0)
877 		return ret;
878 
879 	data->als_dev_en = false;
880 	data->pxs_dev_en = false;
881 
882 	/*
883 	 * Int pin keeps state after power off. Set pin to high impedance
884 	 * mode to prevent power drain.
885 	 */
886 	ret = regmap_read(data->regmap, RPR0521_REG_INTERRUPT, &tmp);
887 	if (ret) {
888 		dev_err(&data->client->dev, "Failed to reset int pin.\n");
889 		return ret;
890 	}
891 
892 	return 0;
893 }
894 
895 static bool rpr0521_is_volatile_reg(struct device *dev, unsigned int reg)
896 {
897 	switch (reg) {
898 	case RPR0521_REG_MODE_CTRL:
899 	case RPR0521_REG_ALS_CTRL:
900 	case RPR0521_REG_PXS_CTRL:
901 		return false;
902 	default:
903 		return true;
904 	}
905 }
906 
907 static const struct regmap_config rpr0521_regmap_config = {
908 	.name		= RPR0521_REGMAP_NAME,
909 
910 	.reg_bits	= 8,
911 	.val_bits	= 8,
912 
913 	.max_register	= RPR0521_REG_ID,
914 	.cache_type	= REGCACHE_RBTREE,
915 	.volatile_reg	= rpr0521_is_volatile_reg,
916 };
917 
918 static int rpr0521_probe(struct i2c_client *client)
919 {
920 	struct rpr0521_data *data;
921 	struct iio_dev *indio_dev;
922 	struct regmap *regmap;
923 	int ret;
924 
925 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
926 	if (!indio_dev)
927 		return -ENOMEM;
928 
929 	regmap = devm_regmap_init_i2c(client, &rpr0521_regmap_config);
930 	if (IS_ERR(regmap)) {
931 		dev_err(&client->dev, "regmap_init failed!\n");
932 		return PTR_ERR(regmap);
933 	}
934 
935 	data = iio_priv(indio_dev);
936 	i2c_set_clientdata(client, indio_dev);
937 	data->client = client;
938 	data->regmap = regmap;
939 
940 	mutex_init(&data->lock);
941 
942 	indio_dev->info = &rpr0521_info;
943 	indio_dev->name = RPR0521_DRV_NAME;
944 	indio_dev->channels = rpr0521_channels;
945 	indio_dev->num_channels = ARRAY_SIZE(rpr0521_channels);
946 	indio_dev->modes = INDIO_DIRECT_MODE;
947 
948 	ret = rpr0521_init(data);
949 	if (ret < 0) {
950 		dev_err(&client->dev, "rpr0521 chip init failed\n");
951 		return ret;
952 	}
953 
954 	ret = pm_runtime_set_active(&client->dev);
955 	if (ret < 0)
956 		goto err_poweroff;
957 
958 	pm_runtime_enable(&client->dev);
959 	pm_runtime_set_autosuspend_delay(&client->dev, RPR0521_SLEEP_DELAY_MS);
960 	pm_runtime_use_autosuspend(&client->dev);
961 
962 	/*
963 	 * If sensor write/read is needed in _probe after _use_autosuspend,
964 	 * sensor needs to be _resumed first using rpr0521_set_power_state().
965 	 */
966 
967 	/* IRQ to trigger setup */
968 	if (client->irq) {
969 		/* Trigger0 producer setup */
970 		data->drdy_trigger0 = devm_iio_trigger_alloc(
971 			indio_dev->dev.parent,
972 			"%s-dev%d", indio_dev->name, iio_device_id(indio_dev));
973 		if (!data->drdy_trigger0) {
974 			ret = -ENOMEM;
975 			goto err_pm_disable;
976 		}
977 		data->drdy_trigger0->ops = &rpr0521_trigger_ops;
978 		indio_dev->available_scan_masks = rpr0521_available_scan_masks;
979 		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
980 
981 		/* Ties irq to trigger producer handler. */
982 		ret = devm_request_threaded_irq(&client->dev, client->irq,
983 			rpr0521_drdy_irq_handler, rpr0521_drdy_irq_thread,
984 			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
985 			RPR0521_IRQ_NAME, indio_dev);
986 		if (ret < 0) {
987 			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
988 				client->irq);
989 			goto err_pm_disable;
990 			}
991 
992 		ret = devm_iio_trigger_register(indio_dev->dev.parent,
993 						data->drdy_trigger0);
994 		if (ret) {
995 			dev_err(&client->dev, "iio trigger register failed\n");
996 			goto err_pm_disable;
997 		}
998 
999 		/*
1000 		 * Now whole pipe from physical interrupt (irq defined by
1001 		 * devicetree to device) to trigger0 output is set up.
1002 		 */
1003 
1004 		/* Trigger consumer setup */
1005 		ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,
1006 			indio_dev,
1007 			iio_pollfunc_store_time,
1008 			rpr0521_trigger_consumer_handler,
1009 			&rpr0521_buffer_setup_ops);
1010 		if (ret < 0) {
1011 			dev_err(&client->dev, "iio triggered buffer setup failed\n");
1012 			goto err_pm_disable;
1013 		}
1014 	}
1015 
1016 	ret = iio_device_register(indio_dev);
1017 	if (ret)
1018 		goto err_pm_disable;
1019 
1020 	return 0;
1021 
1022 err_pm_disable:
1023 	pm_runtime_disable(&client->dev);
1024 	pm_runtime_set_suspended(&client->dev);
1025 err_poweroff:
1026 	rpr0521_poweroff(data);
1027 
1028 	return ret;
1029 }
1030 
1031 static void rpr0521_remove(struct i2c_client *client)
1032 {
1033 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1034 
1035 	iio_device_unregister(indio_dev);
1036 
1037 	pm_runtime_disable(&client->dev);
1038 	pm_runtime_set_suspended(&client->dev);
1039 
1040 	rpr0521_poweroff(iio_priv(indio_dev));
1041 }
1042 
1043 static int rpr0521_runtime_suspend(struct device *dev)
1044 {
1045 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1046 	struct rpr0521_data *data = iio_priv(indio_dev);
1047 	int ret;
1048 
1049 	mutex_lock(&data->lock);
1050 	/* If measurements are enabled, enable them on resume */
1051 	if (!data->als_need_dis)
1052 		data->als_ps_need_en = data->als_dev_en;
1053 	if (!data->pxs_need_dis)
1054 		data->pxs_ps_need_en = data->pxs_dev_en;
1055 
1056 	/* disable channels and sets {als,pxs}_dev_en to false */
1057 	ret = rpr0521_poweroff(data);
1058 	regcache_mark_dirty(data->regmap);
1059 	mutex_unlock(&data->lock);
1060 
1061 	return ret;
1062 }
1063 
1064 static int rpr0521_runtime_resume(struct device *dev)
1065 {
1066 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1067 	struct rpr0521_data *data = iio_priv(indio_dev);
1068 	int ret;
1069 
1070 	regcache_sync(data->regmap);
1071 	if (data->als_ps_need_en) {
1072 		ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
1073 		if (ret < 0)
1074 			return ret;
1075 		data->als_ps_need_en = false;
1076 	}
1077 
1078 	if (data->pxs_ps_need_en) {
1079 		ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
1080 		if (ret < 0)
1081 			return ret;
1082 		data->pxs_ps_need_en = false;
1083 	}
1084 	msleep(100);	//wait for first measurement result
1085 
1086 	return 0;
1087 }
1088 
1089 static const struct dev_pm_ops rpr0521_pm_ops = {
1090 	RUNTIME_PM_OPS(rpr0521_runtime_suspend, rpr0521_runtime_resume, NULL)
1091 };
1092 
1093 static const struct acpi_device_id rpr0521_acpi_match[] = {
1094 	{"RPR0521", 0},
1095 	{ }
1096 };
1097 MODULE_DEVICE_TABLE(acpi, rpr0521_acpi_match);
1098 
1099 static const struct i2c_device_id rpr0521_id[] = {
1100 	{ "rpr0521" },
1101 	{ }
1102 };
1103 
1104 MODULE_DEVICE_TABLE(i2c, rpr0521_id);
1105 
1106 static struct i2c_driver rpr0521_driver = {
1107 	.driver = {
1108 		.name	= RPR0521_DRV_NAME,
1109 		.pm	= pm_ptr(&rpr0521_pm_ops),
1110 		.acpi_match_table = rpr0521_acpi_match,
1111 	},
1112 	.probe		= rpr0521_probe,
1113 	.remove		= rpr0521_remove,
1114 	.id_table	= rpr0521_id,
1115 };
1116 
1117 module_i2c_driver(rpr0521_driver);
1118 
1119 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1120 MODULE_DESCRIPTION("RPR0521 ROHM Ambient Light and Proximity Sensor driver");
1121 MODULE_LICENSE("GPL v2");
1122