1 /*
2 * Copyright (c) 2013-2015 liming@allwinnertech.com
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 */
8 #include <linux/input.h>
9 #include <linux/i2c.h>
10 #include <linux/delay.h>
11 #include <linux/irq.h>
12 #include <linux/interrupt.h>
13 #include <linux/irqdesc.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <asm/io.h>
19 #include <asm/uaccess.h>
20 #include <linux/clk.h>
21 #include <linux/gpio.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/of.h>
24 #include <linux/of_gpio.h>
25 #include <linux/of_platform.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include "init-input.h"
29
30 bool sensor_has_inited_flag;
31 /***************************CTP************************************/
32
33 /**
34 * sunxi_ctp_startup() - get config info from sys_config.fex file.
35 *@ctp_type: sensor type
36 * return value:
37 * = 0; success;
38 * < 0; err
39 */
sunxi_ctp_startup(enum input_sensor_type * ctp_type)40 static int sunxi_ctp_startup(enum input_sensor_type *ctp_type)
41 {
42 int ret = -1;
43 struct ctp_config_info *data = container_of(ctp_type,
44 struct ctp_config_info, input_type);
45 struct device_node *np = NULL;
46 struct i2c_client *client = NULL;
47 struct platform_device *pdev = NULL;
48 const char *idx;
49 char ctp_name[12];
50
51 np = data->node;
52
53 if (!np) {
54 if (data->np_name != NULL) {
55 np = of_find_node_by_name(NULL, data->np_name);
56 } else {
57 np = of_find_node_by_name(NULL, "ctp");
58 }
59 }
60 if (!np) {
61 pr_err("ERROR! get ctp_para failed, func:%s, line:%d\n", __func__, __LINE__);
62 goto devicetree_get_item_err;
63 }
64
65 if (!of_device_is_available(np)) {
66 pr_err("%s: ctp is not used\n", __func__);
67 goto devicetree_get_item_err;
68 } else
69 data->ctp_used = 1;
70
71 client = of_find_i2c_device_by_node(np);
72 if (client) {
73 data->dev = &client->dev;
74 data->isI2CClient = 1;
75 } else {
76 pdev = of_find_device_by_node(np);
77 if (pdev)
78 data->dev = &pdev->dev;
79 }
80 if (!data->dev) {
81 pr_err("get device_node fail\n");
82 return -1;
83 }
84
85 ret = of_property_read_u32(np, "ctp_twi_id", &data->twi_id);
86 if (ret) {
87 pr_err("get twi_id is fail, %d\n", ret);
88 goto devicetree_get_item_err;
89 }
90
91 ret = of_property_read_string(np, "ctp_fw_idx", &idx);
92 if (ret)
93 idx = 0;
94
95 snprintf(ctp_name, 12, "ctp_name%s", idx);
96 ret = of_property_read_string(np, ctp_name, &data->name);
97 if (ret)
98 ret = of_property_read_string(np, "ctp_name", &data->name);
99 if (ret)
100 pr_err("get ctp_name is fail, %d\n", ret);
101
102 ret = of_property_read_string(np, "ctp_power_ldo", &data->ctp_power);
103 if (ret)
104 pr_err("get ctp_power is fail, %d\n", ret);
105
106 ret = of_property_read_u32(np, "ctp_power_ldo_vol",
107 &data->ctp_power_vol);
108 if (ret)
109 pr_err("get ctp_power_ldo_vol is fail, %d\n", ret);
110
111 data->ctp_power_io.gpio = of_get_named_gpio_flags(np, "ctp_power_io", 0,
112 (enum of_gpio_flags *)(&(data->ctp_power_io)));
113 if (!gpio_is_valid(data->ctp_power_io.gpio))
114 pr_err("%s: ctp_power_io is invalid.\n", __func__);
115
116 data->wakeup_gpio.gpio = of_get_named_gpio_flags(np, "ctp_wakeup", 0,
117 (enum of_gpio_flags *)(&(data->wakeup_gpio)));
118 if (!gpio_is_valid(data->wakeup_gpio.gpio))
119 pr_err("%s: wakeup_gpio is invalid.\n", __func__);
120
121 ret = of_property_read_u32(np, "ctp_screen_max_x", &data->screen_max_x);
122 if (ret)
123 pr_err("get ctp_screen_max_x is fail, %d\n", ret);
124
125 ret = of_property_read_u32(np, "ctp_screen_max_y", &data->screen_max_y);
126 if (ret)
127 pr_err("get screen_max_y is fail, %d\n", ret);
128
129 ret = of_property_read_u32(np, "ctp_revert_x_flag",
130 &data->revert_x_flag);
131 if (ret)
132 pr_err("get revert_x_flag is fail, %d\n", ret);
133
134 ret = of_property_read_u32(np, "ctp_revert_y_flag",
135 &data->revert_y_flag);
136 if (ret)
137 pr_err("get revert_y_flag is fail, %d\n", ret);
138
139 ret = of_property_read_u32(np, "ctp_exchange_x_y_flag",
140 &data->exchange_x_y_flag);
141 if (ret)
142 pr_err("get ctp_exchange_x_y_flag is fail, %d\n", ret);
143
144 data->irq_gpio.gpio = of_get_named_gpio_flags(np, "ctp_int_port", 0,
145 (enum of_gpio_flags *)(&(data->irq_gpio)));
146 if (!gpio_is_valid(data->irq_gpio.gpio))
147 pr_err("%s: irq_gpio is invalid.\n", __func__);
148 else
149 data->int_number = data->irq_gpio.gpio;
150
151 ret = of_property_read_u32(np, "ctp_gesture_wakeup", &(data->ctp_gesture_wakeup));
152 if (ret) {
153 pr_err("get ctp_gesture_wakeup fail, no gesture wakeup\n");
154 }
155 #ifdef TOUCH_KEY_LIGHT_SUPPORT
156
157 data->key_light_gpio.gpio = of_get_named_gpio(np, "ctp_light", 0);
158 if (!gpio_is_valid(data->key_light_gpio.gpio))
159 pr_err("%s: key_light_gpio is invalid.\n", __func__);
160 #endif
161 return 0;
162
163 devicetree_get_item_err:
164 pr_notice("=========script_get_item_err============\n");
165 return ret;
166 }
167
168 /**
169 * sunxi_ctp_free() - free ctp related resource
170 * @ctp_type:sensor type
171 */
sunxi_ctp_free(enum input_sensor_type * ctp_type)172 static void sunxi_ctp_free(enum input_sensor_type *ctp_type)
173 {
174 struct ctp_config_info *data = container_of(ctp_type,
175 struct ctp_config_info, input_type);
176
177 gpio_free(data->wakeup_gpio.gpio);
178
179 #ifdef TOUCH_KEY_LIGHT_SUPPORT
180 gpio_free(data->key_light_gpio.gpio);
181 #endif
182 if (!IS_ERR_OR_NULL(data->ctp_power_ldo)) {
183 regulator_disable(data->ctp_power_ldo);
184 regulator_put(data->ctp_power_ldo);
185 data->ctp_power_ldo = NULL;
186 } else if (gpio_is_valid(data->ctp_power_io.gpio))
187 gpio_free(data->ctp_power_io.gpio);
188 }
189
190 /**
191 * sunxi_ctp_init - initialize platform related resource
192 * @ctp_type:sensor type
193 * return value: 0 : success
194 * -EIO : i/o err.
195 *
196 */
sunxi_ctp_init(enum input_sensor_type * ctp_type)197 static int sunxi_ctp_init(enum input_sensor_type *ctp_type)
198 {
199 int ret = -1;
200 struct ctp_config_info *data = container_of(ctp_type,
201 struct ctp_config_info, input_type);
202
203 data->ctp_power_ldo = NULL;
204 data->ctp_power_ldo = regulator_get(data->dev, "ctp");
205 if (!IS_ERR_OR_NULL(data->ctp_power_ldo)) {
206 regulator_set_voltage(data->ctp_power_ldo,
207 (int)(data->ctp_power_vol)*1000,
208 (int)(data->ctp_power_vol)*1000);
209 } else if (gpio_is_valid(data->ctp_power_io.gpio)) {
210 if (gpio_request(data->ctp_power_io.gpio, NULL) != 0)
211 pr_err("ctp_power_io gpio_request is failed, check if ctp independent power supply by gpio, ignore firstly\n");
212 else
213 gpio_direction_output(data->ctp_power_io.gpio, 1);
214 }
215 if (gpio_request(data->wakeup_gpio.gpio, NULL) != 0) {
216 pr_err("wakeup gpio_request is failed\n");
217 return ret;
218 }
219 if (gpio_direction_output(data->wakeup_gpio.gpio, 1) != 0) {
220 pr_err("wakeup gpio set err!");
221 return ret;
222 }
223
224 #ifdef TOUCH_KEY_LIGHT_SUPPORT
225 if (gpio_request(data->key_light_gpio.gpio, NULL) != 0) {
226 pr_err("key_light gpio_request is failed\n");
227 return ret;
228 }
229
230 if (gpio_direction_output(data->key_light_gpio.gpio, 1) != 0) {
231 pr_err("key_light gpio set err!");
232 return ret;
233 }
234 #endif
235 ret = 0;
236 return ret;
237 }
238 /*************************CTP END************************************/
239
240 /*************************GSENSOR************************************/
241
242 /*
243 * sunxi_gsensor_free() - free gsensor related resource
244 * @gsensor_type:sensor type
245 */
sunxi_gsensor_free(enum input_sensor_type * gsensor_type)246 static void sunxi_gsensor_free(enum input_sensor_type *gsensor_type)
247 {
248 struct sensor_config_info *data = container_of(gsensor_type,
249 struct sensor_config_info, input_type);
250
251 if (!IS_ERR_OR_NULL(data->sensor_power_ldo)) {
252 regulator_disable(data->sensor_power_ldo);
253 regulator_put(data->sensor_power_ldo);
254 data->sensor_power_ldo = NULL;
255 }
256 }
257
258 /**
259 * sunxi_gsensor_init() - initialize platform related resource
260 * @gsensor_type:sensor type
261 * return value: 0 : success
262 * -EIO : i/o err.
263 *
264 */
sunxi_gsensor_init(enum input_sensor_type * gsensor_type)265 static int sunxi_gsensor_init(enum input_sensor_type *gsensor_type)
266 {
267 struct sensor_config_info *data = container_of(gsensor_type,
268 struct sensor_config_info, input_type);
269 data->sensor_power_ldo = regulator_get(data->dev, "gsensor");
270 if (!IS_ERR_OR_NULL(data->sensor_power_ldo)) {
271 regulator_set_voltage(data->sensor_power_ldo,
272 (int)(data->sensor_power_vol)*1000,
273 (int)(data->sensor_power_vol)*1000);
274 }
275 return 0;
276 }
277
278 /**
279 * sunxi_gsensor_startup() - get config info from sys_config.fex file.
280 * @gsensor_type:sensor type
281 * return value:
282 * = 0; success;
283 * < 0; err
284 */
sunxi_gsensor_startup(enum input_sensor_type * gsensor_type)285 static int sunxi_gsensor_startup(enum input_sensor_type *gsensor_type)
286 {
287 int ret = -1;
288 struct sensor_config_info *data = container_of(gsensor_type,
289 struct sensor_config_info, input_type);
290 struct device_node *np = NULL;
291 struct i2c_client *client = NULL;
292 struct platform_device *pdev = NULL;
293
294 np = data->node;
295
296 if (!np) {
297 if (data->np_name != NULL) {
298 np = of_find_node_by_name(NULL, data->np_name);
299 } else {
300 np = of_find_node_by_name(NULL, "gsensor");
301 }
302 }
303
304 if (!np) {
305 pr_err("ERROR! get gsensor_para failed, func:%s, line:%d\n",
306 __func__, __LINE__);
307 goto devicetree_get_item_err;
308 }
309
310 if (!of_device_is_available(np)) {
311 pr_err("%s: gsensor is not used\n", __func__);
312 goto devicetree_get_item_err;
313 } else
314 data->sensor_used = 1;
315
316 client = of_find_i2c_device_by_node(np);
317 if (client) {
318 data->dev = &client->dev;
319 data->isI2CClient = 1;
320 } else {
321 pdev = of_find_device_by_node(np);
322 if (pdev)
323 data->dev = &pdev->dev;
324 }
325
326 if (!data->dev) {
327 pr_err("get device_node fail\n");
328 return -1;
329 }
330
331 if (data->sensor_used == 1) {
332 ret = of_property_read_u32(np, "gsensor_twi_id", &data->twi_id);
333 if (ret) {
334 pr_err("get gsensor_twi_id is fail, %d\n", ret);
335 goto devicetree_get_item_err;
336 }
337 } else
338 pr_err("%s gsensor_unused\n", __func__);
339 ret = of_property_read_u32(np, "gsensor_vcc_io_val",
340 &data->sensor_power_vol);
341 if (ret) {
342 pr_err("get gsensor_vcc_io_val is fail, %d\n", ret);
343 goto devicetree_get_item_err;
344 }
345
346 return ret;
347
348 devicetree_get_item_err:
349 pr_notice("=========gsensor script_get_err============\n");
350 ret = -1;
351 return ret;
352
353 }
354
355 /**************************GSENSOR END*********************************/
356
357 /*************************** GYR *************************************/
358
359 /**
360 * sunxi_gyr_free() - free gyr related resource
361 * @gyr_type:sensor type
362 * return value:
363 */
sunxi_gyr_free(enum input_sensor_type * gyr_type)364 static void sunxi_gyr_free(enum input_sensor_type *gyr_type)
365 {
366 }
367
368 /**
369 * sunxi_gyr_init() - initialize platform related resource
370 * @gyr_type:sensor type
371 * return value: 0 : success
372 * -EIO : i/o err.
373 *
374 */
sunxi_gyr_init(enum input_sensor_type * gyr_type)375 static int sunxi_gyr_init(enum input_sensor_type *gyr_type)
376 {
377 return 0;
378 }
379
380 /**
381 * sunxi_gyr_startup() - get config info from sys_config.fex file.
382 * @gyr_type:sensor type
383 * return value:
384 * = 0; success;
385 * < 0; err
386 */
sunxi_gyr_startup(enum input_sensor_type * gyr_type)387 static int sunxi_gyr_startup(enum input_sensor_type *gyr_type)
388 {
389 int ret = -1;
390 struct sensor_config_info *data = container_of(gyr_type,
391 struct sensor_config_info, input_type);
392 struct device_node *np = NULL;
393
394 np = of_find_node_by_name(NULL, "gy");
395 if (!np) {
396 pr_err("ERROR! get gy_para failed, func:%s, line:%d\n",
397 __func__, __LINE__);
398 goto devicetree_get_item_err;
399 }
400
401 if (!of_device_is_available(np)) {
402 pr_err("%s: gy is not used\n", __func__);
403 goto devicetree_get_item_err;
404 } else
405 data->sensor_used = 1;
406
407 if (data->sensor_used == 1) {
408 ret = of_property_read_u32(np, "gy_twi_id", &data->twi_id);
409 if (ret) {
410 pr_err("get gy_twi_id is fail, %d\n", ret);
411 goto devicetree_get_item_err;
412 }
413 } else
414 pr_err("%s gy_unused\n", __func__);
415
416 return ret;
417
418 devicetree_get_item_err:
419 pr_notice("=========script_get_err============\n");
420 ret = -1;
421 return ret;
422 }
423
424 /****************************** GYR END **********************************/
425
426 /****************************** COMPASS *********************************/
427
428 /**
429 * sunxi_ecompass_free() - free e_compass related resource
430 * @e_compass_type:sensor type
431 * return value:
432 */
sunxi_ecompass_free(enum input_sensor_type * e_compass_type)433 static void sunxi_ecompass_free(enum input_sensor_type *e_compass_type)
434 {
435 }
436
437 /**
438 * sunxi_ecompass_init() - initialize platform related resource
439 * @e_compass_type:sensor type
440 * return value: 0 : success
441 * -EIO : i/o err.
442 *
443 */
sunxi_ecompass_init(enum input_sensor_type * e_compass_type)444 static int sunxi_ecompass_init(enum input_sensor_type *e_compass_type)
445 {
446 return 0;
447 }
448
449 /**
450 * sunxi_ecompass_staetup() - get config info from sys_config.fex file.
451 * @e_compass_type:sensor type
452 * return value:
453 * = 0; success;
454 * < 0; err
455 */
456
sunxi_ecompass_startup(enum input_sensor_type * e_compass_type)457 static int sunxi_ecompass_startup(enum input_sensor_type *e_compass_type)
458 {
459 int ret = -1;
460 struct sensor_config_info *data = container_of(e_compass_type,
461 struct sensor_config_info, input_type);
462 struct device_node *np = NULL;
463
464 np = of_find_node_by_name(NULL, "compass");
465 if (!np) {
466 pr_err("ERROR! get compass_para failed, func:%s, line:%d\n",
467 __func__, __LINE__);
468 goto devicetree_get_item_err;
469 }
470
471 if (!of_device_is_available(np)) {
472 pr_err("%s: compass is not used\n", __func__);
473 goto devicetree_get_item_err;
474 } else
475 data->sensor_used = 1;
476
477 if (data->sensor_used == 1) {
478 ret = of_property_read_u32(np, "compass_twi_id", &data->twi_id);
479 if (ret) {
480 pr_err("get compass_twi_id is fail, %d\n", ret);
481 goto devicetree_get_item_err;
482 }
483 } else
484 pr_err("%s gsensor_unused\n", __func__);
485
486 return ret;
487
488 devicetree_get_item_err:
489 pr_notice("=========script_get_err============\n");
490 ret = -1;
491 return ret;
492
493 }
494 /***************************** COMPASS END ***********************************/
495
496 /**************************** LIGHT SENSOR ***********************************/
497
498 /**
499 * sunxi_ls_free() - free ls related resource
500 * @ls_type:sensor type
501 * return value:
502 */
sunxi_ls_free(enum input_sensor_type * ls_type)503 static void sunxi_ls_free(enum input_sensor_type *ls_type)
504 {
505 struct sensor_config_info *data = container_of(ls_type,
506 struct sensor_config_info, input_type);
507 /* disable ldo if it exist */
508
509 if (!IS_ERR_OR_NULL(data->sensor_power_ldo)) {
510 regulator_disable(data->sensor_power_ldo);
511 regulator_put(data->sensor_power_ldo);
512 }
513 }
514
515 /**
516 * sunxi_ls_init() - initialize platform related resource
517 * @ls_type:sensor type
518 * return value: 0 : success
519 * -EIO : i/o err.
520 *
521 */
sunxi_ls_init(enum input_sensor_type * ls_type)522 static int sunxi_ls_init(enum input_sensor_type *ls_type)
523 {
524 struct sensor_config_info *data = container_of(ls_type,
525 struct sensor_config_info, input_type);
526 data->sensor_power_ldo = regulator_get(data->dev, "lightsensor");
527 if (!IS_ERR_OR_NULL(data->sensor_power_ldo)) {
528 /*regulator_set_voltage(ldo, 3000000, 3000000);*/
529 if (0 != regulator_enable(data->sensor_power_ldo))
530 pr_err("%s: regulator_enable error!\n", __func__);
531 usleep_range(10000, 15000);
532
533 }
534 return 0;
535 }
536
537 /**
538 * sunxi_ls_startup() - get config info from sysconfig.fex file.
539 * @ls_type:sensor type
540 * return value:
541 * = 0; success;
542 * < 0; err
543 */
544
sunxi_ls_startup(enum input_sensor_type * ls_type)545 static int sunxi_ls_startup(enum input_sensor_type *ls_type)
546 {
547 int ret = -1;
548 struct sensor_config_info *data = container_of(ls_type,
549 struct sensor_config_info, input_type);
550 struct device_node *np = NULL;
551 struct i2c_client *client = NULL;
552 struct platform_device *pdev = NULL;
553
554 np = data->node;
555 if (!np) {
556 if (data->np_name != NULL) {
557 np = of_find_node_by_name(NULL, data->np_name);
558 } else {
559 np = of_find_node_by_name(NULL, "lightsensor");
560 }
561 }
562
563 if (!np) {
564 pr_err("ERROR! get ls_para failed, func:%s, line:%d\n",
565 __func__, __LINE__);
566 goto devicetree_get_item_err;
567 }
568
569 if (!of_device_is_available(np)) {
570 pr_err("%s: ls is not used\n", __func__);
571 goto devicetree_get_item_err;
572 } else
573 data->sensor_used = 1;
574
575 client = of_find_i2c_device_by_node(np);
576 if (client) {
577 data->dev = &client->dev;
578 data->isI2CClient = 1;
579 } else {
580 pdev = of_find_device_by_node(np);
581 if (pdev)
582 data->dev = &pdev->dev;
583 }
584 if (!data->dev) {
585 pr_err("get device_node fail\n");
586 return -1;
587 }
588
589 if (data->sensor_used == 1) {
590 ret = of_property_read_u32(np, "ls_twi_id", &data->twi_id);
591 if (ret) {
592 pr_err("get compass_twi_id is fail, %d\n", ret);
593 goto devicetree_get_item_err;
594 }
595
596 data->irq_gpio.gpio = of_get_named_gpio(np, "ls_int", 0);
597 if (!gpio_is_valid(data->irq_gpio.gpio))
598 pr_err("%s: irq_gpio is invalid.\n", __func__);
599 else
600 data->int_number = data->irq_gpio.gpio;
601 #if !IS_ENABLED(CONFIG_SUNXI_REGULATOR_DT)
602 of_property_read_string(np, "ls_vcc", &data->sensor_power);
603 #endif
604 } else {
605 pr_err("%s light sensor unused \n", __func__);
606 }
607
608 return ret;
609
610 devicetree_get_item_err:
611 ret = -1;
612 return ret;
613
614 }
615 /**************** LIGHT SENSOR END ***********************/
616
617 /************************ MOTOR *************************/
618
619 /**
620 * sunxi_motor_free()- free ths related resource
621 * @motor_type:sensor type
622 * return value:
623 */
sunxi_motor_free(enum input_sensor_type * motor_type)624 static void sunxi_motor_free(enum input_sensor_type *motor_type)
625 {
626 struct motor_config_info *data = container_of(motor_type,
627 struct motor_config_info, input_type);
628
629 if (data->motor_gpio.gpio != 0)
630 gpio_free(data->motor_gpio.gpio);
631 if (data->dev != NULL) {
632 if (data->dev->init_name)
633 kfree(data->dev->init_name);
634 kfree(data->dev);
635 data->dev = NULL;
636 }
637 }
638
639 /**
640 * sunxi_motor_init() - initialize platform related resource
641 * @motor_type:sensor type
642 * return value: 0 : success
643 * -EIO : i/o err.
644 *
645 */
sunxi_motor_init(enum input_sensor_type * motor_type)646 static int sunxi_motor_init(enum input_sensor_type *motor_type)
647 {
648 struct motor_config_info *data = container_of(motor_type,
649 struct motor_config_info, input_type);
650
651 #ifdef CONFIG_SUNXI_REGULATOR_DT
652 data->motor_power_ldo = regulator_get(data->dev, "motor");
653 if (!IS_ERR(data->motor_power_ldo)) {
654 regulator_set_voltage(data->motor_power_ldo,
655 (int)(data->ldo_voltage)*1000,
656 (int)(data->ldo_voltage)*1000);
657 }
658 #endif
659 if (gpio_is_valid(data->motor_gpio.gpio)) {
660 if (gpio_request(data->motor_gpio.gpio, "vibe") != 0) {
661 pr_err("ERROR: vibe Gpio_request is failed\n");
662 return -1;
663 }
664 gpio_direction_output(data->motor_gpio.gpio, data->vibe_off);
665 }
666
667 return 0;
668 }
669
670 /**
671 * sunxi_motor_startup() - get config info from sys_config.fex file.
672 * @motor_type:sensor type
673 * return value:
674 * = 0; success;
675 * < 0; err
676 */
sunxi_motor_startup(enum input_sensor_type * motor_type)677 static int sunxi_motor_startup(enum input_sensor_type *motor_type)
678 {
679 struct motor_config_info *data = container_of(motor_type,
680 struct motor_config_info, input_type);
681 struct device_node *np = NULL;
682 int ret = -1;
683 struct platform_device *pdev = NULL;
684 #ifdef CONFIG_SUNXI_REGULATOR_DT
685 struct device *dev = NULL;
686 #endif
687
688 np = of_find_node_by_name(NULL, "motor_para");
689 if (!np) {
690 pr_err("ERROR! get motor_para failed, func:%s, line:%d\n",
691 __func__, __LINE__);
692 return -1;
693 }
694
695 if (of_device_is_available(np))
696 data->motor_used = 1;
697 else {
698 pr_err("%s: motor_para is not used\n", __func__);
699 return -1;
700 }
701 #ifdef CONFIG_SUNXI_REGULATOR_DT
702 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
703 if (dev) {
704 char *dev_name = kzalloc(10, GFP_KERNEL);
705 strcpy(dev_name, "motor");
706 dev->init_name = dev_name;
707 dev->of_node = np;
708 data->dev = dev;
709 }
710 #endif
711
712 data->motor_gpio.gpio = of_get_named_gpio_flags(np, "motor_shake", 0,
713 (enum of_gpio_flags *)(&(data->motor_gpio)));
714 if (!gpio_is_valid(data->motor_gpio.gpio))
715 pr_err("%s: motor_shake is invalid\n", __func__);
716
717 ret = of_property_read_string(np, "motor_ldo", &data->ldo);
718 if (ret)
719 pr_err("get motor_ldo is fail, %d\n", ret);
720
721 pdev = of_find_device_by_node(np);
722 if (pdev)
723 data->dev = &pdev->dev;
724
725 if (!data->dev) {
726 pr_err("get device_node fail\n");
727 return -1;
728 }
729
730 data->motor_power_ldo = regulator_get(data->dev, "motor");
731 if (!IS_ERR(data->motor_power_ldo)) {
732 regulator_set_voltage(data->motor_power_ldo,
733 (int)(data->ldo_voltage)*1000,
734 (int)(data->ldo_voltage)*1000);
735 }
736
737 ret = of_property_read_u32(np, "motor_ldo_voltage", &data->ldo_voltage);
738 if (ret)
739 pr_err("get motor_ldo_voltage is fail, %d\n", ret);
740
741 return 0;
742 }
743
744 /******************************** MOTOR END ***********************************/
745
746
747 static int (* const sunxi_startup[])(enum input_sensor_type *input_type) = {
748 sunxi_ctp_startup,
749 sunxi_gsensor_startup,
750 sunxi_gyr_startup,
751 sunxi_ecompass_startup,
752 sunxi_ls_startup,
753 sunxi_motor_startup,
754 };
755
756 static int (*sunxi_init[])(enum input_sensor_type *input_type) = {
757 sunxi_ctp_init,
758 sunxi_gsensor_init,
759 sunxi_gyr_init,
760 sunxi_ecompass_init,
761 sunxi_ls_init,
762 sunxi_motor_init,
763 };
764
765 static void (*sunxi_free[])(enum input_sensor_type *input_type) = {
766 sunxi_ctp_free,
767 sunxi_gsensor_free,
768 sunxi_gyr_free,
769 sunxi_ecompass_free,
770 sunxi_ls_free,
771 sunxi_motor_free,
772 };
773
input_set_power_enable(enum input_sensor_type * input_type,u32 enable)774 int input_set_power_enable(enum input_sensor_type *input_type, u32 enable)
775 {
776 int ret = -1;
777 struct regulator *ldo = NULL;
778 u32 power_io = 0;
779 void *data = NULL;
780
781 switch (*input_type) {
782 case CTP_TYPE:
783 data = container_of(input_type,
784 struct ctp_config_info, input_type);
785 ldo = ((struct ctp_config_info *)data)->ctp_power_ldo;
786 power_io = ((struct ctp_config_info *)data)->ctp_power_io.gpio;
787 break;
788 case GSENSOR_TYPE:
789 data = container_of(input_type, struct sensor_config_info,
790 input_type);
791 ldo = ((struct sensor_config_info *)data)->sensor_power_ldo;
792 break;
793 case LS_TYPE:
794 break;
795 case MOTOR_TYPE:
796 data = container_of(input_type, struct motor_config_info,
797 input_type);
798 ldo = ((struct motor_config_info *)data)->motor_power_ldo;
799 default:
800 break;
801 }
802 if ((enable != 0) && (enable != 1))
803 return ret;
804
805 if (!IS_ERR_OR_NULL(ldo)) {
806 if (enable) {
807 if (regulator_enable(ldo) != 0)
808 pr_err("%s: enable ldo error!\n", __func__);
809 } else {
810 regulator_disable(ldo);
811 }
812 } else if (gpio_is_valid(power_io)) {
813 if (enable)
814 __gpio_set_value(power_io, 1);
815 else
816 __gpio_set_value(power_io, 0);
817 }
818
819 return 0;
820 }
821 EXPORT_SYMBOL(input_set_power_enable);
822 /**
823 * input_set_int_enable() - input set irq enable
824 * Input_type:sensor type
825 * enable:
826 * return value: 0 : success
827 * -EIO : i/o err.
828 */
input_set_int_enable(enum input_sensor_type * input_type,u32 enable)829 int input_set_int_enable(enum input_sensor_type *input_type, u32 enable)
830 {
831 int ret = -1;
832 u32 irq_number = 0;
833 void *data = NULL;
834
835 switch (*input_type) {
836 case CTP_TYPE:
837 data = container_of(input_type,
838 struct ctp_config_info, input_type);
839 irq_number = gpio_to_irq(((struct ctp_config_info *)data)->int_number);
840 break;
841 case GSENSOR_TYPE:
842 break;
843 case LS_TYPE:
844 data = container_of(input_type,
845 struct sensor_config_info, input_type);
846 irq_number = gpio_to_irq(((struct sensor_config_info *)data)->int_number);
847 break;
848 default:
849 break;
850 }
851
852 if ((enable != 0) && (enable != 1))
853 return ret;
854
855 if (enable == 1)
856 enable_irq(irq_number);
857 else
858 disable_irq_nosync(irq_number);
859 /*
860 if (*input_type == CTP_TYPE && enable == 0)
861 dump_stack();
862 */
863 return 0;
864 }
865 EXPORT_SYMBOL(input_set_int_enable);
866
input_set_int_enable_force(enum input_sensor_type * input_type,u32 enable)867 int input_set_int_enable_force(enum input_sensor_type *input_type, u32 enable)
868 {
869 int ret = -1;
870 u32 irq_number = 0;
871 void *data = NULL;
872 struct irq_desc *desc = NULL;
873
874 switch (*input_type) {
875 case CTP_TYPE:
876 data = container_of(input_type,
877 struct ctp_config_info, input_type);
878 irq_number = gpio_to_irq(((struct ctp_config_info *)data)->int_number);
879 break;
880 case GSENSOR_TYPE:
881 break;
882 case LS_TYPE:
883 data = container_of(input_type,
884 struct sensor_config_info, input_type);
885 irq_number = gpio_to_irq(((struct sensor_config_info *)data)->int_number);
886 break;
887 default:
888 break;
889 }
890
891 if ((enable != 0) && (enable != 1))
892 return ret;
893
894 desc = irq_to_desc(irq_number);
895 if (enable == 1) {
896 while (desc->depth >= 1)
897 enable_irq(irq_number);
898 } else {
899 disable_irq_nosync(irq_number);
900 }
901 return 0;
902 }
903 EXPORT_SYMBOL(input_set_int_enable_force);
904
905 /**
906 * input_free_int - input free irq
907 * Input_type:sensor type
908 * return value: 0 : success
909 * -EIO : i/o err.
910 */
input_free_int(enum input_sensor_type * input_type,void * para)911 int input_free_int(enum input_sensor_type *input_type, void *para)
912 {
913 int irq_number = 0;
914 void *data = NULL;
915 struct device *dev = NULL;
916
917 switch (*input_type) {
918 case CTP_TYPE:
919 data = container_of(input_type,
920 struct ctp_config_info, input_type);
921 irq_number = gpio_to_irq(((struct ctp_config_info *)data)->int_number);
922
923 dev = ((struct ctp_config_info *)data)->dev;
924 break;
925 case GSENSOR_TYPE:
926 break;
927 case LS_TYPE:
928 data = container_of(input_type,
929 struct sensor_config_info, input_type);
930 irq_number = gpio_to_irq(((struct sensor_config_info *)data)->int_number);
931
932 dev = ((struct sensor_config_info *)data)->dev;
933 break;
934 default:
935 break;
936 }
937
938 devm_free_irq(dev, irq_number, para);
939
940 return 0;
941 }
942 EXPORT_SYMBOL(input_free_int);
943
944 /**
945 * input_request_int() - input request irq
946 * Input:
947 * type:
948 * handle:
949 * trig_gype:
950 * para:
951 * return value: 0 : success
952 * -EIO : i/o err.
953 *
954 */
input_request_int(enum input_sensor_type * input_type,irq_handler_t handle,unsigned long trig_type,void * para)955 int input_request_int(enum input_sensor_type *input_type, irq_handler_t handle,
956 unsigned long trig_type, void *para)
957 {
958 int ret = -1;
959 int irq_number = 0;
960
961 void *data = NULL;
962 struct device *dev = NULL;
963
964 switch (*input_type) {
965 case CTP_TYPE:
966 data = container_of(input_type,
967 struct ctp_config_info, input_type);
968 irq_number = gpio_to_irq(((struct ctp_config_info *)data)->int_number);
969 if (irq_number < 0) {
970 pr_warn("map gpio to virq failed, errno = %d\n",
971 irq_number);
972 return -EINVAL;
973 }
974
975 dev = ((struct ctp_config_info *)data)->dev;
976 break;
977 case GSENSOR_TYPE:
978 break;
979 case LS_TYPE:
980 data = container_of(input_type,
981 struct sensor_config_info, input_type);
982 irq_number = gpio_to_irq(((struct sensor_config_info *)data)->int_number);
983 if (irq_number < 0) {
984 pr_warn("map gpio to virq failed, errno = %d\n",
985 irq_number);
986 return -EINVAL;
987 }
988
989 dev = ((struct sensor_config_info *)data)->dev;
990 break;
991 default:
992 break;
993 }
994
995 /* request virq, set virq type to high level trigger */
996 ret = devm_request_irq(dev, irq_number, handle,
997 trig_type, "PA3_EINT", para);
998 if (ret < 0) {
999 pr_warn("request virq %d failed, errno = %d\n",
1000 irq_number, ret);
1001 return -EINVAL;
1002 }
1003
1004 return 0;
1005 }
1006 EXPORT_SYMBOL(input_request_int);
1007
input_sensor_free(enum input_sensor_type * input_type)1008 void input_sensor_free(enum input_sensor_type *input_type)
1009 {
1010 (*sunxi_free[*input_type])(input_type);
1011 }
1012 EXPORT_SYMBOL(input_sensor_free);
1013
input_sensor_init(enum input_sensor_type * input_type)1014 int input_sensor_init(enum input_sensor_type *input_type)
1015 {
1016 int ret = -1;
1017
1018 ret = (*sunxi_init[*input_type])(input_type);
1019
1020 return ret;
1021 }
1022 EXPORT_SYMBOL(input_sensor_init);
1023
input_sensor_startup(enum input_sensor_type * input_type)1024 int input_sensor_startup(enum input_sensor_type *input_type)
1025 {
1026 int ret = -1;
1027
1028 ret = (*sunxi_startup[*input_type])(input_type);
1029
1030 return ret;
1031 }
1032 EXPORT_SYMBOL(input_sensor_startup);
1033
1034 MODULE_LICENSE("GPL");
1035 MODULE_DESCRIPTION("Provide function for allwinner sensor an touchscreen devices");
1036 MODULE_AUTHOR("Allwinner");
1037 MODULE_VERSION("1.0.2");
1038