1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * kernel/drivers/input/sensors/lsensor/ls_em3071x.c
4 *
5 * Copyright (C) 2020 Rockchip Co.,Ltd.
6 * Author: Wang Jie <dave.wang@rock-chips.com>
7 */
8
9 #include <linux/interrupt.h>
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/irq.h>
13 #include <linux/miscdevice.h>
14 #include <linux/gpio.h>
15 #include <linux/uaccess.h>
16 #include <linux/atomic.h>
17 #include <linux/delay.h>
18 #include <linux/input.h>
19 #include <linux/workqueue.h>
20 #include <linux/freezer.h>
21 #include <linux/of_gpio.h>
22 #ifdef CONFIG_HAS_EARLYSUSPEND
23 #include <linux/earlysuspend.h>
24 #endif
25 #include <linux/sensor-dev.h>
26
27 #define ALS_CMD 0x01
28 #define STA_TUS 0X02
29 #define ALS_DT1 0x09
30 #define ALS_DT2 0X0a
31 #define ALS_THDL1 0X05
32 #define ALS_THDL2 0X06
33 #define ALS_THDH1 0X07
34
35 #define SW_RESET 0X0E
36
37 /* ALS_CMD */
38 #define ALS_SD_ENABLE 0x06
39 #define ALS_SD_DISABLE 0xF8
40 #define ALS_INT_DISABLE (0 << 1)
41 #define ALS_INT_ENABLE (1 << 1)
42 #define ALS_1T_100MS (0 << 2)
43 #define ALS_2T_200MS (1 << 2)
44 #define ALS_4T_400MS (2 << 2)
45 #define ALS_8T_800MS (3 << 2)
46 #define ALS_RANGE_57671 (0 << 6)
47 #define ALS_RANGE_28836 (1 << 6)
48
49 /* PS_CMD */
50 #define PS_SD_ENABLE (0 << 0)
51 #define PS_SD_DISABLE (1 << 0)
52 #define PS_INT_DISABLE (0 << 1)
53 #define PS_INT_ENABLE (1 << 1)
54 #define PS_10T_2MS (0 << 2)
55 #define PS_15T_3MS (1 << 2)
56 #define PS_20T_4MS (2 << 2)
57 #define PS_25T_5MS (3 << 2)
58 #define PS_CUR_100MA (0 << 4)
59 #define PS_CUR_200MA (1 << 4)
60 #define PS_SLP_10MS (0 << 5)
61 #define PS_SLP_30MS (1 << 5)
62 #define PS_SLP_90MS (2 << 5)
63 #define PS_SLP_270MS (3 << 5)
64 #define TRIG_PS_OR_LS (0 << 7)
65 #define TRIG_PS_AND_LS (1 << 7)
66
67 /* STA_TUS */
68 #define STA_PS_INT (1 << 5)
69 #define STA_ALS_INT (1 << 4)
70
sensor_active(struct i2c_client * client,int enable,int rate)71 static int sensor_active(struct i2c_client *client, int enable, int rate)
72 {
73 struct sensor_private_data *sensor =
74 (struct sensor_private_data *) i2c_get_clientdata(client);
75 int result = 0;
76 int status = 0;
77
78 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
79 if (enable) {
80 status = ALS_SD_ENABLE;
81 sensor->ops->ctrl_data |= status;
82 } else {
83 status = ~ALS_SD_ENABLE;
84 sensor->ops->ctrl_data &= status;
85 }
86
87 result = sensor_write_reg(client, sensor->ops->ctrl_reg,
88 sensor->ops->ctrl_data);
89 if (result) {
90 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
91 return result;
92 }
93
94 dev_dbg(&client->dev, "%s:reg = 0x%x, reg_ctrl = 0x%x, enable= %d\n",
95 __func__,
96 sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
97
98 return result;
99 }
100
101
sensor_init(struct i2c_client * client)102 static int sensor_init(struct i2c_client *client)
103 {
104 struct sensor_private_data *sensor =
105 (struct sensor_private_data *) i2c_get_clientdata(client);
106 int result = 0;
107
108 result = sensor->ops->active(client, 0, 0);
109 if (result) {
110 dev_err(&client->dev, "%s: line = %d, result = %d\n",
111 __func__, __LINE__, result);
112 return result;
113 }
114
115 sensor->status_cur = SENSOR_OFF;
116 result = sensor_write_reg(client, SW_RESET, 0);
117 if (result) {
118 dev_err(&client->dev,
119 "%s: fail to set SW_RESET(%d)\n", __func__, result);
120 return result;
121 }
122
123 /* it is important,if not then als can not trig intterupt */
124 result = sensor_write_reg(client, ALS_THDL1, 0);
125 if (result) {
126 dev_err(&client->dev,
127 "%s: fail to set ALS_THDL1(%d)\n", __func__, result);
128 return result;
129 }
130
131 result = sensor_write_reg(client, ALS_THDL2, 0XF0);
132 if (result) {
133 dev_err(&client->dev,
134 "%s: fail to set ALS_THDL2(%d)\n", __func__, result);
135 return result;
136 }
137
138 result = sensor_write_reg(client, ALS_THDH1, 0XFF);
139 if (result) {
140 dev_err(&client->dev,
141 "%s: fail to set ALS_THDH1(%d)\n", __func__, result);
142 return result;
143 }
144
145 result = sensor_write_reg(client, STA_TUS, 0X00);
146 if (result) {
147 dev_err(&client->dev,
148 "%s: fail to set STA_TUS(%d)\n", __func__, result);
149 return result;
150 }
151
152 return result;
153 }
154
155
light_report_value(struct input_dev * input,int data)156 static int light_report_value(struct input_dev *input, int data)
157 {
158 unsigned char index = 0;
159
160 if (data <= 10) {
161 index = 0;
162 goto report;
163 } else if (data <= 60) {
164 index = 1;
165 goto report;
166 } else if (data <= 122) {
167 index = 2;
168 goto report;
169 } else if (data <= 200) {
170 index = 3;
171 goto report;
172 } else if (data <= 400) {
173 index = 4;
174 goto report;
175 } else if (data <= 800) {
176 index = 5;
177 goto report;
178 } else if (data <= 1260) {
179 index = 6;
180 goto report;
181 } else {
182 index = 7;
183 goto report;
184 }
185
186 report:
187 input_report_abs(input, ABS_MISC, index);
188 input_sync(input);
189
190 return index;
191 }
192
193
sensor_report_value(struct i2c_client * client)194 static int sensor_report_value(struct i2c_client *client)
195 {
196 struct sensor_private_data *sensor =
197 (struct sensor_private_data *) i2c_get_clientdata(client);
198 int result = 0;
199 int value = 0;
200 char buffer[2] = {0};
201 char index = 0;
202
203 buffer[0] = sensor_read_reg(client, 0X09);
204 buffer[1] = sensor_read_reg(client, 0X0A);
205 value = ((buffer[1] & 0X0F) << 8) | buffer[0];
206
207 index = light_report_value(sensor->input_dev, value);
208
209 dev_dbg(&client->dev,
210 "%s: value = %d, index = %d\n", __func__, value, index);
211
212 if (sensor->pdata->irq_enable) {
213 value = sensor_read_reg(client, sensor->ops->int_status_reg);
214 if (value & STA_ALS_INT) {
215 value &= ~STA_ALS_INT;
216 result = sensor_write_reg(client,
217 sensor->ops->int_status_reg, value);
218 if (result) {
219 dev_err(&client->dev,
220 "%s:write status reg error(%d)\n",
221 __func__, result);
222 return result;
223 }
224 }
225 }
226
227 return result;
228 }
229
230 static struct sensor_operate light_em3071x_ops = {
231 .name = "ls_em3071x",
232 .type = SENSOR_TYPE_LIGHT,
233 .id_i2c = LIGHT_ID_EM3071X,
234 .read_reg = ALS_DT1,
235 .read_len = 2,
236 .id_reg = SENSOR_UNKNOW_DATA,
237 .id_data = SENSOR_UNKNOW_DATA,
238 .precision = 16,
239 .ctrl_reg = ALS_CMD,
240 .int_status_reg = STA_TUS,
241 .range = {100, 65535},
242 .brightness = {10, 255},
243 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
244 .active = sensor_active,
245 .init = sensor_init,
246 .report = sensor_report_value,
247 };
248
249 /****************operate according to sensor chip:end************/
light_em3071x_probe(struct i2c_client * client,const struct i2c_device_id * devid)250 static int light_em3071x_probe(struct i2c_client *client,
251 const struct i2c_device_id *devid)
252 {
253 return sensor_register_device(client, NULL, devid, &light_em3071x_ops);
254 }
255
light_em3071x_remove(struct i2c_client * client)256 static int light_em3071x_remove(struct i2c_client *client)
257 {
258 return sensor_unregister_device(client, NULL, &light_em3071x_ops);
259 }
260
261 static const struct i2c_device_id light_em3071x_id[] = {
262 {"ls_em3071x", LIGHT_ID_EM3071X},
263 {}
264 };
265
266 static struct i2c_driver light_em3071x_driver = {
267 .probe = light_em3071x_probe,
268 .remove = light_em3071x_remove,
269 .shutdown = sensor_shutdown,
270 .id_table = light_em3071x_id,
271 .driver = {
272 .name = "light_em3071x",
273 #ifdef CONFIG_PM
274 .pm = &sensor_pm_ops,
275 #endif
276 },
277 };
278
279 module_i2c_driver(light_em3071x_driver);
280
281 MODULE_AUTHOR("Wang Jie <dave.wang@rock-chips.com>");
282 MODULE_DESCRIPTION("em3071x light driver");
283 MODULE_LICENSE("GPL");
284