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