ARM: tegra: ardbeg/loki: Fix calibration offsets
[linux-3.10.git] / arch / arm / mach-tegra / board-loki-sdhci.c
1 /*
2  * arch/arm/mach-tegra/board-loki-sdhci.c
3  *
4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/resource.h>
20 #include <linux/platform_device.h>
21 #include <linux/wlan_plat.h>
22 #include <linux/delay.h>
23 #include <linux/gpio.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
26 #include <linux/mmc/host.h>
27 #include <linux/wl12xx.h>
28 #include <linux/platform_data/mmc-sdhci-tegra.h>
29 #include <linux/mfd/max77660/max77660-core.h>
30
31 #include <asm/mach-types.h>
32 #include <mach/irqs.h>
33 #include <mach/gpio-tegra.h>
34
35 #include "gpio-names.h"
36 #include "board.h"
37 #include "board-loki.h"
38 #include "iomap.h"
39
40 #define LOKI_WLAN_RST   TEGRA_GPIO_PR3
41 #define LOKI_WLAN_PWR   TEGRA_GPIO_PCC5
42 #define LOKI_WLAN_WOW   TEGRA_GPIO_PU5
43
44 #define LOKI_SD_CD      TEGRA_GPIO_PV2
45
46 static void (*wifi_status_cb)(int card_present, void *dev_id);
47 static void *wifi_status_cb_devid;
48 static int loki_wifi_status_register(void (*callback)(int , void *), void *);
49
50 static int loki_wifi_reset(int on);
51 static int loki_wifi_power(int on);
52 static int loki_wifi_set_carddetect(int val);
53
54 static struct wifi_platform_data loki_wifi_control = {
55         .set_power      = loki_wifi_power,
56         .set_reset      = loki_wifi_reset,
57         .set_carddetect = loki_wifi_set_carddetect,
58 };
59
60 static struct resource wifi_resource[] = {
61         [0] = {
62                 .name   = "bcm4329_wlan_irq",
63                 .flags  = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL
64                                 | IORESOURCE_IRQ_SHAREABLE,
65         },
66 };
67
68 static struct platform_device loki_wifi_device = {
69         .name           = "bcm4329_wlan",
70         .id             = 1,
71         .num_resources  = 1,
72         .resource       = wifi_resource,
73         .dev            = {
74                 .platform_data = &loki_wifi_control,
75         },
76 };
77
78 static struct resource sdhci_resource0[] = {
79         [0] = {
80                 .start  = INT_SDMMC1,
81                 .end    = INT_SDMMC1,
82                 .flags  = IORESOURCE_IRQ,
83         },
84         [1] = {
85                 .start  = TEGRA_SDMMC1_BASE,
86                 .end    = TEGRA_SDMMC1_BASE + TEGRA_SDMMC1_SIZE-1,
87                 .flags  = IORESOURCE_MEM,
88         },
89 };
90
91 static struct resource sdhci_resource2[] = {
92         [0] = {
93                 .start  = INT_SDMMC3,
94                 .end    = INT_SDMMC3,
95                 .flags  = IORESOURCE_IRQ,
96         },
97         [1] = {
98                 .start  = TEGRA_SDMMC3_BASE,
99                 .end    = TEGRA_SDMMC3_BASE + TEGRA_SDMMC3_SIZE-1,
100                 .flags  = IORESOURCE_MEM,
101         },
102 };
103
104 static struct resource sdhci_resource3[] = {
105         [0] = {
106                 .start  = INT_SDMMC4,
107                 .end    = INT_SDMMC4,
108                 .flags  = IORESOURCE_IRQ,
109         },
110         [1] = {
111                 .start  = TEGRA_SDMMC4_BASE,
112                 .end    = TEGRA_SDMMC4_BASE + TEGRA_SDMMC4_SIZE-1,
113                 .flags  = IORESOURCE_MEM,
114         },
115 };
116
117 #ifdef CONFIG_MMC_EMBEDDED_SDIO
118 static struct embedded_sdio_data embedded_sdio_data0 = {
119         .cccr   = {
120                 .sdio_vsn       = 2,
121                 .multi_block    = 1,
122                 .low_speed      = 0,
123                 .wide_bus       = 0,
124                 .high_power     = 1,
125                 .high_speed     = 1,
126         },
127         .cis  = {
128                 .vendor  = 0x02d0,
129                 .device  = 0x4329,
130         },
131 };
132 #endif
133
134 static struct tegra_sdhci_platform_data tegra_sdhci_platform_data0 = {
135         .mmc_data = {
136                 .register_status_notify = loki_wifi_status_register,
137 #ifdef CONFIG_MMC_EMBEDDED_SDIO
138                 .embedded_sdio = &embedded_sdio_data0,
139 #endif
140                 .built_in = 0,
141                 .ocr_mask = MMC_OCR_1V8_MASK,
142         },
143         .cd_gpio = -1,
144         .wp_gpio = -1,
145         .power_gpio = -1,
146         .tap_delay = 0,
147         .trim_delay = 0x2,
148         .ddr_clk_limit = 41000000,
149         .uhs_mask = MMC_UHS_MASK_SDR104 |
150                 /* FIXME: Enable UHS mode */
151                 MMC_UHS_MASK_SDR12 | MMC_UHS_MASK_SDR25 |
152                 MMC_UHS_MASK_DDR50 | MMC_UHS_MASK_SDR50,
153         .calib_3v3_offsets = 0x7676,
154         .calib_1v8_offsets = 0x7676,
155 };
156
157 static struct tegra_sdhci_platform_data tegra_sdhci_platform_data2 = {
158         .cd_gpio = LOKI_SD_CD,
159         .wp_gpio = -1,
160         .power_gpio = -1,
161         .tap_delay = 0,
162         .trim_delay = 0x3,
163         .uhs_mask = MMC_UHS_MASK_SDR104 |
164                 /* FIXME: Enable UHS mode */
165                 MMC_UHS_MASK_SDR12 | MMC_UHS_MASK_SDR25 |
166                 MMC_UHS_MASK_DDR50 | MMC_UHS_MASK_SDR50,
167         .calib_3v3_offsets = 0x7676,
168         .calib_1v8_offsets = 0x7676,
169 };
170
171 static struct tegra_sdhci_platform_data tegra_sdhci_platform_data3 = {
172         .cd_gpio = -1,
173         .wp_gpio = -1,
174         .power_gpio = -1,
175         .is_8bit = 1,
176         .tap_delay = 0x4,
177         .trim_delay = 0x4,
178         .ddr_trim_delay = 0x4,
179         .mmc_data = {
180                 .built_in = 1,
181                 .ocr_mask = MMC_OCR_1V8_MASK,
182         },
183         .uhs_mask = MMC_MASK_HS200,
184         .ddr_clk_limit = 51000000,
185         .max_clk_limit = 102000000,
186         .calib_3v3_offsets = 0x0202,
187         .calib_1v8_offsets = 0x0202,
188 };
189
190 static struct platform_device tegra_sdhci_device0 = {
191         .name           = "sdhci-tegra",
192         .id             = 0,
193         .resource       = sdhci_resource0,
194         .num_resources  = ARRAY_SIZE(sdhci_resource0),
195         .dev = {
196                 .platform_data = &tegra_sdhci_platform_data0,
197         },
198 };
199
200 static struct platform_device tegra_sdhci_device2 = {
201         .name           = "sdhci-tegra",
202         .id             = 2,
203         .resource       = sdhci_resource2,
204         .num_resources  = ARRAY_SIZE(sdhci_resource2),
205         .dev = {
206                 .platform_data = &tegra_sdhci_platform_data2,
207         },
208 };
209
210 static struct platform_device tegra_sdhci_device3 = {
211         .name           = "sdhci-tegra",
212         .id             = 3,
213         .resource       = sdhci_resource3,
214         .num_resources  = ARRAY_SIZE(sdhci_resource3),
215         .dev = {
216                 .platform_data = &tegra_sdhci_platform_data3,
217         },
218 };
219
220 static int loki_wifi_status_register(
221                 void (*callback)(int card_present, void *dev_id),
222                 void *dev_id)
223 {
224         if (wifi_status_cb)
225                 return -EAGAIN;
226         wifi_status_cb = callback;
227         wifi_status_cb_devid = dev_id;
228         return 0;
229 }
230
231 static int loki_wifi_set_carddetect(int val)
232 {
233         pr_debug("%s: %d\n", __func__, val);
234         if (wifi_status_cb)
235                 wifi_status_cb(val, wifi_status_cb_devid);
236         else
237                 pr_warn("%s: Nobody to notify\n", __func__);
238         return 0;
239 }
240
241 static struct regulator *loki_vdd_com_3v3;
242 static struct regulator *loki_vddio_com_1v8;
243
244 #define LOKI_VDD_WIFI_3V3 "avdd"
245 #define LOKI_VDD_WIFI_1V8 "dvdd"
246
247 static int loki_wifi_regulator_enable(void)
248 {
249         int ret = 0;
250
251         /* Enable COM's vdd_com_3v3 regulator*/
252         if (IS_ERR_OR_NULL(loki_vdd_com_3v3)) {
253                 loki_vdd_com_3v3 = regulator_get(&loki_wifi_device.dev,
254                                         LOKI_VDD_WIFI_3V3);
255                 if (IS_ERR_OR_NULL(loki_vdd_com_3v3)) {
256                         pr_err("Couldn't get regulator "
257                                 LOKI_VDD_WIFI_3V3 "\n");
258                         return PTR_ERR(loki_vdd_com_3v3);
259                 }
260
261                 ret = regulator_enable(loki_vdd_com_3v3);
262                 if (ret < 0) {
263                         pr_err("Couldn't enable regulator "
264                                 LOKI_VDD_WIFI_3V3 "\n");
265                         regulator_put(loki_vdd_com_3v3);
266                         loki_vdd_com_3v3 = NULL;
267                         return ret;
268                 }
269         }
270
271         /* Enable COM's vddio_com_1v8 regulator*/
272         if (IS_ERR_OR_NULL(loki_vddio_com_1v8)) {
273                 loki_vddio_com_1v8 = regulator_get(&loki_wifi_device.dev,
274                         LOKI_VDD_WIFI_1V8);
275                 if (IS_ERR_OR_NULL(loki_vddio_com_1v8)) {
276                         pr_err("Couldn't get regulator "
277                                 LOKI_VDD_WIFI_1V8 "\n");
278                         regulator_disable(loki_vdd_com_3v3);
279
280                         regulator_put(loki_vdd_com_3v3);
281                         loki_vdd_com_3v3 = NULL;
282                         return PTR_ERR(loki_vddio_com_1v8);
283                 }
284
285                 ret = regulator_enable(loki_vddio_com_1v8);
286                 if (ret < 0) {
287                         pr_err("Couldn't enable regulator "
288                                 LOKI_VDD_WIFI_1V8 "\n");
289                         regulator_put(loki_vddio_com_1v8);
290                         loki_vddio_com_1v8 = NULL;
291
292                         regulator_disable(loki_vdd_com_3v3);
293                         regulator_put(loki_vdd_com_3v3);
294                         loki_vdd_com_3v3 = NULL;
295                         return ret;
296                 }
297         }
298
299         return ret;
300 }
301
302 static void loki_wifi_regulator_disable(void)
303 {
304         /* Disable COM's vdd_com_3v3 regulator*/
305         if (!IS_ERR_OR_NULL(loki_vdd_com_3v3)) {
306                 regulator_disable(loki_vdd_com_3v3);
307                 regulator_put(loki_vdd_com_3v3);
308                 loki_vdd_com_3v3 = NULL;
309         }
310
311         /* Disable COM's vddio_com_1v8 regulator*/
312         if (!IS_ERR_OR_NULL(loki_vddio_com_1v8)) {
313                 regulator_disable(loki_vddio_com_1v8);
314                 regulator_put(loki_vddio_com_1v8);
315                 loki_vddio_com_1v8 = NULL;
316         }
317 }
318
319 static int loki_wifi_power(int on)
320 {
321         int ret = 0;
322
323         pr_err("%s: %d\n", __func__, on);
324
325         /* Enable COM's regulators on wi-fi poer on*/
326         if (on == 1) {
327                 ret = loki_wifi_regulator_enable();
328                 if (ret < 0) {
329                         pr_err("Failed to enable COM regulators\n");
330                         return ret;
331                 }
332         }
333
334         gpio_set_value(LOKI_WLAN_PWR, on);
335         gpio_set_value(LOKI_WLAN_RST, on);
336         mdelay(100);
337
338         /* Disable COM's regulators on wi-fi poer off*/
339         if (on != 1) {
340                 pr_debug("Disabling COM regulators\n");
341                 loki_wifi_regulator_disable();
342         }
343
344         return 0;
345 }
346
347 static int loki_wifi_reset(int on)
348 {
349         pr_debug("%s: do nothing\n", __func__);
350         return 0;
351 }
352
353 static int __init loki_wifi_init(void)
354 {
355         int rc;
356
357         rc = gpio_request(LOKI_WLAN_PWR, "wlan_power");
358         if (rc)
359                 pr_err("WLAN_PWR gpio request failed:%d\n", rc);
360         rc = gpio_request(LOKI_WLAN_RST, "wlan_rst");
361         if (rc)
362                 pr_err("WLAN_RST gpio request failed:%d\n", rc);
363         rc = gpio_request(LOKI_WLAN_WOW, "bcmsdh_sdmmc");
364         if (rc)
365                 pr_err("WLAN_WOW gpio request failed:%d\n", rc);
366
367         rc = gpio_direction_output(LOKI_WLAN_PWR, 0);
368         if (rc)
369                 pr_err("WLAN_PWR gpio direction configuration failed:%d\n", rc);
370         rc = gpio_direction_output(LOKI_WLAN_RST, 0);
371         if (rc)
372                 pr_err("WLAN_RST gpio direction configuration failed:%d\n", rc);
373
374         rc = gpio_direction_input(LOKI_WLAN_WOW);
375         if (rc)
376                 pr_err("WLAN_WOW gpio direction configuration failed:%d\n", rc);
377
378         wifi_resource[0].start = wifi_resource[0].end =
379                 gpio_to_irq(LOKI_WLAN_WOW);
380
381         platform_device_register(&loki_wifi_device);
382         return 0;
383 }
384
385 #ifdef CONFIG_TEGRA_PREPOWER_WIFI
386 static int __init loki_wifi_prepower(void)
387 {
388         if (!of_machine_is_compatible("nvidia,loki"))
389                 return 0;
390         loki_wifi_power(1);
391
392         return 0;
393 }
394
395 subsys_initcall_sync(loki_wifi_prepower);
396 #endif
397
398 int __init loki_sdhci_init(void)
399 {
400         platform_device_register(&tegra_sdhci_device3);
401         platform_device_register(&tegra_sdhci_device2);
402         platform_device_register(&tegra_sdhci_device0);
403         loki_wifi_init();
404
405         return 0;
406 }