mmc: slot-gpio: Record GPIO descriptors instead of GPIO numbers
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / core / slot-gpio.c
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 struct mmc_gpio {
22         struct gpio_desc *ro_gpio;
23         struct gpio_desc *cd_gpio;
24         bool override_ro_active_level;
25         bool override_cd_active_level;
26         char *ro_label;
27         char cd_label[0];
28 };
29
30 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
31 {
32         /* Schedule a card detection after a debounce timeout */
33         struct mmc_host *host = dev_id;
34
35         if (host->ops->card_event)
36                 host->ops->card_event(host);
37
38         mmc_detect_change(host, msecs_to_jiffies(200));
39
40         return IRQ_HANDLED;
41 }
42
43 static int mmc_gpio_alloc(struct mmc_host *host)
44 {
45         size_t len = strlen(dev_name(host->parent)) + 4;
46         struct mmc_gpio *ctx;
47
48         mutex_lock(&host->slot.lock);
49
50         ctx = host->slot.handler_priv;
51         if (!ctx) {
52                 /*
53                  * devm_kzalloc() can be called after device_initialize(), even
54                  * before device_add(), i.e., between mmc_alloc_host() and
55                  * mmc_add_host()
56                  */
57                 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
58                                    GFP_KERNEL);
59                 if (ctx) {
60                         ctx->ro_label = ctx->cd_label + len;
61                         snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
62                         snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
63                         host->slot.handler_priv = ctx;
64                 }
65         }
66
67         mutex_unlock(&host->slot.lock);
68
69         return ctx ? 0 : -ENOMEM;
70 }
71
72 int mmc_gpio_get_ro(struct mmc_host *host)
73 {
74         struct mmc_gpio *ctx = host->slot.handler_priv;
75
76         if (!ctx || !ctx->ro_gpio)
77                 return -ENOSYS;
78
79         if (ctx->override_ro_active_level)
80                 return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
81                         !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
82
83         return gpiod_get_value_cansleep(ctx->ro_gpio);
84 }
85 EXPORT_SYMBOL(mmc_gpio_get_ro);
86
87 int mmc_gpio_get_cd(struct mmc_host *host)
88 {
89         struct mmc_gpio *ctx = host->slot.handler_priv;
90
91         if (!ctx || !ctx->cd_gpio)
92                 return -ENOSYS;
93
94         if (ctx->override_cd_active_level)
95                 return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
96                         !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
97
98         return gpiod_get_value_cansleep(ctx->cd_gpio);
99 }
100 EXPORT_SYMBOL(mmc_gpio_get_cd);
101
102 /**
103  * mmc_gpio_request_ro - request a gpio for write-protection
104  * @host: mmc host
105  * @gpio: gpio number requested
106  *
107  * As devm_* managed functions are used in mmc_gpio_request_ro(), client
108  * drivers do not need to explicitly call mmc_gpio_free_ro() for freeing up,
109  * if the requesting and freeing are only needed at probing and unbinding time
110  * for once.  However, if client drivers do something special like runtime
111  * switching for write-protection, they are responsible for calling
112  * mmc_gpio_request_ro() and mmc_gpio_free_ro() as a pair on their own.
113  *
114  * Returns zero on success, else an error.
115  */
116 int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
117 {
118         struct mmc_gpio *ctx;
119         int ret;
120
121         if (!gpio_is_valid(gpio))
122                 return -EINVAL;
123
124         ret = mmc_gpio_alloc(host);
125         if (ret < 0)
126                 return ret;
127
128         ctx = host->slot.handler_priv;
129
130         ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
131                                     ctx->ro_label);
132         if (ret < 0)
133                 return ret;
134
135         ctx->override_ro_active_level = true;
136         ctx->ro_gpio = gpio_to_desc(gpio);
137
138         return 0;
139 }
140 EXPORT_SYMBOL(mmc_gpio_request_ro);
141
142 /**
143  * mmc_gpio_request_cd - request a gpio for card-detection
144  * @host: mmc host
145  * @gpio: gpio number requested
146  * @debounce: debounce time in microseconds
147  *
148  * As devm_* managed functions are used in mmc_gpio_request_cd(), client
149  * drivers do not need to explicitly call mmc_gpio_free_cd() for freeing up,
150  * if the requesting and freeing are only needed at probing and unbinding time
151  * for once.  However, if client drivers do something special like runtime
152  * switching for card-detection, they are responsible for calling
153  * mmc_gpio_request_cd() and mmc_gpio_free_cd() as a pair on their own.
154  *
155  * If GPIO debouncing is desired, set the debounce parameter to a non-zero
156  * value. The caller is responsible for ensuring that the GPIO driver associated
157  * with the GPIO supports debouncing, otherwise an error will be returned.
158  *
159  * Returns zero on success, else an error.
160  */
161 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
162                         unsigned int debounce)
163 {
164         struct mmc_gpio *ctx;
165         int irq = gpio_to_irq(gpio);
166         int ret;
167
168         ret = mmc_gpio_alloc(host);
169         if (ret < 0)
170                 return ret;
171
172         ctx = host->slot.handler_priv;
173
174         ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
175                                     ctx->cd_label);
176         if (ret < 0)
177                 /*
178                  * don't bother freeing memory. It might still get used by other
179                  * slot functions, in any case it will be freed, when the device
180                  * is destroyed.
181                  */
182                 return ret;
183
184         if (debounce) {
185                 ret = gpio_set_debounce(gpio, debounce);
186                 if (ret < 0)
187                         return ret;
188         }
189
190         /*
191          * Even if gpio_to_irq() returns a valid IRQ number, the platform might
192          * still prefer to poll, e.g., because that IRQ number is already used
193          * by another unit and cannot be shared.
194          */
195         if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
196                 irq = -EINVAL;
197
198         if (irq >= 0) {
199                 ret = devm_request_threaded_irq(&host->class_dev, irq,
200                         NULL, mmc_gpio_cd_irqt,
201                         IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
202                         ctx->cd_label, host);
203                 if (ret < 0)
204                         irq = ret;
205         }
206
207         host->slot.cd_irq = irq;
208
209         if (irq < 0)
210                 host->caps |= MMC_CAP_NEEDS_POLL;
211
212         ctx->override_cd_active_level = true;
213         ctx->cd_gpio = gpio_to_desc(gpio);
214
215         return 0;
216 }
217 EXPORT_SYMBOL(mmc_gpio_request_cd);
218
219 /**
220  * mmc_gpio_free_ro - free the write-protection gpio
221  * @host: mmc host
222  *
223  * It's provided only for cases that client drivers need to manually free
224  * up the write-protection gpio requested by mmc_gpio_request_ro().
225  */
226 void mmc_gpio_free_ro(struct mmc_host *host)
227 {
228         struct mmc_gpio *ctx = host->slot.handler_priv;
229         int gpio;
230
231         if (!ctx || !ctx->ro_gpio)
232                 return;
233
234         gpio = desc_to_gpio(ctx->ro_gpio);
235         ctx->ro_gpio = NULL;
236
237         devm_gpio_free(&host->class_dev, gpio);
238 }
239 EXPORT_SYMBOL(mmc_gpio_free_ro);
240
241 /**
242  * mmc_gpio_free_cd - free the card-detection gpio
243  * @host: mmc host
244  *
245  * It's provided only for cases that client drivers need to manually free
246  * up the card-detection gpio requested by mmc_gpio_request_cd().
247  */
248 void mmc_gpio_free_cd(struct mmc_host *host)
249 {
250         struct mmc_gpio *ctx = host->slot.handler_priv;
251         int gpio;
252
253         if (!ctx || !ctx->cd_gpio)
254                 return;
255
256         if (host->slot.cd_irq >= 0) {
257                 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
258                 host->slot.cd_irq = -EINVAL;
259         }
260
261         gpio = desc_to_gpio(ctx->cd_gpio);
262         ctx->cd_gpio = NULL;
263
264         devm_gpio_free(&host->class_dev, gpio);
265 }
266 EXPORT_SYMBOL(mmc_gpio_free_cd);