gpio.c 9.1 KB

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