sysfs_power.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. static int wait_ibf(void)
  12. {
  13. int i = 0;
  14. while (inb(EC_CMD_PORT) & EC_IBF)
  15. {
  16. if (++i > TIMEOUT_LOOPS)
  17. {
  18. return -1;
  19. }
  20. udelay(1);
  21. }
  22. return 0;
  23. }
  24. static int wait_obf(void)
  25. {
  26. int i = 0;
  27. while (!(inb(EC_CMD_PORT) & EC_OBF))
  28. {
  29. if (++i > TIMEOUT_LOOPS)
  30. {
  31. return -1;
  32. }
  33. udelay(1);
  34. }
  35. return 0;
  36. }
  37. static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
  38. {
  39. unsigned char WEC, REC;
  40. switch (page)
  41. {
  42. case 0:
  43. {
  44. WEC = 0x96;
  45. REC = 0x95;
  46. break;
  47. }
  48. case 1:
  49. {
  50. WEC = 0x98;
  51. REC = 0x97;
  52. break;
  53. }
  54. default:
  55. {
  56. WEC = 0x81;
  57. REC = 0x80;
  58. break;
  59. }
  60. }
  61. if (wait_ibf() < 0)
  62. return -1;
  63. outb(REC, EC_CMD_PORT);
  64. if (wait_ibf() < 0)
  65. return -1;
  66. outb(offset, EC_DATA_PORT);
  67. if (wait_obf() < 0)
  68. return -1;
  69. *data = inb(EC_DATA_PORT);
  70. return 0;
  71. }
  72. static ssize_t ac_power_show(struct kobject *kobj, struct kobj_attribute *attr,
  73. char *buf)
  74. {
  75. int ac_power_flag = 0;
  76. uint8_t val = 0x00;
  77. if (oem_ec_read_ram(2, 0x36, &val) < 0)
  78. return -1;
  79. ac_power_flag = (val & 0x03) ? 1 : 0;
  80. return sprintf(buf, "%d\n", ac_power_flag);
  81. }
  82. static ssize_t ac_power_store(struct kobject *kobj, struct kobj_attribute *attr,
  83. const char *buf, size_t count)
  84. {
  85. return -EINVAL;
  86. }
  87. static struct kobj_attribute ac_power =
  88. __ATTR(ac_power, 0644, ac_power_show, ac_power_store);
  89. static struct attribute *sysfs_power_attrs[] = {
  90. &ac_power.attr,
  91. NULL,
  92. };
  93. static struct attribute_group sysfs_power_attr_group = {
  94. .attrs = sysfs_power_attrs,
  95. };
  96. int sysfs_power_init(void)
  97. {
  98. int ret = 0;
  99. ret = sysfs_create_group(hwmon_kobj, &sysfs_power_attr_group);
  100. return ret;
  101. }
  102. void sysfs_power_exit(void)
  103. {
  104. sysfs_remove_group(hwmon_kobj, &sysfs_power_attr_group);
  105. }