adsp-loader.c 7.1 KB

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