gpio.c 3.8 KB

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