Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Janz MODULbus VMOD-TTL GPIO Driver |
| 3 | * |
| 4 | * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu> |
| 5 | * |
| 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. |
| 10 | */ |
| 11 | |
| 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/io.h> |
Linus Walleij | 1c947b7 | 2018-03-12 12:08:02 +0100 | [diff] [blame] | 19 | #include <linux/gpio/driver.h> |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 20 | #include <linux/slab.h> |
Linus Walleij | 96d0d03 | 2018-03-12 12:11:45 +0100 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 22 | |
| 23 | #include <linux/mfd/janz.h> |
| 24 | |
| 25 | #define DRV_NAME "janz-ttl" |
| 26 | |
| 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 |
| 33 | |
| 34 | #define MASTER_INT_CTL 0x00 |
| 35 | #define MASTER_CONF_CTL 0x01 |
| 36 | |
Linus Walleij | 96d0d03 | 2018-03-12 12:11:45 +0100 | [diff] [blame] | 37 | #define CONF_PAE BIT(2) |
| 38 | #define CONF_PBE BIT(7) |
| 39 | #define CONF_PCE BIT(4) |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 40 | |
| 41 | struct ttl_control_regs { |
| 42 | __be16 portc; |
| 43 | __be16 portb; |
| 44 | __be16 porta; |
| 45 | __be16 control; |
| 46 | }; |
| 47 | |
| 48 | struct ttl_module { |
| 49 | struct gpio_chip gpio; |
| 50 | |
| 51 | /* base address of registers */ |
| 52 | struct ttl_control_regs __iomem *regs; |
| 53 | |
| 54 | u8 portc_shadow; |
| 55 | u8 portb_shadow; |
| 56 | u8 porta_shadow; |
| 57 | |
| 58 | spinlock_t lock; |
| 59 | }; |
| 60 | |
| 61 | static int ttl_get_value(struct gpio_chip *gpio, unsigned offset) |
| 62 | { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 63 | struct ttl_module *mod = dev_get_drvdata(gpio->parent); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 64 | u8 *shadow; |
| 65 | int ret; |
| 66 | |
| 67 | if (offset < 8) { |
| 68 | shadow = &mod->porta_shadow; |
| 69 | } else if (offset < 16) { |
| 70 | shadow = &mod->portb_shadow; |
| 71 | offset -= 8; |
| 72 | } else { |
| 73 | shadow = &mod->portc_shadow; |
| 74 | offset -= 16; |
| 75 | } |
| 76 | |
| 77 | spin_lock(&mod->lock); |
Linus Walleij | 96d0d03 | 2018-03-12 12:11:45 +0100 | [diff] [blame] | 78 | ret = *shadow & BIT(offset); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 79 | spin_unlock(&mod->lock); |
Linus Walleij | 828240c | 2015-12-21 11:03:33 +0100 | [diff] [blame] | 80 | return !!ret; |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void ttl_set_value(struct gpio_chip *gpio, unsigned offset, int value) |
| 84 | { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 85 | struct ttl_module *mod = dev_get_drvdata(gpio->parent); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 86 | void __iomem *port; |
| 87 | u8 *shadow; |
| 88 | |
| 89 | if (offset < 8) { |
| 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; |
| 95 | offset -= 8; |
| 96 | } else { |
| 97 | port = &mod->regs->portc; |
| 98 | shadow = &mod->portc_shadow; |
| 99 | offset -= 16; |
| 100 | } |
| 101 | |
| 102 | spin_lock(&mod->lock); |
| 103 | if (value) |
Linus Walleij | 96d0d03 | 2018-03-12 12:11:45 +0100 | [diff] [blame] | 104 | *shadow |= BIT(offset); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 105 | else |
Linus Walleij | 96d0d03 | 2018-03-12 12:11:45 +0100 | [diff] [blame] | 106 | *shadow &= ~BIT(offset); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 107 | |
| 108 | iowrite16be(*shadow, port); |
| 109 | spin_unlock(&mod->lock); |
| 110 | } |
| 111 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 112 | static void ttl_write_reg(struct ttl_module *mod, u8 reg, u16 val) |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 113 | { |
| 114 | iowrite16be(reg, &mod->regs->control); |
| 115 | iowrite16be(val, &mod->regs->control); |
| 116 | } |
| 117 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 118 | static void ttl_setup_device(struct ttl_module *mod) |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 119 | { |
| 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); |
| 124 | |
| 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); |
| 129 | |
| 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); |
| 134 | |
| 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); |
| 139 | |
| 140 | /* enable all ports */ |
| 141 | ttl_write_reg(mod, MASTER_CONF_CTL, CONF_PAE | CONF_PBE | CONF_PCE); |
| 142 | } |
| 143 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 144 | static int ttl_probe(struct platform_device *pdev) |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 145 | { |
| 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; |
| 151 | int ret; |
| 152 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 153 | pdata = dev_get_platdata(&pdev->dev); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 154 | if (!pdata) { |
| 155 | dev_err(dev, "no platform data\n"); |
abdoulaye berthe | ae28193 | 2014-05-13 18:42:12 +0200 | [diff] [blame] | 156 | return -ENXIO; |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 157 | } |
| 158 | |
abdoulaye berthe | ae28193 | 2014-05-13 18:42:12 +0200 | [diff] [blame] | 159 | mod = devm_kzalloc(dev, sizeof(*mod), GFP_KERNEL); |
| 160 | if (!mod) |
| 161 | return -ENOMEM; |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 162 | |
| 163 | platform_set_drvdata(pdev, mod); |
| 164 | spin_lock_init(&mod->lock); |
| 165 | |
| 166 | /* get access to the MODULbus registers for this module */ |
| 167 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
abdoulaye berthe | ae28193 | 2014-05-13 18:42:12 +0200 | [diff] [blame] | 168 | mod->regs = devm_ioremap_resource(dev, res); |
| 169 | if (IS_ERR(mod->regs)) |
| 170 | return PTR_ERR(mod->regs); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 171 | |
| 172 | ttl_setup_device(mod); |
| 173 | |
| 174 | /* Initialize the GPIO data structures */ |
| 175 | gpio = &mod->gpio; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 176 | gpio->parent = &pdev->dev; |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 177 | gpio->label = pdev->name; |
| 178 | gpio->get = ttl_get_value; |
| 179 | gpio->set = ttl_set_value; |
| 180 | gpio->owner = THIS_MODULE; |
| 181 | |
| 182 | /* request dynamic allocation */ |
| 183 | gpio->base = -1; |
| 184 | gpio->ngpio = 20; |
| 185 | |
Laxman Dewangan | abeb3f4 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 186 | ret = devm_gpiochip_add_data(dev, gpio, NULL); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 187 | if (ret) { |
| 188 | dev_err(dev, "unable to add GPIO chip\n"); |
abdoulaye berthe | ae28193 | 2014-05-13 18:42:12 +0200 | [diff] [blame] | 189 | return ret; |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 192 | return 0; |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 195 | static struct platform_driver ttl_driver = { |
| 196 | .driver = { |
| 197 | .name = DRV_NAME, |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 198 | }, |
| 199 | .probe = ttl_probe, |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 200 | }; |
| 201 | |
Mark Brown | 6f61415 | 2011-12-08 00:24:00 +0800 | [diff] [blame] | 202 | module_platform_driver(ttl_driver); |
Ira W. Snyder | 800e69f | 2010-04-07 10:08:01 +0200 | [diff] [blame] | 203 | |
| 204 | MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>"); |
| 205 | MODULE_DESCRIPTION("Janz MODULbus VMOD-TTL Driver"); |
| 206 | MODULE_LICENSE("GPL"); |
| 207 | MODULE_ALIAS("platform:janz-ttl"); |