44 * Based on gpio-mpc8xxx.c
55 */
66
7+ #include <linux/bitops.h>
8+ #include <linux/device.h>
9+ #include <linux/err.h>
710#include <linux/io.h>
11+ #include <linux/irq.h>
12+ #include <linux/mod_devicetable.h>
813#include <linux/module.h>
9- #include <linux/gpio/driver.h>
10- #include <linux/gpio/legacy-of-mm-gpiochip.h>
1114#include <linux/platform_device.h>
15+ #include <linux/property.h>
16+ #include <linux/spinlock.h>
17+ #include <linux/types.h>
18+
19+ #include <linux/gpio/driver.h>
1220
1321#define ALTERA_GPIO_MAX_NGPIO 32
1422#define ALTERA_GPIO_DATA 0x0
1826
1927/**
2028* struct altera_gpio_chip
21- * @mmchip : memory mapped chip structure.
29+ * @gc : GPIO chip structure.
30+ * @regs : memory mapped IO address for the controller registers.
2231* @gpio_lock : synchronization lock so that new irq/set/get requests
2332* will be blocked until the current one completes.
2433* @interrupt_trigger : specifies the hardware configured IRQ trigger type
2534* (rising, falling, both, high)
2635* @mapped_irq : kernel mapped irq number.
2736*/
2837struct altera_gpio_chip {
29- struct of_mm_gpio_chip mmchip ;
38+ struct gpio_chip gc ;
39+ void __iomem * regs ;
3040 raw_spinlock_t gpio_lock ;
3141 int interrupt_trigger ;
3242 int mapped_irq ;
3343};
3444
3545static void altera_gpio_irq_unmask (struct irq_data * d )
3646{
37- struct altera_gpio_chip * altera_gc ;
38- struct of_mm_gpio_chip * mm_gc ;
47+ struct gpio_chip * gc = irq_data_get_irq_chip_data ( d ) ;
48+ struct altera_gpio_chip * altera_gc = gpiochip_get_data ( gc ) ;
3949 unsigned long flags ;
4050 u32 intmask ;
4151
42- altera_gc = gpiochip_get_data (irq_data_get_irq_chip_data (d ));
43- mm_gc = & altera_gc -> mmchip ;
44- gpiochip_enable_irq (& mm_gc -> gc , irqd_to_hwirq (d ));
52+ gpiochip_enable_irq (gc , irqd_to_hwirq (d ));
4553
4654 raw_spin_lock_irqsave (& altera_gc -> gpio_lock , flags );
47- intmask = readl (mm_gc -> regs + ALTERA_GPIO_IRQ_MASK );
55+ intmask = readl (altera_gc -> regs + ALTERA_GPIO_IRQ_MASK );
4856 /* Set ALTERA_GPIO_IRQ_MASK bit to unmask */
4957 intmask |= BIT (irqd_to_hwirq (d ));
50- writel (intmask , mm_gc -> regs + ALTERA_GPIO_IRQ_MASK );
58+ writel (intmask , altera_gc -> regs + ALTERA_GPIO_IRQ_MASK );
5159 raw_spin_unlock_irqrestore (& altera_gc -> gpio_lock , flags );
5260}
5361
5462static void altera_gpio_irq_mask (struct irq_data * d )
5563{
56- struct altera_gpio_chip * altera_gc ;
57- struct of_mm_gpio_chip * mm_gc ;
64+ struct gpio_chip * gc = irq_data_get_irq_chip_data ( d ) ;
65+ struct altera_gpio_chip * altera_gc = gpiochip_get_data ( gc ) ;
5866 unsigned long flags ;
5967 u32 intmask ;
6068
61- altera_gc = gpiochip_get_data (irq_data_get_irq_chip_data (d ));
62- mm_gc = & altera_gc -> mmchip ;
63-
6469 raw_spin_lock_irqsave (& altera_gc -> gpio_lock , flags );
65- intmask = readl (mm_gc -> regs + ALTERA_GPIO_IRQ_MASK );
70+ intmask = readl (altera_gc -> regs + ALTERA_GPIO_IRQ_MASK );
6671 /* Clear ALTERA_GPIO_IRQ_MASK bit to mask */
6772 intmask &= ~BIT (irqd_to_hwirq (d ));
68- writel (intmask , mm_gc -> regs + ALTERA_GPIO_IRQ_MASK );
73+ writel (intmask , altera_gc -> regs + ALTERA_GPIO_IRQ_MASK );
6974 raw_spin_unlock_irqrestore (& altera_gc -> gpio_lock , flags );
70- gpiochip_disable_irq (& mm_gc -> gc , irqd_to_hwirq (d ));
75+
76+ gpiochip_disable_irq (gc , irqd_to_hwirq (d ));
7177}
7278
7379/*
@@ -77,9 +83,8 @@ static void altera_gpio_irq_mask(struct irq_data *d)
7783static int altera_gpio_irq_set_type (struct irq_data * d ,
7884 unsigned int type )
7985{
80- struct altera_gpio_chip * altera_gc ;
81-
82- altera_gc = gpiochip_get_data (irq_data_get_irq_chip_data (d ));
86+ struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
87+ struct altera_gpio_chip * altera_gc = gpiochip_get_data (gc );
8388
8489 if (type == IRQ_TYPE_NONE ) {
8590 irq_set_handler_locked (d , handle_bad_irq );
@@ -105,103 +110,86 @@ static unsigned int altera_gpio_irq_startup(struct irq_data *d)
105110
106111static int altera_gpio_get (struct gpio_chip * gc , unsigned offset )
107112{
108- struct of_mm_gpio_chip * mm_gc ;
113+ struct altera_gpio_chip * altera_gc = gpiochip_get_data ( gc ) ;
109114
110- mm_gc = to_of_mm_gpio_chip (gc );
111-
112- return !!(readl (mm_gc -> regs + ALTERA_GPIO_DATA ) & BIT (offset ));
115+ return !!(readl (altera_gc -> regs + ALTERA_GPIO_DATA ) & BIT (offset ));
113116}
114117
115118static void altera_gpio_set (struct gpio_chip * gc , unsigned offset , int value )
116119{
117- struct of_mm_gpio_chip * mm_gc ;
118- struct altera_gpio_chip * chip ;
120+ struct altera_gpio_chip * altera_gc = gpiochip_get_data (gc );
119121 unsigned long flags ;
120122 unsigned int data_reg ;
121123
122- mm_gc = to_of_mm_gpio_chip (gc );
123- chip = gpiochip_get_data (gc );
124-
125- raw_spin_lock_irqsave (& chip -> gpio_lock , flags );
126- data_reg = readl (mm_gc -> regs + ALTERA_GPIO_DATA );
124+ raw_spin_lock_irqsave (& altera_gc -> gpio_lock , flags );
125+ data_reg = readl (altera_gc -> regs + ALTERA_GPIO_DATA );
127126 if (value )
128127 data_reg |= BIT (offset );
129128 else
130129 data_reg &= ~BIT (offset );
131- writel (data_reg , mm_gc -> regs + ALTERA_GPIO_DATA );
132- raw_spin_unlock_irqrestore (& chip -> gpio_lock , flags );
130+ writel (data_reg , altera_gc -> regs + ALTERA_GPIO_DATA );
131+ raw_spin_unlock_irqrestore (& altera_gc -> gpio_lock , flags );
133132}
134133
135134static int altera_gpio_direction_input (struct gpio_chip * gc , unsigned offset )
136135{
137- struct of_mm_gpio_chip * mm_gc ;
138- struct altera_gpio_chip * chip ;
136+ struct altera_gpio_chip * altera_gc = gpiochip_get_data (gc );
139137 unsigned long flags ;
140138 unsigned int gpio_ddr ;
141139
142- mm_gc = to_of_mm_gpio_chip (gc );
143- chip = gpiochip_get_data (gc );
144-
145- raw_spin_lock_irqsave (& chip -> gpio_lock , flags );
140+ raw_spin_lock_irqsave (& altera_gc -> gpio_lock , flags );
146141 /* Set pin as input, assumes software controlled IP */
147- gpio_ddr = readl (mm_gc -> regs + ALTERA_GPIO_DIR );
142+ gpio_ddr = readl (altera_gc -> regs + ALTERA_GPIO_DIR );
148143 gpio_ddr &= ~BIT (offset );
149- writel (gpio_ddr , mm_gc -> regs + ALTERA_GPIO_DIR );
150- raw_spin_unlock_irqrestore (& chip -> gpio_lock , flags );
144+ writel (gpio_ddr , altera_gc -> regs + ALTERA_GPIO_DIR );
145+ raw_spin_unlock_irqrestore (& altera_gc -> gpio_lock , flags );
151146
152147 return 0 ;
153148}
154149
155150static int altera_gpio_direction_output (struct gpio_chip * gc ,
156151 unsigned offset , int value )
157152{
158- struct of_mm_gpio_chip * mm_gc ;
159- struct altera_gpio_chip * chip ;
153+ struct altera_gpio_chip * altera_gc = gpiochip_get_data (gc );
160154 unsigned long flags ;
161155 unsigned int data_reg , gpio_ddr ;
162156
163- mm_gc = to_of_mm_gpio_chip (gc );
164- chip = gpiochip_get_data (gc );
165-
166- raw_spin_lock_irqsave (& chip -> gpio_lock , flags );
157+ raw_spin_lock_irqsave (& altera_gc -> gpio_lock , flags );
167158 /* Sets the GPIO value */
168- data_reg = readl (mm_gc -> regs + ALTERA_GPIO_DATA );
159+ data_reg = readl (altera_gc -> regs + ALTERA_GPIO_DATA );
169160 if (value )
170161 data_reg |= BIT (offset );
171162 else
172163 data_reg &= ~BIT (offset );
173- writel (data_reg , mm_gc -> regs + ALTERA_GPIO_DATA );
164+ writel (data_reg , altera_gc -> regs + ALTERA_GPIO_DATA );
174165
175166 /* Set pin as output, assumes software controlled IP */
176- gpio_ddr = readl (mm_gc -> regs + ALTERA_GPIO_DIR );
167+ gpio_ddr = readl (altera_gc -> regs + ALTERA_GPIO_DIR );
177168 gpio_ddr |= BIT (offset );
178- writel (gpio_ddr , mm_gc -> regs + ALTERA_GPIO_DIR );
179- raw_spin_unlock_irqrestore (& chip -> gpio_lock , flags );
169+ writel (gpio_ddr , altera_gc -> regs + ALTERA_GPIO_DIR );
170+ raw_spin_unlock_irqrestore (& altera_gc -> gpio_lock , flags );
180171
181172 return 0 ;
182173}
183174
184175static void altera_gpio_irq_edge_handler (struct irq_desc * desc )
185176{
186- struct altera_gpio_chip * altera_gc ;
177+ struct gpio_chip * gc = irq_desc_get_handler_data (desc );
178+ struct altera_gpio_chip * altera_gc = gpiochip_get_data (gc );
179+ struct irq_domain * irqdomain = gc -> irq .domain ;
187180 struct irq_chip * chip ;
188- struct of_mm_gpio_chip * mm_gc ;
189- struct irq_domain * irqdomain ;
190181 unsigned long status ;
191182 int i ;
192183
193- altera_gc = gpiochip_get_data (irq_desc_get_handler_data (desc ));
194184 chip = irq_desc_get_chip (desc );
195- mm_gc = & altera_gc -> mmchip ;
196- irqdomain = altera_gc -> mmchip .gc .irq .domain ;
197185
198186 chained_irq_enter (chip , desc );
199187
200188 while ((status =
201- (readl (mm_gc -> regs + ALTERA_GPIO_EDGE_CAP ) &
202- readl (mm_gc -> regs + ALTERA_GPIO_IRQ_MASK )))) {
203- writel (status , mm_gc -> regs + ALTERA_GPIO_EDGE_CAP );
204- for_each_set_bit (i , & status , mm_gc -> gc . ngpio )
189+ (readl (altera_gc -> regs + ALTERA_GPIO_EDGE_CAP ) &
190+ readl (altera_gc -> regs + ALTERA_GPIO_IRQ_MASK )))) {
191+ writel (status , altera_gc -> regs + ALTERA_GPIO_EDGE_CAP );
192+ for_each_set_bit (i , & status , gc -> ngpio )
205193 generic_handle_domain_irq (irqdomain , i );
206194 }
207195
@@ -210,24 +198,21 @@ static void altera_gpio_irq_edge_handler(struct irq_desc *desc)
210198
211199static void altera_gpio_irq_leveL_high_handler (struct irq_desc * desc )
212200{
213- struct altera_gpio_chip * altera_gc ;
201+ struct gpio_chip * gc = irq_desc_get_handler_data (desc );
202+ struct altera_gpio_chip * altera_gc = gpiochip_get_data (gc );
203+ struct irq_domain * irqdomain = gc -> irq .domain ;
214204 struct irq_chip * chip ;
215- struct of_mm_gpio_chip * mm_gc ;
216- struct irq_domain * irqdomain ;
217205 unsigned long status ;
218206 int i ;
219207
220- altera_gc = gpiochip_get_data (irq_desc_get_handler_data (desc ));
221208 chip = irq_desc_get_chip (desc );
222- mm_gc = & altera_gc -> mmchip ;
223- irqdomain = altera_gc -> mmchip .gc .irq .domain ;
224209
225210 chained_irq_enter (chip , desc );
226211
227- status = readl (mm_gc -> regs + ALTERA_GPIO_DATA );
228- status &= readl (mm_gc -> regs + ALTERA_GPIO_IRQ_MASK );
212+ status = readl (altera_gc -> regs + ALTERA_GPIO_DATA );
213+ status &= readl (altera_gc -> regs + ALTERA_GPIO_IRQ_MASK );
229214
230- for_each_set_bit (i , & status , mm_gc -> gc . ngpio )
215+ for_each_set_bit (i , & status , gc -> ngpio )
231216 generic_handle_domain_irq (irqdomain , i );
232217
233218 chained_irq_exit (chip , desc );
@@ -246,7 +231,7 @@ static const struct irq_chip altera_gpio_irq_chip = {
246231
247232static int altera_gpio_probe (struct platform_device * pdev )
248233{
249- struct device_node * node = pdev -> dev . of_node ;
234+ struct device * dev = & pdev -> dev ;
250235 int reg , ret ;
251236 struct altera_gpio_chip * altera_gc ;
252237 struct gpio_irq_chip * girq ;
@@ -257,39 +242,42 @@ static int altera_gpio_probe(struct platform_device *pdev)
257242
258243 raw_spin_lock_init (& altera_gc -> gpio_lock );
259244
260- if (of_property_read_u32 ( node , "altr,ngpio" , & reg ))
245+ if (device_property_read_u32 ( dev , "altr,ngpio" , & reg ))
261246 /* By default assume maximum ngpio */
262- altera_gc -> mmchip . gc .ngpio = ALTERA_GPIO_MAX_NGPIO ;
247+ altera_gc -> gc .ngpio = ALTERA_GPIO_MAX_NGPIO ;
263248 else
264- altera_gc -> mmchip . gc .ngpio = reg ;
249+ altera_gc -> gc .ngpio = reg ;
265250
266- if (altera_gc -> mmchip . gc .ngpio > ALTERA_GPIO_MAX_NGPIO ) {
251+ if (altera_gc -> gc .ngpio > ALTERA_GPIO_MAX_NGPIO ) {
267252 dev_warn (& pdev -> dev ,
268253 "ngpio is greater than %d, defaulting to %d\n" ,
269254 ALTERA_GPIO_MAX_NGPIO , ALTERA_GPIO_MAX_NGPIO );
270- altera_gc -> mmchip . gc .ngpio = ALTERA_GPIO_MAX_NGPIO ;
255+ altera_gc -> gc .ngpio = ALTERA_GPIO_MAX_NGPIO ;
271256 }
272257
273- altera_gc -> mmchip . gc .direction_input = altera_gpio_direction_input ;
274- altera_gc -> mmchip . gc .direction_output = altera_gpio_direction_output ;
275- altera_gc -> mmchip . gc .get = altera_gpio_get ;
276- altera_gc -> mmchip . gc .set = altera_gpio_set ;
277- altera_gc -> mmchip . gc .owner = THIS_MODULE ;
278- altera_gc -> mmchip . gc .parent = & pdev -> dev ;
258+ altera_gc -> gc .direction_input = altera_gpio_direction_input ;
259+ altera_gc -> gc .direction_output = altera_gpio_direction_output ;
260+ altera_gc -> gc .get = altera_gpio_get ;
261+ altera_gc -> gc .set = altera_gpio_set ;
262+ altera_gc -> gc .owner = THIS_MODULE ;
263+ altera_gc -> gc .parent = & pdev -> dev ;
279264
280- altera_gc -> mapped_irq = platform_get_irq_optional (pdev , 0 );
265+ altera_gc -> regs = devm_platform_ioremap_resource (pdev , 0 );
266+ if (IS_ERR (altera_gc -> regs ))
267+ return dev_err_probe (dev , PTR_ERR (altera_gc -> regs ), "failed to ioremap memory resource\n" );
281268
269+ altera_gc -> mapped_irq = platform_get_irq_optional (pdev , 0 );
282270 if (altera_gc -> mapped_irq < 0 )
283271 goto skip_irq ;
284272
285- if (of_property_read_u32 ( node , "altr,interrupt-type" , & reg )) {
273+ if (device_property_read_u32 ( dev , "altr,interrupt-type" , & reg )) {
286274 dev_err (& pdev -> dev ,
287275 "altr,interrupt-type value not set in device tree\n" );
288276 return - EINVAL ;
289277 }
290278 altera_gc -> interrupt_trigger = reg ;
291279
292- girq = & altera_gc -> mmchip . gc .irq ;
280+ girq = & altera_gc -> gc .irq ;
293281 gpio_irq_chip_set_chip (girq , & altera_gpio_irq_chip );
294282
295283 if (altera_gc -> interrupt_trigger == IRQ_TYPE_LEVEL_HIGH )
@@ -306,24 +294,15 @@ static int altera_gpio_probe(struct platform_device *pdev)
306294 girq -> parents [0 ] = altera_gc -> mapped_irq ;
307295
308296skip_irq :
309- ret = of_mm_gpiochip_add_data ( node , & altera_gc -> mmchip , altera_gc );
297+ ret = devm_gpiochip_add_data ( dev , & altera_gc -> gc , altera_gc );
310298 if (ret ) {
311299 dev_err (& pdev -> dev , "Failed adding memory mapped gpiochip\n" );
312300 return ret ;
313301 }
314302
315- platform_set_drvdata (pdev , altera_gc );
316-
317303 return 0 ;
318304}
319305
320- static void altera_gpio_remove (struct platform_device * pdev )
321- {
322- struct altera_gpio_chip * altera_gc = platform_get_drvdata (pdev );
323-
324- of_mm_gpiochip_remove (& altera_gc -> mmchip );
325- }
326-
327306static const struct of_device_id altera_gpio_of_match [] = {
328307 { .compatible = "altr,pio-1.0" , },
329308 {},
@@ -336,7 +315,6 @@ static struct platform_driver altera_gpio_driver = {
336315 .of_match_table = altera_gpio_of_match ,
337316 },
338317 .probe = altera_gpio_probe ,
339- .remove = altera_gpio_remove ,
340318};
341319
342320static int __init altera_gpio_init (void )
0 commit comments