• 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 	struct mmc_gpio *ctx = devm_kzalloc(host->parent,
43 					    sizeof(*ctx), GFP_KERNEL);
44 
45 	if (ctx) {
46 		ctx->cd_debounce_delay_ms = 200;
47 		ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL,
48 				"%s cd", dev_name(host->parent));
49 		if (!ctx->cd_label)
50 			return -ENOMEM;
51 		ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL,
52 				"%s ro", dev_name(host->parent));
53 		if (!ctx->ro_label)
54 			return -ENOMEM;
55 		host->slot.handler_priv = ctx;
56 		host->slot.cd_irq = -EINVAL;
57 	}
58 
59 	return ctx ? 0 : -ENOMEM;
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 	if (debounce) {
186 		ret = gpiod_set_debounce(desc, debounce);
187 		if (ret < 0)
188 			ctx->cd_debounce_delay_ms = debounce / 1000;
189 	}
190 
191 	/* override forces default (active-low) polarity ... */
192 	if (override_active_level && !gpiod_is_active_low(desc))
193 		gpiod_toggle_active_low(desc);
194 
195 	/* ... or active-high */
196 	if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
197 		gpiod_toggle_active_low(desc);
198 
199 	ctx->cd_gpio = desc;
200 
201 	return 0;
202 }
203 EXPORT_SYMBOL(mmc_gpiod_request_cd);
204 
mmc_can_gpio_cd(struct mmc_host * host)205 bool mmc_can_gpio_cd(struct mmc_host *host)
206 {
207 	struct mmc_gpio *ctx = host->slot.handler_priv;
208 
209 	return ctx->cd_gpio ? true : false;
210 }
211 EXPORT_SYMBOL(mmc_can_gpio_cd);
212 
213 /**
214  * mmc_gpiod_request_ro - request a gpio descriptor for write protection
215  * @host: mmc host
216  * @con_id: function within the GPIO consumer
217  * @idx: index of the GPIO to obtain in the consumer
218  * @debounce: debounce time in microseconds
219  *
220  * Returns zero on success, else an error.
221  */
mmc_gpiod_request_ro(struct mmc_host * host,const char * con_id,unsigned int idx,unsigned int debounce)222 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
223 			 unsigned int idx, unsigned int debounce)
224 {
225 	struct mmc_gpio *ctx = host->slot.handler_priv;
226 	struct gpio_desc *desc;
227 	int ret;
228 
229 	desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
230 	if (IS_ERR(desc))
231 		return PTR_ERR(desc);
232 
233 	if (debounce) {
234 		ret = gpiod_set_debounce(desc, debounce);
235 		if (ret < 0)
236 			return ret;
237 	}
238 
239 	if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
240 		gpiod_toggle_active_low(desc);
241 
242 	ctx->ro_gpio = desc;
243 
244 	return 0;
245 }
246 EXPORT_SYMBOL(mmc_gpiod_request_ro);
247 
mmc_can_gpio_ro(struct mmc_host * host)248 bool mmc_can_gpio_ro(struct mmc_host *host)
249 {
250 	struct mmc_gpio *ctx = host->slot.handler_priv;
251 
252 	return ctx->ro_gpio ? true : false;
253 }
254 EXPORT_SYMBOL(mmc_can_gpio_ro);
255