slot-gpio.c 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Generic GPIO card-detect helper
 *
 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/mmc/host.h>
16
#include <linux/mmc/slot-gpio.h>
17 18 19
#include <linux/module.h>
#include <linux/slab.h>

20
struct mmc_gpio {
21
	int ro_gpio;
22
	int cd_gpio;
23
	char *ro_label;
24
	char cd_label[0];
25 26
};

27
static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
28 29
{
	/* Schedule a card detection after a debounce timeout */
30 31 32 33 34 35 36
	struct mmc_host *host = dev_id;

	if (host->ops->card_event)
		host->ops->card_event(host);

	mmc_detect_change(host, msecs_to_jiffies(200));

37 38 39
	return IRQ_HANDLED;
}

40 41 42 43 44 45 46 47 48 49 50 51 52 53
static int mmc_gpio_alloc(struct mmc_host *host)
{
	size_t len = strlen(dev_name(host->parent)) + 4;
	struct mmc_gpio *ctx;

	mutex_lock(&host->slot.lock);

	ctx = host->slot.handler_priv;
	if (!ctx) {
		/*
		 * devm_kzalloc() can be called after device_initialize(), even
		 * before device_add(), i.e., between mmc_alloc_host() and
		 * mmc_add_host()
		 */
54
		ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
55 56
				   GFP_KERNEL);
		if (ctx) {
57
			ctx->ro_label = ctx->cd_label + len;
58
			snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
59
			snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
60
			ctx->cd_gpio = -EINVAL;
61
			ctx->ro_gpio = -EINVAL;
62 63 64 65 66 67 68 69 70
			host->slot.handler_priv = ctx;
		}
	}

	mutex_unlock(&host->slot.lock);

	return ctx ? 0 : -ENOMEM;
}

71 72 73 74 75 76 77 78 79 80 81 82
int mmc_gpio_get_ro(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;

	if (!ctx || !gpio_is_valid(ctx->ro_gpio))
		return -ENOSYS;

	return !gpio_get_value_cansleep(ctx->ro_gpio) ^
		!!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
}
EXPORT_SYMBOL(mmc_gpio_get_ro);

83 84 85 86 87 88 89 90 91 92 93 94
int mmc_gpio_get_cd(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;

	if (!ctx || !gpio_is_valid(ctx->cd_gpio))
		return -ENOSYS;

	return !gpio_get_value_cansleep(ctx->cd_gpio) ^
		!!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
}
EXPORT_SYMBOL(mmc_gpio_get_cd);

95 96 97 98 99 100 101 102 103 104 105 106 107 108
/**
 * mmc_gpio_request_ro - request a gpio for write-protection
 * @host: mmc host
 * @gpio: gpio number requested
 *
 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
 * drivers do not need to explicitly call mmc_gpio_free_ro() for freeing up,
 * if the requesting and freeing are only needed at probing and unbinding time
 * for once.  However, if client drivers do something special like runtime
 * switching for write-protection, they are responsible for calling
 * mmc_gpio_request_ro() and mmc_gpio_free_ro() as a pair on their own.
 *
 * Returns zero on success, else an error.
 */
109 110 111 112 113 114 115 116 117 118 119 120 121 122
int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
{
	struct mmc_gpio *ctx;
	int ret;

	if (!gpio_is_valid(gpio))
		return -EINVAL;

	ret = mmc_gpio_alloc(host);
	if (ret < 0)
		return ret;

	ctx = host->slot.handler_priv;

123 124
	ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
				    ctx->ro_label);
125 126 127 128 129 130
	if (ret < 0)
		return ret;

	ctx->ro_gpio = gpio;

	return 0;
131 132 133
}
EXPORT_SYMBOL(mmc_gpio_request_ro);

134 135 136 137 138 139 140 141 142 143 144 145 146 147
/**
 * mmc_gpio_request_cd - request a gpio for card-detection
 * @host: mmc host
 * @gpio: gpio number requested
 *
 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
 * drivers do not need to explicitly call mmc_gpio_free_cd() for freeing up,
 * if the requesting and freeing are only needed at probing and unbinding time
 * for once.  However, if client drivers do something special like runtime
 * switching for card-detection, they are responsible for calling
 * mmc_gpio_request_cd() and mmc_gpio_free_cd() as a pair on their own.
 *
 * Returns zero on success, else an error.
 */
148
int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
149
{
150
	struct mmc_gpio *ctx;
151
	int irq = gpio_to_irq(gpio);
152 153
	int ret;

154 155 156
	ret = mmc_gpio_alloc(host);
	if (ret < 0)
		return ret;
157

158
	ctx = host->slot.handler_priv;
159

160 161
	ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
				    ctx->cd_label);
162
	if (ret < 0)
163 164 165 166 167 168
		/*
		 * don't bother freeing memory. It might still get used by other
		 * slot functions, in any case it will be freed, when the device
		 * is destroyed.
		 */
		return ret;
169

170 171 172 173 174 175 176 177 178
	/*
	 * Even if gpio_to_irq() returns a valid IRQ number, the platform might
	 * still prefer to poll, e.g., because that IRQ number is already used
	 * by another unit and cannot be shared.
	 */
	if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
		irq = -EINVAL;

	if (irq >= 0) {
179 180
		ret = devm_request_threaded_irq(&host->class_dev, irq,
			NULL, mmc_gpio_cd_irqt,
181 182
			IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
			ctx->cd_label, host);
183 184 185
		if (ret < 0)
			irq = ret;
	}
186

187
	host->slot.cd_irq = irq;
188 189 190 191 192

	if (irq < 0)
		host->caps |= MMC_CAP_NEEDS_POLL;

	ctx->cd_gpio = gpio;
193 194 195

	return 0;
}
196
EXPORT_SYMBOL(mmc_gpio_request_cd);
197

198 199 200 201 202 203 204
/**
 * mmc_gpio_free_ro - free the write-protection gpio
 * @host: mmc host
 *
 * It's provided only for cases that client drivers need to manually free
 * up the write-protection gpio requested by mmc_gpio_request_ro().
 */
205 206 207 208 209 210 211 212 213 214 215
void mmc_gpio_free_ro(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	int gpio;

	if (!ctx || !gpio_is_valid(ctx->ro_gpio))
		return;

	gpio = ctx->ro_gpio;
	ctx->ro_gpio = -EINVAL;

216
	devm_gpio_free(&host->class_dev, gpio);
217 218 219
}
EXPORT_SYMBOL(mmc_gpio_free_ro);

220 221 222 223 224 225 226
/**
 * mmc_gpio_free_cd - free the card-detection gpio
 * @host: mmc host
 *
 * It's provided only for cases that client drivers need to manually free
 * up the card-detection gpio requested by mmc_gpio_request_cd().
 */
227
void mmc_gpio_free_cd(struct mmc_host *host)
228
{
229
	struct mmc_gpio *ctx = host->slot.handler_priv;
230
	int gpio;
231

232
	if (!ctx || !gpio_is_valid(ctx->cd_gpio))
233 234
		return;

235
	if (host->slot.cd_irq >= 0) {
236
		devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
237 238 239 240 241 242
		host->slot.cd_irq = -EINVAL;
	}

	gpio = ctx->cd_gpio;
	ctx->cd_gpio = -EINVAL;

243
	devm_gpio_free(&host->class_dev, gpio);
244
}
245
EXPORT_SYMBOL(mmc_gpio_free_cd);