sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / mmc / core / slot-gpio.c
blobbabe591aea969204bb9500e6c30ff1e64bdafd81
1 /*
2 * Generic GPIO card-detect helper
4 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/err.h>
12 #include <linux/gpio.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/jiffies.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/slot-gpio.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include "slot-gpio.h"
23 struct mmc_gpio {
24 struct gpio_desc *ro_gpio;
25 struct gpio_desc *cd_gpio;
26 bool override_ro_active_level;
27 bool override_cd_active_level;
28 irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
29 char *ro_label;
30 char cd_label[0];
33 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
35 /* Schedule a card detection after a debounce timeout */
36 struct mmc_host *host = dev_id;
38 host->trigger_card_event = true;
39 mmc_detect_change(host, msecs_to_jiffies(200));
41 return IRQ_HANDLED;
44 int mmc_gpio_alloc(struct mmc_host *host)
46 size_t len = strlen(dev_name(host->parent)) + 4;
47 struct mmc_gpio *ctx = devm_kzalloc(host->parent,
48 sizeof(*ctx) + 2 * len, GFP_KERNEL);
50 if (ctx) {
51 ctx->ro_label = ctx->cd_label + len;
52 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
53 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
54 host->slot.handler_priv = ctx;
55 host->slot.cd_irq = -EINVAL;
58 return ctx ? 0 : -ENOMEM;
61 int mmc_gpio_get_ro(struct mmc_host *host)
63 struct mmc_gpio *ctx = host->slot.handler_priv;
65 if (!ctx || !ctx->ro_gpio)
66 return -ENOSYS;
68 if (ctx->override_ro_active_level)
69 return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
70 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
72 return gpiod_get_value_cansleep(ctx->ro_gpio);
74 EXPORT_SYMBOL(mmc_gpio_get_ro);
76 int mmc_gpio_get_cd(struct mmc_host *host)
78 struct mmc_gpio *ctx = host->slot.handler_priv;
80 if (!ctx || !ctx->cd_gpio)
81 return -ENOSYS;
83 if (ctx->override_cd_active_level)
84 return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
85 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
87 return gpiod_get_value_cansleep(ctx->cd_gpio);
89 EXPORT_SYMBOL(mmc_gpio_get_cd);
91 /**
92 * mmc_gpio_request_ro - request a gpio for write-protection
93 * @host: mmc host
94 * @gpio: gpio number requested
96 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
97 * drivers do not need to worry about freeing up memory.
99 * Returns zero on success, else an error.
101 int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
103 struct mmc_gpio *ctx = host->slot.handler_priv;
104 int ret;
106 if (!gpio_is_valid(gpio))
107 return -EINVAL;
109 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
110 ctx->ro_label);
111 if (ret < 0)
112 return ret;
114 ctx->override_ro_active_level = true;
115 ctx->ro_gpio = gpio_to_desc(gpio);
117 return 0;
119 EXPORT_SYMBOL(mmc_gpio_request_ro);
121 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
123 struct mmc_gpio *ctx = host->slot.handler_priv;
124 int ret, irq;
126 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
127 return;
129 irq = gpiod_to_irq(ctx->cd_gpio);
132 * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
133 * still prefer to poll, e.g., because that IRQ number is already used
134 * by another unit and cannot be shared.
136 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
137 irq = -EINVAL;
139 if (irq >= 0) {
140 if (!ctx->cd_gpio_isr)
141 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
142 ret = devm_request_threaded_irq(host->parent, irq,
143 NULL, ctx->cd_gpio_isr,
144 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
145 ctx->cd_label, host);
146 if (ret < 0)
147 irq = ret;
150 host->slot.cd_irq = irq;
152 if (irq < 0)
153 host->caps |= MMC_CAP_NEEDS_POLL;
155 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
157 /* Register an alternate interrupt service routine for
158 * the card-detect GPIO.
160 void mmc_gpio_set_cd_isr(struct mmc_host *host,
161 irqreturn_t (*isr)(int irq, void *dev_id))
163 struct mmc_gpio *ctx = host->slot.handler_priv;
165 WARN_ON(ctx->cd_gpio_isr);
166 ctx->cd_gpio_isr = isr;
168 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
171 * mmc_gpio_request_cd - request a gpio for card-detection
172 * @host: mmc host
173 * @gpio: gpio number requested
174 * @debounce: debounce time in microseconds
176 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
177 * drivers do not need to worry about freeing up memory.
179 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
180 * value. The caller is responsible for ensuring that the GPIO driver associated
181 * with the GPIO supports debouncing, otherwise an error will be returned.
183 * Returns zero on success, else an error.
185 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
186 unsigned int debounce)
188 struct mmc_gpio *ctx = host->slot.handler_priv;
189 int ret;
191 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
192 ctx->cd_label);
193 if (ret < 0)
195 * don't bother freeing memory. It might still get used by other
196 * slot functions, in any case it will be freed, when the device
197 * is destroyed.
199 return ret;
201 if (debounce) {
202 ret = gpio_set_debounce(gpio, debounce);
203 if (ret < 0)
204 return ret;
207 ctx->override_cd_active_level = true;
208 ctx->cd_gpio = gpio_to_desc(gpio);
210 return 0;
212 EXPORT_SYMBOL(mmc_gpio_request_cd);
215 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
216 * @host: mmc host
217 * @con_id: function within the GPIO consumer
218 * @idx: index of the GPIO to obtain in the consumer
219 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
220 * @debounce: debounce time in microseconds
221 * @gpio_invert: will return whether the GPIO line is inverted or not, set
222 * to NULL to ignore
224 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
225 * descriptor API. Note that it must be called prior to mmc_add_host()
226 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
228 * Returns zero on success, else an error.
230 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
231 unsigned int idx, bool override_active_level,
232 unsigned int debounce, bool *gpio_invert)
234 struct mmc_gpio *ctx = host->slot.handler_priv;
235 struct gpio_desc *desc;
236 int ret;
238 if (!con_id)
239 con_id = ctx->cd_label;
241 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
242 if (IS_ERR(desc))
243 return PTR_ERR(desc);
245 if (debounce) {
246 ret = gpiod_set_debounce(desc, debounce);
247 if (ret < 0)
248 return ret;
251 if (gpio_invert)
252 *gpio_invert = !gpiod_is_active_low(desc);
254 ctx->override_cd_active_level = override_active_level;
255 ctx->cd_gpio = desc;
257 return 0;
259 EXPORT_SYMBOL(mmc_gpiod_request_cd);
261 bool mmc_can_gpio_cd(struct mmc_host *host)
263 struct mmc_gpio *ctx = host->slot.handler_priv;
265 return ctx->cd_gpio ? true : false;
267 EXPORT_SYMBOL(mmc_can_gpio_cd);
270 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
271 * @host: mmc host
272 * @con_id: function within the GPIO consumer
273 * @idx: index of the GPIO to obtain in the consumer
274 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
275 * @debounce: debounce time in microseconds
276 * @gpio_invert: will return whether the GPIO line is inverted or not,
277 * set to NULL to ignore
279 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
280 * descriptor API.
282 * Returns zero on success, else an error.
284 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
285 unsigned int idx, bool override_active_level,
286 unsigned int debounce, bool *gpio_invert)
288 struct mmc_gpio *ctx = host->slot.handler_priv;
289 struct gpio_desc *desc;
290 int ret;
292 if (!con_id)
293 con_id = ctx->ro_label;
295 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
296 if (IS_ERR(desc))
297 return PTR_ERR(desc);
299 if (debounce) {
300 ret = gpiod_set_debounce(desc, debounce);
301 if (ret < 0)
302 return ret;
305 if (gpio_invert)
306 *gpio_invert = !gpiod_is_active_low(desc);
308 ctx->override_ro_active_level = override_active_level;
309 ctx->ro_gpio = desc;
311 return 0;
313 EXPORT_SYMBOL(mmc_gpiod_request_ro);