pinctrl-apple-gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. static const 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 int *num_maps)
  91. {
  92. unsigned int 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 bool apple_gpio_pinmux_func_is_gpio(struct pinctrl_dev *pctldev,
  145. unsigned int selector)
  146. {
  147. /* Function selector 0 is always the GPIO mode */
  148. return (selector == 0);
  149. }
  150. static int apple_gpio_pinmux_set(struct pinctrl_dev *pctldev, unsigned int func,
  151. unsigned int group)
  152. {
  153. struct apple_gpio_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  154. apple_gpio_set_reg(
  155. pctl, group, REG_GPIOx_PERIPH | REG_GPIOx_INPUT_ENABLE,
  156. FIELD_PREP(REG_GPIOx_PERIPH, func) | REG_GPIOx_INPUT_ENABLE);
  157. return 0;
  158. }
  159. static const struct pinmux_ops apple_gpio_pinmux_ops = {
  160. .get_functions_count = pinmux_generic_get_function_count,
  161. .get_function_name = pinmux_generic_get_function_name,
  162. .get_function_groups = pinmux_generic_get_function_groups,
  163. .function_is_gpio = apple_gpio_pinmux_func_is_gpio,
  164. .set_mux = apple_gpio_pinmux_set,
  165. .strict = true,
  166. };
  167. /* GPIO chip functions */
  168. static int apple_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  169. {
  170. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  171. unsigned int reg = apple_gpio_get_reg(pctl, offset);
  172. if (FIELD_GET(REG_GPIOx_MODE, reg) == REG_GPIOx_OUT)
  173. return GPIO_LINE_DIRECTION_OUT;
  174. return GPIO_LINE_DIRECTION_IN;
  175. }
  176. static int apple_gpio_get(struct gpio_chip *chip, unsigned int offset)
  177. {
  178. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  179. unsigned int reg = apple_gpio_get_reg(pctl, offset);
  180. /*
  181. * If this is an input GPIO, read the actual value (not the
  182. * cached regmap value)
  183. */
  184. if (FIELD_GET(REG_GPIOx_MODE, reg) != REG_GPIOx_OUT)
  185. reg = readl_relaxed(pctl->base + REG_GPIO(offset));
  186. return !!(reg & REG_GPIOx_DATA);
  187. }
  188. static int apple_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
  189. {
  190. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  191. apple_gpio_set_reg(pctl, offset, REG_GPIOx_DATA, value ? REG_GPIOx_DATA : 0);
  192. return 0;
  193. }
  194. static int apple_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  195. {
  196. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  197. apple_gpio_set_reg(pctl, offset,
  198. REG_GPIOx_PERIPH | REG_GPIOx_MODE | REG_GPIOx_DATA |
  199. REG_GPIOx_INPUT_ENABLE,
  200. FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_IN_IRQ_OFF) |
  201. REG_GPIOx_INPUT_ENABLE);
  202. return 0;
  203. }
  204. static int apple_gpio_direction_output(struct gpio_chip *chip,
  205. unsigned int offset, int value)
  206. {
  207. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  208. apple_gpio_set_reg(pctl, offset,
  209. REG_GPIOx_PERIPH | REG_GPIOx_MODE | REG_GPIOx_DATA,
  210. FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_OUT) |
  211. (value ? REG_GPIOx_DATA : 0));
  212. return 0;
  213. }
  214. /* IRQ chip functions */
  215. static void apple_gpio_irq_ack(struct irq_data *data)
  216. {
  217. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(irq_data_get_irq_chip_data(data));
  218. unsigned int irqgrp = FIELD_GET(REG_GPIOx_GRP, apple_gpio_get_reg(pctl, data->hwirq));
  219. writel(BIT(data->hwirq % 32), pctl->base + REG_IRQ(irqgrp, data->hwirq));
  220. }
  221. static unsigned int apple_gpio_irq_type(unsigned int type)
  222. {
  223. switch (type & IRQ_TYPE_SENSE_MASK) {
  224. case IRQ_TYPE_EDGE_RISING:
  225. return REG_GPIOx_IN_IRQ_UP;
  226. case IRQ_TYPE_EDGE_FALLING:
  227. return REG_GPIOx_IN_IRQ_DN;
  228. case IRQ_TYPE_EDGE_BOTH:
  229. return REG_GPIOx_IN_IRQ_ANY;
  230. case IRQ_TYPE_LEVEL_HIGH:
  231. return REG_GPIOx_IN_IRQ_HI;
  232. case IRQ_TYPE_LEVEL_LOW:
  233. return REG_GPIOx_IN_IRQ_LO;
  234. default:
  235. return REG_GPIOx_IN_IRQ_OFF;
  236. }
  237. }
  238. static void apple_gpio_irq_mask(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. apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE,
  243. FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_IN_IRQ_OFF));
  244. gpiochip_disable_irq(gc, data->hwirq);
  245. }
  246. static void apple_gpio_irq_unmask(struct irq_data *data)
  247. {
  248. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  249. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(gc);
  250. unsigned int irqtype = apple_gpio_irq_type(irqd_get_trigger_type(data));
  251. gpiochip_enable_irq(gc, data->hwirq);
  252. apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE,
  253. FIELD_PREP(REG_GPIOx_MODE, irqtype));
  254. }
  255. static unsigned int apple_gpio_irq_startup(struct irq_data *data)
  256. {
  257. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  258. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip);
  259. apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_GRP,
  260. FIELD_PREP(REG_GPIOx_GRP, 0));
  261. apple_gpio_direction_input(chip, data->hwirq);
  262. apple_gpio_irq_unmask(data);
  263. return 0;
  264. }
  265. static int apple_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  266. {
  267. struct apple_gpio_pinctrl *pctl = gpiochip_get_data(irq_data_get_irq_chip_data(data));
  268. unsigned int irqtype = apple_gpio_irq_type(type);
  269. if (irqtype == REG_GPIOx_IN_IRQ_OFF)
  270. return -EINVAL;
  271. apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE,
  272. FIELD_PREP(REG_GPIOx_MODE, irqtype));
  273. if (type & IRQ_TYPE_LEVEL_MASK)
  274. irq_set_handler_locked(data, handle_level_irq);
  275. else
  276. irq_set_handler_locked(data, handle_edge_irq);
  277. return 0;
  278. }
  279. static void apple_gpio_irq_handler(struct irq_desc *desc)
  280. {
  281. struct irq_chip *chip = irq_desc_get_chip(desc);
  282. u8 *grpp = irq_desc_get_handler_data(desc);
  283. struct apple_gpio_pinctrl *pctl;
  284. unsigned int pinh, pinl;
  285. unsigned long pending;
  286. struct gpio_chip *gc;
  287. pctl = container_of(grpp - *grpp, typeof(*pctl), irqgrps[0]);
  288. gc = &pctl->gpio_chip;
  289. chained_irq_enter(chip, desc);
  290. for (pinh = 0; pinh < gc->ngpio; pinh += 32) {
  291. pending = readl_relaxed(pctl->base + REG_IRQ(*grpp, pinh));
  292. for_each_set_bit(pinl, &pending, 32)
  293. generic_handle_domain_irq(gc->irq.domain, pinh + pinl);
  294. }
  295. chained_irq_exit(chip, desc);
  296. }
  297. static const struct irq_chip apple_gpio_irqchip = {
  298. .name = "Apple-GPIO",
  299. .irq_startup = apple_gpio_irq_startup,
  300. .irq_ack = apple_gpio_irq_ack,
  301. .irq_mask = apple_gpio_irq_mask,
  302. .irq_unmask = apple_gpio_irq_unmask,
  303. .irq_set_type = apple_gpio_irq_set_type,
  304. .flags = IRQCHIP_IMMUTABLE,
  305. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  306. };
  307. /* Probe & register */
  308. static int apple_gpio_register(struct apple_gpio_pinctrl *pctl)
  309. {
  310. struct gpio_irq_chip *girq = &pctl->gpio_chip.irq;
  311. void **irq_data = NULL;
  312. int ret;
  313. pctl->gpio_chip.label = dev_name(pctl->dev);
  314. pctl->gpio_chip.request = gpiochip_generic_request;
  315. pctl->gpio_chip.free = gpiochip_generic_free;
  316. pctl->gpio_chip.get_direction = apple_gpio_get_direction;
  317. pctl->gpio_chip.direction_input = apple_gpio_direction_input;
  318. pctl->gpio_chip.direction_output = apple_gpio_direction_output;
  319. pctl->gpio_chip.get = apple_gpio_get;
  320. pctl->gpio_chip.set = apple_gpio_set;
  321. pctl->gpio_chip.base = -1;
  322. pctl->gpio_chip.ngpio = pctl->pinctrl_desc.npins;
  323. pctl->gpio_chip.parent = pctl->dev;
  324. if (girq->num_parents) {
  325. int i;
  326. gpio_irq_chip_set_chip(girq, &apple_gpio_irqchip);
  327. girq->parent_handler = apple_gpio_irq_handler;
  328. girq->parents = kmalloc_array(girq->num_parents,
  329. sizeof(*girq->parents),
  330. GFP_KERNEL);
  331. irq_data = kmalloc_objs(*irq_data, girq->num_parents);
  332. if (!girq->parents || !irq_data) {
  333. ret = -ENOMEM;
  334. goto out_free_irq_data;
  335. }
  336. for (i = 0; i < girq->num_parents; i++) {
  337. ret = platform_get_irq(to_platform_device(pctl->dev), i);
  338. if (ret < 0)
  339. goto out_free_irq_data;
  340. girq->parents[i] = ret;
  341. pctl->irqgrps[i] = i;
  342. irq_data[i] = &pctl->irqgrps[i];
  343. }
  344. girq->parent_handler_data_array = irq_data;
  345. girq->per_parent_data = true;
  346. girq->default_type = IRQ_TYPE_NONE;
  347. girq->handler = handle_level_irq;
  348. }
  349. ret = devm_gpiochip_add_data(pctl->dev, &pctl->gpio_chip, pctl);
  350. out_free_irq_data:
  351. kfree(girq->parents);
  352. kfree(irq_data);
  353. return ret;
  354. }
  355. static int apple_gpio_pinctrl_probe(struct platform_device *pdev)
  356. {
  357. struct apple_gpio_pinctrl *pctl;
  358. struct pinctrl_pin_desc *pins;
  359. unsigned int npins;
  360. const char **pin_names;
  361. unsigned int *pin_nums;
  362. static const char *pinmux_functions[] = {
  363. "gpio", "periph1", "periph2", "periph3"
  364. };
  365. unsigned int i, nirqs = 0;
  366. int res;
  367. if (of_property_read_bool(pdev->dev.of_node, "interrupt-controller")) {
  368. res = platform_irq_count(pdev);
  369. if (res > 0)
  370. nirqs = res;
  371. }
  372. pctl = devm_kzalloc(&pdev->dev, struct_size(pctl, irqgrps, nirqs),
  373. GFP_KERNEL);
  374. if (!pctl)
  375. return -ENOMEM;
  376. pctl->dev = &pdev->dev;
  377. pctl->gpio_chip.irq.num_parents = nirqs;
  378. dev_set_drvdata(&pdev->dev, pctl);
  379. if (of_property_read_u32(pdev->dev.of_node, "apple,npins", &npins))
  380. return dev_err_probe(&pdev->dev, -EINVAL,
  381. "apple,npins property not found\n");
  382. pins = devm_kmalloc_array(&pdev->dev, npins, sizeof(pins[0]),
  383. GFP_KERNEL);
  384. pin_names = devm_kmalloc_array(&pdev->dev, npins, sizeof(pin_names[0]),
  385. GFP_KERNEL);
  386. pin_nums = devm_kmalloc_array(&pdev->dev, npins, sizeof(pin_nums[0]),
  387. GFP_KERNEL);
  388. if (!pins || !pin_names || !pin_nums)
  389. return -ENOMEM;
  390. pctl->base = devm_platform_ioremap_resource(pdev, 0);
  391. if (IS_ERR(pctl->base))
  392. return PTR_ERR(pctl->base);
  393. pctl->map = devm_regmap_init_mmio(&pdev->dev, pctl->base, &regmap_config);
  394. if (IS_ERR(pctl->map))
  395. return dev_err_probe(&pdev->dev, PTR_ERR(pctl->map),
  396. "Failed to create regmap\n");
  397. for (i = 0; i < npins; i++) {
  398. pins[i].number = i;
  399. pins[i].name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "PIN%u", i);
  400. if (!pins[i].name)
  401. return -ENOMEM;
  402. pins[i].drv_data = pctl;
  403. pin_names[i] = pins[i].name;
  404. pin_nums[i] = i;
  405. }
  406. pctl->pinctrl_desc.name = dev_name(pctl->dev);
  407. pctl->pinctrl_desc.pins = pins;
  408. pctl->pinctrl_desc.npins = npins;
  409. pctl->pinctrl_desc.pctlops = &apple_gpio_pinctrl_ops;
  410. pctl->pinctrl_desc.pmxops = &apple_gpio_pinmux_ops;
  411. pctl->pctldev = devm_pinctrl_register(&pdev->dev, &pctl->pinctrl_desc, pctl);
  412. if (IS_ERR(pctl->pctldev))
  413. return dev_err_probe(&pdev->dev, PTR_ERR(pctl->pctldev),
  414. "Failed to register pinctrl device.\n");
  415. for (i = 0; i < npins; i++) {
  416. res = pinctrl_generic_add_group(pctl->pctldev, pins[i].name,
  417. pin_nums + i, 1, pctl);
  418. if (res < 0)
  419. return dev_err_probe(pctl->dev, res,
  420. "Failed to register group");
  421. }
  422. for (i = 0; i < ARRAY_SIZE(pinmux_functions); ++i) {
  423. res = pinmux_generic_add_function(pctl->pctldev, pinmux_functions[i],
  424. pin_names, npins, pctl);
  425. if (res < 0)
  426. return dev_err_probe(pctl->dev, res,
  427. "Failed to register function.");
  428. }
  429. return apple_gpio_register(pctl);
  430. }
  431. static const struct of_device_id apple_gpio_pinctrl_of_match[] = {
  432. { .compatible = "apple,t8103-pinctrl", },
  433. { .compatible = "apple,pinctrl", },
  434. { }
  435. };
  436. MODULE_DEVICE_TABLE(of, apple_gpio_pinctrl_of_match);
  437. static struct platform_driver apple_gpio_pinctrl_driver = {
  438. .driver = {
  439. .name = "apple-gpio-pinctrl",
  440. .of_match_table = apple_gpio_pinctrl_of_match,
  441. .suppress_bind_attrs = true,
  442. },
  443. .probe = apple_gpio_pinctrl_probe,
  444. };
  445. module_platform_driver(apple_gpio_pinctrl_driver);
  446. MODULE_DESCRIPTION("Apple pinctrl/GPIO driver");
  447. MODULE_AUTHOR("Stan Skowronek <stan@corellium.com>");
  448. MODULE_AUTHOR("Joey Gouly <joey.gouly@arm.com>");
  449. MODULE_LICENSE("GPL v2");