aboutsummaryrefslogtreecommitdiff
path: root/drivers/power/pcf50606-charger.c
blob: 04b30a67e5a8e7cb01e5270fdec6e1a2df98d03e (plain)
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
/* NXP PCF50606 Main Battery Charger Driver
 *
 * (C) 2006-2008 by Openmoko, Inc.
 * Author: Balaji Rao <balajirrao@openmoko.org>
 * All rights reserved.
 *
 * Broken down from monstrous PCF50606 driver mainly by
 * Harald Welte, Andy Green and Werner Almesberger
 *
 *  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.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/sysfs.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>

#include <linux/mfd/pcf50606/core.h>
#include <linux/mfd/pcf50606/mbc.h>

struct pcf50606_mbc {
	struct pcf50606 *pcf;

	int charger_online;
	struct power_supply charger;
};

int pcf50606_charge_fast(struct pcf50606 *pcf, int on)
{
	struct pcf50606_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);

	/*
	 * This is a fix to work around boot-time ordering problems if
	 * the s3c2410_udc is initialized before the pcf50606 mbc is
	 * ready.
	 */
	if (!mbc)
		return -ENODEV;

	if (on) {
		pcf50606_reg_set_bit_mask(pcf, PCF50606_REG_MBCC1,
				 PCF50606_MBCC1_AUTOFST,
				 PCF50606_MBCC1_AUTOFST);\
			mbc->charger_online = 1;
	} else {
		/* disable automatic fast-charge */
		pcf50606_reg_clear_bits(pcf, PCF50606_REG_MBCC1,
					PCF50606_MBCC1_AUTOFST);
		/* switch to idle mode to abort existing charge process */
		pcf50606_reg_set_bit_mask(pcf, PCF50606_REG_MBCC1,
				PCF50606_MBCC1_CHGMOD_MASK,
				PCF50606_MBCC1_CHGMOD_IDLE);
			mbc->charger_online = 0;
	}

    return 0;
}
EXPORT_SYMBOL_GPL(pcf50606_charge_fast);

static ssize_t
show_charge_mode(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct pcf50606_mbc *mbc = dev_get_drvdata(dev);

	uint8_t charge_mode = pcf50606_reg_read(mbc->pcf, PCF50606_REG_MBCC1);
	charge_mode &= PCF50606_MBCC1_CHGMOD_MASK;

	return sprintf(buf, "%d\n", charge_mode);
}

static ssize_t set_charge_mode(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
{
	struct pcf50606_mbc *mbc = dev_get_drvdata(dev);
	uint8_t mbcc1 = pcf50606_reg_read(mbc->pcf, PCF50606_REG_MBCC1);

	mbcc1 &= ~PCF50606_MBCC1_CHGMOD_MASK;

	if (!strcmp(buf, "qualification"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_QUAL;
	else if (!strcmp(buf, "pre"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_PRE;
	else if (!strcmp(buf, "trickle"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_TRICKLE;
	else if (!strcmp(buf, "fast_cccv"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_FAST_CCCV;
	/* We don't allow the other fast modes for security reasons */
	else if (!strcmp(buf, "idle"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_IDLE;
	else
		return -EINVAL;

	pcf50606_reg_write(mbc->pcf, PCF50606_REG_MBCC1, mbcc1);

	return count;
}

static DEVICE_ATTR(charge_mode, S_IRUGO, show_charge_mode, set_charge_mode);

static void
pcf50606_mbc_irq_handler(int irq, void *data)
{
	struct pcf50606_mbc *mbc = data;

	power_supply_changed(&mbc->charger);

	if (mbc->pcf->pdata->mbc_event_callback)
		mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
}

static int charger_get_property(struct power_supply *psy,
			enum power_supply_property psp,
			union power_supply_propval *val)
{
	struct pcf50606_mbc *mbc = container_of(psy, struct pcf50606_mbc, charger);
	int ret = 0;

	switch (psp) {
	case POWER_SUPPLY_PROP_ONLINE:
		val->intval =  mbc->charger_online;
		break;
	default:
		ret = -EINVAL;
		break;
	}
	return ret;
}

static enum power_supply_property power_props[] = {
	POWER_SUPPLY_PROP_ONLINE,
};

static const uint8_t mbc_irq_handlers[] = {
	PCF50606_IRQ_CHGINS,
	PCF50606_IRQ_CHGRM,
	PCF50606_IRQ_CHGFOK,
	PCF50606_IRQ_CHGERR,
	PCF50606_IRQ_CHGFRDY,
	PCF50606_IRQ_CHGPROT,
};

static int __devinit pcf50606_mbc_probe(struct platform_device *pdev)
{
	struct pcf50606_mbc *mbc;
	int ret;
	size_t i;
	uint8_t oocs;

	mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
	if (!mbc)
		return -ENOMEM;

	mbc->pcf = dev_to_pcf50606(pdev->dev.parent);

	mbc->charger.name		= "charger";
	mbc->charger.type		= POWER_SUPPLY_TYPE_MAINS;
	mbc->charger.properties		= power_props;
	mbc->charger.num_properties	= ARRAY_SIZE(power_props);
	mbc->charger.get_property	= &charger_get_property;
	mbc->charger.supplied_to	= mbc->pcf->pdata->batteries;
	mbc->charger.num_supplicants	= mbc->pcf->pdata->num_batteries;

	ret = power_supply_register(&pdev->dev, &mbc->charger);
	if (ret) {
		dev_err(mbc->pcf->dev, "failed to register charger\n");
		kfree(mbc);
		return ret;
	}

	/* Set up IRQ handlers */
	for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
		pcf50606_register_irq(mbc->pcf, mbc_irq_handlers[i],
					pcf50606_mbc_irq_handler, mbc);

	ret = sysfs_create_file(&pdev->dev.kobj,  &dev_attr_charge_mode)
	if (ret)
		dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");

	oocs = pcf50606_reg_read(mbc->pcf, PCF50606_REG_OOCS);
	if (oocs & PCF50606_OOCS_CHGOK)
		pcf50606_mbc_irq_handler(PCF50606_IRQ_CHGINS, mbc);

	platform_set_drvdata(pdev, mbc);

	return 0;
}

static int __devexit pcf50606_mbc_remove(struct platform_device *pdev)
{
	struct pcf50606_mbc *mbc = platform_get_drvdata(pdev);
	size_t i;

	/* Remove IRQ handlers */
	for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
		pcf50606_free_irq(mbc->pcf, mbc_irq_handlers[i]);

	power_supply_unregister(&mbc->charger);

	platform_set_drvdata(pdev, NULL);
	kfree(mbc);

	return 0;
}

static struct platform_driver pcf50606_mbc_driver = {
	.driver = {
		.name = "pcf50606-mbc",
	},
	.probe = pcf50606_mbc_probe,
	.remove = __devexit_p(pcf50606_mbc_remove),
};

static int __init pcf50606_mbc_init(void)
{
	return platform_driver_register(&pcf50606_mbc_driver);
}
module_init(pcf50606_mbc_init);

static void __exit pcf50606_mbc_exit(void)
{
	platform_driver_unregister(&pcf50606_mbc_driver);
}
module_exit(pcf50606_mbc_exit);

MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
MODULE_DESCRIPTION("PCF50606 mbc driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:pcf50606-mbc");