xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/accel/icm2060x_acc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kernel/drivers/input/sensors/accel/icm2060x_acc.c
4  *
5  * Copyright (C) 2020 Rockchip Co.,Ltd.
6  * Author: Wang Jie <dave.wang@rock-chips.com>
7  */
8 #include <linux/interrupt.h>
9 #include <linux/i2c.h>
10 #include <linux/slab.h>
11 #include <linux/irq.h>
12 #include <linux/miscdevice.h>
13 #include <linux/gpio.h>
14 #include <linux/uaccess.h>
15 #include <linux/atomic.h>
16 #include <linux/delay.h>
17 #include <linux/input.h>
18 #include <linux/workqueue.h>
19 #include <linux/freezer.h>
20 #include <linux/of_gpio.h>
21 #ifdef CONFIG_HAS_EARLYSUSPEND
22 #include <linux/earlysuspend.h>
23 #endif
24 #include <linux/sensor-dev.h>
25 #include <linux/icm2060x.h>
26 
sensor_active(struct i2c_client * client,int enable,int rate)27 static int sensor_active(struct i2c_client *client, int enable, int rate)
28 {
29 	struct sensor_private_data *sensor =
30 	    (struct sensor_private_data *) i2c_get_clientdata(client);
31 	int result = 0;
32 	int status = 0;
33 	u8 pwrm1 = 0;
34 
35 	sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
36 	pwrm1 = sensor_read_reg(client, ICM2060X_PWR_MGMT_1);
37 
38 	if (!enable) {
39 		status = BIT_ACCEL_STBY;
40 		sensor->ops->ctrl_data |= status;
41 		if ((sensor->ops->ctrl_data & BIT_GYRO_STBY) == BIT_GYRO_STBY)
42 			pwrm1 |= ICM2060X_PWRM1_SLEEP;
43 	} else {
44 		status = ~BIT_ACCEL_STBY;
45 		sensor->ops->ctrl_data &= status;
46 		pwrm1 &= ~ICM2060X_PWRM1_SLEEP;
47 	}
48 
49 	result = sensor_write_reg(client, sensor->ops->ctrl_reg,
50 						sensor->ops->ctrl_data);
51 	if (result) {
52 		dev_err(&client->dev,
53 			"%s: fail to set pwrm2(%d)\n", __func__, result);
54 		return result;
55 	}
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 res = 0;
73 	u8 device_id = 0;
74 	struct sensor_private_data *sensor =
75 	    (struct sensor_private_data *) i2c_get_clientdata(client);
76 
77 	device_id = sensor_read_reg(client, ICM2060X_WHOAMI);
78 	if (device_id != ICM20600_DEVICE_ID &&
79 	    device_id != ICM20607_DEVICE_ID) {
80 		dev_err(&client->dev, "%s: check id err, read_id: %d\n",
81 			__func__, device_id);
82 		return -1;
83 	}
84 
85 	res = sensor_write_reg(client, ICM2060X_PWR_MGMT_1, 0x80);
86 	if (res) {
87 		dev_err(&client->dev,
88 			"set ICM2060X_PWR_MGMT_1 error,res: %d!\n", res);
89 		return res;
90 	}
91 	usleep_range(1000, 2000);
92 
93 	res = sensor_write_reg(client, ICM2060X_GYRO_CONFIG, 0x18);
94 	if (res) {
95 		dev_err(&client->dev,
96 			"set ICM2060X_GYRO_CONFIG error,res: %d!\n", res);
97 		return res;
98 	}
99 	usleep_range(1000, 2000);
100 
101 	res = sensor_write_reg(client, ICM2060X_ACCEL_CONFIG, 0x00);
102 	if (res) {
103 		dev_err(&client->dev,
104 			"set ICM2060X_ACCEL_CONFIG error,res: %d!\n", res);
105 		return res;
106 	}
107 	usleep_range(1000, 2000);
108 
109 	res = sensor_write_reg(client, ICM2060X_ACCEL_CONFIG2, 0x00);
110 	if (res) {
111 		dev_err(&client->dev,
112 			"set ICM2060X_ACCEL_CONFIG2 error,res: %d!\n", res);
113 		return res;
114 	}
115 
116 	res = sensor_write_reg(client, ICM2060X_PWR_MGMT_2, 0x3F);
117 	if (res) {
118 		dev_err(&client->dev,
119 			"set ICM2060X_PWR_MGMT_2 error,res: %d!\n", res);
120 		return res;
121 	}
122 	usleep_range(1000, 2000);
123 
124 	res = sensor_write_reg(client, ICM2060X_PWR_MGMT_1, 0x41);
125 	if (res) {
126 		dev_err(&client->dev,
127 			"set ICM2060X_PWR_MGMT_1 error,res: %d!\n", res);
128 		return res;
129 	}
130 	usleep_range(1000, 2000);
131 
132 	res = sensor->ops->active(client, 0, sensor->pdata->poll_delay_ms);
133 	if (res) {
134 		dev_err(&client->dev,
135 			"%s: fail to active sensor(%d)\n", __func__, res);
136 		return res;
137 	}
138 
139 	return res;
140 }
141 
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)142 static int gsensor_report_value(struct i2c_client *client,
143 				struct sensor_axis *axis)
144 {
145 	struct sensor_private_data *sensor =
146 	    (struct sensor_private_data *) i2c_get_clientdata(client);
147 
148 	if (sensor->status_cur == SENSOR_ON) {
149 		/* Report acceleration sensor information */
150 		input_report_abs(sensor->input_dev, ABS_X, axis->x);
151 		input_report_abs(sensor->input_dev, ABS_Y, axis->y);
152 		input_report_abs(sensor->input_dev, ABS_Z, axis->z);
153 		input_sync(sensor->input_dev);
154 	}
155 
156 	return 0;
157 }
158 
sensor_report_value(struct i2c_client * client)159 static int sensor_report_value(struct i2c_client *client)
160 {
161 	struct sensor_private_data *sensor =
162 		(struct sensor_private_data *) i2c_get_clientdata(client);
163 	struct sensor_platform_data *pdata = sensor->pdata;
164 	int ret = 0;
165 	short x, y, z;
166 	struct sensor_axis axis;
167 	u8 buffer[6] = {0};
168 	char value = 0;
169 
170 	if (sensor->ops->read_len < 6) {
171 		dev_err(&client->dev, "%s: length is error, len = %d\n",
172 			__func__, sensor->ops->read_len);
173 		return -EINVAL;
174 	}
175 
176 	/* Data bytes from hardware xL, xH, yL, yH, zL, zH */
177 	*buffer = sensor->ops->read_reg;
178 	ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
179 	if (ret < 0) {
180 		dev_err(&client->dev,
181 			"%s: read data failed, ret = %d\n", __func__, ret);
182 		return ret;
183 	}
184 
185 	x = ((buffer[0] << 8) & 0xff00) + (buffer[1] & 0xFF);
186 	y = ((buffer[2] << 8) & 0xff00) + (buffer[3] & 0xFF);
187 	z = ((buffer[4] << 8) & 0xff00) + (buffer[5] & 0xFF);
188 
189 	axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y +
190 		 (pdata->orientation[2]) * z;
191 	axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y +
192 		 (pdata->orientation[5]) * z;
193 	axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y +
194 		 (pdata->orientation[8]) * z;
195 
196 	gsensor_report_value(client, &axis);
197 
198 	mutex_lock(&(sensor->data_mutex));
199 	sensor->axis = axis;
200 	mutex_unlock(&(sensor->data_mutex));
201 
202 	if ((sensor->pdata->irq_enable) && (sensor->ops->int_status_reg >= 0))
203 		value = sensor_read_reg(client, sensor->ops->int_status_reg);
204 
205 	return ret;
206 }
207 
208 static struct sensor_operate gsensor_icm2060x_ops = {
209 	.name		= "icm2060x_acc",
210 	.type		= SENSOR_TYPE_ACCEL,
211 	.id_i2c		= ACCEL_ID_ICM2060X,
212 	.read_reg	= ICM2060X_ACCEL_XOUT_H,
213 	.read_len	= 6,
214 	.id_reg		= SENSOR_UNKNOW_DATA,
215 	.id_data	= SENSOR_UNKNOW_DATA,
216 	.precision	= ICM2060X_PRECISION,
217 	.ctrl_reg	= ICM2060X_PWR_MGMT_2,
218 	.int_status_reg = ICM2060X_INT_STATUS,
219 	.range		= {-32768, 32768},
220 	.trig		= IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
221 	.active		= sensor_active,
222 	.init		= sensor_init,
223 	.report		= sensor_report_value,
224 };
225 
226 /****************operate according to sensor chip:end************/
gsensor_icm2060x_probe(struct i2c_client * client,const struct i2c_device_id * devid)227 static int gsensor_icm2060x_probe(struct i2c_client *client,
228 				 const struct i2c_device_id *devid)
229 {
230 	return sensor_register_device(client, NULL, devid, &gsensor_icm2060x_ops);
231 }
232 
gsensor_icm2060x_remove(struct i2c_client * client)233 static int gsensor_icm2060x_remove(struct i2c_client *client)
234 {
235 	return sensor_unregister_device(client, NULL, &gsensor_icm2060x_ops);
236 }
237 
238 static const struct i2c_device_id gsensor_icm2060x_id[] = {
239 	{"icm2060x_acc", ACCEL_ID_ICM2060X},
240 	{}
241 };
242 
243 static struct i2c_driver gsensor_icm2060x_driver = {
244 	.probe = gsensor_icm2060x_probe,
245 	.remove = gsensor_icm2060x_remove,
246 	.shutdown = sensor_shutdown,
247 	.id_table = gsensor_icm2060x_id,
248 	.driver = {
249 		.name = "gsensor_icm2060x",
250 #ifdef CONFIG_PM
251 		.pm = &sensor_pm_ops,
252 #endif
253 	},
254 };
255 
256 module_i2c_driver(gsensor_icm2060x_driver);
257 
258 MODULE_AUTHOR("Wang Jie <dave.wang@rock-chips.com>");
259 MODULE_DESCRIPTION("icm2060x_acc 3-Axis accelerometer driver");
260 MODULE_LICENSE("GPL");
261