Release 4.7 drivers/mmc/core/slot-gpio.c
  
  
/*
 * 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/gpio/consumer.h>
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/mmc/host.h>
#include <linux/mmc/slot-gpio.h>
#include <linux/module.h>
#include <linux/slab.h>
#include "slot-gpio.h"
struct mmc_gpio {
	
struct gpio_desc *ro_gpio;
	
struct gpio_desc *cd_gpio;
	
bool override_ro_active_level;
	
bool override_cd_active_level;
	
irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
	
char *ro_label;
	
char cd_label[0];
};
static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
{
	/* Schedule a card detection after a debounce timeout */
	struct mmc_host *host = dev_id;
	host->trigger_card_event = true;
	mmc_detect_change(host, msecs_to_jiffies(200));
	return IRQ_HANDLED;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| guennadi liakhovetski | guennadi liakhovetski | 37 | 92.50% | 3 | 75.00% | 
| markus mayer | markus mayer | 3 | 7.50% | 1 | 25.00% | 
 | Total | 40 | 100.00% | 4 | 100.00% | 
int mmc_gpio_alloc(struct mmc_host *host)
{
	size_t len = strlen(dev_name(host->parent)) + 4;
	struct mmc_gpio *ctx = devm_kzalloc(host->parent,
				sizeof(*ctx) + 2 * len,	GFP_KERNEL);
	if (ctx) {
		ctx->ro_label = ctx->cd_label + len;
		snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
		snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
		host->slot.handler_priv = ctx;
		host->slot.cd_irq = -EINVAL;
	}
	return ctx ? 0 : -ENOMEM;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| guennadi liakhovetski | guennadi liakhovetski | 118 | 93.65% | 2 | 50.00% | 
| ulf hansson | ulf hansson | 8 | 6.35% | 2 | 50.00% | 
 | Total | 126 | 100.00% | 4 | 100.00% | 
int mmc_gpio_get_ro(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	if (!ctx || !ctx->ro_gpio)
		return -ENOSYS;
	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);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| guennadi liakhovetski | guennadi liakhovetski | 53 | 77.94% | 1 | 50.00% | 
| adrian hunter | adrian hunter | 15 | 22.06% | 1 | 50.00% | 
 | Total | 68 | 100.00% | 2 | 100.00% | 
EXPORT_SYMBOL(mmc_gpio_get_ro);
int mmc_gpio_get_cd(struct mmc_host *host)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	if (!ctx || !ctx->cd_gpio)
		return -ENOSYS;
	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);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| guennadi liakhovetski | guennadi liakhovetski | 53 | 77.94% | 1 | 50.00% | 
| adrian hunter | adrian hunter | 15 | 22.06% | 1 | 50.00% | 
 | Total | 68 | 100.00% | 2 | 100.00% | 
EXPORT_SYMBOL(mmc_gpio_get_cd);
/**
 * 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 worry about freeing up memory.
 *
 * Returns zero on success, else an error.
 */
int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	int ret;
	if (!gpio_is_valid(gpio))
		return -EINVAL;
	ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
				    ctx->ro_label);
	if (ret < 0)
		return ret;
	ctx->override_ro_active_level = true;
	ctx->ro_gpio = gpio_to_desc(gpio);
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| guennadi liakhovetski | guennadi liakhovetski | 46 | 54.76% | 1 | 16.67% | 
| chris ball | chris ball | 18 | 21.43% | 1 | 16.67% | 
| adrian hunter | adrian hunter | 9 | 10.71% | 1 | 16.67% | 
| ulf hansson | ulf hansson | 7 | 8.33% | 2 | 33.33% | 
| shawn guo | shawn guo | 4 | 4.76% | 1 | 16.67% | 
 | Total | 84 | 100.00% | 6 | 100.00% | 
EXPORT_SYMBOL(mmc_gpio_request_ro);
void mmc_gpiod_request_cd_irq(struct mmc_host *host)
{
	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) {
		if (!ctx->cd_gpio_isr)
			ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
		ret = devm_request_threaded_irq(host->parent, irq,
			NULL, ctx->cd_gpio_isr,
			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;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| guennadi liakhovetski | guennadi liakhovetski | 100 | 65.79% | 7 | 58.33% | 
| adrian hunter | adrian hunter | 26 | 17.11% | 1 | 8.33% | 
| neil brown | neil brown | 16 | 10.53% | 1 | 8.33% | 
| laurent pinchart | laurent pinchart | 5 | 3.29% | 1 | 8.33% | 
| shawn guo | shawn guo | 4 | 2.63% | 1 | 8.33% | 
| ulf hansson | ulf hansson | 1 | 0.66% | 1 | 8.33% | 
 | Total | 152 | 100.00% | 12 | 100.00% | 
EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
/* Register an alternate interrupt service routine for
 * the card-detect GPIO.
 */
void mmc_gpio_set_cd_isr(struct mmc_host *host,
			 irqreturn_t (*isr)(int irq, void *dev_id))
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	WARN_ON(ctx->cd_gpio_isr);
	ctx->cd_gpio_isr = isr;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| neil brown | neil brown | 48 | 100.00% | 1 | 100.00% | 
 | Total | 48 | 100.00% | 1 | 100.00% | 
EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
/**
 * mmc_gpio_request_cd - request a gpio for card-detection
 * @host: mmc host
 * @gpio: gpio number requested
 * @debounce: debounce time in microseconds
 *
 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
 * drivers do not need to worry about freeing up memory.
 *
 * 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.
 *
 * Returns zero on success, else an error.
 */
int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
			unsigned int debounce)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	int ret;
	ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
				    ctx->cd_label);
	if (ret < 0)
		/*
                 * 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;
	if (debounce) {
		ret = gpio_set_debounce(gpio, debounce);
		if (ret < 0)
			return ret;
	}
	ctx->override_cd_active_level = true;
	ctx->cd_gpio = gpio_to_desc(gpio);
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| adrian hunter | adrian hunter | 85 | 84.16% | 2 | 33.33% | 
| guennadi liakhovetski | guennadi liakhovetski | 10 | 9.90% | 2 | 33.33% | 
| ulf hansson | ulf hansson | 6 | 5.94% | 2 | 33.33% | 
 | Total | 101 | 100.00% | 6 | 100.00% | 
EXPORT_SYMBOL(mmc_gpio_request_cd);
/**
 * 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
 * @gpio_invert: will return whether the GPIO line is inverted or not, set
 * to NULL to ignore
 *
 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
 * descriptor API.  Note 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, bool *gpio_invert)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	struct gpio_desc *desc;
	int ret;
	if (!con_id)
		con_id = ctx->cd_label;
	desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
	if (IS_ERR(desc))
		return PTR_ERR(desc);
	if (debounce) {
		ret = gpiod_set_debounce(desc, debounce);
		if (ret < 0)
			return ret;
	}
	if (gpio_invert)
		*gpio_invert = !gpiod_is_active_low(desc);
	ctx->override_cd_active_level = override_active_level;
	ctx->cd_gpio = desc;
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| adrian hunter | adrian hunter | 115 | 82.14% | 1 | 25.00% | 
| linus walleij | linus walleij | 19 | 13.57% | 2 | 50.00% | 
| ulf hansson | ulf hansson | 6 | 4.29% | 1 | 25.00% | 
 | Total | 140 | 100.00% | 4 | 100.00% | 
EXPORT_SYMBOL(mmc_gpiod_request_cd);
/**
 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
 * @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
 * @gpio_invert: will return whether the GPIO line is inverted or not,
 * set to NULL to ignore
 *
 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
 * descriptor API.
 *
 * Returns zero on success, else an error.
 */
int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
			 unsigned int idx, bool override_active_level,
			 unsigned int debounce, bool *gpio_invert)
{
	struct mmc_gpio *ctx = host->slot.handler_priv;
	struct gpio_desc *desc;
	int ret;
	if (!con_id)
		con_id = ctx->ro_label;
	desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
	if (IS_ERR(desc))
		return PTR_ERR(desc);
	if (debounce) {
		ret = gpiod_set_debounce(desc, debounce);
		if (ret < 0)
			return ret;
	}
	if (gpio_invert)
		*gpio_invert = !gpiod_is_active_low(desc);
	ctx->override_ro_active_level = override_active_level;
	ctx->ro_gpio = desc;
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus walleij | linus walleij | 134 | 95.71% | 2 | 66.67% | 
| ulf hansson | ulf hansson | 6 | 4.29% | 1 | 33.33% | 
 | Total | 140 | 100.00% | 3 | 100.00% | 
EXPORT_SYMBOL(mmc_gpiod_request_ro);
Overall Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| guennadi liakhovetski | guennadi liakhovetski | 479 | 44.03% | 9 | 36.00% | 
| adrian hunter | adrian hunter | 290 | 26.65% | 3 | 12.00% | 
| linus walleij | linus walleij | 158 | 14.52% | 3 | 12.00% | 
| neil brown | neil brown | 84 | 7.72% | 1 | 4.00% | 
| ulf hansson | ulf hansson | 41 | 3.77% | 4 | 16.00% | 
| chris ball | chris ball | 18 | 1.65% | 1 | 4.00% | 
| shawn guo | shawn guo | 8 | 0.74% | 1 | 4.00% | 
| laurent pinchart | laurent pinchart | 5 | 0.46% | 1 | 4.00% | 
| markus mayer | markus mayer | 3 | 0.28% | 1 | 4.00% | 
| h hartley sweeten | h hartley sweeten | 2 | 0.18% | 1 | 4.00% | 
 | Total | 1088 | 100.00% | 25 | 100.00% | 
  
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.