2 * Janz MODULbus VMOD-TTL GPIO Driver
4 * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/mfd/core.h>
20 #include <linux/gpio.h>
21 #include <linux/slab.h>
23 #include <linux/mfd/janz.h>
25 #define DRV_NAME "janz-ttl"
27 #define PORTA_DIRECTION 0x23
28 #define PORTB_DIRECTION 0x2B
29 #define PORTC_DIRECTION 0x06
30 #define PORTA_IOCTL 0x24
31 #define PORTB_IOCTL 0x2C
32 #define PORTC_IOCTL 0x07
34 #define MASTER_INT_CTL 0x00
35 #define MASTER_CONF_CTL 0x01
37 #define CONF_PAE (1 << 2)
38 #define CONF_PBE (1 << 7)
39 #define CONF_PCE (1 << 4)
41 struct ttl_control_regs
{
49 struct gpio_chip gpio
;
51 /* base address of registers */
52 struct ttl_control_regs __iomem
*regs
;
61 static int ttl_get_value(struct gpio_chip
*gpio
, unsigned offset
)
63 struct ttl_module
*mod
= dev_get_drvdata(gpio
->dev
);
68 shadow
= &mod
->porta_shadow
;
69 } else if (offset
< 16) {
70 shadow
= &mod
->portb_shadow
;
73 shadow
= &mod
->portc_shadow
;
77 spin_lock(&mod
->lock
);
78 ret
= *shadow
& (1 << offset
);
79 spin_unlock(&mod
->lock
);
83 static void ttl_set_value(struct gpio_chip
*gpio
, unsigned offset
, int value
)
85 struct ttl_module
*mod
= dev_get_drvdata(gpio
->dev
);
90 port
= &mod
->regs
->porta
;
91 shadow
= &mod
->porta_shadow
;
92 } else if (offset
< 16) {
93 port
= &mod
->regs
->portb
;
94 shadow
= &mod
->portb_shadow
;
97 port
= &mod
->regs
->portc
;
98 shadow
= &mod
->portc_shadow
;
102 spin_lock(&mod
->lock
);
104 *shadow
|= (1 << offset
);
106 *shadow
&= ~(1 << offset
);
108 iowrite16be(*shadow
, port
);
109 spin_unlock(&mod
->lock
);
112 static void __devinit
ttl_write_reg(struct ttl_module
*mod
, u8 reg
, u16 val
)
114 iowrite16be(reg
, &mod
->regs
->control
);
115 iowrite16be(val
, &mod
->regs
->control
);
118 static void __devinit
ttl_setup_device(struct ttl_module
*mod
)
120 /* reset the device to a known state */
121 iowrite16be(0x0000, &mod
->regs
->control
);
122 iowrite16be(0x0001, &mod
->regs
->control
);
123 iowrite16be(0x0000, &mod
->regs
->control
);
125 /* put all ports in open-drain mode */
126 ttl_write_reg(mod
, PORTA_IOCTL
, 0x00ff);
127 ttl_write_reg(mod
, PORTB_IOCTL
, 0x00ff);
128 ttl_write_reg(mod
, PORTC_IOCTL
, 0x000f);
130 /* set all ports as outputs */
131 ttl_write_reg(mod
, PORTA_DIRECTION
, 0x0000);
132 ttl_write_reg(mod
, PORTB_DIRECTION
, 0x0000);
133 ttl_write_reg(mod
, PORTC_DIRECTION
, 0x0000);
135 /* set all ports to drive zeroes */
136 iowrite16be(0x0000, &mod
->regs
->porta
);
137 iowrite16be(0x0000, &mod
->regs
->portb
);
138 iowrite16be(0x0000, &mod
->regs
->portc
);
140 /* enable all ports */
141 ttl_write_reg(mod
, MASTER_CONF_CTL
, CONF_PAE
| CONF_PBE
| CONF_PCE
);
144 static int __devinit
ttl_probe(struct platform_device
*pdev
)
146 struct janz_platform_data
*pdata
;
147 struct device
*dev
= &pdev
->dev
;
148 struct ttl_module
*mod
;
149 struct gpio_chip
*gpio
;
150 struct resource
*res
;
153 pdata
= mfd_get_data(pdev
);
155 dev_err(dev
, "no platform data\n");
160 mod
= kzalloc(sizeof(*mod
), GFP_KERNEL
);
162 dev_err(dev
, "unable to allocate private data\n");
167 platform_set_drvdata(pdev
, mod
);
168 spin_lock_init(&mod
->lock
);
170 /* get access to the MODULbus registers for this module */
171 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
173 dev_err(dev
, "MODULbus registers not found\n");
178 mod
->regs
= ioremap(res
->start
, resource_size(res
));
180 dev_err(dev
, "MODULbus registers not ioremap\n");
185 ttl_setup_device(mod
);
187 /* Initialize the GPIO data structures */
189 gpio
->dev
= &pdev
->dev
;
190 gpio
->label
= pdev
->name
;
191 gpio
->get
= ttl_get_value
;
192 gpio
->set
= ttl_set_value
;
193 gpio
->owner
= THIS_MODULE
;
195 /* request dynamic allocation */
199 ret
= gpiochip_add(gpio
);
201 dev_err(dev
, "unable to add GPIO chip\n");
202 goto out_iounmap_regs
;
205 dev_info(&pdev
->dev
, "module %d: registered GPIO device\n",
217 static int __devexit
ttl_remove(struct platform_device
*pdev
)
219 struct ttl_module
*mod
= platform_get_drvdata(pdev
);
220 struct device
*dev
= &pdev
->dev
;
223 ret
= gpiochip_remove(&mod
->gpio
);
225 dev_err(dev
, "unable to remove GPIO chip\n");
234 static struct platform_driver ttl_driver
= {
237 .owner
= THIS_MODULE
,
240 .remove
= __devexit_p(ttl_remove
),
243 static int __init
ttl_init(void)
245 return platform_driver_register(&ttl_driver
);
248 static void __exit
ttl_exit(void)
250 platform_driver_unregister(&ttl_driver
);
253 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
254 MODULE_DESCRIPTION("Janz MODULbus VMOD-TTL Driver");
255 MODULE_LICENSE("GPL");
256 MODULE_ALIAS("platform:janz-ttl");
258 module_init(ttl_init
);
259 module_exit(ttl_exit
);