batteryled.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. // blue is charge
  23. // white is health
  24. // Because our LED does not have blue and white. Only red and green. So use red to indicate charging, green to indicate healthy.
  25. enum battery_led_color
  26. {
  27. BATTERY_LED_OFF = 0x00,
  28. BATTERY_LED_WHITE = 0x01,
  29. BATTERY_LED_BLUE = 0x02,
  30. BATTERY_LED_WHITE_BLINK = 0x03,
  31. BATTERY_LED_BLUE_BLINK = 0x04
  32. };
  33. extern struct kobject *vfiec_kobj;
  34. static struct kobject *batteryled_kobj;
  35. static unsigned int led_charge_val = 0;
  36. static unsigned int led_health_val = 0;
  37. static struct delayed_work delay_work1;
  38. static int wait_ibf(void)
  39. {
  40. int i = 0;
  41. while (inb(EC_CMD_PORT) & EC_IBF)
  42. {
  43. if (++i > TIMEOUT_LOOPS)
  44. {
  45. return -1;
  46. }
  47. udelay(1);
  48. }
  49. return 0;
  50. }
  51. static int wait_obf(void)
  52. {
  53. int i = 0;
  54. while (!(inb(EC_CMD_PORT) & EC_OBF))
  55. {
  56. if (++i > TIMEOUT_LOOPS)
  57. {
  58. return -1;
  59. }
  60. udelay(1);
  61. }
  62. return 0;
  63. }
  64. static int ec_read_ram(uint8_t offset, uint8_t *data)
  65. {
  66. if (wait_ibf() < 0)
  67. return -1;
  68. outb(CMD_READ_RAM, EC_CMD_PORT);
  69. if (wait_ibf() < 0)
  70. return -1;
  71. outb(offset, EC_DATA_PORT);
  72. if (wait_obf() < 0)
  73. return -1;
  74. *data = inb(EC_DATA_PORT);
  75. return 0;
  76. }
  77. static int ec_write_ram(uint8_t offset, uint8_t data)
  78. {
  79. if (wait_ibf() < 0)
  80. return -1;
  81. outb(CMD_WRITE_RAM, EC_CMD_PORT);
  82. if (wait_ibf() < 0)
  83. return -1;
  84. outb(offset, EC_DATA_PORT);
  85. if (wait_ibf() < 0)
  86. return -1;
  87. outb(data, EC_DATA_PORT);
  88. return 0;
  89. }
  90. static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
  91. {
  92. unsigned char WEC, REC;
  93. switch(page)
  94. {
  95. case 0:
  96. {
  97. WEC = 0x96;
  98. REC = 0x95;
  99. break;
  100. }
  101. case 1:
  102. {
  103. WEC = 0x98;
  104. REC = 0x97;
  105. break;
  106. }
  107. default:
  108. {
  109. WEC = 0x81;
  110. REC = 0x80;
  111. break;
  112. }
  113. }
  114. if (wait_ibf() < 0)
  115. return -1;
  116. outb(REC, EC_CMD_PORT);
  117. if (wait_ibf() < 0)
  118. return -1;
  119. outb(offset, EC_DATA_PORT);
  120. if (wait_obf() < 0)
  121. return -1;
  122. *data = inb(EC_DATA_PORT);
  123. return 0;
  124. }
  125. static int oem_ec_write_ram(uint8_t page, uint8_t offset, uint8_t data)
  126. {
  127. unsigned char WEC, REC;
  128. switch(page)
  129. {
  130. case 0:
  131. {
  132. WEC = 0x96;
  133. REC = 0x95;
  134. break;
  135. }
  136. case 1:
  137. {
  138. WEC = 0x98;
  139. REC = 0x97;
  140. break;
  141. }
  142. default:
  143. {
  144. WEC = 0x81;
  145. REC = 0x80;
  146. break;
  147. }
  148. }
  149. if (wait_ibf() < 0)
  150. return -1;
  151. outb(WEC, EC_CMD_PORT);
  152. if (wait_ibf() < 0)
  153. return -1;
  154. outb(offset, EC_DATA_PORT);
  155. if (wait_ibf() < 0)
  156. return -1;
  157. outb(data, EC_DATA_PORT);
  158. return 0;
  159. }
  160. /* ==================== max_fade_brightness ==================== */
  161. static ssize_t led_health_show(struct kobject *kobj,
  162. struct kobj_attribute *attr,
  163. char *buf)
  164. {
  165. uint8_t data = 0;
  166. int ret = 0;
  167. ret = oem_ec_read_ram(2, 0x31, &data);
  168. return sprintf(buf, "%02x\n", data);
  169. }
  170. static ssize_t led_health_store(struct kobject *kobj,
  171. struct kobj_attribute *attr,
  172. const char *buf, size_t count)
  173. {
  174. u32 val;
  175. int ret;
  176. printk("%s: %s\n", __func__, buf);
  177. ret = kstrtou32(buf, 10, &val);
  178. if (ret < 0)
  179. return ret;
  180. if(val == 0x0 || val == 0x1 || val == 0x3)
  181. {
  182. led_health_val = val;
  183. }
  184. else
  185. {
  186. return -EINVAL;
  187. }
  188. schedule_delayed_work(&delay_work1, msecs_to_jiffies(10));
  189. return count;
  190. }
  191. static struct kobj_attribute led_health =
  192. __ATTR(led_health, 0644, led_health_show, led_health_store);
  193. /* ==================== mode ==================== */
  194. static ssize_t led_charge_show(struct kobject *kobj, struct kobj_attribute *attr,
  195. char *buf)
  196. {
  197. uint8_t data = 0;
  198. int ret = 0;
  199. ret = oem_ec_read_ram(2, 0x31, &data);
  200. return sprintf(buf, "%02x\n", data);
  201. }
  202. static ssize_t led_charge_store(struct kobject *kobj, struct kobj_attribute *attr,
  203. const char *buf, size_t count)
  204. {
  205. u32 val;
  206. int ret;
  207. printk("%s: %s\n", __func__, buf);
  208. ret = kstrtou32(buf, 10, &val);
  209. if (ret < 0)
  210. return ret;
  211. if(val == 0x0 || val == 0x2 || val == 0x4)
  212. {
  213. led_charge_val = val;
  214. }
  215. else
  216. {
  217. return -EINVAL;
  218. }
  219. schedule_delayed_work(&delay_work1, msecs_to_jiffies(10));
  220. return count;
  221. }
  222. static struct kobj_attribute led_charge =
  223. __ATTR(led_charge, 0644, led_charge_show, led_charge_store);
  224. static struct attribute *batteryled_attrs[] = {
  225. &led_charge.attr,
  226. &led_health.attr,
  227. NULL,
  228. };
  229. static struct attribute_group batteryled_attr_group = {
  230. .attrs = batteryled_attrs,
  231. };
  232. static void delay_work_func(struct work_struct *work)
  233. {
  234. static uint8_t blink_flag = 0;
  235. uint8_t data = 0;
  236. if(blink_flag == 0)
  237. {
  238. //Turn off when flickering
  239. blink_flag = 1;
  240. if(led_health_val == 0x0)
  241. {
  242. data = data & 0xf3;
  243. }
  244. else if(led_health_val == 0x1)
  245. {
  246. data = data | 0x04;
  247. }
  248. else if(led_health_val == 0x3)
  249. {
  250. data = data & 0xf3;
  251. }
  252. if(led_charge_val == 0x0)
  253. {
  254. data = data & 0xcf;
  255. }
  256. else if(led_charge_val == 0x2)
  257. {
  258. data = data | 0x20;
  259. }
  260. else if(led_charge_val == 0x4)
  261. {
  262. data = data & 0xcf;
  263. }
  264. }
  265. else if(blink_flag == 1)
  266. {
  267. // Bright when flashing
  268. blink_flag = 0;
  269. if(led_health_val == 0x0)
  270. {
  271. data = data & 0xf3;
  272. }
  273. else if(led_health_val == 0x1)
  274. {
  275. data = data | 0x04;
  276. }
  277. else if(led_health_val == 0x3)
  278. {
  279. data = data | 0x04;
  280. }
  281. if(led_charge_val == 0x0)
  282. {
  283. data = data & 0xcf;
  284. }
  285. else if(led_charge_val == 0x2)
  286. {
  287. data = data | 0x20;
  288. }
  289. else if(led_charge_val == 0x4)
  290. {
  291. data = data | 0x20;
  292. }
  293. }
  294. oem_ec_write_ram(2, 0x31, data);
  295. oem_ec_write_ram(2, 0x21, 0x3C);
  296. schedule_delayed_work(&delay_work1, msecs_to_jiffies(500));
  297. }
  298. int batteryled_init(void)
  299. {
  300. int ret;
  301. INIT_DELAYED_WORK(&delay_work1, delay_work_func);
  302. /* Create /sys/kernel/vfiec/batteryled */
  303. batteryled_kobj = kobject_create_and_add("batteryled", vfiec_kobj);
  304. if (!batteryled_kobj)
  305. {
  306. ret = -ENOMEM;
  307. return ret;
  308. }
  309. /* Create property file */
  310. ret = sysfs_create_group(batteryled_kobj, &batteryled_attr_group);
  311. if (ret)
  312. {
  313. pr_err("Failed to create sysfs group: %d\n", ret);
  314. goto free_batteryled_kobj;
  315. }
  316. return 0;
  317. free_batteryled_kobj:
  318. kobject_put(batteryled_kobj);
  319. return ret;
  320. }
  321. void batteryled_exit(void)
  322. {
  323. cancel_delayed_work_sync(&delay_work1);
  324. sysfs_remove_group(batteryled_kobj, &batteryled_attr_group);
  325. kobject_put(batteryled_kobj);
  326. }