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
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/clk.h>
15 #include <linux/err.h>
17 #include <linux/irq.h>
18 #include <linux/bitops.h>
19 #include <linux/gpio.h>
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(unsigned long reg
,
33 unsigned long reg_width
)
37 return __raw_readb(reg
);
39 return __raw_readw(reg
);
41 return __raw_readl(reg
);
48 static void gpio_write_raw_reg(unsigned long reg
,
49 unsigned long reg_width
,
54 __raw_writeb(data
, reg
);
57 __raw_writew(data
, reg
);
60 __raw_writel(data
, reg
);
67 static void gpio_write_bit(struct pinmux_data_reg
*dr
,
68 unsigned long in_pos
, unsigned long value
)
72 pos
= dr
->reg_width
- (in_pos
+ 1);
74 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
76 dr
->reg
, !!value
, pos
, dr
->reg_width
);
79 set_bit(pos
, &dr
->reg_shadow
);
81 clear_bit(pos
, &dr
->reg_shadow
);
83 gpio_write_raw_reg(dr
->reg
, dr
->reg_width
, dr
->reg_shadow
);
86 static int gpio_read_reg(unsigned long reg
, unsigned long reg_width
,
87 unsigned long field_width
, unsigned long in_pos
)
89 unsigned long data
, mask
, pos
;
92 mask
= (1 << field_width
) - 1;
93 pos
= reg_width
- ((in_pos
+ 1) * field_width
);
95 pr_debug("read_reg: addr = %lx, pos = %ld, "
96 "r_width = %ld, f_width = %ld\n",
97 reg
, pos
, reg_width
, field_width
);
99 data
= gpio_read_raw_reg(reg
, reg_width
);
100 return (data
>> pos
) & mask
;
103 static void gpio_write_reg(unsigned long reg
, unsigned long reg_width
,
104 unsigned long field_width
, unsigned long in_pos
,
107 unsigned long mask
, pos
;
109 mask
= (1 << field_width
) - 1;
110 pos
= reg_width
- ((in_pos
+ 1) * field_width
);
112 pr_debug("write_reg addr = %lx, value = %ld, pos = %ld, "
113 "r_width = %ld, f_width = %ld\n",
114 reg
, value
, pos
, reg_width
, field_width
);
116 mask
= ~(mask
<< pos
);
117 value
= value
<< pos
;
121 __raw_writeb((__raw_readb(reg
) & mask
) | value
, reg
);
124 __raw_writew((__raw_readw(reg
) & mask
) | value
, reg
);
127 __raw_writel((__raw_readl(reg
) & mask
) | value
, reg
);
132 static int setup_data_reg(struct pinmux_info
*gpioc
, unsigned gpio
)
134 struct pinmux_gpio
*gpiop
= &gpioc
->gpios
[gpio
];
135 struct pinmux_data_reg
*data_reg
;
138 if (!enum_in_range(gpiop
->enum_id
, &gpioc
->data
))
143 data_reg
= gpioc
->data_regs
+ k
;
145 if (!data_reg
->reg_width
)
148 for (n
= 0; n
< data_reg
->reg_width
; n
++) {
149 if (data_reg
->enum_ids
[n
] == gpiop
->enum_id
) {
150 gpiop
->flags
&= ~PINMUX_FLAG_DREG
;
151 gpiop
->flags
|= (k
<< PINMUX_FLAG_DREG_SHIFT
);
152 gpiop
->flags
&= ~PINMUX_FLAG_DBIT
;
153 gpiop
->flags
|= (n
<< PINMUX_FLAG_DBIT_SHIFT
);
165 static void setup_data_regs(struct pinmux_info
*gpioc
)
167 struct pinmux_data_reg
*drp
;
170 for (k
= gpioc
->first_gpio
; k
<= gpioc
->last_gpio
; k
++)
171 setup_data_reg(gpioc
, k
);
175 drp
= gpioc
->data_regs
+ k
;
180 drp
->reg_shadow
= gpio_read_raw_reg(drp
->reg
, drp
->reg_width
);
185 static int get_data_reg(struct pinmux_info
*gpioc
, unsigned gpio
,
186 struct pinmux_data_reg
**drp
, int *bitp
)
188 struct pinmux_gpio
*gpiop
= &gpioc
->gpios
[gpio
];
191 if (!enum_in_range(gpiop
->enum_id
, &gpioc
->data
))
194 k
= (gpiop
->flags
& PINMUX_FLAG_DREG
) >> PINMUX_FLAG_DREG_SHIFT
;
195 n
= (gpiop
->flags
& PINMUX_FLAG_DBIT
) >> PINMUX_FLAG_DBIT_SHIFT
;
196 *drp
= gpioc
->data_regs
+ k
;
201 static int get_config_reg(struct pinmux_info
*gpioc
, pinmux_enum_t enum_id
,
202 struct pinmux_cfg_reg
**crp
, int *indexp
,
203 unsigned long **cntp
)
205 struct pinmux_cfg_reg
*config_reg
;
206 unsigned long r_width
, f_width
;
211 config_reg
= gpioc
->cfg_regs
+ k
;
213 r_width
= config_reg
->reg_width
;
214 f_width
= config_reg
->field_width
;
218 for (n
= 0; n
< (r_width
/ f_width
) * 1 << f_width
; n
++) {
219 if (config_reg
->enum_ids
[n
] == enum_id
) {
222 *cntp
= &config_reg
->cnt
[n
/ (1 << f_width
)];
232 static int get_gpio_enum_id(struct pinmux_info
*gpioc
, unsigned gpio
,
233 int pos
, pinmux_enum_t
*enum_idp
)
235 pinmux_enum_t enum_id
= gpioc
->gpios
[gpio
].enum_id
;
236 pinmux_enum_t
*data
= gpioc
->gpio_data
;
239 if (!enum_in_range(enum_id
, &gpioc
->data
)) {
240 if (!enum_in_range(enum_id
, &gpioc
->mark
)) {
241 pr_err("non data/mark enum_id for gpio %d\n", gpio
);
247 *enum_idp
= data
[pos
+ 1];
251 for (k
= 0; k
< gpioc
->gpio_data_size
; k
++) {
252 if (data
[k
] == enum_id
) {
253 *enum_idp
= data
[k
+ 1];
258 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio
);
262 static void write_config_reg(struct pinmux_info
*gpioc
,
263 struct pinmux_cfg_reg
*crp
,
266 unsigned long ncomb
, pos
, value
;
268 ncomb
= 1 << crp
->field_width
;
270 value
= index
% ncomb
;
272 gpio_write_reg(crp
->reg
, crp
->reg_width
, crp
->field_width
, pos
, value
);
275 static int check_config_reg(struct pinmux_info
*gpioc
,
276 struct pinmux_cfg_reg
*crp
,
279 unsigned long ncomb
, pos
, value
;
281 ncomb
= 1 << crp
->field_width
;
283 value
= index
% ncomb
;
285 if (gpio_read_reg(crp
->reg
, crp
->reg_width
,
286 crp
->field_width
, pos
) == value
)
292 enum { GPIO_CFG_DRYRUN
, GPIO_CFG_REQ
, GPIO_CFG_FREE
};
294 static int pinmux_config_gpio(struct pinmux_info
*gpioc
, unsigned gpio
,
295 int pinmux_type
, int cfg_mode
)
297 struct pinmux_cfg_reg
*cr
= NULL
;
298 pinmux_enum_t enum_id
;
299 struct pinmux_range
*range
;
300 int in_range
, pos
, index
;
303 switch (pinmux_type
) {
305 case PINMUX_TYPE_FUNCTION
:
309 case PINMUX_TYPE_OUTPUT
:
310 range
= &gpioc
->output
;
313 case PINMUX_TYPE_INPUT
:
314 range
= &gpioc
->input
;
317 case PINMUX_TYPE_INPUT_PULLUP
:
318 range
= &gpioc
->input_pu
;
321 case PINMUX_TYPE_INPUT_PULLDOWN
:
322 range
= &gpioc
->input_pd
;
333 pos
= get_gpio_enum_id(gpioc
, gpio
, pos
, &enum_id
);
340 in_range
= enum_in_range(enum_id
, &gpioc
->function
);
341 if (!in_range
&& range
) {
342 in_range
= enum_in_range(enum_id
, range
);
344 if (in_range
&& enum_id
== range
->force
)
351 if (get_config_reg(gpioc
, enum_id
, &cr
, &index
, &cntp
) != 0)
355 case GPIO_CFG_DRYRUN
:
356 if (!*cntp
|| !check_config_reg(gpioc
, cr
, index
))
361 write_config_reg(gpioc
, cr
, index
);
376 static DEFINE_SPINLOCK(gpio_lock
);
378 static struct pinmux_info
*chip_to_pinmux(struct gpio_chip
*chip
)
380 return container_of(chip
, struct pinmux_info
, chip
);
383 static int sh_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
385 struct pinmux_info
*gpioc
= chip_to_pinmux(chip
);
386 struct pinmux_data_reg
*dummy
;
388 int i
, ret
, pinmux_type
;
395 spin_lock_irqsave(&gpio_lock
, flags
);
397 if ((gpioc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
) != PINMUX_TYPE_NONE
)
400 /* setup pin function here if no data is associated with pin */
402 if (get_data_reg(gpioc
, offset
, &dummy
, &i
) != 0)
403 pinmux_type
= PINMUX_TYPE_FUNCTION
;
405 pinmux_type
= PINMUX_TYPE_GPIO
;
407 if (pinmux_type
== PINMUX_TYPE_FUNCTION
) {
408 if (pinmux_config_gpio(gpioc
, offset
,
410 GPIO_CFG_DRYRUN
) != 0)
413 if (pinmux_config_gpio(gpioc
, offset
,
419 gpioc
->gpios
[offset
].flags
&= ~PINMUX_FLAG_TYPE
;
420 gpioc
->gpios
[offset
].flags
|= pinmux_type
;
424 spin_unlock_irqrestore(&gpio_lock
, flags
);
429 static void sh_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
431 struct pinmux_info
*gpioc
= chip_to_pinmux(chip
);
438 spin_lock_irqsave(&gpio_lock
, flags
);
440 pinmux_type
= gpioc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
441 pinmux_config_gpio(gpioc
, offset
, pinmux_type
, GPIO_CFG_FREE
);
442 gpioc
->gpios
[offset
].flags
&= ~PINMUX_FLAG_TYPE
;
443 gpioc
->gpios
[offset
].flags
|= PINMUX_TYPE_NONE
;
445 spin_unlock_irqrestore(&gpio_lock
, flags
);
448 static int pinmux_direction(struct pinmux_info
*gpioc
,
449 unsigned gpio
, int new_pinmux_type
)
457 pinmux_type
= gpioc
->gpios
[gpio
].flags
& PINMUX_FLAG_TYPE
;
459 switch (pinmux_type
) {
460 case PINMUX_TYPE_GPIO
:
462 case PINMUX_TYPE_OUTPUT
:
463 case PINMUX_TYPE_INPUT
:
464 case PINMUX_TYPE_INPUT_PULLUP
:
465 case PINMUX_TYPE_INPUT_PULLDOWN
:
466 pinmux_config_gpio(gpioc
, gpio
, pinmux_type
, GPIO_CFG_FREE
);
472 if (pinmux_config_gpio(gpioc
, gpio
,
474 GPIO_CFG_DRYRUN
) != 0)
477 if (pinmux_config_gpio(gpioc
, gpio
,
482 gpioc
->gpios
[gpio
].flags
&= ~PINMUX_FLAG_TYPE
;
483 gpioc
->gpios
[gpio
].flags
|= new_pinmux_type
;
490 static int sh_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
492 struct pinmux_info
*gpioc
= chip_to_pinmux(chip
);
496 spin_lock_irqsave(&gpio_lock
, flags
);
497 ret
= pinmux_direction(gpioc
, offset
, PINMUX_TYPE_INPUT
);
498 spin_unlock_irqrestore(&gpio_lock
, flags
);
503 static void sh_gpio_set_value(struct pinmux_info
*gpioc
,
504 unsigned gpio
, int value
)
506 struct pinmux_data_reg
*dr
= NULL
;
509 if (!gpioc
|| get_data_reg(gpioc
, gpio
, &dr
, &bit
) != 0)
512 gpio_write_bit(dr
, bit
, value
);
515 static int sh_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
518 struct pinmux_info
*gpioc
= chip_to_pinmux(chip
);
522 sh_gpio_set_value(gpioc
, offset
, value
);
523 spin_lock_irqsave(&gpio_lock
, flags
);
524 ret
= pinmux_direction(gpioc
, offset
, PINMUX_TYPE_OUTPUT
);
525 spin_unlock_irqrestore(&gpio_lock
, flags
);
530 static int sh_gpio_get_value(struct pinmux_info
*gpioc
, unsigned gpio
)
532 struct pinmux_data_reg
*dr
= NULL
;
535 if (!gpioc
|| get_data_reg(gpioc
, gpio
, &dr
, &bit
) != 0) {
540 return gpio_read_reg(dr
->reg
, dr
->reg_width
, 1, bit
);
543 static int sh_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
545 return sh_gpio_get_value(chip_to_pinmux(chip
), offset
);
548 static void sh_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
550 sh_gpio_set_value(chip_to_pinmux(chip
), offset
, value
);
553 int register_pinmux(struct pinmux_info
*pip
)
555 struct gpio_chip
*chip
= &pip
->chip
;
557 pr_info("sh pinmux: %s handling gpio %d -> %d\n",
558 pip
->name
, pip
->first_gpio
, pip
->last_gpio
);
560 setup_data_regs(pip
);
562 chip
->request
= sh_gpio_request
;
563 chip
->free
= sh_gpio_free
;
564 chip
->direction_input
= sh_gpio_direction_input
;
565 chip
->get
= sh_gpio_get
;
566 chip
->direction_output
= sh_gpio_direction_output
;
567 chip
->set
= sh_gpio_set
;
569 WARN_ON(pip
->first_gpio
!= 0); /* needs testing */
571 chip
->label
= pip
->name
;
572 chip
->owner
= THIS_MODULE
;
573 chip
->base
= pip
->first_gpio
;
574 chip
->ngpio
= (pip
->last_gpio
- pip
->first_gpio
) + 1;
576 return gpiochip_add(chip
);