gpio.c 9.6 KB

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