sysfs_power.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/kobject.h>
  5. #include <linux/sysfs.h>
  6. #include <linux/acpi.h>
  7. #include <linux/io.h>
  8. #include <linux/delay.h>
  9. #include "gpioregs.h"
  10. extern struct kobject *hwmon_kobj;
  11. int sysfs_power_off_flag = 0;
  12. static int wait_ibf(void)
  13. {
  14. int i = 0;
  15. while (inb(EC_CMD_PORT) & EC_IBF)
  16. {
  17. if (++i > TIMEOUT_LOOPS)
  18. {
  19. return -1;
  20. }
  21. udelay(1);
  22. }
  23. return 0;
  24. }
  25. static int wait_obf(void)
  26. {
  27. int i = 0;
  28. while (!(inb(EC_CMD_PORT) & EC_OBF))
  29. {
  30. if (++i > TIMEOUT_LOOPS)
  31. {
  32. return -1;
  33. }
  34. udelay(1);
  35. }
  36. return 0;
  37. }
  38. static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
  39. {
  40. unsigned char WEC, REC;
  41. switch (page)
  42. {
  43. case 0:
  44. {
  45. WEC = 0x96;
  46. REC = 0x95;
  47. break;
  48. }
  49. case 1:
  50. {
  51. WEC = 0x98;
  52. REC = 0x97;
  53. break;
  54. }
  55. default:
  56. {
  57. WEC = 0x81;
  58. REC = 0x80;
  59. break;
  60. }
  61. }
  62. if (wait_ibf() < 0)
  63. return -1;
  64. outb(REC, EC_CMD_PORT);
  65. if (wait_ibf() < 0)
  66. return -1;
  67. outb(offset, EC_DATA_PORT);
  68. if (wait_obf() < 0)
  69. return -1;
  70. *data = inb(EC_DATA_PORT);
  71. return 0;
  72. }
  73. static ssize_t ac_power_show(struct kobject *kobj, struct kobj_attribute *attr,
  74. char *buf)
  75. {
  76. int ac_power_flag = 0;
  77. uint8_t val = 0x00;
  78. if (oem_ec_read_ram(2, 0x36, &val) < 0)
  79. return -1;
  80. ac_power_flag = (val & 0x03) ? 1 : 0;
  81. if(sysfs_power_off_flag == 1)
  82. {
  83. ac_power_flag = 0;
  84. }
  85. return sprintf(buf, "%d\n", ac_power_flag);
  86. }
  87. static ssize_t ac_power_store(struct kobject *kobj, struct kobj_attribute *attr,
  88. const char *buf, size_t count)
  89. {
  90. u32 val;
  91. int ret;
  92. ret = kstrtou32(buf, 10, &val);
  93. if (ret < 0)
  94. {
  95. pr_err("ac_power_store format error\n");
  96. return ret;
  97. }
  98. if(val != 0)
  99. {
  100. pr_err("ac_power_store only support 0\n");
  101. return -EINVAL;
  102. }
  103. if(val == 0)
  104. {
  105. sysfs_power_off_flag = 1;
  106. }
  107. return count;
  108. }
  109. static struct kobj_attribute ac_power =
  110. __ATTR(ac_power, 0644, ac_power_show, ac_power_store);
  111. static struct attribute *sysfs_power_attrs[] = {
  112. &ac_power.attr,
  113. NULL,
  114. };
  115. static struct attribute_group sysfs_power_attr_group = {
  116. .attrs = sysfs_power_attrs,
  117. };
  118. int sysfs_power_init(void)
  119. {
  120. int ret = 0;
  121. ret = sysfs_create_group(hwmon_kobj, &sysfs_power_attr_group);
  122. return ret;
  123. }
  124. void sysfs_power_exit(void)
  125. {
  126. sysfs_remove_group(hwmon_kobj, &sysfs_power_attr_group);
  127. }