led_ctl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/io.h>
  5. #include <time.h>
  6. #include <errno.h>
  7. #include "led_ctl.h"
  8. #define EC_CMD_PORT 0x66
  9. #define EC_DATA_PORT 0x62
  10. #define EC_OBF 0x01 // Output Buffer Full
  11. #define EC_IBF 0x02 // Input Buffer Full
  12. #define CMD_READ_RAM 0x97
  13. #define CMD_WRITE_RAM 0x98
  14. #define OFFSET_COLOR_CTL 0x01
  15. #define OFFSET_BLINK_TIME 0x02
  16. #define OFFSET_BLINK_CTL 0x03
  17. #define OFFSET_TURNOFF_CTL 0x04
  18. #define BIT0 (1 << 0)
  19. #define TIMEOUT_LOOPS 100000
  20. static int wait_ibf(void)
  21. {
  22. int i = 0;
  23. while (inb(EC_CMD_PORT) & EC_IBF)
  24. {
  25. if (++i > TIMEOUT_LOOPS)
  26. {
  27. fprintf(stderr, "Error: EC IBF Timeout!\n");
  28. return -1;
  29. }
  30. usleep(1);
  31. }
  32. return 0;
  33. }
  34. static int wait_obf(void)
  35. {
  36. int i = 0;
  37. while (!(inb(EC_CMD_PORT) & EC_OBF))
  38. {
  39. if (++i > TIMEOUT_LOOPS)
  40. {
  41. fprintf(stderr, "Error: EC OBF Timeout!\n");
  42. return -1;
  43. }
  44. usleep(1);
  45. }
  46. return 0;
  47. }
  48. static int ec_read_ram(uint8_t offset, uint8_t *data)
  49. {
  50. if (wait_ibf() < 0)
  51. return -1;
  52. outb(CMD_READ_RAM, EC_CMD_PORT);
  53. if (wait_ibf() < 0)
  54. return -1;
  55. outb(offset, EC_DATA_PORT);
  56. if (wait_obf() < 0)
  57. return -1;
  58. *data = inb(EC_DATA_PORT);
  59. return 0;
  60. }
  61. static int ec_write_ram(uint8_t offset, uint8_t data)
  62. {
  63. if (wait_ibf() < 0)
  64. return -1;
  65. outb(CMD_WRITE_RAM, EC_CMD_PORT);
  66. if (wait_ibf() < 0)
  67. return -1;
  68. outb(offset, EC_DATA_PORT);
  69. if (wait_ibf() < 0)
  70. return -1;
  71. outb(data, EC_DATA_PORT);
  72. return 0;
  73. }
  74. static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
  75. {
  76. unsigned char WEC, REC;
  77. switch(page)
  78. {
  79. case 0:
  80. {
  81. WEC = 0x96;
  82. REC = 0x95;
  83. break;
  84. }
  85. case 1:
  86. {
  87. WEC = 0x98;
  88. REC = 0x97;
  89. break;
  90. }
  91. default:
  92. {
  93. WEC = 0x81;
  94. REC = 0x80;
  95. break;
  96. }
  97. }
  98. if (wait_ibf() < 0)
  99. return -1;
  100. outb(REC, EC_CMD_PORT);
  101. if (wait_ibf() < 0)
  102. return -1;
  103. outb(offset, EC_DATA_PORT);
  104. if (wait_obf() < 0)
  105. return -1;
  106. *data = inb(EC_DATA_PORT);
  107. return 0;
  108. }
  109. static int oem_ec_write_ram(uint8_t page, uint8_t offset, uint8_t data)
  110. {
  111. unsigned char WEC, REC;
  112. switch(page)
  113. {
  114. case 0:
  115. {
  116. WEC = 0x96;
  117. REC = 0x95;
  118. break;
  119. }
  120. case 1:
  121. {
  122. WEC = 0x98;
  123. REC = 0x97;
  124. break;
  125. }
  126. default:
  127. {
  128. WEC = 0x81;
  129. REC = 0x80;
  130. break;
  131. }
  132. }
  133. if (wait_ibf() < 0)
  134. return -1;
  135. outb(WEC, EC_CMD_PORT);
  136. if (wait_ibf() < 0)
  137. return -1;
  138. outb(offset, EC_DATA_PORT);
  139. if (wait_ibf() < 0)
  140. return -1;
  141. outb(data, EC_DATA_PORT);
  142. return 0;
  143. }
  144. // ---------API functions---------
  145. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>heart led function begin>>>>>>>>>>>>>>>>>>>>>>>>>>
  146. void heart_led_turn_off(void)
  147. {
  148. oem_ec_write_ram(1, OFFSET_TURNOFF_CTL, 0x01);
  149. }
  150. void heart_led_turn_on(void)
  151. {
  152. oem_ec_write_ram(1, OFFSET_TURNOFF_CTL, 0x00);
  153. }
  154. int led_driver_init(void)
  155. {
  156. if (ioperm(EC_DATA_PORT, 1, 1))
  157. {
  158. perror("Failed to get permission for 0x62");
  159. return -1;
  160. }
  161. if (ioperm(EC_CMD_PORT, 1, 1))
  162. {
  163. perror("Failed to get permission for 0x66");
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. int led_set_color(led_color_t color)
  169. {
  170. uint8_t val;
  171. if (ec_read_ram(OFFSET_COLOR_CTL, &val) < 0)
  172. return -1;
  173. if (color == LED_RED)
  174. val |= BIT0;
  175. else
  176. val &= ~BIT0;
  177. heart_led_turn_on();
  178. return ec_write_ram(OFFSET_COLOR_CTL, val);
  179. }
  180. int led_set_blink(int enable, uint8_t interval_unit)
  181. {
  182. uint8_t ctl;
  183. if (enable)
  184. {
  185. if (ec_write_ram(OFFSET_BLINK_TIME, interval_unit) < 0)
  186. return -1;
  187. }
  188. if (ec_read_ram(OFFSET_BLINK_CTL, &ctl) < 0)
  189. return -1;
  190. if (enable)
  191. ctl |= BIT0;
  192. else
  193. ctl &= ~BIT0;
  194. heart_led_turn_on();
  195. return ec_write_ram(OFFSET_BLINK_CTL, ctl);
  196. }
  197. int led_print_status(void)
  198. {
  199. uint8_t color_reg, blink_time, blink_ctl;
  200. if (ec_read_ram(OFFSET_COLOR_CTL, &color_reg) < 0)
  201. return -1;
  202. if (ec_read_ram(OFFSET_BLINK_TIME, &blink_time) < 0)
  203. return -1;
  204. if (ec_read_ram(OFFSET_BLINK_CTL, &blink_ctl) < 0)
  205. return -1;
  206. printf(" Color : %s\n", (color_reg & BIT0) ? "RED" : "GREEN");
  207. printf(" Blink : %s\n", (blink_ctl & BIT0) ? "ON" : "OFF");
  208. if (blink_ctl & BIT0)
  209. {
  210. printf(" Interval : %d ms (%d units)\n", blink_time * 100, blink_time);
  211. }
  212. return 0;
  213. }
  214. //<<<<<<<<<<<<<<<<<<<<<<<<<<<heart led function end<<<<<<<<<<<<<<<<<<<<<<<<<<
  215. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bat_led function begin>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  216. void bat_led_ctrl_by_sys(void)
  217. {
  218. oem_ec_write_ram(2, 0x21, 0x3C); //enable bat led control by system
  219. }
  220. void bat_led_ctrl_by_ec(void)
  221. {
  222. oem_ec_write_ram(2, 0x21, 0x00); //enable bat led control by ec
  223. }
  224. int bat_led_set_color(uint8_t state)
  225. {
  226. uint8_t val;
  227. /*
  228. val is led state: off; red; green
  229. val bit meaning:
  230. bit 2-3: health led
  231. bit 4-5: charge led
  232. */
  233. switch(state)
  234. {
  235. case 0:
  236. val = 0x10; // charge led green
  237. break;
  238. case 1:
  239. val = 0x20; // charge led red
  240. break;
  241. case 2:
  242. val = 0x04;// health led green
  243. break;
  244. case 3:
  245. val = 0x08;// health led red
  246. break;
  247. case 4:
  248. val = 0x00;//turn off both led
  249. break;
  250. default:
  251. return 0;
  252. }
  253. oem_ec_write_ram(2, 0x31, val);
  254. bat_led_ctrl_by_sys();
  255. return 1;
  256. }
  257. //<<<<<<<<<<<<<<<<<<<<<<<<<<<bat_led function end<<<<<<<<<<<<<<<<<<<<<<<<<<
  258. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bat infor function begin>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  259. void get_bat_info(void)
  260. {
  261. uint8_t BAT1_RelativeStateOfChgL = 0;
  262. uint8_t BAT1_HEALTH = 0;
  263. oem_ec_read_ram(2, 0x93, &BAT1_RelativeStateOfChgL);
  264. oem_ec_read_ram(2, 0x9D, &BAT1_HEALTH);
  265. printf(" BAT1_RelativeStateOfChgL is : %d\n", BAT1_RelativeStateOfChgL);
  266. printf(" BAT1_HEALTH is : %d\n", BAT1_HEALTH);
  267. }
  268. //<<<<<<<<<<<<<<<<<<<<<<<<<<<bat infor function end<<<<<<<<<<<<<<<<<<<<<<<<<<
  269. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>AC & power function begin>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  270. uint8_t soft_rest_btn(void)//read GPIO of btn SW_HRST1
  271. {
  272. uint8_t val = 0x00;
  273. if (oem_ec_read_ram(2, 0x34, &val) < 0)
  274. return 0;
  275. printf(" btn state is : %d\n", (val & 0x01) ? 1 : 0);
  276. return (val & 0x01) ? 1 : 0;
  277. }
  278. uint8_t ac_present(void) //read GPIO of DC IN
  279. {
  280. uint8_t val = 0x00;
  281. if (oem_ec_read_ram(2, 0x36, &val) < 0)
  282. return 0;
  283. printf(" AC state is : %d\n", (val & 0x03) ? 1 : 0);
  284. return (val & 0x08) ? 1 : 0;
  285. }
  286. void ac_bat_state(void) //read EC state
  287. {
  288. /*
  289. meaning of state bit
  290. #define F_BAT1_DISCHRGE BIT(7)
  291. #define F_BAT1_CHARGING BIT(6)
  292. #define F_BAT1_PRESENT BIT(1)
  293. #define F_AC_ON_LINE BIT(0)
  294. */
  295. uint8_t state = 0x00;
  296. oem_ec_read_ram(2, 0x80, &state);
  297. if((state & 0x03) == 0x01)
  298. {
  299. printf(" DC is in, bat out\n");
  300. }
  301. else if((state & 0x03) == 0x02)
  302. {
  303. printf(" DC is out, bat in\n");
  304. }
  305. else if((state & 0x03) == 0x03)
  306. {
  307. printf(" DC is in, bat in\n");
  308. }
  309. else if((state & 0xC2) == 0x82)
  310. {
  311. printf(" BAT is present and discharge\n");
  312. }
  313. else if((state & 0xC2) == 0x42)
  314. {
  315. printf(" BAT is present and charging\n");
  316. }
  317. }
  318. //<<<<<<<<<<<<<<<<<<<<<<<<<<AC & power function end<<<<<<<<<<<<<<<<<<<<<<<<<<
  319. void get_ec_version(void)
  320. {
  321. uint8_t val[4] = {0};
  322. uint8_t i = 0 ;
  323. for(i = 0; i < 4; i++ )
  324. {
  325. oem_ec_read_ram(2, i, &val[i]);
  326. }
  327. printf(" EC version is : %02x%02x%02x%02x\n", val[0], val[1], val[2], val[3]);
  328. }