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 #include <linux/device.h>
13 #include <linux/gpio.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
22 struct sh_pfc_gpio_data_reg
{
23 const struct pinmux_data_reg
*info
;
27 struct sh_pfc_gpio_pin
{
34 struct gpio_chip gpio_chip
;
36 struct sh_pfc_window
*mem
;
37 struct sh_pfc_gpio_data_reg
*regs
;
38 struct sh_pfc_gpio_pin
*pins
;
41 static struct sh_pfc
*gpio_to_pfc(struct gpio_chip
*gc
)
43 struct sh_pfc_chip
*chip
= gpiochip_get_data(gc
);
47 static void gpio_get_data_reg(struct sh_pfc_chip
*chip
, unsigned int offset
,
48 struct sh_pfc_gpio_data_reg
**reg
,
51 int idx
= sh_pfc_get_pin_index(chip
->pfc
, offset
);
52 struct sh_pfc_gpio_pin
*gpio_pin
= &chip
->pins
[idx
];
54 *reg
= &chip
->regs
[gpio_pin
->dreg
];
55 *bit
= gpio_pin
->dbit
;
58 static u32
gpio_read_data_reg(struct sh_pfc_chip
*chip
,
59 const struct pinmux_data_reg
*dreg
)
61 phys_addr_t address
= dreg
->reg
;
62 void __iomem
*mem
= address
- chip
->mem
->phys
+ chip
->mem
->virt
;
64 return sh_pfc_read_raw_reg(mem
, dreg
->reg_width
);
67 static void gpio_write_data_reg(struct sh_pfc_chip
*chip
,
68 const struct pinmux_data_reg
*dreg
, u32 value
)
70 phys_addr_t address
= dreg
->reg
;
71 void __iomem
*mem
= address
- chip
->mem
->phys
+ chip
->mem
->virt
;
73 sh_pfc_write_raw_reg(mem
, dreg
->reg_width
, value
);
76 static void gpio_setup_data_reg(struct sh_pfc_chip
*chip
, unsigned idx
)
78 struct sh_pfc
*pfc
= chip
->pfc
;
79 struct sh_pfc_gpio_pin
*gpio_pin
= &chip
->pins
[idx
];
80 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
81 const struct pinmux_data_reg
*dreg
;
85 for (i
= 0, dreg
= pfc
->info
->data_regs
; dreg
->reg_width
; ++i
, ++dreg
) {
86 for (bit
= 0; bit
< dreg
->reg_width
; bit
++) {
87 if (dreg
->enum_ids
[bit
] == pin
->enum_id
) {
98 static int gpio_setup_data_regs(struct sh_pfc_chip
*chip
)
100 struct sh_pfc
*pfc
= chip
->pfc
;
101 const struct pinmux_data_reg
*dreg
;
104 /* Count the number of data registers, allocate memory and initialize
107 for (i
= 0; pfc
->info
->data_regs
[i
].reg_width
; ++i
)
110 chip
->regs
= devm_kzalloc(pfc
->dev
, i
* sizeof(*chip
->regs
),
112 if (chip
->regs
== NULL
)
115 for (i
= 0, dreg
= pfc
->info
->data_regs
; dreg
->reg_width
; ++i
, ++dreg
) {
116 chip
->regs
[i
].info
= dreg
;
117 chip
->regs
[i
].shadow
= gpio_read_data_reg(chip
, dreg
);
120 for (i
= 0; i
< pfc
->info
->nr_pins
; i
++) {
121 if (pfc
->info
->pins
[i
].enum_id
== 0)
124 gpio_setup_data_reg(chip
, i
);
130 /* -----------------------------------------------------------------------------
134 static int gpio_pin_request(struct gpio_chip
*gc
, unsigned offset
)
136 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
137 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
139 if (idx
< 0 || pfc
->info
->pins
[idx
].enum_id
== 0)
142 return pinctrl_request_gpio(offset
);
145 static void gpio_pin_free(struct gpio_chip
*gc
, unsigned offset
)
147 return pinctrl_free_gpio(offset
);
150 static void gpio_pin_set_value(struct sh_pfc_chip
*chip
, unsigned offset
,
153 struct sh_pfc_gpio_data_reg
*reg
;
157 gpio_get_data_reg(chip
, offset
, ®
, &bit
);
159 pos
= reg
->info
->reg_width
- (bit
+ 1);
162 reg
->shadow
|= BIT(pos
);
164 reg
->shadow
&= ~BIT(pos
);
166 gpio_write_data_reg(chip
, reg
->info
, reg
->shadow
);
169 static int gpio_pin_direction_input(struct gpio_chip
*gc
, unsigned offset
)
171 return pinctrl_gpio_direction_input(offset
);
174 static int gpio_pin_direction_output(struct gpio_chip
*gc
, unsigned offset
,
177 gpio_pin_set_value(gpiochip_get_data(gc
), offset
, value
);
179 return pinctrl_gpio_direction_output(offset
);
182 static int gpio_pin_get(struct gpio_chip
*gc
, unsigned offset
)
184 struct sh_pfc_chip
*chip
= gpiochip_get_data(gc
);
185 struct sh_pfc_gpio_data_reg
*reg
;
189 gpio_get_data_reg(chip
, offset
, ®
, &bit
);
191 pos
= reg
->info
->reg_width
- (bit
+ 1);
193 return (gpio_read_data_reg(chip
, reg
->info
) >> pos
) & 1;
196 static void gpio_pin_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
198 gpio_pin_set_value(gpiochip_get_data(gc
), offset
, value
);
201 static int gpio_pin_to_irq(struct gpio_chip
*gc
, unsigned offset
)
203 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
206 for (i
= 0; i
< pfc
->info
->gpio_irq_size
; i
++) {
207 const short *gpios
= pfc
->info
->gpio_irq
[i
].gpios
;
209 for (k
= 0; gpios
[k
] >= 0; k
++) {
210 if (gpios
[k
] == offset
)
221 static int gpio_pin_setup(struct sh_pfc_chip
*chip
)
223 struct sh_pfc
*pfc
= chip
->pfc
;
224 struct gpio_chip
*gc
= &chip
->gpio_chip
;
227 chip
->pins
= devm_kzalloc(pfc
->dev
, pfc
->info
->nr_pins
*
228 sizeof(*chip
->pins
), GFP_KERNEL
);
229 if (chip
->pins
== NULL
)
232 ret
= gpio_setup_data_regs(chip
);
236 gc
->request
= gpio_pin_request
;
237 gc
->free
= gpio_pin_free
;
238 gc
->direction_input
= gpio_pin_direction_input
;
239 gc
->get
= gpio_pin_get
;
240 gc
->direction_output
= gpio_pin_direction_output
;
241 gc
->set
= gpio_pin_set
;
242 gc
->to_irq
= gpio_pin_to_irq
;
244 gc
->label
= pfc
->info
->name
;
245 gc
->parent
= pfc
->dev
;
246 gc
->owner
= THIS_MODULE
;
248 gc
->ngpio
= pfc
->nr_gpio_pins
;
253 /* -----------------------------------------------------------------------------
258 static int gpio_function_request(struct gpio_chip
*gc
, unsigned offset
)
260 static bool __print_once
;
261 struct sh_pfc
*pfc
= gpio_to_pfc(gc
);
262 unsigned int mark
= pfc
->info
->func_gpios
[offset
].enum_id
;
268 "Use of GPIO API for function requests is deprecated."
269 " Convert to pinctrl\n");
276 spin_lock_irqsave(&pfc
->lock
, flags
);
277 ret
= sh_pfc_config_mux(pfc
, mark
, PINMUX_TYPE_FUNCTION
);
278 spin_unlock_irqrestore(&pfc
->lock
, flags
);
283 static int gpio_function_setup(struct sh_pfc_chip
*chip
)
285 struct sh_pfc
*pfc
= chip
->pfc
;
286 struct gpio_chip
*gc
= &chip
->gpio_chip
;
288 gc
->request
= gpio_function_request
;
290 gc
->label
= pfc
->info
->name
;
291 gc
->owner
= THIS_MODULE
;
292 gc
->base
= pfc
->nr_gpio_pins
;
293 gc
->ngpio
= pfc
->info
->nr_func_gpios
;
299 /* -----------------------------------------------------------------------------
300 * Register/unregister
303 static struct sh_pfc_chip
*
304 sh_pfc_add_gpiochip(struct sh_pfc
*pfc
, int(*setup
)(struct sh_pfc_chip
*),
305 struct sh_pfc_window
*mem
)
307 struct sh_pfc_chip
*chip
;
310 chip
= devm_kzalloc(pfc
->dev
, sizeof(*chip
), GFP_KERNEL
);
312 return ERR_PTR(-ENOMEM
);
321 ret
= devm_gpiochip_add_data(pfc
->dev
, &chip
->gpio_chip
, chip
);
322 if (unlikely(ret
< 0))
325 dev_info(pfc
->dev
, "%s handling gpio %u -> %u\n",
326 chip
->gpio_chip
.label
, chip
->gpio_chip
.base
,
327 chip
->gpio_chip
.base
+ chip
->gpio_chip
.ngpio
- 1);
332 int sh_pfc_register_gpiochip(struct sh_pfc
*pfc
)
334 struct sh_pfc_chip
*chip
;
338 if (pfc
->info
->data_regs
== NULL
)
341 /* Find the memory window that contain the GPIO registers. Boards that
342 * register a separate GPIO device will not supply a memory resource
343 * that covers the data registers. In that case don't try to handle
346 address
= pfc
->info
->data_regs
[0].reg
;
347 for (i
= 0; i
< pfc
->num_windows
; ++i
) {
348 struct sh_pfc_window
*window
= &pfc
->windows
[i
];
350 if (address
>= window
->phys
&&
351 address
< window
->phys
+ window
->size
)
355 if (i
== pfc
->num_windows
)
358 /* If we have IRQ resources make sure their number is correct. */
359 if (pfc
->num_irqs
!= pfc
->info
->gpio_irq_size
) {
360 dev_err(pfc
->dev
, "invalid number of IRQ resources\n");
364 /* Register the real GPIOs chip. */
365 chip
= sh_pfc_add_gpiochip(pfc
, gpio_pin_setup
, &pfc
->windows
[i
]);
367 return PTR_ERR(chip
);
371 if (IS_ENABLED(CONFIG_OF
) && pfc
->dev
->of_node
)
376 * Register the GPIO to pin mappings. As pins with GPIO ports
377 * must come first in the ranges, skip the pins without GPIO
378 * ports by stopping at the first range that contains such a
381 for (i
= 0; i
< pfc
->nr_ranges
; ++i
) {
382 const struct sh_pfc_pin_range
*range
= &pfc
->ranges
[i
];
385 if (range
->start
>= pfc
->nr_gpio_pins
)
388 ret
= gpiochip_add_pin_range(&chip
->gpio_chip
,
389 dev_name(pfc
->dev
), range
->start
, range
->start
,
390 range
->end
- range
->start
+ 1);
395 /* Register the function GPIOs chip. */
396 if (pfc
->info
->nr_func_gpios
== 0)
399 chip
= sh_pfc_add_gpiochip(pfc
, gpio_function_setup
, NULL
);
401 return PTR_ERR(chip
);
402 #endif /* CONFIG_SUPERH */