Merge branch 'for-4.18/multitouch' into for-linus
[sfrench/cifs-2.6.git] / arch / arm / mach-omap2 / hsmmc.c
1 /*
2  * linux/arch/arm/mach-omap2/hsmmc.c
3  *
4  * Copyright (C) 2007-2008 Texas Instruments
5  * Copyright (C) 2008 Nokia Corporation
6  * Author: Texas Instruments
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.
11  */
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/mmc/host.h>
17 #include <linux/platform_data/hsmmc-omap.h>
18
19 #include "soc.h"
20 #include "omap_device.h"
21 #include "omap-pm.h"
22
23 #include "hsmmc.h"
24 #include "control.h"
25
26 #if IS_ENABLED(CONFIG_MMC_OMAP_HS)
27
28 static u16 control_pbias_offset;
29 static u16 control_devconf1_offset;
30
31 #define HSMMC_NAME_LEN  9
32
33 static int __init omap_hsmmc_pdata_init(struct omap2_hsmmc_info *c,
34                                         struct omap_hsmmc_platform_data *mmc)
35 {
36         char *hc_name;
37
38         hc_name = kzalloc(sizeof(char) * (HSMMC_NAME_LEN + 1), GFP_KERNEL);
39         if (!hc_name) {
40                 kfree(hc_name);
41                 return -ENOMEM;
42         }
43
44         snprintf(hc_name, (HSMMC_NAME_LEN + 1), "mmc%islot%i", c->mmc, 1);
45         mmc->name = hc_name;
46         mmc->caps = c->caps;
47         mmc->reg_offset = 0;
48
49         return 0;
50 }
51
52 static int omap_hsmmc_done;
53
54 void omap_hsmmc_late_init(struct omap2_hsmmc_info *c)
55 {
56         struct platform_device *pdev;
57         int res;
58
59         if (omap_hsmmc_done)
60                 return;
61
62         omap_hsmmc_done = 1;
63
64         for (; c->mmc; c++) {
65                 pdev = c->pdev;
66                 if (!pdev)
67                         continue;
68                 res = omap_device_register(pdev);
69                 if (res)
70                         pr_err("Could not late init MMC\n");
71         }
72 }
73
74 #define MAX_OMAP_MMC_HWMOD_NAME_LEN             16
75
76 static void __init omap_hsmmc_init_one(struct omap2_hsmmc_info *hsmmcinfo,
77                                         int ctrl_nr)
78 {
79         struct omap_hwmod *oh;
80         struct omap_hwmod *ohs[1];
81         struct omap_device *od;
82         struct platform_device *pdev;
83         char oh_name[MAX_OMAP_MMC_HWMOD_NAME_LEN];
84         struct omap_hsmmc_platform_data *mmc_data;
85         struct omap_hsmmc_dev_attr *mmc_dev_attr;
86         char *name;
87         int res;
88
89         mmc_data = kzalloc(sizeof(*mmc_data), GFP_KERNEL);
90         if (!mmc_data)
91                 return;
92
93         res = omap_hsmmc_pdata_init(hsmmcinfo, mmc_data);
94         if (res < 0)
95                 goto free_mmc;
96
97         name = "omap_hsmmc";
98         res = snprintf(oh_name, MAX_OMAP_MMC_HWMOD_NAME_LEN,
99                      "mmc%d", ctrl_nr);
100         WARN(res >= MAX_OMAP_MMC_HWMOD_NAME_LEN,
101              "String buffer overflow in MMC%d device setup\n", ctrl_nr);
102
103         oh = omap_hwmod_lookup(oh_name);
104         if (!oh) {
105                 pr_err("Could not look up %s\n", oh_name);
106                 goto free_name;
107         }
108         ohs[0] = oh;
109         if (oh->dev_attr != NULL) {
110                 mmc_dev_attr = oh->dev_attr;
111                 mmc_data->controller_flags = mmc_dev_attr->flags;
112         }
113
114         pdev = platform_device_alloc(name, ctrl_nr - 1);
115         if (!pdev) {
116                 pr_err("Could not allocate pdev for %s\n", name);
117                 goto free_name;
118         }
119         dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
120
121         od = omap_device_alloc(pdev, ohs, 1);
122         if (IS_ERR(od)) {
123                 pr_err("Could not allocate od for %s\n", name);
124                 goto put_pdev;
125         }
126
127         res = platform_device_add_data(pdev, mmc_data,
128                               sizeof(struct omap_hsmmc_platform_data));
129         if (res) {
130                 pr_err("Could not add pdata for %s\n", name);
131                 goto put_pdev;
132         }
133
134         hsmmcinfo->pdev = pdev;
135
136         res = omap_device_register(pdev);
137         if (res) {
138                 pr_err("Could not register od for %s\n", name);
139                 goto free_od;
140         }
141
142         goto free_mmc;
143
144 free_od:
145         omap_device_delete(od);
146
147 put_pdev:
148         platform_device_put(pdev);
149
150 free_name:
151         kfree(mmc_data->name);
152
153 free_mmc:
154         kfree(mmc_data);
155 }
156
157 void __init omap_hsmmc_init(struct omap2_hsmmc_info *controllers)
158 {
159         if (omap_hsmmc_done)
160                 return;
161
162         omap_hsmmc_done = 1;
163
164         if (cpu_is_omap2430()) {
165                 control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
166                 control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
167         } else {
168                 control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
169                 control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
170         }
171
172         for (; controllers->mmc; controllers++)
173                 omap_hsmmc_init_one(controllers, controllers->mmc);
174
175 }
176
177 #endif