xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/gyro/icm2060x_gyro.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kernel/drivers/input/sensors/gyro/icm2060x_gyro.c
4  *
5  * Copyright (C) 2020 Rockchip Co.,Ltd.
6  * Author: Wang Jie <dave.wang@rock-chips.com>
7  */
8 
9 #include <linux/interrupt.h>
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/irq.h>
13 #include <linux/miscdevice.h>
14 #include <linux/gpio.h>
15 #include <linux/uaccess.h>
16 #include <linux/atomic.h>
17 #include <linux/delay.h>
18 #include <linux/input.h>
19 #include <linux/workqueue.h>
20 #include <linux/freezer.h>
21 #include <linux/of_gpio.h>
22 #ifdef CONFIG_HAS_EARLYSUSPEND
23 #include <linux/earlysuspend.h>
24 #endif
25 #include <linux/sensor-dev.h>
26 #include <linux/icm2060x.h>
27 
sensor_active(struct i2c_client * client,int enable,int rate)28 static int sensor_active(struct i2c_client *client, int enable, int rate)
29 {
30 	struct sensor_private_data *sensor =
31 	    (struct sensor_private_data *) i2c_get_clientdata(client);
32 	int result = 0;
33 	int status = 0;
34 	u8 pwrm1 = 0;
35 
36 	sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
37 	pwrm1 = sensor_read_reg(client, ICM2060X_PWR_MGMT_1);
38 
39 	if (!enable) {
40 		status = BIT_GYRO_STBY;
41 		sensor->ops->ctrl_data |= status;
42 		if ((sensor->ops->ctrl_data & BIT_ACCEL_STBY) == BIT_ACCEL_STBY)
43 			pwrm1 |= ICM2060X_PWRM1_SLEEP;
44 	} else {
45 		status = ~BIT_GYRO_STBY;
46 		sensor->ops->ctrl_data &= status;
47 		pwrm1 &= ~ICM2060X_PWRM1_SLEEP;
48 	}
49 
50 	result = sensor_write_reg(client, sensor->ops->ctrl_reg,
51 					sensor->ops->ctrl_data);
52 	if (result) {
53 		dev_err(&client->dev,
54 			"%s:fail to set ctrl_reg(%d)\n", __func__, result);
55 		return result;
56 	}
57 	msleep(20);
58 
59 	result = sensor_write_reg(client, ICM2060X_PWR_MGMT_1, pwrm1);
60 	if (result) {
61 		dev_err(&client->dev,
62 			"%s: fail to set pwrm1(%d)\n", __func__, result);
63 		return result;
64 	}
65 	msleep(50);
66 
67 	return result;
68 }
69 
sensor_init(struct i2c_client * client)70 static int sensor_init(struct i2c_client *client)
71 {
72 	int result;
73 	struct sensor_private_data *sensor =
74 	    (struct sensor_private_data *) i2c_get_clientdata(client);
75 
76 	/* init on icm2060x_acc.c */
77 	result = sensor->ops->active(client, 0, sensor->pdata->poll_delay_ms);
78 	if (result) {
79 		dev_err(&client->dev,
80 			"%s: sensor init err(%d)\n", __func__, result);
81 		return result;
82 	}
83 
84 	return result;
85 }
86 
gyro_report_value(struct i2c_client * client,struct sensor_axis * axis)87 static int gyro_report_value(struct i2c_client *client,
88 			     struct sensor_axis *axis)
89 {
90 	struct sensor_private_data *sensor =
91 	    (struct sensor_private_data *) i2c_get_clientdata(client);
92 
93 	if (sensor->status_cur == SENSOR_ON) {
94 		/* Report gyro sensor information */
95 		input_report_rel(sensor->input_dev, ABS_RX, axis->x);
96 		input_report_rel(sensor->input_dev, ABS_RY, axis->y);
97 		input_report_rel(sensor->input_dev, ABS_RZ, axis->z);
98 		input_sync(sensor->input_dev);
99 	}
100 
101 	return 0;
102 }
103 
sensor_report_value(struct i2c_client * client)104 static int sensor_report_value(struct i2c_client *client)
105 {
106 	struct sensor_private_data *sensor =
107 		(struct sensor_private_data *) i2c_get_clientdata(client);
108 	struct sensor_platform_data *pdata = sensor->pdata;
109 	int ret = 0;
110 	short x, y, z;
111 	struct sensor_axis axis;
112 	u8 buffer[6] = {0};
113 	char value = 0;
114 
115 	if (sensor->ops->read_len < 6) {
116 		dev_err(&client->dev, "%s: length is error,len = %d\n",
117 			__func__, sensor->ops->read_len);
118 		return -1;
119 	}
120 
121 	*buffer = sensor->ops->read_reg;
122 	ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
123 	if (ret < 0) {
124 		dev_err(&client->dev,
125 			"%s: read data failed, ret = %d\n", __func__, ret);
126 		return ret;
127 	}
128 
129 	x = ((buffer[0] << 8) & 0xFF00) + (buffer[1] & 0xFF);
130 	y = ((buffer[2] << 8) & 0xFF00) + (buffer[3] & 0xFF);
131 	z = ((buffer[4] << 8) & 0xFF00) + (buffer[5] & 0xFF);
132 
133 	axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y +
134 		 (pdata->orientation[2]) * z;
135 	axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y +
136 		 (pdata->orientation[5]) * z;
137 	axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y +
138 		 (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_icm2060x_ops = {
153 	.name		= "icm2060x_gyro",
154 	.type		= SENSOR_TYPE_GYROSCOPE,
155 	.id_i2c		= GYRO_ID_ICM2060X,
156 	.read_reg	= ICM2060X_GYRO_XOUT_H,
157 	.read_len	= 6,
158 	.id_reg		= SENSOR_UNKNOW_DATA,
159 	.id_data	= SENSOR_UNKNOW_DATA,
160 	.precision	= ICM2060X_PRECISION,
161 	.ctrl_reg	= ICM2060X_PWR_MGMT_2,
162 	.int_status_reg = ICM2060X_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_icm2060x_probe(struct i2c_client * client,const struct i2c_device_id * devid)171 static int gyro_icm2060x_probe(struct i2c_client *client,
172 				const struct i2c_device_id *devid)
173 {
174 	return sensor_register_device(client, NULL, devid, &gyro_icm2060x_ops);
175 }
176 
gyro_icm2060x_remove(struct i2c_client * client)177 static int gyro_icm2060x_remove(struct i2c_client *client)
178 {
179 	return sensor_unregister_device(client, NULL, &gyro_icm2060x_ops);
180 }
181 
182 static const struct i2c_device_id gyro_icm2060x_id[] = {
183 	{"icm2060x_gyro", GYRO_ID_ICM2060X},
184 	{}
185 };
186 
187 static struct i2c_driver gyro_icm2060x_driver = {
188 	.probe = gyro_icm2060x_probe,
189 	.remove = gyro_icm2060x_remove,
190 	.shutdown = sensor_shutdown,
191 	.id_table = gyro_icm2060x_id,
192 	.driver = {
193 		.name = "gyro_icm2060x",
194 #ifdef CONFIG_PM
195 		.pm = &sensor_pm_ops,
196 #endif
197 	},
198 };
199 
gyro_icm2060x_init(void)200 static int __init gyro_icm2060x_init(void)
201 {
202 	return i2c_add_driver(&gyro_icm2060x_driver);
203 }
204 
gyro_icm2060x_exit(void)205 static void __exit gyro_icm2060x_exit(void)
206 {
207 	i2c_del_driver(&gyro_icm2060x_driver);
208 }
209 
210 /* must register after icm2060x_acc */
211 device_initcall_sync(gyro_icm2060x_init);
212 module_exit(gyro_icm2060x_exit);
213 
214 MODULE_AUTHOR("Wang Jie <dave.wang@rock-chips.com>");
215 MODULE_DESCRIPTION("icm2060x_gyro 3-Axis Gyroscope driver");
216 MODULE_LICENSE("GPL");
217