spmi-apple-controller.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Apple SoC SPMI device driver
  4. *
  5. * Copyright The Asahi Linux Contributors
  6. *
  7. * Inspired by:
  8. * OpenBSD support Copyright (c) 2021 Mark Kettenis <kettenis@openbsd.org>
  9. * Correllium support Copyright (C) 2021 Corellium LLC
  10. * hisi-spmi-controller.c
  11. * spmi-pmic-arb.c Copyright (c) 2021, The Linux Foundation.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/spmi.h>
  19. /* SPMI Controller Registers */
  20. #define SPMI_STATUS_REG 0
  21. #define SPMI_CMD_REG 0x4
  22. #define SPMI_RSP_REG 0x8
  23. #define SPMI_RX_FIFO_EMPTY BIT(24)
  24. #define REG_POLL_INTERVAL_US 10000
  25. #define REG_POLL_TIMEOUT_US (REG_POLL_INTERVAL_US * 5)
  26. struct apple_spmi {
  27. void __iomem *regs;
  28. };
  29. #define poll_reg(spmi, reg, val, cond) \
  30. readl_poll_timeout((spmi)->regs + (reg), (val), (cond), \
  31. REG_POLL_INTERVAL_US, REG_POLL_TIMEOUT_US)
  32. static inline u32 apple_spmi_pack_cmd(u8 opc, u8 sid, u16 saddr, size_t len)
  33. {
  34. return opc | sid << 8 | saddr << 16 | (len - 1) | (1 << 15);
  35. }
  36. /* Wait for Rx FIFO to have something */
  37. static int apple_spmi_wait_rx_not_empty(struct spmi_controller *ctrl)
  38. {
  39. struct apple_spmi *spmi = spmi_controller_get_drvdata(ctrl);
  40. int ret;
  41. u32 status;
  42. ret = poll_reg(spmi, SPMI_STATUS_REG, status, !(status & SPMI_RX_FIFO_EMPTY));
  43. if (ret) {
  44. dev_err(&ctrl->dev,
  45. "failed to wait for RX FIFO not empty\n");
  46. return ret;
  47. }
  48. return 0;
  49. }
  50. static int spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  51. u16 saddr, u8 *buf, size_t len)
  52. {
  53. struct apple_spmi *spmi = spmi_controller_get_drvdata(ctrl);
  54. u32 spmi_cmd = apple_spmi_pack_cmd(opc, sid, saddr, len);
  55. u32 rsp;
  56. size_t len_read = 0;
  57. u8 i;
  58. int ret;
  59. writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
  60. ret = apple_spmi_wait_rx_not_empty(ctrl);
  61. if (ret)
  62. return ret;
  63. /* Discard SPMI reply status */
  64. readl(spmi->regs + SPMI_RSP_REG);
  65. /* Read SPMI data reply */
  66. while (len_read < len) {
  67. rsp = readl(spmi->regs + SPMI_RSP_REG);
  68. i = 0;
  69. while ((len_read < len) && (i < 4)) {
  70. buf[len_read++] = ((0xff << (8 * i)) & rsp) >> (8 * i);
  71. i += 1;
  72. }
  73. }
  74. return 0;
  75. }
  76. static int spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  77. u16 saddr, const u8 *buf, size_t len)
  78. {
  79. struct apple_spmi *spmi = spmi_controller_get_drvdata(ctrl);
  80. u32 spmi_cmd = apple_spmi_pack_cmd(opc, sid, saddr, len);
  81. size_t i = 0, j;
  82. int ret;
  83. writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
  84. while (i < len) {
  85. j = 0;
  86. spmi_cmd = 0;
  87. while ((j < 4) & (i < len))
  88. spmi_cmd |= buf[i++] << (j++ * 8);
  89. writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
  90. }
  91. ret = apple_spmi_wait_rx_not_empty(ctrl);
  92. if (ret)
  93. return ret;
  94. /* Discard */
  95. readl(spmi->regs + SPMI_RSP_REG);
  96. return 0;
  97. }
  98. static int apple_spmi_probe(struct platform_device *pdev)
  99. {
  100. struct apple_spmi *spmi;
  101. struct spmi_controller *ctrl;
  102. int ret;
  103. ctrl = devm_spmi_controller_alloc(&pdev->dev, sizeof(*spmi));
  104. if (IS_ERR(ctrl))
  105. return -ENOMEM;
  106. spmi = spmi_controller_get_drvdata(ctrl);
  107. spmi->regs = devm_platform_ioremap_resource(pdev, 0);
  108. if (IS_ERR(spmi->regs))
  109. return PTR_ERR(spmi->regs);
  110. ctrl->dev.of_node = pdev->dev.of_node;
  111. ctrl->read_cmd = spmi_read_cmd;
  112. ctrl->write_cmd = spmi_write_cmd;
  113. ret = devm_spmi_controller_add(&pdev->dev, ctrl);
  114. if (ret)
  115. return dev_err_probe(&pdev->dev, ret,
  116. "spmi_controller_add failed\n");
  117. return 0;
  118. }
  119. static const struct of_device_id apple_spmi_match_table[] = {
  120. { .compatible = "apple,t8103-spmi", },
  121. { .compatible = "apple,spmi", },
  122. {}
  123. };
  124. MODULE_DEVICE_TABLE(of, apple_spmi_match_table);
  125. static struct platform_driver apple_spmi_driver = {
  126. .probe = apple_spmi_probe,
  127. .driver = {
  128. .name = "apple-spmi",
  129. .of_match_table = apple_spmi_match_table,
  130. },
  131. };
  132. module_platform_driver(apple_spmi_driver);
  133. MODULE_AUTHOR("Jean-Francois Bortolotti <jeff@borto.fr>");
  134. MODULE_DESCRIPTION("Apple SoC SPMI driver");
  135. MODULE_LICENSE("GPL");