gpio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * SuperH Pin Function Controller GPIO driver.
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. * Copyright (C) 2009 - 2012 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
  12. #include <linux/device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include "core.h"
  20. struct sh_pfc_chip {
  21. struct sh_pfc *pfc;
  22. struct gpio_chip gpio_chip;
  23. };
  24. static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
  25. {
  26. return container_of(gc, struct sh_pfc_chip, gpio_chip);
  27. }
  28. static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
  29. {
  30. return gpio_to_pfc_chip(gc)->pfc;
  31. }
  32. /* -----------------------------------------------------------------------------
  33. * Pin GPIOs
  34. */
  35. static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
  36. {
  37. struct sh_pfc *pfc = gpio_to_pfc(gc);
  38. if (pfc->info->pins[offset].enum_id == 0)
  39. return -EINVAL;
  40. return pinctrl_request_gpio(offset);
  41. }
  42. static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
  43. {
  44. return pinctrl_free_gpio(offset);
  45. }
  46. static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
  47. {
  48. struct pinmux_data_reg *dr;
  49. int bit;
  50. sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
  51. sh_pfc_write_bit(dr, bit, value);
  52. }
  53. static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
  54. {
  55. return pinctrl_gpio_direction_input(offset);
  56. }
  57. static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
  58. int value)
  59. {
  60. gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
  61. return pinctrl_gpio_direction_output(offset);
  62. }
  63. static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
  64. {
  65. struct sh_pfc *pfc = gpio_to_pfc(gc);
  66. struct pinmux_data_reg *dr;
  67. int bit;
  68. sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
  69. return sh_pfc_read_bit(dr, bit);
  70. }
  71. static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
  72. {
  73. gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
  74. }
  75. static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
  76. {
  77. struct sh_pfc *pfc = gpio_to_pfc(gc);
  78. int i, k;
  79. for (i = 0; i < pfc->info->gpio_irq_size; i++) {
  80. unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
  81. for (k = 0; gpios[k]; k++) {
  82. if (gpios[k] == offset)
  83. return pfc->info->gpio_irq[i].irq;
  84. }
  85. }
  86. return -ENOSYS;
  87. }
  88. static void gpio_pin_setup(struct sh_pfc_chip *chip)
  89. {
  90. struct sh_pfc *pfc = chip->pfc;
  91. struct gpio_chip *gc = &chip->gpio_chip;
  92. gc->request = gpio_pin_request;
  93. gc->free = gpio_pin_free;
  94. gc->direction_input = gpio_pin_direction_input;
  95. gc->get = gpio_pin_get;
  96. gc->direction_output = gpio_pin_direction_output;
  97. gc->set = gpio_pin_set;
  98. gc->to_irq = gpio_pin_to_irq;
  99. gc->label = pfc->info->name;
  100. gc->dev = pfc->dev;
  101. gc->owner = THIS_MODULE;
  102. gc->base = 0;
  103. gc->ngpio = pfc->info->nr_pins;
  104. }
  105. /* -----------------------------------------------------------------------------
  106. * Function GPIOs
  107. */
  108. static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
  109. {
  110. struct sh_pfc *pfc = gpio_to_pfc(gc);
  111. unsigned int mark = pfc->info->func_gpios[offset].enum_id;
  112. unsigned long flags;
  113. int ret = -EINVAL;
  114. pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
  115. if (mark == 0)
  116. return ret;
  117. spin_lock_irqsave(&pfc->lock, flags);
  118. if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
  119. goto done;
  120. if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
  121. goto done;
  122. ret = 0;
  123. done:
  124. spin_unlock_irqrestore(&pfc->lock, flags);
  125. return ret;
  126. }
  127. static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
  128. {
  129. struct sh_pfc *pfc = gpio_to_pfc(gc);
  130. unsigned int mark = pfc->info->func_gpios[offset].enum_id;
  131. unsigned long flags;
  132. spin_lock_irqsave(&pfc->lock, flags);
  133. sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
  134. spin_unlock_irqrestore(&pfc->lock, flags);
  135. }
  136. static void gpio_function_setup(struct sh_pfc_chip *chip)
  137. {
  138. struct sh_pfc *pfc = chip->pfc;
  139. struct gpio_chip *gc = &chip->gpio_chip;
  140. gc->request = gpio_function_request;
  141. gc->free = gpio_function_free;
  142. gc->label = pfc->info->name;
  143. gc->owner = THIS_MODULE;
  144. gc->base = pfc->info->nr_pins;
  145. gc->ngpio = pfc->info->nr_func_gpios;
  146. }
  147. /* -----------------------------------------------------------------------------
  148. * Register/unregister
  149. */
  150. static struct sh_pfc_chip *
  151. sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
  152. {
  153. struct sh_pfc_chip *chip;
  154. int ret;
  155. chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
  156. if (unlikely(!chip))
  157. return ERR_PTR(-ENOMEM);
  158. chip->pfc = pfc;
  159. setup(chip);
  160. ret = gpiochip_add(&chip->gpio_chip);
  161. if (unlikely(ret < 0))
  162. return ERR_PTR(ret);
  163. pr_info("%s handling gpio %u -> %u\n",
  164. chip->gpio_chip.label, chip->gpio_chip.base,
  165. chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
  166. return chip;
  167. }
  168. int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
  169. {
  170. struct sh_pfc_chip *chip;
  171. chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
  172. if (IS_ERR(chip))
  173. return PTR_ERR(chip);
  174. pfc->gpio = chip;
  175. chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
  176. if (IS_ERR(chip))
  177. return PTR_ERR(chip);
  178. pfc->func = chip;
  179. return 0;
  180. }
  181. int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
  182. {
  183. int err;
  184. int ret;
  185. ret = gpiochip_remove(&pfc->gpio->gpio_chip);
  186. err = gpiochip_remove(&pfc->func->gpio_chip);
  187. return ret < 0 ? ret : err;
  188. }