CHROMIUM: usb: dwc3: rockchip: Check for plug/unplug events on resume
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / rt5025-core.c
1 /*
2  *  drivers/mfd/rt5025-core.c
3  *  Driver for Richtek RT5025 Core PMIC
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/version.h>
17 #include <linux/i2c.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/rt5025.h>
20
21 #ifdef CONFIG_REGULATOR_RT5025
22 #ifdef CONFIG_OF
23 #define RT5025_BUCKVR_DEVS(_id, _idx)                                   \
24 {                                                                       \
25         .name           = RT5025_DEV_NAME "-regulator",         \
26         .num_resources  = 0,                                            \
27         .of_compatible  = "rt," RT5025_DEV_NAME "-dcdc" #_idx ,         \
28         .id             = RT5025_ID_##_id,                              \
29 }
30
31 #define RT5025_LDOVR_DEVS(_id, _idx)                                    \
32 {                                                                       \
33         .name           = RT5025_DEV_NAME "-regulator",         \
34         .num_resources  = 0,                                            \
35         .of_compatible  = "rt," RT5025_DEV_NAME "-ldo" #_idx ,  \
36         .id             = RT5025_ID_##_id,                              \
37 }
38 #else
39 #define RT5025_BUCKVR_DEVS(_id, _idx)                                   \
40 {                                                                       \
41         .name           = RT5025_DEV_NAME "-regulator",         \
42         .num_resources  = 0,                                            \
43         .id             = RT5025_ID_##_id,                              \
44 }
45
46 #define RT5025_LDOVR_DEVS(_id, _idx)                                    \
47 {                                                                       \
48         .name           = RT5025_DEV_NAME "-regulator",         \
49         .num_resources  = 0,                                            \
50         .id             = RT5025_ID_##_id,                              \
51 }
52 #endif /* #ifdef CONFIG_OF */
53 static struct mfd_cell regulator_devs[] = {
54         RT5025_BUCKVR_DEVS(DCDC1, 1),
55         RT5025_BUCKVR_DEVS(DCDC2, 2),
56         RT5025_BUCKVR_DEVS(DCDC3, 3),
57         RT5025_BUCKVR_DEVS(DCDC4, 4),
58         RT5025_LDOVR_DEVS(LDO1,   1),
59         RT5025_LDOVR_DEVS(LDO2,   2),
60         RT5025_LDOVR_DEVS(LDO3,   3),
61         RT5025_LDOVR_DEVS(LDO4,   4),
62         RT5025_LDOVR_DEVS(LDO5,   5),
63         RT5025_LDOVR_DEVS(LDO6,   6),
64 };
65 #endif /* #ifdef CONFIG_REGULATOR_RT5025 */
66
67 #ifdef CONFIG_CHARGER_RT5025
68 static struct mfd_cell chg_devs[] = {
69 {
70         .name = RT5025_DEV_NAME "-charger",
71         .id = -1,
72         .num_resources = 0,
73 #ifdef CONFIG_OF
74         .of_compatible  = "rt," RT5025_DEV_NAME "-charger" ,
75 #endif /*#ifdef CONFIG_OF */
76 },
77 };
78 #endif /* #ifdef CONFIG_CHARGER_RT5025 */
79
80 #ifdef CONFIG_BATTERY_RT5025
81 static struct mfd_cell fg_devs[] = {
82 {
83         .name = RT5025_DEV_NAME "-battery",
84         .id = -1,
85         .num_resources = 0,
86 #ifdef CONFIG_OF
87         .of_compatible  = "rt," RT5025_DEV_NAME "-battery" ,
88 #endif /*#ifdef CONFIG_OF */
89 },
90 };
91 #endif /* #ifdef CONFIG_BATTERY_RT5025 */
92
93 #ifdef CONFIG_GPIO_RT5025
94 static struct mfd_cell gpio_devs[] = {
95 {
96         .name = RT5025_DEV_NAME "-gpio",
97         .id = -1,
98         .num_resources = 0,
99 #ifdef CONFIG_OF
100         .of_compatible  = "rt," RT5025_DEV_NAME "-gpio" ,
101 #endif /*#ifdef CONFIG_OF */
102 },
103 };
104 #endif /* #ifdef CONFIG_GPIO_RT5025 */
105
106 #ifdef CONFIG_MISC_RT5025
107 static struct mfd_cell misc_devs[] = {
108 {
109         .name = RT5025_DEV_NAME "-misc",
110         .id = -1,
111         .num_resources = 0,
112 #ifdef CONFIG_OF
113         .of_compatible  = "rt," RT5025_DEV_NAME "-misc" ,
114 #endif /*#ifdef CONFIG_OF */
115 },
116 };
117 #endif /* #ifdef CONFIG_MISC_RT5025 */
118
119 #ifdef CONFIG_IRQ_RT5025
120 static struct mfd_cell irq_devs[] = {
121 {
122         .name = RT5025_DEV_NAME "-irq",
123         .id = -1,
124         .num_resources = 0,
125 #ifdef CONFIG_OF
126         .of_compatible  = "rt," RT5025_DEV_NAME "-irq" ,
127 #endif /*#ifdef CONFIG_OF */
128 },
129 };
130 #endif /* #ifdef CONFIG_IRQ_RT5025 */
131
132 #ifdef CONFIG_DEBUG_RT5025
133 static struct mfd_cell debug_devs[] = {
134 {
135         .name = RT5025_DEV_NAME "-debug",
136         .id = -1,
137         .num_resources = 0,
138 #ifdef CONFIG_OF
139         .of_compatible  = "rt," RT5025_DEV_NAME "-debug" ,
140 #endif /*#ifdef CONFIG_OF */
141 },
142 };
143 #endif /* #ifdef CONFIG_DEBUG_RT5025 */
144
145 int rt5025_core_init(struct rt5025_chip *chip,
146         struct rt5025_platform_data *pdata)
147 {
148         int ret = 0;
149         bool use_dt = chip->dev->of_node;
150
151         RTINFO("Start to initialize all device\n");
152
153         #ifdef CONFIG_REGULATOR_RT5025
154         if (use_dt || (pdata && pdata->regulator[0])) {
155                 RTINFO("mfd add regulators dev\n");
156                 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
157                 ret = mfd_add_devices(chip->dev, 0, &regulator_devs[0],
158                                       ARRAY_SIZE(regulator_devs),
159                                       NULL, 0, NULL);
160                 #else
161                 ret = mfd_add_devices(chip->dev, 0, &regulator_devs[0],
162                                       ARRAY_SIZE(regulator_devs),
163                                       NULL, 0);
164                 #endif /* LINUX_VERSION_CODE >= KERNL_VERSION(3,6,0) */
165                 if (ret < 0) {
166                         dev_err(chip->dev, "Failed to add regulator subdev\n");
167                         goto out_dev;
168                 }
169         }
170         #endif /* #ifdef CONFIG_REGULATOR_RT5025 */
171
172         #ifdef CONFIG_CHARGER_RT5025
173         if (use_dt || (pdata && pdata->chg_pdata)) {
174             RTINFO("mfd add charger dev\n");
175                 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
176                 ret = mfd_add_devices(chip->dev, 0, &chg_devs[0],
177                                         ARRAY_SIZE(chg_devs),
178                                         NULL, 0, NULL);
179                 #else
180                 ret = mfd_add_devices(chip->dev, 0, &chg_devs[0],
181                                         ARRAY_SIZE(chg_devs),
182                                         NULL, 0);
183                 #endif
184                 if (ret < 0) {
185                         dev_err(chip->dev, "Failed to add power supply subdev\n");
186                         goto out_dev;
187                 }
188         }
189         #endif /* #ifdef CONFIG_CHARGER_RT5025 */
190
191         #ifdef CONFIG_BATTERY_RT5025
192         if (use_dt || (pdata)) {
193             RTINFO("mfd add fuelgauge dev\n");
194                 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
195                 ret = mfd_add_devices(chip->dev, 0, &fg_devs[0],
196                                         ARRAY_SIZE(fg_devs),
197                                         NULL, 0, NULL);
198                 #else
199                 ret = mfd_add_devices(chip->dev, 0, &fg_devs[0],
200                                         ARRAY_SIZE(fg_devs),
201                                         NULL, 0);
202                 #endif
203                 if (ret < 0) {
204                         dev_err(chip->dev, "Failed to add power supply "
205                                 "subdev\n");
206                         goto out_dev;
207                 }
208         }
209         #endif /* #ifdef CONFIG_BATTERY_RT5025 */
210
211         /*Initialize the RT5025_GPIO*/
212         #ifdef CONFIG_GPIO_RT5025
213         if (use_dt || (pdata && pdata->gpio_pdata)) {
214                 RTINFO("mfd add gpios dev\n");
215                 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
216                 ret = mfd_add_devices(chip->dev, 0, &gpio_devs[0],
217                                       ARRAY_SIZE(gpio_devs),
218                                       NULL, 0, NULL);
219                 #else
220                 ret = mfd_add_devices(chip->dev, 0, &gpio_devs[0],
221                                       ARRAY_SIZE(gpio_devs),
222                                       NULL, 0);
223                 #endif /* LINUX_VERSION_CODE>=KERNL_VERSION(3,6,0) */
224                 if (ret < 0) {
225                         dev_err(chip->dev, "Failed to add gpio subdev\n");
226                         goto out_dev;
227                 }
228         }
229         #endif /* #ifdef CONFIG_GPIO_RT5025 */
230
231         #ifdef CONFIG_MISC_RT5025
232         if (use_dt || (pdata && pdata->misc_pdata)) {
233                 RTINFO("mfd add misc dev\n");
234                 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
235                 ret = mfd_add_devices(chip->dev, 0, &misc_devs[0],
236                                       ARRAY_SIZE(misc_devs),
237                                       NULL, 0, NULL);
238                 #else
239                 ret = mfd_add_devices(chip->dev, 0, &misc_devs[0],
240                                       ARRAY_SIZE(misc_devs),
241                                       NULL, 0);
242                 #endif /* LINUX_VERSION_CODE>=KERNL_VERSION(3,6,0) */
243                 if (ret < 0) {
244                         dev_err(chip->dev, "Failed to add misc subdev\n");
245                         goto out_dev;
246                 }
247         }
248         #endif /* #ifdef CONFIG_MISC_RT5025 */
249
250         #ifdef CONFIG_IRQ_RT5025
251         if (use_dt || (pdata && pdata->irq_pdata)) {
252                 RTINFO("mfd add irq dev\n");
253                 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
254                 ret = mfd_add_devices(chip->dev, 0, &irq_devs[0],
255                                       ARRAY_SIZE(irq_devs),
256                                       NULL, 0, NULL);
257                 #else
258                 ret = mfd_add_devices(chip->dev, 0, &irq_devs[0],
259                                       ARRAY_SIZE(irq_devs),
260                                       NULL, 0);
261                 #endif /* LINUX_VERSION_CODE>=KERNL_VERSION(3,6,0) */
262                 if (ret < 0) {
263                         dev_err(chip->dev, "Failed to add irq subdev\n");
264                         goto out_dev;
265                 }
266         }
267         #endif /* #ifdef CONFIG_IRQ_RT5025 */
268
269         #ifdef CONFIG_DEBUG_RT5025
270         RTINFO("mfd add debug dev\n");
271         #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
272         ret = mfd_add_devices(chip->dev, 0, &debug_devs[0],
273                               ARRAY_SIZE(debug_devs),
274                               NULL, 0, NULL);
275         #else
276         ret = mfd_add_devices(chip->dev, 0, &debug_devs[0],
277                               ARRAY_SIZE(debug_devs),
278                               NULL, 0);
279         #endif /* LINUX_VERSION_CODE>=KERNL_VERSION(3,6,0) */
280         if (ret < 0) {
281                 dev_err(chip->dev, "Failed to add debug subdev\n");
282                 goto out_dev;
283         }
284         #endif /* CONFIG_DEBUG_RT5025 */
285
286         RTINFO("Initialize all device successfully\n");
287         return ret;
288 out_dev:
289         mfd_remove_devices(chip->dev);
290         return ret;
291 }
292 EXPORT_SYMBOL(rt5025_core_init);
293
294 int rt5025_core_deinit(struct rt5025_chip *chip)
295 {
296         mfd_remove_devices(chip->dev);
297         return 0;
298 }
299 EXPORT_SYMBOL(rt5025_core_deinit);