xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/accel/lsm330_acc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 
sensor_active(struct i2c_client * client,int enable,int rate)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 
sensor_init(struct i2c_client * client)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 
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)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 	if (sensor->status_cur == SENSOR_ON) {
162 		input_report_abs(sensor->input_dev, ABS_X, axis->x);
163 		input_report_abs(sensor->input_dev, ABS_Y, axis->y);
164 		input_report_abs(sensor->input_dev, ABS_Z, axis->z);
165 		input_sync(sensor->input_dev);
166 	}
167 
168 	return 0;
169 }
170 
171 #define GSENSOR_MIN 10
172 
sensor_report_value(struct i2c_client * client)173 static int sensor_report_value(struct i2c_client *client)
174 {
175 	struct sensor_private_data *sensor =
176 		(struct sensor_private_data *)i2c_get_clientdata(client);
177 	struct sensor_platform_data *pdata = sensor->pdata;
178 	char buffer[6];
179 	int ret = 0;
180 	char value = 0;
181 	struct sensor_axis axis;
182 	short x, y, z;
183 	unsigned char reg_buf = 0;
184 	unsigned char i = 0;
185 
186 	if (sensor->ops->read_len < 6) {
187 		dev_err(&client->dev, "%s:Read len is error,len=%d\n",
188 			__func__,
189 			sensor->ops->read_len);
190 		return -1;
191 	}
192 	memset(buffer, 0, 6);
193 
194 	reg_buf = sensor->ops->read_reg;
195 	for (i = 0; i < sensor->ops->read_len; i++) {
196 		buffer[i] = sensor_read_reg(client, reg_buf);
197 		reg_buf++;
198 	}
199 
200 	x = ((buffer[1] << 8) & 0xFF00) + (buffer[0] & 0xFF);
201 	y = ((buffer[3] << 8) & 0xFF00) + (buffer[2] & 0xFF);
202 	z = ((buffer[5] << 8) & 0xFF00) + (buffer[4] & 0xFF);
203 
204 	axis.x = (pdata->orientation[0]) * x +
205 		(pdata->orientation[1]) * y +
206 		(pdata->orientation[2]) * z;
207 	axis.y = (pdata->orientation[3]) * x +
208 		(pdata->orientation[4]) * y +
209 		(pdata->orientation[5]) * z;
210 	axis.z = (pdata->orientation[6]) * x +
211 		(pdata->orientation[7]) * y +
212 		(pdata->orientation[8]) * z;
213 
214 	gsensor_report_value(client, &axis);
215 	mutex_lock(&sensor->data_mutex);
216 	sensor->axis = axis;
217 	mutex_unlock(&sensor->data_mutex);
218 
219 	if (sensor->pdata->irq_enable) {
220 		value = sensor_read_reg(client, sensor->ops->int_status_reg);
221 		DBG("%s:gsensor int status :0x%x\n", __func__, value);
222 	}
223 
224 	return ret;
225 }
226 
227 static struct sensor_operate gsensor_lsm330_ops = {
228 	.name			= "lsm330_acc",
229 	.type			= SENSOR_TYPE_ACCEL,
230 	.id_i2c			= ACCEL_ID_LSM330,
231 	.read_reg			= OUT_X_L_A,
232 	.read_len			= 6,
233 	.id_reg			= WHO_AM_I_A,
234 	.id_data			= LSM330_DEVICE_ID_A,
235 	.precision			= 16,
236 	.ctrl_reg			= CTRL_REG5_A,
237 	.int_status_reg	= STATUS_REG_A,
238 	.range			= {-32768, 32768},
239 	.trig				= IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
240 	.active			= sensor_active,
241 	.init				= sensor_init,
242 	.report			= sensor_report_value,
243 };
244 
gsensor_lsm330_probe(struct i2c_client * client,const struct i2c_device_id * devid)245 static int gsensor_lsm330_probe(struct i2c_client *client,
246 				const struct i2c_device_id *devid)
247 {
248 	return sensor_register_device(client, NULL, devid, &gsensor_lsm330_ops);
249 }
250 
gsensor_lsm330_remove(struct i2c_client * client)251 static int gsensor_lsm330_remove(struct i2c_client *client)
252 {
253 	return sensor_unregister_device(client, NULL, &gsensor_lsm330_ops);
254 }
255 
256 static const struct i2c_device_id gsensor_lsm330_id[] = {
257 	{"lsm330_acc", ACCEL_ID_LSM330},
258 	{}
259 };
260 
261 static struct i2c_driver gsensor_lsm330_driver = {
262 	.probe = gsensor_lsm330_probe,
263 	.remove = gsensor_lsm330_remove,
264 	.shutdown = sensor_shutdown,
265 	.id_table = gsensor_lsm330_id,
266 	.driver = {
267 		.name = "gsensor_lsm330",
268 #ifdef CONFIG_PM
269 		.pm = &sensor_pm_ops,
270 #endif
271 	},
272 };
273 
274 module_i2c_driver(gsensor_lsm330_driver);
275 
276 MODULE_AUTHOR("Bin Yang <yangbin@rock-chips.com>");
277 MODULE_DESCRIPTION("lsm330_acc 3-Axis accelerometer driver");
278 MODULE_LICENSE("GPL");
279