Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Fuel gauge driver for CellWise 2013 / 2015
0004  *
0005  * Copyright (C) 2012, RockChip
0006  * Copyright (C) 2020, Tobias Schramm
0007  *
0008  * Authors: xuhuicong <xhc@rock-chips.com>
0009  * Authors: Tobias Schramm <t.schramm@manjaro.org>
0010  */
0011 
0012 #include <linux/bits.h>
0013 #include <linux/delay.h>
0014 #include <linux/i2c.h>
0015 #include <linux/gfp.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include <linux/power_supply.h>
0020 #include <linux/property.h>
0021 #include <linux/regmap.h>
0022 #include <linux/time.h>
0023 #include <linux/workqueue.h>
0024 
0025 #define CW2015_SIZE_BATINFO     64
0026 
0027 #define CW2015_RESET_TRIES      5
0028 
0029 #define CW2015_REG_VERSION      0x00
0030 #define CW2015_REG_VCELL        0x02
0031 #define CW2015_REG_SOC          0x04
0032 #define CW2015_REG_RRT_ALERT        0x06
0033 #define CW2015_REG_CONFIG       0x08
0034 #define CW2015_REG_MODE         0x0A
0035 #define CW2015_REG_BATINFO      0x10
0036 
0037 #define CW2015_MODE_SLEEP_MASK      GENMASK(7, 6)
0038 #define CW2015_MODE_SLEEP       (0x03 << 6)
0039 #define CW2015_MODE_NORMAL      (0x00 << 6)
0040 #define CW2015_MODE_QUICK_START     (0x03 << 4)
0041 #define CW2015_MODE_RESTART     (0x0f << 0)
0042 
0043 #define CW2015_CONFIG_UPDATE_FLG    (0x01 << 1)
0044 #define CW2015_ATHD(x)          ((x) << 3)
0045 #define CW2015_MASK_ATHD        GENMASK(7, 3)
0046 #define CW2015_MASK_SOC         GENMASK(12, 0)
0047 
0048 /* reset gauge of no valid state of charge could be polled for 40s */
0049 #define CW2015_BAT_SOC_ERROR_MS     (40 * MSEC_PER_SEC)
0050 /* reset gauge if state of charge stuck for half an hour during charging */
0051 #define CW2015_BAT_CHARGING_STUCK_MS    (1800 * MSEC_PER_SEC)
0052 
0053 /* poll interval from CellWise GPL Android driver example */
0054 #define CW2015_DEFAULT_POLL_INTERVAL_MS     8000
0055 
0056 #define CW2015_AVERAGING_SAMPLES        3
0057 
0058 struct cw_battery {
0059     struct device *dev;
0060     struct workqueue_struct *battery_workqueue;
0061     struct delayed_work battery_delay_work;
0062     struct regmap *regmap;
0063     struct power_supply *rk_bat;
0064     struct power_supply_battery_info *battery;
0065     u8 *bat_profile;
0066 
0067     bool charger_attached;
0068     bool battery_changed;
0069 
0070     int soc;
0071     int voltage_mv;
0072     int status;
0073     int time_to_empty;
0074     int charge_count;
0075 
0076     u32 poll_interval_ms;
0077     u8 alert_level;
0078 
0079     unsigned int read_errors;
0080     unsigned int charge_stuck_cnt;
0081 };
0082 
0083 static int cw_read_word(struct cw_battery *cw_bat, u8 reg, u16 *val)
0084 {
0085     __be16 value;
0086     int ret;
0087 
0088     ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value));
0089     if (ret)
0090         return ret;
0091 
0092     *val = be16_to_cpu(value);
0093     return 0;
0094 }
0095 
0096 static int cw_update_profile(struct cw_battery *cw_bat)
0097 {
0098     int ret;
0099     unsigned int reg_val;
0100     u8 reset_val;
0101 
0102     /* make sure gauge is not in sleep mode */
0103     ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, &reg_val);
0104     if (ret)
0105         return ret;
0106 
0107     reset_val = reg_val;
0108     if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
0109         dev_err(cw_bat->dev,
0110             "Gauge is in sleep mode, can't update battery info\n");
0111         return -EINVAL;
0112     }
0113 
0114     /* write new battery info */
0115     ret = regmap_raw_write(cw_bat->regmap, CW2015_REG_BATINFO,
0116                    cw_bat->bat_profile,
0117                    CW2015_SIZE_BATINFO);
0118     if (ret)
0119         return ret;
0120 
0121     /* set config update flag  */
0122     reg_val |= CW2015_CONFIG_UPDATE_FLG;
0123     reg_val &= ~CW2015_MASK_ATHD;
0124     reg_val |= CW2015_ATHD(cw_bat->alert_level);
0125     ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
0126     if (ret)
0127         return ret;
0128 
0129     /* reset gauge to apply new battery profile */
0130     reset_val &= ~CW2015_MODE_RESTART;
0131     reg_val = reset_val | CW2015_MODE_RESTART;
0132     ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
0133     if (ret)
0134         return ret;
0135 
0136     /* wait for gauge to reset */
0137     msleep(20);
0138 
0139     /* clear reset flag */
0140     ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
0141     if (ret)
0142         return ret;
0143 
0144     /* wait for gauge to become ready */
0145     ret = regmap_read_poll_timeout(cw_bat->regmap, CW2015_REG_SOC,
0146                        reg_val, reg_val <= 100,
0147                        10 * USEC_PER_MSEC, 10 * USEC_PER_SEC);
0148     if (ret)
0149         dev_err(cw_bat->dev,
0150             "Gauge did not become ready after profile upload\n");
0151     else
0152         dev_dbg(cw_bat->dev, "Battery profile updated\n");
0153 
0154     return ret;
0155 }
0156 
0157 static int cw_init(struct cw_battery *cw_bat)
0158 {
0159     int ret;
0160     unsigned int reg_val = CW2015_MODE_SLEEP;
0161 
0162     if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
0163         reg_val = CW2015_MODE_NORMAL;
0164         ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
0165         if (ret)
0166             return ret;
0167     }
0168 
0169     ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, &reg_val);
0170     if (ret)
0171         return ret;
0172 
0173     if ((reg_val & CW2015_MASK_ATHD) != CW2015_ATHD(cw_bat->alert_level)) {
0174         dev_dbg(cw_bat->dev, "Setting new alert level\n");
0175         reg_val &= ~CW2015_MASK_ATHD;
0176         reg_val |= ~CW2015_ATHD(cw_bat->alert_level);
0177         ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
0178         if (ret)
0179             return ret;
0180     }
0181 
0182     ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, &reg_val);
0183     if (ret)
0184         return ret;
0185 
0186     if (!(reg_val & CW2015_CONFIG_UPDATE_FLG)) {
0187         dev_dbg(cw_bat->dev,
0188             "Battery profile not present, uploading battery profile\n");
0189         if (cw_bat->bat_profile) {
0190             ret = cw_update_profile(cw_bat);
0191             if (ret) {
0192                 dev_err(cw_bat->dev,
0193                     "Failed to upload battery profile\n");
0194                 return ret;
0195             }
0196         } else {
0197             dev_warn(cw_bat->dev,
0198                  "No profile specified, continuing without profile\n");
0199         }
0200     } else if (cw_bat->bat_profile) {
0201         u8 bat_info[CW2015_SIZE_BATINFO];
0202 
0203         ret = regmap_raw_read(cw_bat->regmap, CW2015_REG_BATINFO,
0204                       bat_info, CW2015_SIZE_BATINFO);
0205         if (ret) {
0206             dev_err(cw_bat->dev,
0207                 "Failed to read stored battery profile\n");
0208             return ret;
0209         }
0210 
0211         if (memcmp(bat_info, cw_bat->bat_profile, CW2015_SIZE_BATINFO)) {
0212             dev_warn(cw_bat->dev, "Replacing stored battery profile\n");
0213             ret = cw_update_profile(cw_bat);
0214             if (ret)
0215                 return ret;
0216         }
0217     } else {
0218         dev_warn(cw_bat->dev,
0219              "Can't check current battery profile, no profile provided\n");
0220     }
0221 
0222     dev_dbg(cw_bat->dev, "Battery profile configured\n");
0223     return 0;
0224 }
0225 
0226 static int cw_power_on_reset(struct cw_battery *cw_bat)
0227 {
0228     int ret;
0229     unsigned char reset_val;
0230 
0231     reset_val = CW2015_MODE_SLEEP;
0232     ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
0233     if (ret)
0234         return ret;
0235 
0236     /* wait for gauge to enter sleep */
0237     msleep(20);
0238 
0239     reset_val = CW2015_MODE_NORMAL;
0240     ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
0241     if (ret)
0242         return ret;
0243 
0244     ret = cw_init(cw_bat);
0245     if (ret)
0246         return ret;
0247     return 0;
0248 }
0249 
0250 #define HYSTERESIS(current, previous, up, down) \
0251     (((current) < (previous) + (up)) && ((current) > (previous) - (down)))
0252 
0253 static int cw_get_soc(struct cw_battery *cw_bat)
0254 {
0255     unsigned int soc;
0256     int ret;
0257 
0258     ret = regmap_read(cw_bat->regmap, CW2015_REG_SOC, &soc);
0259     if (ret)
0260         return ret;
0261 
0262     if (soc > 100) {
0263         int max_error_cycles =
0264             CW2015_BAT_SOC_ERROR_MS / cw_bat->poll_interval_ms;
0265 
0266         dev_err(cw_bat->dev, "Invalid SoC %d%%\n", soc);
0267         cw_bat->read_errors++;
0268         if (cw_bat->read_errors > max_error_cycles) {
0269             dev_warn(cw_bat->dev,
0270                  "Too many invalid SoC reports, resetting gauge\n");
0271             cw_power_on_reset(cw_bat);
0272             cw_bat->read_errors = 0;
0273         }
0274         return cw_bat->soc;
0275     }
0276     cw_bat->read_errors = 0;
0277 
0278     /* Reset gauge if stuck while charging */
0279     if (cw_bat->status == POWER_SUPPLY_STATUS_CHARGING && soc == cw_bat->soc) {
0280         int max_stuck_cycles =
0281             CW2015_BAT_CHARGING_STUCK_MS / cw_bat->poll_interval_ms;
0282 
0283         cw_bat->charge_stuck_cnt++;
0284         if (cw_bat->charge_stuck_cnt > max_stuck_cycles) {
0285             dev_warn(cw_bat->dev,
0286                  "SoC stuck @%u%%, resetting gauge\n", soc);
0287             cw_power_on_reset(cw_bat);
0288             cw_bat->charge_stuck_cnt = 0;
0289         }
0290     } else {
0291         cw_bat->charge_stuck_cnt = 0;
0292     }
0293 
0294     /* Ignore voltage dips during charge */
0295     if (cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 0, 3))
0296         soc = cw_bat->soc;
0297 
0298     /* Ignore voltage spikes during discharge */
0299     if (!cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 3, 0))
0300         soc = cw_bat->soc;
0301 
0302     return soc;
0303 }
0304 
0305 static int cw_get_voltage(struct cw_battery *cw_bat)
0306 {
0307     int ret, i, voltage_mv;
0308     u16 reg_val;
0309     u32 avg = 0;
0310 
0311     for (i = 0; i < CW2015_AVERAGING_SAMPLES; i++) {
0312         ret = cw_read_word(cw_bat, CW2015_REG_VCELL, &reg_val);
0313         if (ret)
0314             return ret;
0315 
0316         avg += reg_val;
0317     }
0318     avg /= CW2015_AVERAGING_SAMPLES;
0319 
0320     /*
0321      * 305 uV per ADC step
0322      * Use 312 / 1024  as efficient approximation of 305 / 1000
0323      * Negligible error of 0.1%
0324      */
0325     voltage_mv = avg * 312 / 1024;
0326 
0327     dev_dbg(cw_bat->dev, "Read voltage: %d mV, raw=0x%04x\n",
0328         voltage_mv, reg_val);
0329     return voltage_mv;
0330 }
0331 
0332 static int cw_get_time_to_empty(struct cw_battery *cw_bat)
0333 {
0334     int ret;
0335     u16 value16;
0336 
0337     ret = cw_read_word(cw_bat, CW2015_REG_RRT_ALERT, &value16);
0338     if (ret)
0339         return ret;
0340 
0341     return value16 & CW2015_MASK_SOC;
0342 }
0343 
0344 static void cw_update_charge_status(struct cw_battery *cw_bat)
0345 {
0346     int ret;
0347 
0348     ret = power_supply_am_i_supplied(cw_bat->rk_bat);
0349     if (ret < 0) {
0350         dev_warn(cw_bat->dev, "Failed to get supply state: %d\n", ret);
0351     } else {
0352         bool charger_attached;
0353 
0354         charger_attached = !!ret;
0355         if (cw_bat->charger_attached != charger_attached) {
0356             cw_bat->battery_changed = true;
0357             if (charger_attached)
0358                 cw_bat->charge_count++;
0359         }
0360         cw_bat->charger_attached = charger_attached;
0361     }
0362 }
0363 
0364 static void cw_update_soc(struct cw_battery *cw_bat)
0365 {
0366     int soc;
0367 
0368     soc = cw_get_soc(cw_bat);
0369     if (soc < 0)
0370         dev_err(cw_bat->dev, "Failed to get SoC from gauge: %d\n", soc);
0371     else if (cw_bat->soc != soc) {
0372         cw_bat->soc = soc;
0373         cw_bat->battery_changed = true;
0374     }
0375 }
0376 
0377 static void cw_update_voltage(struct cw_battery *cw_bat)
0378 {
0379     int voltage_mv;
0380 
0381     voltage_mv = cw_get_voltage(cw_bat);
0382     if (voltage_mv < 0)
0383         dev_err(cw_bat->dev, "Failed to get voltage from gauge: %d\n",
0384             voltage_mv);
0385     else
0386         cw_bat->voltage_mv = voltage_mv;
0387 }
0388 
0389 static void cw_update_status(struct cw_battery *cw_bat)
0390 {
0391     int status = POWER_SUPPLY_STATUS_DISCHARGING;
0392 
0393     if (cw_bat->charger_attached) {
0394         if (cw_bat->soc >= 100)
0395             status = POWER_SUPPLY_STATUS_FULL;
0396         else
0397             status = POWER_SUPPLY_STATUS_CHARGING;
0398     }
0399 
0400     if (cw_bat->status != status)
0401         cw_bat->battery_changed = true;
0402     cw_bat->status = status;
0403 }
0404 
0405 static void cw_update_time_to_empty(struct cw_battery *cw_bat)
0406 {
0407     int time_to_empty;
0408 
0409     time_to_empty = cw_get_time_to_empty(cw_bat);
0410     if (time_to_empty < 0)
0411         dev_err(cw_bat->dev, "Failed to get time to empty from gauge: %d\n",
0412             time_to_empty);
0413     else if (cw_bat->time_to_empty != time_to_empty) {
0414         cw_bat->time_to_empty = time_to_empty;
0415         cw_bat->battery_changed = true;
0416     }
0417 }
0418 
0419 static void cw_bat_work(struct work_struct *work)
0420 {
0421     struct delayed_work *delay_work;
0422     struct cw_battery *cw_bat;
0423     int ret;
0424     unsigned int reg_val;
0425 
0426     delay_work = to_delayed_work(work);
0427     cw_bat = container_of(delay_work, struct cw_battery, battery_delay_work);
0428     ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, &reg_val);
0429     if (ret) {
0430         dev_err(cw_bat->dev, "Failed to read mode from gauge: %d\n", ret);
0431     } else {
0432         if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
0433             int i;
0434 
0435             for (i = 0; i < CW2015_RESET_TRIES; i++) {
0436                 if (!cw_power_on_reset(cw_bat))
0437                     break;
0438             }
0439         }
0440         cw_update_soc(cw_bat);
0441         cw_update_voltage(cw_bat);
0442         cw_update_charge_status(cw_bat);
0443         cw_update_status(cw_bat);
0444         cw_update_time_to_empty(cw_bat);
0445     }
0446     dev_dbg(cw_bat->dev, "charger_attached = %d\n", cw_bat->charger_attached);
0447     dev_dbg(cw_bat->dev, "status = %d\n", cw_bat->status);
0448     dev_dbg(cw_bat->dev, "soc = %d%%\n", cw_bat->soc);
0449     dev_dbg(cw_bat->dev, "voltage = %dmV\n", cw_bat->voltage_mv);
0450 
0451     if (cw_bat->battery_changed)
0452         power_supply_changed(cw_bat->rk_bat);
0453     cw_bat->battery_changed = false;
0454 
0455     queue_delayed_work(cw_bat->battery_workqueue,
0456                &cw_bat->battery_delay_work,
0457                msecs_to_jiffies(cw_bat->poll_interval_ms));
0458 }
0459 
0460 static bool cw_battery_valid_time_to_empty(struct cw_battery *cw_bat)
0461 {
0462     return  cw_bat->time_to_empty > 0 &&
0463         cw_bat->time_to_empty < CW2015_MASK_SOC &&
0464         cw_bat->status == POWER_SUPPLY_STATUS_DISCHARGING;
0465 }
0466 
0467 static int cw_battery_get_property(struct power_supply *psy,
0468                    enum power_supply_property psp,
0469                    union power_supply_propval *val)
0470 {
0471     struct cw_battery *cw_bat;
0472 
0473     cw_bat = power_supply_get_drvdata(psy);
0474     switch (psp) {
0475     case POWER_SUPPLY_PROP_CAPACITY:
0476         val->intval = cw_bat->soc;
0477         break;
0478 
0479     case POWER_SUPPLY_PROP_STATUS:
0480         val->intval = cw_bat->status;
0481         break;
0482 
0483     case POWER_SUPPLY_PROP_PRESENT:
0484         val->intval = !!cw_bat->voltage_mv;
0485         break;
0486 
0487     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0488         val->intval = cw_bat->voltage_mv * 1000;
0489         break;
0490 
0491     case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
0492         if (cw_battery_valid_time_to_empty(cw_bat))
0493             val->intval = cw_bat->time_to_empty;
0494         else
0495             val->intval = 0;
0496         break;
0497 
0498     case POWER_SUPPLY_PROP_TECHNOLOGY:
0499         val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
0500         break;
0501 
0502     case POWER_SUPPLY_PROP_CHARGE_COUNTER:
0503         val->intval = cw_bat->charge_count;
0504         break;
0505 
0506     case POWER_SUPPLY_PROP_CHARGE_FULL:
0507     case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0508         if (cw_bat->battery->charge_full_design_uah > 0)
0509             val->intval = cw_bat->battery->charge_full_design_uah;
0510         else
0511             val->intval = 0;
0512         break;
0513 
0514     case POWER_SUPPLY_PROP_CHARGE_NOW:
0515         val->intval = cw_bat->battery->charge_full_design_uah;
0516         val->intval = val->intval * cw_bat->soc / 100;
0517         break;
0518 
0519     case POWER_SUPPLY_PROP_CURRENT_NOW:
0520         if (cw_battery_valid_time_to_empty(cw_bat) &&
0521             cw_bat->battery->charge_full_design_uah > 0) {
0522             /* calculate remaining capacity */
0523             val->intval = cw_bat->battery->charge_full_design_uah;
0524             val->intval = val->intval * cw_bat->soc / 100;
0525 
0526             /* estimate current based on time to empty */
0527             val->intval = 60 * val->intval / cw_bat->time_to_empty;
0528         } else {
0529             val->intval = 0;
0530         }
0531 
0532         break;
0533 
0534     default:
0535         break;
0536     }
0537     return 0;
0538 }
0539 
0540 static enum power_supply_property cw_battery_properties[] = {
0541     POWER_SUPPLY_PROP_CAPACITY,
0542     POWER_SUPPLY_PROP_STATUS,
0543     POWER_SUPPLY_PROP_PRESENT,
0544     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0545     POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
0546     POWER_SUPPLY_PROP_TECHNOLOGY,
0547     POWER_SUPPLY_PROP_CHARGE_COUNTER,
0548     POWER_SUPPLY_PROP_CHARGE_FULL,
0549     POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0550     POWER_SUPPLY_PROP_CHARGE_NOW,
0551     POWER_SUPPLY_PROP_CURRENT_NOW,
0552 };
0553 
0554 static const struct power_supply_desc cw2015_bat_desc = {
0555     .name       = "cw2015-battery",
0556     .type       = POWER_SUPPLY_TYPE_BATTERY,
0557     .properties = cw_battery_properties,
0558     .num_properties = ARRAY_SIZE(cw_battery_properties),
0559     .get_property   = cw_battery_get_property,
0560 };
0561 
0562 static int cw2015_parse_properties(struct cw_battery *cw_bat)
0563 {
0564     struct device *dev = cw_bat->dev;
0565     int length;
0566     int ret;
0567 
0568     length = device_property_count_u8(dev, "cellwise,battery-profile");
0569     if (length < 0) {
0570         dev_warn(cw_bat->dev,
0571              "No battery-profile found, using current flash contents\n");
0572     } else if (length != CW2015_SIZE_BATINFO) {
0573         dev_err(cw_bat->dev, "battery-profile must be %d bytes\n",
0574             CW2015_SIZE_BATINFO);
0575         return -EINVAL;
0576     } else {
0577         cw_bat->bat_profile = devm_kzalloc(dev, length, GFP_KERNEL);
0578         if (!cw_bat->bat_profile)
0579             return -ENOMEM;
0580 
0581         ret = device_property_read_u8_array(dev,
0582                         "cellwise,battery-profile",
0583                         cw_bat->bat_profile,
0584                         length);
0585         if (ret)
0586             return ret;
0587     }
0588 
0589     ret = device_property_read_u32(dev, "cellwise,monitor-interval-ms",
0590                        &cw_bat->poll_interval_ms);
0591     if (ret) {
0592         dev_dbg(cw_bat->dev, "Using default poll interval\n");
0593         cw_bat->poll_interval_ms = CW2015_DEFAULT_POLL_INTERVAL_MS;
0594     }
0595 
0596     return 0;
0597 }
0598 
0599 static const struct regmap_range regmap_ranges_rd_yes[] = {
0600     regmap_reg_range(CW2015_REG_VERSION, CW2015_REG_VERSION),
0601     regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_CONFIG),
0602     regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE),
0603     regmap_reg_range(CW2015_REG_BATINFO,
0604             CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
0605 };
0606 
0607 static const struct regmap_access_table regmap_rd_table = {
0608     .yes_ranges = regmap_ranges_rd_yes,
0609     .n_yes_ranges = 4,
0610 };
0611 
0612 static const struct regmap_range regmap_ranges_wr_yes[] = {
0613     regmap_reg_range(CW2015_REG_RRT_ALERT, CW2015_REG_CONFIG),
0614     regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE),
0615     regmap_reg_range(CW2015_REG_BATINFO,
0616             CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
0617 };
0618 
0619 static const struct regmap_access_table regmap_wr_table = {
0620     .yes_ranges = regmap_ranges_wr_yes,
0621     .n_yes_ranges = 3,
0622 };
0623 
0624 static const struct regmap_range regmap_ranges_vol_yes[] = {
0625     regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_SOC + 1),
0626 };
0627 
0628 static const struct regmap_access_table regmap_vol_table = {
0629     .yes_ranges = regmap_ranges_vol_yes,
0630     .n_yes_ranges = 1,
0631 };
0632 
0633 static const struct regmap_config cw2015_regmap_config = {
0634     .reg_bits = 8,
0635     .val_bits = 8,
0636     .rd_table = &regmap_rd_table,
0637     .wr_table = &regmap_wr_table,
0638     .volatile_table = &regmap_vol_table,
0639     .max_register = CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1,
0640 };
0641 
0642 static int cw_bat_probe(struct i2c_client *client)
0643 {
0644     int ret;
0645     struct cw_battery *cw_bat;
0646     struct power_supply_config psy_cfg = { 0 };
0647 
0648     cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL);
0649     if (!cw_bat)
0650         return -ENOMEM;
0651 
0652     i2c_set_clientdata(client, cw_bat);
0653     cw_bat->dev = &client->dev;
0654     cw_bat->soc = 1;
0655 
0656     ret = cw2015_parse_properties(cw_bat);
0657     if (ret) {
0658         dev_err(cw_bat->dev, "Failed to parse cw2015 properties\n");
0659         return ret;
0660     }
0661 
0662     cw_bat->regmap = devm_regmap_init_i2c(client, &cw2015_regmap_config);
0663     if (IS_ERR(cw_bat->regmap)) {
0664         dev_err(cw_bat->dev, "Failed to allocate regmap: %ld\n",
0665             PTR_ERR(cw_bat->regmap));
0666         return PTR_ERR(cw_bat->regmap);
0667     }
0668 
0669     ret = cw_init(cw_bat);
0670     if (ret) {
0671         dev_err(cw_bat->dev, "Init failed: %d\n", ret);
0672         return ret;
0673     }
0674 
0675     psy_cfg.drv_data = cw_bat;
0676     psy_cfg.fwnode = dev_fwnode(cw_bat->dev);
0677 
0678     cw_bat->rk_bat = devm_power_supply_register(&client->dev,
0679                             &cw2015_bat_desc,
0680                             &psy_cfg);
0681     if (IS_ERR(cw_bat->rk_bat)) {
0682         /* try again if this happens */
0683         dev_err_probe(&client->dev, PTR_ERR(cw_bat->rk_bat),
0684             "Failed to register power supply\n");
0685         return PTR_ERR(cw_bat->rk_bat);
0686     }
0687 
0688     ret = power_supply_get_battery_info(cw_bat->rk_bat, &cw_bat->battery);
0689     if (ret) {
0690         /* Allocate an empty battery */
0691         cw_bat->battery = devm_kzalloc(&client->dev,
0692                            sizeof(*cw_bat->battery),
0693                            GFP_KERNEL);
0694         if (!cw_bat->battery)
0695             return -ENOMEM;
0696         dev_warn(cw_bat->dev,
0697              "No monitored battery, some properties will be missing\n");
0698     }
0699 
0700     cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery");
0701     INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work);
0702     queue_delayed_work(cw_bat->battery_workqueue,
0703                &cw_bat->battery_delay_work, msecs_to_jiffies(10));
0704     return 0;
0705 }
0706 
0707 static int __maybe_unused cw_bat_suspend(struct device *dev)
0708 {
0709     struct i2c_client *client = to_i2c_client(dev);
0710     struct cw_battery *cw_bat = i2c_get_clientdata(client);
0711 
0712     cancel_delayed_work_sync(&cw_bat->battery_delay_work);
0713     return 0;
0714 }
0715 
0716 static int __maybe_unused cw_bat_resume(struct device *dev)
0717 {
0718     struct i2c_client *client = to_i2c_client(dev);
0719     struct cw_battery *cw_bat = i2c_get_clientdata(client);
0720 
0721     queue_delayed_work(cw_bat->battery_workqueue,
0722                &cw_bat->battery_delay_work, 0);
0723     return 0;
0724 }
0725 
0726 static SIMPLE_DEV_PM_OPS(cw_bat_pm_ops, cw_bat_suspend, cw_bat_resume);
0727 
0728 static int cw_bat_remove(struct i2c_client *client)
0729 {
0730     struct cw_battery *cw_bat = i2c_get_clientdata(client);
0731 
0732     cancel_delayed_work_sync(&cw_bat->battery_delay_work);
0733     power_supply_put_battery_info(cw_bat->rk_bat, cw_bat->battery);
0734     return 0;
0735 }
0736 
0737 static const struct i2c_device_id cw_bat_id_table[] = {
0738     { "cw2015", 0 },
0739     { }
0740 };
0741 
0742 static const struct of_device_id cw2015_of_match[] = {
0743     { .compatible = "cellwise,cw2015" },
0744     { }
0745 };
0746 MODULE_DEVICE_TABLE(of, cw2015_of_match);
0747 
0748 static struct i2c_driver cw_bat_driver = {
0749     .driver = {
0750         .name = "cw2015",
0751         .of_match_table = cw2015_of_match,
0752         .pm = &cw_bat_pm_ops,
0753     },
0754     .probe_new = cw_bat_probe,
0755     .remove = cw_bat_remove,
0756     .id_table = cw_bat_id_table,
0757 };
0758 
0759 module_i2c_driver(cw_bat_driver);
0760 
0761 MODULE_AUTHOR("xhc<xhc@rock-chips.com>");
0762 MODULE_AUTHOR("Tobias Schramm <t.schramm@manjaro.org>");
0763 MODULE_DESCRIPTION("cw2015/cw2013 battery driver");
0764 MODULE_LICENSE("GPL");