blob: 8cb913fade1338f961ac9b273ce370967163bdb4 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* linux/arch/arm/mach-msm/board-sapphire-mmc.c
2 * Copyright (C) 2007-2009 HTC Corporation.
3 * Author: Thomas Tsai <thomas_tsai@htc.com>
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#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/platform_device.h>
18#include <linux/delay.h>
19#include <linux/mmc/host.h>
20#include <linux/mmc/sdio_ids.h>
21#include <linux/err.h>
22#include <linux/debugfs.h>
23
24#include <linux/gpio.h>
25#include <linux/io.h>
26#include <asm/mach-types.h>
27
28#include <mach/vreg.h>
29#include <mach/htc_pwrsink.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070030#include <mach/proc_comm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031
32#include <asm/mach/mmc.h>
33
34#include "devices.h"
35#include "gpio_chip.h"
36#include "board-sapphire.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
38#define DEBUG_SDSLOT_VDD 0
39
40extern int msm_add_sdcc(unsigned int controller, struct mmc_platform_data *plat,
41 unsigned int stat_irq, unsigned long stat_irq_flags);
42
43/* ---- COMMON ---- */
44static void config_gpio_table(uint32_t *table, int len)
45{
46 int n;
47 unsigned id;
48 for (n = 0; n < len; n++) {
49 id = table[n];
50 msm_proc_comm(PCOM_RPC_GPIO_TLMM_CONFIG_EX, &id, 0);
51 }
52}
53
54/* ---- SDCARD ---- */
55
56static uint32_t sdcard_on_gpio_table[] = {
57 PCOM_GPIO_CFG(62, 2, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_8MA), /* CLK */
58 PCOM_GPIO_CFG(63, 2, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_8MA), /* CMD */
59 PCOM_GPIO_CFG(64, 2, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_8MA), /* DAT3 */
60 PCOM_GPIO_CFG(65, 2, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_8MA), /* DAT2 */
61 PCOM_GPIO_CFG(66, 2, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_4MA), /* DAT1 */
62 PCOM_GPIO_CFG(67, 2, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_4MA), /* DAT0 */
63};
64
65static uint32_t sdcard_off_gpio_table[] = {
66 PCOM_GPIO_CFG(62, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* CLK */
67 PCOM_GPIO_CFG(63, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* CMD */
68 PCOM_GPIO_CFG(64, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* DAT3 */
69 PCOM_GPIO_CFG(65, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* DAT2 */
70 PCOM_GPIO_CFG(66, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* DAT1 */
71 PCOM_GPIO_CFG(67, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* DAT0 */
72};
73
74static uint opt_disable_sdcard;
75
76static int __init sapphire_disablesdcard_setup(char *str)
77{
78 int cal = simple_strtol(str, NULL, 0);
79
80 opt_disable_sdcard = cal;
81 return 1;
82}
83
84__setup("board_sapphire.disable_sdcard=", sapphire_disablesdcard_setup);
85
86static struct vreg *vreg_sdslot; /* SD slot power */
87
88struct mmc_vdd_xlat {
89 int mask;
90 int level;
91};
92
93static struct mmc_vdd_xlat mmc_vdd_table[] = {
94 { MMC_VDD_165_195, 1800 },
95 { MMC_VDD_20_21, 2050 },
96 { MMC_VDD_21_22, 2150 },
97 { MMC_VDD_22_23, 2250 },
98 { MMC_VDD_23_24, 2350 },
99 { MMC_VDD_24_25, 2450 },
100 { MMC_VDD_25_26, 2550 },
101 { MMC_VDD_26_27, 2650 },
102 { MMC_VDD_27_28, 2750 },
103 { MMC_VDD_28_29, 2850 },
104 { MMC_VDD_29_30, 2950 },
105};
106
107static unsigned int sdslot_vdd = 0xffffffff;
108static unsigned int sdslot_vreg_enabled;
109
110static uint32_t sapphire_sdslot_switchvdd(struct device *dev, unsigned int vdd)
111{
112 int i, rc;
113
114 BUG_ON(!vreg_sdslot);
115
116 if (vdd == sdslot_vdd)
117 return 0;
118
119 sdslot_vdd = vdd;
120
121 if (vdd == 0) {
122#if DEBUG_SDSLOT_VDD
123 printk(KERN_DEBUG "%s: Disabling SD slot power\n", __func__);
124#endif
125 config_gpio_table(sdcard_off_gpio_table,
126 ARRAY_SIZE(sdcard_off_gpio_table));
127 vreg_disable(vreg_sdslot);
128 sdslot_vreg_enabled = 0;
129 return 0;
130 }
131
132 if (!sdslot_vreg_enabled) {
133 rc = vreg_enable(vreg_sdslot);
134 if (rc) {
135 printk(KERN_ERR "%s: Error enabling vreg (%d)\n",
136 __func__, rc);
137 }
138 config_gpio_table(sdcard_on_gpio_table,
139 ARRAY_SIZE(sdcard_on_gpio_table));
140 sdslot_vreg_enabled = 1;
141 }
142
143 for (i = 0; i < ARRAY_SIZE(mmc_vdd_table); i++) {
144 if (mmc_vdd_table[i].mask == (1 << vdd)) {
145#if DEBUG_SDSLOT_VDD
146 printk(KERN_DEBUG "%s: Setting level to %u\n",
147 __func__, mmc_vdd_table[i].level);
148#endif
149 rc = vreg_set_level(vreg_sdslot,
150 mmc_vdd_table[i].level);
151 if (rc) {
152 printk(KERN_ERR
153 "%s: Error setting vreg level (%d)\n",
154 __func__, rc);
155 }
156 return 0;
157 }
158 }
159
160 printk(KERN_ERR "%s: Invalid VDD %d specified\n", __func__, vdd);
161 return 0;
162}
163
164static unsigned int sapphire_sdslot_status(struct device *dev)
165{
166 unsigned int status;
167
168 status = (unsigned int) gpio_get_value(SAPPHIRE_GPIO_SDMC_CD_N);
169 return !status;
170}
171
172#define SAPPHIRE_MMC_VDD (MMC_VDD_165_195 | MMC_VDD_20_21 | MMC_VDD_21_22 \
173 | MMC_VDD_22_23 | MMC_VDD_23_24 | MMC_VDD_24_25 \
174 | MMC_VDD_25_26 | MMC_VDD_26_27 | MMC_VDD_27_28 \
175 | MMC_VDD_28_29 | MMC_VDD_29_30)
176
177static struct mmc_platform_data sapphire_sdslot_data = {
178 .ocr_mask = SAPPHIRE_MMC_VDD,
179 .status = sapphire_sdslot_status,
180 .translate_vdd = sapphire_sdslot_switchvdd,
181};
182
183/* ---- WIFI ---- */
184
185static uint32_t wifi_on_gpio_table[] = {
186 PCOM_GPIO_CFG(51, 1, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_4MA), /* DAT3 */
187 PCOM_GPIO_CFG(52, 1, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_4MA), /* DAT2 */
188 PCOM_GPIO_CFG(53, 1, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_4MA), /* DAT1 */
189 PCOM_GPIO_CFG(54, 1, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_4MA), /* DAT0 */
190 PCOM_GPIO_CFG(55, 1, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_8MA), /* CMD */
191 PCOM_GPIO_CFG(56, 1, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_8MA), /* CLK */
192 PCOM_GPIO_CFG(29, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_4MA), /* WLAN IRQ */
193};
194
195static uint32_t wifi_off_gpio_table[] = {
196 PCOM_GPIO_CFG(51, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* DAT3 */
197 PCOM_GPIO_CFG(52, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* DAT2 */
198 PCOM_GPIO_CFG(53, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* DAT1 */
199 PCOM_GPIO_CFG(54, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* DAT0 */
200 PCOM_GPIO_CFG(55, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* CMD */
201 PCOM_GPIO_CFG(56, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* CLK */
202 PCOM_GPIO_CFG(29, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_4MA), /* WLAN IRQ */
203};
204
205static struct vreg *vreg_wifi_osc; /* WIFI 32khz oscilator */
206static int sapphire_wifi_cd = 0; /* WIFI virtual 'card detect' status */
207
208static struct sdio_embedded_func wifi_func = {
209 .f_class = SDIO_CLASS_WLAN,
210 .f_maxblksize = 512,
211};
212
213static struct embedded_sdio_data sapphire_wifi_emb_data = {
214 .cis = {
215 .vendor = 0x104c,
216 .device = 0x9066,
217 .blksize = 512,
218 .max_dtr = 20000000,
219 },
220 .cccr = {
221 .multi_block = 0,
222 .low_speed = 0,
223 .wide_bus = 1,
224 .high_power = 0,
225 .high_speed = 0,
226 },
227 .funcs = &wifi_func,
228 .num_funcs = 1,
229};
230
231static void (*wifi_status_cb)(int card_present, void *dev_id);
232static void *wifi_status_cb_devid;
233
234static int sapphire_wifi_status_register(void (*callback)(int card_present,
235 void *dev_id),
236 void *dev_id)
237{
238 if (wifi_status_cb)
239 return -EAGAIN;
240 wifi_status_cb = callback;
241 wifi_status_cb_devid = dev_id;
242 return 0;
243}
244
245static unsigned int sapphire_wifi_status(struct device *dev)
246{
247 return sapphire_wifi_cd;
248}
249
250int sapphire_wifi_set_carddetect(int val)
251{
252 printk(KERN_DEBUG "%s: %d\n", __func__, val);
253 sapphire_wifi_cd = val;
254 if (wifi_status_cb)
255 wifi_status_cb(val, wifi_status_cb_devid);
256 else
257 printk(KERN_WARNING "%s: Nobody to notify\n", __func__);
258 return 0;
259}
260#ifndef CONFIG_WIFI_CONTROL_FUNC
261EXPORT_SYMBOL(sapphire_wifi_set_carddetect);
262#endif
263
264int sapphire_wifi_power_state=0;
265int sapphire_bt_power_state=0;
266
267int sapphire_wifi_power(int on)
268{
269 int rc;
270
271 printk(KERN_DEBUG "%s: %d\n", __func__, on);
272
273 if (on) {
274 config_gpio_table(wifi_on_gpio_table,
275 ARRAY_SIZE(wifi_on_gpio_table));
276 rc = vreg_enable(vreg_wifi_osc);
277 if (rc)
278 return rc;
279 htc_pwrsink_set(PWRSINK_WIFI, 70);
280 } else {
281 config_gpio_table(wifi_off_gpio_table,
282 ARRAY_SIZE(wifi_off_gpio_table));
283 htc_pwrsink_set(PWRSINK_WIFI, 0);
284 }
285 gpio_set_value(SAPPHIRE_GPIO_MAC_32K_EN, on);
286 mdelay(100);
287 gpio_set_value(SAPPHIRE_GPIO_WIFI_EN, on);
288 mdelay(100);
289 if (!on) {
290 if(!sapphire_bt_power_state)
291 {
292 vreg_disable(vreg_wifi_osc);
293 printk("WiFi disable vreg_wifi_osc.\n");
294 }
295 else
296 printk("WiFi shouldn't disable vreg_wifi_osc. BT is using it!!\n");
297 }
298 sapphire_wifi_power_state = on;
299 return 0;
300}
301#ifndef CONFIG_WIFI_CONTROL_FUNC
302EXPORT_SYMBOL(sapphire_wifi_power);
303#endif
304
305/* Eenable VREG_MMC pin to turn on fastclock oscillator : colin */
306int sapphire_bt_fastclock_power(int on)
307{
308 int rc;
309
310 printk(KERN_DEBUG "sapphire_bt_fastclock_power on = %d\n", on);
311 if (vreg_wifi_osc) {
312 if (on) {
313 rc = vreg_enable(vreg_wifi_osc);
314 printk(KERN_DEBUG "BT vreg_enable vreg_mmc, rc=%d\n",
315 rc);
316 if (rc) {
317 printk("Error turn sapphire_bt_fastclock_power rc=%d\n", rc);
318 return rc;
319 }
320 } else {
321 if (!sapphire_wifi_power_state) {
322 vreg_disable(vreg_wifi_osc);
323 printk(KERN_DEBUG "BT disable vreg_wifi_osc.\n");
324 } else
325 printk(KERN_DEBUG "BT shouldn't disable vreg_wifi_osc. WiFi is using it!!\n");
326 }
327 }
328 sapphire_bt_power_state = on;
329 return 0;
330}
331EXPORT_SYMBOL(sapphire_bt_fastclock_power);
332
333static int sapphire_wifi_reset_state;
334void sapphire_wifi_reset(int on)
335{
336 printk(KERN_DEBUG "%s: %d\n", __func__, on);
337 gpio_set_value(SAPPHIRE_GPIO_WIFI_PA_RESETX, !on);
338 sapphire_wifi_reset_state = on;
339 mdelay(50);
340}
341#ifndef CONFIG_WIFI_CONTROL_FUNC
342EXPORT_SYMBOL(sapphire_wifi_reset);
343#endif
344
345static struct mmc_platform_data sapphire_wifi_data = {
346 .ocr_mask = MMC_VDD_28_29,
347 .status = sapphire_wifi_status,
348 .register_status_notify = sapphire_wifi_status_register,
349 .embedded_sdio = &sapphire_wifi_emb_data,
350};
351
352int __init sapphire_init_mmc(unsigned int sys_rev)
353{
354 wifi_status_cb = NULL;
355
356 sdslot_vreg_enabled = 0;
357
358 vreg_sdslot = vreg_get(0, "gp6");
359 if (IS_ERR(vreg_sdslot))
360 return PTR_ERR(vreg_sdslot);
361 vreg_wifi_osc = vreg_get(0, "mmc");
362 if (IS_ERR(vreg_wifi_osc))
363 return PTR_ERR(vreg_wifi_osc);
364
365 set_irq_wake(SAPPHIRE_GPIO_TO_INT(SAPPHIRE_GPIO_SDMC_CD_N), 1);
366
367 msm_add_sdcc(1, &sapphire_wifi_data, 0, 0);
368
369 if (!opt_disable_sdcard)
370 msm_add_sdcc(2, &sapphire_sdslot_data,
371 SAPPHIRE_GPIO_TO_INT(SAPPHIRE_GPIO_SDMC_CD_N), 0);
372 else
373 printk(KERN_INFO "sapphire: SD-Card interface disabled\n");
374 return 0;
375}
376
377#if defined(CONFIG_DEBUG_FS)
378static int sapphiremmc_dbg_wifi_reset_set(void *data, u64 val)
379{
380 sapphire_wifi_reset((int) val);
381 return 0;
382}
383
384static int sapphiremmc_dbg_wifi_reset_get(void *data, u64 *val)
385{
386 *val = sapphire_wifi_reset_state;
387 return 0;
388}
389
390static int sapphiremmc_dbg_wifi_cd_set(void *data, u64 val)
391{
392 sapphire_wifi_set_carddetect((int) val);
393 return 0;
394}
395
396static int sapphiremmc_dbg_wifi_cd_get(void *data, u64 *val)
397{
398 *val = sapphire_wifi_cd;
399 return 0;
400}
401
402static int sapphiremmc_dbg_wifi_pwr_set(void *data, u64 val)
403{
404 sapphire_wifi_power((int) val);
405 return 0;
406}
407
408static int sapphiremmc_dbg_wifi_pwr_get(void *data, u64 *val)
409{
410
411 *val = sapphire_wifi_power_state;
412 return 0;
413}
414
415static int sapphiremmc_dbg_sd_pwr_set(void *data, u64 val)
416{
417 sapphire_sdslot_switchvdd(NULL, (unsigned int) val);
418 return 0;
419}
420
421static int sapphiremmc_dbg_sd_pwr_get(void *data, u64 *val)
422{
423 *val = sdslot_vdd;
424 return 0;
425}
426
427static int sapphiremmc_dbg_sd_cd_set(void *data, u64 val)
428{
429 return -ENOSYS;
430}
431
432static int sapphiremmc_dbg_sd_cd_get(void *data, u64 *val)
433{
434 *val = sapphire_sdslot_status(NULL);
435 return 0;
436}
437
438DEFINE_SIMPLE_ATTRIBUTE(sapphiremmc_dbg_wifi_reset_fops,
439 sapphiremmc_dbg_wifi_reset_get,
440 sapphiremmc_dbg_wifi_reset_set, "%llu\n");
441
442DEFINE_SIMPLE_ATTRIBUTE(sapphiremmc_dbg_wifi_cd_fops,
443 sapphiremmc_dbg_wifi_cd_get,
444 sapphiremmc_dbg_wifi_cd_set, "%llu\n");
445
446DEFINE_SIMPLE_ATTRIBUTE(sapphiremmc_dbg_wifi_pwr_fops,
447 sapphiremmc_dbg_wifi_pwr_get,
448 sapphiremmc_dbg_wifi_pwr_set, "%llu\n");
449
450DEFINE_SIMPLE_ATTRIBUTE(sapphiremmc_dbg_sd_pwr_fops,
451 sapphiremmc_dbg_sd_pwr_get,
452 sapphiremmc_dbg_sd_pwr_set, "%llu\n");
453
454DEFINE_SIMPLE_ATTRIBUTE(sapphiremmc_dbg_sd_cd_fops,
455 sapphiremmc_dbg_sd_cd_get,
456 sapphiremmc_dbg_sd_cd_set, "%llu\n");
457
458static int __init sapphiremmc_dbg_init(void)
459{
460 struct dentry *dent;
461
462 if (!machine_is_sapphire())
463 return 0;
464
465 dent = debugfs_create_dir("sapphiremmc_dbg", 0);
466 if (IS_ERR(dent))
467 return PTR_ERR(dent);
468
469 debugfs_create_file("wifi_reset", 0644, dent, NULL,
470 &sapphiremmc_dbg_wifi_reset_fops);
471 debugfs_create_file("wifi_cd", 0644, dent, NULL,
472 &sapphiremmc_dbg_wifi_cd_fops);
473 debugfs_create_file("wifi_pwr", 0644, dent, NULL,
474 &sapphiremmc_dbg_wifi_pwr_fops);
475
476 debugfs_create_file("sd_pwr", 0644, dent, NULL,
477 &sapphiremmc_dbg_sd_pwr_fops);
478 debugfs_create_file("sd_cd", 0644, dent, NULL,
479 &sapphiremmc_dbg_sd_cd_fops);
480
481 return 0;
482}
483
484device_initcall(sapphiremmc_dbg_init);
485
486#endif