Merge branch 'overlayfs-af_unix-fix' into overlayfs-linus
[linux-2.6-block.git] / drivers / pinctrl / sh-pfc / gpio.c
CommitLineData
b3c185a7
PM
1/*
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 */
c6193eac 11
1724acfd 12#include <linux/device.h>
b3c185a7 13#include <linux/gpio.h>
90efde22 14#include <linux/init.h>
b3c185a7 15#include <linux/module.h>
ca5481c6 16#include <linux/pinctrl/consumer.h>
90efde22
LP
17#include <linux/slab.h>
18#include <linux/spinlock.h>
b3c185a7 19
f9165132
LP
20#include "core.h"
21
51cb226b
LP
22struct sh_pfc_gpio_data_reg {
23 const struct pinmux_data_reg *info;
fc88936a 24 u32 shadow;
51cb226b
LP
25};
26
1a0039dc
LP
27struct sh_pfc_gpio_pin {
28 u8 dbit;
29 u8 dreg;
30};
31
b3c185a7 32struct sh_pfc_chip {
1a0039dc
LP
33 struct sh_pfc *pfc;
34 struct gpio_chip gpio_chip;
e51d5343 35
1a0039dc 36 struct sh_pfc_window *mem;
51cb226b 37 struct sh_pfc_gpio_data_reg *regs;
1a0039dc 38 struct sh_pfc_gpio_pin *pins;
b3c185a7
PM
39};
40
b3c185a7
PM
41static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
42{
7cb093c4
LW
43 struct sh_pfc_chip *chip = gpiochip_get_data(gc);
44 return chip->pfc;
b3c185a7
PM
45}
46
757b055a 47static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int offset,
51cb226b
LP
48 struct sh_pfc_gpio_data_reg **reg,
49 unsigned int *bit)
41f1219f 50{
757b055a 51 int idx = sh_pfc_get_pin_index(chip->pfc, offset);
1a0039dc 52 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
41f1219f 53
1a0039dc
LP
54 *reg = &chip->regs[gpio_pin->dreg];
55 *bit = gpio_pin->dbit;
41f1219f
LP
56}
57
fc88936a
GU
58static u32 gpio_read_data_reg(struct sh_pfc_chip *chip,
59 const struct pinmux_data_reg *dreg)
41f1219f 60{
1f34de05
GU
61 phys_addr_t address = dreg->reg;
62 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
e51d5343
LP
63
64 return sh_pfc_read_raw_reg(mem, dreg->reg_width);
65}
41f1219f 66
e51d5343 67static void gpio_write_data_reg(struct sh_pfc_chip *chip,
fc88936a 68 const struct pinmux_data_reg *dreg, u32 value)
e51d5343 69{
1f34de05
GU
70 phys_addr_t address = dreg->reg;
71 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
41f1219f 72
e51d5343
LP
73 sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
74}
41f1219f 75
757b055a 76static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned idx)
e51d5343 77{
1a0039dc 78 struct sh_pfc *pfc = chip->pfc;
757b055a
LP
79 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
80 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
e51d5343
LP
81 const struct pinmux_data_reg *dreg;
82 unsigned int bit;
83 unsigned int i;
41f1219f 84
17c7cbb0 85 for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
e51d5343 86 for (bit = 0; bit < dreg->reg_width; bit++) {
1a0039dc
LP
87 if (dreg->enum_ids[bit] == pin->enum_id) {
88 gpio_pin->dreg = i;
89 gpio_pin->dbit = bit;
41f1219f
LP
90 return;
91 }
92 }
41f1219f
LP
93 }
94
95 BUG();
96}
97
e51d5343 98static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
41f1219f 99{
e51d5343 100 struct sh_pfc *pfc = chip->pfc;
51cb226b 101 const struct pinmux_data_reg *dreg;
e51d5343 102 unsigned int i;
41f1219f 103
51cb226b
LP
104 /* Count the number of data registers, allocate memory and initialize
105 * them.
106 */
107 for (i = 0; pfc->info->data_regs[i].reg_width; ++i)
108 ;
109
110 chip->regs = devm_kzalloc(pfc->dev, i * sizeof(*chip->regs),
111 GFP_KERNEL);
112 if (chip->regs == NULL)
113 return -ENOMEM;
114
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);
118 }
41f1219f 119
e51d5343
LP
120 for (i = 0; i < pfc->info->nr_pins; i++) {
121 if (pfc->info->pins[i].enum_id == 0)
122 continue;
123
1a0039dc 124 gpio_setup_data_reg(chip, i);
41f1219f 125 }
e51d5343
LP
126
127 return 0;
41f1219f
LP
128}
129
16883814
LP
130/* -----------------------------------------------------------------------------
131 * Pin GPIOs
132 */
b3c185a7 133
16883814 134static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
b3c185a7 135{
0b73ee5d 136 struct sh_pfc *pfc = gpio_to_pfc(gc);
1a0039dc 137 int idx = sh_pfc_get_pin_index(pfc, offset);
0b73ee5d 138
1a0039dc 139 if (idx < 0 || pfc->info->pins[idx].enum_id == 0)
0b73ee5d
LP
140 return -EINVAL;
141
16883814 142 return pinctrl_request_gpio(offset);
b3c185a7
PM
143}
144
16883814 145static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
b3c185a7 146{
16883814 147 return pinctrl_free_gpio(offset);
b3c185a7
PM
148}
149
e51d5343
LP
150static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
151 int value)
b3c185a7 152{
51cb226b 153 struct sh_pfc_gpio_data_reg *reg;
41f1219f 154 unsigned int bit;
cef28a28 155 unsigned int pos;
b3c185a7 156
51cb226b 157 gpio_get_data_reg(chip, offset, &reg, &bit);
41f1219f 158
51cb226b 159 pos = reg->info->reg_width - (bit + 1);
41f1219f
LP
160
161 if (value)
fc88936a 162 reg->shadow |= BIT(pos);
41f1219f 163 else
fc88936a 164 reg->shadow &= ~BIT(pos);
41f1219f 165
51cb226b 166 gpio_write_data_reg(chip, reg->info, reg->shadow);
b3c185a7
PM
167}
168
16883814 169static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
ca5481c6
PM
170{
171 return pinctrl_gpio_direction_input(offset);
172}
173
16883814 174static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
ca5481c6
PM
175 int value)
176{
7cb093c4 177 gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
ca5481c6
PM
178
179 return pinctrl_gpio_direction_output(offset);
180}
181
16883814 182static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
b3c185a7 183{
7cb093c4 184 struct sh_pfc_chip *chip = gpiochip_get_data(gc);
51cb226b 185 struct sh_pfc_gpio_data_reg *reg;
41f1219f 186 unsigned int bit;
cef28a28 187 unsigned int pos;
16883814 188
51cb226b 189 gpio_get_data_reg(chip, offset, &reg, &bit);
41f1219f 190
51cb226b 191 pos = reg->info->reg_width - (bit + 1);
41f1219f 192
51cb226b 193 return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
b3c185a7
PM
194}
195
16883814 196static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
b3c185a7 197{
7cb093c4 198 gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
b3c185a7
PM
199}
200
16883814 201static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
b3c185a7
PM
202{
203 struct sh_pfc *pfc = gpio_to_pfc(gc);
8d72a7fc 204 unsigned int i, k;
c07f54f6
LP
205
206 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
6d5bddd5 207 const short *gpios = pfc->info->gpio_irq[i].gpios;
c07f54f6 208
316b2550 209 for (k = 0; gpios[k] >= 0; k++) {
c07f54f6 210 if (gpios[k] == offset)
70c8f01a 211 goto found;
b3c185a7
PM
212 }
213 }
214
9697643f 215 return 0;
70c8f01a
LP
216
217found:
4adeabd0 218 return pfc->irqs[i];
b3c185a7
PM
219}
220
e51d5343 221static int gpio_pin_setup(struct sh_pfc_chip *chip)
b3c185a7
PM
222{
223 struct sh_pfc *pfc = chip->pfc;
224 struct gpio_chip *gc = &chip->gpio_chip;
e51d5343
LP
225 int ret;
226
a1a3580c
LP
227 chip->pins = devm_kzalloc(pfc->dev, pfc->info->nr_pins *
228 sizeof(*chip->pins), GFP_KERNEL);
1a0039dc
LP
229 if (chip->pins == NULL)
230 return -ENOMEM;
231
e51d5343
LP
232 ret = gpio_setup_data_regs(chip);
233 if (ret < 0)
234 return ret;
b3c185a7 235
16883814
LP
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;
b3c185a7 243
19bb7fe3 244 gc->label = pfc->info->name;
58383c78 245 gc->parent = pfc->dev;
b3c185a7 246 gc->owner = THIS_MODULE;
d7a7ca57 247 gc->base = 0;
28818fa5 248 gc->ngpio = pfc->nr_gpio_pins;
e51d5343
LP
249
250 return 0;
b3c185a7
PM
251}
252
16883814
LP
253/* -----------------------------------------------------------------------------
254 * Function GPIOs
255 */
256
56f891b4 257#ifdef CONFIG_SUPERH
16883814
LP
258static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
259{
9a643c9a 260 static bool __print_once;
16883814 261 struct sh_pfc *pfc = gpio_to_pfc(gc);
a68fdca9 262 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
16883814 263 unsigned long flags;
b705c054 264 int ret;
16883814 265
9a643c9a
LP
266 if (!__print_once) {
267 dev_notice(pfc->dev,
268 "Use of GPIO API for function requests is deprecated."
269 " Convert to pinctrl\n");
270 __print_once = true;
271 }
16883814 272
a68fdca9 273 if (mark == 0)
b705c054 274 return -EINVAL;
16883814
LP
275
276 spin_lock_irqsave(&pfc->lock, flags);
b705c054 277 ret = sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION);
16883814 278 spin_unlock_irqrestore(&pfc->lock, flags);
b705c054 279
16883814
LP
280 return ret;
281}
282
e51d5343 283static int gpio_function_setup(struct sh_pfc_chip *chip)
16883814
LP
284{
285 struct sh_pfc *pfc = chip->pfc;
286 struct gpio_chip *gc = &chip->gpio_chip;
287
288 gc->request = gpio_function_request;
16883814
LP
289
290 gc->label = pfc->info->name;
291 gc->owner = THIS_MODULE;
28818fa5 292 gc->base = pfc->nr_gpio_pins;
16883814 293 gc->ngpio = pfc->info->nr_func_gpios;
e51d5343
LP
294
295 return 0;
16883814 296}
56f891b4 297#endif
16883814
LP
298
299/* -----------------------------------------------------------------------------
300 * Register/unregister
301 */
302
303static struct sh_pfc_chip *
ceef91dc
LP
304sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *),
305 struct sh_pfc_window *mem)
b3c185a7
PM
306{
307 struct sh_pfc_chip *chip;
308 int ret;
309
1724acfd 310 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
b3c185a7 311 if (unlikely(!chip))
16883814 312 return ERR_PTR(-ENOMEM);
b3c185a7 313
ceef91dc 314 chip->mem = mem;
b3c185a7
PM
315 chip->pfc = pfc;
316
e51d5343
LP
317 ret = setup(chip);
318 if (ret < 0)
319 return ERR_PTR(ret);
b3c185a7 320
7cb093c4 321 ret = gpiochip_add_data(&chip->gpio_chip, chip);
1724acfd 322 if (unlikely(ret < 0))
16883814
LP
323 return ERR_PTR(ret);
324
9a643c9a
LP
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);
16883814
LP
328
329 return chip;
330}
331
332int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
333{
334 struct sh_pfc_chip *chip;
1f34de05 335 phys_addr_t address;
63d57383 336 unsigned int i;
16883814 337
1a4fd58f
LP
338 if (pfc->info->data_regs == NULL)
339 return 0;
340
ceef91dc
LP
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
344 * GPIOs.
345 */
1f34de05 346 address = pfc->info->data_regs[0].reg;
ceef91dc 347 for (i = 0; i < pfc->num_windows; ++i) {
5b46ac3a 348 struct sh_pfc_window *window = &pfc->windows[i];
ceef91dc 349
1f34de05
GU
350 if (address >= window->phys &&
351 address < window->phys + window->size)
ceef91dc
LP
352 break;
353 }
354
355 if (i == pfc->num_windows)
356 return 0;
357
70c8f01a 358 /* If we have IRQ resources make sure their number is correct. */
4adeabd0 359 if (pfc->num_irqs != pfc->info->gpio_irq_size) {
70c8f01a
LP
360 dev_err(pfc->dev, "invalid number of IRQ resources\n");
361 return -EINVAL;
362 }
363
63d57383 364 /* Register the real GPIOs chip. */
5b46ac3a 365 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup, &pfc->windows[i]);
16883814
LP
366 if (IS_ERR(chip))
367 return PTR_ERR(chip);
6f6a4a68
LP
368
369 pfc->gpio = chip;
b3c185a7 370
18fab399
GU
371 if (IS_ENABLED(CONFIG_OF) && pfc->dev->of_node)
372 return 0;
4f82e3ee 373
90d06613
GU
374#ifdef CONFIG_SUPERH
375 /*
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
379 * pin.
380 */
381 for (i = 0; i < pfc->nr_ranges; ++i) {
382 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
383 int ret;
384
385 if (range->start >= pfc->nr_gpio_pins)
386 break;
387
388 ret = gpiochip_add_pin_range(&chip->gpio_chip,
389 dev_name(pfc->dev), range->start, range->start,
390 range->end - range->start + 1);
391 if (ret < 0)
392 return ret;
63d57383 393 }
247127f9 394
63d57383 395 /* Register the function GPIOs chip. */
542a564d
LP
396 if (pfc->info->nr_func_gpios == 0)
397 return 0;
398
ceef91dc 399 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL);
16883814
LP
400 if (IS_ERR(chip))
401 return PTR_ERR(chip);
402
403 pfc->func = chip;
56f891b4 404#endif /* CONFIG_SUPERH */
b3c185a7 405
b3c185a7
PM
406 return 0;
407}
408
6f6a4a68 409int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
b3c185a7 410{
b4e7c55d 411 gpiochip_remove(&pfc->gpio->gpio_chip);
56f891b4 412#ifdef CONFIG_SUPERH
b4e7c55d 413 gpiochip_remove(&pfc->func->gpio_chip);
56f891b4 414#endif
b4e7c55d 415 return 0;
b3c185a7 416}