gpio.c 9.3 KB

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