blob: 0aac4bc156f60481d88a90f054030332b160da20 [file] [log] [blame]
Daniel Balutaefa86e9f2015-06-17 12:42:51 +03001/*
2 * RPR-0521 ROHM Ambient Light and Proximity Sensor
3 *
4 * Copyright (c) 2015, Intel Corporation.
5 *
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.
9 *
10 * IIO driver for RPR-0521RS (7-bit I2C slave address 0x38).
11 *
Mikko Koivunen484c3142017-05-18 15:12:51 +030012 * TODO: illuminance channel, buffer
Daniel Balutaefa86e9f2015-06-17 12:42:51 +030013 */
14
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>
21
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/pm_runtime.h>
25
26#define RPR0521_REG_SYSTEM_CTRL 0x40
27#define RPR0521_REG_MODE_CTRL 0x41
28#define RPR0521_REG_ALS_CTRL 0x42
29#define RPR0521_REG_PXS_CTRL 0x43
30#define RPR0521_REG_PXS_DATA 0x44 /* 16-bit, little endian */
31#define RPR0521_REG_ALS_DATA0 0x46 /* 16-bit, little endian */
32#define RPR0521_REG_ALS_DATA1 0x48 /* 16-bit, little endian */
33#define RPR0521_REG_ID 0x92
34
35#define RPR0521_MODE_ALS_MASK BIT(7)
36#define RPR0521_MODE_PXS_MASK BIT(6)
37#define RPR0521_MODE_MEAS_TIME_MASK GENMASK(3, 0)
38#define RPR0521_ALS_DATA0_GAIN_MASK GENMASK(5, 4)
39#define RPR0521_ALS_DATA0_GAIN_SHIFT 4
40#define RPR0521_ALS_DATA1_GAIN_MASK GENMASK(3, 2)
41#define RPR0521_ALS_DATA1_GAIN_SHIFT 2
42#define RPR0521_PXS_GAIN_MASK GENMASK(5, 4)
43#define RPR0521_PXS_GAIN_SHIFT 4
44
45#define RPR0521_MODE_ALS_ENABLE BIT(7)
46#define RPR0521_MODE_ALS_DISABLE 0x00
47#define RPR0521_MODE_PXS_ENABLE BIT(6)
48#define RPR0521_MODE_PXS_DISABLE 0x00
49
50#define RPR0521_MANUFACT_ID 0xE0
51#define RPR0521_DEFAULT_MEAS_TIME 0x06 /* ALS - 100ms, PXS - 100ms */
52
53#define RPR0521_DRV_NAME "RPR0521"
54#define RPR0521_REGMAP_NAME "rpr0521_regmap"
55
56#define RPR0521_SLEEP_DELAY_MS 2000
57
58#define RPR0521_ALS_SCALE_AVAIL "0.007812 0.015625 0.5 1"
59#define RPR0521_PXS_SCALE_AVAIL "0.125 0.5 1"
60
61struct rpr0521_gain {
62 int scale;
63 int uscale;
64};
65
66static const struct rpr0521_gain rpr0521_als_gain[4] = {
67 {1, 0}, /* x1 */
68 {0, 500000}, /* x2 */
69 {0, 15625}, /* x64 */
70 {0, 7812}, /* x128 */
71};
72
73static const struct rpr0521_gain rpr0521_pxs_gain[3] = {
74 {1, 0}, /* x1 */
75 {0, 500000}, /* x2 */
76 {0, 125000}, /* x4 */
77};
78
79enum rpr0521_channel {
80 RPR0521_CHAN_ALS_DATA0,
81 RPR0521_CHAN_ALS_DATA1,
82 RPR0521_CHAN_PXS,
83};
84
85struct rpr0521_reg_desc {
86 u8 address;
87 u8 device_mask;
88};
89
90static const struct rpr0521_reg_desc rpr0521_data_reg[] = {
91 [RPR0521_CHAN_ALS_DATA0] = {
92 .address = RPR0521_REG_ALS_DATA0,
93 .device_mask = RPR0521_MODE_ALS_MASK,
94 },
95 [RPR0521_CHAN_ALS_DATA1] = {
96 .address = RPR0521_REG_ALS_DATA1,
97 .device_mask = RPR0521_MODE_ALS_MASK,
98 },
99 [RPR0521_CHAN_PXS] = {
100 .address = RPR0521_REG_PXS_DATA,
101 .device_mask = RPR0521_MODE_PXS_MASK,
102 },
103};
104
105static const struct rpr0521_gain_info {
106 u8 reg;
107 u8 mask;
108 u8 shift;
109 const struct rpr0521_gain *gain;
110 int size;
111} rpr0521_gain[] = {
112 [RPR0521_CHAN_ALS_DATA0] = {
113 .reg = RPR0521_REG_ALS_CTRL,
114 .mask = RPR0521_ALS_DATA0_GAIN_MASK,
115 .shift = RPR0521_ALS_DATA0_GAIN_SHIFT,
116 .gain = rpr0521_als_gain,
117 .size = ARRAY_SIZE(rpr0521_als_gain),
118 },
119 [RPR0521_CHAN_ALS_DATA1] = {
120 .reg = RPR0521_REG_ALS_CTRL,
121 .mask = RPR0521_ALS_DATA1_GAIN_MASK,
122 .shift = RPR0521_ALS_DATA1_GAIN_SHIFT,
123 .gain = rpr0521_als_gain,
124 .size = ARRAY_SIZE(rpr0521_als_gain),
125 },
126 [RPR0521_CHAN_PXS] = {
127 .reg = RPR0521_REG_PXS_CTRL,
128 .mask = RPR0521_PXS_GAIN_MASK,
129 .shift = RPR0521_PXS_GAIN_SHIFT,
130 .gain = rpr0521_pxs_gain,
131 .size = ARRAY_SIZE(rpr0521_pxs_gain),
132 },
133};
134
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300135struct rpr0521_samp_freq {
136 int als_hz;
137 int als_uhz;
138 int pxs_hz;
139 int pxs_uhz;
140};
141
142static const struct rpr0521_samp_freq rpr0521_samp_freq_i[13] = {
143/* {ALS, PXS}, W==currently writable option */
144 {0, 0, 0, 0}, /* W0000, 0=standby */
145 {0, 0, 100, 0}, /* 0001 */
146 {0, 0, 25, 0}, /* 0010 */
147 {0, 0, 10, 0}, /* 0011 */
148 {0, 0, 2, 500000}, /* 0100 */
149 {10, 0, 20, 0}, /* 0101 */
150 {10, 0, 10, 0}, /* W0110 */
151 {10, 0, 2, 500000}, /* 0111 */
152 {2, 500000, 20, 0}, /* 1000, measurement 100ms, sleep 300ms */
153 {2, 500000, 10, 0}, /* 1001, measurement 100ms, sleep 300ms */
154 {2, 500000, 0, 0}, /* 1010, high sensitivity mode */
155 {2, 500000, 2, 500000}, /* W1011, high sensitivity mode */
156 {20, 0, 20, 0} /* 1100, ALS_data x 0.5, see specification P.18 */
157};
158
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300159struct rpr0521_data {
160 struct i2c_client *client;
161
162 /* protect device params updates (e.g state, gain) */
163 struct mutex lock;
164
165 /* device active status */
166 bool als_dev_en;
167 bool pxs_dev_en;
168
Mikko Koivunen484c3142017-05-18 15:12:51 +0300169 /* optimize runtime pm ops - enable/disable device only if needed */
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300170 bool als_ps_need_en;
171 bool pxs_ps_need_en;
Mikko Koivunen484c3142017-05-18 15:12:51 +0300172 bool als_need_dis;
173 bool pxs_need_dis;
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300174
175 struct regmap *regmap;
176};
177
178static IIO_CONST_ATTR(in_intensity_scale_available, RPR0521_ALS_SCALE_AVAIL);
179static IIO_CONST_ATTR(in_proximity_scale_available, RPR0521_PXS_SCALE_AVAIL);
180
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300181/*
182 * Start with easy freq first, whole table of freq combinations is more
183 * complicated.
184 */
185static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("2.5 10");
186
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300187static struct attribute *rpr0521_attributes[] = {
188 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
189 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300190 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300191 NULL,
192};
193
194static const struct attribute_group rpr0521_attribute_group = {
195 .attrs = rpr0521_attributes,
196};
197
198static const struct iio_chan_spec rpr0521_channels[] = {
199 {
200 .type = IIO_INTENSITY,
201 .modified = 1,
202 .address = RPR0521_CHAN_ALS_DATA0,
203 .channel2 = IIO_MOD_LIGHT_BOTH,
204 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
205 BIT(IIO_CHAN_INFO_SCALE),
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300206 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300207 },
208 {
209 .type = IIO_INTENSITY,
210 .modified = 1,
211 .address = RPR0521_CHAN_ALS_DATA1,
212 .channel2 = IIO_MOD_LIGHT_IR,
213 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
214 BIT(IIO_CHAN_INFO_SCALE),
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300215 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300216 },
217 {
218 .type = IIO_PROXIMITY,
219 .address = RPR0521_CHAN_PXS,
220 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
221 BIT(IIO_CHAN_INFO_SCALE),
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300222 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300223 }
224};
225
226static int rpr0521_als_enable(struct rpr0521_data *data, u8 status)
227{
228 int ret;
229
230 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
231 RPR0521_MODE_ALS_MASK,
232 status);
233 if (ret < 0)
234 return ret;
235
Mikko Koivunenf87fa262017-05-18 15:12:49 +0300236 if (status & RPR0521_MODE_ALS_MASK)
237 data->als_dev_en = true;
238 else
239 data->als_dev_en = false;
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300240
241 return 0;
242}
243
244static int rpr0521_pxs_enable(struct rpr0521_data *data, u8 status)
245{
246 int ret;
247
248 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
249 RPR0521_MODE_PXS_MASK,
250 status);
251 if (ret < 0)
252 return ret;
253
Mikko Koivunenf87fa262017-05-18 15:12:49 +0300254 if (status & RPR0521_MODE_PXS_MASK)
255 data->pxs_dev_en = true;
256 else
257 data->pxs_dev_en = false;
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300258
259 return 0;
260}
261
262/**
263 * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
264 *
265 * @data: rpr0521 device private data
266 * @on: state to be set for devices in @device_mask
267 * @device_mask: bitmask specifying for which device we need to update @on state
268 *
Mikko Koivunen484c3142017-05-18 15:12:51 +0300269 * Calls for this function must be balanced so that each ON should have matching
270 * OFF. Otherwise pm usage_count gets out of sync.
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300271 */
272static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
273 u8 device_mask)
274{
275#ifdef CONFIG_PM
276 int ret;
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300277
278 if (device_mask & RPR0521_MODE_ALS_MASK) {
Mikko Koivunen484c3142017-05-18 15:12:51 +0300279 data->als_ps_need_en = on;
280 data->als_need_dis = !on;
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300281 }
282
283 if (device_mask & RPR0521_MODE_PXS_MASK) {
Mikko Koivunen484c3142017-05-18 15:12:51 +0300284 data->pxs_ps_need_en = on;
285 data->pxs_need_dis = !on;
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300286 }
287
Mikko Koivunen484c3142017-05-18 15:12:51 +0300288 /*
289 * On: _resume() is called only when we are suspended
290 * Off: _suspend() is called after delay if _resume() is not
291 * called before that.
292 * Note: If either measurement is re-enabled before _suspend(),
293 * both stay enabled until _suspend().
294 */
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300295 if (on) {
296 ret = pm_runtime_get_sync(&data->client->dev);
297 } else {
298 pm_runtime_mark_last_busy(&data->client->dev);
299 ret = pm_runtime_put_autosuspend(&data->client->dev);
300 }
301 if (ret < 0) {
302 dev_err(&data->client->dev,
303 "Failed: rpr0521_set_power_state for %d, ret %d\n",
304 on, ret);
305 if (on)
306 pm_runtime_put_noidle(&data->client->dev);
307
308 return ret;
309 }
Mikko Koivunen484c3142017-05-18 15:12:51 +0300310
311 if (on) {
312 /* If _resume() was not called, enable measurement now. */
313 if (data->als_ps_need_en) {
314 ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
315 if (ret)
316 return ret;
317 data->als_ps_need_en = false;
318 }
319
320 if (data->pxs_ps_need_en) {
321 ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
322 if (ret)
323 return ret;
324 data->pxs_ps_need_en = false;
325 }
326 }
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300327#endif
328 return 0;
329}
330
331static int rpr0521_get_gain(struct rpr0521_data *data, int chan,
332 int *val, int *val2)
333{
334 int ret, reg, idx;
335
336 ret = regmap_read(data->regmap, rpr0521_gain[chan].reg, &reg);
337 if (ret < 0)
338 return ret;
339
340 idx = (rpr0521_gain[chan].mask & reg) >> rpr0521_gain[chan].shift;
341 *val = rpr0521_gain[chan].gain[idx].scale;
342 *val2 = rpr0521_gain[chan].gain[idx].uscale;
343
344 return 0;
345}
346
347static int rpr0521_set_gain(struct rpr0521_data *data, int chan,
348 int val, int val2)
349{
350 int i, idx = -EINVAL;
351
352 /* get gain index */
353 for (i = 0; i < rpr0521_gain[chan].size; i++)
354 if (val == rpr0521_gain[chan].gain[i].scale &&
355 val2 == rpr0521_gain[chan].gain[i].uscale) {
356 idx = i;
357 break;
358 }
359
360 if (idx < 0)
361 return idx;
362
363 return regmap_update_bits(data->regmap, rpr0521_gain[chan].reg,
364 rpr0521_gain[chan].mask,
365 idx << rpr0521_gain[chan].shift);
366}
367
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300368static int rpr0521_read_samp_freq(struct rpr0521_data *data,
369 enum iio_chan_type chan_type,
370 int *val, int *val2)
371{
372 int reg, ret;
373
374 ret = regmap_read(data->regmap, RPR0521_REG_MODE_CTRL, &reg);
375 if (ret < 0)
376 return ret;
377
378 reg &= RPR0521_MODE_MEAS_TIME_MASK;
379 if (reg >= ARRAY_SIZE(rpr0521_samp_freq_i))
380 return -EINVAL;
381
382 switch (chan_type) {
383 case IIO_INTENSITY:
384 *val = rpr0521_samp_freq_i[reg].als_hz;
385 *val2 = rpr0521_samp_freq_i[reg].als_uhz;
386 return 0;
387
388 case IIO_PROXIMITY:
389 *val = rpr0521_samp_freq_i[reg].pxs_hz;
390 *val2 = rpr0521_samp_freq_i[reg].pxs_uhz;
391 return 0;
392
393 default:
394 return -EINVAL;
395 }
396}
397
398static int rpr0521_write_samp_freq_common(struct rpr0521_data *data,
399 enum iio_chan_type chan_type,
400 int val, int val2)
401{
402 int i;
403
404 /*
405 * Ignore channel
406 * both pxs and als are setup only to same freq because of simplicity
407 */
408 switch (val) {
409 case 0:
410 i = 0;
411 break;
412
413 case 2:
414 if (val2 != 500000)
415 return -EINVAL;
416
417 i = 11;
418 break;
419
420 case 10:
421 i = 6;
422 break;
423
424 default:
425 return -EINVAL;
426 }
427
428 return regmap_update_bits(data->regmap,
429 RPR0521_REG_MODE_CTRL,
430 RPR0521_MODE_MEAS_TIME_MASK,
431 i);
432}
433
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300434static int rpr0521_read_raw(struct iio_dev *indio_dev,
435 struct iio_chan_spec const *chan, int *val,
436 int *val2, long mask)
437{
438 struct rpr0521_data *data = iio_priv(indio_dev);
439 int ret;
440 u8 device_mask;
441 __le16 raw_data;
442
443 switch (mask) {
444 case IIO_CHAN_INFO_RAW:
445 if (chan->type != IIO_INTENSITY && chan->type != IIO_PROXIMITY)
446 return -EINVAL;
447
448 device_mask = rpr0521_data_reg[chan->address].device_mask;
449
450 mutex_lock(&data->lock);
451 ret = rpr0521_set_power_state(data, true, device_mask);
452 if (ret < 0) {
453 mutex_unlock(&data->lock);
454 return ret;
455 }
456
457 ret = regmap_bulk_read(data->regmap,
458 rpr0521_data_reg[chan->address].address,
Mikko Koivunenae591652017-05-18 15:12:52 +0300459 &raw_data, sizeof(raw_data));
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300460 if (ret < 0) {
461 rpr0521_set_power_state(data, false, device_mask);
462 mutex_unlock(&data->lock);
463 return ret;
464 }
465
466 ret = rpr0521_set_power_state(data, false, device_mask);
467 mutex_unlock(&data->lock);
468 if (ret < 0)
469 return ret;
470
471 *val = le16_to_cpu(raw_data);
472
473 return IIO_VAL_INT;
Mikko Koivunen624c3892017-05-18 15:12:53 +0300474
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300475 case IIO_CHAN_INFO_SCALE:
476 mutex_lock(&data->lock);
477 ret = rpr0521_get_gain(data, chan->address, val, val2);
478 mutex_unlock(&data->lock);
479 if (ret < 0)
480 return ret;
481
482 return IIO_VAL_INT_PLUS_MICRO;
Mikko Koivunen624c3892017-05-18 15:12:53 +0300483
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300484 case IIO_CHAN_INFO_SAMP_FREQ:
485 mutex_lock(&data->lock);
486 ret = rpr0521_read_samp_freq(data, chan->type, val, val2);
487 mutex_unlock(&data->lock);
488 if (ret < 0)
489 return ret;
490
491 return IIO_VAL_INT_PLUS_MICRO;
492
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300493 default:
494 return -EINVAL;
495 }
496}
497
498static int rpr0521_write_raw(struct iio_dev *indio_dev,
499 struct iio_chan_spec const *chan, int val,
500 int val2, long mask)
501{
502 struct rpr0521_data *data = iio_priv(indio_dev);
503 int ret;
504
505 switch (mask) {
506 case IIO_CHAN_INFO_SCALE:
507 mutex_lock(&data->lock);
508 ret = rpr0521_set_gain(data, chan->address, val, val2);
509 mutex_unlock(&data->lock);
510
511 return ret;
Mikko Koivunen624c3892017-05-18 15:12:53 +0300512
Mikko Koivunenc91a88e2017-05-18 15:12:54 +0300513 case IIO_CHAN_INFO_SAMP_FREQ:
514 mutex_lock(&data->lock);
515 ret = rpr0521_write_samp_freq_common(data, chan->type,
516 val, val2);
517 mutex_unlock(&data->lock);
518
519 return ret;
520
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300521 default:
522 return -EINVAL;
523 }
524}
525
526static const struct iio_info rpr0521_info = {
527 .driver_module = THIS_MODULE,
528 .read_raw = rpr0521_read_raw,
529 .write_raw = rpr0521_write_raw,
530 .attrs = &rpr0521_attribute_group,
531};
532
533static int rpr0521_init(struct rpr0521_data *data)
534{
535 int ret;
536 int id;
537
538 ret = regmap_read(data->regmap, RPR0521_REG_ID, &id);
539 if (ret < 0) {
540 dev_err(&data->client->dev, "Failed to read REG_ID register\n");
541 return ret;
542 }
543
544 if (id != RPR0521_MANUFACT_ID) {
545 dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
546 id, RPR0521_MANUFACT_ID);
547 return -ENODEV;
548 }
549
550 /* set default measurement time - 100 ms for both ALS and PS */
551 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
552 RPR0521_MODE_MEAS_TIME_MASK,
553 RPR0521_DEFAULT_MEAS_TIME);
554 if (ret) {
555 pr_err("regmap_update_bits returned %d\n", ret);
556 return ret;
557 }
558
Mikko Koivunen484c3142017-05-18 15:12:51 +0300559#ifndef CONFIG_PM
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300560 ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
561 if (ret < 0)
562 return ret;
563 ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
564 if (ret < 0)
565 return ret;
Mikko Koivunen484c3142017-05-18 15:12:51 +0300566#endif
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300567
568 return 0;
569}
570
571static int rpr0521_poweroff(struct rpr0521_data *data)
572{
573 int ret;
574
575 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
576 RPR0521_MODE_ALS_MASK |
577 RPR0521_MODE_PXS_MASK,
578 RPR0521_MODE_ALS_DISABLE |
579 RPR0521_MODE_PXS_DISABLE);
580 if (ret < 0)
581 return ret;
582
583 data->als_dev_en = false;
584 data->pxs_dev_en = false;
585
586 return 0;
587}
588
589static bool rpr0521_is_volatile_reg(struct device *dev, unsigned int reg)
590{
591 switch (reg) {
592 case RPR0521_REG_MODE_CTRL:
593 case RPR0521_REG_ALS_CTRL:
594 case RPR0521_REG_PXS_CTRL:
595 return false;
596 default:
597 return true;
598 }
599}
600
601static const struct regmap_config rpr0521_regmap_config = {
602 .name = RPR0521_REGMAP_NAME,
603
604 .reg_bits = 8,
605 .val_bits = 8,
606
607 .max_register = RPR0521_REG_ID,
608 .cache_type = REGCACHE_RBTREE,
609 .volatile_reg = rpr0521_is_volatile_reg,
610};
611
612static int rpr0521_probe(struct i2c_client *client,
613 const struct i2c_device_id *id)
614{
615 struct rpr0521_data *data;
616 struct iio_dev *indio_dev;
617 struct regmap *regmap;
618 int ret;
619
620 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
621 if (!indio_dev)
622 return -ENOMEM;
623
624 regmap = devm_regmap_init_i2c(client, &rpr0521_regmap_config);
625 if (IS_ERR(regmap)) {
626 dev_err(&client->dev, "regmap_init failed!\n");
627 return PTR_ERR(regmap);
628 }
629
630 data = iio_priv(indio_dev);
631 i2c_set_clientdata(client, indio_dev);
632 data->client = client;
633 data->regmap = regmap;
634
635 mutex_init(&data->lock);
636
637 indio_dev->dev.parent = &client->dev;
638 indio_dev->info = &rpr0521_info;
639 indio_dev->name = RPR0521_DRV_NAME;
640 indio_dev->channels = rpr0521_channels;
641 indio_dev->num_channels = ARRAY_SIZE(rpr0521_channels);
642 indio_dev->modes = INDIO_DIRECT_MODE;
643
644 ret = rpr0521_init(data);
645 if (ret < 0) {
646 dev_err(&client->dev, "rpr0521 chip init failed\n");
647 return ret;
648 }
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300649
650 ret = pm_runtime_set_active(&client->dev);
651 if (ret < 0)
Mikko Koivunen12d74942017-05-18 15:12:50 +0300652 goto err_poweroff;
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300653
654 pm_runtime_enable(&client->dev);
655 pm_runtime_set_autosuspend_delay(&client->dev, RPR0521_SLEEP_DELAY_MS);
656 pm_runtime_use_autosuspend(&client->dev);
657
Mikko Koivunen12d74942017-05-18 15:12:50 +0300658 ret = iio_device_register(indio_dev);
659 if (ret)
660 goto err_pm_disable;
661
662 return 0;
663
664err_pm_disable:
665 pm_runtime_disable(&client->dev);
666 pm_runtime_set_suspended(&client->dev);
667 pm_runtime_put_noidle(&client->dev);
668err_poweroff:
669 rpr0521_poweroff(data);
670
671 return ret;
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300672}
673
674static int rpr0521_remove(struct i2c_client *client)
675{
676 struct iio_dev *indio_dev = i2c_get_clientdata(client);
677
Adriana Reus7d0ead52015-11-05 16:25:29 +0200678 iio_device_unregister(indio_dev);
679
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300680 pm_runtime_disable(&client->dev);
681 pm_runtime_set_suspended(&client->dev);
682 pm_runtime_put_noidle(&client->dev);
683
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300684 rpr0521_poweroff(iio_priv(indio_dev));
685
686 return 0;
687}
688
689#ifdef CONFIG_PM
690static int rpr0521_runtime_suspend(struct device *dev)
691{
692 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
693 struct rpr0521_data *data = iio_priv(indio_dev);
694 int ret;
695
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300696 mutex_lock(&data->lock);
Mikko Koivunen484c3142017-05-18 15:12:51 +0300697 /* If measurements are enabled, enable them on resume */
698 if (!data->als_need_dis)
699 data->als_ps_need_en = data->als_dev_en;
700 if (!data->pxs_need_dis)
701 data->pxs_ps_need_en = data->pxs_dev_en;
702
703 /* disable channels and sets {als,pxs}_dev_en to false */
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300704 ret = rpr0521_poweroff(data);
Mikko Koivunen484c3142017-05-18 15:12:51 +0300705 regcache_mark_dirty(data->regmap);
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300706 mutex_unlock(&data->lock);
707
708 return ret;
709}
710
711static int rpr0521_runtime_resume(struct device *dev)
712{
713 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
714 struct rpr0521_data *data = iio_priv(indio_dev);
715 int ret;
716
Mikko Koivunen484c3142017-05-18 15:12:51 +0300717 regcache_sync(data->regmap);
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300718 if (data->als_ps_need_en) {
719 ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
720 if (ret < 0)
721 return ret;
722 data->als_ps_need_en = false;
723 }
724
725 if (data->pxs_ps_need_en) {
726 ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
727 if (ret < 0)
728 return ret;
729 data->pxs_ps_need_en = false;
730 }
Mikko Koivunen484c3142017-05-18 15:12:51 +0300731 msleep(100); //wait for first measurement result
Daniel Balutaefa86e9f2015-06-17 12:42:51 +0300732
733 return 0;
734}
735#endif
736
737static const struct dev_pm_ops rpr0521_pm_ops = {
738 SET_RUNTIME_PM_OPS(rpr0521_runtime_suspend,
739 rpr0521_runtime_resume, NULL)
740};
741
742static const struct acpi_device_id rpr0521_acpi_match[] = {
743 {"RPR0521", 0},
744 { }
745};
746MODULE_DEVICE_TABLE(acpi, rpr0521_acpi_match);
747
748static const struct i2c_device_id rpr0521_id[] = {
749 {"rpr0521", 0},
750 { }
751};
752
753MODULE_DEVICE_TABLE(i2c, rpr0521_id);
754
755static struct i2c_driver rpr0521_driver = {
756 .driver = {
757 .name = RPR0521_DRV_NAME,
758 .pm = &rpr0521_pm_ops,
759 .acpi_match_table = ACPI_PTR(rpr0521_acpi_match),
760 },
761 .probe = rpr0521_probe,
762 .remove = rpr0521_remove,
763 .id_table = rpr0521_id,
764};
765
766module_i2c_driver(rpr0521_driver);
767
768MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
769MODULE_DESCRIPTION("RPR0521 ROHM Ambient Light and Proximity Sensor driver");
770MODULE_LICENSE("GPL v2");