2 * Driver for TANBAC TB0219 base board.
4 * Copyright (C) 2005 Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/device.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
26 #include <asm/reboot.h>
28 MODULE_AUTHOR("Yoichi Yuasa <yuasa@hh.iij4u.or.jp>");
29 MODULE_DESCRIPTION("TANBAC TB0219 base board driver");
30 MODULE_LICENSE("GPL");
32 static int major; /* default is dynamic major device number */
33 module_param(major, int, 0);
34 MODULE_PARM_DESC(major, "Major device number");
36 static void (*old_machine_restart)(char *command);
37 static void __iomem *tb0219_base;
38 static spinlock_t tb0219_lock;
40 #define tb0219_read(offset) readw(tb0219_base + (offset))
41 #define tb0219_write(offset, value) writew((value), tb0219_base + (offset))
43 #define TB0219_START 0x0a000000UL
44 #define TB0219_SIZE 0x20UL
46 #define TB0219_LED 0x00
47 #define TB0219_GPIO_INPUT 0x02
48 #define TB0219_GPIO_OUTPUT 0x04
49 #define TB0219_DIP_SWITCH 0x06
50 #define TB0219_MISC 0x08
51 #define TB0219_RESET 0x0e
52 #define TB0219_PCI_SLOT1_IRQ_STATUS 0x10
53 #define TB0219_PCI_SLOT2_IRQ_STATUS 0x12
54 #define TB0219_PCI_SLOT3_IRQ_STATUS 0x14
93 static inline char get_led(void)
95 return (char)tb0219_read(TB0219_LED);
98 static inline char get_gpio_input_pin(unsigned int pin)
102 values = tb0219_read(TB0219_GPIO_INPUT);
103 if (values & (1 << pin))
109 static inline char get_gpio_output_pin(unsigned int pin)
113 values = tb0219_read(TB0219_GPIO_OUTPUT);
114 if (values & (1 << pin))
120 static inline char get_dip_switch(unsigned int pin)
124 values = tb0219_read(TB0219_DIP_SWITCH);
125 if (values & (1 << pin))
131 static inline int set_led(char command)
133 tb0219_write(TB0219_LED, command);
138 static inline int set_gpio_output_pin(unsigned int pin, char command)
143 if (command != '0' && command != '1')
146 spin_lock_irqsave(&tb0219_lock, flags);
147 value = tb0219_read(TB0219_GPIO_OUTPUT);
149 value &= ~(1 << pin);
152 tb0219_write(TB0219_GPIO_OUTPUT, value);
153 spin_unlock_irqrestore(&tb0219_lock, flags);
159 static ssize_t tanbac_tb0219_read(struct file *file, char __user *buf, size_t len,
165 minor = iminor(file->f_dentry->d_inode);
171 value = get_gpio_input_pin(minor - 16);
174 value = get_gpio_output_pin(minor - 32);
177 value = get_dip_switch(minor - 48);
186 if (put_user(value, buf))
192 static ssize_t tanbac_tb0219_write(struct file *file, const char __user *data,
193 size_t len, loff_t *ppos)
201 minor = iminor(file->f_dentry->d_inode);
207 type = TYPE_GPIO_OUTPUT;
213 for (i = 0; i < len; i++) {
214 if (get_user(c, data + i))
221 case TYPE_GPIO_OUTPUT:
222 retval = set_gpio_output_pin(minor - 32, c);
233 static int tanbac_tb0219_open(struct inode *inode, struct file *file)
237 minor = iminor(inode);
243 return nonseekable_open(inode, file);
251 static int tanbac_tb0219_release(struct inode *inode, struct file *file)
256 static struct file_operations tb0219_fops = {
257 .owner = THIS_MODULE,
258 .read = tanbac_tb0219_read,
259 .write = tanbac_tb0219_write,
260 .open = tanbac_tb0219_open,
261 .release = tanbac_tb0219_release,
264 static void tb0219_restart(char *command)
266 tb0219_write(TB0219_RESET, 0);
269 static int tb0219_probe(struct device *dev)
273 if (request_mem_region(TB0219_START, TB0219_SIZE, "TB0219") == NULL)
276 tb0219_base = ioremap(TB0219_START, TB0219_SIZE);
277 if (tb0219_base == NULL) {
278 release_mem_region(TB0219_START, TB0219_SIZE);
282 retval = register_chrdev(major, "TB0219", &tb0219_fops);
284 iounmap(tb0219_base);
286 release_mem_region(TB0219_START, TB0219_SIZE);
290 spin_lock_init(&tb0219_lock);
292 old_machine_restart = _machine_restart;
293 _machine_restart = tb0219_restart;
297 printk(KERN_INFO "TB0219: major number %d\n", major);
303 static int tb0219_remove(struct device *dev)
305 _machine_restart = old_machine_restart;
307 iounmap(tb0219_base);
310 release_mem_region(TB0219_START, TB0219_SIZE);
315 static struct platform_device *tb0219_platform_device;
317 static struct device_driver tb0219_device_driver = {
319 .bus = &platform_bus_type,
320 .probe = tb0219_probe,
321 .remove = tb0219_remove,
324 static int __devinit tanbac_tb0219_init(void)
328 tb0219_platform_device = platform_device_register_simple("TB0219", -1, NULL, 0);
329 if (IS_ERR(tb0219_platform_device))
330 return PTR_ERR(tb0219_platform_device);
332 retval = driver_register(&tb0219_device_driver);
334 platform_device_unregister(tb0219_platform_device);
339 static void __devexit tanbac_tb0219_exit(void)
341 driver_unregister(&tb0219_device_driver);
343 platform_device_unregister(tb0219_platform_device);
346 module_init(tanbac_tb0219_init);
347 module_exit(tanbac_tb0219_exit);