dynpf_scmi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/scmi_protocol.h>
  6. #include <linux/qcom_scmi_vendor.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/err.h>
  11. #include <linux/errno.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/cpu.h>
  15. #include <linux/slab.h>
  16. static struct kobject dynpf_kobj;
  17. static struct scmi_protocol_handle *ph;
  18. static const struct qcom_scmi_vendor_ops *ops;
  19. #define DYNPF_ALGO_STR 0x53434d495f444750 /* "SCMI_DGP" */
  20. static struct scmi_device *sdev;
  21. struct qcom_dynpf_attr {
  22. struct attribute attr;
  23. ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
  24. char *buf);
  25. ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
  26. const char *buf, size_t count);
  27. };
  28. enum dynpf_param_ids {
  29. PARAM_ENABLE_DYNPF = 1,
  30. };
  31. #define to_dynpf_attr(_attr) \
  32. container_of(_attr, struct qcom_dynpf_attr, attr)
  33. #define DYNPF_ATTR_RW(_name) \
  34. static struct qcom_dynpf_attr _name = \
  35. __ATTR(_name, 0644, show_##_name, store_##_name) \
  36. static ssize_t store_enable_dynpf(struct kobject *kobj,
  37. struct attribute *attr, const char *buf,
  38. size_t count)
  39. {
  40. u32 val;
  41. int ret;
  42. ret = kstrtouint(buf, 10, &val);
  43. if (ret < 0)
  44. return ret;
  45. ret = ops->set_param(ph, &val, DYNPF_ALGO_STR, PARAM_ENABLE_DYNPF,
  46. sizeof(u32));
  47. return ((ret < 0) ? ret : count);
  48. }
  49. static ssize_t show_enable_dynpf(struct kobject *kobj,
  50. struct attribute *attr, char *buf)
  51. {
  52. u32 val;
  53. int ret;
  54. ret = ops->get_param(ph, &val, DYNPF_ALGO_STR, PARAM_ENABLE_DYNPF, 0,
  55. sizeof(u32));
  56. if (ret < 0)
  57. return ret;
  58. return scnprintf(buf, PAGE_SIZE, "%lu\n", le32_to_cpu(val));
  59. }
  60. DYNPF_ATTR_RW(enable_dynpf);
  61. static struct attribute *dynpf_settings_attrs[] = {
  62. &enable_dynpf.attr,
  63. NULL,
  64. };
  65. ATTRIBUTE_GROUPS(dynpf_settings);
  66. static ssize_t attr_show(struct kobject *kobj, struct attribute *attr,
  67. char *buf)
  68. {
  69. struct qcom_dynpf_attr *dynpf_attr = to_dynpf_attr(attr);
  70. ssize_t ret = -EIO;
  71. if (dynpf_attr->show)
  72. ret = dynpf_attr->show(kobj, attr, buf);
  73. return ret;
  74. }
  75. static ssize_t attr_store(struct kobject *kobj, struct attribute *attr,
  76. const char *buf, size_t count)
  77. {
  78. struct qcom_dynpf_attr *dynpf_attr = to_dynpf_attr(attr);
  79. ssize_t ret = -EIO;
  80. if (dynpf_attr->store)
  81. ret = dynpf_attr->store(kobj, attr, buf, count);
  82. return ret;
  83. }
  84. static const struct sysfs_ops dynpf_sysfs_ops = {
  85. .show = attr_show,
  86. .store = attr_store,
  87. };
  88. static struct kobj_type dynpf_settings_ktype = {
  89. .sysfs_ops = &dynpf_sysfs_ops,
  90. .default_groups = dynpf_settings_groups,
  91. };
  92. static int qcom_dynpf_probe(struct platform_device *pdev)
  93. {
  94. int ret;
  95. sdev = get_qcom_scmi_device();
  96. if (IS_ERR(sdev)) {
  97. ret = PTR_ERR(sdev);
  98. if (ret != -EPROBE_DEFER)
  99. dev_err(&pdev->dev, "Error getting scmi_dev ret = %d\n", ret);
  100. return ret;
  101. }
  102. ops = sdev->handle->devm_protocol_get(sdev, QCOM_SCMI_VENDOR_PROTOCOL, &ph);
  103. if (IS_ERR(ops)) {
  104. ret = PTR_ERR(ops);
  105. ops = NULL;
  106. return ret;
  107. }
  108. ret = kobject_init_and_add(&dynpf_kobj, &dynpf_settings_ktype,
  109. &cpu_subsys.dev_root->kobj, "dynpf");
  110. if (ret < 0) {
  111. dev_err(&pdev->dev, "failed to init dynpf kobj: %d\n", ret);
  112. kobject_put(&dynpf_kobj);
  113. }
  114. return 0;
  115. }
  116. static const struct of_device_id dynpf_table[] = {
  117. {.compatible = "qcom,dynpf"},
  118. {},
  119. };
  120. static struct platform_driver dynpf_driver = {
  121. .driver = {
  122. .name = "qcom-dynpf",
  123. .of_match_table = dynpf_table,
  124. },
  125. .probe = qcom_dynpf_probe,
  126. };
  127. module_platform_driver(dynpf_driver);
  128. MODULE_SOFTDEP("pre: qcom_scmi_client");
  129. MODULE_DESCRIPTION("Qcom DYNPF SCMI driver");
  130. MODULE_LICENSE("GPL");