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/bug.h>
20 static struct pinmux_info *gpioc;
22 #define pfc_phys_to_virt(p, a) ((void *)a)
24 static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
26 if (enum_id < r->begin)
35 static unsigned long gpio_read_raw_reg(void *mapped_reg,
36 unsigned long reg_width)
41 return readb(mapped_reg);
43 return readw(mapped_reg);
45 return readl(mapped_reg);
52 static void gpio_write_raw_reg(void *mapped_reg,
53 unsigned long reg_width,
58 writeb(data, mapped_reg);
61 writew(data, mapped_reg);
64 writel(data, mapped_reg);
71 static int gpio_read_bit(struct pinmux_data_reg *dr,
77 pos = dr->reg_width - (in_pos + 1);
79 debug("read_bit: addr = %lx, pos = %ld, r_width = %ld\n",
80 dr->reg + offset, pos, dr->reg_width);
82 return (gpio_read_raw_reg(dr->mapped_reg + offset,
83 dr->reg_width) >> pos) & 1;
86 static void gpio_write_bit(struct pinmux_data_reg *dr,
87 unsigned long in_pos, unsigned long value)
91 pos = dr->reg_width - (in_pos + 1);
93 debug("write_bit addr = %lx, value = %d, pos = %ld, "
95 dr->reg, !!value, pos, dr->reg_width);
98 __set_bit(pos, &dr->reg_shadow);
100 __clear_bit(pos, &dr->reg_shadow);
102 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
105 static void config_reg_helper(struct pinmux_info *gpioc,
106 struct pinmux_cfg_reg *crp,
107 unsigned long in_pos,
109 void __iomem **mapped_regp,
113 unsigned long *maskp,
118 *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
120 if (crp->field_width) {
121 *maskp = (1 << crp->field_width) - 1;
122 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
124 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
125 *posp = crp->reg_width;
126 for (k = 0; k <= in_pos; k++)
127 *posp -= crp->var_field_width[k];
131 static int read_config_reg(struct pinmux_info *gpioc,
132 struct pinmux_cfg_reg *crp,
137 unsigned long mask, pos;
139 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
141 debug("read_reg: addr = %lx, field = %ld, "
142 "r_width = %ld, f_width = %ld\n",
143 crp->reg, field, crp->reg_width, crp->field_width);
145 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
148 static void write_config_reg(struct pinmux_info *gpioc,
149 struct pinmux_cfg_reg *crp,
150 unsigned long field, unsigned long value)
153 unsigned long mask, pos, data;
155 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
157 debug("write_reg addr = %lx, value = %ld, field = %ld, "
158 "r_width = %ld, f_width = %ld\n",
159 crp->reg, value, field, crp->reg_width, crp->field_width);
161 mask = ~(mask << pos);
162 value = value << pos;
164 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
168 if (gpioc->unlock_reg)
169 gpio_write_raw_reg(pfc_phys_to_virt(gpioc, gpioc->unlock_reg),
172 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
175 static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
177 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
178 struct pinmux_data_reg *data_reg;
181 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
186 data_reg = gpioc->data_regs + k;
188 if (!data_reg->reg_width)
191 data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
193 for (n = 0; n < data_reg->reg_width; n++) {
194 if (data_reg->enum_ids[n] == gpiop->enum_id) {
195 gpiop->flags &= ~PINMUX_FLAG_DREG;
196 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
197 gpiop->flags &= ~PINMUX_FLAG_DBIT;
198 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
210 static void setup_data_regs(struct pinmux_info *gpioc)
212 struct pinmux_data_reg *drp;
215 for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
216 setup_data_reg(gpioc, k);
220 drp = gpioc->data_regs + k;
225 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
231 static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
232 struct pinmux_data_reg **drp, int *bitp)
234 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
237 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
240 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
241 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
242 *drp = gpioc->data_regs + k;
247 static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
248 struct pinmux_cfg_reg **crp,
249 int *fieldp, int *valuep,
250 unsigned long **cntp)
252 struct pinmux_cfg_reg *config_reg;
253 unsigned long r_width, f_width, curr_width, ncomb;
254 int k, m, n, pos, bit_pos;
258 config_reg = gpioc->cfg_regs + k;
260 r_width = config_reg->reg_width;
261 f_width = config_reg->field_width;
268 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
270 curr_width = f_width;
272 curr_width = config_reg->var_field_width[m];
274 ncomb = 1 << curr_width;
275 for (n = 0; n < ncomb; n++) {
276 if (config_reg->enum_ids[pos + n] == enum_id) {
280 *cntp = &config_reg->cnt[m];
293 static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
294 int pos, pinmux_enum_t *enum_idp)
296 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
297 pinmux_enum_t *data = gpioc->gpio_data;
300 if (!enum_in_range(enum_id, &gpioc->data)) {
301 if (!enum_in_range(enum_id, &gpioc->mark)) {
302 debug("non data/mark enum_id for gpio %d\n", gpio);
308 *enum_idp = data[pos + 1];
312 for (k = 0; k < gpioc->gpio_data_size; k++) {
313 if (data[k] == enum_id) {
314 *enum_idp = data[k + 1];
319 debug("cannot locate data/mark enum_id for gpio %d\n", gpio);
323 enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
325 static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
326 int pinmux_type, int cfg_mode)
328 struct pinmux_cfg_reg *cr = NULL;
329 pinmux_enum_t enum_id;
330 struct pinmux_range *range;
331 int in_range, pos, field, value;
334 switch (pinmux_type) {
336 case PINMUX_TYPE_FUNCTION:
340 case PINMUX_TYPE_OUTPUT:
341 range = &gpioc->output;
344 case PINMUX_TYPE_INPUT:
345 range = &gpioc->input;
348 case PINMUX_TYPE_INPUT_PULLUP:
349 range = &gpioc->input_pu;
352 case PINMUX_TYPE_INPUT_PULLDOWN:
353 range = &gpioc->input_pd;
365 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
372 /* first check if this is a function enum */
373 in_range = enum_in_range(enum_id, &gpioc->function);
375 /* not a function enum */
378 * other range exists, so this pin is
379 * a regular GPIO pin that now is being
380 * bound to a specific direction.
382 * for this case we only allow function enums
383 * and the enums that match the other range.
385 in_range = enum_in_range(enum_id, range);
388 * special case pass through for fixed
389 * input-only or output-only pins without
390 * function enum register association.
392 if (in_range && enum_id == range->force)
396 * no other range exists, so this pin
397 * must then be of the function type.
399 * allow function type pins to select
400 * any combination of function/in/out
401 * in their MARK lists.
410 if (get_config_reg(gpioc, enum_id, &cr,
411 &field, &value, &cntp) != 0)
415 case GPIO_CFG_DRYRUN:
417 (read_config_reg(gpioc, cr, field) != value))
422 write_config_reg(gpioc, cr, field, value);
438 static DEFINE_SPINLOCK(gpio_lock);
439 static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
441 return container_of(chip, struct pinmux_info, chip);
445 static int sh_gpio_request(unsigned offset)
447 struct pinmux_data_reg *dummy;
448 int i, ret, pinmux_type;
455 if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
458 /* setup pin function here if no data is associated with pin */
460 if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
461 pinmux_type = PINMUX_TYPE_FUNCTION;
463 pinmux_type = PINMUX_TYPE_GPIO;
465 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
466 if (pinmux_config_gpio(gpioc, offset,
468 GPIO_CFG_DRYRUN) != 0)
471 if (pinmux_config_gpio(gpioc, offset,
477 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
478 gpioc->gpios[offset].flags |= pinmux_type;
485 static void sh_gpio_free(unsigned offset)
492 pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
493 pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
494 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
495 gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
498 static int pinmux_direction(struct pinmux_info *gpioc,
499 unsigned gpio, int new_pinmux_type)
507 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
509 switch (pinmux_type) {
510 case PINMUX_TYPE_GPIO:
512 case PINMUX_TYPE_OUTPUT:
513 case PINMUX_TYPE_INPUT:
514 case PINMUX_TYPE_INPUT_PULLUP:
515 case PINMUX_TYPE_INPUT_PULLDOWN:
516 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
522 if (pinmux_config_gpio(gpioc, gpio,
524 GPIO_CFG_DRYRUN) != 0)
527 if (pinmux_config_gpio(gpioc, gpio,
532 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
533 gpioc->gpios[gpio].flags |= new_pinmux_type;
540 static int sh_gpio_direction_input(unsigned offset)
542 return pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
545 static void sh_gpio_set_value(struct pinmux_info *gpioc,
546 unsigned gpio, int value)
548 struct pinmux_data_reg *dr = NULL;
551 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
554 gpio_write_bit(dr, bit, value);
557 static int sh_gpio_direction_output(unsigned offset, int value)
559 sh_gpio_set_value(gpioc, offset, value);
560 return pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
563 static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
565 struct pinmux_data_reg *dr = NULL;
566 int bit = 0, offset = 0;
568 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
570 #if defined(CONFIG_RCAR_GEN3)
571 if ((gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_INPUT)
575 return gpio_read_bit(dr, offset, bit);
578 static int sh_gpio_get(unsigned offset)
580 return sh_gpio_get_value(gpioc, offset);
583 static void sh_gpio_set(unsigned offset, int value)
585 sh_gpio_set_value(gpioc, offset, value);
588 int register_pinmux(struct pinmux_info *pip)
592 debug("%s deregistering\n", pip->name);
593 setup_data_regs(gpioc);
598 int unregister_pinmux(struct pinmux_info *pip)
600 debug("%s deregistering\n", pip->name);
608 int gpio_request(unsigned gpio, const char *label)
610 sh_gpio_request(gpio);
614 int gpio_free(unsigned gpio)
620 int gpio_direction_input(unsigned gpio)
622 return sh_gpio_direction_input(gpio);
625 int gpio_direction_output(unsigned gpio, int value)
627 return sh_gpio_direction_output(gpio, value);
630 void gpio_set_value(unsigned gpio, int value)
632 sh_gpio_set(gpio, value);
635 int gpio_get_value(unsigned gpio)
637 return sh_gpio_get(gpio);