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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
 * (C) Copyright 2022 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
 
#include <common.h>
#include <dm.h>
#include <errno.h>
#include <i2c.h>
#include <power/fuel_gauge.h>
#include <power/pmic.h>
#include <linux/usb/phy-rockchip-usb2.h>
 
DECLARE_GLOBAL_DATA_PTR;
 
static int dbg_enable;
 
#define CW_DBG(args...) \
   do { \
       if (dbg_enable) { \
           printf(args); \
       } \
   } while (0)
 
#define REG_CHIP_ID            0x00
#define REG_VCELL_H            0x02
#define REG_VCELL_L            0x03
#define REG_SOC_INT            0x04
#define REG_SOC_DECIMAL            0x05
#define REG_TEMP            0x06
#define REG_MODE_CONFIG            0x08
#define REG_GPIO_CONFIG            0x0A
#define REG_SOC_ALERT            0x0B
#define REG_TEMP_MAX            0x0C
#define REG_TEMP_MIN            0x0D
#define REG_CURRENT_H            0x0E
#define REG_CURRENT_L            0x0F
#define REG_T_HOST_H            0xA0
#define REG_T_HOST_L            0xA1
#define REG_USER_CONF            0xA2
#define REG_CYCLE_H            0xA4
#define REG_CYCLE_L            0xA5
#define REG_SOH                0xA6
#define REG_IC_STATE            0xA7
#define REG_FW_VERSION            0xAB
#define REG_BAT_PROFILE            0x10
 
#define CONFIG_MODE_RESTART        0x30
#define CONFIG_MODE_ACTIVE        0x00
#define CONFIG_MODE_SLEEP        0xF0
#define CONFIG_UPDATE_FLG        0x80
#define IC_VCHIP_ID            0xA0
#define IC_READY_MARK            0x0C
 
#define GPIO_ENABLE_MIN_TEMP        0
#define GPIO_ENABLE_MAX_TEMP        0
#define GPIO_ENABLE_SOC_CHANGE        0
#define GPIO_SOC_IRQ_VALUE        0x0 /* 0x7F */
#define DEFINED_MAX_TEMP        45
#define DEFINED_MIN_TEMP        0
 
#define CWFG_NAME            "cw221X"
#define SIZE_OF_PROFILE            80
 
/* mhom rsense * 1000 for convenience calculation */
#define USER_RSENSE            1500
 
#define queue_delayed_work_time        5000
#define queue_start_work_time        50
 
#define CW_SLEEP_20MS            20
#define CW_SLEEP_10MS            10
#define CW_UI_FULL            100
#define COMPLEMENT_CODE_U16        0x8000
#define CW_SLEEP_100MS            100
#define CW_SLEEP_200MS            200
#define CW_SLEEP_COUNTS            50
#define CW_TRUE                1
#define CW_RETRY_COUNT            3
#define CW_VOL_UNIT            1000
#define CW_LOW_VOLTAGE_REF        2500
#define CW_LOW_VOLTAGE            3000
#define CW_LOW_VOLTAGE_STEP        10
 
#define CW221X_NOT_ACTIVE        1
#define CW221X_PROFILE_NOT_READY    2
#define CW221X_PROFILE_NEED_UPDATE    3
 
static u8 config_profile_info[SIZE_OF_PROFILE] = {
   0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0xB2,
   0xC2, 0xCA, 0xC2, 0xBD, 0x9C, 0x5C, 0x38, 0xFF, 0xFF, 0xC4,
   0x86, 0x74, 0x60, 0x55, 0x4F, 0x4D, 0x4B, 0x80, 0xC0, 0xDB,
   0xCD, 0xD0, 0xCE, 0xD2, 0xD3, 0xD2, 0xD0, 0xCE, 0xC3, 0xD5,
   0xB9, 0xC9, 0xC5, 0xA3, 0x92, 0x8A, 0x80, 0x72, 0x63, 0x62,
   0x74, 0x90, 0xA6, 0x7E, 0x5F, 0x48, 0x80, 0x00, 0xAB, 0x10,
   0x00, 0xA1, 0xFB, 0x00, 0x00, 0x00, 0x64, 0x1E, 0xB1, 0x04,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4D,
};
 
enum charger_type {
   CHARGER_TYPE_NO = 0,
   CHARGER_TYPE_USB,
   CHARGER_TYPE_AC,
   CHARGER_TYPE_DC,
   CHARGER_TYPE_UNDEF,
};
 
struct cw221x_info {
   struct udevice *dev;
   int capacity;
   u8 *bat_profile;
   int chip_id;
   int voltage;
   int ic_soc_h;
   int ic_soc_l;
   int ui_soc;
   int temp;
   long cw_current;
   int cycle;
   int soh;
};
 
static u8 cw221x_read(struct cw221x_info *cw221x, u8 reg, u8 *buffer)
{
   u8 val;
   int ret;
 
   ret = dm_i2c_read(cw221x->dev, reg, &val, 1);
   if (ret) {
       printf("cw221x: read reg 0x%02x failed, ret=%d\n", reg, ret);
       return ret;
   }
 
   *buffer = val;
   return 0;
}
 
static int cw221x_write(struct cw221x_info *cw221x, u8 reg, u8 val)
{
   int ret;
 
   ret = dm_i2c_write(cw221x->dev, reg, &val, 1);
   if (ret)
       printf("cw221x: write reg 0x%02x failed, ret=%d\n", reg, ret);
 
   return ret;
}
 
static int cw221x_read_half_word(struct cw221x_info *cw221x,
                int reg,
                u16 *buffer)
{
   u8 vall, valh;
   int ret;
 
   ret = cw221x_read(cw221x, reg, &valh);
   if (ret)
       return ret;
 
   ret = cw221x_read(cw221x, reg + 1, &vall);
   if (ret)
       return ret;
 
   *buffer = ((u16)valh << 8) | vall;
 
   return 0;
}
 
static int cw221x_ofdata_to_platdata(struct udevice *dev)
{
   struct cw221x_info *cw221x = dev_get_priv(dev);
   int i, len, size;
   const u8 *info;
 
   if (!dev_read_prop(dev, "cellwise,battery-profile", &len) ||
       (len != SIZE_OF_PROFILE)) {
       cw221x->bat_profile = config_profile_info;
       return 0;
   }
 
   CW_DBG("cw221x: use dts profile\n");
   size = sizeof(*cw221x->bat_profile) * len;
   cw221x->bat_profile = calloc(size, 1);
   if (!cw221x->bat_profile) {
       CW_DBG("cw221x: calloc bat_profile fail\n");
       return -EINVAL;
   }
 
   info = dev_read_u8_array_ptr(dev, "cellwise,battery-profile", len);
   if (!info) {
       CW_DBG("cw221x: fdtdec_get battery profile fail\n");
       return -EINVAL;
   }
   for (i = 0; i < len; i++) {
       cw221x->bat_profile[i] = info[i];
       CW_DBG("%#x ", cw221x->bat_profile[i]);
       if ((i + 1) % 8 == 0)
           CW_DBG("\n");
   }
 
   return 0;
}
 
static int cw221x_get_vol(struct cw221x_info *cw221x)
{
   u16 value16, value16_1, value16_2;
   int voltage;
 
   cw221x_read_half_word(cw221x, REG_VCELL_H, &value16);
   mdelay(10);
 
   cw221x_read_half_word(cw221x, REG_VCELL_H, &value16_1);
   mdelay(10);
 
   if (value16 != value16_1) {
       cw221x_read_half_word(cw221x, REG_VCELL_H, &value16_2);
       if (value16_2 < 0)
           return -1;
       voltage = value16_2 * 5 / 16;
 
       return voltage;
   }
 
   voltage = value16 * 5 / 16;
   CW_DBG("cw221x: voltage:%d\n", voltage);
 
   return voltage;
}
 
static int cw221x_dwc_otg_check_dpdm(void)
{
#if defined(CONFIG_PHY_ROCKCHIP_INNO_USB2) && !defined(CONFIG_SPL_BUILD)
   return rockchip_chg_get_type();
#else
   CW_DBG("cw221x: rockchip_chg_get_type() is not implement\n");
   return CHARGER_TYPE_NO;
#endif
}
 
static int cw221x_get_usb_state(struct cw221x_info *cw221x)
{
   int charger_type;
 
   switch (cw221x_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 cw221x_check_charge(struct cw221x_info *cw221x)
{
   if (cw221x_get_usb_state(cw221x) != CHARGER_TYPE_NO)
       return true;
 
   return false;
}
 
static int cw221x_get_soc(struct cw221x_info *cw221x)
{
   int ui_100 = CW_UI_FULL;
   int ui_soc;
   u16 value16, value16_1, value16_2;
   int ret;
 
   ret = cw221x_read_half_word(cw221x, REG_SOC_INT, &value16);
   if (ret) {
       printf("cw221x: get soc error!!!");
       return ret;
   }
   mdelay(10);
 
   ret = cw221x_read_half_word(cw221x, REG_SOC_INT, &value16_1);
   if (ret) {
       printf("cw221x: get soc error!!!");
       return ret;
   }
   mdelay(10);
 
   if (value16 != value16_1) {
       ret = cw221x_read_half_word(cw221x, REG_SOC_INT, &value16_2);
       if (ret) {
           printf("cw221x: get soc error!!!");
           return ret;
       }
 
       if (value16_2 < 0)
           return -1;
 
       value16 = value16_2;
   }
 
   ui_soc = value16 * 100 / (ui_100 * 256);
 
   if (ui_soc >= 100)
       ui_soc = 100;
   CW_DBG("soc: %d\n", ui_soc);
 
   return ui_soc;
}
 
static int cw221x_update_get_soc(struct udevice *dev)
{
   struct cw221x_info *cw221x = dev_get_priv(dev);
 
   return cw221x_get_soc(cw221x);
}
 
/*
 * The 0x00 register is an UNSIGNED 8bit read-only register. Its value is
 * fixed to 0xA0 in shutdown mode and active mode.
 */
static void cw221X_get_chip_id(struct cw221x_info *cw221x)
{
   u8 chip_id;
   int ret;
 
   ret = cw221x_read(cw221x, REG_CHIP_ID, &chip_id);
   if (ret) {
       printf("cw221x: get chip id error!!!");
       return;
   }
 
   cw221x->chip_id = chip_id;
}
 
static int cw221X_active(struct cw221x_info *cw221x)
{
   unsigned char reg_val = CONFIG_MODE_RESTART;
 
   cw221x_write(cw221x, REG_MODE_CONFIG, reg_val);
   mdelay(20);
   reg_val = CONFIG_MODE_ACTIVE;
   cw221x_write(cw221x, REG_MODE_CONFIG, reg_val);
   mdelay(20);
 
   return 0;
}
 
static int cw221X_sleep(struct cw221x_info *cw221x)
{
   unsigned char reg_val = CONFIG_MODE_RESTART;
 
   cw221x_write(cw221x, REG_MODE_CONFIG, reg_val);
   mdelay(20);
   reg_val = CONFIG_MODE_SLEEP;
   cw221x_write(cw221x, REG_MODE_CONFIG, reg_val);
   mdelay(20);
 
   return 0;
}
 
static int cw_write_profile(struct cw221x_info *cw221x, u8 buf[])
{
   int ret;
   int i;
 
   for (i = 0; i < SIZE_OF_PROFILE; i++) {
       ret = cw221x_write(cw221x, REG_BAT_PROFILE + i, buf[i]);
       if (ret < 0) {
           printf("cw221x: write profile error\n");
           return ret;
       }
   }
 
   return ret;
}
 
/*
 * Get the cw221X running state
 * Determine whether the profile needs to be updated
 */
static int cw221X_get_state(struct cw221x_info *cw221x)
{
   unsigned char reg_val;
   int ret;
   int i;
 
   ret = cw221x_read(cw221x, REG_MODE_CONFIG, &reg_val);
   if (ret) {
       printf("cw221x: get mode config error!!!");
       return ret;
   }
 
   if (reg_val != CONFIG_MODE_ACTIVE)
       return CW221X_NOT_ACTIVE;
 
   ret = cw221x_read(cw221x, REG_SOC_ALERT, &reg_val);
   if (ret) {
       printf("cw221x: get soc alert error!!!");
       return ret;
   }
 
   if (0x00 == (reg_val & CONFIG_UPDATE_FLG))
       return CW221X_PROFILE_NOT_READY;
 
   for (i = 0; i < SIZE_OF_PROFILE; i++) {
       ret = cw221x_read(cw221x, (REG_BAT_PROFILE + i), &reg_val);
       if (ret) {
           printf("cw221x: get battery profile error!!!");
           return ret;
       }
 
       if (cw221x->bat_profile[i] != reg_val)
           break;
   }
 
   if (i != SIZE_OF_PROFILE)
       return CW221X_PROFILE_NEED_UPDATE;
 
   return 0;
}
 
static int cw_config_start_ic(struct cw221x_info *cw221x)
{
   int count = 0;
   u8 reg_val;
   int ret;
 
   cw221X_sleep(cw221x);
   cw_write_profile(cw221x, cw221x->bat_profile);
   cw221X_active(cw221x);
 
   /* set UPDATE_FLAG AND SOC INTTERRUP VALUE */
   reg_val = CONFIG_UPDATE_FLG | GPIO_SOC_IRQ_VALUE;
   ret = cw221x_write(cw221x, REG_SOC_ALERT, reg_val);
   if (ret < 0)
       return ret;
 
   /* close all interruptes */
   reg_val = 0;
   ret = cw221x_write(cw221x, REG_GPIO_CONFIG, reg_val);
   if (ret < 0)
       return ret;
 
   while (CW_TRUE) {
       mdelay(CW_SLEEP_100MS);
       ret = cw221x_read(cw221x, REG_IC_STATE, &reg_val);
       if (ret) {
           printf("cw221x: get ic state error!!!");
           return ret;
       }
 
       if (IC_READY_MARK == (reg_val & IC_READY_MARK))
           break;
       count++;
       if (count >= CW_SLEEP_COUNTS) {
           cw221X_sleep(cw221x);
           return -1;
       }
   }
   return 0;
}
 
static int cw221X_init(struct cw221x_info *cw221x)
{
   int ret;
 
   cw221X_get_chip_id(cw221x);
   if (cw221x->chip_id != IC_VCHIP_ID) {
       printf("not cw221X\n");
       return -EINVAL;
   }
 
   CW_DBG("cw221X:\n");
   ret = cw221X_get_state(cw221x);
 
   if (ret != 0) {
       CW_DBG("cw221x: need update profile; %d\n", ret);
       ret = cw_config_start_ic(cw221x);
       if (ret < 0)
           return ret;
   } else {
       CW_DBG("cw221x: not need update profile\n");
   }
 
   CW_DBG("cw221X: init success!\n");
 
   return 0;
}
 
/* get complement code function, unsigned short must be U16 */
static long get_complement_code(unsigned short raw_code)
{
   long complement_code;
   int dir;
 
   if (0 != (raw_code & COMPLEMENT_CODE_U16)) {
       dir = -1;
       raw_code = (0xFFFF - raw_code) + 1;
   } else {
       dir = 1;
   }
 
   complement_code = (long)raw_code * dir;
 
   return complement_code;
}
 
static int cw221x_get_current(struct cw221x_info *cw221x)
{
   long long cw_current;
   u16 value16;
   int ret;
 
   ret = cw221x_read_half_word(cw221x, REG_CURRENT_H, &value16);
   if (ret) {
       printf("cw221x: get current error!!!");
       return ret;
   }
 
   cw_current = get_complement_code(value16);
   cw_current = cw_current * 1600 / USER_RSENSE;
 
   CW_DBG("cw221x: current: %lld\n", cw_current);
 
   return cw_current;
}
 
static int cw221x_update_get_current(struct udevice *dev)
{
   struct cw221x_info *cw221x = dev_get_priv(dev);
 
   return cw221x_get_current(cw221x);
}
 
static int cw221x_get_temperature(struct udevice *dev, int *temp)
{
   struct cw221x_info *cw221x = dev_get_priv(dev);
   u8 reg_val;
   int bat_temp;
   int ret;
 
   ret = cw221x_read(cw221x, REG_TEMP, &reg_val);
   if (ret)
       return ret;
 
   bat_temp = (int)reg_val * 10 / 2 - 400;
   CW_DBG("cw221x: temperature: %d\n", bat_temp);
   *temp = bat_temp;
 
   return 0;
}
 
static bool cw221x_update_get_chrg_online(struct udevice *dev)
{
   struct cw221x_info *cw221x = dev_get_priv(dev);
 
   return cw221x_check_charge(cw221x);
}
 
static int cw221x_update_get_voltage(struct udevice *dev)
{
   struct cw221x_info *cw221x = dev_get_priv(dev);
   int temperture;
 
   cw221x_get_current(cw221x);
   cw221x_get_temperature(dev, &temperture);
 
   return cw221x_get_vol(cw221x);
}
 
static int cw221x_capability(struct udevice *dev)
{
   return FG_CAP_FUEL_GAUGE;
}
 
static struct dm_fuel_gauge_ops cw221x_fg_ops = {
   .capability = cw221x_capability,
   .get_soc = cw221x_update_get_soc,
   .get_voltage = cw221x_update_get_voltage,
   .get_current = cw221x_update_get_current,
   .get_temperature = cw221x_get_temperature,
   .get_chrg_online = cw221x_update_get_chrg_online,
};
 
static int cw221x_fg_probe(struct udevice *dev)
{
   struct cw221x_info *cw221x = dev_get_priv(dev);
 
   cw221x->dev = dev;
   printf("cw221x driver version-20220903");
   cw221X_init(cw221x);
   printf("cw221x vol: %d, soc: %d\n",
          cw221x_get_vol(cw221x), cw221x_get_soc(cw221x));
 
   return 0;
}
 
static const struct udevice_id cw221x_ids[] = {
   { .compatible = "cellwise,cw221X" },
   { }
};
 
U_BOOT_DRIVER(cw221x_fg) = {
   .name = "cw221x_fg",
   .id = UCLASS_FG,
   .of_match = cw221x_ids,
   .probe = cw221x_fg_probe,
   .ofdata_to_platdata = cw221x_ofdata_to_platdata,
   .ops = &cw221x_fg_ops,
   .priv_auto_alloc_size = sizeof(struct cw221x_info),
};