Loading...
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
19struct 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
28static 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
40int 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
62int mmc_gpio_get_ro(struct mmc_host *host)
63{
64 struct mmc_gpio *ctx = host->slot.handler_priv;
65
66 if (!ctx || !ctx->ro_gpio)
67 return -ENOSYS;
68
69 return gpiod_get_value_cansleep(ctx->ro_gpio);
70}
71EXPORT_SYMBOL(mmc_gpio_get_ro);
72
73int mmc_gpio_get_cd(struct mmc_host *host)
74{
75 struct mmc_gpio *ctx = host->slot.handler_priv;
76 int cansleep;
77
78 if (!ctx || !ctx->cd_gpio)
79 return -ENOSYS;
80
81 cansleep = gpiod_cansleep(ctx->cd_gpio);
82 return cansleep ?
83 gpiod_get_value_cansleep(ctx->cd_gpio) :
84 gpiod_get_value(ctx->cd_gpio);
85}
86EXPORT_SYMBOL(mmc_gpio_get_cd);
87
88void mmc_gpiod_request_cd_irq(struct mmc_host *host)
89{
90 struct mmc_gpio *ctx = host->slot.handler_priv;
91 int irq = -EINVAL;
92 int ret;
93
94 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
95 return;
96
97 /*
98 * Do not use IRQ if the platform prefers to poll, e.g., because that
99 * IRQ number is already used by another unit and cannot be shared.
100 */
101 if (!(host->caps & MMC_CAP_NEEDS_POLL))
102 irq = gpiod_to_irq(ctx->cd_gpio);
103
104 if (irq >= 0) {
105 if (!ctx->cd_gpio_isr)
106 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
107 ret = devm_request_threaded_irq(host->parent, irq,
108 NULL, ctx->cd_gpio_isr,
109 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
110 ctx->cd_label, host);
111 if (ret < 0)
112 irq = ret;
113 }
114
115 host->slot.cd_irq = irq;
116
117 if (irq < 0)
118 host->caps |= MMC_CAP_NEEDS_POLL;
119}
120EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
121
122int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
123{
124 int ret = 0;
125
126 if (!(host->caps & MMC_CAP_CD_WAKE) ||
127 host->slot.cd_irq < 0 ||
128 on == host->slot.cd_wake_enabled)
129 return 0;
130
131 if (on) {
132 ret = enable_irq_wake(host->slot.cd_irq);
133 host->slot.cd_wake_enabled = !ret;
134 } else {
135 disable_irq_wake(host->slot.cd_irq);
136 host->slot.cd_wake_enabled = false;
137 }
138
139 return ret;
140}
141EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
142
143/* Register an alternate interrupt service routine for
144 * the card-detect GPIO.
145 */
146void mmc_gpio_set_cd_isr(struct mmc_host *host,
147 irqreturn_t (*isr)(int irq, void *dev_id))
148{
149 struct mmc_gpio *ctx = host->slot.handler_priv;
150
151 WARN_ON(ctx->cd_gpio_isr);
152 ctx->cd_gpio_isr = isr;
153}
154EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
155
156/**
157 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
158 * @host: mmc host
159 * @con_id: function within the GPIO consumer
160 * @idx: index of the GPIO to obtain in the consumer
161 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
162 * @debounce: debounce time in microseconds
163 *
164 * Note that this must be called prior to mmc_add_host()
165 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
166 *
167 * Returns zero on success, else an error.
168 */
169int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
170 unsigned int idx, bool override_active_level,
171 unsigned int debounce)
172{
173 struct mmc_gpio *ctx = host->slot.handler_priv;
174 struct gpio_desc *desc;
175 int ret;
176
177 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
178 if (IS_ERR(desc))
179 return PTR_ERR(desc);
180
181 /* Update default label if no con_id provided */
182 if (!con_id)
183 gpiod_set_consumer_name(desc, ctx->cd_label);
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}
203EXPORT_SYMBOL(mmc_gpiod_request_cd);
204
205bool 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}
211EXPORT_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 */
222int 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 /* Update default label if no con_id provided */
234 if (!con_id)
235 gpiod_set_consumer_name(desc, ctx->ro_label);
236
237 if (debounce) {
238 ret = gpiod_set_debounce(desc, debounce);
239 if (ret < 0)
240 return ret;
241 }
242
243 if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
244 gpiod_toggle_active_low(desc);
245
246 ctx->ro_gpio = desc;
247
248 return 0;
249}
250EXPORT_SYMBOL(mmc_gpiod_request_ro);
251
252bool mmc_can_gpio_ro(struct mmc_host *host)
253{
254 struct mmc_gpio *ctx = host->slot.handler_priv;
255
256 return ctx->ro_gpio ? true : false;
257}
258EXPORT_SYMBOL(mmc_can_gpio_ro);
1/*
2 * Generic GPIO card-detect helper
3 *
4 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 *
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 */
10
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>
20
21#include "slot-gpio.h"
22
23struct 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];
31};
32
33static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
34{
35 /* Schedule a card detection after a debounce timeout */
36 struct mmc_host *host = dev_id;
37
38 host->trigger_card_event = true;
39 mmc_detect_change(host, msecs_to_jiffies(200));
40
41 return IRQ_HANDLED;
42}
43
44int mmc_gpio_alloc(struct mmc_host *host)
45{
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);
49
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;
56 }
57
58 return ctx ? 0 : -ENOMEM;
59}
60
61int mmc_gpio_get_ro(struct mmc_host *host)
62{
63 struct mmc_gpio *ctx = host->slot.handler_priv;
64
65 if (!ctx || !ctx->ro_gpio)
66 return -ENOSYS;
67
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);
71
72 return gpiod_get_value_cansleep(ctx->ro_gpio);
73}
74EXPORT_SYMBOL(mmc_gpio_get_ro);
75
76int mmc_gpio_get_cd(struct mmc_host *host)
77{
78 struct mmc_gpio *ctx = host->slot.handler_priv;
79
80 if (!ctx || !ctx->cd_gpio)
81 return -ENOSYS;
82
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);
86
87 return gpiod_get_value_cansleep(ctx->cd_gpio);
88}
89EXPORT_SYMBOL(mmc_gpio_get_cd);
90
91/**
92 * mmc_gpio_request_ro - request a gpio for write-protection
93 * @host: mmc host
94 * @gpio: gpio number requested
95 *
96 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
97 * drivers do not need to worry about freeing up memory.
98 *
99 * Returns zero on success, else an error.
100 */
101int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
102{
103 struct mmc_gpio *ctx = host->slot.handler_priv;
104 int ret;
105
106 if (!gpio_is_valid(gpio))
107 return -EINVAL;
108
109 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
110 ctx->ro_label);
111 if (ret < 0)
112 return ret;
113
114 ctx->override_ro_active_level = true;
115 ctx->ro_gpio = gpio_to_desc(gpio);
116
117 return 0;
118}
119EXPORT_SYMBOL(mmc_gpio_request_ro);
120
121void mmc_gpiod_request_cd_irq(struct mmc_host *host)
122{
123 struct mmc_gpio *ctx = host->slot.handler_priv;
124 int irq = -EINVAL;
125 int ret;
126
127 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
128 return;
129
130 /*
131 * Do not use IRQ if the platform prefers to poll, e.g., because that
132 * IRQ number is already used by another unit and cannot be shared.
133 */
134 if (!(host->caps & MMC_CAP_NEEDS_POLL))
135 irq = gpiod_to_irq(ctx->cd_gpio);
136
137 if (irq >= 0) {
138 if (!ctx->cd_gpio_isr)
139 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
140 ret = devm_request_threaded_irq(host->parent, irq,
141 NULL, ctx->cd_gpio_isr,
142 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
143 ctx->cd_label, host);
144 if (ret < 0)
145 irq = ret;
146 }
147
148 host->slot.cd_irq = irq;
149
150 if (irq < 0)
151 host->caps |= MMC_CAP_NEEDS_POLL;
152}
153EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
154
155int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
156{
157 int ret = 0;
158
159 if (!(host->caps & MMC_CAP_CD_WAKE) ||
160 host->slot.cd_irq < 0 ||
161 on == host->slot.cd_wake_enabled)
162 return 0;
163
164 if (on) {
165 ret = enable_irq_wake(host->slot.cd_irq);
166 host->slot.cd_wake_enabled = !ret;
167 } else {
168 disable_irq_wake(host->slot.cd_irq);
169 host->slot.cd_wake_enabled = false;
170 }
171
172 return ret;
173}
174EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
175
176/* Register an alternate interrupt service routine for
177 * the card-detect GPIO.
178 */
179void mmc_gpio_set_cd_isr(struct mmc_host *host,
180 irqreturn_t (*isr)(int irq, void *dev_id))
181{
182 struct mmc_gpio *ctx = host->slot.handler_priv;
183
184 WARN_ON(ctx->cd_gpio_isr);
185 ctx->cd_gpio_isr = isr;
186}
187EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
188
189/**
190 * mmc_gpio_request_cd - request a gpio for card-detection
191 * @host: mmc host
192 * @gpio: gpio number requested
193 * @debounce: debounce time in microseconds
194 *
195 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
196 * drivers do not need to worry about freeing up memory.
197 *
198 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
199 * value. The caller is responsible for ensuring that the GPIO driver associated
200 * with the GPIO supports debouncing, otherwise an error will be returned.
201 *
202 * Returns zero on success, else an error.
203 */
204int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
205 unsigned int debounce)
206{
207 struct mmc_gpio *ctx = host->slot.handler_priv;
208 int ret;
209
210 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
211 ctx->cd_label);
212 if (ret < 0)
213 /*
214 * don't bother freeing memory. It might still get used by other
215 * slot functions, in any case it will be freed, when the device
216 * is destroyed.
217 */
218 return ret;
219
220 if (debounce) {
221 ret = gpio_set_debounce(gpio, debounce);
222 if (ret < 0)
223 return ret;
224 }
225
226 ctx->override_cd_active_level = true;
227 ctx->cd_gpio = gpio_to_desc(gpio);
228
229 return 0;
230}
231EXPORT_SYMBOL(mmc_gpio_request_cd);
232
233/**
234 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
235 * @host: mmc host
236 * @con_id: function within the GPIO consumer
237 * @idx: index of the GPIO to obtain in the consumer
238 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
239 * @debounce: debounce time in microseconds
240 * @gpio_invert: will return whether the GPIO line is inverted or not, set
241 * to NULL to ignore
242 *
243 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
244 * descriptor API. Note that it must be called prior to mmc_add_host()
245 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
246 *
247 * Returns zero on success, else an error.
248 */
249int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
250 unsigned int idx, bool override_active_level,
251 unsigned int debounce, bool *gpio_invert)
252{
253 struct mmc_gpio *ctx = host->slot.handler_priv;
254 struct gpio_desc *desc;
255 int ret;
256
257 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
258 if (IS_ERR(desc))
259 return PTR_ERR(desc);
260
261 if (debounce) {
262 ret = gpiod_set_debounce(desc, debounce);
263 if (ret < 0)
264 return ret;
265 }
266
267 if (gpio_invert)
268 *gpio_invert = !gpiod_is_active_low(desc);
269
270 ctx->override_cd_active_level = override_active_level;
271 ctx->cd_gpio = desc;
272
273 return 0;
274}
275EXPORT_SYMBOL(mmc_gpiod_request_cd);
276
277bool mmc_can_gpio_cd(struct mmc_host *host)
278{
279 struct mmc_gpio *ctx = host->slot.handler_priv;
280
281 return ctx->cd_gpio ? true : false;
282}
283EXPORT_SYMBOL(mmc_can_gpio_cd);
284
285/**
286 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
287 * @host: mmc host
288 * @con_id: function within the GPIO consumer
289 * @idx: index of the GPIO to obtain in the consumer
290 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
291 * @debounce: debounce time in microseconds
292 * @gpio_invert: will return whether the GPIO line is inverted or not,
293 * set to NULL to ignore
294 *
295 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
296 * descriptor API.
297 *
298 * Returns zero on success, else an error.
299 */
300int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
301 unsigned int idx, bool override_active_level,
302 unsigned int debounce, bool *gpio_invert)
303{
304 struct mmc_gpio *ctx = host->slot.handler_priv;
305 struct gpio_desc *desc;
306 int ret;
307
308 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
309 if (IS_ERR(desc))
310 return PTR_ERR(desc);
311
312 if (debounce) {
313 ret = gpiod_set_debounce(desc, debounce);
314 if (ret < 0)
315 return ret;
316 }
317
318 if (gpio_invert)
319 *gpio_invert = !gpiod_is_active_low(desc);
320
321 ctx->override_ro_active_level = override_active_level;
322 ctx->ro_gpio = desc;
323
324 return 0;
325}
326EXPORT_SYMBOL(mmc_gpiod_request_ro);
327
328bool mmc_can_gpio_ro(struct mmc_host *host)
329{
330 struct mmc_gpio *ctx = host->slot.handler_priv;
331
332 return ctx->ro_gpio ? true : false;
333}
334EXPORT_SYMBOL(mmc_can_gpio_ro);