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