|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <linux/atomic.h> |
| 7 | +#include <linux/bug.h> |
| 8 | +#include <linux/interrupt.h> |
| 9 | +#include <linux/jiffies.h> |
| 10 | +#include <linux/kernel.h> |
| 11 | +#include <linux/module.h> |
| 12 | +#include <linux/of.h> |
| 13 | +#include <linux/platform_device.h> |
| 14 | +#include <linux/slab.h> |
| 15 | +#include <linux/types.h> |
| 16 | +#include <linux/wait.h> |
| 17 | + |
| 18 | +#include <soc/qcom/rpmh.h> |
| 19 | + |
| 20 | +#include "rpmh-internal.h" |
| 21 | + |
| 22 | +#define RPMH_TIMEOUT_MS msecs_to_jiffies(10000) |
| 23 | + |
| 24 | +#define DEFINE_RPMH_MSG_ONSTACK(dev, s, q, name) \ |
| 25 | + struct rpmh_request name = { \ |
| 26 | + .msg = { \ |
| 27 | + .state = s, \ |
| 28 | + .cmds = name.cmd, \ |
| 29 | + .num_cmds = 0, \ |
| 30 | + .wait_for_compl = true, \ |
| 31 | + }, \ |
| 32 | + .cmd = { { 0 } }, \ |
| 33 | + .completion = q, \ |
| 34 | + .dev = dev, \ |
| 35 | + } |
| 36 | + |
| 37 | +#define ctrlr_to_drv(ctrlr) container_of(ctrlr, struct rsc_drv, client) |
| 38 | + |
| 39 | +static struct rpmh_ctrlr *get_rpmh_ctrlr(const struct device *dev) |
| 40 | +{ |
| 41 | + struct rsc_drv *drv = dev_get_drvdata(dev->parent); |
| 42 | + |
| 43 | + return &drv->client; |
| 44 | +} |
| 45 | + |
| 46 | +void rpmh_tx_done(const struct tcs_request *msg, int r) |
| 47 | +{ |
| 48 | + struct rpmh_request *rpm_msg = container_of(msg, struct rpmh_request, |
| 49 | + msg); |
| 50 | + struct completion *compl = rpm_msg->completion; |
| 51 | + |
| 52 | + rpm_msg->err = r; |
| 53 | + |
| 54 | + if (r) |
| 55 | + dev_err(rpm_msg->dev, "RPMH TX fail in msg addr=%#x, err=%d\n", |
| 56 | + rpm_msg->msg.cmds[0].addr, r); |
| 57 | + |
| 58 | + /* Signal the blocking thread we are done */ |
| 59 | + if (compl) |
| 60 | + complete(compl); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * __rpmh_write: send the RPMH request |
| 65 | + * |
| 66 | + * @dev: The device making the request |
| 67 | + * @state: Active/Sleep request type |
| 68 | + * @rpm_msg: The data that needs to be sent (cmds). |
| 69 | + */ |
| 70 | +static int __rpmh_write(const struct device *dev, enum rpmh_state state, |
| 71 | + struct rpmh_request *rpm_msg) |
| 72 | +{ |
| 73 | + struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev); |
| 74 | + |
| 75 | + rpm_msg->msg.state = state; |
| 76 | + |
| 77 | + if (state != RPMH_ACTIVE_ONLY_STATE) |
| 78 | + return -EINVAL; |
| 79 | + |
| 80 | + WARN_ON(irqs_disabled()); |
| 81 | + |
| 82 | + return rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msg->msg); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * rpmh_write: Write a set of RPMH commands and block until response |
| 87 | + * |
| 88 | + * @rc: The RPMH handle got from rpmh_get_client |
| 89 | + * @state: Active/sleep set |
| 90 | + * @cmd: The payload data |
| 91 | + * @n: The number of elements in @cmd |
| 92 | + * |
| 93 | + * May sleep. Do not call from atomic contexts. |
| 94 | + */ |
| 95 | +int rpmh_write(const struct device *dev, enum rpmh_state state, |
| 96 | + const struct tcs_cmd *cmd, u32 n) |
| 97 | +{ |
| 98 | + DECLARE_COMPLETION_ONSTACK(compl); |
| 99 | + DEFINE_RPMH_MSG_ONSTACK(dev, state, &compl, rpm_msg); |
| 100 | + int ret; |
| 101 | + |
| 102 | + if (!cmd || !n || n > MAX_RPMH_PAYLOAD) |
| 103 | + return -EINVAL; |
| 104 | + |
| 105 | + memcpy(rpm_msg.cmd, cmd, n * sizeof(*cmd)); |
| 106 | + rpm_msg.msg.num_cmds = n; |
| 107 | + |
| 108 | + ret = __rpmh_write(dev, state, &rpm_msg); |
| 109 | + if (ret) |
| 110 | + return ret; |
| 111 | + |
| 112 | + ret = wait_for_completion_timeout(&compl, RPMH_TIMEOUT_MS); |
| 113 | + WARN_ON(!ret); |
| 114 | + return (ret > 0) ? 0 : -ETIMEDOUT; |
| 115 | +} |
| 116 | +EXPORT_SYMBOL(rpmh_write); |
0 commit comments