gpio.c 9.2 KB

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