18 #include <linux/slab.h>
22 #include <linux/module.h>
35 #define MAX_BUS_CLK (4)
38 #define NUM_GPIOS(x) (x + 2)
79 return sdhci_priv(host);
86 static u32 get_curclk(
u32 ctrl2)
99 if (get_curclk(tmp) != ourhost->
cur_clk) {
100 dev_dbg(&ourhost->
pdev->dev,
"restored ctrl2 clock setting\n");
114 static unsigned int sdhci_s3c_get_max_clk(
struct sdhci_host *host)
116 struct sdhci_s3c *ourhost = to_s3c(host);
123 sdhci_s3c_check_sclk(host);
126 busclk = ourhost->
clk_bus[clk];
144 static unsigned int sdhci_s3c_consider_clock(
struct sdhci_s3c *ourhost,
161 return wanted -
rate;
166 for (div = 1; div < 256; div *= 2) {
167 if ((rate / div) <= wanted)
171 dev_dbg(&ourhost->
pdev->dev,
"clk %d: rate %ld, want %d, got %ld\n",
172 src, rate, wanted, rate / div);
174 return wanted - (rate /
div);
185 static void sdhci_s3c_set_clock(
struct sdhci_host *host,
unsigned int clock)
187 struct sdhci_s3c *ourhost = to_s3c(host);
199 delta = sdhci_s3c_consider_clock(ourhost, src, clock);
207 "selected source %d, clock %d, delta %d\n",
208 best_src, clock, best);
211 if (ourhost->
cur_clk != best_src) {
212 struct clk *clk = ourhost->
clk_bus[best_src];
214 clk_prepare_enable(clk);
243 if (clock < 25 * 1000000)
257 static unsigned int sdhci_s3c_get_min_clock(
struct sdhci_host *host)
259 struct sdhci_s3c *ourhost = to_s3c(host);
264 delta = sdhci_s3c_consider_clock(ourhost, src, 0);
275 static unsigned int sdhci_cmu_get_max_clock(
struct sdhci_host *host)
277 struct sdhci_s3c *ourhost = to_s3c(host);
283 static unsigned int sdhci_cmu_get_min_clock(
struct sdhci_host *host)
285 struct sdhci_s3c *ourhost = to_s3c(host);
295 static void sdhci_cmu_set_clock(
struct sdhci_host *host,
unsigned int clock)
297 struct sdhci_s3c *ourhost = to_s3c(host);
306 sdhci_s3c_set_clock(host, clock);
320 dev_err(dev,
"%s: Internal clock never stabilised.\n",
340 static int sdhci_s3c_platform_8bit_width(
struct sdhci_host *host,
int width)
366 static struct sdhci_ops sdhci_s3c_ops = {
367 .get_max_clock = sdhci_s3c_get_max_clk,
368 .set_clock = sdhci_s3c_set_clock,
369 .get_min_clock = sdhci_s3c_get_min_clock,
370 .platform_8bit_width = sdhci_s3c_platform_8bit_width,
375 struct sdhci_host *host = platform_get_drvdata(dev);
383 #ifdef CONFIG_PM_RUNTIME
384 clk_prepare_enable(sc->
clk_io);
392 #ifdef CONFIG_PM_RUNTIME
393 clk_disable_unprepare(sc->
clk_io);
397 spin_unlock_irqrestore(&host->
lock, flags);
405 if (sc->
pdata->ext_cd_gpio_invert)
407 sdhci_s3c_notify_change(sc->
pdev, status);
411 static void sdhci_s3c_setup_card_detect_gpio(
struct sdhci_s3c *sc)
421 sdhci_s3c_gpio_card_detect_thread,
425 dev_name(dev), sc) == 0) {
429 sdhci_s3c_notify_change(sc->
pdev, status);
431 dev_warn(dev,
"cannot request irq for card detect\n");
435 dev_err(dev,
"cannot request gpio for card detect\n");
444 struct sdhci_s3c *ourhost = to_s3c(host);
449 if (of_property_read_u32(node,
"bus-width", &max_width))
469 gpio = of_get_named_gpio(node,
"cd-gpios", 0);
470 if (gpio_is_valid(gpio)) {
473 }
else if (gpio != -
ENOENT) {
474 dev_err(dev,
"invalid card detect gpio specified\n");
478 gpio = of_get_named_gpio(node,
"samsung,cd-pinmux-gpio", 0);
479 if (gpio_is_valid(gpio)) {
482 }
else if (gpio != -
ENOENT) {
483 dev_err(dev,
"invalid card detect gpio specified\n");
487 dev_info(dev,
"assuming no card detect line available\n");
499 dev_err(dev,
"card detect gpio request failed\n");
508 gpio = of_get_gpio(node, cnt);
509 if (!gpio_is_valid(gpio)) {
510 dev_err(dev,
"invalid gpio[%d]\n", cnt);
511 goto err_free_dt_cd_gpio;
519 dev_err(dev,
"gpio[%d] request failed\n", cnt);
520 goto err_free_dt_gpios;
548 if (pdev->
dev.of_node) {
568 if (!pdev->
dev.platform_data && !pdev->
dev.of_node) {
569 dev_err(dev,
"no device data specified\n");
575 dev_err(dev,
"no irq specified\n");
581 dev_err(dev,
"sdhci_alloc_host() failed\n");
582 return PTR_ERR(host);
584 sc = sdhci_priv(host);
592 if (pdev->
dev.of_node) {
593 ret = sdhci_s3c_parse_dt(&pdev->
dev, host, pdata);
597 memcpy(pdata, pdev->
dev.platform_data,
sizeof(*pdata));
601 drv_data = sdhci_s3c_get_driver_data(pdev);
607 platform_set_drvdata(pdev, host);
611 dev_err(dev,
"failed to get io clock\n");
612 ret = PTR_ERR(sc->
clk_io);
617 clk_prepare_enable(sc->
clk_io);
619 for (clks = 0, ptr = 0; ptr <
MAX_BUS_CLK; ptr++) {
623 snprintf(name, 14,
"mmc_busclk.%d", ptr);
637 dev_info(dev,
"clock source %d: %s (%ld Hz)\n",
642 dev_err(dev,
"failed to find any bus clocks\n");
647 #ifndef CONFIG_PM_RUNTIME
654 dev_err(dev,
"failed to map registers\n");
663 host->
hw_name =
"samsung-hsmmc";
664 host->
ops = &sdhci_s3c_ops;
674 #ifndef CONFIG_MMC_SDHCI_S3C_DMA
722 sdhci_s3c_ops.
set_clock = sdhci_cmu_set_clock;
736 pm_runtime_use_autosuspend(&pdev->
dev);
737 pm_suspend_ignore_children(&pdev->
dev, 1);
741 dev_err(dev,
"sdhci_add_host() failed\n");
743 pm_runtime_get_noresume(&pdev->
dev);
754 sdhci_s3c_setup_card_detect_gpio(sc);
756 #ifdef CONFIG_PM_RUNTIME
758 clk_disable_unprepare(sc->
clk_io);
763 #ifndef CONFIG_PM_RUNTIME
773 clk_disable_unprepare(sc->
clk_io);
790 struct sdhci_host *host = platform_get_drvdata(pdev);
804 #ifdef CONFIG_PM_RUNTIME
806 clk_prepare_enable(sc->
clk_io);
810 pm_runtime_dont_use_autosuspend(&pdev->
dev);
811 pm_runtime_disable(&pdev->
dev);
813 #ifndef CONFIG_PM_RUNTIME
821 clk_disable_unprepare(sc->
clk_io);
824 if (pdev->
dev.of_node) {
830 platform_set_drvdata(pdev,
NULL);
835 #ifdef CONFIG_PM_SLEEP
836 static int sdhci_s3c_suspend(
struct device *dev)
840 return sdhci_suspend_host(host);
843 static int sdhci_s3c_resume(
struct device *dev)
847 return sdhci_resume_host(host);
851 #ifdef CONFIG_PM_RUNTIME
852 static int sdhci_s3c_runtime_suspend(
struct device *dev)
855 struct sdhci_s3c *ourhost = to_s3c(host);
856 struct clk *busclk = ourhost->
clk_io;
859 ret = sdhci_runtime_suspend_host(host);
862 clk_disable_unprepare(busclk);
866 static int sdhci_s3c_runtime_resume(
struct device *dev)
869 struct sdhci_s3c *ourhost = to_s3c(host);
870 struct clk *busclk = ourhost->
clk_io;
873 clk_prepare_enable(busclk);
875 ret = sdhci_runtime_resume_host(host);
881 static const struct dev_pm_ops sdhci_s3c_pmops = {
887 #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
890 #define SDHCI_S3C_PMOPS NULL
893 #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
897 #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
899 #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
907 .name =
"exynos4-sdhci",
915 static const struct of_device_id sdhci_s3c_dt_match[] = {
917 { .compatible =
"samsung,exynos4210-sdhci",
925 .
probe = sdhci_s3c_probe,
927 .id_table = sdhci_s3c_driver_ids,