Path: blob/master/drivers/gpio/gpio-plat-samsung.c
15111 views
/* arch/arm/plat-samsung/gpiolib.c1*2* Copyright 2008 Openmoko, Inc.3* Copyright 2008 Simtec Electronics4* Ben Dooks <[email protected]>5* http://armlinux.simtec.co.uk/6*7* Copyright (c) 2009 Samsung Electronics Co., Ltd.8* http://www.samsung.com/9*10* SAMSUNG - GPIOlib support11*12* This program is free software; you can redistribute it and/or modify13* it under the terms of the GNU General Public License version 2 as14* published by the Free Software Foundation.15*/1617#include <linux/kernel.h>18#include <linux/irq.h>19#include <linux/io.h>20#include <linux/gpio.h>21#include <plat/gpio-core.h>22#include <plat/gpio-cfg.h>23#include <plat/gpio-cfg-helpers.h>2425#ifndef DEBUG_GPIO26#define gpio_dbg(x...) do { } while (0)27#else28#define gpio_dbg(x...) printk(KERN_DEBUG x)29#endif3031/* The samsung_gpiolib_4bit routines are to control the gpio banks where32* the gpio configuration register (GPxCON) has 4 bits per GPIO, as the33* following example:34*35* base + 0x00: Control register, 4 bits per gpio36* gpio n: 4 bits starting at (4*n)37* 0000 = input, 0001 = output, others mean special-function38* base + 0x04: Data register, 1 bit per gpio39* bit n: data bit n40*41* Note, since the data register is one bit per gpio and is at base + 0x442* we can use s3c_gpiolib_get and s3c_gpiolib_set to change the state of43* the output.44*/4546static int samsung_gpiolib_4bit_input(struct gpio_chip *chip,47unsigned int offset)48{49struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);50void __iomem *base = ourchip->base;51unsigned long con;5253con = __raw_readl(base + GPIOCON_OFF);54con &= ~(0xf << con_4bit_shift(offset));55__raw_writel(con, base + GPIOCON_OFF);5657gpio_dbg("%s: %p: CON now %08lx\n", __func__, base, con);5859return 0;60}6162static int samsung_gpiolib_4bit_output(struct gpio_chip *chip,63unsigned int offset, int value)64{65struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);66void __iomem *base = ourchip->base;67unsigned long con;68unsigned long dat;6970con = __raw_readl(base + GPIOCON_OFF);71con &= ~(0xf << con_4bit_shift(offset));72con |= 0x1 << con_4bit_shift(offset);7374dat = __raw_readl(base + GPIODAT_OFF);7576if (value)77dat |= 1 << offset;78else79dat &= ~(1 << offset);8081__raw_writel(dat, base + GPIODAT_OFF);82__raw_writel(con, base + GPIOCON_OFF);83__raw_writel(dat, base + GPIODAT_OFF);8485gpio_dbg("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);8687return 0;88}8990/* The next set of routines are for the case where the GPIO configuration91* registers are 4 bits per GPIO but there is more than one register (the92* bank has more than 8 GPIOs.93*94* This case is the similar to the 4 bit case, but the registers are as95* follows:96*97* base + 0x00: Control register, 4 bits per gpio (lower 8 GPIOs)98* gpio n: 4 bits starting at (4*n)99* 0000 = input, 0001 = output, others mean special-function100* base + 0x04: Control register, 4 bits per gpio (up to 8 additions GPIOs)101* gpio n: 4 bits starting at (4*n)102* 0000 = input, 0001 = output, others mean special-function103* base + 0x08: Data register, 1 bit per gpio104* bit n: data bit n105*106* To allow us to use the s3c_gpiolib_get and s3c_gpiolib_set routines we107* store the 'base + 0x4' address so that these routines see the data108* register at ourchip->base + 0x04.109*/110111static int samsung_gpiolib_4bit2_input(struct gpio_chip *chip,112unsigned int offset)113{114struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);115void __iomem *base = ourchip->base;116void __iomem *regcon = base;117unsigned long con;118119if (offset > 7)120offset -= 8;121else122regcon -= 4;123124con = __raw_readl(regcon);125con &= ~(0xf << con_4bit_shift(offset));126__raw_writel(con, regcon);127128gpio_dbg("%s: %p: CON %08lx\n", __func__, base, con);129130return 0;131}132133static int samsung_gpiolib_4bit2_output(struct gpio_chip *chip,134unsigned int offset, int value)135{136struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);137void __iomem *base = ourchip->base;138void __iomem *regcon = base;139unsigned long con;140unsigned long dat;141unsigned con_offset = offset;142143if (con_offset > 7)144con_offset -= 8;145else146regcon -= 4;147148con = __raw_readl(regcon);149con &= ~(0xf << con_4bit_shift(con_offset));150con |= 0x1 << con_4bit_shift(con_offset);151152dat = __raw_readl(base + GPIODAT_OFF);153154if (value)155dat |= 1 << offset;156else157dat &= ~(1 << offset);158159__raw_writel(dat, base + GPIODAT_OFF);160__raw_writel(con, regcon);161__raw_writel(dat, base + GPIODAT_OFF);162163gpio_dbg("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);164165return 0;166}167168void __init samsung_gpiolib_add_4bit(struct s3c_gpio_chip *chip)169{170chip->chip.direction_input = samsung_gpiolib_4bit_input;171chip->chip.direction_output = samsung_gpiolib_4bit_output;172chip->pm = __gpio_pm(&s3c_gpio_pm_4bit);173}174175void __init samsung_gpiolib_add_4bit2(struct s3c_gpio_chip *chip)176{177chip->chip.direction_input = samsung_gpiolib_4bit2_input;178chip->chip.direction_output = samsung_gpiolib_4bit2_output;179chip->pm = __gpio_pm(&s3c_gpio_pm_4bit);180}181182void __init samsung_gpiolib_add_4bit_chips(struct s3c_gpio_chip *chip,183int nr_chips)184{185for (; nr_chips > 0; nr_chips--, chip++) {186samsung_gpiolib_add_4bit(chip);187s3c_gpiolib_add(chip);188}189}190191void __init samsung_gpiolib_add_4bit2_chips(struct s3c_gpio_chip *chip,192int nr_chips)193{194for (; nr_chips > 0; nr_chips--, chip++) {195samsung_gpiolib_add_4bit2(chip);196s3c_gpiolib_add(chip);197}198}199200void __init samsung_gpiolib_add_2bit_chips(struct s3c_gpio_chip *chip,201int nr_chips)202{203for (; nr_chips > 0; nr_chips--, chip++)204s3c_gpiolib_add(chip);205}206207208