voltage_sysfs.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. /* Helper functions for IO access */
  12. static uint8_t hwm_read_reg(uint16_t hwm_base, uint8_t reg)
  13. {
  14. outb(reg, hwm_base + HWM_INDEX_OFFSET);
  15. return inb(hwm_base + HWM_DATA_OFFSET);
  16. }
  17. #if 0
  18. static void hwm_write_reg(uint16_t hwm_base, uint8_t reg, uint8_t val)
  19. {
  20. outb(reg, hwm_base + HWM_INDEX_OFFSET);
  21. outb(val, hwm_base + HWM_DATA_OFFSET);
  22. }
  23. #endif
  24. static uint8_t hwm_read_reg_retry(uint16_t hwm_base, uint8_t reg)
  25. {
  26. uint8_t v = hwm_read_reg(hwm_base, reg);
  27. if (v == 0xFF) {
  28. v = hwm_read_reg(hwm_base, reg);
  29. }
  30. return v;
  31. }
  32. static int vin_raw_to_volt(uint8_t raw, int r_top_kohm, int r_bottom_kohm)
  33. {
  34. int vm = raw * 11;
  35. if (r_top_kohm > 0 && r_bottom_kohm > 0) {
  36. vm = (vm * (r_top_kohm + r_bottom_kohm)) / r_bottom_kohm;
  37. }
  38. return vm;
  39. }
  40. static int ec_raw_to_volt(uint8_t raw, int r_top_kohm, int r_bottom_kohm)
  41. {
  42. int vm = raw * 12; /* EC unit: 10mV */
  43. vm = (vm * (r_top_kohm + r_bottom_kohm)) / r_bottom_kohm;
  44. return vm ;
  45. }
  46. #if 0
  47. static int ec_wait_ibf(void)
  48. {
  49. int i = 0;
  50. while (inb(EC_CMD_PORT) & EC_IBF) {
  51. if (++i > TIMEOUT_LOOPS) {
  52. printk("Error: EC IBF Timeout!\n");
  53. return -1;
  54. }
  55. udelay(1);
  56. }
  57. return 0;
  58. }
  59. static int ec_wait_obf(void)
  60. {
  61. int i = 0;
  62. while (!(inb(EC_CMD_PORT) & EC_OBF)) {
  63. if (++i > TIMEOUT_LOOPS) {
  64. printk("Error: EC OBF Timeout!\n");
  65. return -1;
  66. }
  67. udelay(1);
  68. }
  69. return 0;
  70. }
  71. #endif
  72. static int wait_ibf(void)
  73. {
  74. int i = 0;
  75. while (inb(EC_CMD_PORT) & EC_IBF)
  76. {
  77. if (++i > TIMEOUT_LOOPS)
  78. {
  79. return -1;
  80. }
  81. udelay(1);
  82. }
  83. return 0;
  84. }
  85. static int wait_obf(void)
  86. {
  87. int i = 0;
  88. while (!(inb(EC_CMD_PORT) & EC_OBF))
  89. {
  90. if (++i > TIMEOUT_LOOPS)
  91. {
  92. return -1;
  93. }
  94. udelay(1);
  95. }
  96. return 0;
  97. }
  98. #if 0
  99. static int ec_read_ram(uint8_t offset, uint8_t *data)
  100. {
  101. if (wait_ibf() < 0)
  102. return -1;
  103. outb(CMD_READ_RAM, EC_CMD_PORT);
  104. if (wait_ibf() < 0)
  105. return -1;
  106. outb(offset, EC_DATA_PORT);
  107. if (wait_obf() < 0)
  108. return -1;
  109. *data = inb(EC_DATA_PORT);
  110. return 0;
  111. }
  112. static int ec_write_ram(uint8_t offset, uint8_t data)
  113. {
  114. if (wait_ibf() < 0)
  115. return -1;
  116. outb(CMD_WRITE_RAM, EC_CMD_PORT);
  117. if (wait_ibf() < 0)
  118. return -1;
  119. outb(offset, EC_DATA_PORT);
  120. if (wait_ibf() < 0)
  121. return -1;
  122. outb(data, EC_DATA_PORT);
  123. return 0;
  124. }
  125. #endif
  126. static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
  127. {
  128. unsigned char WEC, REC;
  129. switch(page)
  130. {
  131. case 0:
  132. {
  133. WEC = 0x96;
  134. REC = 0x95;
  135. break;
  136. }
  137. case 1:
  138. {
  139. WEC = 0x98;
  140. REC = 0x97;
  141. break;
  142. }
  143. default:
  144. {
  145. WEC = 0x81;
  146. REC = 0x80;
  147. break;
  148. }
  149. }
  150. if (wait_ibf() < 0)
  151. return -1;
  152. outb(REC, EC_CMD_PORT);
  153. if (wait_ibf() < 0)
  154. return -1;
  155. outb(offset, EC_DATA_PORT);
  156. if (wait_obf() < 0)
  157. return -1;
  158. *data = inb(EC_DATA_PORT);
  159. return 0;
  160. }
  161. #if 0
  162. static int oem_ec_write_ram(uint8_t page, uint8_t offset, uint8_t data)
  163. {
  164. unsigned char WEC, REC;
  165. switch(page)
  166. {
  167. case 0:
  168. {
  169. WEC = 0x96;
  170. REC = 0x95;
  171. break;
  172. }
  173. case 1:
  174. {
  175. WEC = 0x98;
  176. REC = 0x97;
  177. break;
  178. }
  179. default:
  180. {
  181. WEC = 0x81;
  182. REC = 0x80;
  183. break;
  184. }
  185. }
  186. if (wait_ibf() < 0)
  187. return -1;
  188. outb(WEC, EC_CMD_PORT);
  189. if (wait_ibf() < 0)
  190. return -1;
  191. outb(offset, EC_DATA_PORT);
  192. if (wait_ibf() < 0)
  193. return -1;
  194. outb(data, EC_DATA_PORT);
  195. return 0;
  196. }
  197. #endif
  198. static ssize_t voltage_5v_show(struct kobject *kobj, struct kobj_attribute *attr,
  199. char *buf)
  200. {
  201. int vlotage = 0;
  202. uint8_t raw;
  203. raw = hwm_read_reg_retry(IT8786_HWM_BASE_DEFAULT, 0x22);
  204. vlotage = vin_raw_to_volt(raw, 3, 2);
  205. return sprintf(buf, "%d\n", vlotage);
  206. }
  207. static ssize_t voltage_5v_store(struct kobject *kobj, struct kobj_attribute *attr,
  208. const char *buf, size_t count)
  209. {
  210. return -EINVAL;
  211. }
  212. static ssize_t voltage_vcore_show(struct kobject *kobj, struct kobj_attribute *attr,
  213. char *buf)
  214. {
  215. int vlotage = 0;
  216. uint8_t raw;
  217. raw = hwm_read_reg_retry(IT8786_HWM_BASE_DEFAULT, 0x20);
  218. vlotage = vin_raw_to_volt(raw, 0, 1);
  219. return sprintf(buf, "%d\n", vlotage);
  220. }
  221. static ssize_t voltage_vcore_store(struct kobject *kobj, struct kobj_attribute *attr,
  222. const char *buf, size_t count)
  223. {
  224. return -EINVAL;
  225. }
  226. static ssize_t voltage_12v_show(struct kobject *kobj, struct kobj_attribute *attr,
  227. char *buf)
  228. {
  229. int vlotage = 0;
  230. uint8_t raw;
  231. if(oem_ec_read_ram(0x00, 0x6a, &raw) < 0)
  232. {
  233. return -1;
  234. }
  235. // raw = hwm_read_reg_retry(IT8786_HWM_BASE_DEFAULT, 0x6a);
  236. vlotage = ec_raw_to_volt(raw, 10, 2);
  237. return sprintf(buf, "%d\n", vlotage);
  238. }
  239. static ssize_t voltage_12v_store(struct kobject *kobj, struct kobj_attribute *attr,
  240. const char *buf, size_t count)
  241. {
  242. return -EINVAL;
  243. }
  244. static struct kobj_attribute voltage_5v_attr =
  245. __ATTR(voltage_5v, 0444, voltage_5v_show, voltage_5v_store);
  246. static struct kobj_attribute voltage_vcorev_attr =
  247. __ATTR(voltage_vcore, 0444, voltage_vcore_show, voltage_vcore_store);
  248. static struct kobj_attribute voltage_12v_attr =
  249. __ATTR(voltage_12v, 0444, voltage_12v_show, voltage_12v_store);
  250. static struct attribute *voltage_sysfs_attrs[] = {
  251. &voltage_5v_attr.attr,
  252. &voltage_vcorev_attr.attr,
  253. &voltage_12v_attr.attr,
  254. NULL,
  255. };
  256. static struct attribute_group voltage_sysfs_attr_group = {
  257. .attrs = voltage_sysfs_attrs,
  258. };
  259. int voltage_sysfs_init(void)
  260. {
  261. int ret = 0;
  262. ret = sysfs_create_group(hwmon_kobj, &voltage_sysfs_attr_group);
  263. return ret;
  264. }
  265. void voltage_sysfs_exit(void)
  266. {
  267. sysfs_remove_group(hwmon_kobj, &voltage_sysfs_attr_group);
  268. }