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>
19 static struct pinmux_info *gpioc;
21 #define pfc_phys_to_virt(p, a) ((void *)a)
23 static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
25 if (enum_id < r->begin)
34 static unsigned long gpio_read_raw_reg(void *mapped_reg,
35 unsigned long reg_width)
40 return readb(mapped_reg);
42 return readw(mapped_reg);
44 return readl(mapped_reg);
51 static void gpio_write_raw_reg(void *mapped_reg,
52 unsigned long reg_width,
57 writeb(data, mapped_reg);
60 writew(data, mapped_reg);
63 writel(data, mapped_reg);
70 static int gpio_read_bit(struct pinmux_data_reg *dr,
76 pos = dr->reg_width - (in_pos + 1);
78 debug("read_bit: addr = %lx, pos = %ld, r_width = %ld\n",
79 dr->reg + offset, pos, dr->reg_width);
81 return (gpio_read_raw_reg(dr->mapped_reg + offset,
82 dr->reg_width) >> pos) & 1;
85 static void gpio_write_bit(struct pinmux_data_reg *dr,
86 unsigned long in_pos, unsigned long value)
90 pos = dr->reg_width - (in_pos + 1);
92 debug("write_bit addr = %lx, value = %d, pos = %ld, "
94 dr->reg, !!value, pos, dr->reg_width);
97 __set_bit(pos, &dr->reg_shadow);
99 __clear_bit(pos, &dr->reg_shadow);
101 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
104 static void config_reg_helper(struct pinmux_info *gpioc,
105 struct pinmux_cfg_reg *crp,
106 unsigned long in_pos,
108 void __iomem **mapped_regp,
112 unsigned long *maskp,
117 *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
119 if (crp->field_width) {
120 *maskp = (1 << crp->field_width) - 1;
121 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
123 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
124 *posp = crp->reg_width;
125 for (k = 0; k <= in_pos; k++)
126 *posp -= crp->var_field_width[k];
130 static int read_config_reg(struct pinmux_info *gpioc,
131 struct pinmux_cfg_reg *crp,
136 unsigned long mask, pos;
138 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
140 debug("read_reg: addr = %lx, field = %ld, "
141 "r_width = %ld, f_width = %ld\n",
142 crp->reg, field, crp->reg_width, crp->field_width);
144 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
147 static void write_config_reg(struct pinmux_info *gpioc,
148 struct pinmux_cfg_reg *crp,
149 unsigned long field, unsigned long value)
152 unsigned long mask, pos, data;
154 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
156 debug("write_reg addr = %lx, value = %ld, field = %ld, "
157 "r_width = %ld, f_width = %ld\n",
158 crp->reg, value, field, crp->reg_width, crp->field_width);
160 mask = ~(mask << pos);
161 value = value << pos;
163 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
167 if (gpioc->unlock_reg)
168 gpio_write_raw_reg(pfc_phys_to_virt(gpioc, gpioc->unlock_reg),
171 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
174 static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
176 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
177 struct pinmux_data_reg *data_reg;
180 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
185 data_reg = gpioc->data_regs + k;
187 if (!data_reg->reg_width)
190 data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
192 for (n = 0; n < data_reg->reg_width; n++) {
193 if (data_reg->enum_ids[n] == gpiop->enum_id) {
194 gpiop->flags &= ~PINMUX_FLAG_DREG;
195 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
196 gpiop->flags &= ~PINMUX_FLAG_DBIT;
197 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
209 static void setup_data_regs(struct pinmux_info *gpioc)
211 struct pinmux_data_reg *drp;
214 for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
215 setup_data_reg(gpioc, k);
219 drp = gpioc->data_regs + k;
224 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
230 static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
231 struct pinmux_data_reg **drp, int *bitp)
233 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
236 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
239 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
240 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
241 *drp = gpioc->data_regs + k;
246 static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
247 struct pinmux_cfg_reg **crp,
248 int *fieldp, int *valuep,
249 unsigned long **cntp)
251 struct pinmux_cfg_reg *config_reg;
252 unsigned long r_width, f_width, curr_width, ncomb;
253 int k, m, n, pos, bit_pos;
257 config_reg = gpioc->cfg_regs + k;
259 r_width = config_reg->reg_width;
260 f_width = config_reg->field_width;
267 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
269 curr_width = f_width;
271 curr_width = config_reg->var_field_width[m];
273 ncomb = 1 << curr_width;
274 for (n = 0; n < ncomb; n++) {
275 if (config_reg->enum_ids[pos + n] == enum_id) {
279 *cntp = &config_reg->cnt[m];
292 static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
293 int pos, pinmux_enum_t *enum_idp)
295 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
296 pinmux_enum_t *data = gpioc->gpio_data;
299 if (!enum_in_range(enum_id, &gpioc->data)) {
300 if (!enum_in_range(enum_id, &gpioc->mark)) {
301 debug("non data/mark enum_id for gpio %d\n", gpio);
307 *enum_idp = data[pos + 1];
311 for (k = 0; k < gpioc->gpio_data_size; k++) {
312 if (data[k] == enum_id) {
313 *enum_idp = data[k + 1];
318 debug("cannot locate data/mark enum_id for gpio %d\n", gpio);
322 enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
324 static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
325 int pinmux_type, int cfg_mode)
327 struct pinmux_cfg_reg *cr = NULL;
328 pinmux_enum_t enum_id;
329 struct pinmux_range *range;
330 int in_range, pos, field, value;
333 switch (pinmux_type) {
335 case PINMUX_TYPE_FUNCTION:
339 case PINMUX_TYPE_OUTPUT:
340 range = &gpioc->output;
343 case PINMUX_TYPE_INPUT:
344 range = &gpioc->input;
347 case PINMUX_TYPE_INPUT_PULLUP:
348 range = &gpioc->input_pu;
351 case PINMUX_TYPE_INPUT_PULLDOWN:
352 range = &gpioc->input_pd;
364 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
371 /* first check if this is a function enum */
372 in_range = enum_in_range(enum_id, &gpioc->function);
374 /* not a function enum */
377 * other range exists, so this pin is
378 * a regular GPIO pin that now is being
379 * bound to a specific direction.
381 * for this case we only allow function enums
382 * and the enums that match the other range.
384 in_range = enum_in_range(enum_id, range);
387 * special case pass through for fixed
388 * input-only or output-only pins without
389 * function enum register association.
391 if (in_range && enum_id == range->force)
395 * no other range exists, so this pin
396 * must then be of the function type.
398 * allow function type pins to select
399 * any combination of function/in/out
400 * in their MARK lists.
409 if (get_config_reg(gpioc, enum_id, &cr,
410 &field, &value, &cntp) != 0)
414 case GPIO_CFG_DRYRUN:
416 (read_config_reg(gpioc, cr, field) != value))
421 write_config_reg(gpioc, cr, field, value);
437 static DEFINE_SPINLOCK(gpio_lock);
438 static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
440 return container_of(chip, struct pinmux_info, chip);
444 static int sh_gpio_request(unsigned offset)
446 struct pinmux_data_reg *dummy;
447 int i, ret, pinmux_type;
454 if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
457 /* setup pin function here if no data is associated with pin */
459 if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
460 pinmux_type = PINMUX_TYPE_FUNCTION;
462 pinmux_type = PINMUX_TYPE_GPIO;
464 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
465 if (pinmux_config_gpio(gpioc, offset,
467 GPIO_CFG_DRYRUN) != 0)
470 if (pinmux_config_gpio(gpioc, offset,
476 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
477 gpioc->gpios[offset].flags |= pinmux_type;
484 static void sh_gpio_free(unsigned offset)
491 pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
492 pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
493 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
494 gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
497 static int pinmux_direction(struct pinmux_info *gpioc,
498 unsigned gpio, int new_pinmux_type)
506 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
508 switch (pinmux_type) {
509 case PINMUX_TYPE_GPIO:
511 case PINMUX_TYPE_OUTPUT:
512 case PINMUX_TYPE_INPUT:
513 case PINMUX_TYPE_INPUT_PULLUP:
514 case PINMUX_TYPE_INPUT_PULLDOWN:
515 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
521 if (pinmux_config_gpio(gpioc, gpio,
523 GPIO_CFG_DRYRUN) != 0)
526 if (pinmux_config_gpio(gpioc, gpio,
531 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
532 gpioc->gpios[gpio].flags |= new_pinmux_type;
539 static int sh_gpio_direction_input(unsigned offset)
541 return pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
544 static void sh_gpio_set_value(struct pinmux_info *gpioc,
545 unsigned gpio, int value)
547 struct pinmux_data_reg *dr = NULL;
550 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
553 gpio_write_bit(dr, bit, value);
556 static int sh_gpio_direction_output(unsigned offset, int value)
558 sh_gpio_set_value(gpioc, offset, value);
559 return pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
562 static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
564 struct pinmux_data_reg *dr = NULL;
565 int bit = 0, offset = 0;
567 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
569 #if defined(CONFIG_RCAR_GEN3)
570 if ((gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_INPUT)
574 return gpio_read_bit(dr, offset, bit);
577 static int sh_gpio_get(unsigned offset)
579 return sh_gpio_get_value(gpioc, offset);
582 static void sh_gpio_set(unsigned offset, int value)
584 sh_gpio_set_value(gpioc, offset, value);
587 int register_pinmux(struct pinmux_info *pip)
591 debug("%s deregistering\n", pip->name);
592 setup_data_regs(gpioc);
597 int unregister_pinmux(struct pinmux_info *pip)
599 debug("%s deregistering\n", pip->name);
607 int gpio_request(unsigned gpio, const char *label)
609 sh_gpio_request(gpio);
613 int gpio_free(unsigned gpio)
619 int gpio_direction_input(unsigned gpio)
621 return sh_gpio_direction_input(gpio);
624 int gpio_direction_output(unsigned gpio, int value)
626 return sh_gpio_direction_output(gpio, value);
629 void gpio_set_value(unsigned gpio, int value)
631 sh_gpio_set(gpio, value);
634 int gpio_get_value(unsigned gpio)
636 return sh_gpio_get(gpio);