qcom-apcs-ipc-mailbox.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 = 0xFFC,
  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 qcom_apcs_ipc *apcs;
  50. struct regmap *regmap;
  51. struct resource *res;
  52. unsigned long offset;
  53. void __iomem *base;
  54. unsigned long i;
  55. int ret;
  56. const struct of_device_id apcs_clk_match_table[] = {
  57. { .compatible = "qcom,msm8916-apcs-kpss-global", },
  58. { .compatible = "qcom,qcs404-apcs-apps-global", },
  59. {}
  60. };
  61. apcs = devm_kzalloc(&pdev->dev, sizeof(*apcs), GFP_KERNEL);
  62. if (!apcs)
  63. return -ENOMEM;
  64. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  65. base = devm_ioremap_resource(&pdev->dev, res);
  66. if (IS_ERR(base))
  67. return PTR_ERR(base);
  68. regmap = devm_regmap_init_mmio(&pdev->dev, base, &apcs_regmap_config);
  69. if (IS_ERR(regmap))
  70. return PTR_ERR(regmap);
  71. offset = (unsigned long)of_device_get_match_data(&pdev->dev);
  72. apcs->regmap = regmap;
  73. apcs->offset = offset;
  74. /* Initialize channel identifiers */
  75. for (i = 0; i < ARRAY_SIZE(apcs->mbox_chans); i++)
  76. apcs->mbox_chans[i].con_priv = (void *)i;
  77. apcs->mbox.dev = &pdev->dev;
  78. apcs->mbox.ops = &qcom_apcs_ipc_ops;
  79. apcs->mbox.chans = apcs->mbox_chans;
  80. apcs->mbox.num_chans = ARRAY_SIZE(apcs->mbox_chans);
  81. ret = mbox_controller_register(&apcs->mbox);
  82. if (ret) {
  83. dev_err(&pdev->dev, "failed to register APCS IPC controller\n");
  84. return ret;
  85. }
  86. if (of_match_device(apcs_clk_match_table, &pdev->dev)) {
  87. apcs->clk = platform_device_register_data(&pdev->dev,
  88. "qcom-apcs-msm8916-clk",
  89. -1, NULL, 0);
  90. if (IS_ERR(apcs->clk))
  91. dev_err(&pdev->dev, "failed to register APCS clk\n");
  92. }
  93. platform_set_drvdata(pdev, apcs);
  94. return 0;
  95. }
  96. static int qcom_apcs_ipc_remove(struct platform_device *pdev)
  97. {
  98. struct qcom_apcs_ipc *apcs = platform_get_drvdata(pdev);
  99. struct platform_device *clk = apcs->clk;
  100. mbox_controller_unregister(&apcs->mbox);
  101. platform_device_unregister(clk);
  102. return 0;
  103. }
  104. /* .data is the offset of the ipc register within the global block */
  105. static const struct of_device_id qcom_apcs_ipc_of_match[] = {
  106. { .compatible = "qcom,msm8916-apcs-kpss-global", .data = (void *)8 },
  107. { .compatible = "qcom,msm8996-apcs-hmss-global", .data = (void *)16 },
  108. { .compatible = "qcom,msm8998-apcs-hmss-global", .data = (void *)8 },
  109. { .compatible = "qcom,sdm845-apss-shared", .data = (void *)12 },
  110. {}
  111. };
  112. MODULE_DEVICE_TABLE(of, qcom_apcs_ipc_of_match);
  113. static struct platform_driver qcom_apcs_ipc_driver = {
  114. .probe = qcom_apcs_ipc_probe,
  115. .remove = qcom_apcs_ipc_remove,
  116. .driver = {
  117. .name = "qcom_apcs_ipc",
  118. .of_match_table = qcom_apcs_ipc_of_match,
  119. },
  120. };
  121. static int __init qcom_apcs_ipc_init(void)
  122. {
  123. return platform_driver_register(&qcom_apcs_ipc_driver);
  124. }
  125. postcore_initcall(qcom_apcs_ipc_init);
  126. static void __exit qcom_apcs_ipc_exit(void)
  127. {
  128. platform_driver_unregister(&qcom_apcs_ipc_driver);
  129. }
  130. module_exit(qcom_apcs_ipc_exit);
  131. MODULE_LICENSE("GPL v2");
  132. MODULE_DESCRIPTION("Qualcomm APCS IPC driver");