cdsp-loader.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <linux/of_device.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/workqueue.h>
  22. #include <soc/qcom/subsystem_restart.h>
  23. #define BOOT_CMD 1
  24. #define IMAGE_UNLOAD_CMD 0
  25. #define CDSP_SUBSYS_DOWN 0
  26. #define CDSP_SUBSYS_LOADED 1
  27. static ssize_t cdsp_boot_store(struct kobject *kobj,
  28. struct kobj_attribute *attr,
  29. const char *buf, size_t count);
  30. struct cdsp_loader_private {
  31. void *pil_h;
  32. struct kobject *boot_cdsp_obj;
  33. struct attribute_group *attr_group;
  34. };
  35. static struct kobj_attribute cdsp_boot_attribute =
  36. __ATTR(boot, 0220, NULL, cdsp_boot_store);
  37. static struct attribute *attrs[] = {
  38. &cdsp_boot_attribute.attr,
  39. NULL,
  40. };
  41. static u32 cdsp_state = CDSP_SUBSYS_DOWN;
  42. static struct platform_device *cdsp_private;
  43. static struct work_struct cdsp_ldr_work;
  44. static void cdsp_loader_unload(struct platform_device *pdev);
  45. static void cdsp_load_fw(struct work_struct *cdsp_ldr_work)
  46. {
  47. struct platform_device *pdev = cdsp_private;
  48. struct cdsp_loader_private *priv = NULL;
  49. int rc = 0;
  50. const char *img_name;
  51. if (!pdev) {
  52. dev_err(&pdev->dev, "%s: Platform device null\n", __func__);
  53. goto fail;
  54. }
  55. if (!pdev->dev.of_node) {
  56. dev_err(&pdev->dev,
  57. "%s: Device tree information missing\n", __func__);
  58. goto fail;
  59. }
  60. rc = of_property_read_string(pdev->dev.of_node,
  61. "qcom,proc-img-to-load",
  62. &img_name);
  63. if (rc)
  64. goto fail;
  65. if (!strcmp(img_name, "cdsp")) {
  66. /* cdsp_state always returns "0".*/
  67. if (cdsp_state == CDSP_SUBSYS_DOWN) {
  68. priv = platform_get_drvdata(pdev);
  69. if (!priv) {
  70. dev_err(&pdev->dev,
  71. " %s: Private data get failed\n", __func__);
  72. goto fail;
  73. }
  74. priv->pil_h = subsystem_get("cdsp");
  75. if (IS_ERR(priv->pil_h)) {
  76. dev_err(&pdev->dev, "%s: pil get failed,\n",
  77. __func__);
  78. goto fail;
  79. }
  80. /* Set the state of the CDSP.*/
  81. cdsp_state = CDSP_SUBSYS_LOADED;
  82. } else if (cdsp_state == CDSP_SUBSYS_LOADED) {
  83. dev_dbg(&pdev->dev,
  84. "%s: CDSP state = %x\n", __func__, cdsp_state);
  85. }
  86. dev_dbg(&pdev->dev, "%s: CDSP image is loaded\n", __func__);
  87. return;
  88. }
  89. fail:
  90. dev_err(&pdev->dev, "%s: CDSP image loading failed\n", __func__);
  91. }
  92. static void cdsp_loader_do(struct platform_device *pdev)
  93. {
  94. schedule_work(&cdsp_ldr_work);
  95. }
  96. static ssize_t cdsp_boot_store(struct kobject *kobj,
  97. struct kobj_attribute *attr,
  98. const char *buf,
  99. size_t count)
  100. {
  101. int boot = 0, ret = 0;
  102. ret = sscanf(buf, "%du", &boot);
  103. if (ret != 1)
  104. pr_debug("%s: invalid arguments for cdsp_loader.\n", __func__);
  105. if (boot == BOOT_CMD) {
  106. pr_debug("%s: going to call cdsp_loader_do\n", __func__);
  107. cdsp_loader_do(cdsp_private);
  108. } else if (boot == IMAGE_UNLOAD_CMD) {
  109. pr_debug("%s: going to call cdsp_unloader\n", __func__);
  110. cdsp_loader_unload(cdsp_private);
  111. }
  112. return count;
  113. }
  114. static void cdsp_loader_unload(struct platform_device *pdev)
  115. {
  116. struct cdsp_loader_private *priv = NULL;
  117. priv = platform_get_drvdata(pdev);
  118. if (!priv)
  119. return;
  120. if (priv->pil_h) {
  121. dev_dbg(&pdev->dev, "%s: calling subsystem put\n", __func__);
  122. subsystem_put(priv->pil_h);
  123. priv->pil_h = NULL;
  124. }
  125. }
  126. static int cdsp_loader_init_sysfs(struct platform_device *pdev)
  127. {
  128. int ret = -EINVAL;
  129. struct cdsp_loader_private *priv = NULL;
  130. cdsp_private = NULL;
  131. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  132. if (!priv) {
  133. ret = -ENOMEM;
  134. return ret;
  135. }
  136. platform_set_drvdata(pdev, priv);
  137. priv->pil_h = NULL;
  138. priv->boot_cdsp_obj = NULL;
  139. priv->attr_group = devm_kzalloc(&pdev->dev,
  140. sizeof(*(priv->attr_group)),
  141. GFP_KERNEL);
  142. if (!priv->attr_group) {
  143. dev_err(&pdev->dev, "%s: malloc attr_group failed\n",
  144. __func__);
  145. ret = -ENOMEM;
  146. goto error_return;
  147. }
  148. priv->attr_group->attrs = attrs;
  149. priv->boot_cdsp_obj = kobject_create_and_add("boot_cdsp", kernel_kobj);
  150. if (!priv->boot_cdsp_obj) {
  151. dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
  152. __func__);
  153. ret = -ENOMEM;
  154. goto error_return;
  155. }
  156. ret = sysfs_create_group(priv->boot_cdsp_obj, priv->attr_group);
  157. if (ret) {
  158. dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
  159. __func__, ret);
  160. goto error_return;
  161. }
  162. cdsp_private = pdev;
  163. return 0;
  164. error_return:
  165. if (priv->boot_cdsp_obj) {
  166. kobject_del(priv->boot_cdsp_obj);
  167. priv->boot_cdsp_obj = NULL;
  168. }
  169. return ret;
  170. }
  171. static int cdsp_loader_remove(struct platform_device *pdev)
  172. {
  173. struct cdsp_loader_private *priv = NULL;
  174. priv = platform_get_drvdata(pdev);
  175. if (!priv)
  176. return 0;
  177. if (priv->pil_h) {
  178. subsystem_put(priv->pil_h);
  179. priv->pil_h = NULL;
  180. }
  181. if (priv->boot_cdsp_obj) {
  182. sysfs_remove_group(priv->boot_cdsp_obj, priv->attr_group);
  183. kobject_del(priv->boot_cdsp_obj);
  184. priv->boot_cdsp_obj = NULL;
  185. }
  186. return 0;
  187. }
  188. static int cdsp_loader_probe(struct platform_device *pdev)
  189. {
  190. int ret = cdsp_loader_init_sysfs(pdev);
  191. if (ret != 0) {
  192. dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
  193. return ret;
  194. }
  195. INIT_WORK(&cdsp_ldr_work, cdsp_load_fw);
  196. return 0;
  197. }
  198. static const struct of_device_id cdsp_loader_dt_match[] = {
  199. { .compatible = "qcom,cdsp-loader" },
  200. { }
  201. };
  202. MODULE_DEVICE_TABLE(of, cdsp_loader_dt_match);
  203. static struct platform_driver cdsp_loader_driver = {
  204. .driver = {
  205. .name = "cdsp-loader",
  206. .owner = THIS_MODULE,
  207. .of_match_table = cdsp_loader_dt_match,
  208. },
  209. .probe = cdsp_loader_probe,
  210. .remove = cdsp_loader_remove,
  211. };
  212. static int __init cdsp_loader_init(void)
  213. {
  214. return platform_driver_register(&cdsp_loader_driver);
  215. }
  216. module_init(cdsp_loader_init);
  217. static void __exit cdsp_loader_exit(void)
  218. {
  219. platform_driver_unregister(&cdsp_loader_driver);
  220. }
  221. module_exit(cdsp_loader_exit);
  222. MODULE_DESCRIPTION("CDSP Loader module");
  223. MODULE_LICENSE("GPL v2");