This source file includes following definitions.
- isl29028_find_prox_sleep_index
- isl29028_set_proxim_sampling
- isl29028_enable_proximity
- isl29028_set_als_scale
- isl29028_set_als_ir_mode
- isl29028_read_als_ir
- isl29028_read_proxim
- isl29028_als_get
- isl29028_ir_get
- isl29028_set_pm_runtime_busy
- isl29028_write_raw
- isl29028_read_raw
- isl29028_clear_configure_reg
- isl29028_is_volatile_reg
- isl29028_probe
- isl29028_remove
- isl29028_suspend
- isl29028_resume
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/regmap.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/pm_runtime.h>
24
25 #define ISL29028_CONV_TIME_MS 100
26
27 #define ISL29028_REG_CONFIGURE 0x01
28
29 #define ISL29028_CONF_ALS_IR_MODE_ALS 0
30 #define ISL29028_CONF_ALS_IR_MODE_IR BIT(0)
31 #define ISL29028_CONF_ALS_IR_MODE_MASK BIT(0)
32
33 #define ISL29028_CONF_ALS_RANGE_LOW_LUX 0
34 #define ISL29028_CONF_ALS_RANGE_HIGH_LUX BIT(1)
35 #define ISL29028_CONF_ALS_RANGE_MASK BIT(1)
36
37 #define ISL29028_CONF_ALS_DIS 0
38 #define ISL29028_CONF_ALS_EN BIT(2)
39 #define ISL29028_CONF_ALS_EN_MASK BIT(2)
40
41 #define ISL29028_CONF_PROX_SLP_SH 4
42 #define ISL29028_CONF_PROX_SLP_MASK (7 << ISL29028_CONF_PROX_SLP_SH)
43
44 #define ISL29028_CONF_PROX_EN BIT(7)
45 #define ISL29028_CONF_PROX_EN_MASK BIT(7)
46
47 #define ISL29028_REG_INTERRUPT 0x02
48
49 #define ISL29028_REG_PROX_DATA 0x08
50 #define ISL29028_REG_ALSIR_L 0x09
51 #define ISL29028_REG_ALSIR_U 0x0A
52
53 #define ISL29028_REG_TEST1_MODE 0x0E
54 #define ISL29028_REG_TEST2_MODE 0x0F
55
56 #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
57
58 #define ISL29028_POWER_OFF_DELAY_MS 2000
59
60 struct isl29028_prox_data {
61 int sampling_int;
62 int sampling_fract;
63 int sleep_time;
64 };
65
66 static const struct isl29028_prox_data isl29028_prox_data[] = {
67 { 1, 250000, 800 },
68 { 2, 500000, 400 },
69 { 5, 0, 200 },
70 { 10, 0, 100 },
71 { 13, 300000, 75 },
72 { 20, 0, 50 },
73 { 80, 0, 13 },
74
75
76
77 { 100, 0, 0 }
78 };
79
80 enum isl29028_als_ir_mode {
81 ISL29028_MODE_NONE = 0,
82 ISL29028_MODE_ALS,
83 ISL29028_MODE_IR,
84 };
85
86 struct isl29028_chip {
87 struct mutex lock;
88 struct regmap *regmap;
89 int prox_sampling_int;
90 int prox_sampling_frac;
91 bool enable_prox;
92 int lux_scale;
93 enum isl29028_als_ir_mode als_ir_mode;
94 };
95
96 static int isl29028_find_prox_sleep_index(int sampling_int, int sampling_fract)
97 {
98 int i;
99
100 for (i = 0; i < ARRAY_SIZE(isl29028_prox_data); ++i) {
101 if (isl29028_prox_data[i].sampling_int == sampling_int &&
102 isl29028_prox_data[i].sampling_fract == sampling_fract)
103 return i;
104 }
105
106 return -EINVAL;
107 }
108
109 static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
110 int sampling_int, int sampling_fract)
111 {
112 struct device *dev = regmap_get_device(chip->regmap);
113 int sleep_index, ret;
114
115 sleep_index = isl29028_find_prox_sleep_index(sampling_int,
116 sampling_fract);
117 if (sleep_index < 0)
118 return sleep_index;
119
120 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
121 ISL29028_CONF_PROX_SLP_MASK,
122 sleep_index << ISL29028_CONF_PROX_SLP_SH);
123
124 if (ret < 0) {
125 dev_err(dev, "%s(): Error %d setting the proximity sampling\n",
126 __func__, ret);
127 return ret;
128 }
129
130 chip->prox_sampling_int = sampling_int;
131 chip->prox_sampling_frac = sampling_fract;
132
133 return ret;
134 }
135
136 static int isl29028_enable_proximity(struct isl29028_chip *chip)
137 {
138 int prox_index, ret;
139
140 ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling_int,
141 chip->prox_sampling_frac);
142 if (ret < 0)
143 return ret;
144
145 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
146 ISL29028_CONF_PROX_EN_MASK,
147 ISL29028_CONF_PROX_EN);
148 if (ret < 0)
149 return ret;
150
151
152 prox_index = isl29028_find_prox_sleep_index(chip->prox_sampling_int,
153 chip->prox_sampling_frac);
154 if (prox_index < 0)
155 return prox_index;
156
157 msleep(isl29028_prox_data[prox_index].sleep_time);
158
159 return 0;
160 }
161
162 static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
163 {
164 struct device *dev = regmap_get_device(chip->regmap);
165 int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX :
166 ISL29028_CONF_ALS_RANGE_LOW_LUX;
167 int ret;
168
169 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
170 ISL29028_CONF_ALS_RANGE_MASK, val);
171 if (ret < 0) {
172 dev_err(dev, "%s(): Error %d setting the ALS scale\n", __func__,
173 ret);
174 return ret;
175 }
176
177 chip->lux_scale = lux_scale;
178
179 return ret;
180 }
181
182 static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
183 enum isl29028_als_ir_mode mode)
184 {
185 int ret;
186
187 if (chip->als_ir_mode == mode)
188 return 0;
189
190 ret = isl29028_set_als_scale(chip, chip->lux_scale);
191 if (ret < 0)
192 return ret;
193
194 switch (mode) {
195 case ISL29028_MODE_ALS:
196 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
197 ISL29028_CONF_ALS_IR_MODE_MASK,
198 ISL29028_CONF_ALS_IR_MODE_ALS);
199 if (ret < 0)
200 return ret;
201
202 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
203 ISL29028_CONF_ALS_RANGE_MASK,
204 ISL29028_CONF_ALS_RANGE_HIGH_LUX);
205 break;
206 case ISL29028_MODE_IR:
207 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
208 ISL29028_CONF_ALS_IR_MODE_MASK,
209 ISL29028_CONF_ALS_IR_MODE_IR);
210 break;
211 case ISL29028_MODE_NONE:
212 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
213 ISL29028_CONF_ALS_EN_MASK,
214 ISL29028_CONF_ALS_DIS);
215 }
216
217 if (ret < 0)
218 return ret;
219
220
221 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
222 ISL29028_CONF_ALS_EN_MASK,
223 ISL29028_CONF_ALS_EN);
224 if (ret < 0)
225 return ret;
226
227
228 msleep(ISL29028_CONV_TIME_MS);
229
230 chip->als_ir_mode = mode;
231
232 return 0;
233 }
234
235 static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
236 {
237 struct device *dev = regmap_get_device(chip->regmap);
238 unsigned int lsb;
239 unsigned int msb;
240 int ret;
241
242 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
243 if (ret < 0) {
244 dev_err(dev,
245 "%s(): Error %d reading register ALSIR_L\n",
246 __func__, ret);
247 return ret;
248 }
249
250 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
251 if (ret < 0) {
252 dev_err(dev,
253 "%s(): Error %d reading register ALSIR_U\n",
254 __func__, ret);
255 return ret;
256 }
257
258 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
259
260 return 0;
261 }
262
263 static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
264 {
265 struct device *dev = regmap_get_device(chip->regmap);
266 unsigned int data;
267 int ret;
268
269 if (!chip->enable_prox) {
270 ret = isl29028_enable_proximity(chip);
271 if (ret < 0)
272 return ret;
273
274 chip->enable_prox = true;
275 }
276
277 ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
278 if (ret < 0) {
279 dev_err(dev, "%s(): Error %d reading register PROX_DATA\n",
280 __func__, ret);
281 return ret;
282 }
283
284 *prox = data;
285
286 return 0;
287 }
288
289 static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
290 {
291 struct device *dev = regmap_get_device(chip->regmap);
292 int ret;
293 int als_ir_data;
294
295 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS);
296 if (ret < 0) {
297 dev_err(dev, "%s(): Error %d enabling ALS mode\n", __func__,
298 ret);
299 return ret;
300 }
301
302 ret = isl29028_read_als_ir(chip, &als_ir_data);
303 if (ret < 0)
304 return ret;
305
306
307
308
309
310
311 if (chip->lux_scale == 125)
312 als_ir_data = (als_ir_data * 31) / 1000;
313 else
314 als_ir_data = (als_ir_data * 49) / 100;
315
316 *als_data = als_ir_data;
317
318 return 0;
319 }
320
321 static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
322 {
323 struct device *dev = regmap_get_device(chip->regmap);
324 int ret;
325
326 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR);
327 if (ret < 0) {
328 dev_err(dev, "%s(): Error %d enabling IR mode\n", __func__,
329 ret);
330 return ret;
331 }
332
333 return isl29028_read_als_ir(chip, ir_data);
334 }
335
336 static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
337 {
338 struct device *dev = regmap_get_device(chip->regmap);
339 int ret;
340
341 if (on) {
342 ret = pm_runtime_get_sync(dev);
343 if (ret < 0)
344 pm_runtime_put_noidle(dev);
345 } else {
346 pm_runtime_mark_last_busy(dev);
347 ret = pm_runtime_put_autosuspend(dev);
348 }
349
350 return ret;
351 }
352
353
354 static int isl29028_write_raw(struct iio_dev *indio_dev,
355 struct iio_chan_spec const *chan,
356 int val, int val2, long mask)
357 {
358 struct isl29028_chip *chip = iio_priv(indio_dev);
359 struct device *dev = regmap_get_device(chip->regmap);
360 int ret;
361
362 ret = isl29028_set_pm_runtime_busy(chip, true);
363 if (ret < 0)
364 return ret;
365
366 mutex_lock(&chip->lock);
367
368 ret = -EINVAL;
369 switch (chan->type) {
370 case IIO_PROXIMITY:
371 if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
372 dev_err(dev,
373 "%s(): proximity: Mask value 0x%08lx is not supported\n",
374 __func__, mask);
375 break;
376 }
377
378 if (val < 1 || val > 100) {
379 dev_err(dev,
380 "%s(): proximity: Sampling frequency %d is not in the range [1:100]\n",
381 __func__, val);
382 break;
383 }
384
385 ret = isl29028_set_proxim_sampling(chip, val, val2);
386 break;
387 case IIO_LIGHT:
388 if (mask != IIO_CHAN_INFO_SCALE) {
389 dev_err(dev,
390 "%s(): light: Mask value 0x%08lx is not supported\n",
391 __func__, mask);
392 break;
393 }
394
395 if (val != 125 && val != 2000) {
396 dev_err(dev,
397 "%s(): light: Lux scale %d is not in the set {125, 2000}\n",
398 __func__, val);
399 break;
400 }
401
402 ret = isl29028_set_als_scale(chip, val);
403 break;
404 default:
405 dev_err(dev, "%s(): Unsupported channel type %x\n",
406 __func__, chan->type);
407 break;
408 }
409
410 mutex_unlock(&chip->lock);
411
412 if (ret < 0)
413 return ret;
414
415 ret = isl29028_set_pm_runtime_busy(chip, false);
416 if (ret < 0)
417 return ret;
418
419 return ret;
420 }
421
422 static int isl29028_read_raw(struct iio_dev *indio_dev,
423 struct iio_chan_spec const *chan,
424 int *val, int *val2, long mask)
425 {
426 struct isl29028_chip *chip = iio_priv(indio_dev);
427 struct device *dev = regmap_get_device(chip->regmap);
428 int ret, pm_ret;
429
430 ret = isl29028_set_pm_runtime_busy(chip, true);
431 if (ret < 0)
432 return ret;
433
434 mutex_lock(&chip->lock);
435
436 ret = -EINVAL;
437 switch (mask) {
438 case IIO_CHAN_INFO_RAW:
439 case IIO_CHAN_INFO_PROCESSED:
440 switch (chan->type) {
441 case IIO_LIGHT:
442 ret = isl29028_als_get(chip, val);
443 break;
444 case IIO_INTENSITY:
445 ret = isl29028_ir_get(chip, val);
446 break;
447 case IIO_PROXIMITY:
448 ret = isl29028_read_proxim(chip, val);
449 break;
450 default:
451 break;
452 }
453
454 if (ret < 0)
455 break;
456
457 ret = IIO_VAL_INT;
458 break;
459 case IIO_CHAN_INFO_SAMP_FREQ:
460 if (chan->type != IIO_PROXIMITY)
461 break;
462
463 *val = chip->prox_sampling_int;
464 *val2 = chip->prox_sampling_frac;
465 ret = IIO_VAL_INT;
466 break;
467 case IIO_CHAN_INFO_SCALE:
468 if (chan->type != IIO_LIGHT)
469 break;
470 *val = chip->lux_scale;
471 ret = IIO_VAL_INT;
472 break;
473 default:
474 dev_err(dev, "%s(): mask value 0x%08lx is not supported\n",
475 __func__, mask);
476 break;
477 }
478
479 mutex_unlock(&chip->lock);
480
481 if (ret < 0)
482 return ret;
483
484
485
486
487
488
489 pm_ret = isl29028_set_pm_runtime_busy(chip, false);
490 if (pm_ret < 0)
491 return pm_ret;
492
493 return ret;
494 }
495
496 static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
497 "1.25 2.5 5 10 13.3 20 80 100");
498 static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000");
499
500 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
501 static struct attribute *isl29028_attributes[] = {
502 ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
503 ISL29028_CONST_ATTR(in_illuminance_scale_available),
504 NULL,
505 };
506
507 static const struct attribute_group isl29108_group = {
508 .attrs = isl29028_attributes,
509 };
510
511 static const struct iio_chan_spec isl29028_channels[] = {
512 {
513 .type = IIO_LIGHT,
514 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
515 BIT(IIO_CHAN_INFO_SCALE),
516 }, {
517 .type = IIO_INTENSITY,
518 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
519 }, {
520 .type = IIO_PROXIMITY,
521 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
522 BIT(IIO_CHAN_INFO_SAMP_FREQ),
523 }
524 };
525
526 static const struct iio_info isl29028_info = {
527 .attrs = &isl29108_group,
528 .read_raw = isl29028_read_raw,
529 .write_raw = isl29028_write_raw,
530 };
531
532 static int isl29028_clear_configure_reg(struct isl29028_chip *chip)
533 {
534 struct device *dev = regmap_get_device(chip->regmap);
535 int ret;
536
537 ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
538 if (ret < 0)
539 dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n",
540 __func__, ret);
541
542 chip->als_ir_mode = ISL29028_MODE_NONE;
543 chip->enable_prox = false;
544
545 return ret;
546 }
547
548 static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg)
549 {
550 switch (reg) {
551 case ISL29028_REG_INTERRUPT:
552 case ISL29028_REG_PROX_DATA:
553 case ISL29028_REG_ALSIR_L:
554 case ISL29028_REG_ALSIR_U:
555 return true;
556 default:
557 return false;
558 }
559 }
560
561 static const struct regmap_config isl29028_regmap_config = {
562 .reg_bits = 8,
563 .val_bits = 8,
564 .volatile_reg = isl29028_is_volatile_reg,
565 .max_register = ISL29028_NUM_REGS - 1,
566 .num_reg_defaults_raw = ISL29028_NUM_REGS,
567 .cache_type = REGCACHE_RBTREE,
568 };
569
570 static int isl29028_probe(struct i2c_client *client,
571 const struct i2c_device_id *id)
572 {
573 struct isl29028_chip *chip;
574 struct iio_dev *indio_dev;
575 int ret;
576
577 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
578 if (!indio_dev)
579 return -ENOMEM;
580
581 chip = iio_priv(indio_dev);
582
583 i2c_set_clientdata(client, indio_dev);
584 mutex_init(&chip->lock);
585
586 chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
587 if (IS_ERR(chip->regmap)) {
588 ret = PTR_ERR(chip->regmap);
589 dev_err(&client->dev, "%s: Error %d initializing regmap\n",
590 __func__, ret);
591 return ret;
592 }
593
594 chip->enable_prox = false;
595 chip->prox_sampling_int = 20;
596 chip->prox_sampling_frac = 0;
597 chip->lux_scale = 2000;
598
599 ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
600 if (ret < 0) {
601 dev_err(&client->dev,
602 "%s(): Error %d writing to TEST1_MODE register\n",
603 __func__, ret);
604 return ret;
605 }
606
607 ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
608 if (ret < 0) {
609 dev_err(&client->dev,
610 "%s(): Error %d writing to TEST2_MODE register\n",
611 __func__, ret);
612 return ret;
613 }
614
615 ret = isl29028_clear_configure_reg(chip);
616 if (ret < 0)
617 return ret;
618
619 indio_dev->info = &isl29028_info;
620 indio_dev->channels = isl29028_channels;
621 indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
622 indio_dev->name = id->name;
623 indio_dev->dev.parent = &client->dev;
624 indio_dev->modes = INDIO_DIRECT_MODE;
625
626 pm_runtime_enable(&client->dev);
627 pm_runtime_set_autosuspend_delay(&client->dev,
628 ISL29028_POWER_OFF_DELAY_MS);
629 pm_runtime_use_autosuspend(&client->dev);
630
631 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
632 if (ret < 0) {
633 dev_err(&client->dev,
634 "%s(): iio registration failed with error %d\n",
635 __func__, ret);
636 return ret;
637 }
638
639 return 0;
640 }
641
642 static int isl29028_remove(struct i2c_client *client)
643 {
644 struct iio_dev *indio_dev = i2c_get_clientdata(client);
645 struct isl29028_chip *chip = iio_priv(indio_dev);
646
647 iio_device_unregister(indio_dev);
648
649 pm_runtime_disable(&client->dev);
650 pm_runtime_set_suspended(&client->dev);
651 pm_runtime_put_noidle(&client->dev);
652
653 return isl29028_clear_configure_reg(chip);
654 }
655
656 static int __maybe_unused isl29028_suspend(struct device *dev)
657 {
658 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
659 struct isl29028_chip *chip = iio_priv(indio_dev);
660 int ret;
661
662 mutex_lock(&chip->lock);
663
664 ret = isl29028_clear_configure_reg(chip);
665
666 mutex_unlock(&chip->lock);
667
668 return ret;
669 }
670
671 static int __maybe_unused isl29028_resume(struct device *dev)
672 {
673
674
675
676
677
678 return 0;
679 }
680
681 static const struct dev_pm_ops isl29028_pm_ops = {
682 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
683 pm_runtime_force_resume)
684 SET_RUNTIME_PM_OPS(isl29028_suspend, isl29028_resume, NULL)
685 };
686
687 static const struct i2c_device_id isl29028_id[] = {
688 {"isl29028", 0},
689 {"isl29030", 0},
690 {}
691 };
692 MODULE_DEVICE_TABLE(i2c, isl29028_id);
693
694 static const struct of_device_id isl29028_of_match[] = {
695 { .compatible = "isl,isl29028", },
696 { .compatible = "isil,isl29028", },
697 { .compatible = "isil,isl29030", },
698 { },
699 };
700 MODULE_DEVICE_TABLE(of, isl29028_of_match);
701
702 static struct i2c_driver isl29028_driver = {
703 .driver = {
704 .name = "isl29028",
705 .pm = &isl29028_pm_ops,
706 .of_match_table = isl29028_of_match,
707 },
708 .probe = isl29028_probe,
709 .remove = isl29028_remove,
710 .id_table = isl29028_id,
711 };
712
713 module_i2c_driver(isl29028_driver);
714
715 MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
716 MODULE_LICENSE("GPL v2");
717 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");