Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * This file is subject to the terms and conditions of the GNU General Public
  3 * License.  See the file "COPYING" in the main directory of this archive
  4 * for more details.
  5 *
  6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7 * Copyright (C) 2008 Florian Fainelli <florian@openwrt.org>
  8 */
  9
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 12#include <linux/spinlock.h>
 13#include <linux/platform_device.h>
 14#include <linux/gpio.h>
 15
 16#include <bcm63xx_cpu.h>
 17#include <bcm63xx_gpio.h>
 18#include <bcm63xx_io.h>
 19#include <bcm63xx_regs.h>
 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 21static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
 22static u32 gpio_out_low, gpio_out_high;
 23
 24static void bcm63xx_gpio_set(struct gpio_chip *chip,
 25			     unsigned gpio, int val)
 26{
 27	u32 reg;
 28	u32 mask;
 29	u32 *v;
 30	unsigned long flags;
 31
 32	if (gpio >= chip->ngpio)
 33		BUG();
 34
 35	if (gpio < 32) {
 36		reg = GPIO_DATA_LO_REG;
 37		mask = 1 << gpio;
 38		v = &gpio_out_low;
 39	} else {
 40		reg = GPIO_DATA_HI_REG;
 41		mask = 1 << (gpio - 32);
 42		v = &gpio_out_high;
 43	}
 44
 45	spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
 46	if (val)
 47		*v |= mask;
 48	else
 49		*v &= ~mask;
 50	bcm_gpio_writel(*v, reg);
 51	spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
 52}
 53
 54static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
 55{
 56	u32 reg;
 57	u32 mask;
 58
 59	if (gpio >= chip->ngpio)
 60		BUG();
 61
 62	if (gpio < 32) {
 63		reg = GPIO_DATA_LO_REG;
 64		mask = 1 << gpio;
 65	} else {
 66		reg = GPIO_DATA_HI_REG;
 67		mask = 1 << (gpio - 32);
 68	}
 69
 70	return !!(bcm_gpio_readl(reg) & mask);
 71}
 72
 73static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
 74				      unsigned gpio, int dir)
 75{
 76	u32 reg;
 77	u32 mask;
 78	u32 tmp;
 79	unsigned long flags;
 80
 81	if (gpio >= chip->ngpio)
 82		BUG();
 83
 84	if (gpio < 32) {
 85		reg = GPIO_CTL_LO_REG;
 86		mask = 1 << gpio;
 87	} else {
 88		reg = GPIO_CTL_HI_REG;
 89		mask = 1 << (gpio - 32);
 90	}
 91
 92	spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
 93	tmp = bcm_gpio_readl(reg);
 94	if (dir == BCM63XX_GPIO_DIR_IN)
 95		tmp &= ~mask;
 96	else
 97		tmp |= mask;
 98	bcm_gpio_writel(tmp, reg);
 99	spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
100
101	return 0;
102}
103
104static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
105{
106	return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN);
107}
108
109static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
110					 unsigned gpio, int value)
111{
112	bcm63xx_gpio_set(chip, gpio, value);
113	return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT);
114}
115
116
117static struct gpio_chip bcm63xx_gpio_chip = {
118	.label			= "bcm63xx-gpio",
119	.direction_input	= bcm63xx_gpio_direction_input,
120	.direction_output	= bcm63xx_gpio_direction_output,
121	.get			= bcm63xx_gpio_get,
122	.set			= bcm63xx_gpio_set,
123	.base			= 0,
124};
125
126int __init bcm63xx_gpio_init(void)
127{
128	gpio_out_low = bcm_gpio_readl(GPIO_DATA_LO_REG);
129	gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG);
 
 
 
130	bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
131	pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
132
133	return gpiochip_add(&bcm63xx_gpio_chip);
134}
v5.4
  1/*
  2 * This file is subject to the terms and conditions of the GNU General Public
  3 * License.  See the file "COPYING" in the main directory of this archive
  4 * for more details.
  5 *
  6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7 * Copyright (C) 2008-2011 Florian Fainelli <florian@openwrt.org>
  8 */
  9
 10#include <linux/kernel.h>
 11#include <linux/init.h>
 12#include <linux/spinlock.h>
 13#include <linux/platform_device.h>
 14#include <linux/gpio/driver.h>
 15
 16#include <bcm63xx_cpu.h>
 17#include <bcm63xx_gpio.h>
 18#include <bcm63xx_io.h>
 19#include <bcm63xx_regs.h>
 20
 21static u32 gpio_out_low_reg;
 22
 23static void bcm63xx_gpio_out_low_reg_init(void)
 24{
 25	switch (bcm63xx_get_cpu_id()) {
 26	case BCM6345_CPU_ID:
 27		gpio_out_low_reg = GPIO_DATA_LO_REG_6345;
 28		break;
 29	default:
 30		gpio_out_low_reg = GPIO_DATA_LO_REG;
 31		break;
 32	}
 33}
 34
 35static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
 36static u32 gpio_out_low, gpio_out_high;
 37
 38static void bcm63xx_gpio_set(struct gpio_chip *chip,
 39			     unsigned gpio, int val)
 40{
 41	u32 reg;
 42	u32 mask;
 43	u32 *v;
 44	unsigned long flags;
 45
 46	if (gpio >= chip->ngpio)
 47		BUG();
 48
 49	if (gpio < 32) {
 50		reg = gpio_out_low_reg;
 51		mask = 1 << gpio;
 52		v = &gpio_out_low;
 53	} else {
 54		reg = GPIO_DATA_HI_REG;
 55		mask = 1 << (gpio - 32);
 56		v = &gpio_out_high;
 57	}
 58
 59	spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
 60	if (val)
 61		*v |= mask;
 62	else
 63		*v &= ~mask;
 64	bcm_gpio_writel(*v, reg);
 65	spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
 66}
 67
 68static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
 69{
 70	u32 reg;
 71	u32 mask;
 72
 73	if (gpio >= chip->ngpio)
 74		BUG();
 75
 76	if (gpio < 32) {
 77		reg = gpio_out_low_reg;
 78		mask = 1 << gpio;
 79	} else {
 80		reg = GPIO_DATA_HI_REG;
 81		mask = 1 << (gpio - 32);
 82	}
 83
 84	return !!(bcm_gpio_readl(reg) & mask);
 85}
 86
 87static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
 88				      unsigned gpio, int dir)
 89{
 90	u32 reg;
 91	u32 mask;
 92	u32 tmp;
 93	unsigned long flags;
 94
 95	if (gpio >= chip->ngpio)
 96		BUG();
 97
 98	if (gpio < 32) {
 99		reg = GPIO_CTL_LO_REG;
100		mask = 1 << gpio;
101	} else {
102		reg = GPIO_CTL_HI_REG;
103		mask = 1 << (gpio - 32);
104	}
105
106	spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
107	tmp = bcm_gpio_readl(reg);
108	if (dir == BCM63XX_GPIO_DIR_IN)
109		tmp &= ~mask;
110	else
111		tmp |= mask;
112	bcm_gpio_writel(tmp, reg);
113	spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
114
115	return 0;
116}
117
118static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
119{
120	return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN);
121}
122
123static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
124					 unsigned gpio, int value)
125{
126	bcm63xx_gpio_set(chip, gpio, value);
127	return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT);
128}
129
130
131static struct gpio_chip bcm63xx_gpio_chip = {
132	.label			= "bcm63xx-gpio",
133	.direction_input	= bcm63xx_gpio_direction_input,
134	.direction_output	= bcm63xx_gpio_direction_output,
135	.get			= bcm63xx_gpio_get,
136	.set			= bcm63xx_gpio_set,
137	.base			= 0,
138};
139
140int __init bcm63xx_gpio_init(void)
141{
142	bcm63xx_gpio_out_low_reg_init();
143
144	gpio_out_low = bcm_gpio_readl(gpio_out_low_reg);
145	if (!BCMCPU_IS_6345())
146		gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG);
147	bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
148	pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
149
150	return gpiochip_add_data(&bcm63xx_gpio_chip, NULL);
151}