blob: 5481d19518f9c96bea121ab9e8137edf8fbc13d6 [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 Damm18925e12011-12-14 01:00:55 +0900177 *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900178
Magnus Damm18925e12011-12-14 01:00:55 +0900179 *maskp = (1 << crp->field_width) - 1;
180 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900181}
Magnus Damm2967dab2008-10-08 20:41:43 +0900182
Magnus Damm18925e12011-12-14 01:00:55 +0900183static int read_config_reg(struct pinmux_info *gpioc,
184 struct pinmux_cfg_reg *crp,
185 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900186{
Magnus Damm18925e12011-12-14 01:00:55 +0900187 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900188 unsigned long mask, pos;
189
Magnus Damm18925e12011-12-14 01:00:55 +0900190 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900191
Magnus Damm18925e12011-12-14 01:00:55 +0900192 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900193 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900194 crp->reg, field, crp->reg_width, crp->field_width);
195
196 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
197}
198
199static void write_config_reg(struct pinmux_info *gpioc,
200 struct pinmux_cfg_reg *crp,
201 unsigned long field, unsigned long value)
202{
203 void __iomem *mapped_reg;
204 unsigned long mask, pos;
205
206 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
207
208 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
209 "r_width = %ld, f_width = %ld\n",
210 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900211
212 mask = ~(mask << pos);
213 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900214
Magnus Damm18925e12011-12-14 01:00:55 +0900215 switch (crp->reg_width) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900216 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900217 iowrite8((ioread8(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900218 break;
219 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900220 iowrite16((ioread16(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900221 break;
222 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900223 iowrite32((ioread32(mapped_reg) & mask) | value, mapped_reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900224 break;
225 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900226}
227
Magnus Damm18801be2008-12-25 18:17:09 +0900228static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900229{
Magnus Damm18801be2008-12-25 18:17:09 +0900230 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900231 struct pinmux_data_reg *data_reg;
232 int k, n;
233
Magnus Damm18801be2008-12-25 18:17:09 +0900234 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900235 return -1;
236
237 k = 0;
238 while (1) {
239 data_reg = gpioc->data_regs + k;
240
241 if (!data_reg->reg_width)
242 break;
243
Magnus Dammb0e10212011-12-09 12:14:27 +0900244 data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
245
Magnus Damm2967dab2008-10-08 20:41:43 +0900246 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900247 if (data_reg->enum_ids[n] == gpiop->enum_id) {
248 gpiop->flags &= ~PINMUX_FLAG_DREG;
249 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
250 gpiop->flags &= ~PINMUX_FLAG_DBIT;
251 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900252 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900253 }
254 }
255 k++;
256 }
257
Magnus Damm18801be2008-12-25 18:17:09 +0900258 BUG();
259
Magnus Damm2967dab2008-10-08 20:41:43 +0900260 return -1;
261}
262
Magnus Damm32920942008-12-25 18:17:26 +0900263static void setup_data_regs(struct pinmux_info *gpioc)
264{
265 struct pinmux_data_reg *drp;
266 int k;
267
268 for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
269 setup_data_reg(gpioc, k);
270
271 k = 0;
272 while (1) {
273 drp = gpioc->data_regs + k;
274
275 if (!drp->reg_width)
276 break;
277
Magnus Dammb0e10212011-12-09 12:14:27 +0900278 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
279 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900280 k++;
281 }
282}
283
Magnus Damm18801be2008-12-25 18:17:09 +0900284static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
285 struct pinmux_data_reg **drp, int *bitp)
286{
287 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
288 int k, n;
289
290 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
291 return -1;
292
293 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
294 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
295 *drp = gpioc->data_regs + k;
296 *bitp = n;
297 return 0;
298}
299
Magnus Damm2967dab2008-10-08 20:41:43 +0900300static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900301 struct pinmux_cfg_reg **crp,
302 int *fieldp, int *valuep,
Magnus Damm2967dab2008-10-08 20:41:43 +0900303 unsigned long **cntp)
304{
305 struct pinmux_cfg_reg *config_reg;
306 unsigned long r_width, f_width;
307 int k, n;
308
309 k = 0;
310 while (1) {
311 config_reg = gpioc->cfg_regs + k;
312
313 r_width = config_reg->reg_width;
314 f_width = config_reg->field_width;
315
316 if (!r_width)
317 break;
Magnus Dammc63bcc62011-10-17 18:01:19 +0900318 for (n = 0; n < (r_width / f_width) * (1 << f_width); n++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900319 if (config_reg->enum_ids[n] == enum_id) {
320 *crp = config_reg;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900321 *fieldp = n / (1 << f_width);
322 *valuep = n % (1 << f_width);
Magnus Damm2967dab2008-10-08 20:41:43 +0900323 *cntp = &config_reg->cnt[n / (1 << f_width)];
324 return 0;
325 }
326 }
327 k++;
328 }
329
330 return -1;
331}
332
333static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
334 int pos, pinmux_enum_t *enum_idp)
335{
336 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
337 pinmux_enum_t *data = gpioc->gpio_data;
338 int k;
339
340 if (!enum_in_range(enum_id, &gpioc->data)) {
341 if (!enum_in_range(enum_id, &gpioc->mark)) {
342 pr_err("non data/mark enum_id for gpio %d\n", gpio);
343 return -1;
344 }
345 }
346
347 if (pos) {
348 *enum_idp = data[pos + 1];
349 return pos + 1;
350 }
351
352 for (k = 0; k < gpioc->gpio_data_size; k++) {
353 if (data[k] == enum_id) {
354 *enum_idp = data[k + 1];
355 return k + 1;
356 }
357 }
358
359 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
360 return -1;
361}
362
Magnus Damm2967dab2008-10-08 20:41:43 +0900363enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
364
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900365static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
366 int pinmux_type, int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900367{
368 struct pinmux_cfg_reg *cr = NULL;
369 pinmux_enum_t enum_id;
370 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900371 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900372 unsigned long *cntp;
373
374 switch (pinmux_type) {
375
376 case PINMUX_TYPE_FUNCTION:
377 range = NULL;
378 break;
379
380 case PINMUX_TYPE_OUTPUT:
381 range = &gpioc->output;
382 break;
383
384 case PINMUX_TYPE_INPUT:
385 range = &gpioc->input;
386 break;
387
388 case PINMUX_TYPE_INPUT_PULLUP:
389 range = &gpioc->input_pu;
390 break;
391
392 case PINMUX_TYPE_INPUT_PULLDOWN:
393 range = &gpioc->input_pd;
394 break;
395
396 default:
397 goto out_err;
398 }
399
400 pos = 0;
401 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900402 field = 0;
403 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900404 while (1) {
405 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
406 if (pos <= 0)
407 goto out_err;
408
409 if (!enum_id)
410 break;
411
Magnus Damm50dd3142010-01-19 13:52:28 +0000412 /* first check if this is a function enum */
Magnus Damm2967dab2008-10-08 20:41:43 +0900413 in_range = enum_in_range(enum_id, &gpioc->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000414 if (!in_range) {
415 /* not a function enum */
416 if (range) {
417 /*
418 * other range exists, so this pin is
419 * a regular GPIO pin that now is being
420 * bound to a specific direction.
421 *
422 * for this case we only allow function enums
423 * and the enums that match the other range.
424 */
425 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900426
Magnus Damm50dd3142010-01-19 13:52:28 +0000427 /*
428 * special case pass through for fixed
429 * input-only or output-only pins without
430 * function enum register association.
431 */
432 if (in_range && enum_id == range->force)
433 continue;
434 } else {
435 /*
436 * no other range exists, so this pin
437 * must then be of the function type.
438 *
439 * allow function type pins to select
440 * any combination of function/in/out
441 * in their MARK lists.
442 */
443 in_range = 1;
444 }
Magnus Damm42eed422008-10-22 18:29:17 +0900445 }
446
Magnus Damm2967dab2008-10-08 20:41:43 +0900447 if (!in_range)
448 continue;
449
Magnus Dammad4a07f2011-12-14 01:00:46 +0900450 if (get_config_reg(gpioc, enum_id, &cr,
451 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900452 goto out_err;
453
454 switch (cfg_mode) {
455 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900456 if (!*cntp ||
457 (read_config_reg(gpioc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900458 continue;
459 break;
460
461 case GPIO_CFG_REQ:
Magnus Dammad4a07f2011-12-14 01:00:46 +0900462 write_config_reg(gpioc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900463 *cntp = *cntp + 1;
464 break;
465
466 case GPIO_CFG_FREE:
467 *cntp = *cntp - 1;
468 break;
469 }
470 }
471
472 return 0;
473 out_err:
474 return -1;
475}
476
477static DEFINE_SPINLOCK(gpio_lock);
478
Magnus Damm69edbba2008-12-25 18:17:34 +0900479static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
Magnus Damm2967dab2008-10-08 20:41:43 +0900480{
Magnus Damm69edbba2008-12-25 18:17:34 +0900481 return container_of(chip, struct pinmux_info, chip);
482}
483
484static int sh_gpio_request(struct gpio_chip *chip, unsigned offset)
485{
486 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900487 struct pinmux_data_reg *dummy;
488 unsigned long flags;
489 int i, ret, pinmux_type;
490
491 ret = -EINVAL;
492
493 if (!gpioc)
494 goto err_out;
495
496 spin_lock_irqsave(&gpio_lock, flags);
497
Magnus Damm69edbba2008-12-25 18:17:34 +0900498 if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
Magnus Damm2967dab2008-10-08 20:41:43 +0900499 goto err_unlock;
500
501 /* setup pin function here if no data is associated with pin */
502
Magnus Damm69edbba2008-12-25 18:17:34 +0900503 if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900504 pinmux_type = PINMUX_TYPE_FUNCTION;
505 else
506 pinmux_type = PINMUX_TYPE_GPIO;
507
508 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
Magnus Damm69edbba2008-12-25 18:17:34 +0900509 if (pinmux_config_gpio(gpioc, offset,
Magnus Damm2967dab2008-10-08 20:41:43 +0900510 pinmux_type,
511 GPIO_CFG_DRYRUN) != 0)
512 goto err_unlock;
513
Magnus Damm69edbba2008-12-25 18:17:34 +0900514 if (pinmux_config_gpio(gpioc, offset,
Magnus Damm2967dab2008-10-08 20:41:43 +0900515 pinmux_type,
516 GPIO_CFG_REQ) != 0)
517 BUG();
518 }
519
Magnus Damm69edbba2008-12-25 18:17:34 +0900520 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
521 gpioc->gpios[offset].flags |= pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900522
523 ret = 0;
524 err_unlock:
525 spin_unlock_irqrestore(&gpio_lock, flags);
526 err_out:
527 return ret;
528}
Magnus Damm2967dab2008-10-08 20:41:43 +0900529
Magnus Damm69edbba2008-12-25 18:17:34 +0900530static void sh_gpio_free(struct gpio_chip *chip, unsigned offset)
Magnus Damm2967dab2008-10-08 20:41:43 +0900531{
Magnus Damm69edbba2008-12-25 18:17:34 +0900532 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900533 unsigned long flags;
534 int pinmux_type;
535
536 if (!gpioc)
537 return;
538
539 spin_lock_irqsave(&gpio_lock, flags);
540
Magnus Damm69edbba2008-12-25 18:17:34 +0900541 pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
542 pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
543 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
544 gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
Magnus Damm2967dab2008-10-08 20:41:43 +0900545
546 spin_unlock_irqrestore(&gpio_lock, flags);
547}
Magnus Damm2967dab2008-10-08 20:41:43 +0900548
549static int pinmux_direction(struct pinmux_info *gpioc,
550 unsigned gpio, int new_pinmux_type)
551{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900552 int pinmux_type;
553 int ret = -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900554
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900555 if (!gpioc)
556 goto err_out;
557
Magnus Damm2967dab2008-10-08 20:41:43 +0900558 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
559
560 switch (pinmux_type) {
561 case PINMUX_TYPE_GPIO:
562 break;
563 case PINMUX_TYPE_OUTPUT:
564 case PINMUX_TYPE_INPUT:
565 case PINMUX_TYPE_INPUT_PULLUP:
566 case PINMUX_TYPE_INPUT_PULLDOWN:
567 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
568 break;
569 default:
570 goto err_out;
571 }
572
573 if (pinmux_config_gpio(gpioc, gpio,
574 new_pinmux_type,
575 GPIO_CFG_DRYRUN) != 0)
576 goto err_out;
577
578 if (pinmux_config_gpio(gpioc, gpio,
579 new_pinmux_type,
580 GPIO_CFG_REQ) != 0)
581 BUG();
582
Magnus Damm18801be2008-12-25 18:17:09 +0900583 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
584 gpioc->gpios[gpio].flags |= new_pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900585
586 ret = 0;
587 err_out:
588 return ret;
589}
590
Magnus Damm69edbba2008-12-25 18:17:34 +0900591static int sh_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
Magnus Damm2967dab2008-10-08 20:41:43 +0900592{
Magnus Damm69edbba2008-12-25 18:17:34 +0900593 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900594 unsigned long flags;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900595 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900596
597 spin_lock_irqsave(&gpio_lock, flags);
Magnus Damm69edbba2008-12-25 18:17:34 +0900598 ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900599 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900600
Magnus Damm2967dab2008-10-08 20:41:43 +0900601 return ret;
602}
Magnus Damm2967dab2008-10-08 20:41:43 +0900603
Magnus Damm69edbba2008-12-25 18:17:34 +0900604static void sh_gpio_set_value(struct pinmux_info *gpioc,
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900605 unsigned gpio, int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900606{
607 struct pinmux_data_reg *dr = NULL;
608 int bit = 0;
609
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900610 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900611 BUG();
612 else
Magnus Damm32920942008-12-25 18:17:26 +0900613 gpio_write_bit(dr, bit, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900614}
615
Magnus Damm69edbba2008-12-25 18:17:34 +0900616static int sh_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
617 int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900618{
Magnus Damm69edbba2008-12-25 18:17:34 +0900619 struct pinmux_info *gpioc = chip_to_pinmux(chip);
Magnus Damm2967dab2008-10-08 20:41:43 +0900620 unsigned long flags;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900621 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900622
Magnus Damm69edbba2008-12-25 18:17:34 +0900623 sh_gpio_set_value(gpioc, offset, value);
Magnus Damm32920942008-12-25 18:17:26 +0900624 spin_lock_irqsave(&gpio_lock, flags);
Magnus Damm69edbba2008-12-25 18:17:34 +0900625 ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900626 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900627
Magnus Damm2967dab2008-10-08 20:41:43 +0900628 return ret;
629}
Magnus Damm2967dab2008-10-08 20:41:43 +0900630
Magnus Damm69edbba2008-12-25 18:17:34 +0900631static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900632{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900633 struct pinmux_data_reg *dr = NULL;
634 int bit = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900635
Paul Mundte8184a42010-10-04 05:15:20 +0900636 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
637 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900638
Magnus Damm92554d92011-12-14 01:00:37 +0900639 return gpio_read_bit(dr, bit);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900640}
641
Magnus Damm69edbba2008-12-25 18:17:34 +0900642static int sh_gpio_get(struct gpio_chip *chip, unsigned offset)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900643{
Magnus Damm69edbba2008-12-25 18:17:34 +0900644 return sh_gpio_get_value(chip_to_pinmux(chip), offset);
Magnus Damm2967dab2008-10-08 20:41:43 +0900645}
Magnus Damm2967dab2008-10-08 20:41:43 +0900646
Magnus Damm69edbba2008-12-25 18:17:34 +0900647static void sh_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900648{
Magnus Damm69edbba2008-12-25 18:17:34 +0900649 sh_gpio_set_value(chip_to_pinmux(chip), offset, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900650}
Magnus Damm2967dab2008-10-08 20:41:43 +0900651
Magnus Dammad2a8e72011-09-28 16:50:58 +0900652static int sh_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
653{
654 struct pinmux_info *gpioc = chip_to_pinmux(chip);
655 pinmux_enum_t enum_id;
656 pinmux_enum_t *enum_ids;
657 int i, k, pos;
658
659 pos = 0;
660 enum_id = 0;
661 while (1) {
662 pos = get_gpio_enum_id(gpioc, offset, pos, &enum_id);
663 if (pos <= 0 || !enum_id)
664 break;
665
666 for (i = 0; i < gpioc->gpio_irq_size; i++) {
667 enum_ids = gpioc->gpio_irq[i].enum_ids;
668 for (k = 0; enum_ids[k]; k++) {
669 if (enum_ids[k] == enum_id)
670 return gpioc->gpio_irq[i].irq;
671 }
672 }
673 }
674
675 return -ENOSYS;
676}
677
Magnus Damm2967dab2008-10-08 20:41:43 +0900678int register_pinmux(struct pinmux_info *pip)
679{
Magnus Damm69edbba2008-12-25 18:17:34 +0900680 struct gpio_chip *chip = &pip->chip;
Magnus Dammb0e10212011-12-09 12:14:27 +0900681 int ret;
Magnus Damm69edbba2008-12-25 18:17:34 +0900682
Paul Mundtb72421d2010-10-04 03:54:56 +0900683 pr_info("%s handling gpio %d -> %d\n",
Magnus Damm2967dab2008-10-08 20:41:43 +0900684 pip->name, pip->first_gpio, pip->last_gpio);
685
Magnus Dammb0e10212011-12-09 12:14:27 +0900686 ret = pfc_ioremap(pip);
687 if (ret < 0)
688 return ret;
689
Magnus Damm69edbba2008-12-25 18:17:34 +0900690 setup_data_regs(pip);
691
692 chip->request = sh_gpio_request;
693 chip->free = sh_gpio_free;
694 chip->direction_input = sh_gpio_direction_input;
695 chip->get = sh_gpio_get;
696 chip->direction_output = sh_gpio_direction_output;
697 chip->set = sh_gpio_set;
Magnus Dammad2a8e72011-09-28 16:50:58 +0900698 chip->to_irq = sh_gpio_to_irq;
Magnus Damm69edbba2008-12-25 18:17:34 +0900699
700 WARN_ON(pip->first_gpio != 0); /* needs testing */
701
702 chip->label = pip->name;
703 chip->owner = THIS_MODULE;
704 chip->base = pip->first_gpio;
705 chip->ngpio = (pip->last_gpio - pip->first_gpio) + 1;
706
Magnus Dammb0e10212011-12-09 12:14:27 +0900707 ret = gpiochip_add(chip);
708 if (ret < 0)
709 pfc_iounmap(pip);
710
711 return ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900712}
Paul Mundtb72421d2010-10-04 03:54:56 +0900713
714int unregister_pinmux(struct pinmux_info *pip)
715{
716 pr_info("%s deregistering\n", pip->name);
Magnus Dammb0e10212011-12-09 12:14:27 +0900717 pfc_iounmap(pip);
Paul Mundtb72421d2010-10-04 03:54:56 +0900718 return gpiochip_remove(&pip->chip);
719}