blob: 7597a024fac8ef569e6e5acc316014194a5b4c14 [file] [log] [blame]
Paul Mundtb3c185a2012-06-20 17:29:04 +09001/*
2 * SuperH Pin Function Controller GPIO driver.
3 *
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
6 *
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
9 * for more details.
10 */
Paul Mundta2d3aff2012-07-11 17:21:04 +090011#define pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt
Paul Mundtb3c185a2012-06-20 17:29:04 +090012
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>
Paul Mundtca5481c62012-07-10 12:08:14 +090019#include <linux/pinctrl/consumer.h>
Rob Herring66791852012-08-28 12:54:42 -050020#include <linux/sh_pfc.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090021
22struct sh_pfc_chip {
23 struct sh_pfc *pfc;
24 struct gpio_chip gpio_chip;
25};
26
27static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
28{
29 return container_of(gc, struct sh_pfc_chip, gpio_chip);
30}
31
32static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
33{
34 return gpio_to_pfc_chip(gc)->pfc;
35}
36
37static int sh_gpio_request(struct gpio_chip *gc, unsigned offset)
38{
Paul Mundtca5481c62012-07-10 12:08:14 +090039 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090040}
41
42static void sh_gpio_free(struct gpio_chip *gc, unsigned offset)
43{
Paul Mundtca5481c62012-07-10 12:08:14 +090044 pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090045}
46
47static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value)
48{
49 struct pinmux_data_reg *dr = NULL;
50 int bit = 0;
51
52 if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
53 BUG();
54 else
55 sh_pfc_write_bit(dr, bit, value);
56}
57
Paul Mundtb3c185a2012-06-20 17:29:04 +090058static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio)
59{
60 struct pinmux_data_reg *dr = NULL;
61 int bit = 0;
62
63 if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
64 return -EINVAL;
65
66 return sh_pfc_read_bit(dr, bit);
67}
68
Paul Mundtca5481c62012-07-10 12:08:14 +090069static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
70{
71 return pinctrl_gpio_direction_input(offset);
72}
73
74static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
75 int value)
76{
77 sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
78
79 return pinctrl_gpio_direction_output(offset);
80}
81
Paul Mundtb3c185a2012-06-20 17:29:04 +090082static int sh_gpio_get(struct gpio_chip *gc, unsigned offset)
83{
84 return sh_gpio_get_value(gpio_to_pfc(gc), offset);
85}
86
87static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
88{
89 sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
90}
91
92static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
93{
94 struct sh_pfc *pfc = gpio_to_pfc(gc);
95 pinmux_enum_t enum_id;
96 pinmux_enum_t *enum_ids;
97 int i, k, pos;
98
99 pos = 0;
100 enum_id = 0;
101 while (1) {
102 pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
103 if (pos <= 0 || !enum_id)
104 break;
105
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100106 for (i = 0; i < pfc->pdata->gpio_irq_size; i++) {
107 enum_ids = pfc->pdata->gpio_irq[i].enum_ids;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900108 for (k = 0; enum_ids[k]; k++) {
109 if (enum_ids[k] == enum_id)
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100110 return pfc->pdata->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900111 }
112 }
113 }
114
115 return -ENOSYS;
116}
117
118static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip)
119{
120 struct sh_pfc *pfc = chip->pfc;
121 struct gpio_chip *gc = &chip->gpio_chip;
122
123 gc->request = sh_gpio_request;
124 gc->free = sh_gpio_free;
125 gc->direction_input = sh_gpio_direction_input;
126 gc->get = sh_gpio_get;
127 gc->direction_output = sh_gpio_direction_output;
128 gc->set = sh_gpio_set;
129 gc->to_irq = sh_gpio_to_irq;
130
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100131 WARN_ON(pfc->pdata->first_gpio != 0); /* needs testing */
Paul Mundtb3c185a2012-06-20 17:29:04 +0900132
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100133 gc->label = pfc->pdata->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900134 gc->owner = THIS_MODULE;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100135 gc->base = pfc->pdata->first_gpio;
136 gc->ngpio = (pfc->pdata->last_gpio - pfc->pdata->first_gpio) + 1;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900137}
138
139int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
140{
141 struct sh_pfc_chip *chip;
142 int ret;
143
144 chip = kzalloc(sizeof(struct sh_pfc_chip), GFP_KERNEL);
145 if (unlikely(!chip))
146 return -ENOMEM;
147
148 chip->pfc = pfc;
149
150 sh_pfc_gpio_setup(chip);
151
152 ret = gpiochip_add(&chip->gpio_chip);
153 if (unlikely(ret < 0))
154 kfree(chip);
155
156 pr_info("%s handling gpio %d -> %d\n",
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100157 pfc->pdata->name, pfc->pdata->first_gpio,
158 pfc->pdata->last_gpio);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900159
160 return ret;
161}
162EXPORT_SYMBOL_GPL(sh_pfc_register_gpiochip);
163
164static int sh_pfc_gpio_match(struct gpio_chip *gc, void *data)
165{
166 return !!strstr(gc->label, data);
167}
168
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800169static int sh_pfc_gpio_probe(struct platform_device *pdev)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900170{
171 struct sh_pfc_chip *chip;
172 struct gpio_chip *gc;
173
174 gc = gpiochip_find("_pfc", sh_pfc_gpio_match);
175 if (unlikely(!gc)) {
176 pr_err("Cant find gpio chip\n");
177 return -ENODEV;
178 }
179
180 chip = gpio_to_pfc_chip(gc);
181 platform_set_drvdata(pdev, chip);
182
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100183 pr_info("attaching to GPIO chip %s\n", chip->pfc->pdata->name);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900184
185 return 0;
186}
187
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800188static int sh_pfc_gpio_remove(struct platform_device *pdev)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900189{
190 struct sh_pfc_chip *chip = platform_get_drvdata(pdev);
191 int ret;
192
193 ret = gpiochip_remove(&chip->gpio_chip);
194 if (unlikely(ret < 0))
195 return ret;
196
197 kfree(chip);
198 return 0;
199}
200
201static struct platform_driver sh_pfc_gpio_driver = {
202 .probe = sh_pfc_gpio_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800203 .remove = sh_pfc_gpio_remove,
Paul Mundtb3c185a2012-06-20 17:29:04 +0900204 .driver = {
205 .name = KBUILD_MODNAME,
206 .owner = THIS_MODULE,
207 },
208};
209
210static struct platform_device sh_pfc_gpio_device = {
211 .name = KBUILD_MODNAME,
212 .id = -1,
213};
214
215static int __init sh_pfc_gpio_init(void)
216{
217 int rc;
218
219 rc = platform_driver_register(&sh_pfc_gpio_driver);
220 if (likely(!rc)) {
221 rc = platform_device_register(&sh_pfc_gpio_device);
222 if (unlikely(rc))
223 platform_driver_unregister(&sh_pfc_gpio_driver);
224 }
225
226 return rc;
227}
228
229static void __exit sh_pfc_gpio_exit(void)
230{
231 platform_device_unregister(&sh_pfc_gpio_device);
232 platform_driver_unregister(&sh_pfc_gpio_driver);
233}
234
235module_init(sh_pfc_gpio_init);
236module_exit(sh_pfc_gpio_exit);
237
238MODULE_AUTHOR("Magnus Damm, Paul Mundt");
239MODULE_DESCRIPTION("GPIO driver for SuperH pin function controller");
240MODULE_LICENSE("GPL v2");
241MODULE_ALIAS("platform:pfc-gpio");