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
13 #include <asm/bitops.h>
17 static struct pinmux_info *gpioc;
19 #define pfc_phys_to_virt(p, a) ((void *)a)
21 static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
23 if (enum_id < r->begin)
32 static unsigned long gpio_read_raw_reg(void *mapped_reg,
33 unsigned long reg_width)
38 return readb(mapped_reg);
40 return readw(mapped_reg);
42 return readl(mapped_reg);
49 static void gpio_write_raw_reg(void *mapped_reg,
50 unsigned long reg_width,
55 writeb(data, mapped_reg);
58 writew(data, mapped_reg);
61 writel(data, mapped_reg);
68 static int gpio_read_bit(struct pinmux_data_reg *dr,
73 pos = dr->reg_width - (in_pos + 1);
75 debug("read_bit: addr = %lx, pos = %ld, "
76 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
78 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
81 static void gpio_write_bit(struct pinmux_data_reg *dr,
82 unsigned long in_pos, unsigned long value)
86 pos = dr->reg_width - (in_pos + 1);
88 debug("write_bit addr = %lx, value = %d, pos = %ld, "
90 dr->reg, !!value, pos, dr->reg_width);
93 __set_bit(pos, &dr->reg_shadow);
95 __clear_bit(pos, &dr->reg_shadow);
97 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
100 static void config_reg_helper(struct pinmux_info *gpioc,
101 struct pinmux_cfg_reg *crp,
102 unsigned long in_pos,
104 void __iomem **mapped_regp,
108 unsigned long *maskp,
113 *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
115 if (crp->field_width) {
116 *maskp = (1 << crp->field_width) - 1;
117 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
119 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
120 *posp = crp->reg_width;
121 for (k = 0; k <= in_pos; k++)
122 *posp -= crp->var_field_width[k];
126 static int read_config_reg(struct pinmux_info *gpioc,
127 struct pinmux_cfg_reg *crp,
132 unsigned long mask, pos;
134 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
136 debug("read_reg: addr = %lx, field = %ld, "
137 "r_width = %ld, f_width = %ld\n",
138 crp->reg, field, crp->reg_width, crp->field_width);
140 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
143 static void write_config_reg(struct pinmux_info *gpioc,
144 struct pinmux_cfg_reg *crp,
145 unsigned long field, unsigned long value)
148 unsigned long mask, pos, data;
150 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
152 debug("write_reg addr = %lx, value = %ld, field = %ld, "
153 "r_width = %ld, f_width = %ld\n",
154 crp->reg, value, field, crp->reg_width, crp->field_width);
156 mask = ~(mask << pos);
157 value = value << pos;
159 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
163 if (gpioc->unlock_reg)
164 gpio_write_raw_reg(pfc_phys_to_virt(gpioc, gpioc->unlock_reg),
167 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
170 static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
172 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
173 struct pinmux_data_reg *data_reg;
176 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
181 data_reg = gpioc->data_regs + k;
183 if (!data_reg->reg_width)
186 data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
188 for (n = 0; n < data_reg->reg_width; n++) {
189 if (data_reg->enum_ids[n] == gpiop->enum_id) {
190 gpiop->flags &= ~PINMUX_FLAG_DREG;
191 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
192 gpiop->flags &= ~PINMUX_FLAG_DBIT;
193 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
205 static void setup_data_regs(struct pinmux_info *gpioc)
207 struct pinmux_data_reg *drp;
210 for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
211 setup_data_reg(gpioc, k);
215 drp = gpioc->data_regs + k;
220 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
226 static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
227 struct pinmux_data_reg **drp, int *bitp)
229 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
232 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
235 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
236 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
237 *drp = gpioc->data_regs + k;
242 static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
243 struct pinmux_cfg_reg **crp,
244 int *fieldp, int *valuep,
245 unsigned long **cntp)
247 struct pinmux_cfg_reg *config_reg;
248 unsigned long r_width, f_width, curr_width, ncomb;
249 int k, m, n, pos, bit_pos;
253 config_reg = gpioc->cfg_regs + k;
255 r_width = config_reg->reg_width;
256 f_width = config_reg->field_width;
263 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
265 curr_width = f_width;
267 curr_width = config_reg->var_field_width[m];
269 ncomb = 1 << curr_width;
270 for (n = 0; n < ncomb; n++) {
271 if (config_reg->enum_ids[pos + n] == enum_id) {
275 *cntp = &config_reg->cnt[m];
288 static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
289 int pos, pinmux_enum_t *enum_idp)
291 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
292 pinmux_enum_t *data = gpioc->gpio_data;
295 if (!enum_in_range(enum_id, &gpioc->data)) {
296 if (!enum_in_range(enum_id, &gpioc->mark)) {
297 debug("non data/mark enum_id for gpio %d\n", gpio);
303 *enum_idp = data[pos + 1];
307 for (k = 0; k < gpioc->gpio_data_size; k++) {
308 if (data[k] == enum_id) {
309 *enum_idp = data[k + 1];
314 debug("cannot locate data/mark enum_id for gpio %d\n", gpio);
318 enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
320 static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
321 int pinmux_type, int cfg_mode)
323 struct pinmux_cfg_reg *cr = NULL;
324 pinmux_enum_t enum_id;
325 struct pinmux_range *range;
326 int in_range, pos, field, value;
329 switch (pinmux_type) {
331 case PINMUX_TYPE_FUNCTION:
335 case PINMUX_TYPE_OUTPUT:
336 range = &gpioc->output;
339 case PINMUX_TYPE_INPUT:
340 range = &gpioc->input;
343 case PINMUX_TYPE_INPUT_PULLUP:
344 range = &gpioc->input_pu;
347 case PINMUX_TYPE_INPUT_PULLDOWN:
348 range = &gpioc->input_pd;
360 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
367 /* first check if this is a function enum */
368 in_range = enum_in_range(enum_id, &gpioc->function);
370 /* not a function enum */
373 * other range exists, so this pin is
374 * a regular GPIO pin that now is being
375 * bound to a specific direction.
377 * for this case we only allow function enums
378 * and the enums that match the other range.
380 in_range = enum_in_range(enum_id, range);
383 * special case pass through for fixed
384 * input-only or output-only pins without
385 * function enum register association.
387 if (in_range && enum_id == range->force)
391 * no other range exists, so this pin
392 * must then be of the function type.
394 * allow function type pins to select
395 * any combination of function/in/out
396 * in their MARK lists.
405 if (get_config_reg(gpioc, enum_id, &cr,
406 &field, &value, &cntp) != 0)
410 case GPIO_CFG_DRYRUN:
412 (read_config_reg(gpioc, cr, field) != value))
417 write_config_reg(gpioc, cr, field, value);
433 static DEFINE_SPINLOCK(gpio_lock);
434 static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
436 return container_of(chip, struct pinmux_info, chip);
440 static int sh_gpio_request(unsigned offset)
442 struct pinmux_data_reg *dummy;
443 int i, ret, pinmux_type;
450 if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
453 /* setup pin function here if no data is associated with pin */
455 if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
456 pinmux_type = PINMUX_TYPE_FUNCTION;
458 pinmux_type = PINMUX_TYPE_GPIO;
460 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
461 if (pinmux_config_gpio(gpioc, offset,
463 GPIO_CFG_DRYRUN) != 0)
466 if (pinmux_config_gpio(gpioc, offset,
472 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
473 gpioc->gpios[offset].flags |= pinmux_type;
480 static void sh_gpio_free(unsigned offset)
487 pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
488 pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
489 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
490 gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
493 static int pinmux_direction(struct pinmux_info *gpioc,
494 unsigned gpio, int new_pinmux_type)
502 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
504 switch (pinmux_type) {
505 case PINMUX_TYPE_GPIO:
507 case PINMUX_TYPE_OUTPUT:
508 case PINMUX_TYPE_INPUT:
509 case PINMUX_TYPE_INPUT_PULLUP:
510 case PINMUX_TYPE_INPUT_PULLDOWN:
511 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
517 if (pinmux_config_gpio(gpioc, gpio,
519 GPIO_CFG_DRYRUN) != 0)
522 if (pinmux_config_gpio(gpioc, gpio,
527 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
528 gpioc->gpios[gpio].flags |= new_pinmux_type;
535 static int sh_gpio_direction_input(unsigned offset)
537 return pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
540 static void sh_gpio_set_value(struct pinmux_info *gpioc,
541 unsigned gpio, int value)
543 struct pinmux_data_reg *dr = NULL;
546 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
549 gpio_write_bit(dr, bit, value);
552 static int sh_gpio_direction_output(unsigned offset, int value)
554 sh_gpio_set_value(gpioc, offset, value);
555 return pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
558 static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
560 struct pinmux_data_reg *dr = NULL;
563 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
566 return gpio_read_bit(dr, bit);
569 static int sh_gpio_get(unsigned offset)
571 return sh_gpio_get_value(gpioc, offset);
574 static void sh_gpio_set(unsigned offset, int value)
576 sh_gpio_set_value(gpioc, offset, value);
579 int register_pinmux(struct pinmux_info *pip)
583 debug("%s deregistering\n", pip->name);
584 setup_data_regs(gpioc);
589 int unregister_pinmux(struct pinmux_info *pip)
591 debug("%s deregistering\n", pip->name);
599 int gpio_request(unsigned gpio, const char *label)
601 sh_gpio_request(gpio);
605 int gpio_free(unsigned gpio)
611 int gpio_direction_input(unsigned gpio)
613 return sh_gpio_direction_input(gpio);
616 int gpio_direction_output(unsigned gpio, int value)
618 return sh_gpio_direction_output(gpio, value);
621 void gpio_set_value(unsigned gpio, int value)
623 sh_gpio_set(gpio, value);
626 int gpio_get_value(unsigned gpio)
628 return sh_gpio_get(gpio);