hc
2023-08-30 862c27fc9920c83318c784bfdadf43a65df1ec8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * (C) Copyright 2008-2015 Fuzhou Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
#include <common.h>
#include <asm/gpio.h>
#include <dm.h>
#include <dm/device.h>
#include <errno.h>
#include <fdtdec.h>
#include <i2c.h>
#include <linux/usb/phy-rockchip-usb2.h>
#include <malloc.h>
#include <power/battery.h>
#include <power/fuel_gauge.h>
#include <power/pmic.h>
#include "fg_regs.h"
 
DECLARE_GLOBAL_DATA_PTR;
 
#define COMPAT_ROCKCHIP_CW201X "cw201x"
 
#define REG_VERSION        0x0
#define REG_VCELL        0x2
#define REG_SOC            0x4
#define REG_RRT_ALERT        0x6
#define REG_CONFIG        0x8
#define REG_MODE        0xA
#define REG_BATINFO        0x10
 
#define MODE_SLEEP_MASK        (0x3 << 6)
#define MODE_SLEEP        (0x3 << 6)
#define MODE_NORMAL        (0x0 << 6)
#define MODE_QUICK_START    (0x3 << 4)
#define MODE_RESTART        (0xf << 0)
 
#define CONFIG_UPDATE_FLG    (0x1 << 1)
#define ATHD            (0x0 << 3)
 
enum charger_type {
   CHARGER_TYPE_NO = 0,
   CHARGER_TYPE_USB,
   CHARGER_TYPE_AC,
   CHARGER_TYPE_DC,
   CHARGER_TYPE_UNDEF,
};
 
struct cw201x_info {
   struct udevice *dev;
   int capacity;
   u32 *cw_bat_config_info;
   int divider_res1;
   int divider_res2;
   int hw_id_check;
   struct gpio_desc hw_id0;
   struct gpio_desc hw_id1;
   int support_dc_adp;
   struct gpio_desc dc_det_gpio;
   int dc_det_flag;
   bool dual_cell;
};
 
static u8 cw201x_read(struct cw201x_info *cw201x, u8 reg)
{
   u8 val;
   int ret;
 
   ret = dm_i2c_read(cw201x->dev, reg, &val, 1);
   if (ret) {
       debug("write error to device: %p register: %#x!",
             cw201x->dev, reg);
       return ret;
   }
 
   return val;
}
 
static int cw201x_write(struct cw201x_info *cw201x, u8 reg, u8 val)
{
   int ret;
 
   ret = dm_i2c_write(cw201x->dev, reg, &val, 1);
   if (ret) {
       debug("write error to device: %p register: %#x!",
             cw201x->dev, reg);
       return ret;
   }
 
   return 0;
}
 
static u16 cw201x_read_half_word(struct cw201x_info *cw201x, int reg)
{
   u8 vall, valh;
   u16 val;
 
   valh = cw201x_read(cw201x, reg);
   vall = cw201x_read(cw201x, reg + 1);
   val = ((u16)valh << 8) | vall;
 
   return val;
}
 
static int cw201x_parse_config_info(struct cw201x_info *cw201x)
{
   int ret;
   int i, len, size;
   const u8 *info;
   struct udevice *dev = cw201x->dev;
 
   if (dev_read_prop(dev, "bat_config_info", &len)) {
       len /= sizeof(u32);
       size = sizeof(*cw201x->cw_bat_config_info) * len;
       cw201x->cw_bat_config_info = calloc(size, 1);
       if (!cw201x->cw_bat_config_info) {
           debug("calloc cw_bat_config_info fail\n");
           return -EINVAL;
       }
       ret = dev_read_u32_array(dev, "bat_config_info",
                    cw201x->cw_bat_config_info, len);
       if (ret) {
           debug("fdtdec_get cw_bat_config_info fail\n");
           return -EINVAL;
       }
 
       return 0;
   }
 
   if (!dev_read_prop(dev, "cellwise,battery-profile", &len))
       return -EINVAL;
 
   size = sizeof(*cw201x->cw_bat_config_info) * len;
   cw201x->cw_bat_config_info = calloc(size, 1);
   if (!cw201x->cw_bat_config_info) {
       debug("calloc cw_bat_config_info fail\n");
       return -EINVAL;
   }
 
   info = dev_read_u8_array_ptr(dev, "cellwise,battery-profile", len);
   if (!info) {
       debug("fdtdec_get battery profile fail\n");
       return -EINVAL;
   }
   for (i = 0; i < len; i++) {
       cw201x->cw_bat_config_info[i] = info[i];
       debug("%#x ", cw201x->cw_bat_config_info[i]);
       if ((i+1) % 8 == 0)
           debug("\n");
   }
 
   return 0;
}
 
static int cw201x_ofdata_to_platdata(struct udevice *dev)
{
   struct cw201x_info *cw201x = dev_get_priv(dev);
   int ret;
   int hw_id0_val, hw_id1_val;
 
   cw201x->dev = dev;
   ret = cw201x_parse_config_info(cw201x);
   if (ret)
       return ret;
 
   cw201x->dual_cell = dev_read_bool(dev, "cellwise,dual-cell");
   ret = gpio_request_by_name_nodev(dev_ofnode(dev), "dc_det_gpio",
                    0, &cw201x->dc_det_gpio, GPIOD_IS_IN);
   if (!ret) {
       cw201x->support_dc_adp = 1;
       debug("DC is valid\n");
   } else {
       debug("DC is invalid, ret=%d\n", ret);
   }
 
   cw201x->hw_id_check = dev_read_u32_default(dev, "hw_id_check", 0);
   if (cw201x->hw_id_check) {
       ret = gpio_request_by_name_nodev(dev_ofnode(dev),
                        "hw_id0_gpio", 0,
                        &cw201x->hw_id0, GPIOD_IS_IN);
       if (ret)
           return -EINVAL;
       hw_id0_val = dm_gpio_get_value(&cw201x->hw_id0);
 
       ret = gpio_request_by_name_nodev(dev_ofnode(dev),
                        "hw_id1_gpio", 0,
                        &cw201x->hw_id1, GPIOD_IS_IN);
       if (ret)
           return -EINVAL;
       hw_id1_val = dm_gpio_get_value(&cw201x->hw_id1);
 
       /* ID1 = 0, ID0 = 1 : Battery */
       if (!hw_id0_val || hw_id1_val)
           return -EINVAL;
   }
 
   cw201x->divider_res1 = dev_read_u32_default(dev, "divider_res1", 0);
   cw201x->divider_res2 = dev_read_u32_default(dev, "divider_res2", 0);
 
   return 0;
}
 
static int cw201x_get_vol(struct cw201x_info *cw201x)
{
   u16 value16, value16_1, value16_2, value16_3;
   int voltage;
   int res1, res2;
   int retry = 0;
 
__retry:
   value16 = cw201x_read_half_word(cw201x, REG_VCELL);
   if (value16 < 0)
       return -1;
 
   value16_1 = cw201x_read_half_word(cw201x, REG_VCELL);
   if (value16_1 < 0)
       return -1;
 
   value16_2 = cw201x_read_half_word(cw201x, REG_VCELL);
   if (value16_2 < 0)
       return -1;
 
   if (value16 > value16_1) {
       value16_3 = value16;
       value16 = value16_1;
       value16_1 = value16_3;
   }
 
   if (value16_1 > value16_2) {
       value16_3 = value16_1;
       value16_1 = value16_2;
       value16_2 = value16_3;
   }
 
   if (value16 > value16_1) {
       value16_3 = value16;
       value16 = value16_1;
       value16_1 = value16_3;
   }
 
   voltage = value16_1 * 312 / 1024;
   if (voltage <= 0 && retry < 10) {
       retry++;
       mdelay(20);
       goto __retry;
   }
 
   if (cw201x->divider_res1 &&
       cw201x->divider_res2) {
       res1 = cw201x->divider_res1;
       res2 = cw201x->divider_res2;
       voltage = voltage * (res1 + res2) / res2;
   }
 
   if (cw201x->dual_cell)
       voltage *= 2;
 
   debug("the cw201x voltage=%d\n", voltage);
   return voltage;
}
 
static int cw201x_dwc_otg_check_dpdm(void)
{
#if defined(CONFIG_PHY_ROCKCHIP_INNO_USB2) && !defined(CONFIG_SPL_BUILD)
   return rockchip_chg_get_type();
#else
   debug("rockchip_chg_get_type() is not implement\n");
   return CHARGER_TYPE_NO;
#endif
}
 
static int cw201x_get_usb_state(struct cw201x_info *cw201x)
{
   int charger_type;
 
   switch (cw201x_dwc_otg_check_dpdm()) {
   case 0:
       charger_type = CHARGER_TYPE_NO;
       break;
   case 1:
   case 3:
       charger_type = CHARGER_TYPE_USB;
       break;
   case 2:
       charger_type = CHARGER_TYPE_AC;
       break;
   default:
       charger_type = CHARGER_TYPE_NO;
       break;
   }
 
   return charger_type;
}
 
static bool cw201x_get_dc_state(struct cw201x_info *cw201x)
{
   if (dm_gpio_get_value(&cw201x->dc_det_gpio))
       return true;
 
   return false;
}
 
static bool cw201x_check_charge(struct cw201x_info *cw201x)
{
   if (cw201x_get_usb_state(cw201x) != CHARGER_TYPE_NO)
       return true;
   if (cw201x_get_dc_state(cw201x))
       return true;
 
   return false;
}
 
static int cw201x_get_soc(struct cw201x_info *cw201x)
{
   int cap, i = 0;
 
   while (i < 10) {
       cap = cw201x_read(cw201x, REG_SOC);
       if ((cap < 0) || (cap > 100))
           cap = cw201x->capacity;
       i++;
       if (cap)
           break;
   }
   cw201x->capacity = cap;
 
   return cw201x->capacity;
}
 
static int cw201x_update_get_soc(struct udevice *dev)
{
   struct cw201x_info *cw201x = dev_get_priv(dev);
 
   return cw201x_get_soc(cw201x);
}
 
static int cw201x_update_get_voltage(struct udevice *dev)
{
   struct cw201x_info *cw201x = dev_get_priv(dev);
 
   return cw201x_get_vol(cw201x);
}
 
static int cw201x_update_get_current(struct udevice *dev)
{
   return 0;
}
 
static bool cw201x_update_get_chrg_online(struct udevice *dev)
{
   struct cw201x_info *cw201x = dev_get_priv(dev);
 
   return cw201x_check_charge(cw201x);
}
 
static int cw201x_capability(struct udevice *dev)
{
   return FG_CAP_FUEL_GAUGE;
}
 
static struct dm_fuel_gauge_ops cw201x_fg_ops = {
   .capability = cw201x_capability,
   .get_soc = cw201x_update_get_soc,
   .get_voltage = cw201x_update_get_voltage,
   .get_current = cw201x_update_get_current,
   .get_chrg_online = cw201x_update_get_chrg_online,
};
 
static int cw201x_fg_cfg(struct cw201x_info *cw201x)
{
   u8 val = MODE_SLEEP;
   int i;
 
   if ((val & MODE_SLEEP_MASK) == MODE_SLEEP) {
       val = MODE_NORMAL;
       cw201x_write(cw201x, REG_MODE, val);
   }
 
   for (i = 0; i < 64; i++) {
       cw201x_write(cw201x, REG_BATINFO + i,
                (u8)cw201x->cw_bat_config_info[i]);
   }
 
   return 0;
}
 
static int cw201x_fg_probe(struct udevice *dev)
{
   struct cw201x_info *cw201x = dev_get_priv(dev);
 
   cw201x->dev = dev;
   cw201x_fg_cfg(cw201x);
 
   debug("vol: %d, soc: %d\n",
         cw201x_get_vol(cw201x), cw201x_get_soc(cw201x));
 
   return 0;
}
 
static const struct udevice_id cw201x_ids[] = {
   { .compatible = "cw201x" },
   { .compatible = "cellwise,cw2015" },
   { }
};
 
U_BOOT_DRIVER(cw201x_fg) = {
   .name = "cw201x_fg",
   .id = UCLASS_FG,
   .of_match = cw201x_ids,
   .probe = cw201x_fg_probe,
   .ofdata_to_platdata = cw201x_ofdata_to_platdata,
   .ops = &cw201x_fg_ops,
   .priv_auto_alloc_size = sizeof(struct cw201x_info),
};