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