pinctrl-imx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Core driver for the imx pin controller
  3. *
  4. * Copyright (C) 2012 Freescale Semiconductor, Inc.
  5. * Copyright (C) 2012 Linaro Ltd.
  6. *
  7. * Author: Dong Aisheng <dong.aisheng@linaro.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_address.h>
  21. #include <linux/pinctrl/machine.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include <linux/pinctrl/pinctrl.h>
  24. #include <linux/pinctrl/pinmux.h>
  25. #include <linux/slab.h>
  26. #include <linux/regmap.h>
  27. #include "../core.h"
  28. #include "../pinconf.h"
  29. #include "../pinmux.h"
  30. #include "pinctrl-imx.h"
  31. /* The bits in CONFIG cell defined in binding doc*/
  32. #define IMX_NO_PAD_CTL 0x80000000 /* no pin config need */
  33. #define IMX_PAD_SION 0x40000000 /* set SION */
  34. static inline const struct group_desc *imx_pinctrl_find_group_by_name(
  35. struct pinctrl_dev *pctldev,
  36. const char *name)
  37. {
  38. const struct group_desc *grp = NULL;
  39. int i;
  40. for (i = 0; i < pctldev->num_groups; i++) {
  41. grp = pinctrl_generic_get_group(pctldev, i);
  42. if (grp && !strcmp(grp->name, name))
  43. break;
  44. }
  45. return grp;
  46. }
  47. static void imx_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  48. unsigned offset)
  49. {
  50. seq_printf(s, "%s", dev_name(pctldev->dev));
  51. }
  52. static int imx_dt_node_to_map(struct pinctrl_dev *pctldev,
  53. struct device_node *np,
  54. struct pinctrl_map **map, unsigned *num_maps)
  55. {
  56. struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
  57. struct imx_pinctrl_soc_info *info = ipctl->info;
  58. const struct group_desc *grp;
  59. struct pinctrl_map *new_map;
  60. struct device_node *parent;
  61. int map_num = 1;
  62. int i, j;
  63. /*
  64. * first find the group of this node and check if we need create
  65. * config maps for pins
  66. */
  67. grp = imx_pinctrl_find_group_by_name(pctldev, np->name);
  68. if (!grp) {
  69. dev_err(info->dev, "unable to find group for node %s\n",
  70. np->name);
  71. return -EINVAL;
  72. }
  73. for (i = 0; i < grp->num_pins; i++) {
  74. struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
  75. if (!(pin->config & IMX_NO_PAD_CTL))
  76. map_num++;
  77. }
  78. new_map = kmalloc(sizeof(struct pinctrl_map) * map_num, GFP_KERNEL);
  79. if (!new_map)
  80. return -ENOMEM;
  81. *map = new_map;
  82. *num_maps = map_num;
  83. /* create mux map */
  84. parent = of_get_parent(np);
  85. if (!parent) {
  86. kfree(new_map);
  87. return -EINVAL;
  88. }
  89. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  90. new_map[0].data.mux.function = parent->name;
  91. new_map[0].data.mux.group = np->name;
  92. of_node_put(parent);
  93. /* create config map */
  94. new_map++;
  95. for (i = j = 0; i < grp->num_pins; i++) {
  96. struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
  97. if (!(pin->config & IMX_NO_PAD_CTL)) {
  98. new_map[j].type = PIN_MAP_TYPE_CONFIGS_PIN;
  99. new_map[j].data.configs.group_or_pin =
  100. pin_get_name(pctldev, pin->pin);
  101. new_map[j].data.configs.configs = &pin->config;
  102. new_map[j].data.configs.num_configs = 1;
  103. j++;
  104. }
  105. }
  106. dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
  107. (*map)->data.mux.function, (*map)->data.mux.group, map_num);
  108. return 0;
  109. }
  110. static void imx_dt_free_map(struct pinctrl_dev *pctldev,
  111. struct pinctrl_map *map, unsigned num_maps)
  112. {
  113. kfree(map);
  114. }
  115. static const struct pinctrl_ops imx_pctrl_ops = {
  116. .get_groups_count = pinctrl_generic_get_group_count,
  117. .get_group_name = pinctrl_generic_get_group_name,
  118. .get_group_pins = pinctrl_generic_get_group_pins,
  119. .pin_dbg_show = imx_pin_dbg_show,
  120. .dt_node_to_map = imx_dt_node_to_map,
  121. .dt_free_map = imx_dt_free_map,
  122. };
  123. static int imx_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
  124. unsigned group)
  125. {
  126. struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
  127. struct imx_pinctrl_soc_info *info = ipctl->info;
  128. const struct imx_pin_reg *pin_reg;
  129. unsigned int npins, pin_id;
  130. int i;
  131. struct group_desc *grp = NULL;
  132. struct function_desc *func = NULL;
  133. /*
  134. * Configure the mux mode for each pin in the group for a specific
  135. * function.
  136. */
  137. grp = pinctrl_generic_get_group(pctldev, group);
  138. if (!grp)
  139. return -EINVAL;
  140. func = pinmux_generic_get_function(pctldev, selector);
  141. if (!func)
  142. return -EINVAL;
  143. npins = grp->num_pins;
  144. dev_dbg(ipctl->dev, "enable function %s group %s\n",
  145. func->name, grp->name);
  146. for (i = 0; i < npins; i++) {
  147. struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
  148. pin_id = pin->pin;
  149. pin_reg = &info->pin_regs[pin_id];
  150. if (pin_reg->mux_reg == -1) {
  151. dev_dbg(ipctl->dev, "Pin(%s) does not support mux function\n",
  152. info->pins[pin_id].name);
  153. continue;
  154. }
  155. if (info->flags & SHARE_MUX_CONF_REG) {
  156. u32 reg;
  157. reg = readl(ipctl->base + pin_reg->mux_reg);
  158. reg &= ~info->mux_mask;
  159. reg |= (pin->mux_mode << info->mux_shift);
  160. writel(reg, ipctl->base + pin_reg->mux_reg);
  161. dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
  162. pin_reg->mux_reg, reg);
  163. } else {
  164. writel(pin->mux_mode, ipctl->base + pin_reg->mux_reg);
  165. dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
  166. pin_reg->mux_reg, pin->mux_mode);
  167. }
  168. /*
  169. * If the select input value begins with 0xff, it's a quirky
  170. * select input and the value should be interpreted as below.
  171. * 31 23 15 7 0
  172. * | 0xff | shift | width | select |
  173. * It's used to work around the problem that the select
  174. * input for some pin is not implemented in the select
  175. * input register but in some general purpose register.
  176. * We encode the select input value, width and shift of
  177. * the bit field into input_val cell of pin function ID
  178. * in device tree, and then decode them here for setting
  179. * up the select input bits in general purpose register.
  180. */
  181. if (pin->input_val >> 24 == 0xff) {
  182. u32 val = pin->input_val;
  183. u8 select = val & 0xff;
  184. u8 width = (val >> 8) & 0xff;
  185. u8 shift = (val >> 16) & 0xff;
  186. u32 mask = ((1 << width) - 1) << shift;
  187. /*
  188. * The input_reg[i] here is actually some IOMUXC general
  189. * purpose register, not regular select input register.
  190. */
  191. val = readl(ipctl->base + pin->input_reg);
  192. val &= ~mask;
  193. val |= select << shift;
  194. writel(val, ipctl->base + pin->input_reg);
  195. } else if (pin->input_reg) {
  196. /*
  197. * Regular select input register can never be at offset
  198. * 0, and we only print register value for regular case.
  199. */
  200. if (ipctl->input_sel_base)
  201. writel(pin->input_val, ipctl->input_sel_base +
  202. pin->input_reg);
  203. else
  204. writel(pin->input_val, ipctl->base +
  205. pin->input_reg);
  206. dev_dbg(ipctl->dev,
  207. "==>select_input: offset 0x%x val 0x%x\n",
  208. pin->input_reg, pin->input_val);
  209. }
  210. }
  211. return 0;
  212. }
  213. struct pinmux_ops imx_pmx_ops = {
  214. .get_functions_count = pinmux_generic_get_function_count,
  215. .get_function_name = pinmux_generic_get_function_name,
  216. .get_function_groups = pinmux_generic_get_function_groups,
  217. .set_mux = imx_pmx_set,
  218. };
  219. /* decode generic config into raw register values */
  220. static u32 imx_pinconf_decode_generic_config(struct imx_pinctrl *ipctl,
  221. unsigned long *configs,
  222. unsigned int num_configs)
  223. {
  224. struct imx_pinctrl_soc_info *info = ipctl->info;
  225. struct imx_cfg_params_decode *decode;
  226. enum pin_config_param param;
  227. u32 raw_config = 0;
  228. u32 param_val;
  229. int i, j;
  230. WARN_ON(num_configs > info->num_decodes);
  231. for (i = 0; i < num_configs; i++) {
  232. param = pinconf_to_config_param(configs[i]);
  233. param_val = pinconf_to_config_argument(configs[i]);
  234. decode = info->decodes;
  235. for (j = 0; j < info->num_decodes; j++) {
  236. if (param == decode->param) {
  237. if (decode->invert)
  238. param_val = !param_val;
  239. raw_config |= (param_val << decode->shift)
  240. & decode->mask;
  241. break;
  242. }
  243. decode++;
  244. }
  245. }
  246. if (info->fixup)
  247. info->fixup(configs, num_configs, &raw_config);
  248. return raw_config;
  249. }
  250. static u32 imx_pinconf_parse_generic_config(struct device_node *np,
  251. struct imx_pinctrl *ipctl)
  252. {
  253. struct imx_pinctrl_soc_info *info = ipctl->info;
  254. struct pinctrl_dev *pctl = ipctl->pctl;
  255. unsigned int num_configs;
  256. unsigned long *configs;
  257. int ret;
  258. if (!info->generic_pinconf)
  259. return 0;
  260. ret = pinconf_generic_parse_dt_config(np, pctl, &configs,
  261. &num_configs);
  262. if (ret)
  263. return 0;
  264. return imx_pinconf_decode_generic_config(ipctl, configs, num_configs);
  265. }
  266. static int imx_pinconf_get(struct pinctrl_dev *pctldev,
  267. unsigned pin_id, unsigned long *config)
  268. {
  269. struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
  270. struct imx_pinctrl_soc_info *info = ipctl->info;
  271. const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
  272. if (pin_reg->conf_reg == -1) {
  273. dev_err(info->dev, "Pin(%s) does not support config function\n",
  274. info->pins[pin_id].name);
  275. return -EINVAL;
  276. }
  277. *config = readl(ipctl->base + pin_reg->conf_reg);
  278. if (info->flags & SHARE_MUX_CONF_REG)
  279. *config &= ~info->mux_mask;
  280. return 0;
  281. }
  282. static int imx_pinconf_set(struct pinctrl_dev *pctldev,
  283. unsigned pin_id, unsigned long *configs,
  284. unsigned num_configs)
  285. {
  286. struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
  287. struct imx_pinctrl_soc_info *info = ipctl->info;
  288. const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
  289. int i;
  290. if (pin_reg->conf_reg == -1) {
  291. dev_err(info->dev, "Pin(%s) does not support config function\n",
  292. info->pins[pin_id].name);
  293. return -EINVAL;
  294. }
  295. dev_dbg(ipctl->dev, "pinconf set pin %s\n",
  296. info->pins[pin_id].name);
  297. for (i = 0; i < num_configs; i++) {
  298. if (info->flags & SHARE_MUX_CONF_REG) {
  299. u32 reg;
  300. reg = readl(ipctl->base + pin_reg->conf_reg);
  301. reg &= info->mux_mask;
  302. reg |= configs[i];
  303. writel(reg, ipctl->base + pin_reg->conf_reg);
  304. dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
  305. pin_reg->conf_reg, reg);
  306. } else {
  307. writel(configs[i], ipctl->base + pin_reg->conf_reg);
  308. dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%lx\n",
  309. pin_reg->conf_reg, configs[i]);
  310. }
  311. } /* for each config */
  312. return 0;
  313. }
  314. static void imx_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  315. struct seq_file *s, unsigned pin_id)
  316. {
  317. struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
  318. struct imx_pinctrl_soc_info *info = ipctl->info;
  319. const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
  320. unsigned long config;
  321. if (!pin_reg || pin_reg->conf_reg == -1) {
  322. seq_printf(s, "N/A");
  323. return;
  324. }
  325. config = readl(ipctl->base + pin_reg->conf_reg);
  326. seq_printf(s, "0x%lx", config);
  327. }
  328. static void imx_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  329. struct seq_file *s, unsigned group)
  330. {
  331. struct group_desc *grp;
  332. unsigned long config;
  333. const char *name;
  334. int i, ret;
  335. if (group >= pctldev->num_groups)
  336. return;
  337. seq_printf(s, "\n");
  338. grp = pinctrl_generic_get_group(pctldev, group);
  339. if (!grp)
  340. return;
  341. for (i = 0; i < grp->num_pins; i++) {
  342. struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
  343. name = pin_get_name(pctldev, pin->pin);
  344. ret = imx_pinconf_get(pctldev, pin->pin, &config);
  345. if (ret)
  346. return;
  347. seq_printf(s, " %s: 0x%lx\n", name, config);
  348. }
  349. }
  350. static const struct pinconf_ops imx_pinconf_ops = {
  351. .pin_config_get = imx_pinconf_get,
  352. .pin_config_set = imx_pinconf_set,
  353. .pin_config_dbg_show = imx_pinconf_dbg_show,
  354. .pin_config_group_dbg_show = imx_pinconf_group_dbg_show,
  355. };
  356. /*
  357. * Each pin represented in fsl,pins consists of 5 u32 PIN_FUNC_ID and
  358. * 1 u32 CONFIG, so 24 types in total for each pin.
  359. */
  360. #define FSL_PIN_SIZE 24
  361. #define SHARE_FSL_PIN_SIZE 20
  362. static int imx_pinctrl_parse_groups(struct device_node *np,
  363. struct group_desc *grp,
  364. struct imx_pinctrl *ipctl,
  365. u32 index)
  366. {
  367. struct imx_pinctrl_soc_info *info = ipctl->info;
  368. int size, pin_size;
  369. const __be32 *list;
  370. int i;
  371. u32 config;
  372. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  373. if (info->flags & SHARE_MUX_CONF_REG)
  374. pin_size = SHARE_FSL_PIN_SIZE;
  375. else
  376. pin_size = FSL_PIN_SIZE;
  377. if (info->generic_pinconf)
  378. pin_size -= 4;
  379. /* Initialise group */
  380. grp->name = np->name;
  381. /*
  382. * the binding format is fsl,pins = <PIN_FUNC_ID CONFIG ...>,
  383. * do sanity check and calculate pins number
  384. *
  385. * First try legacy 'fsl,pins' property, then fall back to the
  386. * generic 'pinmux'.
  387. *
  388. * Note: for generic 'pinmux' case, there's no CONFIG part in
  389. * the binding format.
  390. */
  391. list = of_get_property(np, "fsl,pins", &size);
  392. if (!list) {
  393. list = of_get_property(np, "pinmux", &size);
  394. if (!list) {
  395. dev_err(info->dev,
  396. "no fsl,pins and pins property in node %pOF\n", np);
  397. return -EINVAL;
  398. }
  399. }
  400. /* we do not check return since it's safe node passed down */
  401. if (!size || size % pin_size) {
  402. dev_err(info->dev, "Invalid fsl,pins or pins property in node %pOF\n", np);
  403. return -EINVAL;
  404. }
  405. /* first try to parse the generic pin config */
  406. config = imx_pinconf_parse_generic_config(np, ipctl);
  407. grp->num_pins = size / pin_size;
  408. grp->data = devm_kzalloc(info->dev, grp->num_pins *
  409. sizeof(struct imx_pin), GFP_KERNEL);
  410. grp->pins = devm_kzalloc(info->dev, grp->num_pins *
  411. sizeof(unsigned int), GFP_KERNEL);
  412. if (!grp->pins || !grp->data)
  413. return -ENOMEM;
  414. for (i = 0; i < grp->num_pins; i++) {
  415. u32 mux_reg = be32_to_cpu(*list++);
  416. u32 conf_reg;
  417. unsigned int pin_id;
  418. struct imx_pin_reg *pin_reg;
  419. struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
  420. if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg)
  421. mux_reg = -1;
  422. if (info->flags & SHARE_MUX_CONF_REG) {
  423. conf_reg = mux_reg;
  424. } else {
  425. conf_reg = be32_to_cpu(*list++);
  426. if (!conf_reg)
  427. conf_reg = -1;
  428. }
  429. pin_id = (mux_reg != -1) ? mux_reg / 4 : conf_reg / 4;
  430. pin_reg = &info->pin_regs[pin_id];
  431. pin->pin = pin_id;
  432. grp->pins[i] = pin_id;
  433. pin_reg->mux_reg = mux_reg;
  434. pin_reg->conf_reg = conf_reg;
  435. pin->input_reg = be32_to_cpu(*list++);
  436. pin->mux_mode = be32_to_cpu(*list++);
  437. pin->input_val = be32_to_cpu(*list++);
  438. if (info->generic_pinconf) {
  439. /* generic pin config decoded */
  440. pin->config = config;
  441. } else {
  442. /* legacy pin config read from devicetree */
  443. config = be32_to_cpu(*list++);
  444. /* SION bit is in mux register */
  445. if (config & IMX_PAD_SION)
  446. pin->mux_mode |= IOMUXC_CONFIG_SION;
  447. pin->config = config & ~IMX_PAD_SION;
  448. }
  449. dev_dbg(info->dev, "%s: 0x%x 0x%08lx", info->pins[pin_id].name,
  450. pin->mux_mode, pin->config);
  451. }
  452. return 0;
  453. }
  454. static int imx_pinctrl_parse_functions(struct device_node *np,
  455. struct imx_pinctrl *ipctl,
  456. u32 index)
  457. {
  458. struct pinctrl_dev *pctl = ipctl->pctl;
  459. struct imx_pinctrl_soc_info *info = ipctl->info;
  460. struct device_node *child;
  461. struct function_desc *func;
  462. struct group_desc *grp;
  463. u32 i = 0;
  464. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  465. func = pinmux_generic_get_function(pctl, index);
  466. if (!func)
  467. return -EINVAL;
  468. /* Initialise function */
  469. func->name = np->name;
  470. func->num_group_names = of_get_child_count(np);
  471. if (func->num_group_names == 0) {
  472. dev_err(info->dev, "no groups defined in %pOF\n", np);
  473. return -EINVAL;
  474. }
  475. func->group_names = devm_kcalloc(info->dev, func->num_group_names,
  476. sizeof(char *), GFP_KERNEL);
  477. if (!func->group_names)
  478. return -ENOMEM;
  479. for_each_child_of_node(np, child) {
  480. func->group_names[i] = child->name;
  481. grp = devm_kzalloc(info->dev, sizeof(struct group_desc),
  482. GFP_KERNEL);
  483. if (!grp)
  484. return -ENOMEM;
  485. mutex_lock(&info->mutex);
  486. radix_tree_insert(&pctl->pin_group_tree,
  487. info->group_index++, grp);
  488. mutex_unlock(&info->mutex);
  489. imx_pinctrl_parse_groups(child, grp, ipctl, i++);
  490. }
  491. return 0;
  492. }
  493. /*
  494. * Check if the DT contains pins in the direct child nodes. This indicates the
  495. * newer DT format to store pins. This function returns true if the first found
  496. * fsl,pins property is in a child of np. Otherwise false is returned.
  497. */
  498. static bool imx_pinctrl_dt_is_flat_functions(struct device_node *np)
  499. {
  500. struct device_node *function_np;
  501. struct device_node *pinctrl_np;
  502. for_each_child_of_node(np, function_np) {
  503. if (of_property_read_bool(function_np, "fsl,pins"))
  504. return true;
  505. for_each_child_of_node(function_np, pinctrl_np) {
  506. if (of_property_read_bool(pinctrl_np, "fsl,pins"))
  507. return false;
  508. }
  509. }
  510. return true;
  511. }
  512. static int imx_pinctrl_probe_dt(struct platform_device *pdev,
  513. struct imx_pinctrl *ipctl)
  514. {
  515. struct device_node *np = pdev->dev.of_node;
  516. struct device_node *child;
  517. struct pinctrl_dev *pctl = ipctl->pctl;
  518. struct imx_pinctrl_soc_info *info = ipctl->info;
  519. u32 nfuncs = 0;
  520. u32 i = 0;
  521. bool flat_funcs;
  522. if (!np)
  523. return -ENODEV;
  524. flat_funcs = imx_pinctrl_dt_is_flat_functions(np);
  525. if (flat_funcs) {
  526. nfuncs = 1;
  527. } else {
  528. nfuncs = of_get_child_count(np);
  529. if (nfuncs <= 0) {
  530. dev_err(&pdev->dev, "no functions defined\n");
  531. return -EINVAL;
  532. }
  533. }
  534. for (i = 0; i < nfuncs; i++) {
  535. struct function_desc *function;
  536. function = devm_kzalloc(&pdev->dev, sizeof(*function),
  537. GFP_KERNEL);
  538. if (!function)
  539. return -ENOMEM;
  540. mutex_lock(&info->mutex);
  541. radix_tree_insert(&pctl->pin_function_tree, i, function);
  542. mutex_unlock(&info->mutex);
  543. }
  544. pctl->num_functions = nfuncs;
  545. info->group_index = 0;
  546. if (flat_funcs) {
  547. pctl->num_groups = of_get_child_count(np);
  548. } else {
  549. pctl->num_groups = 0;
  550. for_each_child_of_node(np, child)
  551. pctl->num_groups += of_get_child_count(child);
  552. }
  553. if (flat_funcs) {
  554. imx_pinctrl_parse_functions(np, ipctl, 0);
  555. } else {
  556. i = 0;
  557. for_each_child_of_node(np, child)
  558. imx_pinctrl_parse_functions(child, ipctl, i++);
  559. }
  560. return 0;
  561. }
  562. int imx_pinctrl_probe(struct platform_device *pdev,
  563. struct imx_pinctrl_soc_info *info)
  564. {
  565. struct regmap_config config = { .name = "gpr" };
  566. struct device_node *dev_np = pdev->dev.of_node;
  567. struct pinctrl_desc *imx_pinctrl_desc;
  568. struct device_node *np;
  569. struct imx_pinctrl *ipctl;
  570. struct resource *res;
  571. struct regmap *gpr;
  572. int ret, i;
  573. if (!info || !info->pins || !info->npins) {
  574. dev_err(&pdev->dev, "wrong pinctrl info\n");
  575. return -EINVAL;
  576. }
  577. info->dev = &pdev->dev;
  578. if (info->gpr_compatible) {
  579. gpr = syscon_regmap_lookup_by_compatible(info->gpr_compatible);
  580. if (!IS_ERR(gpr))
  581. regmap_attach_dev(&pdev->dev, gpr, &config);
  582. }
  583. /* Create state holders etc for this driver */
  584. ipctl = devm_kzalloc(&pdev->dev, sizeof(*ipctl), GFP_KERNEL);
  585. if (!ipctl)
  586. return -ENOMEM;
  587. info->pin_regs = devm_kmalloc(&pdev->dev, sizeof(*info->pin_regs) *
  588. info->npins, GFP_KERNEL);
  589. if (!info->pin_regs)
  590. return -ENOMEM;
  591. for (i = 0; i < info->npins; i++) {
  592. info->pin_regs[i].mux_reg = -1;
  593. info->pin_regs[i].conf_reg = -1;
  594. }
  595. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  596. ipctl->base = devm_ioremap_resource(&pdev->dev, res);
  597. if (IS_ERR(ipctl->base))
  598. return PTR_ERR(ipctl->base);
  599. if (of_property_read_bool(dev_np, "fsl,input-sel")) {
  600. np = of_parse_phandle(dev_np, "fsl,input-sel", 0);
  601. if (!np) {
  602. dev_err(&pdev->dev, "iomuxc fsl,input-sel property not found\n");
  603. return -EINVAL;
  604. }
  605. ipctl->input_sel_base = of_iomap(np, 0);
  606. of_node_put(np);
  607. if (!ipctl->input_sel_base) {
  608. dev_err(&pdev->dev,
  609. "iomuxc input select base address not found\n");
  610. return -ENOMEM;
  611. }
  612. }
  613. imx_pinctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*imx_pinctrl_desc),
  614. GFP_KERNEL);
  615. if (!imx_pinctrl_desc)
  616. return -ENOMEM;
  617. imx_pinctrl_desc->name = dev_name(&pdev->dev);
  618. imx_pinctrl_desc->pins = info->pins;
  619. imx_pinctrl_desc->npins = info->npins;
  620. imx_pinctrl_desc->pctlops = &imx_pctrl_ops;
  621. imx_pinctrl_desc->pmxops = &imx_pmx_ops;
  622. imx_pinctrl_desc->confops = &imx_pinconf_ops;
  623. imx_pinctrl_desc->owner = THIS_MODULE;
  624. /* for generic pinconf */
  625. imx_pinctrl_desc->custom_params = info->custom_params;
  626. imx_pinctrl_desc->num_custom_params = info->num_custom_params;
  627. /* platform specific callback */
  628. imx_pmx_ops.gpio_set_direction = info->gpio_set_direction;
  629. mutex_init(&info->mutex);
  630. ipctl->info = info;
  631. ipctl->dev = info->dev;
  632. platform_set_drvdata(pdev, ipctl);
  633. ret = devm_pinctrl_register_and_init(&pdev->dev,
  634. imx_pinctrl_desc, ipctl,
  635. &ipctl->pctl);
  636. if (ret) {
  637. dev_err(&pdev->dev, "could not register IMX pinctrl driver\n");
  638. return ret;
  639. }
  640. ret = imx_pinctrl_probe_dt(pdev, ipctl);
  641. if (ret) {
  642. dev_err(&pdev->dev, "fail to probe dt properties\n");
  643. return ret;
  644. }
  645. dev_info(&pdev->dev, "initialized IMX pinctrl driver\n");
  646. return pinctrl_enable(ipctl->pctl);
  647. }