137137/**
138138 * struct bu21013_ts - touch panel data structure
139139 * @client: pointer to the i2c client
140- * @wait: variable to wait_queue_head_t structure
141140 * @touch_stopped: touch stop flag
142141 * @chip: pointer to the touch panel controller
143142 * @in_dev: pointer to the input device structure
149148 */
150149struct bu21013_ts {
151150 struct i2c_client * client ;
152- wait_queue_head_t wait ;
153151 const struct bu21013_platform_device * chip ;
154152 struct input_dev * in_dev ;
155153 struct regulator * regulator ;
@@ -242,11 +240,13 @@ static irqreturn_t bu21013_gpio_irq(int irq, void *device_data)
242240 break ;
243241 }
244242
243+ if (unlikely (ts -> touch_stopped ))
244+ break ;
245+
245246 keep_polling = gpiod_get_value (ts -> int_gpiod );
246247 if (keep_polling )
247- wait_event_timeout (ts -> wait , ts -> touch_stopped ,
248- msecs_to_jiffies (2 ));
249- } while (keep_polling && !ts -> touch_stopped );
248+ usleep_range (2000 , 2500 );
249+ } while (keep_polling );
250250
251251 return IRQ_HANDLED ;
252252}
@@ -388,20 +388,6 @@ static int bu21013_init_chip(struct bu21013_ts *ts)
388388 return 0 ;
389389}
390390
391- /**
392- * bu21013_free_irq() - frees IRQ registered for touchscreen
393- * @ts: device structure pointer
394- *
395- * This function signals interrupt thread to stop processing and
396- * frees interrupt.
397- */
398- static void bu21013_free_irq (struct bu21013_ts * ts )
399- {
400- ts -> touch_stopped = true;
401- wake_up (& ts -> wait );
402- free_irq (ts -> irq , ts );
403- }
404-
405391#ifdef CONFIG_OF
406392static const struct bu21013_platform_device *
407393bu21013_parse_dt (struct device * dev )
@@ -439,6 +425,20 @@ bu21013_parse_dt(struct device *dev)
439425}
440426#endif
441427
428+ static void bu21013_power_off (void * _ts )
429+ {
430+ struct bu21013_ts * ts = _ts ;
431+
432+ regulator_disable (ts -> regulator );
433+ }
434+
435+ static void bu21013_disable_chip (void * _ts )
436+ {
437+ struct bu21013_ts * ts = _ts ;
438+
439+ gpiod_set_value (ts -> cs_gpiod , 0 );
440+ }
441+
442442static int bu21013_probe (struct i2c_client * client ,
443443 const struct i2c_device_id * id )
444444{
@@ -460,133 +460,119 @@ static int bu21013_probe(struct i2c_client *client,
460460 return PTR_ERR (pdata );
461461 }
462462
463- ts = kzalloc (sizeof (* ts ), GFP_KERNEL );
464- in_dev = input_allocate_device ();
465- if (!ts || !in_dev ) {
463+ ts = devm_kzalloc (& client -> dev , sizeof (* ts ), GFP_KERNEL );
464+ if (!ts )
465+ return - ENOMEM ;
466+
467+ ts -> chip = pdata ;
468+ ts -> client = client ;
469+
470+ in_dev = devm_input_allocate_device (& client -> dev );
471+ if (!in_dev ) {
466472 dev_err (& client -> dev , "device memory alloc failed\n" );
467- error = - ENOMEM ;
468- goto err_free_mem ;
473+ return - ENOMEM ;
469474 }
475+ ts -> in_dev = in_dev ;
470476
471- /* Named "INT" on the chip, DT binding is "touch" */
472- ts -> int_gpiod = gpiod_get (& client -> dev , "touch" , GPIOD_IN );
473- error = PTR_ERR_OR_ZERO (ts -> int_gpiod );
474- if (error ) {
475- if (error != - EPROBE_DEFER )
476- dev_err (& client -> dev , "failed to get INT GPIO\n" );
477- goto err_free_mem ;
478- }
479- gpiod_set_consumer_name (ts -> int_gpiod , "BU21013 INT" );
477+ /* register the device to input subsystem */
478+ in_dev -> name = DRIVER_TP ;
479+ in_dev -> id .bustype = BUS_I2C ;
480480
481- ts -> in_dev = in_dev ;
482- ts -> chip = pdata ;
483- ts -> client = client ;
484- ts -> irq = gpiod_to_irq (ts -> int_gpiod );
481+ __set_bit (EV_SYN , in_dev -> evbit );
482+ __set_bit (EV_KEY , in_dev -> evbit );
483+ __set_bit (EV_ABS , in_dev -> evbit );
484+
485+ input_set_abs_params (in_dev , ABS_MT_POSITION_X ,
486+ 0 , pdata -> touch_x_max , 0 , 0 );
487+ input_set_abs_params (in_dev , ABS_MT_POSITION_Y ,
488+ 0 , pdata -> touch_y_max , 0 , 0 );
489+ input_set_drvdata (in_dev , ts );
485490
486- ts -> regulator = regulator_get (& client -> dev , "avdd" );
491+ ts -> regulator = devm_regulator_get (& client -> dev , "avdd" );
487492 if (IS_ERR (ts -> regulator )) {
488493 dev_err (& client -> dev , "regulator_get failed\n" );
489- error = PTR_ERR (ts -> regulator );
490- goto err_put_int_gpio ;
494+ return PTR_ERR (ts -> regulator );
491495 }
492496
493497 error = regulator_enable (ts -> regulator );
494- if (error < 0 ) {
498+ if (error ) {
495499 dev_err (& client -> dev , "regulator enable failed\n" );
496- goto err_put_regulator ;
500+ return error ;
497501 }
498502
499- ts -> touch_stopped = false;
500- init_waitqueue_head (& ts -> wait );
503+ error = devm_add_action_or_reset (& client -> dev , bu21013_power_off , ts );
504+ if (error ) {
505+ dev_err (& client -> dev , "failed to install power off handler\n" );
506+ return error ;
507+ }
501508
502509 /* Named "CS" on the chip, DT binding is "reset" */
503- ts -> cs_gpiod = gpiod_get (& client -> dev , "reset" , GPIOD_OUT_HIGH );
510+ ts -> cs_gpiod = devm_gpiod_get (& client -> dev , "reset" , GPIOD_OUT_HIGH );
504511 error = PTR_ERR_OR_ZERO (ts -> cs_gpiod );
505512 if (error ) {
506513 if (error != - EPROBE_DEFER )
507514 dev_err (& client -> dev , "failed to get CS GPIO\n" );
508- goto err_disable_regulator ;
515+ return error ;
509516 }
510517 gpiod_set_consumer_name (ts -> cs_gpiod , "BU21013 CS" );
511518
519+ error = devm_add_action_or_reset (& client -> dev ,
520+ bu21013_disable_chip , ts );
521+ if (error ) {
522+ dev_err (& client -> dev ,
523+ "failed to install chip disable handler\n" );
524+ return error ;
525+ }
526+
527+ /* Named "INT" on the chip, DT binding is "touch" */
528+ ts -> int_gpiod = devm_gpiod_get (& client -> dev , "touch" , GPIOD_IN );
529+ error = PTR_ERR_OR_ZERO (ts -> int_gpiod );
530+ if (error ) {
531+ if (error != - EPROBE_DEFER )
532+ dev_err (& client -> dev , "failed to get INT GPIO\n" );
533+ return error ;
534+ }
535+ gpiod_set_consumer_name (ts -> int_gpiod , "BU21013 INT" );
536+
512537 /* configure the touch panel controller */
513538 error = bu21013_init_chip (ts );
514539 if (error ) {
515540 dev_err (& client -> dev , "error in bu21013 config\n" );
516- goto err_cs_disable ;
541+ return error ;
517542 }
518543
519- /* register the device to input subsystem */
520- in_dev -> name = DRIVER_TP ;
521- in_dev -> id .bustype = BUS_I2C ;
522- in_dev -> dev .parent = & client -> dev ;
523-
524- __set_bit (EV_SYN , in_dev -> evbit );
525- __set_bit (EV_KEY , in_dev -> evbit );
526- __set_bit (EV_ABS , in_dev -> evbit );
527-
528- input_set_abs_params (in_dev , ABS_MT_POSITION_X ,
529- 0 , pdata -> touch_x_max , 0 , 0 );
530- input_set_abs_params (in_dev , ABS_MT_POSITION_Y ,
531- 0 , pdata -> touch_y_max , 0 , 0 );
532- input_set_drvdata (in_dev , ts );
533-
534- error = request_threaded_irq (ts -> irq , NULL , bu21013_gpio_irq ,
535- IRQF_TRIGGER_FALLING | IRQF_SHARED |
536- IRQF_ONESHOT ,
537- DRIVER_TP , ts );
544+ ts -> irq = gpiod_to_irq (ts -> int_gpiod );
545+ error = devm_request_threaded_irq (& client -> dev , ts -> irq ,
546+ NULL , bu21013_gpio_irq ,
547+ IRQF_TRIGGER_FALLING |
548+ IRQF_SHARED |
549+ IRQF_ONESHOT ,
550+ DRIVER_TP , ts );
538551 if (error ) {
539552 dev_err (& client -> dev , "request irq %d failed\n" ,
540553 ts -> irq );
541- goto err_cs_disable ;
554+ return error ;
542555 }
543556
544557 error = input_register_device (in_dev );
545558 if (error ) {
546559 dev_err (& client -> dev , "failed to register input device\n" );
547- goto err_free_irq ;
560+ return error ;
548561 }
549562
550563 device_init_wakeup (& client -> dev , pdata -> wakeup );
551564 i2c_set_clientdata (client , ts );
552565
553566 return 0 ;
554-
555- err_free_irq :
556- bu21013_free_irq (ts );
557- err_cs_disable :
558- gpiod_set_value (ts -> cs_gpiod , 0 );
559- gpiod_put (ts -> cs_gpiod );
560- err_disable_regulator :
561- regulator_disable (ts -> regulator );
562- err_put_regulator :
563- regulator_put (ts -> regulator );
564- err_put_int_gpio :
565- gpiod_put (ts -> int_gpiod );
566- err_free_mem :
567- input_free_device (in_dev );
568- kfree (ts );
569-
570- return error ;
571567}
572568
573569static int bu21013_remove (struct i2c_client * client )
574570{
575571 struct bu21013_ts * ts = i2c_get_clientdata (client );
576572
577- bu21013_free_irq (ts );
578-
579- gpiod_set_value (ts -> cs_gpiod , 0 );
580- gpiod_put (ts -> cs_gpiod );
581-
582- input_unregister_device (ts -> in_dev );
583-
584- regulator_disable (ts -> regulator );
585- regulator_put (ts -> regulator );
586-
587- gpiod_put (ts -> int_gpiod );
588-
589- kfree (ts );
573+ /* Make sure IRQ will exit quickly even if there is contact */
574+ ts -> touch_stopped = true;
575+ /* The resources will be freed by devm */
590576
591577 return 0 ;
592578}
0 commit comments