Skip to content

Commit bf287a9

Browse files
Laurent Pinchartcjb
authored andcommitted
mmc: mmc_spi: Support CD/RO GPIOs
Add support for passing CD/RO GPIO numbers directly to the mmc_spi driver instead of relying solely on board code callbacks to retrieve the CD/RO signals values. The driver will enable debouncing on the card detect GPIO if the cd_debounce field is set to a non-zero value. Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Chris Ball <cjb@laptop.org>
1 parent 214fc30 commit bf287a9

3 files changed

Lines changed: 52 additions & 43 deletions

File tree

‎drivers/mmc/host/mmc_spi.c‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include <linux/mmc/host.h>
3838
#include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
39+
#include <linux/mmc/slot-gpio.h>
3940

4041
#include <linux/spi/spi.h>
4142
#include <linux/spi/mmc_spi.h>
@@ -1278,11 +1279,8 @@ static int mmc_spi_get_ro(struct mmc_host *mmc)
12781279

12791280
if (host->pdata && host->pdata->get_ro)
12801281
return !!host->pdata->get_ro(mmc->parent);
1281-
/*
1282-
* Board doesn't support read only detection; let the mmc core
1283-
* decide what to do.
1284-
*/
1285-
return -ENOSYS;
1282+
else
1283+
return mmc_gpio_get_ro(mmc);
12861284
}
12871285

12881286
static int mmc_spi_get_cd(struct mmc_host *mmc)
@@ -1291,7 +1289,8 @@ static int mmc_spi_get_cd(struct mmc_host *mmc)
12911289

12921290
if (host->pdata && host->pdata->get_cd)
12931291
return !!host->pdata->get_cd(mmc->parent);
1294-
return -ENOSYS;
1292+
else
1293+
return mmc_gpio_get_cd(mmc);
12951294
}
12961295

12971296
static const struct mmc_host_ops mmc_spi_ops = {
@@ -1324,6 +1323,7 @@ static int mmc_spi_probe(struct spi_device *spi)
13241323
struct mmc_host *mmc;
13251324
struct mmc_spi_host *host;
13261325
int status;
1326+
bool has_ro = false;
13271327

13281328
/* We rely on full duplex transfers, mostly to reduce
13291329
* per-transfer overheads (by making fewer transfers).
@@ -1448,18 +1448,33 @@ static int mmc_spi_probe(struct spi_device *spi)
14481448
}
14491449

14501450
/* pass platform capabilities, if any */
1451-
if (host->pdata)
1451+
if (host->pdata) {
14521452
mmc->caps |= host->pdata->caps;
1453+
mmc->caps2 |= host->pdata->caps2;
1454+
}
14531455

14541456
status = mmc_add_host(mmc);
14551457
if (status != 0)
14561458
goto fail_add_host;
14571459

1460+
if (host->pdata && host->pdata->flags & MMC_SPI_USE_CD_GPIO) {
1461+
status = mmc_gpio_request_cd(mmc, host->pdata->cd_gpio,
1462+
host->pdata->cd_debounce);
1463+
if (status != 0)
1464+
goto fail_add_host;
1465+
}
1466+
1467+
if (host->pdata && host->pdata->flags & MMC_SPI_USE_RO_GPIO) {
1468+
has_ro = true;
1469+
status = mmc_gpio_request_ro(mmc, host->pdata->ro_gpio);
1470+
if (status != 0)
1471+
goto fail_add_host;
1472+
}
1473+
14581474
dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n",
14591475
dev_name(&mmc->class_dev),
14601476
host->dma_dev ? "" : ", no DMA",
1461-
(host->pdata && host->pdata->get_ro)
1462-
? "" : ", no WP",
1477+
has_ro ? "" : ", no WP",
14631478
(host->pdata && host->pdata->setpower)
14641479
? "" : ", no poweroff",
14651480
(mmc->caps & MMC_CAP_NEEDS_POLL)

‎drivers/mmc/host/of_mmc_spi.c‎

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,6 @@ static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
5050
return container_of(dev->platform_data, struct of_mmc_spi, pdata);
5151
}
5252

53-
static int of_mmc_spi_read_gpio(struct device *dev, int gpio_num)
54-
{
55-
struct of_mmc_spi *oms = to_of_mmc_spi(dev);
56-
bool active_low = oms->alow_gpios[gpio_num];
57-
bool value = gpio_get_value(oms->gpios[gpio_num]);
58-
59-
return active_low ^ value;
60-
}
61-
62-
static int of_mmc_spi_get_cd(struct device *dev)
63-
{
64-
return of_mmc_spi_read_gpio(dev, CD_GPIO);
65-
}
66-
67-
static int of_mmc_spi_get_ro(struct device *dev)
68-
{
69-
return of_mmc_spi_read_gpio(dev, WP_GPIO);
70-
}
71-
7253
static int of_mmc_spi_init(struct device *dev,
7354
irqreturn_t (*irqhandler)(int, void *), void *mmc)
7455
{
@@ -130,20 +111,22 @@ struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
130111
if (!gpio_is_valid(oms->gpios[i]))
131112
continue;
132113

133-
ret = gpio_request(oms->gpios[i], dev_name(dev));
134-
if (ret < 0) {
135-
oms->gpios[i] = -EINVAL;
136-
continue;
137-
}
138-
139114
if (gpio_flags & OF_GPIO_ACTIVE_LOW)
140115
oms->alow_gpios[i] = true;
141116
}
142117

143-
if (gpio_is_valid(oms->gpios[CD_GPIO]))
144-
oms->pdata.get_cd = of_mmc_spi_get_cd;
145-
if (gpio_is_valid(oms->gpios[WP_GPIO]))
146-
oms->pdata.get_ro = of_mmc_spi_get_ro;
118+
if (gpio_is_valid(oms->gpios[CD_GPIO])) {
119+
oms->pdata.cd_gpio = oms->gpios[CD_GPIO];
120+
oms->pdata.flags |= MMC_SPI_USE_CD_GPIO;
121+
if (!oms->alow_gpios[CD_GPIO])
122+
oms->pdata.caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
123+
}
124+
if (gpio_is_valid(oms->gpios[WP_GPIO])) {
125+
oms->pdata.ro_gpio = oms->gpios[WP_GPIO];
126+
oms->pdata.flags |= MMC_SPI_USE_RO_GPIO;
127+
if (!oms->alow_gpios[WP_GPIO])
128+
oms->pdata.caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
129+
}
147130

148131
oms->detect_irq = irq_of_parse_and_map(np, 0);
149132
if (oms->detect_irq != 0) {
@@ -166,15 +149,10 @@ void mmc_spi_put_pdata(struct spi_device *spi)
166149
struct device *dev = &spi->dev;
167150
struct device_node *np = dev->of_node;
168151
struct of_mmc_spi *oms = to_of_mmc_spi(dev);
169-
int i;
170152

171153
if (!dev->platform_data || !np)
172154
return;
173155

174-
for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
175-
if (gpio_is_valid(oms->gpios[i]))
176-
gpio_free(oms->gpios[i]);
177-
}
178156
kfree(oms);
179157
dev->platform_data = NULL;
180158
}

‎include/linux/spi/mmc_spi.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
struct device;
88
struct mmc_host;
99

10+
#define MMC_SPI_USE_CD_GPIO (1 << 0)
11+
#define MMC_SPI_USE_RO_GPIO (1 << 1)
12+
#define MMC_SPI_CD_GPIO_ACTIVE_LOW (1 << 2)
13+
#define MMC_SPI_RO_GPIO_ACTIVE_LOW (1 << 3)
14+
1015
/* Put this in platform_data of a device being used to manage an MMC/SD
1116
* card slot. (Modeled after PXA mmc glue; see that for usage examples.)
1217
*
@@ -30,8 +35,19 @@ struct mmc_spi_platform_data {
3035
*/
3136
int (*get_cd)(struct device *);
3237

38+
/*
39+
* Card Detect and Read Only GPIOs. To enable debouncing on the card
40+
* detect GPIO, set the cd_debounce to the debounce time in
41+
* microseconds.
42+
*/
43+
unsigned int flags;
44+
unsigned int cd_gpio;
45+
unsigned int cd_debounce;
46+
unsigned int ro_gpio;
47+
3348
/* Capabilities to pass into mmc core (e.g. MMC_CAP_NEEDS_POLL). */
3449
unsigned long caps;
50+
unsigned long caps2;
3551

3652
/* how long to debounce card detect, in msecs */
3753
u16 detect_delay;

0 commit comments

Comments
 (0)