1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * kernel/drivers/input/sensors/accel/mxc6655xa.c
4 *
5 * Copyright (C) 2020 Rockchip Co.,Ltd.
6 * Author: Wang Jie <dave.wang@rock-chips.com>
7 */
8
9 #include <linux/interrupt.h>
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/irq.h>
13 #include <linux/miscdevice.h>
14 #include <linux/gpio.h>
15 #include <linux/uaccess.h>
16 #include <linux/atomic.h>
17 #include <linux/delay.h>
18 #include <linux/input.h>
19 #include <linux/workqueue.h>
20 #include <linux/freezer.h>
21 #include <linux/of_gpio.h>
22 #ifdef CONFIG_HAS_EARLYSUSPEND
23 #include <linux/earlysuspend.h>
24 #endif
25 #include <linux/sensor-dev.h>
26
27 /* Linear acceleration register */
28 #define MXC6655_INT_SRC0 0x00
29 #define MXC6655_INT_CLR0 0x00
30 #define MXC6655_INT_SRC1 0x01
31 #define MXC6655_INT_CLR1 0x01
32 #define MXC6655_STATUS 0x02
33 #define MXC6655_OUT_X_H 0x03
34 #define MXC6655_OUT_X_L 0x04
35 #define MXC6655_OUT_Y_H 0x05
36 #define MXC6655_OUT_Y_L 0x06
37 #define MXC6655_OUT_Z_H 0x07
38 #define MXC6655_OUT_Z_L 0x08
39 #define MXC6655_OUT_TEMP 0x09
40 #define MXC6655_INT_MASK0 0x0A
41 #define MXC6655_INT_MASK1 0x0B
42 #define MXC6655_DETECTION 0x0C
43 #define MXC6655_CONTROL 0x0D
44 #define MXC6655_WHO_AM_I_A 0x0F
45 #define MXC6655_DEVICE_ID_A 0x05
46 #define MXC6655_POWER_DOWN BIT(0)
47 #define MXC6655_INT_ENABLE BIT(0)
48 #define MXC6655_RANGE (16384 * 2)
49 #define MXC6655_PRECISION 12
50 #define MXC6655_BOUNDARY (0x1 << (MXC6655_PRECISION - 1))
51 #define MXC6655_GRAVITY_STEP (MXC6655_RANGE / MXC6655_BOUNDARY)
52
sensor_active(struct i2c_client * client,int enable,int rate)53 static int sensor_active(struct i2c_client *client, int enable, int rate)
54 {
55 struct sensor_private_data *sensor =
56 (struct sensor_private_data *)i2c_get_clientdata(client);
57 int result = 0;
58
59 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
60 if (enable)
61 sensor->ops->ctrl_data &= ~MXC6655_POWER_DOWN;
62 else
63 sensor->ops->ctrl_data |= MXC6655_POWER_DOWN;
64
65 result = sensor_write_reg(client, sensor->ops->ctrl_reg,
66 sensor->ops->ctrl_data);
67 if (result)
68 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
69
70 pr_debug("%s:reg = 0x%x, reg_ctrl = 0x%x, enable= %d\n",
71 __func__,
72 sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
73
74 return result;
75 }
76
sensor_init(struct i2c_client * client)77 static int sensor_init(struct i2c_client *client)
78 {
79 struct sensor_private_data *sensor =
80 (struct sensor_private_data *)i2c_get_clientdata(client);
81 int status = 0;
82 int result = 0;
83
84 result = sensor->ops->active(client, 0, 0);
85 if (result) {
86 dev_err(&client->dev,
87 "%s:line=%d,error\n", __func__, __LINE__);
88 return result;
89 }
90 sensor->status_cur = SENSOR_OFF;
91
92 /* Operating mode control and full-scale range(2g) */
93 result = sensor_write_reg(client, sensor->ops->ctrl_reg, 0x01);
94 if (result) {
95 dev_err(&client->dev,
96 "%s:fail to set MXC6655_CONTROL.\n", __func__);
97 return result;
98 }
99
100 /* Enable or Disable for DRDY Interrupt */
101 status = sensor_read_reg(client, MXC6655_INT_MASK1);
102 if (sensor->pdata->irq_enable)
103 status |= MXC6655_INT_ENABLE;
104 else
105 status &= ~MXC6655_INT_ENABLE;
106 result = sensor_write_reg(client, MXC6655_INT_MASK1, status);
107 if (result) {
108 dev_err(&client->dev,
109 "%s:fail to set MXC6655_INT_MASK1.\n", __func__);
110 return result;
111 }
112
113 return result;
114 }
115
sensor_convert_data(struct i2c_client * client,char high_byte,char low_byte)116 static int sensor_convert_data(struct i2c_client *client,
117 char high_byte, char low_byte)
118 {
119 s64 result;
120
121 result = ((int)high_byte << (MXC6655_PRECISION - 8)) |
122 ((int)low_byte >> (16 - MXC6655_PRECISION));
123
124 if (result < MXC6655_BOUNDARY)
125 result = result * MXC6655_GRAVITY_STEP;
126 else
127 result = ~(((~result & (0x7fff >> (16 - MXC6655_PRECISION))) +
128 1) * MXC6655_GRAVITY_STEP) + 1;
129
130 return (int)result;
131 }
132
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)133 static int gsensor_report_value(struct i2c_client *client,
134 struct sensor_axis *axis)
135 {
136 struct sensor_private_data *sensor =
137 (struct sensor_private_data *)i2c_get_clientdata(client);
138
139 if (sensor->status_cur == SENSOR_ON) {
140 input_report_abs(sensor->input_dev, ABS_X, axis->x);
141 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
142 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
143 }
144
145 return 0;
146 }
147
sensor_report_value(struct i2c_client * client)148 static int sensor_report_value(struct i2c_client *client)
149 {
150 struct sensor_axis axis;
151 struct sensor_private_data *sensor =
152 (struct sensor_private_data *)i2c_get_clientdata(client);
153 struct sensor_platform_data *pdata = sensor->pdata;
154 char buffer[6] = {0};
155 char value = 0;
156 int x, y, z;
157 int ret = 0;
158
159 if (sensor->ops->read_len < 6) {
160 dev_err(&client->dev, "%s:Read len is error,len= %d\n",
161 __func__, sensor->ops->read_len);
162 return -1;
163 }
164
165 *buffer = sensor->ops->read_reg;
166 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
167 if (ret < 0) {
168 dev_err(&client->dev,
169 "mxc6655 read data failed, ret = %d\n", ret);
170 return ret;
171 }
172
173 /* x,y,z axis is the 12-bit acceleration output */
174 x = sensor_convert_data(sensor->client, buffer[0], buffer[1]);
175 y = sensor_convert_data(sensor->client, buffer[2], buffer[3]);
176 z = sensor_convert_data(sensor->client, buffer[4], buffer[5]);
177
178 pr_debug("%s: x = %d, y = %d, z = %d\n", __func__, x, y, z);
179
180 axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y +
181 (pdata->orientation[2]) * z;
182 axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y +
183 (pdata->orientation[5]) * z;
184 axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y +
185 (pdata->orientation[8]) * z;
186
187 gsensor_report_value(client, &axis);
188
189 mutex_lock(&sensor->data_mutex);
190 sensor->axis = axis;
191 mutex_unlock(&sensor->data_mutex);
192
193 if (sensor->pdata->irq_enable) {
194 value = sensor_read_reg(client, sensor->ops->int_status_reg);
195 if (value & 0x01) {
196 pr_debug("%s:gsensor int status :0x%x\n",
197 __func__, value);
198 ret = sensor_write_reg(client, MXC6655_INT_CLR1, 0x01);
199 if (ret) {
200 dev_err(&client->dev,
201 "%s:fail to clear MXC6655_INT_CLR1.\n",
202 __func__);
203 return ret;
204 }
205 }
206 }
207
208 return ret;
209 }
210
211 static struct sensor_operate gsensor_mxc6655_ops = {
212 .name = "gs_mxc6655xa",
213 .type = SENSOR_TYPE_ACCEL,
214 .id_i2c = ACCEL_ID_MXC6655XA,
215 .read_reg = MXC6655_OUT_X_H,
216 .read_len = 6,
217 .id_reg = MXC6655_WHO_AM_I_A,
218 .id_data = MXC6655_DEVICE_ID_A,
219 .precision = MXC6655_PRECISION,
220 .ctrl_reg = MXC6655_CONTROL,
221 .int_status_reg = MXC6655_INT_SRC1,
222 .range = {-MXC6655_RANGE, MXC6655_RANGE},
223 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
224 .active = sensor_active,
225 .init = sensor_init,
226 .report = sensor_report_value,
227 };
228
gsensor_mxc6655_probe(struct i2c_client * client,const struct i2c_device_id * devid)229 static int gsensor_mxc6655_probe(struct i2c_client *client,
230 const struct i2c_device_id *devid)
231 {
232 return sensor_register_device(client, NULL, devid, &gsensor_mxc6655_ops);
233 }
234
gsensor_mxc6655_remove(struct i2c_client * client)235 static int gsensor_mxc6655_remove(struct i2c_client *client)
236 {
237 return sensor_unregister_device(client, NULL, &gsensor_mxc6655_ops);
238 }
239
240 static const struct i2c_device_id gsensor_mxc6655_id[] = {
241 {"gs_mxc6655xa", ACCEL_ID_MXC6655XA},
242 {}
243 };
244
245 static struct i2c_driver gsensor_mxc6655_driver = {
246 .probe = gsensor_mxc6655_probe,
247 .remove = gsensor_mxc6655_remove,
248 .shutdown = sensor_shutdown,
249 .id_table = gsensor_mxc6655_id,
250 .driver = {
251 .name = "gsensor_mxc6655",
252 #ifdef CONFIG_PM
253 .pm = &sensor_pm_ops,
254 #endif
255 },
256 };
257
258 module_i2c_driver(gsensor_mxc6655_driver);
259
260 MODULE_AUTHOR("Wang Jie <dave.wang@rock-chips.com>");
261 MODULE_DESCRIPTION("mxc6655 3-Axis accelerometer driver");
262 MODULE_LICENSE("GPL");
263