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
// SPDX-License-Identifier: GPL-2.0
/*
 * kernel/drivers/input/sensors/accel/mxc6655xa.c
 *
 * Copyright (C) 2020 Rockchip Co.,Ltd.
 * Author: Wang Jie <dave.wang@rock-chips.com>
 */
 
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/uaccess.h>
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/freezer.h>
#include <linux/of_gpio.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#include <linux/sensor-dev.h>
 
/* Linear acceleration  register */
#define MXC6655_INT_SRC0    0x00
#define MXC6655_INT_CLR0    0x00
#define MXC6655_INT_SRC1    0x01
#define MXC6655_INT_CLR1    0x01
#define MXC6655_STATUS        0x02
#define    MXC6655_OUT_X_H        0x03
#define    MXC6655_OUT_X_L        0x04
#define    MXC6655_OUT_Y_H        0x05
#define    MXC6655_OUT_Y_L        0x06
#define    MXC6655_OUT_Z_H        0x07
#define    MXC6655_OUT_Z_L        0x08
#define MXC6655_OUT_TEMP    0x09
#define MXC6655_INT_MASK0    0x0A
#define MXC6655_INT_MASK1    0x0B
#define MXC6655_DETECTION    0x0C
#define MXC6655_CONTROL        0x0D
#define    MXC6655_WHO_AM_I_A    0x0F
#define    MXC6655_DEVICE_ID_A    0x05
#define MXC6655_POWER_DOWN    BIT(0)
#define MXC6655_INT_ENABLE    BIT(0)
#define MXC6655_RANGE        (16384 * 2)
#define MXC6655_PRECISION    12
#define MXC6655_BOUNDARY    (0x1 << (MXC6655_PRECISION - 1))
#define MXC6655_GRAVITY_STEP    (MXC6655_RANGE / MXC6655_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 &= ~MXC6655_POWER_DOWN;
   else
       sensor->ops->ctrl_data |= MXC6655_POWER_DOWN;
 
   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__);
 
   pr_debug("%s:reg = 0x%x, reg_ctrl = 0x%x, enable= %d\n",
        __func__,
        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;
 
   /*  Operating mode control and full-scale range(2g) */
   result = sensor_write_reg(client, sensor->ops->ctrl_reg, 0x01);
   if (result) {
       dev_err(&client->dev,
           "%s:fail to set MXC6655_CONTROL.\n", __func__);
       return result;
   }
 
   /* Enable or Disable for DRDY Interrupt */
   status = sensor_read_reg(client, MXC6655_INT_MASK1);
   if (sensor->pdata->irq_enable)
       status |= MXC6655_INT_ENABLE;
   else
       status &= ~MXC6655_INT_ENABLE;
   result = sensor_write_reg(client, MXC6655_INT_MASK1, status);
   if (result) {
       dev_err(&client->dev,
           "%s:fail to set MXC6655_INT_MASK1.\n", __func__);
       return result;
   }
 
   return result;
}
 
static int sensor_convert_data(struct i2c_client *client,
                  char high_byte, char low_byte)
{
   s64 result;
 
   result = ((int)high_byte << (MXC6655_PRECISION - 8)) |
        ((int)low_byte >> (16 - MXC6655_PRECISION));
 
   if (result < MXC6655_BOUNDARY)
       result = result * MXC6655_GRAVITY_STEP;
   else
       result = ~(((~result & (0x7fff >> (16 - MXC6655_PRECISION))) +
             1) * MXC6655_GRAVITY_STEP) + 1;
 
   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 (sensor->status_cur == SENSOR_ON) {
       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);
   }
 
   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;
   char buffer[6] = {0};
   char value = 0;
   int x, y, z;
   int ret = 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 -1;
   }
 
   *buffer = sensor->ops->read_reg;
   ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
   if (ret < 0) {
       dev_err(&client->dev,
           "mxc6655 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]);
 
   pr_debug("%s: x = %d, y = %d, z = %d\n", __func__, x, y, z);
 
   axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y +
        (pdata->orientation[2]) * z;
   axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y +
        (pdata->orientation[5]) * z;
   axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y +
        (pdata->orientation[8]) * z;
 
   gsensor_report_value(client, &axis);
 
   mutex_lock(&sensor->data_mutex);
   sensor->axis = axis;
   mutex_unlock(&sensor->data_mutex);
 
   if (sensor->pdata->irq_enable) {
       value = sensor_read_reg(client, sensor->ops->int_status_reg);
       if (value & 0x01) {
           pr_debug("%s:gsensor int status :0x%x\n",
                __func__, value);
           ret = sensor_write_reg(client, MXC6655_INT_CLR1, 0x01);
           if (ret) {
               dev_err(&client->dev,
                   "%s:fail to clear MXC6655_INT_CLR1.\n",
                   __func__);
               return ret;
           }
       }
   }
 
   return ret;
}
 
static struct sensor_operate gsensor_mxc6655_ops = {
   .name        = "gs_mxc6655xa",
   .type        = SENSOR_TYPE_ACCEL,
   .id_i2c        = ACCEL_ID_MXC6655XA,
   .read_reg    = MXC6655_OUT_X_H,
   .read_len    = 6,
   .id_reg        = MXC6655_WHO_AM_I_A,
   .id_data    = MXC6655_DEVICE_ID_A,
   .precision    = MXC6655_PRECISION,
   .ctrl_reg    = MXC6655_CONTROL,
   .int_status_reg    = MXC6655_INT_SRC1,
   .range        = {-MXC6655_RANGE, MXC6655_RANGE},
   .trig        = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
   .active        = sensor_active,
   .init        = sensor_init,
   .report        = sensor_report_value,
};
 
static int gsensor_mxc6655_probe(struct i2c_client *client,
               const struct i2c_device_id *devid)
{
   return sensor_register_device(client, NULL, devid, &gsensor_mxc6655_ops);
}
 
static int gsensor_mxc6655_remove(struct i2c_client *client)
{
   return sensor_unregister_device(client, NULL, &gsensor_mxc6655_ops);
}
 
static const struct i2c_device_id gsensor_mxc6655_id[] = {
   {"gs_mxc6655xa", ACCEL_ID_MXC6655XA},
   {}
};
 
static struct i2c_driver gsensor_mxc6655_driver = {
   .probe = gsensor_mxc6655_probe,
   .remove = gsensor_mxc6655_remove,
   .shutdown = sensor_shutdown,
   .id_table = gsensor_mxc6655_id,
   .driver = {
       .name = "gsensor_mxc6655",
   #ifdef CONFIG_PM
       .pm = &sensor_pm_ops,
   #endif
   },
};
 
module_i2c_driver(gsensor_mxc6655_driver);
 
MODULE_AUTHOR("Wang Jie <dave.wang@rock-chips.com>");
MODULE_DESCRIPTION("mxc6655 3-Axis accelerometer driver");
MODULE_LICENSE("GPL");