pinctrl-rzg2l.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas RZ/G2L Pin Control and GPIO driver core
  4. *
  5. * Copyright (C) 2021 Renesas Electronics Corporation.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/io.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/pinctrl/pinconf-generic.h>
  17. #include <linux/pinctrl/pinconf.h>
  18. #include <linux/pinctrl/pinctrl.h>
  19. #include <linux/pinctrl/pinmux.h>
  20. #include <linux/spinlock.h>
  21. #include <dt-bindings/pinctrl/rzg2l-pinctrl.h>
  22. #include "../core.h"
  23. #include "../pinconf.h"
  24. #include "../pinmux.h"
  25. #define DRV_NAME "pinctrl-rzg2l"
  26. /*
  27. * Use 16 lower bits [15:0] for pin identifier
  28. * Use 16 higher bits [31:16] for pin mux function
  29. */
  30. #define MUX_PIN_ID_MASK GENMASK(15, 0)
  31. #define MUX_FUNC_MASK GENMASK(31, 16)
  32. #define MUX_FUNC_OFFS 16
  33. #define MUX_FUNC(pinconf) (((pinconf) & MUX_FUNC_MASK) >> MUX_FUNC_OFFS)
  34. /* PIN capabilities */
  35. #define PIN_CFG_IOLH_A BIT(0)
  36. #define PIN_CFG_IOLH_B BIT(1)
  37. #define PIN_CFG_SR BIT(2)
  38. #define PIN_CFG_IEN BIT(3)
  39. #define PIN_CFG_PUPD BIT(4)
  40. #define PIN_CFG_IO_VMC_SD0 BIT(5)
  41. #define PIN_CFG_IO_VMC_SD1 BIT(6)
  42. #define PIN_CFG_IO_VMC_QSPI BIT(7)
  43. #define PIN_CFG_IO_VMC_ETH0 BIT(8)
  44. #define PIN_CFG_IO_VMC_ETH1 BIT(9)
  45. #define PIN_CFG_FILONOFF BIT(10)
  46. #define PIN_CFG_FILNUM BIT(11)
  47. #define PIN_CFG_FILCLKSEL BIT(12)
  48. #define RZG2L_MPXED_PIN_FUNCS (PIN_CFG_IOLH_A | \
  49. PIN_CFG_SR | \
  50. PIN_CFG_PUPD | \
  51. PIN_CFG_FILONOFF | \
  52. PIN_CFG_FILNUM | \
  53. PIN_CFG_FILCLKSEL)
  54. #define RZG2L_MPXED_ETH_PIN_FUNCS(x) ((x) | \
  55. PIN_CFG_FILONOFF | \
  56. PIN_CFG_FILNUM | \
  57. PIN_CFG_FILCLKSEL)
  58. /*
  59. * n indicates number of pins in the port, a is the register index
  60. * and f is pin configuration capabilities supported.
  61. */
  62. #define RZG2L_GPIO_PORT_PACK(n, a, f) (((n) << 28) | ((a) << 20) | (f))
  63. #define RZG2L_GPIO_PORT_GET_PINCNT(x) (((x) & GENMASK(30, 28)) >> 28)
  64. #define RZG2L_GPIO_PORT_GET_INDEX(x) (((x) & GENMASK(26, 20)) >> 20)
  65. #define RZG2L_GPIO_PORT_GET_CFGS(x) ((x) & GENMASK(19, 0))
  66. /*
  67. * BIT(31) indicates dedicated pin, p is the register index while
  68. * referencing to SR/IEN/IOLH/FILxx registers, b is the register bits
  69. * (b * 8) and f is the pin configuration capabilities supported.
  70. */
  71. #define RZG2L_SINGLE_PIN BIT(31)
  72. #define RZG2L_SINGLE_PIN_PACK(p, b, f) (RZG2L_SINGLE_PIN | \
  73. ((p) << 24) | ((b) << 20) | (f))
  74. #define RZG2L_SINGLE_PIN_GET_PORT_OFFSET(x) (((x) & GENMASK(30, 24)) >> 24)
  75. #define RZG2L_SINGLE_PIN_GET_BIT(x) (((x) & GENMASK(22, 20)) >> 20)
  76. #define RZG2L_SINGLE_PIN_GET_CFGS(x) ((x) & GENMASK(19, 0))
  77. #define P(n) (0x0000 + 0x10 + (n))
  78. #define PM(n) (0x0100 + 0x20 + (n) * 2)
  79. #define PMC(n) (0x0200 + 0x10 + (n))
  80. #define PFC(n) (0x0400 + 0x40 + (n) * 4)
  81. #define PIN(n) (0x0800 + 0x10 + (n))
  82. #define IOLH(n) (0x1000 + (n) * 8)
  83. #define IEN(n) (0x1800 + (n) * 8)
  84. #define ISEL(n) (0x2c80 + (n) * 8)
  85. #define PWPR (0x3014)
  86. #define SD_CH(n) (0x3000 + (n) * 4)
  87. #define QSPI (0x3008)
  88. #define PVDD_1800 1 /* I/O domain voltage <= 1.8V */
  89. #define PVDD_3300 0 /* I/O domain voltage >= 3.3V */
  90. #define PWPR_B0WI BIT(7) /* Bit Write Disable */
  91. #define PWPR_PFCWE BIT(6) /* PFC Register Write Enable */
  92. #define PM_MASK 0x03
  93. #define PVDD_MASK 0x01
  94. #define PFC_MASK 0x07
  95. #define IEN_MASK 0x01
  96. #define IOLH_MASK 0x03
  97. #define PM_INPUT 0x1
  98. #define PM_OUTPUT 0x2
  99. #define RZG2L_PIN_ID_TO_PORT(id) ((id) / RZG2L_PINS_PER_PORT)
  100. #define RZG2L_PIN_ID_TO_PORT_OFFSET(id) (RZG2L_PIN_ID_TO_PORT(id) + 0x10)
  101. #define RZG2L_PIN_ID_TO_PIN(id) ((id) % RZG2L_PINS_PER_PORT)
  102. #define RZG2L_TINT_MAX_INTERRUPT 32
  103. #define RZG2L_TINT_IRQ_START_INDEX 9
  104. #define RZG2L_PACK_HWIRQ(t, i) (((t) << 16) | (i))
  105. struct rzg2l_dedicated_configs {
  106. const char *name;
  107. u32 config;
  108. };
  109. struct rzg2l_pinctrl_data {
  110. const char * const *port_pins;
  111. const u32 *port_pin_configs;
  112. unsigned int n_ports;
  113. struct rzg2l_dedicated_configs *dedicated_pins;
  114. unsigned int n_port_pins;
  115. unsigned int n_dedicated_pins;
  116. };
  117. struct rzg2l_pinctrl {
  118. struct pinctrl_dev *pctl;
  119. struct pinctrl_desc desc;
  120. struct pinctrl_pin_desc *pins;
  121. const struct rzg2l_pinctrl_data *data;
  122. void __iomem *base;
  123. struct device *dev;
  124. struct clk *clk;
  125. struct gpio_chip gpio_chip;
  126. struct pinctrl_gpio_range gpio_range;
  127. DECLARE_BITMAP(tint_slot, RZG2L_TINT_MAX_INTERRUPT);
  128. spinlock_t bitmap_lock; /* protect tint_slot bitmap */
  129. unsigned int hwirq[RZG2L_TINT_MAX_INTERRUPT];
  130. spinlock_t lock; /* lock read/write registers */
  131. struct mutex mutex; /* serialize adding groups and functions */
  132. };
  133. static const unsigned int iolh_groupa_mA[] = { 2, 4, 8, 12 };
  134. static const unsigned int iolh_groupb_oi[] = { 100, 66, 50, 33 };
  135. static void rzg2l_pinctrl_set_pfc_mode(struct rzg2l_pinctrl *pctrl,
  136. u8 port, u8 pin, u8 func)
  137. {
  138. unsigned long flags;
  139. u32 reg;
  140. spin_lock_irqsave(&pctrl->lock, flags);
  141. /* Set pin to 'Non-use (Hi-Z input protection)' */
  142. reg = readw(pctrl->base + PM(port));
  143. reg &= ~(PM_MASK << (pin * 2));
  144. writew(reg, pctrl->base + PM(port));
  145. /* Temporarily switch to GPIO mode with PMC register */
  146. reg = readb(pctrl->base + PMC(port));
  147. writeb(reg & ~BIT(pin), pctrl->base + PMC(port));
  148. /* Set the PWPR register to allow PFC register to write */
  149. writel(0x0, pctrl->base + PWPR); /* B0WI=0, PFCWE=0 */
  150. writel(PWPR_PFCWE, pctrl->base + PWPR); /* B0WI=0, PFCWE=1 */
  151. /* Select Pin function mode with PFC register */
  152. reg = readl(pctrl->base + PFC(port));
  153. reg &= ~(PFC_MASK << (pin * 4));
  154. writel(reg | (func << (pin * 4)), pctrl->base + PFC(port));
  155. /* Set the PWPR register to be write-protected */
  156. writel(0x0, pctrl->base + PWPR); /* B0WI=0, PFCWE=0 */
  157. writel(PWPR_B0WI, pctrl->base + PWPR); /* B0WI=1, PFCWE=0 */
  158. /* Switch to Peripheral pin function with PMC register */
  159. reg = readb(pctrl->base + PMC(port));
  160. writeb(reg | BIT(pin), pctrl->base + PMC(port));
  161. spin_unlock_irqrestore(&pctrl->lock, flags);
  162. };
  163. static int rzg2l_pinctrl_set_mux(struct pinctrl_dev *pctldev,
  164. unsigned int func_selector,
  165. unsigned int group_selector)
  166. {
  167. struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  168. struct function_desc *func;
  169. unsigned int i, *psel_val;
  170. struct group_desc *group;
  171. int *pins;
  172. func = pinmux_generic_get_function(pctldev, func_selector);
  173. if (!func)
  174. return -EINVAL;
  175. group = pinctrl_generic_get_group(pctldev, group_selector);
  176. if (!group)
  177. return -EINVAL;
  178. psel_val = func->data;
  179. pins = group->pins;
  180. for (i = 0; i < group->num_pins; i++) {
  181. dev_dbg(pctrl->dev, "port:%u pin: %u PSEL:%u\n",
  182. RZG2L_PIN_ID_TO_PORT(pins[i]), RZG2L_PIN_ID_TO_PIN(pins[i]),
  183. psel_val[i]);
  184. rzg2l_pinctrl_set_pfc_mode(pctrl, RZG2L_PIN_ID_TO_PORT(pins[i]),
  185. RZG2L_PIN_ID_TO_PIN(pins[i]), psel_val[i]);
  186. }
  187. return 0;
  188. };
  189. static int rzg2l_map_add_config(struct pinctrl_map *map,
  190. const char *group_or_pin,
  191. enum pinctrl_map_type type,
  192. unsigned long *configs,
  193. unsigned int num_configs)
  194. {
  195. unsigned long *cfgs;
  196. cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
  197. GFP_KERNEL);
  198. if (!cfgs)
  199. return -ENOMEM;
  200. map->type = type;
  201. map->data.configs.group_or_pin = group_or_pin;
  202. map->data.configs.configs = cfgs;
  203. map->data.configs.num_configs = num_configs;
  204. return 0;
  205. }
  206. static int rzg2l_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  207. struct device_node *np,
  208. struct device_node *parent,
  209. struct pinctrl_map **map,
  210. unsigned int *num_maps,
  211. unsigned int *index)
  212. {
  213. struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  214. struct pinctrl_map *maps = *map;
  215. unsigned int nmaps = *num_maps;
  216. unsigned long *configs = NULL;
  217. unsigned int *pins, *psel_val;
  218. unsigned int num_pinmux = 0;
  219. unsigned int idx = *index;
  220. unsigned int num_pins, i;
  221. unsigned int num_configs;
  222. struct property *pinmux;
  223. struct property *prop;
  224. int ret, gsel, fsel;
  225. const char **pin_fn;
  226. const char *name;
  227. const char *pin;
  228. pinmux = of_find_property(np, "pinmux", NULL);
  229. if (pinmux)
  230. num_pinmux = pinmux->length / sizeof(u32);
  231. ret = of_property_count_strings(np, "pins");
  232. if (ret == -EINVAL) {
  233. num_pins = 0;
  234. } else if (ret < 0) {
  235. dev_err(pctrl->dev, "Invalid pins list in DT\n");
  236. return ret;
  237. } else {
  238. num_pins = ret;
  239. }
  240. if (!num_pinmux && !num_pins)
  241. return 0;
  242. if (num_pinmux && num_pins) {
  243. dev_err(pctrl->dev,
  244. "DT node must contain either a pinmux or pins and not both\n");
  245. return -EINVAL;
  246. }
  247. ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
  248. if (ret < 0)
  249. return ret;
  250. if (num_pins && !num_configs) {
  251. dev_err(pctrl->dev, "DT node must contain a config\n");
  252. ret = -ENODEV;
  253. goto done;
  254. }
  255. if (num_pinmux)
  256. nmaps += 1;
  257. if (num_pins)
  258. nmaps += num_pins;
  259. maps = krealloc_array(maps, nmaps, sizeof(*maps), GFP_KERNEL);
  260. if (!maps) {
  261. ret = -ENOMEM;
  262. goto done;
  263. }
  264. *map = maps;
  265. *num_maps = nmaps;
  266. if (num_pins) {
  267. of_property_for_each_string(np, "pins", prop, pin) {
  268. ret = rzg2l_map_add_config(&maps[idx], pin,
  269. PIN_MAP_TYPE_CONFIGS_PIN,
  270. configs, num_configs);
  271. if (ret < 0)
  272. goto done;
  273. idx++;
  274. }
  275. ret = 0;
  276. goto done;
  277. }
  278. pins = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*pins), GFP_KERNEL);
  279. psel_val = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*psel_val),
  280. GFP_KERNEL);
  281. pin_fn = devm_kzalloc(pctrl->dev, sizeof(*pin_fn), GFP_KERNEL);
  282. if (!pins || !psel_val || !pin_fn) {
  283. ret = -ENOMEM;
  284. goto done;
  285. }
  286. /* Collect pin locations and mux settings from DT properties */
  287. for (i = 0; i < num_pinmux; ++i) {
  288. u32 value;
  289. ret = of_property_read_u32_index(np, "pinmux", i, &value);
  290. if (ret)
  291. goto done;
  292. pins[i] = value & MUX_PIN_ID_MASK;
  293. psel_val[i] = MUX_FUNC(value);
  294. }
  295. if (parent) {
  296. name = devm_kasprintf(pctrl->dev, GFP_KERNEL, "%pOFn.%pOFn",
  297. parent, np);
  298. if (!name) {
  299. ret = -ENOMEM;
  300. goto done;
  301. }
  302. } else {
  303. name = np->name;
  304. }
  305. mutex_lock(&pctrl->mutex);
  306. /* Register a single pin group listing all the pins we read from DT */
  307. gsel = pinctrl_generic_add_group(pctldev, name, pins, num_pinmux, NULL);
  308. if (gsel < 0) {
  309. ret = gsel;
  310. goto unlock;
  311. }
  312. /*
  313. * Register a single group function where the 'data' is an array PSEL
  314. * register values read from DT.
  315. */
  316. pin_fn[0] = name;
  317. fsel = pinmux_generic_add_function(pctldev, name, pin_fn, 1, psel_val);
  318. if (fsel < 0) {
  319. ret = fsel;
  320. goto remove_group;
  321. }
  322. mutex_unlock(&pctrl->mutex);
  323. maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
  324. maps[idx].data.mux.group = name;
  325. maps[idx].data.mux.function = name;
  326. idx++;
  327. dev_dbg(pctrl->dev, "Parsed %pOF with %d pins\n", np, num_pinmux);
  328. ret = 0;
  329. goto done;
  330. remove_group:
  331. pinctrl_generic_remove_group(pctldev, gsel);
  332. unlock:
  333. mutex_unlock(&pctrl->mutex);
  334. done:
  335. *index = idx;
  336. kfree(configs);
  337. return ret;
  338. }
  339. static void rzg2l_dt_free_map(struct pinctrl_dev *pctldev,
  340. struct pinctrl_map *map,
  341. unsigned int num_maps)
  342. {
  343. unsigned int i;
  344. if (!map)
  345. return;
  346. for (i = 0; i < num_maps; ++i) {
  347. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
  348. map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  349. kfree(map[i].data.configs.configs);
  350. }
  351. kfree(map);
  352. }
  353. static int rzg2l_dt_node_to_map(struct pinctrl_dev *pctldev,
  354. struct device_node *np,
  355. struct pinctrl_map **map,
  356. unsigned int *num_maps)
  357. {
  358. struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  359. struct device_node *child;
  360. unsigned int index;
  361. int ret;
  362. *map = NULL;
  363. *num_maps = 0;
  364. index = 0;
  365. for_each_child_of_node(np, child) {
  366. ret = rzg2l_dt_subnode_to_map(pctldev, child, np, map,
  367. num_maps, &index);
  368. if (ret < 0) {
  369. of_node_put(child);
  370. goto done;
  371. }
  372. }
  373. if (*num_maps == 0) {
  374. ret = rzg2l_dt_subnode_to_map(pctldev, np, NULL, map,
  375. num_maps, &index);
  376. if (ret < 0)
  377. goto done;
  378. }
  379. if (*num_maps)
  380. return 0;
  381. dev_err(pctrl->dev, "no mapping found in node %pOF\n", np);
  382. ret = -EINVAL;
  383. done:
  384. if (ret < 0)
  385. rzg2l_dt_free_map(pctldev, *map, *num_maps);
  386. return ret;
  387. }
  388. static int rzg2l_validate_gpio_pin(struct rzg2l_pinctrl *pctrl,
  389. u32 cfg, u32 port, u8 bit)
  390. {
  391. u8 pincount = RZG2L_GPIO_PORT_GET_PINCNT(cfg);
  392. u32 port_index = RZG2L_GPIO_PORT_GET_INDEX(cfg);
  393. u32 data;
  394. if (bit >= pincount || port >= pctrl->data->n_port_pins)
  395. return -EINVAL;
  396. data = pctrl->data->port_pin_configs[port];
  397. if (port_index != RZG2L_GPIO_PORT_GET_INDEX(data))
  398. return -EINVAL;
  399. return 0;
  400. }
  401. static u32 rzg2l_read_pin_config(struct rzg2l_pinctrl *pctrl, u32 offset,
  402. u8 bit, u32 mask)
  403. {
  404. void __iomem *addr = pctrl->base + offset;
  405. /* handle _L/_H for 32-bit register read/write */
  406. if (bit >= 4) {
  407. bit -= 4;
  408. addr += 4;
  409. }
  410. return (readl(addr) >> (bit * 8)) & mask;
  411. }
  412. static void rzg2l_rmw_pin_config(struct rzg2l_pinctrl *pctrl, u32 offset,
  413. u8 bit, u32 mask, u32 val)
  414. {
  415. void __iomem *addr = pctrl->base + offset;
  416. unsigned long flags;
  417. u32 reg;
  418. /* handle _L/_H for 32-bit register read/write */
  419. if (bit >= 4) {
  420. bit -= 4;
  421. addr += 4;
  422. }
  423. spin_lock_irqsave(&pctrl->lock, flags);
  424. reg = readl(addr) & ~(mask << (bit * 8));
  425. writel(reg | (val << (bit * 8)), addr);
  426. spin_unlock_irqrestore(&pctrl->lock, flags);
  427. }
  428. static int rzg2l_pinctrl_pinconf_get(struct pinctrl_dev *pctldev,
  429. unsigned int _pin,
  430. unsigned long *config)
  431. {
  432. struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  433. enum pin_config_param param = pinconf_to_config_param(*config);
  434. const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
  435. unsigned int *pin_data = pin->drv_data;
  436. unsigned int arg = 0;
  437. unsigned long flags;
  438. void __iomem *addr;
  439. u32 port_offset;
  440. u32 cfg = 0;
  441. u8 bit = 0;
  442. if (!pin_data)
  443. return -EINVAL;
  444. if (*pin_data & RZG2L_SINGLE_PIN) {
  445. port_offset = RZG2L_SINGLE_PIN_GET_PORT_OFFSET(*pin_data);
  446. cfg = RZG2L_SINGLE_PIN_GET_CFGS(*pin_data);
  447. bit = RZG2L_SINGLE_PIN_GET_BIT(*pin_data);
  448. } else {
  449. cfg = RZG2L_GPIO_PORT_GET_CFGS(*pin_data);
  450. port_offset = RZG2L_PIN_ID_TO_PORT_OFFSET(_pin);
  451. bit = RZG2L_PIN_ID_TO_PIN(_pin);
  452. if (rzg2l_validate_gpio_pin(pctrl, *pin_data, RZG2L_PIN_ID_TO_PORT(_pin), bit))
  453. return -EINVAL;
  454. }
  455. switch (param) {
  456. case PIN_CONFIG_INPUT_ENABLE:
  457. if (!(cfg & PIN_CFG_IEN))
  458. return -EINVAL;
  459. arg = rzg2l_read_pin_config(pctrl, IEN(port_offset), bit, IEN_MASK);
  460. if (!arg)
  461. return -EINVAL;
  462. break;
  463. case PIN_CONFIG_POWER_SOURCE: {
  464. u32 pwr_reg = 0x0;
  465. if (cfg & PIN_CFG_IO_VMC_SD0)
  466. pwr_reg = SD_CH(0);
  467. else if (cfg & PIN_CFG_IO_VMC_SD1)
  468. pwr_reg = SD_CH(1);
  469. else if (cfg & PIN_CFG_IO_VMC_QSPI)
  470. pwr_reg = QSPI;
  471. else
  472. return -EINVAL;
  473. spin_lock_irqsave(&pctrl->lock, flags);
  474. addr = pctrl->base + pwr_reg;
  475. arg = (readl(addr) & PVDD_MASK) ? 1800 : 3300;
  476. spin_unlock_irqrestore(&pctrl->lock, flags);
  477. break;
  478. }
  479. case PIN_CONFIG_DRIVE_STRENGTH: {
  480. unsigned int index;
  481. if (!(cfg & PIN_CFG_IOLH_A))
  482. return -EINVAL;
  483. index = rzg2l_read_pin_config(pctrl, IOLH(port_offset), bit, IOLH_MASK);
  484. arg = iolh_groupa_mA[index];
  485. break;
  486. }
  487. case PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS: {
  488. unsigned int index;
  489. if (!(cfg & PIN_CFG_IOLH_B))
  490. return -EINVAL;
  491. index = rzg2l_read_pin_config(pctrl, IOLH(port_offset), bit, IOLH_MASK);
  492. arg = iolh_groupb_oi[index];
  493. break;
  494. }
  495. default:
  496. return -ENOTSUPP;
  497. }
  498. *config = pinconf_to_config_packed(param, arg);
  499. return 0;
  500. };
  501. static int rzg2l_pinctrl_pinconf_set(struct pinctrl_dev *pctldev,
  502. unsigned int _pin,
  503. unsigned long *_configs,
  504. unsigned int num_configs)
  505. {
  506. struct rzg2l_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  507. const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
  508. unsigned int *pin_data = pin->drv_data;
  509. enum pin_config_param param;
  510. unsigned long flags;
  511. void __iomem *addr;
  512. u32 port_offset;
  513. unsigned int i;
  514. u32 cfg = 0;
  515. u8 bit = 0;
  516. if (!pin_data)
  517. return -EINVAL;
  518. if (*pin_data & RZG2L_SINGLE_PIN) {
  519. port_offset = RZG2L_SINGLE_PIN_GET_PORT_OFFSET(*pin_data);
  520. cfg = RZG2L_SINGLE_PIN_GET_CFGS(*pin_data);
  521. bit = RZG2L_SINGLE_PIN_GET_BIT(*pin_data);
  522. } else {
  523. cfg = RZG2L_GPIO_PORT_GET_CFGS(*pin_data);
  524. port_offset = RZG2L_PIN_ID_TO_PORT_OFFSET(_pin);
  525. bit = RZG2L_PIN_ID_TO_PIN(_pin);
  526. if (rzg2l_validate_gpio_pin(pctrl, *pin_data, RZG2L_PIN_ID_TO_PORT(_pin), bit))
  527. return -EINVAL;
  528. }
  529. for (i = 0; i < num_configs; i++) {
  530. param = pinconf_to_config_param(_configs[i]);
  531. switch (param) {
  532. case PIN_CONFIG_INPUT_ENABLE: {
  533. unsigned int arg =
  534. pinconf_to_config_argument(_configs[i]);
  535. if (!(cfg & PIN_CFG_IEN))
  536. return -EINVAL;
  537. rzg2l_rmw_pin_config(pctrl, IEN(port_offset), bit, IEN_MASK, !!arg);
  538. break;
  539. }
  540. case PIN_CONFIG_POWER_SOURCE: {
  541. unsigned int mV = pinconf_to_config_argument(_configs[i]);
  542. u32 pwr_reg = 0x0;
  543. if (mV != 1800 && mV != 3300)
  544. return -EINVAL;
  545. if (cfg & PIN_CFG_IO_VMC_SD0)
  546. pwr_reg = SD_CH(0);
  547. else if (cfg & PIN_CFG_IO_VMC_SD1)
  548. pwr_reg = SD_CH(1);
  549. else if (cfg & PIN_CFG_IO_VMC_QSPI)
  550. pwr_reg = QSPI;
  551. else
  552. return -EINVAL;
  553. addr = pctrl->base + pwr_reg;
  554. spin_lock_irqsave(&pctrl->lock, flags);
  555. writel((mV == 1800) ? PVDD_1800 : PVDD_3300, addr);
  556. spin_unlock_irqrestore(&pctrl->lock, flags);
  557. break;
  558. }
  559. case PIN_CONFIG_DRIVE_STRENGTH: {
  560. unsigned int arg = pinconf_to_config_argument(_configs[i]);
  561. unsigned int index;
  562. if (!(cfg & PIN_CFG_IOLH_A))
  563. return -EINVAL;
  564. for (index = 0; index < ARRAY_SIZE(iolh_groupa_mA); index++) {
  565. if (arg == iolh_groupa_mA[index])
  566. break;
  567. }
  568. if (index >= ARRAY_SIZE(iolh_groupa_mA))
  569. return -EINVAL;
  570. rzg2l_rmw_pin_config(pctrl, IOLH(port_offset), bit, IOLH_MASK, index);
  571. break;
  572. }
  573. case PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS: {
  574. unsigned int arg = pinconf_to_config_argument(_configs[i]);
  575. unsigned int index;
  576. if (!(cfg & PIN_CFG_IOLH_B))
  577. return -EINVAL;
  578. for (index = 0; index < ARRAY_SIZE(iolh_groupb_oi); index++) {
  579. if (arg == iolh_groupb_oi[index])
  580. break;
  581. }
  582. if (index >= ARRAY_SIZE(iolh_groupb_oi))
  583. return -EINVAL;
  584. rzg2l_rmw_pin_config(pctrl, IOLH(port_offset), bit, IOLH_MASK, index);
  585. break;
  586. }
  587. default:
  588. return -EOPNOTSUPP;
  589. }
  590. }
  591. return 0;
  592. }
  593. static int rzg2l_pinctrl_pinconf_group_set(struct pinctrl_dev *pctldev,
  594. unsigned int group,
  595. unsigned long *configs,
  596. unsigned int num_configs)
  597. {
  598. const unsigned int *pins;
  599. unsigned int i, npins;
  600. int ret;
  601. ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
  602. if (ret)
  603. return ret;
  604. for (i = 0; i < npins; i++) {
  605. ret = rzg2l_pinctrl_pinconf_set(pctldev, pins[i], configs,
  606. num_configs);
  607. if (ret)
  608. return ret;
  609. }
  610. return 0;
  611. };
  612. static int rzg2l_pinctrl_pinconf_group_get(struct pinctrl_dev *pctldev,
  613. unsigned int group,
  614. unsigned long *config)
  615. {
  616. const unsigned int *pins;
  617. unsigned int i, npins, prev_config = 0;
  618. int ret;
  619. ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
  620. if (ret)
  621. return ret;
  622. for (i = 0; i < npins; i++) {
  623. ret = rzg2l_pinctrl_pinconf_get(pctldev, pins[i], config);
  624. if (ret)
  625. return ret;
  626. /* Check config matching between to pin */
  627. if (i && prev_config != *config)
  628. return -EOPNOTSUPP;
  629. prev_config = *config;
  630. }
  631. return 0;
  632. };
  633. static const struct pinctrl_ops rzg2l_pinctrl_pctlops = {
  634. .get_groups_count = pinctrl_generic_get_group_count,
  635. .get_group_name = pinctrl_generic_get_group_name,
  636. .get_group_pins = pinctrl_generic_get_group_pins,
  637. .dt_node_to_map = rzg2l_dt_node_to_map,
  638. .dt_free_map = rzg2l_dt_free_map,
  639. };
  640. static const struct pinmux_ops rzg2l_pinctrl_pmxops = {
  641. .get_functions_count = pinmux_generic_get_function_count,
  642. .get_function_name = pinmux_generic_get_function_name,
  643. .get_function_groups = pinmux_generic_get_function_groups,
  644. .set_mux = rzg2l_pinctrl_set_mux,
  645. .strict = true,
  646. };
  647. static const struct pinconf_ops rzg2l_pinctrl_confops = {
  648. .is_generic = true,
  649. .pin_config_get = rzg2l_pinctrl_pinconf_get,
  650. .pin_config_set = rzg2l_pinctrl_pinconf_set,
  651. .pin_config_group_set = rzg2l_pinctrl_pinconf_group_set,
  652. .pin_config_group_get = rzg2l_pinctrl_pinconf_group_get,
  653. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  654. };
  655. static int rzg2l_gpio_request(struct gpio_chip *chip, unsigned int offset)
  656. {
  657. struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
  658. u32 port = RZG2L_PIN_ID_TO_PORT(offset);
  659. u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
  660. unsigned long flags;
  661. u8 reg8;
  662. int ret;
  663. ret = pinctrl_gpio_request(chip->base + offset);
  664. if (ret)
  665. return ret;
  666. spin_lock_irqsave(&pctrl->lock, flags);
  667. /* Select GPIO mode in PMC Register */
  668. reg8 = readb(pctrl->base + PMC(port));
  669. reg8 &= ~BIT(bit);
  670. writeb(reg8, pctrl->base + PMC(port));
  671. spin_unlock_irqrestore(&pctrl->lock, flags);
  672. return 0;
  673. }
  674. static void rzg2l_gpio_set_direction(struct rzg2l_pinctrl *pctrl, u32 port,
  675. u8 bit, bool output)
  676. {
  677. unsigned long flags;
  678. u16 reg16;
  679. spin_lock_irqsave(&pctrl->lock, flags);
  680. reg16 = readw(pctrl->base + PM(port));
  681. reg16 &= ~(PM_MASK << (bit * 2));
  682. reg16 |= (output ? PM_OUTPUT : PM_INPUT) << (bit * 2);
  683. writew(reg16, pctrl->base + PM(port));
  684. spin_unlock_irqrestore(&pctrl->lock, flags);
  685. }
  686. static int rzg2l_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  687. {
  688. struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
  689. u32 port = RZG2L_PIN_ID_TO_PORT(offset);
  690. u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
  691. if (!(readb(pctrl->base + PMC(port)) & BIT(bit))) {
  692. u16 reg16;
  693. reg16 = readw(pctrl->base + PM(port));
  694. reg16 = (reg16 >> (bit * 2)) & PM_MASK;
  695. if (reg16 == PM_OUTPUT)
  696. return GPIO_LINE_DIRECTION_OUT;
  697. }
  698. return GPIO_LINE_DIRECTION_IN;
  699. }
  700. static int rzg2l_gpio_direction_input(struct gpio_chip *chip,
  701. unsigned int offset)
  702. {
  703. struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
  704. u32 port = RZG2L_PIN_ID_TO_PORT(offset);
  705. u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
  706. rzg2l_gpio_set_direction(pctrl, port, bit, false);
  707. return 0;
  708. }
  709. static void rzg2l_gpio_set(struct gpio_chip *chip, unsigned int offset,
  710. int value)
  711. {
  712. struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
  713. u32 port = RZG2L_PIN_ID_TO_PORT(offset);
  714. u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
  715. unsigned long flags;
  716. u8 reg8;
  717. spin_lock_irqsave(&pctrl->lock, flags);
  718. reg8 = readb(pctrl->base + P(port));
  719. if (value)
  720. writeb(reg8 | BIT(bit), pctrl->base + P(port));
  721. else
  722. writeb(reg8 & ~BIT(bit), pctrl->base + P(port));
  723. spin_unlock_irqrestore(&pctrl->lock, flags);
  724. }
  725. static int rzg2l_gpio_direction_output(struct gpio_chip *chip,
  726. unsigned int offset, int value)
  727. {
  728. struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
  729. u32 port = RZG2L_PIN_ID_TO_PORT(offset);
  730. u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
  731. rzg2l_gpio_set(chip, offset, value);
  732. rzg2l_gpio_set_direction(pctrl, port, bit, true);
  733. return 0;
  734. }
  735. static int rzg2l_gpio_get(struct gpio_chip *chip, unsigned int offset)
  736. {
  737. struct rzg2l_pinctrl *pctrl = gpiochip_get_data(chip);
  738. u32 port = RZG2L_PIN_ID_TO_PORT(offset);
  739. u8 bit = RZG2L_PIN_ID_TO_PIN(offset);
  740. u16 reg16;
  741. reg16 = readw(pctrl->base + PM(port));
  742. reg16 = (reg16 >> (bit * 2)) & PM_MASK;
  743. if (reg16 == PM_INPUT)
  744. return !!(readb(pctrl->base + PIN(port)) & BIT(bit));
  745. else if (reg16 == PM_OUTPUT)
  746. return !!(readb(pctrl->base + P(port)) & BIT(bit));
  747. else
  748. return -EINVAL;
  749. }
  750. static void rzg2l_gpio_free(struct gpio_chip *chip, unsigned int offset)
  751. {
  752. unsigned int virq;
  753. pinctrl_gpio_free(chip->base + offset);
  754. virq = irq_find_mapping(chip->irq.domain, offset);
  755. if (virq)
  756. irq_dispose_mapping(virq);
  757. /*
  758. * Set the GPIO as an input to ensure that the next GPIO request won't
  759. * drive the GPIO pin as an output.
  760. */
  761. rzg2l_gpio_direction_input(chip, offset);
  762. }
  763. static const char * const rzg2l_gpio_names[] = {
  764. "P0_0", "P0_1", "P0_2", "P0_3", "P0_4", "P0_5", "P0_6", "P0_7",
  765. "P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7",
  766. "P2_0", "P2_1", "P2_2", "P2_3", "P2_4", "P2_5", "P2_6", "P2_7",
  767. "P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7",
  768. "P4_0", "P4_1", "P4_2", "P4_3", "P4_4", "P4_5", "P4_6", "P4_7",
  769. "P5_0", "P5_1", "P5_2", "P5_3", "P5_4", "P5_5", "P5_6", "P5_7",
  770. "P6_0", "P6_1", "P6_2", "P6_3", "P6_4", "P6_5", "P6_6", "P6_7",
  771. "P7_0", "P7_1", "P7_2", "P7_3", "P7_4", "P7_5", "P7_6", "P7_7",
  772. "P8_0", "P8_1", "P8_2", "P8_3", "P8_4", "P8_5", "P8_6", "P8_7",
  773. "P9_0", "P9_1", "P9_2", "P9_3", "P9_4", "P9_5", "P9_6", "P9_7",
  774. "P10_0", "P10_1", "P10_2", "P10_3", "P10_4", "P10_5", "P10_6", "P10_7",
  775. "P11_0", "P11_1", "P11_2", "P11_3", "P11_4", "P11_5", "P11_6", "P11_7",
  776. "P12_0", "P12_1", "P12_2", "P12_3", "P12_4", "P12_5", "P12_6", "P12_7",
  777. "P13_0", "P13_1", "P13_2", "P13_3", "P13_4", "P13_5", "P13_6", "P13_7",
  778. "P14_0", "P14_1", "P14_2", "P14_3", "P14_4", "P14_5", "P14_6", "P14_7",
  779. "P15_0", "P15_1", "P15_2", "P15_3", "P15_4", "P15_5", "P15_6", "P15_7",
  780. "P16_0", "P16_1", "P16_2", "P16_3", "P16_4", "P16_5", "P16_6", "P16_7",
  781. "P17_0", "P17_1", "P17_2", "P17_3", "P17_4", "P17_5", "P17_6", "P17_7",
  782. "P18_0", "P18_1", "P18_2", "P18_3", "P18_4", "P18_5", "P18_6", "P18_7",
  783. "P19_0", "P19_1", "P19_2", "P19_3", "P19_4", "P19_5", "P19_6", "P19_7",
  784. "P20_0", "P20_1", "P20_2", "P20_3", "P20_4", "P20_5", "P20_6", "P20_7",
  785. "P21_0", "P21_1", "P21_2", "P21_3", "P21_4", "P21_5", "P21_6", "P21_7",
  786. "P22_0", "P22_1", "P22_2", "P22_3", "P22_4", "P22_5", "P22_6", "P22_7",
  787. "P23_0", "P23_1", "P23_2", "P23_3", "P23_4", "P23_5", "P23_6", "P23_7",
  788. "P24_0", "P24_1", "P24_2", "P24_3", "P24_4", "P24_5", "P24_6", "P24_7",
  789. "P25_0", "P25_1", "P25_2", "P25_3", "P25_4", "P25_5", "P25_6", "P25_7",
  790. "P26_0", "P26_1", "P26_2", "P26_3", "P26_4", "P26_5", "P26_6", "P26_7",
  791. "P27_0", "P27_1", "P27_2", "P27_3", "P27_4", "P27_5", "P27_6", "P27_7",
  792. "P28_0", "P28_1", "P28_2", "P28_3", "P28_4", "P28_5", "P28_6", "P28_7",
  793. "P29_0", "P29_1", "P29_2", "P29_3", "P29_4", "P29_5", "P29_6", "P29_7",
  794. "P30_0", "P30_1", "P30_2", "P30_3", "P30_4", "P30_5", "P30_6", "P30_7",
  795. "P31_0", "P31_1", "P31_2", "P31_3", "P31_4", "P31_5", "P31_6", "P31_7",
  796. "P32_0", "P32_1", "P32_2", "P32_3", "P32_4", "P32_5", "P32_6", "P32_7",
  797. "P33_0", "P33_1", "P33_2", "P33_3", "P33_4", "P33_5", "P33_6", "P33_7",
  798. "P34_0", "P34_1", "P34_2", "P34_3", "P34_4", "P34_5", "P34_6", "P34_7",
  799. "P35_0", "P35_1", "P35_2", "P35_3", "P35_4", "P35_5", "P35_6", "P35_7",
  800. "P36_0", "P36_1", "P36_2", "P36_3", "P36_4", "P36_5", "P36_6", "P36_7",
  801. "P37_0", "P37_1", "P37_2", "P37_3", "P37_4", "P37_5", "P37_6", "P37_7",
  802. "P38_0", "P38_1", "P38_2", "P38_3", "P38_4", "P38_5", "P38_6", "P38_7",
  803. "P39_0", "P39_1", "P39_2", "P39_3", "P39_4", "P39_5", "P39_6", "P39_7",
  804. "P40_0", "P40_1", "P40_2", "P40_3", "P40_4", "P40_5", "P40_6", "P40_7",
  805. "P41_0", "P41_1", "P41_2", "P41_3", "P41_4", "P41_5", "P41_6", "P41_7",
  806. "P42_0", "P42_1", "P42_2", "P42_3", "P42_4", "P42_5", "P42_6", "P42_7",
  807. "P43_0", "P43_1", "P43_2", "P43_3", "P43_4", "P43_5", "P43_6", "P43_7",
  808. "P44_0", "P44_1", "P44_2", "P44_3", "P44_4", "P44_5", "P44_6", "P44_7",
  809. "P45_0", "P45_1", "P45_2", "P45_3", "P45_4", "P45_5", "P45_6", "P45_7",
  810. "P46_0", "P46_1", "P46_2", "P46_3", "P46_4", "P46_5", "P46_6", "P46_7",
  811. "P47_0", "P47_1", "P47_2", "P47_3", "P47_4", "P47_5", "P47_6", "P47_7",
  812. "P48_0", "P48_1", "P48_2", "P48_3", "P48_4", "P48_5", "P48_6", "P48_7",
  813. };
  814. static const u32 rzg2l_gpio_configs[] = {
  815. RZG2L_GPIO_PORT_PACK(2, 0x10, RZG2L_MPXED_PIN_FUNCS),
  816. RZG2L_GPIO_PORT_PACK(2, 0x11, RZG2L_MPXED_PIN_FUNCS),
  817. RZG2L_GPIO_PORT_PACK(2, 0x12, RZG2L_MPXED_PIN_FUNCS),
  818. RZG2L_GPIO_PORT_PACK(2, 0x13, RZG2L_MPXED_PIN_FUNCS),
  819. RZG2L_GPIO_PORT_PACK(2, 0x14, RZG2L_MPXED_PIN_FUNCS),
  820. RZG2L_GPIO_PORT_PACK(3, 0x15, RZG2L_MPXED_PIN_FUNCS),
  821. RZG2L_GPIO_PORT_PACK(2, 0x16, RZG2L_MPXED_PIN_FUNCS),
  822. RZG2L_GPIO_PORT_PACK(3, 0x17, RZG2L_MPXED_PIN_FUNCS),
  823. RZG2L_GPIO_PORT_PACK(3, 0x18, RZG2L_MPXED_PIN_FUNCS),
  824. RZG2L_GPIO_PORT_PACK(2, 0x19, RZG2L_MPXED_PIN_FUNCS),
  825. RZG2L_GPIO_PORT_PACK(2, 0x1a, RZG2L_MPXED_PIN_FUNCS),
  826. RZG2L_GPIO_PORT_PACK(2, 0x1b, RZG2L_MPXED_PIN_FUNCS),
  827. RZG2L_GPIO_PORT_PACK(2, 0x1c, RZG2L_MPXED_PIN_FUNCS),
  828. RZG2L_GPIO_PORT_PACK(3, 0x1d, RZG2L_MPXED_PIN_FUNCS),
  829. RZG2L_GPIO_PORT_PACK(2, 0x1e, RZG2L_MPXED_PIN_FUNCS),
  830. RZG2L_GPIO_PORT_PACK(2, 0x1f, RZG2L_MPXED_PIN_FUNCS),
  831. RZG2L_GPIO_PORT_PACK(2, 0x20, RZG2L_MPXED_PIN_FUNCS),
  832. RZG2L_GPIO_PORT_PACK(3, 0x21, RZG2L_MPXED_PIN_FUNCS),
  833. RZG2L_GPIO_PORT_PACK(2, 0x22, RZG2L_MPXED_PIN_FUNCS),
  834. RZG2L_GPIO_PORT_PACK(2, 0x23, RZG2L_MPXED_PIN_FUNCS),
  835. RZG2L_GPIO_PORT_PACK(3, 0x24, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  836. RZG2L_GPIO_PORT_PACK(2, 0x25, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  837. RZG2L_GPIO_PORT_PACK(2, 0x26, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  838. RZG2L_GPIO_PORT_PACK(2, 0x27, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  839. RZG2L_GPIO_PORT_PACK(2, 0x28, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  840. RZG2L_GPIO_PORT_PACK(2, 0x29, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  841. RZG2L_GPIO_PORT_PACK(2, 0x2a, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  842. RZG2L_GPIO_PORT_PACK(2, 0x2b, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  843. RZG2L_GPIO_PORT_PACK(2, 0x2c, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  844. RZG2L_GPIO_PORT_PACK(2, 0x2d, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  845. RZG2L_GPIO_PORT_PACK(2, 0x2e, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  846. RZG2L_GPIO_PORT_PACK(2, 0x2f, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  847. RZG2L_GPIO_PORT_PACK(2, 0x30, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  848. RZG2L_GPIO_PORT_PACK(2, 0x31, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  849. RZG2L_GPIO_PORT_PACK(2, 0x32, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  850. RZG2L_GPIO_PORT_PACK(2, 0x33, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  851. RZG2L_GPIO_PORT_PACK(2, 0x34, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  852. RZG2L_GPIO_PORT_PACK(3, 0x35, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  853. RZG2L_GPIO_PORT_PACK(2, 0x36, RZG2L_MPXED_PIN_FUNCS),
  854. RZG2L_GPIO_PORT_PACK(3, 0x37, RZG2L_MPXED_PIN_FUNCS),
  855. RZG2L_GPIO_PORT_PACK(3, 0x38, RZG2L_MPXED_PIN_FUNCS),
  856. RZG2L_GPIO_PORT_PACK(2, 0x39, RZG2L_MPXED_PIN_FUNCS),
  857. RZG2L_GPIO_PORT_PACK(5, 0x3a, RZG2L_MPXED_PIN_FUNCS),
  858. RZG2L_GPIO_PORT_PACK(4, 0x3b, RZG2L_MPXED_PIN_FUNCS),
  859. RZG2L_GPIO_PORT_PACK(4, 0x3c, RZG2L_MPXED_PIN_FUNCS),
  860. RZG2L_GPIO_PORT_PACK(4, 0x3d, RZG2L_MPXED_PIN_FUNCS),
  861. RZG2L_GPIO_PORT_PACK(4, 0x3e, RZG2L_MPXED_PIN_FUNCS),
  862. RZG2L_GPIO_PORT_PACK(4, 0x3f, RZG2L_MPXED_PIN_FUNCS),
  863. RZG2L_GPIO_PORT_PACK(5, 0x40, RZG2L_MPXED_PIN_FUNCS),
  864. };
  865. static const u32 r9a07g043_gpio_configs[] = {
  866. RZG2L_GPIO_PORT_PACK(4, 0x10, RZG2L_MPXED_PIN_FUNCS),
  867. RZG2L_GPIO_PORT_PACK(5, 0x11, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  868. RZG2L_GPIO_PORT_PACK(4, 0x12, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  869. RZG2L_GPIO_PORT_PACK(4, 0x13, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  870. RZG2L_GPIO_PORT_PACK(6, 0x14, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)),
  871. RZG2L_GPIO_PORT_PACK(5, 0x15, RZG2L_MPXED_PIN_FUNCS),
  872. RZG2L_GPIO_PORT_PACK(5, 0x16, RZG2L_MPXED_PIN_FUNCS),
  873. RZG2L_GPIO_PORT_PACK(5, 0x17, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  874. RZG2L_GPIO_PORT_PACK(5, 0x18, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  875. RZG2L_GPIO_PORT_PACK(4, 0x19, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  876. RZG2L_GPIO_PORT_PACK(5, 0x1a, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)),
  877. RZG2L_GPIO_PORT_PACK(4, 0x1b, RZG2L_MPXED_PIN_FUNCS),
  878. RZG2L_GPIO_PORT_PACK(2, 0x1c, RZG2L_MPXED_PIN_FUNCS),
  879. RZG2L_GPIO_PORT_PACK(5, 0x1d, RZG2L_MPXED_PIN_FUNCS),
  880. RZG2L_GPIO_PORT_PACK(3, 0x1e, RZG2L_MPXED_PIN_FUNCS),
  881. RZG2L_GPIO_PORT_PACK(4, 0x1f, RZG2L_MPXED_PIN_FUNCS),
  882. RZG2L_GPIO_PORT_PACK(2, 0x20, RZG2L_MPXED_PIN_FUNCS),
  883. RZG2L_GPIO_PORT_PACK(4, 0x21, RZG2L_MPXED_PIN_FUNCS),
  884. RZG2L_GPIO_PORT_PACK(6, 0x22, RZG2L_MPXED_PIN_FUNCS),
  885. };
  886. static struct {
  887. struct rzg2l_dedicated_configs common[35];
  888. struct rzg2l_dedicated_configs rzg2l_pins[7];
  889. } rzg2l_dedicated_pins = {
  890. .common = {
  891. { "NMI", RZG2L_SINGLE_PIN_PACK(0x1, 0,
  892. (PIN_CFG_FILONOFF | PIN_CFG_FILNUM | PIN_CFG_FILCLKSEL)) },
  893. { "TMS/SWDIO", RZG2L_SINGLE_PIN_PACK(0x2, 0,
  894. (PIN_CFG_IOLH_A | PIN_CFG_SR | PIN_CFG_IEN)) },
  895. { "TDO", RZG2L_SINGLE_PIN_PACK(0x3, 0,
  896. (PIN_CFG_IOLH_A | PIN_CFG_SR | PIN_CFG_IEN)) },
  897. { "AUDIO_CLK1", RZG2L_SINGLE_PIN_PACK(0x4, 0, PIN_CFG_IEN) },
  898. { "AUDIO_CLK2", RZG2L_SINGLE_PIN_PACK(0x4, 1, PIN_CFG_IEN) },
  899. { "SD0_CLK", RZG2L_SINGLE_PIN_PACK(0x6, 0,
  900. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD0)) },
  901. { "SD0_CMD", RZG2L_SINGLE_PIN_PACK(0x6, 1,
  902. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  903. { "SD0_RST#", RZG2L_SINGLE_PIN_PACK(0x6, 2,
  904. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD0)) },
  905. { "SD0_DATA0", RZG2L_SINGLE_PIN_PACK(0x7, 0,
  906. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  907. { "SD0_DATA1", RZG2L_SINGLE_PIN_PACK(0x7, 1,
  908. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  909. { "SD0_DATA2", RZG2L_SINGLE_PIN_PACK(0x7, 2,
  910. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  911. { "SD0_DATA3", RZG2L_SINGLE_PIN_PACK(0x7, 3,
  912. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  913. { "SD0_DATA4", RZG2L_SINGLE_PIN_PACK(0x7, 4,
  914. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  915. { "SD0_DATA5", RZG2L_SINGLE_PIN_PACK(0x7, 5,
  916. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  917. { "SD0_DATA6", RZG2L_SINGLE_PIN_PACK(0x7, 6,
  918. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  919. { "SD0_DATA7", RZG2L_SINGLE_PIN_PACK(0x7, 7,
  920. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) },
  921. { "SD1_CLK", RZG2L_SINGLE_PIN_PACK(0x8, 0,
  922. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD1)) },
  923. { "SD1_CMD", RZG2L_SINGLE_PIN_PACK(0x8, 1,
  924. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
  925. { "SD1_DATA0", RZG2L_SINGLE_PIN_PACK(0x9, 0,
  926. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
  927. { "SD1_DATA1", RZG2L_SINGLE_PIN_PACK(0x9, 1,
  928. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
  929. { "SD1_DATA2", RZG2L_SINGLE_PIN_PACK(0x9, 2,
  930. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
  931. { "SD1_DATA3", RZG2L_SINGLE_PIN_PACK(0x9, 3,
  932. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) },
  933. { "QSPI0_SPCLK", RZG2L_SINGLE_PIN_PACK(0xa, 0,
  934. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  935. { "QSPI0_IO0", RZG2L_SINGLE_PIN_PACK(0xa, 1,
  936. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  937. { "QSPI0_IO1", RZG2L_SINGLE_PIN_PACK(0xa, 2,
  938. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  939. { "QSPI0_IO2", RZG2L_SINGLE_PIN_PACK(0xa, 3,
  940. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  941. { "QSPI0_IO3", RZG2L_SINGLE_PIN_PACK(0xa, 4,
  942. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  943. { "QSPI0_SSL", RZG2L_SINGLE_PIN_PACK(0xa, 5,
  944. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  945. { "QSPI_RESET#", RZG2L_SINGLE_PIN_PACK(0xc, 0,
  946. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  947. { "QSPI_WP#", RZG2L_SINGLE_PIN_PACK(0xc, 1,
  948. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  949. { "WDTOVF_PERROUT#", RZG2L_SINGLE_PIN_PACK(0xd, 0, (PIN_CFG_IOLH_A | PIN_CFG_SR)) },
  950. { "RIIC0_SDA", RZG2L_SINGLE_PIN_PACK(0xe, 0, PIN_CFG_IEN) },
  951. { "RIIC0_SCL", RZG2L_SINGLE_PIN_PACK(0xe, 1, PIN_CFG_IEN) },
  952. { "RIIC1_SDA", RZG2L_SINGLE_PIN_PACK(0xe, 2, PIN_CFG_IEN) },
  953. { "RIIC1_SCL", RZG2L_SINGLE_PIN_PACK(0xe, 3, PIN_CFG_IEN) },
  954. },
  955. .rzg2l_pins = {
  956. { "QSPI_INT#", RZG2L_SINGLE_PIN_PACK(0xc, 2, (PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  957. { "QSPI1_SPCLK", RZG2L_SINGLE_PIN_PACK(0xb, 0,
  958. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  959. { "QSPI1_IO0", RZG2L_SINGLE_PIN_PACK(0xb, 1,
  960. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  961. { "QSPI1_IO1", RZG2L_SINGLE_PIN_PACK(0xb, 2,
  962. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  963. { "QSPI1_IO2", RZG2L_SINGLE_PIN_PACK(0xb, 3,
  964. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  965. { "QSPI1_IO3", RZG2L_SINGLE_PIN_PACK(0xb, 4,
  966. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  967. { "QSPI1_SSL", RZG2L_SINGLE_PIN_PACK(0xb, 5,
  968. (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) },
  969. }
  970. };
  971. static int rzg2l_gpio_get_gpioint(unsigned int virq, const struct rzg2l_pinctrl_data *data)
  972. {
  973. unsigned int gpioint;
  974. unsigned int i;
  975. u32 port, bit;
  976. port = virq / 8;
  977. bit = virq % 8;
  978. if (port >= data->n_ports ||
  979. bit >= RZG2L_GPIO_PORT_GET_PINCNT(data->port_pin_configs[port]))
  980. return -EINVAL;
  981. gpioint = bit;
  982. for (i = 0; i < port; i++)
  983. gpioint += RZG2L_GPIO_PORT_GET_PINCNT(data->port_pin_configs[i]);
  984. return gpioint;
  985. }
  986. static void rzg2l_gpio_irq_disable(struct irq_data *d)
  987. {
  988. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  989. struct rzg2l_pinctrl *pctrl = container_of(gc, struct rzg2l_pinctrl, gpio_chip);
  990. unsigned int hwirq = irqd_to_hwirq(d);
  991. unsigned long flags;
  992. void __iomem *addr;
  993. u32 port;
  994. u8 bit;
  995. irq_chip_disable_parent(d);
  996. port = RZG2L_PIN_ID_TO_PORT(hwirq);
  997. bit = RZG2L_PIN_ID_TO_PIN(hwirq);
  998. addr = pctrl->base + ISEL(port);
  999. if (bit >= 4) {
  1000. bit -= 4;
  1001. addr += 4;
  1002. }
  1003. spin_lock_irqsave(&pctrl->lock, flags);
  1004. writel(readl(addr) & ~BIT(bit * 8), addr);
  1005. spin_unlock_irqrestore(&pctrl->lock, flags);
  1006. gpiochip_disable_irq(gc, hwirq);
  1007. }
  1008. static void rzg2l_gpio_irq_enable(struct irq_data *d)
  1009. {
  1010. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1011. struct rzg2l_pinctrl *pctrl = container_of(gc, struct rzg2l_pinctrl, gpio_chip);
  1012. unsigned int hwirq = irqd_to_hwirq(d);
  1013. unsigned long flags;
  1014. void __iomem *addr;
  1015. u32 port;
  1016. u8 bit;
  1017. gpiochip_enable_irq(gc, hwirq);
  1018. port = RZG2L_PIN_ID_TO_PORT(hwirq);
  1019. bit = RZG2L_PIN_ID_TO_PIN(hwirq);
  1020. addr = pctrl->base + ISEL(port);
  1021. if (bit >= 4) {
  1022. bit -= 4;
  1023. addr += 4;
  1024. }
  1025. spin_lock_irqsave(&pctrl->lock, flags);
  1026. writel(readl(addr) | BIT(bit * 8), addr);
  1027. spin_unlock_irqrestore(&pctrl->lock, flags);
  1028. irq_chip_enable_parent(d);
  1029. }
  1030. static int rzg2l_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  1031. {
  1032. return irq_chip_set_type_parent(d, type);
  1033. }
  1034. static void rzg2l_gpio_irqc_eoi(struct irq_data *d)
  1035. {
  1036. irq_chip_eoi_parent(d);
  1037. }
  1038. static void rzg2l_gpio_irq_print_chip(struct irq_data *data, struct seq_file *p)
  1039. {
  1040. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  1041. seq_printf(p, dev_name(gc->parent));
  1042. }
  1043. static const struct irq_chip rzg2l_gpio_irqchip = {
  1044. .name = "rzg2l-gpio",
  1045. .irq_disable = rzg2l_gpio_irq_disable,
  1046. .irq_enable = rzg2l_gpio_irq_enable,
  1047. .irq_mask = irq_chip_mask_parent,
  1048. .irq_unmask = irq_chip_unmask_parent,
  1049. .irq_set_type = rzg2l_gpio_irq_set_type,
  1050. .irq_eoi = rzg2l_gpio_irqc_eoi,
  1051. .irq_print_chip = rzg2l_gpio_irq_print_chip,
  1052. .flags = IRQCHIP_IMMUTABLE,
  1053. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  1054. };
  1055. static int rzg2l_gpio_child_to_parent_hwirq(struct gpio_chip *gc,
  1056. unsigned int child,
  1057. unsigned int child_type,
  1058. unsigned int *parent,
  1059. unsigned int *parent_type)
  1060. {
  1061. struct rzg2l_pinctrl *pctrl = gpiochip_get_data(gc);
  1062. unsigned long flags;
  1063. int gpioint, irq;
  1064. gpioint = rzg2l_gpio_get_gpioint(child, pctrl->data);
  1065. if (gpioint < 0)
  1066. return gpioint;
  1067. spin_lock_irqsave(&pctrl->bitmap_lock, flags);
  1068. irq = bitmap_find_free_region(pctrl->tint_slot, RZG2L_TINT_MAX_INTERRUPT, get_order(1));
  1069. spin_unlock_irqrestore(&pctrl->bitmap_lock, flags);
  1070. if (irq < 0)
  1071. return -ENOSPC;
  1072. pctrl->hwirq[irq] = child;
  1073. irq += RZG2L_TINT_IRQ_START_INDEX;
  1074. /* All these interrupts are level high in the CPU */
  1075. *parent_type = IRQ_TYPE_LEVEL_HIGH;
  1076. *parent = RZG2L_PACK_HWIRQ(gpioint, irq);
  1077. return 0;
  1078. }
  1079. static int rzg2l_gpio_populate_parent_fwspec(struct gpio_chip *chip,
  1080. union gpio_irq_fwspec *gfwspec,
  1081. unsigned int parent_hwirq,
  1082. unsigned int parent_type)
  1083. {
  1084. struct irq_fwspec *fwspec = &gfwspec->fwspec;
  1085. fwspec->fwnode = chip->irq.parent_domain->fwnode;
  1086. fwspec->param_count = 2;
  1087. fwspec->param[0] = parent_hwirq;
  1088. fwspec->param[1] = parent_type;
  1089. return 0;
  1090. }
  1091. static void rzg2l_gpio_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  1092. unsigned int nr_irqs)
  1093. {
  1094. struct irq_data *d;
  1095. d = irq_domain_get_irq_data(domain, virq);
  1096. if (d) {
  1097. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1098. struct rzg2l_pinctrl *pctrl = container_of(gc, struct rzg2l_pinctrl, gpio_chip);
  1099. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1100. unsigned long flags;
  1101. unsigned int i;
  1102. for (i = 0; i < RZG2L_TINT_MAX_INTERRUPT; i++) {
  1103. if (pctrl->hwirq[i] == hwirq) {
  1104. spin_lock_irqsave(&pctrl->bitmap_lock, flags);
  1105. bitmap_release_region(pctrl->tint_slot, i, get_order(1));
  1106. spin_unlock_irqrestore(&pctrl->bitmap_lock, flags);
  1107. pctrl->hwirq[i] = 0;
  1108. break;
  1109. }
  1110. }
  1111. }
  1112. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  1113. }
  1114. static void rzg2l_init_irq_valid_mask(struct gpio_chip *gc,
  1115. unsigned long *valid_mask,
  1116. unsigned int ngpios)
  1117. {
  1118. struct rzg2l_pinctrl *pctrl = gpiochip_get_data(gc);
  1119. struct gpio_chip *chip = &pctrl->gpio_chip;
  1120. unsigned int offset;
  1121. /* Forbid unused lines to be mapped as IRQs */
  1122. for (offset = 0; offset < chip->ngpio; offset++) {
  1123. u32 port, bit;
  1124. port = offset / 8;
  1125. bit = offset % 8;
  1126. if (port >= pctrl->data->n_ports ||
  1127. bit >= RZG2L_GPIO_PORT_GET_PINCNT(pctrl->data->port_pin_configs[port]))
  1128. clear_bit(offset, valid_mask);
  1129. }
  1130. }
  1131. static int rzg2l_gpio_register(struct rzg2l_pinctrl *pctrl)
  1132. {
  1133. struct device_node *np = pctrl->dev->of_node;
  1134. struct gpio_chip *chip = &pctrl->gpio_chip;
  1135. const char *name = dev_name(pctrl->dev);
  1136. struct irq_domain *parent_domain;
  1137. struct of_phandle_args of_args;
  1138. struct device_node *parent_np;
  1139. struct gpio_irq_chip *girq;
  1140. int ret;
  1141. parent_np = of_irq_find_parent(np);
  1142. if (!parent_np)
  1143. return -ENXIO;
  1144. parent_domain = irq_find_host(parent_np);
  1145. of_node_put(parent_np);
  1146. if (!parent_domain)
  1147. return -EPROBE_DEFER;
  1148. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &of_args);
  1149. if (ret) {
  1150. dev_err(pctrl->dev, "Unable to parse gpio-ranges\n");
  1151. return ret;
  1152. }
  1153. if (of_args.args[0] != 0 || of_args.args[1] != 0 ||
  1154. of_args.args[2] != pctrl->data->n_port_pins) {
  1155. dev_err(pctrl->dev, "gpio-ranges does not match selected SOC\n");
  1156. return -EINVAL;
  1157. }
  1158. chip->names = pctrl->data->port_pins;
  1159. chip->request = rzg2l_gpio_request;
  1160. chip->free = rzg2l_gpio_free;
  1161. chip->get_direction = rzg2l_gpio_get_direction;
  1162. chip->direction_input = rzg2l_gpio_direction_input;
  1163. chip->direction_output = rzg2l_gpio_direction_output;
  1164. chip->get = rzg2l_gpio_get;
  1165. chip->set = rzg2l_gpio_set;
  1166. chip->label = name;
  1167. chip->parent = pctrl->dev;
  1168. chip->owner = THIS_MODULE;
  1169. chip->base = -1;
  1170. chip->ngpio = of_args.args[2];
  1171. girq = &chip->irq;
  1172. gpio_irq_chip_set_chip(girq, &rzg2l_gpio_irqchip);
  1173. girq->fwnode = of_node_to_fwnode(np);
  1174. girq->parent_domain = parent_domain;
  1175. girq->child_to_parent_hwirq = rzg2l_gpio_child_to_parent_hwirq;
  1176. girq->populate_parent_alloc_arg = rzg2l_gpio_populate_parent_fwspec;
  1177. girq->child_irq_domain_ops.free = rzg2l_gpio_irq_domain_free;
  1178. girq->init_valid_mask = rzg2l_init_irq_valid_mask;
  1179. pctrl->gpio_range.id = 0;
  1180. pctrl->gpio_range.pin_base = 0;
  1181. pctrl->gpio_range.base = 0;
  1182. pctrl->gpio_range.npins = chip->ngpio;
  1183. pctrl->gpio_range.name = chip->label;
  1184. pctrl->gpio_range.gc = chip;
  1185. ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl);
  1186. if (ret) {
  1187. dev_err(pctrl->dev, "failed to add GPIO controller\n");
  1188. return ret;
  1189. }
  1190. dev_dbg(pctrl->dev, "Registered gpio controller\n");
  1191. return 0;
  1192. }
  1193. static int rzg2l_pinctrl_register(struct rzg2l_pinctrl *pctrl)
  1194. {
  1195. struct pinctrl_pin_desc *pins;
  1196. unsigned int i, j;
  1197. u32 *pin_data;
  1198. int ret;
  1199. pctrl->desc.name = DRV_NAME;
  1200. pctrl->desc.npins = pctrl->data->n_port_pins + pctrl->data->n_dedicated_pins;
  1201. pctrl->desc.pctlops = &rzg2l_pinctrl_pctlops;
  1202. pctrl->desc.pmxops = &rzg2l_pinctrl_pmxops;
  1203. pctrl->desc.confops = &rzg2l_pinctrl_confops;
  1204. pctrl->desc.owner = THIS_MODULE;
  1205. pins = devm_kcalloc(pctrl->dev, pctrl->desc.npins, sizeof(*pins), GFP_KERNEL);
  1206. if (!pins)
  1207. return -ENOMEM;
  1208. pin_data = devm_kcalloc(pctrl->dev, pctrl->desc.npins,
  1209. sizeof(*pin_data), GFP_KERNEL);
  1210. if (!pin_data)
  1211. return -ENOMEM;
  1212. pctrl->pins = pins;
  1213. pctrl->desc.pins = pins;
  1214. for (i = 0, j = 0; i < pctrl->data->n_port_pins; i++) {
  1215. pins[i].number = i;
  1216. pins[i].name = pctrl->data->port_pins[i];
  1217. if (i && !(i % RZG2L_PINS_PER_PORT))
  1218. j++;
  1219. pin_data[i] = pctrl->data->port_pin_configs[j];
  1220. pins[i].drv_data = &pin_data[i];
  1221. }
  1222. for (i = 0; i < pctrl->data->n_dedicated_pins; i++) {
  1223. unsigned int index = pctrl->data->n_port_pins + i;
  1224. pins[index].number = index;
  1225. pins[index].name = pctrl->data->dedicated_pins[i].name;
  1226. pin_data[index] = pctrl->data->dedicated_pins[i].config;
  1227. pins[index].drv_data = &pin_data[index];
  1228. }
  1229. ret = devm_pinctrl_register_and_init(pctrl->dev, &pctrl->desc, pctrl,
  1230. &pctrl->pctl);
  1231. if (ret) {
  1232. dev_err(pctrl->dev, "pinctrl registration failed\n");
  1233. return ret;
  1234. }
  1235. ret = pinctrl_enable(pctrl->pctl);
  1236. if (ret) {
  1237. dev_err(pctrl->dev, "pinctrl enable failed\n");
  1238. return ret;
  1239. }
  1240. ret = rzg2l_gpio_register(pctrl);
  1241. if (ret) {
  1242. dev_err(pctrl->dev, "failed to add GPIO chip: %i\n", ret);
  1243. return ret;
  1244. }
  1245. return 0;
  1246. }
  1247. static void rzg2l_pinctrl_clk_disable(void *data)
  1248. {
  1249. clk_disable_unprepare(data);
  1250. }
  1251. static int rzg2l_pinctrl_probe(struct platform_device *pdev)
  1252. {
  1253. struct rzg2l_pinctrl *pctrl;
  1254. int ret;
  1255. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  1256. if (!pctrl)
  1257. return -ENOMEM;
  1258. pctrl->dev = &pdev->dev;
  1259. pctrl->data = of_device_get_match_data(&pdev->dev);
  1260. if (!pctrl->data)
  1261. return -EINVAL;
  1262. pctrl->base = devm_platform_ioremap_resource(pdev, 0);
  1263. if (IS_ERR(pctrl->base))
  1264. return PTR_ERR(pctrl->base);
  1265. pctrl->clk = devm_clk_get(pctrl->dev, NULL);
  1266. if (IS_ERR(pctrl->clk)) {
  1267. ret = PTR_ERR(pctrl->clk);
  1268. dev_err(pctrl->dev, "failed to get GPIO clk : %i\n", ret);
  1269. return ret;
  1270. }
  1271. spin_lock_init(&pctrl->lock);
  1272. spin_lock_init(&pctrl->bitmap_lock);
  1273. mutex_init(&pctrl->mutex);
  1274. platform_set_drvdata(pdev, pctrl);
  1275. ret = clk_prepare_enable(pctrl->clk);
  1276. if (ret) {
  1277. dev_err(pctrl->dev, "failed to enable GPIO clk: %i\n", ret);
  1278. return ret;
  1279. }
  1280. ret = devm_add_action_or_reset(&pdev->dev, rzg2l_pinctrl_clk_disable,
  1281. pctrl->clk);
  1282. if (ret) {
  1283. dev_err(pctrl->dev,
  1284. "failed to register GPIO clk disable action, %i\n",
  1285. ret);
  1286. return ret;
  1287. }
  1288. ret = rzg2l_pinctrl_register(pctrl);
  1289. if (ret)
  1290. return ret;
  1291. dev_info(pctrl->dev, "%s support registered\n", DRV_NAME);
  1292. return 0;
  1293. }
  1294. static struct rzg2l_pinctrl_data r9a07g043_data = {
  1295. .port_pins = rzg2l_gpio_names,
  1296. .port_pin_configs = r9a07g043_gpio_configs,
  1297. .n_ports = ARRAY_SIZE(r9a07g043_gpio_configs),
  1298. .dedicated_pins = rzg2l_dedicated_pins.common,
  1299. .n_port_pins = ARRAY_SIZE(r9a07g043_gpio_configs) * RZG2L_PINS_PER_PORT,
  1300. .n_dedicated_pins = ARRAY_SIZE(rzg2l_dedicated_pins.common),
  1301. };
  1302. static struct rzg2l_pinctrl_data r9a07g044_data = {
  1303. .port_pins = rzg2l_gpio_names,
  1304. .port_pin_configs = rzg2l_gpio_configs,
  1305. .n_ports = ARRAY_SIZE(rzg2l_gpio_configs),
  1306. .dedicated_pins = rzg2l_dedicated_pins.common,
  1307. .n_port_pins = ARRAY_SIZE(rzg2l_gpio_names),
  1308. .n_dedicated_pins = ARRAY_SIZE(rzg2l_dedicated_pins.common) +
  1309. ARRAY_SIZE(rzg2l_dedicated_pins.rzg2l_pins),
  1310. };
  1311. static const struct of_device_id rzg2l_pinctrl_of_table[] = {
  1312. {
  1313. .compatible = "renesas,r9a07g043-pinctrl",
  1314. .data = &r9a07g043_data,
  1315. },
  1316. {
  1317. .compatible = "renesas,r9a07g044-pinctrl",
  1318. .data = &r9a07g044_data,
  1319. },
  1320. { /* sentinel */ }
  1321. };
  1322. static struct platform_driver rzg2l_pinctrl_driver = {
  1323. .driver = {
  1324. .name = DRV_NAME,
  1325. .of_match_table = of_match_ptr(rzg2l_pinctrl_of_table),
  1326. },
  1327. .probe = rzg2l_pinctrl_probe,
  1328. };
  1329. static int __init rzg2l_pinctrl_init(void)
  1330. {
  1331. return platform_driver_register(&rzg2l_pinctrl_driver);
  1332. }
  1333. core_initcall(rzg2l_pinctrl_init);
  1334. MODULE_AUTHOR("Lad Prabhakar <[email protected]>");
  1335. MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/G2L family");
  1336. MODULE_LICENSE("GPL v2");