1 // SPDX-License-Identifier: GPL-2.0
3 * SuperH Pin Function Controller GPIO driver.
5 * Copyright (C) 2008 Magnus Damm
6 * Copyright (C) 2009 - 2012 Paul Mundt
9 #include <linux/device.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/module.h>
12 #include <linux/pinctrl/consumer.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
18 struct sh_pfc_gpio_data_reg
{
19 const struct pinmux_data_reg
*info
;
23 struct sh_pfc_gpio_pin
{
30 struct gpio_chip gpio_chip
;
32 struct sh_pfc_window
*mem
;
33 struct sh_pfc_gpio_data_reg
*regs
;
34 struct sh_pfc_gpio_pin
*pins
;
37 static struct sh_pfc
*gpio_to_pfc(struct gpio_chip
*gc
)
39 struct sh_pfc_chip
*chip
= gpiochip_get_data(gc
);
43 static void gpio_get_data_reg(struct sh_pfc_chip
*chip
, unsigned int offset
,
44 struct sh_pfc_gpio_data_reg
**reg
,
47 int idx
= sh_pfc_get_pin_index(chip
->pfc
, offset
);
48 struct sh_pfc_gpio_pin
*gpio_pin
= &chip
->pins
[idx
];
50 *reg
= &chip
->regs
[gpio_pin
->dreg
];
51 *bit
= gpio_pin
->dbit
;
54 static u32
gpio_read_data_reg(struct sh_pfc_chip
*chip
,
55 const struct pinmux_data_reg
*dreg
)
57 phys_addr_t address
= dreg
->reg
;
58 void __iomem
*mem
= address
- chip
->mem
->phys
+ chip
->mem
->virt
;
60 return sh_pfc_read_raw_reg(mem
, dreg
->reg_width
);
63 static void gpio_write_data_reg(struct sh_pfc_chip
*chip
,
64 const struct pinmux_data_reg
*dreg
, u32 value
)
66 phys_addr_t address
= dreg
->reg
;
67 void __iomem
*mem
= address
- chip
->mem
->phys
+ chip
->mem
->virt
;
69 sh_pfc_write_raw_reg(mem
, dreg
->reg_width
, value
);
72 static void gpio_setup_data_reg(struct sh_pfc_chip
*chip
, unsigned idx
)
74 struct sh_pfc
*pfc
= chip
->pfc
;
75 struct sh_pfc_gpio_pin
*gpio_pin
= &chip
->pins
[idx
];
76 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
77 const struct pinmux_data_reg
*dreg
;
81 for (i
= 0, dreg
= pfc
->info
->data_regs
; dreg
->reg_width
; ++i
, ++dreg
) {
82 for (bit
= 0; bit
< dreg
->reg_width
; bit
++) {
83 if (dreg
->enum_ids
[bit
] == pin
->enum_id
) {
94 static int gpio_setup_data_regs(struct sh_pfc_chip
*chip
)
96 struct sh_pfc
*pfc
= chip
->pfc
;
97 const struct pinmux_data_reg
*dreg
;
100 /* Count the number of data registers, allocate memory and initialize
103 for (i
= 0; pfc
->info
->data_regs
[i
].reg_width
; ++i
)
106 chip
->regs
= devm_kcalloc(pfc
->dev
, i
, sizeof(*chip
->regs
),
108 if (chip
->regs
== NULL
)
111 for (i
= 0, dreg
= pfc
->info
->data_regs
; dreg
->reg_width
; ++i
, ++dreg
) {
112 chip
->regs
[i
].info
= dreg
;
113 chip
->regs
[i
].shadow
= gpio_read_data_reg(chip
, dreg
);
116 for (i
= 0; i
< pfc
->info
->nr_pins
; i
++) {
117 if (pfc
->info
->pins
[i
].enum_id
== 0)
120 gpio_setup_data_reg(chip
, i
);
126 /* -----------------------------------------------------------------------------
130 static int gpio_pin_request(struct gpio_chip
*gc
, unsigned offset
)
132 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
133 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
135 if (idx
< 0 || pfc
->info
->pins
[idx
].enum_id
== 0)
138 return pinctrl_gpio_request(gc
, offset
);
141 static void gpio_pin_free(struct gpio_chip
*gc
, unsigned offset
)
143 return pinctrl_gpio_free(gc
, offset
);
146 static void gpio_pin_set_value(struct sh_pfc_chip
*chip
, unsigned offset
,
149 struct sh_pfc_gpio_data_reg
*reg
;
153 gpio_get_data_reg(chip
, offset
, ®
, &bit
);
155 pos
= reg
->info
->reg_width
- (bit
+ 1);
158 reg
->shadow
|= BIT(pos
);
160 reg
->shadow
&= ~BIT(pos
);
162 gpio_write_data_reg(chip
, reg
->info
, reg
->shadow
);
165 static int gpio_pin_direction_input(struct gpio_chip
*gc
, unsigned offset
)
167 return pinctrl_gpio_direction_input(gc
, offset
);
170 static int gpio_pin_direction_output(struct gpio_chip
*gc
, unsigned offset
,
173 gpio_pin_set_value(gpiochip_get_data(gc
), offset
, value
);
175 return pinctrl_gpio_direction_output(gc
, offset
);
178 static int gpio_pin_get(struct gpio_chip
*gc
, unsigned offset
)
180 struct sh_pfc_chip
*chip
= gpiochip_get_data(gc
);
181 struct sh_pfc_gpio_data_reg
*reg
;
185 gpio_get_data_reg(chip
, offset
, ®
, &bit
);
187 pos
= reg
->info
->reg_width
- (bit
+ 1);
189 return (gpio_read_data_reg(chip
, reg
->info
) >> pos
) & 1;
192 static void gpio_pin_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
194 gpio_pin_set_value(gpiochip_get_data(gc
), offset
, value
);
197 static int gpio_pin_to_irq(struct gpio_chip
*gc
, unsigned offset
)
199 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
202 for (i
= 0; i
< pfc
->info
->gpio_irq_size
; i
++) {
203 const short *gpios
= pfc
->info
->gpio_irq
[i
].gpios
;
205 for (k
= 0; gpios
[k
] >= 0; k
++) {
206 if (gpios
[k
] == offset
)
214 static int gpio_pin_setup(struct sh_pfc_chip
*chip
)
216 struct sh_pfc
*pfc
= chip
->pfc
;
217 struct gpio_chip
*gc
= &chip
->gpio_chip
;
220 chip
->pins
= devm_kcalloc(pfc
->dev
,
221 pfc
->info
->nr_pins
, sizeof(*chip
->pins
),
223 if (chip
->pins
== NULL
)
226 ret
= gpio_setup_data_regs(chip
);
230 gc
->request
= gpio_pin_request
;
231 gc
->free
= gpio_pin_free
;
232 gc
->direction_input
= gpio_pin_direction_input
;
233 gc
->get
= gpio_pin_get
;
234 gc
->direction_output
= gpio_pin_direction_output
;
235 gc
->set
= gpio_pin_set
;
236 gc
->to_irq
= gpio_pin_to_irq
;
238 gc
->label
= pfc
->info
->name
;
239 gc
->parent
= pfc
->dev
;
240 gc
->owner
= THIS_MODULE
;
241 gc
->base
= IS_ENABLED(CONFIG_PINCTRL_SH_FUNC_GPIO
) ? 0 : -1;
242 gc
->ngpio
= pfc
->nr_gpio_pins
;
247 /* -----------------------------------------------------------------------------
251 #ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
252 static int gpio_function_request(struct gpio_chip
*gc
, unsigned offset
)
254 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
255 unsigned int mark
= pfc
->info
->func_gpios
[offset
].enum_id
;
259 dev_notice_once(pfc
->dev
,
260 "Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
265 spin_lock_irqsave(&pfc
->lock
, flags
);
266 ret
= sh_pfc_config_mux(pfc
, mark
, PINMUX_TYPE_FUNCTION
);
267 spin_unlock_irqrestore(&pfc
->lock
, flags
);
272 static int gpio_function_setup(struct sh_pfc_chip
*chip
)
274 struct sh_pfc
*pfc
= chip
->pfc
;
275 struct gpio_chip
*gc
= &chip
->gpio_chip
;
277 gc
->request
= gpio_function_request
;
279 gc
->label
= pfc
->info
->name
;
280 gc
->owner
= THIS_MODULE
;
281 gc
->base
= pfc
->nr_gpio_pins
;
282 gc
->ngpio
= pfc
->info
->nr_func_gpios
;
286 #endif /* CONFIG_PINCTRL_SH_FUNC_GPIO */
288 /* -----------------------------------------------------------------------------
289 * Register/unregister
292 static struct sh_pfc_chip
*
293 sh_pfc_add_gpiochip(struct sh_pfc
*pfc
, int(*setup
)(struct sh_pfc_chip
*),
294 struct sh_pfc_window
*mem
)
296 struct sh_pfc_chip
*chip
;
299 chip
= devm_kzalloc(pfc
->dev
, sizeof(*chip
), GFP_KERNEL
);
301 return ERR_PTR(-ENOMEM
);
310 ret
= devm_gpiochip_add_data(pfc
->dev
, &chip
->gpio_chip
, chip
);
311 if (unlikely(ret
< 0))
314 dev_info(pfc
->dev
, "%s handling gpio %u -> %u\n",
315 chip
->gpio_chip
.label
, chip
->gpio_chip
.base
,
316 chip
->gpio_chip
.base
+ chip
->gpio_chip
.ngpio
- 1);
321 int sh_pfc_register_gpiochip(struct sh_pfc
*pfc
)
323 struct sh_pfc_chip
*chip
;
327 if (pfc
->info
->data_regs
== NULL
)
330 /* Find the memory window that contains the GPIO registers. Boards that
331 * register a separate GPIO device will not supply a memory resource
332 * that covers the data registers. In that case don't try to handle
335 address
= pfc
->info
->data_regs
[0].reg
;
336 for (i
= 0; i
< pfc
->num_windows
; ++i
) {
337 struct sh_pfc_window
*window
= &pfc
->windows
[i
];
339 if (address
>= window
->phys
&&
340 address
< window
->phys
+ window
->size
)
344 if (i
== pfc
->num_windows
)
347 /* If we have IRQ resources make sure their number is correct. */
348 if (pfc
->num_irqs
!= pfc
->info
->gpio_irq_size
) {
349 dev_err(pfc
->dev
, "invalid number of IRQ resources\n");
353 /* Register the real GPIOs chip. */
354 chip
= sh_pfc_add_gpiochip(pfc
, gpio_pin_setup
, &pfc
->windows
[i
]);
356 return PTR_ERR(chip
);
360 if (IS_ENABLED(CONFIG_OF
) && pfc
->dev
->of_node
)
363 #ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
365 * Register the GPIO to pin mappings. As pins with GPIO ports
366 * must come first in the ranges, skip the pins without GPIO
367 * ports by stopping at the first range that contains such a
370 for (i
= 0; i
< pfc
->nr_ranges
; ++i
) {
371 const struct sh_pfc_pin_range
*range
= &pfc
->ranges
[i
];
374 if (range
->start
>= pfc
->nr_gpio_pins
)
377 ret
= gpiochip_add_pin_range(&chip
->gpio_chip
,
378 dev_name(pfc
->dev
), range
->start
, range
->start
,
379 range
->end
- range
->start
+ 1);
384 /* Register the function GPIOs chip. */
385 if (pfc
->info
->nr_func_gpios
) {
386 chip
= sh_pfc_add_gpiochip(pfc
, gpio_function_setup
, NULL
);
388 return PTR_ERR(chip
);
390 #endif /* CONFIG_PINCTRL_SH_FUNC_GPIO */