UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[firefly-linux-kernel-4.4.55.git] / drivers / power / rt-battery.c
1 /*
2  *  drivers/power/rt-battery.c
3  *  Driver for Richtek RT Test Battery driver
4  *
5  *  Copyright (C) 2014 Richtek Technology Corp.
6  *  cy_huang <cy_huang@richtek.com>
7  *
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.
12  */
13
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
22 #include <linux/power/rt-battery.h>
23
24 struct rt_battery_info {
25         struct device *dev;
26         struct power_supply psy;
27         int chg_status;
28         unsigned char batt_present:1;
29         unsigned char suspend:1;
30 };
31
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,
39 };
40
41 static int rt_battery_set_property(struct power_supply *psy,
42                                    enum power_supply_property psp,
43                                    const union power_supply_propval *val)
44 {
45         struct rt_battery_info *rbi = dev_get_drvdata(psy->dev->parent);
46         int ret = 0;
47
48         switch (psp) {
49         case POWER_SUPPLY_PROP_STATUS:
50                 rbi->chg_status = val->intval;
51                 break;
52         case POWER_SUPPLY_PROP_PRESENT:
53                 rbi->batt_present = val->intval;
54                 break;
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:
59         default:
60                 ret = -EINVAL;
61                 break;
62         }
63         return ret;
64 }
65
66 static int rt_battery_get_property(struct power_supply *psy,
67                                    enum power_supply_property psp,
68                                    union power_supply_propval *val)
69 {
70         struct rt_battery_info *rbi = dev_get_drvdata(psy->dev->parent);
71         int ret = 0;
72
73         switch (psp) {
74         case POWER_SUPPLY_PROP_STATUS:
75                 val->intval = rbi->chg_status;
76                 break;
77         case POWER_SUPPLY_PROP_HEALTH:
78                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
79                 break;
80         case POWER_SUPPLY_PROP_PRESENT:
81                 val->intval = rbi->batt_present;
82                 break;
83         case POWER_SUPPLY_PROP_ONLINE:
84                 val->intval = 1;
85                 break;
86         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
87                 val->intval = 4000 * 1000;
88                 break;
89         case POWER_SUPPLY_PROP_CAPACITY:
90                 if (rbi->chg_status == POWER_SUPPLY_STATUS_FULL)
91                         val->intval = 100;
92                 else
93                         val->intval = 50;
94                 break;
95         default:
96                 ret = -EINVAL;
97                 break;
98         }
99         return ret;
100 }
101
102 static int rt_battery_probe(struct platform_device *pdev)
103 {
104         struct rt_battery_info *rbi;
105         int ret;
106
107         rbi = devm_kzalloc(&pdev->dev, sizeof(*rbi), GFP_KERNEL);
108         if (!rbi)
109                 return -ENOMEM;
110         rbi->dev = &pdev->dev;
111         rbi->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
112         rbi->batt_present = 1;
113         platform_set_drvdata(pdev, rbi);
114
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);
122         if (ret < 0) {
123                 dev_err(&pdev->dev, "battery supply registered fail\n");
124                 goto out_dev;
125         }
126         dev_info(&pdev->dev, "driver successfully loaded\n");
127         return 0;
128 out_dev:
129         return ret;
130 }
131
132 static int rt_battery_remove(struct platform_device *pdev)
133 {
134         struct rt_battery_info *rbi = platform_get_drvdata(pdev);
135
136         power_supply_unregister(&rbi->psy);
137         return 0;
138 }
139
140 static int rt_battery_suspend(struct platform_device *pdev, pm_message_t state)
141 {
142         struct rt_battery_info *rbi = platform_get_drvdata(pdev);
143
144         rbi->suspend = 1;
145         return 0;
146 }
147
148 static int rt_battery_resume(struct platform_device *pdev)
149 {
150         struct rt_battery_info *rbi = platform_get_drvdata(pdev);
151
152         rbi->suspend = 0;
153         return 0;
154 }
155
156 static const struct of_device_id rt_match_table[] = {
157         {.compatible = "rt,rt-battery",},
158         {},
159 };
160
161 static struct platform_driver rt_battery_driver = {
162         .driver = {
163                    .name = "rt-battery",
164                    .owner = THIS_MODULE,
165                    .of_match_table = rt_match_table,
166                    },
167         .probe = rt_battery_probe,
168         .remove = rt_battery_remove,
169         .suspend = rt_battery_suspend,
170         .resume = rt_battery_resume,
171 };
172
173 static int __init rt_battery_init(void)
174 {
175         return platform_driver_register(&rt_battery_driver);
176 }
177 subsys_initcall(rt_battery_init);
178
179 static void __exit rt_battery_exit(void)
180 {
181         platform_driver_unregister(&rt_battery_driver);
182 }
183 module_exit(rt_battery_exit);
184
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");