pinctrl.c 20 KB

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