xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/psensor/ps_stk3410.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *
3  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4  *
5  * Authors: Bin Yang <yangbin@rock-chips.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2  of
9  * the License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/irq.h>
21 #include <linux/miscdevice.h>
22 #include <linux/gpio.h>
23 #include <linux/uaccess.h>
24 #include <linux/atomic.h>
25 #include <linux/delay.h>
26 #include <linux/input.h>
27 #include <linux/workqueue.h>
28 #include <linux/freezer.h>
29 #include <linux/of_gpio.h>
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
32 #endif
33 #include <linux/sensor-dev.h>
34 
35 #define	STK_STATE	0x00
36 #define	PS_CTRL		0x01
37 #define	ALS_CTRL	0x02
38 #define	LED_CTRL	0x03
39 #define	INT_CTRL	0x04
40 #define	STK_WAIT	0x05
41 #define	THDH1_PS	0x06
42 #define	THDH2_PS	0x07
43 #define	THDL1_PS	0x08
44 #define	THDL2_PS	0x09
45 #define	THDH1_ALS	0x0A
46 #define	THDH2_ALS	0x0B
47 #define	THDL1_ALS	0x0C
48 #define	THDL2_ALS	0x0D
49 #define	STK_FLAG	0x10
50 #define	DATA1_PS	0x11
51 #define	DATA2_PS	0x12
52 #define	DATA1_ALS	0x13
53 #define	DATA2_ALS	0x14
54 #define	DATA1_OFFSET	0x15
55 #define	DATA2_OFFSET	0x16
56 #define	DATA1_IR	0x17
57 #define	DATA2_IR	0x18
58 #define	DATA1_GS0	0x24
59 #define	DATA2_GS0	0x25
60 #define	DATA1_GS1	0x26
61 #define	DATA2_GS1	0x27
62 #define	STKPDT_ID	0x3E
63 #define	SOFT_RESET	0x80
64 
65 /* STK_STATE	0x00 */
66 #define	PS_DISABLE	(0 << 0)
67 #define	PS_ENABLE	(1 << 0)
68 #define	ALS_DISABLE	(0 << 1)
69 #define	ALS_ENABLE	(1 << 1)
70 #define	WAIT_DISABLE	(0 << 2)
71 #define	WAIT_ENABLE	(1 << 2)
72 #define	IRO_DISABLE	(0 << 4)
73 #define	IRO_ENABLE	(1 << 4)
74 #define	ASO_DISABLE	(0 << 5)
75 #define	ASO_ENABLE	(1 << 5)
76 #define	AK_DISABLE	(0 << 6)
77 #define	AK_ENABLE	(1 << 6)
78 #define	IRS_DISABLE	(0 << 7)
79 #define	IRS_ENABLE	(1 << 7)
80 
81 /* PS/GS_CTRL 0x01 */
82 #define	PS_REFT_MS	(1 << 0)	/* [3:0] 0.185 ms ,  default value is 0.37ms */
83 #define	PS_GAIN_1G	(0 << 4)
84 #define	PS_GAIN_4G	(1 << 4)
85 #define	PS_GAIN_16G	(2 << 4)
86 #define	PS_GAIN_64G	(3 << 4)
87 #define	PS_PRS_1T	(0 << 6)
88 #define	PS_PRS_4T	(1 << 6)
89 
90 /* ALS_CTRL 0x02 */
91 #define	ALS_REFT_MS	(9 << 0)/* [3:0] 0.185 ms,  default value is 94.85ms */
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_PRS_1T	(0 << 6)
97 #define	ALS_PRS_4T	(1 << 6)
98 
99 /* LED_CTRL 0x03 */
100 #define	LED_REFT_US	0x3F	/* [5:0] 2.89us ,  default value is 0.185ms */
101 #define	LED_CUR_12MA	(0 << 6)
102 #define	LED_CUR_25MA	(1 << 6)
103 #define	LED_CUR_50MA	(2 << 6)
104 #define	LED_CUR_100MA	(3 << 6)
105 
106 /* INT 0x04 */
107 #define	PS_INT_DISABLE		0xF8
108 #define	PS_INT_ENABLE		(1 << 0)
109 #define	PS_INT_ENABLE_FLGNFH	(2 << 0)
110 #define	PS_INT_ENABLE_FLGNFL	(3 << 0)
111 #define	PS_INT_MODE_ENABLE	(4 << 0)
112 #define	PS_INT_ENABLE_THL	(5 << 0)
113 #define	PS_INT_ENABLE_THH	(6 << 0)
114 #define	PS_INT_ENABLE_THHL	(7 << 0)
115 #define	ALS_INT_DISABLE		(0 << 3)
116 #define	ALS_INT_ENABLE		(1 << 3)
117 #define	INT_CTRL_PS_OR_LS	(0 << 7)
118 #define	INT_CTRL_PS_AND_LS	(1 << 7)
119 
120 /* FLAG 0x10 */
121 #define	STK_FLAG_NF	(1 << 0)
122 #define	STK_FLAG_IR_RDY	(1 << 1)
123 #define	STK_FLAG_OUI	(1 << 2)
124 #define	STK_FLAG_PSINT	(1 << 4)
125 #define	STK_FLAG_ALSINT	(1 << 5)
126 #define	STK_FLAG_PSDR	(1 << 6)
127 #define	STK_FLAG_ALSDR	(1 << 7)
128 
129 static int ps_threshold_low;
130 static int ps_threshold_high;
131 
sensor_active(struct i2c_client * client,int enable,int rate)132 static int sensor_active(struct i2c_client *client, int enable, int rate)
133 {
134 	struct sensor_private_data *sensor =
135 		(struct sensor_private_data *)i2c_get_clientdata(client);
136 	int result = 0;
137 	int status = 0;
138 
139 	sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
140 	if (!enable) {
141 		status = ~PS_ENABLE;
142 		sensor->ops->ctrl_data &= status;
143 	} else {
144 		status = PS_ENABLE;
145 		sensor->ops->ctrl_data |= status;
146 	}
147 
148 	DBG
149 	("%s:reg=0x%x, reg_ctrl=0x%x, enable=%d\n",
150 	__func__,
151 	sensor->ops->ctrl_reg,
152 	sensor->ops->ctrl_data,
153 	enable);
154 
155 	result = sensor_write_reg
156 			(client, sensor->ops->ctrl_reg,
157 			sensor->ops->ctrl_data);
158 	if (result)
159 		dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
160 
161 	return result;
162 }
163 
sensor_init(struct i2c_client * client)164 static int sensor_init(struct i2c_client *client)
165 {
166 	struct sensor_private_data *sensor =
167 		(struct sensor_private_data *)i2c_get_clientdata(client);
168 	struct device_node *np = client->dev.of_node;
169 	int ps_val = 0;
170 	int ret = 0;
171 	int result = 0;
172 	int val = 0;
173 
174 	result = sensor->ops->active(client, 0, 0);
175 	if (result) {
176 		dev_err(&client->dev, "%s:sensor active fail\n", __func__);
177 		return result;
178 	}
179 	sensor->status_cur = SENSOR_OFF;
180 
181 	ret = of_property_read_u32(np, "ps_threshold_low", &ps_val);
182 	if (ret)
183 		dev_err(&client->dev, "%s:Unable to read ps_threshold_low\n",
184 			__func__);
185 
186 	ps_threshold_low = ps_val;
187 	result = sensor_write_reg(client, THDL1_PS, (unsigned char)ps_val);
188 	if (result) {
189 		dev_err(&client->dev, "%s:write THDL1_PS fail\n", __func__);
190 		return result;
191 	}
192 	result = sensor_write_reg
193 			(client, THDL2_PS,
194 			(unsigned char)(ps_val >> 8));
195 	if (result) {
196 		dev_err(&client->dev, "%s:write THDL1_PS fail\n", __func__);
197 		return result;
198 	}
199 
200 	ret = of_property_read_u32(np, "ps_threshold_high", &ps_val);
201 	if (ret)
202 		dev_err(&client->dev, "%s:Unable to read ps_threshold_high\n",
203 			__func__);
204 
205 	ps_threshold_high = ps_val;
206 	result = sensor_write_reg(client, THDH1_PS, (unsigned char)ps_val);
207 	if (result) {
208 		dev_err(&client->dev, "%s:write THDH1_PS fail\n",
209 			__func__);
210 		return result;
211 	}
212 	result = sensor_write_reg
213 		(client, THDH2_PS, (unsigned char)(ps_val >> 8));
214 	if (result) {
215 		dev_err(&client->dev, "%s:write THDH1_PS fail\n",
216 			__func__);
217 		return result;
218 	}
219 
220 	ret = of_property_read_u32(np, "ps_ctrl_gain", &ps_val);
221 	if (ret)
222 		dev_err(&client->dev, "%s:Unable to read ps_ctrl_gain\n",
223 			__func__);
224 
225 	result = sensor_write_reg
226 		(client, PS_CTRL,
227 		(unsigned char)((ps_val << 4) | PS_REFT_MS));
228 	if (result) {
229 		dev_err(&client->dev, "%s:write PS_CTRL fail\n", __func__);
230 		return result;
231 	}
232 
233 	ret = of_property_read_u32(np, "ps_led_current", &ps_val);
234 	if (ret)
235 		dev_err(&client->dev, "%s:Unable to read ps_led_current\n",
236 			__func__);
237 
238 	result = sensor_write_reg
239 		(client, LED_CTRL,
240 		(unsigned char)((ps_val << 6) | LED_REFT_US));
241 	if (result) {
242 		dev_err(&client->dev, "%s:write LED_CTRL fail\n", __func__);
243 		return result;
244 	}
245 
246 	val = sensor_read_reg(client, INT_CTRL);
247 	val &= ~INT_CTRL_PS_AND_LS;
248 	if (sensor->pdata->irq_enable)
249 		val |= PS_INT_ENABLE_FLGNFL;
250 	else
251 		val &= PS_INT_DISABLE;
252 	result = sensor_write_reg(client, INT_CTRL, val);
253 	if (result) {
254 		dev_err(&client->dev, "%s:write INT_CTRL fail\n", __func__);
255 		return result;
256 	}
257 
258 	return result;
259 }
260 
stk3410_get_ps_value(int ps)261 static int stk3410_get_ps_value(int ps)
262 {
263 	int index = 0;
264 	static int val = 1;
265 
266 	if (ps > ps_threshold_high) {
267 		index = 0;
268 		val = 0;
269 	} else if (ps < ps_threshold_low) {
270 		index = 1;
271 		val = 1;
272 	} else {
273 		index = val;
274 	}
275 
276 	return index;
277 }
278 
sensor_report_value(struct i2c_client * client)279 static int sensor_report_value(struct i2c_client *client)
280 {
281 	struct sensor_private_data *sensor =
282 		(struct sensor_private_data *)i2c_get_clientdata(client);
283 	int result = 0;
284 	int value = 0;
285 	char buffer[2] = {0};
286 	int index = 1;
287 
288 	if (sensor->ops->read_len < 2) {
289 		dev_err(&client->dev, "%s:length is error, len=%d\n", __func__,
290 			sensor->ops->read_len);
291 		return -1;
292 	}
293 
294 	buffer[0] = sensor->ops->read_reg;
295 	result = sensor_rx_data(client, buffer, sensor->ops->read_len);
296 	if (result) {
297 		dev_err(&client->dev, "%s:sensor read data fail\n", __func__);
298 		return result;
299 	}
300 	value = (buffer[0] << 8) | buffer[1];
301 	index = stk3410_get_ps_value(value);
302 	input_report_abs(sensor->input_dev, ABS_DISTANCE, index);
303 	input_sync(sensor->input_dev);
304 
305 	DBG("%s:%s value=0x%x\n", __func__, sensor->ops->name, value);
306 
307 	if (sensor->pdata->irq_enable && sensor->ops->int_status_reg) {
308 		value = sensor_read_reg(client, sensor->ops->int_status_reg);
309 		if (value & STK_FLAG_NF) {
310 			value &= ~STK_FLAG_NF;
311 			result = sensor_write_reg
312 				(client, sensor->ops->int_status_reg,
313 				value);
314 			if (result) {
315 				dev_err(&client->dev, "%s:write status reg error\n",
316 					__func__);
317 				return result;
318 			}
319 		}
320 	}
321 
322 	return result;
323 }
324 
325 static struct sensor_operate psensor_stk3410_ops = {
326 	.name			= "ps_stk3410",
327 	.type			= SENSOR_TYPE_PROXIMITY,
328 	.id_i2c			= PROXIMITY_ID_STK3410,
329 	.read_reg		= DATA1_PS,
330 	.read_len		= 2,
331 	.id_reg			= SENSOR_UNKNOW_DATA,
332 	.id_data		= SENSOR_UNKNOW_DATA,
333 	.precision		= 16,
334 	.ctrl_reg		= STK_STATE,
335 	.int_status_reg	= STK_FLAG,
336 	.range			= {100, 65535},
337 	.brightness		= {10, 255},
338 	.trig			= IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
339 	.active			= sensor_active,
340 	.init			= sensor_init,
341 	.report			= sensor_report_value,
342 };
343 
proximity_stk3410_probe(struct i2c_client * client,const struct i2c_device_id * devid)344 static int proximity_stk3410_probe(struct i2c_client *client,
345 				   const struct i2c_device_id *devid)
346 {
347 	return sensor_register_device(client, NULL, devid, &psensor_stk3410_ops);
348 }
349 
proximity_stk3410_remove(struct i2c_client * client)350 static int proximity_stk3410_remove(struct i2c_client *client)
351 {
352 	return sensor_unregister_device(client, NULL, &psensor_stk3410_ops);
353 }
354 
355 static const struct i2c_device_id proximity_stk3410_id[] = {
356 	{"ps_stk3410", PROXIMITY_ID_STK3410},
357 	{}
358 };
359 
360 static struct i2c_driver proximity_stk3410_driver = {
361 	.probe = proximity_stk3410_probe,
362 	.remove = proximity_stk3410_remove,
363 	.shutdown = sensor_shutdown,
364 	.id_table = proximity_stk3410_id,
365 	.driver = {
366 		.name = "proximity_stk3410",
367 	#ifdef CONFIG_PM
368 		.pm = &sensor_pm_ops,
369 	#endif
370 	},
371 };
372 
373 module_i2c_driver(proximity_stk3410_driver);
374 
375 MODULE_AUTHOR("Bin Yang <yangbin@rock-chips.com>");
376 MODULE_DESCRIPTION("stk3410 proximity driver");
377 MODULE_LICENSE("GPL");
378