xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/lsensor/ls_stk3332.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
4  *
5  * Author: Kay Guo <kay.guo@rock-chips.com>
6  */
7 #include <linux/atomic.h>
8 #include <linux/delay.h>
9 #ifdef CONFIG_HAS_EARLYSUSPEND
10 #include <linux/earlysuspend.h>
11 #endif
12 #include <linux/freezer.h>
13 #include <linux/gpio.h>
14 #include <linux/i2c.h>
15 #include <linux/input.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/miscdevice.h>
19 #include <linux/of_gpio.h>
20 #include <linux/sensor-dev.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/workqueue.h>
24 
25 #define	STK_STATE	0x00
26 #define	PS_CTRL		0x01
27 #define	ALS_CTRL1	0x02
28 #define	LED_CTRL	0x03
29 #define	INT_CTRL1	0x04
30 #define	STK_WAIT	0x05
31 #define	THDH1_PS	0x06
32 #define	THDH2_PS	0x07
33 #define	THDL1_PS	0x08
34 #define	THDL2_PS	0x09
35 #define	THDH1_ALS	0x0A
36 #define	THDH2_ALS	0x0B
37 #define	THDL1_ALS	0x0C
38 #define	THDL2_ALS	0x0D
39 #define	STK_FLAG	0x10
40 #define	DATA1_PS	0x11
41 #define	DATA2_PS	0x12
42 #define	DATA1_ALS	0x13
43 #define	DATA2_ALS	0x14
44 #define DATA1_C         0x1B
45 #define DATA2_C         0x1C
46 #define DATA1_PS_OFFSET 0x1D
47 #define DATA2_PS_OFFSET 0x1E
48 #define DATA_CTRL1      0x20
49 #define DATA_CTRL2      0x21
50 #define DATA_CTRL3      0x22
51 #define DATA_CTRL4      0x23
52 #define	STKPDT_ID	0x3E
53 #define STK_RESERVED    0x3F
54 #define ALS_CTRL2       0x4E
55 #define INTELLI_WAIT    0x4F
56 #define	SOFT_RESET	0x80
57 #define PSPD_CTRL       0xA1
58 #define INT_CTRL2       0xA5
59 
60 /* STK_STATE	0x00 */
61 #define	PS_DISABLE	(0 << 0)
62 #define	PS_ENABLE	(1 << 0)
63 #define	ALS_DISABLE	(0 << 1)
64 #define	ALS_ENABLE	(1 << 1)
65 #define	WAIT_DISABLE	(0 << 2)
66 #define	WAIT_ENABLE	(1 << 2)
67 #define INTELLI_DISABLE (0 << 3)
68 #define INTELLI_ENABLE  (1 << 3)
69 #define	CTAUTOK_DISABLE	(0 << 4)
70 #define	CTAUTOK_ENABLE	(1 << 4)
71 
72 /* PS/GS_CTRL 0x01 */
73 #define PS_IT_96US      (0 << 0)
74 #define PS_IT_192US     (1 << 0)
75 #define PS_IT_384US     (2 << 0)
76 #define PS_IT_768US     (3 << 0)
77 #define PS_IT_1MS54     (4 << 0)
78 #define PS_IT_3MS07     (5 << 0)
79 #define PS_IT_6MS14     (6 << 0)
80 
81 #define	PS_GAIN_1G	(0 << 4)
82 #define	PS_GAIN_2G	(1 << 4)
83 #define	PS_GAIN_4G	(2 << 4)
84 #define	PS_GAIN_8G	(3 << 4)
85 #define	PS_PRST_1T	(0 << 6)
86 #define	PS_PRST_2T	(1 << 6)
87 #define	PS_PRST_4T	(2 << 6)
88 #define	PS_PRST_16T	(3 << 6)
89 
90 /* ALS_CTRL1 0x02 */
91 #define	ALS_REFT_MS	(1 << 0)/* [3:0] 25 ms,  default value is 50ms */
92 #define	ALS_GAIN_1G	(0 << 4)
93 #define	ALS_GAIN_4G	(1 << 4)
94 #define	ALS_GAIN_16G	(2 << 4)
95 #define	ALS_GAIN_64G	(3 << 4)
96 #define	ALS_PRST_1T	(0 << 6)
97 #define	ALS_PRST_2T	(1 << 6)
98 #define	ALS_PRST_4T	(2 << 6)
99 #define	ALS_PRST_8T	(3 << 6)
100 
101 /* LED_CTRL 0x03 */
102 #define	LED_REFT_US	0x03	/* [5:0] 2.89us ,  default value is 0.185ms */
103 #define CTIR_DISABLE    (0 << 0)
104 #define CTIR_ENABLE     (1 << 0)
105 #define CTIRFC_DISABLE  (0 << 1)
106 #define CTIRFC_ENABLE   (1 << 1)
107 #define	LED_CUR_12MA	(2 << 5)
108 #define	LED_CUR_25MA	(3 << 5)
109 #define	LED_CUR_50MA	(4 << 5)
110 #define	LED_CUR_100MA	(5 << 5)
111 #define	LED_CUR_150MA	(6 << 5)
112 
113 /* INT 0x04 */
114 #define	PS_INT_DISABLE		(0 << 0)
115 #define	PS_INT_ENABLE		(1 << 0)
116 #define	PS_INT_ENABLE_FLGNFH	(2 << 0)
117 #define	PS_INT_ENABLE_FLGNFL	(3 << 0)
118 #define	PS_INT_MODE_ENABLE	(4 << 0)
119 #define	PS_INT_ENABLE_THL	(5 << 0)
120 #define	PS_INT_ENABLE_THH	(6 << 0)
121 #define	PS_INT_ENABLE_THHL	(7 << 0)
122 #define	ALS_INT_DISABLE		(0 << 3)
123 #define	ALS_INT_ENABLE		(1 << 3)
124 #define	INT_CTRL_PS_OR_LS	(0 << 7)
125 #define	INT_CTRL_PS_AND_LS	(1 << 7)
126 
127 /* FLAG 0x10 */
128 #define	STK_FLAG_NF	        (1 << 0)
129 #define	STK_FLAG_INPS_INT	(1 << 1)
130 #define	STK_FLAG_ALS_STATE	(1 << 2)
131 #define	STK_FLAG_PS_INT		(1 << 4)
132 #define	STK_FLAG_ALS_INT	(1 << 5)
133 #define	STK_FLAG_PSDR           (1 << 6)
134 #define	STK_FLAG_ALSDR          (1 << 7)
135 
sensor_active(struct i2c_client * client,int enable,int rate)136 static int sensor_active(struct i2c_client *client, int enable, int rate)
137 {
138 	struct sensor_private_data *sensor =
139 		(struct sensor_private_data *)i2c_get_clientdata(client);
140 	int result = 0;
141 	int status = 0;
142 
143 	sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
144 	if (!enable) {
145 		status = ~ALS_ENABLE;
146 		sensor->ops->ctrl_data &= status;
147 	} else {
148 		status |= ALS_ENABLE;
149 		sensor->ops->ctrl_data |= status;
150 	}
151 
152 	dev_dbg(&client->dev, "reg=0x%x, reg_ctrl=0x%x, enable=%d\n",
153 		sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
154 
155 	result = sensor_write_reg(client, sensor->ops->ctrl_reg,
156 				  sensor->ops->ctrl_data);
157 	if (result)
158 		dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
159 
160 	return result;
161 }
162 
sensor_init(struct i2c_client * client)163 static int sensor_init(struct i2c_client *client)
164 {
165 	struct sensor_private_data *sensor =
166 		(struct sensor_private_data *)i2c_get_clientdata(client);
167 	struct device_node *np = client->dev.of_node;
168 	int als_val = 0;
169 	int val = 0;
170 	int ret = 0;
171 
172 	ret = sensor->ops->active(client, 0, 0);
173 	if (ret) {
174 		dev_err(&client->dev, "%s:sensor active fail\n", __func__);
175 		return ret;
176 	}
177 	sensor->status_cur = SENSOR_OFF;
178 
179 	ret = of_property_read_u32(np, "als_threshold_low", &als_val);
180 	if (ret)
181 		dev_err(&client->dev, "%s:Unable to read als_threshold_low\n",
182 			__func__);
183 	ret = sensor_write_reg(client, THDL1_ALS,
184 			       (unsigned char)(als_val >> 8));
185 	if (ret) {
186 		dev_err(&client->dev, "%s:write THDL1_ALS fail\n", __func__);
187 		return ret;
188 	}
189 
190 	ret = sensor_write_reg(client, THDL2_ALS, (unsigned char)als_val);
191 	if (ret) {
192 		dev_err(&client->dev, "%s:write THDL1_ALS fail\n", __func__);
193 		return ret;
194 	}
195 
196 	ret = of_property_read_u32(np, "als_threshold_high", &als_val);
197 	if (ret)
198 		dev_err(&client->dev, "%s:Unable to read als_threshold_high\n",
199 			__func__);
200 
201 	ret = sensor_write_reg(client, THDH1_ALS,
202 			       (unsigned char)(als_val >> 8));
203 	if (ret) {
204 		dev_err(&client->dev, "%s:write THDH1_ALS fail\n", __func__);
205 		return ret;
206 	}
207 
208 	ret = sensor_write_reg(client, THDH2_ALS, (unsigned char)als_val);
209 	if (ret) {
210 		dev_err(&client->dev, "%s:write THDH1_ALS fail\n", __func__);
211 		return ret;
212 	}
213 
214 	ret = of_property_read_u32(np, "als_ctrl_gain", &als_val);
215 	if (ret)
216 		dev_err(&client->dev, "%s:Unable to read als_ctrl_gain\n",
217 			__func__);
218 
219 	ret = sensor_write_reg(client, ALS_CTRL1,
220 			       (unsigned char)((als_val << 4) | ALS_REFT_MS));
221 	if (ret) {
222 		dev_err(&client->dev, "%s:write ALS_CTRL fail\n", __func__);
223 		return ret;
224 	}
225 
226 	val = sensor_read_reg(client, INT_CTRL1);
227 	val &= ~INT_CTRL_PS_AND_LS;
228 	if (sensor->pdata->irq_enable)
229 		val |= ALS_INT_ENABLE;
230 	else
231 		val &= ~ALS_INT_ENABLE;
232 	ret = sensor_write_reg(client, INT_CTRL1, val);
233 	if (ret) {
234 		dev_err(&client->dev, "%s:write INT_CTRL1 fail\n", __func__);
235 		return ret;
236 	}
237 
238 	return ret;
239 }
240 
light_report_value(struct input_dev * input,int data)241 static int light_report_value(struct input_dev *input, int data)
242 {
243 	unsigned char index = 0;
244 
245 	if (data <= 100) {
246 		index = 0;
247 		goto report;
248 	} else if (data <= 1600) {
249 		index = 1;
250 		goto report;
251 	} else if (data <= 2250) {
252 		index = 2;
253 		goto report;
254 	} else if (data <= 3200) {
255 		index = 3;
256 		goto report;
257 	} else if (data <= 6400) {
258 		index = 4;
259 		goto report;
260 	} else if (data <= 12800) {
261 		index = 5;
262 		goto report;
263 	} else if (data <= 26000) {
264 		index = 6;
265 		goto report;
266 	} else {
267 		index = 7;
268 		goto report;
269 	}
270 
271 report:
272 	input_report_abs(input, ABS_MISC, index);
273 	input_sync(input);
274 	return index;
275 }
276 
sensor_report_value(struct i2c_client * client)277 static int sensor_report_value(struct i2c_client *client)
278 {
279 	struct sensor_private_data *sensor =
280 		(struct sensor_private_data *)i2c_get_clientdata(client);
281 	int result = 0;
282 	int value = 0;
283 	int index = 0;
284 	char buffer[2] = { 0 };
285 
286 	if (sensor->ops->read_len < 2) {
287 		dev_err(&client->dev, "%s:length is error, len=%d\n", __func__,
288 			sensor->ops->read_len);
289 		return -EINVAL;
290 	}
291 
292 	buffer[0] = sensor->ops->read_reg;
293 	result = sensor_rx_data(client, buffer, sensor->ops->read_len);
294 	if (result) {
295 		dev_err(&client->dev, "%s:sensor read data fail\n", __func__);
296 		return result;
297 	}
298 	value = (buffer[0] << 8) | buffer[1];
299 	index = light_report_value(sensor->input_dev, value);
300 	dev_dbg(&client->dev, "%s result=0x%x, index=%d\n",
301 		sensor->ops->name, value, index);
302 
303 	if (sensor->pdata->irq_enable && sensor->ops->int_status_reg) {
304 		value = sensor_read_reg(client, sensor->ops->int_status_reg);
305 		if (value & STK_FLAG_ALS_INT) {
306 			value &= ~STK_FLAG_ALS_INT;
307 			result = sensor_write_reg(client,
308 						  sensor->ops->int_status_reg,
309 						  value);
310 			if (result) {
311 				dev_err(&client->dev, "write status reg error\n");
312 				return result;
313 			}
314 		}
315 	}
316 
317 	return result;
318 }
319 
320 static struct sensor_operate light_stk3332_ops = {
321 	.name			= "ls_stk3332",
322 	.type			= SENSOR_TYPE_LIGHT,
323 	.id_i2c			= LIGHT_ID_STK3332,
324 	.read_reg		= DATA1_ALS,
325 	.read_len		= 2,
326 	.id_reg			= SENSOR_UNKNOW_DATA,
327 	.id_data		= SENSOR_UNKNOW_DATA,
328 	.precision		= 16,
329 	.ctrl_reg		= STK_STATE,
330 	.int_status_reg         = STK_FLAG,
331 	.range			= { 100, 65535 },
332 	.brightness		= { 10, 255 },
333 	.trig			= IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
334 	.active			= sensor_active,
335 	.init			= sensor_init,
336 	.report			= sensor_report_value,
337 };
338 
light_stk3332_probe(struct i2c_client * client,const struct i2c_device_id * devid)339 static int light_stk3332_probe(struct i2c_client *client,
340 			       const struct i2c_device_id *devid)
341 {
342 	return sensor_register_device(client, NULL, devid, &light_stk3332_ops);
343 }
344 
light_stk3332_remove(struct i2c_client * client)345 static int light_stk3332_remove(struct i2c_client *client)
346 {
347 	return sensor_unregister_device(client, NULL, &light_stk3332_ops);
348 }
349 
350 static const struct i2c_device_id light_stk3332_id[] = {
351 	{ "ls_stk3332", LIGHT_ID_STK3332 },
352 	{}
353 };
354 
355 static struct i2c_driver light_stk3332_driver = {
356 	.probe = light_stk3332_probe,
357 	.remove = light_stk3332_remove,
358 	.shutdown = sensor_shutdown,
359 	.id_table = light_stk3332_id,
360 	.driver = {
361 		.name = "light_stk3332",
362 #ifdef CONFIG_PM
363 		.pm = &sensor_pm_ops,
364 #endif
365 	},
366 };
367 
368 module_i2c_driver(light_stk3332_driver);
369 
370 MODULE_AUTHOR("Kay Guo <kay.guo@rock-chips.com>");
371 MODULE_DESCRIPTION("stk3332 light driver");
372 MODULE_LICENSE("GPL");
373