pinctrl-apple-gpio.c 15 KB

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