gpio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
  39. if (pin->enum_id == 0)
  40. return -EINVAL;
  41. return pinctrl_request_gpio(offset);
  42. }
  43. static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
  44. {
  45. return pinctrl_free_gpio(offset);
  46. }
  47. static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
  48. {
  49. struct pinmux_data_reg *dr;
  50. int bit;
  51. sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
  52. sh_pfc_write_bit(dr, bit, value);
  53. }
  54. static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
  55. {
  56. return pinctrl_gpio_direction_input(offset);
  57. }
  58. static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
  59. int value)
  60. {
  61. gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
  62. return pinctrl_gpio_direction_output(offset);
  63. }
  64. static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
  65. {
  66. struct sh_pfc *pfc = gpio_to_pfc(gc);
  67. struct pinmux_data_reg *dr;
  68. int bit;
  69. sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
  70. return sh_pfc_read_bit(dr, bit);
  71. }
  72. static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
  73. {
  74. gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
  75. }
  76. static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
  77. {
  78. struct sh_pfc *pfc = gpio_to_pfc(gc);
  79. int i, k;
  80. for (i = 0; i < pfc->info->gpio_irq_size; i++) {
  81. unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
  82. for (k = 0; gpios[k]; k++) {
  83. if (gpios[k] == offset)
  84. return pfc->info->gpio_irq[i].irq;
  85. }
  86. }
  87. return -ENOSYS;
  88. }
  89. static void gpio_pin_setup(struct sh_pfc_chip *chip)
  90. {
  91. struct sh_pfc *pfc = chip->pfc;
  92. struct gpio_chip *gc = &chip->gpio_chip;
  93. gc->request = gpio_pin_request;
  94. gc->free = gpio_pin_free;
  95. gc->direction_input = gpio_pin_direction_input;
  96. gc->get = gpio_pin_get;
  97. gc->direction_output = gpio_pin_direction_output;
  98. gc->set = gpio_pin_set;
  99. gc->to_irq = gpio_pin_to_irq;
  100. gc->label = pfc->info->name;
  101. gc->dev = pfc->dev;
  102. gc->owner = THIS_MODULE;
  103. gc->base = 0;
  104. gc->ngpio = pfc->info->nr_pins;
  105. }
  106. /* -----------------------------------------------------------------------------
  107. * Function GPIOs
  108. */
  109. static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
  110. {
  111. struct sh_pfc *pfc = gpio_to_pfc(gc);
  112. unsigned int mark = pfc->info->func_gpios[offset].enum_id;
  113. unsigned long flags;
  114. int ret = -EINVAL;
  115. pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
  116. if (mark == 0)
  117. return ret;
  118. spin_lock_irqsave(&pfc->lock, flags);
  119. if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
  120. goto done;
  121. if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
  122. goto done;
  123. ret = 0;
  124. done:
  125. spin_unlock_irqrestore(&pfc->lock, flags);
  126. return ret;
  127. }
  128. static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
  129. {
  130. struct sh_pfc *pfc = gpio_to_pfc(gc);
  131. unsigned int mark = pfc->info->func_gpios[offset].enum_id;
  132. unsigned long flags;
  133. spin_lock_irqsave(&pfc->lock, flags);
  134. sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
  135. spin_unlock_irqrestore(&pfc->lock, flags);
  136. }
  137. static void gpio_function_setup(struct sh_pfc_chip *chip)
  138. {
  139. struct sh_pfc *pfc = chip->pfc;
  140. struct gpio_chip *gc = &chip->gpio_chip;
  141. gc->request = gpio_function_request;
  142. gc->free = gpio_function_free;
  143. gc->label = pfc->info->name;
  144. gc->owner = THIS_MODULE;
  145. gc->base = pfc->info->nr_pins;
  146. gc->ngpio = pfc->info->nr_func_gpios;
  147. }
  148. /* -----------------------------------------------------------------------------
  149. * Register/unregister
  150. */
  151. static struct sh_pfc_chip *
  152. sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
  153. {
  154. struct sh_pfc_chip *chip;
  155. int ret;
  156. chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
  157. if (unlikely(!chip))
  158. return ERR_PTR(-ENOMEM);
  159. chip->pfc = pfc;
  160. setup(chip);
  161. ret = gpiochip_add(&chip->gpio_chip);
  162. if (unlikely(ret < 0))
  163. return ERR_PTR(ret);
  164. pr_info("%s handling gpio %u -> %u\n",
  165. chip->gpio_chip.label, chip->gpio_chip.base,
  166. chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
  167. return chip;
  168. }
  169. int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
  170. {
  171. struct sh_pfc_chip *chip;
  172. int ret;
  173. chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
  174. if (IS_ERR(chip))
  175. return PTR_ERR(chip);
  176. pfc->gpio = chip;
  177. ret = gpiochip_add_pin_range(&chip->gpio_chip, dev_name(pfc->dev), 0, 0,
  178. chip->gpio_chip.ngpio);
  179. if (ret < 0)
  180. return ret;
  181. chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
  182. if (IS_ERR(chip))
  183. return PTR_ERR(chip);
  184. pfc->func = chip;
  185. return 0;
  186. }
  187. int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
  188. {
  189. int err;
  190. int ret;
  191. ret = gpiochip_remove(&pfc->gpio->gpio_chip);
  192. err = gpiochip_remove(&pfc->func->gpio_chip);
  193. return ret < 0 ? ret : err;
  194. }