pfc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Pin Control driver for SuperH Pin Function Controller.
  4. *
  5. * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
  6. *
  7. * Copyright (C) 2008 Magnus Damm
  8. * Copyright (C) 2009 - 2012 Paul Mundt
  9. * Copyright (C) 2017 Marek Vasut
  10. */
  11. #define DRV_NAME "sh-pfc"
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <dm/pinctrl.h>
  16. #include <linux/io.h>
  17. #include <linux/sizes.h>
  18. #include "sh_pfc.h"
  19. enum sh_pfc_model {
  20. SH_PFC_R8A7790 = 0,
  21. SH_PFC_R8A7791,
  22. SH_PFC_R8A7792,
  23. SH_PFC_R8A7793,
  24. SH_PFC_R8A7794,
  25. SH_PFC_R8A7795,
  26. SH_PFC_R8A7796,
  27. SH_PFC_R8A77970,
  28. SH_PFC_R8A77990,
  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(struct sh_pfc *pfc, u32 reg)
  100. {
  101. return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32);
  102. }
  103. void sh_pfc_write(struct sh_pfc *pfc, u32 reg, 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((void __iomem *)(uintptr_t)reg, 32, 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 pinmux_bias_reg *
  276. sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin,
  277. unsigned int *bit)
  278. {
  279. unsigned int i, j;
  280. for (i = 0; pfc->info->bias_regs[i].puen; i++) {
  281. for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) {
  282. if (pfc->info->bias_regs[i].pins[j] == pin) {
  283. *bit = j;
  284. return &pfc->info->bias_regs[i];
  285. }
  286. }
  287. }
  288. WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
  289. return NULL;
  290. }
  291. static int sh_pfc_init_ranges(struct sh_pfc *pfc)
  292. {
  293. struct sh_pfc_pin_range *range;
  294. unsigned int nr_ranges;
  295. unsigned int i;
  296. if (pfc->info->pins[0].pin == (u16)-1) {
  297. /* Pin number -1 denotes that the SoC doesn't report pin numbers
  298. * in its pin arrays yet. Consider the pin numbers range as
  299. * continuous and allocate a single range.
  300. */
  301. pfc->nr_ranges = 1;
  302. pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
  303. if (pfc->ranges == NULL)
  304. return -ENOMEM;
  305. pfc->ranges->start = 0;
  306. pfc->ranges->end = pfc->info->nr_pins - 1;
  307. pfc->nr_gpio_pins = pfc->info->nr_pins;
  308. return 0;
  309. }
  310. /* Count, allocate and fill the ranges. The PFC SoC data pins array must
  311. * be sorted by pin numbers, and pins without a GPIO port must come
  312. * last.
  313. */
  314. for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
  315. if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
  316. nr_ranges++;
  317. }
  318. pfc->nr_ranges = nr_ranges;
  319. pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
  320. if (pfc->ranges == NULL)
  321. return -ENOMEM;
  322. range = pfc->ranges;
  323. range->start = pfc->info->pins[0].pin;
  324. for (i = 1; i < pfc->info->nr_pins; ++i) {
  325. if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
  326. continue;
  327. range->end = pfc->info->pins[i-1].pin;
  328. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  329. pfc->nr_gpio_pins = range->end + 1;
  330. range++;
  331. range->start = pfc->info->pins[i].pin;
  332. }
  333. range->end = pfc->info->pins[i-1].pin;
  334. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  335. pfc->nr_gpio_pins = range->end + 1;
  336. return 0;
  337. }
  338. static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
  339. {
  340. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  341. return priv->pfc.info->nr_pins;
  342. }
  343. static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
  344. unsigned selector)
  345. {
  346. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  347. return priv->pfc.info->pins[selector].name;
  348. }
  349. static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
  350. {
  351. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  352. return priv->pfc.info->nr_groups;
  353. }
  354. static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
  355. unsigned selector)
  356. {
  357. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  358. return priv->pfc.info->groups[selector].name;
  359. }
  360. static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
  361. {
  362. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  363. return priv->pfc.info->nr_functions;
  364. }
  365. static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
  366. unsigned selector)
  367. {
  368. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  369. return priv->pfc.info->functions[selector].name;
  370. }
  371. int sh_pfc_config_mux_for_gpio(struct udevice *dev, unsigned pin_selector)
  372. {
  373. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  374. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  375. struct sh_pfc *pfc = &priv->pfc;
  376. struct sh_pfc_pin_config *cfg;
  377. const struct sh_pfc_pin *pin = NULL;
  378. int i, idx;
  379. for (i = 1; i < pfc->info->nr_pins; i++) {
  380. if (priv->pfc.info->pins[i].pin != pin_selector)
  381. continue;
  382. pin = &priv->pfc.info->pins[i];
  383. break;
  384. }
  385. if (!pin)
  386. return -EINVAL;
  387. idx = sh_pfc_get_pin_index(pfc, pin->pin);
  388. cfg = &pmx->configs[idx];
  389. if (cfg->type != PINMUX_TYPE_NONE)
  390. return -EBUSY;
  391. return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
  392. }
  393. static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
  394. unsigned func_selector)
  395. {
  396. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  397. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  398. struct sh_pfc *pfc = &priv->pfc;
  399. const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
  400. int idx = sh_pfc_get_pin_index(pfc, pin->pin);
  401. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  402. if (cfg->type != PINMUX_TYPE_NONE)
  403. return -EBUSY;
  404. return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
  405. }
  406. static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
  407. unsigned func_selector)
  408. {
  409. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  410. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  411. struct sh_pfc *pfc = &priv->pfc;
  412. const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
  413. unsigned int i;
  414. int ret = 0;
  415. for (i = 0; i < grp->nr_pins; ++i) {
  416. int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
  417. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  418. if (cfg->type != PINMUX_TYPE_NONE) {
  419. ret = -EBUSY;
  420. goto done;
  421. }
  422. }
  423. for (i = 0; i < grp->nr_pins; ++i) {
  424. ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
  425. if (ret < 0)
  426. break;
  427. }
  428. done:
  429. return ret;
  430. }
  431. #if CONFIG_IS_ENABLED(PINCONF)
  432. static const struct pinconf_param sh_pfc_pinconf_params[] = {
  433. { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
  434. { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
  435. { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
  436. { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
  437. { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
  438. };
  439. static void __iomem *
  440. sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
  441. unsigned int *offset, unsigned int *size)
  442. {
  443. const struct pinmux_drive_reg_field *field;
  444. const struct pinmux_drive_reg *reg;
  445. unsigned int i;
  446. for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
  447. for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
  448. field = &reg->fields[i];
  449. if (field->size && field->pin == pin) {
  450. *offset = field->offset;
  451. *size = field->size;
  452. return (void __iomem *)(uintptr_t)reg->reg;
  453. }
  454. }
  455. }
  456. return NULL;
  457. }
  458. static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
  459. unsigned int pin, u16 strength)
  460. {
  461. unsigned int offset;
  462. unsigned int size;
  463. unsigned int step;
  464. void __iomem *reg;
  465. void __iomem *unlock_reg =
  466. (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
  467. u32 val;
  468. reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
  469. if (!reg)
  470. return -EINVAL;
  471. step = size == 2 ? 6 : 3;
  472. if (strength < step || strength > 24)
  473. return -EINVAL;
  474. /* Convert the value from mA based on a full drive strength value of
  475. * 24mA. We can make the full value configurable later if needed.
  476. */
  477. strength = strength / step - 1;
  478. val = sh_pfc_read_raw_reg(reg, 32);
  479. val &= ~GENMASK(offset + size - 1, offset);
  480. val |= strength << offset;
  481. if (unlock_reg)
  482. sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
  483. sh_pfc_write_raw_reg(reg, 32, val);
  484. return 0;
  485. }
  486. /* Check whether the requested parameter is supported for a pin. */
  487. static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
  488. unsigned int param)
  489. {
  490. int idx = sh_pfc_get_pin_index(pfc, _pin);
  491. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  492. switch (param) {
  493. case PIN_CONFIG_BIAS_DISABLE:
  494. return pin->configs &
  495. (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
  496. case PIN_CONFIG_BIAS_PULL_UP:
  497. return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
  498. case PIN_CONFIG_BIAS_PULL_DOWN:
  499. return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
  500. case PIN_CONFIG_DRIVE_STRENGTH:
  501. return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
  502. case PIN_CONFIG_POWER_SOURCE:
  503. return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
  504. default:
  505. return false;
  506. }
  507. }
  508. static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
  509. unsigned int param, unsigned int arg)
  510. {
  511. struct sh_pfc *pfc = pmx->pfc;
  512. void __iomem *pocctrl;
  513. void __iomem *unlock_reg =
  514. (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
  515. u32 addr, val;
  516. int bit, ret;
  517. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  518. return -ENOTSUPP;
  519. switch (param) {
  520. case PIN_CONFIG_BIAS_PULL_UP:
  521. case PIN_CONFIG_BIAS_PULL_DOWN:
  522. case PIN_CONFIG_BIAS_DISABLE:
  523. if (!pfc->info->ops || !pfc->info->ops->set_bias)
  524. return -ENOTSUPP;
  525. pfc->info->ops->set_bias(pfc, _pin, param);
  526. break;
  527. case PIN_CONFIG_DRIVE_STRENGTH:
  528. ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
  529. if (ret < 0)
  530. return ret;
  531. break;
  532. case PIN_CONFIG_POWER_SOURCE:
  533. if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
  534. return -ENOTSUPP;
  535. bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
  536. if (bit < 0) {
  537. printf("invalid pin %#x", _pin);
  538. return bit;
  539. }
  540. if (arg != 1800 && arg != 3300)
  541. return -EINVAL;
  542. pocctrl = (void __iomem *)(uintptr_t)addr;
  543. val = sh_pfc_read_raw_reg(pocctrl, 32);
  544. if (arg == 3300)
  545. val |= BIT(bit);
  546. else
  547. val &= ~BIT(bit);
  548. if (unlock_reg)
  549. sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
  550. sh_pfc_write_raw_reg(pocctrl, 32, val);
  551. break;
  552. default:
  553. return -ENOTSUPP;
  554. }
  555. return 0;
  556. }
  557. static int sh_pfc_pinconf_pin_set(struct udevice *dev,
  558. unsigned int pin_selector,
  559. unsigned int param, unsigned int arg)
  560. {
  561. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  562. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  563. struct sh_pfc *pfc = &priv->pfc;
  564. const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
  565. sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
  566. return 0;
  567. }
  568. static int sh_pfc_pinconf_group_set(struct udevice *dev,
  569. unsigned int group_selector,
  570. unsigned int param, unsigned int arg)
  571. {
  572. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  573. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  574. struct sh_pfc *pfc = &priv->pfc;
  575. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
  576. unsigned int i;
  577. for (i = 0; i < grp->nr_pins; i++)
  578. sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
  579. return 0;
  580. }
  581. #endif
  582. static struct pinctrl_ops sh_pfc_pinctrl_ops = {
  583. .get_pins_count = sh_pfc_pinctrl_get_pins_count,
  584. .get_pin_name = sh_pfc_pinctrl_get_pin_name,
  585. .get_groups_count = sh_pfc_pinctrl_get_groups_count,
  586. .get_group_name = sh_pfc_pinctrl_get_group_name,
  587. .get_functions_count = sh_pfc_pinctrl_get_functions_count,
  588. .get_function_name = sh_pfc_pinctrl_get_function_name,
  589. #if CONFIG_IS_ENABLED(PINCONF)
  590. .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
  591. .pinconf_params = sh_pfc_pinconf_params,
  592. .pinconf_set = sh_pfc_pinconf_pin_set,
  593. .pinconf_group_set = sh_pfc_pinconf_group_set,
  594. #endif
  595. .pinmux_set = sh_pfc_pinctrl_pin_set,
  596. .pinmux_group_set = sh_pfc_pinctrl_group_set,
  597. .set_state = pinctrl_generic_set_state,
  598. };
  599. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  600. {
  601. unsigned int i;
  602. /* Allocate and initialize the pins and configs arrays. */
  603. pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
  604. GFP_KERNEL);
  605. if (unlikely(!pmx->configs))
  606. return -ENOMEM;
  607. for (i = 0; i < pfc->info->nr_pins; ++i) {
  608. struct sh_pfc_pin_config *cfg = &pmx->configs[i];
  609. cfg->type = PINMUX_TYPE_NONE;
  610. }
  611. return 0;
  612. }
  613. static int sh_pfc_pinctrl_probe(struct udevice *dev)
  614. {
  615. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  616. enum sh_pfc_model model = dev_get_driver_data(dev);
  617. fdt_addr_t base;
  618. base = devfdt_get_addr(dev);
  619. if (base == FDT_ADDR_T_NONE)
  620. return -EINVAL;
  621. priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
  622. if (!priv->pfc.regs)
  623. return -ENOMEM;
  624. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  625. if (model == SH_PFC_R8A7790)
  626. priv->pfc.info = &r8a7790_pinmux_info;
  627. #endif
  628. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  629. if (model == SH_PFC_R8A7791)
  630. priv->pfc.info = &r8a7791_pinmux_info;
  631. #endif
  632. #ifdef CONFIG_PINCTRL_PFC_R8A7792
  633. if (model == SH_PFC_R8A7792)
  634. priv->pfc.info = &r8a7792_pinmux_info;
  635. #endif
  636. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  637. if (model == SH_PFC_R8A7793)
  638. priv->pfc.info = &r8a7793_pinmux_info;
  639. #endif
  640. #ifdef CONFIG_PINCTRL_PFC_R8A7794
  641. if (model == SH_PFC_R8A7794)
  642. priv->pfc.info = &r8a7794_pinmux_info;
  643. #endif
  644. #ifdef CONFIG_PINCTRL_PFC_R8A7795
  645. if (model == SH_PFC_R8A7795)
  646. priv->pfc.info = &r8a7795_pinmux_info;
  647. #endif
  648. #ifdef CONFIG_PINCTRL_PFC_R8A7796
  649. if (model == SH_PFC_R8A7796)
  650. priv->pfc.info = &r8a7796_pinmux_info;
  651. #endif
  652. #ifdef CONFIG_PINCTRL_PFC_R8A77970
  653. if (model == SH_PFC_R8A77970)
  654. priv->pfc.info = &r8a77970_pinmux_info;
  655. #endif
  656. #ifdef CONFIG_PINCTRL_PFC_R8A77990
  657. if (model == SH_PFC_R8A77990)
  658. priv->pfc.info = &r8a77990_pinmux_info;
  659. #endif
  660. #ifdef CONFIG_PINCTRL_PFC_R8A77995
  661. if (model == SH_PFC_R8A77995)
  662. priv->pfc.info = &r8a77995_pinmux_info;
  663. #endif
  664. priv->pmx.pfc = &priv->pfc;
  665. sh_pfc_init_ranges(&priv->pfc);
  666. sh_pfc_map_pins(&priv->pfc, &priv->pmx);
  667. return 0;
  668. }
  669. static const struct udevice_id sh_pfc_pinctrl_ids[] = {
  670. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  671. {
  672. .compatible = "renesas,pfc-r8a7790",
  673. .data = SH_PFC_R8A7790,
  674. },
  675. #endif
  676. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  677. {
  678. .compatible = "renesas,pfc-r8a7791",
  679. .data = SH_PFC_R8A7791,
  680. },
  681. #endif
  682. #ifdef CONFIG_PINCTRL_PFC_R8A7792
  683. {
  684. .compatible = "renesas,pfc-r8a7792",
  685. .data = SH_PFC_R8A7792,
  686. },
  687. #endif
  688. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  689. {
  690. .compatible = "renesas,pfc-r8a7793",
  691. .data = SH_PFC_R8A7793,
  692. },
  693. #endif
  694. #ifdef CONFIG_PINCTRL_PFC_R8A7794
  695. {
  696. .compatible = "renesas,pfc-r8a7794",
  697. .data = SH_PFC_R8A7794,
  698. },
  699. #endif
  700. #ifdef CONFIG_PINCTRL_PFC_R8A7795
  701. {
  702. .compatible = "renesas,pfc-r8a7795",
  703. .data = SH_PFC_R8A7795,
  704. },
  705. #endif
  706. #ifdef CONFIG_PINCTRL_PFC_R8A7796
  707. {
  708. .compatible = "renesas,pfc-r8a7796",
  709. .data = SH_PFC_R8A7796,
  710. }, {
  711. .compatible = "renesas,pfc-r8a77965",
  712. .data = SH_PFC_R8A7796,
  713. },
  714. #endif
  715. #ifdef CONFIG_PINCTRL_PFC_R8A77970
  716. {
  717. .compatible = "renesas,pfc-r8a77970",
  718. .data = SH_PFC_R8A77970,
  719. },
  720. #endif
  721. #ifdef CONFIG_PINCTRL_PFC_R8A77990
  722. {
  723. .compatible = "renesas,pfc-r8a77990",
  724. .data = SH_PFC_R8A77990,
  725. },
  726. #endif
  727. #ifdef CONFIG_PINCTRL_PFC_R8A77995
  728. {
  729. .compatible = "renesas,pfc-r8a77995",
  730. .data = SH_PFC_R8A77995,
  731. },
  732. #endif
  733. { },
  734. };
  735. U_BOOT_DRIVER(pinctrl_sh_pfc) = {
  736. .name = "sh_pfc_pinctrl",
  737. .id = UCLASS_PINCTRL,
  738. .of_match = sh_pfc_pinctrl_ids,
  739. .priv_auto_alloc_size = sizeof(struct sh_pfc_pinctrl_priv),
  740. .ops = &sh_pfc_pinctrl_ops,
  741. .probe = sh_pfc_pinctrl_probe,
  742. };