2 * drivers/power/rt-battery.c
3 * Driver for Richtek RT Test Battery 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>
22 #include <linux/power/rt-battery.h>
24 struct rt_battery_info {
26 struct power_supply psy;
28 unsigned char batt_present:1;
29 unsigned char suspend:1;
32 static enum power_supply_property rt_battery_props[] = {
33 POWER_SUPPLY_PROP_STATUS,
34 POWER_SUPPLY_PROP_HEALTH,
35 POWER_SUPPLY_PROP_PRESENT,
36 POWER_SUPPLY_PROP_ONLINE,
37 POWER_SUPPLY_PROP_CAPACITY,
38 POWER_SUPPLY_PROP_VOLTAGE_NOW,
41 static int rt_battery_set_property(struct power_supply *psy,
42 enum power_supply_property psp,
43 const union power_supply_propval *val)
45 struct rt_battery_info *rbi = dev_get_drvdata(psy->dev->parent);
49 case POWER_SUPPLY_PROP_STATUS:
50 rbi->chg_status = val->intval;
52 case POWER_SUPPLY_PROP_PRESENT:
53 rbi->batt_present = val->intval;
55 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
56 case POWER_SUPPLY_PROP_HEALTH:
57 case POWER_SUPPLY_PROP_ONLINE:
58 case POWER_SUPPLY_PROP_CAPACITY:
66 static int rt_battery_get_property(struct power_supply *psy,
67 enum power_supply_property psp,
68 union power_supply_propval *val)
70 struct rt_battery_info *rbi = dev_get_drvdata(psy->dev->parent);
74 case POWER_SUPPLY_PROP_STATUS:
75 val->intval = rbi->chg_status;
77 case POWER_SUPPLY_PROP_HEALTH:
78 val->intval = POWER_SUPPLY_HEALTH_GOOD;
80 case POWER_SUPPLY_PROP_PRESENT:
81 val->intval = rbi->batt_present;
83 case POWER_SUPPLY_PROP_ONLINE:
86 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
87 val->intval = 4000 * 1000;
89 case POWER_SUPPLY_PROP_CAPACITY:
90 if (rbi->chg_status == POWER_SUPPLY_STATUS_FULL)
102 static int rt_battery_probe(struct platform_device *pdev)
104 struct rt_battery_info *rbi;
107 rbi = devm_kzalloc(&pdev->dev, sizeof(*rbi), GFP_KERNEL);
110 rbi->dev = &pdev->dev;
111 rbi->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
112 rbi->batt_present = 1;
113 platform_set_drvdata(pdev, rbi);
115 rbi->psy.name = RT_BATT_NAME;
116 rbi->psy.type = POWER_SUPPLY_TYPE_BATTERY;
117 rbi->psy.set_property = rt_battery_set_property;
118 rbi->psy.get_property = rt_battery_get_property;
119 rbi->psy.properties = rt_battery_props;
120 rbi->psy.num_properties = ARRAY_SIZE(rt_battery_props);
121 ret = power_supply_register(&pdev->dev, &rbi->psy);
123 dev_err(&pdev->dev, "battery supply registered fail\n");
126 dev_info(&pdev->dev, "driver successfully loaded\n");
132 static int rt_battery_remove(struct platform_device *pdev)
134 struct rt_battery_info *rbi = platform_get_drvdata(pdev);
136 power_supply_unregister(&rbi->psy);
140 static int rt_battery_suspend(struct platform_device *pdev, pm_message_t state)
142 struct rt_battery_info *rbi = platform_get_drvdata(pdev);
148 static int rt_battery_resume(struct platform_device *pdev)
150 struct rt_battery_info *rbi = platform_get_drvdata(pdev);
156 static const struct of_device_id rt_match_table[] = {
157 {.compatible = "rt,rt-battery",},
161 static struct platform_driver rt_battery_driver = {
163 .name = "rt-battery",
164 .owner = THIS_MODULE,
165 .of_match_table = rt_match_table,
167 .probe = rt_battery_probe,
168 .remove = rt_battery_remove,
169 .suspend = rt_battery_suspend,
170 .resume = rt_battery_resume,
173 static int __init rt_battery_init(void)
175 return platform_driver_register(&rt_battery_driver);
177 subsys_initcall(rt_battery_init);
179 static void __exit rt_battery_exit(void)
181 platform_driver_unregister(&rt_battery_driver);
183 module_exit(rt_battery_exit);
185 MODULE_LICENSE("GPL");
186 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com>");
187 MODULE_DESCRIPTION("RT Test Battery driver");
188 MODULE_ALIAS("platform:rt-battery");
189 MODULE_VERSION("1.0.0_G");