batteryled.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/device.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/slab.h>
  9. #include <linux/pci.h>
  10. #include <linux/i2c.h>
  11. #include <linux/acpi.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/wait.h>
  14. #include <linux/sched.h>
  15. #include <linux/poll.h>
  16. #include <linux/mutex.h>
  17. #include <linux/delay.h>
  18. #include <linux/sched.h>
  19. #include <linux/timer.h>
  20. #include <linux/workqueue.h>
  21. #include "gpioregs.h"
  22. #include "smart_battery.h"
  23. // blue is charge
  24. // white is health
  25. // Because our LED does not have blue and white. Only red and green. So use red to indicate charging, green to indicate healthy.
  26. enum battery_led_color
  27. {
  28. BATTERY_LED_OFF = 0x00,
  29. BATTERY_LED_WHITE = 0x01,
  30. BATTERY_LED_BLUE = 0x02,
  31. BATTERY_LED_WHITE_BLINK = 0x03,
  32. BATTERY_LED_BLUE_BLINK = 0x04
  33. };
  34. extern struct kobject *vfiec_kobj;
  35. static struct kobject *batteryled_kobj;
  36. static unsigned int led_charge_val = 0;
  37. static unsigned int led_health_val = 0;
  38. static struct delayed_work delay_work1;
  39. static struct delayed_work delay_work2;
  40. static int wait_ibf(void)
  41. {
  42. int i = 0;
  43. while (inb(EC_CMD_PORT) & EC_IBF)
  44. {
  45. if (++i > TIMEOUT_LOOPS)
  46. {
  47. return -1;
  48. }
  49. udelay(1);
  50. }
  51. return 0;
  52. }
  53. static int wait_obf(void)
  54. {
  55. int i = 0;
  56. while (!(inb(EC_CMD_PORT) & EC_OBF))
  57. {
  58. if (++i > TIMEOUT_LOOPS)
  59. {
  60. return -1;
  61. }
  62. udelay(1);
  63. }
  64. return 0;
  65. }
  66. #if 0
  67. static int ec_read_ram(uint8_t offset, uint8_t *data)
  68. {
  69. if (wait_ibf() < 0)
  70. return -1;
  71. outb(CMD_READ_RAM, EC_CMD_PORT);
  72. if (wait_ibf() < 0)
  73. return -1;
  74. outb(offset, EC_DATA_PORT);
  75. if (wait_obf() < 0)
  76. return -1;
  77. *data = inb(EC_DATA_PORT);
  78. return 0;
  79. }
  80. static int ec_write_ram(uint8_t offset, uint8_t data)
  81. {
  82. if (wait_ibf() < 0)
  83. return -1;
  84. outb(CMD_WRITE_RAM, EC_CMD_PORT);
  85. if (wait_ibf() < 0)
  86. return -1;
  87. outb(offset, EC_DATA_PORT);
  88. if (wait_ibf() < 0)
  89. return -1;
  90. outb(data, EC_DATA_PORT);
  91. return 0;
  92. }
  93. #endif
  94. static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
  95. {
  96. unsigned char WEC, REC;
  97. switch(page)
  98. {
  99. case 0:
  100. {
  101. WEC = 0x96;
  102. REC = 0x95;
  103. break;
  104. }
  105. case 1:
  106. {
  107. WEC = 0x98;
  108. REC = 0x97;
  109. break;
  110. }
  111. default:
  112. {
  113. WEC = 0x81;
  114. REC = 0x80;
  115. break;
  116. }
  117. }
  118. if (wait_ibf() < 0)
  119. return -1;
  120. outb(REC, EC_CMD_PORT);
  121. if (wait_ibf() < 0)
  122. return -1;
  123. outb(offset, EC_DATA_PORT);
  124. if (wait_obf() < 0)
  125. return -1;
  126. *data = inb(EC_DATA_PORT);
  127. return 0;
  128. }
  129. static int oem_ec_write_ram(uint8_t page, uint8_t offset, uint8_t data)
  130. {
  131. unsigned char WEC, REC;
  132. switch(page)
  133. {
  134. case 0:
  135. {
  136. WEC = 0x96;
  137. REC = 0x95;
  138. break;
  139. }
  140. case 1:
  141. {
  142. WEC = 0x98;
  143. REC = 0x97;
  144. break;
  145. }
  146. default:
  147. {
  148. WEC = 0x81;
  149. REC = 0x80;
  150. break;
  151. }
  152. }
  153. if (wait_ibf() < 0)
  154. return -1;
  155. outb(WEC, EC_CMD_PORT);
  156. if (wait_ibf() < 0)
  157. return -1;
  158. outb(offset, EC_DATA_PORT);
  159. if (wait_ibf() < 0)
  160. return -1;
  161. outb(data, EC_DATA_PORT);
  162. return 0;
  163. }
  164. /* ==================== max_fade_brightness ==================== */
  165. static ssize_t led_health_show(struct kobject *kobj,
  166. struct kobj_attribute *attr,
  167. char *buf)
  168. {
  169. uint8_t data = 0;
  170. int ret = 0;
  171. ret = oem_ec_read_ram(2, 0x31, &data);
  172. return sprintf(buf, "%02x\n", led_health_val);
  173. }
  174. static ssize_t led_health_store(struct kobject *kobj,
  175. struct kobj_attribute *attr,
  176. const char *buf, size_t count)
  177. {
  178. u32 val;
  179. int ret;
  180. printk("%s: %s\n", __func__, buf);
  181. ret = kstrtou32(buf, 10, &val);
  182. if (ret < 0)
  183. return ret;
  184. if(val == 0x0 || val == 0x1 || val == 0x2 || val == 0x3 || val == 0x4)
  185. {
  186. led_health_val = val;
  187. }
  188. else
  189. {
  190. return -EINVAL;
  191. }
  192. schedule_delayed_work(&delay_work1, msecs_to_jiffies(10));
  193. return count;
  194. }
  195. static struct kobj_attribute led_health =
  196. __ATTR(led_health, 0644, led_health_show, led_health_store);
  197. /* ==================== mode ==================== */
  198. static ssize_t led_charge_show(struct kobject *kobj, struct kobj_attribute *attr,
  199. char *buf)
  200. {
  201. uint8_t data = 0;
  202. int ret = 0;
  203. ret = oem_ec_read_ram(2, 0x31, &data);
  204. return sprintf(buf, "%02x\n", led_charge_val);
  205. }
  206. static ssize_t led_charge_store(struct kobject *kobj, struct kobj_attribute *attr,
  207. const char *buf, size_t count)
  208. {
  209. u32 val;
  210. int ret;
  211. printk("%s: %s\n", __func__, buf);
  212. ret = kstrtou32(buf, 10, &val);
  213. if (ret < 0)
  214. return ret;
  215. if(val == 0x0 || val == 0x1 || val == 0x2 || val == 0x3 || val == 0x4)
  216. {
  217. led_charge_val = val;
  218. }
  219. else
  220. {
  221. return -EINVAL;
  222. }
  223. schedule_delayed_work(&delay_work1, msecs_to_jiffies(10));
  224. return count;
  225. }
  226. static struct kobj_attribute led_charge =
  227. __ATTR(led_charge, 0644, led_charge_show, led_charge_store);
  228. static struct attribute *batteryled_attrs[] = {
  229. &led_charge.attr,
  230. &led_health.attr,
  231. NULL,
  232. };
  233. static struct attribute_group batteryled_attr_group = {
  234. .attrs = batteryled_attrs,
  235. };
  236. static void delay_work_func2(struct work_struct *work)
  237. {
  238. struct smart_battery_info info;
  239. int ret = 0;
  240. ret = get_battery_info(&info);
  241. if(ret == 0)
  242. {
  243. if(info.capacity >= 50)
  244. {
  245. led_health_val = 0x2;
  246. }
  247. else if(info.capacity < 50 && info.capacity >= 20)
  248. {
  249. led_health_val = 0x1;
  250. }
  251. else
  252. {
  253. led_health_val = 0x3;
  254. }
  255. if(info.status == 0x00)
  256. {
  257. led_charge_val = 0x0;
  258. led_health_val = 0x0;
  259. }
  260. else if(info.status == 0x01)
  261. {
  262. led_charge_val = 0x4;
  263. }
  264. else if(info.status == 0x02)
  265. {
  266. led_charge_val = 0x2;
  267. }
  268. }
  269. schedule_delayed_work(&delay_work2, msecs_to_jiffies(5000));
  270. }
  271. static void delay_work_func(struct work_struct *work)
  272. {
  273. static uint8_t blink_flag = 0;
  274. uint8_t data = 0;
  275. // val is led state: off; red; green
  276. // val bit meaning:
  277. // bit 2-3: health led bit2-green, bit3-red blude=green white=red
  278. // bit 4-5: charge led bit4-green, bit5-red
  279. if(blink_flag == 0)
  280. {
  281. //Turn off when flickering
  282. blink_flag = 1;
  283. if(led_health_val == 0x0)
  284. {
  285. data = data & 0xf3;
  286. }
  287. else if(led_health_val == 0x1)
  288. {
  289. data = data | 0x8;
  290. }
  291. else if(led_health_val == 0x2)
  292. {
  293. data = data | 0x4;
  294. }
  295. else if(led_health_val == 0x3)
  296. {
  297. data = data & 0xf3;
  298. }
  299. else if(led_health_val == 0x4)
  300. {
  301. data = data & 0xf3;
  302. }
  303. if(led_charge_val == 0x0)
  304. {
  305. data = data & 0xcf;
  306. }
  307. else if(led_charge_val == 0x1)
  308. {
  309. data = data | 0x20;
  310. }
  311. else if(led_charge_val == 0x2)
  312. {
  313. data = data | 0x10;
  314. }
  315. else if(led_charge_val == 0x3)
  316. {
  317. data = data & 0xcf;
  318. }
  319. else if(led_charge_val == 0x4)
  320. {
  321. data = data & 0xcf;
  322. }
  323. }
  324. else if(blink_flag == 1)
  325. {
  326. // Bright when flashing
  327. blink_flag = 0;
  328. if(led_health_val == 0x0)
  329. {
  330. data = data & 0xf3;
  331. }
  332. else if(led_health_val == 0x1)
  333. {
  334. data = data | 0x08;
  335. }
  336. else if(led_health_val == 0x2)
  337. {
  338. data = data | 0x04;
  339. }
  340. else if(led_health_val == 0x3)
  341. {
  342. data = data | 0x08;
  343. }
  344. else if(led_health_val == 0x4)
  345. {
  346. data = data | 0x04;
  347. }
  348. if(led_charge_val == 0x0)
  349. {
  350. data = data & 0xcf;
  351. }
  352. else if(led_charge_val == 0x1)
  353. {
  354. data = data | 0x20;
  355. }
  356. else if(led_charge_val == 0x2)
  357. {
  358. data = data | 0x10;
  359. }
  360. else if(led_charge_val == 0x3)
  361. {
  362. data = data | 0x20;
  363. }
  364. else if(led_charge_val == 0x4)
  365. {
  366. data = data | 0x10;
  367. }
  368. }
  369. oem_ec_write_ram(2, 0x31, data);
  370. oem_ec_write_ram(2, 0x21, 0x3C);
  371. schedule_delayed_work(&delay_work1, msecs_to_jiffies(500));
  372. }
  373. int batteryled_init(void)
  374. {
  375. int ret;
  376. INIT_DELAYED_WORK(&delay_work1, delay_work_func);
  377. INIT_DELAYED_WORK(&delay_work2, delay_work_func2);
  378. /* Create /sys/kernel/vfiec/batteryled */
  379. batteryled_kobj = kobject_create_and_add("batteryled", vfiec_kobj);
  380. if (!batteryled_kobj)
  381. {
  382. ret = -ENOMEM;
  383. return ret;
  384. }
  385. /* Create property file */
  386. ret = sysfs_create_group(batteryled_kobj, &batteryled_attr_group);
  387. if (ret)
  388. {
  389. pr_err("Failed to create sysfs group: %d\n", ret);
  390. goto free_batteryled_kobj;
  391. }
  392. schedule_delayed_work(&delay_work1, msecs_to_jiffies(100));
  393. schedule_delayed_work(&delay_work2, msecs_to_jiffies(100));
  394. return 0;
  395. free_batteryled_kobj:
  396. kobject_put(batteryled_kobj);
  397. return ret;
  398. }
  399. void batteryled_exit(void)
  400. {
  401. cancel_delayed_work_sync(&delay_work1);
  402. cancel_delayed_work_sync(&delay_work2);
  403. sysfs_remove_group(batteryled_kobj, &batteryled_attr_group);
  404. kobject_put(batteryled_kobj);
  405. }