1 /* drivers/power/rt5025-power.c
2 * I2C Driver for Richtek RT5025 PMIC
3 * Multi function device - multi functional baseband PMIC Power part
6 * Author: CY Huang <cy_huang@richtek.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/power_supply.h>
18 #include <linux/platform_device.h>
19 #include <linux/err.h>
20 #include <linux/version.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <linux/mfd/rt5025.h>
24 #include <linux/power/rt5025-power.h>
25 #include <linux/delay.h>
28 static enum power_supply_property rt5025_adap_props[] = {
29 POWER_SUPPLY_PROP_ONLINE,
32 static char *rt5025_supply_list[] = {
38 static int rt5025_set_charging_current_switch (struct i2c_client *i2c, int onoff)
42 ret = rt5025_set_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCEN_MASK);
44 ret = rt5025_clr_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCEN_MASK);
48 static int rt5025_set_charging_buck(struct i2c_client *i2c, int onoff)
52 ret = rt5025_set_bits(i2c, RT5025_REG_CHGCTL2, RT5025_CHGBUCKEN_MASK);
54 ret = rt5025_clr_bits(i2c, RT5025_REG_CHGCTL2, RT5025_CHGBUCKEN_MASK);
59 static int rt5025_set_charging_current(struct i2c_client *i2c, int cur_value)
68 else if (cur_value >= 500 && cur_value <= 2000)
70 data = (cur_value-500)/100;
78 else if (cur_value > 500 && cur_value <= 1000)
80 else if (cur_value > 100 && cur_value >= 500)
83 rt5025_assign_bits(i2c, RT5025_REG_CHGCTL4, RT5025_CHGAICR_MASK, data);
87 static int rt5025_chgstat_changed(struct rt5025_power_info *info, unsigned new_val)
94 rt5025_set_charging_current_switch(info->i2c, 1);
95 rt5025_set_charging_buck(info->i2c, 1);
97 info->chg_stat = 0x00;
99 if (info->chip->battery_info)
101 if (info->chg_term == 0)
102 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_DISCHARGING);
103 else if (info->chg_term > 0)
105 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_FULL);
111 if (info->event_callback)
112 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_CHARGING);
116 //rt5025_set_charging_current_switch(info->i2c, 1);
117 info->chg_stat = 0x01;
119 if (info->chip->battery_info)
121 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_CHARGING);
125 if (info->event_callback)
126 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_CHARGING);
131 rt5025_set_charging_current_switch(info->i2c, 0);
133 info->chg_stat = 0x02;
135 if (info->chip->battery_info)
137 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_FULL);
141 if (info->event_callback)
142 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_FULL);
147 rt5025_set_charging_buck(info->i2c, 0);
148 rt5025_set_charging_current_switch(info->i2c, 0);
150 info->chg_stat = 0x03;
152 if (info->chip->battery_info)
154 if (info->chg_term == 0)
155 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_CHARGING);
156 else if (info->chg_term > 1)
158 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_FULL);
163 if (info->event_callback)
164 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_DISCHARGING);
174 int rt5025_power_passirq_to_gauge(struct rt5025_power_info *info)
176 if (info->event_callback)
177 info->event_callback->rt5025_gauge_irq_handler();
180 EXPORT_SYMBOL(rt5025_power_passirq_to_gauge);
183 int rt5025_power_charge_detect(struct rt5025_power_info *info)
186 unsigned char chgstatval = 0;
187 unsigned old_usbval, old_acval, old_chgval, new_usbval, new_acval, new_chgval;
189 old_acval = info->ac_online;
190 old_usbval = info->usb_online;
191 old_chgval = info->chg_stat;
193 ret = rt5025_reg_read(info->i2c, RT5025_REG_CHGSTAT);
196 dev_err(info->dev, "read chg stat reg fail\n");
201 new_acval = (chgstatval&RT5025_CHG_ACONLINE)>>RT5025_CHG_ACSHIFT;
202 if (old_acval != new_acval)
204 info->ac_online = new_acval;
205 power_supply_changed(&info->ac);
207 new_usbval = (chgstatval&RT5025_CHG_USBONLINE)>>RT5025_CHG_USBSHIFT;
208 if (old_usbval != new_usbval)
210 info->usb_online = new_usbval;
211 power_supply_changed(&info->usb);
214 if (old_acval != new_acval || old_usbval != new_usbval)
215 schedule_delayed_work(&info->usb_detect_work, 0); //no delay
217 new_chgval = (chgstatval&RT5025_CHGSTAT_MASK)>>RT5025_CHGSTAT_SHIFT;
219 if (new_acval || new_usbval)
221 if (old_chgval != new_chgval)
223 ret = rt5025_chgstat_changed(info, new_chgval);
229 rt5025_set_charging_buck(info->i2c, 0);
230 rt5025_set_charging_current_switch(info->i2c, 0);
232 info->chg_stat = RT5025_CHGSTAT_UNKNOWN;
233 if (info->chip->jeita_info)
234 rt5025_notify_charging_cable(info->chip->jeita_info, JEITA_NO_CHARGE);
236 if (info->chip->battery_info)
237 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_NOT_CHARGING);
239 if (info->event_callback)
240 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_NOT_CHARGING);
246 EXPORT_SYMBOL(rt5025_power_charge_detect);
248 static int rt5025_adap_get_props(struct power_supply *psy,
249 enum power_supply_property psp,
250 union power_supply_propval *val)
252 struct rt5025_power_info *info = dev_get_drvdata(psy->dev->parent);
255 case POWER_SUPPLY_PROP_ONLINE:
256 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
257 val->intval = info->ac_online;
258 else if (psy->type == POWER_SUPPLY_TYPE_USB)
259 val->intval = info->usb_online;
270 extern int dwc_vbus_status(void);
273 static void usb_detect_work_func(struct work_struct *work)
275 struct delayed_work *delayed_work = (struct delayed_work *)container_of(work, struct delayed_work, work);
276 struct rt5025_power_info *pi = (struct rt5025_power_info *)container_of(delayed_work, struct rt5025_power_info, usb_detect_work);
278 pr_info("rt5025: %s ++", __func__);
280 mutex_lock(&pi->var_lock);
283 rt5025_set_charging_current(pi->i2c, 1000);
284 rt5025_notify_charging_cable(pi->chip->jeita_info, JEITA_AC_ADAPTER);
287 else if (pi->usb_online)
289 pr_info("%s: usb_cnt %d\n", __func__, pi->usb_cnt);
290 switch(dwc_vbus_status())
292 case 2: // USB Wall charger
293 rt5025_set_charging_current(pi->i2c, 1000);
294 rt5025_notify_charging_cable(pi->chip->jeita_info, JEITA_USB_TA);
295 pr_info("rt5025: detect usb wall charger\n");
299 rt5025_set_charging_current(pi->i2c, 500);
300 rt5025_notify_charging_cable(pi->chip->jeita_info, JEITA_NORMAL_USB);
301 pr_info("rt5025: detect normal usb\n");
304 if (pi->usb_cnt++ < 60)
305 schedule_delayed_work(&pi->usb_detect_work, 1*HZ);
309 //default to prevent over current charging
310 rt5025_set_charging_current(pi->i2c, 500);
311 rt5025_notify_charging_cable(pi->chip->jeita_info, JEITA_NO_CHARGE);
315 mutex_unlock(&pi->var_lock);
317 pr_info("rt5025: %s --", __func__);
320 static int __devinit rt5025_init_charger(struct rt5025_power_info *info, struct rt5025_power_data* pd)
324 //init charger buckck & charger current en to disable stat
325 info->chg_stat = RT5025_CHGSTAT_UNKNOWN;
327 if (info->event_callback)
328 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_DISCHARGING);
330 rt5025_set_bits(info->i2c, RT5025_REG_CHGCTL4, RT5025_CHGRST_MASK);
332 //init register setting
333 rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL2, pd->CHGControl2.val);
334 rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL3, pd->CHGControl3.val);
335 rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL4, pd->CHGControl4.val);
336 rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL5, pd->CHGControl5.val);
337 rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL6, pd->CHGControl6.val);
338 rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL7, pd->CHGControl7.val);
340 rt5025_power_charge_detect(info);
345 static int __devinit rt5025_power_probe(struct platform_device *pdev)
347 struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
348 struct rt5025_platform_data *pdata = chip->dev->platform_data;
349 struct rt5025_power_info *pi;
352 pi = kzalloc(sizeof(*pi), GFP_KERNEL);
357 pi->dev = &pdev->dev;
359 mutex_init(&pi->var_lock);
360 INIT_DELAYED_WORK(&pi->usb_detect_work, usb_detect_work_func);
363 ret = rt5025_gauge_init(pi);
368 platform_set_drvdata(pdev, pi);
370 pi->ac.name = "rt5025-dc";
371 pi->ac.type = POWER_SUPPLY_TYPE_MAINS;
372 pi->ac.supplied_to = rt5025_supply_list;
373 pi->ac.properties = rt5025_adap_props;
374 pi->ac.num_properties = ARRAY_SIZE(rt5025_adap_props);
375 pi->ac.get_property = rt5025_adap_get_props;
376 ret = power_supply_register(&pdev->dev, &pi->ac);
380 pi->usb.name = "rt5025-usb";
381 pi->usb.type = POWER_SUPPLY_TYPE_USB;
382 pi->ac.supplied_to = rt5025_supply_list;
383 pi->usb.properties = rt5025_adap_props;
384 pi->usb.num_properties = ARRAY_SIZE(rt5025_adap_props);
385 pi->usb.get_property = rt5025_adap_get_props;
386 ret = power_supply_register(&pdev->dev, &pi->usb);
390 rt5025_init_charger(pi, pdata->power_data);
391 chip->power_info = pi;
395 power_supply_unregister(&pi->ac);
402 static int rt5025_power_suspend(struct platform_device *pdev, pm_message_t state)
405 struct rt5025_power_info *pi = platform_get_drvdata(pdev);
407 if (pi->event_callback)
408 pi->event_callback->rt5025_gauge_suspend();
413 static int rt5025_power_resume(struct platform_device *pdev)
416 struct rt5025_power_info *pi = platform_get_drvdata(pdev);
418 if (pi->event_callback)
419 pi->event_callback->rt5025_gauge_resume();
424 static int __devexit rt5025_power_remove(struct platform_device *pdev)
426 struct rt5025_power_info *pi = platform_get_drvdata(pdev);
427 struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
430 if (pi->event_callback)
431 pi->event_callback->rt5025_gauge_remove();
433 power_supply_unregister(&pi->usb);
434 power_supply_unregister(&pi->ac);
435 chip->power_info = NULL;
441 static struct platform_driver rt5025_power_driver =
444 .name = RT5025_DEVICE_NAME "-power",
445 .owner = THIS_MODULE,
447 .probe = rt5025_power_probe,
448 .remove = __devexit_p(rt5025_power_remove),
449 .suspend = rt5025_power_suspend,
450 .resume = rt5025_power_resume,
453 static int __init rt5025_power_init(void)
455 return platform_driver_register(&rt5025_power_driver);
457 device_initcall_sync(rt5025_power_init);
459 static void __exit rt5025_power_exit(void)
461 platform_driver_unregister(&rt5025_power_driver);
463 module_exit(rt5025_power_exit);
466 MODULE_LICENSE("GPL v2");
467 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
468 MODULE_DESCRIPTION("Power/Gauge driver for RT5025");
469 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-power");