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
8 #include <linux/atomic.h>
9 #include <linux/delay.h>
10 #ifdef CONFIG_HAS_EARLYSUSPEND
11 #include <linux/earlysuspend.h>
12 #endif
13 #include <linux/freezer.h>
14 #include <linux/gpio.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/miscdevice.h>
20 #include <linux/of_gpio.h>
21 #include <linux/sensor-dev.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/workqueue.h>
25 #include "da228e_core.h"
26
27 /* Linear acceleration register */
28 #define DA228E_CONFIG 0X00
29 #define DA228E_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 DA228E_RANGE 0x0F
40 #define ODR_AXIS 0x10
41 #define DA228E_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 DA228E_CHIPID_DATA 0x13
53 #define DA228E_CTRL_NORMAL 0x34
54 #define DA228E_CTRL_SUSPEND 0x80
55 #define INT_ACTIVE_ENABLE 0x87
56 #define INT_NEW_DATA_ENABLE 0x10
57
58 #define DA228E_OFFSET_MAX 200
59 #define DA228E_OFFSET_CUS 130
60 #define DA228E_OFFSET_SEN 1024
61
62 #define GSENSOR_MIN 2
63 #define DA228E_PRECISION 12
64 #define DA228E_DATA_RANGE (16384 * 4)
65 #define DA228E_BOUNDARY (0x1 << (DA228E_PRECISION - 1))
66 #define DA228E_GRAVITY_STEP (DA228E_DATA_RANGE/DA228E_BOUNDARY)
67
68 /******************************************************************************/
69
sensor_active(struct i2c_client * client,int enable,int rate)70 static int sensor_active(struct i2c_client *client, int enable, int rate)
71 {
72 struct sensor_private_data *sensor =
73 (struct sensor_private_data *)i2c_get_clientdata(client);
74 int result = 0;
75
76 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
77 if (enable)
78 sensor->ops->ctrl_data &= DA228E_CTRL_NORMAL;
79 else
80 sensor->ops->ctrl_data |= DA228E_CTRL_SUSPEND;
81
82 result = sensor_write_reg(client, sensor->ops->ctrl_reg,
83 sensor->ops->ctrl_data);
84 if (result)
85 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
86
87 dev_dbg(&client->dev, "%s:reg = 0x%x, reg_ctrl = 0x%x, enable= %d\n",
88 __func__,
89 sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
90
91 return result;
92 }
93
sensor_init(struct i2c_client * client)94 static int sensor_init(struct i2c_client *client)
95 {
96 struct sensor_private_data *sensor =
97 (struct sensor_private_data *)i2c_get_clientdata(client);
98 int status = 0;
99 int result = 0;
100
101 result = sensor->ops->active(client, 0, 0);
102 if (result) {
103 dev_err(&client->dev,
104 "%s:line=%d,error\n", __func__, __LINE__);
105 return result;
106 }
107 sensor->status_cur = SENSOR_OFF;
108
109 result = sensor_write_reg(client, DA228E_CONFIG, 0x24);
110 mdelay(25);
111 /*+/-4G,12bit normal mode ODR = 62.5hz*/
112 result |= sensor_write_reg(client, DA228E_RANGE, 0x61);
113 result |= sensor_write_reg(client, DA228E_MODE_BW, 0x34);
114 result |= sensor_write_reg(client, ODR_AXIS, 0x06);
115 if (result) {
116 dev_err(&client->dev, "%s:fail to config DA228E_accel.\n",
117 __func__);
118 return result;
119 }
120
121 /* Enable or Disable for active Interrupt */
122 status = sensor_read_reg(client, INT_ACTIVE_SET1);
123 if (sensor->pdata->irq_enable)
124 status |= INT_ACTIVE_ENABLE;
125 else
126 status &= ~INT_ACTIVE_ENABLE;
127 result = sensor_write_reg(client, INT_ACTIVE_SET1, status);
128 if (result) {
129 dev_err(&client->dev,
130 "%s:fail to set DA228E_INT_ACTIVE.\n", __func__);
131 return result;
132 }
133
134 /* Enable or Disable for new data Interrupt */
135 status = sensor_read_reg(client, INT_DATA_SET2);
136 if (sensor->pdata->irq_enable)
137 status |= INT_NEW_DATA_ENABLE;
138 else
139 status &= ~INT_NEW_DATA_ENABLE;
140 result = sensor_write_reg(client, INT_DATA_SET2, status);
141 if (result) {
142 dev_err(&client->dev,
143 "%s:fail to set DA228E_INT_NEW_DATA.\n", __func__);
144 return result;
145 }
146
147 return result;
148 }
149
sensor_convert_data(struct i2c_client * client,unsigned char low_byte4,unsigned char high_byte8)150 static int sensor_convert_data(struct i2c_client *client,
151 unsigned char low_byte4, unsigned char high_byte8)
152 {
153 s64 result;
154
155 result = ((short)((high_byte8 << 8)|low_byte4)) >> 4;
156
157 return (int)result;
158 }
159
160
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)161 static int gsensor_report_value(struct i2c_client *client,
162 struct sensor_axis *axis)
163 {
164 struct sensor_private_data *sensor =
165 (struct sensor_private_data *)i2c_get_clientdata(client);
166 if ((abs(sensor->axis.x - axis->x) > GSENSOR_MIN) ||
167 (abs(sensor->axis.y - axis->y) > GSENSOR_MIN) ||
168 (abs(sensor->axis.z - axis->z) > GSENSOR_MIN)) {
169 input_report_abs(sensor->input_dev, ABS_X, axis->x);
170 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
171 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
172 input_sync(sensor->input_dev);
173 }
174
175 return 0;
176 }
177
sensor_report_value(struct i2c_client * client)178 static int sensor_report_value(struct i2c_client *client)
179 {
180 struct sensor_axis axis;
181 struct sensor_private_data *sensor =
182 (struct sensor_private_data *)i2c_get_clientdata(client);
183 struct sensor_platform_data *pdata = sensor->pdata;
184 unsigned char buffer[6] = {0};
185 int x, y, z;
186 int ret = 0;
187 int tmp_x = 0, tmp_y = 0, tmp_z = 0;
188
189 if (sensor->ops->read_len < 6) {
190 dev_err(&client->dev, "%s:Read len is error,len= %d\n",
191 __func__, sensor->ops->read_len);
192 return -EINVAL;
193 }
194
195 *buffer = sensor->ops->read_reg;
196 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
197 if (ret < 0) {
198 dev_err(&client->dev,
199 "da228e read data failed, ret = %d\n", ret);
200 return ret;
201 }
202
203 /* x,y,z axis is the 12-bit acceleration output */
204 x = sensor_convert_data(sensor->client, buffer[0], buffer[1]);
205 y = sensor_convert_data(sensor->client, buffer[2], buffer[3]);
206 z = sensor_convert_data(sensor->client, buffer[4], buffer[5]);
207
208 da228e_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 * DA228E_GRAVITY_STEP;
213 tmp_y = y * DA228E_GRAVITY_STEP;
214 tmp_z = z * DA228E_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, "%s:<map:>axis=%d, %d, %d\n", __func__,
225 axis.x, axis.y, axis.z);
226
227 gsensor_report_value(client, &axis);
228
229 mutex_lock(&(sensor->data_mutex));
230 sensor->axis = axis;
231 mutex_unlock(&(sensor->data_mutex));
232
233 if (sensor->pdata->irq_enable) {
234 ret = sensor_write_reg(client, INT_MAP1, 0);
235 if (ret) {
236 dev_err(&client->dev,
237 "%s:fail to clear DA228E_INT_register.\n",
238 __func__);
239 return ret;
240 }
241 ret = sensor_write_reg(client, INT_MAP2, 0);
242 if (ret) {
243 dev_err(&client->dev,
244 "%s:fail to clear DA228E_INT_register.\n",
245 __func__);
246 return ret;
247 }
248 }
249
250 return ret;
251 }
252
253 /******************************************************************************/
sensor_suspend(struct i2c_client * client)254 static int sensor_suspend(struct i2c_client *client)
255 {
256 int result = 0;
257
258 // MI_FUN;
259 // result = mir3da_set_enable(client, false);
260 // if (result) {
261 // MI_ERR("sensor_suspend disable fail!!\n");
262 // return result;
263 // }
264
265 return result;
266 }
267
268 /******************************************************************************/
sensor_resume(struct i2c_client * client)269 static int sensor_resume(struct i2c_client *client)
270 {
271 int result = 0;
272
273 // MI_FUN;
274
275 /*
276 * result = mir3da_chip_resume(client);
277 * if(result) {
278 * MI_ERR("sensor_resume chip resume fail!!\n");
279 * return result;
280 * }
281 */
282 // result = mir3da_set_enable(client, true);
283 // if (result) {
284 // MI_ERR("sensor_resume enable fail!!\n");
285 // return result;
286 // }
287
288 return result;
289 }
290
291 static struct sensor_operate gsensor_da228e_ops = {
292 .name = "gs_da228e",
293 .type = SENSOR_TYPE_ACCEL,
294 .id_i2c = ACCEL_ID_DA228E,
295 .read_reg = ACC_X_LSB,
296 .read_len = 6,
297 .id_reg = DA228E_CHIP_ID,
298 .id_data = DA228E_CHIPID_DATA,
299 .precision = DA228E_PRECISION,
300 .ctrl_reg = DA228E_MODE_BW,
301 .int_status_reg = INT_MAP1,
302 .range = {-DA228E_DATA_RANGE, DA228E_DATA_RANGE},
303 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
304 .active = sensor_active,
305 .init = sensor_init,
306 .report = sensor_report_value,
307 .suspend = sensor_suspend,
308 .resume = sensor_resume,
309 };
310
gsensor_da228e_probe(struct i2c_client * client,const struct i2c_device_id * devid)311 static int gsensor_da228e_probe(struct i2c_client *client,
312 const struct i2c_device_id *devid)
313 {
314 return sensor_register_device(client, NULL, devid, &gsensor_da228e_ops);
315 }
316
gsensor_da228e_remove(struct i2c_client * client)317 static int gsensor_da228e_remove(struct i2c_client *client)
318 {
319 return sensor_unregister_device(client, NULL, &gsensor_da228e_ops);
320 }
321
322 static const struct i2c_device_id gsensor_da228e_id[] = {
323 {"gs_da228e", ACCEL_ID_DA228E},
324 {}
325 };
326
327 static struct i2c_driver gsensor_da228e_driver = {
328 .probe = gsensor_da228e_probe,
329 .remove = gsensor_da228e_remove,
330 .shutdown = sensor_shutdown,
331 .id_table = gsensor_da228e_id,
332 .driver = {
333 .name = "gsensor_da228e",
334 #ifdef CONFIG_PM
335 .pm = &sensor_pm_ops,
336 #endif
337 },
338 };
339
340 module_i2c_driver(gsensor_da228e_driver);
341
342 MODULE_AUTHOR("Guo Wangqiang <kay.guo@rock-chips.com>");
343 MODULE_DESCRIPTION("da228e 3-Axis accelerometer driver");
344 MODULE_LICENSE("GPL");
345