blob: 7bae4011534a5cca33b7021bf2cf505eec625c13 [file] [log] [blame]
Asutosh Das33a4ff52012-12-18 16:14:02 +05301/*
2 * drivers/mmc/host/sdhci-msm.c - Qualcomm MSM SDHCI Platform
3 * driver source file
4 *
5 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/module.h>
19#include <linux/mmc/host.h>
20#include <linux/mmc/card.h>
21#include <linux/mmc/sdio_func.h>
22#include <linux/gfp.h>
23#include <linux/of.h>
24#include <linux/of_gpio.h>
25#include <linux/regulator/consumer.h>
26#include <linux/types.h>
27#include <linux/input.h>
28#include <linux/platform_device.h>
29#include <linux/wait.h>
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070030#include <linux/io.h>
31#include <linux/delay.h>
32#include <linux/scatterlist.h>
33#include <linux/slab.h>
34#include <linux/mmc/mmc.h>
Asutosh Dasbbc84782013-02-11 15:31:35 +053035#include <linux/pm.h>
36#include <linux/pm_runtime.h>
Sahitya Tummala62448d92013-03-12 14:57:46 +053037#include <linux/mmc/cd-gpio.h>
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070038#include <mach/gpio.h>
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +053039#include <mach/msm_bus.h>
Asutosh Das33a4ff52012-12-18 16:14:02 +053040
41#include "sdhci-pltfm.h"
42
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -070043#define SDHCI_VER_100 0x2B
Asutosh Das33a4ff52012-12-18 16:14:02 +053044#define CORE_HC_MODE 0x78
45#define HC_MODE_EN 0x1
46
47#define CORE_POWER 0x0
48#define CORE_SW_RST (1 << 7)
49
50#define CORE_PWRCTL_STATUS 0xDC
51#define CORE_PWRCTL_MASK 0xE0
52#define CORE_PWRCTL_CLEAR 0xE4
53#define CORE_PWRCTL_CTL 0xE8
54
55#define CORE_PWRCTL_BUS_OFF 0x01
56#define CORE_PWRCTL_BUS_ON (1 << 1)
57#define CORE_PWRCTL_IO_LOW (1 << 2)
58#define CORE_PWRCTL_IO_HIGH (1 << 3)
59
60#define CORE_PWRCTL_BUS_SUCCESS 0x01
61#define CORE_PWRCTL_BUS_FAIL (1 << 1)
62#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
63#define CORE_PWRCTL_IO_FAIL (1 << 3)
64
65#define INT_MASK 0xF
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070066#define MAX_PHASES 16
67
68#define CORE_DLL_LOCK (1 << 7)
69#define CORE_DLL_EN (1 << 16)
70#define CORE_CDR_EN (1 << 17)
71#define CORE_CK_OUT_EN (1 << 18)
72#define CORE_CDR_EXT_EN (1 << 19)
73#define CORE_DLL_PDN (1 << 29)
74#define CORE_DLL_RST (1 << 30)
75#define CORE_DLL_CONFIG 0x100
76#define CORE_DLL_TEST_CTL 0x104
77#define CORE_DLL_STATUS 0x108
78
79#define CORE_VENDOR_SPEC 0x10C
80#define CORE_CLK_PWRSAVE (1 << 1)
81#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
82
Asutosh Das3781bd82013-01-10 21:11:04 +053083/* 8KB descriptors */
84#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummala04c3a462013-01-11 11:30:45 +053085#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das3781bd82013-01-10 21:11:04 +053086
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070087static const u32 tuning_block_64[] = {
88 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
89 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
90 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
91 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
92};
93
94static const u32 tuning_block_128[] = {
95 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
96 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
97 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
98 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
99 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
100 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
101 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
102 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
103};
Asutosh Das33a4ff52012-12-18 16:14:02 +0530104
105/* This structure keeps information per regulator */
106struct sdhci_msm_reg_data {
107 /* voltage regulator handle */
108 struct regulator *reg;
109 /* regulator name */
110 const char *name;
111 /* voltage level to be set */
112 u32 low_vol_level;
113 u32 high_vol_level;
114 /* Load values for low power and high power mode */
115 u32 lpm_uA;
116 u32 hpm_uA;
117
118 /* is this regulator enabled? */
119 bool is_enabled;
120 /* is this regulator needs to be always on? */
121 bool is_always_on;
122 /* is low power mode setting required for this regulator? */
123 bool lpm_sup;
124};
125
126/*
127 * This structure keeps information for all the
128 * regulators required for a SDCC slot.
129 */
130struct sdhci_msm_slot_reg_data {
131 /* keeps VDD/VCC regulator info */
132 struct sdhci_msm_reg_data *vdd_data;
133 /* keeps VDD IO regulator info */
134 struct sdhci_msm_reg_data *vdd_io_data;
135};
136
137struct sdhci_msm_gpio {
138 u32 no;
139 const char *name;
140 bool is_enabled;
141};
142
143struct sdhci_msm_gpio_data {
144 struct sdhci_msm_gpio *gpio;
145 u8 size;
146};
147
Asutosh Das390519d2012-12-21 12:21:42 +0530148struct sdhci_msm_pad_pull {
149 enum msm_tlmm_pull_tgt no;
150 u32 val;
151};
152
153struct sdhci_msm_pad_pull_data {
154 struct sdhci_msm_pad_pull *on;
155 struct sdhci_msm_pad_pull *off;
156 u8 size;
157};
158
159struct sdhci_msm_pad_drv {
160 enum msm_tlmm_hdrive_tgt no;
161 u32 val;
162};
163
164struct sdhci_msm_pad_drv_data {
165 struct sdhci_msm_pad_drv *on;
166 struct sdhci_msm_pad_drv *off;
167 u8 size;
168};
169
170struct sdhci_msm_pad_data {
171 struct sdhci_msm_pad_pull_data *pull;
172 struct sdhci_msm_pad_drv_data *drv;
173};
174
175
Asutosh Das33a4ff52012-12-18 16:14:02 +0530176struct sdhci_msm_pin_data {
177 /*
178 * = 1 if controller pins are using gpios
179 * = 0 if controller has dedicated MSM pads
180 */
Asutosh Das390519d2012-12-21 12:21:42 +0530181 u8 is_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530182 bool cfg_sts;
183 struct sdhci_msm_gpio_data *gpio_data;
Asutosh Das390519d2012-12-21 12:21:42 +0530184 struct sdhci_msm_pad_data *pad_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530185};
186
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530187struct sdhci_msm_bus_voting_data {
188 struct msm_bus_scale_pdata *bus_pdata;
189 unsigned int *bw_vecs;
190 unsigned int bw_vecs_size;
191};
192
Asutosh Das33a4ff52012-12-18 16:14:02 +0530193struct sdhci_msm_pltfm_data {
194 /* Supported UHS-I Modes */
195 u32 caps;
196
197 /* More capabilities */
198 u32 caps2;
199
200 unsigned long mmc_bus_width;
201 u32 max_clk;
202 struct sdhci_msm_slot_reg_data *vreg_data;
203 bool nonremovable;
204 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalab4e84042013-03-10 07:03:17 +0530205 u32 cpu_dma_latency_us;
Sahitya Tummala62448d92013-03-12 14:57:46 +0530206 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530207 struct sdhci_msm_bus_voting_data *voting_data;
208};
209
210struct sdhci_msm_bus_vote {
211 uint32_t client_handle;
212 uint32_t curr_vote;
213 int min_bw_vote;
214 int max_bw_vote;
215 bool is_max_bw_needed;
216 struct delayed_work vote_work;
217 struct device_attribute max_bus_bw;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530218};
219
220struct sdhci_msm_host {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530221 struct platform_device *pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530222 void __iomem *core_mem; /* MSM SDCC mapped address */
Asutosh Dasbbc84782013-02-11 15:31:35 +0530223 int pwr_irq; /* power irq */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530224 struct clk *clk; /* main SD/MMC bus clock */
225 struct clk *pclk; /* SDHC peripheral bus clock */
226 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530227 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530228 struct sdhci_msm_pltfm_data *pdata;
229 struct mmc_host *mmc;
230 struct sdhci_pltfm_data sdhci_msm_pdata;
231 wait_queue_head_t pwr_irq_wait;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530232 struct sdhci_msm_bus_vote msm_bus_vote;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530233};
234
235enum vdd_io_level {
236 /* set vdd_io_data->low_vol_level */
237 VDD_IO_LOW,
238 /* set vdd_io_data->high_vol_level */
239 VDD_IO_HIGH,
240 /*
241 * set whatever there in voltage_level (third argument) of
242 * sdhci_msm_set_vdd_io_vol() function.
243 */
244 VDD_IO_SET_LEVEL,
245};
246
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700247/* MSM platform specific tuning */
248static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
249 u8 poll)
250{
251 int rc = 0;
252 u32 wait_cnt = 50;
253 u8 ck_out_en = 0;
254 struct mmc_host *mmc = host->mmc;
255
256 /* poll for CK_OUT_EN bit. max. poll time = 50us */
257 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
258 CORE_CK_OUT_EN);
259
260 while (ck_out_en != poll) {
261 if (--wait_cnt == 0) {
262 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
263 mmc_hostname(mmc), __func__, poll);
264 rc = -ETIMEDOUT;
265 goto out;
266 }
267 udelay(1);
268
269 ck_out_en = !!(readl_relaxed(host->ioaddr +
270 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
271 }
272out:
273 return rc;
274}
275
276static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
277{
278 int rc = 0;
279 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
280 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
281 0x8};
282 unsigned long flags;
283 u32 config;
284 struct mmc_host *mmc = host->mmc;
285
286 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
287 spin_lock_irqsave(&host->lock, flags);
288
289 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
290 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
291 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
292 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
293
294 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
295 rc = msm_dll_poll_ck_out_en(host, 0);
296 if (rc)
297 goto err_out;
298
299 /*
300 * Write the selected DLL clock output phase (0 ... 15)
301 * to CDR_SELEXT bit field of DLL_CONFIG register.
302 */
303 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
304 & ~(0xF << 20))
305 | (grey_coded_phase_table[phase] << 20)),
306 host->ioaddr + CORE_DLL_CONFIG);
307
308 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
309 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
310 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
311
312 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
313 rc = msm_dll_poll_ck_out_en(host, 1);
314 if (rc)
315 goto err_out;
316
317 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
318 config |= CORE_CDR_EN;
319 config &= ~CORE_CDR_EXT_EN;
320 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
321 goto out;
322
323err_out:
324 pr_err("%s: %s: Failed to set DLL phase: %d\n",
325 mmc_hostname(mmc), __func__, phase);
326out:
327 spin_unlock_irqrestore(&host->lock, flags);
328 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
329 return rc;
330}
331
332/*
333 * Find out the greatest range of consecuitive selected
334 * DLL clock output phases that can be used as sampling
335 * setting for SD3.0 UHS-I card read operation (in SDR104
336 * timing mode) or for eMMC4.5 card read operation (in HS200
337 * timing mode).
338 * Select the 3/4 of the range and configure the DLL with the
339 * selected DLL clock output phase.
340 */
341
342static int msm_find_most_appropriate_phase(struct sdhci_host *host,
343 u8 *phase_table, u8 total_phases)
344{
345 int ret;
346 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
347 u8 phases_per_row[MAX_PHASES] = {0};
348 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
349 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
350 bool phase_0_found = false, phase_15_found = false;
351 struct mmc_host *mmc = host->mmc;
352
353 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
354 if (!total_phases || (total_phases > MAX_PHASES)) {
355 pr_err("%s: %s: invalid argument: total_phases=%d\n",
356 mmc_hostname(mmc), __func__, total_phases);
357 return -EINVAL;
358 }
359
360 for (cnt = 0; cnt < total_phases; cnt++) {
361 ranges[row_index][col_index] = phase_table[cnt];
362 phases_per_row[row_index] += 1;
363 col_index++;
364
365 if ((cnt + 1) == total_phases) {
366 continue;
367 /* check if next phase in phase_table is consecutive or not */
368 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
369 row_index++;
370 col_index = 0;
371 }
372 }
373
374 if (row_index >= MAX_PHASES)
375 return -EINVAL;
376
377 /* Check if phase-0 is present in first valid window? */
378 if (!ranges[0][0]) {
379 phase_0_found = true;
380 phase_0_raw_index = 0;
381 /* Check if cycle exist between 2 valid windows */
382 for (cnt = 1; cnt <= row_index; cnt++) {
383 if (phases_per_row[cnt]) {
384 for (i = 0; i < phases_per_row[cnt]; i++) {
385 if (ranges[cnt][i] == 15) {
386 phase_15_found = true;
387 phase_15_raw_index = cnt;
388 break;
389 }
390 }
391 }
392 }
393 }
394
395 /* If 2 valid windows form cycle then merge them as single window */
396 if (phase_0_found && phase_15_found) {
397 /* number of phases in raw where phase 0 is present */
398 u8 phases_0 = phases_per_row[phase_0_raw_index];
399 /* number of phases in raw where phase 15 is present */
400 u8 phases_15 = phases_per_row[phase_15_raw_index];
401
402 if (phases_0 + phases_15 >= MAX_PHASES)
403 /*
404 * If there are more than 1 phase windows then total
405 * number of phases in both the windows should not be
406 * more than or equal to MAX_PHASES.
407 */
408 return -EINVAL;
409
410 /* Merge 2 cyclic windows */
411 i = phases_15;
412 for (cnt = 0; cnt < phases_0; cnt++) {
413 ranges[phase_15_raw_index][i] =
414 ranges[phase_0_raw_index][cnt];
415 if (++i >= MAX_PHASES)
416 break;
417 }
418
419 phases_per_row[phase_0_raw_index] = 0;
420 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
421 }
422
423 for (cnt = 0; cnt <= row_index; cnt++) {
424 if (phases_per_row[cnt] > curr_max) {
425 curr_max = phases_per_row[cnt];
426 selected_row_index = cnt;
427 }
428 }
429
430 i = ((curr_max * 3) / 4);
431 if (i)
432 i--;
433
434 ret = (int)ranges[selected_row_index][i];
435
436 if (ret >= MAX_PHASES) {
437 ret = -EINVAL;
438 pr_err("%s: %s: invalid phase selected=%d\n",
439 mmc_hostname(mmc), __func__, ret);
440 }
441
442 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
443 return ret;
444}
445
446static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
447{
448 u32 mclk_freq = 0;
449
450 /* Program the MCLK value to MCLK_FREQ bit field */
451 if (host->clock <= 112000000)
452 mclk_freq = 0;
453 else if (host->clock <= 125000000)
454 mclk_freq = 1;
455 else if (host->clock <= 137000000)
456 mclk_freq = 2;
457 else if (host->clock <= 150000000)
458 mclk_freq = 3;
459 else if (host->clock <= 162000000)
460 mclk_freq = 4;
461 else if (host->clock <= 175000000)
462 mclk_freq = 5;
463 else if (host->clock <= 187000000)
464 mclk_freq = 6;
465 else if (host->clock <= 200000000)
466 mclk_freq = 7;
467
468 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
469 & ~(7 << 24)) | (mclk_freq << 24)),
470 host->ioaddr + CORE_DLL_CONFIG);
471}
472
473/* Initialize the DLL (Programmable Delay Line ) */
474static int msm_init_cm_dll(struct sdhci_host *host)
475{
476 struct mmc_host *mmc = host->mmc;
477 int rc = 0;
478 unsigned long flags;
479 u32 wait_cnt;
480
481 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
482 spin_lock_irqsave(&host->lock, flags);
483
484 /*
485 * Make sure that clock is always enabled when DLL
486 * tuning is in progress. Keeping PWRSAVE ON may
487 * turn off the clock. So let's disable the PWRSAVE
488 * here and re-enable it once tuning is completed.
489 */
490 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
491 & ~CORE_CLK_PWRSAVE),
492 host->ioaddr + CORE_VENDOR_SPEC);
493
494 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
495 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
496 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
497
498 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
499 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
500 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
501 msm_cm_dll_set_freq(host);
502
503 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
504 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
505 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
506
507 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
508 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
509 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
510
511 /* Set DLL_EN bit to 1. */
512 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
513 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
514
515 /* Set CK_OUT_EN bit to 1. */
516 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
517 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
518
519 wait_cnt = 50;
520 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
521 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
522 CORE_DLL_LOCK)) {
523 /* max. wait for 50us sec for LOCK bit to be set */
524 if (--wait_cnt == 0) {
525 pr_err("%s: %s: DLL failed to LOCK\n",
526 mmc_hostname(mmc), __func__);
527 rc = -ETIMEDOUT;
528 goto out;
529 }
530 /* wait for 1us before polling again */
531 udelay(1);
532 }
533
534out:
535 /* re-enable PWRSAVE */
536 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
537 CORE_CLK_PWRSAVE),
538 host->ioaddr + CORE_VENDOR_SPEC);
539 spin_unlock_irqrestore(&host->lock, flags);
540 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
541 return rc;
542}
543
544int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
545{
546 unsigned long flags;
547 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
548 const u32 *tuning_block_pattern = tuning_block_64;
549 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
550 int rc;
551 struct mmc_host *mmc = host->mmc;
552
553 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
554 /* Tuning is only required for SDR104 modes */
555 spin_lock_irqsave(&host->lock, flags);
556
557 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
558 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
559 tuning_block_pattern = tuning_block_128;
560 size = sizeof(tuning_block_128);
561 }
562 spin_unlock_irqrestore(&host->lock, flags);
563
564 /* first of all reset the tuning block */
565 rc = msm_init_cm_dll(host);
566 if (rc)
567 goto out;
568
569 data_buf = kmalloc(size, GFP_KERNEL);
570 if (!data_buf) {
571 rc = -ENOMEM;
572 goto out;
573 }
574
575 phase = 0;
576 do {
577 struct mmc_command cmd = {0};
578 struct mmc_data data = {0};
579 struct mmc_request mrq = {
580 .cmd = &cmd,
581 .data = &data
582 };
583 struct scatterlist sg;
584
585 /* set the phase in delay line hw block */
586 rc = msm_config_cm_dll_phase(host, phase);
587 if (rc)
588 goto kfree;
589
590 cmd.opcode = opcode;
591 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
592
593 data.blksz = size;
594 data.blocks = 1;
595 data.flags = MMC_DATA_READ;
596 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
597
598 data.sg = &sg;
599 data.sg_len = 1;
600 sg_init_one(&sg, data_buf, size);
601 memset(data_buf, 0, size);
602 mmc_wait_for_req(mmc, &mrq);
603
604 if (!cmd.error && !data.error &&
605 !memcmp(data_buf, tuning_block_pattern, size)) {
606 /* tuning is successful at this tuning point */
607 tuned_phases[tuned_phase_cnt++] = phase;
608 pr_debug("%s: %s: found good phase = %d\n",
609 mmc_hostname(mmc), __func__, phase);
610 }
611 } while (++phase < 16);
612
613 if (tuned_phase_cnt) {
614 rc = msm_find_most_appropriate_phase(host, tuned_phases,
615 tuned_phase_cnt);
616 if (rc < 0)
617 goto kfree;
618 else
619 phase = (u8)rc;
620
621 /*
622 * Finally set the selected phase in delay
623 * line hw block.
624 */
625 rc = msm_config_cm_dll_phase(host, phase);
626 if (rc)
627 goto kfree;
628 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
629 mmc_hostname(mmc), __func__, phase);
630 } else {
631 /* tuning failed */
632 pr_err("%s: %s: no tuning point found\n",
633 mmc_hostname(mmc), __func__);
634 rc = -EAGAIN;
635 }
636
637kfree:
638 kfree(data_buf);
639out:
640 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
641 return rc;
642}
643
Asutosh Das33a4ff52012-12-18 16:14:02 +0530644static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
645{
646 struct sdhci_msm_gpio_data *curr;
647 int i, ret = 0;
648
649 curr = pdata->pin_data->gpio_data;
650 for (i = 0; i < curr->size; i++) {
651 if (!gpio_is_valid(curr->gpio[i].no)) {
652 ret = -EINVAL;
653 pr_err("%s: Invalid gpio = %d\n", __func__,
654 curr->gpio[i].no);
655 goto free_gpios;
656 }
657 if (enable) {
658 ret = gpio_request(curr->gpio[i].no,
659 curr->gpio[i].name);
660 if (ret) {
661 pr_err("%s: gpio_request(%d, %s) failed %d\n",
662 __func__, curr->gpio[i].no,
663 curr->gpio[i].name, ret);
664 goto free_gpios;
665 }
666 curr->gpio[i].is_enabled = true;
667 } else {
668 gpio_free(curr->gpio[i].no);
669 curr->gpio[i].is_enabled = false;
670 }
671 }
672 return ret;
673
674free_gpios:
675 for (i--; i >= 0; i--) {
676 gpio_free(curr->gpio[i].no);
677 curr->gpio[i].is_enabled = false;
678 }
679 return ret;
680}
681
Asutosh Das390519d2012-12-21 12:21:42 +0530682static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
683{
684 struct sdhci_msm_pad_data *curr;
685 int i;
686
687 curr = pdata->pin_data->pad_data;
688 for (i = 0; i < curr->drv->size; i++) {
689 if (enable)
690 msm_tlmm_set_hdrive(curr->drv->on[i].no,
691 curr->drv->on[i].val);
692 else
693 msm_tlmm_set_hdrive(curr->drv->off[i].no,
694 curr->drv->off[i].val);
695 }
696
697 for (i = 0; i < curr->pull->size; i++) {
698 if (enable)
699 msm_tlmm_set_pull(curr->pull->on[i].no,
700 curr->pull->on[i].val);
701 else
702 msm_tlmm_set_pull(curr->pull->off[i].no,
703 curr->pull->off[i].val);
704 }
705
706 return 0;
707}
708
Asutosh Das33a4ff52012-12-18 16:14:02 +0530709static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
710{
711 int ret = 0;
712
713 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
714 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530715 if (pdata->pin_data->is_gpio)
716 ret = sdhci_msm_setup_gpio(pdata, enable);
717 else
718 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530719
Asutosh Das33a4ff52012-12-18 16:14:02 +0530720 if (!ret)
721 pdata->pin_data->cfg_sts = enable;
722
723 return ret;
724}
725
Asutosh Das390519d2012-12-21 12:21:42 +0530726static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
727 u32 **out, int *len, u32 size)
728{
729 int ret = 0;
730 struct device_node *np = dev->of_node;
731 size_t sz;
732 u32 *arr = NULL;
733
734 if (!of_get_property(np, prop_name, len)) {
735 ret = -EINVAL;
736 goto out;
737 }
738 sz = *len = *len / sizeof(*arr);
739 if (sz <= 0 || (size > 0 && (sz != size))) {
740 dev_err(dev, "%s invalid size\n", prop_name);
741 ret = -EINVAL;
742 goto out;
743 }
744
745 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
746 if (!arr) {
747 dev_err(dev, "%s failed allocating memory\n", prop_name);
748 ret = -ENOMEM;
749 goto out;
750 }
751
752 ret = of_property_read_u32_array(np, prop_name, arr, sz);
753 if (ret < 0) {
754 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
755 goto out;
756 }
757 *out = arr;
758out:
759 if (ret)
760 *len = 0;
761 return ret;
762}
763
Asutosh Das33a4ff52012-12-18 16:14:02 +0530764#define MAX_PROP_SIZE 32
765static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
766 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
767{
768 int len, ret = 0;
769 const __be32 *prop;
770 char prop_name[MAX_PROP_SIZE];
771 struct sdhci_msm_reg_data *vreg;
772 struct device_node *np = dev->of_node;
773
774 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
775 if (!of_parse_phandle(np, prop_name, 0)) {
776 dev_err(dev, "No vreg data found for %s\n", vreg_name);
777 ret = -EINVAL;
778 return ret;
779 }
780
781 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
782 if (!vreg) {
783 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
784 ret = -ENOMEM;
785 return ret;
786 }
787
788 vreg->name = vreg_name;
789
790 snprintf(prop_name, MAX_PROP_SIZE,
791 "qcom,%s-always-on", vreg_name);
792 if (of_get_property(np, prop_name, NULL))
793 vreg->is_always_on = true;
794
795 snprintf(prop_name, MAX_PROP_SIZE,
796 "qcom,%s-lpm-sup", vreg_name);
797 if (of_get_property(np, prop_name, NULL))
798 vreg->lpm_sup = true;
799
800 snprintf(prop_name, MAX_PROP_SIZE,
801 "qcom,%s-voltage-level", vreg_name);
802 prop = of_get_property(np, prop_name, &len);
803 if (!prop || (len != (2 * sizeof(__be32)))) {
804 dev_warn(dev, "%s %s property\n",
805 prop ? "invalid format" : "no", prop_name);
806 } else {
807 vreg->low_vol_level = be32_to_cpup(&prop[0]);
808 vreg->high_vol_level = be32_to_cpup(&prop[1]);
809 }
810
811 snprintf(prop_name, MAX_PROP_SIZE,
812 "qcom,%s-current-level", vreg_name);
813 prop = of_get_property(np, prop_name, &len);
814 if (!prop || (len != (2 * sizeof(__be32)))) {
815 dev_warn(dev, "%s %s property\n",
816 prop ? "invalid format" : "no", prop_name);
817 } else {
818 vreg->lpm_uA = be32_to_cpup(&prop[0]);
819 vreg->hpm_uA = be32_to_cpup(&prop[1]);
820 }
821
822 *vreg_data = vreg;
823 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
824 vreg->name, vreg->is_always_on ? "always_on," : "",
825 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
826 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
827
828 return ret;
829}
830
Asutosh Das390519d2012-12-21 12:21:42 +0530831/* GPIO/Pad data extraction */
832static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
833 struct sdhci_msm_pad_pull_data **pad_pull_data)
834{
835 int ret = 0, base = 0, len, i;
836 u32 *tmp;
837 struct sdhci_msm_pad_pull_data *pull_data;
838 struct sdhci_msm_pad_pull *pull;
839
840 switch (id) {
841 case 1:
842 base = TLMM_PULL_SDC1_CLK;
843 break;
844 case 2:
845 base = TLMM_PULL_SDC2_CLK;
846 break;
847 case 3:
848 base = TLMM_PULL_SDC3_CLK;
849 break;
850 case 4:
851 base = TLMM_PULL_SDC4_CLK;
852 break;
853 default:
854 dev_err(dev, "%s: Invalid slot id\n", __func__);
855 ret = -EINVAL;
856 goto out;
857 }
858
859 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
860 GFP_KERNEL);
861 if (!pull_data) {
862 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
863 ret = -ENOMEM;
864 goto out;
865 }
866 pull_data->size = 3; /* array size for clk, cmd, data */
867
868 /* Allocate on, off configs for clk, cmd, data */
869 pull = devm_kzalloc(dev, 2 * pull_data->size *\
870 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
871 if (!pull) {
872 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
873 ret = -ENOMEM;
874 goto out;
875 }
876 pull_data->on = pull;
877 pull_data->off = pull + pull_data->size;
878
879 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
880 &tmp, &len, pull_data->size);
881 if (ret)
882 goto out;
883
884 for (i = 0; i < len; i++) {
885 pull_data->on[i].no = base + i;
886 pull_data->on[i].val = tmp[i];
887 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
888 i, pull_data->on[i].val);
889 }
890
891 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
892 &tmp, &len, pull_data->size);
893 if (ret)
894 goto out;
895
896 for (i = 0; i < len; i++) {
897 pull_data->off[i].no = base + i;
898 pull_data->off[i].val = tmp[i];
899 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
900 i, pull_data->off[i].val);
901 }
902
903 *pad_pull_data = pull_data;
904out:
905 return ret;
906}
907
908static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
909 struct sdhci_msm_pad_drv_data **pad_drv_data)
910{
911 int ret = 0, base = 0, len, i;
912 u32 *tmp;
913 struct sdhci_msm_pad_drv_data *drv_data;
914 struct sdhci_msm_pad_drv *drv;
915
916 switch (id) {
917 case 1:
918 base = TLMM_HDRV_SDC1_CLK;
919 break;
920 case 2:
921 base = TLMM_HDRV_SDC2_CLK;
922 break;
923 case 3:
924 base = TLMM_HDRV_SDC3_CLK;
925 break;
926 case 4:
927 base = TLMM_HDRV_SDC4_CLK;
928 break;
929 default:
930 dev_err(dev, "%s: Invalid slot id\n", __func__);
931 ret = -EINVAL;
932 goto out;
933 }
934
935 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
936 GFP_KERNEL);
937 if (!drv_data) {
938 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
939 ret = -ENOMEM;
940 goto out;
941 }
942 drv_data->size = 3; /* array size for clk, cmd, data */
943
944 /* Allocate on, off configs for clk, cmd, data */
945 drv = devm_kzalloc(dev, 2 * drv_data->size *\
946 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
947 if (!drv) {
948 dev_err(dev, "No memory msm_mmc_pad_drv\n");
949 ret = -ENOMEM;
950 goto out;
951 }
952 drv_data->on = drv;
953 drv_data->off = drv + drv_data->size;
954
955 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
956 &tmp, &len, drv_data->size);
957 if (ret)
958 goto out;
959
960 for (i = 0; i < len; i++) {
961 drv_data->on[i].no = base + i;
962 drv_data->on[i].val = tmp[i];
963 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
964 i, drv_data->on[i].val);
965 }
966
967 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
968 &tmp, &len, drv_data->size);
969 if (ret)
970 goto out;
971
972 for (i = 0; i < len; i++) {
973 drv_data->off[i].no = base + i;
974 drv_data->off[i].val = tmp[i];
975 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
976 i, drv_data->off[i].val);
977 }
978
979 *pad_drv_data = drv_data;
980out:
981 return ret;
982}
983
Asutosh Das33a4ff52012-12-18 16:14:02 +0530984#define GPIO_NAME_MAX_LEN 32
985static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
986 struct sdhci_msm_pltfm_data *pdata)
987{
Asutosh Das390519d2012-12-21 12:21:42 +0530988 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530989 struct sdhci_msm_pin_data *pin_data;
990 struct device_node *np = dev->of_node;
991
992 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
993 if (!pin_data) {
994 dev_err(dev, "No memory for pin_data\n");
995 ret = -ENOMEM;
996 goto out;
997 }
998
999 cnt = of_gpio_count(np);
1000 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301001 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301002 pin_data->gpio_data = devm_kzalloc(dev,
1003 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1004 if (!pin_data->gpio_data) {
1005 dev_err(dev, "No memory for gpio_data\n");
1006 ret = -ENOMEM;
1007 goto out;
1008 }
1009 pin_data->gpio_data->size = cnt;
1010 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1011 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1012
1013 if (!pin_data->gpio_data->gpio) {
1014 dev_err(dev, "No memory for gpio\n");
1015 ret = -ENOMEM;
1016 goto out;
1017 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301018 for (i = 0; i < cnt; i++) {
1019 const char *name = NULL;
1020 char result[GPIO_NAME_MAX_LEN];
1021 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1022 of_property_read_string_index(np,
1023 "qcom,gpio-names", i, &name);
1024
1025 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1026 dev_name(dev), name ? name : "?");
1027 pin_data->gpio_data->gpio[i].name = result;
1028 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301029 pin_data->gpio_data->gpio[i].name,
1030 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301031 }
Asutosh Das390519d2012-12-21 12:21:42 +05301032 } else {
1033 pin_data->pad_data =
1034 devm_kzalloc(dev,
1035 sizeof(struct sdhci_msm_pad_data),
1036 GFP_KERNEL);
1037 if (!pin_data->pad_data) {
1038 dev_err(dev,
1039 "No memory for pin_data->pad_data\n");
1040 ret = -ENOMEM;
1041 goto out;
1042 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301043
Asutosh Das390519d2012-12-21 12:21:42 +05301044 ret = of_alias_get_id(np, "sdhc");
1045 if (ret < 0) {
1046 dev_err(dev, "Failed to get slot index %d\n", ret);
1047 goto out;
1048 }
1049 id = ret;
1050
1051 ret = sdhci_msm_dt_get_pad_pull_info(
1052 dev, id, &pin_data->pad_data->pull);
1053 if (ret)
1054 goto out;
1055 ret = sdhci_msm_dt_get_pad_drv_info(
1056 dev, id, &pin_data->pad_data->drv);
1057 if (ret)
1058 goto out;
1059
1060 }
1061 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301062out:
1063 if (ret)
1064 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1065 return ret;
1066}
1067
1068/* Parse platform data */
1069static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1070{
1071 struct sdhci_msm_pltfm_data *pdata = NULL;
1072 struct device_node *np = dev->of_node;
1073 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301074 u32 cpu_dma_latency;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301075 int len, i;
1076
1077 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1078 if (!pdata) {
1079 dev_err(dev, "failed to allocate memory for platform data\n");
1080 goto out;
1081 }
1082
Sahitya Tummala62448d92013-03-12 14:57:46 +05301083 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, 0);
1084
Asutosh Das33a4ff52012-12-18 16:14:02 +05301085 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1086 if (bus_width == 8)
1087 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1088 else if (bus_width == 4)
1089 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1090 else {
1091 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1092 pdata->mmc_bus_width = 0;
1093 }
1094
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301095 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1096 &cpu_dma_latency))
1097 pdata->cpu_dma_latency_us = cpu_dma_latency;
1098
Asutosh Das33a4ff52012-12-18 16:14:02 +05301099 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1100 sdhci_msm_slot_reg_data),
1101 GFP_KERNEL);
1102 if (!pdata->vreg_data) {
1103 dev_err(dev, "failed to allocate memory for vreg data\n");
1104 goto out;
1105 }
1106
1107 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1108 "vdd")) {
1109 dev_err(dev, "failed parsing vdd data\n");
1110 goto out;
1111 }
1112 if (sdhci_msm_dt_parse_vreg_info(dev,
1113 &pdata->vreg_data->vdd_io_data,
1114 "vdd-io")) {
1115 dev_err(dev, "failed parsing vdd-io data\n");
1116 goto out;
1117 }
1118
1119 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1120 dev_err(dev, "failed parsing gpio data\n");
1121 goto out;
1122 }
1123
1124 of_property_read_u32(np, "qcom,max-clk-rate", &pdata->max_clk);
1125
1126 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1127
1128 for (i = 0; i < len; i++) {
1129 const char *name = NULL;
1130
1131 of_property_read_string_index(np,
1132 "qcom,bus-speed-mode", i, &name);
1133 if (!name)
1134 continue;
1135
1136 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
1137 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1138 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1139 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1140 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1141 pdata->caps |= MMC_CAP_1_8V_DDR
1142 | MMC_CAP_UHS_DDR50;
1143 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1144 pdata->caps |= MMC_CAP_1_2V_DDR
1145 | MMC_CAP_UHS_DDR50;
1146 }
1147
1148 if (of_get_property(np, "qcom,nonremovable", NULL))
1149 pdata->nonremovable = true;
1150
1151 return pdata;
1152out:
1153 return NULL;
1154}
1155
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301156/* Returns required bandwidth in Bytes per Sec */
1157static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1158 struct mmc_ios *ios)
1159{
1160 unsigned int bw;
1161
1162 bw = host->clock;
1163 /*
1164 * For DDR mode, SDCC controller clock will be at
1165 * the double rate than the actual clock that goes to card.
1166 */
1167 if (ios->bus_width == MMC_BUS_WIDTH_4)
1168 bw /= 2;
1169 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1170 bw /= 8;
1171
1172 return bw;
1173}
1174
1175static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1176 unsigned int bw)
1177{
1178 unsigned int *table = host->pdata->voting_data->bw_vecs;
1179 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1180 int i;
1181
1182 if (host->msm_bus_vote.is_max_bw_needed && bw)
1183 return host->msm_bus_vote.max_bw_vote;
1184
1185 for (i = 0; i < size; i++) {
1186 if (bw <= table[i])
1187 break;
1188 }
1189
1190 if (i && (i == size))
1191 i--;
1192
1193 return i;
1194}
1195
1196/*
1197 * This function must be called with host lock acquired.
1198 * Caller of this function should also ensure that msm bus client
1199 * handle is not null.
1200 */
1201static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1202 int vote,
1203 unsigned long flags)
1204{
1205 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1206 int rc = 0;
1207
1208 if (vote != msm_host->msm_bus_vote.curr_vote) {
1209 spin_unlock_irqrestore(&host->lock, flags);
1210 rc = msm_bus_scale_client_update_request(
1211 msm_host->msm_bus_vote.client_handle, vote);
1212 spin_lock_irqsave(&host->lock, flags);
1213 if (rc) {
1214 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1215 mmc_hostname(host->mmc),
1216 msm_host->msm_bus_vote.client_handle, vote, rc);
1217 goto out;
1218 }
1219 msm_host->msm_bus_vote.curr_vote = vote;
1220 }
1221out:
1222 return rc;
1223}
1224
1225/*
1226 * Internal work. Work to set 0 bandwidth for msm bus.
1227 */
1228static void sdhci_msm_bus_work(struct work_struct *work)
1229{
1230 struct sdhci_msm_host *msm_host;
1231 struct sdhci_host *host;
1232 unsigned long flags;
1233
1234 msm_host = container_of(work, struct sdhci_msm_host,
1235 msm_bus_vote.vote_work.work);
1236 host = platform_get_drvdata(msm_host->pdev);
1237
1238 if (!msm_host->msm_bus_vote.client_handle)
1239 return;
1240
1241 spin_lock_irqsave(&host->lock, flags);
1242 /* don't vote for 0 bandwidth if any request is in progress */
1243 if (!host->mrq) {
1244 sdhci_msm_bus_set_vote(msm_host,
1245 msm_host->msm_bus_vote.min_bw_vote, flags);
1246 } else
1247 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1248 mmc_hostname(host->mmc), __func__);
1249 spin_unlock_irqrestore(&host->lock, flags);
1250}
1251
1252/*
1253 * This function cancels any scheduled delayed work and sets the bus
1254 * vote based on bw (bandwidth) argument.
1255 */
1256static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1257 unsigned int bw)
1258{
1259 int vote;
1260 unsigned long flags;
1261 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1262 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1263
1264 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1265 spin_lock_irqsave(&host->lock, flags);
1266 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1267 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1268 spin_unlock_irqrestore(&host->lock, flags);
1269}
1270
1271#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1272
1273/* This function queues a work which will set the bandwidth requiement to 0 */
1274static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1275{
1276 unsigned long flags;
1277 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1278 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1279
1280 spin_lock_irqsave(&host->lock, flags);
1281 if (msm_host->msm_bus_vote.min_bw_vote !=
1282 msm_host->msm_bus_vote.curr_vote)
1283 queue_delayed_work(system_nrt_wq,
1284 &msm_host->msm_bus_vote.vote_work,
1285 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1286 spin_unlock_irqrestore(&host->lock, flags);
1287}
1288
1289static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1290 struct platform_device *pdev)
1291{
1292 int rc = 0;
1293 struct msm_bus_scale_pdata *bus_pdata;
1294
1295 struct sdhci_msm_bus_voting_data *data;
1296 struct device *dev = &pdev->dev;
1297
1298 data = devm_kzalloc(dev,
1299 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1300 if (!data) {
1301 dev_err(&pdev->dev,
1302 "%s: failed to allocate memory\n", __func__);
1303 rc = -ENOMEM;
1304 goto out;
1305 }
1306 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1307 if (data->bus_pdata) {
1308 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1309 &data->bw_vecs, &data->bw_vecs_size, 0);
1310 if (rc) {
1311 dev_err(&pdev->dev,
1312 "%s: Failed to get bus-bw-vectors-bps\n",
1313 __func__);
1314 goto out;
1315 }
1316 host->pdata->voting_data = data;
1317 }
1318 if (host->pdata->voting_data &&
1319 host->pdata->voting_data->bus_pdata &&
1320 host->pdata->voting_data->bw_vecs &&
1321 host->pdata->voting_data->bw_vecs_size) {
1322
1323 bus_pdata = host->pdata->voting_data->bus_pdata;
1324 host->msm_bus_vote.client_handle =
1325 msm_bus_scale_register_client(bus_pdata);
1326 if (!host->msm_bus_vote.client_handle) {
1327 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1328 rc = -EFAULT;
1329 goto out;
1330 }
1331 /* cache the vote index for minimum and maximum bandwidth */
1332 host->msm_bus_vote.min_bw_vote =
1333 sdhci_msm_bus_get_vote_for_bw(host, 0);
1334 host->msm_bus_vote.max_bw_vote =
1335 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1336 } else {
1337 devm_kfree(dev, data);
1338 }
1339
1340out:
1341 return rc;
1342}
1343
1344static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1345{
1346 if (host->msm_bus_vote.client_handle)
1347 msm_bus_scale_unregister_client(
1348 host->msm_bus_vote.client_handle);
1349}
1350
1351static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1352{
1353 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1354 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1355 struct mmc_ios *ios = &host->mmc->ios;
1356 unsigned int bw;
1357
1358 if (!msm_host->msm_bus_vote.client_handle)
1359 return;
1360
1361 bw = sdhci_get_bw_required(host, ios);
1362 if (enable)
1363 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
1364 else
1365 sdhci_msm_bus_queue_work(host);
1366}
1367
Asutosh Das33a4ff52012-12-18 16:14:02 +05301368/* Regulator utility functions */
1369static int sdhci_msm_vreg_init_reg(struct device *dev,
1370 struct sdhci_msm_reg_data *vreg)
1371{
1372 int ret = 0;
1373
1374 /* check if regulator is already initialized? */
1375 if (vreg->reg)
1376 goto out;
1377
1378 /* Get the regulator handle */
1379 vreg->reg = devm_regulator_get(dev, vreg->name);
1380 if (IS_ERR(vreg->reg)) {
1381 ret = PTR_ERR(vreg->reg);
1382 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1383 __func__, vreg->name, ret);
1384 goto out;
1385 }
1386
1387 /* sanity check */
1388 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1389 pr_err("%s: %s invalid constraints specified\n",
1390 __func__, vreg->name);
1391 ret = -EINVAL;
1392 }
1393
1394out:
1395 return ret;
1396}
1397
1398static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1399{
1400 if (vreg->reg)
1401 devm_regulator_put(vreg->reg);
1402}
1403
1404static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1405 *vreg, int uA_load)
1406{
1407 int ret = 0;
1408
1409 /*
1410 * regulators that do not support regulator_set_voltage also
1411 * do not support regulator_set_optimum_mode
1412 */
1413 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1414 if (ret < 0)
1415 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
1416 __func__, vreg->name, uA_load, ret);
1417 else
1418 /*
1419 * regulator_set_optimum_mode() can return non zero
1420 * value even for success case.
1421 */
1422 ret = 0;
1423 return ret;
1424}
1425
1426static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1427 int min_uV, int max_uV)
1428{
1429 int ret = 0;
1430
1431 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1432 if (ret) {
1433 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1434 __func__, vreg->name, min_uV, max_uV, ret);
1435 }
1436
1437 return ret;
1438}
1439
1440static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1441{
1442 int ret = 0;
1443
1444 /* Put regulator in HPM (high power mode) */
1445 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1446 if (ret < 0)
1447 return ret;
1448
1449 if (!vreg->is_enabled) {
1450 /* Set voltage level */
1451 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1452 vreg->high_vol_level);
1453 if (ret)
1454 return ret;
1455 }
1456 ret = regulator_enable(vreg->reg);
1457 if (ret) {
1458 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1459 __func__, vreg->name, ret);
1460 return ret;
1461 }
1462 vreg->is_enabled = true;
1463 return ret;
1464}
1465
1466static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1467{
1468 int ret = 0;
1469
1470 /* Never disable regulator marked as always_on */
1471 if (vreg->is_enabled && !vreg->is_always_on) {
1472 ret = regulator_disable(vreg->reg);
1473 if (ret) {
1474 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1475 __func__, vreg->name, ret);
1476 goto out;
1477 }
1478 vreg->is_enabled = false;
1479
1480 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1481 if (ret < 0)
1482 goto out;
1483
1484 /* Set min. voltage level to 0 */
1485 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1486 if (ret)
1487 goto out;
1488 } else if (vreg->is_enabled && vreg->is_always_on) {
1489 if (vreg->lpm_sup) {
1490 /* Put always_on regulator in LPM (low power mode) */
1491 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1492 vreg->lpm_uA);
1493 if (ret < 0)
1494 goto out;
1495 }
1496 }
1497out:
1498 return ret;
1499}
1500
1501static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1502 bool enable, bool is_init)
1503{
1504 int ret = 0, i;
1505 struct sdhci_msm_slot_reg_data *curr_slot;
1506 struct sdhci_msm_reg_data *vreg_table[2];
1507
1508 curr_slot = pdata->vreg_data;
1509 if (!curr_slot) {
1510 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1511 __func__);
1512 goto out;
1513 }
1514
1515 vreg_table[0] = curr_slot->vdd_data;
1516 vreg_table[1] = curr_slot->vdd_io_data;
1517
1518 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1519 if (vreg_table[i]) {
1520 if (enable)
1521 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1522 else
1523 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1524 if (ret)
1525 goto out;
1526 }
1527 }
1528out:
1529 return ret;
1530}
1531
1532/*
1533 * Reset vreg by ensuring it is off during probe. A call
1534 * to enable vreg is needed to balance disable vreg
1535 */
1536static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1537{
1538 int ret;
1539
1540 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1541 if (ret)
1542 return ret;
1543 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1544 return ret;
1545}
1546
1547/* This init function should be called only once for each SDHC slot */
1548static int sdhci_msm_vreg_init(struct device *dev,
1549 struct sdhci_msm_pltfm_data *pdata,
1550 bool is_init)
1551{
1552 int ret = 0;
1553 struct sdhci_msm_slot_reg_data *curr_slot;
1554 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1555
1556 curr_slot = pdata->vreg_data;
1557 if (!curr_slot)
1558 goto out;
1559
1560 curr_vdd_reg = curr_slot->vdd_data;
1561 curr_vdd_io_reg = curr_slot->vdd_io_data;
1562
1563 if (!is_init)
1564 /* Deregister all regulators from regulator framework */
1565 goto vdd_io_reg_deinit;
1566
1567 /*
1568 * Get the regulator handle from voltage regulator framework
1569 * and then try to set the voltage level for the regulator
1570 */
1571 if (curr_vdd_reg) {
1572 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1573 if (ret)
1574 goto out;
1575 }
1576 if (curr_vdd_io_reg) {
1577 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1578 if (ret)
1579 goto vdd_reg_deinit;
1580 }
1581 ret = sdhci_msm_vreg_reset(pdata);
1582 if (ret)
1583 dev_err(dev, "vreg reset failed (%d)\n", ret);
1584 goto out;
1585
1586vdd_io_reg_deinit:
1587 if (curr_vdd_io_reg)
1588 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1589vdd_reg_deinit:
1590 if (curr_vdd_reg)
1591 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1592out:
1593 return ret;
1594}
1595
1596
1597static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1598 enum vdd_io_level level,
1599 unsigned int voltage_level)
1600{
1601 int ret = 0;
1602 int set_level;
1603 struct sdhci_msm_reg_data *vdd_io_reg;
1604
1605 if (!pdata->vreg_data)
1606 return ret;
1607
1608 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1609 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1610 switch (level) {
1611 case VDD_IO_LOW:
1612 set_level = vdd_io_reg->low_vol_level;
1613 break;
1614 case VDD_IO_HIGH:
1615 set_level = vdd_io_reg->high_vol_level;
1616 break;
1617 case VDD_IO_SET_LEVEL:
1618 set_level = voltage_level;
1619 break;
1620 default:
1621 pr_err("%s: invalid argument level = %d",
1622 __func__, level);
1623 ret = -EINVAL;
1624 return ret;
1625 }
1626 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1627 set_level);
1628 }
1629 return ret;
1630}
1631
1632static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1633{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001634 struct sdhci_host *host = (struct sdhci_host *)data;
1635 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1636 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301637 u8 irq_status = 0;
1638 u8 irq_ack = 0;
1639 int ret = 0;
1640
1641 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1642 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1643 mmc_hostname(msm_host->mmc), irq, irq_status);
1644
1645 /* Clear the interrupt */
1646 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1647 /*
1648 * SDHC has core_mem and hc_mem device memory and these memory
1649 * addresses do not fall within 1KB region. Hence, any update to
1650 * core_mem address space would require an mb() to ensure this gets
1651 * completed before its next update to registers within hc_mem.
1652 */
1653 mb();
1654
1655 /* Handle BUS ON/OFF*/
1656 if (irq_status & CORE_PWRCTL_BUS_ON) {
1657 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
1658 if (!ret)
1659 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
1660 if (ret)
1661 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1662 else
1663 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1664 }
1665 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1666 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
1667 if (!ret)
1668 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
1669 if (ret)
1670 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1671 else
1672 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1673 }
1674 /* Handle IO LOW/HIGH */
1675 if (irq_status & CORE_PWRCTL_IO_LOW) {
1676 /* Switch voltage Low */
1677 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1678 if (ret)
1679 irq_ack |= CORE_PWRCTL_IO_FAIL;
1680 else
1681 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1682 }
1683 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1684 /* Switch voltage High */
1685 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1686 if (ret)
1687 irq_ack |= CORE_PWRCTL_IO_FAIL;
1688 else
1689 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1690 }
1691
1692 /* ACK status to the core */
1693 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1694 /*
1695 * SDHC has core_mem and hc_mem device memory and these memory
1696 * addresses do not fall within 1KB region. Hence, any update to
1697 * core_mem address space would require an mb() to ensure this gets
1698 * completed before its next update to registers within hc_mem.
1699 */
1700 mb();
1701
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001702 if (irq_status & CORE_PWRCTL_IO_HIGH)
1703 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1704 ~CORE_IO_PAD_PWR_SWITCH),
1705 host->ioaddr + CORE_VENDOR_SPEC);
1706 if (irq_status & CORE_PWRCTL_IO_LOW)
1707 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1708 CORE_IO_PAD_PWR_SWITCH),
1709 host->ioaddr + CORE_VENDOR_SPEC);
1710 mb();
1711
Asutosh Das33a4ff52012-12-18 16:14:02 +05301712 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1713 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
1714 wake_up_interruptible(&msm_host->pwr_irq_wait);
1715 return IRQ_HANDLED;
1716}
1717
1718/* This function returns the max. current supported by VDD rail in mA */
1719static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
1720 *host)
1721{
1722 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
1723 if (!curr_slot)
1724 return 0;
1725 if (curr_slot->vdd_data)
1726 return curr_slot->vdd_data->hpm_uA / 1000;
1727 else
1728 return 0;
1729}
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301730static ssize_t
1731show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1732 char *buf)
1733{
1734 struct sdhci_host *host = dev_get_drvdata(dev);
1735 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1736 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1737
1738 return snprintf(buf, PAGE_SIZE, "%u\n",
1739 msm_host->msm_bus_vote.is_max_bw_needed);
1740}
1741
1742static ssize_t
1743store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1744 const char *buf, size_t count)
1745{
1746 struct sdhci_host *host = dev_get_drvdata(dev);
1747 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1748 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1749 uint32_t value;
1750 unsigned long flags;
1751
1752 if (!kstrtou32(buf, 0, &value)) {
1753 spin_lock_irqsave(&host->lock, flags);
1754 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1755 spin_unlock_irqrestore(&host->lock, flags);
1756 }
1757 return count;
1758}
Asutosh Das33a4ff52012-12-18 16:14:02 +05301759
1760static void sdhci_msm_check_power_status(struct sdhci_host *host)
1761{
1762 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1763 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1764 int ret = 0;
1765
1766 pr_debug("%s: %s: power status before waiting 0x%x\n",
1767 mmc_hostname(host->mmc), __func__,
1768 readb_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
1769
1770 ret = wait_event_interruptible(msm_host->pwr_irq_wait,
1771 (readb_relaxed(msm_host->core_mem +
1772 CORE_PWRCTL_CTL)) != 0x0);
1773 if (ret)
1774 pr_warning("%s: %s: returned due to error %d\n",
1775 mmc_hostname(host->mmc), __func__, ret);
1776 pr_debug("%s: %s: ret %d power status after handling power IRQ 0x%x\n",
1777 mmc_hostname(host->mmc), __func__, ret,
1778 readb_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
1779}
1780
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001781static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1782{
1783 if (enable)
1784 writel_relaxed((readl_relaxed(host->ioaddr +
1785 CORE_DLL_CONFIG) | CORE_CDR_EN),
1786 host->ioaddr + CORE_DLL_CONFIG);
1787 else
1788 writel_relaxed((readl_relaxed(host->ioaddr +
1789 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1790 host->ioaddr + CORE_DLL_CONFIG);
1791}
1792
Asutosh Das3781bd82013-01-10 21:11:04 +05301793static unsigned int sdhci_msm_max_segs(void)
1794{
1795 return SDHCI_MSM_MAX_SEGMENTS;
1796}
1797
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301798void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1799{
1800 int rc;
1801 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1802 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1803 unsigned long flags;
1804
1805 if (clock && !atomic_read(&msm_host->clks_on)) {
1806 pr_debug("%s: request to enable clock at rate %u\n",
1807 mmc_hostname(host->mmc), clock);
1808 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1809 rc = clk_prepare_enable(msm_host->bus_clk);
1810 if (rc) {
1811 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1812 mmc_hostname(host->mmc), __func__, rc);
1813 goto out;
1814 }
1815 }
1816 if (!IS_ERR(msm_host->pclk)) {
1817 rc = clk_prepare_enable(msm_host->pclk);
1818 if (rc) {
1819 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1820 mmc_hostname(host->mmc), __func__, rc);
1821 goto disable_bus_clk;
1822 }
1823 }
1824 rc = clk_prepare_enable(msm_host->clk);
1825 if (rc) {
1826 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1827 mmc_hostname(host->mmc), __func__, rc);
1828 goto disable_pclk;
1829 }
1830 mb();
1831 atomic_set(&msm_host->clks_on, 1);
1832
1833 } else if (!clock && atomic_read(&msm_host->clks_on)) {
1834 pr_debug("%s: request to disable clocks\n",
1835 mmc_hostname(host->mmc));
1836 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1837 mb();
1838 clk_disable_unprepare(msm_host->clk);
1839 if (!IS_ERR(msm_host->pclk))
1840 clk_disable_unprepare(msm_host->pclk);
1841 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1842 clk_disable_unprepare(msm_host->bus_clk);
1843 atomic_set(&msm_host->clks_on, 0);
1844 }
1845 spin_lock_irqsave(&host->lock, flags);
1846 host->clock = clock;
1847 spin_unlock_irqrestore(&host->lock, flags);
1848 goto out;
1849disable_pclk:
1850 if (!IS_ERR_OR_NULL(msm_host->pclk))
1851 clk_disable_unprepare(msm_host->pclk);
1852disable_bus_clk:
1853 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1854 clk_disable_unprepare(msm_host->bus_clk);
1855out:
1856 return;
1857}
1858
Asutosh Das33a4ff52012-12-18 16:14:02 +05301859static struct sdhci_ops sdhci_msm_ops = {
1860 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001861 .execute_tuning = sdhci_msm_execute_tuning,
1862 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05301863 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301864 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301865 .platform_bus_voting = sdhci_msm_bus_voting,
Asutosh Das33a4ff52012-12-18 16:14:02 +05301866};
1867
1868static int __devinit sdhci_msm_probe(struct platform_device *pdev)
1869{
1870 struct sdhci_host *host;
1871 struct sdhci_pltfm_host *pltfm_host;
1872 struct sdhci_msm_host *msm_host;
1873 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05301874 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301875 u32 vdd_max_current;
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07001876 u32 host_version;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301877
1878 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
1879 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
1880 GFP_KERNEL);
1881 if (!msm_host) {
1882 ret = -ENOMEM;
1883 goto out;
1884 }
1885 init_waitqueue_head(&msm_host->pwr_irq_wait);
1886
1887 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
1888 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
1889 if (IS_ERR(host)) {
1890 ret = PTR_ERR(host);
1891 goto out;
1892 }
1893
1894 pltfm_host = sdhci_priv(host);
1895 pltfm_host->priv = msm_host;
1896 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301897 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301898
1899 /* Extract platform data */
1900 if (pdev->dev.of_node) {
1901 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
1902 if (!msm_host->pdata) {
1903 dev_err(&pdev->dev, "DT parsing error\n");
1904 goto pltfm_free;
1905 }
1906 } else {
1907 dev_err(&pdev->dev, "No device tree node\n");
1908 goto pltfm_free;
1909 }
1910
1911 /* Setup Clocks */
1912
1913 /* Setup SDCC bus voter clock. */
1914 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
1915 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1916 /* Vote for max. clk rate for max. performance */
1917 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
1918 if (ret)
1919 goto pltfm_free;
1920 ret = clk_prepare_enable(msm_host->bus_clk);
1921 if (ret)
1922 goto pltfm_free;
1923 }
1924
1925 /* Setup main peripheral bus clock */
1926 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
1927 if (!IS_ERR(msm_host->pclk)) {
1928 ret = clk_prepare_enable(msm_host->pclk);
1929 if (ret)
1930 goto bus_clk_disable;
1931 }
1932
1933 /* Setup SDC MMC clock */
1934 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
1935 if (IS_ERR(msm_host->clk)) {
1936 ret = PTR_ERR(msm_host->clk);
1937 goto pclk_disable;
1938 }
1939
1940 ret = clk_prepare_enable(msm_host->clk);
1941 if (ret)
1942 goto pclk_disable;
1943
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301944 atomic_set(&msm_host->clks_on, 1);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301945 /* Setup regulators */
1946 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
1947 if (ret) {
1948 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
1949 goto clk_disable;
1950 }
1951
1952 /* Reset the core and Enable SDHC mode */
1953 core_memres = platform_get_resource_byname(pdev,
1954 IORESOURCE_MEM, "core_mem");
1955 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
1956 resource_size(core_memres));
1957
1958 if (!msm_host->core_mem) {
1959 dev_err(&pdev->dev, "Failed to remap registers\n");
1960 ret = -ENOMEM;
1961 goto vreg_deinit;
1962 }
1963
1964 /* Set SW_RST bit in POWER register (Offset 0x0) */
1965 writel_relaxed(CORE_SW_RST, msm_host->core_mem + CORE_POWER);
1966 /* Set HC_MODE_EN bit in HC_MODE register */
1967 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
1968
1969 /*
1970 * Following are the deviations from SDHC spec v3.0 -
1971 * 1. Card detection is handled using separate GPIO.
1972 * 2. Bus power control is handled by interacting with PMIC.
1973 */
1974 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1975 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05301976 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301977
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07001978 host_version = readl_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
1979 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
1980 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
1981 SDHCI_VENDOR_VER_SHIFT));
1982 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
1983 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
1984 /*
1985 * Add 40us delay in interrupt handler when
1986 * operating at initialization frequency(400KHz).
1987 */
1988 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
1989 /*
1990 * Set Software Reset for DAT line in Software
1991 * Reset Register (Bit 2).
1992 */
1993 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
1994 }
1995
1996 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05301997 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
1998 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301999 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302000 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302001 goto vreg_deinit;
2002 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302003 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302004 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002005 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302006 if (ret) {
2007 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302008 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302009 goto vreg_deinit;
2010 }
2011
2012 /* Enable pwr irq interrupts */
2013 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2014
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302015 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2016 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2017
Asutosh Das33a4ff52012-12-18 16:14:02 +05302018 /* Set host capabilities */
2019 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2020 msm_host->mmc->caps |= msm_host->pdata->caps;
2021
2022 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2023 if (vdd_max_current >= 800)
2024 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2025 else if (vdd_max_current >= 600)
2026 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2027 else if (vdd_max_current >= 400)
2028 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2029 else
2030 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2031
2032 if (vdd_max_current > 150)
2033 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2034 MMC_CAP_SET_XPC_300|
2035 MMC_CAP_SET_XPC_330;
2036
Sahitya Tummalacbe1b7a2013-02-28 12:21:58 +05302037 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302038 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302039 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302040 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2041 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2042 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2043 MMC_CAP2_DETECT_ON_ERR);
2044 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2045 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
2046 msm_host->mmc->caps2 |= MMC_CAP2_INIT_BKOPS;
2047 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
2048
2049 if (msm_host->pdata->nonremovable)
2050 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2051
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302052 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2053
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302054 ret = sdhci_msm_bus_register(msm_host, pdev);
2055 if (ret)
2056 goto vreg_deinit;
2057
2058 if (msm_host->msm_bus_vote.client_handle)
2059 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2060 sdhci_msm_bus_work);
2061
Sahitya Tummala62448d92013-03-12 14:57:46 +05302062 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2063 ret = mmc_cd_gpio_request(msm_host->mmc,
2064 msm_host->pdata->status_gpio);
2065 if (ret) {
2066 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2067 __func__, ret);
2068 goto bus_unregister;
2069 }
2070 }
2071
Asutosh Das33a4ff52012-12-18 16:14:02 +05302072 ret = sdhci_add_host(host);
2073 if (ret) {
2074 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302075 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302076 }
2077
2078 /* Set core clk rate, optionally override from dts */
2079 if (msm_host->pdata->max_clk)
2080 host->max_clk = msm_host->pdata->max_clk;
2081 ret = clk_set_rate(msm_host->clk, host->max_clk);
2082 if (ret) {
2083 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
2084 goto remove_host;
2085 }
2086
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302087 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2088 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2089 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2090 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2091 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2092 ret = device_create_file(&pdev->dev,
2093 &msm_host->msm_bus_vote.max_bus_bw);
2094 if (ret)
2095 goto remove_host;
2096
Asutosh Dasbbc84782013-02-11 15:31:35 +05302097 ret = pm_runtime_set_active(&pdev->dev);
2098 if (ret)
2099 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
2100 mmc_hostname(host->mmc), __func__, ret);
2101 else
2102 pm_runtime_enable(&pdev->dev);
2103
Asutosh Das33a4ff52012-12-18 16:14:02 +05302104 /* Successful initialization */
2105 goto out;
2106
2107remove_host:
2108 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2109 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302110free_cd_gpio:
2111 if (gpio_is_valid(msm_host->pdata->status_gpio))
2112 mmc_cd_gpio_free(msm_host->mmc);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302113bus_unregister:
2114 sdhci_msm_bus_unregister(msm_host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302115vreg_deinit:
2116 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
2117clk_disable:
2118 if (!IS_ERR(msm_host->clk))
2119 clk_disable_unprepare(msm_host->clk);
2120pclk_disable:
2121 if (!IS_ERR(msm_host->pclk))
2122 clk_disable_unprepare(msm_host->pclk);
2123bus_clk_disable:
2124 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2125 clk_disable_unprepare(msm_host->bus_clk);
2126pltfm_free:
2127 sdhci_pltfm_free(pdev);
2128out:
2129 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2130 return ret;
2131}
2132
2133static int __devexit sdhci_msm_remove(struct platform_device *pdev)
2134{
2135 struct sdhci_host *host = platform_get_drvdata(pdev);
2136 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2137 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2138 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2139 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2140 0xffffffff);
2141
2142 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302143 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302144 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302145 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302146 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302147
2148 if (gpio_is_valid(msm_host->pdata->status_gpio))
2149 mmc_cd_gpio_free(msm_host->mmc);
2150
Asutosh Das33a4ff52012-12-18 16:14:02 +05302151 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302152
Asutosh Das33a4ff52012-12-18 16:14:02 +05302153 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05302154 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302155
2156 if (msm_host->msm_bus_vote.client_handle) {
2157 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2158 sdhci_msm_bus_unregister(msm_host);
2159 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302160 return 0;
2161}
2162
Asutosh Dasbbc84782013-02-11 15:31:35 +05302163static int sdhci_msm_runtime_suspend(struct device *dev)
2164{
2165 struct sdhci_host *host = dev_get_drvdata(dev);
2166 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2167 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2168
2169 disable_irq(host->irq);
2170 disable_irq(msm_host->pwr_irq);
2171
2172 return 0;
2173}
2174
2175static int sdhci_msm_runtime_resume(struct device *dev)
2176{
2177 struct sdhci_host *host = dev_get_drvdata(dev);
2178 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2179 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2180
2181 enable_irq(msm_host->pwr_irq);
2182 enable_irq(host->irq);
2183
2184 return 0;
2185}
2186
2187#ifdef CONFIG_PM_SLEEP
2188
2189static int sdhci_msm_suspend(struct device *dev)
2190{
2191 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302192 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2193 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302194 int ret = 0;
2195
Sahitya Tummala62448d92013-03-12 14:57:46 +05302196 if (gpio_is_valid(msm_host->pdata->status_gpio))
2197 mmc_cd_gpio_free(msm_host->mmc);
2198
Asutosh Dasbbc84782013-02-11 15:31:35 +05302199 if (pm_runtime_suspended(dev)) {
2200 pr_debug("%s: %s: already runtime suspended\n",
2201 mmc_hostname(host->mmc), __func__);
2202 goto out;
2203 }
2204
2205 return sdhci_msm_runtime_suspend(dev);
2206out:
2207 return ret;
2208}
2209
2210static int sdhci_msm_resume(struct device *dev)
2211{
2212 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302213 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2214 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302215 int ret = 0;
2216
Sahitya Tummala62448d92013-03-12 14:57:46 +05302217 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2218 ret = mmc_cd_gpio_request(msm_host->mmc,
2219 msm_host->pdata->status_gpio);
2220 if (ret)
2221 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
2222 mmc_hostname(host->mmc), __func__, ret);
2223 }
2224
Asutosh Dasbbc84782013-02-11 15:31:35 +05302225 if (pm_runtime_suspended(dev)) {
2226 pr_debug("%s: %s: runtime suspended, defer system resume\n",
2227 mmc_hostname(host->mmc), __func__);
2228 goto out;
2229 }
2230
2231 return sdhci_msm_runtime_resume(dev);
2232out:
2233 return ret;
2234}
2235#endif
2236
2237#ifdef CONFIG_PM
2238static const struct dev_pm_ops sdhci_msm_pmops = {
2239 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
2240 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
2241 NULL)
2242};
2243
2244#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
2245
2246#else
2247#define SDHCI_PM_OPS NULL
2248#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05302249static const struct of_device_id sdhci_msm_dt_match[] = {
2250 {.compatible = "qcom,sdhci-msm"},
2251};
2252MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2253
2254static struct platform_driver sdhci_msm_driver = {
2255 .probe = sdhci_msm_probe,
2256 .remove = __devexit_p(sdhci_msm_remove),
2257 .driver = {
2258 .name = "sdhci_msm",
2259 .owner = THIS_MODULE,
2260 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05302261 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302262 },
2263};
2264
2265module_platform_driver(sdhci_msm_driver);
2266
2267MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2268MODULE_LICENSE("GPL v2");