2424#define MAX_NR_PIO_DEVICES 8
2525
2626struct pio_device {
27+ struct gpio_chip chip ;
2728 void __iomem * regs ;
2829 const struct platform_device * pdev ;
2930 struct clk * clk ;
3031 u32 pinmux_mask ;
31- u32 gpio_mask ;
3232 char name [8 ];
3333};
3434
@@ -64,7 +64,8 @@ void __init at32_select_periph(unsigned int pin, unsigned int periph,
6464 goto fail ;
6565 }
6666
67- if (unlikely (test_and_set_bit (pin_index , & pio -> pinmux_mask ))) {
67+ if (unlikely (test_and_set_bit (pin_index , & pio -> pinmux_mask )
68+ || gpiochip_is_requested (& pio -> chip , pin_index ))) {
6869 printk ("%s: pin %u is busy\n" , pio -> name , pin_index );
6970 goto fail ;
7071 }
@@ -79,9 +80,6 @@ void __init at32_select_periph(unsigned int pin, unsigned int periph,
7980 if (!(flags & AT32_GPIOF_PULLUP ))
8081 pio_writel (pio , PUDR , mask );
8182
82- /* gpio_request NOT allowed */
83- set_bit (pin_index , & pio -> gpio_mask );
84-
8583 return ;
8684
8785fail :
@@ -130,9 +128,6 @@ void __init at32_select_gpio(unsigned int pin, unsigned long flags)
130128
131129 pio_writel (pio , PER , mask );
132130
133- /* gpio_request now allowed */
134- clear_bit (pin_index , & pio -> gpio_mask );
135-
136131 return ;
137132
138133fail :
@@ -166,96 +161,50 @@ void __init at32_reserve_pin(unsigned int pin)
166161
167162/* GPIO API */
168163
169- int gpio_request ( unsigned int gpio , const char * label )
164+ static int direction_input ( struct gpio_chip * chip , unsigned offset )
170165{
171- struct pio_device * pio ;
172- unsigned int pin ;
173-
174- pio = gpio_to_pio (gpio );
175- if (!pio )
176- return - ENODEV ;
166+ struct pio_device * pio = container_of (chip , struct pio_device , chip );
167+ u32 mask = 1 << offset ;
177168
178- pin = gpio & 0x1f ;
179- if (test_and_set_bit (pin , & pio -> gpio_mask ))
180- return - EBUSY ;
169+ if (!(pio_readl (pio , PSR ) & mask ))
170+ return - EINVAL ;
181171
172+ pio_writel (pio , ODR , mask );
182173 return 0 ;
183174}
184- EXPORT_SYMBOL (gpio_request );
185175
186- void gpio_free ( unsigned int gpio )
176+ static int gpio_get ( struct gpio_chip * chip , unsigned offset )
187177{
188- struct pio_device * pio ;
189- unsigned int pin ;
178+ struct pio_device * pio = container_of (chip , struct pio_device , chip );
190179
191- pio = gpio_to_pio (gpio );
192- if (!pio ) {
193- printk (KERN_ERR
194- "gpio: attempted to free invalid pin %u\n" , gpio );
195- return ;
196- }
197-
198- pin = gpio & 0x1f ;
199- if (!test_and_clear_bit (pin , & pio -> gpio_mask ))
200- printk (KERN_ERR "gpio: freeing free or non-gpio pin %s-%u\n" ,
201- pio -> name , pin );
180+ return (pio_readl (pio , PDSR ) >> offset ) & 1 ;
202181}
203- EXPORT_SYMBOL (gpio_free );
204182
205- int gpio_direction_input (unsigned int gpio )
206- {
207- struct pio_device * pio ;
208- unsigned int pin ;
209-
210- pio = gpio_to_pio (gpio );
211- if (!pio )
212- return - ENODEV ;
213-
214- pin = gpio & 0x1f ;
215- pio_writel (pio , ODR , 1 << pin );
216-
217- return 0 ;
218- }
219- EXPORT_SYMBOL (gpio_direction_input );
183+ static void gpio_set (struct gpio_chip * chip , unsigned offset , int value );
220184
221- int gpio_direction_output ( unsigned int gpio , int value )
185+ static int direction_output ( struct gpio_chip * chip , unsigned offset , int value )
222186{
223- struct pio_device * pio ;
224- unsigned int pin ;
225-
226- pio = gpio_to_pio (gpio );
227- if (!pio )
228- return - ENODEV ;
187+ struct pio_device * pio = container_of (chip , struct pio_device , chip );
188+ u32 mask = 1 << offset ;
229189
230- gpio_set_value (gpio , value );
231-
232- pin = gpio & 0x1f ;
233- pio_writel (pio , OER , 1 << pin );
190+ if (!(pio_readl (pio , PSR ) & mask ))
191+ return - EINVAL ;
234192
193+ gpio_set (chip , offset , value );
194+ pio_writel (pio , OER , mask );
235195 return 0 ;
236196}
237- EXPORT_SYMBOL (gpio_direction_output );
238197
239- int gpio_get_value ( unsigned int gpio )
198+ static void gpio_set ( struct gpio_chip * chip , unsigned offset , int value )
240199{
241- struct pio_device * pio = & pio_dev [gpio >> 5 ];
200+ struct pio_device * pio = container_of (chip , struct pio_device , chip );
201+ u32 mask = 1 << offset ;
242202
243- return (pio_readl (pio , PDSR ) >> (gpio & 0x1f )) & 1 ;
244- }
245- EXPORT_SYMBOL (gpio_get_value );
246-
247- void gpio_set_value (unsigned int gpio , int value )
248- {
249- struct pio_device * pio = & pio_dev [gpio >> 5 ];
250- u32 mask ;
251-
252- mask = 1 << (gpio & 0x1f );
253203 if (value )
254204 pio_writel (pio , SODR , mask );
255205 else
256206 pio_writel (pio , CODR , mask );
257207}
258- EXPORT_SYMBOL (gpio_set_value );
259208
260209/*--------------------------------------------------------------------------*/
261210
@@ -337,6 +286,63 @@ gpio_irq_setup(struct pio_device *pio, int irq, int gpio_irq)
337286 set_irq_chained_handler (irq , gpio_irq_handler );
338287}
339288
289+ /*--------------------------------------------------------------------------*/
290+
291+ #ifdef CONFIG_DEBUG_FS
292+
293+ #include <linux/seq_file.h>
294+
295+ /*
296+ * This shows more info than the generic gpio dump code:
297+ * pullups, deglitching, open drain drive.
298+ */
299+ static void pio_bank_show (struct seq_file * s , struct gpio_chip * chip )
300+ {
301+ struct pio_device * pio = container_of (chip , struct pio_device , chip );
302+ u32 psr , osr , imr , pdsr , pusr , ifsr , mdsr ;
303+ unsigned i ;
304+ u32 mask ;
305+ char bank ;
306+
307+ psr = pio_readl (pio , PSR );
308+ osr = pio_readl (pio , OSR );
309+ imr = pio_readl (pio , IMR );
310+ pdsr = pio_readl (pio , PDSR );
311+ pusr = pio_readl (pio , PUSR );
312+ ifsr = pio_readl (pio , IFSR );
313+ mdsr = pio_readl (pio , MDSR );
314+
315+ bank = 'A' + pio -> pdev -> id ;
316+
317+ for (i = 0 , mask = 1 ; i < 32 ; i ++ , mask <<= 1 ) {
318+ const char * label ;
319+
320+ label = gpiochip_is_requested (chip , i );
321+ if (!label )
322+ continue ;
323+
324+ seq_printf (s , " gpio-%-3d P%c%-2d (%-12s) %s %s %s" ,
325+ chip -> base + i , bank , i ,
326+ label ,
327+ (osr & mask ) ? "out" : "in " ,
328+ (mask & pdsr ) ? "hi" : "lo" ,
329+ (mask & pusr ) ? " " : "up" );
330+ if (ifsr & mask )
331+ seq_printf (s , " deglitch" );
332+ if ((osr & mdsr ) & mask )
333+ seq_printf (s , " open-drain" );
334+ if (imr & mask )
335+ seq_printf (s , " irq-%d edge-both" ,
336+ gpio_to_irq (chip -> base + i ));
337+ seq_printf (s , "\n" );
338+ }
339+ }
340+
341+ #else
342+ #define pio_bank_show NULL
343+ #endif
344+
345+
340346/*--------------------------------------------------------------------------*/
341347
342348static int __init pio_probe (struct platform_device * pdev )
@@ -349,6 +355,18 @@ static int __init pio_probe(struct platform_device *pdev)
349355 pio = & pio_dev [pdev -> id ];
350356 BUG_ON (!pio -> regs );
351357
358+ pio -> chip .label = pio -> name ;
359+ pio -> chip .base = pdev -> id * 32 ;
360+ pio -> chip .ngpio = 32 ;
361+
362+ pio -> chip .direction_input = direction_input ;
363+ pio -> chip .get = gpio_get ;
364+ pio -> chip .direction_output = direction_output ;
365+ pio -> chip .set = gpio_set ;
366+ pio -> chip .dbg_show = pio_bank_show ;
367+
368+ gpiochip_add (& pio -> chip );
369+
352370 gpio_irq_setup (pio , irq , gpio_irq_base );
353371
354372 platform_set_drvdata (pdev , pio );
@@ -406,12 +424,6 @@ void __init at32_init_pio(struct platform_device *pdev)
406424 pio -> pdev = pdev ;
407425 pio -> regs = ioremap (regs -> start , regs -> end - regs -> start + 1 );
408426
409- /*
410- * request_gpio() is only valid for pins that have been
411- * explicitly configured as GPIO and not previously requested
412- */
413- pio -> gpio_mask = ~0UL ;
414-
415427 /* start with irqs disabled and acked */
416428 pio_writel (pio , IDR , ~0UL );
417429 (void ) pio_readl (pio , ISR );
0 commit comments