light_aon_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Alibaba Group Holding Limited.
  4. */
  5. #include <linux/debugfs.h>
  6. #include <linux/err.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/firmware/thead/ipc.h>
  16. #define MBOX_MAX_MSG_LEN 28
  17. static struct dentry *root_debugfs_dir;
  18. struct light_aon_msg_req_misc_set_ctrl {
  19. struct light_aon_rpc_msg_hdr hdr;
  20. u32 ctrl;
  21. u32 val;
  22. u16 resource;
  23. u16 reserved[7];
  24. } __packed __aligned(4);
  25. struct light_aon_msg_req_misc_get_ctrl {
  26. struct light_aon_rpc_msg_hdr hdr;
  27. u32 ctrl;
  28. u16 resource;
  29. u16 reserved[9];
  30. } __packed __aligned(4);
  31. struct light_aon_msg_resp_misc_get_ctrl {
  32. struct light_aon_rpc_msg_hdr hdr;
  33. u32 val;
  34. u32 reserved[5];
  35. } __packed __aligned(4);
  36. struct light_aon_device {
  37. struct device *dev;
  38. char *test_buf;
  39. struct light_aon_ipc *ipc_handle;
  40. };
  41. static ssize_t light_aon_test_buf_write(struct file *filp,
  42. const char __user *userbuf,
  43. size_t count, loff_t *ppos)
  44. {
  45. struct light_aon_device *tdev = filp->private_data;
  46. int ret;
  47. if (count > MBOX_MAX_MSG_LEN)
  48. count = MBOX_MAX_MSG_LEN;
  49. ret = copy_from_user(tdev->test_buf, userbuf, count);
  50. if (ret) {
  51. ret = -EFAULT;
  52. goto out;
  53. }
  54. ret = light_aon_misc_set_control(tdev->ipc_handle, 0x1, 0x2, 0x3);
  55. ret |= light_aon_misc_set_control(tdev->ipc_handle, 0x11, 0x12, 0x13);
  56. ret |= light_aon_misc_set_control(tdev->ipc_handle, 0x21, 0x22, 0x23);
  57. ret |= light_aon_misc_set_control(tdev->ipc_handle, 0x31, 0x32, 0x33);
  58. if (ret)
  59. dev_err(tdev->dev, "failed to set control\n");
  60. //print_hex_dump(KERN_INFO, __func__, DUMP_PREFIX_NONE, 16, 1, tdev->test_buf, MBOX_MAX_MSG_LEN, true);
  61. out:
  62. return ret < 0 ? ret : count;
  63. }
  64. static ssize_t light_aon_test_buf_read(struct file *filp,
  65. char __user *userbuf,
  66. size_t count, loff_t *ppos)
  67. {
  68. struct light_aon_device *tdev = filp->private_data;
  69. //print_hex_dump(KERN_INFO, __func__, DUMP_PREFIX_NONE, 16, 1, tdev->test_buf, MBOX_MAX_MSG_LEN, true);
  70. memset(tdev->test_buf, 0, MBOX_MAX_MSG_LEN);
  71. return MBOX_MAX_MSG_LEN;
  72. }
  73. static const struct file_operations light_aon_test_buf_ops = {
  74. .write = light_aon_test_buf_write,
  75. .read = light_aon_test_buf_read,
  76. .open = simple_open,
  77. .llseek = generic_file_llseek,
  78. };
  79. static int light_aon_add_debugfs(struct platform_device *pdev, struct light_aon_device *tdev)
  80. {
  81. root_debugfs_dir = debugfs_create_dir("light_aon",NULL);
  82. if (!root_debugfs_dir) {
  83. dev_err(&pdev->dev, "Failed to create light_aon_test debugfs\n");
  84. return -EINVAL;
  85. }
  86. debugfs_create_file("test", 0600, root_debugfs_dir, tdev, &light_aon_test_buf_ops);
  87. return 0;
  88. }
  89. static int light_aon_probe(struct platform_device *pdev)
  90. {
  91. struct light_aon_device *tdev;
  92. int ret;
  93. tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
  94. if (!tdev)
  95. return -ENOMEM;
  96. tdev->dev = &pdev->dev;
  97. platform_set_drvdata(pdev, tdev);
  98. tdev->test_buf = devm_kzalloc(&pdev->dev, MBOX_MAX_MSG_LEN, GFP_KERNEL);
  99. if (!tdev->test_buf)
  100. return -ENOMEM;
  101. ret = light_aon_get_handle(&(tdev->ipc_handle));
  102. if (ret) {
  103. dev_err(&pdev->dev, "failed to get ipc_handle\n");
  104. return ret;
  105. }
  106. ret = light_aon_add_debugfs(pdev, tdev);
  107. if (ret)
  108. return ret;
  109. dev_info(&pdev->dev, "Successfully registered\n");
  110. return 0;
  111. }
  112. static int light_aon_remove(struct platform_device *pdev)
  113. {
  114. debugfs_remove_recursive(root_debugfs_dir);
  115. return 0;
  116. }
  117. static const struct of_device_id light_aon_match[] = {
  118. { .compatible = "thead,light-aon-test" },
  119. {},
  120. };
  121. static struct platform_driver light_aon_driver = {
  122. .driver = {
  123. .name = "thead,light-aon-test",
  124. .of_match_table = light_aon_match,
  125. },
  126. .probe = light_aon_probe,
  127. .remove = light_aon_remove,
  128. };
  129. module_platform_driver(light_aon_driver);
  130. MODULE_AUTHOR("fugang.duan <duanfugang.dfg@linux.alibaba.com>");
  131. MODULE_DESCRIPTION("Thead Light firmware protocol test driver");
  132. MODULE_LICENSE("GPL v2");