pfc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * Pin Control driver for SuperH Pin Function Controller.
  3. *
  4. * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
  5. *
  6. * Copyright (C) 2008 Magnus Damm
  7. * Copyright (C) 2009 - 2012 Paul Mundt
  8. * Copyright (C) 2017 Marek Vasut
  9. *
  10. * SPDX-License-Identifier: GPL-2.0
  11. */
  12. #define DRV_NAME "sh-pfc"
  13. #include <common.h>
  14. #include <dm.h>
  15. #include <errno.h>
  16. #include <dm/pinctrl.h>
  17. #include <linux/io.h>
  18. #include <linux/sizes.h>
  19. #include "sh_pfc.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. enum sh_pfc_model {
  22. SH_PFC_R8A7790 = 0,
  23. SH_PFC_R8A7791,
  24. SH_PFC_R8A7792,
  25. SH_PFC_R8A7793,
  26. SH_PFC_R8A7795,
  27. SH_PFC_R8A7796,
  28. SH_PFC_R8A77970,
  29. SH_PFC_R8A77995,
  30. };
  31. struct sh_pfc_pin_config {
  32. u32 type;
  33. };
  34. struct sh_pfc_pinctrl {
  35. struct sh_pfc *pfc;
  36. struct sh_pfc_pin_config *configs;
  37. const char *func_prop_name;
  38. const char *groups_prop_name;
  39. const char *pins_prop_name;
  40. };
  41. struct sh_pfc_pin_range {
  42. u16 start;
  43. u16 end;
  44. };
  45. struct sh_pfc_pinctrl_priv {
  46. struct sh_pfc pfc;
  47. struct sh_pfc_pinctrl pmx;
  48. };
  49. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  50. {
  51. unsigned int offset;
  52. unsigned int i;
  53. for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
  54. const struct sh_pfc_pin_range *range = &pfc->ranges[i];
  55. if (pin <= range->end)
  56. return pin >= range->start
  57. ? offset + pin - range->start : -1;
  58. offset += range->end - range->start + 1;
  59. }
  60. return -EINVAL;
  61. }
  62. static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
  63. {
  64. if (enum_id < r->begin)
  65. return 0;
  66. if (enum_id > r->end)
  67. return 0;
  68. return 1;
  69. }
  70. u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
  71. {
  72. switch (reg_width) {
  73. case 8:
  74. return readb(mapped_reg);
  75. case 16:
  76. return readw(mapped_reg);
  77. case 32:
  78. return readl(mapped_reg);
  79. }
  80. BUG();
  81. return 0;
  82. }
  83. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
  84. u32 data)
  85. {
  86. switch (reg_width) {
  87. case 8:
  88. writeb(data, mapped_reg);
  89. return;
  90. case 16:
  91. writew(data, mapped_reg);
  92. return;
  93. case 32:
  94. writel(data, mapped_reg);
  95. return;
  96. }
  97. BUG();
  98. }
  99. u32 sh_pfc_read_reg(struct sh_pfc *pfc, u32 reg, unsigned int width)
  100. {
  101. return sh_pfc_read_raw_reg(pfc->regs + reg, width);
  102. }
  103. void sh_pfc_write_reg(struct sh_pfc *pfc, u32 reg, unsigned int width, u32 data)
  104. {
  105. void __iomem *unlock_reg =
  106. (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
  107. if (pfc->info->unlock_reg)
  108. sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
  109. sh_pfc_write_raw_reg(pfc->regs + reg, width, data);
  110. }
  111. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  112. const struct pinmux_cfg_reg *crp,
  113. unsigned int in_pos,
  114. void __iomem **mapped_regp, u32 *maskp,
  115. unsigned int *posp)
  116. {
  117. unsigned int k;
  118. *mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
  119. if (crp->field_width) {
  120. *maskp = (1 << crp->field_width) - 1;
  121. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  122. } else {
  123. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  124. *posp = crp->reg_width;
  125. for (k = 0; k <= in_pos; k++)
  126. *posp -= crp->var_field_width[k];
  127. }
  128. }
  129. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  130. const struct pinmux_cfg_reg *crp,
  131. unsigned int field, u32 value)
  132. {
  133. void __iomem *mapped_reg;
  134. void __iomem *unlock_reg =
  135. (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
  136. unsigned int pos;
  137. u32 mask, data;
  138. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  139. dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
  140. "r_width = %u, f_width = %u\n",
  141. crp->reg, value, field, crp->reg_width, crp->field_width);
  142. mask = ~(mask << pos);
  143. value = value << pos;
  144. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  145. data &= mask;
  146. data |= value;
  147. if (pfc->info->unlock_reg)
  148. sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
  149. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  150. }
  151. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
  152. const struct pinmux_cfg_reg **crp,
  153. unsigned int *fieldp, u32 *valuep)
  154. {
  155. unsigned int k = 0;
  156. while (1) {
  157. const struct pinmux_cfg_reg *config_reg =
  158. pfc->info->cfg_regs + k;
  159. unsigned int r_width = config_reg->reg_width;
  160. unsigned int f_width = config_reg->field_width;
  161. unsigned int curr_width;
  162. unsigned int bit_pos;
  163. unsigned int pos = 0;
  164. unsigned int m = 0;
  165. if (!r_width)
  166. break;
  167. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  168. u32 ncomb;
  169. u32 n;
  170. if (f_width)
  171. curr_width = f_width;
  172. else
  173. curr_width = config_reg->var_field_width[m];
  174. ncomb = 1 << curr_width;
  175. for (n = 0; n < ncomb; n++) {
  176. if (config_reg->enum_ids[pos + n] == enum_id) {
  177. *crp = config_reg;
  178. *fieldp = m;
  179. *valuep = n;
  180. return 0;
  181. }
  182. }
  183. pos += ncomb;
  184. m++;
  185. }
  186. k++;
  187. }
  188. return -EINVAL;
  189. }
  190. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
  191. u16 *enum_idp)
  192. {
  193. const u16 *data = pfc->info->pinmux_data;
  194. unsigned int k;
  195. if (pos) {
  196. *enum_idp = data[pos + 1];
  197. return pos + 1;
  198. }
  199. for (k = 0; k < pfc->info->pinmux_data_size; k++) {
  200. if (data[k] == mark) {
  201. *enum_idp = data[k + 1];
  202. return k + 1;
  203. }
  204. }
  205. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  206. mark);
  207. return -EINVAL;
  208. }
  209. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  210. {
  211. const struct pinmux_range *range;
  212. int pos = 0;
  213. switch (pinmux_type) {
  214. case PINMUX_TYPE_GPIO:
  215. case PINMUX_TYPE_FUNCTION:
  216. range = NULL;
  217. break;
  218. case PINMUX_TYPE_OUTPUT:
  219. range = &pfc->info->output;
  220. break;
  221. case PINMUX_TYPE_INPUT:
  222. range = &pfc->info->input;
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. /* Iterate over all the configuration fields we need to update. */
  228. while (1) {
  229. const struct pinmux_cfg_reg *cr;
  230. unsigned int field;
  231. u16 enum_id;
  232. u32 value;
  233. int in_range;
  234. int ret;
  235. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  236. if (pos < 0)
  237. return pos;
  238. if (!enum_id)
  239. break;
  240. /* Check if the configuration field selects a function. If it
  241. * doesn't, skip the field if it's not applicable to the
  242. * requested pinmux type.
  243. */
  244. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  245. if (!in_range) {
  246. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  247. /* Functions are allowed to modify all
  248. * fields.
  249. */
  250. in_range = 1;
  251. } else if (pinmux_type != PINMUX_TYPE_GPIO) {
  252. /* Input/output types can only modify fields
  253. * that correspond to their respective ranges.
  254. */
  255. in_range = sh_pfc_enum_in_range(enum_id, range);
  256. /*
  257. * special case pass through for fixed
  258. * input-only or output-only pins without
  259. * function enum register association.
  260. */
  261. if (in_range && enum_id == range->force)
  262. continue;
  263. }
  264. /* GPIOs are only allowed to modify function fields. */
  265. }
  266. if (!in_range)
  267. continue;
  268. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  269. if (ret < 0)
  270. return ret;
  271. sh_pfc_write_config_reg(pfc, cr, field, value);
  272. }
  273. return 0;
  274. }
  275. const struct sh_pfc_bias_info *
  276. sh_pfc_pin_to_bias_info(const struct sh_pfc_bias_info *info,
  277. unsigned int num, unsigned int pin)
  278. {
  279. unsigned int i;
  280. for (i = 0; i < num; i++)
  281. if (info[i].pin == pin)
  282. return &info[i];
  283. printf("Pin %u is not in bias info list\n", pin);
  284. return NULL;
  285. }
  286. static int sh_pfc_init_ranges(struct sh_pfc *pfc)
  287. {
  288. struct sh_pfc_pin_range *range;
  289. unsigned int nr_ranges;
  290. unsigned int i;
  291. if (pfc->info->pins[0].pin == (u16)-1) {
  292. /* Pin number -1 denotes that the SoC doesn't report pin numbers
  293. * in its pin arrays yet. Consider the pin numbers range as
  294. * continuous and allocate a single range.
  295. */
  296. pfc->nr_ranges = 1;
  297. pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
  298. if (pfc->ranges == NULL)
  299. return -ENOMEM;
  300. pfc->ranges->start = 0;
  301. pfc->ranges->end = pfc->info->nr_pins - 1;
  302. pfc->nr_gpio_pins = pfc->info->nr_pins;
  303. return 0;
  304. }
  305. /* Count, allocate and fill the ranges. The PFC SoC data pins array must
  306. * be sorted by pin numbers, and pins without a GPIO port must come
  307. * last.
  308. */
  309. for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
  310. if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
  311. nr_ranges++;
  312. }
  313. pfc->nr_ranges = nr_ranges;
  314. pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
  315. if (pfc->ranges == NULL)
  316. return -ENOMEM;
  317. range = pfc->ranges;
  318. range->start = pfc->info->pins[0].pin;
  319. for (i = 1; i < pfc->info->nr_pins; ++i) {
  320. if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
  321. continue;
  322. range->end = pfc->info->pins[i-1].pin;
  323. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  324. pfc->nr_gpio_pins = range->end + 1;
  325. range++;
  326. range->start = pfc->info->pins[i].pin;
  327. }
  328. range->end = pfc->info->pins[i-1].pin;
  329. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  330. pfc->nr_gpio_pins = range->end + 1;
  331. return 0;
  332. }
  333. static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
  334. {
  335. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  336. return priv->pfc.info->nr_pins;
  337. }
  338. static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
  339. unsigned selector)
  340. {
  341. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  342. return priv->pfc.info->pins[selector].name;
  343. }
  344. static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
  345. {
  346. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  347. return priv->pfc.info->nr_groups;
  348. }
  349. static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
  350. unsigned selector)
  351. {
  352. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  353. return priv->pfc.info->groups[selector].name;
  354. }
  355. static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
  356. {
  357. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  358. return priv->pfc.info->nr_functions;
  359. }
  360. static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
  361. unsigned selector)
  362. {
  363. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  364. return priv->pfc.info->functions[selector].name;
  365. }
  366. int sh_pfc_config_mux_for_gpio(struct udevice *dev, unsigned pin_selector)
  367. {
  368. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  369. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  370. struct sh_pfc *pfc = &priv->pfc;
  371. struct sh_pfc_pin_config *cfg;
  372. const struct sh_pfc_pin *pin = NULL;
  373. int i, idx;
  374. for (i = 1; i < pfc->info->nr_pins; i++) {
  375. if (priv->pfc.info->pins[i].pin != pin_selector)
  376. continue;
  377. pin = &priv->pfc.info->pins[i];
  378. break;
  379. }
  380. if (!pin)
  381. return -EINVAL;
  382. idx = sh_pfc_get_pin_index(pfc, pin->pin);
  383. cfg = &pmx->configs[idx];
  384. if (cfg->type != PINMUX_TYPE_NONE)
  385. return -EBUSY;
  386. return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
  387. }
  388. static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
  389. unsigned func_selector)
  390. {
  391. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  392. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  393. struct sh_pfc *pfc = &priv->pfc;
  394. const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
  395. int idx = sh_pfc_get_pin_index(pfc, pin->pin);
  396. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  397. if (cfg->type != PINMUX_TYPE_NONE)
  398. return -EBUSY;
  399. return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
  400. }
  401. static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
  402. unsigned func_selector)
  403. {
  404. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  405. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  406. struct sh_pfc *pfc = &priv->pfc;
  407. const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
  408. unsigned int i;
  409. int ret = 0;
  410. for (i = 0; i < grp->nr_pins; ++i) {
  411. int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
  412. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  413. if (cfg->type != PINMUX_TYPE_NONE) {
  414. ret = -EBUSY;
  415. goto done;
  416. }
  417. }
  418. for (i = 0; i < grp->nr_pins; ++i) {
  419. ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
  420. if (ret < 0)
  421. break;
  422. }
  423. done:
  424. return ret;
  425. }
  426. #if CONFIG_IS_ENABLED(PINCONF)
  427. static const struct pinconf_param sh_pfc_pinconf_params[] = {
  428. { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
  429. { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
  430. { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
  431. { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
  432. { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
  433. };
  434. static void __iomem *
  435. sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
  436. unsigned int *offset, unsigned int *size)
  437. {
  438. const struct pinmux_drive_reg_field *field;
  439. const struct pinmux_drive_reg *reg;
  440. unsigned int i;
  441. for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
  442. for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
  443. field = &reg->fields[i];
  444. if (field->size && field->pin == pin) {
  445. *offset = field->offset;
  446. *size = field->size;
  447. return (void __iomem *)(uintptr_t)reg->reg;
  448. }
  449. }
  450. }
  451. return NULL;
  452. }
  453. static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
  454. unsigned int pin, u16 strength)
  455. {
  456. unsigned int offset;
  457. unsigned int size;
  458. unsigned int step;
  459. void __iomem *reg;
  460. void __iomem *unlock_reg =
  461. (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
  462. u32 val;
  463. reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
  464. if (!reg)
  465. return -EINVAL;
  466. step = size == 2 ? 6 : 3;
  467. if (strength < step || strength > 24)
  468. return -EINVAL;
  469. /* Convert the value from mA based on a full drive strength value of
  470. * 24mA. We can make the full value configurable later if needed.
  471. */
  472. strength = strength / step - 1;
  473. val = sh_pfc_read_raw_reg(reg, 32);
  474. val &= ~GENMASK(offset + size - 1, offset);
  475. val |= strength << offset;
  476. if (unlock_reg)
  477. sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
  478. sh_pfc_write_raw_reg(reg, 32, val);
  479. return 0;
  480. }
  481. /* Check whether the requested parameter is supported for a pin. */
  482. static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
  483. unsigned int param)
  484. {
  485. int idx = sh_pfc_get_pin_index(pfc, _pin);
  486. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  487. switch (param) {
  488. case PIN_CONFIG_BIAS_DISABLE:
  489. return pin->configs &
  490. (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
  491. case PIN_CONFIG_BIAS_PULL_UP:
  492. return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
  493. case PIN_CONFIG_BIAS_PULL_DOWN:
  494. return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
  495. case PIN_CONFIG_DRIVE_STRENGTH:
  496. return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
  497. case PIN_CONFIG_POWER_SOURCE:
  498. return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
  499. default:
  500. return false;
  501. }
  502. }
  503. static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
  504. unsigned int param, unsigned int arg)
  505. {
  506. struct sh_pfc *pfc = pmx->pfc;
  507. void __iomem *pocctrl;
  508. void __iomem *unlock_reg =
  509. (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
  510. u32 addr, val;
  511. int bit, ret;
  512. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  513. return -ENOTSUPP;
  514. switch (param) {
  515. case PIN_CONFIG_BIAS_PULL_UP:
  516. case PIN_CONFIG_BIAS_PULL_DOWN:
  517. case PIN_CONFIG_BIAS_DISABLE:
  518. if (!pfc->info->ops || !pfc->info->ops->set_bias)
  519. return -ENOTSUPP;
  520. pfc->info->ops->set_bias(pfc, _pin, param);
  521. break;
  522. case PIN_CONFIG_DRIVE_STRENGTH:
  523. ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
  524. if (ret < 0)
  525. return ret;
  526. break;
  527. case PIN_CONFIG_POWER_SOURCE:
  528. if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
  529. return -ENOTSUPP;
  530. bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
  531. if (bit < 0) {
  532. printf("invalid pin %#x", _pin);
  533. return bit;
  534. }
  535. if (arg != 1800 && arg != 3300)
  536. return -EINVAL;
  537. pocctrl = (void __iomem *)(uintptr_t)addr;
  538. val = sh_pfc_read_raw_reg(pocctrl, 32);
  539. if (arg == 3300)
  540. val |= BIT(bit);
  541. else
  542. val &= ~BIT(bit);
  543. if (unlock_reg)
  544. sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
  545. sh_pfc_write_raw_reg(pocctrl, 32, val);
  546. break;
  547. default:
  548. return -ENOTSUPP;
  549. }
  550. return 0;
  551. }
  552. static int sh_pfc_pinconf_pin_set(struct udevice *dev,
  553. unsigned int pin_selector,
  554. unsigned int param, unsigned int arg)
  555. {
  556. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  557. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  558. struct sh_pfc *pfc = &priv->pfc;
  559. const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
  560. sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
  561. return 0;
  562. }
  563. static int sh_pfc_pinconf_group_set(struct udevice *dev,
  564. unsigned int group_selector,
  565. unsigned int param, unsigned int arg)
  566. {
  567. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  568. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  569. struct sh_pfc *pfc = &priv->pfc;
  570. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
  571. unsigned int i;
  572. for (i = 0; i < grp->nr_pins; i++)
  573. sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
  574. return 0;
  575. }
  576. #endif
  577. static struct pinctrl_ops sh_pfc_pinctrl_ops = {
  578. .get_pins_count = sh_pfc_pinctrl_get_pins_count,
  579. .get_pin_name = sh_pfc_pinctrl_get_pin_name,
  580. .get_groups_count = sh_pfc_pinctrl_get_groups_count,
  581. .get_group_name = sh_pfc_pinctrl_get_group_name,
  582. .get_functions_count = sh_pfc_pinctrl_get_functions_count,
  583. .get_function_name = sh_pfc_pinctrl_get_function_name,
  584. #if CONFIG_IS_ENABLED(PINCONF)
  585. .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
  586. .pinconf_params = sh_pfc_pinconf_params,
  587. .pinconf_set = sh_pfc_pinconf_pin_set,
  588. .pinconf_group_set = sh_pfc_pinconf_group_set,
  589. #endif
  590. .pinmux_set = sh_pfc_pinctrl_pin_set,
  591. .pinmux_group_set = sh_pfc_pinctrl_group_set,
  592. .set_state = pinctrl_generic_set_state,
  593. };
  594. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  595. {
  596. unsigned int i;
  597. /* Allocate and initialize the pins and configs arrays. */
  598. pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
  599. GFP_KERNEL);
  600. if (unlikely(!pmx->configs))
  601. return -ENOMEM;
  602. for (i = 0; i < pfc->info->nr_pins; ++i) {
  603. struct sh_pfc_pin_config *cfg = &pmx->configs[i];
  604. cfg->type = PINMUX_TYPE_NONE;
  605. }
  606. return 0;
  607. }
  608. static int sh_pfc_pinctrl_probe(struct udevice *dev)
  609. {
  610. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  611. enum sh_pfc_model model = dev_get_driver_data(dev);
  612. fdt_addr_t base;
  613. base = devfdt_get_addr(dev);
  614. if (base == FDT_ADDR_T_NONE)
  615. return -EINVAL;
  616. priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
  617. if (!priv->pfc.regs)
  618. return -ENOMEM;
  619. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  620. if (model == SH_PFC_R8A7790)
  621. priv->pfc.info = &r8a7790_pinmux_info;
  622. #endif
  623. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  624. if (model == SH_PFC_R8A7791)
  625. priv->pfc.info = &r8a7791_pinmux_info;
  626. #endif
  627. #ifdef CONFIG_PINCTRL_PFC_R8A7792
  628. if (model == SH_PFC_R8A7792)
  629. priv->pfc.info = &r8a7792_pinmux_info;
  630. #endif
  631. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  632. if (model == SH_PFC_R8A7793)
  633. priv->pfc.info = &r8a7793_pinmux_info;
  634. #endif
  635. #ifdef CONFIG_PINCTRL_PFC_R8A7795
  636. if (model == SH_PFC_R8A7795)
  637. priv->pfc.info = &r8a7795_pinmux_info;
  638. #endif
  639. #ifdef CONFIG_PINCTRL_PFC_R8A7796
  640. if (model == SH_PFC_R8A7796)
  641. priv->pfc.info = &r8a7796_pinmux_info;
  642. #endif
  643. #ifdef CONFIG_PINCTRL_PFC_R8A77970
  644. if (model == SH_PFC_R8A77970)
  645. priv->pfc.info = &r8a77970_pinmux_info;
  646. #endif
  647. #ifdef CONFIG_PINCTRL_PFC_R8A77995
  648. if (model == SH_PFC_R8A77995)
  649. priv->pfc.info = &r8a77995_pinmux_info;
  650. #endif
  651. priv->pmx.pfc = &priv->pfc;
  652. sh_pfc_init_ranges(&priv->pfc);
  653. sh_pfc_map_pins(&priv->pfc, &priv->pmx);
  654. return 0;
  655. }
  656. static const struct udevice_id sh_pfc_pinctrl_ids[] = {
  657. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  658. {
  659. .compatible = "renesas,pfc-r8a7790",
  660. .data = SH_PFC_R8A7790,
  661. },
  662. #endif
  663. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  664. {
  665. .compatible = "renesas,pfc-r8a7791",
  666. .data = SH_PFC_R8A7791,
  667. },
  668. #endif
  669. #ifdef CONFIG_PINCTRL_PFC_R8A7792
  670. {
  671. .compatible = "renesas,pfc-r8a7792",
  672. .data = SH_PFC_R8A7792,
  673. },
  674. #endif
  675. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  676. {
  677. .compatible = "renesas,pfc-r8a7793",
  678. .data = SH_PFC_R8A7793,
  679. },
  680. #endif
  681. #ifdef CONFIG_PINCTRL_PFC_R8A7795
  682. {
  683. .compatible = "renesas,pfc-r8a7795",
  684. .data = SH_PFC_R8A7795,
  685. },
  686. #endif
  687. #ifdef CONFIG_PINCTRL_PFC_R8A7796
  688. {
  689. .compatible = "renesas,pfc-r8a7796",
  690. .data = SH_PFC_R8A7796,
  691. },
  692. #endif
  693. #ifdef CONFIG_PINCTRL_PFC_R8A77970
  694. {
  695. .compatible = "renesas,pfc-r8a77970",
  696. .data = SH_PFC_R8A77970,
  697. },
  698. #endif
  699. #ifdef CONFIG_PINCTRL_PFC_R8A77995
  700. {
  701. .compatible = "renesas,pfc-r8a77995",
  702. .data = SH_PFC_R8A77995,
  703. },
  704. #endif
  705. { },
  706. };
  707. U_BOOT_DRIVER(pinctrl_sh_pfc) = {
  708. .name = "sh_pfc_pinctrl",
  709. .id = UCLASS_PINCTRL,
  710. .of_match = sh_pfc_pinctrl_ids,
  711. .priv_auto_alloc_size = sizeof(struct sh_pfc_pinctrl_priv),
  712. .ops = &sh_pfc_pinctrl_ops,
  713. .probe = sh_pfc_pinctrl_probe,
  714. };