2 * drivers/power/rt-power.c
3 * Driver for Richtek RT PMIC Power driver
5 * Copyright (C) 2014 Richtek Technology Corp.
6 * 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; either version 2
11 * of the License, or (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/version.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/workqueue.h>
22 #include <linux/wakelock.h>
24 #include <linux/power/rt-power.h>
26 struct rt_power_info {
28 struct power_supply ac_psy;
29 struct power_supply usb_psy;
30 struct wake_lock usbdet_wakelock;
31 struct delayed_work usbdet_work;
36 unsigned char ac_online:1;
37 unsigned char usbta_online:1;
38 unsigned char usb_online:1;
39 unsigned char suspend:1;
43 #define RT_USBCNT_MAX 60
45 static char *rtpower_supply_list[] = {
49 static enum power_supply_property rtpower_props[] = {
50 POWER_SUPPLY_PROP_ONLINE,
53 static int rtpower_set_charger(struct rt_power_info *pi)
55 struct power_supply *chg_psy;
56 union power_supply_propval pval;
57 int rc = 0, is_chg_on = 0;
59 chg_psy = power_supply_get_by_name("rt-charger");
61 rc = chg_psy->get_property(chg_psy, POWER_SUPPLY_PROP_ONLINE,
64 dev_err(pi->dev, "get chg online prop fail\n");
66 is_chg_on = pval.intval;
68 pval.intval = pi->acchg_icc;
69 rc = chg_psy->set_property(chg_psy,
70 POWER_SUPPLY_PROP_CURRENT_AVG,
73 dev_err(pi->dev, "set acchg aicr fail\n");
75 rc = chg_psy->set_property(chg_psy,
76 POWER_SUPPLY_PROP_CURRENT_NOW,
79 dev_err(pi->dev, "set acchg icc fail\n");
80 pval.intval = POWER_SUPPLY_TYPE_MAINS;
81 rc = chg_psy->set_property(chg_psy,
82 POWER_SUPPLY_PROP_CHARGE_NOW,
85 dev_err(pi->dev, "set charge cable fail\n");
87 pval.intval = pi->chg_volt;
88 rc = chg_psy->set_property(chg_psy,
89 POWER_SUPPLY_PROP_VOLTAGE_NOW,
93 "set chg voltage fail\n");
95 rc = chg_psy->set_property(chg_psy,
96 POWER_SUPPLY_PROP_ONLINE,
100 "set charger online fail\n");
103 rc = chg_psy->set_property(chg_psy,
104 POWER_SUPPLY_PROP_PRESENT,
107 dev_err(pi->dev, "set charger present fail\n");
108 } else if (pi->usbta_online) {
109 pval.intval = pi->usbtachg_icc;
110 rc = chg_psy->set_property(chg_psy,
111 POWER_SUPPLY_PROP_CURRENT_AVG,
114 dev_err(pi->dev, "set usbtachg aicr fail\n");
116 rc = chg_psy->set_property(chg_psy,
117 POWER_SUPPLY_PROP_CURRENT_NOW,
120 dev_err(pi->dev, "set usbtachg icc fail\n");
121 pval.intval = POWER_SUPPLY_TYPE_USB_DCP;
122 rc = chg_psy->set_property(chg_psy,
123 POWER_SUPPLY_PROP_CHARGE_NOW,
126 dev_err(pi->dev, "set charge cable fail\n");
128 pval.intval = pi->chg_volt;
129 rc = chg_psy->set_property(chg_psy,
130 POWER_SUPPLY_PROP_VOLTAGE_NOW,
134 "set chg voltage fail\n");
136 rc = chg_psy->set_property(chg_psy,
137 POWER_SUPPLY_PROP_ONLINE,
141 "set charger online fail\n");
144 rc = chg_psy->set_property(chg_psy,
145 POWER_SUPPLY_PROP_PRESENT,
148 dev_err(pi->dev, "set charger present fail\n");
149 } else if (pi->usb_online) {
150 pval.intval = pi->usbchg_icc;
151 rc = chg_psy->set_property(chg_psy,
152 POWER_SUPPLY_PROP_CURRENT_AVG,
155 dev_err(pi->dev, "set usbchg aicr fail\n");
157 rc = chg_psy->set_property(chg_psy,
158 POWER_SUPPLY_PROP_CURRENT_NOW,
161 dev_err(pi->dev, "set usbchg icc fail\n");
162 pval.intval = POWER_SUPPLY_TYPE_USB;
163 rc = chg_psy->set_property(chg_psy,
164 POWER_SUPPLY_PROP_CHARGE_NOW,
167 dev_err(pi->dev, "set charge cable fail\n");
169 pval.intval = pi->chg_volt;
170 rc = chg_psy->set_property(chg_psy,
171 POWER_SUPPLY_PROP_VOLTAGE_NOW,
175 "set chg voltage fail\n");
177 rc = chg_psy->set_property(chg_psy,
178 POWER_SUPPLY_PROP_ONLINE,
182 "set charger online fail\n");
185 rc = chg_psy->set_property(chg_psy,
186 POWER_SUPPLY_PROP_PRESENT,
189 dev_err(pi->dev, "set charger present fail\n");
192 rc = chg_psy->set_property(chg_psy,
193 POWER_SUPPLY_PROP_ONLINE,
196 dev_err(pi->dev, "set charger online fail\n");
198 rc = chg_psy->set_property(chg_psy,
199 POWER_SUPPLY_PROP_CHARGE_NOW,
202 dev_err(pi->dev, "set charge cable fail\n");
203 pval.intval = pi->chg_volt;
204 rc = chg_psy->set_property(chg_psy,
205 POWER_SUPPLY_PROP_VOLTAGE_NOW,
208 dev_err(pi->dev, "set chg voltage fail\n");
210 rc = chg_psy->set_property(chg_psy,
211 POWER_SUPPLY_PROP_CURRENT_AVG,
214 dev_err(pi->dev, "set chg aicr fail\n");
216 rc = chg_psy->set_property(chg_psy,
217 POWER_SUPPLY_PROP_CURRENT_NOW,
220 dev_err(pi->dev, "set chg icc fail\n");
222 power_supply_changed(chg_psy);
225 dev_err(pi->dev, "cannot get rt-charger psy\n");
230 static int rtpower_get_property(struct power_supply *psy,
231 enum power_supply_property psp,
232 union power_supply_propval *val)
234 struct rt_power_info *pi = dev_get_drvdata(psy->dev->parent);
238 case POWER_SUPPLY_PROP_ONLINE:
239 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
240 val->intval = (pi->ac_online
241 || pi->usbta_online) ? 1 : 0;
242 else if (psy->type == POWER_SUPPLY_TYPE_USB)
243 val->intval = pi->usb_online;
254 static int rtpower_set_property(struct power_supply *psy,
255 enum power_supply_property psp,
256 const union power_supply_propval *val)
258 struct rt_power_info *pi = dev_get_drvdata(psy->dev->parent);
262 case POWER_SUPPLY_PROP_ONLINE:
263 if (psy->type == POWER_SUPPLY_TYPE_MAINS) {
264 if (pi->ac_online != val->intval) {
265 pi->ac_online = val->intval;
266 rc = rtpower_set_charger(pi);
268 } else if (psy->type == POWER_SUPPLY_TYPE_USB) {
269 if (pi->usb_online != val->intval) {
270 pi->usb_online = val->intval;
273 wake_lock(&pi->usbdet_wakelock);
274 schedule_delayed_work(&pi->usbdet_work,
277 pi->usbcnt = RT_USBCNT_MAX;
278 schedule_delayed_work(&pi->usbdet_work,
280 if (pi->usbta_online) {
281 pi->usbta_online = 0;
286 rc = rtpower_set_charger(pi);
299 extern int dwc_otg_check_dpdm(bool wait);
300 static void usbdet_work_func(struct work_struct *work)
302 struct rt_power_info *pi = container_of(work, struct rt_power_info, \
304 int usb_det = dwc_otg_check_dpdm(0);
308 dev_info(pi->dev, "usb ta checked\n");
309 if (pi->usb_online) {
310 pi->usbta_online = 1;
311 rtpower_set_charger(pi);
312 power_supply_changed(&pi->ac_psy);
314 pi->usbcnt = RT_USBCNT_MAX;
318 dev_info(pi->dev, "normal usb\n");
323 if (pi->usbcnt < RT_USBCNT_MAX) {
325 schedule_delayed_work(&pi->usbdet_work, 1*HZ);
327 wake_unlock(&pi->usbdet_wakelock);
331 static int rt_power_probe(struct platform_device *pdev)
333 struct rt_power_info *pi;
334 struct rt_power_data *rt_power_pdata = pdev->dev.platform_data;
337 pi = devm_kzalloc(&pdev->dev, sizeof(*pi), GFP_KERNEL);
340 pi->dev = &pdev->dev;
341 wake_lock_init(&pi->usbdet_wakelock, WAKE_LOCK_SUSPEND, "rt-usb-det");
342 INIT_DELAYED_WORK(&pi->usbdet_work, usbdet_work_func);
343 pi->chg_volt = rt_power_pdata->chg_volt;
344 pi->acchg_icc = rt_power_pdata->acchg_icc;
345 pi->usbtachg_icc = rt_power_pdata->usbtachg_icc;
346 pi->usbchg_icc = rt_power_pdata->usbchg_icc;
347 platform_set_drvdata(pdev, pi);
349 /* ac power supply register*/
350 pi->ac_psy.name = RT_AC_NAME;
351 pi->ac_psy.type = POWER_SUPPLY_TYPE_MAINS;
352 pi->ac_psy.supplied_to = rtpower_supply_list;
353 pi->ac_psy.properties = rtpower_props;
354 pi->ac_psy.num_properties = ARRAY_SIZE(rtpower_props);
355 pi->ac_psy.get_property = rtpower_get_property;
356 pi->ac_psy.set_property = rtpower_set_property;
357 ret = power_supply_register(&pdev->dev, &pi->ac_psy);
359 dev_err(&pdev->dev, " create ac power supply fail\n");
362 /*usb power supply register*/
363 pi->usb_psy.name = RT_USB_NAME;
364 pi->usb_psy.type = POWER_SUPPLY_TYPE_USB;
365 pi->usb_psy.supplied_to = rtpower_supply_list;
366 pi->usb_psy.properties = rtpower_props;
367 pi->usb_psy.num_properties = ARRAY_SIZE(rtpower_props);
368 pi->usb_psy.get_property = rtpower_get_property;
369 pi->usb_psy.set_property = rtpower_set_property;
370 ret = power_supply_register(&pdev->dev, &pi->usb_psy);
372 dev_err(&pdev->dev, " create usb power supply fail\n");
378 power_supply_unregister(&pi->ac_psy);
380 wake_lock_destroy(&pi->usbdet_wakelock);
384 static int rt_power_remove(struct platform_device *pdev)
386 struct rt_power_info *pi = platform_get_drvdata(pdev);
388 power_supply_unregister(&pi->usb_psy);
389 power_supply_unregister(&pi->ac_psy);
390 wake_lock_destroy(&pi->usbdet_wakelock);
394 static int rt_power_suspend(struct platform_device *pdev, pm_message_t state)
396 struct rt_power_info *pi = platform_get_drvdata(pdev);
402 static int rt_power_resume(struct platform_device *pdev)
404 struct rt_power_info *pi = platform_get_drvdata(pdev);
410 static const struct of_device_id rt_match_table[] = {
411 { .compatible = "rt,rt-power",},
415 static struct platform_driver rt_power_driver = {
418 .owner = THIS_MODULE,
419 .of_match_table = rt_match_table,
421 .probe = rt_power_probe,
422 .remove = rt_power_remove,
423 .suspend = rt_power_suspend,
424 .resume = rt_power_resume,
426 static int rt_power_init(void)
428 return platform_driver_register(&rt_power_driver);
430 subsys_initcall(rt_power_init);
432 static void rt_power_exit(void)
434 platform_driver_unregister(&rt_power_driver);
436 module_exit(rt_power_exit);
438 MODULE_LICENSE("GPL");
439 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com>");
440 MODULE_DESCRIPTION("RT Power driver");
441 MODULE_ALIAS("platform:rt-power");
442 MODULE_VERSION("1.0.0_G");