gpio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * @file /arch/mips/pmc-sierra/msp71xx/gpio.c
  3. *
  4. * Generic PMC MSP71xx GPIO handling. These base gpio are controlled by two
  5. * types of registers. The data register sets the output level when in output
  6. * mode and when in input mode will contain the value at the input. The config
  7. * register sets the various modes for each gpio.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * @author Patrick Glass <patrickglass@gmail.com>
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/gpio.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/io.h>
  21. #define MSP71XX_CFG_OFFSET(gpio) (4 * (gpio))
  22. #define CONF_MASK 0x0F
  23. #define MSP71XX_GPIO_INPUT 0x01
  24. #define MSP71XX_GPIO_OUTPUT 0x08
  25. #define MSP71XX_GPIO_BASE 0x0B8400000L
  26. #define to_msp71xx_gpio_chip(c) container_of(c, struct msp71xx_gpio_chip, chip)
  27. static spinlock_t gpio_lock;
  28. /*
  29. * struct msp71xx_gpio_chip - container for gpio chip and registers
  30. * @chip: chip structure for the specified gpio bank
  31. * @data_reg: register for reading and writing the gpio pin value
  32. * @config_reg: register to set the mode for the gpio pin bank
  33. * @out_drive_reg: register to set the output drive mode for the gpio pin bank
  34. */
  35. struct msp71xx_gpio_chip {
  36. struct gpio_chip chip;
  37. void __iomem *data_reg;
  38. void __iomem *config_reg;
  39. void __iomem *out_drive_reg;
  40. };
  41. /*
  42. * msp71xx_gpio_get() - return the chip's gpio value
  43. * @chip: chip structure which controls the specified gpio
  44. * @offset: gpio whose value will be returned
  45. *
  46. * It will return 0 if gpio value is low and other if high.
  47. */
  48. static int msp71xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  49. {
  50. struct msp71xx_gpio_chip *msp_chip = to_msp71xx_gpio_chip(chip);
  51. return __raw_readl(msp_chip->data_reg) & (1 << offset);
  52. }
  53. /*
  54. * msp71xx_gpio_set() - set the output value for the gpio
  55. * @chip: chip structure who controls the specified gpio
  56. * @offset: gpio whose value will be assigned
  57. * @value: logic level to assign to the gpio initially
  58. *
  59. * This will set the gpio bit specified to the desired value. It will set the
  60. * gpio pin low if value is 0 otherwise it will be high.
  61. */
  62. static void msp71xx_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  63. {
  64. struct msp71xx_gpio_chip *msp_chip = to_msp71xx_gpio_chip(chip);
  65. unsigned long flags;
  66. u32 data;
  67. spin_lock_irqsave(&gpio_lock, flags);
  68. data = __raw_readl(msp_chip->data_reg);
  69. if (value)
  70. data |= (1 << offset);
  71. else
  72. data &= ~(1 << offset);
  73. __raw_writel(data, msp_chip->data_reg);
  74. spin_unlock_irqrestore(&gpio_lock, flags);
  75. }
  76. /*
  77. * msp71xx_set_gpio_mode() - declare the mode for a gpio
  78. * @chip: chip structure which controls the specified gpio
  79. * @offset: gpio whose value will be assigned
  80. * @mode: desired configuration for the gpio (see datasheet)
  81. *
  82. * It will set the gpio pin config to the @mode value passed in.
  83. */
  84. static int msp71xx_set_gpio_mode(struct gpio_chip *chip,
  85. unsigned offset, int mode)
  86. {
  87. struct msp71xx_gpio_chip *msp_chip = to_msp71xx_gpio_chip(chip);
  88. const unsigned bit_offset = MSP71XX_CFG_OFFSET(offset);
  89. unsigned long flags;
  90. u32 cfg;
  91. spin_lock_irqsave(&gpio_lock, flags);
  92. cfg = __raw_readl(msp_chip->config_reg);
  93. cfg &= ~(CONF_MASK << bit_offset);
  94. cfg |= (mode << bit_offset);
  95. __raw_writel(cfg, msp_chip->config_reg);
  96. spin_unlock_irqrestore(&gpio_lock, flags);
  97. return 0;
  98. }
  99. /*
  100. * msp71xx_direction_output() - declare the direction mode for a gpio
  101. * @chip: chip structure which controls the specified gpio
  102. * @offset: gpio whose value will be assigned
  103. * @value: logic level to assign to the gpio initially
  104. *
  105. * This call will set the mode for the @gpio to output. It will set the
  106. * gpio pin low if value is 0 otherwise it will be high.
  107. */
  108. static int msp71xx_direction_output(struct gpio_chip *chip,
  109. unsigned offset, int value)
  110. {
  111. msp71xx_gpio_set(chip, offset, value);
  112. return msp71xx_set_gpio_mode(chip, offset, MSP71XX_GPIO_OUTPUT);
  113. }
  114. /*
  115. * msp71xx_direction_input() - declare the direction mode for a gpio
  116. * @chip: chip structure which controls the specified gpio
  117. * @offset: gpio whose to which the value will be assigned
  118. *
  119. * This call will set the mode for the @gpio to input.
  120. */
  121. static int msp71xx_direction_input(struct gpio_chip *chip, unsigned offset)
  122. {
  123. return msp71xx_set_gpio_mode(chip, offset, MSP71XX_GPIO_INPUT);
  124. }
  125. /*
  126. * msp71xx_set_output_drive() - declare the output drive for the gpio line
  127. * @gpio: gpio pin whose output drive you wish to modify
  128. * @value: zero for active drain 1 for open drain drive
  129. *
  130. * This call will set the output drive mode for the @gpio to output.
  131. */
  132. int msp71xx_set_output_drive(unsigned gpio, int value)
  133. {
  134. unsigned long flags;
  135. u32 data;
  136. if (gpio > 15 || gpio < 0)
  137. return -EINVAL;
  138. spin_lock_irqsave(&gpio_lock, flags);
  139. data = __raw_readl((void __iomem *)(MSP71XX_GPIO_BASE + 0x190));
  140. if (value)
  141. data |= (1 << gpio);
  142. else
  143. data &= ~(1 << gpio);
  144. __raw_writel(data, (void __iomem *)(MSP71XX_GPIO_BASE + 0x190));
  145. spin_unlock_irqrestore(&gpio_lock, flags);
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(msp71xx_set_output_drive);
  149. #define MSP71XX_GPIO_BANK(name, dr, cr, base_gpio, num_gpio) \
  150. { \
  151. .chip = { \
  152. .label = name, \
  153. .direction_input = msp71xx_direction_input, \
  154. .direction_output = msp71xx_direction_output, \
  155. .get = msp71xx_gpio_get, \
  156. .set = msp71xx_gpio_set, \
  157. .base = base_gpio, \
  158. .ngpio = num_gpio \
  159. }, \
  160. .data_reg = (void __iomem *)(MSP71XX_GPIO_BASE + dr), \
  161. .config_reg = (void __iomem *)(MSP71XX_GPIO_BASE + cr), \
  162. .out_drive_reg = (void __iomem *)(MSP71XX_GPIO_BASE + 0x190), \
  163. }
  164. /*
  165. * struct msp71xx_gpio_banks[] - container array of gpio banks
  166. * @chip: chip structure for the specified gpio bank
  167. * @data_reg: register for reading and writing the gpio pin value
  168. * @config_reg: register to set the mode for the gpio pin bank
  169. *
  170. * This array structure defines the gpio banks for the PMC MIPS Processor.
  171. * We specify the bank name, the data register, the config register, base
  172. * starting gpio number, and the number of gpios exposed by the bank.
  173. */
  174. static struct msp71xx_gpio_chip msp71xx_gpio_banks[] = {
  175. MSP71XX_GPIO_BANK("GPIO_1_0", 0x170, 0x180, 0, 2),
  176. MSP71XX_GPIO_BANK("GPIO_5_2", 0x174, 0x184, 2, 4),
  177. MSP71XX_GPIO_BANK("GPIO_9_6", 0x178, 0x188, 6, 4),
  178. MSP71XX_GPIO_BANK("GPIO_15_10", 0x17C, 0x18C, 10, 6),
  179. };
  180. void __init msp71xx_init_gpio(void)
  181. {
  182. int i;
  183. spin_lock_init(&gpio_lock);
  184. for (i = 0; i < ARRAY_SIZE(msp71xx_gpio_banks); i++)
  185. gpiochip_add(&msp71xx_gpio_banks[i].chip);
  186. }