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