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
|
/*
* max8677_charger.c - Maxim 8677 USB/Adapter Charger Driver
*
* Copyright (C) 2011 Samsung Electronics
* MyungJoo Ham <myungjoo.ham@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/power_supply.h>
#include <linux/platform_device.h>
#include <linux/power/max8677_charger.h>
#include <mach/gpio.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h>
#define MAX8677_DELAY msecs_to_jiffies(3000)
//#define DEBUG_MAX8677
struct max8677_data {
struct max8677_pdata *pdata;
struct device *dev;
struct power_supply usb;
struct delayed_work work;
bool usb_in;
};
static enum power_supply_property max8677_charger_props[] = {
POWER_SUPPLY_PROP_STATUS, /* Charger status output */
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_ONLINE, /* External power source */
POWER_SUPPLY_PROP_HEALTH, /* Fault or OK */
};
static int max8677_get_usb_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct max8677_data *data = container_of(psy,
struct max8677_data, usb);
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
if (data->pdata->chg) {
if (gpio_get_value(data->pdata->chg) == 0) {
val->intval = POWER_SUPPLY_STATUS_CHARGING;
}
else {
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
}
}
break;
case POWER_SUPPLY_PROP_PRESENT:
case POWER_SUPPLY_PROP_ONLINE:
val->intval = 0;
if(data->usb_in) val->intval = 1;
break;
case POWER_SUPPLY_PROP_HEALTH:
val->intval = POWER_SUPPLY_HEALTH_GOOD;
break;
default:
return -EINVAL;
}
return 0;
}
static void max8677_usb_status(struct max8677_data *data)
{
bool usb_in;
struct max8677_pdata *pdata = data->pdata;
usb_in = gpio_get_value(pdata->uok) ? false : true;
if (data->usb_in == usb_in) return;
data->usb_in = usb_in;
/* Do not touch Current-Limit-Mode */
/* Charger Enable / Disable (cen is negated) */
if (pdata->cen)
gpio_set_value(pdata->cen, usb_in ? 0 : 1);
dev_dbg(data->dev, "USB Charger %s.\n", usb_in ?
"Connected" : "Disconnected");
power_supply_changed(&data->usb);
#if defined(DEBUG_MAX8677)
printk("%s : power_supply_changed!\n", __func__);
#endif
}
static void max8677_work(struct work_struct *work)
{
struct max8677_data *data = container_of(work, struct max8677_data, work.work);
max8677_usb_status(data);
#if defined(DEBUG_MAX8677)
if(data->pdata->done) {
printk("USB Charger Status = %d\n", gpio_get_value(data->pdata->done));
}
#endif
schedule_delayed_work(&data->work, MAX8677_DELAY);
}
static __devinit int max8677_probe(struct platform_device *pdev)
{
struct max8677_data *data;
struct device *dev = &pdev->dev;
struct max8677_pdata *pdata = pdev->dev.platform_data;
int ret = 0;
data = kzalloc(sizeof(struct max8677_data), GFP_KERNEL);
if (data == NULL) {
dev_err(dev, "Cannot allocate memory.\n");
return -ENOMEM;
}
if(pdata == NULL) {
dev_err(dev, "Cannot find platform data.\n");
return -EIO;
}
data->pdata = pdata;
data->dev = dev;
platform_set_drvdata(pdev, data);
if(pdata->uok) {
if(gpio_request(pdata->uok, "Charger UOK")) {
dev_err(dev, "Charger UOK GPIO Request fail\n");
ret = -EINVAL;
goto err_uok;
}
gpio_direction_input(pdata->uok);
s3c_gpio_setpull(pdata->uok, S3C_GPIO_PULL_NONE);
}
else {
dev_err(dev, "USB Check GPIO define error!\n");
ret = -EINVAL;
goto err_uok;
}
if (pdata->cen) {
if(gpio_request(pdata->cen, "Charger CEN")) {
dev_err(dev, "Charger CEN GPIO Request fail\n");
ret = -EINVAL;
goto err_cen;
}
gpio_direction_output(pdata->cen, 1);
s3c_gpio_setpull(pdata->cen, S3C_GPIO_PULL_NONE);
}
else {
dev_err(dev, "Charger CEN GPIO define error!\n");
ret = -EINVAL;
goto err_cen;
}
if (pdata->chg) {
if(gpio_request(pdata->chg, "Charger CHG")) {
dev_err(dev, "Charger CHG GPIO Request fail\n");
ret = -EINVAL;
goto err_chg;
}
gpio_direction_input(pdata->chg);
s3c_gpio_setpull(pdata->chg, S3C_GPIO_PULL_NONE);
}
else {
dev_err(dev, "Charger CHG GPIO define error!\n");
ret = -EINVAL;
goto err_chg;
}
if (pdata->done) {
if(gpio_request(pdata->done, "Charger DONE")) {
dev_err(dev, "Charger DONE GPIO Request fail\n");
ret = -EINVAL;
goto err_chg;
}
gpio_direction_input(pdata->done);
s3c_gpio_setpull(pdata->done, S3C_GPIO_PULL_NONE);
}
else {
dev_err(dev, "Charger DONE GPIO define error!\n");
ret = -EINVAL;
goto err_done;
}
data->usb_in = false;
data->usb.name = "max8677_charger-usb";
data->usb.type = POWER_SUPPLY_TYPE_USB;
data->usb.get_property = max8677_get_usb_property;
data->usb.properties = max8677_charger_props;
data->usb.num_properties = ARRAY_SIZE(max8677_charger_props);
if ((ret = power_supply_register(dev, &data->usb))) {
dev_err(dev, "failed: power supply max8677-usb register.\n");
goto err;
}
else {
dev_info(dev, "power supply max8677-usb registerd.\n");
}
INIT_DELAYED_WORK_DEFERRABLE(&data->work, max8677_work);
schedule_delayed_work(&data->work, MAX8677_DELAY);
return 0;
err:
err_done:
if(pdata->done) gpio_free(pdata->done);
err_chg:
if(pdata->chg) gpio_free(pdata->chg);
err_cen:
if(pdata->cen) gpio_free(pdata->cen);
err_uok:
if(pdata->uok) gpio_free(pdata->uok);
kfree(data);
return ret;
}
static __devexit int max8677_remove(struct platform_device *pdev)
{
struct max8677_data *data = platform_get_drvdata(pdev);
if (data) {
struct max8677_pdata *pdata = data->pdata;
if(pdata->chg) gpio_free(pdata->chg);
if(pdata->cen) gpio_free(pdata->cen);
if(pdata->uok) gpio_free(pdata->uok);
if(pdata->done) gpio_free(pdata->done);
cancel_delayed_work(&data->work);
kfree(data);
}
return 0;
}
static struct platform_driver max8677_driver = {
.probe = max8677_probe,
.remove = __devexit_p(max8677_remove),
.driver = {
.name = "max8677-charger",
.owner = THIS_MODULE,
},
};
static int __init max8677_init(void)
{
return platform_driver_register(&max8677_driver);
}
module_init(max8677_init);
static void __exit max8677_exit(void)
{
platform_driver_unregister(&max8677_driver);
}
module_exit(max8677_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("max8677 Charger Driver");
MODULE_AUTHOR("Hardkernel Co,. LTD");
MODULE_ALIAS("max8677-charger");
|