10 #include <linux/module.h>
14 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
27 #define U300_GPIO_PORT_STRIDE (0x30)
34 #define U300_GPIO_CR (0x00)
35 #define U300_GPIO_CR_SYNC_SEL_ENABLE (0x00000002UL)
36 #define U300_GPIO_CR_BLOCK_CLKRQ_ENABLE (0x00000001UL)
37 #define U300_GPIO_PXPDIR (0x04)
38 #define U300_GPIO_PXPDOR (0x08)
39 #define U300_GPIO_PXPCR (0x0C)
40 #define U300_GPIO_PXPCR_ALL_PINS_MODE_MASK (0x0000FFFFUL)
41 #define U300_GPIO_PXPCR_PIN_MODE_MASK (0x00000003UL)
42 #define U300_GPIO_PXPCR_PIN_MODE_SHIFT (0x00000002UL)
43 #define U300_GPIO_PXPCR_PIN_MODE_INPUT (0x00000000UL)
44 #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL (0x00000001UL)
45 #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN (0x00000002UL)
46 #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE (0x00000003UL)
47 #define U300_GPIO_PXPER (0x10)
48 #define U300_GPIO_PXPER_ALL_PULL_UP_DISABLE_MASK (0x000000FFUL)
49 #define U300_GPIO_PXPER_PULL_UP_DISABLE (0x00000001UL)
50 #define U300_GPIO_PXIEV (0x14)
51 #define U300_GPIO_PXIEN (0x18)
52 #define U300_GPIO_PXIFR (0x1C)
53 #define U300_GPIO_PXICR (0x20)
54 #define U300_GPIO_PXICR_ALL_IRQ_CONFIG_MASK (0x000000FFUL)
55 #define U300_GPIO_PXICR_IRQ_CONFIG_MASK (0x00000001UL)
56 #define U300_GPIO_PXICR_IRQ_CONFIG_FALLING_EDGE (0x00000000UL)
57 #define U300_GPIO_PXICR_IRQ_CONFIG_RISING_EDGE (0x00000001UL)
60 #define U300_GPIO_PINS_PER_PORT 8
61 #define U300_GPIO_MAX (U300_GPIO_PINS_PER_PORT * 7)
98 #define U300_PIN_REG(pin, reg) \
99 (gpio->base + (pin >> 3) * gpio->stride + gpio->reg)
105 #define U300_PIN_BIT(pin) \
115 #define BS335_GPIO_NUM_PORTS 7
117 #define U300_FLOATING_INPUT { \
118 .bias_mode = PIN_CONFIG_BIAS_HIGH_IMPEDANCE, \
122 #define U300_PULL_UP_INPUT { \
123 .bias_mode = PIN_CONFIG_BIAS_PULL_UP, \
127 #define U300_OUTPUT_LOW { \
132 #define U300_OUTPUT_HIGH { \
223 static inline struct u300_gpio *to_u300_gpio(
struct gpio_chip *
chip)
228 static int u300_gpio_request(
struct gpio_chip *
chip,
unsigned offset)
239 static void u300_gpio_free(
struct gpio_chip *
chip,
unsigned offset)
246 static int u300_gpio_get(
struct gpio_chip *
chip,
unsigned offset)
253 static void u300_gpio_set(
struct gpio_chip *chip,
unsigned offset,
int value)
255 struct u300_gpio *gpio = to_u300_gpio(chip);
270 static int u300_gpio_direction_input(
struct gpio_chip *chip,
unsigned offset)
272 struct u300_gpio *gpio = to_u300_gpio(chip);
285 static int u300_gpio_direction_output(
struct gpio_chip *chip,
unsigned offset,
288 struct u300_gpio *gpio = to_u300_gpio(chip);
300 ((offset & 0x07) << 1));
304 ((offset & 0x07) << 1));
306 << ((offset & 0x07) << 1));
309 u300_gpio_set(chip, offset, value);
314 static int u300_gpio_to_irq(
struct gpio_chip *chip,
unsigned offset)
316 struct u300_gpio *gpio = to_u300_gpio(chip);
319 dev_dbg(gpio->
dev,
"request IRQ for GPIO %d, return %d\n", offset,
329 struct u300_gpio *gpio = to_u300_gpio(chip);
330 enum pin_config_param
param = (
enum pin_config_param) *config;
340 drmode >>= ((offset & 0x07) << 1);
343 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
350 case PIN_CONFIG_BIAS_PULL_UP:
357 case PIN_CONFIG_DRIVE_PUSH_PULL:
364 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
371 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
385 enum pin_config_param
param)
387 struct u300_gpio *gpio = to_u300_gpio(chip);
393 case PIN_CONFIG_BIAS_DISABLE:
394 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
398 case PIN_CONFIG_BIAS_PULL_UP:
402 case PIN_CONFIG_DRIVE_PUSH_PULL:
405 << ((offset & 0x07) << 1));
407 << ((offset & 0x07) << 1));
410 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
413 << ((offset & 0x07) << 1));
415 << ((offset & 0x07) << 1));
418 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
421 << ((offset & 0x07) << 1));
423 << ((offset & 0x07) << 1));
428 dev_err(gpio->
dev,
"illegal configuration requested\n");
435 static struct gpio_chip u300_gpio_chip = {
436 .label =
"u300-gpio-chip",
438 .request = u300_gpio_request,
439 .free = u300_gpio_free,
440 .get = u300_gpio_get,
441 .set = u300_gpio_set,
442 .direction_input = u300_gpio_direction_input,
443 .direction_output = u300_gpio_direction_output,
444 .to_irq = u300_gpio_to_irq,
447 static void u300_toggle_trigger(
struct u300_gpio *gpio,
unsigned offset)
453 if (u300_gpio_get(&gpio->
chip, offset)) {
456 dev_dbg(gpio->
dev,
"next IRQ on falling edge on pin %d\n",
461 dev_dbg(gpio->
dev,
"next IRQ on rising edge on pin %d\n",
481 "trigger on both rising and falling edge on pin %d\n",
484 u300_toggle_trigger(gpio, offset);
485 }
else if (trigger & IRQF_TRIGGER_RISING) {
486 dev_dbg(gpio->
dev,
"trigger on rising edge on pin %d\n",
491 }
else if (trigger & IRQF_TRIGGER_FALLING) {
492 dev_dbg(gpio->
dev,
"trigger on falling edge on pin %d\n",
502 static void u300_gpio_irq_enable(
struct irq_data *d)
516 static void u300_gpio_irq_disable(
struct irq_data *d)
530 static struct irq_chip u300_gpio_irqchip = {
531 .name =
"u300-gpio-irqchip",
532 .irq_enable = u300_gpio_irq_enable,
533 .irq_disable = u300_gpio_irq_disable,
534 .irq_set_type = u300_gpio_irq_type,
538 static void u300_gpio_irq_handler(
unsigned irq,
struct irq_desc *
desc)
542 int pinoffset = port->
number << 3;
560 int offset = pinoffset + irqoffset;
570 u300_toggle_trigger(gpio, offset);
583 u300_gpio_direction_output(&gpio->
chip, offset, conf->
outval);
587 PIN_CONFIG_BIAS_HIGH_IMPEDANCE);
591 PIN_CONFIG_DRIVE_PUSH_PULL);
593 dev_dbg(gpio->
dev,
"set up pin %d as output, value: %d\n",
596 u300_gpio_direction_input(&gpio->
chip, offset);
599 u300_gpio_set(&gpio->
chip, offset, 0);
604 dev_dbg(gpio->
dev,
"set up pin %d as input, bias: %04x\n",
615 for (i = 0; i < plat->
ports; i++) {
616 for (j = 0; j < 8; j++) {
618 int offset = (i*8) + j;
620 conf = &bs335_gpio_config[
i][
j];
621 u300_gpio_init_pin(gpio, offset, conf);
626 static inline void u300_gpio_free_ports(
struct u300_gpio *gpio)
650 dev_err(&pdev->
dev,
"failed to allocate memory\n");
654 gpio->
chip = u300_gpio_chip;
663 if (IS_ERR(gpio->
clk)) {
664 err = PTR_ERR(gpio->
clk);
665 dev_err(gpio->
dev,
"could not get GPIO clock\n");
668 err = clk_prepare_enable(gpio->
clk);
670 dev_err(gpio->
dev,
"could not enable GPIO clock\n");
671 goto err_no_clk_enable;
676 dev_err(gpio->
dev,
"could not get GPIO memory resource\n");
678 goto err_no_resource;
682 resource_size(gpio->
memres),
683 "GPIO Controller")) {
685 goto err_no_ioregion;
695 "initializing GPIO Controller COH 901 571/3\n");
707 dev_info(gpio->
dev,
"COH901571/3 block version: %d, " \
708 "number of cores: %d totalling %d pins\n",
709 ((val & 0x000001FC) >> 2),
710 ((val & 0x0000FE00) >> 9),
711 ((val & 0x0000FE00) >> 9) * 8);
714 u300_gpio_init_coh901571(gpio, plat);
718 for (portno = 0 ; portno < plat->
ports; portno++) {
738 irq_set_chained_handler(port->
irq, u300_gpio_irq_handler);
743 int irqno = gpio->
irq_base + (portno << 3) + i;
745 dev_dbg(gpio->
dev,
"handler for IRQ %d on %s\n",
747 irq_set_chip_and_handler(irqno, &u300_gpio_irqchip,
758 dev_dbg(gpio->
dev,
"initialized %d GPIO ports\n", portno);
762 dev_err(gpio->
dev,
"unable to add gpiochip: %d\n", err);
772 platform_set_drvdata(pdev, gpio);
780 u300_gpio_free_ports(gpio);
786 clk_disable_unprepare(gpio->
clk);
797 struct u300_gpio *gpio = platform_get_drvdata(pdev);
805 dev_err(gpio->
dev,
"unable to remove gpiochip: %d\n", err);
808 u300_gpio_free_ports(gpio);
811 resource_size(gpio->
memres));
812 clk_disable_unprepare(gpio->
clk);
814 platform_set_drvdata(pdev,
NULL);
823 .remove =
__exit_p(u300_gpio_remove),
826 static int __init u300_gpio_init(
void)
831 static void __exit u300_gpio_exit(
void)