1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * accel driver for iam20680
4 *
5 * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
6 *
7 * Author: sxj <sxj@rock-chips.com>
8 *
9 */
10 #include <linux/interrupt.h>
11 #include <linux/i2c.h>
12 #include <linux/slab.h>
13 #include <linux/irq.h>
14 #include <linux/miscdevice.h>
15 #include <linux/gpio.h>
16 #include <linux/uaccess.h>
17 #include <linux/atomic.h>
18 #include <linux/delay.h>
19 #include <linux/input.h>
20 #include <linux/workqueue.h>
21 #include <linux/freezer.h>
22 #include <linux/of_gpio.h>
23 #ifdef CONFIG_HAS_EARLYSUSPEND
24 #include <linux/earlysuspend.h>
25 #endif
26 #include <linux/sensor-dev.h>
27 #include <linux/iam20680.h>
28
iam20680_set_rate(struct i2c_client * client,int rate)29 static int iam20680_set_rate(struct i2c_client *client, int rate)
30 {
31 /* always use poll mode, no need to set rate */
32 return 0;
33 }
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, IAM20680_PWR_MGMT_1);
45
46 if (!enable) {
47 status = BIT_ACCEL_STBY;
48 sensor->ops->ctrl_data |= status;
49 if ((sensor->ops->ctrl_data & BIT_GYRO_STBY) == BIT_GYRO_STBY)
50 pwrm1 |= IAM20680_PWRM1_SLEEP;
51 } else {
52 status = ~BIT_ACCEL_STBY;
53 sensor->ops->ctrl_data &= status;
54 pwrm1 &= ~IAM20680_PWRM1_SLEEP;
55
56 iam20680_set_rate(client, rate);
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 set pwrm2\n", __func__);
61 return -1;
62 }
63 msleep(20);
64
65 result = sensor_write_reg(client, IAM20680_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 res = 0;
78 struct sensor_private_data *sensor =
79 (struct sensor_private_data *) i2c_get_clientdata(client);
80
81 res = sensor_write_reg(client, IAM20680_PWR_MGMT_1, 0x80);
82 if (res) {
83 dev_err(&client->dev, "set IAM20680_PWR_MGMT_1 error,res: %d!\n", res);
84 return res;
85 }
86 msleep(40);
87
88 res = sensor_write_reg(client, IAM20680_GYRO_CONFIG, 0x18);
89 if (res) {
90 dev_err(&client->dev, "set IAM20680_GYRO_CONFIG error,res: %d!\n", res);
91 return res;
92 }
93 msleep(10);
94
95 res = sensor_write_reg(client, IAM20680_ACCEL_CONFIG, 0x00);
96 if (res) {
97 dev_err(&client->dev, "set IAM20680_ACCEL_CONFIG error,res: %d!\n", res);
98 return res;
99 }
100 msleep(10);
101
102 res = sensor_write_reg(client, IAM20680_ACCEL_CONFIG2, 0x00);
103 if (res) {
104 dev_err(&client->dev, "set IAM20680_ACCEL_CONFIG2 error,res: %d!\n", res);
105 return res;
106 }
107 res = sensor_write_reg(client, IAM20680_PWR_MGMT_2, 0x3F);
108 if (res) {
109 dev_err(&client->dev, "set IAM20680_PWR_MGMT_2 error,res: %d!\n", res);
110 return res;
111 }
112 msleep(10);
113 res = sensor_write_reg(client, IAM20680_PWR_MGMT_1, 0x41);
114 if (res) {
115 dev_err(&client->dev, "set IAM20680_PWR_MGMT_1 error,res: %d!\n", res);
116 return res;
117 }
118 msleep(10);
119
120 res = sensor->ops->active(client, 0, sensor->pdata->poll_delay_ms);
121 if (res) {
122 dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
123 return res;
124 }
125 return res;
126 }
127
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)128 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
129 {
130 struct sensor_private_data *sensor =
131 (struct sensor_private_data *) i2c_get_clientdata(client);
132
133 if (sensor->status_cur == SENSOR_ON) {
134 /* Report acceleration sensor information */
135 input_report_abs(sensor->input_dev, ABS_X, axis->x);
136 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
137 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
138 input_sync(sensor->input_dev);
139 }
140
141 return 0;
142 }
143
sensor_report_value(struct i2c_client * client)144 static int sensor_report_value(struct i2c_client *client)
145 {
146 struct sensor_private_data *sensor =
147 (struct sensor_private_data *) i2c_get_clientdata(client);
148 struct sensor_platform_data *pdata = sensor->pdata;
149 int ret = 0;
150 short x, y, z;
151 struct sensor_axis axis;
152 u8 buffer[6] = {0};
153
154 if (sensor->ops->read_len < 6) {
155 dev_err(&client->dev, "%s: length is error, len=%d\n",
156 __func__, sensor->ops->read_len);
157 return -1;
158 }
159
160 memset(buffer, 0, 6);
161
162 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
163 do {
164 *buffer = sensor->ops->read_reg;
165 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
166 if (ret < 0)
167 return ret;
168 } while (0);
169
170 x = ((buffer[0] << 8) & 0xff00) + (buffer[1] & 0xFF);
171 y = ((buffer[2] << 8) & 0xff00) + (buffer[3] & 0xFF);
172 z = ((buffer[4] << 8) & 0xff00) + (buffer[5] & 0xFF);
173
174 axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y + (pdata->orientation[2]) * z;
175 axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y + (pdata->orientation[5]) * z;
176 axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y + (pdata->orientation[8]) * z;
177
178 gsensor_report_value(client, &axis);
179
180 mutex_lock(&(sensor->data_mutex));
181 sensor->axis = axis;
182 mutex_unlock(&(sensor->data_mutex));
183
184 return ret;
185 }
186
187 static struct sensor_operate gsensor_iam20680_ops = {
188 .name = "iam20680_acc",
189 .type = SENSOR_TYPE_ACCEL,
190 .id_i2c = ACCEL_ID_IAM20680,
191 .read_reg = IAM20680_ACCEL_XOUT_H,
192 .read_len = 6,
193 .id_reg = IAM20680_WHOAMI,
194 .id_data = IAM20680_DEVICE_ID,
195 .precision = IAM20680_PRECISION,
196 .ctrl_reg = IAM20680_PWR_MGMT_2,
197 .int_status_reg = IAM20680_INT_STATUS,
198 .range = {-32768, 32768},
199 .trig = IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
200 .active = sensor_active,
201 .init = sensor_init,
202 .report = sensor_report_value,
203 };
204
205 /****************operate according to sensor chip:end************/
gsensor_iam20680_probe(struct i2c_client * client,const struct i2c_device_id * devid)206 static int gsensor_iam20680_probe(struct i2c_client *client,
207 const struct i2c_device_id *devid)
208 {
209 return sensor_register_device(client, NULL, devid, &gsensor_iam20680_ops);
210 }
211
gsensor_iam20680_remove(struct i2c_client * client)212 static int gsensor_iam20680_remove(struct i2c_client *client)
213 {
214 return sensor_unregister_device(client, NULL, &gsensor_iam20680_ops);
215 }
216
217 static const struct i2c_device_id gsensor_iam20680_id[] = {
218 {"iam20680_acc", ACCEL_ID_IAM20680},
219 {}
220 };
221
222 static struct i2c_driver gsensor_iam20680_driver = {
223 .probe = gsensor_iam20680_probe,
224 .remove = gsensor_iam20680_remove,
225 .shutdown = sensor_shutdown,
226 .id_table = gsensor_iam20680_id,
227 .driver = {
228 .name = "gsensor_iam20680",
229 #ifdef CONFIG_PM
230 .pm = &sensor_pm_ops,
231 #endif
232 },
233 };
234
235 module_i2c_driver(gsensor_iam20680_driver);
236
237 MODULE_AUTHOR("sxj <sxj@rock-chips.com>");
238 MODULE_DESCRIPTION("iam20680_acc 3-Axis accelerometer driver");
239 MODULE_LICENSE("GPL");
240