2 * Pinmuxed GPIO support for SuperH.
4 * Copyright (C) 2008 Magnus Damm
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
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
18 #include <linux/irq.h>
19 #include <linux/bitops.h>
20 #include <linux/gpio.h>
22 static int enum_in_range(pinmux_enum_t enum_id
, struct pinmux_range
*r
)
24 if (enum_id
< r
->begin
)
33 static unsigned long gpio_read_raw_reg(unsigned long reg
,
34 unsigned long reg_width
)
49 static void gpio_write_raw_reg(unsigned long reg
,
50 unsigned long reg_width
,
68 static void gpio_write_bit(struct pinmux_data_reg
*dr
,
69 unsigned long in_pos
, unsigned long value
)
73 pos
= dr
->reg_width
- (in_pos
+ 1);
76 pr_info("write_bit addr = %lx, value = %ld, pos = %ld, "
78 dr
->reg
, !!value
, pos
, dr
->reg_width
);
82 set_bit(pos
, &dr
->reg_shadow
);
84 clear_bit(pos
, &dr
->reg_shadow
);
86 gpio_write_raw_reg(dr
->reg
, dr
->reg_width
, dr
->reg_shadow
);
89 static int gpio_read_reg(unsigned long reg
, unsigned long reg_width
,
90 unsigned long field_width
, unsigned long in_pos
)
92 unsigned long data
, mask
, pos
;
95 mask
= (1 << field_width
) - 1;
96 pos
= reg_width
- ((in_pos
+ 1) * field_width
);
99 pr_info("read_reg: addr = %lx, pos = %ld, "
100 "r_width = %ld, f_width = %ld\n",
101 reg
, pos
, reg_width
, field_width
);
104 data
= gpio_read_raw_reg(reg
, reg_width
);
105 return (data
>> pos
) & mask
;
108 static void gpio_write_reg(unsigned long reg
, unsigned long reg_width
,
109 unsigned long field_width
, unsigned long in_pos
,
112 unsigned long mask
, pos
;
114 mask
= (1 << field_width
) - 1;
115 pos
= reg_width
- ((in_pos
+ 1) * field_width
);
118 pr_info("write_reg addr = %lx, value = %ld, pos = %ld, "
119 "r_width = %ld, f_width = %ld\n",
120 reg
, value
, pos
, reg_width
, field_width
);
123 mask
= ~(mask
<< pos
);
124 value
= value
<< pos
;
128 ctrl_outb((ctrl_inb(reg
) & mask
) | value
, reg
);
131 ctrl_outw((ctrl_inw(reg
) & mask
) | value
, reg
);
134 ctrl_outl((ctrl_inl(reg
) & mask
) | value
, reg
);
139 static int setup_data_reg(struct pinmux_info
*gpioc
, unsigned gpio
)
141 struct pinmux_gpio
*gpiop
= &gpioc
->gpios
[gpio
];
142 struct pinmux_data_reg
*data_reg
;
145 if (!enum_in_range(gpiop
->enum_id
, &gpioc
->data
))
150 data_reg
= gpioc
->data_regs
+ k
;
152 if (!data_reg
->reg_width
)
155 for (n
= 0; n
< data_reg
->reg_width
; n
++) {
156 if (data_reg
->enum_ids
[n
] == gpiop
->enum_id
) {
157 gpiop
->flags
&= ~PINMUX_FLAG_DREG
;
158 gpiop
->flags
|= (k
<< PINMUX_FLAG_DREG_SHIFT
);
159 gpiop
->flags
&= ~PINMUX_FLAG_DBIT
;
160 gpiop
->flags
|= (n
<< PINMUX_FLAG_DBIT_SHIFT
);
172 static void setup_data_regs(struct pinmux_info
*gpioc
)
174 struct pinmux_data_reg
*drp
;
177 for (k
= gpioc
->first_gpio
; k
<= gpioc
->last_gpio
; k
++)
178 setup_data_reg(gpioc
, k
);
182 drp
= gpioc
->data_regs
+ k
;
187 drp
->reg_shadow
= gpio_read_raw_reg(drp
->reg
, drp
->reg_width
);
192 static int get_data_reg(struct pinmux_info
*gpioc
, unsigned gpio
,
193 struct pinmux_data_reg
**drp
, int *bitp
)
195 struct pinmux_gpio
*gpiop
= &gpioc
->gpios
[gpio
];
198 if (!enum_in_range(gpiop
->enum_id
, &gpioc
->data
))
201 k
= (gpiop
->flags
& PINMUX_FLAG_DREG
) >> PINMUX_FLAG_DREG_SHIFT
;
202 n
= (gpiop
->flags
& PINMUX_FLAG_DBIT
) >> PINMUX_FLAG_DBIT_SHIFT
;
203 *drp
= gpioc
->data_regs
+ k
;
208 static int get_config_reg(struct pinmux_info
*gpioc
, pinmux_enum_t enum_id
,
209 struct pinmux_cfg_reg
**crp
, int *indexp
,
210 unsigned long **cntp
)
212 struct pinmux_cfg_reg
*config_reg
;
213 unsigned long r_width
, f_width
;
218 config_reg
= gpioc
->cfg_regs
+ k
;
220 r_width
= config_reg
->reg_width
;
221 f_width
= config_reg
->field_width
;
225 for (n
= 0; n
< (r_width
/ f_width
) * 1 << f_width
; n
++) {
226 if (config_reg
->enum_ids
[n
] == enum_id
) {
229 *cntp
= &config_reg
->cnt
[n
/ (1 << f_width
)];
239 static int get_gpio_enum_id(struct pinmux_info
*gpioc
, unsigned gpio
,
240 int pos
, pinmux_enum_t
*enum_idp
)
242 pinmux_enum_t enum_id
= gpioc
->gpios
[gpio
].enum_id
;
243 pinmux_enum_t
*data
= gpioc
->gpio_data
;
246 if (!enum_in_range(enum_id
, &gpioc
->data
)) {
247 if (!enum_in_range(enum_id
, &gpioc
->mark
)) {
248 pr_err("non data/mark enum_id for gpio %d\n", gpio
);
254 *enum_idp
= data
[pos
+ 1];
258 for (k
= 0; k
< gpioc
->gpio_data_size
; k
++) {
259 if (data
[k
] == enum_id
) {
260 *enum_idp
= data
[k
+ 1];
265 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio
);
269 static void write_config_reg(struct pinmux_info
*gpioc
,
270 struct pinmux_cfg_reg
*crp
,
273 unsigned long ncomb
, pos
, value
;
275 ncomb
= 1 << crp
->field_width
;
277 value
= index
% ncomb
;
279 gpio_write_reg(crp
->reg
, crp
->reg_width
, crp
->field_width
, pos
, value
);
282 static int check_config_reg(struct pinmux_info
*gpioc
,
283 struct pinmux_cfg_reg
*crp
,
286 unsigned long ncomb
, pos
, value
;
288 ncomb
= 1 << crp
->field_width
;
290 value
= index
% ncomb
;
292 if (gpio_read_reg(crp
->reg
, crp
->reg_width
,
293 crp
->field_width
, pos
) == value
)
299 enum { GPIO_CFG_DRYRUN
, GPIO_CFG_REQ
, GPIO_CFG_FREE
};
301 static int pinmux_config_gpio(struct pinmux_info
*gpioc
, unsigned gpio
,
302 int pinmux_type
, int cfg_mode
)
304 struct pinmux_cfg_reg
*cr
= NULL
;
305 pinmux_enum_t enum_id
;
306 struct pinmux_range
*range
;
307 int in_range
, pos
, index
;
310 switch (pinmux_type
) {
312 case PINMUX_TYPE_FUNCTION
:
316 case PINMUX_TYPE_OUTPUT
:
317 range
= &gpioc
->output
;
320 case PINMUX_TYPE_INPUT
:
321 range
= &gpioc
->input
;
324 case PINMUX_TYPE_INPUT_PULLUP
:
325 range
= &gpioc
->input_pu
;
328 case PINMUX_TYPE_INPUT_PULLDOWN
:
329 range
= &gpioc
->input_pd
;
340 pos
= get_gpio_enum_id(gpioc
, gpio
, pos
, &enum_id
);
347 in_range
= enum_in_range(enum_id
, &gpioc
->function
);
348 if (!in_range
&& range
) {
349 in_range
= enum_in_range(enum_id
, range
);
351 if (in_range
&& enum_id
== range
->force
)
358 if (get_config_reg(gpioc
, enum_id
, &cr
, &index
, &cntp
) != 0)
362 case GPIO_CFG_DRYRUN
:
363 if (!*cntp
|| !check_config_reg(gpioc
, cr
, index
))
368 write_config_reg(gpioc
, cr
, index
);
383 static DEFINE_SPINLOCK(gpio_lock
);
385 static struct pinmux_info
*chip_to_pinmux(struct gpio_chip
*chip
)
387 return container_of(chip
, struct pinmux_info
, chip
);
390 static int sh_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
392 struct pinmux_info
*gpioc
= chip_to_pinmux(chip
);
393 struct pinmux_data_reg
*dummy
;
395 int i
, ret
, pinmux_type
;
402 spin_lock_irqsave(&gpio_lock
, flags
);
404 if ((gpioc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
) != PINMUX_TYPE_NONE
)
407 /* setup pin function here if no data is associated with pin */
409 if (get_data_reg(gpioc
, offset
, &dummy
, &i
) != 0)
410 pinmux_type
= PINMUX_TYPE_FUNCTION
;
412 pinmux_type
= PINMUX_TYPE_GPIO
;
414 if (pinmux_type
== PINMUX_TYPE_FUNCTION
) {
415 if (pinmux_config_gpio(gpioc
, offset
,
417 GPIO_CFG_DRYRUN
) != 0)
420 if (pinmux_config_gpio(gpioc
, offset
,
426 gpioc
->gpios
[offset
].flags
&= ~PINMUX_FLAG_TYPE
;
427 gpioc
->gpios
[offset
].flags
|= pinmux_type
;
431 spin_unlock_irqrestore(&gpio_lock
, flags
);
436 static void sh_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
438 struct pinmux_info
*gpioc
= chip_to_pinmux(chip
);
445 spin_lock_irqsave(&gpio_lock
, flags
);
447 pinmux_type
= gpioc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
448 pinmux_config_gpio(gpioc
, offset
, pinmux_type
, GPIO_CFG_FREE
);
449 gpioc
->gpios
[offset
].flags
&= ~PINMUX_FLAG_TYPE
;
450 gpioc
->gpios
[offset
].flags
|= PINMUX_TYPE_NONE
;
452 spin_unlock_irqrestore(&gpio_lock
, flags
);
455 static int pinmux_direction(struct pinmux_info
*gpioc
,
456 unsigned gpio
, int new_pinmux_type
)
464 pinmux_type
= gpioc
->gpios
[gpio
].flags
& PINMUX_FLAG_TYPE
;
466 switch (pinmux_type
) {
467 case PINMUX_TYPE_GPIO
:
469 case PINMUX_TYPE_OUTPUT
:
470 case PINMUX_TYPE_INPUT
:
471 case PINMUX_TYPE_INPUT_PULLUP
:
472 case PINMUX_TYPE_INPUT_PULLDOWN
:
473 pinmux_config_gpio(gpioc
, gpio
, pinmux_type
, GPIO_CFG_FREE
);
479 if (pinmux_config_gpio(gpioc
, gpio
,
481 GPIO_CFG_DRYRUN
) != 0)
484 if (pinmux_config_gpio(gpioc
, gpio
,
489 gpioc
->gpios
[gpio
].flags
&= ~PINMUX_FLAG_TYPE
;
490 gpioc
->gpios
[gpio
].flags
|= new_pinmux_type
;
497 static int sh_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
499 struct pinmux_info
*gpioc
= chip_to_pinmux(chip
);
503 spin_lock_irqsave(&gpio_lock
, flags
);
504 ret
= pinmux_direction(gpioc
, offset
, PINMUX_TYPE_INPUT
);
505 spin_unlock_irqrestore(&gpio_lock
, flags
);
510 static void sh_gpio_set_value(struct pinmux_info
*gpioc
,
511 unsigned gpio
, int value
)
513 struct pinmux_data_reg
*dr
= NULL
;
516 if (!gpioc
|| get_data_reg(gpioc
, gpio
, &dr
, &bit
) != 0)
519 gpio_write_bit(dr
, bit
, value
);
522 static int sh_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
525 struct pinmux_info
*gpioc
= chip_to_pinmux(chip
);
529 sh_gpio_set_value(gpioc
, offset
, value
);
530 spin_lock_irqsave(&gpio_lock
, flags
);
531 ret
= pinmux_direction(gpioc
, offset
, PINMUX_TYPE_OUTPUT
);
532 spin_unlock_irqrestore(&gpio_lock
, flags
);
537 static int sh_gpio_get_value(struct pinmux_info
*gpioc
, unsigned gpio
)
539 struct pinmux_data_reg
*dr
= NULL
;
542 if (!gpioc
|| get_data_reg(gpioc
, gpio
, &dr
, &bit
) != 0) {
547 return gpio_read_reg(dr
->reg
, dr
->reg_width
, 1, bit
);
550 static int sh_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
552 return sh_gpio_get_value(chip_to_pinmux(chip
), offset
);
555 static void sh_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
557 sh_gpio_set_value(chip_to_pinmux(chip
), offset
, value
);
560 int register_pinmux(struct pinmux_info
*pip
)
562 struct gpio_chip
*chip
= &pip
->chip
;
564 pr_info("sh pinmux: %s handling gpio %d -> %d\n",
565 pip
->name
, pip
->first_gpio
, pip
->last_gpio
);
567 setup_data_regs(pip
);
569 chip
->request
= sh_gpio_request
;
570 chip
->free
= sh_gpio_free
;
571 chip
->direction_input
= sh_gpio_direction_input
;
572 chip
->get
= sh_gpio_get
;
573 chip
->direction_output
= sh_gpio_direction_output
;
574 chip
->set
= sh_gpio_set
;
576 WARN_ON(pip
->first_gpio
!= 0); /* needs testing */
578 chip
->label
= pip
->name
;
579 chip
->owner
= THIS_MODULE
;
580 chip
->base
= pip
->first_gpio
;
581 chip
->ngpio
= (pip
->last_gpio
- pip
->first_gpio
) + 1;
583 return gpiochip_add(chip
);