ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / accel / lsm330_acc.c
1 /*
2  * kernel/drivers/input/sensors/accel/lsm330_acc.c
3  *
4  * Copyright (C) 2012-2016 Rockchip Co.,Ltd.
5  * Author: Bin Yang <yangbin@rock-chips.com>
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <linux/irq.h>
22 #include <linux/miscdevice.h>
23 #include <linux/gpio.h>
24 #include <linux/uaccess.h>
25 #include <linux/atomic.h>
26 #include <linux/delay.h>
27 #include <linux/input.h>
28 #include <linux/workqueue.h>
29 #include <linux/freezer.h>
30 #include <linux/of_gpio.h>
31 #ifdef CONFIG_HAS_EARLYSUSPEND
32 #include <linux/earlysuspend.h>
33 #endif
34 #include <linux/sensor-dev.h>
35
36 /* Linear acceleration  register */
37 #define WHO_AM_I_A      0x0F
38 #define OFF_X   0x10
39 #define OFF_Y   0x11
40 #define OFF_Z   0x12
41 #define CS_X    0x13
42 #define CS_Y    0x14
43 #define CS_Z    0x15
44 #define LC_L    0x16
45 #define LC_H    0x17
46 #define STAT    0x18
47 #define VFC_1   0x1B
48 #define VFC_2   0x1C
49 #define VFC_3   0x1D
50 #define VFC_4   0x1E
51 #define THRS3   0x1F
52 #define CTRL_REG2_A     0x21
53 #define CTRL_REG3_A     0x22
54 #define CTRL_REG4_A     0x23
55 #define CTRL_REG5_A     0x20
56 #define CTRL_REG6_A     0x24
57 #define CTRL_REG7_A     0x25
58 #define STATUS_REG_A    0x27
59 #define OUT_X_L_A       0x28
60 #define OUT_X_H_A       0x29
61 #define OUT_Y_L_A       0x2A
62 #define OUT_Y_H_A       0x2B
63 #define OUT_Z_L_A       0x2C
64 #define OUT_Z_H_A       0x2D
65 #define FIFO_CTRL_REG   0x2E
66 #define FIFO_SRC_REG    0x2F
67
68 #define INT1_CFG_A      0x30
69 #define INT1_SOURCE_A   0x31
70 #define INT1_THS_A      0x32
71 #define INT1_DURATION_A 0x33
72 #define INT2_CFG_A      0x34
73 #define INT2_SOURCE_A   0x35
74 #define INT2_THS_A      0x36
75 #define INT2_DURATION_A 0x37
76 #define CLICK_CFG_A     0x38
77 #define CLICK_SRC_A     0x39
78 #define CLICK_THS_A     0x3A
79 #define TIME_LIMIT_A    0x3B
80 #define TIME_LATENCY_A  0x3C
81 #define TIME_WINDOW_A   0x3D
82 #define ACT_THS 0x3E
83 #define ACT_DUR 0x3F
84
85 #define LSM330_DEVICE_ID_A      0x40
86
87 static int sensor_active(struct i2c_client *client, int enable, int rate)
88 {
89         struct sensor_private_data *sensor =
90             (struct sensor_private_data *)i2c_get_clientdata(client);
91         int result = 0;
92
93         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
94         if (enable)
95                 result = sensor_write_reg(client,
96                         sensor->ops->ctrl_reg,
97                         sensor->ops->ctrl_data | 0x07);
98         else
99                 result = sensor_write_reg(client, sensor->ops->ctrl_reg, 0x00);
100
101         if (result)
102                 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
103
104         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",
105                 __func__,
106                 sensor->ops->ctrl_reg,
107                 sensor->ops->ctrl_data, enable);
108
109         return result;
110 }
111
112 static int sensor_init(struct i2c_client *client)
113 {
114         struct sensor_private_data *sensor =
115             (struct sensor_private_data *)i2c_get_clientdata(client);
116         int result = 0;
117
118         result = sensor->ops->active(client, 0, 0);
119         if (result) {
120                 dev_err(&client->dev, "%s:line=%d,error\n",
121                         __func__, __LINE__);
122                 return result;
123         }
124         sensor->status_cur = SENSOR_OFF;
125
126         result = sensor_write_reg(client, CTRL_REG4_A, 0xE8);
127         if (result) {
128                 dev_err(&client->dev, "%s:fail to set CTRL_REG4_A.\n",
129                         __func__);
130                 return result;
131         }
132         /* Normal / Low power mode (1600 Hz) */
133         result = sensor_write_reg(client, CTRL_REG5_A, 0x90);
134         if (result) {
135                 dev_err(&client->dev, "%s:fail to set CTRL_REG5_A.\n",
136                         __func__);
137                 return result;
138         }
139         result = sensor_write_reg(client, CTRL_REG6_A, 0x00);
140         if (result) {
141                 dev_err(&client->dev, "%s:fail to set CTRL_REG6_A.\n",
142                         __func__);
143                 return result;
144         }
145         result = sensor_write_reg(client, CTRL_REG7_A, 0x00);
146         if (result) {
147                 dev_err(&client->dev, "%s:fail to set CTRL_REG7_A.\n",
148                         __func__);
149                 return result;
150         }
151
152         return result;
153 }
154
155 static int gsensor_report_value(struct i2c_client *client,
156                                 struct sensor_axis *axis)
157 {
158         struct sensor_private_data *sensor =
159                 (struct sensor_private_data *)i2c_get_clientdata(client);
160
161         input_report_abs(sensor->input_dev, ABS_X, axis->x);
162         input_report_abs(sensor->input_dev, ABS_Y, axis->y);
163         input_report_abs(sensor->input_dev, ABS_Z, axis->z);
164         input_sync(sensor->input_dev);
165         DBG("Gsensor x==%d  y==%d z==%d\n", axis->x, axis->y, axis->z);
166
167         return 0;
168 }
169
170 #define GSENSOR_MIN 10
171
172 static int sensor_report_value(struct i2c_client *client)
173 {
174         struct sensor_private_data *sensor =
175                 (struct sensor_private_data *)i2c_get_clientdata(client);
176         struct sensor_platform_data *pdata = sensor->pdata;
177         char buffer[6];
178         int ret = 0;
179         char value = 0;
180         struct sensor_axis axis;
181         short x, y, z;
182         unsigned char reg_buf = 0;
183         unsigned char i = 0;
184
185         if (sensor->ops->read_len < 6) {
186                 dev_err(&client->dev, "%s:Read len is error,len=%d\n",
187                         __func__,
188                         sensor->ops->read_len);
189                 return -1;
190         }
191         memset(buffer, 0, 6);
192
193         reg_buf = sensor->ops->read_reg;
194         for (i = 0; i < sensor->ops->read_len; i++) {
195                 buffer[i] = sensor_read_reg(client, reg_buf);
196                 reg_buf++;
197         }
198
199         x = ((buffer[1] << 8) & 0xFF00) + (buffer[0] & 0xFF);
200         y = ((buffer[3] << 8) & 0xFF00) + (buffer[2] & 0xFF);
201         z = ((buffer[5] << 8) & 0xFF00) + (buffer[4] & 0xFF);
202
203         axis.x = (pdata->orientation[0]) * x +
204                 (pdata->orientation[1]) * y +
205                 (pdata->orientation[2]) * z;
206         axis.y = (pdata->orientation[3]) * x +
207                 (pdata->orientation[4]) * y +
208                 (pdata->orientation[5]) * z;
209         axis.z = (pdata->orientation[6]) * x +
210                 (pdata->orientation[7]) * y +
211                 (pdata->orientation[8]) * z;
212
213         axis.x = 61 * axis.x;
214         axis.y = 61 * axis.y;
215         axis.z = 61 * axis.z;
216
217         if ((abs(sensor->axis.x - axis.x) > GSENSOR_MIN) ||
218             (abs(sensor->axis.y - axis.y) > GSENSOR_MIN) ||
219             (abs(sensor->axis.z - axis.z) > GSENSOR_MIN)) {
220                 gsensor_report_value(client, &axis);
221                 mutex_lock(&sensor->data_mutex);
222                 sensor->axis = axis;
223                 mutex_unlock(&sensor->data_mutex);
224         }
225
226         if (sensor->pdata->irq_enable) {
227                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
228                 DBG("%s:gsensor int status :0x%x\n", __func__, value);
229         }
230
231         return ret;
232 }
233
234 struct sensor_operate gsensor_lsm330_ops = {
235         .name                   = "lsm330_acc",
236         .type                   = SENSOR_TYPE_ACCEL,
237         .id_i2c                 = ACCEL_ID_LSM330,
238         .read_reg               = OUT_X_L_A,
239         .read_len               = 6,
240         .id_reg                 = WHO_AM_I_A,
241         .id_data                = LSM330_DEVICE_ID_A,
242         .precision              = 16,
243         .ctrl_reg               = CTRL_REG5_A,
244         .int_status_reg = STATUS_REG_A,
245         .range                  = {-0xffff, 0xffff},
246         .trig                   = IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
247         .active                 = sensor_active,
248         .init                   = sensor_init,
249         .report                 = sensor_report_value,
250 };
251
252 static struct sensor_operate *gsensor_get_ops(void)
253 {
254         return &gsensor_lsm330_ops;
255 }
256
257 static int __init gsensor_lsm330_init(void)
258 {
259         struct sensor_operate *ops = gsensor_get_ops();
260         int result = 0;
261         int type = ops->type;
262
263         result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
264
265         return result;
266 }
267
268 static void __exit gsensor_lsm330_exit(void)
269 {
270         struct sensor_operate *ops = gsensor_get_ops();
271         int type = ops->type;
272
273         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
274 }
275
276 module_init(gsensor_lsm330_init);
277 module_exit(gsensor_lsm330_exit);