gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. static int sh_gpio_request(struct gpio_chip *gc, unsigned offset)
  33. {
  34. struct sh_pfc *pfc = gpio_to_pfc(gc);
  35. unsigned long flags;
  36. int ret = -EINVAL;
  37. if (offset < pfc->info->nr_pins)
  38. return pinctrl_request_gpio(offset);
  39. pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
  40. spin_lock_irqsave(&pfc->lock, flags);
  41. if (!sh_pfc_gpio_is_function(pfc, offset))
  42. goto done;
  43. if (sh_pfc_config_gpio(pfc, offset, PINMUX_TYPE_FUNCTION,
  44. GPIO_CFG_DRYRUN))
  45. goto done;
  46. if (sh_pfc_config_gpio(pfc, offset, PINMUX_TYPE_FUNCTION,
  47. GPIO_CFG_REQ))
  48. goto done;
  49. ret = 0;
  50. done:
  51. spin_unlock_irqrestore(&pfc->lock, flags);
  52. return ret;
  53. }
  54. static void sh_gpio_free(struct gpio_chip *gc, unsigned offset)
  55. {
  56. struct sh_pfc *pfc = gpio_to_pfc(gc);
  57. unsigned long flags;
  58. if (offset < pfc->info->nr_pins)
  59. return pinctrl_free_gpio(offset);
  60. spin_lock_irqsave(&pfc->lock, flags);
  61. sh_pfc_config_gpio(pfc, offset, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
  62. spin_unlock_irqrestore(&pfc->lock, flags);
  63. }
  64. static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value)
  65. {
  66. struct pinmux_data_reg *dr = NULL;
  67. int bit = 0;
  68. if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
  69. BUG();
  70. else
  71. sh_pfc_write_bit(dr, bit, value);
  72. }
  73. static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio)
  74. {
  75. struct pinmux_data_reg *dr = NULL;
  76. int bit = 0;
  77. if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
  78. return -EINVAL;
  79. return sh_pfc_read_bit(dr, bit);
  80. }
  81. static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  82. {
  83. struct sh_pfc *pfc = gpio_to_pfc(gc);
  84. if (offset >= pfc->info->nr_pins) {
  85. /* Function GPIOs can only be requested, never configured. */
  86. return -EINVAL;
  87. }
  88. return pinctrl_gpio_direction_input(offset);
  89. }
  90. static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  91. int value)
  92. {
  93. struct sh_pfc *pfc = gpio_to_pfc(gc);
  94. if (offset >= pfc->info->nr_pins) {
  95. /* Function GPIOs can only be requested, never configured. */
  96. return -EINVAL;
  97. }
  98. sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
  99. return pinctrl_gpio_direction_output(offset);
  100. }
  101. static int sh_gpio_get(struct gpio_chip *gc, unsigned offset)
  102. {
  103. return sh_gpio_get_value(gpio_to_pfc(gc), offset);
  104. }
  105. static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  106. {
  107. sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
  108. }
  109. static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  110. {
  111. struct sh_pfc *pfc = gpio_to_pfc(gc);
  112. pinmux_enum_t enum_id;
  113. pinmux_enum_t *enum_ids;
  114. int i, k, pos;
  115. pos = 0;
  116. enum_id = 0;
  117. while (1) {
  118. pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
  119. if (pos <= 0 || !enum_id)
  120. break;
  121. for (i = 0; i < pfc->info->gpio_irq_size; i++) {
  122. enum_ids = pfc->info->gpio_irq[i].enum_ids;
  123. for (k = 0; enum_ids[k]; k++) {
  124. if (enum_ids[k] == enum_id)
  125. return pfc->info->gpio_irq[i].irq;
  126. }
  127. }
  128. }
  129. return -ENOSYS;
  130. }
  131. static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip)
  132. {
  133. struct sh_pfc *pfc = chip->pfc;
  134. struct gpio_chip *gc = &chip->gpio_chip;
  135. gc->request = sh_gpio_request;
  136. gc->free = sh_gpio_free;
  137. gc->direction_input = sh_gpio_direction_input;
  138. gc->get = sh_gpio_get;
  139. gc->direction_output = sh_gpio_direction_output;
  140. gc->set = sh_gpio_set;
  141. gc->to_irq = sh_gpio_to_irq;
  142. gc->label = pfc->info->name;
  143. gc->owner = THIS_MODULE;
  144. gc->base = 0;
  145. gc->ngpio = pfc->info->nr_gpios;
  146. }
  147. int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
  148. {
  149. struct sh_pfc_chip *chip;
  150. int ret;
  151. chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
  152. if (unlikely(!chip))
  153. return -ENOMEM;
  154. chip->pfc = pfc;
  155. sh_pfc_gpio_setup(chip);
  156. ret = gpiochip_add(&chip->gpio_chip);
  157. if (unlikely(ret < 0))
  158. return ret;
  159. pfc->gpio = chip;
  160. pr_info("%s handling gpio 0 -> %u\n",
  161. pfc->info->name, pfc->info->nr_gpios - 1);
  162. return 0;
  163. }
  164. int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
  165. {
  166. struct sh_pfc_chip *chip = pfc->gpio;
  167. int ret;
  168. ret = gpiochip_remove(&chip->gpio_chip);
  169. if (unlikely(ret < 0))
  170. return ret;
  171. pfc->gpio = NULL;
  172. return 0;
  173. }