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