pinctrl-rza2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Combined GPIO and pin controller support for Renesas RZ/A2 (R7S9210) SoC
  4. *
  5. * Copyright (C) 2018 Chris Brandt
  6. */
  7. /*
  8. * This pin controller/gpio combined driver supports Renesas devices of RZ/A2
  9. * family.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pinctrl/pinmux.h>
  18. #include "../core.h"
  19. #include "../pinmux.h"
  20. #define DRIVER_NAME "pinctrl-rza2"
  21. #define RZA2_PINS_PER_PORT 8
  22. #define RZA2_PIN_ID_TO_PORT(id) ((id) / RZA2_PINS_PER_PORT)
  23. #define RZA2_PIN_ID_TO_PIN(id) ((id) % RZA2_PINS_PER_PORT)
  24. /*
  25. * Use 16 lower bits [15:0] for pin identifier
  26. * Use 16 higher bits [31:16] for pin mux function
  27. */
  28. #define MUX_PIN_ID_MASK GENMASK(15, 0)
  29. #define MUX_FUNC_MASK GENMASK(31, 16)
  30. #define MUX_FUNC_OFFS 16
  31. #define MUX_FUNC(pinconf) ((pinconf & MUX_FUNC_MASK) >> MUX_FUNC_OFFS)
  32. static const char port_names[] = "0123456789ABCDEFGHJKLM";
  33. struct rza2_pinctrl_priv {
  34. struct device *dev;
  35. void __iomem *base;
  36. struct pinctrl_pin_desc *pins;
  37. struct pinctrl_desc desc;
  38. struct pinctrl_dev *pctl;
  39. struct pinctrl_gpio_range gpio_range;
  40. int npins;
  41. struct mutex mutex; /* serialize adding groups and functions */
  42. };
  43. #define RZA2_PDR(port) (0x0000 + (port) * 2) /* Direction 16-bit */
  44. #define RZA2_PODR(port) (0x0040 + (port)) /* Output Data 8-bit */
  45. #define RZA2_PIDR(port) (0x0060 + (port)) /* Input Data 8-bit */
  46. #define RZA2_PMR(port) (0x0080 + (port)) /* Mode 8-bit */
  47. #define RZA2_DSCR(port) (0x0140 + (port) * 2) /* Drive 16-bit */
  48. #define RZA2_PFS(port, pin) (0x0200 + ((port) * 8) + (pin)) /* Fnct 8-bit */
  49. #define RZA2_PWPR 0x02ff /* Write Protect 8-bit */
  50. #define RZA2_PFENET 0x0820 /* Ethernet Pins 8-bit */
  51. #define RZA2_PPOC 0x0900 /* Dedicated Pins 32-bit */
  52. #define RZA2_PHMOMO 0x0980 /* Peripheral Pins 32-bit */
  53. #define RZA2_PCKIO 0x09d0 /* CKIO Drive 8-bit */
  54. #define RZA2_PDR_INPUT 0x02
  55. #define RZA2_PDR_OUTPUT 0x03
  56. #define RZA2_PDR_MASK 0x03
  57. #define PWPR_B0WI BIT(7) /* Bit Write Disable */
  58. #define PWPR_PFSWE BIT(6) /* PFS Register Write Enable */
  59. #define PFS_ISEL BIT(6) /* Interrupt Select */
  60. static void rza2_set_pin_function(void __iomem *pfc_base, u8 port, u8 pin,
  61. u8 func)
  62. {
  63. u16 mask16;
  64. u16 reg16;
  65. u8 reg8;
  66. /* Set pin to 'Non-use (Hi-z input protection)' */
  67. reg16 = readw(pfc_base + RZA2_PDR(port));
  68. mask16 = RZA2_PDR_MASK << (pin * 2);
  69. reg16 &= ~mask16;
  70. writew(reg16, pfc_base + RZA2_PDR(port));
  71. /* Temporarily switch to GPIO */
  72. reg8 = readb(pfc_base + RZA2_PMR(port));
  73. reg8 &= ~BIT(pin);
  74. writeb(reg8, pfc_base + RZA2_PMR(port));
  75. /* PFS Register Write Protect : OFF */
  76. writeb(0x00, pfc_base + RZA2_PWPR); /* B0WI=0, PFSWE=0 */
  77. writeb(PWPR_PFSWE, pfc_base + RZA2_PWPR); /* B0WI=0, PFSWE=1 */
  78. /* Set Pin function (interrupt disabled, ISEL=0) */
  79. writeb(func, pfc_base + RZA2_PFS(port, pin));
  80. /* PFS Register Write Protect : ON */
  81. writeb(0x00, pfc_base + RZA2_PWPR); /* B0WI=0, PFSWE=0 */
  82. writeb(0x80, pfc_base + RZA2_PWPR); /* B0WI=1, PFSWE=0 */
  83. /* Port Mode : Peripheral module pin functions */
  84. reg8 = readb(pfc_base + RZA2_PMR(port));
  85. reg8 |= BIT(pin);
  86. writeb(reg8, pfc_base + RZA2_PMR(port));
  87. }
  88. static void rza2_pin_to_gpio(void __iomem *pfc_base, unsigned int offset,
  89. u8 dir)
  90. {
  91. u8 port = RZA2_PIN_ID_TO_PORT(offset);
  92. u8 pin = RZA2_PIN_ID_TO_PIN(offset);
  93. u16 mask16;
  94. u16 reg16;
  95. reg16 = readw(pfc_base + RZA2_PDR(port));
  96. mask16 = RZA2_PDR_MASK << (pin * 2);
  97. reg16 &= ~mask16;
  98. if (dir)
  99. reg16 |= RZA2_PDR_INPUT << (pin * 2); /* pin as input */
  100. else
  101. reg16 |= RZA2_PDR_OUTPUT << (pin * 2); /* pin as output */
  102. writew(reg16, pfc_base + RZA2_PDR(port));
  103. }
  104. static int rza2_chip_get_direction(struct gpio_chip *chip, unsigned int offset)
  105. {
  106. struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
  107. u8 port = RZA2_PIN_ID_TO_PORT(offset);
  108. u8 pin = RZA2_PIN_ID_TO_PIN(offset);
  109. u16 reg16;
  110. reg16 = readw(priv->base + RZA2_PDR(port));
  111. reg16 = (reg16 >> (pin * 2)) & RZA2_PDR_MASK;
  112. if (reg16 == RZA2_PDR_OUTPUT)
  113. return GPIO_LINE_DIRECTION_OUT;
  114. if (reg16 == RZA2_PDR_INPUT)
  115. return GPIO_LINE_DIRECTION_IN;
  116. /*
  117. * This GPIO controller has a default Hi-Z state that is not input or
  118. * output, so force the pin to input now.
  119. */
  120. rza2_pin_to_gpio(priv->base, offset, 1);
  121. return GPIO_LINE_DIRECTION_IN;
  122. }
  123. static int rza2_chip_direction_input(struct gpio_chip *chip,
  124. unsigned int offset)
  125. {
  126. struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
  127. rza2_pin_to_gpio(priv->base, offset, 1);
  128. return 0;
  129. }
  130. static int rza2_chip_get(struct gpio_chip *chip, unsigned int offset)
  131. {
  132. struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
  133. u8 port = RZA2_PIN_ID_TO_PORT(offset);
  134. u8 pin = RZA2_PIN_ID_TO_PIN(offset);
  135. return !!(readb(priv->base + RZA2_PIDR(port)) & BIT(pin));
  136. }
  137. static void rza2_chip_set(struct gpio_chip *chip, unsigned int offset,
  138. int value)
  139. {
  140. struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
  141. u8 port = RZA2_PIN_ID_TO_PORT(offset);
  142. u8 pin = RZA2_PIN_ID_TO_PIN(offset);
  143. u8 new_value;
  144. new_value = readb(priv->base + RZA2_PODR(port));
  145. if (value)
  146. new_value |= BIT(pin);
  147. else
  148. new_value &= ~BIT(pin);
  149. writeb(new_value, priv->base + RZA2_PODR(port));
  150. }
  151. static int rza2_chip_direction_output(struct gpio_chip *chip,
  152. unsigned int offset, int val)
  153. {
  154. struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
  155. rza2_chip_set(chip, offset, val);
  156. rza2_pin_to_gpio(priv->base, offset, 0);
  157. return 0;
  158. }
  159. static const char * const rza2_gpio_names[] = {
  160. "P0_0", "P0_1", "P0_2", "P0_3", "P0_4", "P0_5", "P0_6", "P0_7",
  161. "P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7",
  162. "P2_0", "P2_1", "P2_2", "P2_3", "P2_4", "P2_5", "P2_6", "P2_7",
  163. "P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7",
  164. "P4_0", "P4_1", "P4_2", "P4_3", "P4_4", "P4_5", "P4_6", "P4_7",
  165. "P5_0", "P5_1", "P5_2", "P5_3", "P5_4", "P5_5", "P5_6", "P5_7",
  166. "P6_0", "P6_1", "P6_2", "P6_3", "P6_4", "P6_5", "P6_6", "P6_7",
  167. "P7_0", "P7_1", "P7_2", "P7_3", "P7_4", "P7_5", "P7_6", "P7_7",
  168. "P8_0", "P8_1", "P8_2", "P8_3", "P8_4", "P8_5", "P8_6", "P8_7",
  169. "P9_0", "P9_1", "P9_2", "P9_3", "P9_4", "P9_5", "P9_6", "P9_7",
  170. "PA_0", "PA_1", "PA_2", "PA_3", "PA_4", "PA_5", "PA_6", "PA_7",
  171. "PB_0", "PB_1", "PB_2", "PB_3", "PB_4", "PB_5", "PB_6", "PB_7",
  172. "PC_0", "PC_1", "PC_2", "PC_3", "PC_4", "PC_5", "PC_6", "PC_7",
  173. "PD_0", "PD_1", "PD_2", "PD_3", "PD_4", "PD_5", "PD_6", "PD_7",
  174. "PE_0", "PE_1", "PE_2", "PE_3", "PE_4", "PE_5", "PE_6", "PE_7",
  175. "PF_0", "PF_1", "PF_2", "PF_3", "PF_4", "PF_5", "PF_6", "PF_7",
  176. "PG_0", "PG_1", "PG_2", "PG_3", "PG_4", "PG_5", "PG_6", "PG_7",
  177. "PH_0", "PH_1", "PH_2", "PH_3", "PH_4", "PH_5", "PH_6", "PH_7",
  178. /* port I does not exist */
  179. "PJ_0", "PJ_1", "PJ_2", "PJ_3", "PJ_4", "PJ_5", "PJ_6", "PJ_7",
  180. "PK_0", "PK_1", "PK_2", "PK_3", "PK_4", "PK_5", "PK_6", "PK_7",
  181. "PL_0", "PL_1", "PL_2", "PL_3", "PL_4", "PL_5", "PL_6", "PL_7",
  182. "PM_0", "PM_1", "PM_2", "PM_3", "PM_4", "PM_5", "PM_6", "PM_7",
  183. };
  184. static struct gpio_chip chip = {
  185. .names = rza2_gpio_names,
  186. .base = -1,
  187. .get_direction = rza2_chip_get_direction,
  188. .direction_input = rza2_chip_direction_input,
  189. .direction_output = rza2_chip_direction_output,
  190. .get = rza2_chip_get,
  191. .set = rza2_chip_set,
  192. };
  193. static int rza2_gpio_register(struct rza2_pinctrl_priv *priv)
  194. {
  195. struct device_node *np = priv->dev->of_node;
  196. struct of_phandle_args of_args;
  197. int ret;
  198. chip.label = devm_kasprintf(priv->dev, GFP_KERNEL, "%pOFn", np);
  199. chip.parent = priv->dev;
  200. chip.ngpio = priv->npins;
  201. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
  202. &of_args);
  203. if (ret) {
  204. dev_err(priv->dev, "Unable to parse gpio-ranges\n");
  205. return ret;
  206. }
  207. if ((of_args.args[0] != 0) ||
  208. (of_args.args[1] != 0) ||
  209. (of_args.args[2] != priv->npins)) {
  210. dev_err(priv->dev, "gpio-ranges does not match selected SOC\n");
  211. return -EINVAL;
  212. }
  213. priv->gpio_range.id = 0;
  214. priv->gpio_range.pin_base = priv->gpio_range.base = 0;
  215. priv->gpio_range.npins = priv->npins;
  216. priv->gpio_range.name = chip.label;
  217. priv->gpio_range.gc = &chip;
  218. /* Register our gpio chip with gpiolib */
  219. ret = devm_gpiochip_add_data(priv->dev, &chip, priv);
  220. if (ret)
  221. return ret;
  222. /* Register pin range with pinctrl core */
  223. pinctrl_add_gpio_range(priv->pctl, &priv->gpio_range);
  224. dev_dbg(priv->dev, "Registered gpio controller\n");
  225. return 0;
  226. }
  227. static int rza2_pinctrl_register(struct rza2_pinctrl_priv *priv)
  228. {
  229. struct pinctrl_pin_desc *pins;
  230. unsigned int i;
  231. int ret;
  232. pins = devm_kcalloc(priv->dev, priv->npins, sizeof(*pins), GFP_KERNEL);
  233. if (!pins)
  234. return -ENOMEM;
  235. priv->pins = pins;
  236. priv->desc.pins = pins;
  237. priv->desc.npins = priv->npins;
  238. for (i = 0; i < priv->npins; i++) {
  239. pins[i].number = i;
  240. pins[i].name = rza2_gpio_names[i];
  241. }
  242. ret = devm_pinctrl_register_and_init(priv->dev, &priv->desc, priv,
  243. &priv->pctl);
  244. if (ret) {
  245. dev_err(priv->dev, "pinctrl registration failed\n");
  246. return ret;
  247. }
  248. ret = pinctrl_enable(priv->pctl);
  249. if (ret) {
  250. dev_err(priv->dev, "pinctrl enable failed\n");
  251. return ret;
  252. }
  253. ret = rza2_gpio_register(priv);
  254. if (ret) {
  255. dev_err(priv->dev, "GPIO registration failed\n");
  256. return ret;
  257. }
  258. return 0;
  259. }
  260. /*
  261. * For each DT node, create a single pin mapping. That pin mapping will only
  262. * contain a single group of pins, and that group of pins will only have a
  263. * single function that can be selected.
  264. */
  265. static int rza2_dt_node_to_map(struct pinctrl_dev *pctldev,
  266. struct device_node *np,
  267. struct pinctrl_map **map,
  268. unsigned int *num_maps)
  269. {
  270. struct rza2_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  271. unsigned int *pins, *psel_val;
  272. int i, ret, npins, gsel, fsel;
  273. struct property *of_pins;
  274. const char **pin_fn;
  275. /* Find out how many pins to map */
  276. of_pins = of_find_property(np, "pinmux", NULL);
  277. if (!of_pins) {
  278. dev_info(priv->dev, "Missing pinmux property\n");
  279. return -ENOENT;
  280. }
  281. npins = of_pins->length / sizeof(u32);
  282. pins = devm_kcalloc(priv->dev, npins, sizeof(*pins), GFP_KERNEL);
  283. psel_val = devm_kcalloc(priv->dev, npins, sizeof(*psel_val),
  284. GFP_KERNEL);
  285. pin_fn = devm_kzalloc(priv->dev, sizeof(*pin_fn), GFP_KERNEL);
  286. if (!pins || !psel_val || !pin_fn)
  287. return -ENOMEM;
  288. /* Collect pin locations and mux settings from DT properties */
  289. for (i = 0; i < npins; ++i) {
  290. u32 value;
  291. ret = of_property_read_u32_index(np, "pinmux", i, &value);
  292. if (ret)
  293. return ret;
  294. pins[i] = value & MUX_PIN_ID_MASK;
  295. psel_val[i] = MUX_FUNC(value);
  296. }
  297. mutex_lock(&priv->mutex);
  298. /* Register a single pin group listing all the pins we read from DT */
  299. gsel = pinctrl_generic_add_group(pctldev, np->name, pins, npins, NULL);
  300. if (gsel < 0) {
  301. ret = gsel;
  302. goto unlock;
  303. }
  304. /*
  305. * Register a single group function where the 'data' is an array PSEL
  306. * register values read from DT.
  307. */
  308. pin_fn[0] = np->name;
  309. fsel = pinmux_generic_add_function(pctldev, np->name, pin_fn, 1,
  310. psel_val);
  311. if (fsel < 0) {
  312. ret = fsel;
  313. goto remove_group;
  314. }
  315. dev_dbg(priv->dev, "Parsed %pOF with %d pins\n", np, npins);
  316. /* Create map where to retrieve function and mux settings from */
  317. *num_maps = 0;
  318. *map = kzalloc(sizeof(**map), GFP_KERNEL);
  319. if (!*map) {
  320. ret = -ENOMEM;
  321. goto remove_function;
  322. }
  323. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  324. (*map)->data.mux.group = np->name;
  325. (*map)->data.mux.function = np->name;
  326. *num_maps = 1;
  327. mutex_unlock(&priv->mutex);
  328. return 0;
  329. remove_function:
  330. pinmux_generic_remove_function(pctldev, fsel);
  331. remove_group:
  332. pinctrl_generic_remove_group(pctldev, gsel);
  333. unlock:
  334. mutex_unlock(&priv->mutex);
  335. dev_err(priv->dev, "Unable to parse DT node %s\n", np->name);
  336. return ret;
  337. }
  338. static void rza2_dt_free_map(struct pinctrl_dev *pctldev,
  339. struct pinctrl_map *map, unsigned int num_maps)
  340. {
  341. kfree(map);
  342. }
  343. static const struct pinctrl_ops rza2_pinctrl_ops = {
  344. .get_groups_count = pinctrl_generic_get_group_count,
  345. .get_group_name = pinctrl_generic_get_group_name,
  346. .get_group_pins = pinctrl_generic_get_group_pins,
  347. .dt_node_to_map = rza2_dt_node_to_map,
  348. .dt_free_map = rza2_dt_free_map,
  349. };
  350. static int rza2_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
  351. unsigned int group)
  352. {
  353. struct rza2_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  354. struct function_desc *func;
  355. unsigned int i, *psel_val;
  356. struct group_desc *grp;
  357. grp = pinctrl_generic_get_group(pctldev, group);
  358. if (!grp)
  359. return -EINVAL;
  360. func = pinmux_generic_get_function(pctldev, selector);
  361. if (!func)
  362. return -EINVAL;
  363. psel_val = func->data;
  364. for (i = 0; i < grp->num_pins; ++i) {
  365. dev_dbg(priv->dev, "Setting P%c_%d to PSEL=%d\n",
  366. port_names[RZA2_PIN_ID_TO_PORT(grp->pins[i])],
  367. RZA2_PIN_ID_TO_PIN(grp->pins[i]),
  368. psel_val[i]);
  369. rza2_set_pin_function(
  370. priv->base,
  371. RZA2_PIN_ID_TO_PORT(grp->pins[i]),
  372. RZA2_PIN_ID_TO_PIN(grp->pins[i]),
  373. psel_val[i]);
  374. }
  375. return 0;
  376. }
  377. static const struct pinmux_ops rza2_pinmux_ops = {
  378. .get_functions_count = pinmux_generic_get_function_count,
  379. .get_function_name = pinmux_generic_get_function_name,
  380. .get_function_groups = pinmux_generic_get_function_groups,
  381. .set_mux = rza2_set_mux,
  382. .strict = true,
  383. };
  384. static int rza2_pinctrl_probe(struct platform_device *pdev)
  385. {
  386. struct rza2_pinctrl_priv *priv;
  387. int ret;
  388. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  389. if (!priv)
  390. return -ENOMEM;
  391. priv->dev = &pdev->dev;
  392. priv->base = devm_platform_ioremap_resource(pdev, 0);
  393. if (IS_ERR(priv->base))
  394. return PTR_ERR(priv->base);
  395. mutex_init(&priv->mutex);
  396. platform_set_drvdata(pdev, priv);
  397. priv->npins = (int)(uintptr_t)of_device_get_match_data(&pdev->dev) *
  398. RZA2_PINS_PER_PORT;
  399. priv->desc.name = DRIVER_NAME;
  400. priv->desc.pctlops = &rza2_pinctrl_ops;
  401. priv->desc.pmxops = &rza2_pinmux_ops;
  402. priv->desc.owner = THIS_MODULE;
  403. ret = rza2_pinctrl_register(priv);
  404. if (ret)
  405. return ret;
  406. dev_info(&pdev->dev, "Registered ports P0 - P%c\n",
  407. port_names[priv->desc.npins / RZA2_PINS_PER_PORT - 1]);
  408. return 0;
  409. }
  410. static const struct of_device_id rza2_pinctrl_of_match[] = {
  411. { .compatible = "renesas,r7s9210-pinctrl", .data = (void *)22, },
  412. { /* sentinel */ }
  413. };
  414. static struct platform_driver rza2_pinctrl_driver = {
  415. .driver = {
  416. .name = DRIVER_NAME,
  417. .of_match_table = rza2_pinctrl_of_match,
  418. },
  419. .probe = rza2_pinctrl_probe,
  420. };
  421. static int __init rza2_pinctrl_init(void)
  422. {
  423. return platform_driver_register(&rza2_pinctrl_driver);
  424. }
  425. core_initcall(rza2_pinctrl_init);
  426. MODULE_AUTHOR("Chris Brandt <[email protected]>");
  427. MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/A2 SoC");
  428. MODULE_LICENSE("GPL v2");