1 /* drivers/input/sensors/access/mma7660.c
2 *
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@rock-chips.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/irq.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <linux/uaccess.h>
23 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
28 #include <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
31 #endif
32 #include <linux/sensor-dev.h>
33
34 #define MMA7660_ENABLE 1
35 #define MMA7660_REG_X_OUT 0x0
36 #define MMA7660_REG_Y_OUT 0x1
37 #define MMA7660_REG_Z_OUT 0x2
38 #define MMA7660_REG_TILT 0x3
39 #define MMA7660_REG_SRST 0x4
40 #define MMA7660_REG_SPCNT 0x5
41 #define MMA7660_REG_INTSU 0x6
42 #define MMA7660_REG_MODE 0x7
43 #define MMA7660_REG_SR 0x8
44 #define MMA7660_REG_PDET 0x9
45 #define MMA7660_REG_PD 0xa
46 #define MMA7660_PRECISION 6
47
48 /****************operate according to sensor chip:start************/
49
sensor_active(struct i2c_client * client,int enable,int rate)50 static int sensor_active(struct i2c_client *client, int enable, int rate)
51 {
52 struct sensor_private_data *sensor =
53 (struct sensor_private_data *)i2c_get_clientdata(client);
54 int result = 0;
55 int status = 0;
56
57 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
58
59 if (enable) {
60 status = MMA7660_ENABLE;
61 sensor->ops->ctrl_data |= status;
62 } else {
63 status = ~MMA7660_ENABLE;
64 sensor->ops->ctrl_data &= status;
65 }
66
67 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
68 if (result)
69 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
70
71 return result;
72 }
73
sensor_init(struct i2c_client * client)74 static int sensor_init(struct i2c_client *client)
75 {
76 struct sensor_private_data *sensor =
77 (struct sensor_private_data *)i2c_get_clientdata(client);
78 int result = 0;
79
80 result = sensor->ops->active(client, 0, 0);
81 if (result) {
82 dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
83 return result;
84 }
85
86 sensor->status_cur = SENSOR_OFF;
87
88 /*120 Samples/Second Active and Auto-Sleep Mode */
89 result = sensor_write_reg(client, MMA7660_REG_SR, 0x01 << 5);
90 if (result) {
91 dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
92 return result;
93 }
94
95 if (sensor->pdata->irq_enable) {
96 result = sensor_write_reg(client, MMA7660_REG_INTSU, 1 << 4);
97 if (result) {
98 dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
99 return result;
100 }
101 }
102
103 sensor->ops->ctrl_data = 1 << 6;
104 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
105 if (result) {
106 dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
107 return result;
108 }
109
110 return result;
111 }
112
sensor_convert_data(struct i2c_client * client,char high_byte,char low_byte)113 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
114 {
115 int result = (int)low_byte;
116
117 if (low_byte & 0x20)
118 result = ((~result & 0x1f) + 1) * (-768);
119 else
120 result = (result & 0x1f) * 768;
121
122 return result;
123 }
124
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)125 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
126 {
127 struct sensor_private_data *sensor =
128 (struct sensor_private_data *) i2c_get_clientdata(client);
129
130 if (sensor->status_cur == SENSOR_ON) {
131 /* Report acceleration sensor information */
132 input_report_abs(sensor->input_dev, ABS_X, axis->x);
133 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
134 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
135 input_sync(sensor->input_dev);
136 }
137
138 return 0;
139 }
140
sensor_report_value(struct i2c_client * client)141 static int sensor_report_value(struct i2c_client *client)
142 {
143 struct sensor_private_data *sensor =
144 (struct sensor_private_data *)i2c_get_clientdata(client);
145 struct sensor_platform_data *pdata = sensor->pdata;
146 int ret = 0;
147 int x, y, z;
148 struct sensor_axis axis;
149 char buffer[3] = {0};
150 char value = 0;
151 static int flag;
152
153 if (sensor->ops->read_len < 3) {
154 dev_err(&client->dev, "%s:lenth is error,len=%d\n", __func__, sensor->ops->read_len);
155 return -1;
156 }
157
158 memset(buffer, 0, 3);
159
160 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
161 do {
162 *buffer = sensor->ops->read_reg;
163 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
164 if (ret < 0)
165 return ret;
166 } while (0);
167
168 x = sensor_convert_data(sensor->client, 0, buffer[0]);
169 y = sensor_convert_data(sensor->client, 0, buffer[1]);
170 z = sensor_convert_data(sensor->client, 0, buffer[2]);
171
172 axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y + (pdata->orientation[2]) * z;
173 axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y + (pdata->orientation[5]) * z;
174 axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y + (pdata->orientation[8]) * z;
175
176 /*
177 *input dev will ignore report data if data value is the same with last_value,
178 *sample rate will not enough by this way, so just avoid this case
179 */
180 if ((sensor->axis.x == axis.x) && (sensor->axis.y == axis.y) && (sensor->axis.z == axis.z)) {
181 if (flag) {
182 flag = 0;
183 axis.x += 1;
184 axis.y += 1;
185 axis.z += 1;
186 } else {
187 flag = 1;
188 axis.x -= 1;
189 axis.y -= 1;
190 axis.z -= 1;
191 }
192 }
193
194 gsensor_report_value(client, &axis);
195
196 mutex_lock(&sensor->data_mutex);
197 sensor->axis = axis;
198 mutex_unlock(&sensor->data_mutex);
199
200 if (sensor->pdata->irq_enable && sensor->ops->int_status_reg >= 0)
201 value = sensor_read_reg(client, sensor->ops->int_status_reg);
202
203 return ret;
204 }
205
206 static struct sensor_operate gsensor_mma7660_ops = {
207 .name = "mma7660",
208 .type = SENSOR_TYPE_ACCEL,
209 .id_i2c = ACCEL_ID_MMA7660,
210 .read_reg = MMA7660_REG_X_OUT,
211 .read_len = 3,
212 .id_reg = SENSOR_UNKNOW_DATA,
213 .id_data = SENSOR_UNKNOW_DATA,
214 .precision = MMA7660_PRECISION,
215 .ctrl_reg = MMA7660_REG_MODE,
216 .int_status_reg = SENSOR_UNKNOW_DATA,
217 .range = {-24576, 24576},
218 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
219 .active = sensor_active,
220 .init = sensor_init,
221 .report = sensor_report_value,
222 };
223
224 /****************operate according to sensor chip:end************/
gsensor_mma7660_probe(struct i2c_client * client,const struct i2c_device_id * devid)225 static int gsensor_mma7660_probe(struct i2c_client *client,
226 const struct i2c_device_id *devid)
227 {
228 return sensor_register_device(client, NULL, devid, &gsensor_mma7660_ops);
229 }
230
gsensor_mma7660_remove(struct i2c_client * client)231 static int gsensor_mma7660_remove(struct i2c_client *client)
232 {
233 return sensor_unregister_device(client, NULL, &gsensor_mma7660_ops);
234 }
235
236 static const struct i2c_device_id gsensor_mma7660_id[] = {
237 {"gs_mma7660", ACCEL_ID_MMA7660},
238 {}
239 };
240
241 static struct i2c_driver gsensor_mma7660_driver = {
242 .probe = gsensor_mma7660_probe,
243 .remove = gsensor_mma7660_remove,
244 .shutdown = sensor_shutdown,
245 .id_table = gsensor_mma7660_id,
246 .driver = {
247 .name = "gsensor_mma7660",
248 #ifdef CONFIG_PM
249 .pm = &sensor_pm_ops,
250 #endif
251 },
252 };
253
254 module_i2c_driver(gsensor_mma7660_driver);
255
256 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
257 MODULE_DESCRIPTION("mma7660 3-Axis accelerometer driver");
258 MODULE_LICENSE("GPL");
259