Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / power / supply / cw2015_battery.c
blob0146f1bfc29bb5b4ec1e61c2eb76b3255b525c6b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Fuel gauge driver for CellWise 2013 / 2015
5 * Copyright (C) 2012, RockChip
6 * Copyright (C) 2020, Tobias Schramm
8 * Authors: xuhuicong <xhc@rock-chips.com>
9 * Authors: Tobias Schramm <t.schramm@manjaro.org>
12 #include <linux/bits.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/gfp.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/power_supply.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
22 #include <linux/time.h>
23 #include <linux/workqueue.h>
25 #define CW2015_SIZE_BATINFO 64
27 #define CW2015_RESET_TRIES 5
29 #define CW2015_REG_VERSION 0x00
30 #define CW2015_REG_VCELL 0x02
31 #define CW2015_REG_SOC 0x04
32 #define CW2015_REG_RRT_ALERT 0x06
33 #define CW2015_REG_CONFIG 0x08
34 #define CW2015_REG_MODE 0x0A
35 #define CW2015_REG_BATINFO 0x10
37 #define CW2015_MODE_SLEEP_MASK GENMASK(7, 6)
38 #define CW2015_MODE_SLEEP (0x03 << 6)
39 #define CW2015_MODE_NORMAL (0x00 << 6)
40 #define CW2015_MODE_QUICK_START (0x03 << 4)
41 #define CW2015_MODE_RESTART (0x0f << 0)
43 #define CW2015_CONFIG_UPDATE_FLG (0x01 << 1)
44 #define CW2015_ATHD(x) ((x) << 3)
45 #define CW2015_MASK_ATHD GENMASK(7, 3)
46 #define CW2015_MASK_SOC GENMASK(12, 0)
48 /* reset gauge of no valid state of charge could be polled for 40s */
49 #define CW2015_BAT_SOC_ERROR_MS (40 * MSEC_PER_SEC)
50 /* reset gauge if state of charge stuck for half an hour during charging */
51 #define CW2015_BAT_CHARGING_STUCK_MS (1800 * MSEC_PER_SEC)
53 /* poll interval from CellWise GPL Android driver example */
54 #define CW2015_DEFAULT_POLL_INTERVAL_MS 8000
56 #define CW2015_AVERAGING_SAMPLES 3
58 struct cw_battery {
59 struct device *dev;
60 struct workqueue_struct *battery_workqueue;
61 struct delayed_work battery_delay_work;
62 struct regmap *regmap;
63 struct power_supply *rk_bat;
64 struct power_supply_battery_info battery;
65 u8 *bat_profile;
67 bool charger_attached;
68 bool battery_changed;
70 int soc;
71 int voltage_mv;
72 int status;
73 int time_to_empty;
74 int charge_count;
76 u32 poll_interval_ms;
77 u8 alert_level;
79 unsigned int read_errors;
80 unsigned int charge_stuck_cnt;
83 static int cw_read_word(struct cw_battery *cw_bat, u8 reg, u16 *val)
85 __be16 value;
86 int ret;
88 ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value));
89 if (ret)
90 return ret;
92 *val = be16_to_cpu(value);
93 return 0;
96 static int cw_update_profile(struct cw_battery *cw_bat)
98 int ret;
99 unsigned int reg_val;
100 u8 reset_val;
102 /* make sure gauge is not in sleep mode */
103 ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, &reg_val);
104 if (ret)
105 return ret;
107 reset_val = reg_val;
108 if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
109 dev_err(cw_bat->dev,
110 "Gauge is in sleep mode, can't update battery info\n");
111 return -EINVAL;
114 /* write new battery info */
115 ret = regmap_raw_write(cw_bat->regmap, CW2015_REG_BATINFO,
116 cw_bat->bat_profile,
117 CW2015_SIZE_BATINFO);
118 if (ret)
119 return ret;
121 /* set config update flag */
122 reg_val |= CW2015_CONFIG_UPDATE_FLG;
123 reg_val &= ~CW2015_MASK_ATHD;
124 reg_val |= CW2015_ATHD(cw_bat->alert_level);
125 ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
126 if (ret)
127 return ret;
129 /* reset gauge to apply new battery profile */
130 reset_val &= ~CW2015_MODE_RESTART;
131 reg_val = reset_val | CW2015_MODE_RESTART;
132 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
133 if (ret)
134 return ret;
136 /* wait for gauge to reset */
137 msleep(20);
139 /* clear reset flag */
140 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
141 if (ret)
142 return ret;
144 /* wait for gauge to become ready */
145 ret = regmap_read_poll_timeout(cw_bat->regmap, CW2015_REG_SOC,
146 reg_val, reg_val <= 100,
147 10 * USEC_PER_MSEC, 10 * USEC_PER_SEC);
148 if (ret)
149 dev_err(cw_bat->dev,
150 "Gauge did not become ready after profile upload\n");
151 else
152 dev_dbg(cw_bat->dev, "Battery profile updated\n");
154 return ret;
157 static int cw_init(struct cw_battery *cw_bat)
159 int ret;
160 unsigned int reg_val = CW2015_MODE_SLEEP;
162 if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
163 reg_val = CW2015_MODE_NORMAL;
164 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
165 if (ret)
166 return ret;
169 ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, &reg_val);
170 if (ret)
171 return ret;
173 if ((reg_val & CW2015_MASK_ATHD) != CW2015_ATHD(cw_bat->alert_level)) {
174 dev_dbg(cw_bat->dev, "Setting new alert level\n");
175 reg_val &= ~CW2015_MASK_ATHD;
176 reg_val |= ~CW2015_ATHD(cw_bat->alert_level);
177 ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
178 if (ret)
179 return ret;
182 ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, &reg_val);
183 if (ret)
184 return ret;
186 if (!(reg_val & CW2015_CONFIG_UPDATE_FLG)) {
187 dev_dbg(cw_bat->dev,
188 "Battery profile not present, uploading battery profile\n");
189 if (cw_bat->bat_profile) {
190 ret = cw_update_profile(cw_bat);
191 if (ret) {
192 dev_err(cw_bat->dev,
193 "Failed to upload battery profile\n");
194 return ret;
196 } else {
197 dev_warn(cw_bat->dev,
198 "No profile specified, continuing without profile\n");
200 } else if (cw_bat->bat_profile) {
201 u8 bat_info[CW2015_SIZE_BATINFO];
203 ret = regmap_raw_read(cw_bat->regmap, CW2015_REG_BATINFO,
204 bat_info, CW2015_SIZE_BATINFO);
205 if (ret) {
206 dev_err(cw_bat->dev,
207 "Failed to read stored battery profile\n");
208 return ret;
211 if (memcmp(bat_info, cw_bat->bat_profile, CW2015_SIZE_BATINFO)) {
212 dev_warn(cw_bat->dev, "Replacing stored battery profile\n");
213 ret = cw_update_profile(cw_bat);
214 if (ret)
215 return ret;
217 } else {
218 dev_warn(cw_bat->dev,
219 "Can't check current battery profile, no profile provided\n");
222 dev_dbg(cw_bat->dev, "Battery profile configured\n");
223 return 0;
226 static int cw_power_on_reset(struct cw_battery *cw_bat)
228 int ret;
229 unsigned char reset_val;
231 reset_val = CW2015_MODE_SLEEP;
232 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
233 if (ret)
234 return ret;
236 /* wait for gauge to enter sleep */
237 msleep(20);
239 reset_val = CW2015_MODE_NORMAL;
240 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
241 if (ret)
242 return ret;
244 ret = cw_init(cw_bat);
245 if (ret)
246 return ret;
247 return 0;
250 #define HYSTERESIS(current, previous, up, down) \
251 (((current) < (previous) + (up)) && ((current) > (previous) - (down)))
253 static int cw_get_soc(struct cw_battery *cw_bat)
255 unsigned int soc;
256 int ret;
258 ret = regmap_read(cw_bat->regmap, CW2015_REG_SOC, &soc);
259 if (ret)
260 return ret;
262 if (soc > 100) {
263 int max_error_cycles =
264 CW2015_BAT_SOC_ERROR_MS / cw_bat->poll_interval_ms;
266 dev_err(cw_bat->dev, "Invalid SoC %d%%\n", soc);
267 cw_bat->read_errors++;
268 if (cw_bat->read_errors > max_error_cycles) {
269 dev_warn(cw_bat->dev,
270 "Too many invalid SoC reports, resetting gauge\n");
271 cw_power_on_reset(cw_bat);
272 cw_bat->read_errors = 0;
274 return cw_bat->soc;
276 cw_bat->read_errors = 0;
278 /* Reset gauge if stuck while charging */
279 if (cw_bat->status == POWER_SUPPLY_STATUS_CHARGING && soc == cw_bat->soc) {
280 int max_stuck_cycles =
281 CW2015_BAT_CHARGING_STUCK_MS / cw_bat->poll_interval_ms;
283 cw_bat->charge_stuck_cnt++;
284 if (cw_bat->charge_stuck_cnt > max_stuck_cycles) {
285 dev_warn(cw_bat->dev,
286 "SoC stuck @%u%%, resetting gauge\n", soc);
287 cw_power_on_reset(cw_bat);
288 cw_bat->charge_stuck_cnt = 0;
290 } else {
291 cw_bat->charge_stuck_cnt = 0;
294 /* Ignore voltage dips during charge */
295 if (cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 0, 3))
296 soc = cw_bat->soc;
298 /* Ignore voltage spikes during discharge */
299 if (!cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 3, 0))
300 soc = cw_bat->soc;
302 return soc;
305 static int cw_get_voltage(struct cw_battery *cw_bat)
307 int ret, i, voltage_mv;
308 u16 reg_val;
309 u32 avg = 0;
311 for (i = 0; i < CW2015_AVERAGING_SAMPLES; i++) {
312 ret = cw_read_word(cw_bat, CW2015_REG_VCELL, &reg_val);
313 if (ret)
314 return ret;
316 avg += reg_val;
318 avg /= CW2015_AVERAGING_SAMPLES;
321 * 305 uV per ADC step
322 * Use 312 / 1024 as efficient approximation of 305 / 1000
323 * Negligible error of 0.1%
325 voltage_mv = avg * 312 / 1024;
327 dev_dbg(cw_bat->dev, "Read voltage: %d mV, raw=0x%04x\n",
328 voltage_mv, reg_val);
329 return voltage_mv;
332 static int cw_get_time_to_empty(struct cw_battery *cw_bat)
334 int ret;
335 u16 value16;
337 ret = cw_read_word(cw_bat, CW2015_REG_RRT_ALERT, &value16);
338 if (ret)
339 return ret;
341 return value16 & CW2015_MASK_SOC;
344 static void cw_update_charge_status(struct cw_battery *cw_bat)
346 int ret;
348 ret = power_supply_am_i_supplied(cw_bat->rk_bat);
349 if (ret < 0) {
350 dev_warn(cw_bat->dev, "Failed to get supply state: %d\n", ret);
351 } else {
352 bool charger_attached;
354 charger_attached = !!ret;
355 if (cw_bat->charger_attached != charger_attached) {
356 cw_bat->battery_changed = true;
357 if (charger_attached)
358 cw_bat->charge_count++;
360 cw_bat->charger_attached = charger_attached;
364 static void cw_update_soc(struct cw_battery *cw_bat)
366 int soc;
368 soc = cw_get_soc(cw_bat);
369 if (soc < 0)
370 dev_err(cw_bat->dev, "Failed to get SoC from gauge: %d\n", soc);
371 else if (cw_bat->soc != soc) {
372 cw_bat->soc = soc;
373 cw_bat->battery_changed = true;
377 static void cw_update_voltage(struct cw_battery *cw_bat)
379 int voltage_mv;
381 voltage_mv = cw_get_voltage(cw_bat);
382 if (voltage_mv < 0)
383 dev_err(cw_bat->dev, "Failed to get voltage from gauge: %d\n",
384 voltage_mv);
385 else
386 cw_bat->voltage_mv = voltage_mv;
389 static void cw_update_status(struct cw_battery *cw_bat)
391 int status = POWER_SUPPLY_STATUS_DISCHARGING;
393 if (cw_bat->charger_attached) {
394 if (cw_bat->soc >= 100)
395 status = POWER_SUPPLY_STATUS_FULL;
396 else
397 status = POWER_SUPPLY_STATUS_CHARGING;
400 if (cw_bat->status != status)
401 cw_bat->battery_changed = true;
402 cw_bat->status = status;
405 static void cw_update_time_to_empty(struct cw_battery *cw_bat)
407 int time_to_empty;
409 time_to_empty = cw_get_time_to_empty(cw_bat);
410 if (time_to_empty < 0)
411 dev_err(cw_bat->dev, "Failed to get time to empty from gauge: %d\n",
412 time_to_empty);
413 else if (cw_bat->time_to_empty != time_to_empty) {
414 cw_bat->time_to_empty = time_to_empty;
415 cw_bat->battery_changed = true;
419 static void cw_bat_work(struct work_struct *work)
421 struct delayed_work *delay_work;
422 struct cw_battery *cw_bat;
423 int ret;
424 unsigned int reg_val;
426 delay_work = to_delayed_work(work);
427 cw_bat = container_of(delay_work, struct cw_battery, battery_delay_work);
428 ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, &reg_val);
429 if (ret) {
430 dev_err(cw_bat->dev, "Failed to read mode from gauge: %d\n", ret);
431 } else {
432 if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
433 int i;
435 for (i = 0; i < CW2015_RESET_TRIES; i++) {
436 if (!cw_power_on_reset(cw_bat))
437 break;
440 cw_update_soc(cw_bat);
441 cw_update_voltage(cw_bat);
442 cw_update_charge_status(cw_bat);
443 cw_update_status(cw_bat);
444 cw_update_time_to_empty(cw_bat);
446 dev_dbg(cw_bat->dev, "charger_attached = %d\n", cw_bat->charger_attached);
447 dev_dbg(cw_bat->dev, "status = %d\n", cw_bat->status);
448 dev_dbg(cw_bat->dev, "soc = %d%%\n", cw_bat->soc);
449 dev_dbg(cw_bat->dev, "voltage = %dmV\n", cw_bat->voltage_mv);
451 if (cw_bat->battery_changed)
452 power_supply_changed(cw_bat->rk_bat);
453 cw_bat->battery_changed = false;
455 queue_delayed_work(cw_bat->battery_workqueue,
456 &cw_bat->battery_delay_work,
457 msecs_to_jiffies(cw_bat->poll_interval_ms));
460 static bool cw_battery_valid_time_to_empty(struct cw_battery *cw_bat)
462 return cw_bat->time_to_empty > 0 &&
463 cw_bat->time_to_empty < CW2015_MASK_SOC &&
464 cw_bat->status == POWER_SUPPLY_STATUS_DISCHARGING;
467 static int cw_battery_get_property(struct power_supply *psy,
468 enum power_supply_property psp,
469 union power_supply_propval *val)
471 struct cw_battery *cw_bat;
473 cw_bat = power_supply_get_drvdata(psy);
474 switch (psp) {
475 case POWER_SUPPLY_PROP_CAPACITY:
476 val->intval = cw_bat->soc;
477 break;
479 case POWER_SUPPLY_PROP_STATUS:
480 val->intval = cw_bat->status;
481 break;
483 case POWER_SUPPLY_PROP_PRESENT:
484 val->intval = !!cw_bat->voltage_mv;
485 break;
487 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
488 val->intval = cw_bat->voltage_mv * 1000;
489 break;
491 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
492 if (cw_battery_valid_time_to_empty(cw_bat))
493 val->intval = cw_bat->time_to_empty;
494 else
495 val->intval = 0;
496 break;
498 case POWER_SUPPLY_PROP_TECHNOLOGY:
499 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
500 break;
502 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
503 val->intval = cw_bat->charge_count;
504 break;
506 case POWER_SUPPLY_PROP_CHARGE_FULL:
507 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
508 if (cw_bat->battery.charge_full_design_uah > 0)
509 val->intval = cw_bat->battery.charge_full_design_uah;
510 else
511 val->intval = 0;
512 break;
514 case POWER_SUPPLY_PROP_CURRENT_NOW:
515 if (cw_battery_valid_time_to_empty(cw_bat) &&
516 cw_bat->battery.charge_full_design_uah > 0) {
517 /* calculate remaining capacity */
518 val->intval = cw_bat->battery.charge_full_design_uah;
519 val->intval = val->intval * cw_bat->soc / 100;
521 /* estimate current based on time to empty */
522 val->intval = 60 * val->intval / cw_bat->time_to_empty;
523 } else {
524 val->intval = 0;
527 break;
529 default:
530 break;
532 return 0;
535 static enum power_supply_property cw_battery_properties[] = {
536 POWER_SUPPLY_PROP_CAPACITY,
537 POWER_SUPPLY_PROP_STATUS,
538 POWER_SUPPLY_PROP_PRESENT,
539 POWER_SUPPLY_PROP_VOLTAGE_NOW,
540 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
541 POWER_SUPPLY_PROP_TECHNOLOGY,
542 POWER_SUPPLY_PROP_CHARGE_COUNTER,
543 POWER_SUPPLY_PROP_CHARGE_FULL,
544 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
545 POWER_SUPPLY_PROP_CURRENT_NOW,
548 static const struct power_supply_desc cw2015_bat_desc = {
549 .name = "cw2015-battery",
550 .type = POWER_SUPPLY_TYPE_BATTERY,
551 .properties = cw_battery_properties,
552 .num_properties = ARRAY_SIZE(cw_battery_properties),
553 .get_property = cw_battery_get_property,
556 static int cw2015_parse_properties(struct cw_battery *cw_bat)
558 struct device *dev = cw_bat->dev;
559 int length;
560 int ret;
562 length = device_property_count_u8(dev, "cellwise,battery-profile");
563 if (length < 0) {
564 dev_warn(cw_bat->dev,
565 "No battery-profile found, using current flash contents\n");
566 } else if (length != CW2015_SIZE_BATINFO) {
567 dev_err(cw_bat->dev, "battery-profile must be %d bytes\n",
568 CW2015_SIZE_BATINFO);
569 return -EINVAL;
570 } else {
571 cw_bat->bat_profile = devm_kzalloc(dev, length, GFP_KERNEL);
572 if (!cw_bat->bat_profile)
573 return -ENOMEM;
575 ret = device_property_read_u8_array(dev,
576 "cellwise,battery-profile",
577 cw_bat->bat_profile,
578 length);
579 if (ret)
580 return ret;
583 ret = device_property_read_u32(dev, "cellwise,monitor-interval-ms",
584 &cw_bat->poll_interval_ms);
585 if (ret) {
586 dev_dbg(cw_bat->dev, "Using default poll interval\n");
587 cw_bat->poll_interval_ms = CW2015_DEFAULT_POLL_INTERVAL_MS;
590 return 0;
593 static const struct regmap_range regmap_ranges_rd_yes[] = {
594 regmap_reg_range(CW2015_REG_VERSION, CW2015_REG_VERSION),
595 regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_CONFIG),
596 regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE),
597 regmap_reg_range(CW2015_REG_BATINFO,
598 CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
601 static const struct regmap_access_table regmap_rd_table = {
602 .yes_ranges = regmap_ranges_rd_yes,
603 .n_yes_ranges = 4,
606 static const struct regmap_range regmap_ranges_wr_yes[] = {
607 regmap_reg_range(CW2015_REG_RRT_ALERT, CW2015_REG_CONFIG),
608 regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE),
609 regmap_reg_range(CW2015_REG_BATINFO,
610 CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
613 static const struct regmap_access_table regmap_wr_table = {
614 .yes_ranges = regmap_ranges_wr_yes,
615 .n_yes_ranges = 3,
618 static const struct regmap_range regmap_ranges_vol_yes[] = {
619 regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_SOC + 1),
622 static const struct regmap_access_table regmap_vol_table = {
623 .yes_ranges = regmap_ranges_vol_yes,
624 .n_yes_ranges = 1,
627 static const struct regmap_config cw2015_regmap_config = {
628 .reg_bits = 8,
629 .val_bits = 8,
630 .rd_table = &regmap_rd_table,
631 .wr_table = &regmap_wr_table,
632 .volatile_table = &regmap_vol_table,
633 .max_register = CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1,
636 static int cw_bat_probe(struct i2c_client *client)
638 int ret;
639 struct cw_battery *cw_bat;
640 struct power_supply_config psy_cfg = { 0 };
642 cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL);
643 if (!cw_bat)
644 return -ENOMEM;
646 i2c_set_clientdata(client, cw_bat);
647 cw_bat->dev = &client->dev;
648 cw_bat->soc = 1;
650 ret = cw2015_parse_properties(cw_bat);
651 if (ret) {
652 dev_err(cw_bat->dev, "Failed to parse cw2015 properties\n");
653 return ret;
656 cw_bat->regmap = devm_regmap_init_i2c(client, &cw2015_regmap_config);
657 if (IS_ERR(cw_bat->regmap)) {
658 dev_err(cw_bat->dev, "Failed to allocate regmap: %ld\n",
659 PTR_ERR(cw_bat->regmap));
660 return PTR_ERR(cw_bat->regmap);
663 ret = cw_init(cw_bat);
664 if (ret) {
665 dev_err(cw_bat->dev, "Init failed: %d\n", ret);
666 return ret;
669 psy_cfg.drv_data = cw_bat;
670 psy_cfg.fwnode = dev_fwnode(cw_bat->dev);
672 cw_bat->rk_bat = devm_power_supply_register(&client->dev,
673 &cw2015_bat_desc,
674 &psy_cfg);
675 if (IS_ERR(cw_bat->rk_bat)) {
676 dev_err(cw_bat->dev, "Failed to register power supply\n");
677 return PTR_ERR(cw_bat->rk_bat);
680 ret = power_supply_get_battery_info(cw_bat->rk_bat, &cw_bat->battery);
681 if (ret) {
682 dev_warn(cw_bat->dev,
683 "No monitored battery, some properties will be missing\n");
686 cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery");
687 INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work);
688 queue_delayed_work(cw_bat->battery_workqueue,
689 &cw_bat->battery_delay_work, msecs_to_jiffies(10));
690 return 0;
693 static int __maybe_unused cw_bat_suspend(struct device *dev)
695 struct i2c_client *client = to_i2c_client(dev);
696 struct cw_battery *cw_bat = i2c_get_clientdata(client);
698 cancel_delayed_work_sync(&cw_bat->battery_delay_work);
699 return 0;
702 static int __maybe_unused cw_bat_resume(struct device *dev)
704 struct i2c_client *client = to_i2c_client(dev);
705 struct cw_battery *cw_bat = i2c_get_clientdata(client);
707 queue_delayed_work(cw_bat->battery_workqueue,
708 &cw_bat->battery_delay_work, 0);
709 return 0;
712 static SIMPLE_DEV_PM_OPS(cw_bat_pm_ops, cw_bat_suspend, cw_bat_resume);
714 static int cw_bat_remove(struct i2c_client *client)
716 struct cw_battery *cw_bat = i2c_get_clientdata(client);
718 cancel_delayed_work_sync(&cw_bat->battery_delay_work);
719 power_supply_put_battery_info(cw_bat->rk_bat, &cw_bat->battery);
720 return 0;
723 static const struct i2c_device_id cw_bat_id_table[] = {
724 { "cw2015", 0 },
728 static const struct of_device_id cw2015_of_match[] = {
729 { .compatible = "cellwise,cw2015" },
732 MODULE_DEVICE_TABLE(of, cw2015_of_match);
734 static struct i2c_driver cw_bat_driver = {
735 .driver = {
736 .name = "cw2015",
737 .of_match_table = cw2015_of_match,
738 .pm = &cw_bat_pm_ops,
740 .probe_new = cw_bat_probe,
741 .remove = cw_bat_remove,
742 .id_table = cw_bat_id_table,
745 module_i2c_driver(cw_bat_driver);
747 MODULE_AUTHOR("xhc<xhc@rock-chips.com>");
748 MODULE_AUTHOR("Tobias Schramm <t.schramm@manjaro.org>");
749 MODULE_DESCRIPTION("cw2015/cw2013 battery driver");
750 MODULE_LICENSE("GPL");