core.c 18 KB

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