lastpc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <linux/cpumask.h>
  5. #include <linux/list.h>
  6. #include <linux/printk.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/of_address.h>
  10. #include <linux/slab.h>
  11. #include <linux/io.h>
  12. #include <mt-plat/mt_chip.h>
  13. #include <mt-plat/mt_lastpc.h>
  14. #include "lastpc.h"
  15. static const struct of_device_id lastpc_of_ids[] = {
  16. { .compatible = "mediatek,mt6580-mcucfg", },
  17. { .compatible = "mediatek,mt6735-mcucfg", },
  18. { .compatible = "mediatek,mt8163-mcucfg", },
  19. { .compatible = "mediatek,mt8173-mcucfg", },
  20. {}
  21. };
  22. static int lastpc_probe(struct platform_device *pdev);
  23. static int lastpc_remove(struct platform_device *pdev);
  24. static int lastpc_suspend(struct platform_device *pdev, pm_message_t state);
  25. static int lastpc_resume(struct platform_device *pdev);
  26. static char *lastpc_dump_buf;
  27. static struct lastpc lastpc_drv = {
  28. .plt_drv = {
  29. .driver = {
  30. .name = "lastpc",
  31. .bus = &platform_bus_type,
  32. .owner = THIS_MODULE,
  33. .of_match_table = lastpc_of_ids,
  34. },
  35. .probe = lastpc_probe,
  36. .remove = lastpc_remove,
  37. .suspend = lastpc_suspend,
  38. .resume = lastpc_resume,
  39. },
  40. };
  41. static int lastpc_probe(struct platform_device *pdev)
  42. {
  43. struct lastpc_plt *plt = NULL;
  44. pr_debug("%s:%d: enter\n", __func__, __LINE__);
  45. plt = lastpc_drv.cur_plt;
  46. if (plt && plt->ops && plt->ops->probe)
  47. return plt->ops->probe(plt, pdev);
  48. lastpc_drv.base = of_iomap(pdev->dev.of_node, 0);
  49. if (!lastpc_drv.base)
  50. return -ENODEV;
  51. return 0;
  52. }
  53. static int lastpc_remove(struct platform_device *pdev)
  54. {
  55. struct lastpc_plt *plt = lastpc_drv.cur_plt;
  56. pr_debug("%s:%d: enter\n", __func__, __LINE__);
  57. if (plt && plt->ops && plt->ops->remove)
  58. return plt->ops->remove(plt, pdev);
  59. return 0;
  60. }
  61. static int lastpc_suspend(struct platform_device *pdev, pm_message_t state)
  62. {
  63. struct lastpc_plt *plt = lastpc_drv.cur_plt;
  64. pr_debug("%s:%d: enter\n", __func__, __LINE__);
  65. if (plt && plt->ops && plt->ops->suspend)
  66. return plt->ops->suspend(plt, pdev, state);
  67. return 0;
  68. }
  69. static int lastpc_resume(struct platform_device *pdev)
  70. {
  71. struct lastpc_plt *plt = lastpc_drv.cur_plt;
  72. pr_debug("%s:%d: enter\n", __func__, __LINE__);
  73. if (plt && plt->ops && plt->ops->resume)
  74. return plt->ops->resume(plt, pdev);
  75. return 0;
  76. }
  77. int lastpc_register(struct lastpc_plt *plt)
  78. {
  79. if (!plt) {
  80. pr_warn("%s%d: plt is NULL\n", __func__, __LINE__);
  81. return -EINVAL;
  82. }
  83. plt->common = &lastpc_drv;
  84. lastpc_drv.cur_plt = plt;
  85. return 0;
  86. }
  87. int lastpc_dump(char *buf, int len)
  88. {
  89. struct lastpc_plt *plt = lastpc_drv.cur_plt;
  90. if (!buf) {
  91. pr_warn("%s:%d: buf is NULL\n", __func__, __LINE__);
  92. return -EINVAL;
  93. }
  94. if (!plt) {
  95. pr_warn("%s:%d: plt is NULL\n", __func__, __LINE__);
  96. return -ENODEV;
  97. }
  98. if (!plt->common) {
  99. pr_warn("%s:%d: plt->common is NULL\n", __func__, __LINE__);
  100. return -ENODEV;
  101. }
  102. if (!plt->common->base) {
  103. pr_warn("%s:%d: plt->common->base is NULL\n", __func__, __LINE__);
  104. return -ENODEV;
  105. }
  106. if (plt->ops && plt->ops->dump)
  107. return plt->ops->dump(plt, buf, len);
  108. pr_warn("no dump function implemented\n");
  109. return 0;
  110. }
  111. int lastpc_dump_min_len(void)
  112. {
  113. struct lastpc_plt *plt = lastpc_drv.cur_plt;
  114. if (!plt) {
  115. pr_warn("%s:%d: plt is NULL\n", __func__, __LINE__);
  116. return -ENODEV;
  117. }
  118. if (!plt->min_buf_len)
  119. pr_warn("%s:%d: min_buf_len is 0\n", __func__, __LINE__);
  120. return plt->min_buf_len;
  121. }
  122. int mt_reg_dump(char *buf)
  123. {
  124. strncpy(buf, lastpc_dump_buf, strlen(lastpc_dump_buf)+1);
  125. return 0;
  126. }
  127. static ssize_t lastpc_dump_show(struct device_driver *driver, char *buf)
  128. {
  129. struct lastpc_plt *plt = lastpc_drv.cur_plt;
  130. int ret = 0;
  131. ret = plt->ops->dump(plt, buf, -1);
  132. if (ret)
  133. pr_err("%s:%d: dump failed\n", __func__, __LINE__);
  134. return strlen(buf);
  135. }
  136. static ssize_t lastpc_dump_store(struct device_driver *driver, const char *buf, size_t count)
  137. {
  138. return count;
  139. }
  140. DRIVER_ATTR(lastpc_dump, 0664, lastpc_dump_show, lastpc_dump_store);
  141. static ssize_t lastpc_reboot_test_show(struct device_driver *driver, char *buf)
  142. {
  143. return snprintf(buf, PAGE_SIZE, "==LAST PC test==\n"
  144. "1.LAST PC Reboot test\n");
  145. }
  146. static ssize_t lastpc_reboot_test_store(struct device_driver *driver, const char *buf, size_t count)
  147. {
  148. struct lastpc_plt *plt = lastpc_drv.cur_plt;
  149. char *p = (char *)buf;
  150. unsigned long num = 0;
  151. if (kstrtoul(p, 10, &num) != 0) {
  152. pr_err("%s:%d: kstrtoul fail for %s\n", __func__, __LINE__, p);
  153. return 0;
  154. }
  155. switch (num) {
  156. case 1:
  157. if (plt->ops->reboot_test(plt) != 0)
  158. pr_err("%s:%d: reboot test failed\n", __func__, __LINE__);
  159. break;
  160. default:
  161. break;
  162. }
  163. return count;
  164. }
  165. DRIVER_ATTR(lastpc_reboot_test, 0664, lastpc_reboot_test_show, lastpc_reboot_test_store);
  166. static int lastpc_start(void)
  167. {
  168. struct lastpc_plt *plt = lastpc_drv.cur_plt;
  169. if (!plt->ops) {
  170. pr_err("%s:%d: ops not installed\n", __func__, __LINE__);
  171. return -ENODEV;
  172. }
  173. if (plt->ops->start)
  174. return plt->ops->start(plt);
  175. return 0;
  176. }
  177. static int __init lastpc_init(void)
  178. {
  179. int ret = 0;
  180. ret = lastpc_start();
  181. if (ret) {
  182. pr_err("%s:%d: lastpc_start failed\n", __func__, __LINE__);
  183. return -ENODEV;
  184. }
  185. /* since kernel already populates dts, our probe would be callback after this registration */
  186. ret = platform_driver_register(&lastpc_drv.plt_drv);
  187. if (ret) {
  188. pr_err("%s:%d: platform_driver_register failed\n", __func__, __LINE__);
  189. return -ENODEV;
  190. }
  191. ret = driver_create_file(&lastpc_drv.plt_drv.driver, &driver_attr_lastpc_dump);
  192. if (ret)
  193. pr_err("%s:%d: driver_create_file failed.\n", __func__, __LINE__);
  194. ret = driver_create_file(&lastpc_drv.plt_drv.driver, &driver_attr_lastpc_reboot_test);
  195. if (ret)
  196. pr_err("%s:%d: driver_create_file failed.\n", __func__, __LINE__);
  197. lastpc_dump_buf = kzalloc(lastpc_drv.cur_plt->min_buf_len, GFP_KERNEL);
  198. if (!lastpc_dump_buf)
  199. return -ENOMEM;
  200. /* we dump here and then return lastpc_dump_buf to users to prevent lastpc values cleaned by low power mechanism
  201. (MCUSYS might be turned off before lastpc_dump()) */
  202. lastpc_dump(lastpc_dump_buf, lastpc_drv.cur_plt->min_buf_len);
  203. return 0;
  204. }
  205. static void __exit lastpc_exit(void)
  206. {
  207. }
  208. module_init(lastpc_init);
  209. module_exit(lastpc_exit);