1 /* drivers/input/sensors/sensor-dev.c - handle all gsensor in this file
2 *
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@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
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/irq.h>
21 #include <linux/miscdevice.h>
22 #include <linux/gpio.h>
23 #include <linux/uaccess.h>
24 #include <asm/atomic.h>
25 #include <linux/delay.h>
26 #include <linux/input.h>
27 #include <linux/workqueue.h>
28 #include <linux/freezer.h>
29 #include <linux/proc_fs.h>
30 #include <linux/gpio.h>
31 #include <linux/of_gpio.h>
32 #include <linux/of.h>
33 #ifdef CONFIG_HAS_EARLYSUSPEND
34 #include <linux/earlysuspend.h>
35 #endif
36 #include <linux/l3g4200d.h>
37 #include <linux/sensor-dev.h>
38 #include <linux/module.h>
39 #ifdef CONFIG_COMPAT
40 #include <linux/compat.h>
41 #endif
42 #include <linux/soc/rockchip/rk_vendor_storage.h>
43
44 #define SENSOR_CALIBRATION_LEN 64
45 struct sensor_calibration_data {
46 s32 accel_offset[3];
47 s32 gyro_offset[3];
48 u8 is_accel_calibrated;
49 u8 is_gyro_calibrated;
50 };
51
52 static struct sensor_private_data *g_sensor[SENSOR_NUM_TYPES];
53 static struct sensor_operate *sensor_ops[SENSOR_NUM_ID];
54 static int sensor_probe_times[SENSOR_NUM_ID];
55 static struct class *sensor_class;
56 static struct sensor_calibration_data sensor_cali_data;
57
sensor_calibration_data_write(struct sensor_calibration_data * calibration_data)58 static int sensor_calibration_data_write(struct sensor_calibration_data *calibration_data)
59 {
60 int ret;
61 u8 data[SENSOR_CALIBRATION_LEN] = {0};
62
63 memcpy(data, (u8 *)calibration_data, sizeof(struct sensor_calibration_data));
64
65 ret = rk_vendor_write(SENSOR_CALIBRATION_ID, (void *)data, SENSOR_CALIBRATION_LEN);
66 if (ret < 0) {
67 printk(KERN_ERR "%s failed\n", __func__);
68 return ret;
69 }
70
71 return 0;
72 }
73
sensor_calibration_data_read(struct sensor_calibration_data * calibration_data)74 static int sensor_calibration_data_read(struct sensor_calibration_data *calibration_data)
75 {
76 int ret;
77 u8 data[SENSOR_CALIBRATION_LEN] = {0};
78 struct sensor_calibration_data *cdata = (struct sensor_calibration_data *)data;
79
80 ret = rk_vendor_read(SENSOR_CALIBRATION_ID, (void *)data, SENSOR_CALIBRATION_LEN);
81 if (ret < 0) {
82 printk(KERN_ERR "%s failed\n", __func__);
83 return ret;
84 }
85 if (cdata->is_accel_calibrated == 1) {
86 calibration_data->accel_offset[0] = cdata->accel_offset[0];
87 calibration_data->accel_offset[1] = cdata->accel_offset[1];
88 calibration_data->accel_offset[2] = cdata->accel_offset[2];
89 calibration_data->is_accel_calibrated = 1;
90 }
91 if (cdata->is_gyro_calibrated == 1) {
92 calibration_data->gyro_offset[0] = cdata->gyro_offset[0];
93 calibration_data->gyro_offset[1] = cdata->gyro_offset[1];
94 calibration_data->gyro_offset[2] = cdata->gyro_offset[2];
95 calibration_data->is_gyro_calibrated = 1;
96 }
97
98 return 0;
99 }
100
accel_calibration_show(struct class * class,struct class_attribute * attr,char * buf)101 static ssize_t accel_calibration_show(struct class *class,
102 struct class_attribute *attr, char *buf)
103 {
104 int ret;
105 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
106
107 if (sensor == NULL)
108 return sprintf(buf, "no accel sensor find\n");
109
110 if (sensor_cali_data.is_accel_calibrated == 1)
111 return sprintf(buf, "accel calibration: %d, %d, %d\n", sensor_cali_data.accel_offset[0],
112 sensor_cali_data.accel_offset[1], sensor_cali_data.accel_offset[2]);
113
114 ret = sensor_calibration_data_read(&sensor_cali_data);
115 if (ret) {
116 dev_err(&sensor->client->dev, "read accel sensor calibration data failed\n");
117 return sprintf(buf, "read error\n");
118 }
119
120 if (sensor_cali_data.is_accel_calibrated == 1)
121 return sprintf(buf, "accel calibration: %d, %d, %d\n", sensor_cali_data.accel_offset[0],
122 sensor_cali_data.accel_offset[1], sensor_cali_data.accel_offset[2]);
123
124 return sprintf(buf, "read error\n");
125 }
126
127 #define ACCEL_CAPTURE_TIMES 20
128 #define ACCEL_SENSITIVE 16384
129 /* +-1 * 16384 / 9.8 */
130 #define ACCEL_OFFSET_MAX 1600
accel_do_calibration(struct sensor_private_data * sensor)131 static int accel_do_calibration(struct sensor_private_data *sensor)
132 {
133 int i;
134 int ret;
135 int max_try_times = 20;
136 long int sum_accel[3] = {0, 0, 0};
137
138 mutex_lock(&sensor->operation_mutex);
139 for (i = 0; i < ACCEL_CAPTURE_TIMES; ) {
140 ret = sensor->ops->report(sensor->client);
141 if (ret < 0)
142 dev_err(&sensor->client->dev, "in %s read accel data error\n", __func__);
143 if (abs(sensor->axis.x) > ACCEL_OFFSET_MAX ||
144 abs(sensor->axis.y) > ACCEL_OFFSET_MAX ||
145 abs(abs(sensor->axis.z) - ACCEL_SENSITIVE) > ACCEL_OFFSET_MAX) {
146 sum_accel[0] = 0;
147 sum_accel[1] = 0;
148 sum_accel[2] = 0;
149 i = 0;
150 max_try_times--;
151 } else {
152 sum_accel[0] += sensor->axis.x;
153 sum_accel[1] += sensor->axis.y;
154 sum_accel[2] += sensor->axis.z;
155 i++;
156 }
157 if (max_try_times == 0) {
158 mutex_unlock(&sensor->operation_mutex);
159 return -1;
160 }
161 dev_info(&sensor->client->dev, "%d times, read accel data is %d, %d, %d\n",
162 i, sensor->axis.x, sensor->axis.y, sensor->axis.z);
163 msleep(sensor->pdata->poll_delay_ms);
164 }
165 mutex_unlock(&sensor->operation_mutex);
166
167 sensor_cali_data.accel_offset[0] = sum_accel[0] / ACCEL_CAPTURE_TIMES;
168 sensor_cali_data.accel_offset[1] = sum_accel[1] / ACCEL_CAPTURE_TIMES;
169 sensor_cali_data.accel_offset[2] = sum_accel[2] / ACCEL_CAPTURE_TIMES;
170
171 sensor_cali_data.accel_offset[2] = sensor_cali_data.accel_offset[2] > 0
172 ? sensor_cali_data.accel_offset[2] - ACCEL_SENSITIVE : sensor_cali_data.accel_offset[2] + ACCEL_SENSITIVE;
173
174 sensor_cali_data.is_accel_calibrated = 1;
175
176 dev_info(&sensor->client->dev, "accel offset is %d, %d, %d\n", sensor_cali_data.accel_offset[0],
177 sensor_cali_data.accel_offset[1], sensor_cali_data.accel_offset[2]);
178
179 return 0;
180 }
181
accel_calibration_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)182 static ssize_t accel_calibration_store(struct class *class,
183 struct class_attribute *attr, const char *buf, size_t count)
184 {
185 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
186 int val, ret;
187 int pre_status;
188
189 if (sensor == NULL)
190 return -1;
191
192 ret = kstrtoint(buf, 10, &val);
193 if (ret) {
194 dev_err(&sensor->client->dev, "%s: kstrtoint error return %d\n", __func__, ret);
195 return -1;
196 }
197 if (val != 1) {
198 dev_err(&sensor->client->dev, "%s: error value\n", __func__);
199 return -1;
200 }
201 atomic_set(&sensor->is_factory, 1);
202
203 pre_status = sensor->status_cur;
204 if (pre_status == SENSOR_OFF) {
205 mutex_lock(&sensor->operation_mutex);
206 sensor->ops->active(sensor->client, SENSOR_ON, sensor->pdata->poll_delay_ms);
207 mutex_unlock(&sensor->operation_mutex);
208 } else {
209 sensor->stop_work = 1;
210 if (sensor->pdata->irq_enable)
211 disable_irq_nosync(sensor->client->irq);
212 else
213 cancel_delayed_work_sync(&sensor->delaywork);
214 }
215
216 ret = accel_do_calibration(sensor);
217 if (ret < 0) {
218 dev_err(&sensor->client->dev, "accel do calibration failed\n");
219 goto OUT;
220 }
221 ret = sensor_calibration_data_write(&sensor_cali_data);
222 if (ret)
223 dev_err(&sensor->client->dev, "write accel sensor calibration data failed\n");
224
225 OUT:
226 if (pre_status == SENSOR_ON) {
227 sensor->stop_work = 0;
228 if (sensor->pdata->irq_enable)
229 enable_irq(sensor->client->irq);
230 else
231 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
232 } else {
233 mutex_lock(&sensor->operation_mutex);
234 sensor->ops->active(sensor->client, SENSOR_OFF, sensor->pdata->poll_delay_ms);
235 mutex_unlock(&sensor->operation_mutex);
236 }
237
238 atomic_set(&sensor->is_factory, 0);
239 wake_up(&sensor->is_factory_ok);
240
241 return ret ? ret : count;
242 }
243
244 static CLASS_ATTR_RW(accel_calibration);
245
gyro_calibration_show(struct class * class,struct class_attribute * attr,char * buf)246 static ssize_t gyro_calibration_show(struct class *class,
247 struct class_attribute *attr, char *buf)
248 {
249 int ret;
250 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_GYROSCOPE];
251
252 if (sensor == NULL)
253 return sprintf(buf, "no gyro sensor find\n");
254
255 if (sensor_cali_data.is_gyro_calibrated == 1)
256 return sprintf(buf, "gyro calibration: %d, %d, %d\n", sensor_cali_data.gyro_offset[0],
257 sensor_cali_data.gyro_offset[1], sensor_cali_data.gyro_offset[2]);
258
259 ret = sensor_calibration_data_read(&sensor_cali_data);
260 if (ret) {
261 dev_err(&sensor->client->dev, "read gyro sensor calibration data failed\n");
262 return sprintf(buf, "read error\n");
263 }
264
265 if (sensor_cali_data.is_gyro_calibrated == 1)
266 return sprintf(buf, "gyro calibration: %d, %d, %d\n", sensor_cali_data.gyro_offset[0],
267 sensor_cali_data.gyro_offset[1], sensor_cali_data.gyro_offset[2]);
268
269 return sprintf(buf, "read error\n");
270 }
271
272 #define GYRO_CAPTURE_TIMES 20
gyro_do_calibration(struct sensor_private_data * sensor)273 static int gyro_do_calibration(struct sensor_private_data *sensor)
274 {
275 int i;
276 int ret;
277 long int sum_gyro[3] = {0, 0, 0};
278
279 mutex_lock(&sensor->operation_mutex);
280 for (i = 0; i < GYRO_CAPTURE_TIMES; i++) {
281 ret = sensor->ops->report(sensor->client);
282 if (ret < 0) {
283 dev_err(&sensor->client->dev, "in %s read gyro data error\n", __func__);
284 mutex_unlock(&sensor->operation_mutex);
285 return -1;
286 }
287 sum_gyro[0] += sensor->axis.x;
288 sum_gyro[1] += sensor->axis.y;
289 sum_gyro[2] += sensor->axis.z;
290 dev_info(&sensor->client->dev, "%d times, read gyro data is %d, %d, %d\n",
291 i, sensor->axis.x, sensor->axis.y, sensor->axis.z);
292 msleep(sensor->pdata->poll_delay_ms);
293 }
294 mutex_unlock(&sensor->operation_mutex);
295
296 sensor_cali_data.gyro_offset[0] = sum_gyro[0] / GYRO_CAPTURE_TIMES;
297 sensor_cali_data.gyro_offset[1] = sum_gyro[1] / GYRO_CAPTURE_TIMES;
298 sensor_cali_data.gyro_offset[2] = sum_gyro[2] / GYRO_CAPTURE_TIMES;
299 sensor_cali_data.is_gyro_calibrated = 1;
300
301 dev_info(&sensor->client->dev, "gyro offset is %d, %d, %d\n", sensor_cali_data.gyro_offset[0],
302 sensor_cali_data.gyro_offset[1], sensor_cali_data.gyro_offset[2]);
303
304 return 0;
305 }
306
gyro_calibration_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)307 static ssize_t gyro_calibration_store(struct class *class,
308 struct class_attribute *attr, const char *buf, size_t count)
309 {
310 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_GYROSCOPE];
311 int val, ret;
312 int pre_status;
313
314 if (sensor == NULL)
315 return -1;
316
317 ret = kstrtoint(buf, 10, &val);
318 if (ret) {
319 dev_err(&sensor->client->dev, "%s: kstrtoint error return %d\n", __func__, ret);
320 return -1;
321 }
322 if (val != 1) {
323 dev_err(&sensor->client->dev, "%s error value\n", __func__);
324 return -1;
325 }
326 atomic_set(&sensor->is_factory, 1);
327
328 pre_status = sensor->status_cur;
329 if (pre_status == SENSOR_OFF) {
330 mutex_lock(&sensor->operation_mutex);
331 sensor->ops->active(sensor->client, SENSOR_ON, sensor->pdata->poll_delay_ms);
332 mutex_unlock(&sensor->operation_mutex);
333 } else {
334 sensor->stop_work = 1;
335 if (sensor->pdata->irq_enable)
336 disable_irq_nosync(sensor->client->irq);
337 else
338 cancel_delayed_work_sync(&sensor->delaywork);
339 }
340
341 ret = gyro_do_calibration(sensor);
342 if (ret < 0) {
343 dev_err(&sensor->client->dev, "gyro do calibration failed\n");
344 goto OUT;
345 }
346
347 ret = sensor_calibration_data_write(&sensor_cali_data);
348 if (ret)
349 dev_err(&sensor->client->dev, "write gyro sensor calibration data failed\n");
350
351 OUT:
352 if (pre_status == SENSOR_ON) {
353 sensor->stop_work = 0;
354 if (sensor->pdata->irq_enable)
355 enable_irq(sensor->client->irq);
356 else
357 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
358 } else {
359 mutex_lock(&sensor->operation_mutex);
360 sensor->ops->active(sensor->client, SENSOR_OFF, sensor->pdata->poll_delay_ms);
361 mutex_unlock(&sensor->operation_mutex);
362 }
363
364 atomic_set(&sensor->is_factory, 0);
365 wake_up(&sensor->is_factory_ok);
366
367 return ret ? ret : count;
368 }
369
370 static CLASS_ATTR_RW(gyro_calibration);
371
sensor_class_init(void)372 static int sensor_class_init(void)
373 {
374 int ret ;
375
376 sensor_class = class_create(THIS_MODULE, "sensor_class");
377 ret = class_create_file(sensor_class, &class_attr_accel_calibration);
378 if (ret) {
379 printk(KERN_ERR "%s:Fail to creat accel class file\n", __func__);
380 return ret;
381 }
382
383 ret = class_create_file(sensor_class, &class_attr_gyro_calibration);
384 if (ret) {
385 printk(KERN_ERR "%s:Fail to creat gyro class file\n", __func__);
386 return ret;
387 }
388
389 return 0;
390 }
391
sensor_get_id(struct i2c_client * client,int * value)392 static int sensor_get_id(struct i2c_client *client, int *value)
393 {
394 struct sensor_private_data *sensor = (struct sensor_private_data *) i2c_get_clientdata(client);
395 int result = 0;
396 char temp = sensor->ops->id_reg;
397 int i = 0;
398
399 if (sensor->ops->id_reg >= 0) {
400 for (i = 0; i < 3; i++) {
401 result = sensor_rx_data(client, &temp, 1);
402 *value = temp;
403 if (!result)
404 break;
405 }
406
407 if (result)
408 return result;
409
410 if (*value != sensor->ops->id_data) {
411 dev_err(&client->dev, "%s:id=0x%x is not 0x%x\n", __func__, *value, sensor->ops->id_data);
412 result = -1;
413 }
414 }
415
416 return result;
417 }
418
sensor_initial(struct i2c_client * client)419 static int sensor_initial(struct i2c_client *client)
420 {
421 struct sensor_private_data *sensor = (struct sensor_private_data *) i2c_get_clientdata(client);
422 int result = 0;
423
424 /* register setting according to chip datasheet */
425 result = sensor->ops->init(client);
426 if (result < 0) {
427 dev_err(&client->dev, "%s:fail to init sensor\n", __func__);
428 return result;
429 }
430
431 return result;
432 }
433
sensor_chip_init(struct i2c_client * client)434 static int sensor_chip_init(struct i2c_client *client)
435 {
436 struct sensor_private_data *sensor = (struct sensor_private_data *) i2c_get_clientdata(client);
437 struct sensor_operate *ops = sensor_ops[(int)sensor->i2c_id->driver_data];
438 int result = 0;
439
440 if (ops) {
441 sensor->ops = ops;
442 } else {
443 dev_err(&client->dev, "%s:ops is null,sensor name is %s\n", __func__, sensor->i2c_id->name);
444 result = -1;
445 goto error;
446 }
447
448 if ((sensor->type != ops->type) || ((int)sensor->i2c_id->driver_data != ops->id_i2c)) {
449 dev_err(&client->dev, "%s:type or id is different:type=%d,%d,id=%d,%d\n", __func__, sensor->type, ops->type, (int)sensor->i2c_id->driver_data, ops->id_i2c);
450 result = -1;
451 goto error;
452 }
453
454 if (!ops->init || !ops->active || !ops->report) {
455 dev_err(&client->dev, "%s:error:some function is needed\n", __func__);
456 result = -1;
457 goto error;
458 }
459
460 result = sensor_get_id(sensor->client, &sensor->devid);
461 if (result < 0) {
462 dev_err(&client->dev, "%s:fail to read %s devid:0x%x\n", __func__, sensor->i2c_id->name, sensor->devid);
463 result = -2;
464 goto error;
465 }
466
467 dev_info(&client->dev, "%s:%s:devid=0x%x,ops=0x%p\n", __func__, sensor->i2c_id->name, sensor->devid, sensor->ops);
468
469 result = sensor_initial(sensor->client);
470 if (result < 0) {
471 dev_err(&client->dev, "%s:fail to init sensor\n", __func__);
472 result = -2;
473 goto error;
474 }
475 return 0;
476
477 error:
478 return result;
479 }
480
sensor_reset_rate(struct i2c_client * client,int rate)481 static int sensor_reset_rate(struct i2c_client *client, int rate)
482 {
483 struct sensor_private_data *sensor = (struct sensor_private_data *) i2c_get_clientdata(client);
484 int result = 0;
485
486 if (rate < 5)
487 rate = 5;
488 else if (rate > 200)
489 rate = 200;
490
491 dev_info(&client->dev, "set sensor poll time to %dms\n", rate);
492
493 /* work queue is always slow, we need more quickly to match hal rate */
494 if (sensor->pdata->poll_delay_ms == (rate - 4))
495 return 0;
496
497 sensor->pdata->poll_delay_ms = rate - 4;
498
499 if (sensor->status_cur == SENSOR_ON) {
500 if (!sensor->pdata->irq_enable) {
501 sensor->stop_work = 1;
502 cancel_delayed_work_sync(&sensor->delaywork);
503 }
504 sensor->ops->active(client, SENSOR_OFF, rate);
505 result = sensor->ops->active(client, SENSOR_ON, rate);
506 if (!sensor->pdata->irq_enable) {
507 sensor->stop_work = 0;
508 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
509 }
510 }
511
512 return result;
513 }
514
sensor_delaywork_func(struct work_struct * work)515 static void sensor_delaywork_func(struct work_struct *work)
516 {
517 struct delayed_work *delaywork = container_of(work, struct delayed_work, work);
518 struct sensor_private_data *sensor = container_of(delaywork, struct sensor_private_data, delaywork);
519 struct i2c_client *client = sensor->client;
520 int result;
521
522 mutex_lock(&sensor->sensor_mutex);
523 result = sensor->ops->report(client);
524 if (result < 0)
525 dev_err(&client->dev, "%s: Get data failed\n", __func__);
526 mutex_unlock(&sensor->sensor_mutex);
527
528 if ((!sensor->pdata->irq_enable) && (sensor->stop_work == 0))
529 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
530 }
531
532 /*
533 * This is a threaded IRQ handler so can access I2C/SPI. Since all
534 * interrupts are clear on read the IRQ line will be reasserted and
535 * the physical IRQ will be handled again if another interrupt is
536 * asserted while we run - in the normal course of events this is a
537 * rare occurrence so we save I2C/SPI reads. We're also assuming that
538 * it's rare to get lots of interrupts firing simultaneously so try to
539 * minimise I/O.
540 */
sensor_interrupt(int irq,void * dev_id)541 static irqreturn_t sensor_interrupt(int irq, void *dev_id)
542 {
543 struct sensor_private_data *sensor =
544 (struct sensor_private_data *)dev_id;
545 struct i2c_client *client = sensor->client;
546
547 mutex_lock(&sensor->sensor_mutex);
548 if (sensor->ops->report(client) < 0)
549 dev_err(&client->dev, "%s: Get data failed\n", __func__);
550 mutex_unlock(&sensor->sensor_mutex);
551
552 return IRQ_HANDLED;
553 }
554
sensor_irq_init(struct i2c_client * client)555 static int sensor_irq_init(struct i2c_client *client)
556 {
557 struct sensor_private_data *sensor =
558 (struct sensor_private_data *) i2c_get_clientdata(client);
559 int result = 0;
560 int irq;
561
562 if ((sensor->pdata->irq_enable) && (sensor->pdata->irq_flags != SENSOR_UNKNOW_DATA)) {
563 if (sensor->pdata->poll_delay_ms <= 0)
564 sensor->pdata->poll_delay_ms = 30;
565 result = gpio_request(client->irq, sensor->i2c_id->name);
566 if (result)
567 dev_err(&client->dev, "%s:fail to request gpio :%d\n", __func__, client->irq);
568
569 irq = gpio_to_irq(client->irq);
570 result = devm_request_threaded_irq(&client->dev, irq, NULL, sensor_interrupt, sensor->pdata->irq_flags | IRQF_ONESHOT, sensor->ops->name, sensor);
571 if (result) {
572 dev_err(&client->dev, "%s:fail to request irq = %d, ret = 0x%x\n", __func__, irq, result);
573 goto error;
574 }
575
576 client->irq = irq;
577 disable_irq_nosync(client->irq);
578
579 dev_info(&client->dev, "%s:use irq=%d\n", __func__, irq);
580 } else if (!sensor->pdata->irq_enable) {
581 INIT_DELAYED_WORK(&sensor->delaywork, sensor_delaywork_func);
582 sensor->stop_work = 1;
583 if (sensor->pdata->poll_delay_ms <= 0)
584 sensor->pdata->poll_delay_ms = 30;
585
586 dev_info(&client->dev, "%s:use polling,delay=%d ms\n", __func__, sensor->pdata->poll_delay_ms);
587 }
588
589 error:
590 return result;
591 }
592
sensor_shutdown(struct i2c_client * client)593 void sensor_shutdown(struct i2c_client *client)
594 {
595 #ifdef CONFIG_HAS_EARLYSUSPEND
596 struct sensor_private_data *sensor =
597 (struct sensor_private_data *) i2c_get_clientdata(client);
598
599 if ((sensor->ops->suspend) && (sensor->ops->resume))
600 unregister_early_suspend(&sensor->early_suspend);
601 #endif
602 }
603 EXPORT_SYMBOL(sensor_shutdown);
604
605 #ifdef CONFIG_HAS_EARLYSUSPEND
sensor_suspend(struct early_suspend * h)606 static void sensor_suspend(struct early_suspend *h)
607 {
608 struct sensor_private_data *sensor =
609 container_of(h, struct sensor_private_data, early_suspend);
610
611 if (sensor->ops->suspend)
612 sensor->ops->suspend(sensor->client);
613 }
614
sensor_resume(struct early_suspend * h)615 static void sensor_resume(struct early_suspend *h)
616 {
617 struct sensor_private_data *sensor =
618 container_of(h, struct sensor_private_data, early_suspend);
619
620 if (sensor->ops->resume)
621 sensor->ops->resume(sensor->client);
622 }
623 #endif
624
625 #ifdef CONFIG_PM
sensor_of_suspend(struct device * dev)626 static int __maybe_unused sensor_of_suspend(struct device *dev)
627 {
628 struct sensor_private_data *sensor = dev_get_drvdata(dev);
629
630 if (sensor->ops->suspend)
631 sensor->ops->suspend(sensor->client);
632
633 return 0;
634 }
635
sensor_of_resume(struct device * dev)636 static int __maybe_unused sensor_of_resume(struct device *dev)
637 {
638 struct sensor_private_data *sensor = dev_get_drvdata(dev);
639
640 if (sensor->ops->resume)
641 sensor->ops->resume(sensor->client);
642 if (sensor->pdata->power_off_in_suspend)
643 sensor_initial(sensor->client);
644
645 return 0;
646 }
647
648 const struct dev_pm_ops sensor_pm_ops = {
649 SET_SYSTEM_SLEEP_PM_OPS(sensor_of_suspend, sensor_of_resume)
650 };
651 EXPORT_SYMBOL(sensor_pm_ops);
652
653 #define SENSOR_PM_OPS (&sensor_pm_ops)
654 #else
655 #define SENSOR_PM_OPS NULL
656 #endif
657
angle_dev_open(struct inode * inode,struct file * file)658 static int angle_dev_open(struct inode *inode, struct file *file)
659 {
660 return 0;
661 }
662
angle_dev_release(struct inode * inode,struct file * file)663 static int angle_dev_release(struct inode *inode, struct file *file)
664 {
665 return 0;
666 }
667
sensor_enable(struct sensor_private_data * sensor,int enable)668 static int sensor_enable(struct sensor_private_data *sensor, int enable)
669 {
670 int result = 0;
671 struct i2c_client *client = sensor->client;
672
673 if (enable == SENSOR_ON) {
674 result = sensor->ops->active(client, 1, sensor->pdata->poll_delay_ms);
675 if (result < 0) {
676 dev_err(&client->dev, "%s:fail to active sensor,ret=%d\n", __func__, result);
677 return result;
678 }
679 sensor->status_cur = SENSOR_ON;
680 sensor->stop_work = 0;
681 if (sensor->pdata->irq_enable)
682 enable_irq(client->irq);
683 else
684 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
685 dev_info(&client->dev, "sensor on: starting poll sensor data %dms\n", sensor->pdata->poll_delay_ms);
686 } else {
687 sensor->stop_work = 1;
688 if (sensor->pdata->irq_enable)
689 disable_irq_nosync(client->irq);
690 else
691 cancel_delayed_work_sync(&sensor->delaywork);
692 result = sensor->ops->active(client, 0, sensor->pdata->poll_delay_ms);
693 if (result < 0) {
694 dev_err(&client->dev, "%s:fail to disable sensor,ret=%d\n", __func__, result);
695 return result;
696 }
697 sensor->status_cur = SENSOR_OFF;
698 }
699
700 return result;
701 }
702
703 /* ioctl - I/O control */
angle_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)704 static long angle_dev_ioctl(struct file *file,
705 unsigned int cmd, unsigned long arg)
706 {
707 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ANGLE];
708 struct i2c_client *client = sensor->client;
709 void __user *argp = (void __user *)arg;
710 struct sensor_axis axis = {0};
711 short rate;
712 int result = 0;
713
714 switch (cmd) {
715 case GSENSOR_IOCTL_APP_SET_RATE:
716 if (copy_from_user(&rate, argp, sizeof(rate))) {
717 result = -EFAULT;
718 goto error;
719 }
720 break;
721 default:
722 break;
723 }
724
725 switch (cmd) {
726 case GSENSOR_IOCTL_START:
727 mutex_lock(&sensor->operation_mutex);
728 if (++sensor->start_count == 1) {
729 if (sensor->status_cur == SENSOR_OFF) {
730 sensor_enable(sensor, SENSOR_ON);
731 }
732 }
733 mutex_unlock(&sensor->operation_mutex);
734 break;
735
736 case GSENSOR_IOCTL_CLOSE:
737 mutex_lock(&sensor->operation_mutex);
738 if (--sensor->start_count == 0) {
739 if (sensor->status_cur == SENSOR_ON) {
740 sensor_enable(sensor, SENSOR_OFF);
741 }
742 }
743 mutex_unlock(&sensor->operation_mutex);
744 break;
745
746 case GSENSOR_IOCTL_APP_SET_RATE:
747 mutex_lock(&sensor->operation_mutex);
748 result = sensor_reset_rate(client, rate);
749 if (result < 0) {
750 mutex_unlock(&sensor->operation_mutex);
751 goto error;
752 }
753 mutex_unlock(&sensor->operation_mutex);
754 break;
755
756 case GSENSOR_IOCTL_GETDATA:
757 mutex_lock(&sensor->data_mutex);
758 memcpy(&axis, &sensor->axis, sizeof(sensor->axis));
759 mutex_unlock(&sensor->data_mutex);
760 break;
761
762 default:
763 result = -ENOTTY;
764
765 goto error;
766 }
767
768 switch (cmd) {
769 case GSENSOR_IOCTL_GETDATA:
770 if (copy_to_user(argp, &axis, sizeof(axis))) {
771 dev_err(&client->dev, "failed to copy sense data to user space.\n");
772 result = -EFAULT;
773 goto error;
774 }
775 break;
776 default:
777 break;
778 }
779
780 error:
781 return result;
782 }
783
784
gsensor_dev_open(struct inode * inode,struct file * file)785 static int gsensor_dev_open(struct inode *inode, struct file *file)
786 {
787 return 0;
788 }
789
gsensor_dev_release(struct inode * inode,struct file * file)790 static int gsensor_dev_release(struct inode *inode, struct file *file)
791 {
792 return 0;
793 }
794
795 /* ioctl - I/O control */
gsensor_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)796 static long gsensor_dev_ioctl(struct file *file,
797 unsigned int cmd, unsigned long arg)
798 {
799 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
800 struct i2c_client *client = sensor->client;
801 void __user *argp = (void __user *)arg;
802 struct sensor_axis axis = {0};
803 short rate;
804 int result = 0;
805
806 wait_event_interruptible(sensor->is_factory_ok, (atomic_read(&sensor->is_factory) == 0));
807
808 switch (cmd) {
809 case GSENSOR_IOCTL_APP_SET_RATE:
810 if (copy_from_user(&rate, argp, sizeof(rate))) {
811 result = -EFAULT;
812 goto error;
813 }
814 break;
815 default:
816 break;
817 }
818
819 switch (cmd) {
820 case GSENSOR_IOCTL_START:
821 mutex_lock(&sensor->operation_mutex);
822 if (++sensor->start_count == 1) {
823 if (sensor->status_cur == SENSOR_OFF) {
824 sensor_enable(sensor, SENSOR_ON);
825 }
826 }
827 mutex_unlock(&sensor->operation_mutex);
828 break;
829
830 case GSENSOR_IOCTL_CLOSE:
831 mutex_lock(&sensor->operation_mutex);
832 if (--sensor->start_count == 0) {
833 if (sensor->status_cur == SENSOR_ON) {
834 sensor_enable(sensor, SENSOR_OFF);
835 }
836 }
837 mutex_unlock(&sensor->operation_mutex);
838 break;
839
840 case GSENSOR_IOCTL_APP_SET_RATE:
841 mutex_lock(&sensor->operation_mutex);
842 result = sensor_reset_rate(client, rate);
843 if (result < 0) {
844 mutex_unlock(&sensor->operation_mutex);
845 goto error;
846 }
847 mutex_unlock(&sensor->operation_mutex);
848 break;
849
850 case GSENSOR_IOCTL_GETDATA:
851 mutex_lock(&sensor->data_mutex);
852 memcpy(&axis, &sensor->axis, sizeof(sensor->axis));
853 mutex_unlock(&sensor->data_mutex);
854 break;
855
856 case GSENSOR_IOCTL_GET_CALIBRATION:
857 if (sensor_cali_data.is_accel_calibrated != 1) {
858 if (sensor_calibration_data_read(&sensor_cali_data)) {
859 dev_err(&client->dev, "failed to read accel offset data from storage\n");
860 result = -EFAULT;
861 goto error;
862 }
863 }
864 if (sensor_cali_data.is_accel_calibrated == 1) {
865 if (copy_to_user(argp, sensor_cali_data.accel_offset, sizeof(sensor_cali_data.accel_offset))) {
866 dev_err(&client->dev, "failed to copy accel offset data to user\n");
867 result = -EFAULT;
868 goto error;
869 }
870 }
871 break;
872
873 default:
874 result = -ENOTTY;
875 goto error;
876 }
877
878 switch (cmd) {
879 case GSENSOR_IOCTL_GETDATA:
880 if (copy_to_user(argp, &axis, sizeof(axis))) {
881 dev_err(&client->dev, "failed to copy sense data to user space.\n");
882 result = -EFAULT;
883 goto error;
884 }
885 break;
886 default:
887 break;
888 }
889
890 error:
891 return result;
892 }
893
compass_dev_open(struct inode * inode,struct file * file)894 static int compass_dev_open(struct inode *inode, struct file *file)
895 {
896 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_COMPASS];
897 int flag = 0;
898
899 flag = atomic_read(&sensor->flags.open_flag);
900 if (!flag) {
901 atomic_set(&sensor->flags.open_flag, 1);
902 wake_up(&sensor->flags.open_wq);
903 }
904
905 return 0;
906 }
907
compass_dev_release(struct inode * inode,struct file * file)908 static int compass_dev_release(struct inode *inode, struct file *file)
909 {
910 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_COMPASS];
911 int flag = 0;
912
913 flag = atomic_read(&sensor->flags.open_flag);
914 if (flag) {
915 atomic_set(&sensor->flags.open_flag, 0);
916 wake_up(&sensor->flags.open_wq);
917 }
918
919 return 0;
920 }
921
922 #ifdef CONFIG_COMPAT
923 /* ioctl - I/O control */
compass_dev_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)924 static long compass_dev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
925 {
926 void __user *arg64 = compat_ptr(arg);
927 int result = 0;
928
929 if (!file->f_op || !file->f_op->unlocked_ioctl) {
930 printk(KERN_ERR "file->f_op or file->f_op->unlocked_ioctl is null\n");
931 return -ENOTTY;
932 }
933
934 switch (cmd) {
935 case COMPAT_ECS_IOCTL_APP_SET_MFLAG:
936 if (file->f_op->unlocked_ioctl)
937 result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_SET_MFLAG, (unsigned long)arg64);
938 break;
939 case COMPAT_ECS_IOCTL_APP_GET_MFLAG:
940 if (file->f_op->unlocked_ioctl)
941 result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_GET_MFLAG, (unsigned long)arg64);
942 break;
943 case COMPAT_ECS_IOCTL_APP_SET_AFLAG:
944 if (file->f_op->unlocked_ioctl)
945 result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_SET_AFLAG, (unsigned long)arg64);
946 break;
947 case COMPAT_ECS_IOCTL_APP_GET_AFLAG:
948 if (file->f_op->unlocked_ioctl)
949 result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_GET_AFLAG, (unsigned long)arg64);
950 break;
951 case COMPAT_ECS_IOCTL_APP_SET_MVFLAG:
952 if (file->f_op->unlocked_ioctl)
953 result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_SET_MVFLAG, (unsigned long)arg64);
954 break;
955 case COMPAT_ECS_IOCTL_APP_GET_MVFLAG:
956 if (file->f_op->unlocked_ioctl)
957 result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_GET_MVFLAG, (unsigned long)arg64);
958 break;
959 case COMPAT_ECS_IOCTL_APP_SET_DELAY:
960 if (file->f_op->unlocked_ioctl)
961 result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_SET_DELAY, (unsigned long)arg64);
962 break;
963 case COMPAT_ECS_IOCTL_APP_GET_DELAY:
964 if (file->f_op->unlocked_ioctl)
965 result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_GET_DELAY, (unsigned long)arg64);
966 break;
967 default:
968 break;
969 }
970
971 return result;
972 }
973 #endif
974
975 /* ioctl - I/O control */
compass_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)976 static long compass_dev_ioctl(struct file *file,
977 unsigned int cmd, unsigned long arg)
978 {
979 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_COMPASS];
980 struct i2c_client *client = sensor->client;
981 void __user *argp = (void __user *)arg;
982 int result = 0;
983 short flag;
984
985 switch (cmd) {
986 case ECS_IOCTL_APP_SET_MFLAG:
987 case ECS_IOCTL_APP_SET_AFLAG:
988 case ECS_IOCTL_APP_SET_MVFLAG:
989 if (copy_from_user(&flag, argp, sizeof(flag)))
990 return -EFAULT;
991 if (flag < 0 || flag > 1)
992 return -EINVAL;
993 break;
994 case ECS_IOCTL_APP_SET_DELAY:
995 if (copy_from_user(&flag, argp, sizeof(flag)))
996 return -EFAULT;
997 break;
998 default:
999 break;
1000 }
1001
1002 switch (cmd) {
1003 case ECS_IOCTL_APP_SET_MFLAG:
1004 atomic_set(&sensor->flags.m_flag, flag);
1005 break;
1006 case ECS_IOCTL_APP_GET_MFLAG:
1007 flag = atomic_read(&sensor->flags.m_flag);
1008 break;
1009 case ECS_IOCTL_APP_SET_AFLAG:
1010 atomic_set(&sensor->flags.a_flag, flag);
1011 break;
1012 case ECS_IOCTL_APP_GET_AFLAG:
1013 flag = atomic_read(&sensor->flags.a_flag);
1014 break;
1015 case ECS_IOCTL_APP_SET_MVFLAG:
1016 atomic_set(&sensor->flags.mv_flag, flag);
1017 break;
1018 case ECS_IOCTL_APP_GET_MVFLAG:
1019 flag = atomic_read(&sensor->flags.mv_flag);
1020 break;
1021 case ECS_IOCTL_APP_SET_DELAY:
1022 sensor->flags.delay = flag;
1023 mutex_lock(&sensor->operation_mutex);
1024 result = sensor_reset_rate(client, flag);
1025 if (result < 0) {
1026 mutex_unlock(&sensor->operation_mutex);
1027 return result;
1028 }
1029 mutex_unlock(&sensor->operation_mutex);
1030 break;
1031 case ECS_IOCTL_APP_GET_DELAY:
1032 flag = sensor->flags.delay;
1033 break;
1034 default:
1035 return -ENOTTY;
1036 }
1037
1038 switch (cmd) {
1039 case ECS_IOCTL_APP_GET_MFLAG:
1040 case ECS_IOCTL_APP_GET_AFLAG:
1041 case ECS_IOCTL_APP_GET_MVFLAG:
1042 case ECS_IOCTL_APP_GET_DELAY:
1043 if (copy_to_user(argp, &flag, sizeof(flag)))
1044 return -EFAULT;
1045 break;
1046 default:
1047 break;
1048 }
1049
1050 return result;
1051 }
1052
gyro_dev_open(struct inode * inode,struct file * file)1053 static int gyro_dev_open(struct inode *inode, struct file *file)
1054 {
1055 return 0;
1056 }
1057
1058
gyro_dev_release(struct inode * inode,struct file * file)1059 static int gyro_dev_release(struct inode *inode, struct file *file)
1060 {
1061 return 0;
1062 }
1063
1064 /* ioctl - I/O control */
gyro_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1065 static long gyro_dev_ioctl(struct file *file,
1066 unsigned int cmd, unsigned long arg)
1067 {
1068 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_GYROSCOPE];
1069 struct i2c_client *client = sensor->client;
1070 void __user *argp = (void __user *)arg;
1071 int result = 0;
1072 int rate;
1073
1074 wait_event_interruptible(sensor->is_factory_ok, (atomic_read(&sensor->is_factory) == 0));
1075
1076 switch (cmd) {
1077 case L3G4200D_IOCTL_GET_ENABLE:
1078 result = !sensor->status_cur;
1079 if (copy_to_user(argp, &result, sizeof(result))) {
1080 dev_err(&client->dev, "%s:failed to copy status to user space.\n", __func__);
1081 return -EFAULT;
1082 }
1083 break;
1084 case L3G4200D_IOCTL_SET_ENABLE:
1085 if (copy_from_user(&result, argp, sizeof(result))) {
1086 dev_err(&client->dev, "%s:failed to copy gyro sensor status from user space.\n", __func__);
1087 return -EFAULT;
1088 }
1089 mutex_lock(&sensor->operation_mutex);
1090 if (result) {
1091 if (sensor->status_cur == SENSOR_OFF)
1092 sensor_enable(sensor, SENSOR_ON);
1093 } else {
1094 if (sensor->status_cur == SENSOR_ON)
1095 sensor_enable(sensor, SENSOR_OFF);
1096 }
1097 result = sensor->status_cur;
1098 if (copy_to_user(argp, &result, sizeof(result))) {
1099 mutex_unlock(&sensor->operation_mutex);
1100 dev_err(&client->dev, "%s:failed to copy sense data to user space.\n", __func__);
1101 return -EFAULT;
1102 }
1103 mutex_unlock(&sensor->operation_mutex);
1104 break;
1105 case L3G4200D_IOCTL_SET_DELAY:
1106 if (copy_from_user(&rate, argp, sizeof(rate))) {
1107 dev_err(&client->dev, "L3G4200D_IOCTL_SET_DELAY: copy form user failed\n");
1108 return -EFAULT;
1109 }
1110 mutex_lock(&sensor->operation_mutex);
1111 result = sensor_reset_rate(client, rate);
1112 if (result < 0) {
1113 dev_err(&client->dev, "gyro reset rate failed\n");
1114 mutex_unlock(&sensor->operation_mutex);
1115 goto error;
1116 }
1117 mutex_unlock(&sensor->operation_mutex);
1118 break;
1119 case L3G4200D_IOCTL_GET_CALIBRATION:
1120 if (sensor_cali_data.is_gyro_calibrated != 1) {
1121 if (sensor_calibration_data_read(&sensor_cali_data)) {
1122 dev_err(&client->dev, "failed to read gyro offset data from storage\n");
1123 result = -EFAULT;
1124 goto error;
1125 }
1126 }
1127 if (sensor_cali_data.is_gyro_calibrated == 1) {
1128 if (copy_to_user(argp, sensor_cali_data.gyro_offset, sizeof(sensor_cali_data.gyro_offset))) {
1129 dev_err(&client->dev, "failed to copy gyro offset data to user\n");
1130 result = -EFAULT;
1131 goto error;
1132 }
1133 }
1134 break;
1135 default:
1136 return -ENOTTY;
1137 }
1138
1139 error:
1140 return result;
1141 }
1142
light_dev_open(struct inode * inode,struct file * file)1143 static int light_dev_open(struct inode *inode, struct file *file)
1144 {
1145 return 0;
1146 }
1147
light_dev_release(struct inode * inode,struct file * file)1148 static int light_dev_release(struct inode *inode, struct file *file)
1149 {
1150 return 0;
1151 }
1152
1153 #ifdef CONFIG_COMPAT
light_dev_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1154 static long light_dev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1155 {
1156 long ret = 0;
1157 void __user *arg64 = compat_ptr(arg);
1158
1159 if (!file->f_op || !file->f_op->unlocked_ioctl) {
1160 printk(KERN_ERR "[DEBUG] file->f_op or file->f_op->unlocked_ioctl is null\n");
1161 return -ENOTTY;
1162 }
1163
1164 switch (cmd) {
1165 case COMPAT_LIGHTSENSOR_IOCTL_GET_ENABLED:
1166 if (file->f_op->unlocked_ioctl)
1167 ret = file->f_op->unlocked_ioctl(file, LIGHTSENSOR_IOCTL_GET_ENABLED, (unsigned long)arg64);
1168 break;
1169 case COMPAT_LIGHTSENSOR_IOCTL_ENABLE:
1170 if (file->f_op->unlocked_ioctl)
1171 ret = file->f_op->unlocked_ioctl(file, LIGHTSENSOR_IOCTL_ENABLE, (unsigned long)arg64);
1172 break;
1173 case COMPAT_LIGHTSENSOR_IOCTL_SET_RATE:
1174 if (file->f_op->unlocked_ioctl)
1175 ret = file->f_op->unlocked_ioctl(file, LIGHTSENSOR_IOCTL_SET_RATE, (unsigned long)arg64);
1176 break;
1177 default:
1178 break;
1179 }
1180
1181 return ret;
1182 }
1183 #endif
1184
1185 /* ioctl - I/O control */
light_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1186 static long light_dev_ioctl(struct file *file,
1187 unsigned int cmd, unsigned long arg)
1188 {
1189 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_LIGHT];
1190 struct i2c_client *client = sensor->client;
1191 void __user *argp = (void __user *)arg;
1192 int result = 0;
1193 short rate;
1194
1195 switch (cmd) {
1196 case LIGHTSENSOR_IOCTL_SET_RATE:
1197 if (copy_from_user(&rate, argp, sizeof(rate))) {
1198 dev_err(&client->dev, "%s:failed to copy light sensor rate from user space.\n", __func__);
1199 return -EFAULT;
1200 }
1201 mutex_lock(&sensor->operation_mutex);
1202 result = sensor_reset_rate(client, rate);
1203 if (result < 0) {
1204 mutex_unlock(&sensor->operation_mutex);
1205 goto error;
1206 }
1207 mutex_unlock(&sensor->operation_mutex);
1208 break;
1209 case LIGHTSENSOR_IOCTL_GET_ENABLED:
1210 result = sensor->status_cur;
1211 if (copy_to_user(argp, &result, sizeof(result))) {
1212 dev_err(&client->dev, "%s:failed to copy light sensor status to user space.\n", __func__);
1213 return -EFAULT;
1214 }
1215 break;
1216 case LIGHTSENSOR_IOCTL_ENABLE:
1217 if (copy_from_user(&result, argp, sizeof(result))) {
1218 dev_err(&client->dev, "%s:failed to copy light sensor status from user space.\n", __func__);
1219 return -EFAULT;
1220 }
1221
1222 mutex_lock(&sensor->operation_mutex);
1223 if (result) {
1224 if (sensor->status_cur == SENSOR_OFF)
1225 sensor_enable(sensor, SENSOR_ON);
1226 } else {
1227 if (sensor->status_cur == SENSOR_ON)
1228 sensor_enable(sensor, SENSOR_OFF);
1229 }
1230 mutex_unlock(&sensor->operation_mutex);
1231 break;
1232
1233 default:
1234 break;
1235 }
1236
1237 error:
1238 return result;
1239 }
1240
proximity_dev_open(struct inode * inode,struct file * file)1241 static int proximity_dev_open(struct inode *inode, struct file *file)
1242 {
1243 return 0;
1244 }
1245
proximity_dev_release(struct inode * inode,struct file * file)1246 static int proximity_dev_release(struct inode *inode, struct file *file)
1247 {
1248 return 0;
1249 }
1250
1251 #ifdef CONFIG_COMPAT
proximity_dev_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1252 static long proximity_dev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1253 {
1254 long ret = 0;
1255 void __user *arg64 = compat_ptr(arg);
1256
1257 if (!file->f_op || !file->f_op->unlocked_ioctl) {
1258 printk(KERN_ERR "file->f_op or file->f_op->unlocked_ioctl is null\n");
1259 return -ENOTTY;
1260 }
1261
1262 switch (cmd) {
1263 case COMPAT_PSENSOR_IOCTL_GET_ENABLED:
1264 if (file->f_op->unlocked_ioctl)
1265 ret = file->f_op->unlocked_ioctl(file, PSENSOR_IOCTL_GET_ENABLED, (unsigned long)arg64);
1266 break;
1267 case COMPAT_PSENSOR_IOCTL_ENABLE:
1268 if (file->f_op->unlocked_ioctl)
1269 ret = file->f_op->unlocked_ioctl(file, PSENSOR_IOCTL_ENABLE, (unsigned long)arg64);
1270 break;
1271 default:
1272 break;
1273 }
1274
1275 return ret;
1276 }
1277 #endif
1278
1279 /* ioctl - I/O control */
proximity_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1280 static long proximity_dev_ioctl(struct file *file,
1281 unsigned int cmd, unsigned long arg)
1282 {
1283 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_PROXIMITY];
1284 void __user *argp = (void __user *)arg;
1285 int result = 0;
1286
1287 switch (cmd) {
1288 case PSENSOR_IOCTL_GET_ENABLED:
1289 result = sensor->status_cur;
1290 if (copy_to_user(argp, &result, sizeof(result))) {
1291 dev_err(&sensor->client->dev, "%s:failed to copy psensor status to user space.\n", __func__);
1292 return -EFAULT;
1293 }
1294 break;
1295 case PSENSOR_IOCTL_ENABLE:
1296 if (copy_from_user(&result, argp, sizeof(result))) {
1297 dev_err(&sensor->client->dev, "%s:failed to copy psensor status from user space.\n", __func__);
1298 return -EFAULT;
1299 }
1300 mutex_lock(&sensor->operation_mutex);
1301 if (result) {
1302 if (sensor->status_cur == SENSOR_OFF)
1303 sensor_enable(sensor, SENSOR_ON);
1304 } else {
1305 if (sensor->status_cur == SENSOR_ON)
1306 sensor_enable(sensor, SENSOR_OFF);
1307 }
1308 mutex_unlock(&sensor->operation_mutex);
1309 break;
1310
1311 default:
1312 break;
1313 }
1314
1315 return result;
1316 }
1317
temperature_dev_open(struct inode * inode,struct file * file)1318 static int temperature_dev_open(struct inode *inode, struct file *file)
1319 {
1320 return 0;
1321 }
1322
temperature_dev_release(struct inode * inode,struct file * file)1323 static int temperature_dev_release(struct inode *inode, struct file *file)
1324 {
1325 return 0;
1326 }
1327
1328 /* ioctl - I/O control */
temperature_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1329 static long temperature_dev_ioctl(struct file *file,
1330 unsigned int cmd, unsigned long arg)
1331 {
1332 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_TEMPERATURE];
1333 void __user *argp = (void __user *)arg;
1334 int result = 0;
1335
1336 switch (cmd) {
1337 case TEMPERATURE_IOCTL_GET_ENABLED:
1338 result = sensor->status_cur;
1339 if (copy_to_user(argp, &result, sizeof(result))) {
1340 dev_err(&sensor->client->dev, "%s:failed to copy temperature sensor status to user space.\n", __func__);
1341 return -EFAULT;
1342 }
1343 break;
1344 case TEMPERATURE_IOCTL_ENABLE:
1345 if (copy_from_user(&result, argp, sizeof(result))) {
1346 dev_err(&sensor->client->dev, "%s:failed to copy temperature sensor status from user space.\n", __func__);
1347 return -EFAULT;
1348 }
1349 mutex_lock(&sensor->operation_mutex);
1350 if (result) {
1351 if (sensor->status_cur == SENSOR_OFF)
1352 sensor_enable(sensor, SENSOR_ON);
1353 } else {
1354 if (sensor->status_cur == SENSOR_ON)
1355 sensor_enable(sensor, SENSOR_OFF);
1356 }
1357 mutex_unlock(&sensor->operation_mutex);
1358 break;
1359
1360 default:
1361 break;
1362 }
1363
1364 return result;
1365 }
1366
1367
pressure_dev_open(struct inode * inode,struct file * file)1368 static int pressure_dev_open(struct inode *inode, struct file *file)
1369 {
1370 return 0;
1371 }
1372
1373
pressure_dev_release(struct inode * inode,struct file * file)1374 static int pressure_dev_release(struct inode *inode, struct file *file)
1375 {
1376 return 0;
1377 }
1378
1379
1380 /* ioctl - I/O control */
pressure_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1381 static long pressure_dev_ioctl(struct file *file,
1382 unsigned int cmd, unsigned long arg)
1383 {
1384 struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_PRESSURE];
1385 void __user *argp = (void __user *)arg;
1386 int result = 0;
1387
1388 switch (cmd) {
1389 case PRESSURE_IOCTL_GET_ENABLED:
1390 result = sensor->status_cur;
1391 if (copy_to_user(argp, &result, sizeof(result))) {
1392 dev_err(&sensor->client->dev, "%s:failed to copy pressure sensor status to user space.\n", __func__);
1393 return -EFAULT;
1394 }
1395 break;
1396 case PRESSURE_IOCTL_ENABLE:
1397 if (copy_from_user(&result, argp, sizeof(result))) {
1398 dev_err(&sensor->client->dev, "%s:failed to copy pressure sensor status from user space.\n", __func__);
1399 return -EFAULT;
1400 }
1401 mutex_lock(&sensor->operation_mutex);
1402 if (result) {
1403 if (sensor->status_cur == SENSOR_OFF)
1404 sensor_enable(sensor, SENSOR_ON);
1405 } else {
1406 if (sensor->status_cur == SENSOR_ON)
1407 sensor_enable(sensor, SENSOR_OFF);
1408 }
1409 mutex_unlock(&sensor->operation_mutex);
1410 break;
1411
1412 default:
1413 break;
1414 }
1415
1416 return result;
1417 }
1418
sensor_misc_device_register(struct sensor_private_data * sensor,int type)1419 static int sensor_misc_device_register(struct sensor_private_data *sensor, int type)
1420 {
1421 int result = 0;
1422
1423 switch (type) {
1424 case SENSOR_TYPE_ANGLE:
1425 if (!sensor->ops->misc_dev) {
1426 sensor->fops.owner = THIS_MODULE;
1427 sensor->fops.unlocked_ioctl = angle_dev_ioctl;
1428 sensor->fops.open = angle_dev_open;
1429 sensor->fops.release = angle_dev_release;
1430
1431 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1432 sensor->miscdev.name = "angle";
1433 sensor->miscdev.fops = &sensor->fops;
1434 } else {
1435 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1436 }
1437 break;
1438
1439 case SENSOR_TYPE_ACCEL:
1440 if (!sensor->ops->misc_dev) {
1441 sensor->fops.owner = THIS_MODULE;
1442 sensor->fops.unlocked_ioctl = gsensor_dev_ioctl;
1443 #ifdef CONFIG_COMPAT
1444 sensor->fops.compat_ioctl = gsensor_dev_ioctl;
1445 #endif
1446 sensor->fops.open = gsensor_dev_open;
1447 sensor->fops.release = gsensor_dev_release;
1448
1449 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1450 sensor->miscdev.name = "mma8452_daemon";
1451 sensor->miscdev.fops = &sensor->fops;
1452 } else {
1453 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1454 }
1455 break;
1456
1457 case SENSOR_TYPE_COMPASS:
1458 if (!sensor->ops->misc_dev) {
1459 sensor->fops.owner = THIS_MODULE;
1460 sensor->fops.unlocked_ioctl = compass_dev_ioctl;
1461 #ifdef CONFIG_COMPAT
1462 sensor->fops.compat_ioctl = compass_dev_compat_ioctl;
1463 #endif
1464 sensor->fops.open = compass_dev_open;
1465 sensor->fops.release = compass_dev_release;
1466
1467 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1468 sensor->miscdev.name = "compass";
1469 sensor->miscdev.fops = &sensor->fops;
1470 } else {
1471 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1472 }
1473 break;
1474
1475 case SENSOR_TYPE_GYROSCOPE:
1476 if (!sensor->ops->misc_dev) {
1477 sensor->fops.owner = THIS_MODULE;
1478 sensor->fops.unlocked_ioctl = gyro_dev_ioctl;
1479 sensor->fops.open = gyro_dev_open;
1480 sensor->fops.release = gyro_dev_release;
1481
1482 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1483 sensor->miscdev.name = "gyrosensor";
1484 sensor->miscdev.fops = &sensor->fops;
1485 } else {
1486 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1487 }
1488 break;
1489
1490 case SENSOR_TYPE_LIGHT:
1491 if (!sensor->ops->misc_dev) {
1492 sensor->fops.owner = THIS_MODULE;
1493 sensor->fops.unlocked_ioctl = light_dev_ioctl;
1494 #ifdef CONFIG_COMPAT
1495 sensor->fops.compat_ioctl = light_dev_compat_ioctl;
1496 #endif
1497 sensor->fops.open = light_dev_open;
1498 sensor->fops.release = light_dev_release;
1499
1500 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1501 sensor->miscdev.name = "lightsensor";
1502 sensor->miscdev.fops = &sensor->fops;
1503 } else {
1504 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1505 }
1506 break;
1507
1508 case SENSOR_TYPE_PROXIMITY:
1509 if (!sensor->ops->misc_dev) {
1510 sensor->fops.owner = THIS_MODULE;
1511 sensor->fops.unlocked_ioctl = proximity_dev_ioctl;
1512 #ifdef CONFIG_COMPAT
1513 sensor->fops.compat_ioctl = proximity_dev_compat_ioctl;
1514 #endif
1515 sensor->fops.open = proximity_dev_open;
1516 sensor->fops.release = proximity_dev_release;
1517
1518 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1519 sensor->miscdev.name = "psensor";
1520 sensor->miscdev.fops = &sensor->fops;
1521 } else {
1522 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1523 }
1524 break;
1525
1526 case SENSOR_TYPE_TEMPERATURE:
1527 if (!sensor->ops->misc_dev) {
1528 sensor->fops.owner = THIS_MODULE;
1529 sensor->fops.unlocked_ioctl = temperature_dev_ioctl;
1530 sensor->fops.open = temperature_dev_open;
1531 sensor->fops.release = temperature_dev_release;
1532
1533 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1534 sensor->miscdev.name = "temperature";
1535 sensor->miscdev.fops = &sensor->fops;
1536 } else {
1537 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1538 }
1539 break;
1540
1541 case SENSOR_TYPE_PRESSURE:
1542 if (!sensor->ops->misc_dev) {
1543 sensor->fops.owner = THIS_MODULE;
1544 sensor->fops.unlocked_ioctl = pressure_dev_ioctl;
1545 sensor->fops.open = pressure_dev_open;
1546 sensor->fops.release = pressure_dev_release;
1547
1548 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1549 sensor->miscdev.name = "pressure";
1550 sensor->miscdev.fops = &sensor->fops;
1551 } else {
1552 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1553 }
1554 break;
1555
1556 default:
1557 dev_err(&sensor->client->dev, "%s:unknow sensor type=%d\n", __func__, type);
1558 result = -1;
1559 goto error;
1560 }
1561
1562 sensor->miscdev.parent = &sensor->client->dev;
1563 result = misc_register(&sensor->miscdev);
1564 if (result < 0) {
1565 dev_err(&sensor->client->dev,
1566 "fail to register misc device %s\n", sensor->miscdev.name);
1567 goto error;
1568 }
1569 dev_info(&sensor->client->dev, "%s:miscdevice: %s\n", __func__, sensor->miscdev.name);
1570
1571 error:
1572 return result;
1573 }
1574
sensor_probe(struct i2c_client * client,const struct i2c_device_id * devid)1575 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *devid)
1576 {
1577 struct sensor_private_data *sensor;
1578 struct sensor_platform_data *pdata;
1579 struct device_node *np = client->dev.of_node;
1580 enum of_gpio_flags rst_flags, pwr_flags;
1581 unsigned long irq_flags;
1582 int result = 0;
1583 int type = 0;
1584 int reprobe_en = 0;
1585
1586 dev_info(&client->adapter->dev, "%s: %s,%p\n", __func__, devid->name, client);
1587
1588 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1589 result = -ENODEV;
1590 goto out_no_free;
1591 }
1592 if (!np) {
1593 dev_err(&client->dev, "no device tree\n");
1594 return -EINVAL;
1595 }
1596 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1597 if (!pdata) {
1598 result = -ENOMEM;
1599 goto out_no_free;
1600 }
1601 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
1602 if (!sensor) {
1603 result = -ENOMEM;
1604 goto out_no_free;
1605 }
1606
1607 of_property_read_u32(np, "type", &(pdata->type));
1608
1609 pdata->irq_pin = of_get_named_gpio_flags(np, "irq-gpio", 0, (enum of_gpio_flags *)&irq_flags);
1610 pdata->reset_pin = of_get_named_gpio_flags(np, "reset-gpio", 0, &rst_flags);
1611 pdata->power_pin = of_get_named_gpio_flags(np, "power-gpio", 0, &pwr_flags);
1612
1613 of_property_read_u32(np, "irq_enable", &(pdata->irq_enable));
1614 of_property_read_u32(np, "poll_delay_ms", &(pdata->poll_delay_ms));
1615
1616 of_property_read_u32(np, "x_min", &(pdata->x_min));
1617 of_property_read_u32(np, "y_min", &(pdata->y_min));
1618 of_property_read_u32(np, "z_min", &(pdata->z_min));
1619 of_property_read_u32(np, "factory", &(pdata->factory));
1620 of_property_read_u32(np, "layout", &(pdata->layout));
1621 of_property_read_u32(np, "reprobe_en", &reprobe_en);
1622
1623 of_property_read_u8(np, "address", &(pdata->address));
1624 of_get_property(np, "project_name", pdata->project_name);
1625
1626 of_property_read_u32(np, "power-off-in-suspend",
1627 &pdata->power_off_in_suspend);
1628
1629 switch (pdata->layout) {
1630 case 1:
1631 pdata->orientation[0] = 1;
1632 pdata->orientation[1] = 0;
1633 pdata->orientation[2] = 0;
1634
1635 pdata->orientation[3] = 0;
1636 pdata->orientation[4] = 1;
1637 pdata->orientation[5] = 0;
1638
1639 pdata->orientation[6] = 0;
1640 pdata->orientation[7] = 0;
1641 pdata->orientation[8] = 1;
1642 break;
1643
1644 case 2:
1645 pdata->orientation[0] = 0;
1646 pdata->orientation[1] = -1;
1647 pdata->orientation[2] = 0;
1648
1649 pdata->orientation[3] = 1;
1650 pdata->orientation[4] = 0;
1651 pdata->orientation[5] = 0;
1652
1653 pdata->orientation[6] = 0;
1654 pdata->orientation[7] = 0;
1655 pdata->orientation[8] = 1;
1656 break;
1657
1658 case 3:
1659 pdata->orientation[0] = -1;
1660 pdata->orientation[1] = 0;
1661 pdata->orientation[2] = 0;
1662
1663 pdata->orientation[3] = 0;
1664 pdata->orientation[4] = -1;
1665 pdata->orientation[5] = 0;
1666
1667 pdata->orientation[6] = 0;
1668 pdata->orientation[7] = 0;
1669 pdata->orientation[8] = 1;
1670 break;
1671
1672 case 4:
1673 pdata->orientation[0] = 0;
1674 pdata->orientation[1] = 1;
1675 pdata->orientation[2] = 0;
1676
1677 pdata->orientation[3] = -1;
1678 pdata->orientation[4] = 0;
1679 pdata->orientation[5] = 0;
1680
1681 pdata->orientation[6] = 0;
1682 pdata->orientation[7] = 0;
1683 pdata->orientation[8] = 1;
1684 break;
1685
1686 case 5:
1687 pdata->orientation[0] = 1;
1688 pdata->orientation[1] = 0;
1689 pdata->orientation[2] = 0;
1690
1691 pdata->orientation[3] = 0;
1692 pdata->orientation[4] = -1;
1693 pdata->orientation[5] = 0;
1694
1695 pdata->orientation[6] = 0;
1696 pdata->orientation[7] = 0;
1697 pdata->orientation[8] = -1;
1698 break;
1699
1700 case 6:
1701 pdata->orientation[0] = 0;
1702 pdata->orientation[1] = -1;
1703 pdata->orientation[2] = 0;
1704
1705 pdata->orientation[3] = -1;
1706 pdata->orientation[4] = 0;
1707 pdata->orientation[5] = 0;
1708
1709 pdata->orientation[6] = 0;
1710 pdata->orientation[7] = 0;
1711 pdata->orientation[8] = -1;
1712 break;
1713
1714 case 7:
1715 pdata->orientation[0] = -1;
1716 pdata->orientation[1] = 0;
1717 pdata->orientation[2] = 0;
1718
1719 pdata->orientation[3] = 0;
1720 pdata->orientation[4] = 1;
1721 pdata->orientation[5] = 0;
1722
1723 pdata->orientation[6] = 0;
1724 pdata->orientation[7] = 0;
1725 pdata->orientation[8] = -1;
1726 break;
1727
1728 case 8:
1729 pdata->orientation[0] = 0;
1730 pdata->orientation[1] = 1;
1731 pdata->orientation[2] = 0;
1732
1733 pdata->orientation[3] = 1;
1734 pdata->orientation[4] = 0;
1735 pdata->orientation[5] = 0;
1736
1737 pdata->orientation[6] = 0;
1738 pdata->orientation[7] = 0;
1739 pdata->orientation[8] = -1;
1740 break;
1741
1742 case 9:
1743 pdata->orientation[0] = -1;
1744 pdata->orientation[1] = 0;
1745 pdata->orientation[2] = 0;
1746
1747 pdata->orientation[3] = 0;
1748 pdata->orientation[4] = -1;
1749 pdata->orientation[5] = 0;
1750
1751 pdata->orientation[6] = 0;
1752 pdata->orientation[7] = 0;
1753 pdata->orientation[8] = -1;
1754 break;
1755
1756 default:
1757 pdata->orientation[0] = 1;
1758 pdata->orientation[1] = 0;
1759 pdata->orientation[2] = 0;
1760
1761 pdata->orientation[3] = 0;
1762 pdata->orientation[4] = 1;
1763 pdata->orientation[5] = 0;
1764
1765 pdata->orientation[6] = 0;
1766 pdata->orientation[7] = 0;
1767 pdata->orientation[8] = 1;
1768 break;
1769 }
1770
1771 client->irq = pdata->irq_pin;
1772 type = pdata->type;
1773 pdata->irq_flags = irq_flags;
1774 pdata->poll_delay_ms = 30;
1775
1776 if ((type >= SENSOR_NUM_TYPES) || (type <= SENSOR_TYPE_NULL)) {
1777 dev_err(&client->adapter->dev, "sensor type is error %d\n", type);
1778 result = -EFAULT;
1779 goto out_no_free;
1780 }
1781 if (((int)devid->driver_data >= SENSOR_NUM_ID) || ((int)devid->driver_data <= ID_INVALID)) {
1782 dev_err(&client->adapter->dev, "sensor id is error %d\n", (int)devid->driver_data);
1783 result = -EFAULT;
1784 goto out_no_free;
1785 }
1786 i2c_set_clientdata(client, sensor);
1787 sensor->client = client;
1788 sensor->pdata = pdata;
1789 sensor->type = type;
1790 sensor->i2c_id = (struct i2c_device_id *)devid;
1791
1792 memset(&(sensor->axis), 0, sizeof(struct sensor_axis));
1793 mutex_init(&sensor->data_mutex);
1794 mutex_init(&sensor->operation_mutex);
1795 mutex_init(&sensor->sensor_mutex);
1796 mutex_init(&sensor->i2c_mutex);
1797
1798 atomic_set(&sensor->is_factory, 0);
1799 init_waitqueue_head(&sensor->is_factory_ok);
1800
1801 /* As default, report all information */
1802 atomic_set(&sensor->flags.m_flag, 1);
1803 atomic_set(&sensor->flags.a_flag, 1);
1804 atomic_set(&sensor->flags.mv_flag, 1);
1805 atomic_set(&sensor->flags.open_flag, 0);
1806 atomic_set(&sensor->flags.debug_flag, 1);
1807 init_waitqueue_head(&sensor->flags.open_wq);
1808 sensor->flags.delay = 100;
1809
1810 sensor->status_cur = SENSOR_OFF;
1811 sensor->axis.x = 0;
1812 sensor->axis.y = 0;
1813 sensor->axis.z = 0;
1814
1815 result = sensor_chip_init(sensor->client);
1816 if (result < 0) {
1817 if (reprobe_en && (result == -2)) {
1818 sensor_probe_times[sensor->ops->id_i2c]++;
1819 if (sensor_probe_times[sensor->ops->id_i2c] < 3)
1820 result = -EPROBE_DEFER;
1821 }
1822 goto out_free_memory;
1823 }
1824
1825 sensor->input_dev = devm_input_allocate_device(&client->dev);
1826 if (!sensor->input_dev) {
1827 result = -ENOMEM;
1828 dev_err(&client->dev,
1829 "Failed to allocate input device\n");
1830 goto out_free_memory;
1831 }
1832
1833 switch (type) {
1834 case SENSOR_TYPE_ANGLE:
1835 sensor->input_dev->name = "angle";
1836 set_bit(EV_ABS, sensor->input_dev->evbit);
1837 /* x-axis acceleration */
1838 input_set_abs_params(sensor->input_dev, ABS_X, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1839 /* y-axis acceleration */
1840 input_set_abs_params(sensor->input_dev, ABS_Y, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1841 /* z-axis acceleration */
1842 input_set_abs_params(sensor->input_dev, ABS_Z, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1843 break;
1844 case SENSOR_TYPE_ACCEL:
1845 sensor->input_dev->name = "gsensor";
1846 set_bit(EV_ABS, sensor->input_dev->evbit);
1847 /* x-axis acceleration */
1848 input_set_abs_params(sensor->input_dev, ABS_X, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1849 /* y-axis acceleration */
1850 input_set_abs_params(sensor->input_dev, ABS_Y, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1851 /* z-axis acceleration */
1852 input_set_abs_params(sensor->input_dev, ABS_Z, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1853 break;
1854 case SENSOR_TYPE_COMPASS:
1855 sensor->input_dev->name = "compass";
1856 /* Setup input device */
1857 set_bit(EV_ABS, sensor->input_dev->evbit);
1858 /* yaw (0, 360) */
1859 input_set_abs_params(sensor->input_dev, ABS_RX, 0, 23040, 0, 0);
1860 /* pitch (-180, 180) */
1861 input_set_abs_params(sensor->input_dev, ABS_RY, -11520, 11520, 0, 0);
1862 /* roll (-90, 90) */
1863 input_set_abs_params(sensor->input_dev, ABS_RZ, -5760, 5760, 0, 0);
1864 /* x-axis acceleration (720 x 8G) */
1865 input_set_abs_params(sensor->input_dev, ABS_X, -5760, 5760, 0, 0);
1866 /* y-axis acceleration (720 x 8G) */
1867 input_set_abs_params(sensor->input_dev, ABS_Y, -5760, 5760, 0, 0);
1868 /* z-axis acceleration (720 x 8G) */
1869 input_set_abs_params(sensor->input_dev, ABS_Z, -5760, 5760, 0, 0);
1870 /* status of magnetic sensor */
1871 input_set_abs_params(sensor->input_dev, ABS_RUDDER, -32768, 3, 0, 0);
1872 /* status of acceleration sensor */
1873 input_set_abs_params(sensor->input_dev, ABS_WHEEL, -32768, 3, 0, 0);
1874 /* x-axis of raw magnetic vector (-4096, 4095) */
1875 input_set_abs_params(sensor->input_dev, ABS_HAT0X, -20480, 20479, 0, 0);
1876 /* y-axis of raw magnetic vector (-4096, 4095) */
1877 input_set_abs_params(sensor->input_dev, ABS_HAT0Y, -20480, 20479, 0, 0);
1878 /* z-axis of raw magnetic vector (-4096, 4095) */
1879 input_set_abs_params(sensor->input_dev, ABS_BRAKE, -20480, 20479, 0, 0);
1880 break;
1881 case SENSOR_TYPE_GYROSCOPE:
1882 sensor->input_dev->name = "gyro";
1883 /* x-axis acceleration */
1884 input_set_capability(sensor->input_dev, EV_REL, REL_RX);
1885 input_set_abs_params(sensor->input_dev, ABS_RX, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1886 /* y-axis acceleration */
1887 input_set_capability(sensor->input_dev, EV_REL, REL_RY);
1888 input_set_abs_params(sensor->input_dev, ABS_RY, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1889 /* z-axis acceleration */
1890 input_set_capability(sensor->input_dev, EV_REL, REL_RZ);
1891 input_set_abs_params(sensor->input_dev, ABS_RZ, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1892 break;
1893 case SENSOR_TYPE_LIGHT:
1894 sensor->input_dev->name = "lightsensor-level";
1895 set_bit(EV_ABS, sensor->input_dev->evbit);
1896 input_set_abs_params(sensor->input_dev, ABS_MISC, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1897 input_set_abs_params(sensor->input_dev, ABS_TOOL_WIDTH, sensor->ops->brightness[0], sensor->ops->brightness[1], 0, 0);
1898 break;
1899 case SENSOR_TYPE_PROXIMITY:
1900 sensor->input_dev->name = "proximity";
1901 set_bit(EV_ABS, sensor->input_dev->evbit);
1902 input_set_abs_params(sensor->input_dev, ABS_DISTANCE, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1903 break;
1904 case SENSOR_TYPE_TEMPERATURE:
1905 sensor->input_dev->name = "temperature";
1906 set_bit(EV_ABS, sensor->input_dev->evbit);
1907 input_set_abs_params(sensor->input_dev, ABS_THROTTLE, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1908 break;
1909 case SENSOR_TYPE_PRESSURE:
1910 sensor->input_dev->name = "pressure";
1911 set_bit(EV_ABS, sensor->input_dev->evbit);
1912 input_set_abs_params(sensor->input_dev, ABS_PRESSURE, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
1913 break;
1914 default:
1915 dev_err(&client->dev, "%s:unknow sensor type=%d\n", __func__, type);
1916 break;
1917 }
1918 sensor->input_dev->dev.parent = &client->dev;
1919
1920 result = input_register_device(sensor->input_dev);
1921 if (result) {
1922 dev_err(&client->dev,
1923 "Unable to register input device %s\n", sensor->input_dev->name);
1924 goto out_input_register_device_failed;
1925 }
1926
1927 result = sensor_irq_init(sensor->client);
1928 if (result) {
1929 dev_err(&client->dev,
1930 "fail to init sensor irq,ret=%d\n", result);
1931 goto out_input_register_device_failed;
1932 }
1933
1934 sensor->miscdev.parent = &client->dev;
1935 result = sensor_misc_device_register(sensor, type);
1936 if (result) {
1937 dev_err(&client->dev,
1938 "fail to register misc device %s\n", sensor->miscdev.name);
1939 goto out_misc_device_register_device_failed;
1940 }
1941
1942 g_sensor[type] = sensor;
1943
1944 #ifdef CONFIG_HAS_EARLYSUSPEND
1945 if ((sensor->ops->suspend) && (sensor->ops->resume)) {
1946 sensor->early_suspend.suspend = sensor_suspend;
1947 sensor->early_suspend.resume = sensor_resume;
1948 sensor->early_suspend.level = 0x02;
1949 register_early_suspend(&sensor->early_suspend);
1950 }
1951 #endif
1952
1953 dev_info(&client->dev, "%s:initialized ok,sensor name:%s,type:%d,id=%d\n\n", __func__, sensor->ops->name, type, (int)sensor->i2c_id->driver_data);
1954
1955 return result;
1956
1957 out_misc_device_register_device_failed:
1958 out_input_register_device_failed:
1959 out_free_memory:
1960 out_no_free:
1961 dev_err(&client->adapter->dev, "%s failed %d\n\n", __func__, result);
1962 return result;
1963 }
1964
sensor_remove(struct i2c_client * client)1965 static int sensor_remove(struct i2c_client *client)
1966 {
1967 struct sensor_private_data *sensor =
1968 (struct sensor_private_data *) i2c_get_clientdata(client);
1969
1970 sensor->stop_work = 1;
1971 cancel_delayed_work_sync(&sensor->delaywork);
1972 misc_deregister(&sensor->miscdev);
1973 #ifdef CONFIG_HAS_EARLYSUSPEND
1974 if ((sensor->ops->suspend) && (sensor->ops->resume))
1975 unregister_early_suspend(&sensor->early_suspend);
1976 #endif
1977
1978 return 0;
1979 }
1980
sensor_register_device(struct i2c_client * client,struct sensor_platform_data * slave_pdata,const struct i2c_device_id * devid,struct sensor_operate * ops)1981 int sensor_register_device(struct i2c_client *client,
1982 struct sensor_platform_data *slave_pdata,
1983 const struct i2c_device_id *devid,
1984 struct sensor_operate *ops)
1985 {
1986 int result = 0;
1987
1988 if (!client || !ops) {
1989 dev_err(&client->dev, "%s: no device or ops.\n", __func__);
1990 return -ENODEV;
1991 }
1992
1993 if ((ops->id_i2c >= SENSOR_NUM_ID) || (ops->id_i2c <= ID_INVALID) ||
1994 (((int)devid->driver_data) != ops->id_i2c)) {
1995 dev_err(&client->dev, "%s: %s id is error %d\n",
1996 __func__, ops->name, ops->id_i2c);
1997 return -EINVAL;
1998 }
1999
2000 sensor_ops[ops->id_i2c] = ops;
2001 dev_info(&client->dev, "%s: %s, id = %d\n",
2002 __func__, sensor_ops[ops->id_i2c]->name, ops->id_i2c);
2003
2004 sensor_probe(client, devid);
2005
2006 return result;
2007 }
2008 EXPORT_SYMBOL(sensor_register_device);
2009
sensor_unregister_device(struct i2c_client * client,struct sensor_platform_data * slave_pdata,struct sensor_operate * ops)2010 int sensor_unregister_device(struct i2c_client *client,
2011 struct sensor_platform_data *slave_pdata,
2012 struct sensor_operate *ops)
2013 {
2014 int result = 0;
2015
2016 if (!client || !ops) {
2017 dev_err(&client->dev, "%s: no device or ops.\n", __func__);
2018 return -ENODEV;
2019 }
2020
2021 if ((ops->id_i2c >= SENSOR_NUM_ID) || (ops->id_i2c <= ID_INVALID)) {
2022 dev_err(&client->dev, "%s: %s id is error %d\n",
2023 __func__, ops->name, ops->id_i2c);
2024 return -EINVAL;
2025 }
2026
2027 sensor_remove(client);
2028
2029 dev_info(&client->dev, "%s: %s, id = %d\n",
2030 __func__, sensor_ops[ops->id_i2c]->name, ops->id_i2c);
2031 sensor_ops[ops->id_i2c] = NULL;
2032
2033 return result;
2034 }
2035 EXPORT_SYMBOL(sensor_unregister_device);
2036
sensor_init(void)2037 static int __init sensor_init(void)
2038 {
2039 sensor_class_init();
2040
2041 return 0;
2042 }
2043
sensor_exit(void)2044 static void __exit sensor_exit(void)
2045 {
2046 class_remove_file(sensor_class, &class_attr_gyro_calibration);
2047 class_remove_file(sensor_class, &class_attr_accel_calibration);
2048 class_destroy(sensor_class);
2049 }
2050
2051 module_init(sensor_init);
2052 module_exit(sensor_exit);
2053
2054 MODULE_AUTHOR("ROCKCHIP Corporation:lw@rock-chips.com");
2055 MODULE_DESCRIPTION("User space character device interface for sensors");
2056 MODULE_LICENSE("GPL");
2057