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