pfc.c 20 KB

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