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