blob: 725e3703c8194d310d7bfbdfe6143c42bbba0237 [file] [log] [blame]
Meng Wang61af6842018-09-10 17:47:55 +08001// SPDX-License-Identifier: GPL-2.0
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302/*
Banajit Goswamicb3ee0e2018-03-30 13:17:39 -07003 * Copyright (c) 2012-2014, 2017-2018, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304 */
5
6#include <linux/init.h>
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/err.h>
10#include <linux/delay.h>
11#include <linux/platform_device.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053012#include <ipc/apr.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053013#include <linux/of_device.h>
14#include <linux/sysfs.h>
15#include <linux/workqueue.h>
16
17#include <soc/qcom/subsystem_restart.h>
18
19#define Q6_PIL_GET_DELAY_MS 100
20#define BOOT_CMD 1
Shaikh Shadul9850fb12018-10-01 23:08:01 +053021#define SSR_RESET_CMD 1
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053022#define IMAGE_UNLOAD_CMD 0
23
24static ssize_t adsp_boot_store(struct kobject *kobj,
25 struct kobj_attribute *attr,
26 const char *buf, size_t count);
27
Shaikh Shadul9850fb12018-10-01 23:08:01 +053028static ssize_t adsp_ssr_store(struct kobject *kobj,
29 struct kobj_attribute *attr,
30 const char *buf, size_t count);
31
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053032struct adsp_loader_private {
33 void *pil_h;
34 struct kobject *boot_adsp_obj;
35 struct attribute_group *attr_group;
36};
37
38static struct kobj_attribute adsp_boot_attribute =
39 __ATTR(boot, 0220, NULL, adsp_boot_store);
40
Shaikh Shadul9850fb12018-10-01 23:08:01 +053041static struct kobj_attribute adsp_ssr_attribute =
42 __ATTR(ssr, 0220, NULL, adsp_ssr_store);
43
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053044static struct attribute *attrs[] = {
45 &adsp_boot_attribute.attr,
Shaikh Shadul9850fb12018-10-01 23:08:01 +053046 &adsp_ssr_attribute.attr,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053047 NULL,
48};
49
50static struct work_struct adsp_ldr_work;
51static struct platform_device *adsp_private;
52static void adsp_loader_unload(struct platform_device *pdev);
53
54static void adsp_load_fw(struct work_struct *adsp_ldr_work)
55{
56 struct platform_device *pdev = adsp_private;
57 struct adsp_loader_private *priv = NULL;
58
59 const char *adsp_dt = "qcom,adsp-state";
60 int rc = 0;
61 u32 adsp_state;
62 const char *img_name;
63
64 if (!pdev) {
65 dev_err(&pdev->dev, "%s: Platform device null\n", __func__);
66 goto fail;
67 }
68
69 if (!pdev->dev.of_node) {
70 dev_err(&pdev->dev,
71 "%s: Device tree information missing\n", __func__);
72 goto fail;
73 }
74
75 rc = of_property_read_u32(pdev->dev.of_node, adsp_dt, &adsp_state);
76 if (rc) {
77 dev_err(&pdev->dev,
78 "%s: ADSP state = %x\n", __func__, adsp_state);
79 goto fail;
80 }
81
82 rc = of_property_read_string(pdev->dev.of_node,
83 "qcom,proc-img-to-load",
84 &img_name);
85
86 if (rc) {
87 dev_dbg(&pdev->dev,
88 "%s: loading default image ADSP\n", __func__);
89 goto load_adsp;
90 }
91 if (!strcmp(img_name, "modem")) {
92 /* adsp_state always returns "0". So load modem image based on
93 * apr_modem_state to prevent loading of image twice
94 */
95 adsp_state = apr_get_modem_state();
96 if (adsp_state == APR_SUBSYS_DOWN) {
97 priv = platform_get_drvdata(pdev);
98 if (!priv) {
99 dev_err(&pdev->dev,
100 " %s: Private data get failed\n", __func__);
101 goto fail;
102 }
103
104 priv->pil_h = subsystem_get("modem");
105 if (IS_ERR(priv->pil_h)) {
106 dev_err(&pdev->dev, "%s: pil get failed,\n",
107 __func__);
108 goto fail;
109 }
110
111 /* Set the state of the ADSP in APR driver */
112 apr_set_modem_state(APR_SUBSYS_LOADED);
113 } else if (adsp_state == APR_SUBSYS_LOADED) {
114 dev_dbg(&pdev->dev,
115 "%s: MDSP state = %x\n", __func__, adsp_state);
116 }
117
118 dev_dbg(&pdev->dev, "%s: Q6/MDSP image is loaded\n", __func__);
119 return;
120 }
121load_adsp:
122 {
123 adsp_state = apr_get_q6_state();
124 if (adsp_state == APR_SUBSYS_DOWN) {
125 priv = platform_get_drvdata(pdev);
126 if (!priv) {
127 dev_err(&pdev->dev,
128 " %s: Private data get failed\n", __func__);
129 goto fail;
130 }
131
132 priv->pil_h = subsystem_get("adsp");
133 if (IS_ERR(priv->pil_h)) {
134 dev_err(&pdev->dev, "%s: pil get failed,\n",
135 __func__);
136 goto fail;
137 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530138 } else if (adsp_state == APR_SUBSYS_LOADED) {
139 dev_dbg(&pdev->dev,
140 "%s: ADSP state = %x\n", __func__, adsp_state);
141 }
142
143 dev_dbg(&pdev->dev, "%s: Q6/ADSP image is loaded\n", __func__);
144 return;
145 }
146fail:
147 dev_err(&pdev->dev, "%s: Q6 image loading failed\n", __func__);
148}
149
150static void adsp_loader_do(struct platform_device *pdev)
151{
152 schedule_work(&adsp_ldr_work);
153}
154
Shaikh Shadul9850fb12018-10-01 23:08:01 +0530155static ssize_t adsp_ssr_store(struct kobject *kobj,
156 struct kobj_attribute *attr,
157 const char *buf,
158 size_t count)
159{
160 int ssr_command = 0;
161 struct subsys_device *adsp_dev = NULL;
162 struct platform_device *pdev = adsp_private;
163 struct adsp_loader_private *priv = NULL;
164 int rc;
165
166 dev_dbg(&pdev->dev, "%s: going to call adsp ssr\n ", __func__);
167
168 if (kstrtoint(buf, 10, &ssr_command) < 0)
169 return -EINVAL;
170
171 if (ssr_command != SSR_RESET_CMD)
172 return -EINVAL;
173
174 priv = platform_get_drvdata(pdev);
175 if (!priv)
176 return -EINVAL;
177
178 adsp_dev = (struct subsys_device *)priv->pil_h;
179 if (!adsp_dev)
180 return -EINVAL;
181
182 dev_err(&pdev->dev, "requesting for ADSP restart\n");
183
184 /* subsystem_restart_dev has worker queue to handle */
185 rc = subsystem_restart_dev(adsp_dev);
186 if (rc) {
187 dev_err(&pdev->dev, "subsystem_restart_dev failed\n");
188 return rc;
189 }
190
191 dev_dbg(&pdev->dev, "ADSP restarted\n");
192 return count;
193}
194
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530195static ssize_t adsp_boot_store(struct kobject *kobj,
196 struct kobj_attribute *attr,
197 const char *buf,
198 size_t count)
199{
200 int boot = 0;
201
202 if (sscanf(buf, "%du", &boot) != 1) {
203 pr_err("%s: failed to read boot info from string\n", __func__);
204 return -EINVAL;
205 }
206
207 if (boot == BOOT_CMD) {
208 pr_debug("%s: going to call adsp_loader_do\n", __func__);
209 adsp_loader_do(adsp_private);
210 } else if (boot == IMAGE_UNLOAD_CMD) {
211 pr_debug("%s: going to call adsp_unloader\n", __func__);
212 adsp_loader_unload(adsp_private);
213 }
214 return count;
215}
216
217static void adsp_loader_unload(struct platform_device *pdev)
218{
219 struct adsp_loader_private *priv = NULL;
220
221 priv = platform_get_drvdata(pdev);
222
223 if (!priv)
224 return;
225
226 if (priv->pil_h) {
227 dev_dbg(&pdev->dev, "%s: calling subsystem put\n", __func__);
228 subsystem_put(priv->pil_h);
229 priv->pil_h = NULL;
230 }
231}
232
233static int adsp_loader_init_sysfs(struct platform_device *pdev)
234{
235 int ret = -EINVAL;
236 struct adsp_loader_private *priv = NULL;
237
238 adsp_private = NULL;
239 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
240 if (!priv) {
241 ret = -ENOMEM;
242 return ret;
243 }
244
245 platform_set_drvdata(pdev, priv);
246
247 priv->pil_h = NULL;
248 priv->boot_adsp_obj = NULL;
249 priv->attr_group = devm_kzalloc(&pdev->dev,
250 sizeof(*(priv->attr_group)),
251 GFP_KERNEL);
252 if (!priv->attr_group) {
253 ret = -ENOMEM;
254 goto error_return;
255 }
256
257 priv->attr_group->attrs = attrs;
258
259 priv->boot_adsp_obj = kobject_create_and_add("boot_adsp", kernel_kobj);
260 if (!priv->boot_adsp_obj) {
261 dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
262 __func__);
263 ret = -ENOMEM;
264 goto error_return;
265 }
266
267 ret = sysfs_create_group(priv->boot_adsp_obj, priv->attr_group);
268 if (ret) {
269 dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
270 __func__, ret);
271 goto error_return;
272 }
273
274 adsp_private = pdev;
275
276 return 0;
277
278error_return:
279
280 if (priv->boot_adsp_obj) {
281 kobject_del(priv->boot_adsp_obj);
282 priv->boot_adsp_obj = NULL;
283 }
284
285 return ret;
286}
287
288static int adsp_loader_remove(struct platform_device *pdev)
289{
290 struct adsp_loader_private *priv = NULL;
291
292 priv = platform_get_drvdata(pdev);
293
294 if (!priv)
295 return 0;
296
297 if (priv->pil_h) {
298 subsystem_put(priv->pil_h);
299 priv->pil_h = NULL;
300 }
301
302 if (priv->boot_adsp_obj) {
303 sysfs_remove_group(priv->boot_adsp_obj, priv->attr_group);
304 kobject_del(priv->boot_adsp_obj);
305 priv->boot_adsp_obj = NULL;
306 }
307
308 return 0;
309}
310
311static int adsp_loader_probe(struct platform_device *pdev)
312{
313 int ret = adsp_loader_init_sysfs(pdev);
314
315 if (ret != 0) {
316 dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
317 return ret;
318 }
319
320 INIT_WORK(&adsp_ldr_work, adsp_load_fw);
321
322 return 0;
323}
324
325static const struct of_device_id adsp_loader_dt_match[] = {
326 { .compatible = "qcom,adsp-loader" },
327 { }
328};
329MODULE_DEVICE_TABLE(of, adsp_loader_dt_match);
330
331static struct platform_driver adsp_loader_driver = {
332 .driver = {
333 .name = "adsp-loader",
334 .owner = THIS_MODULE,
335 .of_match_table = adsp_loader_dt_match,
336 },
337 .probe = adsp_loader_probe,
338 .remove = adsp_loader_remove,
339};
340
341static int __init adsp_loader_init(void)
342{
343 return platform_driver_register(&adsp_loader_driver);
344}
345module_init(adsp_loader_init);
346
347static void __exit adsp_loader_exit(void)
348{
349 platform_driver_unregister(&adsp_loader_driver);
350}
351module_exit(adsp_loader_exit);
352
353MODULE_DESCRIPTION("ADSP Loader module");
354MODULE_LICENSE("GPL v2");