adsp-loader.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  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. #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. /* Set the state of the ADSP in APR driver */
  119. apr_set_q6_state(APR_SUBSYS_LOADED);
  120. } else if (adsp_state == APR_SUBSYS_LOADED) {
  121. dev_dbg(&pdev->dev,
  122. "%s: ADSP state = %x\n", __func__, adsp_state);
  123. }
  124. dev_dbg(&pdev->dev, "%s: Q6/ADSP image is loaded\n", __func__);
  125. return;
  126. }
  127. fail:
  128. dev_err(&pdev->dev, "%s: Q6 image loading failed\n", __func__);
  129. }
  130. static void adsp_loader_do(struct platform_device *pdev)
  131. {
  132. schedule_work(&adsp_ldr_work);
  133. }
  134. static ssize_t adsp_boot_store(struct kobject *kobj,
  135. struct kobj_attribute *attr,
  136. const char *buf,
  137. size_t count)
  138. {
  139. int boot = 0;
  140. if (sscanf(buf, "%du", &boot) != 1) {
  141. pr_err("%s: failed to read boot info from string\n", __func__);
  142. return -EINVAL;
  143. }
  144. if (boot == BOOT_CMD) {
  145. pr_debug("%s: going to call adsp_loader_do\n", __func__);
  146. adsp_loader_do(adsp_private);
  147. } else if (boot == IMAGE_UNLOAD_CMD) {
  148. pr_debug("%s: going to call adsp_unloader\n", __func__);
  149. adsp_loader_unload(adsp_private);
  150. }
  151. return count;
  152. }
  153. static void adsp_loader_unload(struct platform_device *pdev)
  154. {
  155. struct adsp_loader_private *priv = NULL;
  156. priv = platform_get_drvdata(pdev);
  157. if (!priv)
  158. return;
  159. if (priv->pil_h) {
  160. dev_dbg(&pdev->dev, "%s: calling subsystem put\n", __func__);
  161. subsystem_put(priv->pil_h);
  162. priv->pil_h = NULL;
  163. }
  164. }
  165. static int adsp_loader_init_sysfs(struct platform_device *pdev)
  166. {
  167. int ret = -EINVAL;
  168. struct adsp_loader_private *priv = NULL;
  169. adsp_private = NULL;
  170. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  171. if (!priv) {
  172. ret = -ENOMEM;
  173. return ret;
  174. }
  175. platform_set_drvdata(pdev, priv);
  176. priv->pil_h = NULL;
  177. priv->boot_adsp_obj = NULL;
  178. priv->attr_group = devm_kzalloc(&pdev->dev,
  179. sizeof(*(priv->attr_group)),
  180. GFP_KERNEL);
  181. if (!priv->attr_group) {
  182. ret = -ENOMEM;
  183. goto error_return;
  184. }
  185. priv->attr_group->attrs = attrs;
  186. priv->boot_adsp_obj = kobject_create_and_add("boot_adsp", kernel_kobj);
  187. if (!priv->boot_adsp_obj) {
  188. dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
  189. __func__);
  190. ret = -ENOMEM;
  191. goto error_return;
  192. }
  193. ret = sysfs_create_group(priv->boot_adsp_obj, priv->attr_group);
  194. if (ret) {
  195. dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
  196. __func__, ret);
  197. goto error_return;
  198. }
  199. adsp_private = pdev;
  200. return 0;
  201. error_return:
  202. if (priv->boot_adsp_obj) {
  203. kobject_del(priv->boot_adsp_obj);
  204. priv->boot_adsp_obj = NULL;
  205. }
  206. return ret;
  207. }
  208. static int adsp_loader_remove(struct platform_device *pdev)
  209. {
  210. struct adsp_loader_private *priv = NULL;
  211. priv = platform_get_drvdata(pdev);
  212. if (!priv)
  213. return 0;
  214. if (priv->pil_h) {
  215. subsystem_put(priv->pil_h);
  216. priv->pil_h = NULL;
  217. }
  218. if (priv->boot_adsp_obj) {
  219. sysfs_remove_group(priv->boot_adsp_obj, priv->attr_group);
  220. kobject_del(priv->boot_adsp_obj);
  221. priv->boot_adsp_obj = NULL;
  222. }
  223. return 0;
  224. }
  225. static int adsp_loader_probe(struct platform_device *pdev)
  226. {
  227. int ret = adsp_loader_init_sysfs(pdev);
  228. if (ret != 0) {
  229. dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
  230. return ret;
  231. }
  232. INIT_WORK(&adsp_ldr_work, adsp_load_fw);
  233. return 0;
  234. }
  235. static const struct of_device_id adsp_loader_dt_match[] = {
  236. { .compatible = "qcom,adsp-loader" },
  237. { }
  238. };
  239. MODULE_DEVICE_TABLE(of, adsp_loader_dt_match);
  240. static struct platform_driver adsp_loader_driver = {
  241. .driver = {
  242. .name = "adsp-loader",
  243. .owner = THIS_MODULE,
  244. .of_match_table = adsp_loader_dt_match,
  245. },
  246. .probe = adsp_loader_probe,
  247. .remove = adsp_loader_remove,
  248. };
  249. static int __init adsp_loader_init(void)
  250. {
  251. return platform_driver_register(&adsp_loader_driver);
  252. }
  253. module_init(adsp_loader_init);
  254. static void __exit adsp_loader_exit(void)
  255. {
  256. platform_driver_unregister(&adsp_loader_driver);
  257. }
  258. module_exit(adsp_loader_exit);
  259. MODULE_DESCRIPTION("ADSP Loader module");
  260. MODULE_LICENSE("GPL v2");