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
|
/*
* max8997-charger.c
* MAXIM 8997 charger interface driver
*
* Copyright (C) 2010 Samsung Electronics
*
* <ms925.kim@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/power_supply.h>
#include <linux/regulator/machine.h>
#include <linux/mfd/max8997.h>
#include <linux/mfd/max8997-private.h>
/* MAX8997_REG_STATUS4 */
#define DCINOK_SHIFT 1
#define DCINOK_MASK (1 << DCINOK_SHIFT)
#define DETBAT_SHIFT 2
#define DETBAT_MASK (1 << DETBAT_SHIFT)
/* MAX8997_REG_MBCCTRL1 */
#define TFCH_SHIFT 4
#define TFCH_MASK (7 << TFCH_SHIFT)
/* MAX8997_REG_MBCCTRL2 */
#define MBCHOSTEN_SHIFT 6
#define VCHGR_FC_SHIFT 7
#define MBCHOSTEN_MASK (1 << MBCHOSTEN_SHIFT)
#define VCHGR_FC_MASK (1 << VCHGR_FC_SHIFT)
/* MAX8997_REG_MBCCTRL3 */
#define MBCCV_SHIFT 0
#define MBCCV_MASK (0xF << MBCCV_SHIFT)
/* MAX8997_REG_MBCCTRL4 */
#define MBCICHFC_SHIFT 0
#define MBCICHFC_MASK (0xF << MBCICHFC_SHIFT)
/* MAX8997_REG_MBCCTRL5 */
#define ITOPOFF_SHIFT 0
#define ITOPOFF_MASK (0xF << ITOPOFF_SHIFT)
/* MAX8997_REG_MBCCTRL6 */
#define AUTOSTOP_SHIFT 5
#define AUTOSTOP_MASK (1 << AUTOSTOP_SHIFT)
/* MAX8997_REG_OTPCGHCVS */
#define OTPCGHCVS_SHIFT 0
#define OTPCGHCVS_MASK (3 << OTPCGHCVS_SHIFT)
enum {
BAT_NOT_DETECTED,
BAT_DETECTED
};
struct chg_data {
struct device *dev;
struct max8997_dev *max8997;
struct power_supply psy_bat;
struct max8997_power_data *power;
int irq_topoff;
int irq_chgins;
int irq_chgrm;
};
static enum power_supply_property max8997_battery_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_ONLINE,
};
/* vf check */
static bool max8997_check_detbat(struct chg_data *chg)
{
struct i2c_client *i2c = chg->max8997->i2c;
u8 data = 0;
int ret;
ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &data);
if (ret < 0) {
dev_err(chg->dev, "%s: max8997_read_reg error(%d)\n", __func__,
ret);
return ret;
}
if (data & DETBAT_MASK)
printk(KERN_WARNING "%s: batt not detected(0x%x)\n", __func__,
data);
return data & DETBAT_MASK;
}
/* whether charging enabled or not */
static bool max8997_check_vdcin(struct chg_data *chg)
{
struct i2c_client *i2c = chg->max8997->i2c;
u8 data = 0;
int ret;
ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &data);
if (ret < 0) {
dev_err(chg->dev, "%s: max8997_read_reg error(%d)\n", __func__,
ret);
return ret;
}
return data & DCINOK_MASK;
}
static int max8997_chg_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct chg_data *chg = container_of(psy, struct chg_data, psy_bat);
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
if (max8997_check_vdcin(chg))
val->intval = POWER_SUPPLY_STATUS_CHARGING;
else
val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
break;
case POWER_SUPPLY_PROP_PRESENT:
if (max8997_check_detbat(chg))
val->intval = BAT_NOT_DETECTED;
else
val->intval = BAT_DETECTED;
break;
case POWER_SUPPLY_PROP_ONLINE:
/* battery is always online */
val->intval = 1;
break;
default:
return -EINVAL;
}
return 0;
}
static int max8997_disable_charging(struct chg_data *chg)
{
struct i2c_client *i2c = chg->max8997->i2c;
int ret;
u8 mask;
dev_info(chg->dev, "%s: disable charging\n", __func__);
mask = MBCHOSTEN_MASK | VCHGR_FC_MASK;
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL2, 0, mask);
if (ret < 0)
dev_err(chg->dev, "%s: fail update reg!!!\n", __func__);
return ret;
}
static int max8997_set_charging_current(struct chg_data *chg, int chg_current)
{
struct i2c_client *i2c = chg->max8997->i2c;
int ret;
u8 val;
if (chg_current < 200 || chg_current > 950)
return -EINVAL;
val = ((chg_current - 200) / 50) & 0xf;
dev_info(chg->dev, "%s: charging current=%d", __func__, chg_current);
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL4,
(val << MBCICHFC_SHIFT), MBCICHFC_MASK);
if (ret)
dev_err(chg->dev, "%s: fail to write chg current(%d)\n",
__func__, ret);
return ret;
}
static int max8997_enable_charging(struct chg_data *chg)
{
struct i2c_client *i2c = chg->max8997->i2c;
int ret;
u8 val, mask;
/* set auto stop disable */
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL6,
(0 << AUTOSTOP_SHIFT), AUTOSTOP_MASK);
if (ret)
dev_err(chg->dev, "%s: failt to disable autostop(%d)\n",
__func__, ret);
/* set fast charging enable and main battery charging enable */
val = (1 << MBCHOSTEN_SHIFT) | (1 << VCHGR_FC_SHIFT);
mask = MBCHOSTEN_MASK | VCHGR_FC_MASK;
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL2, val, mask);
if (ret)
dev_err(chg->dev, "%s: failt to enable charging(%d)\n",
__func__, ret);
return ret;
}
/* TODO: remove this function later */
static int max8997_enable_charging_x(struct chg_data *chg, int charge_type)
{
struct i2c_client *i2c = chg->max8997->i2c;
int ret;
u8 val, mask;
/* enable charging */
if (charge_type == POWER_SUPPLY_CHARGE_TYPE_FAST) {
/* ac */
dev_info(chg->dev, "%s: TA charging", __func__);
/* set fast charging current : 650mA */
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL4,
(9 << MBCICHFC_SHIFT), MBCICHFC_MASK);
if (ret)
goto err;
} else if (charge_type == POWER_SUPPLY_CHARGE_TYPE_TRICKLE) {
/* usb */
dev_info(chg->dev, "%s: USB charging", __func__);
/* set fast charging current : 450mA */
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL4,
(5 << MBCICHFC_SHIFT), MBCICHFC_MASK);
if (ret)
goto err;
} else {
dev_err(chg->dev, "%s: invalid arg\n", __func__);
ret = -EINVAL;
goto err;
}
/* set auto stop disable */
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL6,
(0 << AUTOSTOP_SHIFT), AUTOSTOP_MASK);
if (ret)
goto err;
/* set fast charging enable and main battery charging enable */
val = (1 << MBCHOSTEN_SHIFT) | (1 << VCHGR_FC_SHIFT);
mask = MBCHOSTEN_MASK | VCHGR_FC_MASK;
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL2, val, mask);
if (ret)
goto err;
return 0;
err:
dev_err(chg->dev, "%s: max8997 update reg error!(%d)\n", __func__, ret);
return ret;
}
static int max8997_chg_set_property(struct power_supply *psy,
enum power_supply_property psp,
const union power_supply_propval *val)
{
int ret, reg_val;
struct chg_data *chg = container_of(psy, struct chg_data, psy_bat);
struct i2c_client *i2c = chg->max8997->i2c;
switch (psp) {
case POWER_SUPPLY_PROP_CHARGE_TYPE: /* TODO: remove this */
if (val->intval == POWER_SUPPLY_CHARGE_TYPE_NONE)
ret = max8997_disable_charging(chg);
else
ret = max8997_enable_charging_x(chg, val->intval);
break;
case POWER_SUPPLY_PROP_CURRENT_NOW: /* Set charging current */
ret = max8997_set_charging_current(chg, val->intval);
break;
case POWER_SUPPLY_PROP_STATUS: /* Enable/Disable charging */
if (val->intval == POWER_SUPPLY_STATUS_CHARGING)
ret = max8997_enable_charging(chg);
else
ret = max8997_disable_charging(chg);
break;
case POWER_SUPPLY_PROP_CHARGE_FULL: /* Set recharging current */
if (val->intval < 50 || val->intval > 200) {
dev_err(chg->dev, "%s: invalid topoff current(%d)\n",
__func__, val->intval);
return -EINVAL;
}
reg_val = (val->intval - 50) / 10;
dev_info(chg->dev, "%s: Set toppoff current to 0x%x\n",
__func__, reg_val);
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL5,
(reg_val << ITOPOFF_SHIFT), ITOPOFF_MASK);
if (ret) {
dev_err(chg->dev, "%s: max8997 update reg error(%d)\n",
__func__, ret);
return ret;
}
break;
default:
return -EINVAL;
}
return ret;
}
static irqreturn_t max8997_chg_topoff_irq(int irq, void *data)
{
struct chg_data *chg = data;
int ret = 0;
if (chg->power->topoff_cb)
ret = chg->power->topoff_cb();
if (ret) {
dev_err(chg->dev, "%s: error from topoff_cb(%d)\n", __func__,
ret);
return IRQ_HANDLED;
}
dev_info(chg->dev, " Topoff IRQ occurred!\n");
return IRQ_HANDLED;
}
static irqreturn_t max8997_chg_charger_irq(int irq, void *data)
{
struct chg_data *chg = data;
int ret = 0;
bool insert = false;
insert = (irq == chg->irq_chgins);
dev_info(chg->dev, "%s: charger IRQ(%d) occurred!\n", __func__, insert);
if (chg->power->set_charger)
ret = chg->power->set_charger(insert);
if (ret) {
dev_err(chg->dev, "%s: error from set_charger(%d)\n", __func__,
ret);
return IRQ_HANDLED;
}
dev_info(chg->dev, "%s: charger IRQ(%d) occurred!\n", __func__, insert);
return IRQ_HANDLED;
}
static __devinit int max8997_charger_probe(struct platform_device *pdev)
{
struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
struct i2c_client *i2c = max8997->i2c;
struct chg_data *chg;
int ret = 0;
dev_info(&pdev->dev, "%s : MAX8997 Charger Driver Loading\n", __func__);
chg = kzalloc(sizeof(*chg), GFP_KERNEL);
if (!chg)
return -ENOMEM;
chg->dev = &pdev->dev;
chg->max8997 = max8997;
chg->power = pdata->power;
chg->irq_topoff = max8997->irq_base + MAX8997_IRQ_TOPOFF;
chg->irq_chgins = max8997->irq_base + MAX8997_IRQ_CHGINS;
chg->irq_chgrm = max8997->irq_base + MAX8997_IRQ_CHGRM;
chg->psy_bat.name = "max8997-charger",
chg->psy_bat.type = POWER_SUPPLY_TYPE_BATTERY,
chg->psy_bat.properties = max8997_battery_props,
chg->psy_bat.num_properties = ARRAY_SIZE(max8997_battery_props),
chg->psy_bat.get_property = max8997_chg_get_property,
chg->psy_bat.set_property = max8997_chg_set_property,
platform_set_drvdata(pdev, chg);
/* TODO: configure by platform data*/
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, /* Disable */
(0x7 << TFCH_SHIFT), TFCH_MASK);
if (ret < 0)
goto err_kfree;
/* TODO: configure by platform data*/
ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL3, /* 4.2V */
(0x0 << MBCCV_SHIFT), MBCCV_MASK);
if (ret < 0)
goto err_kfree;
/* Set OVP voltage threshold */
ret = max8997_update_reg(i2c, MAX8997_REG_OTPCGHCVS,
(0x01 << OTPCGHCVS_SHIFT), OTPCGHCVS_MASK);
if (ret < 0)
goto err_kfree;
/* init power supplier framework */
ret = power_supply_register(&pdev->dev, &chg->psy_bat);
if (ret) {
pr_err("Failed to register power supply psy_bat\n");
goto err_kfree;
}
ret = request_threaded_irq(chg->irq_topoff, NULL,
max8997_chg_topoff_irq, 0, "chg-topoff", chg);
if (ret < 0)
dev_err(&pdev->dev, "%s: fail to request topoff IRQ: %d: %d\n",
__func__, chg->irq_topoff, ret);
ret = request_threaded_irq(chg->irq_chgins, NULL,
max8997_chg_charger_irq, 0, "chg-insert", chg);
if (ret < 0)
dev_err(&pdev->dev, "%s: fail to request chgins IRQ: %d: %d\n",
__func__, chg->irq_chgins, ret);
ret = request_threaded_irq(chg->irq_chgrm, NULL,
max8997_chg_charger_irq, 0, "chg-remove", chg);
if (ret < 0)
dev_err(&pdev->dev, "%s: fail to request chgrm IRQ: %d: %d\n",
__func__, chg->irq_chgrm, ret);
return 0;
err_kfree:
kfree(chg);
return ret;
}
static int __devexit max8997_charger_remove(struct platform_device *pdev)
{
struct chg_data *chg = platform_get_drvdata(pdev);
power_supply_unregister(&chg->psy_bat);
kfree(chg);
return 0;
}
static int max8997_charger_suspend(struct device *dev)
{
return 0;
}
static int max8997_charger_resume(struct device *dev)
{
return 0;
}
static const struct dev_pm_ops max8997_charger_pm_ops = {
.suspend = max8997_charger_suspend,
.resume = max8997_charger_resume,
};
static struct platform_driver max8997_charger_driver = {
.driver = {
.name = "max8997-charger",
.owner = THIS_MODULE,
.pm = &max8997_charger_pm_ops,
},
.probe = max8997_charger_probe,
.remove = __devexit_p(max8997_charger_remove),
};
static int __init max8997_charger_init(void)
{
return platform_driver_register(&max8997_charger_driver);
}
static void __exit max8997_charger_exit(void)
{
platform_driver_register(&max8997_charger_driver);
}
module_init(max8997_charger_init);
module_exit(max8997_charger_exit);
MODULE_DESCRIPTION("MAXIM 8997 charger control driver");
MODULE_AUTHOR("<ms925.kim@samsung.com>");
MODULE_LICENSE("GPL");
|