batteryled.c 10 KB

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