• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic GPIO card-detect helper
4  *
5  * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6  */
7 
8 #include <linux/err.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/interrupt.h>
11 #include <linux/jiffies.h>
12 #include <linux/mmc/host.h>
13 #include <linux/mmc/slot-gpio.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 
17 #include "slot-gpio.h"
18 
19 struct mmc_gpio {
20 	struct gpio_desc *ro_gpio;
21 	struct gpio_desc *cd_gpio;
22 	bool override_cd_active_level;
23 	irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
24 	char *ro_label;
25 	char *cd_label;
26 	u32 cd_debounce_delay_ms;
27 };
28 
mmc_gpio_cd_irqt(int irq,void * dev_id)29 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
30 {
31 	/* Schedule a card detection after a debounce timeout */
32 	struct mmc_host *host = dev_id;
33 	struct mmc_gpio *ctx = host->slot.handler_priv;
34 
35 	host->trigger_card_event = true;
36 	mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms));
37 
38 	return IRQ_HANDLED;
39 }
40 
mmc_gpio_alloc(struct mmc_host * host)41 int mmc_gpio_alloc(struct mmc_host *host)
42 {
43 	struct mmc_gpio *ctx = devm_kzalloc(host->parent,
44 					    sizeof(*ctx), GFP_KERNEL);
45 
46 	if (ctx) {
47 		ctx->cd_debounce_delay_ms = 200;
48 		ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL,
49 				"%s cd", dev_name(host->parent));
50 		if (!ctx->cd_label)
51 			return -ENOMEM;
52 		ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL,
53 				"%s ro", dev_name(host->parent));
54 		if (!ctx->ro_label)
55 			return -ENOMEM;
56 		host->slot.handler_priv = ctx;
57 		host->slot.cd_irq = -EINVAL;
58 	}
59 
60 	return ctx ? 0 : -ENOMEM;
61 }
62 
mmc_gpio_get_ro(struct mmc_host * host)63 int mmc_gpio_get_ro(struct mmc_host *host)
64 {
65 	struct mmc_gpio *ctx = host->slot.handler_priv;
66 	int cansleep;
67 
68 	if (!ctx || !ctx->ro_gpio)
69 		return -ENOSYS;
70 
71 	cansleep = gpiod_cansleep(ctx->ro_gpio);
72 	return cansleep ?
73 		gpiod_get_value_cansleep(ctx->ro_gpio) :
74 		gpiod_get_value(ctx->ro_gpio);
75 }
76 EXPORT_SYMBOL(mmc_gpio_get_ro);
77 
mmc_gpio_get_cd(struct mmc_host * host)78 int mmc_gpio_get_cd(struct mmc_host *host)
79 {
80 	struct mmc_gpio *ctx = host->slot.handler_priv;
81 	int cansleep;
82 
83 	if (!ctx || !ctx->cd_gpio)
84 		return -ENOSYS;
85 
86 	cansleep = gpiod_cansleep(ctx->cd_gpio);
87 	if (ctx->override_cd_active_level) {
88 		int value = cansleep ?
89 				gpiod_get_raw_value_cansleep(ctx->cd_gpio) :
90 				gpiod_get_raw_value(ctx->cd_gpio);
91 		return !value ^ !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
92 	}
93 
94 	return cansleep ?
95 		gpiod_get_value_cansleep(ctx->cd_gpio) :
96 		gpiod_get_value(ctx->cd_gpio);
97 }
98 EXPORT_SYMBOL(mmc_gpio_get_cd);
99 
mmc_gpiod_request_cd_irq(struct mmc_host * host)100 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
101 {
102 	struct mmc_gpio *ctx = host->slot.handler_priv;
103 	int irq = -EINVAL;
104 	int ret;
105 
106 	if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
107 		return;
108 
109 	/*
110 	 * Do not use IRQ if the platform prefers to poll, e.g., because that
111 	 * IRQ number is already used by another unit and cannot be shared.
112 	 */
113 	if (!(host->caps & MMC_CAP_NEEDS_POLL))
114 		irq = gpiod_to_irq(ctx->cd_gpio);
115 
116 	if (irq >= 0) {
117 		if (!ctx->cd_gpio_isr)
118 			ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
119 		ret = devm_request_threaded_irq(host->parent, irq,
120 			NULL, ctx->cd_gpio_isr,
121 			IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
122 			ctx->cd_label, host);
123 		if (ret < 0)
124 			irq = ret;
125 	}
126 
127 	host->slot.cd_irq = irq;
128 
129 	if (irq < 0)
130 		host->caps |= MMC_CAP_NEEDS_POLL;
131 }
132 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
133 
mmc_gpio_set_cd_wake(struct mmc_host * host,bool on)134 int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
135 {
136 	int ret = 0;
137 
138 	if (!(host->caps & MMC_CAP_CD_WAKE) ||
139 	    host->slot.cd_irq < 0 ||
140 	    on == host->slot.cd_wake_enabled)
141 		return 0;
142 
143 	if (on) {
144 		ret = enable_irq_wake(host->slot.cd_irq);
145 		host->slot.cd_wake_enabled = !ret;
146 	} else {
147 		disable_irq_wake(host->slot.cd_irq);
148 		host->slot.cd_wake_enabled = false;
149 	}
150 
151 	return ret;
152 }
153 EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
154 
155 /* Register an alternate interrupt service routine for
156  * the card-detect GPIO.
157  */
mmc_gpio_set_cd_isr(struct mmc_host * host,irqreturn_t (* isr)(int irq,void * dev_id))158 void mmc_gpio_set_cd_isr(struct mmc_host *host,
159 			 irqreturn_t (*isr)(int irq, void *dev_id))
160 {
161 	struct mmc_gpio *ctx = host->slot.handler_priv;
162 
163 	WARN_ON(ctx->cd_gpio_isr);
164 	ctx->cd_gpio_isr = isr;
165 }
166 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
167 
168 /**
169  * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
170  * @host: mmc host
171  * @con_id: function within the GPIO consumer
172  * @idx: index of the GPIO to obtain in the consumer
173  * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
174  * @debounce: debounce time in microseconds
175  * @gpio_invert: will return whether the GPIO line is inverted or not, set
176  * to NULL to ignore
177  *
178  * Note that this must be called prior to mmc_add_host()
179  * otherwise the caller must also call mmc_gpiod_request_cd_irq().
180  *
181  * Returns zero on success, else an error.
182  */
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)183 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
184 			 unsigned int idx, bool override_active_level,
185 			 unsigned int debounce, bool *gpio_invert)
186 {
187 	struct mmc_gpio *ctx = host->slot.handler_priv;
188 	struct gpio_desc *desc;
189 	int ret;
190 
191 	desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
192 	if (IS_ERR(desc))
193 		return PTR_ERR(desc);
194 
195 	if (debounce) {
196 		ret = gpiod_set_debounce(desc, debounce);
197 		if (ret < 0)
198 			ctx->cd_debounce_delay_ms = debounce / 1000;
199 	}
200 
201 	if (gpio_invert)
202 		*gpio_invert = !gpiod_is_active_low(desc);
203 
204 	ctx->override_cd_active_level = override_active_level;
205 	ctx->cd_gpio = desc;
206 
207 	return 0;
208 }
209 EXPORT_SYMBOL(mmc_gpiod_request_cd);
210 
mmc_can_gpio_cd(struct mmc_host * host)211 bool mmc_can_gpio_cd(struct mmc_host *host)
212 {
213 	struct mmc_gpio *ctx = host->slot.handler_priv;
214 
215 	return ctx->cd_gpio ? true : false;
216 }
217 EXPORT_SYMBOL(mmc_can_gpio_cd);
218 
219 /**
220  * mmc_gpiod_request_ro - request a gpio descriptor for write protection
221  * @host: mmc host
222  * @con_id: function within the GPIO consumer
223  * @idx: index of the GPIO to obtain in the consumer
224  * @debounce: debounce time in microseconds
225  * @gpio_invert: will return whether the GPIO line is inverted or not,
226  * set to NULL to ignore
227  *
228  * Returns zero on success, else an error.
229  */
mmc_gpiod_request_ro(struct mmc_host * host,const char * con_id,unsigned int idx,unsigned int debounce,bool * gpio_invert)230 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
231 			 unsigned int idx,
232 			 unsigned int debounce, bool *gpio_invert)
233 {
234 	struct mmc_gpio *ctx = host->slot.handler_priv;
235 	struct gpio_desc *desc;
236 	int ret;
237 
238 	desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
239 	if (IS_ERR(desc))
240 		return PTR_ERR(desc);
241 
242 	if (debounce) {
243 		ret = gpiod_set_debounce(desc, debounce);
244 		if (ret < 0)
245 			return ret;
246 	}
247 
248 	if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
249 		gpiod_toggle_active_low(desc);
250 
251 	if (gpio_invert)
252 		*gpio_invert = !gpiod_is_active_low(desc);
253 
254 	ctx->ro_gpio = desc;
255 
256 	return 0;
257 }
258 EXPORT_SYMBOL(mmc_gpiod_request_ro);
259 
mmc_can_gpio_ro(struct mmc_host * host)260 bool mmc_can_gpio_ro(struct mmc_host *host)
261 {
262 	struct mmc_gpio *ctx = host->slot.handler_priv;
263 
264 	return ctx->ro_gpio ? true : false;
265 }
266 EXPORT_SYMBOL(mmc_can_gpio_ro);
267