18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Miscellaneous functions for IDT EB434 board 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright 2004 IDT Inc. (rischelp@idt.com) 58c2ecf20Sopenharmony_ci * Copyright 2006 Phil Sutter <n0-1@freewrt.org> 68c2ecf20Sopenharmony_ci * Copyright 2007 Florian Fainelli <florian@openwrt.org> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 98c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License as published by the 108c2ecf20Sopenharmony_ci * Free Software Foundation; either version 2 of the License, or (at your 118c2ecf20Sopenharmony_ci * option) any later version. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 148c2ecf20Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 158c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 168c2ecf20Sopenharmony_ci * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 178c2ecf20Sopenharmony_ci * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 188c2ecf20Sopenharmony_ci * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 198c2ecf20Sopenharmony_ci * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 208c2ecf20Sopenharmony_ci * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 218c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 228c2ecf20Sopenharmony_ci * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License along 258c2ecf20Sopenharmony_ci * with this program; if not, write to the Free Software Foundation, Inc., 268c2ecf20Sopenharmony_ci * 675 Mass Ave, Cambridge, MA 02139, USA. 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#include <linux/kernel.h> 308c2ecf20Sopenharmony_ci#include <linux/init.h> 318c2ecf20Sopenharmony_ci#include <linux/types.h> 328c2ecf20Sopenharmony_ci#include <linux/export.h> 338c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 348c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 358c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h> 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#include <asm/mach-rc32434/rb.h> 388c2ecf20Sopenharmony_ci#include <asm/mach-rc32434/gpio.h> 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistruct rb532_gpio_chip { 418c2ecf20Sopenharmony_ci struct gpio_chip chip; 428c2ecf20Sopenharmony_ci void __iomem *regbase; 438c2ecf20Sopenharmony_ci}; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistatic struct resource rb532_gpio_reg0_res[] = { 468c2ecf20Sopenharmony_ci { 478c2ecf20Sopenharmony_ci .name = "gpio_reg0", 488c2ecf20Sopenharmony_ci .start = REGBASE + GPIOBASE, 498c2ecf20Sopenharmony_ci .end = REGBASE + GPIOBASE + sizeof(struct rb532_gpio_reg) - 1, 508c2ecf20Sopenharmony_ci .flags = IORESOURCE_MEM, 518c2ecf20Sopenharmony_ci } 528c2ecf20Sopenharmony_ci}; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/* rb532_set_bit - sanely set a bit 558c2ecf20Sopenharmony_ci * 568c2ecf20Sopenharmony_ci * bitval: new value for the bit 578c2ecf20Sopenharmony_ci * offset: bit index in the 4 byte address range 588c2ecf20Sopenharmony_ci * ioaddr: 4 byte aligned address being altered 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_cistatic inline void rb532_set_bit(unsigned bitval, 618c2ecf20Sopenharmony_ci unsigned offset, void __iomem *ioaddr) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci unsigned long flags; 648c2ecf20Sopenharmony_ci u32 val; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci local_irq_save(flags); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci val = readl(ioaddr); 698c2ecf20Sopenharmony_ci val &= ~(!bitval << offset); /* unset bit if bitval == 0 */ 708c2ecf20Sopenharmony_ci val |= (!!bitval << offset); /* set bit if bitval == 1 */ 718c2ecf20Sopenharmony_ci writel(val, ioaddr); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci local_irq_restore(flags); 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/* rb532_get_bit - read a bit 778c2ecf20Sopenharmony_ci * 788c2ecf20Sopenharmony_ci * returns the boolean state of the bit, which may be > 1 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_cistatic inline int rb532_get_bit(unsigned offset, void __iomem *ioaddr) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci return readl(ioaddr) & (1 << offset); 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/* 868c2ecf20Sopenharmony_ci * Return GPIO level */ 878c2ecf20Sopenharmony_cistatic int rb532_gpio_get(struct gpio_chip *chip, unsigned offset) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci struct rb532_gpio_chip *gpch; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci gpch = gpiochip_get_data(chip); 928c2ecf20Sopenharmony_ci return !!rb532_get_bit(offset, gpch->regbase + GPIOD); 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci/* 968c2ecf20Sopenharmony_ci * Set output GPIO level 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_cistatic void rb532_gpio_set(struct gpio_chip *chip, 998c2ecf20Sopenharmony_ci unsigned offset, int value) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci struct rb532_gpio_chip *gpch; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci gpch = gpiochip_get_data(chip); 1048c2ecf20Sopenharmony_ci rb532_set_bit(value, offset, gpch->regbase + GPIOD); 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci/* 1088c2ecf20Sopenharmony_ci * Set GPIO direction to input 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_cistatic int rb532_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci struct rb532_gpio_chip *gpch; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci gpch = gpiochip_get_data(chip); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci /* disable alternate function in case it's set */ 1178c2ecf20Sopenharmony_ci rb532_set_bit(0, offset, gpch->regbase + GPIOFUNC); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci rb532_set_bit(0, offset, gpch->regbase + GPIOCFG); 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci/* 1248c2ecf20Sopenharmony_ci * Set GPIO direction to output 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_cistatic int rb532_gpio_direction_output(struct gpio_chip *chip, 1278c2ecf20Sopenharmony_ci unsigned offset, int value) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci struct rb532_gpio_chip *gpch; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci gpch = gpiochip_get_data(chip); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci /* disable alternate function in case it's set */ 1348c2ecf20Sopenharmony_ci rb532_set_bit(0, offset, gpch->regbase + GPIOFUNC); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci /* set the initial output value */ 1378c2ecf20Sopenharmony_ci rb532_set_bit(value, offset, gpch->regbase + GPIOD); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci rb532_set_bit(1, offset, gpch->regbase + GPIOCFG); 1408c2ecf20Sopenharmony_ci return 0; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic int rb532_gpio_to_irq(struct gpio_chip *chip, unsigned gpio) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci return 8 + 4 * 32 + gpio; 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic struct rb532_gpio_chip rb532_gpio_chip[] = { 1498c2ecf20Sopenharmony_ci [0] = { 1508c2ecf20Sopenharmony_ci .chip = { 1518c2ecf20Sopenharmony_ci .label = "gpio0", 1528c2ecf20Sopenharmony_ci .direction_input = rb532_gpio_direction_input, 1538c2ecf20Sopenharmony_ci .direction_output = rb532_gpio_direction_output, 1548c2ecf20Sopenharmony_ci .get = rb532_gpio_get, 1558c2ecf20Sopenharmony_ci .set = rb532_gpio_set, 1568c2ecf20Sopenharmony_ci .to_irq = rb532_gpio_to_irq, 1578c2ecf20Sopenharmony_ci .base = 0, 1588c2ecf20Sopenharmony_ci .ngpio = 32, 1598c2ecf20Sopenharmony_ci }, 1608c2ecf20Sopenharmony_ci }, 1618c2ecf20Sopenharmony_ci}; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci/* 1648c2ecf20Sopenharmony_ci * Set GPIO interrupt level 1658c2ecf20Sopenharmony_ci */ 1668c2ecf20Sopenharmony_civoid rb532_gpio_set_ilevel(int bit, unsigned gpio) 1678c2ecf20Sopenharmony_ci{ 1688c2ecf20Sopenharmony_ci rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOILEVEL); 1698c2ecf20Sopenharmony_ci} 1708c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rb532_gpio_set_ilevel); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci/* 1738c2ecf20Sopenharmony_ci * Set GPIO interrupt status 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_civoid rb532_gpio_set_istat(int bit, unsigned gpio) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOISTAT); 1788c2ecf20Sopenharmony_ci} 1798c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rb532_gpio_set_istat); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci/* 1828c2ecf20Sopenharmony_ci * Configure GPIO alternate function 1838c2ecf20Sopenharmony_ci */ 1848c2ecf20Sopenharmony_civoid rb532_gpio_set_func(unsigned gpio) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci rb532_set_bit(1, gpio, rb532_gpio_chip->regbase + GPIOFUNC); 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rb532_gpio_set_func); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ciint __init rb532_gpio_init(void) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci struct resource *r; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci r = rb532_gpio_reg0_res; 1958c2ecf20Sopenharmony_ci rb532_gpio_chip->regbase = ioremap(r->start, resource_size(r)); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci if (!rb532_gpio_chip->regbase) { 1988c2ecf20Sopenharmony_ci printk(KERN_ERR "rb532: cannot remap GPIO register 0\n"); 1998c2ecf20Sopenharmony_ci return -ENXIO; 2008c2ecf20Sopenharmony_ci } 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci /* Register our GPIO chip */ 2038c2ecf20Sopenharmony_ci gpiochip_add_data(&rb532_gpio_chip->chip, rb532_gpio_chip); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci return 0; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ciarch_initcall(rb532_gpio_init); 208