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
)) {
41 dev_err(&card
->vdev
->dev
, "Channel not available as input\n");
45 retval
= vme_master_read(card
->window
, ®
, 1,
46 PIO2_REGS_DATA
[PIO2_CHANNEL_BANK
[offset
]]);
48 dev_err(&card
->vdev
->dev
, "Unable to read from GPIO\n");
53 * Remember, input on channels configured as both input and output
56 if (reg
& PIO2_CHANNEL_BIT
[offset
]) {
57 if (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
!= BOTH
)
63 if (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
!= BOTH
)
69 static void pio2_gpio_set(struct gpio_chip
*chip
,
70 unsigned int offset
, int value
)
74 struct pio2_card
*card
= gpio_to_pio2_card(chip
);
76 if ((card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== INPUT
) |
77 (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== NOFIT
)) {
78 dev_err(&card
->vdev
->dev
, "Channel not available as output\n");
83 reg
= card
->bank
[PIO2_CHANNEL_BANK
[offset
]].value
|
84 PIO2_CHANNEL_BIT
[offset
];
86 reg
= card
->bank
[PIO2_CHANNEL_BANK
[offset
]].value
&
87 ~PIO2_CHANNEL_BIT
[offset
];
89 retval
= vme_master_write(card
->window
, ®
, 1,
90 PIO2_REGS_DATA
[PIO2_CHANNEL_BANK
[offset
]]);
92 dev_err(&card
->vdev
->dev
, "Unable to write to GPIO\n");
96 card
->bank
[PIO2_CHANNEL_BANK
[offset
]].value
= reg
;
99 /* Directionality configured at board build - send appropriate response */
100 static int pio2_gpio_dir_in(struct gpio_chip
*chip
, unsigned int offset
)
103 struct pio2_card
*card
= gpio_to_pio2_card(chip
);
105 if ((card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== OUTPUT
) |
106 (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== NOFIT
)) {
107 dev_err(&card
->vdev
->dev
,
108 "Channel directionality not configurable at runtime\n");
118 /* Directionality configured at board build - send appropriate response */
119 static int pio2_gpio_dir_out(struct gpio_chip
*chip
,
120 unsigned int offset
, int value
)
123 struct pio2_card
*card
= gpio_to_pio2_card(chip
);
125 if ((card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== INPUT
) |
126 (card
->bank
[PIO2_CHANNEL_BANK
[offset
]].config
== NOFIT
)) {
127 dev_err(&card
->vdev
->dev
,
128 "Channel directionality not configurable at runtime\n");
139 * We return whether this has been successful - this is used in the probe to
140 * ensure we have a valid card.
142 int pio2_gpio_reset(struct pio2_card
*card
)
149 /* Zero output registers */
150 for (i
= 0; i
< 4; i
++) {
151 retval
= vme_master_write(card
->window
, &data
, 1,
155 card
->bank
[i
].value
= 0;
158 /* Set input interrupt masks */
159 for (i
= 0; i
< 4; i
++) {
160 retval
= vme_master_write(card
->window
, &data
, 1,
161 PIO2_REGS_INT_MASK
[i
* 2]);
165 retval
= vme_master_write(card
->window
, &data
, 1,
166 PIO2_REGS_INT_MASK
[(i
* 2) + 1]);
170 for (j
= 0; j
< 8; j
++)
171 card
->bank
[i
].irq
[j
] = NONE
;
174 /* Ensure all I/O interrupts are cleared */
175 for (i
= 0; i
< 4; i
++) {
177 retval
= vme_master_read(card
->window
, &data
, 1,
178 PIO2_REGS_INT_STAT
[i
]);
187 int pio2_gpio_init(struct pio2_card
*card
)
192 label
= kasprintf(GFP_KERNEL
,
193 "%s@%s", driver_name
, dev_name(&card
->vdev
->dev
));
197 card
->gc
.label
= label
;
199 card
->gc
.ngpio
= PIO2_NUM_CHANNELS
;
200 /* Dynamic allocation of base */
202 /* Setup pointers to chip functions */
203 card
->gc
.direction_input
= pio2_gpio_dir_in
;
204 card
->gc
.direction_output
= pio2_gpio_dir_out
;
205 card
->gc
.get
= pio2_gpio_get
;
206 card
->gc
.set
= pio2_gpio_set
;
208 /* This function adds a memory mapped GPIO chip */
209 retval
= gpiochip_add(&card
->gc
);
211 dev_err(&card
->vdev
->dev
, "Unable to register GPIO\n");
212 kfree(card
->gc
.label
);
218 void pio2_gpio_exit(struct pio2_card
*card
)
220 const char *label
= card
->gc
.label
;
222 gpiochip_remove(&card
->gc
);
This page took 0.044655 seconds and 5 git commands to generate.