gpio.c 5.7 KB

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