Guennadi Liakhovetski | 349ab52 | 2011-12-25 21:36:02 +0100 | [diff] [blame] | 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/interrupt.h> |
| 14 | #include <linux/jiffies.h> |
H Hartley Sweeten | 5ca6518 | 2012-04-17 13:03:38 -0700 | [diff] [blame^] | 15 | #include <linux/mmc/cd-gpio.h> |
Guennadi Liakhovetski | 349ab52 | 2011-12-25 21:36:02 +0100 | [diff] [blame] | 16 | #include <linux/mmc/host.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | |
| 20 | struct mmc_cd_gpio { |
| 21 | unsigned int gpio; |
| 22 | char label[0]; |
| 23 | }; |
| 24 | |
| 25 | static irqreturn_t mmc_cd_gpio_irqt(int irq, void *dev_id) |
| 26 | { |
| 27 | /* Schedule a card detection after a debounce timeout */ |
| 28 | mmc_detect_change(dev_id, msecs_to_jiffies(100)); |
| 29 | return IRQ_HANDLED; |
| 30 | } |
| 31 | |
Guennadi Liakhovetski | c9b0546 | 2012-02-09 22:57:07 +0100 | [diff] [blame] | 32 | int mmc_cd_gpio_request(struct mmc_host *host, unsigned int gpio) |
Guennadi Liakhovetski | 349ab52 | 2011-12-25 21:36:02 +0100 | [diff] [blame] | 33 | { |
| 34 | size_t len = strlen(dev_name(host->parent)) + 4; |
Guennadi Liakhovetski | c9b0546 | 2012-02-09 22:57:07 +0100 | [diff] [blame] | 35 | struct mmc_cd_gpio *cd; |
| 36 | int irq = gpio_to_irq(gpio); |
Guennadi Liakhovetski | 349ab52 | 2011-12-25 21:36:02 +0100 | [diff] [blame] | 37 | int ret; |
| 38 | |
Guennadi Liakhovetski | c9b0546 | 2012-02-09 22:57:07 +0100 | [diff] [blame] | 39 | if (irq < 0) |
| 40 | return irq; |
| 41 | |
| 42 | cd = kmalloc(sizeof(*cd) + len, GFP_KERNEL); |
Guennadi Liakhovetski | 349ab52 | 2011-12-25 21:36:02 +0100 | [diff] [blame] | 43 | if (!cd) |
| 44 | return -ENOMEM; |
| 45 | |
| 46 | snprintf(cd->label, len, "%s cd", dev_name(host->parent)); |
| 47 | |
| 48 | ret = gpio_request_one(gpio, GPIOF_DIR_IN, cd->label); |
| 49 | if (ret < 0) |
| 50 | goto egpioreq; |
| 51 | |
| 52 | ret = request_threaded_irq(irq, NULL, mmc_cd_gpio_irqt, |
Guennadi Liakhovetski | c9b0546 | 2012-02-09 22:57:07 +0100 | [diff] [blame] | 53 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 54 | cd->label, host); |
Guennadi Liakhovetski | 349ab52 | 2011-12-25 21:36:02 +0100 | [diff] [blame] | 55 | if (ret < 0) |
| 56 | goto eirqreq; |
| 57 | |
| 58 | cd->gpio = gpio; |
| 59 | host->hotplug.irq = irq; |
| 60 | host->hotplug.handler_priv = cd; |
| 61 | |
| 62 | return 0; |
| 63 | |
| 64 | eirqreq: |
| 65 | gpio_free(gpio); |
| 66 | egpioreq: |
| 67 | kfree(cd); |
| 68 | return ret; |
| 69 | } |
| 70 | EXPORT_SYMBOL(mmc_cd_gpio_request); |
| 71 | |
| 72 | void mmc_cd_gpio_free(struct mmc_host *host) |
| 73 | { |
| 74 | struct mmc_cd_gpio *cd = host->hotplug.handler_priv; |
| 75 | |
| 76 | free_irq(host->hotplug.irq, host); |
| 77 | gpio_free(cd->gpio); |
| 78 | kfree(cd); |
| 79 | } |
| 80 | EXPORT_SYMBOL(mmc_cd_gpio_free); |