pinctrl-apple-gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Apple SoC pinctrl+GPIO+external IRQ driver
  4. *
  5. * Copyright (C) The Asahi Linux Contributors
  6. * Copyright (C) 2020 Corellium LLC
  7. *
  8. * Based on: pinctrl-pistachio.c
  9. * Copyright (C) 2014 Imagination Technologies Ltd.
  10. * Copyright (C) 2014 Google, Inc.
  11. */
  12. #include <dt-bindings/pinctrl/apple.h>
  13. #include <linux/bits.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include "pinctrl-utils.h"
  25. #include "core.h"
  26. #include "pinmux.h"
  27. struct apple_gpio_pinctrl {
  28. struct device *dev;
  29. struct pinctrl_dev *pctldev;
  30. void __iomem *base;
  31. struct regmap *map;
  32. struct pinctrl_desc pinctrl_desc;
  33. struct gpio_chip gpio_chip;
  34. u8 irqgrps[];
  35. };
  36. #define REG_GPIO(x) (4 * (x))
  37. #define REG_GPIOx_DATA BIT(0)
  38. #define REG_GPIOx_MODE GENMASK(3, 1)
  39. #define REG_GPIOx_OUT 1
  40. #define REG_GPIOx_IN_IRQ_HI 2
  41. #define REG_GPIOx_IN_IRQ_LO 3
  42. #define REG_GPIOx_IN_IRQ_UP 4
  43. #define REG_GPIOx_IN_IRQ_DN 5
  44. #define REG_GPIOx_IN_IRQ_ANY 6
  45. #define REG_GPIOx_IN_IRQ_OFF 7
  46. #define REG_GPIOx_PERIPH GENMASK(6, 5)
  47. #define REG_GPIOx_PULL GENMASK(8, 7)
  48. #define REG_GPIOx_PULL_OFF 0
  49. #define REG_GPIOx_PULL_DOWN 1
  50. #define REG_GPIOx_PULL_UP_STRONG 2
  51. #define REG_GPIOx_PULL_UP 3
  52. #define REG_GPIOx_INPUT_ENABLE BIT(9)
  53. #define REG_GPIOx_DRIVE_STRENGTH0 GENMASK(11, 10)
  54. #define REG_GPIOx_SCHMITT BIT(15)
  55. #define REG_GPIOx_GRP GENMASK(18, 16)
  56. #define REG_GPIOx_LOCK BIT(21)
  57. #define REG_GPIOx_DRIVE_STRENGTH1 GENMASK(23, 22)
  58. #define REG_IRQ(g, x) (0x800 + 0x40 * (g) + 4 * ((x) >> 5))
  59. struct regmap_config regmap_config = {
  60. .reg_bits = 32,
  61. .val_bits = 32,
  62. .reg_stride = 4,
  63. .cache_type = REGCACHE_FLAT,
  64. .max_register = 512 * sizeof(u32),
  65. .num_reg_defaults_raw = 512,
  66. .use_relaxed_mmio = true,
  67. .use_raw_spinlock = true,
  68. };
  69. /* No locking needed to mask/unmask IRQs as the interrupt mode is per pin-register. */
  70. static void apple_gpio_set_reg(struct apple_gpio_pinctrl *pctl,
  71. unsigned int pin, u32 mask, u32 value)
  72. {
  73. regmap_update_bits(pctl->map, REG_GPIO(pin), mask, value);
  74. }
  75. static u32 apple_gpio_get_reg(struct apple_gpio_pinctrl *pctl,
  76. unsigned int pin)
  77. {
  78. int ret;
  79. u32 val;
  80. ret = regmap_read(pctl->map, REG_GPIO(pin), &val);
  81. if (ret)
  82. return 0;
  83. return val;
  84. }
  85. /* Pin controller functions */
  86. static int apple_gpio_dt_node_to_map(struct pinctrl_dev *pctldev,
  87. struct device_node *node,
  88. struct pinctrl_map **map,
  89. unsigned *num_maps)
  90. {
  91. unsigned reserved_maps;
  92. struct apple_gpio_pinctrl *pctl;
  93. u32 pinfunc, pin, func;
  94. int num_pins, i, ret;
  95. const char *group_name;
  96. const char *function_name;
  97. *map = NULL;
  98. *num_maps = 0;
  99. reserved_maps = 0;
  100. pctl = pinctrl_dev_get_drvdata(pctldev);
  101. ret = of_property_count_u32_elems(node, "pinmux");
  102. if (ret <= 0) {
  103. dev_err(pctl->dev,
  104. "missing or empty pinmux property in node %pOFn.\n",
  105. node);
  106. return ret ? ret : -EINVAL;
  107. }
  108. num_pins = ret;
  109. ret = pinctrl_utils_reserve_map(pctldev, map, &reserved_maps, num_maps, num_pins);
  110. if (ret)
  111. return ret;
  112. for (i = 0; i < num_pins; i++) {
  113. ret = of_property_read_u32_index(node, "pinmux", i, &pinfunc);
  114. if (ret)
  115. goto free_map;
  116. pin = APPLE_PIN(pinfunc);
  117. func = APPLE_FUNC(pinfunc);
  118. if (func >= pinmux_generic_get_function_count(pctldev)) {
  119. ret = -EINVAL;
  120. goto free_map;
  121. }
  122. group_name = pinctrl_generic_get_group_name(pctldev, pin);
  123. function_name = pinmux_generic_get_function_name(pctl->pctldev, func);
  124. ret = pinctrl_utils_add_map_mux(pctl->pctldev, map,
  125. &reserved_maps, num_maps,
  126. group_name, function_name);
  127. if (ret)
  128. goto free_map;
  129. }
  130. free_map:
  131. if (ret < 0)
  132. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  133. return ret;
  134. }
  135. static const struct pinctrl_ops apple_gpio_pinctrl_ops = {
  136. .get_groups_count = pinctrl_generic_get_group_count,
  137. .get_group_name = pinctrl_generic_get_group_name,
  138. .get_group_pins = pinctrl_generic_get_group_pins,
  139. .dt_node_to_map = apple_gpio_dt_node_to_map,
  140. .dt_free_map = pinctrl_utils_free_map,
  141. };
  142. /* Pin multiplexer functions */
  143. static int apple_gpio_pinmux_set(struct pinctrl_dev *pctldev, unsigned func,
  144. unsigned group)
  145. {
  146. struct apple_gpio_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  147. apple_gpio_set_reg(
  148. pctl, group, REG_GPIOx_PERIPH | REG_GPIOx_INPUT_ENABLE,
  149. FIELD_PREP(REG_GPIOx_PERIPH, func) | REG_GPIOx_INPUT_ENABLE);
  150. return 0;
  151. }
  152. static const struct pinmux_ops apple_gpio_pinmux_ops = {
  153. .get_functions_count = pinmux_generic_get_function_count,
  154. .get_function_name = pinmux_generic_get_function_name,
  155. .get_function_groups = pinmux_generic_get_function_groups,
  156. .set_mux = apple_gpio_pinmux_set,
  157. .strict = true,
  158. };
  159. /* GPIO chip functions */
  160. static int apple_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  161. {
  162. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  163. unsigned int reg = apple_gpio_get_reg(pctl, offset);
  164. if (FIELD_GET(REG_GPIOx_MODE, reg) == REG_GPIOx_OUT)
  165. return GPIO_LINE_DIRECTION_OUT;
  166. return GPIO_LINE_DIRECTION_IN;
  167. }
  168. static int apple_gpio_get(struct gpio_chip *chip, unsigned offset)
  169. {
  170. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  171. unsigned int reg = apple_gpio_get_reg(pctl, offset);
  172. /*
  173. * If this is an input GPIO, read the actual value (not the
  174. * cached regmap value)
  175. */
  176. if (FIELD_GET(REG_GPIOx_MODE, reg) != REG_GPIOx_OUT)
  177. reg = readl_relaxed(pctl->base + REG_GPIO(offset));
  178. return !!(reg & REG_GPIOx_DATA);
  179. }
  180. static void apple_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
  181. {
  182. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  183. apple_gpio_set_reg(pctl, offset, REG_GPIOx_DATA, value ? REG_GPIOx_DATA : 0);
  184. }
  185. static int apple_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  186. {
  187. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  188. apple_gpio_set_reg(pctl, offset,
  189. REG_GPIOx_PERIPH | REG_GPIOx_MODE | REG_GPIOx_DATA |
  190. REG_GPIOx_INPUT_ENABLE,
  191. FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_IN_IRQ_OFF) |
  192. REG_GPIOx_INPUT_ENABLE);
  193. return 0;
  194. }
  195. static int apple_gpio_direction_output(struct gpio_chip *chip,
  196. unsigned int offset, int value)
  197. {
  198. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  199. apple_gpio_set_reg(pctl, offset,
  200. REG_GPIOx_PERIPH | REG_GPIOx_MODE | REG_GPIOx_DATA,
  201. FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_OUT) |
  202. (value ? REG_GPIOx_DATA : 0));
  203. return 0;
  204. }
  205. /* IRQ chip functions */
  206. static void apple_gpio_irq_ack(struct irq_data *data)
  207. {
  208. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(irq_data_get_irq_chip_data(data));
  209. unsigned int irqgrp = FIELD_GET(REG_GPIOx_GRP, apple_gpio_get_reg(pctl, data->hwirq));
  210. writel(BIT(data->hwirq % 32), pctl->base + REG_IRQ(irqgrp, data->hwirq));
  211. }
  212. static unsigned int apple_gpio_irq_type(unsigned int type)
  213. {
  214. switch (type & IRQ_TYPE_SENSE_MASK) {
  215. case IRQ_TYPE_EDGE_RISING:
  216. return REG_GPIOx_IN_IRQ_UP;
  217. case IRQ_TYPE_EDGE_FALLING:
  218. return REG_GPIOx_IN_IRQ_DN;
  219. case IRQ_TYPE_EDGE_BOTH:
  220. return REG_GPIOx_IN_IRQ_ANY;
  221. case IRQ_TYPE_LEVEL_HIGH:
  222. return REG_GPIOx_IN_IRQ_HI;
  223. case IRQ_TYPE_LEVEL_LOW:
  224. return REG_GPIOx_IN_IRQ_LO;
  225. default:
  226. return REG_GPIOx_IN_IRQ_OFF;
  227. }
  228. }
  229. static void apple_gpio_irq_mask(struct irq_data *data)
  230. {
  231. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  232. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(gc);
  233. apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE,
  234. FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_IN_IRQ_OFF));
  235. gpiochip_disable_irq(gc, data->hwirq);
  236. }
  237. static void apple_gpio_irq_unmask(struct irq_data *data)
  238. {
  239. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  240. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(gc);
  241. unsigned int irqtype = apple_gpio_irq_type(irqd_get_trigger_type(data));
  242. gpiochip_enable_irq(gc, data->hwirq);
  243. apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE,
  244. FIELD_PREP(REG_GPIOx_MODE, irqtype));
  245. }
  246. static unsigned int apple_gpio_irq_startup(struct irq_data *data)
  247. {
  248. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  249. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  250. apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_GRP,
  251. FIELD_PREP(REG_GPIOx_GRP, 0));
  252. apple_gpio_direction_input(chip, data->hwirq);
  253. apple_gpio_irq_unmask(data);
  254. return 0;
  255. }
  256. static int apple_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  257. {
  258. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(irq_data_get_irq_chip_data(data));
  259. unsigned int irqtype = apple_gpio_irq_type(type);
  260. if (irqtype == REG_GPIOx_IN_IRQ_OFF)
  261. return -EINVAL;
  262. apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE,
  263. FIELD_PREP(REG_GPIOx_MODE, irqtype));
  264. if (type & IRQ_TYPE_LEVEL_MASK)
  265. irq_set_handler_locked(data, handle_level_irq);
  266. else
  267. irq_set_handler_locked(data, handle_edge_irq);
  268. return 0;
  269. }
  270. static void apple_gpio_irq_handler(struct irq_desc *desc)
  271. {
  272. struct irq_chip *chip = irq_desc_get_chip(desc);
  273. u8 *grpp = irq_desc_get_handler_data(desc);
  274. struct apple_gpio_pinctrl *pctl;
  275. unsigned int pinh, pinl;
  276. unsigned long pending;
  277. struct gpio_chip *gc;
  278. pctl = container_of(grpp - *grpp, typeof(*pctl), irqgrps[0]);
  279. gc = &pctl->gpio_chip;
  280. chained_irq_enter(chip, desc);
  281. for (pinh = 0; pinh < gc->ngpio; pinh += 32) {
  282. pending = readl_relaxed(pctl->base + REG_IRQ(*grpp, pinh));
  283. for_each_set_bit(pinl, &pending, 32)
  284. generic_handle_domain_irq(gc->irq.domain, pinh + pinl);
  285. }
  286. chained_irq_exit(chip, desc);
  287. }
  288. static const struct irq_chip apple_gpio_irqchip = {
  289. .name = "Apple-GPIO",
  290. .irq_startup = apple_gpio_irq_startup,
  291. .irq_ack = apple_gpio_irq_ack,
  292. .irq_mask = apple_gpio_irq_mask,
  293. .irq_unmask = apple_gpio_irq_unmask,
  294. .irq_set_type = apple_gpio_irq_set_type,
  295. .flags = IRQCHIP_IMMUTABLE,
  296. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  297. };
  298. /* Probe & register */
  299. static int apple_gpio_register(struct apple_gpio_pinctrl *pctl)
  300. {
  301. struct gpio_irq_chip *girq = &pctl->gpio_chip.irq;
  302. void **irq_data = NULL;
  303. int ret;
  304. pctl->gpio_chip.label = dev_name(pctl->dev);
  305. pctl->gpio_chip.request = gpiochip_generic_request;
  306. pctl->gpio_chip.free = gpiochip_generic_free;
  307. pctl->gpio_chip.get_direction = apple_gpio_get_direction;
  308. pctl->gpio_chip.direction_input = apple_gpio_direction_input;
  309. pctl->gpio_chip.direction_output = apple_gpio_direction_output;
  310. pctl->gpio_chip.get = apple_gpio_get;
  311. pctl->gpio_chip.set = apple_gpio_set;
  312. pctl->gpio_chip.base = -1;
  313. pctl->gpio_chip.ngpio = pctl->pinctrl_desc.npins;
  314. pctl->gpio_chip.parent = pctl->dev;
  315. if (girq->num_parents) {
  316. int i;
  317. gpio_irq_chip_set_chip(girq, &apple_gpio_irqchip);
  318. girq->parent_handler = apple_gpio_irq_handler;
  319. girq->parents = kmalloc_array(girq->num_parents,
  320. sizeof(*girq->parents),
  321. GFP_KERNEL);
  322. irq_data = kmalloc_array(girq->num_parents, sizeof(*irq_data),
  323. GFP_KERNEL);
  324. if (!girq->parents || !irq_data) {
  325. ret = -ENOMEM;
  326. goto out_free_irq_data;
  327. }
  328. for (i = 0; i < girq->num_parents; i++) {
  329. ret = platform_get_irq(to_platform_device(pctl->dev), i);
  330. if (ret < 0)
  331. goto out_free_irq_data;
  332. girq->parents[i] = ret;
  333. pctl->irqgrps[i] = i;
  334. irq_data[i] = &pctl->irqgrps[i];
  335. }
  336. girq->parent_handler_data_array = irq_data;
  337. girq->per_parent_data = true;
  338. girq->default_type = IRQ_TYPE_NONE;
  339. girq->handler = handle_level_irq;
  340. }
  341. ret = devm_gpiochip_add_data(pctl->dev, &pctl->gpio_chip, pctl);
  342. out_free_irq_data:
  343. kfree(girq->parents);
  344. kfree(irq_data);
  345. return ret;
  346. }
  347. static int apple_gpio_pinctrl_probe(struct platform_device *pdev)
  348. {
  349. struct apple_gpio_pinctrl *pctl;
  350. struct pinctrl_pin_desc *pins;
  351. unsigned int npins;
  352. const char **pin_names;
  353. unsigned int *pin_nums;
  354. static const char* pinmux_functions[] = {
  355. "gpio", "periph1", "periph2", "periph3"
  356. };
  357. unsigned int i, nirqs = 0;
  358. int res;
  359. if (of_property_read_bool(pdev->dev.of_node, "interrupt-controller")) {
  360. res = platform_irq_count(pdev);
  361. if (res > 0)
  362. nirqs = res;
  363. }
  364. pctl = devm_kzalloc(&pdev->dev, struct_size(pctl, irqgrps, nirqs),
  365. GFP_KERNEL);
  366. if (!pctl)
  367. return -ENOMEM;
  368. pctl->dev = &pdev->dev;
  369. pctl->gpio_chip.irq.num_parents = nirqs;
  370. dev_set_drvdata(&pdev->dev, pctl);
  371. if (of_property_read_u32(pdev->dev.of_node, "apple,npins", &npins))
  372. return dev_err_probe(&pdev->dev, -EINVAL,
  373. "apple,npins property not found\n");
  374. pins = devm_kmalloc_array(&pdev->dev, npins, sizeof(pins[0]),
  375. GFP_KERNEL);
  376. pin_names = devm_kmalloc_array(&pdev->dev, npins, sizeof(pin_names[0]),
  377. GFP_KERNEL);
  378. pin_nums = devm_kmalloc_array(&pdev->dev, npins, sizeof(pin_nums[0]),
  379. GFP_KERNEL);
  380. if (!pins || !pin_names || !pin_nums)
  381. return -ENOMEM;
  382. pctl->base = devm_platform_ioremap_resource(pdev, 0);
  383. if (IS_ERR(pctl->base))
  384. return PTR_ERR(pctl->base);
  385. pctl->map = devm_regmap_init_mmio(&pdev->dev, pctl->base, &regmap_config);
  386. if (IS_ERR(pctl->map))
  387. return dev_err_probe(&pdev->dev, PTR_ERR(pctl->map),
  388. "Failed to create regmap\n");
  389. for (i = 0; i < npins; i++) {
  390. pins[i].number = i;
  391. pins[i].name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "PIN%u", i);
  392. pins[i].drv_data = pctl;
  393. pin_names[i] = pins[i].name;
  394. pin_nums[i] = i;
  395. }
  396. pctl->pinctrl_desc.name = dev_name(pctl->dev);
  397. pctl->pinctrl_desc.pins = pins;
  398. pctl->pinctrl_desc.npins = npins;
  399. pctl->pinctrl_desc.pctlops = &apple_gpio_pinctrl_ops;
  400. pctl->pinctrl_desc.pmxops = &apple_gpio_pinmux_ops;
  401. pctl->pctldev = devm_pinctrl_register(&pdev->dev, &pctl->pinctrl_desc, pctl);
  402. if (IS_ERR(pctl->pctldev))
  403. return dev_err_probe(&pdev->dev, PTR_ERR(pctl->pctldev),
  404. "Failed to register pinctrl device.\n");
  405. for (i = 0; i < npins; i++) {
  406. res = pinctrl_generic_add_group(pctl->pctldev, pins[i].name,
  407. pin_nums + i, 1, pctl);
  408. if (res < 0)
  409. return dev_err_probe(pctl->dev, res,
  410. "Failed to register group");
  411. }
  412. for (i = 0; i < ARRAY_SIZE(pinmux_functions); ++i) {
  413. res = pinmux_generic_add_function(pctl->pctldev, pinmux_functions[i],
  414. pin_names, npins, pctl);
  415. if (res < 0)
  416. return dev_err_probe(pctl->dev, res,
  417. "Failed to register function.");
  418. }
  419. return apple_gpio_register(pctl);
  420. }
  421. static const struct of_device_id apple_gpio_pinctrl_of_match[] = {
  422. { .compatible = "apple,pinctrl", },
  423. { }
  424. };
  425. MODULE_DEVICE_TABLE(of, apple_gpio_pinctrl_of_match);
  426. static struct platform_driver apple_gpio_pinctrl_driver = {
  427. .driver = {
  428. .name = "apple-gpio-pinctrl",
  429. .of_match_table = apple_gpio_pinctrl_of_match,
  430. .suppress_bind_attrs = true,
  431. },
  432. .probe = apple_gpio_pinctrl_probe,
  433. };
  434. module_platform_driver(apple_gpio_pinctrl_driver);
  435. MODULE_DESCRIPTION("Apple pinctrl/GPIO driver");
  436. MODULE_AUTHOR("Stan Skowronek <[email protected]>");
  437. MODULE_AUTHOR("Joey Gouly <[email protected]>");
  438. MODULE_LICENSE("GPL v2");