1 /* drivers/input/sensors/access/mpu6880_gyro.c
2 *
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: ouenhui <oeh@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 #include <linux/mpu6500.h>
34
sensor_active(struct i2c_client * client,int enable,int rate)35 static int sensor_active(struct i2c_client *client, int enable, int rate)
36 {
37 struct sensor_private_data *sensor =
38 (struct sensor_private_data *) i2c_get_clientdata(client);
39 int result = 0;
40 int status = 0;
41 u8 pwrm1 = 0;
42
43 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
44 pwrm1 = sensor_read_reg(client, MPU6500_PWR_MGMT_1);
45
46 if (!enable) {
47 status = BIT_GYRO_STBY;
48 sensor->ops->ctrl_data |= status;
49 if ((sensor->ops->ctrl_data & BIT_ACCEL_STBY) == BIT_ACCEL_STBY) {
50 pwrm1 |= MPU6500_PWRM1_SLEEP;
51 }
52 } else {
53 status = ~BIT_GYRO_STBY;
54 sensor->ops->ctrl_data &= status;
55 pwrm1 &= ~MPU6500_PWRM1_SLEEP;
56 }
57
58 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
59 if (result) {
60 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
61 return -1;
62 }
63 msleep(20);
64
65 result = sensor_write_reg(client, MPU6500_PWR_MGMT_1, pwrm1);
66 if (result) {
67 dev_err(&client->dev, "%s:fail to set pwrm1\n", __func__);
68 return -1;
69 }
70 msleep(50);
71
72 return result;
73 }
74
sensor_init(struct i2c_client * client)75 static int sensor_init(struct i2c_client *client)
76 {
77 int ret;
78 struct sensor_private_data *sensor =
79 (struct sensor_private_data *) i2c_get_clientdata(client);
80
81 /* init on mpu6500_acc.c */
82 ret = sensor->ops->active(client, 0, sensor->pdata->poll_delay_ms);
83 if (ret) {
84 dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
85 return ret;
86 }
87
88 return ret;
89 }
90
gyro_report_value(struct i2c_client * client,struct sensor_axis * axis)91 static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)
92 {
93 struct sensor_private_data *sensor =
94 (struct sensor_private_data *) i2c_get_clientdata(client);
95
96 if (sensor->status_cur == SENSOR_ON) {
97 /* Report gyro sensor information */
98 input_report_rel(sensor->input_dev, ABS_RX, axis->x);
99 input_report_rel(sensor->input_dev, ABS_RY, axis->y);
100 input_report_rel(sensor->input_dev, ABS_RZ, axis->z);
101 input_sync(sensor->input_dev);
102 }
103
104 return 0;
105 }
106
sensor_report_value(struct i2c_client * client)107 static int sensor_report_value(struct i2c_client *client)
108 {
109 struct sensor_private_data *sensor =
110 (struct sensor_private_data *) i2c_get_clientdata(client);
111 struct sensor_platform_data *pdata = sensor->pdata;
112 int ret = 0;
113 short x, y, z;
114 struct sensor_axis axis;
115 u8 buffer[6] = {0};
116 char value = 0;
117
118 if (sensor->ops->read_len < 6) {
119 dev_err(&client->dev, "%s:lenth is error,len=%d\n", __func__, sensor->ops->read_len);
120 return -1;
121 }
122
123 memset(buffer, 0, 6);
124
125 do {
126 *buffer = sensor->ops->read_reg;
127 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
128 if (ret < 0)
129 return ret;
130 } while (0);
131
132 x = ((buffer[0] << 8) & 0xFF00) + (buffer[1] & 0xFF);
133 y = ((buffer[2] << 8) & 0xFF00) + (buffer[3] & 0xFF);
134 z = ((buffer[4] << 8) & 0xFF00) + (buffer[5] & 0xFF);
135
136 axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y + (pdata->orientation[2]) * z;
137 axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y + (pdata->orientation[5]) * z;
138 axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y + (pdata->orientation[8]) * z;
139
140 gyro_report_value(client, &axis);
141
142 mutex_lock(&(sensor->data_mutex));
143 sensor->axis = axis;
144 mutex_unlock(&(sensor->data_mutex));
145
146 if ((sensor->pdata->irq_enable) && (sensor->ops->int_status_reg >= 0))
147 value = sensor_read_reg(client, sensor->ops->int_status_reg);
148
149 return ret;
150 }
151
152 static struct sensor_operate gyro_mpu6500_ops = {
153 .name = "mpu6500_gyro",
154 .type = SENSOR_TYPE_GYROSCOPE,
155 .id_i2c = GYRO_ID_MPU6500,
156 .read_reg = MPU6500_GYRO_XOUT_H,
157 .read_len = 6,
158 .id_reg = SENSOR_UNKNOW_DATA,
159 .id_data = SENSOR_UNKNOW_DATA,
160 .precision = MPU6500_PRECISION,
161 .ctrl_reg = MPU6500_PWR_MGMT_2,
162 .int_status_reg = MPU6500_INT_STATUS,
163 .range = {-32768, 32768},
164 .trig = IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
165 .active = sensor_active,
166 .init = sensor_init,
167 .report = sensor_report_value,
168 };
169
170 /****************operate according to sensor chip:end************/
gyro_mpu6500_probe(struct i2c_client * client,const struct i2c_device_id * devid)171 static int gyro_mpu6500_probe(struct i2c_client *client,
172 const struct i2c_device_id *devid)
173 {
174 return sensor_register_device(client, NULL, devid, &gyro_mpu6500_ops);
175 }
176
gyro_mpu6500_remove(struct i2c_client * client)177 static int gyro_mpu6500_remove(struct i2c_client *client)
178 {
179 return sensor_unregister_device(client, NULL, &gyro_mpu6500_ops);
180 }
181
182 static const struct i2c_device_id gyro_mpu6500_id[] = {
183 {"mpu6500_gyro", GYRO_ID_MPU6500},
184 {}
185 };
186
187 static struct i2c_driver gyro_mpu6500_driver = {
188 .probe = gyro_mpu6500_probe,
189 .remove = gyro_mpu6500_remove,
190 .shutdown = sensor_shutdown,
191 .id_table = gyro_mpu6500_id,
192 .driver = {
193 .name = "gyro_mpu6500",
194 #ifdef CONFIG_PM
195 .pm = &sensor_pm_ops,
196 #endif
197 },
198 };
199
gyro_mpu6500_init(void)200 static int __init gyro_mpu6500_init(void)
201 {
202 return i2c_add_driver(&gyro_mpu6500_driver);
203 }
204
gyro_mpu6500_exit(void)205 static void __exit gyro_mpu6500_exit(void)
206 {
207 i2c_del_driver(&gyro_mpu6500_driver);
208 }
209
210 /* must register after mpu6500_acc */
211 device_initcall_sync(gyro_mpu6500_init);
212 module_exit(gyro_mpu6500_exit);
213
214 MODULE_AUTHOR("ouenhui <oeh@rock-chips.com>");
215 MODULE_DESCRIPTION("mpu6500_gyro 3-Axis Gyroscope driver");
216 MODULE_LICENSE("GPL");
217