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 #include "da215s_core.h"
26
27 /* Linear acceleration register */
28 #define DA215S_CONFIG 0X00
29 #define DA215S_CHIP_ID 0x01
30 #define ACC_X_LSB 0x02
31 #define ACC_X_MSB 0x03
32 #define ACC_Y_LSB 0x04
33 #define ACC_Y_MSB 0x05
34 #define ACC_Z_LSB 0x06
35 #define ACC_Z_MSB 0x07
36 #define MOTION_FLAG 0x09
37 #define NEWDATA_FLAG 0x0A
38 #define ACTIVE_STATUS 0x0B
39 #define DA215S_RANGE 0x0F
40 #define ODR_AXIS 0x10
41 #define DA215S_MODE_BW 0x11
42 #define SWAP_POLARITY 0x12
43 #define INT_ACTIVE_SET1 0x16
44 #define INT_DATA_SET2 0x17
45 #define INT_MAP1 0x19
46 #define INT_MAP2 0x1A
47 #define INT_CONFIG 0x20
48 #define INT_LATCH 0x21
49 #define ACTIVE_DUR 0x27
50 #define ACTIVE_THS 0x28
51
52 #define DA215S_CHIPID_DATA 0x13
53 #define DA215S_CTRL_NORMAL 0x34
54 #define DA215S_CTRL_SUSPEND 0x80
55 #define INT_ACTIVE_ENABLE 0x87
56 #define INT_NEW_DATA_ENABLE 0x10
57
58 #define DA215S_OFFSET_MAX 200
59 #define DA215S_OFFSET_CUS 130
60 #define DA215S_OFFSET_SEN 1024
61
62 #define GSENSOR_MIN 2
63 #define DA215S_PRECISION 14
64 #define DA215S_DATA_RANGE (16384*4)
65 #define DA215S_BOUNDARY (0x1 << (DA215S_PRECISION - 1))
66 #define DA215S_GRAVITY_STEP (DA215S_DATA_RANGE/DA215S_BOUNDARY)
67
68
sensor_active(struct i2c_client * client,int enable,int rate)69 static int sensor_active(struct i2c_client *client, int enable, int rate)
70 {
71 struct sensor_private_data *sensor =
72 (struct sensor_private_data *)i2c_get_clientdata(client);
73 int result = 0;
74
75 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
76 if (enable)
77 sensor->ops->ctrl_data &= DA215S_CTRL_NORMAL;
78 else
79 sensor->ops->ctrl_data |= DA215S_CTRL_SUSPEND;
80
81 result = sensor_write_reg(client, sensor->ops->ctrl_reg,
82 sensor->ops->ctrl_data);
83 if (result)
84 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
85
86 dev_dbg(&client->dev, "reg = 0x%x, reg_ctrl = 0x%x, enable= %d\n",
87 sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
88
89 return result;
90 }
91
sensor_init(struct i2c_client * client)92 static int sensor_init(struct i2c_client *client)
93 {
94 struct sensor_private_data *sensor =
95 (struct sensor_private_data *)i2c_get_clientdata(client);
96 int status = 0;
97 int result = 0;
98
99 result = sensor->ops->active(client, 0, 0);
100 if (result) {
101 dev_err(&client->dev,
102 "%s:line=%d,error\n", __func__, __LINE__);
103 return result;
104 }
105 sensor->status_cur = SENSOR_OFF;
106
107 result = sensor_write_reg(client, 0x00, 0x24);
108 mdelay(25);
109 /*+/-4G,14bit normal mode ODR = 62.5hz*/
110 result |= sensor_write_reg(client, DA215S_RANGE, 0x61);
111 result |= sensor_write_reg(client, DA215S_MODE_BW, 0x34);
112 result |= sensor_write_reg(client, ODR_AXIS, 0x06);
113 if (result) {
114 dev_err(&client->dev, "%s:fail to config DA215S_accel.\n",
115 __func__);
116 return result;
117 }
118
119
120 /* Enable or Disable for active Interrupt */
121 status = sensor_read_reg(client, INT_ACTIVE_SET1);
122 if (sensor->pdata->irq_enable)
123 status |= INT_ACTIVE_ENABLE;
124 else
125 status &= ~INT_ACTIVE_ENABLE;
126 result = sensor_write_reg(client, INT_ACTIVE_SET1, status);
127 if (result) {
128 dev_err(&client->dev,
129 "%s:fail to set DA215S_INT_ACTIVE.\n", __func__);
130 return result;
131 }
132
133 /* Enable or Disable for new data Interrupt */
134 status = sensor_read_reg(client, INT_DATA_SET2);
135 if (sensor->pdata->irq_enable)
136 status |= INT_NEW_DATA_ENABLE;
137 else
138 status &= ~INT_NEW_DATA_ENABLE;
139 result = sensor_write_reg(client, INT_DATA_SET2, status);
140 if (result) {
141 dev_err(&client->dev,
142 "%s:fail to set DA215S_INT_NEW_DATA.\n", __func__);
143 return result;
144 }
145
146 return result;
147 }
148
sensor_convert_data(struct i2c_client * client,unsigned char low_byte4,unsigned char high_byte8)149 static int sensor_convert_data(struct i2c_client *client,
150 unsigned char low_byte4, unsigned char high_byte8)
151 {
152 s64 result;
153
154 result = ((short)((high_byte8 << 8)|low_byte4)) >> 2;
155
156 return (int)result;
157 }
158
159
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)160 static int gsensor_report_value(struct i2c_client *client,
161 struct sensor_axis *axis)
162 {
163 struct sensor_private_data *sensor =
164 (struct sensor_private_data *)i2c_get_clientdata(client);
165 if ((abs(sensor->axis.x - axis->x) > GSENSOR_MIN) ||
166 (abs(sensor->axis.y - axis->y) > GSENSOR_MIN) ||
167 (abs(sensor->axis.z - axis->z) > GSENSOR_MIN)) {
168 input_report_abs(sensor->input_dev, ABS_X, axis->x);
169 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
170 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
171 input_sync(sensor->input_dev);
172 }
173
174 return 0;
175 }
176
sensor_report_value(struct i2c_client * client)177 static int sensor_report_value(struct i2c_client *client)
178 {
179 struct sensor_axis axis;
180 struct sensor_private_data *sensor =
181 (struct sensor_private_data *)i2c_get_clientdata(client);
182 struct sensor_platform_data *pdata = sensor->pdata;
183 unsigned char buffer[6] = {0};
184 int x = 0, y = 0, z = 0;
185 int ret = 0;
186 int tmp_x = 0, tmp_y = 0, tmp_z = 0;
187
188 if (sensor->ops->read_len < 6) {
189 dev_err(&client->dev, "%s:Read len is error,len= %d\n",
190 __func__, sensor->ops->read_len);
191 return -EINVAL;
192 }
193
194 *buffer = sensor->ops->read_reg;
195 sensor_rx_data(client, buffer, sensor->ops->read_len);
196 if (ret < 0) {
197 dev_err(&client->dev,
198 "da215s read data failed, ret = %d\n", ret);
199 return ret;
200 }
201
202 /* x,y,z axis is the 12-bit acceleration output */
203 x = sensor_convert_data(sensor->client, buffer[0], buffer[1]);
204 y = sensor_convert_data(sensor->client, buffer[2], buffer[3]);
205 z = sensor_convert_data(sensor->client, buffer[4], buffer[5]);
206
207 dev_dbg(&client->dev, "%s:x=%d, y=%d, z=%d\n", __func__, x, y, z);
208 da215s_temp_calibrate(&x, &y, &z);
209
210 dev_dbg(&client->dev, "%s:x=%d, y=%d, z=%d\n", __func__, x, y, z);
211
212 tmp_x = x * DA215S_GRAVITY_STEP;
213 tmp_y = y * DA215S_GRAVITY_STEP;
214 tmp_z = z * DA215S_GRAVITY_STEP;
215 dev_dbg(&client->dev, "%s:temp_x=%d, temp_y=%d, temp_z=%d\n",
216 __func__, tmp_x, tmp_y, tmp_z);
217
218 axis.x = (pdata->orientation[0]) * tmp_x + (pdata->orientation[1]) * tmp_y +
219 (pdata->orientation[2]) * tmp_z;
220 axis.y = (pdata->orientation[3]) * tmp_x + (pdata->orientation[4]) * tmp_y +
221 (pdata->orientation[5]) * tmp_z;
222 axis.z = (pdata->orientation[6]) * tmp_x + (pdata->orientation[7]) * tmp_y +
223 (pdata->orientation[8]) * tmp_z;
224 dev_dbg(&client->dev, "<map:>axis = %d, %d, %d\n", axis.x, axis.y, axis.z);
225
226 gsensor_report_value(client, &axis);
227
228 mutex_lock(&(sensor->data_mutex));
229 sensor->axis = axis;
230 mutex_unlock(&(sensor->data_mutex));
231
232 if (sensor->pdata->irq_enable) {
233 ret = sensor_write_reg(client, INT_MAP1, 0);
234 if (ret) {
235 dev_err(&client->dev,
236 "%s:fail to clear DA215S_INT_register.\n",
237 __func__);
238 return ret;
239 }
240 ret = sensor_write_reg(client, INT_MAP2, 0);
241 if (ret) {
242 dev_err(&client->dev,
243 "%s:fail to clear DA215S_INT_register.\n",
244 __func__);
245 return ret;
246 }
247 }
248
249 return ret;
250 }
251
252 /******************************************************************************/
sensor_suspend(struct i2c_client * client)253 static int sensor_suspend(struct i2c_client *client)
254 {
255 int result = 0;
256
257 // MI_FUN;
258 // result = mir3da_set_enable(client, false);
259 // if (result) {
260 // MI_ERR("sensor_suspend disable fail!!\n");
261 // return result;
262 // }
263
264 return result;
265 }
266
267 /******************************************************************************/
sensor_resume(struct i2c_client * client)268 static int sensor_resume(struct i2c_client *client)
269 {
270 int result = 0;
271
272 // MI_FUN;
273
274 /*
275 * result = mir3da_chip_resume(client);
276 * if(result) {
277 * MI_ERR("sensor_resume chip resume fail!!\n");
278 * return result;
279 * }
280 */
281 // result = mir3da_set_enable(client, true);
282 // if (result) {
283 // MI_ERR("sensor_resume enable fail!!\n");
284 // return result;
285 // }
286
287 return result;
288 }
289
290 static struct sensor_operate gsensor_da215s_ops = {
291 .name = "gs_da215s",
292 .type = SENSOR_TYPE_ACCEL,
293 .id_i2c = ACCEL_ID_DA215S,
294 .read_reg = ACC_X_LSB,
295 .read_len = 6,
296 .id_reg = DA215S_CHIP_ID,
297 .id_data = DA215S_CHIPID_DATA,
298 .precision = DA215S_PRECISION,
299 .ctrl_reg = DA215S_MODE_BW,
300 .int_status_reg = INT_MAP1,
301 .range = {-DA215S_DATA_RANGE, DA215S_DATA_RANGE},
302 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
303 .active = sensor_active,
304 .init = sensor_init,
305 .report = sensor_report_value,
306 .suspend = sensor_suspend,
307 .resume = sensor_resume,
308 };
309
gsensor_da215s_probe(struct i2c_client * client,const struct i2c_device_id * devid)310 static int gsensor_da215s_probe(struct i2c_client *client,
311 const struct i2c_device_id *devid)
312 {
313 return sensor_register_device(client, NULL, devid, &gsensor_da215s_ops);
314 }
315
gsensor_da215s_remove(struct i2c_client * client)316 static int gsensor_da215s_remove(struct i2c_client *client)
317 {
318 return sensor_unregister_device(client, NULL, &gsensor_da215s_ops);
319 }
320
321 static const struct i2c_device_id gsensor_da215s_id[] = {
322 {"gs_da215s", ACCEL_ID_DA215S},
323 {}
324 };
325
326 static struct i2c_driver gsensor_da215s_driver = {
327 .probe = gsensor_da215s_probe,
328 .remove = gsensor_da215s_remove,
329 .shutdown = sensor_shutdown,
330 .id_table = gsensor_da215s_id,
331 .driver = {
332 .name = "gsensor_da215s",
333 #ifdef CONFIG_PM
334 .pm = &sensor_pm_ops,
335 #endif
336 },
337 };
338
339 module_i2c_driver(gsensor_da215s_driver);
340
341 MODULE_AUTHOR("Guo Wangqiang <kay.guo@rock-chips.com>");
342 MODULE_DESCRIPTION("da215s 3-Axis accelerometer driver");
343 MODULE_LICENSE("GPL");
344