1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/module.h>
23#include <linux/i2c.h>
24#include <linux/regmap.h>
25#include <linux/iio/iio.h>
26#include <linux/iio/sysfs.h>
27#include <linux/iio/events.h>
28#include <linux/iio/consumer.h>
29#include <linux/iio/types.h>
30#include <linux/init.h>
31#include <linux/delay.h>
32#include <linux/regulator/consumer.h>
33#include <linux/pm_runtime.h>
34#include <linux/interrupt.h>
35#include <linux/bits.h>
36#include <linux/math64.h>
37#include <linux/pm.h>
38
39#define GP2AP002_PROX_CHANNEL 0
40#define GP2AP002_ALS_CHANNEL 1
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63#define GP2AP002_PROX 0x00
64#define GP2AP002_GAIN 0x01
65#define GP2AP002_HYS 0x02
66#define GP2AP002_CYCLE 0x03
67#define GP2AP002_OPMOD 0x04
68#define GP2AP002_CON 0x06
69
70#define GP2AP002_PROX_VO_DETECT BIT(0)
71
72
73#define GP2AP002_GAIN_LED_NORMAL BIT(3)
74
75
76
77
78
79#define GP2AP002_HYS_HYSD_SHIFT 7
80#define GP2AP002_HYS_HYSD_MASK BIT(7)
81#define GP2AP002_HYS_HYSC_SHIFT 5
82#define GP2AP002_HYS_HYSC_MASK GENMASK(6, 5)
83#define GP2AP002_HYS_HYSF_SHIFT 0
84#define GP2AP002_HYS_HYSF_MASK GENMASK(3, 0)
85#define GP2AP002_HYS_MASK (GP2AP002_HYS_HYSD_MASK | \
86 GP2AP002_HYS_HYSC_MASK | \
87 GP2AP002_HYS_HYSF_MASK)
88
89
90
91
92
93
94#define GP2AP002_CYCLE_CYCL_SHIFT 3
95#define GP2AP002_CYCLE_CYCL_MASK GENMASK(5, 3)
96
97
98
99
100
101
102#define GP2AP002_CYCLE_OSC_EFFECTIVE 0
103#define GP2AP002_CYCLE_OSC_INEFFECTIVE BIT(2)
104#define GP2AP002_CYCLE_OSC_MASK BIT(2)
105
106
107#define GP2AP002_OPMOD_ASD BIT(4)
108
109#define GP2AP002_OPMOD_SSD_OPERATING BIT(0)
110
111#define GP2AP002_OPMOD_VCON_IRQ BIT(1)
112#define GP2AP002_OPMOD_MASK (BIT(0) | BIT(1) | BIT(4))
113
114
115
116
117
118
119
120#define GP2AP002_CON_OCON_SHIFT 3
121#define GP2AP002_CON_OCON_ENABLE (0x0 << GP2AP002_CON_OCON_SHIFT)
122#define GP2AP002_CON_OCON_LOW (0x2 << GP2AP002_CON_OCON_SHIFT)
123#define GP2AP002_CON_OCON_HIGH (0x3 << GP2AP002_CON_OCON_SHIFT)
124#define GP2AP002_CON_OCON_MASK (0x3 << GP2AP002_CON_OCON_SHIFT)
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140struct gp2ap002 {
141 struct regmap *map;
142 struct device *dev;
143 struct regulator *vdd;
144 struct regulator *vio;
145 struct iio_channel *alsout;
146 u8 hys_far;
147 u8 hys_close;
148 bool is_gp2ap002s00f;
149 int irq;
150 bool enabled;
151};
152
153static irqreturn_t gp2ap002_prox_irq(int irq, void *d)
154{
155 struct iio_dev *indio_dev = d;
156 struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
157 u64 ev;
158 int val;
159 int ret;
160
161 if (!gp2ap002->enabled)
162 goto err_retrig;
163
164 ret = regmap_read(gp2ap002->map, GP2AP002_PROX, &val);
165 if (ret) {
166 dev_err(gp2ap002->dev, "error reading proximity\n");
167 goto err_retrig;
168 }
169
170 if (val & GP2AP002_PROX_VO_DETECT) {
171
172 dev_dbg(gp2ap002->dev, "close\n");
173 ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
174 gp2ap002->hys_far);
175 if (ret)
176 dev_err(gp2ap002->dev,
177 "error setting up proximity hysteresis\n");
178 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
179 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING);
180 } else {
181
182 dev_dbg(gp2ap002->dev, "far\n");
183 ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
184 gp2ap002->hys_close);
185 if (ret)
186 dev_err(gp2ap002->dev,
187 "error setting up proximity hysteresis\n");
188 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
189 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING);
190 }
191 iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
192
193
194
195
196
197
198
199
200 usleep_range(20000, 30000);
201
202err_retrig:
203 ret = regmap_write(gp2ap002->map, GP2AP002_CON,
204 GP2AP002_CON_OCON_ENABLE);
205 if (ret)
206 dev_err(gp2ap002->dev, "error setting up VOUT control\n");
207
208 return IRQ_HANDLED;
209}
210
211
212
213
214
215
216
217
218
219
220
221static const u16 gp2ap002_illuminance_table[] = {
222 0, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 63, 79,
223 100, 126, 158, 200, 251, 316, 398, 501, 631, 794, 1000, 1259, 1585,
224 1995, 2512, 3162, 3981, 5012, 6310, 7943, 10000, 12589, 15849, 19953,
225 25119, 31623, 39811, 50119,
226};
227
228static int gp2ap002_get_lux(struct gp2ap002 *gp2ap002)
229{
230 int ret, res;
231 u16 lux;
232
233 ret = iio_read_channel_processed(gp2ap002->alsout, &res);
234 if (ret < 0)
235 return ret;
236
237 dev_dbg(gp2ap002->dev, "read %d mA from ADC\n", res);
238
239
240 res = clamp(res, 0, (int)ARRAY_SIZE(gp2ap002_illuminance_table) - 1);
241 lux = gp2ap002_illuminance_table[res];
242
243 return (int)lux;
244}
245
246static int gp2ap002_read_raw(struct iio_dev *indio_dev,
247 struct iio_chan_spec const *chan,
248 int *val, int *val2, long mask)
249{
250 struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
251 int ret;
252
253 pm_runtime_get_sync(gp2ap002->dev);
254
255 switch (mask) {
256 case IIO_CHAN_INFO_RAW:
257 switch (chan->type) {
258 case IIO_LIGHT:
259 ret = gp2ap002_get_lux(gp2ap002);
260 if (ret < 0)
261 return ret;
262 *val = ret;
263 ret = IIO_VAL_INT;
264 goto out;
265 default:
266 ret = -EINVAL;
267 goto out;
268 }
269 default:
270 ret = -EINVAL;
271 }
272
273out:
274 pm_runtime_mark_last_busy(gp2ap002->dev);
275 pm_runtime_put_autosuspend(gp2ap002->dev);
276
277 return ret;
278}
279
280static int gp2ap002_init(struct gp2ap002 *gp2ap002)
281{
282 int ret;
283
284
285 ret = regmap_write(gp2ap002->map, GP2AP002_GAIN,
286 GP2AP002_GAIN_LED_NORMAL);
287 if (ret) {
288 dev_err(gp2ap002->dev, "error setting up LED gain\n");
289 return ret;
290 }
291 ret = regmap_write(gp2ap002->map, GP2AP002_HYS, gp2ap002->hys_far);
292 if (ret) {
293 dev_err(gp2ap002->dev,
294 "error setting up proximity hysteresis\n");
295 return ret;
296 }
297
298
299 ret = regmap_write(gp2ap002->map, GP2AP002_CYCLE,
300 GP2AP002_CYCLE_OSC_INEFFECTIVE);
301 if (ret) {
302 dev_err(gp2ap002->dev,
303 "error setting up internal frequency hopping\n");
304 return ret;
305 }
306
307
308 ret = regmap_write(gp2ap002->map, GP2AP002_OPMOD,
309 GP2AP002_OPMOD_SSD_OPERATING |
310 GP2AP002_OPMOD_VCON_IRQ);
311 if (ret) {
312 dev_err(gp2ap002->dev, "error setting up operation mode\n");
313 return ret;
314 }
315
316
317 ret = regmap_write(gp2ap002->map, GP2AP002_CON,
318 GP2AP002_CON_OCON_ENABLE);
319 if (ret)
320 dev_err(gp2ap002->dev, "error setting up VOUT control\n");
321
322 return ret;
323}
324
325static int gp2ap002_read_event_config(struct iio_dev *indio_dev,
326 const struct iio_chan_spec *chan,
327 enum iio_event_type type,
328 enum iio_event_direction dir)
329{
330 struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
331
332
333
334
335
336 return gp2ap002->enabled;
337}
338
339static int gp2ap002_write_event_config(struct iio_dev *indio_dev,
340 const struct iio_chan_spec *chan,
341 enum iio_event_type type,
342 enum iio_event_direction dir,
343 int state)
344{
345 struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
346
347 if (state) {
348
349
350
351
352
353 pm_runtime_get_sync(gp2ap002->dev);
354 gp2ap002->enabled = true;
355 } else {
356 pm_runtime_mark_last_busy(gp2ap002->dev);
357 pm_runtime_put_autosuspend(gp2ap002->dev);
358 gp2ap002->enabled = false;
359 }
360
361 return 0;
362}
363
364static const struct iio_info gp2ap002_info = {
365 .read_raw = gp2ap002_read_raw,
366 .read_event_config = gp2ap002_read_event_config,
367 .write_event_config = gp2ap002_write_event_config,
368};
369
370static const struct iio_event_spec gp2ap002_events[] = {
371 {
372 .type = IIO_EV_TYPE_THRESH,
373 .dir = IIO_EV_DIR_EITHER,
374 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
375 },
376};
377
378static const struct iio_chan_spec gp2ap002_channels[] = {
379 {
380 .type = IIO_PROXIMITY,
381 .event_spec = gp2ap002_events,
382 .num_event_specs = ARRAY_SIZE(gp2ap002_events),
383 },
384 {
385 .type = IIO_LIGHT,
386 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
387 .channel = GP2AP002_ALS_CHANNEL,
388 },
389};
390
391
392
393
394
395
396
397
398static int gp2ap002_regmap_i2c_read(void *context, unsigned int reg,
399 unsigned int *val)
400{
401 struct device *dev = context;
402 struct i2c_client *i2c = to_i2c_client(dev);
403 int ret;
404
405 ret = i2c_smbus_read_word_data(i2c, reg);
406 if (ret < 0)
407 return ret;
408
409 *val = (ret >> 8) & 0xFF;
410
411 return 0;
412}
413
414static int gp2ap002_regmap_i2c_write(void *context, unsigned int reg,
415 unsigned int val)
416{
417 struct device *dev = context;
418 struct i2c_client *i2c = to_i2c_client(dev);
419
420 return i2c_smbus_write_byte_data(i2c, reg, val);
421}
422
423static struct regmap_bus gp2ap002_regmap_bus = {
424 .reg_read = gp2ap002_regmap_i2c_read,
425 .reg_write = gp2ap002_regmap_i2c_write,
426};
427
428static int gp2ap002_probe(struct i2c_client *client,
429 const struct i2c_device_id *id)
430{
431 struct gp2ap002 *gp2ap002;
432 struct iio_dev *indio_dev;
433 struct device *dev = &client->dev;
434 enum iio_chan_type ch_type;
435 static const struct regmap_config config = {
436 .reg_bits = 8,
437 .val_bits = 8,
438 .max_register = GP2AP002_CON,
439 };
440 struct regmap *regmap;
441 int num_chan;
442 const char *compat;
443 u8 val;
444 int ret;
445
446 indio_dev = devm_iio_device_alloc(dev, sizeof(*gp2ap002));
447 if (!indio_dev)
448 return -ENOMEM;
449 i2c_set_clientdata(client, indio_dev);
450
451 gp2ap002 = iio_priv(indio_dev);
452 gp2ap002->dev = dev;
453
454
455
456
457
458
459 ret = device_property_read_string(dev, "compatible", &compat);
460 if (ret) {
461 dev_err(dev, "cannot check compatible\n");
462 return ret;
463 }
464 gp2ap002->is_gp2ap002s00f = !strcmp(compat, "sharp,gp2ap002s00f");
465
466 regmap = devm_regmap_init(dev, &gp2ap002_regmap_bus, dev, &config);
467 if (IS_ERR(regmap)) {
468 dev_err(dev, "Failed to register i2c regmap %ld\n", PTR_ERR(regmap));
469 return PTR_ERR(regmap);
470 }
471 gp2ap002->map = regmap;
472
473
474
475
476
477
478
479
480
481
482
483
484 ret = device_property_read_u8(dev, "sharp,proximity-far-hysteresis",
485 &val);
486 if (ret) {
487 dev_err(dev, "failed to obtain proximity far setting\n");
488 return ret;
489 }
490 dev_dbg(dev, "proximity far setting %02x\n", val);
491 gp2ap002->hys_far = val;
492
493 ret = device_property_read_u8(dev, "sharp,proximity-close-hysteresis",
494 &val);
495 if (ret) {
496 dev_err(dev, "failed to obtain proximity close setting\n");
497 return ret;
498 }
499 dev_dbg(dev, "proximity close setting %02x\n", val);
500 gp2ap002->hys_close = val;
501
502
503 if (!gp2ap002->is_gp2ap002s00f) {
504 gp2ap002->alsout = devm_iio_channel_get(dev, "alsout");
505 if (IS_ERR(gp2ap002->alsout)) {
506 if (PTR_ERR(gp2ap002->alsout) == -ENODEV) {
507 dev_err(dev, "no ADC, deferring...\n");
508 return -EPROBE_DEFER;
509 }
510 dev_err(dev, "failed to get ALSOUT ADC channel\n");
511 return PTR_ERR(gp2ap002->alsout);
512 }
513 ret = iio_get_channel_type(gp2ap002->alsout, &ch_type);
514 if (ret < 0)
515 return ret;
516 if (ch_type != IIO_CURRENT) {
517 dev_err(dev,
518 "wrong type of IIO channel specified for ALSOUT\n");
519 return -EINVAL;
520 }
521 }
522
523 gp2ap002->vdd = devm_regulator_get(dev, "vdd");
524 if (IS_ERR(gp2ap002->vdd)) {
525 dev_err(dev, "failed to get VDD regulator\n");
526 return PTR_ERR(gp2ap002->vdd);
527 }
528 gp2ap002->vio = devm_regulator_get(dev, "vio");
529 if (IS_ERR(gp2ap002->vio)) {
530 dev_err(dev, "failed to get VIO regulator\n");
531 return PTR_ERR(gp2ap002->vio);
532 }
533
534
535 ret = regulator_set_voltage(gp2ap002->vdd, 2400000, 3600000);
536 if (ret) {
537 dev_err(dev, "failed to sett VDD voltage\n");
538 return ret;
539 }
540
541
542 ret = regulator_get_voltage(gp2ap002->vdd);
543 if (ret < 0) {
544 dev_err(dev, "failed to get VDD voltage\n");
545 return ret;
546 }
547 ret = regulator_set_voltage(gp2ap002->vio, 1650000, ret);
548 if (ret) {
549 dev_err(dev, "failed to set VIO voltage\n");
550 return ret;
551 }
552
553 ret = regulator_enable(gp2ap002->vdd);
554 if (ret) {
555 dev_err(dev, "failed to enable VDD regulator\n");
556 return ret;
557 }
558 ret = regulator_enable(gp2ap002->vio);
559 if (ret) {
560 dev_err(dev, "failed to enable VIO regulator\n");
561 goto out_disable_vdd;
562 }
563
564 msleep(20);
565
566
567
568
569
570 ret = gp2ap002_init(gp2ap002);
571 if (ret) {
572 dev_err(dev, "initialization failed\n");
573 goto out_disable_vio;
574 }
575 pm_runtime_get_noresume(dev);
576 pm_runtime_set_active(dev);
577 pm_runtime_enable(dev);
578 gp2ap002->enabled = false;
579
580 ret = devm_request_threaded_irq(dev, client->irq, NULL,
581 gp2ap002_prox_irq, IRQF_ONESHOT,
582 "gp2ap002", indio_dev);
583 if (ret) {
584 dev_err(dev, "unable to request IRQ\n");
585 goto out_put_pm;
586 }
587 gp2ap002->irq = client->irq;
588
589
590
591
592
593
594 pm_runtime_set_autosuspend_delay(dev, 1000);
595 pm_runtime_use_autosuspend(dev);
596 pm_runtime_put(dev);
597
598 indio_dev->info = &gp2ap002_info;
599 indio_dev->name = "gp2ap002";
600 indio_dev->channels = gp2ap002_channels;
601
602 num_chan = ARRAY_SIZE(gp2ap002_channels);
603 if (gp2ap002->is_gp2ap002s00f)
604 num_chan--;
605 indio_dev->num_channels = num_chan;
606 indio_dev->modes = INDIO_DIRECT_MODE;
607
608 ret = iio_device_register(indio_dev);
609 if (ret)
610 goto out_disable_pm;
611 dev_dbg(dev, "Sharp GP2AP002 probed successfully\n");
612
613 return 0;
614
615out_put_pm:
616 pm_runtime_put_noidle(dev);
617out_disable_pm:
618 pm_runtime_disable(dev);
619out_disable_vio:
620 regulator_disable(gp2ap002->vio);
621out_disable_vdd:
622 regulator_disable(gp2ap002->vdd);
623 return ret;
624}
625
626static int gp2ap002_remove(struct i2c_client *client)
627{
628 struct iio_dev *indio_dev = i2c_get_clientdata(client);
629 struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
630 struct device *dev = &client->dev;
631
632 pm_runtime_get_sync(dev);
633 pm_runtime_put_noidle(dev);
634 pm_runtime_disable(dev);
635 iio_device_unregister(indio_dev);
636 regulator_disable(gp2ap002->vio);
637 regulator_disable(gp2ap002->vdd);
638
639 return 0;
640}
641
642static int __maybe_unused gp2ap002_runtime_suspend(struct device *dev)
643{
644 struct iio_dev *indio_dev = dev_get_drvdata(dev);
645 struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
646 int ret;
647
648
649 disable_irq(gp2ap002->irq);
650
651
652 ret = regmap_write(gp2ap002->map, GP2AP002_OPMOD, 0x00);
653 if (ret) {
654 dev_err(gp2ap002->dev, "error setting up operation mode\n");
655 return ret;
656 }
657
658
659
660
661 regulator_disable(gp2ap002->vio);
662 regulator_disable(gp2ap002->vdd);
663
664 return 0;
665}
666
667static int __maybe_unused gp2ap002_runtime_resume(struct device *dev)
668{
669 struct iio_dev *indio_dev = dev_get_drvdata(dev);
670 struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
671 int ret;
672
673 ret = regulator_enable(gp2ap002->vdd);
674 if (ret) {
675 dev_err(dev, "failed to enable VDD regulator in resume path\n");
676 return ret;
677 }
678 ret = regulator_enable(gp2ap002->vio);
679 if (ret) {
680 dev_err(dev, "failed to enable VIO regulator in resume path\n");
681 return ret;
682 }
683
684 msleep(20);
685
686 ret = gp2ap002_init(gp2ap002);
687 if (ret) {
688 dev_err(dev, "re-initialization failed\n");
689 return ret;
690 }
691
692
693 enable_irq(gp2ap002->irq);
694
695 return 0;
696}
697
698static const struct dev_pm_ops gp2ap002_dev_pm_ops = {
699 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
700 pm_runtime_force_resume)
701 SET_RUNTIME_PM_OPS(gp2ap002_runtime_suspend,
702 gp2ap002_runtime_resume, NULL)
703};
704
705static const struct i2c_device_id gp2ap002_id_table[] = {
706 { "gp2ap002", 0 },
707 { },
708};
709MODULE_DEVICE_TABLE(i2c, gp2ap002_id_table);
710
711static const struct of_device_id gp2ap002_of_match[] = {
712 { .compatible = "sharp,gp2ap002a00f" },
713 { .compatible = "sharp,gp2ap002s00f" },
714 { },
715};
716MODULE_DEVICE_TABLE(of, gp2ap002_of_match);
717
718static struct i2c_driver gp2ap002_driver = {
719 .driver = {
720 .name = "gp2ap002",
721 .of_match_table = gp2ap002_of_match,
722 .pm = &gp2ap002_dev_pm_ops,
723 },
724 .probe = gp2ap002_probe,
725 .remove = gp2ap002_remove,
726 .id_table = gp2ap002_id_table,
727};
728module_i2c_driver(gp2ap002_driver);
729
730MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
731MODULE_DESCRIPTION("GP2AP002 ambient light and proximity sensor driver");
732MODULE_LICENSE("GPL v2");
733