gpio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 void gpio_get_data_reg(struct sh_pfc *pfc, unsigned int gpio,
  33. struct pinmux_data_reg **dr, unsigned int *bit)
  34. {
  35. struct sh_pfc_pin *gpiop = sh_pfc_get_pin(pfc, gpio);
  36. *dr = pfc->info->data_regs
  37. + ((gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT);
  38. *bit = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
  39. }
  40. static void gpio_setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
  41. {
  42. struct sh_pfc_pin *gpiop = &pfc->info->pins[gpio];
  43. struct pinmux_data_reg *data_reg;
  44. int k, n;
  45. k = 0;
  46. while (1) {
  47. data_reg = pfc->info->data_regs + k;
  48. if (!data_reg->reg_width)
  49. break;
  50. data_reg->mapped_reg = sh_pfc_phys_to_virt(pfc, data_reg->reg);
  51. for (n = 0; n < data_reg->reg_width; n++) {
  52. if (data_reg->enum_ids[n] == gpiop->enum_id) {
  53. gpiop->flags &= ~PINMUX_FLAG_DREG;
  54. gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
  55. gpiop->flags &= ~PINMUX_FLAG_DBIT;
  56. gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
  57. return;
  58. }
  59. }
  60. k++;
  61. }
  62. BUG();
  63. }
  64. static void gpio_setup_data_regs(struct sh_pfc *pfc)
  65. {
  66. struct pinmux_data_reg *drp;
  67. int k;
  68. for (k = 0; k < pfc->info->nr_pins; k++) {
  69. if (pfc->info->pins[k].enum_id == 0)
  70. continue;
  71. gpio_setup_data_reg(pfc, k);
  72. }
  73. k = 0;
  74. while (1) {
  75. drp = pfc->info->data_regs + k;
  76. if (!drp->reg_width)
  77. break;
  78. drp->reg_shadow = sh_pfc_read_raw_reg(drp->mapped_reg,
  79. drp->reg_width);
  80. k++;
  81. }
  82. }
  83. /* -----------------------------------------------------------------------------
  84. * Pin GPIOs
  85. */
  86. static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
  87. {
  88. struct sh_pfc *pfc = gpio_to_pfc(gc);
  89. struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
  90. if (pin == NULL || pin->enum_id == 0)
  91. return -EINVAL;
  92. return pinctrl_request_gpio(offset);
  93. }
  94. static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
  95. {
  96. return pinctrl_free_gpio(offset);
  97. }
  98. static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
  99. {
  100. struct pinmux_data_reg *dr;
  101. unsigned long pos;
  102. unsigned int bit;
  103. gpio_get_data_reg(pfc, offset, &dr, &bit);
  104. pos = dr->reg_width - (bit + 1);
  105. if (value)
  106. set_bit(pos, &dr->reg_shadow);
  107. else
  108. clear_bit(pos, &dr->reg_shadow);
  109. sh_pfc_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
  110. }
  111. static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
  112. {
  113. return pinctrl_gpio_direction_input(offset);
  114. }
  115. static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
  116. int value)
  117. {
  118. gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
  119. return pinctrl_gpio_direction_output(offset);
  120. }
  121. static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
  122. {
  123. struct sh_pfc *pfc = gpio_to_pfc(gc);
  124. struct pinmux_data_reg *dr;
  125. unsigned long pos;
  126. unsigned int bit;
  127. gpio_get_data_reg(pfc, offset, &dr, &bit);
  128. pos = dr->reg_width - (bit + 1);
  129. return (sh_pfc_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
  130. }
  131. static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
  132. {
  133. gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
  134. }
  135. static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
  136. {
  137. struct sh_pfc *pfc = gpio_to_pfc(gc);
  138. int i, k;
  139. for (i = 0; i < pfc->info->gpio_irq_size; i++) {
  140. unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
  141. for (k = 0; gpios[k]; k++) {
  142. if (gpios[k] == offset)
  143. return pfc->info->gpio_irq[i].irq;
  144. }
  145. }
  146. return -ENOSYS;
  147. }
  148. static void gpio_pin_setup(struct sh_pfc_chip *chip)
  149. {
  150. struct sh_pfc *pfc = chip->pfc;
  151. struct gpio_chip *gc = &chip->gpio_chip;
  152. gc->request = gpio_pin_request;
  153. gc->free = gpio_pin_free;
  154. gc->direction_input = gpio_pin_direction_input;
  155. gc->get = gpio_pin_get;
  156. gc->direction_output = gpio_pin_direction_output;
  157. gc->set = gpio_pin_set;
  158. gc->to_irq = gpio_pin_to_irq;
  159. gc->label = pfc->info->name;
  160. gc->dev = pfc->dev;
  161. gc->owner = THIS_MODULE;
  162. gc->base = 0;
  163. gc->ngpio = pfc->nr_pins;
  164. }
  165. /* -----------------------------------------------------------------------------
  166. * Function GPIOs
  167. */
  168. static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
  169. {
  170. struct sh_pfc *pfc = gpio_to_pfc(gc);
  171. unsigned int mark = pfc->info->func_gpios[offset].enum_id;
  172. unsigned long flags;
  173. int ret = -EINVAL;
  174. pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
  175. if (mark == 0)
  176. return ret;
  177. spin_lock_irqsave(&pfc->lock, flags);
  178. if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
  179. goto done;
  180. if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
  181. goto done;
  182. ret = 0;
  183. done:
  184. spin_unlock_irqrestore(&pfc->lock, flags);
  185. return ret;
  186. }
  187. static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
  188. {
  189. struct sh_pfc *pfc = gpio_to_pfc(gc);
  190. unsigned int mark = pfc->info->func_gpios[offset].enum_id;
  191. unsigned long flags;
  192. spin_lock_irqsave(&pfc->lock, flags);
  193. sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
  194. spin_unlock_irqrestore(&pfc->lock, flags);
  195. }
  196. static void gpio_function_setup(struct sh_pfc_chip *chip)
  197. {
  198. struct sh_pfc *pfc = chip->pfc;
  199. struct gpio_chip *gc = &chip->gpio_chip;
  200. gc->request = gpio_function_request;
  201. gc->free = gpio_function_free;
  202. gc->label = pfc->info->name;
  203. gc->owner = THIS_MODULE;
  204. gc->base = pfc->nr_pins;
  205. gc->ngpio = pfc->info->nr_func_gpios;
  206. }
  207. /* -----------------------------------------------------------------------------
  208. * Register/unregister
  209. */
  210. static struct sh_pfc_chip *
  211. sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
  212. {
  213. struct sh_pfc_chip *chip;
  214. int ret;
  215. chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
  216. if (unlikely(!chip))
  217. return ERR_PTR(-ENOMEM);
  218. chip->pfc = pfc;
  219. setup(chip);
  220. ret = gpiochip_add(&chip->gpio_chip);
  221. if (unlikely(ret < 0))
  222. return ERR_PTR(ret);
  223. pr_info("%s handling gpio %u -> %u\n",
  224. chip->gpio_chip.label, chip->gpio_chip.base,
  225. chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
  226. return chip;
  227. }
  228. int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
  229. {
  230. const struct pinmux_range *ranges;
  231. struct pinmux_range def_range;
  232. struct sh_pfc_chip *chip;
  233. unsigned int nr_ranges;
  234. unsigned int i;
  235. int ret;
  236. gpio_setup_data_regs(pfc);
  237. /* Register the real GPIOs chip. */
  238. chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
  239. if (IS_ERR(chip))
  240. return PTR_ERR(chip);
  241. pfc->gpio = chip;
  242. /* Register the GPIO to pin mappings. */
  243. if (pfc->info->ranges == NULL) {
  244. def_range.begin = 0;
  245. def_range.end = pfc->info->nr_pins - 1;
  246. ranges = &def_range;
  247. nr_ranges = 1;
  248. } else {
  249. ranges = pfc->info->ranges;
  250. nr_ranges = pfc->info->nr_ranges;
  251. }
  252. for (i = 0; i < nr_ranges; ++i) {
  253. const struct pinmux_range *range = &ranges[i];
  254. ret = gpiochip_add_pin_range(&chip->gpio_chip,
  255. dev_name(pfc->dev),
  256. range->begin, range->begin,
  257. range->end - range->begin + 1);
  258. if (ret < 0)
  259. return ret;
  260. }
  261. /* Register the function GPIOs chip. */
  262. chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
  263. if (IS_ERR(chip))
  264. return PTR_ERR(chip);
  265. pfc->func = chip;
  266. return 0;
  267. }
  268. int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
  269. {
  270. int err;
  271. int ret;
  272. ret = gpiochip_remove(&pfc->gpio->gpio_chip);
  273. err = gpiochip_remove(&pfc->func->gpio_chip);
  274. return ret < 0 ? ret : err;
  275. }