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