qcom-apcs-ipc-mailbox.c 4.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (c) 2017, Linaro Ltd
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
13
#include <linux/regmap.h>
14 15 16 17 18 19 20 21
#include <linux/mailbox_controller.h>

#define QCOM_APCS_IPC_BITS	32

struct qcom_apcs_ipc {
	struct mbox_controller mbox;
	struct mbox_chan mbox_chans[QCOM_APCS_IPC_BITS];

22
	struct regmap *regmap;
23
	unsigned long offset;
24
	struct platform_device *clk;
25 26
};

27 28 29 30 31
struct qcom_apcs_ipc_data {
	int offset;
	char *clk_name;
};

32 33 34 35
static const struct qcom_apcs_ipc_data ipq6018_apcs_data = {
	.offset = 8, .clk_name = "qcom,apss-ipq6018-clk"
};

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static const struct qcom_apcs_ipc_data ipq8074_apcs_data = {
	.offset = 8, .clk_name = NULL
};

static const struct qcom_apcs_ipc_data msm8916_apcs_data = {
	.offset = 8, .clk_name = "qcom-apcs-msm8916-clk"
};

static const struct qcom_apcs_ipc_data msm8996_apcs_data = {
	.offset = 16, .clk_name = NULL
};

static const struct qcom_apcs_ipc_data msm8998_apcs_data = {
	.offset = 8, .clk_name = NULL
};

static const struct qcom_apcs_ipc_data apps_shared_apcs_data = {
	.offset = 12, .clk_name = NULL
};

56 57 58 59
static const struct regmap_config apcs_regmap_config = {
	.reg_bits = 32,
	.reg_stride = 4,
	.val_bits = 32,
60
	.max_register = 0xFFC,
61 62 63
	.fast_io = true,
};

64 65 66 67 68 69
static int qcom_apcs_ipc_send_data(struct mbox_chan *chan, void *data)
{
	struct qcom_apcs_ipc *apcs = container_of(chan->mbox,
						  struct qcom_apcs_ipc, mbox);
	unsigned long idx = (unsigned long)chan->con_priv;

70
	return regmap_write(apcs->regmap, apcs->offset, BIT(idx));
71 72 73 74 75 76 77 78 79
}

static const struct mbox_chan_ops qcom_apcs_ipc_ops = {
	.send_data = qcom_apcs_ipc_send_data,
};

static int qcom_apcs_ipc_probe(struct platform_device *pdev)
{
	struct qcom_apcs_ipc *apcs;
80
	const struct qcom_apcs_ipc_data *apcs_data;
81
	struct regmap *regmap;
82 83 84 85 86 87 88 89 90 91 92 93 94 95
	struct resource *res;
	void __iomem *base;
	unsigned long i;
	int ret;

	apcs = devm_kzalloc(&pdev->dev, sizeof(*apcs), GFP_KERNEL);
	if (!apcs)
		return -ENOMEM;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(base))
		return PTR_ERR(base);

96 97 98 99
	regmap = devm_regmap_init_mmio(&pdev->dev, base, &apcs_regmap_config);
	if (IS_ERR(regmap))
		return PTR_ERR(regmap);

100
	apcs_data = of_device_get_match_data(&pdev->dev);
101

102
	apcs->regmap = regmap;
103
	apcs->offset = apcs_data->offset;
104 105 106 107 108 109 110 111 112 113

	/* Initialize channel identifiers */
	for (i = 0; i < ARRAY_SIZE(apcs->mbox_chans); i++)
		apcs->mbox_chans[i].con_priv = (void *)i;

	apcs->mbox.dev = &pdev->dev;
	apcs->mbox.ops = &qcom_apcs_ipc_ops;
	apcs->mbox.chans = apcs->mbox_chans;
	apcs->mbox.num_chans = ARRAY_SIZE(apcs->mbox_chans);

114
	ret = devm_mbox_controller_register(&pdev->dev, &apcs->mbox);
115 116 117 118 119
	if (ret) {
		dev_err(&pdev->dev, "failed to register APCS IPC controller\n");
		return ret;
	}

120
	if (apcs_data->clk_name) {
121
		apcs->clk = platform_device_register_data(&pdev->dev,
122
							  apcs_data->clk_name,
123 124
							  PLATFORM_DEVID_NONE,
							  NULL, 0);
125 126 127 128
		if (IS_ERR(apcs->clk))
			dev_err(&pdev->dev, "failed to register APCS clk\n");
	}

129 130 131 132 133 134 135 136
	platform_set_drvdata(pdev, apcs);

	return 0;
}

static int qcom_apcs_ipc_remove(struct platform_device *pdev)
{
	struct qcom_apcs_ipc *apcs = platform_get_drvdata(pdev);
137
	struct platform_device *clk = apcs->clk;
138

139
	platform_device_unregister(clk);
140 141 142 143 144 145

	return 0;
}

/* .data is the offset of the ipc register within the global block */
static const struct of_device_id qcom_apcs_ipc_of_match[] = {
146
	{ .compatible = "qcom,ipq6018-apcs-apps-global", .data = &ipq6018_apcs_data },
147 148 149 150 151 152 153 154
	{ .compatible = "qcom,ipq8074-apcs-apps-global", .data = &ipq8074_apcs_data },
	{ .compatible = "qcom,msm8916-apcs-kpss-global", .data = &msm8916_apcs_data },
	{ .compatible = "qcom,msm8996-apcs-hmss-global", .data = &msm8996_apcs_data },
	{ .compatible = "qcom,msm8998-apcs-hmss-global", .data = &msm8998_apcs_data },
	{ .compatible = "qcom,qcs404-apcs-apps-global", .data = &msm8916_apcs_data },
	{ .compatible = "qcom,sc7180-apss-shared", .data = &apps_shared_apcs_data },
	{ .compatible = "qcom,sdm845-apss-shared", .data = &apps_shared_apcs_data },
	{ .compatible = "qcom,sm8150-apss-shared", .data = &apps_shared_apcs_data },
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	{}
};
MODULE_DEVICE_TABLE(of, qcom_apcs_ipc_of_match);

static struct platform_driver qcom_apcs_ipc_driver = {
	.probe = qcom_apcs_ipc_probe,
	.remove = qcom_apcs_ipc_remove,
	.driver = {
		.name = "qcom_apcs_ipc",
		.of_match_table = qcom_apcs_ipc_of_match,
	},
};

static int __init qcom_apcs_ipc_init(void)
{
	return platform_driver_register(&qcom_apcs_ipc_driver);
}
postcore_initcall(qcom_apcs_ipc_init);

static void __exit qcom_apcs_ipc_exit(void)
{
	platform_driver_unregister(&qcom_apcs_ipc_driver);
}
module_exit(qcom_apcs_ipc_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Qualcomm APCS IPC driver");