change charge current
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / charge-regulator.c
1 /* drivers/regulator/charge-regulator.c\r
2  *
3  * Copyright (C) 2010 ROCKCHIP, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 /*******************************************************************/
16 /*        COPYRIGHT (C)  ROCK-CHIPS FUZHOU . ALL RIGHTS RESERVED.                         */
17 /*******************************************************************
18 FILE            :               charge-regulator.c\r
19 DESC            :       charge current change driver\r
20 AUTHOR          :       hxy\r
21 DATE            :       2010-09-02\r
22 NOTES           :
23 $LOG: GPIO.C,V $
24 REVISION 0.01
25 ********************************************************************/
26
27
28 #include <linux/bug.h>
29 #include <linux/err.h>\r
30 #include <linux/platform_device.h>\r
31 #include <linux/kernel.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/charge-regulator.h>\r
34 #include <linux/gpio.h>\r
35
36
37 #if 0
38 #define DBG(x...)       printk(KERN_INFO x)
39 #else
40 #define DBG(x...)
41 #endif
42 \r
43 \r
44 const static int charge_current_map[] = {\r
45         475, 1200,\r
46 };\r
47
48 static int charge_current_is_enabled(struct regulator_dev *dev)\r
49 {\r
50         return 0;\r
51 }
52
53 static int charge_current_enable(struct regulator_dev *dev)\r
54 {\r
55         return 0;\r
56 }
57
58 static int charge_current_disable(struct regulator_dev *dev)\r
59 {\r
60         return 0;\r
61 }
62
63 static int charge_get_current(struct regulator_dev *dev)\r
64 {\r
65         const int *current_map = charge_current_map;\r
66 \r
67        struct charge_platform_data *pdata = rdev_get_drvdata(dev);\r
68 \r
69        gpio_direction_input(pdata->gpio_charge);\r
70            \r
71         return gpio_get_value(pdata->gpio_charge) ? current_map[0] *1000:current_map[1]*1000;\r
72 }
73
74 static int charge_set_current(struct regulator_dev *dev,\r
75                                   int min_uA, int max_uA)\r
76 {\r
77        printk("enter charge_set_current , max_uA = %d\n",max_uA);\r
78         struct charge_platform_data *pdata = rdev_get_drvdata(dev);\r
79        const int *current_map = charge_current_map;\r
80        int max_mA = max_uA / 1000;\r
81         printk("charge_set_current:pdata->gpio_charge=%d\n",pdata->gpio_charge);\r
82         if ( max_mA == current_map[0] )\r
83              gpio_direction_output(pdata->gpio_charge, GPIO_HIGH);\r
84         else \r
85              gpio_direction_output(pdata->gpio_charge, GPIO_LOW);\r
86 \r
87        return 0;\r
88 \r
89 }
90
91 static struct regulator_ops charge_current_ops = {\r
92         .is_enabled = charge_current_is_enabled,\r
93         .enable = charge_current_enable,\r
94         .disable = charge_current_disable,\r
95         .get_current_limit = charge_get_current,\r
96         .set_current_limit = charge_set_current,\r
97 };\r
98
99 static struct regulator_desc chargeregulator= {\r
100                 .name = "charge-regulator",\r
101                 .ops = &charge_current_ops,\r
102                 .type = REGULATOR_CURRENT,\r
103 };
104
105 \r
106
107 static int __devinit charge_regulator_probe(struct platform_device *pdev)\r
108 {\r
109 \r
110         struct charge_platform_data *pdata = pdev->dev.platform_data;\r
111         struct regulator_dev *rdev;\r
112         int ret ;\r
113 printk(KERN_INFO "enter charge regulator\n");\r
114 #if 0\r
115         rdev = regulator_register(&chargeregulator, &pdev->dev,\r
116                                 pdev->dev.platform_data, pdata);\r
117 #else\r
118         rdev = regulator_register(&chargeregulator, &pdev->dev,\r
119                                 pdata->init_data, pdata);\r
120 #endif\r
121         if (IS_ERR(rdev)) {\r
122                 dev_dbg(&pdev->dev, "couldn't register regulator\n");
123                 return PTR_ERR(rdev);\r
124         }\r
125         \r
126         ret = gpio_request(pdata->gpio_charge, "charge_current");\r
127 \r
128         if (ret) {\r
129                         dev_err(&pdev->dev,"failed to request charge gpio\n");\r
130                         goto err_gpio;\r
131                 }\r
132 \r
133         platform_set_drvdata(pdev, rdev);\r
134 printk(KERN_INFO "charge_regulator: driver initialized\n");\r
135         return 0;\r
136         \r
137 err_gpio:\r
138         gpio_free(pdata->gpio_charge);\r
139 \r
140         return ret;\r
141 \r
142 }
143 static int __devexit charge_regulator_remove(struct platform_device *pdev)\r
144 {\r
145         struct charge_platform_data *pdata = pdev->dev.platform_data;\r
146         struct regulator_dev *rdev = platform_get_drvdata(pdev);\r
147 \r
148         regulator_unregister(rdev);\r
149         gpio_free(pdata->gpio_charge);\r
150 \r
151         return 0;\r
152 }\r
153 \r
154 static struct platform_driver charge_regulator_driver = {\r
155         .driver = {\r
156                 .name = "charge-regulator",\r
157         },\r
158         .remove = __devexit_p(charge_regulator_remove),\r
159 };\r
160
161 \r
162 static int __init charge_regulator_module_init(void)\r
163 {
164         return platform_driver_probe(&charge_regulator_driver, charge_regulator_probe);
165 }
166
167 static void __exit charge_regulator_module_exit(void)\r
168 {
169         platform_driver_unregister(&charge_regulator_driver);\r
170 }
171 \r
172 \r
173 module_init(charge_regulator_module_init);\r
174 \r
175 module_exit(charge_regulator_module_exit);\r
176
177 MODULE_LICENSE("GPL");
178 MODULE_AUTHOR("hxy <hxy@rock-chips.com>");\r
179 MODULE_DESCRIPTION("charge current change driver");\r
180 \r