Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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