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/types.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/ctype.h>
20 #include <linux/gpio.h>
21 #include <linux/slab.h>
22 #include <linux/vme.h>
26 static const char driver_name[] = "pio2_gpio";
28 static struct pio2_card *gpio_to_pio2_card(struct gpio_chip *chip)
30 return container_of(chip, struct pio2_card, gc);
33 static int pio2_gpio_get(struct gpio_chip *chip, unsigned int offset)
37 struct pio2_card *card = gpio_to_pio2_card(chip);
39 if ((card->bank[PIO2_CHANNEL_BANK[offset]].config == OUTPUT) |
40 (card->bank[PIO2_CHANNEL_BANK[offset]].config == NOFIT)) {
42 dev_err(&card->vdev->dev, "Channel not available as input\n");
46 retval = vme_master_read(card->window, ®, 1,
47 PIO2_REGS_DATA[PIO2_CHANNEL_BANK[offset]]);
49 dev_err(&card->vdev->dev, "Unable to read from GPIO\n");
54 * Remember, input on channels configured as both input and output
57 if (reg & PIO2_CHANNEL_BIT[offset]) {
58 if (card->bank[PIO2_CHANNEL_BANK[offset]].config != BOTH)
64 if (card->bank[PIO2_CHANNEL_BANK[offset]].config != BOTH)
70 static void pio2_gpio_set(struct gpio_chip *chip, unsigned int offset,
75 struct pio2_card *card = gpio_to_pio2_card(chip);
77 if ((card->bank[PIO2_CHANNEL_BANK[offset]].config == INPUT) |
78 (card->bank[PIO2_CHANNEL_BANK[offset]].config == NOFIT)) {
80 dev_err(&card->vdev->dev, "Channel not available as output\n");
85 reg = card->bank[PIO2_CHANNEL_BANK[offset]].value |
86 PIO2_CHANNEL_BIT[offset];
88 reg = card->bank[PIO2_CHANNEL_BANK[offset]].value &
89 ~PIO2_CHANNEL_BIT[offset];
91 retval = vme_master_write(card->window, ®, 1,
92 PIO2_REGS_DATA[PIO2_CHANNEL_BANK[offset]]);
94 dev_err(&card->vdev->dev, "Unable to write to GPIO\n");
98 card->bank[PIO2_CHANNEL_BANK[offset]].value = reg;
101 /* Directionality configured at board build - send appropriate response */
102 static int pio2_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
105 struct pio2_card *card = gpio_to_pio2_card(chip);
107 if ((card->bank[PIO2_CHANNEL_BANK[offset]].config == OUTPUT) |
108 (card->bank[PIO2_CHANNEL_BANK[offset]].config == NOFIT)) {
109 dev_err(&card->vdev->dev,
110 "Channel directionality not configurable at runtime\n");
120 /* Directionality configured at board build - send appropriate response */
121 static int pio2_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int value)
124 struct pio2_card *card = gpio_to_pio2_card(chip);
126 if ((card->bank[PIO2_CHANNEL_BANK[offset]].config == INPUT) |
127 (card->bank[PIO2_CHANNEL_BANK[offset]].config == NOFIT)) {
128 dev_err(&card->vdev->dev,
129 "Channel directionality not configurable at runtime\n");
140 * We return whether this has been successful - this is used in the probe to
141 * ensure we have a valid card.
143 int pio2_gpio_reset(struct pio2_card *card)
150 /* Zero output registers */
151 for (i = 0; i < 4; i++) {
152 retval = vme_master_write(card->window, &data, 1,
156 card->bank[i].value = 0;
159 /* Set input interrupt masks */
160 for (i = 0; i < 4; i++) {
161 retval = vme_master_write(card->window, &data, 1,
162 PIO2_REGS_INT_MASK[i * 2]);
166 retval = vme_master_write(card->window, &data, 1,
167 PIO2_REGS_INT_MASK[(i * 2) + 1]);
171 for (j = 0; j < 8; j++)
172 card->bank[i].irq[j] = NONE;
175 /* Ensure all I/O interrupts are cleared */
176 for (i = 0; i < 4; i++) {
178 retval = vme_master_read(card->window, &data, 1,
179 PIO2_REGS_INT_STAT[i]);
188 int pio2_gpio_init(struct pio2_card *card)
193 label = kasprintf(GFP_KERNEL,
194 "%s@%s", driver_name, dev_name(&card->vdev->dev));
198 card->gc.label = label;
200 card->gc.ngpio = PIO2_NUM_CHANNELS;
201 /* Dynamic allocation of base */
203 /* Setup pointers to chip functions */
204 card->gc.direction_input = pio2_gpio_dir_in;
205 card->gc.direction_output = pio2_gpio_dir_out;
206 card->gc.get = pio2_gpio_get;
207 card->gc.set = pio2_gpio_set;
209 /* This function adds a memory mapped GPIO chip */
210 retval = gpiochip_add(&(card->gc));
212 dev_err(&card->vdev->dev, "Unable to register GPIO\n");
213 kfree(card->gc.label);
219 void pio2_gpio_exit(struct pio2_card *card)
221 const char *label = card->gc.label;
223 gpiochip_remove(&(card->gc));