1 /*
2 * kernel/drivers/input/sensors/accel/lsm330_gyro.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 /* Angular rate sensor register */
37 #define WHO_AM_I_G 0x0F
38 #define CTRL_REG1_G 0x20
39 #define CTRL_REG2_G 0x21
40 #define CTRL_REG3_G 0x22
41 #define CTRL_REG4_G 0x23
42 #define CTRL_REG5_G 0x24
43 #define REFERENCE_G 0x25
44 #define OUT_TEMP_G 0x26
45 #define STATUS_REG_G 0x27
46 #define OUT_X_L_G 0x28
47 #define OUT_X_H_G 0x29
48 #define OUT_Y_L_G 0x2A
49 #define OUT_Y_H_G 0x2B
50 #define OUT_Z_L_G 0x2C
51 #define OUT_Z_H_G 0x2D
52 #define FIFO_CTRL_REG_G 0x2E
53 #define FIFO_SRC_REG_G 0x2F
54 #define INT1_CFG_G 0x30
55 #define INT1_SRC_G 0x31
56 #define INT1_TSH_XH_G 0x32
57 #define INT1_TSH_XL_G 0x33
58 #define INT1_TSH_YH_G 0x34
59 #define INT1_TSH_YL_G 0x35
60 #define INT1_TSH_ZH_G 0x36
61 #define INT1_TSH_ZL_G 0x37
62 #define INT1_DURATION_G 0x38
63
64 #define LSM330_DEVICE_ID_G 0xD4
65
sensor_active(struct i2c_client * client,int enable,int rate)66 static int sensor_active(struct i2c_client *client, int enable, int rate)
67 {
68 struct sensor_private_data *sensor =
69 (struct sensor_private_data *)i2c_get_clientdata(client);
70 int result = 0;
71
72 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
73 if (enable)
74 result = sensor_write_reg(client,
75 sensor->ops->ctrl_reg,
76 sensor->ops->ctrl_data | 0x0F);
77 else
78 result = sensor_write_reg(client,
79 sensor->ops->ctrl_reg,
80 0x00);
81
82 if (result)
83 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
84
85 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",
86 __func__,
87 sensor->ops->ctrl_reg,
88 sensor->ops->ctrl_data, enable);
89
90 return result;
91 }
92
sensor_init(struct i2c_client * client)93 static int sensor_init(struct i2c_client *client)
94 {
95 struct sensor_private_data *sensor =
96 (struct sensor_private_data *)i2c_get_clientdata(client);
97 int result = 0;
98
99 result = sensor->ops->active(client, 0, 0);
100 if (result) {
101 dev_err(&client->dev,
102 "%s:line=%d,error\n",
103 __func__,
104 __LINE__);
105 return result;
106 }
107 sensor->status_cur = SENSOR_OFF;
108
109 /*ODR: 760Hz, Cut-off: 100Hz*/
110 result = sensor_write_reg(client, CTRL_REG1_G, 0xF0);
111 if (result) {
112 dev_err(&client->dev,
113 "%s:fail to set CTRL_REG1_A.\n",
114 __func__);
115 return result;
116 }
117 result = sensor_write_reg(client, CTRL_REG3_G, 0x80);
118 if (result) {
119 dev_err(&client->dev,
120 "%s:fail to set CTRL_REG3_G.\n",
121 __func__);
122 return result;
123 }
124 /*Full-scale selection: 2000dps*/
125 result = sensor_write_reg(client, CTRL_REG4_G, 0x30);
126 if (result) {
127 dev_err(&client->dev,
128 "%s:fail to set CTRL_REG4_G.\n",
129 __func__);
130 return result;
131 }
132 result = sensor_write_reg(client, INT1_CFG_G, 0x7F);
133 if (result) {
134 dev_err(&client->dev, "%s:fail to set INT1_CFG_G.\n", __func__);
135 return result;
136 }
137
138 return result;
139 }
140
gyro_report_value(struct i2c_client * client,struct sensor_axis * axis)141 static int gyro_report_value(struct i2c_client *client,
142 struct sensor_axis *axis)
143 {
144 struct sensor_private_data *sensor =
145 (struct sensor_private_data *)i2c_get_clientdata(client);
146
147 if (sensor->status_cur == SENSOR_ON) {
148 input_report_rel(sensor->input_dev, ABS_RX, axis->x);
149 input_report_rel(sensor->input_dev, ABS_RY, axis->y);
150 input_report_rel(sensor->input_dev, ABS_RZ, axis->z);
151 input_sync(sensor->input_dev);
152 }
153
154 return 0;
155 }
156
sensor_report_value(struct i2c_client * client)157 static int sensor_report_value(struct i2c_client *client)
158 {
159 struct sensor_private_data *sensor =
160 (struct sensor_private_data *)i2c_get_clientdata(client);
161 struct sensor_platform_data *pdata = sensor->pdata;
162 char buffer[6];
163 int ret = 0;
164 char value = 0;
165 struct sensor_axis axis;
166 short x, y, z;
167 unsigned char reg_buf = 0;
168 unsigned char i = 0;
169
170 if (sensor->ops->read_len < 6) {
171 dev_err(&client->dev, "%s:Read len is error,len=%d\n",
172 __func__,
173 sensor->ops->read_len);
174 return -1;
175 }
176 memset(buffer, 0, 6);
177
178 reg_buf = sensor->ops->read_reg;
179 for (i = 0; i < sensor->ops->read_len; i++) {
180 buffer[i] = sensor_read_reg(client, reg_buf);
181 reg_buf++;
182 }
183
184 x = ((buffer[1] << 8) & 0xFF00) + (buffer[0] & 0xFF);
185 y = ((buffer[3] << 8) & 0xFF00) + (buffer[2] & 0xFF);
186 z = ((buffer[5] << 8) & 0xFF00) + (buffer[4] & 0xFF);
187
188 axis.x = (pdata->orientation[0]) * x +
189 (pdata->orientation[1]) * y +
190 (pdata->orientation[2]) * z;
191 axis.y = (pdata->orientation[3]) * x +
192 (pdata->orientation[4]) * y +
193 (pdata->orientation[5]) * z;
194 axis.z = (pdata->orientation[6]) * x +
195 (pdata->orientation[7]) * y +
196 (pdata->orientation[8]) * z;
197
198 gyro_report_value(client, &axis);
199 mutex_lock(&sensor->data_mutex);
200 sensor->axis = axis;
201 mutex_unlock(&sensor->data_mutex);
202
203 if (sensor->pdata->irq_enable) {
204 value = sensor_read_reg(client, sensor->ops->int_status_reg);
205 DBG("%s:gyro int status :0x%x\n", __func__, value);
206 }
207
208 return ret;
209 }
210
211 static struct sensor_operate gyro_lsm330_ops = {
212 .name = "lsm330_gyro",
213 .type = SENSOR_TYPE_GYROSCOPE,
214 .id_i2c = GYRO_ID_LSM330,
215 .read_reg = OUT_X_L_G,
216 .read_len = 6,
217 .id_reg = WHO_AM_I_G,
218 .id_data = LSM330_DEVICE_ID_G,
219 .precision = 16,
220 .ctrl_reg = CTRL_REG1_G,
221 .int_status_reg = INT1_SRC_G,
222 .range = {-32768, 32768},
223 .trig = IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
224 .active = sensor_active,
225 .init = sensor_init,
226 .report = sensor_report_value,
227 };
228
gyro_lsm330_probe(struct i2c_client * client,const struct i2c_device_id * devid)229 static int gyro_lsm330_probe(struct i2c_client *client,
230 const struct i2c_device_id *devid)
231 {
232 return sensor_register_device(client, NULL, devid, &gyro_lsm330_ops);
233 }
234
gyro_lsm330_remove(struct i2c_client * client)235 static int gyro_lsm330_remove(struct i2c_client *client)
236 {
237 return sensor_unregister_device(client, NULL, &gyro_lsm330_ops);
238 }
239
240 static const struct i2c_device_id gyro_lsm330_id[] = {
241 {"lsm330_gyro", GYRO_ID_LSM330},
242 {}
243 };
244
245 static struct i2c_driver gyro_lsm330_driver = {
246 .probe = gyro_lsm330_probe,
247 .remove = gyro_lsm330_remove,
248 .shutdown = sensor_shutdown,
249 .id_table = gyro_lsm330_id,
250 .driver = {
251 .name = "gyro_lsm330",
252 #ifdef CONFIG_PM
253 .pm = &sensor_pm_ops,
254 #endif
255 },
256 };
257
258 module_i2c_driver(gyro_lsm330_driver);
259
260 MODULE_AUTHOR("Bin Yang <yangbin@rock-chips.com>");
261 MODULE_DESCRIPTION("lsm330 3-Axis Gyroscope driver");
262 MODULE_LICENSE("GPL");
263