gpio.c 9.3 KB

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