1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/i2c.h>
28#include <linux/interrupt.h>
29#include <linux/mutex.h>
30#include <linux/platform_data/bh1770glc.h>
31#include <linux/regulator/consumer.h>
32#include <linux/pm_runtime.h>
33#include <linux/workqueue.h>
34#include <linux/delay.h>
35#include <linux/wait.h>
36#include <linux/slab.h>
37
38#define BH1770_ALS_CONTROL 0x80
39#define BH1770_PS_CONTROL 0x81
40#define BH1770_I_LED 0x82
41#define BH1770_I_LED3 0x83
42#define BH1770_ALS_PS_MEAS 0x84
43#define BH1770_PS_MEAS_RATE 0x85
44#define BH1770_ALS_MEAS_RATE 0x86
45#define BH1770_PART_ID 0x8a
46#define BH1770_MANUFACT_ID 0x8b
47#define BH1770_ALS_DATA_0 0x8c
48#define BH1770_ALS_DATA_1 0x8d
49#define BH1770_ALS_PS_STATUS 0x8e
50#define BH1770_PS_DATA_LED1 0x8f
51#define BH1770_PS_DATA_LED2 0x90
52#define BH1770_PS_DATA_LED3 0x91
53#define BH1770_INTERRUPT 0x92
54#define BH1770_PS_TH_LED1 0x93
55#define BH1770_PS_TH_LED2 0x94
56#define BH1770_PS_TH_LED3 0x95
57#define BH1770_ALS_TH_UP_0 0x96
58#define BH1770_ALS_TH_UP_1 0x97
59#define BH1770_ALS_TH_LOW_0 0x98
60#define BH1770_ALS_TH_LOW_1 0x99
61
62
63#define BH1770_MANUFACT_ROHM 0x01
64#define BH1770_MANUFACT_OSRAM 0x03
65
66
67#define BH1770_PART 0x90
68#define BH1770_PART_MASK 0xf0
69#define BH1770_REV_MASK 0x0f
70#define BH1770_REV_SHIFT 0
71#define BH1770_REV_0 0x00
72#define BH1770_REV_1 0x01
73
74
75#define BH1770_STANDBY 0x00
76#define BH1770_FORCED 0x02
77#define BH1770_STANDALONE 0x03
78#define BH1770_SWRESET (0x01 << 2)
79
80#define BH1770_PS_TRIG_MEAS (1 << 0)
81#define BH1770_ALS_TRIG_MEAS (1 << 1)
82
83
84#define BH1770_INT_OUTPUT_MODE (1 << 3)
85#define BH1770_INT_POLARITY (1 << 2)
86#define BH1770_INT_ALS_ENA (1 << 1)
87#define BH1770_INT_PS_ENA (1 << 0)
88
89
90#define BH1770_INT_LED1_DATA (1 << 0)
91#define BH1770_INT_LED1_INT (1 << 1)
92#define BH1770_INT_LED2_DATA (1 << 2)
93#define BH1770_INT_LED2_INT (1 << 3)
94#define BH1770_INT_LED3_DATA (1 << 4)
95#define BH1770_INT_LED3_INT (1 << 5)
96#define BH1770_INT_LEDS_INT ((1 << 1) | (1 << 3) | (1 << 5))
97#define BH1770_INT_ALS_DATA (1 << 6)
98#define BH1770_INT_ALS_INT (1 << 7)
99
100
101#define BH1770_LED1 0x00
102
103#define BH1770_DISABLE 0
104#define BH1770_ENABLE 1
105#define BH1770_PROX_CHANNELS 1
106
107#define BH1770_LUX_DEFAULT_RATE 1
108#define BH1770_PROX_DEFAULT_RATE 1
109#define BH1770_PROX_DEF_RATE_THRESH 6
110#define BH1770_STARTUP_DELAY 50
111#define BH1770_RESET_TIME 10
112#define BH1770_TIMEOUT 2100
113
114#define BH1770_LUX_RANGE 65535
115#define BH1770_PROX_RANGE 255
116#define BH1770_COEF_SCALER 1024
117#define BH1770_CALIB_SCALER 8192
118#define BH1770_LUX_NEUTRAL_CALIB_VALUE (1 * BH1770_CALIB_SCALER)
119#define BH1770_LUX_DEF_THRES 1000
120#define BH1770_PROX_DEF_THRES 70
121#define BH1770_PROX_DEF_ABS_THRES 100
122#define BH1770_DEFAULT_PERSISTENCE 10
123#define BH1770_PROX_MAX_PERSISTENCE 50
124#define BH1770_LUX_GA_SCALE 16384
125#define BH1770_LUX_CF_SCALE 2048
126#define BH1770_NEUTRAL_CF BH1770_LUX_CF_SCALE
127#define BH1770_LUX_CORR_SCALE 4096
128
129#define PROX_ABOVE_THRESHOLD 1
130#define PROX_BELOW_THRESHOLD 0
131
132#define PROX_IGNORE_LUX_LIMIT 500
133
134struct bh1770_chip {
135 struct bh1770_platform_data *pdata;
136 char chipname[10];
137 u8 revision;
138 struct i2c_client *client;
139 struct regulator_bulk_data regs[2];
140 struct mutex mutex;
141 wait_queue_head_t wait;
142
143 bool int_mode_prox;
144 bool int_mode_lux;
145 struct delayed_work prox_work;
146 u32 lux_cf;
147 u32 lux_ga;
148 u32 lux_calib;
149 int lux_rate_index;
150 u32 lux_corr;
151 u16 lux_data_raw;
152 u16 lux_threshold_hi;
153 u16 lux_threshold_lo;
154 u16 lux_thres_hi_onchip;
155 u16 lux_thres_lo_onchip;
156 bool lux_wait_result;
157
158 int prox_enable_count;
159 u16 prox_coef;
160 u16 prox_const;
161 int prox_rate;
162 int prox_rate_threshold;
163 u8 prox_persistence;
164 u8 prox_persistence_counter;
165 u8 prox_data;
166 u8 prox_threshold;
167 u8 prox_threshold_hw;
168 bool prox_force_update;
169 u8 prox_abs_thres;
170 u8 prox_led;
171};
172
173static const char reg_vcc[] = "Vcc";
174static const char reg_vleds[] = "Vleds";
175
176
177
178
179
180static const s16 prox_rates_hz[] = {100, 50, 33, 25, 14, 10, 5, 2};
181static const s16 prox_rates_ms[] = {10, 20, 30, 40, 70, 100, 200, 500};
182
183
184static const u8 prox_curr_ma[] = {5, 10, 20, 50, 100, 150, 200};
185
186
187
188
189
190static const s16 lux_rates_hz[] = {10, 5, 2, 1, 0};
191
192
193
194
195
196static inline int bh1770_lux_interrupt_control(struct bh1770_chip *chip,
197 int lux)
198{
199 chip->int_mode_lux = lux;
200
201 return i2c_smbus_write_byte_data(chip->client,
202 BH1770_INTERRUPT,
203 (lux << 1) | chip->int_mode_prox);
204}
205
206static inline int bh1770_prox_interrupt_control(struct bh1770_chip *chip,
207 int ps)
208{
209 chip->int_mode_prox = ps;
210 return i2c_smbus_write_byte_data(chip->client,
211 BH1770_INTERRUPT,
212 (chip->int_mode_lux << 1) | (ps << 0));
213}
214
215
216static int bh1770_lux_rate(struct bh1770_chip *chip, int rate_index)
217{
218
219 if (pm_runtime_suspended(&chip->client->dev))
220 return 0;
221
222
223 if (chip->prox_enable_count)
224 rate_index = 0;
225
226 return i2c_smbus_write_byte_data(chip->client,
227 BH1770_ALS_MEAS_RATE,
228 rate_index);
229}
230
231static int bh1770_prox_rate(struct bh1770_chip *chip, int mode)
232{
233 int rate;
234
235 rate = (mode == PROX_ABOVE_THRESHOLD) ?
236 chip->prox_rate_threshold : chip->prox_rate;
237
238 return i2c_smbus_write_byte_data(chip->client,
239 BH1770_PS_MEAS_RATE,
240 rate);
241}
242
243
244static inline int bh1770_led_cfg(struct bh1770_chip *chip)
245{
246
247 return i2c_smbus_write_byte_data(chip->client,
248 BH1770_I_LED,
249 (BH1770_LED1 << 6) |
250 (BH1770_LED_5mA << 3) |
251 chip->prox_led);
252}
253
254
255
256
257
258
259
260static inline u8 bh1770_psraw_to_adjusted(struct bh1770_chip *chip, u8 psraw)
261{
262 u16 adjusted;
263 adjusted = (u16)(((u32)(psraw + chip->prox_const) * chip->prox_coef) /
264 BH1770_COEF_SCALER);
265 if (adjusted > BH1770_PROX_RANGE)
266 adjusted = BH1770_PROX_RANGE;
267 return adjusted;
268}
269
270static inline u8 bh1770_psadjusted_to_raw(struct bh1770_chip *chip, u8 ps)
271{
272 u16 raw;
273
274 raw = (((u32)ps * BH1770_COEF_SCALER) / chip->prox_coef);
275 if (raw > chip->prox_const)
276 raw = raw - chip->prox_const;
277 else
278 raw = 0;
279 return raw;
280}
281
282
283
284
285
286
287
288static int bh1770_prox_set_threshold(struct bh1770_chip *chip)
289{
290 u8 tmp = 0;
291
292
293 if (pm_runtime_suspended(&chip->client->dev))
294 return 0;
295
296 tmp = bh1770_psadjusted_to_raw(chip, chip->prox_threshold);
297 chip->prox_threshold_hw = tmp;
298
299 return i2c_smbus_write_byte_data(chip->client, BH1770_PS_TH_LED1,
300 tmp);
301}
302
303static inline u16 bh1770_lux_raw_to_adjusted(struct bh1770_chip *chip, u16 raw)
304{
305 u32 lux;
306 lux = ((u32)raw * chip->lux_corr) / BH1770_LUX_CORR_SCALE;
307 return min(lux, (u32)BH1770_LUX_RANGE);
308}
309
310static inline u16 bh1770_lux_adjusted_to_raw(struct bh1770_chip *chip,
311 u16 adjusted)
312{
313 return (u32)adjusted * BH1770_LUX_CORR_SCALE / chip->lux_corr;
314}
315
316
317static int bh1770_lux_update_thresholds(struct bh1770_chip *chip,
318 u16 threshold_hi, u16 threshold_lo)
319{
320 u8 data[4];
321 int ret;
322
323
324 if (pm_runtime_suspended(&chip->client->dev))
325 return 0;
326
327
328
329
330
331
332 if (threshold_hi != BH1770_LUX_RANGE && threshold_hi != 0)
333 threshold_hi = bh1770_lux_adjusted_to_raw(chip, threshold_hi);
334
335 if (threshold_lo != BH1770_LUX_RANGE && threshold_lo != 0)
336 threshold_lo = bh1770_lux_adjusted_to_raw(chip, threshold_lo);
337
338 if (chip->lux_thres_hi_onchip == threshold_hi &&
339 chip->lux_thres_lo_onchip == threshold_lo)
340 return 0;
341
342 chip->lux_thres_hi_onchip = threshold_hi;
343 chip->lux_thres_lo_onchip = threshold_lo;
344
345 data[0] = threshold_hi;
346 data[1] = threshold_hi >> 8;
347 data[2] = threshold_lo;
348 data[3] = threshold_lo >> 8;
349
350 ret = i2c_smbus_write_i2c_block_data(chip->client,
351 BH1770_ALS_TH_UP_0,
352 ARRAY_SIZE(data),
353 data);
354 return ret;
355}
356
357static int bh1770_lux_get_result(struct bh1770_chip *chip)
358{
359 u16 data;
360 int ret;
361
362 ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_0);
363 if (ret < 0)
364 return ret;
365
366 data = ret & 0xff;
367 ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_1);
368 if (ret < 0)
369 return ret;
370
371 chip->lux_data_raw = data | ((ret & 0xff) << 8);
372
373 return 0;
374}
375
376
377static u32 bh1770_get_corr_value(struct bh1770_chip *chip)
378{
379 u32 tmp;
380
381 tmp = (BH1770_LUX_CORR_SCALE * chip->lux_ga) / BH1770_LUX_GA_SCALE;
382
383 tmp = (tmp * chip->lux_cf) / BH1770_LUX_CF_SCALE;
384
385 tmp = (tmp * chip->lux_calib) / BH1770_CALIB_SCALER;
386 return tmp;
387}
388
389static int bh1770_lux_read_result(struct bh1770_chip *chip)
390{
391 bh1770_lux_get_result(chip);
392 return bh1770_lux_raw_to_adjusted(chip, chip->lux_data_raw);
393}
394
395
396
397
398
399static int bh1770_chip_on(struct bh1770_chip *chip)
400{
401 int ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
402 chip->regs);
403 if (ret < 0)
404 return ret;
405
406 usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
407
408
409 i2c_smbus_write_byte_data(chip->client, BH1770_ALS_CONTROL,
410 BH1770_SWRESET);
411 usleep_range(BH1770_RESET_TIME, BH1770_RESET_TIME * 2);
412
413
414
415
416
417
418 chip->lux_data_raw = 0;
419 chip->prox_data = 0;
420 ret = i2c_smbus_write_byte_data(chip->client,
421 BH1770_ALS_CONTROL, BH1770_STANDALONE);
422
423
424 chip->lux_thres_hi_onchip = BH1770_LUX_RANGE;
425 chip->lux_thres_lo_onchip = 0;
426
427 return ret;
428}
429
430static void bh1770_chip_off(struct bh1770_chip *chip)
431{
432 i2c_smbus_write_byte_data(chip->client,
433 BH1770_INTERRUPT, BH1770_DISABLE);
434 i2c_smbus_write_byte_data(chip->client,
435 BH1770_ALS_CONTROL, BH1770_STANDBY);
436 i2c_smbus_write_byte_data(chip->client,
437 BH1770_PS_CONTROL, BH1770_STANDBY);
438 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
439}
440
441
442static int bh1770_prox_mode_control(struct bh1770_chip *chip)
443{
444 if (chip->prox_enable_count) {
445 chip->prox_force_update = true;
446
447 bh1770_lux_rate(chip, chip->lux_rate_index);
448 bh1770_prox_set_threshold(chip);
449 bh1770_led_cfg(chip);
450 bh1770_prox_rate(chip, PROX_BELOW_THRESHOLD);
451 bh1770_prox_interrupt_control(chip, BH1770_ENABLE);
452 i2c_smbus_write_byte_data(chip->client,
453 BH1770_PS_CONTROL, BH1770_STANDALONE);
454 } else {
455 chip->prox_data = 0;
456 bh1770_lux_rate(chip, chip->lux_rate_index);
457 bh1770_prox_interrupt_control(chip, BH1770_DISABLE);
458 i2c_smbus_write_byte_data(chip->client,
459 BH1770_PS_CONTROL, BH1770_STANDBY);
460 }
461 return 0;
462}
463
464
465static int bh1770_prox_read_result(struct bh1770_chip *chip)
466{
467 int ret;
468 bool above;
469 u8 mode;
470
471 ret = i2c_smbus_read_byte_data(chip->client, BH1770_PS_DATA_LED1);
472 if (ret < 0)
473 goto out;
474
475 if (ret > chip->prox_threshold_hw)
476 above = true;
477 else
478 above = false;
479
480
481
482
483
484
485 if (chip->lux_data_raw > PROX_IGNORE_LUX_LIMIT)
486 ret = 0;
487
488 chip->prox_data = bh1770_psraw_to_adjusted(chip, ret);
489
490
491 if (chip->prox_data >= chip->prox_abs_thres ||
492 chip->prox_force_update)
493 chip->prox_persistence_counter = chip->prox_persistence;
494
495 chip->prox_force_update = false;
496
497
498 if (likely(above)) {
499 if (chip->prox_persistence_counter < chip->prox_persistence) {
500 chip->prox_persistence_counter++;
501 ret = -ENODATA;
502 } else {
503 mode = PROX_ABOVE_THRESHOLD;
504 ret = 0;
505 }
506 } else {
507 chip->prox_persistence_counter = 0;
508 mode = PROX_BELOW_THRESHOLD;
509 chip->prox_data = 0;
510 ret = 0;
511 }
512
513
514 if (ret == 0) {
515 bh1770_prox_rate(chip, mode);
516 sysfs_notify(&chip->client->dev.kobj, NULL, "prox0_raw");
517 }
518out:
519 return ret;
520}
521
522static int bh1770_detect(struct bh1770_chip *chip)
523{
524 struct i2c_client *client = chip->client;
525 s32 ret;
526 u8 manu, part;
527
528 ret = i2c_smbus_read_byte_data(client, BH1770_MANUFACT_ID);
529 if (ret < 0)
530 goto error;
531 manu = (u8)ret;
532
533 ret = i2c_smbus_read_byte_data(client, BH1770_PART_ID);
534 if (ret < 0)
535 goto error;
536 part = (u8)ret;
537
538 chip->revision = (part & BH1770_REV_MASK) >> BH1770_REV_SHIFT;
539 chip->prox_coef = BH1770_COEF_SCALER;
540 chip->prox_const = 0;
541 chip->lux_cf = BH1770_NEUTRAL_CF;
542
543 if ((manu == BH1770_MANUFACT_ROHM) &&
544 ((part & BH1770_PART_MASK) == BH1770_PART)) {
545 snprintf(chip->chipname, sizeof(chip->chipname), "BH1770GLC");
546 return 0;
547 }
548
549 if ((manu == BH1770_MANUFACT_OSRAM) &&
550 ((part & BH1770_PART_MASK) == BH1770_PART)) {
551 snprintf(chip->chipname, sizeof(chip->chipname), "SFH7770");
552
553 chip->prox_coef = 819;
554 chip->prox_const = 40;
555 return 0;
556 }
557
558 ret = -ENODEV;
559error:
560 dev_dbg(&client->dev, "BH1770 or SFH7770 not found\n");
561
562 return ret;
563}
564
565
566
567
568
569
570
571
572
573
574static void bh1770_prox_work(struct work_struct *work)
575{
576 struct bh1770_chip *chip =
577 container_of(work, struct bh1770_chip, prox_work.work);
578
579 mutex_lock(&chip->mutex);
580 bh1770_prox_read_result(chip);
581 mutex_unlock(&chip->mutex);
582}
583
584
585static irqreturn_t bh1770_irq(int irq, void *data)
586{
587 struct bh1770_chip *chip = data;
588 int status;
589 int rate = 0;
590
591 mutex_lock(&chip->mutex);
592 status = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_PS_STATUS);
593
594
595 i2c_smbus_read_byte_data(chip->client, BH1770_INTERRUPT);
596
597
598
599
600
601 if (status & BH1770_INT_ALS_DATA) {
602 bh1770_lux_get_result(chip);
603 if (unlikely(chip->lux_wait_result)) {
604 chip->lux_wait_result = false;
605 wake_up(&chip->wait);
606 bh1770_lux_update_thresholds(chip,
607 chip->lux_threshold_hi,
608 chip->lux_threshold_lo);
609 }
610 }
611
612
613 i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
614 (0 << 1) | (0 << 0));
615
616 if ((status & BH1770_INT_ALS_INT))
617 sysfs_notify(&chip->client->dev.kobj, NULL, "lux0_input");
618
619 if (chip->int_mode_prox && (status & BH1770_INT_LEDS_INT)) {
620 rate = prox_rates_ms[chip->prox_rate_threshold];
621 bh1770_prox_read_result(chip);
622 }
623
624
625 i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
626 (chip->int_mode_lux << 1) |
627 (chip->int_mode_prox << 0));
628 mutex_unlock(&chip->mutex);
629
630
631
632
633
634 if (rate) {
635
636
637
638
639 cancel_delayed_work_sync(&chip->prox_work);
640 schedule_delayed_work(&chip->prox_work,
641 msecs_to_jiffies(rate + 50));
642 }
643 return IRQ_HANDLED;
644}
645
646static ssize_t bh1770_power_state_store(struct device *dev,
647 struct device_attribute *attr,
648 const char *buf, size_t count)
649{
650 struct bh1770_chip *chip = dev_get_drvdata(dev);
651 unsigned long value;
652 ssize_t ret;
653
654 ret = kstrtoul(buf, 0, &value);
655 if (ret)
656 return ret;
657
658 mutex_lock(&chip->mutex);
659 if (value) {
660 pm_runtime_get_sync(dev);
661
662 ret = bh1770_lux_rate(chip, chip->lux_rate_index);
663 if (ret < 0) {
664 pm_runtime_put(dev);
665 goto leave;
666 }
667
668 ret = bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
669 if (ret < 0) {
670 pm_runtime_put(dev);
671 goto leave;
672 }
673
674
675 bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
676 BH1770_LUX_DEF_THRES);
677
678 chip->lux_wait_result = true;
679 bh1770_prox_mode_control(chip);
680 } else if (!pm_runtime_suspended(dev)) {
681 pm_runtime_put(dev);
682 }
683 ret = count;
684leave:
685 mutex_unlock(&chip->mutex);
686 return ret;
687}
688
689static ssize_t bh1770_power_state_show(struct device *dev,
690 struct device_attribute *attr, char *buf)
691{
692 return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
693}
694
695static ssize_t bh1770_lux_result_show(struct device *dev,
696 struct device_attribute *attr, char *buf)
697{
698 struct bh1770_chip *chip = dev_get_drvdata(dev);
699 ssize_t ret;
700 long timeout;
701
702 if (pm_runtime_suspended(dev))
703 return -EIO;
704
705 timeout = wait_event_interruptible_timeout(chip->wait,
706 !chip->lux_wait_result,
707 msecs_to_jiffies(BH1770_TIMEOUT));
708 if (!timeout)
709 return -EIO;
710
711 mutex_lock(&chip->mutex);
712 ret = sprintf(buf, "%d\n", bh1770_lux_read_result(chip));
713 mutex_unlock(&chip->mutex);
714
715 return ret;
716}
717
718static ssize_t bh1770_lux_range_show(struct device *dev,
719 struct device_attribute *attr, char *buf)
720{
721 return sprintf(buf, "%d\n", BH1770_LUX_RANGE);
722}
723
724static ssize_t bh1770_prox_enable_store(struct device *dev,
725 struct device_attribute *attr,
726 const char *buf, size_t count)
727{
728 struct bh1770_chip *chip = dev_get_drvdata(dev);
729 unsigned long value;
730 int ret;
731
732 ret = kstrtoul(buf, 0, &value);
733 if (ret)
734 return ret;
735
736 mutex_lock(&chip->mutex);
737
738 if (!chip->prox_enable_count)
739 chip->prox_data = 0;
740
741 if (value)
742 chip->prox_enable_count++;
743 else if (chip->prox_enable_count > 0)
744 chip->prox_enable_count--;
745 else
746 goto leave;
747
748
749 if (!pm_runtime_suspended(dev))
750 bh1770_prox_mode_control(chip);
751leave:
752 mutex_unlock(&chip->mutex);
753 return count;
754}
755
756static ssize_t bh1770_prox_enable_show(struct device *dev,
757 struct device_attribute *attr, char *buf)
758{
759 struct bh1770_chip *chip = dev_get_drvdata(dev);
760 ssize_t len;
761
762 mutex_lock(&chip->mutex);
763 len = sprintf(buf, "%d\n", chip->prox_enable_count);
764 mutex_unlock(&chip->mutex);
765 return len;
766}
767
768static ssize_t bh1770_prox_result_show(struct device *dev,
769 struct device_attribute *attr, char *buf)
770{
771 struct bh1770_chip *chip = dev_get_drvdata(dev);
772 ssize_t ret;
773
774 mutex_lock(&chip->mutex);
775 if (chip->prox_enable_count && !pm_runtime_suspended(dev))
776 ret = sprintf(buf, "%d\n", chip->prox_data);
777 else
778 ret = -EIO;
779 mutex_unlock(&chip->mutex);
780 return ret;
781}
782
783static ssize_t bh1770_prox_range_show(struct device *dev,
784 struct device_attribute *attr, char *buf)
785{
786 return sprintf(buf, "%d\n", BH1770_PROX_RANGE);
787}
788
789static ssize_t bh1770_get_prox_rate_avail(struct device *dev,
790 struct device_attribute *attr, char *buf)
791{
792 int i;
793 int pos = 0;
794 for (i = 0; i < ARRAY_SIZE(prox_rates_hz); i++)
795 pos += sprintf(buf + pos, "%d ", prox_rates_hz[i]);
796 sprintf(buf + pos - 1, "\n");
797 return pos;
798}
799
800static ssize_t bh1770_get_prox_rate_above(struct device *dev,
801 struct device_attribute *attr, char *buf)
802{
803 struct bh1770_chip *chip = dev_get_drvdata(dev);
804 return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate_threshold]);
805}
806
807static ssize_t bh1770_get_prox_rate_below(struct device *dev,
808 struct device_attribute *attr, char *buf)
809{
810 struct bh1770_chip *chip = dev_get_drvdata(dev);
811 return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate]);
812}
813
814static int bh1770_prox_rate_validate(int rate)
815{
816 int i;
817
818 for (i = 0; i < ARRAY_SIZE(prox_rates_hz) - 1; i++)
819 if (rate >= prox_rates_hz[i])
820 break;
821 return i;
822}
823
824static ssize_t bh1770_set_prox_rate_above(struct device *dev,
825 struct device_attribute *attr,
826 const char *buf, size_t count)
827{
828 struct bh1770_chip *chip = dev_get_drvdata(dev);
829 unsigned long value;
830 int ret;
831
832 ret = kstrtoul(buf, 0, &value);
833 if (ret)
834 return ret;
835
836 mutex_lock(&chip->mutex);
837 chip->prox_rate_threshold = bh1770_prox_rate_validate(value);
838 mutex_unlock(&chip->mutex);
839 return count;
840}
841
842static ssize_t bh1770_set_prox_rate_below(struct device *dev,
843 struct device_attribute *attr,
844 const char *buf, size_t count)
845{
846 struct bh1770_chip *chip = dev_get_drvdata(dev);
847 unsigned long value;
848 int ret;
849
850 ret = kstrtoul(buf, 0, &value);
851 if (ret)
852 return ret;
853
854 mutex_lock(&chip->mutex);
855 chip->prox_rate = bh1770_prox_rate_validate(value);
856 mutex_unlock(&chip->mutex);
857 return count;
858}
859
860static ssize_t bh1770_get_prox_thres(struct device *dev,
861 struct device_attribute *attr, char *buf)
862{
863 struct bh1770_chip *chip = dev_get_drvdata(dev);
864 return sprintf(buf, "%d\n", chip->prox_threshold);
865}
866
867static ssize_t bh1770_set_prox_thres(struct device *dev,
868 struct device_attribute *attr,
869 const char *buf, size_t count)
870{
871 struct bh1770_chip *chip = dev_get_drvdata(dev);
872 unsigned long value;
873 int ret;
874
875 ret = kstrtoul(buf, 0, &value);
876 if (ret)
877 return ret;
878
879 if (value > BH1770_PROX_RANGE)
880 return -EINVAL;
881
882 mutex_lock(&chip->mutex);
883 chip->prox_threshold = value;
884 ret = bh1770_prox_set_threshold(chip);
885 mutex_unlock(&chip->mutex);
886 if (ret < 0)
887 return ret;
888 return count;
889}
890
891static ssize_t bh1770_prox_persistence_show(struct device *dev,
892 struct device_attribute *attr, char *buf)
893{
894 struct bh1770_chip *chip = dev_get_drvdata(dev);
895
896 return sprintf(buf, "%u\n", chip->prox_persistence);
897}
898
899static ssize_t bh1770_prox_persistence_store(struct device *dev,
900 struct device_attribute *attr,
901 const char *buf, size_t len)
902{
903 struct bh1770_chip *chip = dev_get_drvdata(dev);
904 unsigned long value;
905 int ret;
906
907 ret = kstrtoul(buf, 0, &value);
908 if (ret)
909 return ret;
910
911 if (value > BH1770_PROX_MAX_PERSISTENCE)
912 return -EINVAL;
913
914 chip->prox_persistence = value;
915
916 return len;
917}
918
919static ssize_t bh1770_prox_abs_thres_show(struct device *dev,
920 struct device_attribute *attr, char *buf)
921{
922 struct bh1770_chip *chip = dev_get_drvdata(dev);
923 return sprintf(buf, "%u\n", chip->prox_abs_thres);
924}
925
926static ssize_t bh1770_prox_abs_thres_store(struct device *dev,
927 struct device_attribute *attr,
928 const char *buf, size_t len)
929{
930 struct bh1770_chip *chip = dev_get_drvdata(dev);
931 unsigned long value;
932 int ret;
933
934 ret = kstrtoul(buf, 0, &value);
935 if (ret)
936 return ret;
937
938 if (value > BH1770_PROX_RANGE)
939 return -EINVAL;
940
941 chip->prox_abs_thres = value;
942
943 return len;
944}
945
946static ssize_t bh1770_chip_id_show(struct device *dev,
947 struct device_attribute *attr, char *buf)
948{
949 struct bh1770_chip *chip = dev_get_drvdata(dev);
950 return sprintf(buf, "%s rev %d\n", chip->chipname, chip->revision);
951}
952
953static ssize_t bh1770_lux_calib_default_show(struct device *dev,
954 struct device_attribute *attr, char *buf)
955{
956 return sprintf(buf, "%u\n", BH1770_CALIB_SCALER);
957}
958
959static ssize_t bh1770_lux_calib_show(struct device *dev,
960 struct device_attribute *attr, char *buf)
961{
962 struct bh1770_chip *chip = dev_get_drvdata(dev);
963 ssize_t len;
964
965 mutex_lock(&chip->mutex);
966 len = sprintf(buf, "%u\n", chip->lux_calib);
967 mutex_unlock(&chip->mutex);
968 return len;
969}
970
971static ssize_t bh1770_lux_calib_store(struct device *dev,
972 struct device_attribute *attr,
973 const char *buf, size_t len)
974{
975 struct bh1770_chip *chip = dev_get_drvdata(dev);
976 unsigned long value;
977 u32 old_calib;
978 u32 new_corr;
979 int ret;
980
981 ret = kstrtoul(buf, 0, &value);
982 if (ret)
983 return ret;
984
985 mutex_lock(&chip->mutex);
986 old_calib = chip->lux_calib;
987 chip->lux_calib = value;
988 new_corr = bh1770_get_corr_value(chip);
989 if (new_corr == 0) {
990 chip->lux_calib = old_calib;
991 mutex_unlock(&chip->mutex);
992 return -EINVAL;
993 }
994 chip->lux_corr = new_corr;
995
996 bh1770_lux_update_thresholds(chip, chip->lux_threshold_hi,
997 chip->lux_threshold_lo);
998
999 mutex_unlock(&chip->mutex);
1000
1001 return len;
1002}
1003
1004static ssize_t bh1770_get_lux_rate_avail(struct device *dev,
1005 struct device_attribute *attr, char *buf)
1006{
1007 int i;
1008 int pos = 0;
1009 for (i = 0; i < ARRAY_SIZE(lux_rates_hz); i++)
1010 pos += sprintf(buf + pos, "%d ", lux_rates_hz[i]);
1011 sprintf(buf + pos - 1, "\n");
1012 return pos;
1013}
1014
1015static ssize_t bh1770_get_lux_rate(struct device *dev,
1016 struct device_attribute *attr, char *buf)
1017{
1018 struct bh1770_chip *chip = dev_get_drvdata(dev);
1019 return sprintf(buf, "%d\n", lux_rates_hz[chip->lux_rate_index]);
1020}
1021
1022static ssize_t bh1770_set_lux_rate(struct device *dev,
1023 struct device_attribute *attr,
1024 const char *buf, size_t count)
1025{
1026 struct bh1770_chip *chip = dev_get_drvdata(dev);
1027 unsigned long rate_hz;
1028 int ret, i;
1029
1030 ret = kstrtoul(buf, 0, &rate_hz);
1031 if (ret)
1032 return ret;
1033
1034 for (i = 0; i < ARRAY_SIZE(lux_rates_hz) - 1; i++)
1035 if (rate_hz >= lux_rates_hz[i])
1036 break;
1037
1038 mutex_lock(&chip->mutex);
1039 chip->lux_rate_index = i;
1040 ret = bh1770_lux_rate(chip, i);
1041 mutex_unlock(&chip->mutex);
1042
1043 if (ret < 0)
1044 return ret;
1045
1046 return count;
1047}
1048
1049static ssize_t bh1770_get_lux_thresh_above(struct device *dev,
1050 struct device_attribute *attr, char *buf)
1051{
1052 struct bh1770_chip *chip = dev_get_drvdata(dev);
1053 return sprintf(buf, "%d\n", chip->lux_threshold_hi);
1054}
1055
1056static ssize_t bh1770_get_lux_thresh_below(struct device *dev,
1057 struct device_attribute *attr, char *buf)
1058{
1059 struct bh1770_chip *chip = dev_get_drvdata(dev);
1060 return sprintf(buf, "%d\n", chip->lux_threshold_lo);
1061}
1062
1063static ssize_t bh1770_set_lux_thresh(struct bh1770_chip *chip, u16 *target,
1064 const char *buf)
1065{
1066 unsigned long thresh;
1067 int ret;
1068
1069 ret = kstrtoul(buf, 0, &thresh);
1070 if (ret)
1071 return ret;
1072
1073 if (thresh > BH1770_LUX_RANGE)
1074 return -EINVAL;
1075
1076 mutex_lock(&chip->mutex);
1077 *target = thresh;
1078
1079
1080
1081
1082 if (!chip->lux_wait_result)
1083 ret = bh1770_lux_update_thresholds(chip,
1084 chip->lux_threshold_hi,
1085 chip->lux_threshold_lo);
1086 mutex_unlock(&chip->mutex);
1087 return ret;
1088
1089}
1090
1091static ssize_t bh1770_set_lux_thresh_above(struct device *dev,
1092 struct device_attribute *attr,
1093 const char *buf, size_t len)
1094{
1095 struct bh1770_chip *chip = dev_get_drvdata(dev);
1096 int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_hi, buf);
1097 if (ret < 0)
1098 return ret;
1099 return len;
1100}
1101
1102static ssize_t bh1770_set_lux_thresh_below(struct device *dev,
1103 struct device_attribute *attr,
1104 const char *buf, size_t len)
1105{
1106 struct bh1770_chip *chip = dev_get_drvdata(dev);
1107 int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_lo, buf);
1108 if (ret < 0)
1109 return ret;
1110 return len;
1111}
1112
1113static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, bh1770_prox_enable_show,
1114 bh1770_prox_enable_store);
1115static DEVICE_ATTR(prox0_thresh_above1_value, S_IRUGO | S_IWUSR,
1116 bh1770_prox_abs_thres_show,
1117 bh1770_prox_abs_thres_store);
1118static DEVICE_ATTR(prox0_thresh_above0_value, S_IRUGO | S_IWUSR,
1119 bh1770_get_prox_thres,
1120 bh1770_set_prox_thres);
1121static DEVICE_ATTR(prox0_raw, S_IRUGO, bh1770_prox_result_show, NULL);
1122static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, bh1770_prox_range_show, NULL);
1123static DEVICE_ATTR(prox0_thresh_above_count, S_IRUGO | S_IWUSR,
1124 bh1770_prox_persistence_show,
1125 bh1770_prox_persistence_store);
1126static DEVICE_ATTR(prox0_rate_above, S_IRUGO | S_IWUSR,
1127 bh1770_get_prox_rate_above,
1128 bh1770_set_prox_rate_above);
1129static DEVICE_ATTR(prox0_rate_below, S_IRUGO | S_IWUSR,
1130 bh1770_get_prox_rate_below,
1131 bh1770_set_prox_rate_below);
1132static DEVICE_ATTR(prox0_rate_avail, S_IRUGO, bh1770_get_prox_rate_avail, NULL);
1133
1134static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, bh1770_lux_calib_show,
1135 bh1770_lux_calib_store);
1136static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
1137 bh1770_lux_calib_default_show,
1138 NULL);
1139static DEVICE_ATTR(lux0_input, S_IRUGO, bh1770_lux_result_show, NULL);
1140static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, bh1770_lux_range_show, NULL);
1141static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, bh1770_get_lux_rate,
1142 bh1770_set_lux_rate);
1143static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, bh1770_get_lux_rate_avail, NULL);
1144static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
1145 bh1770_get_lux_thresh_above,
1146 bh1770_set_lux_thresh_above);
1147static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
1148 bh1770_get_lux_thresh_below,
1149 bh1770_set_lux_thresh_below);
1150static DEVICE_ATTR(chip_id, S_IRUGO, bh1770_chip_id_show, NULL);
1151static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, bh1770_power_state_show,
1152 bh1770_power_state_store);
1153
1154
1155static struct attribute *sysfs_attrs[] = {
1156 &dev_attr_lux0_calibscale.attr,
1157 &dev_attr_lux0_calibscale_default.attr,
1158 &dev_attr_lux0_input.attr,
1159 &dev_attr_lux0_sensor_range.attr,
1160 &dev_attr_lux0_rate.attr,
1161 &dev_attr_lux0_rate_avail.attr,
1162 &dev_attr_lux0_thresh_above_value.attr,
1163 &dev_attr_lux0_thresh_below_value.attr,
1164 &dev_attr_prox0_raw.attr,
1165 &dev_attr_prox0_sensor_range.attr,
1166 &dev_attr_prox0_raw_en.attr,
1167 &dev_attr_prox0_thresh_above_count.attr,
1168 &dev_attr_prox0_rate_above.attr,
1169 &dev_attr_prox0_rate_below.attr,
1170 &dev_attr_prox0_rate_avail.attr,
1171 &dev_attr_prox0_thresh_above0_value.attr,
1172 &dev_attr_prox0_thresh_above1_value.attr,
1173 &dev_attr_chip_id.attr,
1174 &dev_attr_power_state.attr,
1175 NULL
1176};
1177
1178static const struct attribute_group bh1770_attribute_group = {
1179 .attrs = sysfs_attrs
1180};
1181
1182static int bh1770_probe(struct i2c_client *client,
1183 const struct i2c_device_id *id)
1184{
1185 struct bh1770_chip *chip;
1186 int err;
1187
1188 chip = devm_kzalloc(&client->dev, sizeof *chip, GFP_KERNEL);
1189 if (!chip)
1190 return -ENOMEM;
1191
1192 i2c_set_clientdata(client, chip);
1193 chip->client = client;
1194
1195 mutex_init(&chip->mutex);
1196 init_waitqueue_head(&chip->wait);
1197 INIT_DELAYED_WORK(&chip->prox_work, bh1770_prox_work);
1198
1199 if (client->dev.platform_data == NULL) {
1200 dev_err(&client->dev, "platform data is mandatory\n");
1201 return -EINVAL;
1202 }
1203
1204 chip->pdata = client->dev.platform_data;
1205 chip->lux_calib = BH1770_LUX_NEUTRAL_CALIB_VALUE;
1206 chip->lux_rate_index = BH1770_LUX_DEFAULT_RATE;
1207 chip->lux_threshold_lo = BH1770_LUX_DEF_THRES;
1208 chip->lux_threshold_hi = BH1770_LUX_DEF_THRES;
1209
1210 if (chip->pdata->glass_attenuation == 0)
1211 chip->lux_ga = BH1770_NEUTRAL_GA;
1212 else
1213 chip->lux_ga = chip->pdata->glass_attenuation;
1214
1215 chip->prox_threshold = BH1770_PROX_DEF_THRES;
1216 chip->prox_led = chip->pdata->led_def_curr;
1217 chip->prox_abs_thres = BH1770_PROX_DEF_ABS_THRES;
1218 chip->prox_persistence = BH1770_DEFAULT_PERSISTENCE;
1219 chip->prox_rate_threshold = BH1770_PROX_DEF_RATE_THRESH;
1220 chip->prox_rate = BH1770_PROX_DEFAULT_RATE;
1221 chip->prox_data = 0;
1222
1223 chip->regs[0].supply = reg_vcc;
1224 chip->regs[1].supply = reg_vleds;
1225
1226 err = devm_regulator_bulk_get(&client->dev,
1227 ARRAY_SIZE(chip->regs), chip->regs);
1228 if (err < 0) {
1229 dev_err(&client->dev, "Cannot get regulators\n");
1230 return err;
1231 }
1232
1233 err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
1234 chip->regs);
1235 if (err < 0) {
1236 dev_err(&client->dev, "Cannot enable regulators\n");
1237 return err;
1238 }
1239
1240 usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
1241 err = bh1770_detect(chip);
1242 if (err < 0)
1243 goto fail0;
1244
1245
1246 bh1770_chip_on(chip);
1247 pm_runtime_set_active(&client->dev);
1248 pm_runtime_enable(&client->dev);
1249
1250 chip->lux_corr = bh1770_get_corr_value(chip);
1251 if (chip->lux_corr == 0) {
1252 dev_err(&client->dev, "Improper correction values\n");
1253 err = -EINVAL;
1254 goto fail0;
1255 }
1256
1257 if (chip->pdata->setup_resources) {
1258 err = chip->pdata->setup_resources();
1259 if (err) {
1260 err = -EINVAL;
1261 goto fail0;
1262 }
1263 }
1264
1265 err = sysfs_create_group(&chip->client->dev.kobj,
1266 &bh1770_attribute_group);
1267 if (err < 0) {
1268 dev_err(&chip->client->dev, "Sysfs registration failed\n");
1269 goto fail1;
1270 }
1271
1272
1273
1274
1275
1276
1277 err = request_threaded_irq(client->irq, NULL,
1278 bh1770_irq,
1279 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
1280 IRQF_TRIGGER_LOW,
1281 "bh1770", chip);
1282 if (err) {
1283 dev_err(&client->dev, "could not get IRQ %d\n",
1284 client->irq);
1285 goto fail2;
1286 }
1287 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1288 return err;
1289fail2:
1290 sysfs_remove_group(&chip->client->dev.kobj,
1291 &bh1770_attribute_group);
1292fail1:
1293 if (chip->pdata->release_resources)
1294 chip->pdata->release_resources();
1295fail0:
1296 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1297 return err;
1298}
1299
1300static int bh1770_remove(struct i2c_client *client)
1301{
1302 struct bh1770_chip *chip = i2c_get_clientdata(client);
1303
1304 free_irq(client->irq, chip);
1305
1306 sysfs_remove_group(&chip->client->dev.kobj,
1307 &bh1770_attribute_group);
1308
1309 if (chip->pdata->release_resources)
1310 chip->pdata->release_resources();
1311
1312 cancel_delayed_work_sync(&chip->prox_work);
1313
1314 if (!pm_runtime_suspended(&client->dev))
1315 bh1770_chip_off(chip);
1316
1317 pm_runtime_disable(&client->dev);
1318 pm_runtime_set_suspended(&client->dev);
1319
1320 return 0;
1321}
1322
1323#ifdef CONFIG_PM_SLEEP
1324static int bh1770_suspend(struct device *dev)
1325{
1326 struct i2c_client *client = to_i2c_client(dev);
1327 struct bh1770_chip *chip = i2c_get_clientdata(client);
1328
1329 bh1770_chip_off(chip);
1330
1331 return 0;
1332}
1333
1334static int bh1770_resume(struct device *dev)
1335{
1336 struct i2c_client *client = to_i2c_client(dev);
1337 struct bh1770_chip *chip = i2c_get_clientdata(client);
1338 int ret = 0;
1339
1340 bh1770_chip_on(chip);
1341
1342 if (!pm_runtime_suspended(dev)) {
1343
1344
1345
1346
1347 ret = bh1770_lux_rate(chip, chip->lux_rate_index);
1348 ret |= bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
1349
1350
1351 bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
1352 BH1770_LUX_DEF_THRES);
1353
1354 chip->lux_wait_result = true;
1355 bh1770_prox_mode_control(chip);
1356 }
1357 return ret;
1358}
1359#endif
1360
1361#ifdef CONFIG_PM
1362static int bh1770_runtime_suspend(struct device *dev)
1363{
1364 struct i2c_client *client = to_i2c_client(dev);
1365 struct bh1770_chip *chip = i2c_get_clientdata(client);
1366
1367 bh1770_chip_off(chip);
1368
1369 return 0;
1370}
1371
1372static int bh1770_runtime_resume(struct device *dev)
1373{
1374 struct i2c_client *client = to_i2c_client(dev);
1375 struct bh1770_chip *chip = i2c_get_clientdata(client);
1376
1377 bh1770_chip_on(chip);
1378
1379 return 0;
1380}
1381#endif
1382
1383static const struct i2c_device_id bh1770_id[] = {
1384 {"bh1770glc", 0 },
1385 {"sfh7770", 0 },
1386 {}
1387};
1388
1389MODULE_DEVICE_TABLE(i2c, bh1770_id);
1390
1391static const struct dev_pm_ops bh1770_pm_ops = {
1392 SET_SYSTEM_SLEEP_PM_OPS(bh1770_suspend, bh1770_resume)
1393 SET_RUNTIME_PM_OPS(bh1770_runtime_suspend, bh1770_runtime_resume, NULL)
1394};
1395
1396static struct i2c_driver bh1770_driver = {
1397 .driver = {
1398 .name = "bh1770glc",
1399 .pm = &bh1770_pm_ops,
1400 },
1401 .probe = bh1770_probe,
1402 .remove = bh1770_remove,
1403 .id_table = bh1770_id,
1404};
1405
1406module_i2c_driver(bh1770_driver);
1407
1408MODULE_DESCRIPTION("BH1770GLC / SFH7770 combined ALS and proximity sensor");
1409MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1410MODULE_LICENSE("GPL v2");
1411