pinctrl.c 22 KB

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