2 * SDHCI Controller driver for TI's OMAP SoCs
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
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 of
9 * the License as published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/delay.h>
21 #include <linux/mmc/slot-gpio.h>
22 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/pinctrl/consumer.h>
30 #include "sdhci-pltfm.h"
32 #define SDHCI_OMAP_CON 0x12c
33 #define CON_DW8 BIT(5)
34 #define CON_DMA_MASTER BIT(20)
35 #define CON_DDR BIT(19)
36 #define CON_CLKEXTFREE BIT(16)
37 #define CON_PADEN BIT(15)
38 #define CON_INIT BIT(1)
41 #define SDHCI_OMAP_DLL 0x0134
42 #define DLL_SWT BIT(20)
43 #define DLL_FORCE_SR_C_SHIFT 13
44 #define DLL_FORCE_SR_C_MASK (0x7f << DLL_FORCE_SR_C_SHIFT)
45 #define DLL_FORCE_VALUE BIT(12)
46 #define DLL_CALIB BIT(1)
48 #define SDHCI_OMAP_CMD 0x20c
50 #define SDHCI_OMAP_PSTATE 0x0224
51 #define PSTATE_DLEV_DAT0 BIT(20)
52 #define PSTATE_DATI BIT(1)
54 #define SDHCI_OMAP_HCTL 0x228
55 #define HCTL_SDBP BIT(8)
56 #define HCTL_SDVS_SHIFT 9
57 #define HCTL_SDVS_MASK (0x7 << HCTL_SDVS_SHIFT)
58 #define HCTL_SDVS_33 (0x7 << HCTL_SDVS_SHIFT)
59 #define HCTL_SDVS_30 (0x6 << HCTL_SDVS_SHIFT)
60 #define HCTL_SDVS_18 (0x5 << HCTL_SDVS_SHIFT)
62 #define SDHCI_OMAP_SYSCTL 0x22c
63 #define SYSCTL_CEN BIT(2)
64 #define SYSCTL_CLKD_SHIFT 6
65 #define SYSCTL_CLKD_MASK 0x3ff
67 #define SDHCI_OMAP_STAT 0x230
69 #define SDHCI_OMAP_IE 0x234
70 #define INT_CC_EN BIT(0)
72 #define SDHCI_OMAP_AC12 0x23c
73 #define AC12_V1V8_SIGEN BIT(19)
74 #define AC12_SCLK_SEL BIT(23)
76 #define SDHCI_OMAP_CAPA 0x240
77 #define CAPA_VS33 BIT(24)
78 #define CAPA_VS30 BIT(25)
79 #define CAPA_VS18 BIT(26)
81 #define SDHCI_OMAP_CAPA2 0x0244
82 #define CAPA2_TSDR50 BIT(13)
84 #define SDHCI_OMAP_TIMEOUT 1 /* 1 msec */
86 #define SYSCTL_CLKD_MAX 0x3FF
88 #define IOV_1V8 1800000 /* 180000 uV */
89 #define IOV_3V0 3000000 /* 300000 uV */
90 #define IOV_3V3 3300000 /* 330000 uV */
92 #define MAX_PHASE_DELAY 0x7C
94 /* sdhci-omap controller flags */
95 #define SDHCI_OMAP_REQUIRE_IODELAY BIT(0)
97 struct sdhci_omap_data {
102 struct sdhci_omap_host {
105 struct regulator *pbias;
107 struct sdhci_host *host;
113 struct pinctrl *pinctrl;
114 struct pinctrl_state **pinctrl_state;
117 static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host);
118 static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host);
120 static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
123 return readl(host->base + offset);
126 static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
127 unsigned int offset, u32 data)
129 writel(data, host->base + offset);
132 static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
133 bool power_on, unsigned int iov)
136 struct device *dev = omap_host->dev;
138 if (IS_ERR(omap_host->pbias))
142 ret = regulator_set_voltage(omap_host->pbias, iov, iov);
144 dev_err(dev, "pbias set voltage failed\n");
148 if (omap_host->pbias_enabled)
151 ret = regulator_enable(omap_host->pbias);
153 dev_err(dev, "pbias reg enable fail\n");
157 omap_host->pbias_enabled = true;
159 if (!omap_host->pbias_enabled)
162 ret = regulator_disable(omap_host->pbias);
164 dev_err(dev, "pbias reg disable fail\n");
167 omap_host->pbias_enabled = false;
173 static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
177 struct sdhci_host *host = omap_host->host;
178 struct mmc_host *mmc = host->mmc;
180 ret = sdhci_omap_set_pbias(omap_host, false, 0);
184 if (!IS_ERR(mmc->supply.vqmmc)) {
185 ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov);
187 dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
192 ret = sdhci_omap_set_pbias(omap_host, true, iov);
199 static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
200 unsigned char signal_voltage)
205 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
206 reg &= ~HCTL_SDVS_MASK;
208 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
213 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
216 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
219 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
220 while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)) {
221 if (WARN_ON(ktime_after(ktime_get(), timeout)))
227 static inline void sdhci_omap_set_dll(struct sdhci_omap_host *omap_host,
233 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
234 reg |= DLL_FORCE_VALUE;
235 reg &= ~DLL_FORCE_SR_C_MASK;
236 reg |= (count << DLL_FORCE_SR_C_SHIFT);
237 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
240 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
241 for (i = 0; i < 1000; i++) {
242 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
247 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
250 static void sdhci_omap_disable_tuning(struct sdhci_omap_host *omap_host)
254 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
255 reg &= ~AC12_SCLK_SEL;
256 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
258 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
259 reg &= ~(DLL_FORCE_VALUE | DLL_SWT);
260 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
263 static int sdhci_omap_execute_tuning(struct mmc_host *mmc, u32 opcode)
265 struct sdhci_host *host = mmc_priv(mmc);
266 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
267 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
268 struct device *dev = omap_host->dev;
269 struct mmc_ios *ios = &mmc->ios;
270 u32 start_window = 0, max_window = 0;
271 u8 cur_match, prev_match = 0;
272 u32 length = 0, max_len = 0;
278 pltfm_host = sdhci_priv(host);
279 omap_host = sdhci_pltfm_priv(pltfm_host);
280 dev = omap_host->dev;
282 /* clock tuning is not needed for upto 52MHz */
283 if (ios->clock <= 52000000)
286 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA2);
287 if (ios->timing == MMC_TIMING_UHS_SDR50 && !(reg & CAPA2_TSDR50))
290 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
292 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
295 * OMAP5/DRA74X/DRA72x Errata i802:
296 * DCRC error interrupts (MMCHS_STAT[21] DCRC=0x1) can occur
297 * during the tuning procedure. So disable it during the
300 ier &= ~SDHCI_INT_DATA_CRC;
301 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
302 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
304 while (phase_delay <= MAX_PHASE_DELAY) {
305 sdhci_omap_set_dll(omap_host, phase_delay);
307 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
312 start_window = phase_delay;
317 if (length > max_len) {
318 max_window = start_window;
322 prev_match = cur_match;
327 dev_err(dev, "Unable to find match\n");
332 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
333 if (!(reg & AC12_SCLK_SEL)) {
338 phase_delay = max_window + 4 * (max_len >> 1);
339 sdhci_omap_set_dll(omap_host, phase_delay);
344 dev_err(dev, "Tuning failed\n");
345 sdhci_omap_disable_tuning(omap_host);
348 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
349 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
350 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
354 static int sdhci_omap_card_busy(struct mmc_host *mmc)
358 struct sdhci_host *host = mmc_priv(mmc);
359 struct sdhci_pltfm_host *pltfm_host;
360 struct sdhci_omap_host *omap_host;
363 pltfm_host = sdhci_priv(host);
364 omap_host = sdhci_pltfm_priv(pltfm_host);
366 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
367 ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
368 reg &= ~CON_CLKEXTFREE;
369 if (ac12 & AC12_V1V8_SIGEN)
370 reg |= CON_CLKEXTFREE;
372 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
374 disable_irq(host->irq);
375 ier |= SDHCI_INT_CARD_INT;
376 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
377 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
380 * Delay is required for PSTATE to correctly reflect
381 * DLEV/CLEV values after PADEN is set.
383 usleep_range(50, 100);
384 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE);
385 if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0))
388 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
389 reg &= ~(CON_CLKEXTFREE | CON_PADEN);
390 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
392 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
393 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
394 enable_irq(host->irq);
399 static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
405 struct sdhci_host *host = mmc_priv(mmc);
406 struct sdhci_pltfm_host *pltfm_host;
407 struct sdhci_omap_host *omap_host;
410 pltfm_host = sdhci_priv(host);
411 omap_host = sdhci_pltfm_priv(pltfm_host);
412 dev = omap_host->dev;
414 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
415 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
416 if (!(reg & CAPA_VS33))
419 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
421 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
422 reg &= ~AC12_V1V8_SIGEN;
423 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
426 } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
427 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
428 if (!(reg & CAPA_VS18))
431 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
433 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
434 reg |= AC12_V1V8_SIGEN;
435 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
442 ret = sdhci_omap_enable_iov(omap_host, iov);
444 dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
448 dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
452 static void sdhci_omap_set_timing(struct sdhci_omap_host *omap_host, u8 timing)
455 struct pinctrl_state *pinctrl_state;
456 struct device *dev = omap_host->dev;
458 if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
461 if (omap_host->timing == timing)
464 sdhci_omap_stop_clock(omap_host);
466 pinctrl_state = omap_host->pinctrl_state[timing];
467 ret = pinctrl_select_state(omap_host->pinctrl, pinctrl_state);
469 dev_err(dev, "failed to select pinctrl state\n");
473 sdhci_omap_start_clock(omap_host);
474 omap_host->timing = timing;
477 static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
480 if (omap_host->bus_mode == MMC_POWER_OFF)
481 sdhci_omap_disable_tuning(omap_host);
482 omap_host->power_mode = power_mode;
485 static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
490 if (omap_host->bus_mode == mode)
493 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
494 if (mode == MMC_BUSMODE_OPENDRAIN)
498 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
500 omap_host->bus_mode = mode;
503 static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
505 struct sdhci_host *host = mmc_priv(mmc);
506 struct sdhci_pltfm_host *pltfm_host;
507 struct sdhci_omap_host *omap_host;
509 pltfm_host = sdhci_priv(host);
510 omap_host = sdhci_pltfm_priv(pltfm_host);
512 sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
513 sdhci_omap_set_timing(omap_host, ios->timing);
514 sdhci_set_ios(mmc, ios);
515 sdhci_omap_set_power_mode(omap_host, ios->power_mode);
518 static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
523 dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
524 if (dsor > SYSCTL_CLKD_MAX)
525 dsor = SYSCTL_CLKD_MAX;
530 static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
534 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
536 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
539 static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
543 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
545 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
548 static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
550 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
551 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
552 unsigned long clkdiv;
554 sdhci_omap_stop_clock(omap_host);
559 clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
560 clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
561 sdhci_enable_clk(host, clkdiv);
563 sdhci_omap_start_clock(omap_host);
566 static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
569 struct mmc_host *mmc = host->mmc;
571 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
574 static int sdhci_omap_enable_dma(struct sdhci_host *host)
577 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
578 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
580 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
581 reg |= CON_DMA_MASTER;
582 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
587 static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
589 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
591 return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
594 static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
596 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
597 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
600 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
601 if (width == MMC_BUS_WIDTH_8)
605 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
607 sdhci_set_bus_width(host, width);
610 static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
614 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
615 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
617 if (omap_host->power_mode == power_mode)
620 if (power_mode != MMC_POWER_ON)
623 disable_irq(host->irq);
625 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
627 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
628 sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
631 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
632 while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)) {
633 if (WARN_ON(ktime_after(ktime_get(), timeout)))
638 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
640 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
641 sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
643 enable_irq(host->irq);
646 static void sdhci_omap_set_uhs_signaling(struct sdhci_host *host,
650 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
651 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
653 sdhci_omap_stop_clock(omap_host);
655 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
656 if (timing == MMC_TIMING_UHS_DDR50 || timing == MMC_TIMING_MMC_DDR52)
660 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
662 sdhci_set_uhs_signaling(host, timing);
663 sdhci_omap_start_clock(omap_host);
666 static struct sdhci_ops sdhci_omap_ops = {
667 .set_clock = sdhci_omap_set_clock,
668 .set_power = sdhci_omap_set_power,
669 .enable_dma = sdhci_omap_enable_dma,
670 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
671 .get_min_clock = sdhci_omap_get_min_clock,
672 .set_bus_width = sdhci_omap_set_bus_width,
673 .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
674 .reset = sdhci_reset,
675 .set_uhs_signaling = sdhci_omap_set_uhs_signaling,
678 static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
682 struct device *dev = omap_host->dev;
683 struct regulator *vqmmc;
685 vqmmc = regulator_get(dev, "vqmmc");
687 ret = PTR_ERR(vqmmc);
691 /* voltage capabilities might be set by boot loader, clear it */
692 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
693 reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
695 if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
697 if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
700 sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
703 regulator_put(vqmmc);
708 static const struct sdhci_pltfm_data sdhci_omap_pdata = {
709 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
710 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
711 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
712 SDHCI_QUIRK_NO_HISPD_BIT |
713 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
714 .quirks2 = SDHCI_QUIRK2_NO_1_8_V |
715 SDHCI_QUIRK2_ACMD23_BROKEN |
716 SDHCI_QUIRK2_RSP_136_HAS_CRC,
717 .ops = &sdhci_omap_ops,
720 static const struct sdhci_omap_data dra7_data = {
722 .flags = SDHCI_OMAP_REQUIRE_IODELAY,
725 static const struct of_device_id omap_sdhci_match[] = {
726 { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
729 MODULE_DEVICE_TABLE(of, omap_sdhci_match);
731 static struct pinctrl_state
732 *sdhci_omap_iodelay_pinctrl_state(struct sdhci_omap_host *omap_host, char *mode,
733 u32 *caps, u32 capmask)
735 struct device *dev = omap_host->dev;
736 struct pinctrl_state *pinctrl_state = ERR_PTR(-ENODEV);
738 if (!(*caps & capmask))
741 pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, mode);
742 if (IS_ERR(pinctrl_state)) {
743 dev_err(dev, "no pinctrl state for %s mode", mode);
748 return pinctrl_state;
751 static int sdhci_omap_config_iodelay_pinctrl_state(struct sdhci_omap_host
754 struct device *dev = omap_host->dev;
755 struct sdhci_host *host = omap_host->host;
756 struct mmc_host *mmc = host->mmc;
757 u32 *caps = &mmc->caps;
758 u32 *caps2 = &mmc->caps2;
759 struct pinctrl_state *state;
760 struct pinctrl_state **pinctrl_state;
762 if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
765 pinctrl_state = devm_kzalloc(dev, sizeof(*pinctrl_state) *
766 (MMC_TIMING_MMC_HS200 + 1), GFP_KERNEL);
770 omap_host->pinctrl = devm_pinctrl_get(omap_host->dev);
771 if (IS_ERR(omap_host->pinctrl)) {
772 dev_err(dev, "Cannot get pinctrl\n");
773 return PTR_ERR(omap_host->pinctrl);
776 state = pinctrl_lookup_state(omap_host->pinctrl, "default");
778 dev_err(dev, "no pinctrl state for default mode\n");
779 return PTR_ERR(state);
781 pinctrl_state[MMC_TIMING_LEGACY] = state;
783 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr104", caps,
786 pinctrl_state[MMC_TIMING_UHS_SDR104] = state;
788 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr50", caps,
791 pinctrl_state[MMC_TIMING_UHS_DDR50] = state;
793 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr50", caps,
796 pinctrl_state[MMC_TIMING_UHS_SDR50] = state;
798 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr25", caps,
801 pinctrl_state[MMC_TIMING_UHS_SDR25] = state;
803 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr12", caps,
806 pinctrl_state[MMC_TIMING_UHS_SDR12] = state;
808 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_1_8v", caps,
811 pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
813 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
814 MMC_CAP_SD_HIGHSPEED);
816 pinctrl_state[MMC_TIMING_SD_HS] = state;
818 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
819 MMC_CAP_MMC_HIGHSPEED);
821 pinctrl_state[MMC_TIMING_MMC_HS] = state;
823 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs200_1_8v", caps2,
824 MMC_CAP2_HS200_1_8V_SDR);
826 pinctrl_state[MMC_TIMING_MMC_HS200] = state;
828 omap_host->pinctrl_state = pinctrl_state;
833 static int sdhci_omap_probe(struct platform_device *pdev)
837 struct device *dev = &pdev->dev;
838 struct sdhci_host *host;
839 struct sdhci_pltfm_host *pltfm_host;
840 struct sdhci_omap_host *omap_host;
841 struct mmc_host *mmc;
842 const struct of_device_id *match;
843 struct sdhci_omap_data *data;
845 match = of_match_device(omap_sdhci_match, dev);
849 data = (struct sdhci_omap_data *)match->data;
851 dev_err(dev, "no sdhci omap data\n");
854 offset = data->offset;
856 host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
859 dev_err(dev, "Failed sdhci_pltfm_init\n");
860 return PTR_ERR(host);
863 pltfm_host = sdhci_priv(host);
864 omap_host = sdhci_pltfm_priv(pltfm_host);
865 omap_host->host = host;
866 omap_host->base = host->ioaddr;
867 omap_host->dev = dev;
868 omap_host->power_mode = MMC_POWER_UNDEFINED;
869 omap_host->timing = MMC_TIMING_LEGACY;
870 omap_host->flags = data->flags;
871 host->ioaddr += offset;
874 ret = mmc_of_parse(mmc);
878 pltfm_host->clk = devm_clk_get(dev, "fck");
879 if (IS_ERR(pltfm_host->clk)) {
880 ret = PTR_ERR(pltfm_host->clk);
884 ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
886 dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
890 omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
891 if (IS_ERR(omap_host->pbias)) {
892 ret = PTR_ERR(omap_host->pbias);
895 dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
897 omap_host->pbias_enabled = false;
900 * omap_device_pm_domain has callbacks to enable the main
901 * functional clock, interface clock and also configure the
902 * SYSCONFIG register of omap devices. The callback will be invoked
903 * as part of pm_runtime_get_sync.
905 pm_runtime_enable(dev);
906 ret = pm_runtime_get_sync(dev);
908 dev_err(dev, "pm_runtime_get_sync failed\n");
909 pm_runtime_put_noidle(dev);
910 goto err_rpm_disable;
913 ret = sdhci_omap_set_capabilities(omap_host);
915 dev_err(dev, "failed to set system capabilities\n");
919 ret = sdhci_omap_config_iodelay_pinctrl_state(omap_host);
923 host->mmc_host_ops.get_ro = mmc_gpio_get_ro;
924 host->mmc_host_ops.start_signal_voltage_switch =
925 sdhci_omap_start_signal_voltage_switch;
926 host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
927 host->mmc_host_ops.card_busy = sdhci_omap_card_busy;
928 host->mmc_host_ops.execute_tuning = sdhci_omap_execute_tuning;
930 sdhci_read_caps(host);
931 host->caps |= SDHCI_CAN_DO_ADMA2;
933 ret = sdhci_add_host(host);
940 pm_runtime_put_sync(dev);
943 pm_runtime_disable(dev);
946 sdhci_pltfm_free(pdev);
950 static int sdhci_omap_remove(struct platform_device *pdev)
952 struct device *dev = &pdev->dev;
953 struct sdhci_host *host = platform_get_drvdata(pdev);
955 sdhci_remove_host(host, true);
956 pm_runtime_put_sync(dev);
957 pm_runtime_disable(dev);
958 sdhci_pltfm_free(pdev);
963 static struct platform_driver sdhci_omap_driver = {
964 .probe = sdhci_omap_probe,
965 .remove = sdhci_omap_remove,
967 .name = "sdhci-omap",
968 .of_match_table = omap_sdhci_match,
972 module_platform_driver(sdhci_omap_driver);
974 MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
975 MODULE_AUTHOR("Texas Instruments Inc.");
976 MODULE_LICENSE("GPL v2");
977 MODULE_ALIAS("platform:sdhci_omap");