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>
19 #include <linux/gpio.h>
20 #include <linux/slab.h>
22 #include <linux/mfd/janz.h>
24 #define DRV_NAME "janz-ttl"
26 #define PORTA_DIRECTION 0x23
27 #define PORTB_DIRECTION 0x2B
28 #define PORTC_DIRECTION 0x06
29 #define PORTA_IOCTL 0x24
30 #define PORTB_IOCTL 0x2C
31 #define PORTC_IOCTL 0x07
33 #define MASTER_INT_CTL 0x00
34 #define MASTER_CONF_CTL 0x01
36 #define CONF_PAE (1 << 2)
37 #define CONF_PBE (1 << 7)
38 #define CONF_PCE (1 << 4)
40 struct ttl_control_regs
{
48 struct gpio_chip gpio
;
50 /* base address of registers */
51 struct ttl_control_regs __iomem
*regs
;
60 static int ttl_get_value(struct gpio_chip
*gpio
, unsigned offset
)
62 struct ttl_module
*mod
= dev_get_drvdata(gpio
->dev
);
67 shadow
= &mod
->porta_shadow
;
68 } else if (offset
< 16) {
69 shadow
= &mod
->portb_shadow
;
72 shadow
= &mod
->portc_shadow
;
76 spin_lock(&mod
->lock
);
77 ret
= *shadow
& (1 << offset
);
78 spin_unlock(&mod
->lock
);
82 static void ttl_set_value(struct gpio_chip
*gpio
, unsigned offset
, int value
)
84 struct ttl_module
*mod
= dev_get_drvdata(gpio
->dev
);
89 port
= &mod
->regs
->porta
;
90 shadow
= &mod
->porta_shadow
;
91 } else if (offset
< 16) {
92 port
= &mod
->regs
->portb
;
93 shadow
= &mod
->portb_shadow
;
96 port
= &mod
->regs
->portc
;
97 shadow
= &mod
->portc_shadow
;
101 spin_lock(&mod
->lock
);
103 *shadow
|= (1 << offset
);
105 *shadow
&= ~(1 << offset
);
107 iowrite16be(*shadow
, port
);
108 spin_unlock(&mod
->lock
);
111 static void ttl_write_reg(struct ttl_module
*mod
, u8 reg
, u16 val
)
113 iowrite16be(reg
, &mod
->regs
->control
);
114 iowrite16be(val
, &mod
->regs
->control
);
117 static void ttl_setup_device(struct ttl_module
*mod
)
119 /* reset the device to a known state */
120 iowrite16be(0x0000, &mod
->regs
->control
);
121 iowrite16be(0x0001, &mod
->regs
->control
);
122 iowrite16be(0x0000, &mod
->regs
->control
);
124 /* put all ports in open-drain mode */
125 ttl_write_reg(mod
, PORTA_IOCTL
, 0x00ff);
126 ttl_write_reg(mod
, PORTB_IOCTL
, 0x00ff);
127 ttl_write_reg(mod
, PORTC_IOCTL
, 0x000f);
129 /* set all ports as outputs */
130 ttl_write_reg(mod
, PORTA_DIRECTION
, 0x0000);
131 ttl_write_reg(mod
, PORTB_DIRECTION
, 0x0000);
132 ttl_write_reg(mod
, PORTC_DIRECTION
, 0x0000);
134 /* set all ports to drive zeroes */
135 iowrite16be(0x0000, &mod
->regs
->porta
);
136 iowrite16be(0x0000, &mod
->regs
->portb
);
137 iowrite16be(0x0000, &mod
->regs
->portc
);
139 /* enable all ports */
140 ttl_write_reg(mod
, MASTER_CONF_CTL
, CONF_PAE
| CONF_PBE
| CONF_PCE
);
143 static int ttl_probe(struct platform_device
*pdev
)
145 struct janz_platform_data
*pdata
;
146 struct device
*dev
= &pdev
->dev
;
147 struct ttl_module
*mod
;
148 struct gpio_chip
*gpio
;
149 struct resource
*res
;
152 pdata
= dev_get_platdata(&pdev
->dev
);
154 dev_err(dev
, "no platform data\n");
159 mod
= kzalloc(sizeof(*mod
), GFP_KERNEL
);
161 dev_err(dev
, "unable to allocate private data\n");
166 platform_set_drvdata(pdev
, mod
);
167 spin_lock_init(&mod
->lock
);
169 /* get access to the MODULbus registers for this module */
170 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
172 dev_err(dev
, "MODULbus registers not found\n");
177 mod
->regs
= ioremap(res
->start
, resource_size(res
));
179 dev_err(dev
, "MODULbus registers not ioremap\n");
184 ttl_setup_device(mod
);
186 /* Initialize the GPIO data structures */
188 gpio
->dev
= &pdev
->dev
;
189 gpio
->label
= pdev
->name
;
190 gpio
->get
= ttl_get_value
;
191 gpio
->set
= ttl_set_value
;
192 gpio
->owner
= THIS_MODULE
;
194 /* request dynamic allocation */
198 ret
= gpiochip_add(gpio
);
200 dev_err(dev
, "unable to add GPIO chip\n");
201 goto out_iounmap_regs
;
214 static int ttl_remove(struct platform_device
*pdev
)
216 struct ttl_module
*mod
= platform_get_drvdata(pdev
);
217 struct device
*dev
= &pdev
->dev
;
220 ret
= gpiochip_remove(&mod
->gpio
);
222 dev_err(dev
, "unable to remove GPIO chip\n");
231 static struct platform_driver ttl_driver
= {
234 .owner
= THIS_MODULE
,
237 .remove
= ttl_remove
,
240 module_platform_driver(ttl_driver
);
242 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
243 MODULE_DESCRIPTION("Janz MODULbus VMOD-TTL Driver");
244 MODULE_LICENSE("GPL");
245 MODULE_ALIAS("platform:janz-ttl");