2 * Pinmuxed GPIO support for SuperH.
3 * Copy from linux kernel driver/sh/pfc.c
5 * Copyright (C) 2008 Magnus Damm
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
15 #include <asm/bitops.h>
18 #include <linux/bitops.h>
19 #include <linux/bug.h>
21 static struct pinmux_info *gpioc;
23 #define pfc_phys_to_virt(p, a) ((void *)a)
25 static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
27 if (enum_id < r->begin)
36 static unsigned long gpio_read_raw_reg(void *mapped_reg,
37 unsigned long reg_width)
42 return readb(mapped_reg);
44 return readw(mapped_reg);
46 return readl(mapped_reg);
53 static void gpio_write_raw_reg(void *mapped_reg,
54 unsigned long reg_width,
59 writeb(data, mapped_reg);
62 writew(data, mapped_reg);
65 writel(data, mapped_reg);
72 static int gpio_read_bit(struct pinmux_data_reg *dr,
78 pos = dr->reg_width - (in_pos + 1);
80 debug("read_bit: addr = %lx, pos = %ld, r_width = %ld\n",
81 dr->reg + offset, pos, dr->reg_width);
83 return (gpio_read_raw_reg(dr->mapped_reg + offset,
84 dr->reg_width) >> pos) & 1;
87 static void gpio_write_bit(struct pinmux_data_reg *dr,
88 unsigned long in_pos, unsigned long value)
92 pos = dr->reg_width - (in_pos + 1);
94 debug("write_bit addr = %lx, value = %d, pos = %ld, "
96 dr->reg, !!value, pos, dr->reg_width);
99 __set_bit(pos, &dr->reg_shadow);
101 __clear_bit(pos, &dr->reg_shadow);
103 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
106 static void config_reg_helper(struct pinmux_info *gpioc,
107 struct pinmux_cfg_reg *crp,
108 unsigned long in_pos,
110 void __iomem **mapped_regp,
114 unsigned long *maskp,
119 *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
121 if (crp->field_width) {
122 *maskp = (1 << crp->field_width) - 1;
123 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
125 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
126 *posp = crp->reg_width;
127 for (k = 0; k <= in_pos; k++)
128 *posp -= crp->var_field_width[k];
132 static int read_config_reg(struct pinmux_info *gpioc,
133 struct pinmux_cfg_reg *crp,
138 unsigned long mask, pos;
140 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
142 debug("read_reg: addr = %lx, field = %ld, "
143 "r_width = %ld, f_width = %ld\n",
144 crp->reg, field, crp->reg_width, crp->field_width);
146 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
149 static void write_config_reg(struct pinmux_info *gpioc,
150 struct pinmux_cfg_reg *crp,
151 unsigned long field, unsigned long value)
154 unsigned long mask, pos, data;
156 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
158 debug("write_reg addr = %lx, value = %ld, field = %ld, "
159 "r_width = %ld, f_width = %ld\n",
160 crp->reg, value, field, crp->reg_width, crp->field_width);
162 mask = ~(mask << pos);
163 value = value << pos;
165 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
169 if (gpioc->unlock_reg)
170 gpio_write_raw_reg(pfc_phys_to_virt(gpioc, gpioc->unlock_reg),
173 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
176 static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
178 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
179 struct pinmux_data_reg *data_reg;
182 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
187 data_reg = gpioc->data_regs + k;
189 if (!data_reg->reg_width)
192 data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
194 for (n = 0; n < data_reg->reg_width; n++) {
195 if (data_reg->enum_ids[n] == gpiop->enum_id) {
196 gpiop->flags &= ~PINMUX_FLAG_DREG;
197 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
198 gpiop->flags &= ~PINMUX_FLAG_DBIT;
199 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
211 static void setup_data_regs(struct pinmux_info *gpioc)
213 struct pinmux_data_reg *drp;
216 for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
217 setup_data_reg(gpioc, k);
221 drp = gpioc->data_regs + k;
226 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
232 static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
233 struct pinmux_data_reg **drp, int *bitp)
235 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
238 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
241 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
242 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
243 *drp = gpioc->data_regs + k;
248 static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
249 struct pinmux_cfg_reg **crp,
250 int *fieldp, int *valuep,
251 unsigned long **cntp)
253 struct pinmux_cfg_reg *config_reg;
254 unsigned long r_width, f_width, curr_width, ncomb;
255 int k, m, n, pos, bit_pos;
259 config_reg = gpioc->cfg_regs + k;
261 r_width = config_reg->reg_width;
262 f_width = config_reg->field_width;
269 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
271 curr_width = f_width;
273 curr_width = config_reg->var_field_width[m];
275 ncomb = 1 << curr_width;
276 for (n = 0; n < ncomb; n++) {
277 if (config_reg->enum_ids[pos + n] == enum_id) {
281 *cntp = &config_reg->cnt[m];
294 static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
295 int pos, pinmux_enum_t *enum_idp)
297 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
298 pinmux_enum_t *data = gpioc->gpio_data;
301 if (!enum_in_range(enum_id, &gpioc->data)) {
302 if (!enum_in_range(enum_id, &gpioc->mark)) {
303 debug("non data/mark enum_id for gpio %d\n", gpio);
309 *enum_idp = data[pos + 1];
313 for (k = 0; k < gpioc->gpio_data_size; k++) {
314 if (data[k] == enum_id) {
315 *enum_idp = data[k + 1];
320 debug("cannot locate data/mark enum_id for gpio %d\n", gpio);
324 enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
326 static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
327 int pinmux_type, int cfg_mode)
329 struct pinmux_cfg_reg *cr = NULL;
330 pinmux_enum_t enum_id;
331 struct pinmux_range *range;
332 int in_range, pos, field, value;
335 switch (pinmux_type) {
337 case PINMUX_TYPE_FUNCTION:
341 case PINMUX_TYPE_OUTPUT:
342 range = &gpioc->output;
345 case PINMUX_TYPE_INPUT:
346 range = &gpioc->input;
349 case PINMUX_TYPE_INPUT_PULLUP:
350 range = &gpioc->input_pu;
353 case PINMUX_TYPE_INPUT_PULLDOWN:
354 range = &gpioc->input_pd;
366 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
373 /* first check if this is a function enum */
374 in_range = enum_in_range(enum_id, &gpioc->function);
376 /* not a function enum */
379 * other range exists, so this pin is
380 * a regular GPIO pin that now is being
381 * bound to a specific direction.
383 * for this case we only allow function enums
384 * and the enums that match the other range.
386 in_range = enum_in_range(enum_id, range);
389 * special case pass through for fixed
390 * input-only or output-only pins without
391 * function enum register association.
393 if (in_range && enum_id == range->force)
397 * no other range exists, so this pin
398 * must then be of the function type.
400 * allow function type pins to select
401 * any combination of function/in/out
402 * in their MARK lists.
411 if (get_config_reg(gpioc, enum_id, &cr,
412 &field, &value, &cntp) != 0)
416 case GPIO_CFG_DRYRUN:
418 (read_config_reg(gpioc, cr, field) != value))
423 write_config_reg(gpioc, cr, field, value);
439 static DEFINE_SPINLOCK(gpio_lock);
440 static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
442 return container_of(chip, struct pinmux_info, chip);
446 static int sh_gpio_request(unsigned offset)
448 struct pinmux_data_reg *dummy;
449 int i, ret, pinmux_type;
456 if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
459 /* setup pin function here if no data is associated with pin */
461 if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
462 pinmux_type = PINMUX_TYPE_FUNCTION;
464 pinmux_type = PINMUX_TYPE_GPIO;
466 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
467 if (pinmux_config_gpio(gpioc, offset,
469 GPIO_CFG_DRYRUN) != 0)
472 if (pinmux_config_gpio(gpioc, offset,
478 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
479 gpioc->gpios[offset].flags |= pinmux_type;
486 static void sh_gpio_free(unsigned offset)
493 pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
494 pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
495 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
496 gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
499 static int pinmux_direction(struct pinmux_info *gpioc,
500 unsigned gpio, int new_pinmux_type)
508 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
510 switch (pinmux_type) {
511 case PINMUX_TYPE_GPIO:
513 case PINMUX_TYPE_OUTPUT:
514 case PINMUX_TYPE_INPUT:
515 case PINMUX_TYPE_INPUT_PULLUP:
516 case PINMUX_TYPE_INPUT_PULLDOWN:
517 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
523 if (pinmux_config_gpio(gpioc, gpio,
525 GPIO_CFG_DRYRUN) != 0)
528 if (pinmux_config_gpio(gpioc, gpio,
533 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
534 gpioc->gpios[gpio].flags |= new_pinmux_type;
541 static int sh_gpio_direction_input(unsigned offset)
543 return pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
546 static void sh_gpio_set_value(struct pinmux_info *gpioc,
547 unsigned gpio, int value)
549 struct pinmux_data_reg *dr = NULL;
552 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
555 gpio_write_bit(dr, bit, value);
558 static int sh_gpio_direction_output(unsigned offset, int value)
560 sh_gpio_set_value(gpioc, offset, value);
561 return pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
564 static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
566 struct pinmux_data_reg *dr = NULL;
567 int bit = 0, offset = 0;
569 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
571 #if defined(CONFIG_RCAR_GEN3)
572 if ((gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_INPUT)
576 return gpio_read_bit(dr, offset, bit);
579 static int sh_gpio_get(unsigned offset)
581 return sh_gpio_get_value(gpioc, offset);
584 static void sh_gpio_set(unsigned offset, int value)
586 sh_gpio_set_value(gpioc, offset, value);
589 int register_pinmux(struct pinmux_info *pip)
593 debug("%s deregistering\n", pip->name);
594 setup_data_regs(gpioc);
599 int unregister_pinmux(struct pinmux_info *pip)
601 debug("%s deregistering\n", pip->name);
609 int gpio_request(unsigned gpio, const char *label)
611 sh_gpio_request(gpio);
615 int gpio_free(unsigned gpio)
621 int gpio_direction_input(unsigned gpio)
623 return sh_gpio_direction_input(gpio);
626 int gpio_direction_output(unsigned gpio, int value)
628 return sh_gpio_direction_output(gpio, value);
631 void gpio_set_value(unsigned gpio, int value)
633 sh_gpio_set(gpio, value);
636 int gpio_get_value(unsigned gpio)
638 return sh_gpio_get(gpio);