1
2
3
4
5
6
7
8
9
10
11#include <linux/err.h>
12#include <linux/gpio.h>
13#include <linux/interrupt.h>
14#include <linux/jiffies.h>
15#include <linux/mmc/host.h>
16#include <linux/mmc/slot-gpio.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19
20struct mmc_gpio {
21 int ro_gpio;
22 int cd_gpio;
23 char *ro_label;
24 char cd_label[0];
25};
26
27static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
28{
29
30 struct mmc_host *host = dev_id;
31
32 if (host->ops->card_event)
33 host->ops->card_event(host);
34
35 mmc_detect_change(host, msecs_to_jiffies(200));
36
37 return IRQ_HANDLED;
38}
39
40static int mmc_gpio_alloc(struct mmc_host *host)
41{
42 size_t len = strlen(dev_name(host->parent)) + 4;
43 struct mmc_gpio *ctx;
44
45 mutex_lock(&host->slot.lock);
46
47 ctx = host->slot.handler_priv;
48 if (!ctx) {
49
50
51
52
53
54 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
55 GFP_KERNEL);
56 if (ctx) {
57 ctx->ro_label = ctx->cd_label + len;
58 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
59 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
60 ctx->cd_gpio = -EINVAL;
61 ctx->ro_gpio = -EINVAL;
62 host->slot.handler_priv = ctx;
63 }
64 }
65
66 mutex_unlock(&host->slot.lock);
67
68 return ctx ? 0 : -ENOMEM;
69}
70
71int mmc_gpio_get_ro(struct mmc_host *host)
72{
73 struct mmc_gpio *ctx = host->slot.handler_priv;
74
75 if (!ctx || !gpio_is_valid(ctx->ro_gpio))
76 return -ENOSYS;
77
78 return !gpio_get_value_cansleep(ctx->ro_gpio) ^
79 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
80}
81EXPORT_SYMBOL(mmc_gpio_get_ro);
82
83int mmc_gpio_get_cd(struct mmc_host *host)
84{
85 struct mmc_gpio *ctx = host->slot.handler_priv;
86
87 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
88 return -ENOSYS;
89
90 return !gpio_get_value_cansleep(ctx->cd_gpio) ^
91 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
92}
93EXPORT_SYMBOL(mmc_gpio_get_cd);
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
110{
111 struct mmc_gpio *ctx;
112 int ret;
113
114 if (!gpio_is_valid(gpio))
115 return -EINVAL;
116
117 ret = mmc_gpio_alloc(host);
118 if (ret < 0)
119 return ret;
120
121 ctx = host->slot.handler_priv;
122
123 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
124 ctx->ro_label);
125 if (ret < 0)
126 return ret;
127
128 ctx->ro_gpio = gpio;
129
130 return 0;
131}
132EXPORT_SYMBOL(mmc_gpio_request_ro);
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
154 unsigned int debounce)
155{
156 struct mmc_gpio *ctx;
157 int irq = gpio_to_irq(gpio);
158 int ret;
159
160 ret = mmc_gpio_alloc(host);
161 if (ret < 0)
162 return ret;
163
164 ctx = host->slot.handler_priv;
165
166 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
167 ctx->cd_label);
168 if (ret < 0)
169
170
171
172
173
174 return ret;
175
176 if (debounce) {
177 ret = gpio_set_debounce(gpio, debounce);
178 if (ret < 0)
179 return ret;
180 }
181
182
183
184
185
186
187 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
188 irq = -EINVAL;
189
190 if (irq >= 0) {
191 ret = devm_request_threaded_irq(&host->class_dev, irq,
192 NULL, mmc_gpio_cd_irqt,
193 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
194 ctx->cd_label, host);
195 if (ret < 0)
196 irq = ret;
197 }
198
199 host->slot.cd_irq = irq;
200
201 if (irq < 0)
202 host->caps |= MMC_CAP_NEEDS_POLL;
203
204 ctx->cd_gpio = gpio;
205
206 return 0;
207}
208EXPORT_SYMBOL(mmc_gpio_request_cd);
209
210
211
212
213
214
215
216
217void mmc_gpio_free_ro(struct mmc_host *host)
218{
219 struct mmc_gpio *ctx = host->slot.handler_priv;
220 int gpio;
221
222 if (!ctx || !gpio_is_valid(ctx->ro_gpio))
223 return;
224
225 gpio = ctx->ro_gpio;
226 ctx->ro_gpio = -EINVAL;
227
228 devm_gpio_free(&host->class_dev, gpio);
229}
230EXPORT_SYMBOL(mmc_gpio_free_ro);
231
232
233
234
235
236
237
238
239void mmc_gpio_free_cd(struct mmc_host *host)
240{
241 struct mmc_gpio *ctx = host->slot.handler_priv;
242 int gpio;
243
244 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
245 return;
246
247 if (host->slot.cd_irq >= 0) {
248 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
249 host->slot.cd_irq = -EINVAL;
250 }
251
252 gpio = ctx->cd_gpio;
253 ctx->cd_gpio = -EINVAL;
254
255 devm_gpio_free(&host->class_dev, gpio);
256}
257EXPORT_SYMBOL(mmc_gpio_free_cd);
258