2 * kernel/drivers/input/sensors/accel/lsm330_acc.c
4 * Copyright (C) 2012-2016 Rockchip Co.,Ltd.
5 * Author: Bin Yang <yangbin@rock-chips.com>
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.
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.
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>
34 #include <linux/sensor-dev.h>
36 /* Linear acceleration register */
37 #define WHO_AM_I_A 0x0F
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
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
85 #define LSM330_DEVICE_ID_A 0x40
87 static int sensor_active(struct i2c_client *client, int enable, int rate)
89 struct sensor_private_data *sensor =
90 (struct sensor_private_data *)i2c_get_clientdata(client);
93 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
95 result = sensor_write_reg(client,
96 sensor->ops->ctrl_reg,
97 sensor->ops->ctrl_data | 0x07);
99 result = sensor_write_reg(client, sensor->ops->ctrl_reg, 0x00);
102 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
104 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",
106 sensor->ops->ctrl_reg,
107 sensor->ops->ctrl_data, enable);
112 static int sensor_init(struct i2c_client *client)
114 struct sensor_private_data *sensor =
115 (struct sensor_private_data *)i2c_get_clientdata(client);
118 result = sensor->ops->active(client, 0, 0);
120 dev_err(&client->dev, "%s:line=%d,error\n",
124 sensor->status_cur = SENSOR_OFF;
126 result = sensor_write_reg(client, CTRL_REG4_A, 0xE8);
128 dev_err(&client->dev, "%s:fail to set CTRL_REG4_A.\n",
132 /* Normal / Low power mode (1600 Hz) */
133 result = sensor_write_reg(client, CTRL_REG5_A, 0x90);
135 dev_err(&client->dev, "%s:fail to set CTRL_REG5_A.\n",
139 result = sensor_write_reg(client, CTRL_REG6_A, 0x00);
141 dev_err(&client->dev, "%s:fail to set CTRL_REG6_A.\n",
145 result = sensor_write_reg(client, CTRL_REG7_A, 0x00);
147 dev_err(&client->dev, "%s:fail to set CTRL_REG7_A.\n",
155 static int gsensor_report_value(struct i2c_client *client,
156 struct sensor_axis *axis)
158 struct sensor_private_data *sensor =
159 (struct sensor_private_data *)i2c_get_clientdata(client);
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);
170 #define GSENSOR_MIN 10
172 static int sensor_report_value(struct i2c_client *client)
174 struct sensor_private_data *sensor =
175 (struct sensor_private_data *)i2c_get_clientdata(client);
176 struct sensor_platform_data *pdata = sensor->pdata;
180 struct sensor_axis axis;
182 unsigned char reg_buf = 0;
185 if (sensor->ops->read_len < 6) {
186 dev_err(&client->dev, "%s:Read len is error,len=%d\n",
188 sensor->ops->read_len);
191 memset(buffer, 0, 6);
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);
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);
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;
213 axis.x = 61 * axis.x;
214 axis.y = 61 * axis.y;
215 axis.z = 61 * axis.z;
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);
223 mutex_unlock(&sensor->data_mutex);
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);
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,
240 .id_reg = WHO_AM_I_A,
241 .id_data = LSM330_DEVICE_ID_A,
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,
249 .report = sensor_report_value,
252 static struct sensor_operate *gsensor_get_ops(void)
254 return &gsensor_lsm330_ops;
257 static int __init gsensor_lsm330_init(void)
259 struct sensor_operate *ops = gsensor_get_ops();
261 int type = ops->type;
263 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
268 static void __exit gsensor_lsm330_exit(void)
270 struct sensor_operate *ops = gsensor_get_ops();
271 int type = ops->type;
273 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
276 module_init(gsensor_lsm330_init);
277 module_exit(gsensor_lsm330_exit);