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/driver.h>
21 #include <linux/slab.h>
22 #include <linux/vme.h>
26 static const char driver_name[] = "pio2_gpio";
28 static int pio2_gpio_get(struct gpio_chip *chip, unsigned int offset)
32 struct pio2_card *card = gpiochip_get_data(chip);
34 if ((card->bank[PIO2_CHANNEL_BANK[offset]].config == OUTPUT) |
35 (card->bank[PIO2_CHANNEL_BANK[offset]].config == NOFIT)) {
36 dev_err(&card->vdev->dev, "Channel not available as input\n");
40 retval = vme_master_read(card->window, ®, 1,
41 PIO2_REGS_DATA[PIO2_CHANNEL_BANK[offset]]);
43 dev_err(&card->vdev->dev, "Unable to read from GPIO\n");
48 * Remember, input on channels configured as both input and output
51 if (reg & PIO2_CHANNEL_BIT[offset]) {
52 if (card->bank[PIO2_CHANNEL_BANK[offset]].config != BOTH)
58 if (card->bank[PIO2_CHANNEL_BANK[offset]].config != BOTH)
64 static void pio2_gpio_set(struct gpio_chip *chip,
65 unsigned int offset, int value)
69 struct pio2_card *card = gpiochip_get_data(chip);
71 if ((card->bank[PIO2_CHANNEL_BANK[offset]].config == INPUT) |
72 (card->bank[PIO2_CHANNEL_BANK[offset]].config == NOFIT)) {
73 dev_err(&card->vdev->dev, "Channel not available as output\n");
78 reg = card->bank[PIO2_CHANNEL_BANK[offset]].value |
79 PIO2_CHANNEL_BIT[offset];
81 reg = card->bank[PIO2_CHANNEL_BANK[offset]].value &
82 ~PIO2_CHANNEL_BIT[offset];
84 retval = vme_master_write(card->window, ®, 1,
85 PIO2_REGS_DATA[PIO2_CHANNEL_BANK[offset]]);
87 dev_err(&card->vdev->dev, "Unable to write to GPIO\n");
91 card->bank[PIO2_CHANNEL_BANK[offset]].value = reg;
94 /* Directionality configured at board build - send appropriate response */
95 static int pio2_gpio_dir_in(struct gpio_chip *chip, unsigned int offset)
98 struct pio2_card *card = gpiochip_get_data(chip);
100 if ((card->bank[PIO2_CHANNEL_BANK[offset]].config == OUTPUT) |
101 (card->bank[PIO2_CHANNEL_BANK[offset]].config == NOFIT)) {
102 dev_err(&card->vdev->dev,
103 "Channel directionality not configurable at runtime\n");
113 /* Directionality configured at board build - send appropriate response */
114 static int pio2_gpio_dir_out(struct gpio_chip *chip,
115 unsigned int offset, int value)
118 struct pio2_card *card = gpiochip_get_data(chip);
120 if ((card->bank[PIO2_CHANNEL_BANK[offset]].config == INPUT) |
121 (card->bank[PIO2_CHANNEL_BANK[offset]].config == NOFIT)) {
122 dev_err(&card->vdev->dev,
123 "Channel directionality not configurable at runtime\n");
134 * We return whether this has been successful - this is used in the probe to
135 * ensure we have a valid card.
137 int pio2_gpio_reset(struct pio2_card *card)
144 /* Zero output registers */
145 for (i = 0; i < 4; i++) {
146 retval = vme_master_write(card->window, &data, 1,
150 card->bank[i].value = 0;
153 /* Set input interrupt masks */
154 for (i = 0; i < 4; i++) {
155 retval = vme_master_write(card->window, &data, 1,
156 PIO2_REGS_INT_MASK[i * 2]);
160 retval = vme_master_write(card->window, &data, 1,
161 PIO2_REGS_INT_MASK[(i * 2) + 1]);
165 for (j = 0; j < 8; j++)
166 card->bank[i].irq[j] = NONE;
169 /* Ensure all I/O interrupts are cleared */
170 for (i = 0; i < 4; i++) {
172 retval = vme_master_read(card->window, &data, 1,
173 PIO2_REGS_INT_STAT[i]);
182 int pio2_gpio_init(struct pio2_card *card)
187 label = kasprintf(GFP_KERNEL,
188 "%s@%s", driver_name, dev_name(&card->vdev->dev));
192 card->gc.label = label;
194 card->gc.ngpio = PIO2_NUM_CHANNELS;
195 /* Dynamic allocation of base */
197 /* Setup pointers to chip functions */
198 card->gc.direction_input = pio2_gpio_dir_in;
199 card->gc.direction_output = pio2_gpio_dir_out;
200 card->gc.get = pio2_gpio_get;
201 card->gc.set = pio2_gpio_set;
203 /* This function adds a memory mapped GPIO chip */
204 retval = gpiochip_add_data(&card->gc, card);
206 dev_err(&card->vdev->dev, "Unable to register GPIO\n");
207 kfree(card->gc.label);
213 void pio2_gpio_exit(struct pio2_card *card)
215 const char *label = card->gc.label;
217 gpiochip_remove(&card->gc);