slot-gpio.c 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * 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>
13
#include <linux/gpio/consumer.h>
14 15 16
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/mmc/host.h>
17
#include <linux/mmc/slot-gpio.h>
18 19 20
#include <linux/module.h>
#include <linux/slab.h>

21
struct mmc_gpio {
22 23 24 25
	struct gpio_desc *ro_gpio;
	struct gpio_desc *cd_gpio;
	bool override_ro_active_level;
	bool override_cd_active_level;
26
	char *ro_label;
27
	char cd_label[0];
28 29
};

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

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

	mmc_detect_change(host, msecs_to_jiffies(200));

40 41 42
	return IRQ_HANDLED;
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56
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()
		 */
57
		ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
58 59
				   GFP_KERNEL);
		if (ctx) {
60
			ctx->ro_label = ctx->cd_label + len;
61
			snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
62
			snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
63 64 65 66 67 68 69 70 71
			host->slot.handler_priv = ctx;
		}
	}

	mutex_unlock(&host->slot.lock);

	return ctx ? 0 : -ENOMEM;
}

72 73 74 75
int mmc_gpio_get_ro(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;

76
	if (!ctx || !ctx->ro_gpio)
77 78
		return -ENOSYS;

79 80 81 82 83
	if (ctx->override_ro_active_level)
		return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
			!!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);

	return gpiod_get_value_cansleep(ctx->ro_gpio);
84 85 86
}
EXPORT_SYMBOL(mmc_gpio_get_ro);

87 88 89 90
int mmc_gpio_get_cd(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;

91
	if (!ctx || !ctx->cd_gpio)
92 93
		return -ENOSYS;

94 95 96 97 98
	if (ctx->override_cd_active_level)
		return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
			!!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);

	return gpiod_get_value_cansleep(ctx->cd_gpio);
99 100 101
}
EXPORT_SYMBOL(mmc_gpio_get_cd);

102 103 104 105 106 107 108 109 110 111 112 113 114 115
/**
 * 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.
 */
116 117 118 119 120 121 122 123 124 125 126 127 128 129
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;

130 131
	ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
				    ctx->ro_label);
132 133 134
	if (ret < 0)
		return ret;

135 136
	ctx->override_ro_active_level = true;
	ctx->ro_gpio = gpio_to_desc(gpio);
137 138

	return 0;
139 140 141
}
EXPORT_SYMBOL(mmc_gpio_request_ro);

142
void mmc_gpiod_request_cd_irq(struct mmc_host *host)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	int ret, irq;

	if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
		return;

	irq = gpiod_to_irq(ctx->cd_gpio);

	/*
	 * Even if gpiod_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) {
		ret = devm_request_threaded_irq(&host->class_dev, irq,
			NULL, mmc_gpio_cd_irqt,
			IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
			ctx->cd_label, host);
		if (ret < 0)
			irq = ret;
	}

	host->slot.cd_irq = irq;

	if (irq < 0)
		host->caps |= MMC_CAP_NEEDS_POLL;
}
174
EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
175

176 177 178 179
/**
 * mmc_gpio_request_cd - request a gpio for card-detection
 * @host: mmc host
 * @gpio: gpio number requested
180
 * @debounce: debounce time in microseconds
181 182 183 184 185 186 187 188
 *
 * 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.
 *
189 190 191 192
 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
 * value. The caller is responsible for ensuring that the GPIO driver associated
 * with the GPIO supports debouncing, otherwise an error will be returned.
 *
193 194
 * Returns zero on success, else an error.
 */
195 196
int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
			unsigned int debounce)
197
{
198
	struct mmc_gpio *ctx;
199 200
	int ret;

201 202 203
	ret = mmc_gpio_alloc(host);
	if (ret < 0)
		return ret;
204

205
	ctx = host->slot.handler_priv;
206

207 208
	ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
				    ctx->cd_label);
209
	if (ret < 0)
210 211 212 213 214 215
		/*
		 * 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;
216

217 218 219 220 221 222
	if (debounce) {
		ret = gpio_set_debounce(gpio, debounce);
		if (ret < 0)
			return ret;
	}

223 224
	ctx->override_cd_active_level = true;
	ctx->cd_gpio = gpio_to_desc(gpio);
225

226 227
	mmc_gpiod_request_cd_irq(host);

228 229
	return 0;
}
230
EXPORT_SYMBOL(mmc_gpio_request_cd);
231

232 233 234 235 236 237 238
/**
 * 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().
 */
239 240 241 242 243
void mmc_gpio_free_ro(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	int gpio;

244
	if (!ctx || !ctx->ro_gpio)
245 246
		return;

247 248
	gpio = desc_to_gpio(ctx->ro_gpio);
	ctx->ro_gpio = NULL;
249

250
	devm_gpio_free(&host->class_dev, gpio);
251 252 253
}
EXPORT_SYMBOL(mmc_gpio_free_ro);

254 255 256 257 258 259 260
/**
 * 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().
 */
261
void mmc_gpio_free_cd(struct mmc_host *host)
262
{
263
	struct mmc_gpio *ctx = host->slot.handler_priv;
264
	int gpio;
265

266
	if (!ctx || !ctx->cd_gpio)
267 268
		return;

269
	if (host->slot.cd_irq >= 0) {
270
		devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
271 272 273
		host->slot.cd_irq = -EINVAL;
	}

274 275
	gpio = desc_to_gpio(ctx->cd_gpio);
	ctx->cd_gpio = NULL;
276

277
	devm_gpio_free(&host->class_dev, gpio);
278
}
279
EXPORT_SYMBOL(mmc_gpio_free_cd);
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

/**
 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
 * @host: mmc host
 * @con_id: function within the GPIO consumer
 * @idx: index of the GPIO to obtain in the consumer
 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
 * @debounce: debounce time in microseconds
 *
 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
 * descriptor API.  Note that it is paired with mmc_gpiod_free_cd() not
 * mmc_gpio_free_cd().  Note also that it must be called prior to mmc_add_host()
 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
 *
 * Returns zero on success, else an error.
 */
int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
			 unsigned int idx, bool override_active_level,
			 unsigned int debounce)
{
	struct mmc_gpio *ctx;
	struct gpio_desc *desc;
	int ret;

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

	ctx = host->slot.handler_priv;

	if (!con_id)
		con_id = ctx->cd_label;

	desc = devm_gpiod_get_index(host->parent, con_id, idx);
	if (IS_ERR(desc))
		return PTR_ERR(desc);

	ret = gpiod_direction_input(desc);
	if (ret < 0)
		return ret;

	if (debounce) {
		ret = gpiod_set_debounce(desc, debounce);
		if (ret < 0)
			return ret;
	}

	ctx->override_cd_active_level = override_active_level;
	ctx->cd_gpio = desc;

	return 0;
}
EXPORT_SYMBOL(mmc_gpiod_request_cd);

/**
 * mmc_gpiod_free_cd - free the card-detection gpio descriptor
 * @host: mmc host
 *
 * It's provided only for cases that client drivers need to manually free
 * up the card-detection gpio requested by mmc_gpiod_request_cd().
 */
void mmc_gpiod_free_cd(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;

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

	if (host->slot.cd_irq >= 0) {
		devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
		host->slot.cd_irq = -EINVAL;
	}

	devm_gpiod_put(&host->class_dev, ctx->cd_gpio);

	ctx->cd_gpio = NULL;
}
EXPORT_SYMBOL(mmc_gpiod_free_cd);