blob: f975f4a334391887a65fcc7d3e80b5b3cc676194 [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 Damm18925e12011-12-14 01:00:55 +0900170static void config_reg_helper(struct pinmux_info *gpioc,
171 struct pinmux_cfg_reg *crp,
172 unsigned long in_pos,
173 void __iomem **mapped_regp,
174 unsigned long *maskp,
175 unsigned long *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900176{
Magnus Dammf78a26f2011-12-14 01:01:05 +0900177 int k;
178
Magnus Damm18925e12011-12-14 01:00:55 +0900179 *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900180
Magnus Dammf78a26f2011-12-14 01:01:05 +0900181 if (crp->field_width) {
182 *maskp = (1 << crp->field_width) - 1;
183 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
184 } else {
185 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
186 *posp = crp->reg_width;
187 for (k = 0; k <= in_pos; k++)
188 *posp -= crp->var_field_width[k];
189 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900190}
Magnus Damm2967dab2008-10-08 20:41:43 +0900191
Magnus Damm18925e12011-12-14 01:00:55 +0900192static int read_config_reg(struct pinmux_info *gpioc,
193 struct pinmux_cfg_reg *crp,
194 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900195{
Magnus Damm18925e12011-12-14 01:00:55 +0900196 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900197 unsigned long mask, pos;
198
Magnus Damm18925e12011-12-14 01:00:55 +0900199 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900200
Magnus Damm18925e12011-12-14 01:00:55 +0900201 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900202 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900203 crp->reg, field, crp->reg_width, crp->field_width);
204
205 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
206}
207
208static void write_config_reg(struct pinmux_info *gpioc,
209 struct pinmux_cfg_reg *crp,
210 unsigned long field, unsigned long value)
211{
212 void __iomem *mapped_reg;
213 unsigned long mask, pos;
214
215 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
216
217 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
218 "r_width = %ld, f_width = %ld\n",
219 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900220
221 mask = ~(mask << pos);
222 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900223
Magnus Damm18925e12011-12-14 01:00:55 +0900224 switch (crp->reg_width) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900225 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900226 iowrite8((ioread8(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900227 break;
228 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900229 iowrite16((ioread16(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900230 break;
231 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900232 iowrite32((ioread32(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900233 break;
234 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900235}
236
Magnus Damm18801be2008-12-25 18:17:09 +0900237static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900238{
Magnus Damm18801be2008-12-25 18:17:09 +0900239 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900240 struct pinmux_data_reg *data_reg;
241 int k, n;
242
Magnus Damm18801be2008-12-25 18:17:09 +0900243 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900244 return -1;
245
246 k = 0;
247 while (1) {
248 data_reg = gpioc->data_regs + k;
249
250 if (!data_reg->reg_width)
251 break;
252
Magnus Dammb0e10212011-12-09 12:14:27 +0900253 data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
254
Magnus Damm2967dab2008-10-08 20:41:43 +0900255 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900256 if (data_reg->enum_ids[n] == gpiop->enum_id) {
257 gpiop->flags &= ~PINMUX_FLAG_DREG;
258 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
259 gpiop->flags &= ~PINMUX_FLAG_DBIT;
260 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900261 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900262 }
263 }
264 k++;
265 }
266
Magnus Damm18801be2008-12-25 18:17:09 +0900267 BUG();
268
Magnus Damm2967dab2008-10-08 20:41:43 +0900269 return -1;
270}
271
Magnus Damm32920942008-12-25 18:17:26 +0900272static void setup_data_regs(struct pinmux_info *gpioc)
273{
274 struct pinmux_data_reg *drp;
275 int k;
276
277 for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
278 setup_data_reg(gpioc, k);
279
280 k = 0;
281 while (1) {
282 drp = gpioc->data_regs + k;
283
284 if (!drp->reg_width)
285 break;
286
Magnus Dammb0e10212011-12-09 12:14:27 +0900287 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
288 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900289 k++;
290 }
291}
292
Magnus Damm18801be2008-12-25 18:17:09 +0900293static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
294 struct pinmux_data_reg **drp, int *bitp)
295{
296 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
297 int k, n;
298
299 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
300 return -1;
301
302 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
303 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
304 *drp = gpioc->data_regs + k;
305 *bitp = n;
306 return 0;
307}
308
Magnus Damm2967dab2008-10-08 20:41:43 +0900309static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900310 struct pinmux_cfg_reg **crp,
311 int *fieldp, int *valuep,
Magnus Damm2967dab2008-10-08 20:41:43 +0900312 unsigned long **cntp)
313{
314 struct pinmux_cfg_reg *config_reg;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900315 unsigned long r_width, f_width, curr_width, ncomb;
316 int k, m, n, pos, bit_pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900317
318 k = 0;
319 while (1) {
320 config_reg = gpioc->cfg_regs + k;
321
322 r_width = config_reg->reg_width;
323 f_width = config_reg->field_width;
324
325 if (!r_width)
326 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900327
328 pos = 0;
329 m = 0;
330 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
331 if (f_width)
332 curr_width = f_width;
333 else
334 curr_width = config_reg->var_field_width[m];
335
336 ncomb = 1 << curr_width;
337 for (n = 0; n < ncomb; n++) {
338 if (config_reg->enum_ids[pos + n] == enum_id) {
339 *crp = config_reg;
340 *fieldp = m;
341 *valuep = n;
342 *cntp = &config_reg->cnt[m];
343 return 0;
344 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900345 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900346 pos += ncomb;
347 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900348 }
349 k++;
350 }
351
352 return -1;
353}
354
355static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
356 int pos, pinmux_enum_t *enum_idp)
357{
358 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
359 pinmux_enum_t *data = gpioc->gpio_data;
360 int k;
361
362 if (!enum_in_range(enum_id, &gpioc->data)) {
363 if (!enum_in_range(enum_id, &gpioc->mark)) {
364 pr_err("non data/mark enum_id for gpio %d\n", gpio);
365 return -1;
366 }
367 }
368
369 if (pos) {
370 *enum_idp = data[pos + 1];
371 return pos + 1;
372 }
373
374 for (k = 0; k < gpioc->gpio_data_size; k++) {
375 if (data[k] == enum_id) {
376 *enum_idp = data[k + 1];
377 return k + 1;
378 }
379 }
380
381 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
382 return -1;
383}
384
Magnus Damm2967dab2008-10-08 20:41:43 +0900385enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
386
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900387static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
388 int pinmux_type, int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900389{
390 struct pinmux_cfg_reg *cr = NULL;
391 pinmux_enum_t enum_id;
392 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900393 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900394 unsigned long *cntp;
395
396 switch (pinmux_type) {
397
398 case PINMUX_TYPE_FUNCTION:
399 range = NULL;
400 break;
401
402 case PINMUX_TYPE_OUTPUT:
403 range = &gpioc->output;
404 break;
405
406 case PINMUX_TYPE_INPUT:
407 range = &gpioc->input;
408 break;
409
410 case PINMUX_TYPE_INPUT_PULLUP:
411 range = &gpioc->input_pu;
412 break;
413
414 case PINMUX_TYPE_INPUT_PULLDOWN:
415 range = &gpioc->input_pd;
416 break;
417
418 default:
419 goto out_err;
420 }
421
422 pos = 0;
423 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900424 field = 0;
425 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900426 while (1) {
427 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
428 if (pos <= 0)
429 goto out_err;
430
431 if (!enum_id)
432 break;
433
Magnus Damm50dd3142010-01-19 13:52:28 +0000434 /* first check if this is a function enum */
Magnus Damm2967dab2008-10-08 20:41:43 +0900435 in_range = enum_in_range(enum_id, &gpioc->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000436 if (!in_range) {
437 /* not a function enum */
438 if (range) {
439 /*
440 * other range exists, so this pin is
441 * a regular GPIO pin that now is being
442 * bound to a specific direction.
443 *
444 * for this case we only allow function enums
445 * and the enums that match the other range.
446 */
447 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900448
Magnus Damm50dd3142010-01-19 13:52:28 +0000449 /*
450 * special case pass through for fixed
451 * input-only or output-only pins without
452 * function enum register association.
453 */
454 if (in_range && enum_id == range->force)
455 continue;
456 } else {
457 /*
458 * no other range exists, so this pin
459 * must then be of the function type.
460 *
461 * allow function type pins to select
462 * any combination of function/in/out
463 * in their MARK lists.
464 */
465 in_range = 1;
466 }
Magnus Damm42eed422008-10-22 18:29:17 +0900467 }
468
Magnus Damm2967dab2008-10-08 20:41:43 +0900469 if (!in_range)
470 continue;
471
Magnus Dammad4a07f2011-12-14 01:00:46 +0900472 if (get_config_reg(gpioc, enum_id, &cr,
473 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900474 goto out_err;
475
476 switch (cfg_mode) {
477 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900478 if (!*cntp ||
479 (read_config_reg(gpioc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900480 continue;
481 break;
482
483 case GPIO_CFG_REQ:
Magnus Dammad4a07f2011-12-14 01:00:46 +0900484 write_config_reg(gpioc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900485 *cntp = *cntp + 1;
486 break;
487
488 case GPIO_CFG_FREE:
489 *cntp = *cntp - 1;
490 break;
491 }
492 }
493
494 return 0;
495 out_err:
496 return -1;
497}
498
499static DEFINE_SPINLOCK(gpio_lock);
500
Magnus Damm69edbba2008-12-25 18:17:34 +0900501static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
Magnus Damm2967dab2008-10-08 20:41:43 +0900502{
Magnus Damm69edbba2008-12-25 18:17:34 +0900503 return container_of(chip, struct pinmux_info, chip);
504}
505
506static int sh_gpio_request(struct gpio_chip *chip, unsigned offset)
507{
508 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900509 struct pinmux_data_reg *dummy;
510 unsigned long flags;
511 int i, ret, pinmux_type;
512
513 ret = -EINVAL;
514
515 if (!gpioc)
516 goto err_out;
517
518 spin_lock_irqsave(&gpio_lock, flags);
519
Magnus Damm69edbba2008-12-25 18:17:34 +0900520 if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
Magnus Damm2967dab2008-10-08 20:41:43 +0900521 goto err_unlock;
522
523 /* setup pin function here if no data is associated with pin */
524
Magnus Damm69edbba2008-12-25 18:17:34 +0900525 if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900526 pinmux_type = PINMUX_TYPE_FUNCTION;
527 else
528 pinmux_type = PINMUX_TYPE_GPIO;
529
530 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
Magnus Damm69edbba2008-12-25 18:17:34 +0900531 if (pinmux_config_gpio(gpioc, offset,
Magnus Damm2967dab2008-10-08 20:41:43 +0900532 pinmux_type,
533 GPIO_CFG_DRYRUN) != 0)
534 goto err_unlock;
535
Magnus Damm69edbba2008-12-25 18:17:34 +0900536 if (pinmux_config_gpio(gpioc, offset,
Magnus Damm2967dab2008-10-08 20:41:43 +0900537 pinmux_type,
538 GPIO_CFG_REQ) != 0)
539 BUG();
540 }
541
Magnus Damm69edbba2008-12-25 18:17:34 +0900542 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
543 gpioc->gpios[offset].flags |= pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900544
545 ret = 0;
546 err_unlock:
547 spin_unlock_irqrestore(&gpio_lock, flags);
548 err_out:
549 return ret;
550}
Magnus Damm2967dab2008-10-08 20:41:43 +0900551
Magnus Damm69edbba2008-12-25 18:17:34 +0900552static void sh_gpio_free(struct gpio_chip *chip, unsigned offset)
Magnus Damm2967dab2008-10-08 20:41:43 +0900553{
Magnus Damm69edbba2008-12-25 18:17:34 +0900554 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900555 unsigned long flags;
556 int pinmux_type;
557
558 if (!gpioc)
559 return;
560
561 spin_lock_irqsave(&gpio_lock, flags);
562
Magnus Damm69edbba2008-12-25 18:17:34 +0900563 pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
564 pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
565 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
566 gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
Magnus Damm2967dab2008-10-08 20:41:43 +0900567
568 spin_unlock_irqrestore(&gpio_lock, flags);
569}
Magnus Damm2967dab2008-10-08 20:41:43 +0900570
571static int pinmux_direction(struct pinmux_info *gpioc,
572 unsigned gpio, int new_pinmux_type)
573{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900574 int pinmux_type;
575 int ret = -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900576
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900577 if (!gpioc)
578 goto err_out;
579
Magnus Damm2967dab2008-10-08 20:41:43 +0900580 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
581
582 switch (pinmux_type) {
583 case PINMUX_TYPE_GPIO:
584 break;
585 case PINMUX_TYPE_OUTPUT:
586 case PINMUX_TYPE_INPUT:
587 case PINMUX_TYPE_INPUT_PULLUP:
588 case PINMUX_TYPE_INPUT_PULLDOWN:
589 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
590 break;
591 default:
592 goto err_out;
593 }
594
595 if (pinmux_config_gpio(gpioc, gpio,
596 new_pinmux_type,
597 GPIO_CFG_DRYRUN) != 0)
598 goto err_out;
599
600 if (pinmux_config_gpio(gpioc, gpio,
601 new_pinmux_type,
602 GPIO_CFG_REQ) != 0)
603 BUG();
604
Magnus Damm18801be2008-12-25 18:17:09 +0900605 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
606 gpioc->gpios[gpio].flags |= new_pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900607
608 ret = 0;
609 err_out:
610 return ret;
611}
612
Magnus Damm69edbba2008-12-25 18:17:34 +0900613static int sh_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
Magnus Damm2967dab2008-10-08 20:41:43 +0900614{
Magnus Damm69edbba2008-12-25 18:17:34 +0900615 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900616 unsigned long flags;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900617 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900618
619 spin_lock_irqsave(&gpio_lock, flags);
Magnus Damm69edbba2008-12-25 18:17:34 +0900620 ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900621 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900622
Magnus Damm2967dab2008-10-08 20:41:43 +0900623 return ret;
624}
Magnus Damm2967dab2008-10-08 20:41:43 +0900625
Magnus Damm69edbba2008-12-25 18:17:34 +0900626static void sh_gpio_set_value(struct pinmux_info *gpioc,
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900627 unsigned gpio, int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900628{
629 struct pinmux_data_reg *dr = NULL;
630 int bit = 0;
631
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900632 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900633 BUG();
634 else
Magnus Damm32920942008-12-25 18:17:26 +0900635 gpio_write_bit(dr, bit, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900636}
637
Magnus Damm69edbba2008-12-25 18:17:34 +0900638static int sh_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
639 int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900640{
Magnus Damm69edbba2008-12-25 18:17:34 +0900641 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900642 unsigned long flags;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900643 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900644
Magnus Damm69edbba2008-12-25 18:17:34 +0900645 sh_gpio_set_value(gpioc, offset, value);
Magnus Damm32920942008-12-25 18:17:26 +0900646 spin_lock_irqsave(&gpio_lock, flags);
Magnus Damm69edbba2008-12-25 18:17:34 +0900647 ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900648 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900649
Magnus Damm2967dab2008-10-08 20:41:43 +0900650 return ret;
651}
Magnus Damm2967dab2008-10-08 20:41:43 +0900652
Magnus Damm69edbba2008-12-25 18:17:34 +0900653static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900654{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900655 struct pinmux_data_reg *dr = NULL;
656 int bit = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900657
Paul Mundte8184a42010-10-04 05:15:20 +0900658 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
659 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900660
Magnus Damm92554d92011-12-14 01:00:37 +0900661 return gpio_read_bit(dr, bit);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900662}
663
Magnus Damm69edbba2008-12-25 18:17:34 +0900664static int sh_gpio_get(struct gpio_chip *chip, unsigned offset)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900665{
Magnus Damm69edbba2008-12-25 18:17:34 +0900666 return sh_gpio_get_value(chip_to_pinmux(chip), offset);
Magnus Damm2967dab2008-10-08 20:41:43 +0900667}
Magnus Damm2967dab2008-10-08 20:41:43 +0900668
Magnus Damm69edbba2008-12-25 18:17:34 +0900669static void sh_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900670{
Magnus Damm69edbba2008-12-25 18:17:34 +0900671 sh_gpio_set_value(chip_to_pinmux(chip), offset, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900672}
Magnus Damm2967dab2008-10-08 20:41:43 +0900673
Magnus Dammad2a8e72011-09-28 16:50:58 +0900674static int sh_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
675{
676 struct pinmux_info *gpioc = chip_to_pinmux(chip);
677 pinmux_enum_t enum_id;
678 pinmux_enum_t *enum_ids;
679 int i, k, pos;
680
681 pos = 0;
682 enum_id = 0;
683 while (1) {
684 pos = get_gpio_enum_id(gpioc, offset, pos, &enum_id);
685 if (pos <= 0 || !enum_id)
686 break;
687
688 for (i = 0; i < gpioc->gpio_irq_size; i++) {
689 enum_ids = gpioc->gpio_irq[i].enum_ids;
690 for (k = 0; enum_ids[k]; k++) {
691 if (enum_ids[k] == enum_id)
692 return gpioc->gpio_irq[i].irq;
693 }
694 }
695 }
696
697 return -ENOSYS;
698}
699
Magnus Damm2967dab2008-10-08 20:41:43 +0900700int register_pinmux(struct pinmux_info *pip)
701{
Magnus Damm69edbba2008-12-25 18:17:34 +0900702 struct gpio_chip *chip = &pip->chip;
Magnus Dammb0e10212011-12-09 12:14:27 +0900703 int ret;
Magnus Damm69edbba2008-12-25 18:17:34 +0900704
Paul Mundtb72421d2010-10-04 03:54:56 +0900705 pr_info("%s handling gpio %d -> %d\n",
Magnus Damm2967dab2008-10-08 20:41:43 +0900706 pip->name, pip->first_gpio, pip->last_gpio);
707
Magnus Dammb0e10212011-12-09 12:14:27 +0900708 ret = pfc_ioremap(pip);
709 if (ret < 0)
710 return ret;
711
Magnus Damm69edbba2008-12-25 18:17:34 +0900712 setup_data_regs(pip);
713
714 chip->request = sh_gpio_request;
715 chip->free = sh_gpio_free;
716 chip->direction_input = sh_gpio_direction_input;
717 chip->get = sh_gpio_get;
718 chip->direction_output = sh_gpio_direction_output;
719 chip->set = sh_gpio_set;
Magnus Dammad2a8e72011-09-28 16:50:58 +0900720 chip->to_irq = sh_gpio_to_irq;
Magnus Damm69edbba2008-12-25 18:17:34 +0900721
722 WARN_ON(pip->first_gpio != 0); /* needs testing */
723
724 chip->label = pip->name;
725 chip->owner = THIS_MODULE;
726 chip->base = pip->first_gpio;
727 chip->ngpio = (pip->last_gpio - pip->first_gpio) + 1;
728
Magnus Dammb0e10212011-12-09 12:14:27 +0900729 ret = gpiochip_add(chip);
730 if (ret < 0)
731 pfc_iounmap(pip);
732
733 return ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900734}
Paul Mundtb72421d2010-10-04 03:54:56 +0900735
736int unregister_pinmux(struct pinmux_info *pip)
737{
738 pr_info("%s deregistering\n", pip->name);
Magnus Dammb0e10212011-12-09 12:14:27 +0900739 pfc_iounmap(pip);
Paul Mundtb72421d2010-10-04 03:54:56 +0900740 return gpiochip_remove(&pip->chip);
741}