From 866b9c1e9c89aaaf19cb9b467e31f4f0df8bf337 Mon Sep 17 00:00:00 2001
From: Timothy Pearson <tpearson@raptorengineeringinc.com>
Date: Mon, 18 Apr 2016 16:42:54 +0000
Subject: [PATCH 05/15] drivers/mmc: Initial support for DTB-defined card
 detect pin

Signed-off-by: Timothy Pearson <tpearson@raptorengineeringinc.com>
---
 drivers/mmc/dw_mmc.c          | 16 ++++++++++++++++
 drivers/mmc/rockchip_dw_mmc.c | 12 ++++++++++++
 include/dwmmc.h               |  4 ++++
 3 files changed, 32 insertions(+)

diff --git a/drivers/mmc/dw_mmc.c b/drivers/mmc/dw_mmc.c
index 909e3ca..96e123d 100644
--- a/drivers/mmc/dw_mmc.c
+++ b/drivers/mmc/dw_mmc.c
@@ -1,4 +1,5 @@
 /*
+ * (C) Copyright 2016 Raptor Engineering, LLC
  * (C) Copyright 2012 SAMSUNG Electronics
  * Jaehoon Chung <jh80.chung@samsung.com>
  * Rajeshawari Shinde <rajeshwari.s@samsung.com>
@@ -448,10 +449,25 @@ static int dwmci_init(struct mmc *mmc)
 	return 0;
 }
 
+static int dwmci_getcd(struct mmc *mmc)
+{
+	struct dwmci_host *host = mmc->priv;
+	int ret = 1;
+	int dt_gpio_present = dm_gpio_is_valid(&host->cd_gpio);
+
+	if (dt_gpio_present)
+		ret = dm_gpio_get_value(&host->cd_gpio);
+
+	debug("%s: dt gpio present: %d card present: %d\n", __func__, dm_gpio_is_valid(&host->cd_gpio), ret);
+
+	return ret;
+}
+
 static const struct mmc_ops dwmci_ops = {
 	.send_cmd	= dwmci_send_cmd,
 	.set_ios	= dwmci_set_ios,
 	.init		= dwmci_init,
+	.getcd		= dwmci_getcd,
 };
 
 int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
diff --git a/drivers/mmc/rockchip_dw_mmc.c b/drivers/mmc/rockchip_dw_mmc.c
index cb9e104..2eeab10 100644
--- a/drivers/mmc/rockchip_dw_mmc.c
+++ b/drivers/mmc/rockchip_dw_mmc.c
@@ -30,6 +30,8 @@ static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
 	struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
 	int ret;
 
+	debug("%s\n", __func__);
+
 	ret = clk_set_periph_rate(priv->clk, priv->periph, freq);
 	if (ret < 0) {
 		debug("%s: err=%d\n", __func__, ret);
@@ -44,6 +46,8 @@ static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
 	struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
 	struct dwmci_host *host = &priv->host;
 
+	debug("%s\n", __func__);
+
 	host->name = dev->name;
 	host->ioaddr = (void *)dev_get_addr(dev);
 	host->buswidth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
@@ -57,6 +61,10 @@ static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
 	else
 		host->dev_index = 1;
 
+	gpio_request_by_name(dev, "cd-gpios", 0, &host->cd_gpio, GPIOD_IS_IN);
+
+	debug("%s: dev_index: %d\n", __func__,  host->dev_index);
+
 	return 0;
 }
 
@@ -70,6 +78,8 @@ static int rockchip_dwmmc_probe(struct udevice *dev)
 	int ret;
 	int fifo_depth;
 
+	debug("%s\n", __func__);
+
 	ret = clk_get_by_index(dev, 0, &priv->clk);
 	if (ret < 0)
 		return ret;
@@ -129,6 +139,8 @@ static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
 	struct gpio_desc reset;
 	int ret;
 
+	debug("%s\n", __func__);
+
 	ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
 	if (ret)
 		return ret;
diff --git a/include/dwmmc.h b/include/dwmmc.h
index 05b0817..91edc43 100644
--- a/include/dwmmc.h
+++ b/include/dwmmc.h
@@ -9,6 +9,7 @@
 #define __DWMMC_HW_H
 
 #include <asm/io.h>
+#include <asm-generic/gpio.h>
 #include <mmc.h>
 
 #define DWMCI_CTRL		0x000
@@ -185,6 +186,9 @@ struct dwmci_host {
 
 	/* use fifo mode to read and write data */
 	bool fifo_mode;
+
+	/* Card Detect GPIO */
+	struct gpio_desc cd_gpio;
 };
 
 struct dwmci_idmac {
-- 
1.9.1

