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
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
14 #include <linux/gpio.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
22 struct gpio_chip gpio_chip;
25 static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
27 return container_of(gc, struct sh_pfc_chip, gpio_chip);
30 static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
32 return gpio_to_pfc_chip(gc)->pfc;
35 static int sh_gpio_request(struct gpio_chip *gc, unsigned offset)
37 struct sh_pfc *pfc = gpio_to_pfc(gc);
38 struct pinmux_data_reg *dummy;
40 int i, ret, pinmux_type;
47 spin_lock_irqsave(&pfc->lock, flags);
49 if ((pfc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
52 /* setup pin function here if no data is associated with pin */
54 if (sh_pfc_get_data_reg(pfc, offset, &dummy, &i) != 0)
55 pinmux_type = PINMUX_TYPE_FUNCTION;
57 pinmux_type = PINMUX_TYPE_GPIO;
59 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
60 if (sh_pfc_config_gpio(pfc, offset,
62 GPIO_CFG_DRYRUN) != 0)
65 if (sh_pfc_config_gpio(pfc, offset,
71 pfc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
72 pfc->gpios[offset].flags |= pinmux_type;
76 spin_unlock_irqrestore(&pfc->lock, flags);
81 static void sh_gpio_free(struct gpio_chip *gc, unsigned offset)
83 struct sh_pfc *pfc = gpio_to_pfc(gc);
90 spin_lock_irqsave(&pfc->lock, flags);
92 pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
93 sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
94 pfc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
95 pfc->gpios[offset].flags |= PINMUX_TYPE_NONE;
97 spin_unlock_irqrestore(&pfc->lock, flags);
100 static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
102 struct sh_pfc *pfc = gpio_to_pfc(gc);
106 spin_lock_irqsave(&pfc->lock, flags);
107 ret = sh_pfc_set_direction(pfc, offset, PINMUX_TYPE_INPUT);
108 spin_unlock_irqrestore(&pfc->lock, flags);
113 static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value)
115 struct pinmux_data_reg *dr = NULL;
118 if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
121 sh_pfc_write_bit(dr, bit, value);
124 static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
127 struct sh_pfc *pfc = gpio_to_pfc(gc);
131 sh_gpio_set_value(pfc, offset, value);
133 spin_lock_irqsave(&pfc->lock, flags);
134 ret = sh_pfc_set_direction(pfc, offset, PINMUX_TYPE_OUTPUT);
135 spin_unlock_irqrestore(&pfc->lock, flags);
140 static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio)
142 struct pinmux_data_reg *dr = NULL;
145 if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
148 return sh_pfc_read_bit(dr, bit);
151 static int sh_gpio_get(struct gpio_chip *gc, unsigned offset)
153 return sh_gpio_get_value(gpio_to_pfc(gc), offset);
156 static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
158 sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
161 static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
163 struct sh_pfc *pfc = gpio_to_pfc(gc);
164 pinmux_enum_t enum_id;
165 pinmux_enum_t *enum_ids;
171 pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
172 if (pos <= 0 || !enum_id)
175 for (i = 0; i < pfc->gpio_irq_size; i++) {
176 enum_ids = pfc->gpio_irq[i].enum_ids;
177 for (k = 0; enum_ids[k]; k++) {
178 if (enum_ids[k] == enum_id)
179 return pfc->gpio_irq[i].irq;
187 static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip)
189 struct sh_pfc *pfc = chip->pfc;
190 struct gpio_chip *gc = &chip->gpio_chip;
192 gc->request = sh_gpio_request;
193 gc->free = sh_gpio_free;
194 gc->direction_input = sh_gpio_direction_input;
195 gc->get = sh_gpio_get;
196 gc->direction_output = sh_gpio_direction_output;
197 gc->set = sh_gpio_set;
198 gc->to_irq = sh_gpio_to_irq;
200 WARN_ON(pfc->first_gpio != 0); /* needs testing */
202 gc->label = pfc->name;
203 gc->owner = THIS_MODULE;
204 gc->base = pfc->first_gpio;
205 gc->ngpio = (pfc->last_gpio - pfc->first_gpio) + 1;
208 int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
210 struct sh_pfc_chip *chip;
213 chip = kzalloc(sizeof(struct sh_pfc_chip), GFP_KERNEL);
219 sh_pfc_gpio_setup(chip);
221 ret = gpiochip_add(&chip->gpio_chip);
222 if (unlikely(ret < 0))
225 pr_info("%s handling gpio %d -> %d\n",
226 pfc->name, pfc->first_gpio, pfc->last_gpio);
230 EXPORT_SYMBOL_GPL(sh_pfc_register_gpiochip);
232 static int sh_pfc_gpio_match(struct gpio_chip *gc, void *data)
234 return !!strstr(gc->label, data);
237 static int __devinit sh_pfc_gpio_probe(struct platform_device *pdev)
239 struct sh_pfc_chip *chip;
240 struct gpio_chip *gc;
242 gc = gpiochip_find("_pfc", sh_pfc_gpio_match);
244 pr_err("Cant find gpio chip\n");
248 chip = gpio_to_pfc_chip(gc);
249 platform_set_drvdata(pdev, chip);
251 pr_info("attaching to GPIO chip %s\n", chip->pfc->name);
256 static int __devexit sh_pfc_gpio_remove(struct platform_device *pdev)
258 struct sh_pfc_chip *chip = platform_get_drvdata(pdev);
261 ret = gpiochip_remove(&chip->gpio_chip);
262 if (unlikely(ret < 0))
269 static struct platform_driver sh_pfc_gpio_driver = {
270 .probe = sh_pfc_gpio_probe,
271 .remove = __devexit_p(sh_pfc_gpio_remove),
273 .name = KBUILD_MODNAME,
274 .owner = THIS_MODULE,
278 static struct platform_device sh_pfc_gpio_device = {
279 .name = KBUILD_MODNAME,
283 static int __init sh_pfc_gpio_init(void)
287 rc = platform_driver_register(&sh_pfc_gpio_driver);
289 rc = platform_device_register(&sh_pfc_gpio_device);
291 platform_driver_unregister(&sh_pfc_gpio_driver);
297 static void __exit sh_pfc_gpio_exit(void)
299 platform_device_unregister(&sh_pfc_gpio_device);
300 platform_driver_unregister(&sh_pfc_gpio_driver);
303 module_init(sh_pfc_gpio_init);
304 module_exit(sh_pfc_gpio_exit);
306 MODULE_AUTHOR("Magnus Damm, Paul Mundt");
307 MODULE_DESCRIPTION("GPIO driver for SuperH pin function controller");
308 MODULE_LICENSE("GPL v2");
309 MODULE_ALIAS("platform:pfc-gpio");