qcom-apcs-ipc-mailbox.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2017, Linaro Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/mailbox_controller.h>
  22. #define QCOM_APCS_IPC_BITS 32
  23. struct qcom_apcs_ipc {
  24. struct mbox_controller mbox;
  25. struct mbox_chan mbox_chans[QCOM_APCS_IPC_BITS];
  26. struct regmap *regmap;
  27. unsigned long offset;
  28. struct platform_device *clk;
  29. };
  30. static const struct regmap_config apcs_regmap_config = {
  31. .reg_bits = 32,
  32. .reg_stride = 4,
  33. .val_bits = 32,
  34. .max_register = 0x1000,
  35. .fast_io = true,
  36. };
  37. static int qcom_apcs_ipc_send_data(struct mbox_chan *chan, void *data)
  38. {
  39. struct qcom_apcs_ipc *apcs = container_of(chan->mbox,
  40. struct qcom_apcs_ipc, mbox);
  41. unsigned long idx = (unsigned long)chan->con_priv;
  42. return regmap_write(apcs->regmap, apcs->offset, BIT(idx));
  43. }
  44. static const struct mbox_chan_ops qcom_apcs_ipc_ops = {
  45. .send_data = qcom_apcs_ipc_send_data,
  46. };
  47. static int qcom_apcs_ipc_probe(struct platform_device *pdev)
  48. {
  49. struct device_node *np = pdev->dev.of_node;
  50. struct qcom_apcs_ipc *apcs;
  51. struct regmap *regmap;
  52. struct resource *res;
  53. unsigned long offset;
  54. void __iomem *base;
  55. unsigned long i;
  56. int ret;
  57. apcs = devm_kzalloc(&pdev->dev, sizeof(*apcs), GFP_KERNEL);
  58. if (!apcs)
  59. return -ENOMEM;
  60. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  61. base = devm_ioremap_resource(&pdev->dev, res);
  62. if (IS_ERR(base))
  63. return PTR_ERR(base);
  64. regmap = devm_regmap_init_mmio(&pdev->dev, base, &apcs_regmap_config);
  65. if (IS_ERR(regmap))
  66. return PTR_ERR(regmap);
  67. offset = (unsigned long)of_device_get_match_data(&pdev->dev);
  68. apcs->regmap = regmap;
  69. apcs->offset = offset;
  70. /* Initialize channel identifiers */
  71. for (i = 0; i < ARRAY_SIZE(apcs->mbox_chans); i++)
  72. apcs->mbox_chans[i].con_priv = (void *)i;
  73. apcs->mbox.dev = &pdev->dev;
  74. apcs->mbox.ops = &qcom_apcs_ipc_ops;
  75. apcs->mbox.chans = apcs->mbox_chans;
  76. apcs->mbox.num_chans = ARRAY_SIZE(apcs->mbox_chans);
  77. ret = mbox_controller_register(&apcs->mbox);
  78. if (ret) {
  79. dev_err(&pdev->dev, "failed to register APCS IPC controller\n");
  80. return ret;
  81. }
  82. if (of_device_is_compatible(np, "qcom,msm8916-apcs-kpss-global")) {
  83. apcs->clk = platform_device_register_data(&pdev->dev,
  84. "qcom-apcs-msm8916-clk",
  85. -1, NULL, 0);
  86. if (IS_ERR(apcs->clk))
  87. dev_err(&pdev->dev, "failed to register APCS clk\n");
  88. }
  89. platform_set_drvdata(pdev, apcs);
  90. return 0;
  91. }
  92. static int qcom_apcs_ipc_remove(struct platform_device *pdev)
  93. {
  94. struct qcom_apcs_ipc *apcs = platform_get_drvdata(pdev);
  95. struct platform_device *clk = apcs->clk;
  96. mbox_controller_unregister(&apcs->mbox);
  97. platform_device_unregister(clk);
  98. return 0;
  99. }
  100. /* .data is the offset of the ipc register within the global block */
  101. static const struct of_device_id qcom_apcs_ipc_of_match[] = {
  102. { .compatible = "qcom,msm8916-apcs-kpss-global", .data = (void *)8 },
  103. { .compatible = "qcom,msm8996-apcs-hmss-global", .data = (void *)16 },
  104. { .compatible = "qcom,msm8998-apcs-hmss-global", .data = (void *)8 },
  105. { .compatible = "qcom,qcs404-apcs-apps-global", .data = (void *)8 },
  106. { .compatible = "qcom,sdm845-apss-shared", .data = (void *)12 },
  107. {}
  108. };
  109. MODULE_DEVICE_TABLE(of, qcom_apcs_ipc_of_match);
  110. static struct platform_driver qcom_apcs_ipc_driver = {
  111. .probe = qcom_apcs_ipc_probe,
  112. .remove = qcom_apcs_ipc_remove,
  113. .driver = {
  114. .name = "qcom_apcs_ipc",
  115. .of_match_table = qcom_apcs_ipc_of_match,
  116. },
  117. };
  118. static int __init qcom_apcs_ipc_init(void)
  119. {
  120. return platform_driver_register(&qcom_apcs_ipc_driver);
  121. }
  122. postcore_initcall(qcom_apcs_ipc_init);
  123. static void __exit qcom_apcs_ipc_exit(void)
  124. {
  125. platform_driver_unregister(&qcom_apcs_ipc_driver);
  126. }
  127. module_exit(qcom_apcs_ipc_exit);
  128. MODULE_LICENSE("GPL v2");
  129. MODULE_DESCRIPTION("Qualcomm APCS IPC driver");