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_CTIR_EN 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
136 static int ps_threshold_low;
137 static int ps_threshold_high;
138 static int val_flag;
139
sensor_active(struct i2c_client * client,int enable,int rate)140 static int sensor_active(struct i2c_client *client, int enable, int rate)
141 {
142 struct sensor_private_data *sensor =
143 (struct sensor_private_data *)i2c_get_clientdata(client);
144 int result = 0;
145 int status = 0;
146
147 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
148 if (!enable) {
149 status = ~PS_ENABLE;
150 sensor->ops->ctrl_data &= status;
151 } else {
152 status = PS_ENABLE;
153 sensor->ops->ctrl_data |= status;
154 }
155
156 dev_dbg(&client->dev, "reg=0x%x, reg_ctrl=0x%x, enable=%d\n",
157 sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
158
159 result = sensor_write_reg(client, sensor->ops->ctrl_reg,
160 sensor->ops->ctrl_data);
161 if (result)
162 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
163
164 return result;
165 }
166
sensor_init(struct i2c_client * client)167 static int sensor_init(struct i2c_client *client)
168 {
169 struct sensor_private_data *sensor =
170 (struct sensor_private_data *)i2c_get_clientdata(client);
171 struct device_node *np = client->dev.of_node;
172 int ps_val = 0;
173 int result = 0;
174 int val = 0;
175
176 result = sensor->ops->active(client, 0, 0);
177 if (result) {
178 dev_err(&client->dev, "%s:sensor active fail\n", __func__);
179 return result;
180 }
181 sensor->status_cur = SENSOR_OFF;
182
183 result = of_property_read_u32(np, "ps_threshold_low", &ps_val);
184 if (result)
185 dev_err(&client->dev, "%s:Unable to read ps_threshold_low\n",
186 __func__);
187
188 ps_threshold_low = ps_val;
189 result = sensor_write_reg(client, THDL1_PS,
190 (unsigned char)(ps_val >> 8));
191 if (result) {
192 dev_err(&client->dev, "%s:write THDL1_PS fail\n", __func__);
193 return result;
194 }
195 result = sensor_write_reg(client, THDL2_PS, (unsigned char)ps_val);
196 if (result) {
197 dev_err(&client->dev, "%s:write THDL1_PS fail\n", __func__);
198 return result;
199 }
200
201 result = of_property_read_u32(np, "ps_threshold_high", &ps_val);
202 if (result)
203 dev_err(&client->dev, "%s:Unable to read ps_threshold_high\n",
204 __func__);
205
206 ps_threshold_high = ps_val;
207 result = sensor_write_reg(client, THDH1_PS,
208 (unsigned char)(ps_val >> 8));
209 if (result) {
210 dev_err(&client->dev, "%s:write THDH1_PS fail\n", __func__);
211 return result;
212 }
213
214 result = sensor_write_reg(client, THDH2_PS, (unsigned char)ps_val);
215 if (result) {
216 dev_err(&client->dev, "%s:write THDH1_PS fail\n", __func__);
217 return result;
218 }
219
220 result = of_property_read_u32(np, "ps_ctrl_gain", &ps_val);
221 if (result)
222 dev_err(&client->dev, "%s:Unable to read ps_ctrl_gain\n",
223 __func__);
224
225 result = sensor_write_reg(client, PS_CTRL,
226 (unsigned char)((ps_val << 4) | PS_IT_384US));
227 if (result) {
228 dev_err(&client->dev, "%s:write PS_CTRL fail\n", __func__);
229 return result;
230 }
231
232 result = of_property_read_u32(np, "ps_led_current", &ps_val);
233 if (result)
234 dev_err(&client->dev, "%s:Unable to read ps_led_current\n",
235 __func__);
236
237 result = sensor_write_reg(client, LED_CTRL,
238 (unsigned char)((ps_val << 5) | LED_CTIR_EN));
239 if (result) {
240 dev_err(&client->dev, "%s:write LED_CTRL fail\n", __func__);
241 return result;
242 }
243
244 val = sensor_read_reg(client, INT_CTRL1);
245 val &= ~INT_CTRL_PS_AND_LS;
246 if (sensor->pdata->irq_enable)
247 val |= PS_INT_ENABLE_FLGNFL;
248 else
249 val &= PS_INT_DISABLE;
250 result = sensor_write_reg(client, INT_CTRL1, val);
251 if (result) {
252 dev_err(&client->dev, "%s:write INT_CTRL fail\n", __func__);
253 return result;
254 }
255
256 return result;
257 }
258
stk3332_get_ps_value(int ps)259 static int stk3332_get_ps_value(int ps)
260 {
261 int index = 0;
262
263 if ((ps > ps_threshold_high) && (val_flag == 0)) {
264 index = 1;
265 val_flag = 1;
266 } else if ((ps < ps_threshold_low) && (val_flag == 1)) {
267 index = 0;
268 val_flag = 0;
269 } else {
270 index = -1;
271 }
272
273 return index;
274 }
275
sensor_report_value(struct i2c_client * client)276 static int sensor_report_value(struct i2c_client *client)
277 {
278 struct sensor_private_data *sensor =
279 (struct sensor_private_data *)i2c_get_clientdata(client);
280 int result = 0;
281 int value = 0;
282 char buffer[2] = { 0 };
283 int index = 1;
284
285 if (sensor->ops->read_len < 2) {
286 dev_err(&client->dev, "%s:length is error, len=%d\n", __func__,
287 sensor->ops->read_len);
288 return -EINVAL;
289 }
290
291 buffer[0] = sensor->ops->read_reg;
292 result = sensor_rx_data(client, buffer, sensor->ops->read_len);
293 if (result) {
294 dev_err(&client->dev, "%s:sensor read data fail\n", __func__);
295 return result;
296 }
297 value = (buffer[0] << 8) | buffer[1];
298
299 if (sensor->pdata->irq_enable && sensor->ops->int_status_reg) {
300 value = sensor_read_reg(client, sensor->ops->int_status_reg);
301 if (value & STK_FLAG_NF)
302 index = 0;
303 else
304 index = 1;
305 input_report_abs(sensor->input_dev, ABS_DISTANCE, index);
306 input_sync(sensor->input_dev);
307 value &= ~STK_FLAG_PS_INT;
308 result = sensor_write_reg(client,
309 sensor->ops->int_status_reg,
310 value);
311
312 dev_dbg(&client->dev, "%s object near = %d", sensor->ops->name, index);
313
314 if (result) {
315 dev_err(&client->dev, "write status reg error\n");
316 return result;
317 }
318 } else if (!sensor->pdata->irq_enable) {
319 index = stk3332_get_ps_value(value);
320 if (index >= 0) {
321 input_report_abs(sensor->input_dev, ABS_DISTANCE, index);
322 input_sync(sensor->input_dev);
323 dev_dbg(&client->dev, "%s sensor closed=%d\n",
324 sensor->ops->name, index);
325 }
326 }
327
328 return result;
329 }
330
331 static struct sensor_operate psensor_stk3332_ops = {
332 .name = "ps_stk3332",
333 .type = SENSOR_TYPE_PROXIMITY,
334 .id_i2c = PROXIMITY_ID_STK3332,
335 .read_reg = DATA1_PS,
336 .read_len = 2,
337 .id_reg = SENSOR_UNKNOW_DATA,
338 .id_data = SENSOR_UNKNOW_DATA,
339 .precision = 16,
340 .ctrl_reg = STK_STATE,
341 .int_status_reg = STK_FLAG,
342 .range = { 100, 65535 },
343 .brightness = { 10, 255 },
344 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
345 .active = sensor_active,
346 .init = sensor_init,
347 .report = sensor_report_value,
348 };
349
proximity_stk3332_probe(struct i2c_client * client,const struct i2c_device_id * devid)350 static int proximity_stk3332_probe(struct i2c_client *client,
351 const struct i2c_device_id *devid)
352 {
353 return sensor_register_device(client, NULL, devid, &psensor_stk3332_ops);
354 }
355
proximity_stk3332_remove(struct i2c_client * client)356 static int proximity_stk3332_remove(struct i2c_client *client)
357 {
358 return sensor_unregister_device(client, NULL, &psensor_stk3332_ops);
359 }
360
361 static const struct i2c_device_id proximity_stk3332_id[] = {
362 { "ps_stk3332", PROXIMITY_ID_STK3332 },
363 {}
364 };
365
366 static struct i2c_driver proximity_stk3332_driver = {
367 .probe = proximity_stk3332_probe,
368 .remove = proximity_stk3332_remove,
369 .shutdown = sensor_shutdown,
370 .id_table = proximity_stk3332_id,
371 .driver = {
372 .name = "proximity_stk3332",
373 #ifdef CONFIG_PM
374 .pm = &sensor_pm_ops,
375 #endif
376 },
377 };
378
379 module_i2c_driver(proximity_stk3332_driver);
380
381 MODULE_AUTHOR("Kay Guo<yangbin@rock-chips.com>");
382 MODULE_DESCRIPTION("stk3332 proximity driver");
383 MODULE_LICENSE("GPL");
384