gpio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. return pinctrl_request_gpio(offset);
  35. }
  36. static void sh_gpio_free(struct gpio_chip *gc, unsigned offset)
  37. {
  38. pinctrl_free_gpio(offset);
  39. }
  40. static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value)
  41. {
  42. struct pinmux_data_reg *dr = NULL;
  43. int bit = 0;
  44. if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
  45. BUG();
  46. else
  47. sh_pfc_write_bit(dr, bit, value);
  48. }
  49. static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio)
  50. {
  51. struct pinmux_data_reg *dr = NULL;
  52. int bit = 0;
  53. if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
  54. return -EINVAL;
  55. return sh_pfc_read_bit(dr, bit);
  56. }
  57. static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  58. {
  59. return pinctrl_gpio_direction_input(offset);
  60. }
  61. static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  62. int value)
  63. {
  64. sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
  65. return pinctrl_gpio_direction_output(offset);
  66. }
  67. static int sh_gpio_get(struct gpio_chip *gc, unsigned offset)
  68. {
  69. return sh_gpio_get_value(gpio_to_pfc(gc), offset);
  70. }
  71. static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  72. {
  73. sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
  74. }
  75. static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  76. {
  77. struct sh_pfc *pfc = gpio_to_pfc(gc);
  78. pinmux_enum_t enum_id;
  79. pinmux_enum_t *enum_ids;
  80. int i, k, pos;
  81. pos = 0;
  82. enum_id = 0;
  83. while (1) {
  84. pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
  85. if (pos <= 0 || !enum_id)
  86. break;
  87. for (i = 0; i < pfc->info->gpio_irq_size; i++) {
  88. enum_ids = pfc->info->gpio_irq[i].enum_ids;
  89. for (k = 0; enum_ids[k]; k++) {
  90. if (enum_ids[k] == enum_id)
  91. return pfc->info->gpio_irq[i].irq;
  92. }
  93. }
  94. }
  95. return -ENOSYS;
  96. }
  97. static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip)
  98. {
  99. struct sh_pfc *pfc = chip->pfc;
  100. struct gpio_chip *gc = &chip->gpio_chip;
  101. gc->request = sh_gpio_request;
  102. gc->free = sh_gpio_free;
  103. gc->direction_input = sh_gpio_direction_input;
  104. gc->get = sh_gpio_get;
  105. gc->direction_output = sh_gpio_direction_output;
  106. gc->set = sh_gpio_set;
  107. gc->to_irq = sh_gpio_to_irq;
  108. WARN_ON(pfc->info->first_gpio != 0); /* needs testing */
  109. gc->label = pfc->info->name;
  110. gc->owner = THIS_MODULE;
  111. gc->base = pfc->info->first_gpio;
  112. gc->ngpio = (pfc->info->last_gpio - pfc->info->first_gpio) + 1;
  113. }
  114. int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
  115. {
  116. struct sh_pfc_chip *chip;
  117. int ret;
  118. chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
  119. if (unlikely(!chip))
  120. return -ENOMEM;
  121. chip->pfc = pfc;
  122. sh_pfc_gpio_setup(chip);
  123. ret = gpiochip_add(&chip->gpio_chip);
  124. if (unlikely(ret < 0))
  125. return ret;
  126. pfc->gpio = chip;
  127. pr_info("%s handling gpio %d -> %d\n",
  128. pfc->info->name, pfc->info->first_gpio,
  129. pfc->info->last_gpio);
  130. return 0;
  131. }
  132. int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
  133. {
  134. struct sh_pfc_chip *chip = pfc->gpio;
  135. int ret;
  136. ret = gpiochip_remove(&chip->gpio_chip);
  137. if (unlikely(ret < 0))
  138. return ret;
  139. pfc->gpio = NULL;
  140. return 0;
  141. }