core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.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 "core.h"
  25. static int sh_pfc_map_resources(struct sh_pfc *pfc,
  26. struct platform_device *pdev)
  27. {
  28. unsigned int num_windows, num_irqs;
  29. struct sh_pfc_window *windows;
  30. unsigned int *irqs = NULL;
  31. struct resource *res;
  32. unsigned int i;
  33. int irq;
  34. /* Count the MEM and IRQ resources. */
  35. for (num_windows = 0;; num_windows++) {
  36. res = platform_get_resource(pdev, IORESOURCE_MEM, num_windows);
  37. if (!res)
  38. break;
  39. }
  40. for (num_irqs = 0;; num_irqs++) {
  41. irq = platform_get_irq(pdev, num_irqs);
  42. if (irq == -EPROBE_DEFER)
  43. return irq;
  44. if (irq < 0)
  45. break;
  46. }
  47. if (num_windows == 0)
  48. return -EINVAL;
  49. /* Allocate memory windows and IRQs arrays. */
  50. windows = devm_kcalloc(pfc->dev, num_windows, sizeof(*windows),
  51. GFP_KERNEL);
  52. if (windows == NULL)
  53. return -ENOMEM;
  54. pfc->num_windows = num_windows;
  55. pfc->windows = windows;
  56. if (num_irqs) {
  57. irqs = devm_kcalloc(pfc->dev, num_irqs, sizeof(*irqs),
  58. GFP_KERNEL);
  59. if (irqs == NULL)
  60. return -ENOMEM;
  61. pfc->num_irqs = num_irqs;
  62. pfc->irqs = irqs;
  63. }
  64. /* Fill them. */
  65. for (i = 0; i < num_windows; i++) {
  66. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  67. windows->phys = res->start;
  68. windows->size = resource_size(res);
  69. windows->virt = devm_ioremap_resource(pfc->dev, res);
  70. if (IS_ERR(windows->virt))
  71. return -ENOMEM;
  72. windows++;
  73. }
  74. for (i = 0; i < num_irqs; i++)
  75. *irqs++ = platform_get_irq(pdev, i);
  76. return 0;
  77. }
  78. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc, u32 reg)
  79. {
  80. struct sh_pfc_window *window;
  81. phys_addr_t address = reg;
  82. unsigned int i;
  83. /* scan through physical windows and convert address */
  84. for (i = 0; i < pfc->num_windows; i++) {
  85. window = pfc->windows + i;
  86. if (address < window->phys)
  87. continue;
  88. if (address >= (window->phys + window->size))
  89. continue;
  90. return window->virt + (address - window->phys);
  91. }
  92. BUG();
  93. return NULL;
  94. }
  95. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  96. {
  97. unsigned int offset;
  98. unsigned int i;
  99. for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
  100. const struct sh_pfc_pin_range *range = &pfc->ranges[i];
  101. if (pin <= range->end)
  102. return pin >= range->start
  103. ? offset + pin - range->start : -1;
  104. offset += range->end - range->start + 1;
  105. }
  106. return -EINVAL;
  107. }
  108. static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
  109. {
  110. if (enum_id < r->begin)
  111. return 0;
  112. if (enum_id > r->end)
  113. return 0;
  114. return 1;
  115. }
  116. u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
  117. {
  118. switch (reg_width) {
  119. case 8:
  120. return ioread8(mapped_reg);
  121. case 16:
  122. return ioread16(mapped_reg);
  123. case 32:
  124. return ioread32(mapped_reg);
  125. }
  126. BUG();
  127. return 0;
  128. }
  129. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
  130. u32 data)
  131. {
  132. switch (reg_width) {
  133. case 8:
  134. iowrite8(data, mapped_reg);
  135. return;
  136. case 16:
  137. iowrite16(data, mapped_reg);
  138. return;
  139. case 32:
  140. iowrite32(data, mapped_reg);
  141. return;
  142. }
  143. BUG();
  144. }
  145. u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
  146. {
  147. return sh_pfc_read_raw_reg(sh_pfc_phys_to_virt(pfc, reg), 32);
  148. }
  149. void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
  150. {
  151. if (pfc->info->unlock_reg)
  152. sh_pfc_write_raw_reg(
  153. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  154. ~data);
  155. sh_pfc_write_raw_reg(sh_pfc_phys_to_virt(pfc, reg), 32, data);
  156. }
  157. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  158. const struct pinmux_cfg_reg *crp,
  159. unsigned int in_pos,
  160. void __iomem **mapped_regp, u32 *maskp,
  161. unsigned int *posp)
  162. {
  163. unsigned int k;
  164. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  165. if (crp->field_width) {
  166. *maskp = (1 << crp->field_width) - 1;
  167. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  168. } else {
  169. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  170. *posp = crp->reg_width;
  171. for (k = 0; k <= in_pos; k++)
  172. *posp -= crp->var_field_width[k];
  173. }
  174. }
  175. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  176. const struct pinmux_cfg_reg *crp,
  177. unsigned int field, u32 value)
  178. {
  179. void __iomem *mapped_reg;
  180. unsigned int pos;
  181. u32 mask, data;
  182. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  183. dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
  184. "r_width = %u, f_width = %u\n",
  185. crp->reg, value, field, crp->reg_width, crp->field_width);
  186. mask = ~(mask << pos);
  187. value = value << pos;
  188. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  189. data &= mask;
  190. data |= value;
  191. if (pfc->info->unlock_reg)
  192. sh_pfc_write_raw_reg(
  193. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  194. ~data);
  195. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  196. }
  197. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
  198. const struct pinmux_cfg_reg **crp,
  199. unsigned int *fieldp, u32 *valuep)
  200. {
  201. unsigned int k = 0;
  202. while (1) {
  203. const struct pinmux_cfg_reg *config_reg =
  204. pfc->info->cfg_regs + k;
  205. unsigned int r_width = config_reg->reg_width;
  206. unsigned int f_width = config_reg->field_width;
  207. unsigned int curr_width;
  208. unsigned int bit_pos;
  209. unsigned int pos = 0;
  210. unsigned int m = 0;
  211. if (!r_width)
  212. break;
  213. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  214. u32 ncomb;
  215. u32 n;
  216. if (f_width)
  217. curr_width = f_width;
  218. else
  219. curr_width = config_reg->var_field_width[m];
  220. ncomb = 1 << curr_width;
  221. for (n = 0; n < ncomb; n++) {
  222. if (config_reg->enum_ids[pos + n] == enum_id) {
  223. *crp = config_reg;
  224. *fieldp = m;
  225. *valuep = n;
  226. return 0;
  227. }
  228. }
  229. pos += ncomb;
  230. m++;
  231. }
  232. k++;
  233. }
  234. return -EINVAL;
  235. }
  236. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
  237. u16 *enum_idp)
  238. {
  239. const u16 *data = pfc->info->pinmux_data;
  240. unsigned int k;
  241. if (pos) {
  242. *enum_idp = data[pos + 1];
  243. return pos + 1;
  244. }
  245. for (k = 0; k < pfc->info->pinmux_data_size; k++) {
  246. if (data[k] == mark) {
  247. *enum_idp = data[k + 1];
  248. return k + 1;
  249. }
  250. }
  251. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  252. mark);
  253. return -EINVAL;
  254. }
  255. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  256. {
  257. const struct pinmux_range *range;
  258. int pos = 0;
  259. switch (pinmux_type) {
  260. case PINMUX_TYPE_GPIO:
  261. case PINMUX_TYPE_FUNCTION:
  262. range = NULL;
  263. break;
  264. case PINMUX_TYPE_OUTPUT:
  265. range = &pfc->info->output;
  266. break;
  267. case PINMUX_TYPE_INPUT:
  268. range = &pfc->info->input;
  269. break;
  270. default:
  271. return -EINVAL;
  272. }
  273. /* Iterate over all the configuration fields we need to update. */
  274. while (1) {
  275. const struct pinmux_cfg_reg *cr;
  276. unsigned int field;
  277. u16 enum_id;
  278. u32 value;
  279. int in_range;
  280. int ret;
  281. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  282. if (pos < 0)
  283. return pos;
  284. if (!enum_id)
  285. break;
  286. /* Check if the configuration field selects a function. If it
  287. * doesn't, skip the field if it's not applicable to the
  288. * requested pinmux type.
  289. */
  290. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  291. if (!in_range) {
  292. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  293. /* Functions are allowed to modify all
  294. * fields.
  295. */
  296. in_range = 1;
  297. } else if (pinmux_type != PINMUX_TYPE_GPIO) {
  298. /* Input/output types can only modify fields
  299. * that correspond to their respective ranges.
  300. */
  301. in_range = sh_pfc_enum_in_range(enum_id, range);
  302. /*
  303. * special case pass through for fixed
  304. * input-only or output-only pins without
  305. * function enum register association.
  306. */
  307. if (in_range && enum_id == range->force)
  308. continue;
  309. }
  310. /* GPIOs are only allowed to modify function fields. */
  311. }
  312. if (!in_range)
  313. continue;
  314. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  315. if (ret < 0)
  316. return ret;
  317. sh_pfc_write_config_reg(pfc, cr, field, value);
  318. }
  319. return 0;
  320. }
  321. const struct pinmux_bias_reg *
  322. sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin,
  323. unsigned int *bit)
  324. {
  325. unsigned int i, j;
  326. for (i = 0; pfc->info->bias_regs[i].puen; i++) {
  327. for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) {
  328. if (pfc->info->bias_regs[i].pins[j] == pin) {
  329. *bit = j;
  330. return &pfc->info->bias_regs[i];
  331. }
  332. }
  333. }
  334. WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
  335. return NULL;
  336. }
  337. static int sh_pfc_init_ranges(struct sh_pfc *pfc)
  338. {
  339. struct sh_pfc_pin_range *range;
  340. unsigned int nr_ranges;
  341. unsigned int i;
  342. if (pfc->info->pins[0].pin == (u16)-1) {
  343. /* Pin number -1 denotes that the SoC doesn't report pin numbers
  344. * in its pin arrays yet. Consider the pin numbers range as
  345. * continuous and allocate a single range.
  346. */
  347. pfc->nr_ranges = 1;
  348. pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
  349. GFP_KERNEL);
  350. if (pfc->ranges == NULL)
  351. return -ENOMEM;
  352. pfc->ranges->start = 0;
  353. pfc->ranges->end = pfc->info->nr_pins - 1;
  354. pfc->nr_gpio_pins = pfc->info->nr_pins;
  355. return 0;
  356. }
  357. /* Count, allocate and fill the ranges. The PFC SoC data pins array must
  358. * be sorted by pin numbers, and pins without a GPIO port must come
  359. * last.
  360. */
  361. for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
  362. if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
  363. nr_ranges++;
  364. }
  365. pfc->nr_ranges = nr_ranges;
  366. pfc->ranges = devm_kcalloc(pfc->dev, nr_ranges, sizeof(*pfc->ranges),
  367. GFP_KERNEL);
  368. if (pfc->ranges == NULL)
  369. return -ENOMEM;
  370. range = pfc->ranges;
  371. range->start = pfc->info->pins[0].pin;
  372. for (i = 1; i < pfc->info->nr_pins; ++i) {
  373. if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
  374. continue;
  375. range->end = pfc->info->pins[i-1].pin;
  376. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  377. pfc->nr_gpio_pins = range->end + 1;
  378. range++;
  379. range->start = pfc->info->pins[i].pin;
  380. }
  381. range->end = pfc->info->pins[i-1].pin;
  382. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  383. pfc->nr_gpio_pins = range->end + 1;
  384. return 0;
  385. }
  386. #ifdef CONFIG_OF
  387. static const struct of_device_id sh_pfc_of_table[] = {
  388. #ifdef CONFIG_PINCTRL_PFC_EMEV2
  389. {
  390. .compatible = "renesas,pfc-emev2",
  391. .data = &emev2_pinmux_info,
  392. },
  393. #endif
  394. #ifdef CONFIG_PINCTRL_PFC_R8A73A4
  395. {
  396. .compatible = "renesas,pfc-r8a73a4",
  397. .data = &r8a73a4_pinmux_info,
  398. },
  399. #endif
  400. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  401. {
  402. .compatible = "renesas,pfc-r8a7740",
  403. .data = &r8a7740_pinmux_info,
  404. },
  405. #endif
  406. #ifdef CONFIG_PINCTRL_PFC_R8A7743
  407. {
  408. .compatible = "renesas,pfc-r8a7743",
  409. .data = &r8a7743_pinmux_info,
  410. },
  411. #endif
  412. #ifdef CONFIG_PINCTRL_PFC_R8A7744
  413. {
  414. .compatible = "renesas,pfc-r8a7744",
  415. .data = &r8a7744_pinmux_info,
  416. },
  417. #endif
  418. #ifdef CONFIG_PINCTRL_PFC_R8A7745
  419. {
  420. .compatible = "renesas,pfc-r8a7745",
  421. .data = &r8a7745_pinmux_info,
  422. },
  423. #endif
  424. #ifdef CONFIG_PINCTRL_PFC_R8A77470
  425. {
  426. .compatible = "renesas,pfc-r8a77470",
  427. .data = &r8a77470_pinmux_info,
  428. },
  429. #endif
  430. #ifdef CONFIG_PINCTRL_PFC_R8A774A1
  431. {
  432. .compatible = "renesas,pfc-r8a774a1",
  433. .data = &r8a774a1_pinmux_info,
  434. },
  435. #endif
  436. #ifdef CONFIG_PINCTRL_PFC_R8A774C0
  437. {
  438. .compatible = "renesas,pfc-r8a774c0",
  439. .data = &r8a774c0_pinmux_info,
  440. },
  441. #endif
  442. #ifdef CONFIG_PINCTRL_PFC_R8A7778
  443. {
  444. .compatible = "renesas,pfc-r8a7778",
  445. .data = &r8a7778_pinmux_info,
  446. },
  447. #endif
  448. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  449. {
  450. .compatible = "renesas,pfc-r8a7779",
  451. .data = &r8a7779_pinmux_info,
  452. },
  453. #endif
  454. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  455. {
  456. .compatible = "renesas,pfc-r8a7790",
  457. .data = &r8a7790_pinmux_info,
  458. },
  459. #endif
  460. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  461. {
  462. .compatible = "renesas,pfc-r8a7791",
  463. .data = &r8a7791_pinmux_info,
  464. },
  465. #endif
  466. #ifdef CONFIG_PINCTRL_PFC_R8A7792
  467. {
  468. .compatible = "renesas,pfc-r8a7792",
  469. .data = &r8a7792_pinmux_info,
  470. },
  471. #endif
  472. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  473. {
  474. .compatible = "renesas,pfc-r8a7793",
  475. .data = &r8a7793_pinmux_info,
  476. },
  477. #endif
  478. #ifdef CONFIG_PINCTRL_PFC_R8A7794
  479. {
  480. .compatible = "renesas,pfc-r8a7794",
  481. .data = &r8a7794_pinmux_info,
  482. },
  483. #endif
  484. #ifdef CONFIG_PINCTRL_PFC_R8A7795
  485. {
  486. .compatible = "renesas,pfc-r8a7795",
  487. .data = &r8a7795_pinmux_info,
  488. },
  489. #endif
  490. #ifdef CONFIG_PINCTRL_PFC_R8A7796
  491. {
  492. .compatible = "renesas,pfc-r8a7796",
  493. .data = &r8a7796_pinmux_info,
  494. },
  495. #endif
  496. #ifdef CONFIG_PINCTRL_PFC_R8A77965
  497. {
  498. .compatible = "renesas,pfc-r8a77965",
  499. .data = &r8a77965_pinmux_info,
  500. },
  501. #endif
  502. #ifdef CONFIG_PINCTRL_PFC_R8A77970
  503. {
  504. .compatible = "renesas,pfc-r8a77970",
  505. .data = &r8a77970_pinmux_info,
  506. },
  507. #endif
  508. #ifdef CONFIG_PINCTRL_PFC_R8A77980
  509. {
  510. .compatible = "renesas,pfc-r8a77980",
  511. .data = &r8a77980_pinmux_info,
  512. },
  513. #endif
  514. #ifdef CONFIG_PINCTRL_PFC_R8A77990
  515. {
  516. .compatible = "renesas,pfc-r8a77990",
  517. .data = &r8a77990_pinmux_info,
  518. },
  519. #endif
  520. #ifdef CONFIG_PINCTRL_PFC_R8A77995
  521. {
  522. .compatible = "renesas,pfc-r8a77995",
  523. .data = &r8a77995_pinmux_info,
  524. },
  525. #endif
  526. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  527. {
  528. .compatible = "renesas,pfc-sh73a0",
  529. .data = &sh73a0_pinmux_info,
  530. },
  531. #endif
  532. { },
  533. };
  534. #endif
  535. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM_PSCI_FW)
  536. static void sh_pfc_nop_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
  537. {
  538. }
  539. static void sh_pfc_save_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
  540. {
  541. pfc->saved_regs[idx] = sh_pfc_read(pfc, reg);
  542. }
  543. static void sh_pfc_restore_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
  544. {
  545. sh_pfc_write(pfc, reg, pfc->saved_regs[idx]);
  546. }
  547. static unsigned int sh_pfc_walk_regs(struct sh_pfc *pfc,
  548. void (*do_reg)(struct sh_pfc *pfc, u32 reg, unsigned int idx))
  549. {
  550. unsigned int i, n = 0;
  551. if (pfc->info->cfg_regs)
  552. for (i = 0; pfc->info->cfg_regs[i].reg; i++)
  553. do_reg(pfc, pfc->info->cfg_regs[i].reg, n++);
  554. if (pfc->info->drive_regs)
  555. for (i = 0; pfc->info->drive_regs[i].reg; i++)
  556. do_reg(pfc, pfc->info->drive_regs[i].reg, n++);
  557. if (pfc->info->bias_regs)
  558. for (i = 0; pfc->info->bias_regs[i].puen; i++) {
  559. do_reg(pfc, pfc->info->bias_regs[i].puen, n++);
  560. if (pfc->info->bias_regs[i].pud)
  561. do_reg(pfc, pfc->info->bias_regs[i].pud, n++);
  562. }
  563. if (pfc->info->ioctrl_regs)
  564. for (i = 0; pfc->info->ioctrl_regs[i].reg; i++)
  565. do_reg(pfc, pfc->info->ioctrl_regs[i].reg, n++);
  566. return n;
  567. }
  568. static int sh_pfc_suspend_init(struct sh_pfc *pfc)
  569. {
  570. unsigned int n;
  571. /* This is the best we can do to check for the presence of PSCI */
  572. if (!psci_ops.cpu_suspend)
  573. return 0;
  574. n = sh_pfc_walk_regs(pfc, sh_pfc_nop_reg);
  575. if (!n)
  576. return 0;
  577. pfc->saved_regs = devm_kmalloc_array(pfc->dev, n,
  578. sizeof(*pfc->saved_regs),
  579. GFP_KERNEL);
  580. if (!pfc->saved_regs)
  581. return -ENOMEM;
  582. dev_dbg(pfc->dev, "Allocated space to save %u regs\n", n);
  583. return 0;
  584. }
  585. static int sh_pfc_suspend_noirq(struct device *dev)
  586. {
  587. struct sh_pfc *pfc = dev_get_drvdata(dev);
  588. if (pfc->saved_regs)
  589. sh_pfc_walk_regs(pfc, sh_pfc_save_reg);
  590. return 0;
  591. }
  592. static int sh_pfc_resume_noirq(struct device *dev)
  593. {
  594. struct sh_pfc *pfc = dev_get_drvdata(dev);
  595. if (pfc->saved_regs)
  596. sh_pfc_walk_regs(pfc, sh_pfc_restore_reg);
  597. return 0;
  598. }
  599. static const struct dev_pm_ops sh_pfc_pm = {
  600. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sh_pfc_suspend_noirq, sh_pfc_resume_noirq)
  601. };
  602. #define DEV_PM_OPS &sh_pfc_pm
  603. #else
  604. static int sh_pfc_suspend_init(struct sh_pfc *pfc) { return 0; }
  605. #define DEV_PM_OPS NULL
  606. #endif /* CONFIG_PM_SLEEP && CONFIG_ARM_PSCI_FW */
  607. static int sh_pfc_probe(struct platform_device *pdev)
  608. {
  609. #ifdef CONFIG_OF
  610. struct device_node *np = pdev->dev.of_node;
  611. #endif
  612. const struct sh_pfc_soc_info *info;
  613. struct sh_pfc *pfc;
  614. int ret;
  615. #ifdef CONFIG_OF
  616. if (np)
  617. info = of_device_get_match_data(&pdev->dev);
  618. else
  619. #endif
  620. info = (const void *)platform_get_device_id(pdev)->driver_data;
  621. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  622. if (pfc == NULL)
  623. return -ENOMEM;
  624. pfc->info = info;
  625. pfc->dev = &pdev->dev;
  626. ret = sh_pfc_map_resources(pfc, pdev);
  627. if (unlikely(ret < 0))
  628. return ret;
  629. spin_lock_init(&pfc->lock);
  630. if (info->ops && info->ops->init) {
  631. ret = info->ops->init(pfc);
  632. if (ret < 0)
  633. return ret;
  634. /* .init() may have overridden pfc->info */
  635. info = pfc->info;
  636. }
  637. ret = sh_pfc_suspend_init(pfc);
  638. if (ret)
  639. return ret;
  640. /* Enable dummy states for those platforms without pinctrl support */
  641. if (!of_have_populated_dt())
  642. pinctrl_provide_dummies();
  643. ret = sh_pfc_init_ranges(pfc);
  644. if (ret < 0)
  645. return ret;
  646. /*
  647. * Initialize pinctrl bindings first
  648. */
  649. ret = sh_pfc_register_pinctrl(pfc);
  650. if (unlikely(ret != 0))
  651. return ret;
  652. #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
  653. /*
  654. * Then the GPIO chip
  655. */
  656. ret = sh_pfc_register_gpiochip(pfc);
  657. if (unlikely(ret != 0)) {
  658. /*
  659. * If the GPIO chip fails to come up we still leave the
  660. * PFC state as it is, given that there are already
  661. * extant users of it that have succeeded by this point.
  662. */
  663. dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
  664. }
  665. #endif
  666. platform_set_drvdata(pdev, pfc);
  667. dev_info(pfc->dev, "%s support registered\n", info->name);
  668. return 0;
  669. }
  670. static const struct platform_device_id sh_pfc_id_table[] = {
  671. #ifdef CONFIG_PINCTRL_PFC_SH7203
  672. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  673. #endif
  674. #ifdef CONFIG_PINCTRL_PFC_SH7264
  675. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  676. #endif
  677. #ifdef CONFIG_PINCTRL_PFC_SH7269
  678. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  679. #endif
  680. #ifdef CONFIG_PINCTRL_PFC_SH7720
  681. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  682. #endif
  683. #ifdef CONFIG_PINCTRL_PFC_SH7722
  684. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  685. #endif
  686. #ifdef CONFIG_PINCTRL_PFC_SH7723
  687. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  688. #endif
  689. #ifdef CONFIG_PINCTRL_PFC_SH7724
  690. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  691. #endif
  692. #ifdef CONFIG_PINCTRL_PFC_SH7734
  693. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  694. #endif
  695. #ifdef CONFIG_PINCTRL_PFC_SH7757
  696. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  697. #endif
  698. #ifdef CONFIG_PINCTRL_PFC_SH7785
  699. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  700. #endif
  701. #ifdef CONFIG_PINCTRL_PFC_SH7786
  702. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  703. #endif
  704. #ifdef CONFIG_PINCTRL_PFC_SHX3
  705. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  706. #endif
  707. { },
  708. };
  709. static struct platform_driver sh_pfc_driver = {
  710. .probe = sh_pfc_probe,
  711. .id_table = sh_pfc_id_table,
  712. .driver = {
  713. .name = DRV_NAME,
  714. .of_match_table = of_match_ptr(sh_pfc_of_table),
  715. .pm = DEV_PM_OPS,
  716. },
  717. };
  718. static int __init sh_pfc_init(void)
  719. {
  720. return platform_driver_register(&sh_pfc_driver);
  721. }
  722. postcore_initcall(sh_pfc_init);