gpio.c 9.6 KB

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