Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 37.20 KB | None | 0 0
  1. /* Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
  2.  * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
  3.  * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  4.  *
  5.  * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 2 and
  9.  * only version 2 as published by the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  */
  16. #define pr_fmt(fmt) "BQ: %s: " fmt, __func__
  17.  
  18. #include <linux/module.h>
  19. #include <linux/param.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/power_supply.h>
  25. #include <linux/idr.h>
  26. #include <linux/i2c.h>
  27. #include <linux/slab.h>
  28. #include <asm/unaligned.h>
  29. #include <linux/time.h>
  30. #include <linux/gpio.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/regulator/machine.h>
  33. #include <linux/err.h>
  34. #include <linux/rtc.h>
  35.  
  36. #ifdef CONFIG_OF
  37. #include <linux/gpio.h>
  38. #include <linux/of_gpio.h>
  39. #endif
  40. #include <linux/wakelock.h>
  41. #include "oem_external_fg.h"
  42.  
  43.  
  44. #define DRIVER_VERSION          "1.1.0"
  45. /* Bq27541 standard data commands */
  46. #define BQ27541_REG_CNTL        0x00
  47. #define BQ27541_REG_AR          0x02
  48. #define BQ27541_REG_ARTTE       0x04
  49. #define BQ27541_REG_TEMP        0x06
  50. #define BQ27541_REG_VOLT        0x08
  51. #define BQ27541_REG_FLAGS       0x0A
  52. #define BQ27541_REG_NAC         0x0C
  53. #define BQ27541_REG_FAC         0x0e
  54. #define BQ27541_REG_RM          0x10
  55. #define BQ27541_REG_FCC         0x12
  56. #define BQ27541_REG_AI          0x14
  57. #define BQ27541_REG_TTE         0x16
  58. #define BQ27541_REG_TTF         0x18
  59. #define BQ27541_REG_SI          0x1a
  60. #define BQ27541_REG_STTE        0x1c
  61. #define BQ27541_REG_MLI         0x1e
  62. #define BQ27541_REG_MLTTE       0x20
  63. #define BQ27541_REG_AE          0x22
  64. #define BQ27541_REG_AP          0x24
  65. #define BQ27541_REG_TTECP       0x26
  66. #define BQ27541_REG_SOH         0x28
  67. #define BQ27541_REG_SOC         0x2c
  68. #define BQ27541_REG_NIC         0x2e
  69. #define BQ27541_REG_ICR         0x30
  70. #define BQ27541_REG_LOGIDX      0x32
  71. #define BQ27541_REG_LOGBUF      0x34
  72.  
  73. #define BQ27541_FLAG_DSC        BIT(0)
  74. #define BQ27541_FLAG_FC         BIT(9)
  75.  
  76. #define BQ27541_CS_DLOGEN       BIT(15)
  77. #define BQ27541_CS_SS           BIT(13)
  78.  
  79. /* Control subcommands */
  80. #define BQ27541_SUBCMD_CTNL_STATUS  0x0000
  81. #define BQ27541_SUBCMD_DEVCIE_TYPE  0x0001
  82. #define BQ27541_SUBCMD_FW_VER  0x0002
  83. #define BQ27541_SUBCMD_HW_VER  0x0003
  84. #define BQ27541_SUBCMD_DF_CSUM  0x0004
  85. #define BQ27541_SUBCMD_PREV_MACW   0x0007
  86. #define BQ27541_SUBCMD_CHEM_ID   0x0008
  87. #define BQ27541_SUBCMD_BD_OFFSET   0x0009
  88. #define BQ27541_SUBCMD_INT_OFFSET  0x000a
  89. #define BQ27541_SUBCMD_CC_VER   0x000b
  90. #define BQ27541_SUBCMD_OCV  0x000c
  91. #define BQ27541_SUBCMD_BAT_INS   0x000d
  92. #define BQ27541_SUBCMD_BAT_REM   0x000e
  93. #define BQ27541_SUBCMD_SET_HIB   0x0011
  94. #define BQ27541_SUBCMD_CLR_HIB   0x0012
  95. #define BQ27541_SUBCMD_SET_SLP   0x0013
  96. #define BQ27541_SUBCMD_CLR_SLP   0x0014
  97. #define BQ27541_SUBCMD_FCT_RES   0x0015
  98. #define BQ27541_SUBCMD_ENABLE_DLOG  0x0018
  99. #define BQ27541_SUBCMD_DISABLE_DLOG 0x0019
  100. #define BQ27541_SUBCMD_SEALED   0x0020
  101. #define BQ27541_SUBCMD_ENABLE_IT    0x0021
  102. #define BQ27541_SUBCMD_DISABLE_IT   0x0023
  103. #define BQ27541_SUBCMD_CAL_MODE  0x0040
  104. #define BQ27541_SUBCMD_RESET   0x0041
  105. #define ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN   (-2731)
  106. #define BQ27541_INIT_DELAY   ((HZ)*1)
  107.  
  108. #define BQ27541_REG_DCAP_L          (0x3c)
  109. #define BQ27541_REG_DCAP_H          (0x3d)
  110. #define BQ27541_VALUE_DCAP_L            (0X07)
  111. #define BQ27541_VALUE_DCAP_H            (0X0D)
  112.  
  113. #define ERROR_SOC  33
  114. #define ERROR_BATT_VOL  (3800 * 1000)
  115. /* If the system has several batteries we need a different name for each
  116.  * of them...
  117.  */
  118. static DEFINE_MUTEX(battery_mutex);
  119.  
  120. struct bq27541_device_info {
  121.     struct device           *dev;
  122.     struct i2c_client       *client;
  123.     struct work_struct      counter;
  124.     /* 300ms delay is needed after bq27541 is powered up
  125.      * and before any successful I2C transaction
  126.      */
  127.     struct  delayed_work        hw_config;
  128.     struct  delayed_work        battery_soc_work;
  129.     struct wake_lock update_soc_wake_lock;
  130.     struct power_supply     *batt_psy;
  131.     int saltate_counter;
  132.     /*  Add for retry when config fail */
  133.     int retry_count;
  134.     /*  Add for get right soc when sleep long time */
  135.     int soc_pre;
  136.     int  batt_vol_pre;
  137.     int current_pre;
  138.     int health_pre;
  139.     unsigned long rtc_resume_time;
  140.     unsigned long rtc_suspend_time;
  141.     atomic_t suspended;
  142.     int temp_pre;
  143.     int lcd_off_delt_soc;
  144.     int low_bat_capacity;
  145.     int low_bat_voltage_uv;
  146.     bool lcd_is_off;
  147.     bool alow_reading;
  148.     bool fastchg_started;
  149.     bool support_4p4v_bat;
  150.     bool is_4p4v_bat;
  151.     unsigned long   lcd_off_time;
  152.     unsigned long   soc_pre_time;
  153.     unsigned long   soc_store_time;
  154. };
  155.  
  156.  
  157. #include <linux/workqueue.h>
  158.  
  159. struct update_pre_capacity_data{
  160.     struct delayed_work work;
  161.     struct workqueue_struct *workqueue;
  162.     int suspend_time;
  163. };
  164. static struct update_pre_capacity_data update_pre_capacity_data;
  165.  
  166. static int coulomb_counter;
  167. static spinlock_t lock; /* protect access to coulomb_counter */
  168.  
  169. static int bq27541_i2c_txsubcmd(u8 reg, unsigned short subcmd,
  170.         struct bq27541_device_info *di);
  171.  
  172. static int bq27541_read_i2c(u8 reg, int *rt_value, int b_single,
  173.         struct bq27541_device_info *di);
  174.  
  175. static int bq27541_read(u8 reg, int *rt_value, int b_single,
  176.         struct bq27541_device_info *di)
  177. {
  178.     return bq27541_read_i2c(reg, rt_value, b_single, di);
  179. }
  180.  
  181. /*
  182.  * Return the battery temperature in tenths of degree Celsius
  183.  * Or < 0 if something fails.
  184.  */
  185. static int bq27541_battery_temperature(struct bq27541_device_info *di)
  186. {
  187.     int ret;
  188.     int temp = 0;
  189.     static int count = 0;
  190.  
  191.     /* Add for get right soc when sleep long time */
  192.     if(atomic_read(&di->suspended) == 1) {
  193.         return di->temp_pre + ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
  194.     }
  195.  
  196.     if(di->alow_reading) {
  197.         ret = bq27541_read(BQ27541_REG_TEMP, &temp, 0, di);
  198.         /* Add for don't report battery not connect when reading error once. */
  199.         if (ret) {
  200.             count++;
  201.             pr_err("error reading temperature\n");
  202.             if(count > 1) {
  203.                 count = 0;
  204.                 /* Add for it report bad status when plug out battery */
  205.                 di->temp_pre = -400 - ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
  206.                 return -400;
  207.             } else {
  208.                 return di->temp_pre + ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
  209.             }
  210.         }
  211.         count = 0;
  212.     } else {
  213.         return di->temp_pre + ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
  214.     }
  215.     di->temp_pre = temp;
  216.     return temp + ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
  217. }
  218.  
  219. /*
  220.  * Return the battery Voltage in milivolts
  221.  * Or < 0 if something fails.
  222.  */
  223. static int bq27541_battery_voltage(struct bq27541_device_info *di)
  224. {
  225.     int ret;
  226.     int volt = 0;
  227.  
  228.     /* Add for get right soc when sleep long time */
  229.     if (atomic_read(&di->suspended) == 1) {
  230.         return di->batt_vol_pre;
  231.     }
  232.     if (di->alow_reading) {
  233.         ret = bq27541_read(BQ27541_REG_VOLT, &volt, 0, di);
  234.         if (ret) {
  235.             pr_err("error reading voltage,ret:%d\n", ret);
  236.             return ret;
  237.         }
  238.     } else {
  239.         return di->batt_vol_pre;
  240.     }
  241.     di->batt_vol_pre = volt * 1000;
  242.     return volt * 1000;
  243. }
  244.  
  245. static void bq27541_cntl_cmd(struct bq27541_device_info *di,
  246.         int subcmd)
  247. {
  248.     bq27541_i2c_txsubcmd(BQ27541_REG_CNTL, subcmd, di);
  249. }
  250.  
  251. /*
  252.  * i2c specific code
  253.  */
  254. static int bq27541_i2c_txsubcmd(u8 reg, unsigned short subcmd,
  255.         struct bq27541_device_info *di)
  256. {
  257.     struct i2c_msg msg;
  258.     unsigned char data[3];
  259.     int ret;
  260.  
  261.     if (!di->client)
  262.         return -ENODEV;
  263.  
  264.     memset(data, 0, sizeof(data));
  265.     data[0] = reg;
  266.     data[1] = subcmd & 0x00FF;
  267.     data[2] = (subcmd & 0xFF00) >> 8;
  268.  
  269.     msg.addr = di->client->addr;
  270.     msg.flags = 0;
  271.     msg.len = 3;
  272.     msg.buf = data;
  273.  
  274.     ret = i2c_transfer(di->client->adapter, &msg, 1);
  275.     if (ret < 0)
  276.         return -EIO;
  277.  
  278.     return 0;
  279. }
  280.  
  281. static bool bq27541_regist_done = false ;
  282. static bool check_4p4v_bat_present(struct bq27541_device_info *di)
  283. {
  284.     int flags = 0, ret = 0;
  285.     ret = bq27541_read(BQ27541_REG_DCAP_H, &flags, 0, di);
  286.     if(ret < 0)
  287.     {
  288.         pr_err("get bq27541 DCAP_H fail\n");
  289.         bq27541_regist_done = true;
  290.         return false;
  291.     }
  292.     if((flags & 0xff) != BQ27541_VALUE_DCAP_H){
  293.         pr_err("err bq dcap_l value:0x%x\n",flags);
  294.         return false;
  295.     }
  296.     ret = bq27541_read(BQ27541_REG_DCAP_L, &flags, 0, di);
  297.     if(ret < 0)
  298.     {
  299.         pr_err("get bq27541 DCAP_L fail\n");
  300.         bq27541_regist_done = true;
  301.         return false;
  302.     }
  303.     if((flags & 0xff) != BQ27541_VALUE_DCAP_L){
  304.         pr_err("err bq dcap_l value:0x%x\n",flags);
  305.         return false;
  306.     }
  307.     return true;
  308. }
  309.  
  310. static int bq27541_chip_config(struct bq27541_device_info *di)
  311. {
  312.     int flags = 0, ret = 0;
  313.  
  314.     bq27541_cntl_cmd(di, BQ27541_SUBCMD_CTNL_STATUS);
  315.     udelay(66);
  316.     ret = bq27541_read(BQ27541_REG_CNTL, &flags, 0, di);
  317.     if (ret < 0) {
  318.         pr_err("error reading register %02x ret = %d\n",
  319.                 BQ27541_REG_CNTL, ret);
  320.         return ret;
  321.     }
  322.     udelay(66);
  323.     bq27541_cntl_cmd(di, BQ27541_SUBCMD_ENABLE_IT);
  324.     udelay(66);
  325.  
  326.     if (!(flags & BQ27541_CS_DLOGEN)) {
  327.         bq27541_cntl_cmd(di, BQ27541_SUBCMD_ENABLE_DLOG);
  328.         udelay(66);
  329.     }
  330.  
  331.     return 0;
  332. }
  333.  
  334. static void bq27541_coulomb_counter_work(struct work_struct *work)
  335. {
  336.     int value = 0, temp = 0, index = 0, ret = 0;
  337.     struct bq27541_device_info *di;
  338.     unsigned long flags;
  339.     int count = 0;
  340.  
  341.     di = container_of(work, struct bq27541_device_info, counter);
  342.  
  343.     /* retrieve 30 values from FIFO of coulomb data logging buffer
  344.      * and average over time
  345.      */
  346.     do {
  347.         ret = bq27541_read(BQ27541_REG_LOGBUF, &temp, 0, di);
  348.         if (ret < 0)
  349.             break;
  350.         if (temp != 0x7FFF) {
  351.             ++count;
  352.             value += temp;
  353.         }
  354.         /* delay 66uS, waiting time between continuous reading
  355.          * results
  356.          */
  357.         udelay(66);
  358.         ret = bq27541_read(BQ27541_REG_LOGIDX, &index, 0, di);
  359.         if (ret < 0)
  360.             break;
  361.         udelay(66);
  362.     } while (index != 0 || temp != 0x7FFF);
  363.  
  364.     if (ret < 0) {
  365.         pr_err("Error reading datalog register\n");
  366.         return;
  367.     }
  368.  
  369.     if (count) {
  370.         spin_lock_irqsave(&lock, flags);
  371.         coulomb_counter = value/count;
  372.         spin_unlock_irqrestore(&lock, flags);
  373.     }
  374. }
  375.  
  376. struct bq27541_device_info *bq27541_di;
  377. #define TEN_PERCENT                            10
  378. #define SOC_SHUTDOWN_VALID_LIMITS              20
  379. #define TEN_MINUTES                            600
  380. #define FIVE_MINUTES                           300
  381. #define TWO_POINT_FIVE_MINUTES                 150
  382. #define ONE_MINUTE                             60
  383. #define TWENTY_MINUTES                         1200
  384. #define TWENTY_PERCENT                         20
  385.  
  386. #define CAPACITY_SALTATE_COUNTER_60            38 /* 40 1min */
  387. #define CAPACITY_SALTATE_COUNTER_95            78 /* 60 2.5min */
  388. #define CAPACITY_SALTATE_COUNTER_FULL          200 /* 150 120 5min */
  389. #define CAPACITY_SALTATE_COUNTER_CHARGING_TERM 30 /* 30 1min */
  390. #define CAPACITY_SALTATE_COUNTER               4
  391. #define CAPACITY_SALTATE_COUNTER_NOT_CHARGING  24 /* >=40sec */
  392. #define CAPACITY_CALIBRATE_TIME_60_PERCENT     45 /* 45s */
  393.  
  394. static int bq27541_average_current(struct bq27541_device_info *di);
  395.  
  396. extern int get_charging_status(void);
  397. extern int fuelgauge_battery_temp_region_get(void);
  398. extern int load_soc(void);
  399. extern void backup_soc_ex(int soc);
  400. extern bool get_oem_charge_done_status(void);
  401. extern void clean_backup_soc_ex(void);
  402.  
  403. static int get_current_time(unsigned long *now_tm_sec)
  404. {
  405.     struct rtc_time tm;
  406.     struct rtc_device *rtc;
  407.     int rc;
  408.  
  409.     rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
  410.     if (rtc == NULL) {
  411.         pr_err("%s: unable to open rtc device (%s)\n",
  412.                 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
  413.         return -EINVAL;
  414.     }
  415.  
  416.     rc = rtc_read_time(rtc, &tm);
  417.     if (rc) {
  418.         pr_err("Error reading rtc device (%s) : %d\n",
  419.                 CONFIG_RTC_HCTOSYS_DEVICE, rc);
  420.         goto close_time;
  421.     }
  422.  
  423.     rc = rtc_valid_tm(&tm);
  424.     if (rc) {
  425.         pr_err("Invalid RTC time (%s): %d\n",
  426.                 CONFIG_RTC_HCTOSYS_DEVICE, rc);
  427.         goto close_time;
  428.     }
  429.     rtc_tm_to_time(&tm, now_tm_sec);
  430.  
  431. close_time:
  432.     rtc_class_close(rtc);
  433.     return rc;
  434. }
  435.  
  436. int get_prop_pre_shutdown_soc(void)
  437. {
  438.     int soc_load;
  439.  
  440.     soc_load = load_soc();
  441.     if (soc_load == -1)
  442.         return 50;
  443.     else
  444.         return soc_load;
  445. }
  446.  
  447. static int fg_soc_calibrate(struct  bq27541_device_info *di, int soc)
  448. {
  449.     union power_supply_propval ret = {0,};
  450.     unsigned int soc_calib;
  451.     unsigned long soc_current_time, time_last;
  452.     static bool first_enter = false;
  453.     static int charging_status, charging_status_pre = 0;
  454.     bool chg_done;
  455.     int temp_region, vbat_mv, ibat_ma, soc_load, soc_temp, counter_temp = 0;
  456.  
  457.     if (false == first_enter) {
  458.         di->batt_psy = power_supply_get_by_name("battery");
  459.         if(di->batt_psy) {
  460.             first_enter = true;
  461.             soc_load = load_soc();
  462.             pr_info("soc=%d, soc_load=%d\n", soc, soc_load);
  463.             if (soc_load == -1) {
  464.                 /* get last soc error */
  465.                 di->soc_pre = soc;
  466.             } else if (soc_load > 0 && soc_load < 100) {
  467.                 if(soc_load > soc)
  468.                     di->soc_pre = soc_load - 1;
  469.                 else
  470.                     di->soc_pre = soc_load;
  471.             } else if (soc_load == 100
  472.                     && abs(soc_load - soc) > TEN_PERCENT) {
  473.                 /* decrease soc when gap between soc_load and */
  474.                 /* real_soc is over 10%                       */
  475.                 di->soc_pre = soc_load - 1;
  476.             } else {
  477.                 di->soc_pre = soc_load;
  478.             }
  479.  
  480.             if (!di->batt_psy) {
  481.                 pr_err("batt_psy is absent, soc_pre=%d\n", di->soc_pre);
  482.                 return di->soc_pre;
  483.             }
  484.             /* store the soc when boot first time */
  485.             get_current_time(&di->soc_pre_time);
  486.             clean_backup_soc_ex();
  487.         } else {
  488.             return soc;
  489.         }
  490.     }
  491.     soc_temp = di->soc_pre;
  492.  
  493.     if (!di->batt_psy) {
  494.         soc_calib = soc;
  495.         goto out;
  496.     }
  497.  
  498.     ret.intval = get_charging_status();
  499.     di->batt_vol_pre = bq27541_battery_voltage(di);
  500.     chg_done = get_oem_charge_done_status();
  501.     temp_region = fuelgauge_battery_temp_region_get();
  502.     if ((temp_region == BATT_TEMP_LITTLE_COOL
  503.             || temp_region == BATT_TEMP_COOL
  504.             || temp_region == BATT_TEMP_NORMAL
  505.             || temp_region == BATT_TEMP_PRE_NORMAL)
  506.             && chg_done) {
  507.         ret.intval = POWER_SUPPLY_STATUS_FULL;
  508.     }
  509.  
  510.     if (ret.intval == POWER_SUPPLY_STATUS_CHARGING
  511.             || ret.intval == POWER_SUPPLY_STATUS_FULL
  512.             || bq27541_di->fastchg_started)
  513.         charging_status = 1;
  514.     else
  515.         charging_status = 0;
  516.  
  517.     if (charging_status ^ charging_status_pre) {
  518.         if (charging_status_pre) {
  519.             get_current_time(&soc_current_time);
  520.             di->soc_store_time = soc_current_time - di->soc_pre_time;
  521.         }
  522.  
  523.         get_current_time(&di->soc_pre_time);
  524.         if (!charging_status_pre && di->soc_store_time)
  525.             di->soc_pre_time -= di->soc_store_time;
  526.         charging_status_pre = charging_status;
  527.         di->saltate_counter = 0;
  528.     }
  529.  
  530.     get_current_time(&soc_current_time);
  531.     time_last = soc_current_time - di->soc_pre_time;
  532.     if (charging_status) { /* is charging */
  533.         if (ret.intval == POWER_SUPPLY_STATUS_FULL) {
  534.             soc_calib = di->soc_pre;
  535.             if (di->soc_pre < 100
  536.                     && (temp_region == BATT_TEMP_LITTLE_COOL
  537.                     || temp_region == BATT_TEMP_NORMAL
  538.                     || temp_region == BATT_TEMP_PRE_NORMAL
  539.                     || temp_region == BATT_TEMP_COOL)) {
  540.                 if (di->saltate_counter < CAPACITY_SALTATE_COUNTER_CHARGING_TERM) {
  541.                     di->saltate_counter++;
  542.                 } else {
  543.                     soc_calib = di->soc_pre + 1;
  544.                     di->saltate_counter = 0;
  545.                 }
  546.             }
  547.         } else {
  548.             if (soc - di->soc_pre > 0) {
  549.                 di->saltate_counter++;
  550.                 if ((bq27541_di->fastchg_started && time_last < 20)
  551.                         || (!bq27541_di->fastchg_started && time_last < 30))
  552.                     return di->soc_pre;
  553.                 else
  554.                     di->saltate_counter = 0;
  555.                 soc_calib = di->soc_pre + 1;
  556.             } else if (soc < (di->soc_pre - 1)) {
  557.                 di->saltate_counter++;
  558.                 if (di->soc_pre == 100) {
  559.                     counter_temp = CAPACITY_SALTATE_COUNTER_FULL; /* t>=5min */
  560.                 } else if (di->soc_pre > 95) {
  561.                     counter_temp = CAPACITY_SALTATE_COUNTER_95; /* t>=2.5min */
  562.                 } else if (di->soc_pre > 60) {
  563.                     counter_temp = CAPACITY_SALTATE_COUNTER_60; /* t>=1min */
  564.                 } else {
  565.                     if (time_last > CAPACITY_CALIBRATE_TIME_60_PERCENT
  566.                             && (soc - di->soc_pre) < 0)
  567.                         counter_temp = 0;
  568.                     else
  569.                         /* t>=40sec */
  570.                         counter_temp = CAPACITY_SALTATE_COUNTER_NOT_CHARGING;
  571.                 }
  572.  
  573.                 /* avoid dead battery shutdown */
  574.                 if (di->batt_vol_pre <= di->low_bat_voltage_uv
  575.                         && di->batt_vol_pre > 2500 * 1000
  576.                         && di->soc_pre <= di->low_bat_capacity) {
  577.                     /* check again */
  578.                     vbat_mv = bq27541_battery_voltage(di);
  579.                     if (vbat_mv <= di->low_bat_voltage_uv
  580.                             && vbat_mv > 2500 * 1000) {
  581.                         /* about 9s */
  582.                         counter_temp = CAPACITY_SALTATE_COUNTER - 1;
  583.                     }
  584.                 }
  585.  
  586.                 ibat_ma = bq27541_average_current(di);
  587.                 /* don't allow soc down if chg current > -200mA */
  588.                 if (di->saltate_counter < counter_temp
  589.                         || ibat_ma < -200 * 1000)
  590.                     return di->soc_pre;
  591.                 else
  592.                     di->saltate_counter = 0;
  593.  
  594.                 soc_calib = di->soc_pre - 1;
  595.             } else if ((soc == 0 && soc < di->soc_pre)
  596.                     && di->soc_pre <= 2) {
  597.                 di->saltate_counter++;
  598.                 if (time_last > CAPACITY_CALIBRATE_TIME_60_PERCENT
  599.                         && (soc - di->soc_pre) < 0)
  600.                     counter_temp = 0;
  601.                 else
  602.                     /* t>=40sec */
  603.                     counter_temp = CAPACITY_SALTATE_COUNTER_NOT_CHARGING;
  604.  
  605.                 if (di->saltate_counter < counter_temp)
  606.                     return di->soc_pre;
  607.                 else
  608.                     di->saltate_counter = 0;
  609.                 soc_calib = di->soc_pre - 1;
  610.             } else {
  611.                 soc_calib = di->soc_pre;
  612.             }
  613.         }
  614.     } else { /* not charging */
  615.         if ((soc < di->soc_pre)
  616.                 || (di->batt_vol_pre <= di->low_bat_voltage_uv
  617.                 && di->batt_vol_pre > 2500 * 1000)) {
  618.             if(di->soc_pre == 100){
  619.                 counter_temp = FIVE_MINUTES;
  620.             }
  621.             else if (di->soc_pre > 95){
  622.                 counter_temp = TWO_POINT_FIVE_MINUTES;
  623.             }
  624.             else if (di->soc_pre > 60){
  625.                 counter_temp = ONE_MINUTE;
  626.             }
  627.             else {
  628.                 counter_temp = CAPACITY_CALIBRATE_TIME_60_PERCENT;
  629.             }
  630.  
  631.             /* avoid dead battery shutdown */
  632.             if (di->batt_vol_pre <= di->low_bat_voltage_uv
  633.                     && di->batt_vol_pre > 2500 * 1000
  634.                     && di->soc_pre <= di->low_bat_capacity) {
  635.                 /* check again */
  636.                 vbat_mv = bq27541_battery_voltage(di);
  637.                 if (vbat_mv <= di->low_bat_voltage_uv
  638.                         && vbat_mv > 2500 * 1000 && time_last > 9)
  639.                     counter_temp = 0;
  640.             }
  641.  
  642.             if (time_last < counter_temp)
  643.                 return di->soc_pre;
  644.         }
  645.  
  646.         if (soc < di->soc_pre)
  647.             soc_calib = di->soc_pre - 1;
  648.         else if (di->batt_vol_pre <= di->low_bat_voltage_uv
  649.                 && di->batt_vol_pre > 2500 * 1000
  650.                 && di->soc_pre > 0 && time_last > 9)
  651.             soc_calib = di->soc_pre - 1;
  652.         else
  653.             soc_calib = di->soc_pre;
  654.     }
  655.  
  656. out:
  657.     if(soc_calib > 100)
  658.         soc_calib = 100;
  659.     if(soc_calib < 0)
  660.         soc_calib = 0;
  661.     di->soc_pre = soc_calib;
  662.  
  663.     if(soc_temp != soc_calib) {
  664.         get_current_time(&di->soc_pre_time);
  665.         /* store when soc changed */
  666.         power_supply_changed(di->batt_psy);
  667.         pr_info("soc:%d, soc_calib:%d, VOLT:%d, fcc:%d, current:%d\n",
  668.                 soc, soc_calib, bq27541_battery_voltage(di) / 1000,
  669.                 bq27541_full_capacity(di),
  670.                 bq27541_average_current(di) / 1000);
  671.     }
  672.  
  673.     return soc_calib;
  674. }
  675.  
  676.  
  677. static int bq27541_battery_soc(struct bq27541_device_info *di, int suspend_time_ms)
  678. {
  679.     int ret;
  680.     int soc = 0;
  681.     int soc_delt = 0;
  682.     static int soc_pre;
  683.     bool fg_soc_changed=false;
  684.     /* Add for get right soc when sleep long time */
  685.     if(atomic_read(&di->suspended) == 1) {
  686.         dev_warn(di->dev, "di->suspended di->soc_pre=%d\n", di->soc_pre);
  687.         return di->soc_pre;
  688.     }
  689.     if(di->alow_reading) {
  690.         ret = bq27541_read(BQ27541_REG_SOC, &soc, 0, di);
  691.         if (ret) {
  692.             pr_err("error reading soc=%d, ret:%d\n", soc, ret);
  693.             goto read_soc_err;
  694.         }
  695.         if(soc_pre != soc)
  696.             pr_err("bq27541_battery_soc = %d\n", soc);
  697.  
  698.         soc_pre = soc;
  699.     } else {
  700.         if(di->soc_pre)
  701.             return di->soc_pre;
  702.         else
  703.             return 0;
  704.     }
  705.     /* Add for get right soc when sleep long time */
  706.     if (suspend_time_ms && di->lcd_is_off) {
  707.         if (soc < di->soc_pre) {
  708.             soc_delt  =  di->soc_pre - soc;
  709.             fg_soc_changed = (soc < TWENTY_PERCENT
  710.                     || soc_delt > di->lcd_off_delt_soc
  711.                     || suspend_time_ms > TEN_MINUTES);
  712.             pr_info("suspend_time_ms=%d,soc_delt=%d,di->lcd_off_delt_soc=%d\n",
  713.                     suspend_time_ms, soc_delt, di->lcd_off_delt_soc);
  714.             if (fg_soc_changed) {
  715.                 if (suspend_time_ms/TEN_MINUTES) {
  716.                     di->soc_pre -= (suspend_time_ms / TEN_MINUTES < soc_delt
  717.                             ? suspend_time_ms / TEN_MINUTES : soc_delt);
  718.                 } else {
  719.                     di->soc_pre -= 1;
  720.                 }
  721.                 /* store when soc changed */
  722.                 get_current_time(&di->soc_pre_time);
  723.                 power_supply_changed(di->batt_psy);
  724.                 pr_err("system resume,soc:%d, soc_calib:%d,VOLT:%d,current:%d\n",
  725.                     soc, di->soc_pre, bq27541_battery_voltage(di) / 1000,
  726.                     bq27541_average_current(di) / 1000);
  727.             }
  728.         }
  729.         goto read_soc_err;
  730.     }
  731.     soc = fg_soc_calibrate(di,soc);
  732.     return soc;
  733.  
  734. read_soc_err:
  735.     if(di->soc_pre) {
  736.         dev_warn(di->dev, "read_soc_exit ,di->soc_pre=%d\n", di->soc_pre);
  737.         return di->soc_pre;
  738.     }
  739.     else
  740.         return 0;
  741. }
  742.  
  743. static int bq27541_average_current(struct bq27541_device_info *di)
  744. {
  745.     int ret;
  746.     int curr = 0;
  747.  
  748.     /* Add for get right soc when sleep long time */
  749.     if(atomic_read(&di->suspended) == 1)
  750.         return -di->current_pre;
  751.     if (di->alow_reading) {
  752.         ret = bq27541_read(BQ27541_REG_AI, &curr, 0, di);
  753.         if (ret) {
  754.             pr_err("error reading current.\n");
  755.             return ret;
  756.         }
  757.     } else {
  758.         return -di->current_pre;
  759.     }
  760.     /* negative current */
  761.     if (curr & 0x8000)
  762.         curr = -((~(curr-1)) & 0xFFFF);
  763.     di->current_pre = 1000 * curr;
  764.     return -curr * 1000;
  765. }
  766.  
  767. static int bq27541_remaining_capacity(struct bq27541_device_info *di)
  768. {
  769.     int ret;
  770.     int cap = 0;
  771.  
  772.     if(di->alow_reading) {
  773.         ret = bq27541_read(BQ27541_REG_RM, &cap, 0, di);
  774.         if (ret) {
  775.             pr_err("error reading capacity.\n");
  776.             return ret;
  777.         }
  778.     }
  779.  
  780.     return cap;
  781. }
  782.  
  783. static int bq27541_full_capacity(struct bq27541_device_info *di)
  784. {
  785.     int ret;
  786.     int cap = 0;
  787.  
  788.     if(di->alow_reading) {
  789.         ret = bq27541_read(BQ27541_REG_FCC, &cap, 0, di);
  790.         if (ret) {
  791.             pr_err("error reading full capacity.\n");
  792.             return ret;
  793.         }
  794.     }
  795.  
  796.     return cap;
  797. }
  798.  
  799. static int bq27541_batt_health(struct bq27541_device_info *di)
  800. {
  801.     int ret;
  802.     int health = 0;
  803.  
  804.     if(di->alow_reading) {
  805.         ret = bq27541_read(BQ27541_REG_NIC, &health, 0, di);
  806.         if (ret) {
  807.             pr_err("error reading health\n");
  808.             return ret;
  809.         }
  810.         di->health_pre = health;
  811.     }
  812.  
  813.     return di->health_pre;
  814. }
  815.  
  816. static int bq27541_get_battery_mvolts(void)
  817. {
  818.     return bq27541_battery_voltage(bq27541_di);
  819. }
  820.  
  821. static int bq27541_get_batt_remaining_capacity(void)
  822. {
  823.     return bq27541_remaining_capacity(bq27541_di);
  824. }
  825.  
  826. static int bq27541_get_batt_full_capacity(void)
  827. {
  828.     return bq27541_full_capacity(bq27541_di);
  829. }
  830.  
  831. static int bq27541_get_batt_health(void)
  832. {
  833.     return bq27541_batt_health(bq27541_di);
  834. }
  835.  
  836. static int bq27541_get_battery_temperature(void)
  837. {
  838.     return bq27541_battery_temperature(bq27541_di);
  839. }
  840. static bool bq27541_is_battery_present(void)
  841. {
  842.     return true;
  843. }
  844.  
  845. static bool bq27541_is_battery_temp_within_range(void)
  846. {
  847.     return true;
  848. }
  849.  
  850. static bool bq27541_is_battery_id_valid(void)
  851. {
  852.     return true;
  853. }
  854.  
  855. static int bq27541_get_battery_soc(void)
  856. {
  857.     return bq27541_battery_soc(bq27541_di, 0);
  858. }
  859.  
  860. static int bq27541_get_average_current(void)
  861. {
  862.     return bq27541_average_current(bq27541_di);
  863. }
  864.  
  865. static int bq27541_set_alow_reading(int enable)
  866. {
  867.     if(bq27541_di)
  868.         bq27541_di->alow_reading = enable;
  869.  
  870.     return 0;
  871. }
  872.  
  873. static int bq27541_set_lcd_off_status(int off)
  874. {
  875.     int soc,ret;
  876.  
  877.     pr_info("off=%d\n", off);
  878.     if (bq27541_di) {
  879.         if (off) {
  880.             ret = bq27541_read(BQ27541_REG_SOC, &soc, 0, bq27541_di);
  881.             if (ret) {
  882.                 bq27541_di->lcd_off_delt_soc=0;
  883.                 pr_err("soc error reading ret=%d,soc%d\n", ret, soc);
  884.             } else {
  885.                 bq27541_di->lcd_off_delt_soc = bq27541_di->soc_pre-soc;
  886.                 pr_info("lcd_off_delt_soc:%d,soc=%d,soc_pre=%d\n",
  887.                         bq27541_di->lcd_off_delt_soc, soc,
  888.                         bq27541_di->soc_pre);
  889.             }
  890.             get_current_time(&bq27541_di->lcd_off_time);
  891.             bq27541_di->lcd_is_off=true;
  892.         } else {
  893.             bq27541_di->lcd_is_off=false;
  894.             bq27541_di->lcd_off_delt_soc=0;
  895.         }
  896.     }
  897.     return 0;
  898. }
  899.  
  900. static int bq27541_get_fastchg_started_status(bool fastchg_started_status)
  901. {
  902.     if(bq27541_di)
  903.         bq27541_di->fastchg_started = fastchg_started_status;
  904.  
  905.     return 0;
  906. }
  907. static bool bq27541_get_4p4v_battery_present(void)
  908. {
  909.     if(bq27541_di)
  910.         return bq27541_di->is_4p4v_bat;
  911.     return false;
  912. }
  913.  
  914.  
  915. static struct external_battery_gauge bq27541_batt_gauge = {
  916.     .get_battery_mvolts     = bq27541_get_battery_mvolts,
  917.     .get_battery_temperature    = bq27541_get_battery_temperature,
  918.     .is_battery_present     = bq27541_is_battery_present,
  919.     .is_battery_temp_within_range   = bq27541_is_battery_temp_within_range,
  920.     .is_battery_id_valid        = bq27541_is_battery_id_valid,
  921.     .get_batt_remaining_capacity        =bq27541_get_batt_remaining_capacity,
  922.     .get_batt_health        = bq27541_get_batt_health,
  923.     .get_battery_soc            = bq27541_get_battery_soc,
  924.     .get_average_current        = bq27541_get_average_current,
  925.     .set_alow_reading       = bq27541_set_alow_reading,
  926.     .set_lcd_off_status     = bq27541_set_lcd_off_status,
  927.     .fast_chg_started_status    = bq27541_get_fastchg_started_status,
  928.     .get_4p4v_battery_present   = bq27541_get_4p4v_battery_present,
  929. };
  930. #define BATTERY_SOC_UPDATE_MS 6000
  931. #define RESUME_SCHDULE_SOC_UPDATE_WORK_MS 60000
  932.  
  933. static int is_usb_pluged(void)
  934. {
  935.     static struct power_supply *psy;
  936.     union power_supply_propval ret = {0,};
  937.     int Vbus;
  938.     if (!psy) {
  939.         psy = power_supply_get_by_name("battery");
  940.         if (!psy) {
  941.             pr_err("failed to get ps battery\n");
  942.             return -EINVAL;
  943.         }
  944.     }
  945.  
  946.     if (psy->get_property(psy, POWER_SUPPLY_PROP_CHARGE_NOW, &ret))
  947.         return -EINVAL;
  948.  
  949.     if (ret.intval <= 0)
  950.         return -EINVAL;
  951.  
  952.     Vbus = ret.intval;
  953.     return (Vbus > 2500) ? true : false;
  954. }
  955.  
  956. static bool get_dash_started(void)
  957. {
  958.     if(bq27541_di && bq27541_di->fastchg_started)
  959.         return bq27541_di->fastchg_started ;
  960.     else
  961.         return false;
  962. }
  963.  
  964. static void update_battery_soc_work(struct work_struct *work)
  965. {
  966.     if(is_usb_pluged()== true) {
  967.         queue_delayed_work(system_power_efficient_wq,
  968.                 &bq27541_di->battery_soc_work,
  969.                 msecs_to_jiffies(BATTERY_SOC_UPDATE_MS));
  970.         if(get_dash_started()==true)
  971.             return;
  972.         if(!bq27541_di->alow_reading)
  973.             bq27541_set_alow_reading(true);
  974.         return;
  975.     }
  976.     bq27541_set_alow_reading(true);
  977.     bq27541_get_battery_mvolts();
  978.     bq27541_get_average_current();
  979.     bq27541_get_battery_temperature();
  980.     bq27541_get_battery_soc();
  981.     bq27541_get_batt_remaining_capacity();
  982.     bq27541_set_alow_reading(false);
  983.     queue_delayed_work(system_power_efficient_wq,
  984.         &bq27541_di->battery_soc_work,
  985.         msecs_to_jiffies(BATTERY_SOC_UPDATE_MS));
  986. }
  987.  
  988. bool get_extern_fg_regist_done(void)
  989. {
  990.     return bq27541_regist_done;
  991. }
  992. static int set_property_on_fg(enum power_supply_property prop, int val)
  993. {
  994.     int rc;
  995.     union power_supply_propval ret = {0, };
  996.     struct power_supply *bms_psy;
  997.  
  998.     bms_psy = power_supply_get_by_name("bms");
  999.     if (!bms_psy) {
  1000.         pr_err("bms_psy not exist\n");
  1001.         return -EINVAL;
  1002.     }
  1003.  
  1004.     ret.intval = val;
  1005.     rc = bms_psy->set_property(bms_psy, prop, &ret);
  1006.     if (rc)
  1007.         pr_err("bms psy does not allow updating prop %d rc = %d\n",prop, rc);
  1008.  
  1009.     return rc;
  1010. }
  1011.  
  1012. static void check_bat_type(struct bq27541_device_info * di)
  1013. {
  1014.         di->is_4p4v_bat = check_4p4v_bat_present(di);
  1015.         set_property_on_fg(POWER_SUPPLY_PROP_BATTERY_4P4V_PRESENT,di->is_4p4v_bat);
  1016.         pr_info("is_4p4v_bat=%d,support_4p4v_bat=%d\n",di->is_4p4v_bat,di->support_4p4v_bat);
  1017. }
  1018.  
  1019. static int is_battery_type_right(struct bq27541_device_info * di)
  1020. {
  1021.         if(di->support_4p4v_bat)
  1022.         {
  1023.             if(di->is_4p4v_bat)
  1024.                 return true;
  1025.         }
  1026.         else
  1027.         {
  1028.             if(!di->is_4p4v_bat)
  1029.                 return true;
  1030.         }
  1031.         return false;
  1032. }
  1033. static void bq27541_hw_config(struct work_struct *work)
  1034. {
  1035.     int ret = 0, flags = 0, type = 0, fw_ver = 0;
  1036.     struct bq27541_device_info *di;
  1037.  
  1038.     di = container_of(work, struct bq27541_device_info,
  1039.             hw_config.work);
  1040.     ret = bq27541_chip_config(di);
  1041.     if (ret) {
  1042.         pr_err("Failed to config Bq27541\n");
  1043.         /* Add for retry when config fail */
  1044.         di->retry_count--;
  1045.         if (di->retry_count > 0) {
  1046.             queue_delayed_work(system_power_efficient_wq,
  1047.                 &di->hw_config, HZ);
  1048.         } else
  1049.             bq27541_regist_done = true;
  1050.  
  1051.         return;
  1052.     }
  1053.  
  1054.     ret = is_battery_type_right(di);
  1055.     if(!ret)
  1056.         return;
  1057.     external_battery_gauge_register(&bq27541_batt_gauge);
  1058.     bq27541_information_register(&bq27541_batt_gauge);
  1059.     bq27541_cntl_cmd(di, BQ27541_SUBCMD_CTNL_STATUS);
  1060.     udelay(66);
  1061.     bq27541_read(BQ27541_REG_CNTL, &flags, 0, di);
  1062.     bq27541_cntl_cmd(di, BQ27541_SUBCMD_DEVCIE_TYPE);
  1063.     udelay(66);
  1064.     bq27541_read(BQ27541_REG_CNTL, &type, 0, di);
  1065.     bq27541_cntl_cmd(di, BQ27541_SUBCMD_FW_VER);
  1066.     udelay(66);
  1067.     bq27541_read(BQ27541_REG_CNTL, &fw_ver, 0, di);
  1068.     bq27541_regist_done = true;
  1069.     pr_info("DEVICE_TYPE is 0x%02X, FIRMWARE_VERSION is 0x%02X\n",
  1070.             type, fw_ver);
  1071.     pr_info("Complete bq27541 configuration 0x%02X\n", flags);
  1072. }
  1073.  
  1074. static int bq27541_read_i2c(u8 reg, int *rt_value, int b_single,
  1075.         struct bq27541_device_info *di)
  1076. {
  1077.     struct i2c_client *client = di->client;
  1078.     struct i2c_msg msg[2];
  1079.     unsigned char data[2];
  1080.     int err;
  1081.  
  1082.     if (!client->adapter)
  1083.         return -ENODEV;
  1084.     mutex_lock(&battery_mutex);
  1085.  
  1086.     /* Write register */
  1087.     msg[0].addr = client->addr;
  1088.     msg[0].flags = 0;
  1089.     msg[0].len = 1;
  1090.     msg[0].buf = data;
  1091.     data[0] = reg;
  1092.     /* Read data */
  1093.     msg[1].addr = client->addr;
  1094.     msg[1].flags = I2C_M_RD;
  1095.     if (!b_single)
  1096.         msg[1].len = 2;
  1097.     else
  1098.         msg[1].len = 1;
  1099.     msg[1].buf = data;
  1100.     err = i2c_transfer(client->adapter, msg, 2);
  1101.     if (err >= 0) {
  1102.         if (!b_single)
  1103.             *rt_value = get_unaligned_le16(data);
  1104.         else
  1105.             *rt_value = data[0];
  1106.         mutex_unlock(&battery_mutex);
  1107.         return 0;
  1108.     }
  1109.     mutex_unlock(&battery_mutex);
  1110.     return err;
  1111. }
  1112.  
  1113. #ifdef CONFIG_BQ27541_TEST_ENABLE
  1114. static int reg;
  1115. static int subcmd;
  1116. static ssize_t bq27541_read_stdcmd(struct device *dev,
  1117.         struct device_attribute *attr, char *buf)
  1118. {
  1119.     int ret;
  1120.     int temp = 0;
  1121.     struct platform_device *client;
  1122.     struct bq27541_device_info *di;
  1123.  
  1124.     client = to_platform_device(dev);
  1125.     di = platform_get_drvdata(client);
  1126.  
  1127.     if (reg <= BQ27541_REG_ICR && reg > 0x00) {
  1128.         ret = bq27541_read(reg, &temp, 0, di);
  1129.         if (ret)
  1130.             ret = snprintf(buf, PAGE_SIZE, "Read Error!\n");
  1131.         else
  1132.             ret = snprintf(buf, PAGE_SIZE, "0x%02x\n", temp);
  1133.     } else
  1134.         ret = snprintf(buf, PAGE_SIZE, "Register Error!\n");
  1135.  
  1136.     return ret;
  1137. }
  1138.  
  1139. static ssize_t bq27541_write_stdcmd(struct device *dev,
  1140.         struct device_attribute *attr, const char *buf, size_t count)
  1141. {
  1142.     ssize_t ret = strnlen(buf, PAGE_SIZE);
  1143.     int cmd;
  1144.  
  1145.     sscanf(buf, "%x", &cmd);
  1146.     reg = cmd;
  1147.     return ret;
  1148. }
  1149.  
  1150. static ssize_t bq27541_read_subcmd(struct device *dev,
  1151.         struct device_attribute *attr, char *buf)
  1152. {
  1153.     int ret;
  1154.     int temp = 0;
  1155.     struct platform_device *client;
  1156.     struct bq27541_device_info *di;
  1157.  
  1158.     client = to_platform_device(dev);
  1159.     di = platform_get_drvdata(client);
  1160.  
  1161.     if (subcmd == BQ27541_SUBCMD_DEVCIE_TYPE ||
  1162.             subcmd == BQ27541_SUBCMD_FW_VER ||
  1163.             subcmd == BQ27541_SUBCMD_HW_VER ||
  1164.             subcmd == BQ27541_SUBCMD_CHEM_ID) {
  1165.  
  1166.         bq27541_cntl_cmd(di, subcmd); /* Retrieve Chip status */
  1167.         udelay(66);
  1168.         ret = bq27541_read(BQ27541_REG_CNTL, &temp, 0, di);
  1169.  
  1170.         if (ret)
  1171.             ret = snprintf(buf, PAGE_SIZE, "Read Error!\n");
  1172.         else
  1173.             ret = snprintf(buf, PAGE_SIZE, "0x%02x\n", temp);
  1174.     } else
  1175.         ret = snprintf(buf, PAGE_SIZE, "Register Error!\n");
  1176.  
  1177.     return ret;
  1178. }
  1179.  
  1180. static ssize_t bq27541_write_subcmd(struct device *dev,
  1181.         struct device_attribute *attr, const char *buf, size_t count)
  1182. {
  1183.     ssize_t ret = strnlen(buf, PAGE_SIZE);
  1184.     int cmd;
  1185.  
  1186.     sscanf(buf, "%x", &cmd);
  1187.     subcmd = cmd;
  1188.     return ret;
  1189. }
  1190.  
  1191. static DEVICE_ATTR(std_cmd, S_IRUGO|S_IWUGO, bq27541_read_stdcmd,
  1192.         bq27541_write_stdcmd);
  1193. static DEVICE_ATTR(sub_cmd, S_IRUGO|S_IWUGO, bq27541_read_subcmd,
  1194.         bq27541_write_subcmd);
  1195. static struct attribute *fs_attrs[] = {
  1196.     &dev_attr_std_cmd.attr,
  1197.     &dev_attr_sub_cmd.attr,
  1198.     NULL,
  1199. };
  1200. static struct attribute_group fs_attr_group = {
  1201.     .attrs = fs_attrs,
  1202. };
  1203.  
  1204. static struct platform_device this_device = {
  1205.     .name           = "bq27541-test",
  1206.     .id         = -1,
  1207.     .dev.platform_data  = NULL,
  1208. };
  1209. #endif
  1210.  
  1211. static void update_pre_capacity_func(struct work_struct *w)
  1212. {
  1213.     pr_info("enter\n");
  1214.     bq27541_set_alow_reading(true);
  1215.     bq27541_battery_soc(bq27541_di, update_pre_capacity_data.suspend_time);
  1216.     bq27541_set_alow_reading(false);
  1217.     wake_unlock(&bq27541_di->update_soc_wake_lock);
  1218.     pr_info("exit\n");
  1219. }
  1220.  
  1221. #define MAX_RETRY_COUNT 5
  1222. #define DEFAULT_INVALID_SOC_PRE  -22
  1223. static int bq27541_parse_dt(struct bq27541_device_info *di)
  1224. {
  1225.     struct device_node *dev_node = di->client->dev.of_node;
  1226.     if (!dev_node) {
  1227.         pr_err("device tree info. missing\n");
  1228.         return -EINVAL;
  1229.     }
  1230.     if(of_property_read_bool(dev_node, "oem,support-4p4v-battery"))
  1231.         di->support_4p4v_bat = true;
  1232.  
  1233.     of_property_read_u32(di->client->dev.of_node,
  1234.                 "op,low-bat-voltage-uv",
  1235.                 &di->low_bat_voltage_uv);
  1236.     of_property_read_u32(di->client->dev.of_node,
  1237.             "op,low-bat-capacity",
  1238.             &di->low_bat_capacity);
  1239.     return 0;
  1240. }
  1241.  
  1242. static int bq27541_battery_probe(struct i2c_client *client,
  1243.         const struct i2c_device_id *id)
  1244. {
  1245.     struct bq27541_device_info *di;
  1246. #ifdef CONFIG_BQ27541_TEST_ENABLE
  1247.     int ret;
  1248. #endif
  1249.  
  1250.     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  1251.         return -ENODEV;
  1252.  
  1253.     di = kzalloc(sizeof(*di), GFP_KERNEL);
  1254.     if (!di) {
  1255.         dev_err(&client->dev, "failed to allocate device info data\n");
  1256.         return -ENOMEM;
  1257.     }
  1258.  
  1259.     update_pre_capacity_data.workqueue = create_workqueue("update_pre_capacity");
  1260.     INIT_DELAYED_WORK(&(update_pre_capacity_data.work), update_pre_capacity_func);
  1261.  
  1262.     i2c_set_clientdata(client, di);
  1263.     di->dev = &client->dev;
  1264.     di->client = client;
  1265.     bq27541_parse_dt(di);
  1266.  
  1267.     wake_lock_init(&di->update_soc_wake_lock,
  1268.             WAKE_LOCK_SUSPEND, "bq_delt_soc_wake_lock");
  1269.     di->soc_pre = DEFAULT_INVALID_SOC_PRE;
  1270.     di->alow_reading = true;
  1271.     /* Add for retry when config fail */
  1272.     di->retry_count = MAX_RETRY_COUNT;
  1273.     atomic_set(&di->suspended, 0);
  1274.  
  1275. #ifdef CONFIG_BQ27541_TEST_ENABLE
  1276.     platform_set_drvdata(&this_device, di);
  1277.     ret = platform_device_register(&this_device);
  1278.     if (!ret) {
  1279.         ret = sysfs_create_group(&this_device.dev.kobj,
  1280.                 &fs_attr_group);
  1281.         if (ret)
  1282.             goto batt_failed_1;
  1283.     } else
  1284.         goto batt_failed_1;
  1285. #endif
  1286.  
  1287.     spin_lock_init(&lock);
  1288.  
  1289.     bq27541_di = di;
  1290.     INIT_WORK(&di->counter, bq27541_coulomb_counter_work);
  1291.     INIT_DELAYED_WORK(&di->hw_config, bq27541_hw_config);
  1292.     INIT_DELAYED_WORK(&di->battery_soc_work, update_battery_soc_work);
  1293.     queue_delayed_work(system_power_efficient_wq,
  1294.         &di->hw_config, BQ27541_INIT_DELAY);
  1295.     queue_delayed_work(system_power_efficient_wq,
  1296.         &di->battery_soc_work, BATTERY_SOC_UPDATE_MS);
  1297.     check_bat_type(di);
  1298.  
  1299.     dev_info(&client->dev, "probe success\n");
  1300.  
  1301.     return 0;
  1302.  
  1303. #ifdef CONFIG_BQ27541_TEST_ENABLE
  1304. batt_failed_1:
  1305.     kfree(di);
  1306.     return ret;
  1307. #endif
  1308. }
  1309.  
  1310. static int bq27541_battery_remove(struct i2c_client *client)
  1311. {
  1312.     struct bq27541_device_info *di = i2c_get_clientdata(client);
  1313.     external_battery_gauge_unregister(&bq27541_batt_gauge);
  1314.     bq27541_information_unregister(&bq27541_batt_gauge);
  1315.     bq27541_cntl_cmd(di, BQ27541_SUBCMD_DISABLE_DLOG);
  1316.     udelay(66);
  1317.     bq27541_cntl_cmd(di, BQ27541_SUBCMD_DISABLE_IT);
  1318.     cancel_delayed_work_sync(&di->hw_config);
  1319.  
  1320.     kfree(di);
  1321.     return 0;
  1322. }
  1323.  
  1324. static int bq27541_battery_suspend(struct i2c_client *client, pm_message_t message)
  1325. {
  1326.     int ret=0;
  1327.     struct bq27541_device_info *di = i2c_get_clientdata(client);
  1328.     cancel_delayed_work_sync(&di->battery_soc_work);
  1329.     atomic_set(&di->suspended, 1);
  1330.     ret = get_current_time(&di->rtc_suspend_time);
  1331.     if (ret ) {
  1332.         pr_err("Failed to read RTC time\n");
  1333.         return 0;
  1334.     }
  1335.     return 0;
  1336. }
  1337.  
  1338. /*1 minute*/
  1339. #define RESUME_TIME  1*60
  1340. static int bq27541_battery_resume(struct i2c_client *client)
  1341. {
  1342.     int ret=0;
  1343.     int suspend_time;
  1344.     struct bq27541_device_info *di = i2c_get_clientdata(client);
  1345.     atomic_set(&di->suspended, 0);
  1346.     ret = get_current_time(&di->rtc_resume_time);
  1347.     if (ret ) {
  1348.         pr_err("Failed to read RTC time\n");
  1349.         return 0;
  1350.     }
  1351.     suspend_time =  di->rtc_resume_time - di->rtc_suspend_time;
  1352.     pr_info("suspend_time=%d\n", suspend_time);
  1353.     update_pre_capacity_data.suspend_time = suspend_time;
  1354.  
  1355.     if (di->rtc_resume_time - di->lcd_off_time >= TWO_POINT_FIVE_MINUTES) {
  1356.         pr_err("di->rtc_resume_time - di->lcd_off_time=%ld\n",
  1357.                 di->rtc_resume_time - di->lcd_off_time);
  1358.         wake_lock(&di->update_soc_wake_lock);
  1359.         get_current_time(&di->lcd_off_time);
  1360.         queue_delayed_work(update_pre_capacity_data.workqueue,
  1361.                 &(update_pre_capacity_data.work), msecs_to_jiffies(1000));
  1362.     }
  1363.     queue_delayed_work(system_power_efficient_wq,
  1364.         &bq27541_di->battery_soc_work,
  1365.         msecs_to_jiffies(RESUME_SCHDULE_SOC_UPDATE_WORK_MS));
  1366.     return 0;
  1367. }
  1368. static void bq27541_shutdown(struct i2c_client *client)
  1369. {
  1370.     struct bq27541_device_info *di = i2c_get_clientdata(client);
  1371.     if(di->soc_pre != DEFAULT_INVALID_SOC_PRE)
  1372.         backup_soc_ex(di->soc_pre);
  1373. }
  1374.  
  1375. static const struct of_device_id bq27541_match[] = {
  1376.     { .compatible = "ti,bq27541-battery" },
  1377.     { },
  1378. };
  1379.  
  1380. static const struct i2c_device_id bq27541_id[] = {
  1381.     { "bq27541-battery", 1 },
  1382.     {},
  1383. };
  1384. MODULE_DEVICE_TABLE(i2c, BQ27541_id);
  1385.  
  1386. static struct i2c_driver bq27541_battery_driver = {
  1387.     .driver     = {
  1388.         .name = "bq27541-battery",
  1389.     },
  1390.     .probe      = bq27541_battery_probe,
  1391.     .remove     = bq27541_battery_remove,
  1392.     .suspend    = bq27541_battery_suspend ,
  1393.     .resume     = bq27541_battery_resume,
  1394.     .shutdown   = bq27541_shutdown,
  1395.     .id_table   = bq27541_id,
  1396. };
  1397.  
  1398. static int __init bq27541_battery_init(void)
  1399. {
  1400.     int ret;
  1401.  
  1402.     ret = i2c_add_driver(&bq27541_battery_driver);
  1403.     if (ret)
  1404.         printk(KERN_ERR "Unable to register BQ27541 driver\n");
  1405.  
  1406.     return ret;
  1407. }
  1408. module_init(bq27541_battery_init);
  1409.  
  1410. static void __exit bq27541_battery_exit(void)
  1411. {
  1412.     i2c_del_driver(&bq27541_battery_driver);
  1413. }
  1414. module_exit(bq27541_battery_exit);
  1415.  
  1416. MODULE_LICENSE("GPL v2");
  1417. MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
  1418. MODULE_DESCRIPTION("BQ27541 battery monitor driver");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement