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>
26 struct gpio_chip gpio_chip;
29 static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
31 return container_of(gc, struct sh_pfc_chip, gpio_chip);
34 static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
36 return gpio_to_pfc_chip(gc)->pfc;
39 static int sh_gpio_request(struct gpio_chip *gc, unsigned offset)
41 return pinctrl_request_gpio(offset);
44 static void sh_gpio_free(struct gpio_chip *gc, unsigned offset)
46 pinctrl_free_gpio(offset);
49 static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value)
51 struct pinmux_data_reg *dr = NULL;
54 if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
57 sh_pfc_write_bit(dr, bit, value);
60 static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio)
62 struct pinmux_data_reg *dr = NULL;
65 if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
68 return sh_pfc_read_bit(dr, bit);
71 static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
73 return pinctrl_gpio_direction_input(offset);
76 static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
79 sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
81 return pinctrl_gpio_direction_output(offset);
84 static int sh_gpio_get(struct gpio_chip *gc, unsigned offset)
86 return sh_gpio_get_value(gpio_to_pfc(gc), offset);
89 static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
91 sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
94 static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
96 struct sh_pfc *pfc = gpio_to_pfc(gc);
97 pinmux_enum_t enum_id;
98 pinmux_enum_t *enum_ids;
104 pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
105 if (pos <= 0 || !enum_id)
108 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
109 enum_ids = pfc->info->gpio_irq[i].enum_ids;
110 for (k = 0; enum_ids[k]; k++) {
111 if (enum_ids[k] == enum_id)
112 return pfc->info->gpio_irq[i].irq;
120 static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip)
122 struct sh_pfc *pfc = chip->pfc;
123 struct gpio_chip *gc = &chip->gpio_chip;
125 gc->request = sh_gpio_request;
126 gc->free = sh_gpio_free;
127 gc->direction_input = sh_gpio_direction_input;
128 gc->get = sh_gpio_get;
129 gc->direction_output = sh_gpio_direction_output;
130 gc->set = sh_gpio_set;
131 gc->to_irq = sh_gpio_to_irq;
133 WARN_ON(pfc->info->first_gpio != 0); /* needs testing */
135 gc->label = pfc->info->name;
136 gc->owner = THIS_MODULE;
137 gc->base = pfc->info->first_gpio;
138 gc->ngpio = (pfc->info->last_gpio - pfc->info->first_gpio) + 1;
141 int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
143 struct sh_pfc_chip *chip;
146 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
152 sh_pfc_gpio_setup(chip);
154 ret = gpiochip_add(&chip->gpio_chip);
155 if (unlikely(ret < 0))
160 pr_info("%s handling gpio %d -> %d\n",
161 pfc->info->name, pfc->info->first_gpio,
162 pfc->info->last_gpio);
167 int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
169 struct sh_pfc_chip *chip = pfc->gpio;
172 ret = gpiochip_remove(&chip->gpio_chip);
173 if (unlikely(ret < 0))