@@ -24,6 +24,10 @@ static int button_irqs[] = { -1, -1 };
2424/* Define GPIOs for LEDs.
2525 * FIXME: Change the numbers for the GPIO on your board.
2626 */
27+ #ifdef NO_GPIO_REQUEST_ARRAY
28+ static unsigned int led_gpio = 4 ;
29+ static unsigned int button_gpios [] = { 17 , 18 };
30+ #else
2731static struct gpio leds [] = { { 4 , GPIOF_OUT_INIT_LOW , "LED 1" } };
2832
2933/* Define GPIOs for BUTTONS
@@ -33,6 +37,7 @@ static struct gpio buttons[] = {
3337 { 17 , GPIOF_IN , "LED 1 ON BUTTON" },
3438 { 18 , GPIOF_IN , "LED 1 OFF BUTTON" },
3539};
40+ #endif
3641
3742/* This happens immediately, when the IRQ is triggered */
3843static irqreturn_t button_top_half (int irq , void * ident )
@@ -57,7 +62,7 @@ static int __init bottomhalf_init(void)
5762
5863/* register LED gpios */
5964#ifdef NO_GPIO_REQUEST_ARRAY
60- ret = gpio_request (leds [ 0 ]. gpio , leds [ 0 ]. label );
65+ ret = gpio_request (led_gpio , "LED 1" );
6166#else
6267 ret = gpio_request_array (leds , ARRAY_SIZE (leds ));
6368#endif
@@ -67,20 +72,43 @@ static int __init bottomhalf_init(void)
6772 return ret ;
6873 }
6974
75+ #ifdef NO_GPIO_REQUEST_ARRAY
76+ ret = gpio_direction_output (led_gpio , 0 );
77+
78+ if (ret ) {
79+ pr_err ("Unable to configure LED GPIO direction: %d\n" , ret );
80+ goto fail2 ;
81+ }
82+ #endif
83+
7084/* register BUTTON gpios */
7185#ifdef NO_GPIO_REQUEST_ARRAY
72- ret = gpio_request (buttons [0 ]. gpio , buttons [ 0 ]. label );
86+ ret = gpio_request (button_gpios [0 ], "LED 1 ON BUTTON" );
7387
7488 if (ret ) {
7589 pr_err ("Unable to request GPIOs for BUTTONs: %d\n" , ret );
76- goto fail1 ;
90+ goto fail2 ;
7791 }
7892
79- ret = gpio_request (buttons [1 ]. gpio , buttons [ 1 ]. label );
93+ ret = gpio_request (button_gpios [1 ], "LED 1 OFF BUTTON" );
8094
8195 if (ret ) {
8296 pr_err ("Unable to request GPIOs for BUTTONs: %d\n" , ret );
83- goto fail2 ;
97+ goto fail3 ;
98+ }
99+
100+ ret = gpio_direction_input (button_gpios [0 ]);
101+
102+ if (ret ) {
103+ pr_err ("Unable to configure BUTTON1 GPIO direction: %d\n" , ret );
104+ goto fail4 ;
105+ }
106+
107+ ret = gpio_direction_input (button_gpios [1 ]);
108+
109+ if (ret ) {
110+ pr_err ("Unable to configure BUTTON2 GPIO direction: %d\n" , ret );
111+ goto fail4 ;
84112 }
85113#else
86114 ret = gpio_request_array (buttons , ARRAY_SIZE (buttons ));
@@ -91,14 +119,20 @@ static int __init bottomhalf_init(void)
91119 }
92120#endif
93121
122+ #ifdef NO_GPIO_REQUEST_ARRAY
123+ pr_info ("Current button1 value: %d\n" , gpio_get_value (button_gpios [0 ]));
124+
125+ ret = gpio_to_irq (button_gpios [0 ]);
126+ #else
94127 pr_info ("Current button1 value: %d\n" , gpio_get_value (buttons [0 ].gpio ));
95128
96129 ret = gpio_to_irq (buttons [0 ].gpio );
130+ #endif
97131
98132 if (ret < 0 ) {
99133 pr_err ("Unable to request IRQ: %d\n" , ret );
100134#ifdef NO_GPIO_REQUEST_ARRAY
101- goto fail3 ;
135+ goto fail4 ;
102136#else
103137 goto fail2 ;
104138#endif
@@ -111,23 +145,31 @@ static int __init bottomhalf_init(void)
111145 ret = request_threaded_irq (button_irqs [0 ], button_top_half ,
112146 button_bottom_half ,
113147 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING ,
148+ #ifdef NO_GPIO_REQUEST_ARRAY
149+ "gpiomod#button1" , NULL );
150+ #else
114151 "gpiomod#button1" , & buttons [0 ]);
152+ #endif
115153
116154 if (ret ) {
117155 pr_err ("Unable to request IRQ: %d\n" , ret );
118156#ifdef NO_GPIO_REQUEST_ARRAY
119- goto fail3 ;
157+ goto fail4 ;
120158#else
121159 goto fail2 ;
122160#endif
123161 }
124162
163+ #ifdef NO_GPIO_REQUEST_ARRAY
164+ ret = gpio_to_irq (button_gpios [1 ]);
165+ #else
125166 ret = gpio_to_irq (buttons [1 ].gpio );
167+ #endif
126168
127169 if (ret < 0 ) {
128170 pr_err ("Unable to request IRQ: %d\n" , ret );
129171#ifdef NO_GPIO_REQUEST_ARRAY
130- goto fail3 ;
172+ goto fail5 ;
131173#else
132174 goto fail2 ;
133175#endif
@@ -140,12 +182,16 @@ static int __init bottomhalf_init(void)
140182 ret = request_threaded_irq (button_irqs [1 ], button_top_half ,
141183 button_bottom_half ,
142184 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING ,
185+ #ifdef NO_GPIO_REQUEST_ARRAY
186+ "gpiomod#button2" , NULL );
187+ #else
143188 "gpiomod#button2" , & buttons [1 ]);
189+ #endif
144190
145191 if (ret ) {
146192 pr_err ("Unable to request IRQ: %d\n" , ret );
147193#ifdef NO_GPIO_REQUEST_ARRAY
148- goto fail4 ;
194+ goto fail5 ;
149195#else
150196 goto fail3 ;
151197#endif
@@ -155,17 +201,17 @@ static int __init bottomhalf_init(void)
155201
156202/* cleanup what has been setup so far */
157203#ifdef NO_GPIO_REQUEST_ARRAY
204+ fail5 :
205+ free_irq (button_irqs [0 ], NULL );
206+
158207fail4 :
159- free_irq ( button_irqs [ 0 ], & buttons [ 0 ]);
208+ gpio_free ( button_gpios [ 1 ]);
160209
161210fail3 :
162- gpio_free (buttons [ 1 ]. gpio );
211+ gpio_free (button_gpios [ 0 ] );
163212
164213fail2 :
165- gpio_free (buttons [0 ].gpio );
166-
167- fail1 :
168- gpio_free (leds [0 ].gpio );
214+ gpio_free (led_gpio );
169215#else
170216fail3 :
171217 free_irq (button_irqs [0 ], & buttons [0 ]);
@@ -185,12 +231,17 @@ static void __exit bottomhalf_exit(void)
185231 pr_info ("%s\n" , __func__ );
186232
187233 /* free irqs */
234+ #ifdef NO_GPIO_REQUEST_ARRAY
235+ free_irq (button_irqs [0 ], NULL );
236+ free_irq (button_irqs [1 ], NULL );
237+ #else
188238 free_irq (button_irqs [0 ], & buttons [0 ]);
189239 free_irq (button_irqs [1 ], & buttons [1 ]);
240+ #endif
190241
191242/* turn all LEDs off */
192243#ifdef NO_GPIO_REQUEST_ARRAY
193- gpio_set_value (leds [ 0 ]. gpio , 0 );
244+ gpio_set_value (led_gpio , 0 );
194245#else
195246 int i ;
196247 for (i = 0 ; i < ARRAY_SIZE (leds ); i ++ )
@@ -199,9 +250,9 @@ static void __exit bottomhalf_exit(void)
199250
200251/* unregister */
201252#ifdef NO_GPIO_REQUEST_ARRAY
202- gpio_free (leds [ 0 ]. gpio );
203- gpio_free (buttons [0 ]. gpio );
204- gpio_free (buttons [1 ]. gpio );
253+ gpio_free (led_gpio );
254+ gpio_free (button_gpios [0 ]);
255+ gpio_free (button_gpios [1 ]);
205256#else
206257 gpio_free_array (leds , ARRAY_SIZE (leds ));
207258 gpio_free_array (buttons , ARRAY_SIZE (buttons ));
0 commit comments