1 /* NXP PCF50633 Main Battery Charger Driver
2  *
3  * (C) 2006-2008 by Openmoko, Inc.
4  * Author: Balaji Rao <balajirrao@openmoko.org>
5  * All rights reserved.
6  *
7  * Broken down from monstrous PCF50633 driver mainly by
8  * Harald Welte, Andy Green and Werner Almesberger
9  *
10  *  This program is free software; you can redistribute  it and/or modify it
11  *  under  the terms of  the GNU General  Public License as published by the
12  *  Free Software Foundation;  either version 2 of the  License, or (at your
13  *  option) any later version.
14  *
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/device.h>
23 #include <linux/sysfs.h>
24 #include <linux/platform_device.h>
25 #include <linux/power_supply.h>
26 
27 #include <linux/mfd/pcf50633/core.h>
28 #include <linux/mfd/pcf50633/mbc.h>
29 
30 struct pcf50633_mbc {
31 	struct pcf50633 *pcf;
32 
33 	int adapter_online;
34 	int usb_online;
35 
36 	struct power_supply *usb;
37 	struct power_supply *adapter;
38 	struct power_supply *ac;
39 };
40 
41 int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)
42 {
43 	struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
44 	int ret = 0;
45 	u8 bits;
46 	u8 mbcs2, chgmod;
47 	unsigned int mbcc5;
48 
49 	if (ma >= 1000) {
50 		bits = PCF50633_MBCC7_USB_1000mA;
51 		ma = 1000;
52 	} else if (ma >= 500) {
53 		bits = PCF50633_MBCC7_USB_500mA;
54 		ma = 500;
55 	} else if (ma >= 100) {
56 		bits = PCF50633_MBCC7_USB_100mA;
57 		ma = 100;
58 	} else {
59 		bits = PCF50633_MBCC7_USB_SUSPEND;
60 		ma = 0;
61 	}
62 
63 	ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
64 					PCF50633_MBCC7_USB_MASK, bits);
65 	if (ret)
66 		dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma);
67 	else
68 		dev_info(pcf->dev, "usb curlim to %d mA\n", ma);
69 
70 	/*
71 	 * We limit the charging current to be the USB current limit.
72 	 * The reason is that on pcf50633, when it enters PMU Standby mode,
73 	 * which it does when the device goes "off", the USB current limit
74 	 * reverts to the variant default.  In at least one common case, that
75 	 * default is 500mA.  By setting the charging current to be the same
76 	 * as the USB limit we set here before PMU standby, we enforce it only
77 	 * using the correct amount of current even when the USB current limit
78 	 * gets reset to the wrong thing
79 	 */
80 
81 	if (mbc->pcf->pdata->charger_reference_current_ma) {
82 		mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
83 		if (mbcc5 > 255)
84 			mbcc5 = 255;
85 		pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
86 	}
87 
88 	mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
89 	chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
90 
91 	/* If chgmod == BATFULL, setting chgena has no effect.
92 	 * Datasheet says we need to set resume instead but when autoresume is
93 	 * used resume doesn't work. Clear and set chgena instead.
94 	 */
95 	if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
96 		pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
97 				PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA);
98 	else {
99 		pcf50633_reg_clear_bits(pcf, PCF50633_REG_MBCC1,
100 				PCF50633_MBCC1_CHGENA);
101 		pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
102 				PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA);
103 	}
104 
105 	power_supply_changed(mbc->usb);
106 
107 	return ret;
108 }
109 EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
110 
111 int pcf50633_mbc_get_status(struct pcf50633 *pcf)
112 {
113 	struct pcf50633_mbc *mbc  = platform_get_drvdata(pcf->mbc_pdev);
114 	int status = 0;
115 	u8 chgmod;
116 
117 	if (!mbc)
118 		return 0;
119 
120 	chgmod = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2)
121 		& PCF50633_MBCS2_MBC_MASK;
122 
123 	if (mbc->usb_online)
124 		status |= PCF50633_MBC_USB_ONLINE;
125 	if (chgmod == PCF50633_MBCS2_MBC_USB_PRE ||
126 	    chgmod == PCF50633_MBCS2_MBC_USB_PRE_WAIT ||
127 	    chgmod == PCF50633_MBCS2_MBC_USB_FAST ||
128 	    chgmod == PCF50633_MBCS2_MBC_USB_FAST_WAIT)
129 		status |= PCF50633_MBC_USB_ACTIVE;
130 	if (mbc->adapter_online)
131 		status |= PCF50633_MBC_ADAPTER_ONLINE;
132 	if (chgmod == PCF50633_MBCS2_MBC_ADP_PRE ||
133 	    chgmod == PCF50633_MBCS2_MBC_ADP_PRE_WAIT ||
134 	    chgmod == PCF50633_MBCS2_MBC_ADP_FAST ||
135 	    chgmod == PCF50633_MBCS2_MBC_ADP_FAST_WAIT)
136 		status |= PCF50633_MBC_ADAPTER_ACTIVE;
137 
138 	return status;
139 }
140 EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
141 
142 int pcf50633_mbc_get_usb_online_status(struct pcf50633 *pcf)
143 {
144 	struct pcf50633_mbc *mbc  = platform_get_drvdata(pcf->mbc_pdev);
145 
146 	if (!mbc)
147 		return 0;
148 
149 	return mbc->usb_online;
150 }
151 EXPORT_SYMBOL_GPL(pcf50633_mbc_get_usb_online_status);
152 
153 static ssize_t
154 show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
155 {
156 	struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
157 
158 	u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
159 	u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
160 
161 	return sprintf(buf, "%d\n", chgmod);
162 }
163 static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
164 
165 static ssize_t
166 show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
167 {
168 	struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
169 	u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
170 						PCF50633_MBCC7_USB_MASK;
171 	unsigned int ma;
172 
173 	if (usblim == PCF50633_MBCC7_USB_1000mA)
174 		ma = 1000;
175 	else if (usblim == PCF50633_MBCC7_USB_500mA)
176 		ma = 500;
177 	else if (usblim == PCF50633_MBCC7_USB_100mA)
178 		ma = 100;
179 	else
180 		ma = 0;
181 
182 	return sprintf(buf, "%u\n", ma);
183 }
184 
185 static ssize_t set_usblim(struct device *dev,
186 		struct device_attribute *attr, const char *buf, size_t count)
187 {
188 	struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
189 	unsigned long ma;
190 	int ret;
191 
192 	ret = kstrtoul(buf, 10, &ma);
193 	if (ret)
194 		return ret;
195 
196 	pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
197 
198 	return count;
199 }
200 
201 static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
202 
203 static ssize_t
204 show_chglim(struct device *dev, struct device_attribute *attr, char *buf)
205 {
206 	struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
207 	u8 mbcc5 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC5);
208 	unsigned int ma;
209 
210 	if (!mbc->pcf->pdata->charger_reference_current_ma)
211 		return -ENODEV;
212 
213 	ma = (mbc->pcf->pdata->charger_reference_current_ma *  mbcc5) >> 8;
214 
215 	return sprintf(buf, "%u\n", ma);
216 }
217 
218 static ssize_t set_chglim(struct device *dev,
219 		struct device_attribute *attr, const char *buf, size_t count)
220 {
221 	struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
222 	unsigned long ma;
223 	unsigned int mbcc5;
224 	int ret;
225 
226 	if (!mbc->pcf->pdata->charger_reference_current_ma)
227 		return -ENODEV;
228 
229 	ret = kstrtoul(buf, 10, &ma);
230 	if (ret)
231 		return ret;
232 
233 	mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
234 	if (mbcc5 > 255)
235 		mbcc5 = 255;
236 	pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
237 
238 	return count;
239 }
240 
241 /*
242  * This attribute allows to change MBC charging limit on the fly
243  * independently of usb current limit. It also gets set automatically every
244  * time usb current limit is changed.
245  */
246 static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim);
247 
248 static struct attribute *pcf50633_mbc_sysfs_attrs[] = {
249 	&dev_attr_chgmode.attr,
250 	&dev_attr_usb_curlim.attr,
251 	&dev_attr_chg_curlim.attr,
252 	NULL,
253 };
254 
255 ATTRIBUTE_GROUPS(pcf50633_mbc_sysfs);
256 
257 static void
258 pcf50633_mbc_irq_handler(int irq, void *data)
259 {
260 	struct pcf50633_mbc *mbc = data;
261 
262 	/* USB */
263 	if (irq == PCF50633_IRQ_USBINS) {
264 		mbc->usb_online = 1;
265 	} else if (irq == PCF50633_IRQ_USBREM) {
266 		mbc->usb_online = 0;
267 		pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
268 	}
269 
270 	/* Adapter */
271 	if (irq == PCF50633_IRQ_ADPINS)
272 		mbc->adapter_online = 1;
273 	else if (irq == PCF50633_IRQ_ADPREM)
274 		mbc->adapter_online = 0;
275 
276 	power_supply_changed(mbc->ac);
277 	power_supply_changed(mbc->usb);
278 	power_supply_changed(mbc->adapter);
279 
280 	if (mbc->pcf->pdata->mbc_event_callback)
281 		mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
282 }
283 
284 static int adapter_get_property(struct power_supply *psy,
285 			enum power_supply_property psp,
286 			union power_supply_propval *val)
287 {
288 	struct pcf50633_mbc *mbc = power_supply_get_drvdata(psy);
289 	int ret = 0;
290 
291 	switch (psp) {
292 	case POWER_SUPPLY_PROP_ONLINE:
293 		val->intval =  mbc->adapter_online;
294 		break;
295 	default:
296 		ret = -EINVAL;
297 		break;
298 	}
299 	return ret;
300 }
301 
302 static int usb_get_property(struct power_supply *psy,
303 			enum power_supply_property psp,
304 			union power_supply_propval *val)
305 {
306 	struct pcf50633_mbc *mbc = power_supply_get_drvdata(psy);
307 	int ret = 0;
308 	u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
309 						PCF50633_MBCC7_USB_MASK;
310 
311 	switch (psp) {
312 	case POWER_SUPPLY_PROP_ONLINE:
313 		val->intval = mbc->usb_online &&
314 				(usblim <= PCF50633_MBCC7_USB_500mA);
315 		break;
316 	default:
317 		ret = -EINVAL;
318 		break;
319 	}
320 	return ret;
321 }
322 
323 static int ac_get_property(struct power_supply *psy,
324 			enum power_supply_property psp,
325 			union power_supply_propval *val)
326 {
327 	struct pcf50633_mbc *mbc = power_supply_get_drvdata(psy);
328 	int ret = 0;
329 	u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
330 						PCF50633_MBCC7_USB_MASK;
331 
332 	switch (psp) {
333 	case POWER_SUPPLY_PROP_ONLINE:
334 		val->intval = mbc->usb_online &&
335 				(usblim == PCF50633_MBCC7_USB_1000mA);
336 		break;
337 	default:
338 		ret = -EINVAL;
339 		break;
340 	}
341 	return ret;
342 }
343 
344 static enum power_supply_property power_props[] = {
345 	POWER_SUPPLY_PROP_ONLINE,
346 };
347 
348 static const u8 mbc_irq_handlers[] = {
349 	PCF50633_IRQ_ADPINS,
350 	PCF50633_IRQ_ADPREM,
351 	PCF50633_IRQ_USBINS,
352 	PCF50633_IRQ_USBREM,
353 	PCF50633_IRQ_BATFULL,
354 	PCF50633_IRQ_CHGHALT,
355 	PCF50633_IRQ_THLIMON,
356 	PCF50633_IRQ_THLIMOFF,
357 	PCF50633_IRQ_USBLIMON,
358 	PCF50633_IRQ_USBLIMOFF,
359 	PCF50633_IRQ_LOWSYS,
360 	PCF50633_IRQ_LOWBAT,
361 };
362 
363 static const struct power_supply_desc pcf50633_mbc_adapter_desc = {
364 	.name		= "adapter",
365 	.type		= POWER_SUPPLY_TYPE_MAINS,
366 	.properties	= power_props,
367 	.num_properties	= ARRAY_SIZE(power_props),
368 	.get_property	= &adapter_get_property,
369 };
370 
371 static const struct power_supply_desc pcf50633_mbc_usb_desc = {
372 	.name		= "usb",
373 	.type		= POWER_SUPPLY_TYPE_USB,
374 	.properties	= power_props,
375 	.num_properties	= ARRAY_SIZE(power_props),
376 	.get_property	= usb_get_property,
377 };
378 
379 static const struct power_supply_desc pcf50633_mbc_ac_desc = {
380 	.name		= "ac",
381 	.type		= POWER_SUPPLY_TYPE_MAINS,
382 	.properties	= power_props,
383 	.num_properties	= ARRAY_SIZE(power_props),
384 	.get_property	= ac_get_property,
385 };
386 
387 static int pcf50633_mbc_probe(struct platform_device *pdev)
388 {
389 	struct power_supply_config psy_cfg = {};
390 	struct power_supply_config usb_psy_cfg;
391 	struct pcf50633_mbc *mbc;
392 	int i;
393 	u8 mbcs1;
394 
395 	mbc = devm_kzalloc(&pdev->dev, sizeof(*mbc), GFP_KERNEL);
396 	if (!mbc)
397 		return -ENOMEM;
398 
399 	platform_set_drvdata(pdev, mbc);
400 	mbc->pcf = dev_to_pcf50633(pdev->dev.parent);
401 
402 	/* Set up IRQ handlers */
403 	for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
404 		pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
405 					pcf50633_mbc_irq_handler, mbc);
406 
407 	psy_cfg.supplied_to		= mbc->pcf->pdata->batteries;
408 	psy_cfg.num_supplicants		= mbc->pcf->pdata->num_batteries;
409 	psy_cfg.drv_data		= mbc;
410 
411 	/* Create power supplies */
412 	mbc->adapter = power_supply_register(&pdev->dev,
413 					     &pcf50633_mbc_adapter_desc,
414 					     &psy_cfg);
415 	if (IS_ERR(mbc->adapter)) {
416 		dev_err(mbc->pcf->dev, "failed to register adapter\n");
417 		return PTR_ERR(mbc->adapter);
418 	}
419 
420 	usb_psy_cfg = psy_cfg;
421 	usb_psy_cfg.attr_grp = pcf50633_mbc_sysfs_groups;
422 
423 	mbc->usb = power_supply_register(&pdev->dev, &pcf50633_mbc_usb_desc,
424 					 &usb_psy_cfg);
425 	if (IS_ERR(mbc->usb)) {
426 		dev_err(mbc->pcf->dev, "failed to register usb\n");
427 		power_supply_unregister(mbc->adapter);
428 		return PTR_ERR(mbc->usb);
429 	}
430 
431 	mbc->ac = power_supply_register(&pdev->dev, &pcf50633_mbc_ac_desc,
432 					&psy_cfg);
433 	if (IS_ERR(mbc->ac)) {
434 		dev_err(mbc->pcf->dev, "failed to register ac\n");
435 		power_supply_unregister(mbc->adapter);
436 		power_supply_unregister(mbc->usb);
437 		return PTR_ERR(mbc->ac);
438 	}
439 
440 	mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
441 	if (mbcs1 & PCF50633_MBCS1_USBPRES)
442 		pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
443 	if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
444 		pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
445 
446 	return 0;
447 }
448 
449 static int pcf50633_mbc_remove(struct platform_device *pdev)
450 {
451 	struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
452 	int i;
453 
454 	/* Remove IRQ handlers */
455 	for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
456 		pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
457 
458 	power_supply_unregister(mbc->usb);
459 	power_supply_unregister(mbc->adapter);
460 	power_supply_unregister(mbc->ac);
461 
462 	return 0;
463 }
464 
465 static struct platform_driver pcf50633_mbc_driver = {
466 	.driver = {
467 		.name = "pcf50633-mbc",
468 	},
469 	.probe = pcf50633_mbc_probe,
470 	.remove = pcf50633_mbc_remove,
471 };
472 
473 module_platform_driver(pcf50633_mbc_driver);
474 
475 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
476 MODULE_DESCRIPTION("PCF50633 mbc driver");
477 MODULE_LICENSE("GPL");
478 MODULE_ALIAS("platform:pcf50633-mbc");
479