2 * SuperH Pin Function Controller GPIO driver.
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
12 #define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
14 #include <linux/device.h>
15 #include <linux/gpio.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
24 struct sh_pfc_gpio_data_reg
{
25 const struct pinmux_data_reg
*info
;
29 struct sh_pfc_gpio_pin
{
36 struct gpio_chip gpio_chip
;
38 struct sh_pfc_window
*mem
;
39 struct sh_pfc_gpio_data_reg
*regs
;
40 struct sh_pfc_gpio_pin
*pins
;
43 static struct sh_pfc_chip
*gpio_to_pfc_chip(struct gpio_chip
*gc
)
45 return container_of(gc
, struct sh_pfc_chip
, gpio_chip
);
48 static struct sh_pfc
*gpio_to_pfc(struct gpio_chip
*gc
)
50 return gpio_to_pfc_chip(gc
)->pfc
;
53 static void gpio_get_data_reg(struct sh_pfc_chip
*chip
, unsigned int gpio
,
54 struct sh_pfc_gpio_data_reg
**reg
,
57 int idx
= sh_pfc_get_pin_index(chip
->pfc
, gpio
);
58 struct sh_pfc_gpio_pin
*gpio_pin
= &chip
->pins
[idx
];
60 *reg
= &chip
->regs
[gpio_pin
->dreg
];
61 *bit
= gpio_pin
->dbit
;
64 static unsigned long gpio_read_data_reg(struct sh_pfc_chip
*chip
,
65 const struct pinmux_data_reg
*dreg
)
67 void __iomem
*mem
= dreg
->reg
- chip
->mem
->phys
+ chip
->mem
->virt
;
69 return sh_pfc_read_raw_reg(mem
, dreg
->reg_width
);
72 static void gpio_write_data_reg(struct sh_pfc_chip
*chip
,
73 const struct pinmux_data_reg
*dreg
,
76 void __iomem
*mem
= dreg
->reg
- chip
->mem
->phys
+ chip
->mem
->virt
;
78 sh_pfc_write_raw_reg(mem
, dreg
->reg_width
, value
);
81 static void gpio_setup_data_reg(struct sh_pfc_chip
*chip
, unsigned gpio
)
83 struct sh_pfc
*pfc
= chip
->pfc
;
84 struct sh_pfc_gpio_pin
*gpio_pin
= &chip
->pins
[gpio
];
85 struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[gpio
];
86 const struct pinmux_data_reg
*dreg
;
90 for (i
= 0, dreg
= pfc
->info
->data_regs
; dreg
->reg
; ++i
, ++dreg
) {
91 for (bit
= 0; bit
< dreg
->reg_width
; bit
++) {
92 if (dreg
->enum_ids
[bit
] == pin
->enum_id
) {
103 static int gpio_setup_data_regs(struct sh_pfc_chip
*chip
)
105 struct sh_pfc
*pfc
= chip
->pfc
;
106 unsigned long addr
= pfc
->info
->data_regs
[0].reg
;
107 const struct pinmux_data_reg
*dreg
;
110 /* Find the window that contain the GPIO registers. */
111 for (i
= 0; i
< pfc
->num_windows
; ++i
) {
112 struct sh_pfc_window
*window
= &pfc
->window
[i
];
114 if (addr
>= window
->phys
&& addr
< window
->phys
+ window
->size
)
118 if (i
== pfc
->num_windows
)
121 /* GPIO data registers must be in the first memory resource. */
122 chip
->mem
= &pfc
->window
[i
];
124 /* Count the number of data registers, allocate memory and initialize
127 for (i
= 0; pfc
->info
->data_regs
[i
].reg_width
; ++i
)
130 chip
->regs
= devm_kzalloc(pfc
->dev
, i
* sizeof(*chip
->regs
),
132 if (chip
->regs
== NULL
)
135 for (i
= 0, dreg
= pfc
->info
->data_regs
; dreg
->reg_width
; ++i
, ++dreg
) {
136 chip
->regs
[i
].info
= dreg
;
137 chip
->regs
[i
].shadow
= gpio_read_data_reg(chip
, dreg
);
140 for (i
= 0; i
< pfc
->info
->nr_pins
; i
++) {
141 if (pfc
->info
->pins
[i
].enum_id
== 0)
144 gpio_setup_data_reg(chip
, i
);
150 /* -----------------------------------------------------------------------------
154 static int gpio_pin_request(struct gpio_chip
*gc
, unsigned offset
)
156 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
157 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
159 if (idx
< 0 || pfc
->info
->pins
[idx
].enum_id
== 0)
162 return pinctrl_request_gpio(offset
);
165 static void gpio_pin_free(struct gpio_chip
*gc
, unsigned offset
)
167 return pinctrl_free_gpio(offset
);
170 static void gpio_pin_set_value(struct sh_pfc_chip
*chip
, unsigned offset
,
173 struct sh_pfc_gpio_data_reg
*reg
;
177 gpio_get_data_reg(chip
, offset
, ®
, &bit
);
179 pos
= reg
->info
->reg_width
- (bit
+ 1);
182 set_bit(pos
, ®
->shadow
);
184 clear_bit(pos
, ®
->shadow
);
186 gpio_write_data_reg(chip
, reg
->info
, reg
->shadow
);
189 static int gpio_pin_direction_input(struct gpio_chip
*gc
, unsigned offset
)
191 return pinctrl_gpio_direction_input(offset
);
194 static int gpio_pin_direction_output(struct gpio_chip
*gc
, unsigned offset
,
197 gpio_pin_set_value(gpio_to_pfc_chip(gc
), offset
, value
);
199 return pinctrl_gpio_direction_output(offset
);
202 static int gpio_pin_get(struct gpio_chip
*gc
, unsigned offset
)
204 struct sh_pfc_chip
*chip
= gpio_to_pfc_chip(gc
);
205 struct sh_pfc_gpio_data_reg
*reg
;
209 gpio_get_data_reg(chip
, offset
, ®
, &bit
);
211 pos
= reg
->info
->reg_width
- (bit
+ 1);
213 return (gpio_read_data_reg(chip
, reg
->info
) >> pos
) & 1;
216 static void gpio_pin_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
218 gpio_pin_set_value(gpio_to_pfc_chip(gc
), offset
, value
);
221 static int gpio_pin_to_irq(struct gpio_chip
*gc
, unsigned offset
)
223 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
226 for (i
= 0; i
< pfc
->info
->gpio_irq_size
; i
++) {
227 unsigned short *gpios
= pfc
->info
->gpio_irq
[i
].gpios
;
229 for (k
= 0; gpios
[k
]; k
++) {
230 if (gpios
[k
] == offset
)
231 return pfc
->info
->gpio_irq
[i
].irq
;
238 static int gpio_pin_setup(struct sh_pfc_chip
*chip
)
240 struct sh_pfc
*pfc
= chip
->pfc
;
241 struct gpio_chip
*gc
= &chip
->gpio_chip
;
244 chip
->pins
= devm_kzalloc(pfc
->dev
, pfc
->nr_pins
* sizeof(*chip
->pins
),
246 if (chip
->pins
== NULL
)
249 ret
= gpio_setup_data_regs(chip
);
253 gc
->request
= gpio_pin_request
;
254 gc
->free
= gpio_pin_free
;
255 gc
->direction_input
= gpio_pin_direction_input
;
256 gc
->get
= gpio_pin_get
;
257 gc
->direction_output
= gpio_pin_direction_output
;
258 gc
->set
= gpio_pin_set
;
259 gc
->to_irq
= gpio_pin_to_irq
;
261 gc
->label
= pfc
->info
->name
;
263 gc
->owner
= THIS_MODULE
;
265 gc
->ngpio
= pfc
->nr_pins
;
270 /* -----------------------------------------------------------------------------
274 static int gpio_function_request(struct gpio_chip
*gc
, unsigned offset
)
276 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
277 unsigned int mark
= pfc
->info
->func_gpios
[offset
].enum_id
;
281 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
286 spin_lock_irqsave(&pfc
->lock
, flags
);
288 if (sh_pfc_config_mux(pfc
, mark
, PINMUX_TYPE_FUNCTION
, GPIO_CFG_DRYRUN
))
291 if (sh_pfc_config_mux(pfc
, mark
, PINMUX_TYPE_FUNCTION
, GPIO_CFG_REQ
))
297 spin_unlock_irqrestore(&pfc
->lock
, flags
);
301 static void gpio_function_free(struct gpio_chip
*gc
, unsigned offset
)
303 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
304 unsigned int mark
= pfc
->info
->func_gpios
[offset
].enum_id
;
307 spin_lock_irqsave(&pfc
->lock
, flags
);
309 sh_pfc_config_mux(pfc
, mark
, PINMUX_TYPE_FUNCTION
, GPIO_CFG_FREE
);
311 spin_unlock_irqrestore(&pfc
->lock
, flags
);
314 static int gpio_function_setup(struct sh_pfc_chip
*chip
)
316 struct sh_pfc
*pfc
= chip
->pfc
;
317 struct gpio_chip
*gc
= &chip
->gpio_chip
;
319 gc
->request
= gpio_function_request
;
320 gc
->free
= gpio_function_free
;
322 gc
->label
= pfc
->info
->name
;
323 gc
->owner
= THIS_MODULE
;
324 gc
->base
= pfc
->nr_pins
;
325 gc
->ngpio
= pfc
->info
->nr_func_gpios
;
330 /* -----------------------------------------------------------------------------
331 * Register/unregister
334 static struct sh_pfc_chip
*
335 sh_pfc_add_gpiochip(struct sh_pfc
*pfc
, int(*setup
)(struct sh_pfc_chip
*))
337 struct sh_pfc_chip
*chip
;
340 chip
= devm_kzalloc(pfc
->dev
, sizeof(*chip
), GFP_KERNEL
);
342 return ERR_PTR(-ENOMEM
);
350 ret
= gpiochip_add(&chip
->gpio_chip
);
351 if (unlikely(ret
< 0))
354 pr_info("%s handling gpio %u -> %u\n",
355 chip
->gpio_chip
.label
, chip
->gpio_chip
.base
,
356 chip
->gpio_chip
.base
+ chip
->gpio_chip
.ngpio
- 1);
361 int sh_pfc_register_gpiochip(struct sh_pfc
*pfc
)
363 const struct pinmux_range
*ranges
;
364 struct pinmux_range def_range
;
365 struct sh_pfc_chip
*chip
;
366 unsigned int nr_ranges
;
370 /* Register the real GPIOs chip. */
371 chip
= sh_pfc_add_gpiochip(pfc
, gpio_pin_setup
);
373 return PTR_ERR(chip
);
377 /* Register the GPIO to pin mappings. */
378 if (pfc
->info
->ranges
== NULL
) {
380 def_range
.end
= pfc
->info
->nr_pins
- 1;
384 ranges
= pfc
->info
->ranges
;
385 nr_ranges
= pfc
->info
->nr_ranges
;
388 for (i
= 0; i
< nr_ranges
; ++i
) {
389 const struct pinmux_range
*range
= &ranges
[i
];
391 ret
= gpiochip_add_pin_range(&chip
->gpio_chip
,
393 range
->begin
, range
->begin
,
394 range
->end
- range
->begin
+ 1);
399 /* Register the function GPIOs chip. */
400 chip
= sh_pfc_add_gpiochip(pfc
, gpio_function_setup
);
402 return PTR_ERR(chip
);
409 int sh_pfc_unregister_gpiochip(struct sh_pfc
*pfc
)
414 ret
= gpiochip_remove(&pfc
->gpio
->gpio_chip
);
415 err
= gpiochip_remove(&pfc
->func
->gpio_chip
);
417 return ret
< 0 ? ret
: err
;