blob: 806e2dd62137211df55c9e868988cb2a4a45d9ed [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 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010011
12#define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
Paul Mundtb3c185a2012-06-20 17:29:04 +090013
Laurent Pinchart1724acf2012-12-15 23:50:48 +010014#include <linux/device.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090015#include <linux/gpio.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010016#include <linux/init.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090017#include <linux/module.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090018#include <linux/pinctrl/consumer.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010019#include <linux/slab.h>
20#include <linux/spinlock.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090021
Laurent Pinchartf9165132012-12-15 23:50:44 +010022#include "core.h"
23
Paul Mundtb3c185a2012-06-20 17:29:04 +090024struct sh_pfc_chip {
25 struct sh_pfc *pfc;
26 struct gpio_chip gpio_chip;
27};
28
29static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
30{
31 return container_of(gc, struct sh_pfc_chip, gpio_chip);
32}
33
34static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
35{
36 return gpio_to_pfc_chip(gc)->pfc;
37}
38
Laurent Pinchart16883812012-12-06 14:49:25 +010039/* -----------------------------------------------------------------------------
40 * Pin GPIOs
41 */
42
43static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090044{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010045 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart934cb022013-02-14 22:35:09 +010046 struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010047
Laurent Pinchart63d57382013-02-15 01:33:38 +010048 if (pin == NULL || pin->enum_id == 0)
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010049 return -EINVAL;
50
Laurent Pinchart16883812012-12-06 14:49:25 +010051 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090052}
53
Laurent Pinchart16883812012-12-06 14:49:25 +010054static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090055{
Laurent Pinchart16883812012-12-06 14:49:25 +010056 return pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090057}
58
Laurent Pinchart16883812012-12-06 14:49:25 +010059static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +090060{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010061 struct pinmux_data_reg *dr;
62 int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +090063
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010064 sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
Laurent Pinchart16883812012-12-06 14:49:25 +010065 sh_pfc_write_bit(dr, bit, value);
Paul Mundtb3c185a2012-06-20 17:29:04 +090066}
67
Laurent Pinchart16883812012-12-06 14:49:25 +010068static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090069{
Laurent Pinchart16883812012-12-06 14:49:25 +010070 return pinctrl_gpio_direction_input(offset);
71}
72
73static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
74 int value)
75{
76 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
77
78 return pinctrl_gpio_direction_output(offset);
79}
80
81static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
82{
83 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010084 struct pinmux_data_reg *dr;
85 int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +090086
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010087 sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
Paul Mundtb3c185a2012-06-20 17:29:04 +090088 return sh_pfc_read_bit(dr, bit);
89}
90
Laurent Pinchart16883812012-12-06 14:49:25 +010091static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +090092{
Laurent Pinchart16883812012-12-06 14:49:25 +010093 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +090094}
95
Laurent Pinchart16883812012-12-06 14:49:25 +010096static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090097{
98 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchartc07f54f2013-01-03 14:12:14 +010099 int i, k;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900100
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100101 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
102 unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900103
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100104 for (k = 0; gpios[k]; k++) {
105 if (gpios[k] == offset)
106 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900107 }
108 }
109
110 return -ENOSYS;
111}
112
Laurent Pinchart16883812012-12-06 14:49:25 +0100113static void gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900114{
115 struct sh_pfc *pfc = chip->pfc;
116 struct gpio_chip *gc = &chip->gpio_chip;
117
Laurent Pinchart16883812012-12-06 14:49:25 +0100118 gc->request = gpio_pin_request;
119 gc->free = gpio_pin_free;
120 gc->direction_input = gpio_pin_direction_input;
121 gc->get = gpio_pin_get;
122 gc->direction_output = gpio_pin_direction_output;
123 gc->set = gpio_pin_set;
124 gc->to_irq = gpio_pin_to_irq;
125
126 gc->label = pfc->info->name;
127 gc->dev = pfc->dev;
128 gc->owner = THIS_MODULE;
129 gc->base = 0;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100130 gc->ngpio = pfc->nr_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100131}
132
133/* -----------------------------------------------------------------------------
134 * Function GPIOs
135 */
136
137static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
138{
139 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100140 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100141 unsigned long flags;
142 int ret = -EINVAL;
143
144 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
145
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100146 if (mark == 0)
Laurent Pinchart16883812012-12-06 14:49:25 +0100147 return ret;
148
149 spin_lock_irqsave(&pfc->lock, flags);
150
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100151 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
Laurent Pinchart16883812012-12-06 14:49:25 +0100152 goto done;
153
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100154 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
Laurent Pinchart16883812012-12-06 14:49:25 +0100155 goto done;
156
157 ret = 0;
158
159done:
160 spin_unlock_irqrestore(&pfc->lock, flags);
161 return ret;
162}
163
164static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
165{
166 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100167 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100168 unsigned long flags;
169
170 spin_lock_irqsave(&pfc->lock, flags);
171
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100172 sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
Laurent Pinchart16883812012-12-06 14:49:25 +0100173
174 spin_unlock_irqrestore(&pfc->lock, flags);
175}
176
177static void gpio_function_setup(struct sh_pfc_chip *chip)
178{
179 struct sh_pfc *pfc = chip->pfc;
180 struct gpio_chip *gc = &chip->gpio_chip;
181
182 gc->request = gpio_function_request;
183 gc->free = gpio_function_free;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900184
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100185 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900186 gc->owner = THIS_MODULE;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100187 gc->base = pfc->nr_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100188 gc->ngpio = pfc->info->nr_func_gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900189}
190
Laurent Pinchart16883812012-12-06 14:49:25 +0100191/* -----------------------------------------------------------------------------
192 * Register/unregister
193 */
194
195static struct sh_pfc_chip *
196sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
Paul Mundtb3c185a2012-06-20 17:29:04 +0900197{
198 struct sh_pfc_chip *chip;
199 int ret;
200
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100201 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900202 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100203 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900204
205 chip->pfc = pfc;
206
Laurent Pinchart16883812012-12-06 14:49:25 +0100207 setup(chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900208
209 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100210 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100211 return ERR_PTR(ret);
212
213 pr_info("%s handling gpio %u -> %u\n",
214 chip->gpio_chip.label, chip->gpio_chip.base,
215 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
216
217 return chip;
218}
219
220int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
221{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100222 const struct pinmux_range *ranges;
223 struct pinmux_range def_range;
Laurent Pinchart16883812012-12-06 14:49:25 +0100224 struct sh_pfc_chip *chip;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100225 unsigned int nr_ranges;
226 unsigned int i;
Laurent Pinchart247127f2013-03-08 00:45:12 +0100227 int ret;
Laurent Pinchart16883812012-12-06 14:49:25 +0100228
Laurent Pinchart63d57382013-02-15 01:33:38 +0100229 /* Register the real GPIOs chip. */
Laurent Pinchart16883812012-12-06 14:49:25 +0100230 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
231 if (IS_ERR(chip))
232 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100233
234 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900235
Laurent Pinchart63d57382013-02-15 01:33:38 +0100236 /* Register the GPIO to pin mappings. */
237 if (pfc->info->ranges == NULL) {
238 def_range.begin = 0;
239 def_range.end = pfc->info->nr_pins - 1;
240 ranges = &def_range;
241 nr_ranges = 1;
242 } else {
243 ranges = pfc->info->ranges;
244 nr_ranges = pfc->info->nr_ranges;
245 }
Laurent Pinchart247127f2013-03-08 00:45:12 +0100246
Laurent Pinchart63d57382013-02-15 01:33:38 +0100247 for (i = 0; i < nr_ranges; ++i) {
248 const struct pinmux_range *range = &ranges[i];
249
250 ret = gpiochip_add_pin_range(&chip->gpio_chip,
251 dev_name(pfc->dev),
252 range->begin, range->begin,
253 range->end - range->begin + 1);
254 if (ret < 0)
255 return ret;
256 }
257
258 /* Register the function GPIOs chip. */
Laurent Pinchart16883812012-12-06 14:49:25 +0100259 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
260 if (IS_ERR(chip))
261 return PTR_ERR(chip);
262
263 pfc->func = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900264
Paul Mundtb3c185a2012-06-20 17:29:04 +0900265 return 0;
266}
267
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100268int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900269{
Laurent Pinchart16883812012-12-06 14:49:25 +0100270 int err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900271 int ret;
272
Laurent Pinchart16883812012-12-06 14:49:25 +0100273 ret = gpiochip_remove(&pfc->gpio->gpio_chip);
274 err = gpiochip_remove(&pfc->func->gpio_chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900275
Laurent Pinchart16883812012-12-06 14:49:25 +0100276 return ret < 0 ? ret : err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900277}