core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Pin Control and GPIO 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. */
  10. #define DRV_NAME "sh-pfc"
  11. #include <linux/bitops.h>
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/kernel.h>
  18. #include <linux/math.h>
  19. #include <linux/of.h>
  20. #include <linux/pinctrl/machine.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/psci.h>
  23. #include <linux/slab.h>
  24. #include <linux/sys_soc.h>
  25. #include "core.h"
  26. static int sh_pfc_map_resources(struct sh_pfc *pfc,
  27. struct platform_device *pdev)
  28. {
  29. struct sh_pfc_window *windows;
  30. unsigned int *irqs = NULL;
  31. unsigned int num_windows;
  32. struct resource *res;
  33. unsigned int i;
  34. int num_irqs;
  35. /* Count the MEM and IRQ resources. */
  36. for (num_windows = 0;; num_windows++) {
  37. res = platform_get_resource(pdev, IORESOURCE_MEM, num_windows);
  38. if (!res)
  39. break;
  40. }
  41. if (num_windows == 0)
  42. return -EINVAL;
  43. num_irqs = platform_irq_count(pdev);
  44. if (num_irqs < 0)
  45. return num_irqs;
  46. /* Allocate memory windows and IRQs arrays. */
  47. windows = devm_kcalloc(pfc->dev, num_windows, sizeof(*windows),
  48. GFP_KERNEL);
  49. if (windows == NULL)
  50. return -ENOMEM;
  51. pfc->num_windows = num_windows;
  52. pfc->windows = windows;
  53. if (num_irqs) {
  54. irqs = devm_kcalloc(pfc->dev, num_irqs, sizeof(*irqs),
  55. GFP_KERNEL);
  56. if (irqs == NULL)
  57. return -ENOMEM;
  58. pfc->num_irqs = num_irqs;
  59. pfc->irqs = irqs;
  60. }
  61. /* Fill them. */
  62. for (i = 0; i < num_windows; i++) {
  63. windows->virt = devm_platform_get_and_ioremap_resource(pdev, i, &res);
  64. if (IS_ERR(windows->virt))
  65. return -ENOMEM;
  66. windows->phys = res->start;
  67. windows->size = resource_size(res);
  68. windows++;
  69. }
  70. for (i = 0; i < num_irqs; i++)
  71. *irqs++ = platform_get_irq(pdev, i);
  72. return 0;
  73. }
  74. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc, u32 reg)
  75. {
  76. struct sh_pfc_window *window;
  77. phys_addr_t address = reg;
  78. unsigned int i;
  79. /* scan through physical windows and convert address */
  80. for (i = 0; i < pfc->num_windows; i++) {
  81. window = pfc->windows + i;
  82. if (address < window->phys)
  83. continue;
  84. if (address >= (window->phys + window->size))
  85. continue;
  86. return window->virt + (address - window->phys);
  87. }
  88. BUG();
  89. return NULL;
  90. }
  91. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  92. {
  93. unsigned int offset;
  94. unsigned int i;
  95. for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
  96. const struct sh_pfc_pin_range *range = &pfc->ranges[i];
  97. if (pin <= range->end)
  98. return pin >= range->start
  99. ? offset + pin - range->start : -1;
  100. offset += range->end - range->start + 1;
  101. }
  102. return -EINVAL;
  103. }
  104. static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
  105. {
  106. if (enum_id < r->begin)
  107. return 0;
  108. if (enum_id > r->end)
  109. return 0;
  110. return 1;
  111. }
  112. u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
  113. {
  114. switch (reg_width) {
  115. case 8:
  116. return ioread8(mapped_reg);
  117. case 16:
  118. return ioread16(mapped_reg);
  119. case 32:
  120. return ioread32(mapped_reg);
  121. }
  122. BUG();
  123. return 0;
  124. }
  125. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
  126. u32 data)
  127. {
  128. switch (reg_width) {
  129. case 8:
  130. iowrite8(data, mapped_reg);
  131. return;
  132. case 16:
  133. iowrite16(data, mapped_reg);
  134. return;
  135. case 32:
  136. iowrite32(data, mapped_reg);
  137. return;
  138. }
  139. BUG();
  140. }
  141. u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
  142. {
  143. return sh_pfc_read_raw_reg(sh_pfc_phys_to_virt(pfc, reg), 32);
  144. }
  145. static void sh_pfc_unlock_reg(struct sh_pfc *pfc, u32 reg, u32 data)
  146. {
  147. u32 unlock;
  148. if (!pfc->info->unlock_reg)
  149. return;
  150. if (pfc->info->unlock_reg >= 0x80000000UL)
  151. unlock = pfc->info->unlock_reg;
  152. else
  153. /* unlock_reg is a mask */
  154. unlock = reg & ~pfc->info->unlock_reg;
  155. sh_pfc_write_raw_reg(sh_pfc_phys_to_virt(pfc, unlock), 32, ~data);
  156. }
  157. void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
  158. {
  159. sh_pfc_unlock_reg(pfc, reg, data);
  160. sh_pfc_write_raw_reg(sh_pfc_phys_to_virt(pfc, reg), 32, data);
  161. }
  162. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  163. const struct pinmux_cfg_reg *crp,
  164. unsigned int in_pos,
  165. void __iomem **mapped_regp, u32 *maskp,
  166. unsigned int *posp)
  167. {
  168. unsigned int k;
  169. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  170. if (crp->field_width) {
  171. *maskp = (1 << crp->field_width) - 1;
  172. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  173. } else {
  174. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  175. *posp = crp->reg_width;
  176. for (k = 0; k <= in_pos; k++)
  177. *posp -= abs(crp->var_field_width[k]);
  178. }
  179. }
  180. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  181. const struct pinmux_cfg_reg *crp,
  182. unsigned int field, u32 value)
  183. {
  184. void __iomem *mapped_reg;
  185. unsigned int pos;
  186. u32 mask, data;
  187. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  188. dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
  189. "r_width = %u, f_width = %u\n",
  190. crp->reg, value, field, crp->reg_width, hweight32(mask));
  191. mask = ~(mask << pos);
  192. value = value << pos;
  193. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  194. data &= mask;
  195. data |= value;
  196. sh_pfc_unlock_reg(pfc, crp->reg, data);
  197. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  198. }
  199. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
  200. const struct pinmux_cfg_reg **crp,
  201. unsigned int *fieldp, u32 *valuep)
  202. {
  203. unsigned int k = 0;
  204. while (1) {
  205. const struct pinmux_cfg_reg *config_reg =
  206. pfc->info->cfg_regs + k;
  207. unsigned int r_width = config_reg->reg_width;
  208. unsigned int f_width = config_reg->field_width;
  209. unsigned int curr_width;
  210. unsigned int bit_pos;
  211. unsigned int pos = 0;
  212. unsigned int m = 0;
  213. if (!r_width)
  214. break;
  215. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width, m++) {
  216. u32 ncomb;
  217. u32 n;
  218. if (f_width) {
  219. curr_width = f_width;
  220. } else {
  221. curr_width = abs(config_reg->var_field_width[m]);
  222. if (config_reg->var_field_width[m] < 0)
  223. continue;
  224. }
  225. ncomb = 1 << curr_width;
  226. for (n = 0; n < ncomb; n++) {
  227. if (config_reg->enum_ids[pos + n] == enum_id) {
  228. *crp = config_reg;
  229. *fieldp = m;
  230. *valuep = n;
  231. return 0;
  232. }
  233. }
  234. pos += ncomb;
  235. }
  236. k++;
  237. }
  238. return -EINVAL;
  239. }
  240. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
  241. u16 *enum_idp)
  242. {
  243. const u16 *data = pfc->info->pinmux_data;
  244. unsigned int k;
  245. if (pos) {
  246. *enum_idp = data[pos + 1];
  247. return pos + 1;
  248. }
  249. for (k = 0; k < pfc->info->pinmux_data_size; k++) {
  250. if (data[k] == mark) {
  251. *enum_idp = data[k + 1];
  252. return k + 1;
  253. }
  254. }
  255. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  256. mark);
  257. return -EINVAL;
  258. }
  259. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  260. {
  261. const struct pinmux_range *range;
  262. int pos = 0;
  263. switch (pinmux_type) {
  264. case PINMUX_TYPE_GPIO:
  265. case PINMUX_TYPE_FUNCTION:
  266. range = NULL;
  267. break;
  268. #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
  269. case PINMUX_TYPE_OUTPUT:
  270. range = &pfc->info->output;
  271. break;
  272. case PINMUX_TYPE_INPUT:
  273. range = &pfc->info->input;
  274. break;
  275. #endif /* CONFIG_PINCTRL_SH_PFC_GPIO */
  276. default:
  277. return -EINVAL;
  278. }
  279. /* Iterate over all the configuration fields we need to update. */
  280. while (1) {
  281. const struct pinmux_cfg_reg *cr;
  282. unsigned int field;
  283. u16 enum_id;
  284. u32 value;
  285. int in_range;
  286. int ret;
  287. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  288. if (pos < 0)
  289. return pos;
  290. if (!enum_id)
  291. break;
  292. /* Check if the configuration field selects a function. If it
  293. * doesn't, skip the field if it's not applicable to the
  294. * requested pinmux type.
  295. */
  296. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  297. if (!in_range) {
  298. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  299. /* Functions are allowed to modify all
  300. * fields.
  301. */
  302. in_range = 1;
  303. } else if (pinmux_type != PINMUX_TYPE_GPIO) {
  304. /* Input/output types can only modify fields
  305. * that correspond to their respective ranges.
  306. */
  307. in_range = sh_pfc_enum_in_range(enum_id, range);
  308. /*
  309. * special case pass through for fixed
  310. * input-only or output-only pins without
  311. * function enum register association.
  312. */
  313. if (in_range && enum_id == range->force)
  314. continue;
  315. }
  316. /* GPIOs are only allowed to modify function fields. */
  317. }
  318. if (!in_range)
  319. continue;
  320. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  321. if (ret < 0)
  322. return ret;
  323. sh_pfc_write_config_reg(pfc, cr, field, value);
  324. }
  325. return 0;
  326. }
  327. static int sh_pfc_init_ranges(struct sh_pfc *pfc)
  328. {
  329. struct sh_pfc_pin_range *range;
  330. unsigned int nr_ranges;
  331. unsigned int i;
  332. if (pfc->info->pins[0].pin == (u16)-1) {
  333. /* Pin number -1 denotes that the SoC doesn't report pin numbers
  334. * in its pin arrays yet. Consider the pin numbers range as
  335. * continuous and allocate a single range.
  336. */
  337. pfc->nr_ranges = 1;
  338. pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
  339. GFP_KERNEL);
  340. if (pfc->ranges == NULL)
  341. return -ENOMEM;
  342. pfc->ranges->start = 0;
  343. pfc->ranges->end = pfc->info->nr_pins - 1;
  344. pfc->nr_gpio_pins = pfc->info->nr_pins;
  345. return 0;
  346. }
  347. /* Count, allocate and fill the ranges. The PFC SoC data pins array must
  348. * be sorted by pin numbers, and pins without a GPIO port must come
  349. * last.
  350. */
  351. for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
  352. if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
  353. nr_ranges++;
  354. }
  355. pfc->nr_ranges = nr_ranges;
  356. pfc->ranges = devm_kcalloc(pfc->dev, nr_ranges, sizeof(*pfc->ranges),
  357. GFP_KERNEL);
  358. if (pfc->ranges == NULL)
  359. return -ENOMEM;
  360. range = pfc->ranges;
  361. range->start = pfc->info->pins[0].pin;
  362. for (i = 1; i < pfc->info->nr_pins; ++i) {
  363. if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
  364. continue;
  365. range->end = pfc->info->pins[i-1].pin;
  366. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  367. pfc->nr_gpio_pins = range->end + 1;
  368. range++;
  369. range->start = pfc->info->pins[i].pin;
  370. }
  371. range->end = pfc->info->pins[i-1].pin;
  372. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  373. pfc->nr_gpio_pins = range->end + 1;
  374. return 0;
  375. }
  376. #ifdef CONFIG_OF
  377. static const struct of_device_id sh_pfc_of_table[] = {
  378. #ifdef CONFIG_PINCTRL_PFC_EMEV2
  379. {
  380. .compatible = "renesas,pfc-emev2",
  381. .data = &emev2_pinmux_info,
  382. },
  383. #endif
  384. #ifdef CONFIG_PINCTRL_PFC_R8A73A4
  385. {
  386. .compatible = "renesas,pfc-r8a73a4",
  387. .data = &r8a73a4_pinmux_info,
  388. },
  389. #endif
  390. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  391. {
  392. .compatible = "renesas,pfc-r8a7740",
  393. .data = &r8a7740_pinmux_info,
  394. },
  395. #endif
  396. #ifdef CONFIG_PINCTRL_PFC_R8A7742
  397. {
  398. .compatible = "renesas,pfc-r8a7742",
  399. .data = &r8a7742_pinmux_info,
  400. },
  401. #endif
  402. #ifdef CONFIG_PINCTRL_PFC_R8A7743
  403. {
  404. .compatible = "renesas,pfc-r8a7743",
  405. .data = &r8a7743_pinmux_info,
  406. },
  407. #endif
  408. #ifdef CONFIG_PINCTRL_PFC_R8A7744
  409. {
  410. .compatible = "renesas,pfc-r8a7744",
  411. .data = &r8a7744_pinmux_info,
  412. },
  413. #endif
  414. #ifdef CONFIG_PINCTRL_PFC_R8A7745
  415. {
  416. .compatible = "renesas,pfc-r8a7745",
  417. .data = &r8a7745_pinmux_info,
  418. },
  419. #endif
  420. #ifdef CONFIG_PINCTRL_PFC_R8A77470
  421. {
  422. .compatible = "renesas,pfc-r8a77470",
  423. .data = &r8a77470_pinmux_info,
  424. },
  425. #endif
  426. #ifdef CONFIG_PINCTRL_PFC_R8A774A1
  427. {
  428. .compatible = "renesas,pfc-r8a774a1",
  429. .data = &r8a774a1_pinmux_info,
  430. },
  431. #endif
  432. #ifdef CONFIG_PINCTRL_PFC_R8A774B1
  433. {
  434. .compatible = "renesas,pfc-r8a774b1",
  435. .data = &r8a774b1_pinmux_info,
  436. },
  437. #endif
  438. #ifdef CONFIG_PINCTRL_PFC_R8A774C0
  439. {
  440. .compatible = "renesas,pfc-r8a774c0",
  441. .data = &r8a774c0_pinmux_info,
  442. },
  443. #endif
  444. #ifdef CONFIG_PINCTRL_PFC_R8A774E1
  445. {
  446. .compatible = "renesas,pfc-r8a774e1",
  447. .data = &r8a774e1_pinmux_info,
  448. },
  449. #endif
  450. #ifdef CONFIG_PINCTRL_PFC_R8A7778
  451. {
  452. .compatible = "renesas,pfc-r8a7778",
  453. .data = &r8a7778_pinmux_info,
  454. },
  455. #endif
  456. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  457. {
  458. .compatible = "renesas,pfc-r8a7779",
  459. .data = &r8a7779_pinmux_info,
  460. },
  461. #endif
  462. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  463. {
  464. .compatible = "renesas,pfc-r8a7790",
  465. .data = &r8a7790_pinmux_info,
  466. },
  467. #endif
  468. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  469. {
  470. .compatible = "renesas,pfc-r8a7791",
  471. .data = &r8a7791_pinmux_info,
  472. },
  473. #endif
  474. #ifdef CONFIG_PINCTRL_PFC_R8A7792
  475. {
  476. .compatible = "renesas,pfc-r8a7792",
  477. .data = &r8a7792_pinmux_info,
  478. },
  479. #endif
  480. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  481. {
  482. .compatible = "renesas,pfc-r8a7793",
  483. .data = &r8a7793_pinmux_info,
  484. },
  485. #endif
  486. #ifdef CONFIG_PINCTRL_PFC_R8A7794
  487. {
  488. .compatible = "renesas,pfc-r8a7794",
  489. .data = &r8a7794_pinmux_info,
  490. },
  491. #endif
  492. #ifdef CONFIG_PINCTRL_PFC_R8A77951
  493. {
  494. .compatible = "renesas,pfc-r8a7795",
  495. .data = &r8a77951_pinmux_info,
  496. },
  497. #endif
  498. #ifdef CONFIG_PINCTRL_PFC_R8A77960
  499. {
  500. .compatible = "renesas,pfc-r8a7796",
  501. .data = &r8a77960_pinmux_info,
  502. },
  503. #endif
  504. #ifdef CONFIG_PINCTRL_PFC_R8A77961
  505. {
  506. .compatible = "renesas,pfc-r8a77961",
  507. .data = &r8a77961_pinmux_info,
  508. },
  509. #endif
  510. #ifdef CONFIG_PINCTRL_PFC_R8A77965
  511. {
  512. .compatible = "renesas,pfc-r8a77965",
  513. .data = &r8a77965_pinmux_info,
  514. },
  515. #endif
  516. #ifdef CONFIG_PINCTRL_PFC_R8A77970
  517. {
  518. .compatible = "renesas,pfc-r8a77970",
  519. .data = &r8a77970_pinmux_info,
  520. },
  521. #endif
  522. #ifdef CONFIG_PINCTRL_PFC_R8A77980
  523. {
  524. .compatible = "renesas,pfc-r8a77980",
  525. .data = &r8a77980_pinmux_info,
  526. },
  527. #endif
  528. #ifdef CONFIG_PINCTRL_PFC_R8A77990
  529. {
  530. .compatible = "renesas,pfc-r8a77990",
  531. .data = &r8a77990_pinmux_info,
  532. },
  533. #endif
  534. #ifdef CONFIG_PINCTRL_PFC_R8A77995
  535. {
  536. .compatible = "renesas,pfc-r8a77995",
  537. .data = &r8a77995_pinmux_info,
  538. },
  539. #endif
  540. #ifdef CONFIG_PINCTRL_PFC_R8A779A0
  541. {
  542. .compatible = "renesas,pfc-r8a779a0",
  543. .data = &r8a779a0_pinmux_info,
  544. },
  545. #endif
  546. #ifdef CONFIG_PINCTRL_PFC_R8A779F0
  547. {
  548. .compatible = "renesas,pfc-r8a779f0",
  549. .data = &r8a779f0_pinmux_info,
  550. },
  551. #endif
  552. #ifdef CONFIG_PINCTRL_PFC_R8A779G0
  553. {
  554. .compatible = "renesas,pfc-r8a779g0",
  555. .data = &r8a779g0_pinmux_info,
  556. },
  557. #endif
  558. #ifdef CONFIG_PINCTRL_PFC_R8A779H0
  559. {
  560. .compatible = "renesas,pfc-r8a779h0",
  561. .data = &r8a779h0_pinmux_info,
  562. },
  563. #endif
  564. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  565. {
  566. .compatible = "renesas,pfc-sh73a0",
  567. .data = &sh73a0_pinmux_info,
  568. },
  569. #endif
  570. { /* sentinel */ }
  571. };
  572. #endif
  573. #if defined(CONFIG_ARM_PSCI_FW)
  574. static void sh_pfc_nop_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
  575. {
  576. }
  577. static void sh_pfc_save_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
  578. {
  579. pfc->saved_regs[idx] = sh_pfc_read(pfc, reg);
  580. }
  581. static void sh_pfc_restore_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
  582. {
  583. sh_pfc_write(pfc, reg, pfc->saved_regs[idx]);
  584. }
  585. static unsigned int sh_pfc_walk_regs(struct sh_pfc *pfc,
  586. void (*do_reg)(struct sh_pfc *pfc, u32 reg, unsigned int idx))
  587. {
  588. unsigned int i, n = 0;
  589. if (pfc->info->cfg_regs)
  590. for (i = 0; pfc->info->cfg_regs[i].reg; i++)
  591. do_reg(pfc, pfc->info->cfg_regs[i].reg, n++);
  592. if (pfc->info->drive_regs)
  593. for (i = 0; pfc->info->drive_regs[i].reg; i++)
  594. do_reg(pfc, pfc->info->drive_regs[i].reg, n++);
  595. if (pfc->info->bias_regs)
  596. for (i = 0; pfc->info->bias_regs[i].puen ||
  597. pfc->info->bias_regs[i].pud; i++) {
  598. if (pfc->info->bias_regs[i].puen)
  599. do_reg(pfc, pfc->info->bias_regs[i].puen, n++);
  600. if (pfc->info->bias_regs[i].pud)
  601. do_reg(pfc, pfc->info->bias_regs[i].pud, n++);
  602. }
  603. if (pfc->info->ioctrl_regs)
  604. for (i = 0; pfc->info->ioctrl_regs[i].reg; i++)
  605. do_reg(pfc, pfc->info->ioctrl_regs[i].reg, n++);
  606. return n;
  607. }
  608. static int sh_pfc_suspend_init(struct sh_pfc *pfc)
  609. {
  610. unsigned int n;
  611. /* This is the best we can do to check for the presence of PSCI */
  612. if (!psci_ops.cpu_suspend)
  613. return 0;
  614. n = sh_pfc_walk_regs(pfc, sh_pfc_nop_reg);
  615. if (!n)
  616. return 0;
  617. pfc->saved_regs = devm_kmalloc_array(pfc->dev, n,
  618. sizeof(*pfc->saved_regs),
  619. GFP_KERNEL);
  620. if (!pfc->saved_regs)
  621. return -ENOMEM;
  622. dev_dbg(pfc->dev, "Allocated space to save %u regs\n", n);
  623. return 0;
  624. }
  625. static int sh_pfc_suspend_noirq(struct device *dev)
  626. {
  627. struct sh_pfc *pfc = dev_get_drvdata(dev);
  628. if (pfc->saved_regs)
  629. sh_pfc_walk_regs(pfc, sh_pfc_save_reg);
  630. return 0;
  631. }
  632. static int sh_pfc_resume_noirq(struct device *dev)
  633. {
  634. struct sh_pfc *pfc = dev_get_drvdata(dev);
  635. if (pfc->saved_regs)
  636. sh_pfc_walk_regs(pfc, sh_pfc_restore_reg);
  637. return 0;
  638. }
  639. #define pm_psci_sleep_ptr(_ptr) pm_sleep_ptr(_ptr)
  640. #else
  641. static int sh_pfc_suspend_init(struct sh_pfc *pfc) { return 0; }
  642. static int sh_pfc_suspend_noirq(struct device *dev) { return 0; }
  643. static int sh_pfc_resume_noirq(struct device *dev) { return 0; }
  644. #define pm_psci_sleep_ptr(_ptr) PTR_IF(false, (_ptr))
  645. #endif /* CONFIG_ARM_PSCI_FW */
  646. static DEFINE_NOIRQ_DEV_PM_OPS(sh_pfc_pm, sh_pfc_suspend_noirq, sh_pfc_resume_noirq);
  647. #ifdef DEBUG
  648. #define SH_PFC_MAX_REGS 300
  649. #define SH_PFC_MAX_ENUMS 5000
  650. static unsigned int sh_pfc_errors __initdata;
  651. static unsigned int sh_pfc_warnings __initdata;
  652. static bool sh_pfc_bias_done __initdata;
  653. static bool sh_pfc_drive_done __initdata;
  654. static bool sh_pfc_power_done __initdata;
  655. static struct {
  656. u32 reg;
  657. u32 bits;
  658. } *sh_pfc_regs __initdata;
  659. static u32 sh_pfc_num_regs __initdata;
  660. static u16 *sh_pfc_enums __initdata;
  661. static u32 sh_pfc_num_enums __initdata;
  662. #define sh_pfc_err(fmt, ...) \
  663. do { \
  664. pr_err("%s: " fmt, drvname, ##__VA_ARGS__); \
  665. sh_pfc_errors++; \
  666. } while (0)
  667. #define sh_pfc_err_once(type, fmt, ...) \
  668. do { \
  669. if (!sh_pfc_ ## type ## _done) { \
  670. sh_pfc_ ## type ## _done = true; \
  671. sh_pfc_err(fmt, ##__VA_ARGS__); \
  672. } \
  673. } while (0)
  674. #define sh_pfc_warn(fmt, ...) \
  675. do { \
  676. pr_warn("%s: " fmt, drvname, ##__VA_ARGS__); \
  677. sh_pfc_warnings++; \
  678. } while (0)
  679. static bool __init is0s(const u16 *enum_ids, unsigned int n)
  680. {
  681. unsigned int i;
  682. for (i = 0; i < n; i++)
  683. if (enum_ids[i])
  684. return false;
  685. return true;
  686. }
  687. static bool __init same_name(const char *a, const char *b)
  688. {
  689. return a && b && !strcmp(a, b);
  690. }
  691. static void __init sh_pfc_check_reg(const char *drvname, u32 reg, u32 bits)
  692. {
  693. unsigned int i;
  694. for (i = 0; i < sh_pfc_num_regs; i++) {
  695. if (reg != sh_pfc_regs[i].reg)
  696. continue;
  697. if (bits & sh_pfc_regs[i].bits)
  698. sh_pfc_err("reg 0x%x: bits 0x%x conflict\n", reg,
  699. bits & sh_pfc_regs[i].bits);
  700. sh_pfc_regs[i].bits |= bits;
  701. return;
  702. }
  703. if (sh_pfc_num_regs == SH_PFC_MAX_REGS) {
  704. pr_warn_once("%s: Please increase SH_PFC_MAX_REGS\n", drvname);
  705. return;
  706. }
  707. sh_pfc_regs[sh_pfc_num_regs].reg = reg;
  708. sh_pfc_regs[sh_pfc_num_regs].bits = bits;
  709. sh_pfc_num_regs++;
  710. }
  711. static int __init sh_pfc_check_enum(const char *drvname, u16 enum_id)
  712. {
  713. unsigned int i;
  714. for (i = 0; i < sh_pfc_num_enums; i++) {
  715. if (enum_id == sh_pfc_enums[i])
  716. return -EINVAL;
  717. }
  718. if (sh_pfc_num_enums == SH_PFC_MAX_ENUMS) {
  719. pr_warn_once("%s: Please increase SH_PFC_MAX_ENUMS\n", drvname);
  720. return 0;
  721. }
  722. sh_pfc_enums[sh_pfc_num_enums++] = enum_id;
  723. return 0;
  724. }
  725. static void __init sh_pfc_check_reg_enums(const char *drvname, u32 reg,
  726. const u16 *enums, unsigned int n)
  727. {
  728. unsigned int i;
  729. for (i = 0; i < n; i++) {
  730. if (enums[i] && sh_pfc_check_enum(drvname, enums[i]))
  731. sh_pfc_err("reg 0x%x enum_id %u conflict\n", reg,
  732. enums[i]);
  733. }
  734. }
  735. static const struct sh_pfc_pin __init *sh_pfc_find_pin(
  736. const struct sh_pfc_soc_info *info, u32 reg, unsigned int pin)
  737. {
  738. const char *drvname = info->name;
  739. unsigned int i;
  740. if (pin == SH_PFC_PIN_NONE)
  741. return NULL;
  742. for (i = 0; i < info->nr_pins; i++) {
  743. if (pin == info->pins[i].pin)
  744. return &info->pins[i];
  745. }
  746. sh_pfc_err("reg 0x%x: pin %u not found\n", reg, pin);
  747. return NULL;
  748. }
  749. static void __init sh_pfc_check_cfg_reg(const char *drvname,
  750. const struct pinmux_cfg_reg *cfg_reg)
  751. {
  752. unsigned int i, n, rw, r;
  753. int fw;
  754. sh_pfc_check_reg(drvname, cfg_reg->reg,
  755. GENMASK(cfg_reg->reg_width - 1, 0));
  756. if (cfg_reg->field_width) {
  757. fw = cfg_reg->field_width;
  758. n = (cfg_reg->reg_width / fw) << fw;
  759. for (i = 0, r = 0; i < n; i += 1 << fw) {
  760. if (is0s(&cfg_reg->enum_ids[i], 1 << fw))
  761. r++;
  762. }
  763. if ((r << fw) * sizeof(u16) > cfg_reg->reg_width / fw)
  764. sh_pfc_warn("reg 0x%x can be described with variable-width reserved fields\n",
  765. cfg_reg->reg);
  766. /* Skip field checks (done at build time) */
  767. goto check_enum_ids;
  768. }
  769. for (i = 0, n = 0, rw = 0; (fw = cfg_reg->var_field_width[i]); i++) {
  770. if (fw < 0) {
  771. rw += -fw;
  772. } else {
  773. if (is0s(&cfg_reg->enum_ids[n], 1 << fw))
  774. sh_pfc_warn("reg 0x%x: field [%u:%u] can be described as reserved\n",
  775. cfg_reg->reg, rw, rw + fw - 1);
  776. n += 1 << fw;
  777. rw += fw;
  778. }
  779. }
  780. if (rw != cfg_reg->reg_width)
  781. sh_pfc_err("reg 0x%x: var_field_width declares %u instead of %u bits\n",
  782. cfg_reg->reg, rw, cfg_reg->reg_width);
  783. if (n != cfg_reg->nr_enum_ids) {
  784. sh_pfc_err("reg 0x%x: enum_ids[] has %u instead of %u values\n",
  785. cfg_reg->reg, cfg_reg->nr_enum_ids, n);
  786. n = cfg_reg->nr_enum_ids;
  787. }
  788. check_enum_ids:
  789. sh_pfc_check_reg_enums(drvname, cfg_reg->reg, cfg_reg->enum_ids, n);
  790. }
  791. static void __init sh_pfc_check_drive_reg(const struct sh_pfc_soc_info *info,
  792. const struct pinmux_drive_reg *drive)
  793. {
  794. const char *drvname = info->name;
  795. const struct sh_pfc_pin *pin;
  796. unsigned int i;
  797. for (i = 0; i < ARRAY_SIZE(drive->fields); i++) {
  798. const struct pinmux_drive_reg_field *field = &drive->fields[i];
  799. if (!field->pin && !field->offset && !field->size)
  800. continue;
  801. sh_pfc_check_reg(info->name, drive->reg,
  802. GENMASK(field->offset + field->size - 1,
  803. field->offset));
  804. pin = sh_pfc_find_pin(info, drive->reg, field->pin);
  805. if (pin && !(pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH))
  806. sh_pfc_err("drive_reg 0x%x: field %u: pin %s lacks SH_PFC_PIN_CFG_DRIVE_STRENGTH flag\n",
  807. drive->reg, i, pin->name);
  808. }
  809. }
  810. static void __init sh_pfc_check_bias_reg(const struct sh_pfc_soc_info *info,
  811. const struct pinmux_bias_reg *bias)
  812. {
  813. const char *drvname = info->name;
  814. const struct sh_pfc_pin *pin;
  815. unsigned int i;
  816. u32 bits;
  817. for (i = 0, bits = 0; i < ARRAY_SIZE(bias->pins); i++)
  818. if (bias->pins[i] != SH_PFC_PIN_NONE)
  819. bits |= BIT(i);
  820. if (bias->puen)
  821. sh_pfc_check_reg(info->name, bias->puen, bits);
  822. if (bias->pud)
  823. sh_pfc_check_reg(info->name, bias->pud, bits);
  824. for (i = 0; i < ARRAY_SIZE(bias->pins); i++) {
  825. pin = sh_pfc_find_pin(info, bias->puen, bias->pins[i]);
  826. if (!pin)
  827. continue;
  828. if (bias->puen && bias->pud) {
  829. /*
  830. * Pull-enable and pull-up/down control registers
  831. * As some SoCs have pins that support only pull-up
  832. * or pull-down, we just check for one of them
  833. */
  834. if (!(pin->configs & SH_PFC_PIN_CFG_PULL_UP_DOWN))
  835. sh_pfc_err("bias_reg 0x%x:%u: pin %s lacks one or more SH_PFC_PIN_CFG_PULL_* flags\n",
  836. bias->puen, i, pin->name);
  837. } else if (bias->puen) {
  838. /* Pull-up control register only */
  839. if (!(pin->configs & SH_PFC_PIN_CFG_PULL_UP))
  840. sh_pfc_err("bias_reg 0x%x:%u: pin %s lacks SH_PFC_PIN_CFG_PULL_UP flag\n",
  841. bias->puen, i, pin->name);
  842. } else if (bias->pud) {
  843. /* Pull-down control register only */
  844. if (!(pin->configs & SH_PFC_PIN_CFG_PULL_DOWN))
  845. sh_pfc_err("bias_reg 0x%x:%u: pin %s lacks SH_PFC_PIN_CFG_PULL_DOWN flag\n",
  846. bias->pud, i, pin->name);
  847. }
  848. }
  849. }
  850. static void __init sh_pfc_compare_groups(const char *drvname,
  851. const struct sh_pfc_pin_group *a,
  852. const struct sh_pfc_pin_group *b)
  853. {
  854. unsigned int i;
  855. size_t len;
  856. if (same_name(a->name, b->name))
  857. sh_pfc_err("group %s: name conflict\n", a->name);
  858. if (a->nr_pins > b->nr_pins)
  859. swap(a, b);
  860. len = a->nr_pins * sizeof(a->pins[0]);
  861. for (i = 0; i <= b->nr_pins - a->nr_pins; i++) {
  862. if (a->pins == b->pins + i || a->mux == b->mux + i ||
  863. memcmp(a->pins, b->pins + i, len) ||
  864. memcmp(a->mux, b->mux + i, len))
  865. continue;
  866. if (a->nr_pins == b->nr_pins)
  867. sh_pfc_warn("group %s can be an alias for %s\n",
  868. a->name, b->name);
  869. else
  870. sh_pfc_warn("group %s is a subset of %s\n", a->name,
  871. b->name);
  872. }
  873. }
  874. static void __init sh_pfc_check_info(const struct sh_pfc_soc_info *info)
  875. {
  876. const struct pinmux_drive_reg *drive_regs = info->drive_regs;
  877. #define drive_nfields ARRAY_SIZE(drive_regs->fields)
  878. #define drive_ofs(i) drive_regs[(i) / drive_nfields]
  879. #define drive_reg(i) drive_ofs(i).reg
  880. #define drive_bit(i) ((i) % drive_nfields)
  881. #define drive_field(i) drive_ofs(i).fields[drive_bit(i)]
  882. const struct pinmux_bias_reg *bias_regs = info->bias_regs;
  883. #define bias_npins ARRAY_SIZE(bias_regs->pins)
  884. #define bias_ofs(i) bias_regs[(i) / bias_npins]
  885. #define bias_puen(i) bias_ofs(i).puen
  886. #define bias_pud(i) bias_ofs(i).pud
  887. #define bias_bit(i) ((i) % bias_npins)
  888. #define bias_pin(i) bias_ofs(i).pins[bias_bit(i)]
  889. const char *drvname = info->name;
  890. unsigned int *refcnts;
  891. unsigned int i, j, k;
  892. pr_info("sh_pfc: Checking %s\n", drvname);
  893. sh_pfc_num_regs = 0;
  894. sh_pfc_num_enums = 0;
  895. sh_pfc_bias_done = false;
  896. sh_pfc_drive_done = false;
  897. sh_pfc_power_done = false;
  898. /* Check pins */
  899. for (i = 0; i < info->nr_pins; i++) {
  900. const struct sh_pfc_pin *pin = &info->pins[i];
  901. unsigned int x;
  902. if (!pin->name) {
  903. sh_pfc_err("empty pin %u\n", i);
  904. continue;
  905. }
  906. for (j = 0; j < i; j++) {
  907. const struct sh_pfc_pin *pin2 = &info->pins[j];
  908. if (same_name(pin->name, pin2->name))
  909. sh_pfc_err("pin %s: name conflict\n",
  910. pin->name);
  911. if (pin->pin != (u16)-1 && pin->pin == pin2->pin)
  912. sh_pfc_err("pin %s/%s: pin %u conflict\n",
  913. pin->name, pin2->name, pin->pin);
  914. if (pin->enum_id && pin->enum_id == pin2->enum_id)
  915. sh_pfc_err("pin %s/%s: enum_id %u conflict\n",
  916. pin->name, pin2->name,
  917. pin->enum_id);
  918. }
  919. if (pin->configs & SH_PFC_PIN_CFG_PULL_UP_DOWN) {
  920. if (!info->ops || !info->ops->get_bias ||
  921. !info->ops->set_bias)
  922. sh_pfc_err_once(bias, "SH_PFC_PIN_CFG_PULL_* flag set but .[gs]et_bias() not implemented\n");
  923. if (!bias_regs &&
  924. (!info->ops || !info->ops->pin_to_portcr))
  925. sh_pfc_err_once(bias, "SH_PFC_PIN_CFG_PULL_UP flag set but no bias_regs defined and .pin_to_portcr() not implemented\n");
  926. }
  927. if ((pin->configs & SH_PFC_PIN_CFG_PULL_UP_DOWN) && bias_regs) {
  928. const struct pinmux_bias_reg *bias_reg =
  929. rcar_pin_to_bias_reg(info, pin->pin, &x);
  930. if (!bias_reg ||
  931. ((pin->configs & SH_PFC_PIN_CFG_PULL_UP) &&
  932. !bias_reg->puen))
  933. sh_pfc_err("pin %s: SH_PFC_PIN_CFG_PULL_UP flag set but pin not in bias_regs\n",
  934. pin->name);
  935. if (!bias_reg ||
  936. ((pin->configs & SH_PFC_PIN_CFG_PULL_DOWN) &&
  937. !bias_reg->pud))
  938. sh_pfc_err("pin %s: SH_PFC_PIN_CFG_PULL_DOWN flag set but pin not in bias_regs\n",
  939. pin->name);
  940. }
  941. if (pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH) {
  942. if (!drive_regs) {
  943. sh_pfc_err_once(drive, "SH_PFC_PIN_CFG_DRIVE_STRENGTH flag set but drive_regs missing\n");
  944. } else {
  945. for (j = 0; drive_reg(j); j++) {
  946. if (!drive_field(j).pin &&
  947. !drive_field(j).offset &&
  948. !drive_field(j).size)
  949. continue;
  950. if (drive_field(j).pin == pin->pin)
  951. break;
  952. }
  953. if (!drive_reg(j))
  954. sh_pfc_err("pin %s: SH_PFC_PIN_CFG_DRIVE_STRENGTH flag set but not in drive_regs\n",
  955. pin->name);
  956. }
  957. }
  958. if (pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE_MASK) {
  959. if (!info->ops || !info->ops->pin_to_pocctrl)
  960. sh_pfc_err_once(power, "SH_PFC_PIN_CFG_IO_VOLTAGE set but .pin_to_pocctrl() not implemented\n");
  961. else if (info->ops->pin_to_pocctrl(pin->pin, &x) < 0)
  962. sh_pfc_err("pin %s: SH_PFC_PIN_CFG_IO_VOLTAGE set but invalid pin_to_pocctrl()\n",
  963. pin->name);
  964. } else if (info->ops && info->ops->pin_to_pocctrl &&
  965. info->ops->pin_to_pocctrl(pin->pin, &x) >= 0) {
  966. sh_pfc_warn("pin %s: SH_PFC_PIN_CFG_IO_VOLTAGE not set but valid pin_to_pocctrl()\n",
  967. pin->name);
  968. }
  969. }
  970. /* Check groups and functions */
  971. refcnts = kcalloc(info->nr_groups, sizeof(*refcnts), GFP_KERNEL);
  972. if (!refcnts)
  973. return;
  974. for (i = 0; i < info->nr_functions; i++) {
  975. const struct sh_pfc_function *func = &info->functions[i];
  976. if (!func->name) {
  977. sh_pfc_err("empty function %u\n", i);
  978. continue;
  979. }
  980. for (j = 0; j < i; j++) {
  981. if (same_name(func->name, info->functions[j].name))
  982. sh_pfc_err("function %s: name conflict\n",
  983. func->name);
  984. }
  985. for (j = 0; j < func->nr_groups; j++) {
  986. for (k = 0; k < info->nr_groups; k++) {
  987. if (same_name(func->groups[j],
  988. info->groups[k].name)) {
  989. refcnts[k]++;
  990. break;
  991. }
  992. }
  993. if (k == info->nr_groups)
  994. sh_pfc_err("function %s: group %s not found\n",
  995. func->name, func->groups[j]);
  996. }
  997. }
  998. for (i = 0; i < info->nr_groups; i++) {
  999. const struct sh_pfc_pin_group *group = &info->groups[i];
  1000. if (!group->name) {
  1001. sh_pfc_err("empty group %u\n", i);
  1002. continue;
  1003. }
  1004. for (j = 0; j < i; j++)
  1005. sh_pfc_compare_groups(drvname, group, &info->groups[j]);
  1006. if (!refcnts[i])
  1007. sh_pfc_err("orphan group %s\n", group->name);
  1008. else if (refcnts[i] > 1)
  1009. sh_pfc_warn("group %s referenced by %u functions\n",
  1010. group->name, refcnts[i]);
  1011. }
  1012. kfree(refcnts);
  1013. /* Check config register descriptions */
  1014. for (i = 0; info->cfg_regs && info->cfg_regs[i].reg; i++)
  1015. sh_pfc_check_cfg_reg(drvname, &info->cfg_regs[i]);
  1016. /* Check drive strength registers */
  1017. for (i = 0; drive_regs && drive_regs[i].reg; i++)
  1018. sh_pfc_check_drive_reg(info, &drive_regs[i]);
  1019. for (i = 0; drive_regs && drive_reg(i); i++) {
  1020. if (!drive_field(i).pin && !drive_field(i).offset &&
  1021. !drive_field(i).size)
  1022. continue;
  1023. for (j = 0; j < i; j++) {
  1024. if (drive_field(i).pin == drive_field(j).pin &&
  1025. drive_field(j).offset && drive_field(j).size) {
  1026. sh_pfc_err("drive_reg 0x%x:%zu/0x%x:%zu: pin conflict\n",
  1027. drive_reg(i), drive_bit(i),
  1028. drive_reg(j), drive_bit(j));
  1029. }
  1030. }
  1031. }
  1032. /* Check bias registers */
  1033. for (i = 0; bias_regs && (bias_regs[i].puen || bias_regs[i].pud); i++)
  1034. sh_pfc_check_bias_reg(info, &bias_regs[i]);
  1035. for (i = 0; bias_regs && (bias_puen(i) || bias_pud(i)); i++) {
  1036. if (bias_pin(i) == SH_PFC_PIN_NONE)
  1037. continue;
  1038. for (j = 0; j < i; j++) {
  1039. if (bias_pin(i) != bias_pin(j))
  1040. continue;
  1041. if (bias_puen(i) && bias_puen(j))
  1042. sh_pfc_err("bias_reg 0x%x:%zu/0x%x:%zu: pin conflict\n",
  1043. bias_puen(i), bias_bit(i),
  1044. bias_puen(j), bias_bit(j));
  1045. if (bias_pud(i) && bias_pud(j))
  1046. sh_pfc_err("bias_reg 0x%x:%zu/0x%x:%zu: pin conflict\n",
  1047. bias_pud(i), bias_bit(i),
  1048. bias_pud(j), bias_bit(j));
  1049. }
  1050. }
  1051. /* Check ioctrl registers */
  1052. for (i = 0; info->ioctrl_regs && info->ioctrl_regs[i].reg; i++)
  1053. sh_pfc_check_reg(drvname, info->ioctrl_regs[i].reg, U32_MAX);
  1054. /* Check data registers */
  1055. for (i = 0; info->data_regs && info->data_regs[i].reg; i++) {
  1056. sh_pfc_check_reg(drvname, info->data_regs[i].reg,
  1057. GENMASK(info->data_regs[i].reg_width - 1, 0));
  1058. sh_pfc_check_reg_enums(drvname, info->data_regs[i].reg,
  1059. info->data_regs[i].enum_ids,
  1060. info->data_regs[i].reg_width);
  1061. }
  1062. #ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
  1063. /* Check function GPIOs */
  1064. for (i = 0; i < info->nr_func_gpios; i++) {
  1065. const struct pinmux_func *func = &info->func_gpios[i];
  1066. if (!func->name) {
  1067. sh_pfc_err("empty function gpio %u\n", i);
  1068. continue;
  1069. }
  1070. for (j = 0; j < i; j++) {
  1071. if (same_name(func->name, info->func_gpios[j].name))
  1072. sh_pfc_err("func_gpio %s: name conflict\n",
  1073. func->name);
  1074. }
  1075. if (sh_pfc_check_enum(drvname, func->enum_id))
  1076. sh_pfc_err("%s enum_id %u conflict\n", func->name,
  1077. func->enum_id);
  1078. }
  1079. #endif
  1080. }
  1081. static void __init sh_pfc_check_driver(const struct platform_driver *pdrv)
  1082. {
  1083. unsigned int i;
  1084. if (!IS_ENABLED(CONFIG_SUPERH) &&
  1085. !of_find_matching_node(NULL, pdrv->driver.of_match_table))
  1086. return;
  1087. sh_pfc_regs = kcalloc(SH_PFC_MAX_REGS, sizeof(*sh_pfc_regs),
  1088. GFP_KERNEL);
  1089. if (!sh_pfc_regs)
  1090. return;
  1091. sh_pfc_enums = kcalloc(SH_PFC_MAX_ENUMS, sizeof(*sh_pfc_enums),
  1092. GFP_KERNEL);
  1093. if (!sh_pfc_enums)
  1094. goto free_regs;
  1095. pr_warn("sh_pfc: Checking builtin pinmux tables\n");
  1096. for (i = 0; pdrv->id_table[i].name[0]; i++)
  1097. sh_pfc_check_info((void *)pdrv->id_table[i].driver_data);
  1098. #ifdef CONFIG_OF
  1099. for (i = 0; pdrv->driver.of_match_table[i].compatible[0]; i++)
  1100. sh_pfc_check_info(pdrv->driver.of_match_table[i].data);
  1101. #endif
  1102. pr_warn("sh_pfc: Detected %u errors and %u warnings\n", sh_pfc_errors,
  1103. sh_pfc_warnings);
  1104. kfree(sh_pfc_enums);
  1105. free_regs:
  1106. kfree(sh_pfc_regs);
  1107. }
  1108. #else /* !DEBUG */
  1109. static inline void sh_pfc_check_driver(struct platform_driver *pdrv) {}
  1110. #endif /* !DEBUG */
  1111. static int sh_pfc_probe(struct platform_device *pdev)
  1112. {
  1113. const struct sh_pfc_soc_info *info;
  1114. struct sh_pfc *pfc;
  1115. int ret;
  1116. if (pdev->dev.of_node)
  1117. info = of_device_get_match_data(&pdev->dev);
  1118. else
  1119. info = (const void *)platform_get_device_id(pdev)->driver_data;
  1120. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  1121. if (pfc == NULL)
  1122. return -ENOMEM;
  1123. pfc->info = info;
  1124. pfc->dev = &pdev->dev;
  1125. ret = sh_pfc_map_resources(pfc, pdev);
  1126. if (unlikely(ret < 0))
  1127. return ret;
  1128. spin_lock_init(&pfc->lock);
  1129. if (info->ops && info->ops->init) {
  1130. ret = info->ops->init(pfc);
  1131. if (ret < 0)
  1132. return ret;
  1133. /* .init() may have overridden pfc->info */
  1134. info = pfc->info;
  1135. }
  1136. ret = sh_pfc_suspend_init(pfc);
  1137. if (ret)
  1138. return ret;
  1139. /* Enable dummy states for those platforms without pinctrl support */
  1140. if (!of_have_populated_dt())
  1141. pinctrl_provide_dummies();
  1142. ret = sh_pfc_init_ranges(pfc);
  1143. if (ret < 0)
  1144. return ret;
  1145. /*
  1146. * Initialize pinctrl bindings first
  1147. */
  1148. ret = sh_pfc_register_pinctrl(pfc);
  1149. if (unlikely(ret != 0))
  1150. return ret;
  1151. #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
  1152. /*
  1153. * Then the GPIO chip
  1154. */
  1155. ret = sh_pfc_register_gpiochip(pfc);
  1156. if (unlikely(ret != 0)) {
  1157. /*
  1158. * If the GPIO chip fails to come up we still leave the
  1159. * PFC state as it is, given that there are already
  1160. * extant users of it that have succeeded by this point.
  1161. */
  1162. dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
  1163. }
  1164. #endif
  1165. platform_set_drvdata(pdev, pfc);
  1166. dev_info(pfc->dev, "%s support registered\n", info->name);
  1167. return 0;
  1168. }
  1169. static const struct platform_device_id sh_pfc_id_table[] = {
  1170. #ifdef CONFIG_PINCTRL_PFC_SH7203
  1171. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  1172. #endif
  1173. #ifdef CONFIG_PINCTRL_PFC_SH7264
  1174. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  1175. #endif
  1176. #ifdef CONFIG_PINCTRL_PFC_SH7269
  1177. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  1178. #endif
  1179. #ifdef CONFIG_PINCTRL_PFC_SH7720
  1180. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  1181. #endif
  1182. #ifdef CONFIG_PINCTRL_PFC_SH7722
  1183. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  1184. #endif
  1185. #ifdef CONFIG_PINCTRL_PFC_SH7723
  1186. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  1187. #endif
  1188. #ifdef CONFIG_PINCTRL_PFC_SH7724
  1189. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  1190. #endif
  1191. #ifdef CONFIG_PINCTRL_PFC_SH7734
  1192. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  1193. #endif
  1194. #ifdef CONFIG_PINCTRL_PFC_SH7757
  1195. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  1196. #endif
  1197. #ifdef CONFIG_PINCTRL_PFC_SH7785
  1198. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  1199. #endif
  1200. #ifdef CONFIG_PINCTRL_PFC_SH7786
  1201. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  1202. #endif
  1203. #ifdef CONFIG_PINCTRL_PFC_SHX3
  1204. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  1205. #endif
  1206. { /* sentinel */ }
  1207. };
  1208. static struct platform_driver sh_pfc_driver = {
  1209. .probe = sh_pfc_probe,
  1210. .id_table = sh_pfc_id_table,
  1211. .driver = {
  1212. .name = DRV_NAME,
  1213. .of_match_table = of_match_ptr(sh_pfc_of_table),
  1214. .pm = pm_psci_sleep_ptr(&sh_pfc_pm),
  1215. },
  1216. };
  1217. static int __init sh_pfc_init(void)
  1218. {
  1219. sh_pfc_check_driver(&sh_pfc_driver);
  1220. return platform_driver_register(&sh_pfc_driver);
  1221. }
  1222. postcore_initcall(sh_pfc_init);