pinctrl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * SuperH Pin Function Controller pinmux support.
  3. *
  4. * Copyright (C) 2012 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #define DRV_NAME "sh-pfc"
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/pinctrl/machine.h>
  18. #include <linux/pinctrl/pinconf.h>
  19. #include <linux/pinctrl/pinconf-generic.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include "core.h"
  25. #include "../core.h"
  26. #include "../pinconf.h"
  27. struct sh_pfc_pin_config {
  28. u32 type;
  29. };
  30. struct sh_pfc_pinctrl {
  31. struct pinctrl_dev *pctl;
  32. struct pinctrl_desc pctl_desc;
  33. struct sh_pfc *pfc;
  34. struct pinctrl_pin_desc *pins;
  35. struct sh_pfc_pin_config *configs;
  36. const char *func_prop_name;
  37. const char *groups_prop_name;
  38. const char *pins_prop_name;
  39. };
  40. static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
  41. {
  42. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  43. return pmx->pfc->info->nr_groups;
  44. }
  45. static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
  46. unsigned selector)
  47. {
  48. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  49. return pmx->pfc->info->groups[selector].name;
  50. }
  51. static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  52. const unsigned **pins, unsigned *num_pins)
  53. {
  54. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  55. *pins = pmx->pfc->info->groups[selector].pins;
  56. *num_pins = pmx->pfc->info->groups[selector].nr_pins;
  57. return 0;
  58. }
  59. static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  60. unsigned offset)
  61. {
  62. seq_printf(s, "%s", DRV_NAME);
  63. }
  64. #ifdef CONFIG_OF
  65. static int sh_pfc_map_add_config(struct pinctrl_map *map,
  66. const char *group_or_pin,
  67. enum pinctrl_map_type type,
  68. unsigned long *configs,
  69. unsigned int num_configs)
  70. {
  71. unsigned long *cfgs;
  72. cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
  73. GFP_KERNEL);
  74. if (cfgs == NULL)
  75. return -ENOMEM;
  76. map->type = type;
  77. map->data.configs.group_or_pin = group_or_pin;
  78. map->data.configs.configs = cfgs;
  79. map->data.configs.num_configs = num_configs;
  80. return 0;
  81. }
  82. static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  83. struct device_node *np,
  84. struct pinctrl_map **map,
  85. unsigned int *num_maps, unsigned int *index)
  86. {
  87. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  88. struct device *dev = pmx->pfc->dev;
  89. struct pinctrl_map *maps = *map;
  90. unsigned int nmaps = *num_maps;
  91. unsigned int idx = *index;
  92. unsigned int num_configs;
  93. const char *function = NULL;
  94. unsigned long *configs;
  95. struct property *prop;
  96. unsigned int num_groups;
  97. unsigned int num_pins;
  98. const char *group;
  99. const char *pin;
  100. int ret;
  101. /* Support both the old Renesas-specific properties and the new standard
  102. * properties. Mixing old and new properties isn't allowed, neither
  103. * inside a subnode nor across subnodes.
  104. */
  105. if (!pmx->func_prop_name) {
  106. if (of_find_property(np, "groups", NULL) ||
  107. of_find_property(np, "pins", NULL)) {
  108. pmx->func_prop_name = "function";
  109. pmx->groups_prop_name = "groups";
  110. pmx->pins_prop_name = "pins";
  111. } else {
  112. pmx->func_prop_name = "renesas,function";
  113. pmx->groups_prop_name = "renesas,groups";
  114. pmx->pins_prop_name = "renesas,pins";
  115. }
  116. }
  117. /* Parse the function and configuration properties. At least a function
  118. * or one configuration must be specified.
  119. */
  120. ret = of_property_read_string(np, pmx->func_prop_name, &function);
  121. if (ret < 0 && ret != -EINVAL) {
  122. dev_err(dev, "Invalid function in DT\n");
  123. return ret;
  124. }
  125. ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
  126. if (ret < 0)
  127. return ret;
  128. if (!function && num_configs == 0) {
  129. dev_err(dev,
  130. "DT node must contain at least a function or config\n");
  131. ret = -ENODEV;
  132. goto done;
  133. }
  134. /* Count the number of pins and groups and reallocate mappings. */
  135. ret = of_property_count_strings(np, pmx->pins_prop_name);
  136. if (ret == -EINVAL) {
  137. num_pins = 0;
  138. } else if (ret < 0) {
  139. dev_err(dev, "Invalid pins list in DT\n");
  140. goto done;
  141. } else {
  142. num_pins = ret;
  143. }
  144. ret = of_property_count_strings(np, pmx->groups_prop_name);
  145. if (ret == -EINVAL) {
  146. num_groups = 0;
  147. } else if (ret < 0) {
  148. dev_err(dev, "Invalid pin groups list in DT\n");
  149. goto done;
  150. } else {
  151. num_groups = ret;
  152. }
  153. if (!num_pins && !num_groups) {
  154. dev_err(dev, "No pin or group provided in DT node\n");
  155. ret = -ENODEV;
  156. goto done;
  157. }
  158. if (function)
  159. nmaps += num_groups;
  160. if (configs)
  161. nmaps += num_pins + num_groups;
  162. maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
  163. if (maps == NULL) {
  164. ret = -ENOMEM;
  165. goto done;
  166. }
  167. *map = maps;
  168. *num_maps = nmaps;
  169. /* Iterate over pins and groups and create the mappings. */
  170. of_property_for_each_string(np, pmx->groups_prop_name, prop, group) {
  171. if (function) {
  172. maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
  173. maps[idx].data.mux.group = group;
  174. maps[idx].data.mux.function = function;
  175. idx++;
  176. }
  177. if (configs) {
  178. ret = sh_pfc_map_add_config(&maps[idx], group,
  179. PIN_MAP_TYPE_CONFIGS_GROUP,
  180. configs, num_configs);
  181. if (ret < 0)
  182. goto done;
  183. idx++;
  184. }
  185. }
  186. if (!configs) {
  187. ret = 0;
  188. goto done;
  189. }
  190. of_property_for_each_string(np, pmx->pins_prop_name, prop, pin) {
  191. ret = sh_pfc_map_add_config(&maps[idx], pin,
  192. PIN_MAP_TYPE_CONFIGS_PIN,
  193. configs, num_configs);
  194. if (ret < 0)
  195. goto done;
  196. idx++;
  197. }
  198. done:
  199. *index = idx;
  200. kfree(configs);
  201. return ret;
  202. }
  203. static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
  204. struct pinctrl_map *map, unsigned num_maps)
  205. {
  206. unsigned int i;
  207. if (map == NULL)
  208. return;
  209. for (i = 0; i < num_maps; ++i) {
  210. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
  211. map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  212. kfree(map[i].data.configs.configs);
  213. }
  214. kfree(map);
  215. }
  216. static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
  217. struct device_node *np,
  218. struct pinctrl_map **map, unsigned *num_maps)
  219. {
  220. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  221. struct device *dev = pmx->pfc->dev;
  222. struct device_node *child;
  223. unsigned int index;
  224. int ret;
  225. *map = NULL;
  226. *num_maps = 0;
  227. index = 0;
  228. for_each_child_of_node(np, child) {
  229. ret = sh_pfc_dt_subnode_to_map(pctldev, child, map, num_maps,
  230. &index);
  231. if (ret < 0) {
  232. of_node_put(child);
  233. goto done;
  234. }
  235. }
  236. /* If no mapping has been found in child nodes try the config node. */
  237. if (*num_maps == 0) {
  238. ret = sh_pfc_dt_subnode_to_map(pctldev, np, map, num_maps,
  239. &index);
  240. if (ret < 0)
  241. goto done;
  242. }
  243. if (*num_maps)
  244. return 0;
  245. dev_err(dev, "no mapping found in node %s\n", np->full_name);
  246. ret = -EINVAL;
  247. done:
  248. if (ret < 0)
  249. sh_pfc_dt_free_map(pctldev, *map, *num_maps);
  250. return ret;
  251. }
  252. #endif /* CONFIG_OF */
  253. static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
  254. .get_groups_count = sh_pfc_get_groups_count,
  255. .get_group_name = sh_pfc_get_group_name,
  256. .get_group_pins = sh_pfc_get_group_pins,
  257. .pin_dbg_show = sh_pfc_pin_dbg_show,
  258. #ifdef CONFIG_OF
  259. .dt_node_to_map = sh_pfc_dt_node_to_map,
  260. .dt_free_map = sh_pfc_dt_free_map,
  261. #endif
  262. };
  263. static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
  264. {
  265. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  266. return pmx->pfc->info->nr_functions;
  267. }
  268. static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
  269. unsigned selector)
  270. {
  271. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  272. return pmx->pfc->info->functions[selector].name;
  273. }
  274. static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
  275. unsigned selector,
  276. const char * const **groups,
  277. unsigned * const num_groups)
  278. {
  279. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  280. *groups = pmx->pfc->info->functions[selector].groups;
  281. *num_groups = pmx->pfc->info->functions[selector].nr_groups;
  282. return 0;
  283. }
  284. static int sh_pfc_func_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
  285. unsigned group)
  286. {
  287. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  288. struct sh_pfc *pfc = pmx->pfc;
  289. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
  290. unsigned long flags;
  291. unsigned int i;
  292. int ret = 0;
  293. spin_lock_irqsave(&pfc->lock, flags);
  294. for (i = 0; i < grp->nr_pins; ++i) {
  295. int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
  296. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  297. if (cfg->type != PINMUX_TYPE_NONE) {
  298. ret = -EBUSY;
  299. goto done;
  300. }
  301. }
  302. for (i = 0; i < grp->nr_pins; ++i) {
  303. ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
  304. if (ret < 0)
  305. break;
  306. }
  307. done:
  308. spin_unlock_irqrestore(&pfc->lock, flags);
  309. return ret;
  310. }
  311. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  312. struct pinctrl_gpio_range *range,
  313. unsigned offset)
  314. {
  315. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  316. struct sh_pfc *pfc = pmx->pfc;
  317. int idx = sh_pfc_get_pin_index(pfc, offset);
  318. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  319. unsigned long flags;
  320. int ret;
  321. spin_lock_irqsave(&pfc->lock, flags);
  322. if (cfg->type != PINMUX_TYPE_NONE) {
  323. dev_err(pfc->dev,
  324. "Pin %u is busy, can't configure it as GPIO.\n",
  325. offset);
  326. ret = -EBUSY;
  327. goto done;
  328. }
  329. if (!pfc->gpio) {
  330. /* If GPIOs are handled externally the pin mux type need to be
  331. * set to GPIO here.
  332. */
  333. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  334. ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
  335. if (ret < 0)
  336. goto done;
  337. }
  338. cfg->type = PINMUX_TYPE_GPIO;
  339. ret = 0;
  340. done:
  341. spin_unlock_irqrestore(&pfc->lock, flags);
  342. return ret;
  343. }
  344. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  345. struct pinctrl_gpio_range *range,
  346. unsigned offset)
  347. {
  348. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  349. struct sh_pfc *pfc = pmx->pfc;
  350. int idx = sh_pfc_get_pin_index(pfc, offset);
  351. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  352. unsigned long flags;
  353. spin_lock_irqsave(&pfc->lock, flags);
  354. cfg->type = PINMUX_TYPE_NONE;
  355. spin_unlock_irqrestore(&pfc->lock, flags);
  356. }
  357. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  358. struct pinctrl_gpio_range *range,
  359. unsigned offset, bool input)
  360. {
  361. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  362. struct sh_pfc *pfc = pmx->pfc;
  363. int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  364. int idx = sh_pfc_get_pin_index(pfc, offset);
  365. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  366. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  367. unsigned long flags;
  368. unsigned int dir;
  369. int ret;
  370. /* Check if the requested direction is supported by the pin. Not all SoC
  371. * provide pin config data, so perform the check conditionally.
  372. */
  373. if (pin->configs) {
  374. dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
  375. if (!(pin->configs & dir))
  376. return -EINVAL;
  377. }
  378. spin_lock_irqsave(&pfc->lock, flags);
  379. ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
  380. if (ret < 0)
  381. goto done;
  382. cfg->type = new_type;
  383. done:
  384. spin_unlock_irqrestore(&pfc->lock, flags);
  385. return ret;
  386. }
  387. static const struct pinmux_ops sh_pfc_pinmux_ops = {
  388. .get_functions_count = sh_pfc_get_functions_count,
  389. .get_function_name = sh_pfc_get_function_name,
  390. .get_function_groups = sh_pfc_get_function_groups,
  391. .set_mux = sh_pfc_func_set_mux,
  392. .gpio_request_enable = sh_pfc_gpio_request_enable,
  393. .gpio_disable_free = sh_pfc_gpio_disable_free,
  394. .gpio_set_direction = sh_pfc_gpio_set_direction,
  395. };
  396. static u32 sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc,
  397. unsigned int pin, unsigned int *offset, unsigned int *size)
  398. {
  399. const struct pinmux_drive_reg_field *field;
  400. const struct pinmux_drive_reg *reg;
  401. unsigned int i;
  402. for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
  403. for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
  404. field = &reg->fields[i];
  405. if (field->size && field->pin == pin) {
  406. *offset = field->offset;
  407. *size = field->size;
  408. return reg->reg;
  409. }
  410. }
  411. }
  412. return 0;
  413. }
  414. static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc *pfc,
  415. unsigned int pin)
  416. {
  417. unsigned long flags;
  418. unsigned int offset;
  419. unsigned int size;
  420. u32 reg;
  421. u32 val;
  422. reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
  423. if (!reg)
  424. return -EINVAL;
  425. spin_lock_irqsave(&pfc->lock, flags);
  426. val = sh_pfc_read_reg(pfc, reg, 32);
  427. spin_unlock_irqrestore(&pfc->lock, flags);
  428. val = (val >> offset) & GENMASK(size - 1, 0);
  429. /* Convert the value to mA based on a full drive strength value of 24mA.
  430. * We can make the full value configurable later if needed.
  431. */
  432. return (val + 1) * (size == 2 ? 6 : 3);
  433. }
  434. static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
  435. unsigned int pin, u16 strength)
  436. {
  437. unsigned long flags;
  438. unsigned int offset;
  439. unsigned int size;
  440. unsigned int step;
  441. u32 reg;
  442. u32 val;
  443. reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
  444. if (!reg)
  445. return -EINVAL;
  446. step = size == 2 ? 6 : 3;
  447. if (strength < step || strength > 24)
  448. return -EINVAL;
  449. /* Convert the value from mA based on a full drive strength value of
  450. * 24mA. We can make the full value configurable later if needed.
  451. */
  452. strength = strength / step - 1;
  453. spin_lock_irqsave(&pfc->lock, flags);
  454. val = sh_pfc_read_reg(pfc, reg, 32);
  455. val &= ~GENMASK(offset + size - 1, offset);
  456. val |= strength << offset;
  457. sh_pfc_write_reg(pfc, reg, 32, val);
  458. spin_unlock_irqrestore(&pfc->lock, flags);
  459. return 0;
  460. }
  461. /* Check whether the requested parameter is supported for a pin. */
  462. static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
  463. enum pin_config_param param)
  464. {
  465. int idx = sh_pfc_get_pin_index(pfc, _pin);
  466. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  467. switch (param) {
  468. case PIN_CONFIG_BIAS_DISABLE:
  469. return pin->configs &
  470. (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
  471. case PIN_CONFIG_BIAS_PULL_UP:
  472. return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
  473. case PIN_CONFIG_BIAS_PULL_DOWN:
  474. return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
  475. case PIN_CONFIG_DRIVE_STRENGTH:
  476. return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
  477. case PIN_CONFIG_POWER_SOURCE:
  478. return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
  479. default:
  480. return false;
  481. }
  482. }
  483. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
  484. unsigned long *config)
  485. {
  486. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  487. struct sh_pfc *pfc = pmx->pfc;
  488. enum pin_config_param param = pinconf_to_config_param(*config);
  489. unsigned long flags;
  490. unsigned int arg;
  491. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  492. return -ENOTSUPP;
  493. switch (param) {
  494. case PIN_CONFIG_BIAS_DISABLE:
  495. case PIN_CONFIG_BIAS_PULL_UP:
  496. case PIN_CONFIG_BIAS_PULL_DOWN: {
  497. unsigned int bias;
  498. if (!pfc->info->ops || !pfc->info->ops->get_bias)
  499. return -ENOTSUPP;
  500. spin_lock_irqsave(&pfc->lock, flags);
  501. bias = pfc->info->ops->get_bias(pfc, _pin);
  502. spin_unlock_irqrestore(&pfc->lock, flags);
  503. if (bias != param)
  504. return -EINVAL;
  505. arg = 0;
  506. break;
  507. }
  508. case PIN_CONFIG_DRIVE_STRENGTH: {
  509. int ret;
  510. ret = sh_pfc_pinconf_get_drive_strength(pfc, _pin);
  511. if (ret < 0)
  512. return ret;
  513. arg = ret;
  514. break;
  515. }
  516. case PIN_CONFIG_POWER_SOURCE: {
  517. u32 pocctrl, val;
  518. int bit;
  519. if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
  520. return -ENOTSUPP;
  521. bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
  522. if (WARN(bit < 0, "invalid pin %#x", _pin))
  523. return bit;
  524. spin_lock_irqsave(&pfc->lock, flags);
  525. val = sh_pfc_read_reg(pfc, pocctrl, 32);
  526. spin_unlock_irqrestore(&pfc->lock, flags);
  527. arg = (val & BIT(bit)) ? 3300 : 1800;
  528. break;
  529. }
  530. default:
  531. return -ENOTSUPP;
  532. }
  533. *config = pinconf_to_config_packed(param, arg);
  534. return 0;
  535. }
  536. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
  537. unsigned long *configs, unsigned num_configs)
  538. {
  539. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  540. struct sh_pfc *pfc = pmx->pfc;
  541. enum pin_config_param param;
  542. unsigned long flags;
  543. unsigned int i;
  544. for (i = 0; i < num_configs; i++) {
  545. param = pinconf_to_config_param(configs[i]);
  546. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  547. return -ENOTSUPP;
  548. switch (param) {
  549. case PIN_CONFIG_BIAS_PULL_UP:
  550. case PIN_CONFIG_BIAS_PULL_DOWN:
  551. case PIN_CONFIG_BIAS_DISABLE:
  552. if (!pfc->info->ops || !pfc->info->ops->set_bias)
  553. return -ENOTSUPP;
  554. spin_lock_irqsave(&pfc->lock, flags);
  555. pfc->info->ops->set_bias(pfc, _pin, param);
  556. spin_unlock_irqrestore(&pfc->lock, flags);
  557. break;
  558. case PIN_CONFIG_DRIVE_STRENGTH: {
  559. unsigned int arg =
  560. pinconf_to_config_argument(configs[i]);
  561. int ret;
  562. ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
  563. if (ret < 0)
  564. return ret;
  565. break;
  566. }
  567. case PIN_CONFIG_POWER_SOURCE: {
  568. unsigned int mV = pinconf_to_config_argument(configs[i]);
  569. u32 pocctrl, val;
  570. int bit;
  571. if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
  572. return -ENOTSUPP;
  573. bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
  574. if (WARN(bit < 0, "invalid pin %#x", _pin))
  575. return bit;
  576. if (mV != 1800 && mV != 3300)
  577. return -EINVAL;
  578. spin_lock_irqsave(&pfc->lock, flags);
  579. val = sh_pfc_read_reg(pfc, pocctrl, 32);
  580. if (mV == 3300)
  581. val |= BIT(bit);
  582. else
  583. val &= ~BIT(bit);
  584. sh_pfc_write_reg(pfc, pocctrl, 32, val);
  585. spin_unlock_irqrestore(&pfc->lock, flags);
  586. break;
  587. }
  588. default:
  589. return -ENOTSUPP;
  590. }
  591. } /* for each config */
  592. return 0;
  593. }
  594. static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  595. unsigned long *configs,
  596. unsigned num_configs)
  597. {
  598. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  599. const unsigned int *pins;
  600. unsigned int num_pins;
  601. unsigned int i;
  602. pins = pmx->pfc->info->groups[group].pins;
  603. num_pins = pmx->pfc->info->groups[group].nr_pins;
  604. for (i = 0; i < num_pins; ++i)
  605. sh_pfc_pinconf_set(pctldev, pins[i], configs, num_configs);
  606. return 0;
  607. }
  608. static const struct pinconf_ops sh_pfc_pinconf_ops = {
  609. .is_generic = true,
  610. .pin_config_get = sh_pfc_pinconf_get,
  611. .pin_config_set = sh_pfc_pinconf_set,
  612. .pin_config_group_set = sh_pfc_pinconf_group_set,
  613. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  614. };
  615. /* PFC ranges -> pinctrl pin descs */
  616. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  617. {
  618. unsigned int i;
  619. /* Allocate and initialize the pins and configs arrays. */
  620. pmx->pins = devm_kzalloc(pfc->dev,
  621. sizeof(*pmx->pins) * pfc->info->nr_pins,
  622. GFP_KERNEL);
  623. if (unlikely(!pmx->pins))
  624. return -ENOMEM;
  625. pmx->configs = devm_kzalloc(pfc->dev,
  626. sizeof(*pmx->configs) * pfc->info->nr_pins,
  627. GFP_KERNEL);
  628. if (unlikely(!pmx->configs))
  629. return -ENOMEM;
  630. for (i = 0; i < pfc->info->nr_pins; ++i) {
  631. const struct sh_pfc_pin *info = &pfc->info->pins[i];
  632. struct sh_pfc_pin_config *cfg = &pmx->configs[i];
  633. struct pinctrl_pin_desc *pin = &pmx->pins[i];
  634. /* If the pin number is equal to -1 all pins are considered */
  635. pin->number = info->pin != (u16)-1 ? info->pin : i;
  636. pin->name = info->name;
  637. cfg->type = PINMUX_TYPE_NONE;
  638. }
  639. return 0;
  640. }
  641. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  642. {
  643. struct sh_pfc_pinctrl *pmx;
  644. int ret;
  645. pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
  646. if (unlikely(!pmx))
  647. return -ENOMEM;
  648. pmx->pfc = pfc;
  649. ret = sh_pfc_map_pins(pfc, pmx);
  650. if (ret < 0)
  651. return ret;
  652. pmx->pctl_desc.name = DRV_NAME;
  653. pmx->pctl_desc.owner = THIS_MODULE;
  654. pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
  655. pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
  656. pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
  657. pmx->pctl_desc.pins = pmx->pins;
  658. pmx->pctl_desc.npins = pfc->info->nr_pins;
  659. pmx->pctl = devm_pinctrl_register(pfc->dev, &pmx->pctl_desc, pmx);
  660. return PTR_ERR_OR_ZERO(pmx->pctl);
  661. }