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
|
// SPDX-License-Identifier: GPL-2.0+
/*
* max77976_charger.c - Driver for the Maxim MAX77976 battery charger
*
* Copyright (C) 2021 Luca Ceresoli
* Author: Luca Ceresoli <luca.ceresoli@bootlin.com>
*/
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/power_supply.h>
#include <linux/regmap.h>
#define MAX77976_DRIVER_NAME "max77976-charger"
#define MAX77976_CHIP_ID 0x76
static const char *max77976_manufacturer = "Maxim Integrated";
static const char *max77976_model = "MAX77976";
/* --------------------------------------------------------------------------
* Register map
*/
#define MAX77976_REG_CHIP_ID 0x00
#define MAX77976_REG_CHIP_REVISION 0x01
#define MAX77976_REG_CHG_INT_OK 0x12
#define MAX77976_REG_CHG_DETAILS_01 0x14
#define MAX77976_REG_CHG_CNFG_00 0x16
#define MAX77976_REG_CHG_CNFG_02 0x18
#define MAX77976_REG_CHG_CNFG_06 0x1c
#define MAX77976_REG_CHG_CNFG_09 0x1f
/* CHG_DETAILS_01.CHG_DTLS values */
enum max77976_charging_state {
MAX77976_CHARGING_PREQUALIFICATION = 0x0,
MAX77976_CHARGING_FAST_CONST_CURRENT,
MAX77976_CHARGING_FAST_CONST_VOLTAGE,
MAX77976_CHARGING_TOP_OFF,
MAX77976_CHARGING_DONE,
MAX77976_CHARGING_RESERVED_05,
MAX77976_CHARGING_TIMER_FAULT,
MAX77976_CHARGING_SUSPENDED_QBATT_OFF,
MAX77976_CHARGING_OFF,
MAX77976_CHARGING_RESERVED_09,
MAX77976_CHARGING_THERMAL_SHUTDOWN,
MAX77976_CHARGING_WATCHDOG_EXPIRED,
MAX77976_CHARGING_SUSPENDED_JEITA,
MAX77976_CHARGING_SUSPENDED_THM_REMOVAL,
MAX77976_CHARGING_SUSPENDED_PIN,
MAX77976_CHARGING_RESERVED_0F,
};
/* CHG_DETAILS_01.BAT_DTLS values */
enum max77976_battery_state {
MAX77976_BATTERY_BATTERY_REMOVAL = 0x0,
MAX77976_BATTERY_PREQUALIFICATION,
MAX77976_BATTERY_TIMER_FAULT,
MAX77976_BATTERY_REGULAR_VOLTAGE,
MAX77976_BATTERY_LOW_VOLTAGE,
MAX77976_BATTERY_OVERVOLTAGE,
MAX77976_BATTERY_RESERVED,
MAX77976_BATTERY_BATTERY_ONLY, // No valid adapter is present
};
/* CHG_CNFG_00.MODE values */
enum max77976_mode {
MAX77976_MODE_CHARGER_BUCK = 0x5,
MAX77976_MODE_BOOST = 0x9,
};
/* CHG_CNFG_02.CHG_CC: charge current limit, 100..5500 mA, 50 mA steps */
#define MAX77976_CHG_CC_STEP 50000U
#define MAX77976_CHG_CC_MIN 100000U
#define MAX77976_CHG_CC_MAX 5500000U
/* CHG_CNFG_09.CHGIN_ILIM: input current limit, 100..3200 mA, 100 mA steps */
#define MAX77976_CHGIN_ILIM_STEP 100000U
#define MAX77976_CHGIN_ILIM_MIN 100000U
#define MAX77976_CHGIN_ILIM_MAX 3200000U
enum max77976_field_idx {
VERSION, REVISION, /* CHIP_REVISION */
CHGIN_OK, /* CHG_INT_OK */
BAT_DTLS, CHG_DTLS, /* CHG_DETAILS_01 */
MODE, /* CHG_CNFG_00 */
CHG_CC, /* CHG_CNFG_02 */
CHGPROT, /* CHG_CNFG_06 */
CHGIN_ILIM, /* CHG_CNFG_09 */
MAX77976_N_REGMAP_FIELDS
};
static const struct reg_field max77976_reg_field[MAX77976_N_REGMAP_FIELDS] = {
[VERSION] = REG_FIELD(MAX77976_REG_CHIP_REVISION, 4, 7),
[REVISION] = REG_FIELD(MAX77976_REG_CHIP_REVISION, 0, 3),
[CHGIN_OK] = REG_FIELD(MAX77976_REG_CHG_INT_OK, 6, 6),
[CHG_DTLS] = REG_FIELD(MAX77976_REG_CHG_DETAILS_01, 0, 3),
[BAT_DTLS] = REG_FIELD(MAX77976_REG_CHG_DETAILS_01, 4, 6),
[MODE] = REG_FIELD(MAX77976_REG_CHG_CNFG_00, 0, 3),
[CHG_CC] = REG_FIELD(MAX77976_REG_CHG_CNFG_02, 0, 6),
[CHGPROT] = REG_FIELD(MAX77976_REG_CHG_CNFG_06, 2, 3),
[CHGIN_ILIM] = REG_FIELD(MAX77976_REG_CHG_CNFG_09, 0, 5),
};
static const struct regmap_config max77976_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.max_register = 0x24,
};
/* --------------------------------------------------------------------------
* Data structures
*/
struct max77976 {
struct i2c_client *client;
struct regmap *regmap;
struct regmap_field *rfield[MAX77976_N_REGMAP_FIELDS];
};
/* --------------------------------------------------------------------------
* power_supply properties
*/
static int max77976_get_status(struct max77976 *chg, int *val)
{
unsigned int regval;
int err;
err = regmap_field_read(chg->rfield[CHG_DTLS], ®val);
if (err < 0)
return err;
switch (regval) {
case MAX77976_CHARGING_PREQUALIFICATION:
case MAX77976_CHARGING_FAST_CONST_CURRENT:
case MAX77976_CHARGING_FAST_CONST_VOLTAGE:
case MAX77976_CHARGING_TOP_OFF:
*val = POWER_SUPPLY_STATUS_CHARGING;
break;
case MAX77976_CHARGING_DONE:
*val = POWER_SUPPLY_STATUS_FULL;
break;
case MAX77976_CHARGING_TIMER_FAULT:
case MAX77976_CHARGING_SUSPENDED_QBATT_OFF:
case MAX77976_CHARGING_SUSPENDED_JEITA:
case MAX77976_CHARGING_SUSPENDED_THM_REMOVAL:
case MAX77976_CHARGING_SUSPENDED_PIN:
*val = POWER_SUPPLY_STATUS_NOT_CHARGING;
break;
case MAX77976_CHARGING_OFF:
case MAX77976_CHARGING_THERMAL_SHUTDOWN:
case MAX77976_CHARGING_WATCHDOG_EXPIRED:
*val = POWER_SUPPLY_STATUS_DISCHARGING;
break;
default:
*val = POWER_SUPPLY_STATUS_UNKNOWN;
}
return 0;
}
static int max77976_get_charge_type(struct max77976 *chg, int *val)
{
unsigned int regval;
int err;
err = regmap_field_read(chg->rfield[CHG_DTLS], ®val);
if (err < 0)
return err;
switch (regval) {
case MAX77976_CHARGING_PREQUALIFICATION:
*val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
break;
case MAX77976_CHARGING_FAST_CONST_CURRENT:
case MAX77976_CHARGING_FAST_CONST_VOLTAGE:
*val = POWER_SUPPLY_CHARGE_TYPE_FAST;
break;
case MAX77976_CHARGING_TOP_OFF:
*val = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
break;
case MAX77976_CHARGING_DONE:
case MAX77976_CHARGING_TIMER_FAULT:
case MAX77976_CHARGING_SUSPENDED_QBATT_OFF:
case MAX77976_CHARGING_OFF:
case MAX77976_CHARGING_THERMAL_SHUTDOWN:
case MAX77976_CHARGING_WATCHDOG_EXPIRED:
case MAX77976_CHARGING_SUSPENDED_JEITA:
case MAX77976_CHARGING_SUSPENDED_THM_REMOVAL:
case MAX77976_CHARGING_SUSPENDED_PIN:
*val = POWER_SUPPLY_CHARGE_TYPE_NONE;
break;
default:
*val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
}
return 0;
}
static int max77976_get_health(struct max77976 *chg, int *val)
{
unsigned int regval;
int err;
err = regmap_field_read(chg->rfield[BAT_DTLS], ®val);
if (err < 0)
return err;
switch (regval) {
case MAX77976_BATTERY_BATTERY_REMOVAL:
*val = POWER_SUPPLY_HEALTH_NO_BATTERY;
break;
case MAX77976_BATTERY_LOW_VOLTAGE:
case MAX77976_BATTERY_REGULAR_VOLTAGE:
*val = POWER_SUPPLY_HEALTH_GOOD;
break;
case MAX77976_BATTERY_TIMER_FAULT:
*val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
break;
case MAX77976_BATTERY_OVERVOLTAGE:
*val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
break;
case MAX77976_BATTERY_PREQUALIFICATION:
case MAX77976_BATTERY_BATTERY_ONLY:
*val = POWER_SUPPLY_HEALTH_UNKNOWN;
break;
default:
*val = POWER_SUPPLY_HEALTH_UNKNOWN;
}
return 0;
}
static int max77976_get_online(struct max77976 *chg, int *val)
{
unsigned int regval;
int err;
err = regmap_field_read(chg->rfield[CHGIN_OK], ®val);
if (err < 0)
return err;
*val = (regval ? 1 : 0);
return 0;
}
static int max77976_get_integer(struct max77976 *chg, enum max77976_field_idx fidx,
unsigned int clamp_min, unsigned int clamp_max,
unsigned int mult, int *val)
{
unsigned int regval;
int err;
err = regmap_field_read(chg->rfield[fidx], ®val);
if (err < 0)
return err;
*val = clamp_val(regval * mult, clamp_min, clamp_max);
return 0;
}
static int max77976_set_integer(struct max77976 *chg, enum max77976_field_idx fidx,
unsigned int clamp_min, unsigned int clamp_max,
unsigned int div, int val)
{
unsigned int regval;
regval = clamp_val(val, clamp_min, clamp_max) / div;
return regmap_field_write(chg->rfield[fidx], regval);
}
static int max77976_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct max77976 *chg = power_supply_get_drvdata(psy);
int err = 0;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
err = max77976_get_status(chg, &val->intval);
break;
case POWER_SUPPLY_PROP_CHARGE_TYPE:
err = max77976_get_charge_type(chg, &val->intval);
break;
case POWER_SUPPLY_PROP_HEALTH:
err = max77976_get_health(chg, &val->intval);
break;
case POWER_SUPPLY_PROP_ONLINE:
err = max77976_get_online(chg, &val->intval);
break;
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
val->intval = MAX77976_CHG_CC_MAX;
break;
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
err = max77976_get_integer(chg, CHG_CC,
MAX77976_CHG_CC_MIN,
MAX77976_CHG_CC_MAX,
MAX77976_CHG_CC_STEP,
&val->intval);
break;
case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
err = max77976_get_integer(chg, CHGIN_ILIM,
MAX77976_CHGIN_ILIM_MIN,
MAX77976_CHGIN_ILIM_MAX,
MAX77976_CHGIN_ILIM_STEP,
&val->intval);
break;
case POWER_SUPPLY_PROP_MODEL_NAME:
val->strval = max77976_model;
break;
case POWER_SUPPLY_PROP_MANUFACTURER:
val->strval = max77976_manufacturer;
break;
default:
err = -EINVAL;
}
return err;
}
static int max77976_set_property(struct power_supply *psy,
enum power_supply_property psp,
const union power_supply_propval *val)
{
struct max77976 *chg = power_supply_get_drvdata(psy);
int err = 0;
switch (psp) {
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
err = max77976_set_integer(chg, CHG_CC,
MAX77976_CHG_CC_MIN,
MAX77976_CHG_CC_MAX,
MAX77976_CHG_CC_STEP,
val->intval);
break;
case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
err = max77976_set_integer(chg, CHGIN_ILIM,
MAX77976_CHGIN_ILIM_MIN,
MAX77976_CHGIN_ILIM_MAX,
MAX77976_CHGIN_ILIM_STEP,
val->intval);
break;
default:
err = -EINVAL;
}
return err;
};
static int max77976_property_is_writeable(struct power_supply *psy,
enum power_supply_property psp)
{
switch (psp) {
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
return true;
default:
return false;
}
}
static enum power_supply_property max77976_psy_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_CHARGE_TYPE,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
POWER_SUPPLY_PROP_MODEL_NAME,
POWER_SUPPLY_PROP_MANUFACTURER,
};
static const struct power_supply_desc max77976_psy_desc = {
.name = MAX77976_DRIVER_NAME,
.type = POWER_SUPPLY_TYPE_USB,
.properties = max77976_psy_props,
.num_properties = ARRAY_SIZE(max77976_psy_props),
.get_property = max77976_get_property,
.set_property = max77976_set_property,
.property_is_writeable = max77976_property_is_writeable,
};
/* --------------------------------------------------------------------------
* Entry point
*/
static int max77976_detect(struct max77976 *chg)
{
struct device *dev = &chg->client->dev;
unsigned int id, ver, rev;
int err;
err = regmap_read(chg->regmap, MAX77976_REG_CHIP_ID, &id);
if (err)
return dev_err_probe(dev, err, "cannot read chip ID\n");
if (id != MAX77976_CHIP_ID)
return dev_err_probe(dev, -ENXIO, "unknown model ID 0x%02x\n", id);
err = regmap_field_read(chg->rfield[VERSION], &ver);
if (!err)
err = regmap_field_read(chg->rfield[REVISION], &rev);
if (err)
return dev_err_probe(dev, -ENXIO, "cannot read version/revision\n");
dev_info(dev, "detected model MAX779%02x ver %u rev %u", id, ver, rev);
return 0;
}
static int max77976_configure(struct max77976 *chg)
{
struct device *dev = &chg->client->dev;
int err;
/* Magic value to unlock writing to some registers */
err = regmap_field_write(chg->rfield[CHGPROT], 0x3);
if (err)
goto err;
/*
* Mode 5 = Charger ON, OTG OFF, buck ON, boost OFF.
* Other modes are not implemented by this driver.
*/
err = regmap_field_write(chg->rfield[MODE], MAX77976_MODE_CHARGER_BUCK);
if (err)
goto err;
return 0;
err:
return dev_err_probe(dev, err, "error while configuring");
}
static int max77976_probe(struct i2c_client *client)
{
struct device *dev = &client->dev;
struct power_supply_config psy_cfg = {};
struct power_supply *psy;
struct max77976 *chg;
int err;
int i;
chg = devm_kzalloc(dev, sizeof(*chg), GFP_KERNEL);
if (!chg)
return -ENOMEM;
i2c_set_clientdata(client, chg);
psy_cfg.drv_data = chg;
chg->client = client;
chg->regmap = devm_regmap_init_i2c(client, &max77976_regmap_config);
if (IS_ERR(chg->regmap))
return dev_err_probe(dev, PTR_ERR(chg->regmap),
"cannot allocate regmap\n");
for (i = 0; i < MAX77976_N_REGMAP_FIELDS; i++) {
chg->rfield[i] = devm_regmap_field_alloc(dev, chg->regmap,
max77976_reg_field[i]);
if (IS_ERR(chg->rfield[i]))
return dev_err_probe(dev, PTR_ERR(chg->rfield[i]),
"cannot allocate regmap field\n");
}
err = max77976_detect(chg);
if (err)
return err;
err = max77976_configure(chg);
if (err)
return err;
psy = devm_power_supply_register_no_ws(dev, &max77976_psy_desc, &psy_cfg);
if (IS_ERR(psy))
return dev_err_probe(dev, PTR_ERR(psy), "cannot register\n");
return 0;
}
static const struct i2c_device_id max77976_i2c_id[] = {
{ MAX77976_DRIVER_NAME },
{ }
};
MODULE_DEVICE_TABLE(i2c, max77976_i2c_id);
static const struct of_device_id max77976_of_id[] = {
{ .compatible = "maxim,max77976" },
{ },
};
MODULE_DEVICE_TABLE(of, max77976_of_id);
static struct i2c_driver max77976_driver = {
.driver = {
.name = MAX77976_DRIVER_NAME,
.of_match_table = max77976_of_id,
},
.probe = max77976_probe,
.id_table = max77976_i2c_id,
};
module_i2c_driver(max77976_driver);
MODULE_AUTHOR("Luca Ceresoli <luca.ceresoli@bootlin.com>");
MODULE_DESCRIPTION("Maxim MAX77976 charger driver");
MODULE_LICENSE("GPL v2");
|