aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips/pmc-sierra/msp71xx/gpio.c
blob: 69848c5813e2ae5f9d34929d46733e031207f102 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * @file /arch/mips/pmc-sierra/msp71xx/gpio.c
 *
 * Generic PMC MSP71xx GPIO handling. These base gpio are controlled by two
 * types of registers. The data register sets the output level when in output
 * mode and when in input mode will contain the value at the input. The config
 * register sets the various modes for each gpio.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * @author Patrick Glass <patrickglass@gmail.com>
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/spinlock.h>
#include <linux/io.h>

#define MSP71XX_CFG_OFFSET(gpio)	(4 * (gpio))
#define CONF_MASK			0x0F
#define MSP71XX_GPIO_INPUT		0x01
#define MSP71XX_GPIO_OUTPUT		0x08

#define MSP71XX_GPIO_BASE		0x0B8400000L

#define to_msp71xx_gpio_chip(c) container_of(c, struct msp71xx_gpio_chip, chip)

static spinlock_t gpio_lock;

/*
 * struct msp71xx_gpio_chip - container for gpio chip and registers
 * @chip: chip structure for the specified gpio bank
 * @data_reg: register for reading and writing the gpio pin value
 * @config_reg: register to set the mode for the gpio pin bank
 * @out_drive_reg: register to set the output drive mode for the gpio pin bank
 */
struct msp71xx_gpio_chip {
	struct gpio_chip chip;
	void __iomem *data_reg;
	void __iomem *config_reg;
	void __iomem *out_drive_reg;
};

/*
 * msp71xx_gpio_get() - return the chip's gpio value
 * @chip: chip structure which controls the specified gpio
 * @offset: gpio whose value will be returned
 *
 * It will return 0 if gpio value is low and other if high.
 */
static int msp71xx_gpio_get(struct gpio_chip *chip, unsigned offset)
{
	struct msp71xx_gpio_chip *msp_chip = to_msp71xx_gpio_chip(chip);

	return __raw_readl(msp_chip->data_reg) & (1 << offset);
}

/*
 * msp71xx_gpio_set() - set the output value for the gpio
 * @chip: chip structure who controls the specified gpio
 * @offset: gpio whose value will be assigned
 * @value: logic level to assign to the gpio initially
 *
 * This will set the gpio bit specified to the desired value. It will set the
 * gpio pin low if value is 0 otherwise it will be high.
 */
static void msp71xx_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct msp71xx_gpio_chip *msp_chip = to_msp71xx_gpio_chip(chip);
	unsigned long flags;
	u32 data;

	spin_lock_irqsave(&gpio_lock, flags);

	data = __raw_readl(msp_chip->data_reg);
	if (value)
		data |= (1 << offset);
	else
		data &= ~(1 << offset);
	__raw_writel(data, msp_chip->data_reg);

	spin_unlock_irqrestore(&gpio_lock, flags);
}

/*
 * msp71xx_set_gpio_mode() - declare the mode for a gpio
 * @chip: chip structure which controls the specified gpio
 * @offset: gpio whose value will be assigned
 * @mode: desired configuration for the gpio (see datasheet)
 *
 * It will set the gpio pin config to the @mode value passed in.
 */
static int msp71xx_set_gpio_mode(struct gpio_chip *chip,
				 unsigned offset, int mode)
{
	struct msp71xx_gpio_chip *msp_chip = to_msp71xx_gpio_chip(chip);
	const unsigned bit_offset = MSP71XX_CFG_OFFSET(offset);
	unsigned long flags;
	u32 cfg;

	spin_lock_irqsave(&gpio_lock, flags);

	cfg = __raw_readl(msp_chip->config_reg);
	cfg &= ~(CONF_MASK << bit_offset);
	cfg |= (mode << bit_offset);
	__raw_writel(cfg, msp_chip->config_reg);

	spin_unlock_irqrestore(&gpio_lock, flags);

	return 0;
}

/*
 * msp71xx_direction_output() - declare the direction mode for a gpio
 * @chip: chip structure which controls the specified gpio
 * @offset: gpio whose value will be assigned
 * @value: logic level to assign to the gpio initially
 *
 * This call will set the mode for the @gpio to output. It will set the
 * gpio pin low if value is 0 otherwise it will be high.
 */
static int msp71xx_direction_output(struct gpio_chip *chip,
				    unsigned offset, int value)
{
	msp71xx_gpio_set(chip, offset, value);

	return msp71xx_set_gpio_mode(chip, offset, MSP71XX_GPIO_OUTPUT);
}

/*
 * msp71xx_direction_input() - declare the direction mode for a gpio
 * @chip: chip structure which controls the specified gpio
 * @offset: gpio whose to which the value will be assigned
 *
 * This call will set the mode for the @gpio to input.
 */
static int msp71xx_direction_input(struct gpio_chip *chip, unsigned offset)
{
	return msp71xx_set_gpio_mode(chip, offset, MSP71XX_GPIO_INPUT);
}

/*
 * msp71xx_set_output_drive() - declare the output drive for the gpio line
 * @gpio: gpio pin whose output drive you wish to modify
 * @value: zero for active drain 1 for open drain drive
 *
 * This call will set the output drive mode for the @gpio to output.
 */
int msp71xx_set_output_drive(unsigned gpio, int value)
{
	unsigned long flags;
	u32 data;

	if (gpio > 15 || gpio < 0)
		return -EINVAL;

	spin_lock_irqsave(&gpio_lock, flags);

	data = __raw_readl((void __iomem *)(MSP71XX_GPIO_BASE + 0x190));
	if (value)
		data |= (1 << gpio);
	else
		data &= ~(1 << gpio);
	__raw_writel(data, (void __iomem *)(MSP71XX_GPIO_BASE + 0x190));

	spin_unlock_irqrestore(&gpio_lock, flags);

	return 0;
}
EXPORT_SYMBOL(msp71xx_set_output_drive);

#define MSP71XX_GPIO_BANK(name, dr, cr, base_gpio, num_gpio) \
{ \
	.chip = { \
		.label		  = name, \
		.direction_input  = msp71xx_direction_input, \
		.direction_output = msp71xx_direction_output, \
		.get		  = msp71xx_gpio_get, \
		.set		  = msp71xx_gpio_set, \
		.base		  = base_gpio, \
		.ngpio		  = num_gpio \
	}, \
	.data_reg	= (void __iomem *)(MSP71XX_GPIO_BASE + dr), \
	.config_reg	= (void __iomem *)(MSP71XX_GPIO_BASE + cr), \
	.out_drive_reg	= (void __iomem *)(MSP71XX_GPIO_BASE + 0x190), \
}

/*
 * struct msp71xx_gpio_banks[] - container array of gpio banks
 * @chip: chip structure for the specified gpio bank
 * @data_reg: register for reading and writing the gpio pin value
 * @config_reg: register to set the mode for the gpio pin bank
 *
 * This array structure defines the gpio banks for the PMC MIPS Processor.
 * We specify the bank name, the data register, the config register, base
 * starting gpio number, and the number of gpios exposed by the bank.
 */
static struct msp71xx_gpio_chip msp71xx_gpio_banks[] = {

	MSP71XX_GPIO_BANK("GPIO_1_0", 0x170, 0x180, 0, 2),
	MSP71XX_GPIO_BANK("GPIO_5_2", 0x174, 0x184, 2, 4),
	MSP71XX_GPIO_BANK("GPIO_9_6", 0x178, 0x188, 6, 4),
	MSP71XX_GPIO_BANK("GPIO_15_10", 0x17C, 0x18C, 10, 6),
};

void __init msp71xx_init_gpio(void)
{
	int i;

	spin_lock_init(&gpio_lock);

	for (i = 0; i < ARRAY_SIZE(msp71xx_gpio_banks); i++)
		gpiochip_add(&msp71xx_gpio_banks[i].chip);
}