.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
1 | 2 | /* |
---|
2 | 3 | * Fuel gauge driver for CellWise 2013 / 2015 |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2012, RockChip |
---|
| 6 | + * Copyright (C) 2020, Tobias Schramm |
---|
5 | 7 | * |
---|
6 | 8 | * Authors: xuhuicong <xhc@rock-chips.com> |
---|
7 | | - * |
---|
8 | | - * This program is free software; you can redistribute it and/or modify |
---|
9 | | - * it under the terms of the GNU General Public License version 2 as |
---|
10 | | - * published by the Free Software Foundation. |
---|
11 | | - * |
---|
| 9 | + * Authors: Tobias Schramm <t.schramm@manjaro.org> |
---|
12 | 10 | */ |
---|
13 | 11 | |
---|
| 12 | +#include <linux/bits.h> |
---|
14 | 13 | #include <linux/delay.h> |
---|
15 | | -#include <linux/init.h> |
---|
16 | 14 | #include <linux/i2c.h> |
---|
| 15 | +#include <linux/gfp.h> |
---|
| 16 | +#include <linux/gpio/consumer.h> |
---|
17 | 17 | #include <linux/kernel.h> |
---|
18 | 18 | #include <linux/module.h> |
---|
19 | | -#include <linux/of_gpio.h> |
---|
20 | | -#include <linux/platform_device.h> |
---|
21 | 19 | #include <linux/power_supply.h> |
---|
22 | | -#include <linux/slab.h> |
---|
| 20 | +#include <linux/property.h> |
---|
| 21 | +#include <linux/regmap.h> |
---|
| 22 | +#include <linux/time.h> |
---|
23 | 23 | #include <linux/workqueue.h> |
---|
24 | 24 | |
---|
25 | | -#include <linux/power/cw2015_battery.h> |
---|
| 25 | +#define CW2015_SIZE_BATINFO 64 |
---|
26 | 26 | |
---|
27 | | -static int dbg_enable; |
---|
28 | | -module_param_named(dbg_level, dbg_enable, int, 0644); |
---|
| 27 | +#define CW2015_RESET_TRIES 5 |
---|
29 | 28 | |
---|
30 | | -#define cw_printk(args...) \ |
---|
31 | | - do { \ |
---|
32 | | - if (dbg_enable) { \ |
---|
33 | | - pr_info(args); \ |
---|
34 | | - } \ |
---|
35 | | - } while (0) |
---|
| 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 |
---|
36 | 36 | |
---|
37 | | -static int cw_read(struct i2c_client *client, u8 reg, u8 buf[]) |
---|
| 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) |
---|
| 42 | + |
---|
| 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) |
---|
| 47 | + |
---|
| 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) |
---|
| 52 | + |
---|
| 53 | +/* poll interval from CellWise GPL Android driver example */ |
---|
| 54 | +#define CW2015_DEFAULT_POLL_INTERVAL_MS 8000 |
---|
| 55 | + |
---|
| 56 | +#define CW2015_AVERAGING_SAMPLES 3 |
---|
| 57 | + |
---|
| 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; |
---|
| 66 | + |
---|
| 67 | + bool charger_attached; |
---|
| 68 | + bool battery_changed; |
---|
| 69 | + |
---|
| 70 | + int soc; |
---|
| 71 | + int voltage_mv; |
---|
| 72 | + int status; |
---|
| 73 | + int time_to_empty; |
---|
| 74 | + int charge_count; |
---|
| 75 | + |
---|
| 76 | + u32 poll_interval_ms; |
---|
| 77 | + u8 alert_level; |
---|
| 78 | + |
---|
| 79 | + bool dual_cell; |
---|
| 80 | + |
---|
| 81 | + unsigned int read_errors; |
---|
| 82 | + unsigned int charge_stuck_cnt; |
---|
| 83 | +}; |
---|
| 84 | + |
---|
| 85 | +static int cw_read_word(struct cw_battery *cw_bat, u8 reg, u16 *val) |
---|
38 | 86 | { |
---|
39 | | - return i2c_smbus_read_i2c_block_data(client, reg, 1, buf); |
---|
| 87 | + __be16 value; |
---|
| 88 | + int ret; |
---|
| 89 | + |
---|
| 90 | + ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value)); |
---|
| 91 | + if (ret) |
---|
| 92 | + return ret; |
---|
| 93 | + |
---|
| 94 | + *val = be16_to_cpu(value); |
---|
| 95 | + return 0; |
---|
40 | 96 | } |
---|
41 | 97 | |
---|
42 | | -static int cw_write(struct i2c_client *client, u8 reg, u8 const buf[]) |
---|
43 | | -{ |
---|
44 | | - return i2c_smbus_write_i2c_block_data(client, reg, 1, &buf[0]); |
---|
45 | | -} |
---|
46 | | - |
---|
47 | | -static int cw_read_word(struct i2c_client *client, u8 reg, u8 buf[]) |
---|
48 | | -{ |
---|
49 | | - return i2c_smbus_read_i2c_block_data(client, reg, 2, buf); |
---|
50 | | -} |
---|
51 | | - |
---|
52 | | -int cw_update_config_info(struct cw_battery *cw_bat) |
---|
| 98 | +static int cw_update_profile(struct cw_battery *cw_bat) |
---|
53 | 99 | { |
---|
54 | 100 | int ret; |
---|
55 | | - u8 reg_val; |
---|
56 | | - u8 i; |
---|
| 101 | + unsigned int reg_val; |
---|
57 | 102 | u8 reset_val; |
---|
58 | 103 | |
---|
59 | | - cw_printk("[FGADC] test config_info = 0x%x\n", |
---|
60 | | - cw_bat->plat_data.cw_bat_config_info[0]); |
---|
61 | | - |
---|
62 | | - /* make sure no in sleep mode */ |
---|
63 | | - ret = cw_read(cw_bat->client, REG_MODE, ®_val); |
---|
64 | | - if (ret < 0) |
---|
| 104 | + /* make sure gauge is not in sleep mode */ |
---|
| 105 | + ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, ®_val); |
---|
| 106 | + if (ret) |
---|
65 | 107 | return ret; |
---|
66 | 108 | |
---|
67 | 109 | reset_val = reg_val; |
---|
68 | | - if ((reg_val & MODE_SLEEP_MASK) == MODE_SLEEP) { |
---|
69 | | - dev_err(&cw_bat->client->dev, |
---|
70 | | - "device in sleep mode, cannot update battery info\n"); |
---|
71 | | - return -1; |
---|
| 110 | + if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) { |
---|
| 111 | + dev_err(cw_bat->dev, |
---|
| 112 | + "Gauge is in sleep mode, can't update battery info\n"); |
---|
| 113 | + return -EINVAL; |
---|
72 | 114 | } |
---|
73 | 115 | |
---|
74 | | - /* update new battery info */ |
---|
75 | | - for (i = 0; i < SIZE_BATINFO; i++) { |
---|
76 | | - ret = |
---|
77 | | - cw_write(cw_bat->client, REG_BATINFO + i, |
---|
78 | | - (u8 *)&cw_bat->plat_data.cw_bat_config_info[i]); |
---|
79 | | - |
---|
80 | | - if (ret < 0) |
---|
81 | | - return ret; |
---|
82 | | - } |
---|
83 | | - |
---|
84 | | - reg_val |= CONFIG_UPDATE_FLG; /* set UPDATE_FLAG */ |
---|
85 | | - reg_val &= 0x07; /* clear ATHD */ |
---|
86 | | - reg_val |= ATHD; /* set ATHD */ |
---|
87 | | - ret = cw_write(cw_bat->client, REG_CONFIG, ®_val); |
---|
88 | | - if (ret < 0) |
---|
| 116 | + /* write new battery info */ |
---|
| 117 | + ret = regmap_raw_write(cw_bat->regmap, CW2015_REG_BATINFO, |
---|
| 118 | + cw_bat->bat_profile, |
---|
| 119 | + CW2015_SIZE_BATINFO); |
---|
| 120 | + if (ret) |
---|
89 | 121 | return ret; |
---|
90 | 122 | |
---|
91 | | - /* check 2015/cw2013 for ATHD & update_flag */ |
---|
92 | | - ret = cw_read(cw_bat->client, REG_CONFIG, ®_val); |
---|
93 | | - if (ret < 0) |
---|
| 123 | + /* set config update flag */ |
---|
| 124 | + reg_val |= CW2015_CONFIG_UPDATE_FLG; |
---|
| 125 | + reg_val &= ~CW2015_MASK_ATHD; |
---|
| 126 | + reg_val |= CW2015_ATHD(cw_bat->alert_level); |
---|
| 127 | + ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val); |
---|
| 128 | + if (ret) |
---|
94 | 129 | return ret; |
---|
95 | 130 | |
---|
96 | | - if (!(reg_val & CONFIG_UPDATE_FLG)) { |
---|
97 | | - dev_info(&cw_bat->client->dev, |
---|
98 | | - "update flag for new battery info have not set..\n"); |
---|
99 | | - } |
---|
100 | | - |
---|
101 | | - if ((reg_val & 0xf8) != ATHD) |
---|
102 | | - dev_info(&cw_bat->client->dev, "the new ATHD have not set..\n"); |
---|
103 | | - |
---|
104 | | - /* reset */ |
---|
105 | | - reset_val &= ~(MODE_RESTART); |
---|
106 | | - reg_val = reset_val | MODE_RESTART; |
---|
107 | | - ret = cw_write(cw_bat->client, REG_MODE, ®_val); |
---|
108 | | - if (ret < 0) |
---|
| 131 | + /* reset gauge to apply new battery profile */ |
---|
| 132 | + reset_val &= ~CW2015_MODE_RESTART; |
---|
| 133 | + reg_val = reset_val | CW2015_MODE_RESTART; |
---|
| 134 | + ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val); |
---|
| 135 | + if (ret) |
---|
109 | 136 | return ret; |
---|
110 | 137 | |
---|
111 | | - msleep(10); |
---|
112 | | - ret = cw_write(cw_bat->client, REG_MODE, &reset_val); |
---|
113 | | - if (ret < 0) |
---|
| 138 | + /* wait for gauge to reset */ |
---|
| 139 | + msleep(20); |
---|
| 140 | + |
---|
| 141 | + /* clear reset flag */ |
---|
| 142 | + ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); |
---|
| 143 | + if (ret) |
---|
114 | 144 | return ret; |
---|
115 | 145 | |
---|
116 | | - cw_printk("cw2015 update config success!\n"); |
---|
| 146 | + /* wait for gauge to become ready */ |
---|
| 147 | + ret = regmap_read_poll_timeout(cw_bat->regmap, CW2015_REG_SOC, |
---|
| 148 | + reg_val, reg_val <= 100, |
---|
| 149 | + 10 * USEC_PER_MSEC, 10 * USEC_PER_SEC); |
---|
| 150 | + if (ret) |
---|
| 151 | + dev_err(cw_bat->dev, |
---|
| 152 | + "Gauge did not become ready after profile upload\n"); |
---|
| 153 | + else |
---|
| 154 | + dev_dbg(cw_bat->dev, "Battery profile updated\n"); |
---|
117 | 155 | |
---|
118 | | - return 0; |
---|
| 156 | + return ret; |
---|
119 | 157 | } |
---|
120 | 158 | |
---|
121 | 159 | static int cw_init(struct cw_battery *cw_bat) |
---|
122 | 160 | { |
---|
123 | 161 | int ret; |
---|
124 | | - int i; |
---|
125 | | - u8 reg_val = MODE_SLEEP; |
---|
| 162 | + unsigned int reg_val = CW2015_MODE_SLEEP; |
---|
126 | 163 | |
---|
127 | | - if ((reg_val & MODE_SLEEP_MASK) == MODE_SLEEP) { |
---|
128 | | - reg_val = MODE_NORMAL; |
---|
129 | | - ret = cw_write(cw_bat->client, REG_MODE, ®_val); |
---|
130 | | - if (ret < 0) |
---|
| 164 | + if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) { |
---|
| 165 | + reg_val = CW2015_MODE_NORMAL; |
---|
| 166 | + ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val); |
---|
| 167 | + if (ret) |
---|
131 | 168 | return ret; |
---|
132 | 169 | } |
---|
133 | 170 | |
---|
134 | | - ret = cw_read(cw_bat->client, REG_CONFIG, ®_val); |
---|
135 | | - if (ret < 0) |
---|
| 171 | + ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, ®_val); |
---|
| 172 | + if (ret) |
---|
136 | 173 | return ret; |
---|
137 | 174 | |
---|
138 | | - if ((reg_val & 0xf8) != ATHD) { |
---|
139 | | - dev_info(&cw_bat->client->dev, "the new ATHD have not set\n"); |
---|
140 | | - reg_val &= 0x07; /* clear ATHD */ |
---|
141 | | - reg_val |= ATHD; /* set ATHD */ |
---|
142 | | - ret = cw_write(cw_bat->client, REG_CONFIG, ®_val); |
---|
143 | | - if (ret < 0) |
---|
| 175 | + if ((reg_val & CW2015_MASK_ATHD) != CW2015_ATHD(cw_bat->alert_level)) { |
---|
| 176 | + dev_dbg(cw_bat->dev, "Setting new alert level\n"); |
---|
| 177 | + reg_val &= ~CW2015_MASK_ATHD; |
---|
| 178 | + reg_val |= ~CW2015_ATHD(cw_bat->alert_level); |
---|
| 179 | + ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val); |
---|
| 180 | + if (ret) |
---|
144 | 181 | return ret; |
---|
145 | 182 | } |
---|
146 | 183 | |
---|
147 | | - ret = cw_read(cw_bat->client, REG_CONFIG, ®_val); |
---|
148 | | - if (ret < 0) |
---|
| 184 | + ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, ®_val); |
---|
| 185 | + if (ret) |
---|
149 | 186 | return ret; |
---|
150 | 187 | |
---|
151 | | - if (!(reg_val & CONFIG_UPDATE_FLG)) { |
---|
152 | | - cw_printk("update config flg is true, need update config\n"); |
---|
153 | | - ret = cw_update_config_info(cw_bat); |
---|
154 | | - if (ret < 0) { |
---|
155 | | - dev_info(&cw_bat->client->dev, |
---|
156 | | - "update flag for new battery info have not set\n"); |
---|
| 188 | + if (!(reg_val & CW2015_CONFIG_UPDATE_FLG)) { |
---|
| 189 | + dev_dbg(cw_bat->dev, |
---|
| 190 | + "Battery profile not present, uploading battery profile\n"); |
---|
| 191 | + if (cw_bat->bat_profile) { |
---|
| 192 | + ret = cw_update_profile(cw_bat); |
---|
| 193 | + if (ret) { |
---|
| 194 | + dev_err(cw_bat->dev, |
---|
| 195 | + "Failed to upload battery profile\n"); |
---|
| 196 | + return ret; |
---|
| 197 | + } |
---|
| 198 | + } else { |
---|
| 199 | + dev_warn(cw_bat->dev, |
---|
| 200 | + "No profile specified, continuing without profile\n"); |
---|
| 201 | + } |
---|
| 202 | + } else if (cw_bat->bat_profile) { |
---|
| 203 | + u8 bat_info[CW2015_SIZE_BATINFO]; |
---|
| 204 | + |
---|
| 205 | + ret = regmap_raw_read(cw_bat->regmap, CW2015_REG_BATINFO, |
---|
| 206 | + bat_info, CW2015_SIZE_BATINFO); |
---|
| 207 | + if (ret) { |
---|
| 208 | + dev_err(cw_bat->dev, |
---|
| 209 | + "Failed to read stored battery profile\n"); |
---|
157 | 210 | return ret; |
---|
| 211 | + } |
---|
| 212 | + |
---|
| 213 | + if (memcmp(bat_info, cw_bat->bat_profile, CW2015_SIZE_BATINFO)) { |
---|
| 214 | + dev_warn(cw_bat->dev, "Replacing stored battery profile\n"); |
---|
| 215 | + ret = cw_update_profile(cw_bat); |
---|
| 216 | + if (ret) |
---|
| 217 | + return ret; |
---|
158 | 218 | } |
---|
159 | 219 | } else { |
---|
160 | | - for (i = 0; i < SIZE_BATINFO; i++) { |
---|
161 | | - ret = cw_read(cw_bat->client, (REG_BATINFO + i), |
---|
162 | | - ®_val); |
---|
163 | | - if (ret < 0) |
---|
164 | | - return ret; |
---|
165 | | - |
---|
166 | | - if (cw_bat->plat_data.cw_bat_config_info[i] != reg_val) |
---|
167 | | - break; |
---|
168 | | - } |
---|
169 | | - |
---|
170 | | - if (i != SIZE_BATINFO) { |
---|
171 | | - dev_info(&cw_bat->client->dev, |
---|
172 | | - "update flag for new battery info have not set\n"); |
---|
173 | | - ret = cw_update_config_info(cw_bat); |
---|
174 | | - if (ret < 0) |
---|
175 | | - return ret; |
---|
176 | | - } |
---|
| 220 | + dev_warn(cw_bat->dev, |
---|
| 221 | + "Can't check current battery profile, no profile provided\n"); |
---|
177 | 222 | } |
---|
178 | 223 | |
---|
179 | | - for (i = 0; i < 30; i++) { |
---|
180 | | - ret = cw_read(cw_bat->client, REG_SOC, ®_val); |
---|
181 | | - if (ret < 0) |
---|
182 | | - return ret; |
---|
183 | | - else if (reg_val <= 0x64) |
---|
184 | | - break; |
---|
185 | | - msleep(120); |
---|
186 | | - } |
---|
187 | | - |
---|
188 | | - if (i >= 30) { |
---|
189 | | - reg_val = MODE_SLEEP; |
---|
190 | | - ret = cw_write(cw_bat->client, REG_MODE, ®_val); |
---|
191 | | - dev_info(&cw_bat->client->dev, "report battery capacity error"); |
---|
192 | | - return -1; |
---|
193 | | - } |
---|
194 | | - |
---|
195 | | - cw_printk("cw2015 init success!\n"); |
---|
| 224 | + dev_dbg(cw_bat->dev, "Battery profile configured\n"); |
---|
196 | 225 | return 0; |
---|
197 | 226 | } |
---|
198 | 227 | |
---|
199 | | -static int check_chrg_usb_psy(struct device *dev, void *data) |
---|
200 | | -{ |
---|
201 | | - struct power_supply *psy = dev_get_drvdata(dev); |
---|
202 | | - struct cw_battery *cw_bat = (struct cw_battery *)data; |
---|
203 | | - |
---|
204 | | - if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { |
---|
205 | | - cw_bat->chrg_usb_psy = psy; |
---|
206 | | - return 1; |
---|
207 | | - } |
---|
208 | | - return 0; |
---|
209 | | -} |
---|
210 | | - |
---|
211 | | -static int check_chrg_ac_psy(struct device *dev, void *data) |
---|
212 | | -{ |
---|
213 | | - struct power_supply *psy = dev_get_drvdata(dev); |
---|
214 | | - struct cw_battery *cw_bat = (struct cw_battery *)data; |
---|
215 | | - |
---|
216 | | - if (psy->desc->type == POWER_SUPPLY_TYPE_MAINS) { |
---|
217 | | - cw_bat->chrg_ac_psy = psy; |
---|
218 | | - return 1; |
---|
219 | | - } |
---|
220 | | - return 0; |
---|
221 | | -} |
---|
222 | | - |
---|
223 | | -static void get_chrg_psy(struct cw_battery *cw_bat) |
---|
224 | | -{ |
---|
225 | | - if (!cw_bat->chrg_usb_psy) |
---|
226 | | - class_for_each_device(power_supply_class, NULL, cw_bat, |
---|
227 | | - check_chrg_usb_psy); |
---|
228 | | - if (!cw_bat->chrg_ac_psy) |
---|
229 | | - class_for_each_device(power_supply_class, NULL, cw_bat, |
---|
230 | | - check_chrg_ac_psy); |
---|
231 | | -} |
---|
232 | | - |
---|
233 | | -static int get_charge_state(struct cw_battery *cw_bat) |
---|
234 | | -{ |
---|
235 | | - union power_supply_propval val; |
---|
236 | | - int ret = -ENODEV; |
---|
237 | | - int usb_online = 0; |
---|
238 | | - int ac_online = 0; |
---|
239 | | - struct power_supply *chrg_usb_psy; |
---|
240 | | - struct power_supply *chrg_ac_psy; |
---|
241 | | - |
---|
242 | | - if (!cw_bat->chrg_usb_psy || !cw_bat->chrg_ac_psy) |
---|
243 | | - get_chrg_psy(cw_bat); |
---|
244 | | - |
---|
245 | | - chrg_usb_psy = cw_bat->chrg_usb_psy; |
---|
246 | | - chrg_ac_psy = cw_bat->chrg_ac_psy; |
---|
247 | | - if (chrg_usb_psy) { |
---|
248 | | - ret = chrg_usb_psy->desc->get_property(chrg_usb_psy, |
---|
249 | | - POWER_SUPPLY_PROP_ONLINE, |
---|
250 | | - &val); |
---|
251 | | - if (!ret) |
---|
252 | | - usb_online = val.intval; |
---|
253 | | - } |
---|
254 | | - if (chrg_ac_psy) { |
---|
255 | | - ret = chrg_ac_psy->desc->get_property(chrg_ac_psy, |
---|
256 | | - POWER_SUPPLY_PROP_ONLINE, |
---|
257 | | - &val); |
---|
258 | | - if (!ret) |
---|
259 | | - ac_online = val.intval; |
---|
260 | | - } |
---|
261 | | - if (!chrg_usb_psy) |
---|
262 | | - cw_printk("Usb online didn't find\n"); |
---|
263 | | - if (!chrg_ac_psy) |
---|
264 | | - cw_printk("Ac online didn't find\n"); |
---|
265 | | - |
---|
266 | | - cw_printk("ac_online = %d, usb_online = %d\n", ac_online, usb_online); |
---|
267 | | - if (ac_online || usb_online) |
---|
268 | | - return 1; |
---|
269 | | - |
---|
270 | | - return 0; |
---|
271 | | -} |
---|
272 | | - |
---|
273 | | -static int cw_por(struct cw_battery *cw_bat) |
---|
| 228 | +static int cw_power_on_reset(struct cw_battery *cw_bat) |
---|
274 | 229 | { |
---|
275 | 230 | int ret; |
---|
276 | 231 | unsigned char reset_val; |
---|
277 | 232 | |
---|
278 | | - reset_val = MODE_SLEEP; |
---|
279 | | - ret = cw_write(cw_bat->client, REG_MODE, &reset_val); |
---|
280 | | - if (ret < 0) |
---|
| 233 | + reset_val = CW2015_MODE_SLEEP; |
---|
| 234 | + ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); |
---|
| 235 | + if (ret) |
---|
281 | 236 | return ret; |
---|
282 | | - reset_val = MODE_NORMAL; |
---|
| 237 | + |
---|
| 238 | + /* wait for gauge to enter sleep */ |
---|
283 | 239 | msleep(20); |
---|
284 | | - ret = cw_write(cw_bat->client, REG_MODE, &reset_val); |
---|
285 | | - if (ret < 0) |
---|
| 240 | + |
---|
| 241 | + reset_val = CW2015_MODE_NORMAL; |
---|
| 242 | + ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); |
---|
| 243 | + if (ret) |
---|
286 | 244 | return ret; |
---|
| 245 | + |
---|
287 | 246 | ret = cw_init(cw_bat); |
---|
288 | 247 | if (ret) |
---|
289 | 248 | return ret; |
---|
290 | 249 | return 0; |
---|
291 | 250 | } |
---|
292 | 251 | |
---|
293 | | -static int cw_get_capacity(struct cw_battery *cw_bat) |
---|
| 252 | +#define HYSTERESIS(current, previous, up, down) \ |
---|
| 253 | + (((current) < (previous) + (up)) && ((current) > (previous) - (down))) |
---|
| 254 | + |
---|
| 255 | +static int cw_get_soc(struct cw_battery *cw_bat) |
---|
294 | 256 | { |
---|
295 | | - int cw_capacity; |
---|
| 257 | + unsigned int soc; |
---|
296 | 258 | int ret; |
---|
297 | | - unsigned char reg_val[2]; |
---|
298 | 259 | |
---|
299 | | - static int reset_loop; |
---|
300 | | - static int charging_loop; |
---|
301 | | - static int discharging_loop; |
---|
302 | | - static int jump_flag; |
---|
303 | | - static int charging_5_loop; |
---|
304 | | - int sleep_cap; |
---|
305 | | - |
---|
306 | | - ret = cw_read_word(cw_bat->client, REG_SOC, reg_val); |
---|
307 | | - if (ret < 0) |
---|
| 260 | + ret = regmap_read(cw_bat->regmap, CW2015_REG_SOC, &soc); |
---|
| 261 | + if (ret) |
---|
308 | 262 | return ret; |
---|
309 | 263 | |
---|
310 | | - cw_capacity = reg_val[0]; |
---|
| 264 | + if (soc > 100) { |
---|
| 265 | + int max_error_cycles = |
---|
| 266 | + CW2015_BAT_SOC_ERROR_MS / cw_bat->poll_interval_ms; |
---|
311 | 267 | |
---|
312 | | - if ((cw_capacity < 0) || (cw_capacity > 100)) { |
---|
313 | | - cw_printk("Error: cw_capacity = %d\n", cw_capacity); |
---|
314 | | - reset_loop++; |
---|
315 | | - if (reset_loop > |
---|
316 | | - (BATTERY_CAPACITY_ERROR / cw_bat->monitor_sec)) { |
---|
317 | | - cw_por(cw_bat); |
---|
318 | | - reset_loop = 0; |
---|
| 268 | + dev_err(cw_bat->dev, "Invalid SoC %d%%\n", soc); |
---|
| 269 | + cw_bat->read_errors++; |
---|
| 270 | + if (cw_bat->read_errors > max_error_cycles) { |
---|
| 271 | + dev_warn(cw_bat->dev, |
---|
| 272 | + "Too many invalid SoC reports, resetting gauge\n"); |
---|
| 273 | + cw_power_on_reset(cw_bat); |
---|
| 274 | + cw_bat->read_errors = 0; |
---|
319 | 275 | } |
---|
320 | | - return cw_bat->capacity; |
---|
| 276 | + return cw_bat->soc; |
---|
| 277 | + } |
---|
| 278 | + cw_bat->read_errors = 0; |
---|
| 279 | + |
---|
| 280 | + /* Reset gauge if stuck while charging */ |
---|
| 281 | + if (cw_bat->status == POWER_SUPPLY_STATUS_CHARGING && soc == cw_bat->soc) { |
---|
| 282 | + int max_stuck_cycles = |
---|
| 283 | + CW2015_BAT_CHARGING_STUCK_MS / cw_bat->poll_interval_ms; |
---|
| 284 | + |
---|
| 285 | + cw_bat->charge_stuck_cnt++; |
---|
| 286 | + if (cw_bat->charge_stuck_cnt > max_stuck_cycles) { |
---|
| 287 | + dev_warn(cw_bat->dev, |
---|
| 288 | + "SoC stuck @%u%%, resetting gauge\n", soc); |
---|
| 289 | + cw_power_on_reset(cw_bat); |
---|
| 290 | + cw_bat->charge_stuck_cnt = 0; |
---|
| 291 | + } |
---|
321 | 292 | } else { |
---|
322 | | - reset_loop = 0; |
---|
| 293 | + cw_bat->charge_stuck_cnt = 0; |
---|
323 | 294 | } |
---|
324 | 295 | |
---|
325 | | - /* case 1 : aviod swing */ |
---|
326 | | - if (((cw_bat->charger_mode > 0) && |
---|
327 | | - (cw_capacity <= cw_bat->capacity - 1) && |
---|
328 | | - (cw_capacity > cw_bat->capacity - 9)) || |
---|
329 | | - ((cw_bat->charger_mode == 0) && |
---|
330 | | - (cw_capacity == (cw_bat->capacity + 1)))) { |
---|
331 | | - if (!(cw_capacity == 0 && cw_bat->capacity <= 2)) |
---|
332 | | - cw_capacity = cw_bat->capacity; |
---|
333 | | - } |
---|
| 296 | + /* Ignore voltage dips during charge */ |
---|
| 297 | + if (cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 0, 3)) |
---|
| 298 | + soc = cw_bat->soc; |
---|
334 | 299 | |
---|
335 | | - /* case 2 : aviod no charge full */ |
---|
336 | | - if ((cw_bat->charger_mode > 0) && |
---|
337 | | - (cw_capacity >= 95) && (cw_capacity <= cw_bat->capacity)) { |
---|
338 | | - cw_printk("Chaman join no charge full\n"); |
---|
339 | | - charging_loop++; |
---|
340 | | - if (charging_loop > |
---|
341 | | - (BATTERY_UP_MAX_CHANGE / cw_bat->monitor_sec)) { |
---|
342 | | - cw_capacity = (cw_bat->capacity + 1) <= 100 ? |
---|
343 | | - (cw_bat->capacity + 1) : 100; |
---|
344 | | - charging_loop = 0; |
---|
345 | | - jump_flag = 1; |
---|
346 | | - } else { |
---|
347 | | - cw_capacity = cw_bat->capacity; |
---|
348 | | - } |
---|
349 | | - } |
---|
| 300 | + /* Ignore voltage spikes during discharge */ |
---|
| 301 | + if (!cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 3, 0)) |
---|
| 302 | + soc = cw_bat->soc; |
---|
350 | 303 | |
---|
351 | | - /* case 3 : avoid battery level jump to CW_BAT */ |
---|
352 | | - if ((cw_bat->charger_mode == 0) && |
---|
353 | | - (cw_capacity <= cw_bat->capacity) && |
---|
354 | | - (cw_capacity >= 90) && (jump_flag == 1)) { |
---|
355 | | - cw_printk("Chaman join no charge full discharging\n"); |
---|
356 | | -#ifdef CONFIG_PM |
---|
357 | | - if (cw_bat->suspend_resume_mark == 1) { |
---|
358 | | - cw_bat->suspend_resume_mark = 0; |
---|
359 | | - sleep_cap = (cw_bat->after.tv_sec + |
---|
360 | | - discharging_loop * |
---|
361 | | - (cw_bat->monitor_sec / 1000)) / |
---|
362 | | - (BATTERY_DOWN_MAX_CHANGE / 1000); |
---|
363 | | - cw_printk("sleep_cap = %d\n", sleep_cap); |
---|
364 | | - |
---|
365 | | - if (cw_capacity >= cw_bat->capacity - sleep_cap) { |
---|
366 | | - return cw_capacity; |
---|
367 | | - } else { |
---|
368 | | - if (!sleep_cap) |
---|
369 | | - discharging_loop = discharging_loop + |
---|
370 | | - 1 + cw_bat->after.tv_sec / |
---|
371 | | - (cw_bat->monitor_sec / 1000); |
---|
372 | | - else |
---|
373 | | - discharging_loop = 0; |
---|
374 | | - cw_printk("discharging_loop = %d\n", |
---|
375 | | - discharging_loop); |
---|
376 | | - return cw_bat->capacity - sleep_cap; |
---|
377 | | - } |
---|
378 | | - } |
---|
379 | | -#endif |
---|
380 | | - discharging_loop++; |
---|
381 | | - if (discharging_loop > |
---|
382 | | - (BATTERY_DOWN_MAX_CHANGE / cw_bat->monitor_sec)) { |
---|
383 | | - if (cw_capacity >= cw_bat->capacity - 1) |
---|
384 | | - jump_flag = 0; |
---|
385 | | - else |
---|
386 | | - cw_capacity = cw_bat->capacity - 1; |
---|
387 | | - |
---|
388 | | - discharging_loop = 0; |
---|
389 | | - } else { |
---|
390 | | - cw_capacity = cw_bat->capacity; |
---|
391 | | - } |
---|
392 | | - } |
---|
393 | | - |
---|
394 | | - /* case 4 : avoid battery level is 0% when long time charging */ |
---|
395 | | - if ((cw_bat->charger_mode > 0) && (cw_capacity == 0)) { |
---|
396 | | - charging_5_loop++; |
---|
397 | | - if (charging_5_loop > |
---|
398 | | - BATTERY_CHARGING_ZERO / cw_bat->monitor_sec) { |
---|
399 | | - cw_por(cw_bat); |
---|
400 | | - charging_5_loop = 0; |
---|
401 | | - } |
---|
402 | | - } else if (charging_5_loop != 0) { |
---|
403 | | - charging_5_loop = 0; |
---|
404 | | - } |
---|
405 | | -#ifdef CONFIG_PM |
---|
406 | | - if (cw_bat->suspend_resume_mark == 1) |
---|
407 | | - cw_bat->suspend_resume_mark = 0; |
---|
408 | | -#endif |
---|
409 | | - return cw_capacity; |
---|
| 304 | + return soc; |
---|
410 | 305 | } |
---|
411 | 306 | |
---|
412 | 307 | static int cw_get_voltage(struct cw_battery *cw_bat) |
---|
413 | 308 | { |
---|
414 | | - int ret; |
---|
415 | | - u8 reg_val[2]; |
---|
416 | | - u16 value16, value16_1, value16_2, value16_3; |
---|
417 | | - int voltage; |
---|
418 | | - int res1, res2; |
---|
| 309 | + int ret, i, voltage_mv; |
---|
| 310 | + u16 reg_val; |
---|
| 311 | + u32 avg = 0; |
---|
419 | 312 | |
---|
420 | | - ret = cw_read_word(cw_bat->client, REG_VCELL, reg_val); |
---|
421 | | - if (ret < 0) |
---|
422 | | - return ret; |
---|
423 | | - value16 = (reg_val[0] << 8) + reg_val[1]; |
---|
| 313 | + for (i = 0; i < CW2015_AVERAGING_SAMPLES; i++) { |
---|
| 314 | + ret = cw_read_word(cw_bat, CW2015_REG_VCELL, ®_val); |
---|
| 315 | + if (ret) |
---|
| 316 | + return ret; |
---|
424 | 317 | |
---|
425 | | - ret = cw_read_word(cw_bat->client, REG_VCELL, reg_val); |
---|
426 | | - if (ret < 0) |
---|
427 | | - return ret; |
---|
428 | | - value16_1 = (reg_val[0] << 8) + reg_val[1]; |
---|
429 | | - |
---|
430 | | - ret = cw_read_word(cw_bat->client, REG_VCELL, reg_val); |
---|
431 | | - if (ret < 0) |
---|
432 | | - return ret; |
---|
433 | | - value16_2 = (reg_val[0] << 8) + reg_val[1]; |
---|
434 | | - |
---|
435 | | - if (value16 > value16_1) { |
---|
436 | | - value16_3 = value16; |
---|
437 | | - value16 = value16_1; |
---|
438 | | - value16_1 = value16_3; |
---|
| 318 | + avg += reg_val; |
---|
439 | 319 | } |
---|
| 320 | + avg /= CW2015_AVERAGING_SAMPLES; |
---|
440 | 321 | |
---|
441 | | - if (value16_1 > value16_2) { |
---|
442 | | - value16_3 = value16_1; |
---|
443 | | - value16_1 = value16_2; |
---|
444 | | - value16_2 = value16_3; |
---|
445 | | - } |
---|
| 322 | + /* |
---|
| 323 | + * 305 uV per ADC step |
---|
| 324 | + * Use 312 / 1024 as efficient approximation of 305 / 1000 |
---|
| 325 | + * Negligible error of 0.1% |
---|
| 326 | + */ |
---|
| 327 | + voltage_mv = avg * 312 / 1024; |
---|
| 328 | + if (cw_bat->dual_cell) |
---|
| 329 | + voltage_mv *= 2; |
---|
446 | 330 | |
---|
447 | | - if (value16 > value16_1) { |
---|
448 | | - value16_3 = value16; |
---|
449 | | - value16 = value16_1; |
---|
450 | | - value16_1 = value16_3; |
---|
451 | | - } |
---|
452 | | - |
---|
453 | | - voltage = value16_1 * 312 / 1024; |
---|
454 | | - |
---|
455 | | - if (cw_bat->plat_data.divider_res1 && |
---|
456 | | - cw_bat->plat_data.divider_res2) { |
---|
457 | | - res1 = cw_bat->plat_data.divider_res1; |
---|
458 | | - res2 = cw_bat->plat_data.divider_res2; |
---|
459 | | - voltage = voltage * (res1 + res2) / res2; |
---|
460 | | - } else if (cw_bat->dual_battery) { |
---|
461 | | - voltage = voltage * 2; |
---|
462 | | - } |
---|
463 | | - |
---|
464 | | - dev_dbg(&cw_bat->client->dev, "the cw201x voltage=%d,reg_val=%x %x\n", |
---|
465 | | - voltage, reg_val[0], reg_val[1]); |
---|
466 | | - return voltage; |
---|
| 331 | + dev_dbg(cw_bat->dev, "Read voltage: %d mV, raw=0x%04x\n", |
---|
| 332 | + voltage_mv, reg_val); |
---|
| 333 | + return voltage_mv; |
---|
467 | 334 | } |
---|
468 | 335 | |
---|
469 | | -/*This function called when get RRT from cw2015*/ |
---|
470 | 336 | static int cw_get_time_to_empty(struct cw_battery *cw_bat) |
---|
471 | 337 | { |
---|
472 | 338 | int ret; |
---|
473 | | - u8 reg_val; |
---|
474 | 339 | u16 value16; |
---|
475 | 340 | |
---|
476 | | - ret = cw_read(cw_bat->client, REG_RRT_ALERT, ®_val); |
---|
477 | | - if (ret < 0) |
---|
| 341 | + ret = cw_read_word(cw_bat, CW2015_REG_RRT_ALERT, &value16); |
---|
| 342 | + if (ret) |
---|
478 | 343 | return ret; |
---|
479 | 344 | |
---|
480 | | - value16 = reg_val; |
---|
481 | | - |
---|
482 | | - ret = cw_read(cw_bat->client, REG_RRT_ALERT + 1, ®_val); |
---|
483 | | - if (ret < 0) |
---|
484 | | - return ret; |
---|
485 | | - |
---|
486 | | - value16 = ((value16 << 8) + reg_val) & 0x1fff; |
---|
487 | | - return value16; |
---|
| 345 | + return value16 & CW2015_MASK_SOC; |
---|
488 | 346 | } |
---|
489 | 347 | |
---|
490 | 348 | static void cw_update_charge_status(struct cw_battery *cw_bat) |
---|
491 | 349 | { |
---|
492 | | - int cw_charger_mode; |
---|
493 | | - |
---|
494 | | - cw_charger_mode = get_charge_state(cw_bat); |
---|
495 | | - if (cw_bat->charger_mode != cw_charger_mode) { |
---|
496 | | - cw_bat->charger_mode = cw_charger_mode; |
---|
497 | | - cw_bat->bat_change = 1; |
---|
498 | | - if (cw_charger_mode) |
---|
499 | | - cw_bat->charge_count++; |
---|
500 | | - } |
---|
501 | | -} |
---|
502 | | - |
---|
503 | | -static void cw_update_capacity(struct cw_battery *cw_bat) |
---|
504 | | -{ |
---|
505 | | - int cw_capacity; |
---|
506 | | - |
---|
507 | | - cw_capacity = cw_get_capacity(cw_bat); |
---|
508 | | - if ((cw_capacity >= 0) && (cw_capacity <= 100) && |
---|
509 | | - (cw_bat->capacity != cw_capacity)) { |
---|
510 | | - cw_bat->capacity = cw_capacity; |
---|
511 | | - cw_bat->bat_change = 1; |
---|
512 | | - } |
---|
513 | | -} |
---|
514 | | - |
---|
515 | | -static void cw_update_vol(struct cw_battery *cw_bat) |
---|
516 | | -{ |
---|
517 | 350 | int ret; |
---|
518 | 351 | |
---|
519 | | - ret = cw_get_voltage(cw_bat); |
---|
520 | | - if ((ret >= 0) && (cw_bat->voltage != ret)) |
---|
521 | | - cw_bat->voltage = ret; |
---|
| 352 | + ret = power_supply_am_i_supplied(cw_bat->rk_bat); |
---|
| 353 | + if (ret < 0) { |
---|
| 354 | + dev_warn(cw_bat->dev, "Failed to get supply state: %d\n", ret); |
---|
| 355 | + } else { |
---|
| 356 | + bool charger_attached; |
---|
| 357 | + |
---|
| 358 | + charger_attached = !!ret; |
---|
| 359 | + if (cw_bat->charger_attached != charger_attached) { |
---|
| 360 | + cw_bat->battery_changed = true; |
---|
| 361 | + if (charger_attached) |
---|
| 362 | + cw_bat->charge_count++; |
---|
| 363 | + } |
---|
| 364 | + cw_bat->charger_attached = charger_attached; |
---|
| 365 | + } |
---|
| 366 | +} |
---|
| 367 | + |
---|
| 368 | +static void cw_update_soc(struct cw_battery *cw_bat) |
---|
| 369 | +{ |
---|
| 370 | + int soc; |
---|
| 371 | + |
---|
| 372 | + soc = cw_get_soc(cw_bat); |
---|
| 373 | + if (soc < 0) |
---|
| 374 | + dev_err(cw_bat->dev, "Failed to get SoC from gauge: %d\n", soc); |
---|
| 375 | + else if (cw_bat->soc != soc) { |
---|
| 376 | + cw_bat->soc = soc; |
---|
| 377 | + cw_bat->battery_changed = true; |
---|
| 378 | + } |
---|
| 379 | +} |
---|
| 380 | + |
---|
| 381 | +static void cw_update_voltage(struct cw_battery *cw_bat) |
---|
| 382 | +{ |
---|
| 383 | + int voltage_mv; |
---|
| 384 | + |
---|
| 385 | + voltage_mv = cw_get_voltage(cw_bat); |
---|
| 386 | + if (voltage_mv < 0) |
---|
| 387 | + dev_err(cw_bat->dev, "Failed to get voltage from gauge: %d\n", |
---|
| 388 | + voltage_mv); |
---|
| 389 | + else |
---|
| 390 | + cw_bat->voltage_mv = voltage_mv; |
---|
522 | 391 | } |
---|
523 | 392 | |
---|
524 | 393 | static void cw_update_status(struct cw_battery *cw_bat) |
---|
525 | 394 | { |
---|
526 | | - int status; |
---|
| 395 | + int status = POWER_SUPPLY_STATUS_DISCHARGING; |
---|
527 | 396 | |
---|
528 | | - if (cw_bat->charger_mode > 0) { |
---|
529 | | - if (cw_bat->capacity >= 100) |
---|
| 397 | + if (cw_bat->charger_attached) { |
---|
| 398 | + if (cw_bat->soc >= 100) |
---|
530 | 399 | status = POWER_SUPPLY_STATUS_FULL; |
---|
531 | 400 | else |
---|
532 | 401 | status = POWER_SUPPLY_STATUS_CHARGING; |
---|
533 | | - } else { |
---|
534 | | - status = POWER_SUPPLY_STATUS_DISCHARGING; |
---|
535 | 402 | } |
---|
536 | 403 | |
---|
537 | | - if (cw_bat->status != status) { |
---|
538 | | - cw_bat->status = status; |
---|
539 | | - cw_bat->bat_change = 1; |
---|
540 | | - } |
---|
| 404 | + if (cw_bat->status != status) |
---|
| 405 | + cw_bat->battery_changed = true; |
---|
| 406 | + cw_bat->status = status; |
---|
541 | 407 | } |
---|
542 | 408 | |
---|
543 | 409 | static void cw_update_time_to_empty(struct cw_battery *cw_bat) |
---|
544 | 410 | { |
---|
545 | | - int ret; |
---|
| 411 | + int time_to_empty; |
---|
546 | 412 | |
---|
547 | | - ret = cw_get_time_to_empty(cw_bat); |
---|
548 | | - if ((ret >= 0) && (cw_bat->time_to_empty != ret)) { |
---|
549 | | - cw_bat->time_to_empty = ret; |
---|
550 | | - cw_bat->bat_change = 1; |
---|
| 413 | + time_to_empty = cw_get_time_to_empty(cw_bat); |
---|
| 414 | + if (time_to_empty < 0) { |
---|
| 415 | + dev_err(cw_bat->dev, "Failed to get time to empty from gauge: %d\n", |
---|
| 416 | + time_to_empty); |
---|
| 417 | + return; |
---|
551 | 418 | } |
---|
| 419 | + cw_bat->time_to_empty = time_to_empty; |
---|
552 | 420 | } |
---|
553 | 421 | |
---|
554 | 422 | static void cw_bat_work(struct work_struct *work) |
---|
.. | .. |
---|
556 | 424 | struct delayed_work *delay_work; |
---|
557 | 425 | struct cw_battery *cw_bat; |
---|
558 | 426 | int ret; |
---|
559 | | - u8 reg_val; |
---|
560 | | - int i = 0; |
---|
| 427 | + unsigned int reg_val; |
---|
561 | 428 | |
---|
562 | | - delay_work = container_of(work, struct delayed_work, work); |
---|
563 | | - cw_bat = |
---|
564 | | - container_of(delay_work, struct cw_battery, battery_delay_work); |
---|
565 | | - |
---|
566 | | - /* Add for battery swap start */ |
---|
567 | | - ret = cw_read(cw_bat->client, REG_MODE, ®_val); |
---|
568 | | - if (ret < 0) { |
---|
569 | | - cw_bat->bat_mode = MODE_VIRTUAL; |
---|
570 | | - cw_bat->bat_change = 1; |
---|
| 429 | + delay_work = to_delayed_work(work); |
---|
| 430 | + cw_bat = container_of(delay_work, struct cw_battery, battery_delay_work); |
---|
| 431 | + ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, ®_val); |
---|
| 432 | + if (ret) { |
---|
| 433 | + dev_err(cw_bat->dev, "Failed to read mode from gauge: %d\n", ret); |
---|
571 | 434 | } else { |
---|
572 | | - if ((reg_val & MODE_SLEEP_MASK) == MODE_SLEEP) { |
---|
573 | | - for (i = 0; i < 5; i++) { |
---|
574 | | - if (cw_por(cw_bat) == 0) |
---|
| 435 | + if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) { |
---|
| 436 | + int i; |
---|
| 437 | + |
---|
| 438 | + for (i = 0; i < CW2015_RESET_TRIES; i++) { |
---|
| 439 | + if (!cw_power_on_reset(cw_bat)) |
---|
575 | 440 | break; |
---|
576 | 441 | } |
---|
577 | 442 | } |
---|
578 | | - cw_update_capacity(cw_bat); |
---|
579 | | - cw_update_vol(cw_bat); |
---|
| 443 | + cw_update_soc(cw_bat); |
---|
| 444 | + cw_update_voltage(cw_bat); |
---|
580 | 445 | cw_update_charge_status(cw_bat); |
---|
581 | 446 | cw_update_status(cw_bat); |
---|
582 | 447 | cw_update_time_to_empty(cw_bat); |
---|
583 | 448 | } |
---|
584 | | - /* Add for battery swap end */ |
---|
585 | | - cw_printk("charger_mod = %d\n", cw_bat->charger_mode); |
---|
586 | | - cw_printk("status = %d\n", cw_bat->status); |
---|
587 | | - cw_printk("capacity = %d\n", cw_bat->capacity); |
---|
588 | | - cw_printk("voltage = %d\n", cw_bat->voltage); |
---|
| 449 | + dev_dbg(cw_bat->dev, "charger_attached = %d\n", cw_bat->charger_attached); |
---|
| 450 | + dev_dbg(cw_bat->dev, "status = %d\n", cw_bat->status); |
---|
| 451 | + dev_dbg(cw_bat->dev, "soc = %d%%\n", cw_bat->soc); |
---|
| 452 | + dev_dbg(cw_bat->dev, "voltage = %dmV\n", cw_bat->voltage_mv); |
---|
589 | 453 | |
---|
590 | | -#ifdef CONFIG_PM |
---|
591 | | - if (cw_bat->suspend_resume_mark == 1) |
---|
592 | | - cw_bat->suspend_resume_mark = 0; |
---|
593 | | -#endif |
---|
594 | | - |
---|
595 | | - if (cw_bat->bat_change == 1) { |
---|
| 454 | + if (cw_bat->battery_changed) |
---|
596 | 455 | power_supply_changed(cw_bat->rk_bat); |
---|
597 | | - cw_bat->bat_change = 0; |
---|
598 | | - } |
---|
| 456 | + cw_bat->battery_changed = false; |
---|
| 457 | + |
---|
599 | 458 | queue_delayed_work(cw_bat->battery_workqueue, |
---|
600 | 459 | &cw_bat->battery_delay_work, |
---|
601 | | - msecs_to_jiffies(cw_bat->monitor_sec)); |
---|
| 460 | + msecs_to_jiffies(cw_bat->poll_interval_ms)); |
---|
| 461 | +} |
---|
| 462 | + |
---|
| 463 | +static bool cw_battery_valid_time_to_empty(struct cw_battery *cw_bat) |
---|
| 464 | +{ |
---|
| 465 | + return cw_bat->time_to_empty > 0 && |
---|
| 466 | + cw_bat->time_to_empty < CW2015_MASK_SOC && |
---|
| 467 | + cw_bat->status == POWER_SUPPLY_STATUS_DISCHARGING; |
---|
602 | 468 | } |
---|
603 | 469 | |
---|
604 | 470 | static int cw_get_capacity_leve(struct cw_battery *cw_bat) |
---|
605 | 471 | { |
---|
606 | | - if (cw_bat->bat_mode == MODE_VIRTUAL) |
---|
607 | | - return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
---|
608 | | - |
---|
609 | | - if (cw_bat->capacity < 1) |
---|
| 472 | + if (cw_bat->soc < 1) |
---|
610 | 473 | return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
---|
611 | | - else if (cw_bat->capacity <= 20) |
---|
| 474 | + else if (cw_bat->soc <= 20) |
---|
612 | 475 | return POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
---|
613 | | - else if (cw_bat->capacity <= 70) |
---|
| 476 | + else if (cw_bat->soc <= 70) |
---|
614 | 477 | return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
---|
615 | | - else if (cw_bat->capacity <= 90) |
---|
| 478 | + else if (cw_bat->soc <= 90) |
---|
616 | 479 | return POWER_SUPPLY_CAPACITY_LEVEL_HIGH; |
---|
617 | 480 | else |
---|
618 | 481 | return POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
---|
.. | .. |
---|
622 | 485 | enum power_supply_property psp, |
---|
623 | 486 | union power_supply_propval *val) |
---|
624 | 487 | { |
---|
625 | | - int ret = 0; |
---|
626 | 488 | struct cw_battery *cw_bat; |
---|
627 | 489 | |
---|
628 | 490 | cw_bat = power_supply_get_drvdata(psy); |
---|
629 | 491 | switch (psp) { |
---|
630 | 492 | case POWER_SUPPLY_PROP_CAPACITY: |
---|
631 | | - val->intval = cw_bat->capacity; |
---|
632 | | - if (cw_bat->bat_mode == MODE_VIRTUAL) |
---|
633 | | - val->intval = VIRTUAL_SOC; |
---|
| 493 | + val->intval = cw_bat->soc; |
---|
634 | 494 | break; |
---|
| 495 | + |
---|
635 | 496 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
---|
636 | 497 | val->intval = cw_get_capacity_leve(cw_bat); |
---|
637 | 498 | break; |
---|
| 499 | + |
---|
638 | 500 | case POWER_SUPPLY_PROP_STATUS: |
---|
639 | 501 | val->intval = cw_bat->status; |
---|
640 | | - if (cw_bat->bat_mode == MODE_VIRTUAL) |
---|
641 | | - val->intval = VIRTUAL_STATUS; |
---|
642 | 502 | break; |
---|
643 | 503 | |
---|
644 | | - case POWER_SUPPLY_PROP_HEALTH: |
---|
645 | | - val->intval = POWER_SUPPLY_HEALTH_GOOD; |
---|
646 | | - break; |
---|
647 | 504 | case POWER_SUPPLY_PROP_PRESENT: |
---|
648 | | - val->intval = cw_bat->voltage <= 0 ? 0 : 1; |
---|
649 | | - if (cw_bat->bat_mode == MODE_VIRTUAL) |
---|
650 | | - val->intval = VIRTUAL_PRESET; |
---|
| 505 | + val->intval = !!cw_bat->voltage_mv; |
---|
651 | 506 | break; |
---|
652 | 507 | |
---|
653 | 508 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
---|
654 | | - val->intval = cw_bat->voltage * 1000; |
---|
655 | | - if (cw_bat->bat_mode == MODE_VIRTUAL) |
---|
656 | | - val->intval = VIRTUAL_VOLTAGE * 1000; |
---|
| 509 | + val->intval = cw_bat->voltage_mv * 1000; |
---|
657 | 510 | break; |
---|
658 | 511 | |
---|
659 | 512 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
---|
660 | | - val->intval = cw_bat->time_to_empty; |
---|
661 | | - if (cw_bat->bat_mode == MODE_VIRTUAL) |
---|
662 | | - val->intval = VIRTUAL_TIME2EMPTY; |
---|
| 513 | + if (cw_battery_valid_time_to_empty(cw_bat)) |
---|
| 514 | + val->intval = cw_bat->time_to_empty; |
---|
| 515 | + else |
---|
| 516 | + val->intval = 0; |
---|
663 | 517 | break; |
---|
664 | 518 | |
---|
665 | 519 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
---|
.. | .. |
---|
672 | 526 | |
---|
673 | 527 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
---|
674 | 528 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
---|
675 | | - val->intval = cw_bat->plat_data.design_capacity * 1000; |
---|
| 529 | + if (cw_bat->battery.charge_full_design_uah > 0) |
---|
| 530 | + val->intval = cw_bat->battery.charge_full_design_uah; |
---|
| 531 | + else |
---|
| 532 | + val->intval = 0; |
---|
676 | 533 | break; |
---|
677 | 534 | |
---|
678 | | - case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
---|
679 | | - val->intval = 3600; |
---|
680 | | - break; |
---|
| 535 | + case POWER_SUPPLY_PROP_CURRENT_NOW: |
---|
| 536 | + if (cw_battery_valid_time_to_empty(cw_bat) && |
---|
| 537 | + cw_bat->battery.charge_full_design_uah > 0) { |
---|
| 538 | + /* calculate remaining capacity */ |
---|
| 539 | + val->intval = cw_bat->battery.charge_full_design_uah; |
---|
| 540 | + val->intval = val->intval * cw_bat->soc / 100; |
---|
681 | 541 | |
---|
682 | | - case POWER_SUPPLY_PROP_TEMP: |
---|
683 | | - val->intval = VIRTUAL_TEMPERATURE; |
---|
| 542 | + /* estimate current based on time to empty */ |
---|
| 543 | + val->intval = 60 * val->intval / cw_bat->time_to_empty; |
---|
| 544 | + } else { |
---|
| 545 | + val->intval = 0; |
---|
| 546 | + } |
---|
| 547 | + |
---|
684 | 548 | break; |
---|
685 | 549 | |
---|
686 | 550 | default: |
---|
687 | 551 | break; |
---|
688 | 552 | } |
---|
689 | | - return ret; |
---|
| 553 | + return 0; |
---|
690 | 554 | } |
---|
691 | 555 | |
---|
692 | 556 | static enum power_supply_property cw_battery_properties[] = { |
---|
693 | 557 | POWER_SUPPLY_PROP_CAPACITY, |
---|
694 | 558 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
---|
695 | 559 | POWER_SUPPLY_PROP_STATUS, |
---|
696 | | - POWER_SUPPLY_PROP_HEALTH, |
---|
697 | 560 | POWER_SUPPLY_PROP_PRESENT, |
---|
698 | 561 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
---|
699 | 562 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
---|
.. | .. |
---|
701 | 564 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
---|
702 | 565 | POWER_SUPPLY_PROP_CHARGE_FULL, |
---|
703 | 566 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
---|
704 | | - POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
---|
705 | | - POWER_SUPPLY_PROP_TEMP, |
---|
| 567 | + POWER_SUPPLY_PROP_CURRENT_NOW, |
---|
706 | 568 | }; |
---|
707 | 569 | |
---|
708 | 570 | static const struct power_supply_desc cw2015_bat_desc = { |
---|
709 | | - .name = "rk-bat", |
---|
| 571 | + .name = "cw2015-battery", |
---|
710 | 572 | .type = POWER_SUPPLY_TYPE_BATTERY, |
---|
711 | 573 | .properties = cw_battery_properties, |
---|
712 | 574 | .num_properties = ARRAY_SIZE(cw_battery_properties), |
---|
713 | 575 | .get_property = cw_battery_get_property, |
---|
714 | 576 | }; |
---|
715 | 577 | |
---|
716 | | -#ifdef CONFIG_OF |
---|
717 | | -static int cw2015_parse_dt(struct cw_battery *cw_bat) |
---|
| 578 | +static int cw2015_parse_properties(struct cw_battery *cw_bat) |
---|
718 | 579 | { |
---|
719 | | - struct device *dev = &cw_bat->client->dev; |
---|
720 | | - struct device_node *node = dev->of_node; |
---|
721 | | - struct property *prop; |
---|
| 580 | + struct device *dev = cw_bat->dev; |
---|
722 | 581 | int length; |
---|
723 | | - u32 value; |
---|
724 | 582 | int ret; |
---|
725 | | - struct cw_bat_platform_data *data = &cw_bat->plat_data; |
---|
726 | | - struct gpio_desc *hw_id0_io; |
---|
727 | | - struct gpio_desc *hw_id1_io; |
---|
728 | | - int hw_id0_val; |
---|
729 | | - int hw_id1_val; |
---|
730 | 583 | |
---|
731 | | - if (!node) |
---|
732 | | - return -ENODEV; |
---|
733 | | - |
---|
734 | | - memset(data, 0, sizeof(*data)); |
---|
735 | | - |
---|
736 | | - ret = of_property_read_u32(node, "hw_id_check", &value); |
---|
737 | | - if (!ret && value) { |
---|
738 | | - hw_id0_io = gpiod_get_optional(dev, "hw-id0", GPIOD_IN); |
---|
739 | | - if (!hw_id0_io) |
---|
740 | | - return -EINVAL; |
---|
741 | | - if (IS_ERR(hw_id0_io)) |
---|
742 | | - return PTR_ERR(hw_id0_io); |
---|
743 | | - |
---|
744 | | - hw_id0_val = gpiod_get_value(hw_id0_io); |
---|
745 | | - gpiod_put(hw_id0_io); |
---|
746 | | - |
---|
747 | | - hw_id1_io = gpiod_get_optional(dev, "hw-id1", GPIOD_IN); |
---|
748 | | - if (!hw_id1_io) |
---|
749 | | - return -EINVAL; |
---|
750 | | - if (IS_ERR(hw_id1_io)) |
---|
751 | | - return PTR_ERR(hw_id1_io); |
---|
752 | | - |
---|
753 | | - hw_id1_val = gpiod_get_value(hw_id1_io); |
---|
754 | | - gpiod_put(hw_id1_io); |
---|
755 | | - |
---|
756 | | - /* |
---|
757 | | - * ID1 = 0, ID0 = 1 : Battery |
---|
758 | | - * ID1 = 1, ID0 = 0 : Dual Battery |
---|
759 | | - * ID1 = 0, ID0 = 0 : Adapter |
---|
760 | | - */ |
---|
761 | | - if (hw_id0_val == 1 && hw_id1_val == 0) |
---|
762 | | - cw_bat->dual_battery = false; |
---|
763 | | - else if (hw_id0_val == 0 && hw_id1_val == 1) |
---|
764 | | - cw_bat->dual_battery = true; |
---|
765 | | - else |
---|
766 | | - return -EINVAL; |
---|
767 | | - } |
---|
768 | | - |
---|
769 | | - /* determine the number of config info */ |
---|
770 | | - prop = of_find_property(node, "bat_config_info", &length); |
---|
771 | | - if (!prop) |
---|
| 584 | + length = device_property_count_u8(dev, "cellwise,battery-profile"); |
---|
| 585 | + if (length < 0) { |
---|
| 586 | + dev_warn(cw_bat->dev, |
---|
| 587 | + "No battery-profile found, using current flash contents\n"); |
---|
| 588 | + } else if (length != CW2015_SIZE_BATINFO) { |
---|
| 589 | + dev_err(cw_bat->dev, "battery-profile must be %d bytes\n", |
---|
| 590 | + CW2015_SIZE_BATINFO); |
---|
772 | 591 | return -EINVAL; |
---|
773 | | - |
---|
774 | | - length /= sizeof(u32); |
---|
775 | | - |
---|
776 | | - if (length > 0) { |
---|
777 | | - size_t size = sizeof(*data->cw_bat_config_info) * length; |
---|
778 | | - |
---|
779 | | - data->cw_bat_config_info = devm_kzalloc(dev, size, GFP_KERNEL); |
---|
780 | | - if (!data->cw_bat_config_info) |
---|
| 592 | + } else { |
---|
| 593 | + cw_bat->bat_profile = devm_kzalloc(dev, length, GFP_KERNEL); |
---|
| 594 | + if (!cw_bat->bat_profile) |
---|
781 | 595 | return -ENOMEM; |
---|
782 | 596 | |
---|
783 | | - ret = of_property_read_u32_array(node, "bat_config_info", |
---|
784 | | - data->cw_bat_config_info, |
---|
785 | | - length); |
---|
786 | | - if (ret < 0) |
---|
| 597 | + ret = device_property_read_u8_array(dev, |
---|
| 598 | + "cellwise,battery-profile", |
---|
| 599 | + cw_bat->bat_profile, |
---|
| 600 | + length); |
---|
| 601 | + if (ret) |
---|
787 | 602 | return ret; |
---|
788 | 603 | } |
---|
789 | 604 | |
---|
790 | | - cw_bat->bat_mode = MODE_BATTARY; |
---|
791 | | - cw_bat->monitor_sec = DEFAULT_MONITOR_SEC * TIMER_MS_COUNTS; |
---|
| 605 | + cw_bat->dual_cell = device_property_read_bool(dev, "cellwise,dual-cell"); |
---|
792 | 606 | |
---|
793 | | - ret = of_property_read_u32(node, "divider_res1", &value); |
---|
794 | | - if (ret < 0) |
---|
795 | | - value = 0; |
---|
796 | | - data->divider_res1 = value; |
---|
797 | | - |
---|
798 | | - ret = of_property_read_u32(node, "divider_res2", &value); |
---|
799 | | - if (ret < 0) |
---|
800 | | - value = 0; |
---|
801 | | - data->divider_res2 = value; |
---|
802 | | - |
---|
803 | | - ret = of_property_read_u32(node, "virtual_power", &value); |
---|
804 | | - if (ret < 0) |
---|
805 | | - value = 0; |
---|
806 | | - cw_bat->bat_mode = value; |
---|
807 | | - |
---|
808 | | - ret = of_property_read_u32(node, "monitor_sec", &value); |
---|
809 | | - if (ret < 0) |
---|
810 | | - dev_err(dev, "monitor_sec missing!\n"); |
---|
811 | | - else |
---|
812 | | - cw_bat->monitor_sec = value * TIMER_MS_COUNTS; |
---|
813 | | - |
---|
814 | | - ret = of_property_read_u32(node, "design_capacity", &value); |
---|
815 | | - if (ret < 0) { |
---|
816 | | - dev_err(dev, "design_capacity missing!\n"); |
---|
817 | | - data->design_capacity = 2000; |
---|
818 | | - } else { |
---|
819 | | - data->design_capacity = value; |
---|
| 607 | + ret = device_property_read_u32(dev, "cellwise,monitor-interval-ms", |
---|
| 608 | + &cw_bat->poll_interval_ms); |
---|
| 609 | + if (ret) { |
---|
| 610 | + dev_dbg(cw_bat->dev, "Using default poll interval\n"); |
---|
| 611 | + cw_bat->poll_interval_ms = CW2015_DEFAULT_POLL_INTERVAL_MS; |
---|
820 | 612 | } |
---|
821 | 613 | |
---|
822 | 614 | return 0; |
---|
823 | 615 | } |
---|
824 | | -#else |
---|
825 | | -static int cw2015_parse_dt(struct cw_battery *cw_bat) |
---|
826 | | -{ |
---|
827 | | - return -ENODEV; |
---|
828 | | -} |
---|
829 | | -#endif |
---|
830 | 616 | |
---|
831 | | -static int cw_bat_probe(struct i2c_client *client, |
---|
832 | | - const struct i2c_device_id *id) |
---|
| 617 | +static const struct regmap_range regmap_ranges_rd_yes[] = { |
---|
| 618 | + regmap_reg_range(CW2015_REG_VERSION, CW2015_REG_VERSION), |
---|
| 619 | + regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_CONFIG), |
---|
| 620 | + regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE), |
---|
| 621 | + regmap_reg_range(CW2015_REG_BATINFO, |
---|
| 622 | + CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1), |
---|
| 623 | +}; |
---|
| 624 | + |
---|
| 625 | +static const struct regmap_access_table regmap_rd_table = { |
---|
| 626 | + .yes_ranges = regmap_ranges_rd_yes, |
---|
| 627 | + .n_yes_ranges = 4, |
---|
| 628 | +}; |
---|
| 629 | + |
---|
| 630 | +static const struct regmap_range regmap_ranges_wr_yes[] = { |
---|
| 631 | + regmap_reg_range(CW2015_REG_RRT_ALERT, CW2015_REG_CONFIG), |
---|
| 632 | + regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE), |
---|
| 633 | + regmap_reg_range(CW2015_REG_BATINFO, |
---|
| 634 | + CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1), |
---|
| 635 | +}; |
---|
| 636 | + |
---|
| 637 | +static const struct regmap_access_table regmap_wr_table = { |
---|
| 638 | + .yes_ranges = regmap_ranges_wr_yes, |
---|
| 639 | + .n_yes_ranges = 3, |
---|
| 640 | +}; |
---|
| 641 | + |
---|
| 642 | +static const struct regmap_range regmap_ranges_vol_yes[] = { |
---|
| 643 | + regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_SOC + 1), |
---|
| 644 | +}; |
---|
| 645 | + |
---|
| 646 | +static const struct regmap_access_table regmap_vol_table = { |
---|
| 647 | + .yes_ranges = regmap_ranges_vol_yes, |
---|
| 648 | + .n_yes_ranges = 1, |
---|
| 649 | +}; |
---|
| 650 | + |
---|
| 651 | +static const struct regmap_config cw2015_regmap_config = { |
---|
| 652 | + .reg_bits = 8, |
---|
| 653 | + .val_bits = 8, |
---|
| 654 | + .rd_table = ®map_rd_table, |
---|
| 655 | + .wr_table = ®map_wr_table, |
---|
| 656 | + .volatile_table = ®map_vol_table, |
---|
| 657 | + .max_register = CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1, |
---|
| 658 | +}; |
---|
| 659 | + |
---|
| 660 | +static int cw_bat_probe(struct i2c_client *client) |
---|
833 | 661 | { |
---|
834 | 662 | int ret; |
---|
835 | 663 | struct cw_battery *cw_bat; |
---|
836 | | - struct power_supply_config psy_cfg = {0}; |
---|
| 664 | + struct power_supply_config psy_cfg = { 0 }; |
---|
837 | 665 | |
---|
838 | 666 | cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL); |
---|
839 | | - if (!cw_bat) { |
---|
840 | | - dev_err(&client->dev, |
---|
841 | | - "fail to allocate memory for cw2015\n"); |
---|
| 667 | + if (!cw_bat) |
---|
842 | 668 | return -ENOMEM; |
---|
843 | | - } |
---|
844 | 669 | |
---|
845 | 670 | i2c_set_clientdata(client, cw_bat); |
---|
846 | | - cw_bat->client = client; |
---|
| 671 | + cw_bat->dev = &client->dev; |
---|
| 672 | + cw_bat->soc = 1; |
---|
847 | 673 | |
---|
848 | | - ret = cw2015_parse_dt(cw_bat); |
---|
849 | | - if (ret < 0) { |
---|
850 | | - dev_err(&client->dev, |
---|
851 | | - "failed to find cw2015 platform data\n"); |
---|
852 | | - return -1; |
---|
| 674 | + ret = cw2015_parse_properties(cw_bat); |
---|
| 675 | + if (ret) { |
---|
| 676 | + dev_err(cw_bat->dev, "Failed to parse cw2015 properties\n"); |
---|
| 677 | + return ret; |
---|
853 | 678 | } |
---|
854 | 679 | |
---|
855 | | - cw_bat->capacity = 1; |
---|
856 | | - cw_bat->voltage = 0; |
---|
857 | | - cw_bat->status = 0; |
---|
858 | | - cw_bat->suspend_resume_mark = 0; |
---|
859 | | - cw_bat->charger_mode = NO_CHARGING; |
---|
860 | | - cw_bat->bat_change = 0; |
---|
| 680 | + cw_bat->regmap = devm_regmap_init_i2c(client, &cw2015_regmap_config); |
---|
| 681 | + if (IS_ERR(cw_bat->regmap)) { |
---|
| 682 | + dev_err(cw_bat->dev, "Failed to allocate regmap: %ld\n", |
---|
| 683 | + PTR_ERR(cw_bat->regmap)); |
---|
| 684 | + return PTR_ERR(cw_bat->regmap); |
---|
| 685 | + } |
---|
861 | 686 | |
---|
862 | 687 | ret = cw_init(cw_bat); |
---|
863 | 688 | if (ret) { |
---|
864 | | - pr_err("%s cw_init error\n", __func__); |
---|
| 689 | + dev_err(cw_bat->dev, "Init failed: %d\n", ret); |
---|
865 | 690 | return ret; |
---|
866 | 691 | } |
---|
867 | 692 | |
---|
868 | 693 | psy_cfg.drv_data = cw_bat; |
---|
| 694 | + psy_cfg.fwnode = dev_fwnode(cw_bat->dev); |
---|
869 | 695 | |
---|
870 | 696 | cw_bat->rk_bat = devm_power_supply_register(&client->dev, |
---|
871 | | - &cw2015_bat_desc, &psy_cfg); |
---|
| 697 | + &cw2015_bat_desc, |
---|
| 698 | + &psy_cfg); |
---|
872 | 699 | if (IS_ERR(cw_bat->rk_bat)) { |
---|
873 | | - dev_err(&cw_bat->client->dev, |
---|
874 | | - "power supply register rk_bat error\n"); |
---|
875 | | - return -1; |
---|
| 700 | + /* try again if this happens */ |
---|
| 701 | + dev_err_probe(&client->dev, PTR_ERR(cw_bat->rk_bat), |
---|
| 702 | + "Failed to register power supply\n"); |
---|
| 703 | + return PTR_ERR(cw_bat->rk_bat); |
---|
| 704 | + } |
---|
| 705 | + |
---|
| 706 | + ret = power_supply_get_battery_info(cw_bat->rk_bat, &cw_bat->battery); |
---|
| 707 | + if (ret) { |
---|
| 708 | + dev_warn(cw_bat->dev, |
---|
| 709 | + "No monitored battery, some properties will be missing\n"); |
---|
876 | 710 | } |
---|
877 | 711 | |
---|
878 | 712 | cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery"); |
---|
879 | 713 | INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work); |
---|
880 | 714 | queue_delayed_work(cw_bat->battery_workqueue, |
---|
881 | 715 | &cw_bat->battery_delay_work, msecs_to_jiffies(10)); |
---|
882 | | - |
---|
883 | | - dev_info(&cw_bat->client->dev, |
---|
884 | | - "cw2015/cw2013 driver v1.2 probe sucess\n"); |
---|
885 | 716 | return 0; |
---|
886 | 717 | } |
---|
887 | 718 | |
---|
888 | | -#ifdef CONFIG_PM |
---|
889 | | -static int cw_bat_suspend(struct device *dev) |
---|
| 719 | +static int __maybe_unused cw_bat_suspend(struct device *dev) |
---|
890 | 720 | { |
---|
891 | 721 | struct i2c_client *client = to_i2c_client(dev); |
---|
892 | 722 | struct cw_battery *cw_bat = i2c_get_clientdata(client); |
---|
893 | | - read_persistent_clock(&cw_bat->suspend_time_before); |
---|
894 | | - cancel_delayed_work(&cw_bat->battery_delay_work); |
---|
| 723 | + |
---|
| 724 | + cancel_delayed_work_sync(&cw_bat->battery_delay_work); |
---|
895 | 725 | return 0; |
---|
896 | 726 | } |
---|
897 | 727 | |
---|
898 | | -static int cw_bat_resume(struct device *dev) |
---|
| 728 | +static int __maybe_unused cw_bat_resume(struct device *dev) |
---|
899 | 729 | { |
---|
900 | 730 | struct i2c_client *client = to_i2c_client(dev); |
---|
901 | 731 | struct cw_battery *cw_bat = i2c_get_clientdata(client); |
---|
902 | | - cw_bat->suspend_resume_mark = 1; |
---|
903 | | - read_persistent_clock(&cw_bat->after); |
---|
904 | | - cw_bat->after = timespec_sub(cw_bat->after, |
---|
905 | | - cw_bat->suspend_time_before); |
---|
| 732 | + |
---|
906 | 733 | queue_delayed_work(cw_bat->battery_workqueue, |
---|
907 | | - &cw_bat->battery_delay_work, msecs_to_jiffies(2)); |
---|
| 734 | + &cw_bat->battery_delay_work, 0); |
---|
908 | 735 | return 0; |
---|
909 | 736 | } |
---|
910 | 737 | |
---|
911 | | -static const struct dev_pm_ops cw_bat_pm_ops = { |
---|
912 | | - .suspend = cw_bat_suspend, |
---|
913 | | - .resume = cw_bat_resume, |
---|
914 | | -}; |
---|
915 | | -#endif |
---|
| 738 | +static SIMPLE_DEV_PM_OPS(cw_bat_pm_ops, cw_bat_suspend, cw_bat_resume); |
---|
916 | 739 | |
---|
917 | 740 | static int cw_bat_remove(struct i2c_client *client) |
---|
918 | 741 | { |
---|
919 | 742 | struct cw_battery *cw_bat = i2c_get_clientdata(client); |
---|
920 | 743 | |
---|
921 | | - dev_dbg(&cw_bat->client->dev, "%s\n", __func__); |
---|
922 | | - cancel_delayed_work(&cw_bat->battery_delay_work); |
---|
| 744 | + cancel_delayed_work_sync(&cw_bat->battery_delay_work); |
---|
| 745 | + power_supply_put_battery_info(cw_bat->rk_bat, &cw_bat->battery); |
---|
923 | 746 | return 0; |
---|
924 | 747 | } |
---|
925 | 748 | |
---|
926 | 749 | static const struct i2c_device_id cw_bat_id_table[] = { |
---|
927 | | - {"cw201x", 0}, |
---|
928 | | - {} |
---|
| 750 | + { "cw2015", 0 }, |
---|
| 751 | + { } |
---|
929 | 752 | }; |
---|
| 753 | + |
---|
| 754 | +static const struct of_device_id cw2015_of_match[] = { |
---|
| 755 | + { .compatible = "cellwise,cw2015" }, |
---|
| 756 | + { } |
---|
| 757 | +}; |
---|
| 758 | +MODULE_DEVICE_TABLE(of, cw2015_of_match); |
---|
930 | 759 | |
---|
931 | 760 | static struct i2c_driver cw_bat_driver = { |
---|
932 | 761 | .driver = { |
---|
933 | | - .name = "cw201x", |
---|
934 | | -#ifdef CONFIG_PM |
---|
| 762 | + .name = "cw2015", |
---|
| 763 | + .of_match_table = cw2015_of_match, |
---|
935 | 764 | .pm = &cw_bat_pm_ops, |
---|
936 | | -#endif |
---|
937 | 765 | }, |
---|
938 | | - .probe = cw_bat_probe, |
---|
| 766 | + .probe_new = cw_bat_probe, |
---|
939 | 767 | .remove = cw_bat_remove, |
---|
940 | 768 | .id_table = cw_bat_id_table, |
---|
941 | 769 | }; |
---|
942 | 770 | |
---|
943 | | -static int __init cw_bat_init(void) |
---|
944 | | -{ |
---|
945 | | - return i2c_add_driver(&cw_bat_driver); |
---|
946 | | -} |
---|
947 | | - |
---|
948 | | -static void __exit cw_bat_exit(void) |
---|
949 | | -{ |
---|
950 | | - i2c_del_driver(&cw_bat_driver); |
---|
951 | | -} |
---|
952 | | - |
---|
953 | | -module_init(cw_bat_init); |
---|
954 | | -module_exit(cw_bat_exit); |
---|
| 771 | +module_i2c_driver(cw_bat_driver); |
---|
955 | 772 | |
---|
956 | 773 | MODULE_AUTHOR("xhc<xhc@rock-chips.com>"); |
---|
| 774 | +MODULE_AUTHOR("Tobias Schramm <t.schramm@manjaro.org>"); |
---|
957 | 775 | MODULE_DESCRIPTION("cw2015/cw2013 battery driver"); |
---|
958 | 776 | MODULE_LICENSE("GPL"); |
---|