1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2022 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 SYSM_CTRL 0x00
26 #define INT_CTRL 0x01
27 #define INT_FLAG 0x02
28 #define WAIT_TIME 0x03
29 #define ALS_GAIN 0x04
30 #define ALS_TIME 0x05
31 #define LED_CTRL 0x06
32 #define PS_GAIN 0x07
33 #define PS_PULSE 0x08
34 #define PS_TIME 0x09
35
36 #define PERSISTENCE 0x0B
37 #define ALS_THR_LL 0x0C
38 #define ALS_THR_LH 0x0D
39 #define ALS_THR_HL 0x0E
40 #define ALS_THR_HH 0x0F
41 #define PS_THR_LL 0x10
42 #define PS_THR_LH 0x11
43 #define PS_THR_HL 0x12
44 #define PS_THR_HH 0x13
45 #define PS_OFFSET_L 0x14
46 #define PS_OFFSET_H 0x15
47 #define INT_SOURCE 0x16
48 #define ERROR_FLAG 0x17
49 #define PS_DATA_L 0x18
50 #define PS_DATA_H 0x19
51 #define IR_DATA_L 0x1A
52 #define IR_DATA_H 0x1B
53 #define CH0_DATA_L 0x1C
54 #define CH0_DATA_H 0x1D
55 #define CH1_DATA_L 0x1E
56 #define CH1_DATA_H 0x1F
57
58 /* SYSM_CTRL 0x00 */
59 #define ALS_DISABLE (0 << 0)
60 #define ALS_ENABLE (1 << 0)
61 #define PS_DISABLE (0 << 1)
62 #define PS_ENABLE (1 << 1)
63 #define FRST_DISABLE (0 << 5)
64 #define FRST_ENABLE (1 << 5)
65 #define WAIT_DISABLE (0 << 6)
66 #define WAIT_ENABLE (1 << 6)
67 #define SWRST_START (1 << 7)
68
69 /* INT_CTRL 0x01 */
70 #define AINT_DISABLE (0 << 0)
71 #define AINT_ENABLE (1 << 0)
72 #define PINT_DISABLE (0 << 1)
73 #define PINT_ENABLE (1 << 1)
74 #define ALS_PEND_EN (1 << 4)
75 #define ALS_PEND_DIS (0 << 4)
76 #define PS_PEND_EN (1 << 5)
77 #define PS_PEND_DIS (0 << 5)
78 #define SPEED_UP_EN (1 << 6)
79 #define SPEED_UP_DIS (0 << 6)
80 #define PS_INT_HYS (0 << 7)
81 #define PS_INT_ZONE (1 << 7)
82
83 /* INT_FLAG 0x02 */
84 #define ALS_INT_FLAG (1 << 0)
85 #define PS_INT_FLAG (1 << 1)
86 #define OBJ_DET_FLAG (1 << 5)
87 #define DATA_INVALID (1 << 6)
88 #define POWER_ON_FLAG (1 << 7)
89
90 /* WAIT_TIME 0x03 */
91 #define WAIT_TIME_5MS(X) (X)
92 /* ALS_GAIN 0x04*/
93 #define ALS_GAIN_1 0x00
94 #define ALS_GAIN_4 0x01
95 #define ALS_GAIN_8 0x02
96 #define ALS_GAIN_32 0x03
97 #define ALS_GAIN_96 0x04
98 #define ALS_GAIN_192 0x05
99 #define ALS_GAIN_368 0x06
100
101 /* ALS_TIME 0x05 */
102 #define ALS_GET_TIME 0x30
103 /* LED_CTRL */
104 #define IR_12_5MA (0 << 6)
105 #define IR_100MA (1 << 6)
106 #define IR_150MA (2 << 6)
107 #define IR_200MA (3 << 6)
108
109 /* PS_GAIN 0x07 */
110 #define PS_GAIN_1 (1 << 0)
111 #define PS_GAIN_2 (1 << 1)
112 #define PS_GAIN_4 (1 << 2)
113 #define PS_GAIN_8 (1 << 4)
114
115
sensor_active(struct i2c_client * client,int enable,int rate)116 static int sensor_active(struct i2c_client *client, int enable, int rate)
117 {
118 struct sensor_private_data *sensor =
119 (struct sensor_private_data *)i2c_get_clientdata(client);
120 int result = 0;
121 int status = 0;
122
123 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
124 if (!enable) {
125 status = ~ALS_ENABLE;
126 sensor->ops->ctrl_data &= status;
127 } else {
128 status |= ALS_ENABLE;
129 sensor->ops->ctrl_data |= status;
130 }
131
132 dev_dbg(&client->dev, "reg=0x%x, reg_ctrl=0x%x, enable=%d\n",
133 sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
134
135 result = sensor_write_reg(client, sensor->ops->ctrl_reg,
136 sensor->ops->ctrl_data);
137 if (result)
138 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
139
140 return result;
141 }
142
sensor_init(struct i2c_client * client)143 static int sensor_init(struct i2c_client *client)
144 {
145 struct sensor_private_data *sensor =
146 (struct sensor_private_data *)i2c_get_clientdata(client);
147 struct device_node *np = client->dev.of_node;
148 int als_val = 0;
149 int val = 0;
150 int ret = 0;
151
152 ret = sensor->ops->active(client, 0, 0);
153 if (ret) {
154 dev_err(&client->dev, "%s:sensor active fail\n", __func__);
155 return ret;
156 }
157 sensor->status_cur = SENSOR_OFF;
158
159 ret = of_property_read_u32(np, "als_threshold_low", &als_val);
160 if (ret)
161 dev_err(&client->dev, "%s:Unable to read als_threshold_low\n",
162 __func__);
163 ret = sensor_write_reg(client, ALS_THR_LH,
164 (unsigned char)(als_val >> 8));
165 if (ret) {
166 dev_err(&client->dev, "%s:write ALS_THR_LH fail\n", __func__);
167 return ret;
168 }
169
170 ret = sensor_write_reg(client, ALS_THR_LL, (unsigned char)als_val);
171 if (ret) {
172 dev_err(&client->dev, "%s:write ALS_THR_LL fail\n", __func__);
173 return ret;
174 }
175
176 ret = of_property_read_u32(np, "als_threshold_high", &als_val);
177 if (ret)
178 dev_err(&client->dev, "%s:Unable to read als_threshold_high\n",
179 __func__);
180
181 ret = sensor_write_reg(client, ALS_THR_HH,
182 (unsigned char)(als_val >> 8));
183 if (ret) {
184 dev_err(&client->dev, "%s:write ALS_THR_HH fail\n", __func__);
185 return ret;
186 }
187
188 ret = sensor_write_reg(client, ALS_THR_HL, (unsigned char)als_val);
189 if (ret) {
190 dev_err(&client->dev, "%s:write ALS_THR_HL fail\n", __func__);
191 return ret;
192 }
193
194 ret = of_property_read_u32(np, "als_ctrl_gain", &als_val);
195 if (ret)
196 dev_err(&client->dev, "%s:Unable to read als_ctrl_gain\n",
197 __func__);
198
199 ret = sensor_write_reg(client, ALS_GAIN, (unsigned char)als_val);
200 if (ret) {
201 dev_err(&client->dev, "%s:write ALS_GAIN fail\n", __func__);
202 return ret;
203 }
204
205
206 ret = of_property_read_u32(np, "als_ctrl_time", &als_val);
207 if (ret)
208 dev_err(&client->dev, "%s:Unable to read als_ctrl_time\n",
209 __func__);
210
211 ret = sensor_write_reg(client, ALS_TIME, (unsigned char)als_val);
212 if (ret) {
213 dev_err(&client->dev, "%s:write ALS_TIME fail\n", __func__);
214 return ret;
215 }
216
217 val = sensor_read_reg(client, INT_CTRL);
218 if (sensor->pdata->irq_enable)
219 val |= AINT_ENABLE;
220 else
221 val &= ~AINT_ENABLE;
222 ret = sensor_write_reg(client, INT_CTRL, val);
223 if (ret) {
224 dev_err(&client->dev, "%s:write INT_CTRL fail\n", __func__);
225 return ret;
226 }
227
228 return ret;
229 }
230
light_report_value(struct input_dev * input,int data)231 static int light_report_value(struct input_dev *input, int data)
232 {
233 unsigned char index = 0;
234
235 if (data <= 50) {
236 index = 0;
237 goto report;
238 } else if (data <= 160) {
239 index = 1;
240 goto report;
241 } else if (data <= 640) {
242 index = 2;
243 goto report;
244 } else if (data <= 1280) {
245 index = 3;
246 goto report;
247 } else if (data <= 2600) {
248 index = 4;
249 goto report;
250 } else if (data <= 10240) {
251 index = 5;
252 goto report;
253 } else if (data <= 20000) {
254 index = 6;
255 goto report;
256 } else {
257 index = 7;
258 goto report;
259 }
260
261 report:
262 input_report_abs(input, ABS_MISC, index);
263 input_sync(input);
264 return index;
265 }
266
sensor_report_value(struct i2c_client * client)267 static int sensor_report_value(struct i2c_client *client)
268 {
269 struct sensor_private_data *sensor =
270 (struct sensor_private_data *)i2c_get_clientdata(client);
271 int result = 0;
272 int value, ch0_value = 0;
273 int index = 0;
274 char buffer[4] = { 0 };
275
276 if (sensor->ops->read_len < 4) {
277 dev_err(&client->dev, "%s:length is error, len=%d\n", __func__,
278 sensor->ops->read_len);
279 return -EINVAL;
280 }
281
282 buffer[0] = sensor->ops->read_reg;
283 result = sensor_rx_data(client, buffer, sensor->ops->read_len);
284 if (result) {
285 dev_err(&client->dev, "%s:sensor read data fail\n", __func__);
286 return result;
287 }
288 ch0_value = (buffer[1] << 8) | buffer[0];
289 index = light_report_value(sensor->input_dev, ch0_value);
290 dev_dbg(&client->dev, "%s result=0x%x, ch0_index=%d\n",
291 sensor->ops->name, ch0_value, index);
292
293 if (sensor->pdata->irq_enable && sensor->ops->int_status_reg) {
294 value = sensor_read_reg(client, sensor->ops->int_status_reg);
295 if (value & ALS_INT_FLAG) {
296 value &= ~ALS_INT_FLAG;
297 result = sensor_write_reg(client,
298 sensor->ops->int_status_reg,
299 value);
300 if (result) {
301 dev_err(&client->dev, "write status reg error\n");
302 return result;
303 }
304 }
305 }
306
307 return result;
308 }
309
310 static struct sensor_operate light_ucs14620_ops = {
311 .name = "ls_ucs14620",
312 .type = SENSOR_TYPE_LIGHT,
313 .id_i2c = LIGHT_ID_UCS14620,
314 .read_reg = CH0_DATA_L,
315 .read_len = 4,
316 .id_reg = SENSOR_UNKNOW_DATA,
317 .id_data = SENSOR_UNKNOW_DATA,
318 .precision = 16,
319 .ctrl_reg = SYSM_CTRL,
320 .int_status_reg = INT_FLAG,
321 .range = { 100, 65535 },
322 .brightness = { 10, 255 },
323 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
324 .active = sensor_active,
325 .init = sensor_init,
326 .report = sensor_report_value,
327 };
328
light_ucs14620_probe(struct i2c_client * client,const struct i2c_device_id * devid)329 static int light_ucs14620_probe(struct i2c_client *client,
330 const struct i2c_device_id *devid)
331 {
332 return sensor_register_device(client, NULL, devid, &light_ucs14620_ops);
333 }
334
light_ucs14620_remove(struct i2c_client * client)335 static int light_ucs14620_remove(struct i2c_client *client)
336 {
337 return sensor_unregister_device(client, NULL, &light_ucs14620_ops);
338 }
339
340 static const struct i2c_device_id light_ucs14620_id[] = {
341 { "ls_ucs14620", LIGHT_ID_UCS14620 },
342 {}
343 };
344
345 static struct i2c_driver light_ucs14620_driver = {
346 .probe = light_ucs14620_probe,
347 .remove = light_ucs14620_remove,
348 .shutdown = sensor_shutdown,
349 .id_table = light_ucs14620_id,
350 .driver = {
351 .name = "light_ucs14620",
352 #ifdef CONFIG_PM
353 .pm = &sensor_pm_ops,
354 #endif
355 },
356 };
357
358 module_i2c_driver(light_ucs14620_driver);
359
360 MODULE_AUTHOR("Kay Guo <kay.guo@rock-chips.com>");
361 MODULE_DESCRIPTION("ucs14620 light driver");
362 MODULE_LICENSE("GPL");
363