sysfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 Google LLC
  4. *
  5. * Sysfs properties to view and modify EC-controlled features on Wilco devices.
  6. * The entries will appear under /sys/bus/platform/devices/GOOG000C:00/
  7. *
  8. * See Documentation/ABI/testing/sysfs-platform-wilco-ec for more information.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/platform_data/wilco-ec.h>
  13. #include <linux/string.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/types.h>
  16. #define CMD_KB_CMOS 0x7C
  17. #define SUB_CMD_KB_CMOS_AUTO_ON 0x03
  18. struct boot_on_ac_request {
  19. u8 cmd; /* Always CMD_KB_CMOS */
  20. u8 reserved1;
  21. u8 sub_cmd; /* Always SUB_CMD_KB_CMOS_AUTO_ON */
  22. u8 reserved3to5[3];
  23. u8 val; /* Either 0 or 1 */
  24. u8 reserved7;
  25. } __packed;
  26. #define CMD_USB_CHARGE 0x39
  27. enum usb_charge_op {
  28. USB_CHARGE_GET = 0,
  29. USB_CHARGE_SET = 1,
  30. };
  31. struct usb_charge_request {
  32. u8 cmd; /* Always CMD_USB_CHARGE */
  33. u8 reserved;
  34. u8 op; /* One of enum usb_charge_op */
  35. u8 val; /* When setting, either 0 or 1 */
  36. } __packed;
  37. struct usb_charge_response {
  38. u8 reserved;
  39. u8 status; /* Set by EC to 0 on success, other value on failure */
  40. u8 val; /* When getting, set by EC to either 0 or 1 */
  41. } __packed;
  42. #define CMD_EC_INFO 0x38
  43. enum get_ec_info_op {
  44. CMD_GET_EC_LABEL = 0,
  45. CMD_GET_EC_REV = 1,
  46. CMD_GET_EC_MODEL = 2,
  47. CMD_GET_EC_BUILD_DATE = 3,
  48. };
  49. struct get_ec_info_req {
  50. u8 cmd; /* Always CMD_EC_INFO */
  51. u8 reserved;
  52. u8 op; /* One of enum get_ec_info_op */
  53. } __packed;
  54. struct get_ec_info_resp {
  55. u8 reserved[2];
  56. char value[9]; /* __nonstring: might not be null terminated */
  57. } __packed;
  58. static ssize_t boot_on_ac_store(struct device *dev,
  59. struct device_attribute *attr,
  60. const char *buf, size_t count)
  61. {
  62. struct wilco_ec_device *ec = dev_get_drvdata(dev);
  63. struct boot_on_ac_request rq;
  64. struct wilco_ec_message msg;
  65. int ret;
  66. u8 val;
  67. ret = kstrtou8(buf, 10, &val);
  68. if (ret < 0)
  69. return ret;
  70. if (val > 1)
  71. return -EINVAL;
  72. memset(&rq, 0, sizeof(rq));
  73. rq.cmd = CMD_KB_CMOS;
  74. rq.sub_cmd = SUB_CMD_KB_CMOS_AUTO_ON;
  75. rq.val = val;
  76. memset(&msg, 0, sizeof(msg));
  77. msg.type = WILCO_EC_MSG_LEGACY;
  78. msg.request_data = &rq;
  79. msg.request_size = sizeof(rq);
  80. ret = wilco_ec_mailbox(ec, &msg);
  81. if (ret < 0)
  82. return ret;
  83. return count;
  84. }
  85. static DEVICE_ATTR_WO(boot_on_ac);
  86. static ssize_t get_info(struct device *dev, char *buf, enum get_ec_info_op op)
  87. {
  88. struct wilco_ec_device *ec = dev_get_drvdata(dev);
  89. struct get_ec_info_req req = { .cmd = CMD_EC_INFO, .op = op };
  90. struct get_ec_info_resp resp;
  91. int ret;
  92. struct wilco_ec_message msg = {
  93. .type = WILCO_EC_MSG_LEGACY,
  94. .request_data = &req,
  95. .request_size = sizeof(req),
  96. .response_data = &resp,
  97. .response_size = sizeof(resp),
  98. };
  99. ret = wilco_ec_mailbox(ec, &msg);
  100. if (ret < 0)
  101. return ret;
  102. return sysfs_emit(buf, "%.*s\n", (int)sizeof(resp.value), (char *)&resp.value);
  103. }
  104. static ssize_t version_show(struct device *dev, struct device_attribute *attr,
  105. char *buf)
  106. {
  107. return get_info(dev, buf, CMD_GET_EC_LABEL);
  108. }
  109. static DEVICE_ATTR_RO(version);
  110. static ssize_t build_revision_show(struct device *dev,
  111. struct device_attribute *attr, char *buf)
  112. {
  113. return get_info(dev, buf, CMD_GET_EC_REV);
  114. }
  115. static DEVICE_ATTR_RO(build_revision);
  116. static ssize_t build_date_show(struct device *dev,
  117. struct device_attribute *attr, char *buf)
  118. {
  119. return get_info(dev, buf, CMD_GET_EC_BUILD_DATE);
  120. }
  121. static DEVICE_ATTR_RO(build_date);
  122. static ssize_t model_number_show(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. return get_info(dev, buf, CMD_GET_EC_MODEL);
  126. }
  127. static DEVICE_ATTR_RO(model_number);
  128. static int send_usb_charge(struct wilco_ec_device *ec,
  129. struct usb_charge_request *rq,
  130. struct usb_charge_response *rs)
  131. {
  132. struct wilco_ec_message msg;
  133. int ret;
  134. memset(&msg, 0, sizeof(msg));
  135. msg.type = WILCO_EC_MSG_LEGACY;
  136. msg.request_data = rq;
  137. msg.request_size = sizeof(*rq);
  138. msg.response_data = rs;
  139. msg.response_size = sizeof(*rs);
  140. ret = wilco_ec_mailbox(ec, &msg);
  141. if (ret < 0)
  142. return ret;
  143. if (rs->status)
  144. return -EIO;
  145. return 0;
  146. }
  147. static ssize_t usb_charge_show(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct wilco_ec_device *ec = dev_get_drvdata(dev);
  151. struct usb_charge_request rq;
  152. struct usb_charge_response rs;
  153. int ret;
  154. memset(&rq, 0, sizeof(rq));
  155. rq.cmd = CMD_USB_CHARGE;
  156. rq.op = USB_CHARGE_GET;
  157. ret = send_usb_charge(ec, &rq, &rs);
  158. if (ret < 0)
  159. return ret;
  160. return sysfs_emit(buf, "%d\n", rs.val);
  161. }
  162. static ssize_t usb_charge_store(struct device *dev,
  163. struct device_attribute *attr,
  164. const char *buf, size_t count)
  165. {
  166. struct wilco_ec_device *ec = dev_get_drvdata(dev);
  167. struct usb_charge_request rq;
  168. struct usb_charge_response rs;
  169. int ret;
  170. u8 val;
  171. ret = kstrtou8(buf, 10, &val);
  172. if (ret < 0)
  173. return ret;
  174. if (val > 1)
  175. return -EINVAL;
  176. memset(&rq, 0, sizeof(rq));
  177. rq.cmd = CMD_USB_CHARGE;
  178. rq.op = USB_CHARGE_SET;
  179. rq.val = val;
  180. ret = send_usb_charge(ec, &rq, &rs);
  181. if (ret < 0)
  182. return ret;
  183. return count;
  184. }
  185. static DEVICE_ATTR_RW(usb_charge);
  186. static struct attribute *wilco_dev_attrs[] = {
  187. &dev_attr_boot_on_ac.attr,
  188. &dev_attr_build_date.attr,
  189. &dev_attr_build_revision.attr,
  190. &dev_attr_model_number.attr,
  191. &dev_attr_usb_charge.attr,
  192. &dev_attr_version.attr,
  193. NULL,
  194. };
  195. static const struct attribute_group wilco_dev_attr_group = {
  196. .attrs = wilco_dev_attrs,
  197. };
  198. int wilco_ec_add_sysfs(struct wilco_ec_device *ec)
  199. {
  200. return sysfs_create_group(&ec->dev->kobj, &wilco_dev_attr_group);
  201. }
  202. void wilco_ec_remove_sysfs(struct wilco_ec_device *ec)
  203. {
  204. sysfs_remove_group(&ec->dev->kobj, &wilco_dev_attr_group);
  205. }