pinctrl-rzv2m.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas RZ/V2M Pin Control and GPIO driver core
  4. *
  5. * Based on:
  6. * Renesas RZ/G2L Pin Control and GPIO driver core
  7. *
  8. * Copyright (C) 2022 Renesas Electronics Corporation.
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/bitops.h>
  12. #include <linux/clk.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pinctrl/pinconf-generic.h>
  19. #include <linux/pinctrl/pinconf.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/spinlock.h>
  23. #include <dt-bindings/pinctrl/rzv2m-pinctrl.h>
  24. #include "../core.h"
  25. #include "../pinconf.h"
  26. #include "../pinmux.h"
  27. #define DRV_NAME "pinctrl-rzv2m"
  28. /*
  29. * Use 16 lower bits [15:0] for pin identifier
  30. * Use 16 higher bits [31:16] for pin mux function
  31. */
  32. #define MUX_PIN_ID_MASK GENMASK(15, 0)
  33. #define MUX_FUNC_MASK GENMASK(31, 16)
  34. #define MUX_FUNC(pinconf) FIELD_GET(MUX_FUNC_MASK, (pinconf))
  35. /* PIN capabilities */
  36. #define PIN_CFG_GRP_1_8V_2 1
  37. #define PIN_CFG_GRP_1_8V_3 2
  38. #define PIN_CFG_GRP_SWIO_1 3
  39. #define PIN_CFG_GRP_SWIO_2 4
  40. #define PIN_CFG_GRP_3_3V 5
  41. #define PIN_CFG_GRP_MASK GENMASK(2, 0)
  42. #define PIN_CFG_BIAS BIT(3)
  43. #define PIN_CFG_DRV BIT(4)
  44. #define PIN_CFG_SLEW BIT(5)
  45. #define RZV2M_MPXED_PIN_FUNCS (PIN_CFG_BIAS | \
  46. PIN_CFG_DRV | \
  47. PIN_CFG_SLEW)
  48. /*
  49. * n indicates number of pins in the port, a is the register index
  50. * and f is pin configuration capabilities supported.
  51. */
  52. #define RZV2M_GPIO_PORT_PACK(n, a, f) (((n) << 24) | ((a) << 16) | (f))
  53. #define RZV2M_GPIO_PORT_GET_PINCNT(x) FIELD_GET(GENMASK(31, 24), (x))
  54. #define RZV2M_GPIO_PORT_GET_INDEX(x) FIELD_GET(GENMASK(23, 16), (x))
  55. #define RZV2M_GPIO_PORT_GET_CFGS(x) FIELD_GET(GENMASK(15, 0), (x))
  56. #define RZV2M_DEDICATED_PORT_IDX 22
  57. /*
  58. * BIT(31) indicates dedicated pin, b is the register bits (b * 16)
  59. * and f is the pin configuration capabilities supported.
  60. */
  61. #define RZV2M_SINGLE_PIN BIT(31)
  62. #define RZV2M_SINGLE_PIN_PACK(b, f) (RZV2M_SINGLE_PIN | \
  63. ((RZV2M_DEDICATED_PORT_IDX) << 24) | \
  64. ((b) << 16) | (f))
  65. #define RZV2M_SINGLE_PIN_GET_PORT(x) FIELD_GET(GENMASK(30, 24), (x))
  66. #define RZV2M_SINGLE_PIN_GET_BIT(x) FIELD_GET(GENMASK(23, 16), (x))
  67. #define RZV2M_SINGLE_PIN_GET_CFGS(x) FIELD_GET(GENMASK(15, 0), (x))
  68. #define RZV2M_PIN_ID_TO_PORT(id) ((id) / RZV2M_PINS_PER_PORT)
  69. #define RZV2M_PIN_ID_TO_PIN(id) ((id) % RZV2M_PINS_PER_PORT)
  70. #define DO(n) (0x00 + (n) * 0x40)
  71. #define OE(n) (0x04 + (n) * 0x40)
  72. #define IE(n) (0x08 + (n) * 0x40)
  73. #define PFSEL(n) (0x10 + (n) * 0x40)
  74. #define DI(n) (0x20 + (n) * 0x40)
  75. #define PUPD(n) (0x24 + (n) * 0x40)
  76. #define DRV(n) ((n) < RZV2M_DEDICATED_PORT_IDX ? (0x28 + (n) * 0x40) \
  77. : 0x590)
  78. #define SR(n) ((n) < RZV2M_DEDICATED_PORT_IDX ? (0x2c + (n) * 0x40) \
  79. : 0x594)
  80. #define DI_MSK(n) (0x30 + (n) * 0x40)
  81. #define EN_MSK(n) (0x34 + (n) * 0x40)
  82. #define PFC_MASK 0x07
  83. #define PUPD_MASK 0x03
  84. #define DRV_MASK 0x03
  85. struct rzv2m_dedicated_configs {
  86. const char *name;
  87. u32 config;
  88. };
  89. struct rzv2m_pinctrl_data {
  90. const char * const *port_pins;
  91. const u32 *port_pin_configs;
  92. const struct rzv2m_dedicated_configs *dedicated_pins;
  93. unsigned int n_port_pins;
  94. unsigned int n_dedicated_pins;
  95. };
  96. struct rzv2m_pinctrl {
  97. struct pinctrl_dev *pctl;
  98. struct pinctrl_desc desc;
  99. struct pinctrl_pin_desc *pins;
  100. const struct rzv2m_pinctrl_data *data;
  101. void __iomem *base;
  102. struct device *dev;
  103. struct clk *clk;
  104. struct gpio_chip gpio_chip;
  105. struct pinctrl_gpio_range gpio_range;
  106. spinlock_t lock; /* lock read/write registers */
  107. struct mutex mutex; /* serialize adding groups and functions */
  108. };
  109. static const unsigned int drv_1_8V_group2_uA[] = { 1800, 3800, 7800, 11000 };
  110. static const unsigned int drv_1_8V_group3_uA[] = { 1600, 3200, 6400, 9600 };
  111. static const unsigned int drv_SWIO_group2_3_3V_uA[] = { 9000, 11000, 13000, 18000 };
  112. static const unsigned int drv_3_3V_group_uA[] = { 2000, 4000, 8000, 12000 };
  113. /* Helper for registers that have a write enable bit in the upper word */
  114. static void rzv2m_writel_we(void __iomem *addr, u8 shift, u8 value)
  115. {
  116. writel((BIT(16) | value) << shift, addr);
  117. }
  118. static void rzv2m_pinctrl_set_pfc_mode(struct rzv2m_pinctrl *pctrl,
  119. u8 port, u8 pin, u8 func)
  120. {
  121. void __iomem *addr;
  122. /* Mask input/output */
  123. rzv2m_writel_we(pctrl->base + DI_MSK(port), pin, 1);
  124. rzv2m_writel_we(pctrl->base + EN_MSK(port), pin, 1);
  125. /* Select the function and set the write enable bits */
  126. addr = pctrl->base + PFSEL(port) + (pin / 4) * 4;
  127. writel(((PFC_MASK << 16) | func) << ((pin % 4) * 4), addr);
  128. /* Unmask input/output */
  129. rzv2m_writel_we(pctrl->base + EN_MSK(port), pin, 0);
  130. rzv2m_writel_we(pctrl->base + DI_MSK(port), pin, 0);
  131. };
  132. static int rzv2m_pinctrl_set_mux(struct pinctrl_dev *pctldev,
  133. unsigned int func_selector,
  134. unsigned int group_selector)
  135. {
  136. struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  137. struct function_desc *func;
  138. unsigned int i, *psel_val;
  139. struct group_desc *group;
  140. int *pins;
  141. func = pinmux_generic_get_function(pctldev, func_selector);
  142. if (!func)
  143. return -EINVAL;
  144. group = pinctrl_generic_get_group(pctldev, group_selector);
  145. if (!group)
  146. return -EINVAL;
  147. psel_val = func->data;
  148. pins = group->pins;
  149. for (i = 0; i < group->num_pins; i++) {
  150. dev_dbg(pctrl->dev, "port:%u pin: %u PSEL:%u\n",
  151. RZV2M_PIN_ID_TO_PORT(pins[i]), RZV2M_PIN_ID_TO_PIN(pins[i]),
  152. psel_val[i]);
  153. rzv2m_pinctrl_set_pfc_mode(pctrl, RZV2M_PIN_ID_TO_PORT(pins[i]),
  154. RZV2M_PIN_ID_TO_PIN(pins[i]), psel_val[i]);
  155. }
  156. return 0;
  157. };
  158. static int rzv2m_map_add_config(struct pinctrl_map *map,
  159. const char *group_or_pin,
  160. enum pinctrl_map_type type,
  161. unsigned long *configs,
  162. unsigned int num_configs)
  163. {
  164. unsigned long *cfgs;
  165. cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
  166. GFP_KERNEL);
  167. if (!cfgs)
  168. return -ENOMEM;
  169. map->type = type;
  170. map->data.configs.group_or_pin = group_or_pin;
  171. map->data.configs.configs = cfgs;
  172. map->data.configs.num_configs = num_configs;
  173. return 0;
  174. }
  175. static int rzv2m_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  176. struct device_node *np,
  177. struct device_node *parent,
  178. struct pinctrl_map **map,
  179. unsigned int *num_maps,
  180. unsigned int *index)
  181. {
  182. struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  183. struct pinctrl_map *maps = *map;
  184. unsigned int nmaps = *num_maps;
  185. unsigned long *configs = NULL;
  186. unsigned int *pins, *psel_val;
  187. unsigned int num_pinmux = 0;
  188. unsigned int idx = *index;
  189. unsigned int num_pins, i;
  190. unsigned int num_configs;
  191. struct property *pinmux;
  192. struct property *prop;
  193. int ret, gsel, fsel;
  194. const char **pin_fn;
  195. const char *name;
  196. const char *pin;
  197. pinmux = of_find_property(np, "pinmux", NULL);
  198. if (pinmux)
  199. num_pinmux = pinmux->length / sizeof(u32);
  200. ret = of_property_count_strings(np, "pins");
  201. if (ret == -EINVAL) {
  202. num_pins = 0;
  203. } else if (ret < 0) {
  204. dev_err(pctrl->dev, "Invalid pins list in DT\n");
  205. return ret;
  206. } else {
  207. num_pins = ret;
  208. }
  209. if (!num_pinmux && !num_pins)
  210. return 0;
  211. if (num_pinmux && num_pins) {
  212. dev_err(pctrl->dev,
  213. "DT node must contain either a pinmux or pins and not both\n");
  214. return -EINVAL;
  215. }
  216. ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
  217. if (ret < 0)
  218. return ret;
  219. if (num_pins && !num_configs) {
  220. dev_err(pctrl->dev, "DT node must contain a config\n");
  221. ret = -ENODEV;
  222. goto done;
  223. }
  224. if (num_pinmux)
  225. nmaps += 1;
  226. if (num_pins)
  227. nmaps += num_pins;
  228. maps = krealloc_array(maps, nmaps, sizeof(*maps), GFP_KERNEL);
  229. if (!maps) {
  230. ret = -ENOMEM;
  231. goto done;
  232. }
  233. *map = maps;
  234. *num_maps = nmaps;
  235. if (num_pins) {
  236. of_property_for_each_string(np, "pins", prop, pin) {
  237. ret = rzv2m_map_add_config(&maps[idx], pin,
  238. PIN_MAP_TYPE_CONFIGS_PIN,
  239. configs, num_configs);
  240. if (ret < 0)
  241. goto done;
  242. idx++;
  243. }
  244. ret = 0;
  245. goto done;
  246. }
  247. pins = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*pins), GFP_KERNEL);
  248. psel_val = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*psel_val),
  249. GFP_KERNEL);
  250. pin_fn = devm_kzalloc(pctrl->dev, sizeof(*pin_fn), GFP_KERNEL);
  251. if (!pins || !psel_val || !pin_fn) {
  252. ret = -ENOMEM;
  253. goto done;
  254. }
  255. /* Collect pin locations and mux settings from DT properties */
  256. for (i = 0; i < num_pinmux; ++i) {
  257. u32 value;
  258. ret = of_property_read_u32_index(np, "pinmux", i, &value);
  259. if (ret)
  260. goto done;
  261. pins[i] = value & MUX_PIN_ID_MASK;
  262. psel_val[i] = MUX_FUNC(value);
  263. }
  264. if (parent) {
  265. name = devm_kasprintf(pctrl->dev, GFP_KERNEL, "%pOFn.%pOFn",
  266. parent, np);
  267. if (!name) {
  268. ret = -ENOMEM;
  269. goto done;
  270. }
  271. } else {
  272. name = np->name;
  273. }
  274. mutex_lock(&pctrl->mutex);
  275. /* Register a single pin group listing all the pins we read from DT */
  276. gsel = pinctrl_generic_add_group(pctldev, name, pins, num_pinmux, NULL);
  277. if (gsel < 0) {
  278. ret = gsel;
  279. goto unlock;
  280. }
  281. /*
  282. * Register a single group function where the 'data' is an array PSEL
  283. * register values read from DT.
  284. */
  285. pin_fn[0] = name;
  286. fsel = pinmux_generic_add_function(pctldev, name, pin_fn, 1, psel_val);
  287. if (fsel < 0) {
  288. ret = fsel;
  289. goto remove_group;
  290. }
  291. mutex_unlock(&pctrl->mutex);
  292. maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
  293. maps[idx].data.mux.group = name;
  294. maps[idx].data.mux.function = name;
  295. idx++;
  296. dev_dbg(pctrl->dev, "Parsed %pOF with %d pins\n", np, num_pinmux);
  297. ret = 0;
  298. goto done;
  299. remove_group:
  300. pinctrl_generic_remove_group(pctldev, gsel);
  301. unlock:
  302. mutex_unlock(&pctrl->mutex);
  303. done:
  304. *index = idx;
  305. kfree(configs);
  306. return ret;
  307. }
  308. static void rzv2m_dt_free_map(struct pinctrl_dev *pctldev,
  309. struct pinctrl_map *map,
  310. unsigned int num_maps)
  311. {
  312. unsigned int i;
  313. if (!map)
  314. return;
  315. for (i = 0; i < num_maps; ++i) {
  316. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
  317. map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  318. kfree(map[i].data.configs.configs);
  319. }
  320. kfree(map);
  321. }
  322. static int rzv2m_dt_node_to_map(struct pinctrl_dev *pctldev,
  323. struct device_node *np,
  324. struct pinctrl_map **map,
  325. unsigned int *num_maps)
  326. {
  327. struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  328. struct device_node *child;
  329. unsigned int index;
  330. int ret;
  331. *map = NULL;
  332. *num_maps = 0;
  333. index = 0;
  334. for_each_child_of_node(np, child) {
  335. ret = rzv2m_dt_subnode_to_map(pctldev, child, np, map,
  336. num_maps, &index);
  337. if (ret < 0) {
  338. of_node_put(child);
  339. goto done;
  340. }
  341. }
  342. if (*num_maps == 0) {
  343. ret = rzv2m_dt_subnode_to_map(pctldev, np, NULL, map,
  344. num_maps, &index);
  345. if (ret < 0)
  346. goto done;
  347. }
  348. if (*num_maps)
  349. return 0;
  350. dev_err(pctrl->dev, "no mapping found in node %pOF\n", np);
  351. ret = -EINVAL;
  352. done:
  353. if (ret < 0)
  354. rzv2m_dt_free_map(pctldev, *map, *num_maps);
  355. return ret;
  356. }
  357. static int rzv2m_validate_gpio_pin(struct rzv2m_pinctrl *pctrl,
  358. u32 cfg, u32 port, u8 bit)
  359. {
  360. u8 pincount = RZV2M_GPIO_PORT_GET_PINCNT(cfg);
  361. u32 port_index = RZV2M_GPIO_PORT_GET_INDEX(cfg);
  362. u32 data;
  363. if (bit >= pincount || port >= pctrl->data->n_port_pins)
  364. return -EINVAL;
  365. data = pctrl->data->port_pin_configs[port];
  366. if (port_index != RZV2M_GPIO_PORT_GET_INDEX(data))
  367. return -EINVAL;
  368. return 0;
  369. }
  370. static void rzv2m_rmw_pin_config(struct rzv2m_pinctrl *pctrl, u32 offset,
  371. u8 shift, u32 mask, u32 val)
  372. {
  373. void __iomem *addr = pctrl->base + offset;
  374. unsigned long flags;
  375. u32 reg;
  376. spin_lock_irqsave(&pctrl->lock, flags);
  377. reg = readl(addr) & ~(mask << shift);
  378. writel(reg | (val << shift), addr);
  379. spin_unlock_irqrestore(&pctrl->lock, flags);
  380. }
  381. static int rzv2m_pinctrl_pinconf_get(struct pinctrl_dev *pctldev,
  382. unsigned int _pin,
  383. unsigned long *config)
  384. {
  385. struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  386. enum pin_config_param param = pinconf_to_config_param(*config);
  387. const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
  388. unsigned int *pin_data = pin->drv_data;
  389. unsigned int arg = 0;
  390. u32 port;
  391. u32 cfg;
  392. u8 bit;
  393. u32 val;
  394. if (!pin_data)
  395. return -EINVAL;
  396. if (*pin_data & RZV2M_SINGLE_PIN) {
  397. port = RZV2M_SINGLE_PIN_GET_PORT(*pin_data);
  398. cfg = RZV2M_SINGLE_PIN_GET_CFGS(*pin_data);
  399. bit = RZV2M_SINGLE_PIN_GET_BIT(*pin_data);
  400. } else {
  401. cfg = RZV2M_GPIO_PORT_GET_CFGS(*pin_data);
  402. port = RZV2M_PIN_ID_TO_PORT(_pin);
  403. bit = RZV2M_PIN_ID_TO_PIN(_pin);
  404. if (rzv2m_validate_gpio_pin(pctrl, *pin_data, RZV2M_PIN_ID_TO_PORT(_pin), bit))
  405. return -EINVAL;
  406. }
  407. switch (param) {
  408. case PIN_CONFIG_BIAS_DISABLE:
  409. case PIN_CONFIG_BIAS_PULL_UP:
  410. case PIN_CONFIG_BIAS_PULL_DOWN: {
  411. enum pin_config_param bias;
  412. if (!(cfg & PIN_CFG_BIAS))
  413. return -EINVAL;
  414. /* PUPD uses 2-bits per pin */
  415. bit *= 2;
  416. switch ((readl(pctrl->base + PUPD(port)) >> bit) & PUPD_MASK) {
  417. case 0:
  418. bias = PIN_CONFIG_BIAS_PULL_DOWN;
  419. break;
  420. case 2:
  421. bias = PIN_CONFIG_BIAS_PULL_UP;
  422. break;
  423. default:
  424. bias = PIN_CONFIG_BIAS_DISABLE;
  425. }
  426. if (bias != param)
  427. return -EINVAL;
  428. break;
  429. }
  430. case PIN_CONFIG_DRIVE_STRENGTH_UA:
  431. if (!(cfg & PIN_CFG_DRV))
  432. return -EINVAL;
  433. /* DRV uses 2-bits per pin */
  434. bit *= 2;
  435. val = (readl(pctrl->base + DRV(port)) >> bit) & DRV_MASK;
  436. switch (cfg & PIN_CFG_GRP_MASK) {
  437. case PIN_CFG_GRP_1_8V_2:
  438. arg = drv_1_8V_group2_uA[val];
  439. break;
  440. case PIN_CFG_GRP_1_8V_3:
  441. arg = drv_1_8V_group3_uA[val];
  442. break;
  443. case PIN_CFG_GRP_SWIO_2:
  444. arg = drv_SWIO_group2_3_3V_uA[val];
  445. break;
  446. case PIN_CFG_GRP_SWIO_1:
  447. case PIN_CFG_GRP_3_3V:
  448. arg = drv_3_3V_group_uA[val];
  449. break;
  450. default:
  451. return -EINVAL;
  452. }
  453. break;
  454. case PIN_CONFIG_SLEW_RATE:
  455. if (!(cfg & PIN_CFG_SLEW))
  456. return -EINVAL;
  457. arg = readl(pctrl->base + SR(port)) & BIT(bit);
  458. break;
  459. default:
  460. return -ENOTSUPP;
  461. }
  462. *config = pinconf_to_config_packed(param, arg);
  463. return 0;
  464. };
  465. static int rzv2m_pinctrl_pinconf_set(struct pinctrl_dev *pctldev,
  466. unsigned int _pin,
  467. unsigned long *_configs,
  468. unsigned int num_configs)
  469. {
  470. struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  471. const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
  472. unsigned int *pin_data = pin->drv_data;
  473. enum pin_config_param param;
  474. u32 port;
  475. unsigned int i;
  476. u32 cfg;
  477. u8 bit;
  478. u32 val;
  479. if (!pin_data)
  480. return -EINVAL;
  481. if (*pin_data & RZV2M_SINGLE_PIN) {
  482. port = RZV2M_SINGLE_PIN_GET_PORT(*pin_data);
  483. cfg = RZV2M_SINGLE_PIN_GET_CFGS(*pin_data);
  484. bit = RZV2M_SINGLE_PIN_GET_BIT(*pin_data);
  485. } else {
  486. cfg = RZV2M_GPIO_PORT_GET_CFGS(*pin_data);
  487. port = RZV2M_PIN_ID_TO_PORT(_pin);
  488. bit = RZV2M_PIN_ID_TO_PIN(_pin);
  489. if (rzv2m_validate_gpio_pin(pctrl, *pin_data, RZV2M_PIN_ID_TO_PORT(_pin), bit))
  490. return -EINVAL;
  491. }
  492. for (i = 0; i < num_configs; i++) {
  493. param = pinconf_to_config_param(_configs[i]);
  494. switch (param) {
  495. case PIN_CONFIG_BIAS_DISABLE:
  496. case PIN_CONFIG_BIAS_PULL_UP:
  497. case PIN_CONFIG_BIAS_PULL_DOWN:
  498. if (!(cfg & PIN_CFG_BIAS))
  499. return -EINVAL;
  500. /* PUPD uses 2-bits per pin */
  501. bit *= 2;
  502. switch (param) {
  503. case PIN_CONFIG_BIAS_PULL_DOWN:
  504. val = 0;
  505. break;
  506. case PIN_CONFIG_BIAS_PULL_UP:
  507. val = 2;
  508. break;
  509. default:
  510. val = 1;
  511. }
  512. rzv2m_rmw_pin_config(pctrl, PUPD(port), bit, PUPD_MASK, val);
  513. break;
  514. case PIN_CONFIG_DRIVE_STRENGTH_UA: {
  515. unsigned int arg = pinconf_to_config_argument(_configs[i]);
  516. const unsigned int *drv_strengths;
  517. unsigned int index;
  518. if (!(cfg & PIN_CFG_DRV))
  519. return -EINVAL;
  520. switch (cfg & PIN_CFG_GRP_MASK) {
  521. case PIN_CFG_GRP_1_8V_2:
  522. drv_strengths = drv_1_8V_group2_uA;
  523. break;
  524. case PIN_CFG_GRP_1_8V_3:
  525. drv_strengths = drv_1_8V_group3_uA;
  526. break;
  527. case PIN_CFG_GRP_SWIO_2:
  528. drv_strengths = drv_SWIO_group2_3_3V_uA;
  529. break;
  530. case PIN_CFG_GRP_SWIO_1:
  531. case PIN_CFG_GRP_3_3V:
  532. drv_strengths = drv_3_3V_group_uA;
  533. break;
  534. default:
  535. return -EINVAL;
  536. }
  537. for (index = 0; index < 4; index++) {
  538. if (arg == drv_strengths[index])
  539. break;
  540. }
  541. if (index >= 4)
  542. return -EINVAL;
  543. /* DRV uses 2-bits per pin */
  544. bit *= 2;
  545. rzv2m_rmw_pin_config(pctrl, DRV(port), bit, DRV_MASK, index);
  546. break;
  547. }
  548. case PIN_CONFIG_SLEW_RATE: {
  549. unsigned int arg = pinconf_to_config_argument(_configs[i]);
  550. if (!(cfg & PIN_CFG_SLEW))
  551. return -EINVAL;
  552. rzv2m_writel_we(pctrl->base + SR(port), bit, !arg);
  553. break;
  554. }
  555. default:
  556. return -EOPNOTSUPP;
  557. }
  558. }
  559. return 0;
  560. }
  561. static int rzv2m_pinctrl_pinconf_group_set(struct pinctrl_dev *pctldev,
  562. unsigned int group,
  563. unsigned long *configs,
  564. unsigned int num_configs)
  565. {
  566. const unsigned int *pins;
  567. unsigned int i, npins;
  568. int ret;
  569. ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
  570. if (ret)
  571. return ret;
  572. for (i = 0; i < npins; i++) {
  573. ret = rzv2m_pinctrl_pinconf_set(pctldev, pins[i], configs,
  574. num_configs);
  575. if (ret)
  576. return ret;
  577. }
  578. return 0;
  579. };
  580. static int rzv2m_pinctrl_pinconf_group_get(struct pinctrl_dev *pctldev,
  581. unsigned int group,
  582. unsigned long *config)
  583. {
  584. const unsigned int *pins;
  585. unsigned int i, npins, prev_config = 0;
  586. int ret;
  587. ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
  588. if (ret)
  589. return ret;
  590. for (i = 0; i < npins; i++) {
  591. ret = rzv2m_pinctrl_pinconf_get(pctldev, pins[i], config);
  592. if (ret)
  593. return ret;
  594. /* Check config matches previous pins */
  595. if (i && prev_config != *config)
  596. return -EOPNOTSUPP;
  597. prev_config = *config;
  598. }
  599. return 0;
  600. };
  601. static const struct pinctrl_ops rzv2m_pinctrl_pctlops = {
  602. .get_groups_count = pinctrl_generic_get_group_count,
  603. .get_group_name = pinctrl_generic_get_group_name,
  604. .get_group_pins = pinctrl_generic_get_group_pins,
  605. .dt_node_to_map = rzv2m_dt_node_to_map,
  606. .dt_free_map = rzv2m_dt_free_map,
  607. };
  608. static const struct pinmux_ops rzv2m_pinctrl_pmxops = {
  609. .get_functions_count = pinmux_generic_get_function_count,
  610. .get_function_name = pinmux_generic_get_function_name,
  611. .get_function_groups = pinmux_generic_get_function_groups,
  612. .set_mux = rzv2m_pinctrl_set_mux,
  613. .strict = true,
  614. };
  615. static const struct pinconf_ops rzv2m_pinctrl_confops = {
  616. .is_generic = true,
  617. .pin_config_get = rzv2m_pinctrl_pinconf_get,
  618. .pin_config_set = rzv2m_pinctrl_pinconf_set,
  619. .pin_config_group_set = rzv2m_pinctrl_pinconf_group_set,
  620. .pin_config_group_get = rzv2m_pinctrl_pinconf_group_get,
  621. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  622. };
  623. static int rzv2m_gpio_request(struct gpio_chip *chip, unsigned int offset)
  624. {
  625. struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
  626. u32 port = RZV2M_PIN_ID_TO_PORT(offset);
  627. u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
  628. int ret;
  629. ret = pinctrl_gpio_request(chip->base + offset);
  630. if (ret)
  631. return ret;
  632. rzv2m_pinctrl_set_pfc_mode(pctrl, port, bit, 0);
  633. return 0;
  634. }
  635. static void rzv2m_gpio_set_direction(struct rzv2m_pinctrl *pctrl, u32 port,
  636. u8 bit, bool output)
  637. {
  638. rzv2m_writel_we(pctrl->base + OE(port), bit, output);
  639. rzv2m_writel_we(pctrl->base + IE(port), bit, !output);
  640. }
  641. static int rzv2m_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  642. {
  643. struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
  644. u32 port = RZV2M_PIN_ID_TO_PORT(offset);
  645. u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
  646. if (!(readl(pctrl->base + IE(port)) & BIT(bit)))
  647. return GPIO_LINE_DIRECTION_OUT;
  648. return GPIO_LINE_DIRECTION_IN;
  649. }
  650. static int rzv2m_gpio_direction_input(struct gpio_chip *chip,
  651. unsigned int offset)
  652. {
  653. struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
  654. u32 port = RZV2M_PIN_ID_TO_PORT(offset);
  655. u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
  656. rzv2m_gpio_set_direction(pctrl, port, bit, false);
  657. return 0;
  658. }
  659. static void rzv2m_gpio_set(struct gpio_chip *chip, unsigned int offset,
  660. int value)
  661. {
  662. struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
  663. u32 port = RZV2M_PIN_ID_TO_PORT(offset);
  664. u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
  665. rzv2m_writel_we(pctrl->base + DO(port), bit, !!value);
  666. }
  667. static int rzv2m_gpio_direction_output(struct gpio_chip *chip,
  668. unsigned int offset, int value)
  669. {
  670. struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
  671. u32 port = RZV2M_PIN_ID_TO_PORT(offset);
  672. u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
  673. rzv2m_gpio_set(chip, offset, value);
  674. rzv2m_gpio_set_direction(pctrl, port, bit, true);
  675. return 0;
  676. }
  677. static int rzv2m_gpio_get(struct gpio_chip *chip, unsigned int offset)
  678. {
  679. struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
  680. u32 port = RZV2M_PIN_ID_TO_PORT(offset);
  681. u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
  682. int direction = rzv2m_gpio_get_direction(chip, offset);
  683. if (direction == GPIO_LINE_DIRECTION_IN)
  684. return !!(readl(pctrl->base + DI(port)) & BIT(bit));
  685. else
  686. return !!(readl(pctrl->base + DO(port)) & BIT(bit));
  687. }
  688. static void rzv2m_gpio_free(struct gpio_chip *chip, unsigned int offset)
  689. {
  690. pinctrl_gpio_free(chip->base + offset);
  691. /*
  692. * Set the GPIO as an input to ensure that the next GPIO request won't
  693. * drive the GPIO pin as an output.
  694. */
  695. rzv2m_gpio_direction_input(chip, offset);
  696. }
  697. static const char * const rzv2m_gpio_names[] = {
  698. "P0_0", "P0_1", "P0_2", "P0_3", "P0_4", "P0_5", "P0_6", "P0_7",
  699. "P0_8", "P0_9", "P0_10", "P0_11", "P0_12", "P0_13", "P0_14", "P0_15",
  700. "P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7",
  701. "P1_8", "P1_9", "P1_10", "P1_11", "P1_12", "P1_13", "P1_14", "P1_15",
  702. "P2_0", "P2_1", "P2_2", "P2_3", "P2_4", "P2_5", "P2_6", "P2_7",
  703. "P2_8", "P2_9", "P2_10", "P2_11", "P2_12", "P2_13", "P2_14", "P2_15",
  704. "P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7",
  705. "P3_8", "P3_9", "P3_10", "P3_11", "P3_12", "P3_13", "P3_14", "P3_15",
  706. "P4_0", "P4_1", "P4_2", "P4_3", "P4_4", "P4_5", "P4_6", "P4_7",
  707. "P4_8", "P4_9", "P4_10", "P4_11", "P4_12", "P4_13", "P4_14", "P4_15",
  708. "P5_0", "P5_1", "P5_2", "P5_3", "P5_4", "P5_5", "P5_6", "P5_7",
  709. "P5_8", "P5_9", "P5_10", "P5_11", "P5_12", "P5_13", "P5_14", "P5_15",
  710. "P6_0", "P6_1", "P6_2", "P6_3", "P6_4", "P6_5", "P6_6", "P6_7",
  711. "P6_8", "P6_9", "P6_10", "P6_11", "P6_12", "P6_13", "P6_14", "P6_15",
  712. "P7_0", "P7_1", "P7_2", "P7_3", "P7_4", "P7_5", "P7_6", "P7_7",
  713. "P7_8", "P7_9", "P7_10", "P7_11", "P7_12", "P7_13", "P7_14", "P7_15",
  714. "P8_0", "P8_1", "P8_2", "P8_3", "P8_4", "P8_5", "P8_6", "P8_7",
  715. "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
  716. "P9_0", "P9_1", "P9_2", "P9_3", "P9_4", "P9_5", "P9_6", "P9_7",
  717. "P9_8", "P9_9", "P9_10", "P9_11", "P9_12", "P9_13", "P9_14", "P9_15",
  718. "P10_0", "P10_1", "P10_2", "P10_3", "P10_4", "P10_5", "P10_6", "P10_7",
  719. "P10_8", "P10_9", "P10_10", "P10_11", "P10_12", "P10_13", "P10_14", "P10_15",
  720. "P11_0", "P11_1", "P11_2", "P11_3", "P11_4", "P11_5", "P11_6", "P11_7",
  721. "P11_8", "P11_9", "P11_10", "P11_11", "P11_12", "P11_13", "P11_14", "P11_15",
  722. "P12_0", "P12_1", "P12_2", "P12_3", "P12_4", "P12_5", "P12_6", "P12_7",
  723. "P12_8", "P12_9", "P12_10", "P12_11", "P12_12", "P12_13", "P12_14", "P12_15",
  724. "P13_0", "P13_1", "P13_2", "P13_3", "P13_4", "P13_5", "P13_6", "P13_7",
  725. "P13_8", "P13_9", "P13_10", "P13_11", "P13_12", "P13_13", "P13_14", "P13_15",
  726. "P14_0", "P14_1", "P14_2", "P14_3", "P14_4", "P14_5", "P14_6", "P14_7",
  727. "P14_8", "P14_9", "P14_10", "P14_11", "P14_12", "P14_13", "P14_14", "P14_15",
  728. "P15_0", "P15_1", "P15_2", "P15_3", "P15_4", "P15_5", "P15_6", "P15_7",
  729. "P15_8", "P15_9", "P15_10", "P15_11", "P15_12", "P15_13", "P15_14", "P15_15",
  730. "P16_0", "P16_1", "P16_2", "P16_3", "P16_4", "P16_5", "P16_6", "P16_7",
  731. "P16_8", "P16_9", "P16_10", "P16_11", "P16_12", "P16_13", "P16_14", "P16_15",
  732. "P17_0", "P17_1", "P17_2", "P17_3", "P17_4", "P17_5", "P17_6", "P17_7",
  733. "P17_8", "P17_9", "P17_10", "P17_11", "P17_12", "P17_13", "P17_14", "P17_15",
  734. "P18_0", "P18_1", "P18_2", "P18_3", "P18_4", "P18_5", "P18_6", "P18_7",
  735. "P18_8", "P18_9", "P18_10", "P18_11", "P18_12", "P18_13", "P18_14", "P18_15",
  736. "P19_0", "P19_1", "P19_2", "P19_3", "P19_4", "P19_5", "P19_6", "P19_7",
  737. "P19_8", "P19_9", "P19_10", "P19_11", "P19_12", "P19_13", "P19_14", "P19_15",
  738. "P20_0", "P20_1", "P20_2", "P20_3", "P20_4", "P20_5", "P20_6", "P20_7",
  739. "P20_8", "P20_9", "P20_10", "P20_11", "P20_12", "P20_13", "P20_14", "P20_15",
  740. "P21_0", "P21_1", "P21_2", "P21_3", "P21_4", "P21_5", "P21_6", "P21_7",
  741. "P21_8", "P21_9", "P21_10", "P21_11", "P21_12", "P21_13", "P21_14", "P21_15",
  742. };
  743. static const u32 rzv2m_gpio_configs[] = {
  744. RZV2M_GPIO_PORT_PACK(14, 0, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
  745. RZV2M_GPIO_PORT_PACK(16, 1, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
  746. RZV2M_GPIO_PORT_PACK(8, 2, PIN_CFG_GRP_1_8V_3 | RZV2M_MPXED_PIN_FUNCS),
  747. RZV2M_GPIO_PORT_PACK(16, 3, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
  748. RZV2M_GPIO_PORT_PACK(8, 4, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
  749. RZV2M_GPIO_PORT_PACK(4, 5, PIN_CFG_GRP_1_8V_3 | RZV2M_MPXED_PIN_FUNCS),
  750. RZV2M_GPIO_PORT_PACK(12, 6, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
  751. RZV2M_GPIO_PORT_PACK(6, 7, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
  752. RZV2M_GPIO_PORT_PACK(8, 8, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
  753. RZV2M_GPIO_PORT_PACK(8, 9, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
  754. RZV2M_GPIO_PORT_PACK(9, 10, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
  755. RZV2M_GPIO_PORT_PACK(9, 11, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
  756. RZV2M_GPIO_PORT_PACK(4, 12, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
  757. RZV2M_GPIO_PORT_PACK(12, 13, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
  758. RZV2M_GPIO_PORT_PACK(8, 14, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
  759. RZV2M_GPIO_PORT_PACK(16, 15, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
  760. RZV2M_GPIO_PORT_PACK(14, 16, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
  761. RZV2M_GPIO_PORT_PACK(1, 17, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
  762. RZV2M_GPIO_PORT_PACK(0, 18, 0),
  763. RZV2M_GPIO_PORT_PACK(0, 19, 0),
  764. RZV2M_GPIO_PORT_PACK(3, 20, PIN_CFG_GRP_1_8V_2 | PIN_CFG_DRV),
  765. RZV2M_GPIO_PORT_PACK(1, 21, PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW),
  766. };
  767. static const struct rzv2m_dedicated_configs rzv2m_dedicated_pins[] = {
  768. { "NAWPN", RZV2M_SINGLE_PIN_PACK(0,
  769. (PIN_CFG_GRP_SWIO_2 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
  770. { "IM0CLK", RZV2M_SINGLE_PIN_PACK(1,
  771. (PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
  772. { "IM1CLK", RZV2M_SINGLE_PIN_PACK(2,
  773. (PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
  774. { "DETDO", RZV2M_SINGLE_PIN_PACK(5,
  775. (PIN_CFG_GRP_1_8V_3 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
  776. { "DETMS", RZV2M_SINGLE_PIN_PACK(6,
  777. (PIN_CFG_GRP_1_8V_3 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
  778. { "PCRSTOUTB", RZV2M_SINGLE_PIN_PACK(12,
  779. (PIN_CFG_GRP_3_3V | PIN_CFG_DRV | PIN_CFG_SLEW)) },
  780. { "USPWEN", RZV2M_SINGLE_PIN_PACK(14,
  781. (PIN_CFG_GRP_3_3V | PIN_CFG_DRV | PIN_CFG_SLEW)) },
  782. };
  783. static int rzv2m_gpio_register(struct rzv2m_pinctrl *pctrl)
  784. {
  785. struct device_node *np = pctrl->dev->of_node;
  786. struct gpio_chip *chip = &pctrl->gpio_chip;
  787. const char *name = dev_name(pctrl->dev);
  788. struct of_phandle_args of_args;
  789. int ret;
  790. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &of_args);
  791. if (ret) {
  792. dev_err(pctrl->dev, "Unable to parse gpio-ranges\n");
  793. return ret;
  794. }
  795. if (of_args.args[0] != 0 || of_args.args[1] != 0 ||
  796. of_args.args[2] != pctrl->data->n_port_pins) {
  797. dev_err(pctrl->dev, "gpio-ranges does not match selected SOC\n");
  798. return -EINVAL;
  799. }
  800. chip->names = pctrl->data->port_pins;
  801. chip->request = rzv2m_gpio_request;
  802. chip->free = rzv2m_gpio_free;
  803. chip->get_direction = rzv2m_gpio_get_direction;
  804. chip->direction_input = rzv2m_gpio_direction_input;
  805. chip->direction_output = rzv2m_gpio_direction_output;
  806. chip->get = rzv2m_gpio_get;
  807. chip->set = rzv2m_gpio_set;
  808. chip->label = name;
  809. chip->parent = pctrl->dev;
  810. chip->owner = THIS_MODULE;
  811. chip->base = -1;
  812. chip->ngpio = of_args.args[2];
  813. pctrl->gpio_range.id = 0;
  814. pctrl->gpio_range.pin_base = 0;
  815. pctrl->gpio_range.base = 0;
  816. pctrl->gpio_range.npins = chip->ngpio;
  817. pctrl->gpio_range.name = chip->label;
  818. pctrl->gpio_range.gc = chip;
  819. ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl);
  820. if (ret) {
  821. dev_err(pctrl->dev, "failed to add GPIO controller\n");
  822. return ret;
  823. }
  824. dev_dbg(pctrl->dev, "Registered gpio controller\n");
  825. return 0;
  826. }
  827. static int rzv2m_pinctrl_register(struct rzv2m_pinctrl *pctrl)
  828. {
  829. struct pinctrl_pin_desc *pins;
  830. unsigned int i, j;
  831. u32 *pin_data;
  832. int ret;
  833. pctrl->desc.name = DRV_NAME;
  834. pctrl->desc.npins = pctrl->data->n_port_pins + pctrl->data->n_dedicated_pins;
  835. pctrl->desc.pctlops = &rzv2m_pinctrl_pctlops;
  836. pctrl->desc.pmxops = &rzv2m_pinctrl_pmxops;
  837. pctrl->desc.confops = &rzv2m_pinctrl_confops;
  838. pctrl->desc.owner = THIS_MODULE;
  839. pins = devm_kcalloc(pctrl->dev, pctrl->desc.npins, sizeof(*pins), GFP_KERNEL);
  840. if (!pins)
  841. return -ENOMEM;
  842. pin_data = devm_kcalloc(pctrl->dev, pctrl->desc.npins,
  843. sizeof(*pin_data), GFP_KERNEL);
  844. if (!pin_data)
  845. return -ENOMEM;
  846. pctrl->pins = pins;
  847. pctrl->desc.pins = pins;
  848. for (i = 0, j = 0; i < pctrl->data->n_port_pins; i++) {
  849. pins[i].number = i;
  850. pins[i].name = pctrl->data->port_pins[i];
  851. if (i && !(i % RZV2M_PINS_PER_PORT))
  852. j++;
  853. pin_data[i] = pctrl->data->port_pin_configs[j];
  854. pins[i].drv_data = &pin_data[i];
  855. }
  856. for (i = 0; i < pctrl->data->n_dedicated_pins; i++) {
  857. unsigned int index = pctrl->data->n_port_pins + i;
  858. pins[index].number = index;
  859. pins[index].name = pctrl->data->dedicated_pins[i].name;
  860. pin_data[index] = pctrl->data->dedicated_pins[i].config;
  861. pins[index].drv_data = &pin_data[index];
  862. }
  863. ret = devm_pinctrl_register_and_init(pctrl->dev, &pctrl->desc, pctrl,
  864. &pctrl->pctl);
  865. if (ret) {
  866. dev_err(pctrl->dev, "pinctrl registration failed\n");
  867. return ret;
  868. }
  869. ret = pinctrl_enable(pctrl->pctl);
  870. if (ret) {
  871. dev_err(pctrl->dev, "pinctrl enable failed\n");
  872. return ret;
  873. }
  874. ret = rzv2m_gpio_register(pctrl);
  875. if (ret) {
  876. dev_err(pctrl->dev, "failed to add GPIO chip: %i\n", ret);
  877. return ret;
  878. }
  879. return 0;
  880. }
  881. static void rzv2m_pinctrl_clk_disable(void *data)
  882. {
  883. clk_disable_unprepare(data);
  884. }
  885. static int rzv2m_pinctrl_probe(struct platform_device *pdev)
  886. {
  887. struct rzv2m_pinctrl *pctrl;
  888. int ret;
  889. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  890. if (!pctrl)
  891. return -ENOMEM;
  892. pctrl->dev = &pdev->dev;
  893. pctrl->data = of_device_get_match_data(&pdev->dev);
  894. if (!pctrl->data)
  895. return -EINVAL;
  896. pctrl->base = devm_platform_ioremap_resource(pdev, 0);
  897. if (IS_ERR(pctrl->base))
  898. return PTR_ERR(pctrl->base);
  899. pctrl->clk = devm_clk_get(pctrl->dev, NULL);
  900. if (IS_ERR(pctrl->clk)) {
  901. ret = PTR_ERR(pctrl->clk);
  902. dev_err(pctrl->dev, "failed to get GPIO clk : %i\n", ret);
  903. return ret;
  904. }
  905. spin_lock_init(&pctrl->lock);
  906. mutex_init(&pctrl->mutex);
  907. platform_set_drvdata(pdev, pctrl);
  908. ret = clk_prepare_enable(pctrl->clk);
  909. if (ret) {
  910. dev_err(pctrl->dev, "failed to enable GPIO clk: %i\n", ret);
  911. return ret;
  912. }
  913. ret = devm_add_action_or_reset(&pdev->dev, rzv2m_pinctrl_clk_disable,
  914. pctrl->clk);
  915. if (ret) {
  916. dev_err(pctrl->dev,
  917. "failed to register GPIO clk disable action, %i\n",
  918. ret);
  919. return ret;
  920. }
  921. ret = rzv2m_pinctrl_register(pctrl);
  922. if (ret)
  923. return ret;
  924. dev_info(pctrl->dev, "%s support registered\n", DRV_NAME);
  925. return 0;
  926. }
  927. static struct rzv2m_pinctrl_data r9a09g011_data = {
  928. .port_pins = rzv2m_gpio_names,
  929. .port_pin_configs = rzv2m_gpio_configs,
  930. .dedicated_pins = rzv2m_dedicated_pins,
  931. .n_port_pins = ARRAY_SIZE(rzv2m_gpio_configs) * RZV2M_PINS_PER_PORT,
  932. .n_dedicated_pins = ARRAY_SIZE(rzv2m_dedicated_pins),
  933. };
  934. static const struct of_device_id rzv2m_pinctrl_of_table[] = {
  935. {
  936. .compatible = "renesas,r9a09g011-pinctrl",
  937. .data = &r9a09g011_data,
  938. },
  939. { /* sentinel */ }
  940. };
  941. static struct platform_driver rzv2m_pinctrl_driver = {
  942. .driver = {
  943. .name = DRV_NAME,
  944. .of_match_table = of_match_ptr(rzv2m_pinctrl_of_table),
  945. },
  946. .probe = rzv2m_pinctrl_probe,
  947. };
  948. static int __init rzv2m_pinctrl_init(void)
  949. {
  950. return platform_driver_register(&rzv2m_pinctrl_driver);
  951. }
  952. core_initcall(rzv2m_pinctrl_init);
  953. MODULE_AUTHOR("Phil Edworthy <[email protected]>");
  954. MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/V2M");
  955. MODULE_LICENSE("GPL");