4 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2009 GE Intelligent Platforms Embedded Systems, Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/device.h>
19 #include <linux/platform_device.h>
20 #include <linux/ctype.h>
21 #include <linux/gpio.h>
22 #include <linux/slab.h>
23 #include <linux/vme.h>
27 static const char driver_name
[] = "pio2_gpio";
29 static struct pio2_card
*gpio_to_pio2_card(struct gpio_chip
*chip
)
31 return container_of(chip
, struct pio2_card
, gc
);
34 static int pio2_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
38 struct pio2_card
*card
= gpio_to_pio2_card(chip
);
40 if ((card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== OUTPUT
) |
41 (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== NOFIT
)) {
43 dev_err(&card
->vdev
->dev
, "Channel not available as input\n");
47 retval
= vme_master_read(card
->window
, ®
, 1,
48 PIO2_REGS_DATA
[PIO2_CHANNEL_BANK
[offset
]]);
50 dev_err(&card
->vdev
->dev
, "Unable to read from GPIO\n");
55 * Remember, input on channels configured as both input and output
58 if (reg
& PIO2_CHANNEL_BIT
[offset
]) {
59 if (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
!= BOTH
)
64 if (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
!= BOTH
)
71 static void pio2_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
76 struct pio2_card
*card
= gpio_to_pio2_card(chip
);
78 if ((card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== INPUT
) |
79 (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== NOFIT
)) {
81 dev_err(&card
->vdev
->dev
, "Channel not available as output\n");
86 reg
= card
->bank
[PIO2_CHANNEL_BANK
[offset
]].value
|
87 PIO2_CHANNEL_BIT
[offset
];
89 reg
= card
->bank
[PIO2_CHANNEL_BANK
[offset
]].value
&
90 ~PIO2_CHANNEL_BIT
[offset
];
92 retval
= vme_master_write(card
->window
, ®
, 1,
93 PIO2_REGS_DATA
[PIO2_CHANNEL_BANK
[offset
]]);
95 dev_err(&card
->vdev
->dev
, "Unable to write to GPIO\n");
99 card
->bank
[PIO2_CHANNEL_BANK
[offset
]].value
= reg
;
102 /* Directionality configured at board build - send appropriate response */
103 static int pio2_gpio_dir_in(struct gpio_chip
*chip
, unsigned offset
)
106 struct pio2_card
*card
= gpio_to_pio2_card(chip
);
108 if ((card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== OUTPUT
) |
109 (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== NOFIT
)) {
110 dev_err(&card
->vdev
->dev
,
111 "Channel directionality not configurable at runtime\n");
121 /* Directionality configured at board build - send appropriate response */
122 static int pio2_gpio_dir_out(struct gpio_chip
*chip
, unsigned offset
, int value
)
125 struct pio2_card
*card
= gpio_to_pio2_card(chip
);
127 if ((card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== INPUT
) |
128 (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== NOFIT
)) {
129 dev_err(&card
->vdev
->dev
,
130 "Channel directionality not configurable at runtime\n");
141 * We return whether this has been successful - this is used in the probe to
142 * ensure we have a valid card.
144 int pio2_gpio_reset(struct pio2_card
*card
)
151 /* Zero output registers */
152 for (i
= 0; i
< 4; i
++) {
153 retval
= vme_master_write(card
->window
, &data
, 1,
157 card
->bank
[i
].value
= 0;
160 /* Set input interrupt masks */
161 for (i
= 0; i
< 4; i
++) {
162 retval
= vme_master_write(card
->window
, &data
, 1,
163 PIO2_REGS_INT_MASK
[i
* 2]);
167 retval
= vme_master_write(card
->window
, &data
, 1,
168 PIO2_REGS_INT_MASK
[(i
* 2) + 1]);
172 for (j
= 0; j
< 8; j
++)
173 card
->bank
[i
].irq
[j
] = NONE
;
176 /* Ensure all I/O interrupts are cleared */
177 for (i
= 0; i
< 4; i
++) {
179 retval
= vme_master_read(card
->window
, &data
, 1,
180 PIO2_REGS_INT_STAT
[i
]);
189 int pio2_gpio_init(struct pio2_card
*card
)
194 label
= kmalloc(PIO2_NUM_CHANNELS
, GFP_KERNEL
);
198 sprintf(label
, "%s@%s", driver_name
, dev_name(&card
->vdev
->dev
));
199 card
->gc
.label
= label
;
201 card
->gc
.ngpio
= PIO2_NUM_CHANNELS
;
202 /* Dynamic allocation of base */
204 /* Setup pointers to chip functions */
205 card
->gc
.direction_input
= pio2_gpio_dir_in
;
206 card
->gc
.direction_output
= pio2_gpio_dir_out
;
207 card
->gc
.get
= pio2_gpio_get
;
208 card
->gc
.set
= pio2_gpio_set
;
210 /* This function adds a memory mapped GPIO chip */
211 retval
= gpiochip_add(&(card
->gc
));
213 dev_err(&card
->vdev
->dev
, "Unable to register GPIO\n");
214 kfree(card
->gc
.label
);
220 void pio2_gpio_exit(struct pio2_card
*card
)
222 const char *label
= card
->gc
.label
;
224 if (gpiochip_remove(&(card
->gc
)))
225 dev_err(&card
->vdev
->dev
, "Failed to remove GPIO");
This page took 0.069418 seconds and 5 git commands to generate.