1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017, Linaro Ltd
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/io.h>
9 #include <linux/slab.h>
10 #include <linux/of.h>
11 #include <linux/of_platform.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/mailbox_controller.h>
15 
16 #define QCOM_APCS_IPC_BITS	32
17 
18 struct qcom_apcs_ipc {
19 	struct mbox_controller mbox;
20 	struct mbox_chan mbox_chans[QCOM_APCS_IPC_BITS];
21 
22 	struct regmap *regmap;
23 	unsigned long offset;
24 	struct platform_device *clk;
25 };
26 
27 static const struct regmap_config apcs_regmap_config = {
28 	.reg_bits = 32,
29 	.reg_stride = 4,
30 	.val_bits = 32,
31 	.max_register = 0x1000,
32 	.fast_io = true,
33 };
34 
35 static int qcom_apcs_ipc_send_data(struct mbox_chan *chan, void *data)
36 {
37 	struct qcom_apcs_ipc *apcs = container_of(chan->mbox,
38 						  struct qcom_apcs_ipc, mbox);
39 	unsigned long idx = (unsigned long)chan->con_priv;
40 
41 	return regmap_write(apcs->regmap, apcs->offset, BIT(idx));
42 }
43 
44 static const struct mbox_chan_ops qcom_apcs_ipc_ops = {
45 	.send_data = qcom_apcs_ipc_send_data,
46 };
47 
48 static int qcom_apcs_ipc_probe(struct platform_device *pdev)
49 {
50 	struct device_node *np = pdev->dev.of_node;
51 	struct qcom_apcs_ipc *apcs;
52 	struct regmap *regmap;
53 	struct resource *res;
54 	unsigned long offset;
55 	void __iomem *base;
56 	unsigned long i;
57 	int ret;
58 
59 	apcs = devm_kzalloc(&pdev->dev, sizeof(*apcs), GFP_KERNEL);
60 	if (!apcs)
61 		return -ENOMEM;
62 
63 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
64 	base = devm_ioremap_resource(&pdev->dev, res);
65 	if (IS_ERR(base))
66 		return PTR_ERR(base);
67 
68 	regmap = devm_regmap_init_mmio(&pdev->dev, base, &apcs_regmap_config);
69 	if (IS_ERR(regmap))
70 		return PTR_ERR(regmap);
71 
72 	offset = (unsigned long)of_device_get_match_data(&pdev->dev);
73 
74 	apcs->regmap = regmap;
75 	apcs->offset = offset;
76 
77 	/* Initialize channel identifiers */
78 	for (i = 0; i < ARRAY_SIZE(apcs->mbox_chans); i++)
79 		apcs->mbox_chans[i].con_priv = (void *)i;
80 
81 	apcs->mbox.dev = &pdev->dev;
82 	apcs->mbox.ops = &qcom_apcs_ipc_ops;
83 	apcs->mbox.chans = apcs->mbox_chans;
84 	apcs->mbox.num_chans = ARRAY_SIZE(apcs->mbox_chans);
85 
86 	ret = devm_mbox_controller_register(&pdev->dev, &apcs->mbox);
87 	if (ret) {
88 		dev_err(&pdev->dev, "failed to register APCS IPC controller\n");
89 		return ret;
90 	}
91 
92 	if (of_device_is_compatible(np, "qcom,msm8916-apcs-kpss-global")) {
93 		apcs->clk = platform_device_register_data(&pdev->dev,
94 							  "qcom-apcs-msm8916-clk",
95 							  -1, NULL, 0);
96 		if (IS_ERR(apcs->clk))
97 			dev_err(&pdev->dev, "failed to register APCS clk\n");
98 	}
99 
100 	platform_set_drvdata(pdev, apcs);
101 
102 	return 0;
103 }
104 
105 static int qcom_apcs_ipc_remove(struct platform_device *pdev)
106 {
107 	struct qcom_apcs_ipc *apcs = platform_get_drvdata(pdev);
108 	struct platform_device *clk = apcs->clk;
109 
110 	platform_device_unregister(clk);
111 
112 	return 0;
113 }
114 
115 /* .data is the offset of the ipc register within the global block */
116 static const struct of_device_id qcom_apcs_ipc_of_match[] = {
117 	{ .compatible = "qcom,msm8916-apcs-kpss-global", .data = (void *)8 },
118 	{ .compatible = "qcom,msm8996-apcs-hmss-global", .data = (void *)16 },
119 	{ .compatible = "qcom,msm8998-apcs-hmss-global", .data = (void *)8 },
120 	{ .compatible = "qcom,qcs404-apcs-apps-global", .data = (void *)8 },
121 	{ .compatible = "qcom,sdm845-apss-shared", .data = (void *)12 },
122 	{}
123 };
124 MODULE_DEVICE_TABLE(of, qcom_apcs_ipc_of_match);
125 
126 static struct platform_driver qcom_apcs_ipc_driver = {
127 	.probe = qcom_apcs_ipc_probe,
128 	.remove = qcom_apcs_ipc_remove,
129 	.driver = {
130 		.name = "qcom_apcs_ipc",
131 		.of_match_table = qcom_apcs_ipc_of_match,
132 	},
133 };
134 
135 static int __init qcom_apcs_ipc_init(void)
136 {
137 	return platform_driver_register(&qcom_apcs_ipc_driver);
138 }
139 postcore_initcall(qcom_apcs_ipc_init);
140 
141 static void __exit qcom_apcs_ipc_exit(void)
142 {
143 	platform_driver_unregister(&qcom_apcs_ipc_driver);
144 }
145 module_exit(qcom_apcs_ipc_exit);
146 
147 MODULE_LICENSE("GPL v2");
148 MODULE_DESCRIPTION("Qualcomm APCS IPC driver");
149