xref: /openbmc/linux/sound/soc/qcom/qdsp6/q6prm.c (revision 2f0754f2)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, Linaro Limited
3 
4 #include <linux/slab.h>
5 #include <linux/wait.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/of.h>
9 #include <linux/delay.h>
10 #include <linux/of_platform.h>
11 #include <linux/jiffies.h>
12 #include <linux/soc/qcom/apr.h>
13 #include <dt-bindings/soc/qcom,gpr.h>
14 #include <dt-bindings/sound/qcom,q6dsp-lpass-ports.h>
15 #include "q6prm.h"
16 #include "audioreach.h"
17 
18 struct q6prm {
19 	struct device *dev;
20 	gpr_device_t *gdev;
21 	wait_queue_head_t wait;
22 	struct gpr_ibasic_rsp_result_t result;
23 	struct mutex lock;
24 };
25 
26 #define PRM_CMD_REQUEST_HW_RSC		0x0100100F
27 #define PRM_CMD_RSP_REQUEST_HW_RSC	0x02001002
28 #define PRM_CMD_RELEASE_HW_RSC		0x01001010
29 #define PRM_CMD_RSP_RELEASE_HW_RSC	0x02001003
30 #define PARAM_ID_RSC_HW_CORE		0x08001032
31 #define PARAM_ID_RSC_LPASS_CORE		0x0800102B
32 #define PARAM_ID_RSC_AUDIO_HW_CLK	0x0800102C
33 
34 struct prm_cmd_request_hw_core {
35 	struct apm_module_param_data param_data;
36 	uint32_t hw_clk_id;
37 } __packed;
38 
39 struct prm_cmd_request_rsc {
40 	struct apm_module_param_data param_data;
41 	uint32_t num_clk_id;
42 	struct audio_hw_clk_cfg clock_id;
43 } __packed;
44 
45 struct prm_cmd_release_rsc {
46 	struct apm_module_param_data param_data;
47 	uint32_t num_clk_id;
48 	struct audio_hw_clk_rel_cfg clock_id;
49 } __packed;
50 
51 static int q6prm_send_cmd_sync(struct q6prm *prm, struct gpr_pkt *pkt, uint32_t rsp_opcode)
52 {
53 	return audioreach_send_cmd_sync(prm->dev, prm->gdev, &prm->result, &prm->lock,
54 					NULL, &prm->wait, pkt, rsp_opcode);
55 }
56 
57 static int q6prm_set_hw_core_req(struct device *dev, uint32_t hw_block_id, bool enable)
58 {
59 	struct q6prm *prm = dev_get_drvdata(dev->parent);
60 	struct apm_module_param_data *param_data;
61 	struct prm_cmd_request_hw_core *req;
62 	gpr_device_t *gdev = prm->gdev;
63 	uint32_t opcode, rsp_opcode;
64 	struct gpr_pkt *pkt;
65 	int rc;
66 
67 	if (enable) {
68 		opcode = PRM_CMD_REQUEST_HW_RSC;
69 		rsp_opcode = PRM_CMD_RSP_REQUEST_HW_RSC;
70 	} else {
71 		opcode = PRM_CMD_RELEASE_HW_RSC;
72 		rsp_opcode = PRM_CMD_RSP_RELEASE_HW_RSC;
73 	}
74 
75 	pkt = audioreach_alloc_cmd_pkt(sizeof(*req), opcode, 0, gdev->svc.id, GPR_PRM_MODULE_IID);
76 	if (IS_ERR(pkt))
77 		return PTR_ERR(pkt);
78 
79 	req = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
80 
81 	param_data = &req->param_data;
82 
83 	param_data->module_instance_id = GPR_PRM_MODULE_IID;
84 	param_data->error_code = 0;
85 	param_data->param_id = PARAM_ID_RSC_HW_CORE;
86 	param_data->param_size = sizeof(*req) - APM_MODULE_PARAM_DATA_SIZE;
87 
88 	req->hw_clk_id = hw_block_id;
89 
90 	rc = q6prm_send_cmd_sync(prm, pkt, rsp_opcode);
91 
92 	kfree(pkt);
93 
94 	return rc;
95 }
96 
97 int q6prm_vote_lpass_core_hw(struct device *dev, uint32_t hw_block_id,
98 			     const char *client_name, uint32_t *client_handle)
99 {
100 	return q6prm_set_hw_core_req(dev, hw_block_id, true);
101 
102 }
103 EXPORT_SYMBOL_GPL(q6prm_vote_lpass_core_hw);
104 
105 int q6prm_unvote_lpass_core_hw(struct device *dev, uint32_t hw_block_id, uint32_t client_handle)
106 {
107 	return q6prm_set_hw_core_req(dev, hw_block_id, false);
108 }
109 EXPORT_SYMBOL_GPL(q6prm_unvote_lpass_core_hw);
110 
111 static int q6prm_request_lpass_clock(struct device *dev, int clk_id, int clk_attr, int clk_root,
112 				     unsigned int freq)
113 {
114 	struct q6prm *prm = dev_get_drvdata(dev->parent);
115 	struct apm_module_param_data *param_data;
116 	struct prm_cmd_request_rsc *req;
117 	gpr_device_t *gdev = prm->gdev;
118 	struct gpr_pkt *pkt;
119 	int rc;
120 
121 	pkt = audioreach_alloc_cmd_pkt(sizeof(*req), PRM_CMD_REQUEST_HW_RSC, 0, gdev->svc.id,
122 				       GPR_PRM_MODULE_IID);
123 	if (IS_ERR(pkt))
124 		return PTR_ERR(pkt);
125 
126 	req = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
127 
128 	param_data = &req->param_data;
129 
130 	param_data->module_instance_id = GPR_PRM_MODULE_IID;
131 	param_data->error_code = 0;
132 	param_data->param_id = PARAM_ID_RSC_AUDIO_HW_CLK;
133 	param_data->param_size = sizeof(*req) - APM_MODULE_PARAM_DATA_SIZE;
134 
135 	req->num_clk_id = 1;
136 	req->clock_id.clock_id = clk_id;
137 	req->clock_id.clock_freq = freq;
138 	req->clock_id.clock_attri = clk_attr;
139 	req->clock_id.clock_root = clk_root;
140 
141 	rc = q6prm_send_cmd_sync(prm, pkt, PRM_CMD_RSP_REQUEST_HW_RSC);
142 
143 	kfree(pkt);
144 
145 	return rc;
146 }
147 
148 static int q6prm_release_lpass_clock(struct device *dev, int clk_id, int clk_attr, int clk_root,
149 			  unsigned int freq)
150 {
151 	struct q6prm *prm = dev_get_drvdata(dev->parent);
152 	struct apm_module_param_data *param_data;
153 	struct prm_cmd_release_rsc *rel;
154 	gpr_device_t *gdev = prm->gdev;
155 	struct gpr_pkt *pkt;
156 	int rc;
157 
158 	pkt = audioreach_alloc_cmd_pkt(sizeof(*rel), PRM_CMD_RELEASE_HW_RSC, 0, gdev->svc.id,
159 				       GPR_PRM_MODULE_IID);
160 	if (IS_ERR(pkt))
161 		return PTR_ERR(pkt);
162 
163 	rel = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
164 
165 	param_data = &rel->param_data;
166 
167 	param_data->module_instance_id = GPR_PRM_MODULE_IID;
168 	param_data->error_code = 0;
169 	param_data->param_id = PARAM_ID_RSC_AUDIO_HW_CLK;
170 	param_data->param_size = sizeof(*rel) - APM_MODULE_PARAM_DATA_SIZE;
171 
172 	rel->num_clk_id = 1;
173 	rel->clock_id.clock_id = clk_id;
174 
175 	rc = q6prm_send_cmd_sync(prm, pkt, PRM_CMD_RSP_RELEASE_HW_RSC);
176 
177 	kfree(pkt);
178 
179 	return rc;
180 }
181 
182 int q6prm_set_lpass_clock(struct device *dev, int clk_id, int clk_attr, int clk_root,
183 			  unsigned int freq)
184 {
185 	if (freq)
186 		return q6prm_request_lpass_clock(dev, clk_id, clk_attr, clk_attr, freq);
187 
188 	return q6prm_release_lpass_clock(dev, clk_id, clk_attr, clk_attr, freq);
189 }
190 EXPORT_SYMBOL_GPL(q6prm_set_lpass_clock);
191 
192 static int prm_callback(struct gpr_resp_pkt *data, void *priv, int op)
193 {
194 	gpr_device_t *gdev = priv;
195 	struct q6prm *prm = dev_get_drvdata(&gdev->dev);
196 	struct gpr_ibasic_rsp_result_t *result;
197 	struct gpr_hdr *hdr = &data->hdr;
198 
199 	switch (hdr->opcode) {
200 	case PRM_CMD_RSP_REQUEST_HW_RSC:
201 	case PRM_CMD_RSP_RELEASE_HW_RSC:
202 		result = data->payload;
203 		prm->result.opcode = hdr->opcode;
204 		prm->result.status = result->status;
205 		wake_up(&prm->wait);
206 		break;
207 	default:
208 		break;
209 	}
210 
211 	return 0;
212 }
213 
214 static int prm_probe(gpr_device_t *gdev)
215 {
216 	struct device *dev = &gdev->dev;
217 	struct q6prm *cc;
218 
219 	cc = devm_kzalloc(dev, sizeof(*cc), GFP_KERNEL);
220 	if (!cc)
221 		return -ENOMEM;
222 
223 	cc->dev = dev;
224 	cc->gdev = gdev;
225 	mutex_init(&cc->lock);
226 	init_waitqueue_head(&cc->wait);
227 	dev_set_drvdata(dev, cc);
228 
229 	return devm_of_platform_populate(dev);
230 }
231 
232 #ifdef CONFIG_OF
233 static const struct of_device_id prm_device_id[]  = {
234 	{ .compatible = "qcom,q6prm" },
235 	{},
236 };
237 MODULE_DEVICE_TABLE(of, prm_device_id);
238 #endif
239 
240 static gpr_driver_t prm_driver = {
241 	.probe = prm_probe,
242 	.gpr_callback = prm_callback,
243 	.driver = {
244 		.name = "qcom-prm",
245 		.of_match_table = of_match_ptr(prm_device_id),
246 	},
247 };
248 
249 module_gpr_driver(prm_driver);
250 MODULE_DESCRIPTION("Audio Process Manager");
251 MODULE_LICENSE("GPL");
252