gpio_extended.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * @file /arch/mips/pmc-sierra/msp71xx/gpio_extended.c
  3. *
  4. * Generic PMC MSP71xx EXTENDED (EXD) GPIO handling. The extended gpio is
  5. * a set of hardware registers that have no need for explicit locking as
  6. * it is handled by unique method of writing individual set/clr bits.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * @author Patrick Glass <patrickglass@gmail.com>
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/gpio.h>
  18. #include <linux/io.h>
  19. #define MSP71XX_DATA_OFFSET(gpio) (2 * (gpio))
  20. #define MSP71XX_READ_OFFSET(gpio) (MSP71XX_DATA_OFFSET(gpio) + 1)
  21. #define MSP71XX_CFG_OUT_OFFSET(gpio) (MSP71XX_DATA_OFFSET(gpio) + 16)
  22. #define MSP71XX_CFG_IN_OFFSET(gpio) (MSP71XX_CFG_OUT_OFFSET(gpio) + 1)
  23. #define MSP71XX_EXD_GPIO_BASE 0x0BC000000L
  24. #define to_msp71xx_exd_gpio_chip(c) \
  25. container_of(c, struct msp71xx_exd_gpio_chip, chip)
  26. /*
  27. * struct msp71xx_exd_gpio_chip - container for gpio chip and registers
  28. * @chip: chip structure for the specified gpio bank
  29. * @reg: register for control and data of gpio pin
  30. */
  31. struct msp71xx_exd_gpio_chip {
  32. struct gpio_chip chip;
  33. void __iomem *reg;
  34. };
  35. /*
  36. * msp71xx_exd_gpio_get() - return the chip's gpio value
  37. * @chip: chip structure which controls the specified gpio
  38. * @offset: gpio whose value will be returned
  39. *
  40. * It will return 0 if gpio value is low and other if high.
  41. */
  42. static int msp71xx_exd_gpio_get(struct gpio_chip *chip, unsigned offset)
  43. {
  44. struct msp71xx_exd_gpio_chip *msp71xx_chip =
  45. to_msp71xx_exd_gpio_chip(chip);
  46. const unsigned bit = MSP71XX_READ_OFFSET(offset);
  47. return __raw_readl(msp71xx_chip->reg) & (1 << bit);
  48. }
  49. /*
  50. * msp71xx_exd_gpio_set() - set the output value for the gpio
  51. * @chip: chip structure who controls the specified gpio
  52. * @offset: gpio whose value will be assigned
  53. * @value: logic level to assign to the gpio initially
  54. *
  55. * This will set the gpio bit specified to the desired value. It will set the
  56. * gpio pin low if value is 0 otherwise it will be high.
  57. */
  58. static void msp71xx_exd_gpio_set(struct gpio_chip *chip,
  59. unsigned offset, int value)
  60. {
  61. struct msp71xx_exd_gpio_chip *msp71xx_chip =
  62. to_msp71xx_exd_gpio_chip(chip);
  63. const unsigned bit = MSP71XX_DATA_OFFSET(offset);
  64. __raw_writel(1 << (bit + (value ? 1 : 0)), msp71xx_chip->reg);
  65. }
  66. /*
  67. * msp71xx_exd_direction_output() - declare the direction mode for a gpio
  68. * @chip: chip structure which controls the specified gpio
  69. * @offset: gpio whose value will be assigned
  70. * @value: logic level to assign to the gpio initially
  71. *
  72. * This call will set the mode for the @gpio to output. It will set the
  73. * gpio pin low if value is 0 otherwise it will be high.
  74. */
  75. static int msp71xx_exd_direction_output(struct gpio_chip *chip,
  76. unsigned offset, int value)
  77. {
  78. struct msp71xx_exd_gpio_chip *msp71xx_chip =
  79. to_msp71xx_exd_gpio_chip(chip);
  80. msp71xx_exd_gpio_set(chip, offset, value);
  81. __raw_writel(1 << MSP71XX_CFG_OUT_OFFSET(offset), msp71xx_chip->reg);
  82. return 0;
  83. }
  84. /*
  85. * msp71xx_exd_direction_input() - declare the direction mode for a gpio
  86. * @chip: chip structure which controls the specified gpio
  87. * @offset: gpio whose to which the value will be assigned
  88. *
  89. * This call will set the mode for the @gpio to input.
  90. */
  91. static int msp71xx_exd_direction_input(struct gpio_chip *chip, unsigned offset)
  92. {
  93. struct msp71xx_exd_gpio_chip *msp71xx_chip =
  94. to_msp71xx_exd_gpio_chip(chip);
  95. __raw_writel(1 << MSP71XX_CFG_IN_OFFSET(offset), msp71xx_chip->reg);
  96. return 0;
  97. }
  98. #define MSP71XX_EXD_GPIO_BANK(name, exd_reg, base_gpio, num_gpio) \
  99. { \
  100. .chip = { \
  101. .label = name, \
  102. .direction_input = msp71xx_exd_direction_input, \
  103. .direction_output = msp71xx_exd_direction_output, \
  104. .get = msp71xx_exd_gpio_get, \
  105. .set = msp71xx_exd_gpio_set, \
  106. .base = base_gpio, \
  107. .ngpio = num_gpio, \
  108. }, \
  109. .reg = (void __iomem *)(MSP71XX_EXD_GPIO_BASE + exd_reg), \
  110. }
  111. /*
  112. * struct msp71xx_exd_gpio_banks[] - container array of gpio banks
  113. * @chip: chip structure for the specified gpio bank
  114. * @reg: register for reading and writing the gpio pin value
  115. *
  116. * This array structure defines the extended gpio banks for the
  117. * PMC MIPS Processor. We specify the bank name, the data/config
  118. * register,the base starting gpio number, and the number of
  119. * gpios exposed by the bank of gpios.
  120. */
  121. static struct msp71xx_exd_gpio_chip msp71xx_exd_gpio_banks[] = {
  122. MSP71XX_EXD_GPIO_BANK("GPIO_23_16", 0x188, 16, 8),
  123. MSP71XX_EXD_GPIO_BANK("GPIO_27_24", 0x18C, 24, 4),
  124. };
  125. void __init msp71xx_init_gpio_extended(void)
  126. {
  127. int i;
  128. for (i = 0; i < ARRAY_SIZE(msp71xx_exd_gpio_banks); i++)
  129. gpiochip_add(&msp71xx_exd_gpio_banks[i].chip);
  130. }