PCMCIA: pxa: convert PXA socket drivers to use new irq/gpio management
[firefly-linux-kernel-4.4.55.git] / drivers / pcmcia / pxa2xx_colibri.c
1 /*
2  * linux/drivers/pcmcia/pxa2xx_colibri.c
3  *
4  * Driver for Toradex Colibri PXA270 CF socket
5  *
6  * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  */
13
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/gpio.h>
18
19 #include <asm/mach-types.h>
20
21 #include "soc_common.h"
22
23 #define COLIBRI270_RESET_GPIO   53
24 #define COLIBRI270_PPEN_GPIO    107
25 #define COLIBRI270_BVD1_GPIO    83
26 #define COLIBRI270_BVD2_GPIO    82
27 #define COLIBRI270_DETECT_GPIO  84
28 #define COLIBRI270_READY_GPIO   1
29
30 #define COLIBRI320_RESET_GPIO   77
31 #define COLIBRI320_PPEN_GPIO    57
32 #define COLIBRI320_BVD1_GPIO    53
33 #define COLIBRI320_BVD2_GPIO    79
34 #define COLIBRI320_DETECT_GPIO  81
35 #define COLIBRI320_READY_GPIO   29
36
37 enum {
38         DETECT = 0,
39         READY = 1,
40         BVD1 = 2,
41         BVD2 = 3,
42         PPEN = 4,
43         RESET = 5,
44 };
45
46 /* Contents of this array are configured on-the-fly in init function */
47 static struct gpio colibri_pcmcia_gpios[] = {
48         { 0,    GPIOF_IN,       "PCMCIA Detect" },
49         { 0,    GPIOF_IN,       "PCMCIA Ready" },
50         { 0,    GPIOF_IN,       "PCMCIA BVD1" },
51         { 0,    GPIOF_IN,       "PCMCIA BVD2" },
52         { 0,    GPIOF_INIT_LOW, "PCMCIA PPEN" },
53         { 0,    GPIOF_INIT_HIGH,"PCMCIA Reset" },
54 };
55
56 static int colibri_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
57 {
58         int ret;
59
60         ret = gpio_request_array(colibri_pcmcia_gpios,
61                                 ARRAY_SIZE(colibri_pcmcia_gpios));
62         if (ret)
63                 goto err1;
64
65         skt->socket.pci_irq = gpio_to_irq(colibri_pcmcia_gpios[READY].gpio);
66         skt->stat[SOC_STAT_CD].irq = gpio_to_irq(colibri_pcmcia_gpios[DETECT].gpio);
67         skt->stat[SOC_STAT_CD].name = "PCMCIA CD";
68
69 err1:
70         return ret;
71 }
72
73 static void colibri_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
74 {
75         gpio_free_array(colibri_pcmcia_gpios,
76                         ARRAY_SIZE(colibri_pcmcia_gpios));
77 }
78
79 static void colibri_pcmcia_socket_state(struct soc_pcmcia_socket *skt,
80                                         struct pcmcia_state *state)
81 {
82
83         state->detect = !!gpio_get_value(colibri_pcmcia_gpios[DETECT].gpio);
84         state->ready  = !!gpio_get_value(colibri_pcmcia_gpios[READY].gpio);
85         state->bvd1   = !!gpio_get_value(colibri_pcmcia_gpios[BVD1].gpio);
86         state->bvd2   = !!gpio_get_value(colibri_pcmcia_gpios[BVD2].gpio);
87         state->wrprot = 0;
88         state->vs_3v  = 1;
89         state->vs_Xv  = 0;
90 }
91
92 static int
93 colibri_pcmcia_configure_socket(struct soc_pcmcia_socket *skt,
94                                 const socket_state_t *state)
95 {
96         gpio_set_value(colibri_pcmcia_gpios[PPEN].gpio,
97                         !(state->Vcc == 33 && state->Vpp < 50));
98         gpio_set_value(colibri_pcmcia_gpios[RESET].gpio,
99                         state->flags & SS_RESET);
100         return 0;
101 }
102
103 static struct pcmcia_low_level colibri_pcmcia_ops = {
104         .owner                  = THIS_MODULE,
105
106         .first                  = 0,
107         .nr                     = 1,
108
109         .hw_init                = colibri_pcmcia_hw_init,
110         .hw_shutdown            = colibri_pcmcia_hw_shutdown,
111
112         .socket_state           = colibri_pcmcia_socket_state,
113         .configure_socket       = colibri_pcmcia_configure_socket,
114 };
115
116 static struct platform_device *colibri_pcmcia_device;
117
118 static int __init colibri_pcmcia_init(void)
119 {
120         int ret;
121
122         if (!machine_is_colibri() && !machine_is_colibri320())
123                 return -ENODEV;
124
125         colibri_pcmcia_device = platform_device_alloc("pxa2xx-pcmcia", -1);
126         if (!colibri_pcmcia_device)
127                 return -ENOMEM;
128
129         /* Colibri PXA270 */
130         if (machine_is_colibri()) {
131                 colibri_pcmcia_gpios[RESET].gpio        = COLIBRI270_RESET_GPIO;
132                 colibri_pcmcia_gpios[PPEN].gpio         = COLIBRI270_PPEN_GPIO;
133                 colibri_pcmcia_gpios[BVD1].gpio         = COLIBRI270_BVD1_GPIO;
134                 colibri_pcmcia_gpios[BVD2].gpio         = COLIBRI270_BVD2_GPIO;
135                 colibri_pcmcia_gpios[DETECT].gpio       = COLIBRI270_DETECT_GPIO;
136                 colibri_pcmcia_gpios[READY].gpio        = COLIBRI270_READY_GPIO;
137         /* Colibri PXA320 */
138         } else if (machine_is_colibri320()) {
139                 colibri_pcmcia_gpios[RESET].gpio        = COLIBRI320_RESET_GPIO;
140                 colibri_pcmcia_gpios[PPEN].gpio         = COLIBRI320_PPEN_GPIO;
141                 colibri_pcmcia_gpios[BVD1].gpio         = COLIBRI320_BVD1_GPIO;
142                 colibri_pcmcia_gpios[BVD2].gpio         = COLIBRI320_BVD2_GPIO;
143                 colibri_pcmcia_gpios[DETECT].gpio       = COLIBRI320_DETECT_GPIO;
144                 colibri_pcmcia_gpios[READY].gpio        = COLIBRI320_READY_GPIO;
145         }
146
147         ret = platform_device_add_data(colibri_pcmcia_device,
148                 &colibri_pcmcia_ops, sizeof(colibri_pcmcia_ops));
149
150         if (!ret)
151                 ret = platform_device_add(colibri_pcmcia_device);
152
153         if (ret)
154                 platform_device_put(colibri_pcmcia_device);
155
156         return ret;
157 }
158
159 static void __exit colibri_pcmcia_exit(void)
160 {
161         platform_device_unregister(colibri_pcmcia_device);
162 }
163
164 module_init(colibri_pcmcia_init);
165 module_exit(colibri_pcmcia_exit);
166
167 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
168 MODULE_DESCRIPTION("PCMCIA support for Toradex Colibri PXA270/PXA320");
169 MODULE_ALIAS("platform:pxa2xx-pcmcia");
170 MODULE_LICENSE("GPL");