blob: cfca0aaf98204dd39d3d277f892bc36902feee7a [file] [log] [blame]
Magnus Damm2967dab2008-10-08 20:41:43 +09001/*
2 * Pinmuxed GPIO support for SuperH.
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
Paul Mundtb72421d2010-10-04 03:54:56 +090010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Magnus Damm2967dab2008-10-08 20:41:43 +090012#include <linux/errno.h>
13#include <linux/kernel.h>
14#include <linux/list.h>
15#include <linux/module.h>
16#include <linux/clk.h>
17#include <linux/err.h>
18#include <linux/io.h>
19#include <linux/irq.h>
20#include <linux/bitops.h>
21#include <linux/gpio.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090022#include <linux/slab.h>
23#include <linux/ioport.h>
24
25static void pfc_iounmap(struct pinmux_info *pip)
26{
27 int k;
28
29 for (k = 0; k < pip->num_resources; k++)
30 if (pip->window[k].virt)
31 iounmap(pip->window[k].virt);
32
33 kfree(pip->window);
34 pip->window = NULL;
35}
36
37static int pfc_ioremap(struct pinmux_info *pip)
38{
39 struct resource *res;
40 int k;
41
42 if (!pip->num_resources)
43 return 0;
44
45 pip->window = kzalloc(pip->num_resources * sizeof(*pip->window),
46 GFP_NOWAIT);
47 if (!pip->window)
48 goto err1;
49
50 for (k = 0; k < pip->num_resources; k++) {
51 res = pip->resource + k;
52 WARN_ON(resource_type(res) != IORESOURCE_MEM);
53 pip->window[k].phys = res->start;
54 pip->window[k].size = resource_size(res);
55 pip->window[k].virt = ioremap_nocache(res->start,
56 resource_size(res));
57 if (!pip->window[k].virt)
58 goto err2;
59 }
60
61 return 0;
62
63err2:
64 pfc_iounmap(pip);
65err1:
66 return -1;
67}
68
69static void __iomem *pfc_phys_to_virt(struct pinmux_info *pip,
70 unsigned long address)
71{
72 struct pfc_window *window;
73 int k;
74
75 /* scan through physical windows and convert address */
76 for (k = 0; k < pip->num_resources; k++) {
77 window = pip->window + k;
78
79 if (address < window->phys)
80 continue;
81
82 if (address >= (window->phys + window->size))
83 continue;
84
85 return window->virt + (address - window->phys);
86 }
87
88 /* no windows defined, register must be 1:1 mapped virt:phys */
89 return (void __iomem *)address;
90}
Magnus Damm2967dab2008-10-08 20:41:43 +090091
Magnus Damm2967dab2008-10-08 20:41:43 +090092static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
93{
94 if (enum_id < r->begin)
95 return 0;
96
97 if (enum_id > r->end)
98 return 0;
99
100 return 1;
101}
102
Magnus Dammb0e10212011-12-09 12:14:27 +0900103static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900104 unsigned long reg_width)
105{
106 switch (reg_width) {
107 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900108 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900109 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900110 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900111 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900112 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900113 }
114
115 BUG();
116 return 0;
117}
118
Magnus Dammb0e10212011-12-09 12:14:27 +0900119static void gpio_write_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900120 unsigned long reg_width,
121 unsigned long data)
122{
123 switch (reg_width) {
124 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900125 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900126 return;
127 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900128 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900129 return;
130 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900131 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900132 return;
133 }
134
135 BUG();
136}
137
Magnus Damm92554d92011-12-14 01:00:37 +0900138static int gpio_read_bit(struct pinmux_data_reg *dr,
139 unsigned long in_pos)
140{
141 unsigned long pos;
142
143 pos = dr->reg_width - (in_pos + 1);
144
145 pr_debug("read_bit: addr = %lx, pos = %ld, "
146 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
147
148 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
149}
150
Magnus Damm32920942008-12-25 18:17:26 +0900151static void gpio_write_bit(struct pinmux_data_reg *dr,
152 unsigned long in_pos, unsigned long value)
153{
154 unsigned long pos;
155
156 pos = dr->reg_width - (in_pos + 1);
157
Paul Mundtca6f2d72009-12-09 15:51:27 +0900158 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900159 "r_width = %ld\n",
160 dr->reg, !!value, pos, dr->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900161
162 if (value)
163 set_bit(pos, &dr->reg_shadow);
164 else
165 clear_bit(pos, &dr->reg_shadow);
166
Magnus Dammb0e10212011-12-09 12:14:27 +0900167 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Magnus Damm32920942008-12-25 18:17:26 +0900168}
169
Magnus Dammb0e10212011-12-09 12:14:27 +0900170static int gpio_read_reg(void __iomem *mapped_reg, unsigned long reg_width,
171 unsigned long field_width, unsigned long in_pos,
172 unsigned long reg)
Magnus Damm2967dab2008-10-08 20:41:43 +0900173{
174 unsigned long data, mask, pos;
175
176 data = 0;
177 mask = (1 << field_width) - 1;
178 pos = reg_width - ((in_pos + 1) * field_width);
179
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900180 pr_debug("read_reg: addr = %lx, pos = %ld, "
181 "r_width = %ld, f_width = %ld\n",
182 reg, pos, reg_width, field_width);
Magnus Damm2967dab2008-10-08 20:41:43 +0900183
Magnus Dammb0e10212011-12-09 12:14:27 +0900184 data = gpio_read_raw_reg(mapped_reg, reg_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900185 return (data >> pos) & mask;
186}
Magnus Damm2967dab2008-10-08 20:41:43 +0900187
Magnus Dammb0e10212011-12-09 12:14:27 +0900188static void gpio_write_reg(void __iomem *mapped_reg, unsigned long reg_width,
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900189 unsigned long field_width, unsigned long in_pos,
Magnus Dammb0e10212011-12-09 12:14:27 +0900190 unsigned long value, unsigned long reg)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900191{
192 unsigned long mask, pos;
193
194 mask = (1 << field_width) - 1;
195 pos = reg_width - ((in_pos + 1) * field_width);
196
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900197 pr_debug("write_reg addr = %lx, value = %ld, pos = %ld, "
198 "r_width = %ld, f_width = %ld\n",
199 reg, value, pos, reg_width, field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900200
201 mask = ~(mask << pos);
202 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900203
204 switch (reg_width) {
205 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900206 iowrite8((ioread8(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900207 break;
208 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900209 iowrite16((ioread16(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900210 break;
211 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900212 iowrite32((ioread32(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900213 break;
214 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900215}
216
Magnus Damm18801be2008-12-25 18:17:09 +0900217static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900218{
Magnus Damm18801be2008-12-25 18:17:09 +0900219 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900220 struct pinmux_data_reg *data_reg;
221 int k, n;
222
Magnus Damm18801be2008-12-25 18:17:09 +0900223 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900224 return -1;
225
226 k = 0;
227 while (1) {
228 data_reg = gpioc->data_regs + k;
229
230 if (!data_reg->reg_width)
231 break;
232
Magnus Dammb0e10212011-12-09 12:14:27 +0900233 data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
234
Magnus Damm2967dab2008-10-08 20:41:43 +0900235 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900236 if (data_reg->enum_ids[n] == gpiop->enum_id) {
237 gpiop->flags &= ~PINMUX_FLAG_DREG;
238 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
239 gpiop->flags &= ~PINMUX_FLAG_DBIT;
240 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900241 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900242 }
243 }
244 k++;
245 }
246
Magnus Damm18801be2008-12-25 18:17:09 +0900247 BUG();
248
Magnus Damm2967dab2008-10-08 20:41:43 +0900249 return -1;
250}
251
Magnus Damm32920942008-12-25 18:17:26 +0900252static void setup_data_regs(struct pinmux_info *gpioc)
253{
254 struct pinmux_data_reg *drp;
255 int k;
256
257 for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
258 setup_data_reg(gpioc, k);
259
260 k = 0;
261 while (1) {
262 drp = gpioc->data_regs + k;
263
264 if (!drp->reg_width)
265 break;
266
Magnus Dammb0e10212011-12-09 12:14:27 +0900267 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
268 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900269 k++;
270 }
271}
272
Magnus Damm18801be2008-12-25 18:17:09 +0900273static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
274 struct pinmux_data_reg **drp, int *bitp)
275{
276 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
277 int k, n;
278
279 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
280 return -1;
281
282 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
283 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
284 *drp = gpioc->data_regs + k;
285 *bitp = n;
286 return 0;
287}
288
Magnus Damm2967dab2008-10-08 20:41:43 +0900289static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
290 struct pinmux_cfg_reg **crp, int *indexp,
291 unsigned long **cntp)
292{
293 struct pinmux_cfg_reg *config_reg;
294 unsigned long r_width, f_width;
295 int k, n;
296
297 k = 0;
298 while (1) {
299 config_reg = gpioc->cfg_regs + k;
300
301 r_width = config_reg->reg_width;
302 f_width = config_reg->field_width;
303
304 if (!r_width)
305 break;
Magnus Dammc63bcc62011-10-17 18:01:19 +0900306 for (n = 0; n < (r_width / f_width) * (1 << f_width); n++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900307 if (config_reg->enum_ids[n] == enum_id) {
308 *crp = config_reg;
309 *indexp = n;
310 *cntp = &config_reg->cnt[n / (1 << f_width)];
311 return 0;
312 }
313 }
314 k++;
315 }
316
317 return -1;
318}
319
320static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
321 int pos, pinmux_enum_t *enum_idp)
322{
323 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
324 pinmux_enum_t *data = gpioc->gpio_data;
325 int k;
326
327 if (!enum_in_range(enum_id, &gpioc->data)) {
328 if (!enum_in_range(enum_id, &gpioc->mark)) {
329 pr_err("non data/mark enum_id for gpio %d\n", gpio);
330 return -1;
331 }
332 }
333
334 if (pos) {
335 *enum_idp = data[pos + 1];
336 return pos + 1;
337 }
338
339 for (k = 0; k < gpioc->gpio_data_size; k++) {
340 if (data[k] == enum_id) {
341 *enum_idp = data[k + 1];
342 return k + 1;
343 }
344 }
345
346 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
347 return -1;
348}
349
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900350static void write_config_reg(struct pinmux_info *gpioc,
351 struct pinmux_cfg_reg *crp,
352 int index)
Magnus Damm2967dab2008-10-08 20:41:43 +0900353{
354 unsigned long ncomb, pos, value;
Magnus Dammb0e10212011-12-09 12:14:27 +0900355 void __iomem *mapped_reg;
Magnus Damm2967dab2008-10-08 20:41:43 +0900356
357 ncomb = 1 << crp->field_width;
358 pos = index / ncomb;
359 value = index % ncomb;
360
Magnus Dammb0e10212011-12-09 12:14:27 +0900361 mapped_reg = pfc_phys_to_virt(gpioc, crp->reg);
362
363 gpio_write_reg(mapped_reg, crp->reg_width, crp->field_width,
364 pos, value, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900365}
366
367static int check_config_reg(struct pinmux_info *gpioc,
368 struct pinmux_cfg_reg *crp,
369 int index)
370{
371 unsigned long ncomb, pos, value;
Magnus Dammb0e10212011-12-09 12:14:27 +0900372 void __iomem *mapped_reg;
Magnus Damm2967dab2008-10-08 20:41:43 +0900373
374 ncomb = 1 << crp->field_width;
375 pos = index / ncomb;
376 value = index % ncomb;
377
Magnus Dammb0e10212011-12-09 12:14:27 +0900378 mapped_reg = pfc_phys_to_virt(gpioc, crp->reg);
379
380 if (gpio_read_reg(mapped_reg, crp->reg_width,
381 crp->field_width, pos, crp->reg) == value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900382 return 0;
383
384 return -1;
385}
386
387enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
388
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900389static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
390 int pinmux_type, int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900391{
392 struct pinmux_cfg_reg *cr = NULL;
393 pinmux_enum_t enum_id;
394 struct pinmux_range *range;
395 int in_range, pos, index;
396 unsigned long *cntp;
397
398 switch (pinmux_type) {
399
400 case PINMUX_TYPE_FUNCTION:
401 range = NULL;
402 break;
403
404 case PINMUX_TYPE_OUTPUT:
405 range = &gpioc->output;
406 break;
407
408 case PINMUX_TYPE_INPUT:
409 range = &gpioc->input;
410 break;
411
412 case PINMUX_TYPE_INPUT_PULLUP:
413 range = &gpioc->input_pu;
414 break;
415
416 case PINMUX_TYPE_INPUT_PULLDOWN:
417 range = &gpioc->input_pd;
418 break;
419
420 default:
421 goto out_err;
422 }
423
424 pos = 0;
425 enum_id = 0;
426 index = 0;
427 while (1) {
428 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
429 if (pos <= 0)
430 goto out_err;
431
432 if (!enum_id)
433 break;
434
Magnus Damm50dd3142010-01-19 13:52:28 +0000435 /* first check if this is a function enum */
Magnus Damm2967dab2008-10-08 20:41:43 +0900436 in_range = enum_in_range(enum_id, &gpioc->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000437 if (!in_range) {
438 /* not a function enum */
439 if (range) {
440 /*
441 * other range exists, so this pin is
442 * a regular GPIO pin that now is being
443 * bound to a specific direction.
444 *
445 * for this case we only allow function enums
446 * and the enums that match the other range.
447 */
448 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900449
Magnus Damm50dd3142010-01-19 13:52:28 +0000450 /*
451 * special case pass through for fixed
452 * input-only or output-only pins without
453 * function enum register association.
454 */
455 if (in_range && enum_id == range->force)
456 continue;
457 } else {
458 /*
459 * no other range exists, so this pin
460 * must then be of the function type.
461 *
462 * allow function type pins to select
463 * any combination of function/in/out
464 * in their MARK lists.
465 */
466 in_range = 1;
467 }
Magnus Damm42eed422008-10-22 18:29:17 +0900468 }
469
Magnus Damm2967dab2008-10-08 20:41:43 +0900470 if (!in_range)
471 continue;
472
473 if (get_config_reg(gpioc, enum_id, &cr, &index, &cntp) != 0)
474 goto out_err;
475
476 switch (cfg_mode) {
477 case GPIO_CFG_DRYRUN:
478 if (!*cntp || !check_config_reg(gpioc, cr, index))
479 continue;
480 break;
481
482 case GPIO_CFG_REQ:
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900483 write_config_reg(gpioc, cr, index);
Magnus Damm2967dab2008-10-08 20:41:43 +0900484 *cntp = *cntp + 1;
485 break;
486
487 case GPIO_CFG_FREE:
488 *cntp = *cntp - 1;
489 break;
490 }
491 }
492
493 return 0;
494 out_err:
495 return -1;
496}
497
498static DEFINE_SPINLOCK(gpio_lock);
499
Magnus Damm69edbba2008-12-25 18:17:34 +0900500static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
Magnus Damm2967dab2008-10-08 20:41:43 +0900501{
Magnus Damm69edbba2008-12-25 18:17:34 +0900502 return container_of(chip, struct pinmux_info, chip);
503}
504
505static int sh_gpio_request(struct gpio_chip *chip, unsigned offset)
506{
507 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900508 struct pinmux_data_reg *dummy;
509 unsigned long flags;
510 int i, ret, pinmux_type;
511
512 ret = -EINVAL;
513
514 if (!gpioc)
515 goto err_out;
516
517 spin_lock_irqsave(&gpio_lock, flags);
518
Magnus Damm69edbba2008-12-25 18:17:34 +0900519 if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
Magnus Damm2967dab2008-10-08 20:41:43 +0900520 goto err_unlock;
521
522 /* setup pin function here if no data is associated with pin */
523
Magnus Damm69edbba2008-12-25 18:17:34 +0900524 if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900525 pinmux_type = PINMUX_TYPE_FUNCTION;
526 else
527 pinmux_type = PINMUX_TYPE_GPIO;
528
529 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
Magnus Damm69edbba2008-12-25 18:17:34 +0900530 if (pinmux_config_gpio(gpioc, offset,
Magnus Damm2967dab2008-10-08 20:41:43 +0900531 pinmux_type,
532 GPIO_CFG_DRYRUN) != 0)
533 goto err_unlock;
534
Magnus Damm69edbba2008-12-25 18:17:34 +0900535 if (pinmux_config_gpio(gpioc, offset,
Magnus Damm2967dab2008-10-08 20:41:43 +0900536 pinmux_type,
537 GPIO_CFG_REQ) != 0)
538 BUG();
539 }
540
Magnus Damm69edbba2008-12-25 18:17:34 +0900541 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
542 gpioc->gpios[offset].flags |= pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900543
544 ret = 0;
545 err_unlock:
546 spin_unlock_irqrestore(&gpio_lock, flags);
547 err_out:
548 return ret;
549}
Magnus Damm2967dab2008-10-08 20:41:43 +0900550
Magnus Damm69edbba2008-12-25 18:17:34 +0900551static void sh_gpio_free(struct gpio_chip *chip, unsigned offset)
Magnus Damm2967dab2008-10-08 20:41:43 +0900552{
Magnus Damm69edbba2008-12-25 18:17:34 +0900553 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900554 unsigned long flags;
555 int pinmux_type;
556
557 if (!gpioc)
558 return;
559
560 spin_lock_irqsave(&gpio_lock, flags);
561
Magnus Damm69edbba2008-12-25 18:17:34 +0900562 pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
563 pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
564 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
565 gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
Magnus Damm2967dab2008-10-08 20:41:43 +0900566
567 spin_unlock_irqrestore(&gpio_lock, flags);
568}
Magnus Damm2967dab2008-10-08 20:41:43 +0900569
570static int pinmux_direction(struct pinmux_info *gpioc,
571 unsigned gpio, int new_pinmux_type)
572{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900573 int pinmux_type;
574 int ret = -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900575
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900576 if (!gpioc)
577 goto err_out;
578
Magnus Damm2967dab2008-10-08 20:41:43 +0900579 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
580
581 switch (pinmux_type) {
582 case PINMUX_TYPE_GPIO:
583 break;
584 case PINMUX_TYPE_OUTPUT:
585 case PINMUX_TYPE_INPUT:
586 case PINMUX_TYPE_INPUT_PULLUP:
587 case PINMUX_TYPE_INPUT_PULLDOWN:
588 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
589 break;
590 default:
591 goto err_out;
592 }
593
594 if (pinmux_config_gpio(gpioc, gpio,
595 new_pinmux_type,
596 GPIO_CFG_DRYRUN) != 0)
597 goto err_out;
598
599 if (pinmux_config_gpio(gpioc, gpio,
600 new_pinmux_type,
601 GPIO_CFG_REQ) != 0)
602 BUG();
603
Magnus Damm18801be2008-12-25 18:17:09 +0900604 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
605 gpioc->gpios[gpio].flags |= new_pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900606
607 ret = 0;
608 err_out:
609 return ret;
610}
611
Magnus Damm69edbba2008-12-25 18:17:34 +0900612static int sh_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
Magnus Damm2967dab2008-10-08 20:41:43 +0900613{
Magnus Damm69edbba2008-12-25 18:17:34 +0900614 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900615 unsigned long flags;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900616 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900617
618 spin_lock_irqsave(&gpio_lock, flags);
Magnus Damm69edbba2008-12-25 18:17:34 +0900619 ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900620 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900621
Magnus Damm2967dab2008-10-08 20:41:43 +0900622 return ret;
623}
Magnus Damm2967dab2008-10-08 20:41:43 +0900624
Magnus Damm69edbba2008-12-25 18:17:34 +0900625static void sh_gpio_set_value(struct pinmux_info *gpioc,
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900626 unsigned gpio, int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900627{
628 struct pinmux_data_reg *dr = NULL;
629 int bit = 0;
630
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900631 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900632 BUG();
633 else
Magnus Damm32920942008-12-25 18:17:26 +0900634 gpio_write_bit(dr, bit, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900635}
636
Magnus Damm69edbba2008-12-25 18:17:34 +0900637static int sh_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
638 int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900639{
Magnus Damm69edbba2008-12-25 18:17:34 +0900640 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900641 unsigned long flags;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900642 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900643
Magnus Damm69edbba2008-12-25 18:17:34 +0900644 sh_gpio_set_value(gpioc, offset, value);
Magnus Damm32920942008-12-25 18:17:26 +0900645 spin_lock_irqsave(&gpio_lock, flags);
Magnus Damm69edbba2008-12-25 18:17:34 +0900646 ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900647 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900648
Magnus Damm2967dab2008-10-08 20:41:43 +0900649 return ret;
650}
Magnus Damm2967dab2008-10-08 20:41:43 +0900651
Magnus Damm69edbba2008-12-25 18:17:34 +0900652static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900653{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900654 struct pinmux_data_reg *dr = NULL;
655 int bit = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900656
Paul Mundte8184a42010-10-04 05:15:20 +0900657 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
658 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900659
Magnus Damm92554d92011-12-14 01:00:37 +0900660 return gpio_read_bit(dr, bit);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900661}
662
Magnus Damm69edbba2008-12-25 18:17:34 +0900663static int sh_gpio_get(struct gpio_chip *chip, unsigned offset)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900664{
Magnus Damm69edbba2008-12-25 18:17:34 +0900665 return sh_gpio_get_value(chip_to_pinmux(chip), offset);
Magnus Damm2967dab2008-10-08 20:41:43 +0900666}
Magnus Damm2967dab2008-10-08 20:41:43 +0900667
Magnus Damm69edbba2008-12-25 18:17:34 +0900668static void sh_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900669{
Magnus Damm69edbba2008-12-25 18:17:34 +0900670 sh_gpio_set_value(chip_to_pinmux(chip), offset, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900671}
Magnus Damm2967dab2008-10-08 20:41:43 +0900672
Magnus Dammad2a8e72011-09-28 16:50:58 +0900673static int sh_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
674{
675 struct pinmux_info *gpioc = chip_to_pinmux(chip);
676 pinmux_enum_t enum_id;
677 pinmux_enum_t *enum_ids;
678 int i, k, pos;
679
680 pos = 0;
681 enum_id = 0;
682 while (1) {
683 pos = get_gpio_enum_id(gpioc, offset, pos, &enum_id);
684 if (pos <= 0 || !enum_id)
685 break;
686
687 for (i = 0; i < gpioc->gpio_irq_size; i++) {
688 enum_ids = gpioc->gpio_irq[i].enum_ids;
689 for (k = 0; enum_ids[k]; k++) {
690 if (enum_ids[k] == enum_id)
691 return gpioc->gpio_irq[i].irq;
692 }
693 }
694 }
695
696 return -ENOSYS;
697}
698
Magnus Damm2967dab2008-10-08 20:41:43 +0900699int register_pinmux(struct pinmux_info *pip)
700{
Magnus Damm69edbba2008-12-25 18:17:34 +0900701 struct gpio_chip *chip = &pip->chip;
Magnus Dammb0e10212011-12-09 12:14:27 +0900702 int ret;
Magnus Damm69edbba2008-12-25 18:17:34 +0900703
Paul Mundtb72421d2010-10-04 03:54:56 +0900704 pr_info("%s handling gpio %d -> %d\n",
Magnus Damm2967dab2008-10-08 20:41:43 +0900705 pip->name, pip->first_gpio, pip->last_gpio);
706
Magnus Dammb0e10212011-12-09 12:14:27 +0900707 ret = pfc_ioremap(pip);
708 if (ret < 0)
709 return ret;
710
Magnus Damm69edbba2008-12-25 18:17:34 +0900711 setup_data_regs(pip);
712
713 chip->request = sh_gpio_request;
714 chip->free = sh_gpio_free;
715 chip->direction_input = sh_gpio_direction_input;
716 chip->get = sh_gpio_get;
717 chip->direction_output = sh_gpio_direction_output;
718 chip->set = sh_gpio_set;
Magnus Dammad2a8e72011-09-28 16:50:58 +0900719 chip->to_irq = sh_gpio_to_irq;
Magnus Damm69edbba2008-12-25 18:17:34 +0900720
721 WARN_ON(pip->first_gpio != 0); /* needs testing */
722
723 chip->label = pip->name;
724 chip->owner = THIS_MODULE;
725 chip->base = pip->first_gpio;
726 chip->ngpio = (pip->last_gpio - pip->first_gpio) + 1;
727
Magnus Dammb0e10212011-12-09 12:14:27 +0900728 ret = gpiochip_add(chip);
729 if (ret < 0)
730 pfc_iounmap(pip);
731
732 return ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900733}
Paul Mundtb72421d2010-10-04 03:54:56 +0900734
735int unregister_pinmux(struct pinmux_info *pip)
736{
737 pr_info("%s deregistering\n", pip->name);
Magnus Dammb0e10212011-12-09 12:14:27 +0900738 pfc_iounmap(pip);
Paul Mundtb72421d2010-10-04 03:54:56 +0900739 return gpiochip_remove(&pip->chip);
740}