gpio.c 9.4 KB

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