hc
2023-10-25 6c2073b7aa40e29d0eca7d571dd7bc590c7ecaa7
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
/*
 * (C) Copyright 2022 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
 
#include <common.h>
#include <dm.h>
#include <dm/device.h>
#include <dm/uclass.h>
#include <power/fuel_gauge.h>
#include <power/pmic.h>
#include <power/power_delivery/power_delivery.h>
#include <linux/usb/phy-rockchip-usb2.h>
 
DECLARE_GLOBAL_DATA_PTR;
 
#define BQ25890_CHARGE_CURRENT_1500MA        1500
#define BQ25890_SDP_INPUT_CURRENT_500MA        0x45
#define BQ25890_DCP_INPUT_CURRENT_1500MA    0x4f
#define BQ25890_DCP_INPUT_CURRENT_2000MA    0x54
#define BQ25890_DCP_INPUT_CURRENT_3000MA    0x5e
 
#define WATCHDOG_ENSABLE            (0x03 << 4)
 
#define BQ25890_CHARGEOPTION0_REG        0x07
#define BQ25890_CHARGECURREN_REG        0x04
#define BQ25890_CHARGERSTAUS_REG        0x0B
#define BQ25890_INPUTVOLTAGE_REG        0x0D
#define BQ25890_INPUTCURREN_REG            0x00
#define BQ25890_AUTO_DPDM_REG            0x02
 
/*
 * Most of the val -> idx conversions can be computed, given the minimum,
 * maximum and the step between values. For the rest of conversions, we use
 * lookup tables.
 */
enum bq25890_table_ids {
   /* range tables */
   TBL_ICHG,
   TBL_ITERM,
   TBL_IPRECHG,
   TBL_VREG,
   TBL_BATCMP,
   TBL_VCLAMP,
   TBL_BOOSTV,
   TBL_SYSVMIN,
   TBL_VINDPM,
   TBL_IINLIM,
 
   /* lookup tables */
   TBL_TREG,
   TBL_BOOSTI,
};
 
struct bq25890 {
   struct udevice *dev;
   u32 ichg;
   u32 chip_id;
   struct udevice *pd;
   bool pd_online;
};
 
/* Thermal Regulation Threshold lookup table, in degrees Celsius */
static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
 
#define BQ25890_TREG_TBL_SIZE        ARRAY_SIZE(bq25890_treg_tbl)
 
/* Boost mode current limit lookup table, in uA */
static const u32 bq25890_boosti_tbl[] = {
   500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
};
 
#define BQ25890_BOOSTI_TBL_SIZE        ARRAY_SIZE(bq25890_boosti_tbl)
 
struct bq25890_range {
   u32 min;
   u32 max;
   u32 step;
};
 
struct bq25890_lookup {
   const u32 *tbl;
   u32 size;
};
 
static const union {
   struct bq25890_range  rt;
   struct bq25890_lookup lt;
} bq25890_tables[] = {
   /* range tables */
   [TBL_ICHG] =    { .rt = {0,      5056000, 64000} },     /* uA */
   [TBL_ITERM] =    { .rt = {64000,   1024000, 64000} },     /* uA */
   [TBL_VREG] =    { .rt = {3840000, 4608000, 16000} },     /* uV */
   [TBL_BATCMP] =    { .rt = {0,      140,     20} },     /* mOhm */
   [TBL_VCLAMP] =    { .rt = {0,      224000,  32000} },     /* uV */
   [TBL_BOOSTV] =    { .rt = {4550000, 5510000, 64000} },     /* uV */
   [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} },     /* uV */
   [TBL_VINDPM] = { .rt = {2600000, 15300000, 100000} }, /* uV */
   [TBL_IINLIM] = { .rt = {100, 3250000, 100000} }, /* uA */
 
   /* lookup tables */
   [TBL_TREG] =    { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
   [TBL_BOOSTI] =    { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }
};
 
static int bq25890_read(struct bq25890 *charger, uint reg)
{
   u16 val;
   int ret;
 
   ret = dm_i2c_read(charger->dev, reg, (u8 *)&val, 1);
   if (ret) {
       printf("bq25890: read %#x error, ret=%d", reg, ret);
       return ret;
   }
 
   return val;
}
 
static int bq25890_write(struct bq25890 *charger, uint reg, u16 val)
{
   int ret;
 
   ret = dm_i2c_write(charger->dev, reg, (u8 *)&val, 1);
   if (ret) {
       printf("bq25890: write %#x error, ret=%d", reg, ret);
       return ret;
   }
 
   return 0;
}
 
static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
{
   u8 idx;
 
   if (id >= TBL_TREG) {
       const u32 *tbl = bq25890_tables[id].lt.tbl;
       u32 tbl_size = bq25890_tables[id].lt.size;
 
       for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
           ;
   } else {
       const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
       u8 rtbl_size;
 
       rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
 
       for (idx = 1;
            idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
            idx++)
           ;
   }
 
   return idx - 1;
}
 
static bool bq25890_charger_status(struct bq25890 *charger)
{
   int state_of_charger;
   u16 value;
   int i = 0;
 
__retry:
   value = bq25890_read(charger, BQ25890_CHARGERSTAUS_REG);
   state_of_charger = value & 0x04;
   if (!state_of_charger && charger->pd_online) {
       if (i < 3) {
           i++;
           mdelay(20);
           goto __retry;
       }
   }
 
   return state_of_charger;
}
 
static bool bq257xx_charger_status(struct udevice *dev)
{
   struct bq25890 *charger = dev_get_priv(dev);
 
   return bq25890_charger_status(charger);
}
 
static int bq25890_charger_capability(struct udevice *dev)
{
   return FG_CAP_CHARGER;
}
 
static int bq25890_get_usb_type(void)
{
#ifdef CONFIG_PHY_ROCKCHIP_INNO_USB2
   return rockchip_chg_get_type();
#else
   return 0;
#endif
}
 
static int bq25890_get_pd_output_val(struct bq25890 *charger,
                    int *vol, int *cur)
{
   struct power_delivery_data pd_data;
   int ret;
 
   if (!charger->pd)
       return -EINVAL;
 
   memset(&pd_data, 0, sizeof(pd_data));
   ret = power_delivery_get_data(charger->pd, &pd_data);
   if (ret)
       return ret;
   if (!pd_data.online || !pd_data.voltage || !pd_data.current)
       return -EINVAL;
 
   *vol = pd_data.voltage;
   *cur = pd_data.current;
   charger->pd_online = pd_data.online;
 
   return 0;
}
 
static void bq25890_set_auto_dpdm_detect(struct bq25890 *charger, bool enable)
{
   u8 value;
 
   value = bq25890_read(charger, BQ25890_AUTO_DPDM_REG);
   value &= 0xfe;
   value |= enable;
   bq25890_write(charger, BQ25890_AUTO_DPDM_REG, value);
}
 
static void bq25890_charger_current_init(struct bq25890 *charger)
{
   u8 charge_current =  bq25890_find_idx(BQ25890_CHARGE_CURRENT_1500MA * 1000, TBL_ICHG);
   u8 sdp_inputcurrent = BQ25890_SDP_INPUT_CURRENT_500MA;
   u8 dcp_inputcurrent = BQ25890_DCP_INPUT_CURRENT_1500MA;
   int pd_inputvol,  pd_inputcurrent;
   u16 vol_idx = 0, cur_idx;
   u8 temp;
 
   temp = bq25890_read(charger, BQ25890_CHARGEOPTION0_REG);
   temp &= (~WATCHDOG_ENSABLE);
   bq25890_write(charger, BQ25890_CHARGEOPTION0_REG, temp);
 
   if (!bq25890_get_pd_output_val(charger, &pd_inputvol,
                      &pd_inputcurrent)) {
       printf("bq25890: pd charge %duV, %duA\n", pd_inputvol, pd_inputcurrent);
       if (pd_inputvol > 5000000) {
           vol_idx = bq25890_find_idx((pd_inputvol - 1280000 - 3200000),
                          TBL_VINDPM);
           vol_idx = vol_idx << 6;
       }
       cur_idx = bq25890_find_idx(pd_inputcurrent,
                      TBL_IINLIM);
       cur_idx  = cur_idx << 8;
       if (pd_inputcurrent != 0) {
           bq25890_set_auto_dpdm_detect(charger, false);
           bq25890_write(charger, BQ25890_INPUTCURREN_REG,
                     cur_idx);
           if (vol_idx)
               bq25890_write(charger, BQ25890_INPUTVOLTAGE_REG,
                         vol_idx);
           charge_current = bq25890_find_idx(charger->ichg,
                             TBL_ICHG);
       }
   } else {
       if (bq25890_get_usb_type() > 1)
           bq25890_write(charger, BQ25890_INPUTCURREN_REG,
                     dcp_inputcurrent);
       else
           bq25890_write(charger, BQ25890_INPUTCURREN_REG,
                     sdp_inputcurrent);
   }
 
   if (bq25890_charger_status(charger))
       bq25890_write(charger, BQ25890_CHARGECURREN_REG,
                 charge_current);
}
 
static int bq25890_ofdata_to_platdata(struct udevice *dev)
{
   struct bq25890 *charger = dev_get_priv(dev);
 
   charger->dev = dev;
   charger->ichg = dev_read_u32_default(dev, "ti,charge-current", 0);
 
   return 0;
}
 
static int bq25890_probe(struct udevice *dev)
{
   struct bq25890 *charger = dev_get_priv(dev);
   int ret;
 
   ret = uclass_get_device(UCLASS_PD, 0, &charger->pd);
   if (ret) {
       if (ret == -ENODEV)
           printf("Can't find PD\n");
       else
           printf("Get UCLASS PD failed: %d\n", ret);
 
       charger->pd = NULL;
   }
 
   bq25890_charger_current_init(charger);
 
   return 0;
}
 
static const struct udevice_id charger_ids[] = {
   { .compatible = "ti,bq25890" },
   { .compatible = "sy,sy6970" },
   { },
};
 
static struct dm_fuel_gauge_ops charger_ops = {
   .get_chrg_online = bq257xx_charger_status,
   .capability = bq25890_charger_capability,
};
 
U_BOOT_DRIVER(bq25890_charger) = {
   .name = "bq25890_charger",
   .id = UCLASS_FG,
   .probe = bq25890_probe,
   .of_match = charger_ids,
   .ops = &charger_ops,
   .ofdata_to_platdata = bq25890_ofdata_to_platdata,
   .priv_auto_alloc_size = sizeof(struct bq25890),
};