Blame view

drivers/pinctrl/pinctrl-coh901.c 25 KB
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
1
  /*
c103de240   Grant Likely   gpio: reorganize ...
2
   * U300 GPIO module.
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
3
   *
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
4
   * Copyright (C) 2007-2011 ST-Ericsson AB
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
5
   * License terms: GNU General Public License (GPL) version 2
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
6
7
8
9
   * This can driver either of the two basic GPIO cores
   * available in the U300 platforms:
   * COH 901 335   - Used in DB3150 (U300 1.0) and DB3200 (U330 1.0)
   * COH 901 571/3 - Used in DB3210 (U365 2.0) and DB3350 (U335 1.0)
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
10
   * Author: Linus Walleij <linus.walleij@linaro.org>
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
11
   * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
12
13
   */
  #include <linux/module.h>
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
14
  #include <linux/irq.h>
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
15
16
17
18
19
20
21
22
  #include <linux/interrupt.h>
  #include <linux/delay.h>
  #include <linux/errno.h>
  #include <linux/io.h>
  #include <linux/clk.h>
  #include <linux/err.h>
  #include <linux/platform_device.h>
  #include <linux/gpio.h>
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
23
24
  #include <linux/list.h>
  #include <linux/slab.h>
b4e3ac74d   Linus Walleij   pinctrl/coh901: d...
25
  #include <linux/pinctrl/pinmux.h>
eb3cf18cc   Linus Walleij   ARM: 7033/1: mach...
26
  #include <mach/gpio-u300.h>
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
27

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
28
29
30
31
32
33
34
35
36
37
38
39
  /*
   * Bias modes for U300 GPIOs
   *
   * GPIO_U300_CONFIG_BIAS_UNKNOWN: this bias mode is not known to us
   * GPIO_U300_CONFIG_BIAS_FLOAT: no specific bias, the GPIO will float or state
   *	is not controlled by software
   * GPIO_U300_CONFIG_BIAS_PULL_UP: the GPIO will be pulled up (usually with high
   *	impedance to VDD)
   */
  #define GPIO_U300_CONFIG_BIAS_UNKNOWN	0x1000
  #define GPIO_U300_CONFIG_BIAS_FLOAT	0x1001
  #define GPIO_U300_CONFIG_BIAS_PULL_UP	0x1002
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
40

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
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
  /*
   * Drive modes for U300 GPIOs (output)
   *
   * GPIO_U300_CONFIG_DRIVE_PUSH_PULL: the GPIO will be driven actively high and
   *	low, this is the most typical case and is typically achieved with two
   *	active transistors on the output
   * GPIO_U300_CONFIG_DRIVE_OPEN_DRAIN: the GPIO will be driven with open drain
   *	(open collector) which means it is usually wired with other output
   *	ports which are then pulled up with an external resistor
   * GPIO_U300_CONFIG_DRIVE_OPEN_SOURCE: the GPIO will be driven with open drain
   *	(open emitter) which is the same as open drain mutatis mutandis but
   *	pulled to ground
   */
  #define GPIO_U300_CONFIG_DRIVE_PUSH_PULL	0x2000
  #define GPIO_U300_CONFIG_DRIVE_OPEN_DRAIN	0x2001
  #define GPIO_U300_CONFIG_DRIVE_OPEN_SOURCE	0x2002
  
  /*
   * Register definitions for COH 901 335 variant
   */
  #define U300_335_PORT_STRIDE				(0x1C)
  /* Port X Pin Data Register 32bit, this is both input and output (R/W) */
  #define U300_335_PXPDIR					(0x00)
  #define U300_335_PXPDOR					(0x00)
  /* Port X Pin Config Register 32bit (R/W) */
  #define U300_335_PXPCR					(0x04)
  /* This register layout is the same in both blocks */
  #define U300_GPIO_PXPCR_ALL_PINS_MODE_MASK		(0x0000FFFFUL)
  #define U300_GPIO_PXPCR_PIN_MODE_MASK			(0x00000003UL)
  #define U300_GPIO_PXPCR_PIN_MODE_SHIFT			(0x00000002UL)
  #define U300_GPIO_PXPCR_PIN_MODE_INPUT			(0x00000000UL)
  #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL	(0x00000001UL)
  #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN	(0x00000002UL)
  #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE	(0x00000003UL)
  /* Port X Interrupt Event Register 32bit (R/W) */
  #define U300_335_PXIEV					(0x08)
  /* Port X Interrupt Enable Register 32bit (R/W) */
  #define U300_335_PXIEN					(0x0C)
  /* Port X Interrupt Force Register 32bit (R/W) */
  #define U300_335_PXIFR					(0x10)
  /* Port X Interrupt Config Register 32bit (R/W) */
  #define U300_335_PXICR					(0x14)
  /* This register layout is the same in both blocks */
  #define U300_GPIO_PXICR_ALL_IRQ_CONFIG_MASK		(0x000000FFUL)
  #define U300_GPIO_PXICR_IRQ_CONFIG_MASK			(0x00000001UL)
  #define U300_GPIO_PXICR_IRQ_CONFIG_FALLING_EDGE		(0x00000000UL)
  #define U300_GPIO_PXICR_IRQ_CONFIG_RISING_EDGE		(0x00000001UL)
  /* Port X Pull-up Enable Register 32bit (R/W) */
  #define U300_335_PXPER					(0x18)
  /* This register layout is the same in both blocks */
  #define U300_GPIO_PXPER_ALL_PULL_UP_DISABLE_MASK	(0x000000FFUL)
  #define U300_GPIO_PXPER_PULL_UP_DISABLE			(0x00000001UL)
  /* Control Register 32bit (R/W) */
  #define U300_335_CR					(0x54)
  #define U300_335_CR_BLOCK_CLOCK_ENABLE			(0x00000001UL)
  
  /*
   * Register definitions for COH 901 571 / 3 variant
   */
  #define U300_571_PORT_STRIDE				(0x30)
  /*
   * Control Register 32bit (R/W)
   * bit 15-9 (mask 0x0000FE00) contains the number of cores. 8*cores
   * gives the number of GPIO pins.
   * bit 8-2  (mask 0x000001FC) contains the core version ID.
   */
  #define U300_571_CR					(0x00)
  #define U300_571_CR_SYNC_SEL_ENABLE			(0x00000002UL)
  #define U300_571_CR_BLOCK_CLKRQ_ENABLE			(0x00000001UL)
  /*
   * These registers have the same layout and function as the corresponding
   * COH 901 335 registers, just at different offset.
   */
  #define U300_571_PXPDIR					(0x04)
  #define U300_571_PXPDOR					(0x08)
  #define U300_571_PXPCR					(0x0C)
  #define U300_571_PXPER					(0x10)
  #define U300_571_PXIEV					(0x14)
  #define U300_571_PXIEN					(0x18)
  #define U300_571_PXIFR					(0x1C)
  #define U300_571_PXICR					(0x20)
  
  /* 8 bits per port, no version has more than 7 ports */
  #define U300_GPIO_PINS_PER_PORT 8
  #define U300_GPIO_MAX (U300_GPIO_PINS_PER_PORT * 7)
  
  struct u300_gpio {
  	struct gpio_chip chip;
  	struct list_head port_list;
  	struct clk *clk;
  	struct resource *memres;
  	void __iomem *base;
  	struct device *dev;
  	int irq_base;
  	u32 stride;
  	/* Register offsets */
  	u32 pcr;
  	u32 dor;
  	u32 dir;
  	u32 per;
  	u32 icr;
  	u32 ien;
  	u32 iev;
  };
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
145
146
  
  struct u300_gpio_port {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
147
148
149
  	struct list_head node;
  	struct u300_gpio *gpio;
  	char name[8];
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
150
151
  	int irq;
  	int number;
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
152
  	u8 toggle_edge_mode;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
153
  };
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
154
155
156
157
158
159
160
161
162
  /*
   * Macro to expand to read a specific register found in the "gpio"
   * struct. It requires the struct u300_gpio *gpio variable to exist in
   * its context. It calculates the port offset from the given pin
   * offset, muliplies by the port stride and adds the register offset
   * so it provides a pointer to the desired register.
   */
  #define U300_PIN_REG(pin, reg) \
  	(gpio->base + (pin >> 3) * gpio->stride + gpio->reg)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
163

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
164
165
166
167
168
169
  /*
   * Provides a bitmask for a specific gpio pin inside an 8-bit GPIO
   * register.
   */
  #define U300_PIN_BIT(pin) \
  	(1 << (pin & 0x07))
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
170

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
171
172
173
174
  struct u300_gpio_confdata {
  	u16 bias_mode;
  	bool output;
  	int outval;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
175
  };
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
176
177
178
179
  /* BS335 has seven ports of 8 bits each = GPIO pins 0..55 */
  #define BS335_GPIO_NUM_PORTS 7
  /* BS365 has five ports of 8 bits each = GPIO pins 0..39 */
  #define BS365_GPIO_NUM_PORTS 5
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
180

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
181
182
183
184
  #define U300_FLOATING_INPUT { \
  	.bias_mode = GPIO_U300_CONFIG_BIAS_FLOAT, \
  	.output = false, \
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
185

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
186
187
188
189
  #define U300_PULL_UP_INPUT { \
  	.bias_mode = GPIO_U300_CONFIG_BIAS_PULL_UP, \
  	.output = false, \
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
190

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
191
192
193
194
  #define U300_OUTPUT_LOW { \
  	.output = true, \
  	.outval = 0, \
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
195

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
196
197
198
199
  #define U300_OUTPUT_HIGH { \
  	.output = true, \
  	.outval = 1, \
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
200

bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
201
202
  
  /* Initial configuration */
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
203
204
  static const struct __initdata u300_gpio_confdata
  bs335_gpio_config[BS335_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
205
206
  	/* Port 0, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
207
208
209
210
211
212
213
214
  		U300_FLOATING_INPUT,
  		U300_OUTPUT_HIGH,
  		U300_FLOATING_INPUT,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
215
216
217
  	},
  	/* Port 1, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
218
219
220
221
222
223
224
225
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_PULL_UP_INPUT,
  		U300_FLOATING_INPUT,
  		U300_OUTPUT_HIGH,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
226
227
228
  	},
  	/* Port 2, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
229
230
231
232
233
234
235
236
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_OUTPUT_LOW,
  		U300_PULL_UP_INPUT,
  		U300_OUTPUT_LOW,
  		U300_PULL_UP_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
237
238
239
  	},
  	/* Port 3, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
240
241
242
243
244
245
246
247
  		U300_PULL_UP_INPUT,
  		U300_OUTPUT_LOW,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
248
249
250
  	},
  	/* Port 4, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
251
252
253
254
255
256
257
258
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
259
260
261
  	},
  	/* Port 5, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
262
263
264
265
266
267
268
269
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
270
271
272
  	},
  	/* Port 6, pind 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
273
274
275
276
277
278
279
280
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
281
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
282
  };
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
283

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
284
285
  static const struct __initdata u300_gpio_confdata
  bs365_gpio_config[BS365_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
286
287
  	/* Port 0, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
288
289
290
291
292
293
294
295
  		U300_FLOATING_INPUT,
  		U300_OUTPUT_LOW,
  		U300_FLOATING_INPUT,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_PULL_UP_INPUT,
  		U300_FLOATING_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
296
297
298
  	},
  	/* Port 1, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
299
300
301
302
303
304
305
306
  		U300_OUTPUT_LOW,
  		U300_FLOATING_INPUT,
  		U300_OUTPUT_LOW,
  		U300_FLOATING_INPUT,
  		U300_FLOATING_INPUT,
  		U300_OUTPUT_HIGH,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
307
308
309
  	},
  	/* Port 2, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
310
311
312
313
314
315
316
317
  		U300_FLOATING_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
318
319
320
  	},
  	/* Port 3, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
321
322
323
324
325
326
327
328
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
329
330
331
  	},
  	/* Port 4, pins 0-7 */
  	{
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
332
333
334
335
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
  		U300_PULL_UP_INPUT,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
336
  		/* These 4 pins doesn't exist on DB3210 */
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
337
338
339
340
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
  		U300_OUTPUT_LOW,
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
341
  	}
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
342
  };
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
343

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
344
345
346
  /**
   * to_u300_gpio() - get the pointer to u300_gpio
   * @chip: the gpio chip member of the structure u300_gpio
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
347
   */
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
348
  static inline struct u300_gpio *to_u300_gpio(struct gpio_chip *chip)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
349
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
350
  	return container_of(chip, struct u300_gpio, chip);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
351
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
352

b4e3ac74d   Linus Walleij   pinctrl/coh901: d...
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  static int u300_gpio_request(struct gpio_chip *chip, unsigned offset)
  {
  	/*
  	 * Map back to global GPIO space and request muxing, the direction
  	 * parameter does not matter for this controller.
  	 */
  	int gpio = chip->base + offset;
  
  	return pinmux_request_gpio(gpio);
  }
  
  static void u300_gpio_free(struct gpio_chip *chip, unsigned offset)
  {
  	int gpio = chip->base + offset;
  
  	pinmux_free_gpio(gpio);
  }
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
370
  static int u300_gpio_get(struct gpio_chip *chip, unsigned offset)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
371
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
372
  	struct u300_gpio *gpio = to_u300_gpio(chip);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
373

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
374
  	return readl(U300_PIN_REG(offset, dir)) & U300_PIN_BIT(offset);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
375
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
376

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
377
  static void u300_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
ee17962e2   Linus Walleij   ARM: 5731/2: Fix ...
378
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
379
380
381
  	struct u300_gpio *gpio = to_u300_gpio(chip);
  	unsigned long flags;
  	u32 val;
ee17962e2   Linus Walleij   ARM: 5731/2: Fix ...
382

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
383
  	local_irq_save(flags);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
384

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
385
386
387
388
389
  	val = readl(U300_PIN_REG(offset, dor));
  	if (value)
  		writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
  	else
  		writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
390

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
391
  	local_irq_restore(flags);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
392
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
393

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
394
  static int u300_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
395
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
396
397
398
  	struct u300_gpio *gpio = to_u300_gpio(chip);
  	unsigned long flags;
  	u32 val;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
399

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
400
401
402
403
404
405
406
  	local_irq_save(flags);
  	val = readl(U300_PIN_REG(offset, pcr));
  	/* Mask out this pin, note 2 bits per setting */
  	val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
  	writel(val, U300_PIN_REG(offset, pcr));
  	local_irq_restore(flags);
  	return 0;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
407
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
408

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
409
410
  static int u300_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  				      int value)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
411
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
412
  	struct u300_gpio *gpio = to_u300_gpio(chip);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
413
  	unsigned long flags;
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
414
415
  	u32 oldmode;
  	u32 val;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
416
417
  
  	local_irq_save(flags);
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  	val = readl(U300_PIN_REG(offset, pcr));
  	/*
  	 * Drive mode must be set by the special mode set function, set
  	 * push/pull mode by default if no mode has been selected.
  	 */
  	oldmode = val & (U300_GPIO_PXPCR_PIN_MODE_MASK <<
  			 ((offset & 0x07) << 1));
  	/* mode = 0 means input, else some mode is already set */
  	if (oldmode == 0) {
  		val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK <<
  			 ((offset & 0x07) << 1));
  		val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  			<< ((offset & 0x07) << 1));
  		writel(val, U300_PIN_REG(offset, pcr));
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
432
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
433
  	u300_gpio_set(chip, offset, value);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
434
  	local_irq_restore(flags);
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
435
  	return 0;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
436
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
437

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
438
  static int u300_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
439
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
440
441
442
443
444
445
446
447
448
449
450
451
452
  	struct u300_gpio *gpio = to_u300_gpio(chip);
  	int retirq = gpio->irq_base + offset;
  
  	dev_dbg(gpio->dev, "request IRQ for GPIO %d, return %d
  ", offset,
  		retirq);
  	return retirq;
  }
  
  static int u300_gpio_config(struct gpio_chip *chip, unsigned offset,
  		     u16 param, unsigned long *data)
  {
  	struct u300_gpio *gpio = to_u300_gpio(chip);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
453
454
  	unsigned long flags;
  	u32 val;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
455
  	local_irq_save(flags);
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  	switch (param) {
  	case GPIO_U300_CONFIG_BIAS_UNKNOWN:
  	case GPIO_U300_CONFIG_BIAS_FLOAT:
  		val = readl(U300_PIN_REG(offset, per));
  		writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  		break;
  	case GPIO_U300_CONFIG_BIAS_PULL_UP:
  		val = readl(U300_PIN_REG(offset, per));
  		writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  		break;
  	case GPIO_U300_CONFIG_DRIVE_PUSH_PULL:
  		val = readl(U300_PIN_REG(offset, pcr));
  		val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  			 << ((offset & 0x07) << 1));
  		val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  			<< ((offset & 0x07) << 1));
  		writel(val, U300_PIN_REG(offset, pcr));
  		break;
  	case GPIO_U300_CONFIG_DRIVE_OPEN_DRAIN:
  		val = readl(U300_PIN_REG(offset, pcr));
  		val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  			 << ((offset & 0x07) << 1));
  		val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN
  			<< ((offset & 0x07) << 1));
  		writel(val, U300_PIN_REG(offset, pcr));
  		break;
  	case GPIO_U300_CONFIG_DRIVE_OPEN_SOURCE:
  		val = readl(U300_PIN_REG(offset, pcr));
  		val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  			 << ((offset & 0x07) << 1));
  		val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE
  			<< ((offset & 0x07) << 1));
  		writel(val, U300_PIN_REG(offset, pcr));
  		break;
  	default:
  		local_irq_restore(flags);
  		dev_err(gpio->dev, "illegal configuration requested
  ");
  		return -EINVAL;
  	}
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
496
497
498
  	local_irq_restore(flags);
  	return 0;
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
499

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
500
501
502
  static struct gpio_chip u300_gpio_chip = {
  	.label			= "u300-gpio-chip",
  	.owner			= THIS_MODULE,
b4e3ac74d   Linus Walleij   pinctrl/coh901: d...
503
504
  	.request		= u300_gpio_request,
  	.free			= u300_gpio_free,
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
505
506
507
508
509
510
511
512
  	.get			= u300_gpio_get,
  	.set			= u300_gpio_set,
  	.direction_input	= u300_gpio_direction_input,
  	.direction_output	= u300_gpio_direction_output,
  	.to_irq			= u300_gpio_to_irq,
  };
  
  static void u300_toggle_trigger(struct u300_gpio *gpio, unsigned offset)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
513
  {
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
514
  	u32 val;
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  	val = readl(U300_PIN_REG(offset, icr));
  	/* Set mode depending on state */
  	if (u300_gpio_get(&gpio->chip, offset)) {
  		/* High now, let's trigger on falling edge next then */
  		writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  		dev_dbg(gpio->dev, "next IRQ on falling edge on pin %d
  ",
  			offset);
  	} else {
  		/* Low now, let's trigger on rising edge next then */
  		writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  		dev_dbg(gpio->dev, "next IRQ on rising edge on pin %d
  ",
  			offset);
  	}
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
530
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
531

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
532
  static int u300_gpio_irq_type(struct irq_data *d, unsigned trigger)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
533
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
534
535
536
  	struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  	struct u300_gpio *gpio = port->gpio;
  	int offset = d->irq - gpio->irq_base;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
537
  	u32 val;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
538

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  	if ((trigger & IRQF_TRIGGER_RISING) &&
  	    (trigger & IRQF_TRIGGER_FALLING)) {
  		/*
  		 * The GPIO block can only trigger on falling OR rising edges,
  		 * not both. So we need to toggle the mode whenever the pin
  		 * goes from one state to the other with a special state flag
  		 */
  		dev_dbg(gpio->dev,
  			"trigger on both rising and falling edge on pin %d
  ",
  			offset);
  		port->toggle_edge_mode |= U300_PIN_BIT(offset);
  		u300_toggle_trigger(gpio, offset);
  	} else if (trigger & IRQF_TRIGGER_RISING) {
  		dev_dbg(gpio->dev, "trigger on rising edge on pin %d
  ",
  			offset);
  		val = readl(U300_PIN_REG(offset, icr));
  		writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  		port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  	} else if (trigger & IRQF_TRIGGER_FALLING) {
  		dev_dbg(gpio->dev, "trigger on falling edge on pin %d
  ",
  			offset);
  		val = readl(U300_PIN_REG(offset, icr));
  		writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  		port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  	}
  
  	return 0;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
569
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
570

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
571
  static void u300_gpio_irq_enable(struct irq_data *d)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
572
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
573
574
575
  	struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  	struct u300_gpio *gpio = port->gpio;
  	int offset = d->irq - gpio->irq_base;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
576
577
578
579
  	u32 val;
  	unsigned long flags;
  
  	local_irq_save(flags);
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
580
581
  	val = readl(U300_PIN_REG(offset, ien));
  	writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
582
583
  	local_irq_restore(flags);
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
584

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
585
  static void u300_gpio_irq_disable(struct irq_data *d)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
586
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
587
588
589
  	struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  	struct u300_gpio *gpio = port->gpio;
  	int offset = d->irq - gpio->irq_base;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
590
591
592
593
  	u32 val;
  	unsigned long flags;
  
  	local_irq_save(flags);
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
594
595
  	val = readl(U300_PIN_REG(offset, ien));
  	writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
596
597
  	local_irq_restore(flags);
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
598

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
599
600
601
602
603
604
605
606
607
  static struct irq_chip u300_gpio_irqchip = {
  	.name			= "u300-gpio-irqchip",
  	.irq_enable		= u300_gpio_irq_enable,
  	.irq_disable		= u300_gpio_irq_disable,
  	.irq_set_type		= u300_gpio_irq_type,
  
  };
  
  static void u300_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
608
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
609
610
611
612
  	struct u300_gpio_port *port = irq_get_handler_data(irq);
  	struct u300_gpio *gpio = port->gpio;
  	int pinoffset = port->number << 3; /* get the right stride */
  	unsigned long val;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
613

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
614
  	desc->irq_data.chip->irq_ack(&desc->irq_data);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
615
  	/* Read event register */
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
616
  	val = readl(U300_PIN_REG(pinoffset, iev));
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
617
  	/* Mask relevant bits */
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
618
  	val &= 0xFFU; /* 8 bits per port */
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
619
  	/* ACK IRQ (clear event) */
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
  	writel(val, U300_PIN_REG(pinoffset, iev));
  
  	/* Call IRQ handler */
  	if (val != 0) {
  		int irqoffset;
  
  		for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) {
  			int pin_irq = gpio->irq_base + (port->number << 3)
  				+ irqoffset;
  			int offset = pinoffset + irqoffset;
  
  			dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d
  ",
  				pin_irq, offset);
  			generic_handle_irq(pin_irq);
  			/*
  			 * Triggering IRQ on both rising and falling edge
  			 * needs mockery
  			 */
  			if (port->toggle_edge_mode & U300_PIN_BIT(offset))
  				u300_toggle_trigger(gpio, offset);
  		}
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
642
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
643
644
  
  	desc->irq_data.chip->irq_unmask(&desc->irq_data);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
645
  }
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
646
647
648
  static void __init u300_gpio_init_pin(struct u300_gpio *gpio,
  				      int offset,
  				      const struct u300_gpio_confdata *conf)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
649
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
650
651
652
  	/* Set mode: input or output */
  	if (conf->output) {
  		u300_gpio_direction_output(&gpio->chip, offset, conf->outval);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
653

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
  		/* Deactivate bias mode for output */
  		u300_gpio_config(&gpio->chip, offset,
  				 GPIO_U300_CONFIG_BIAS_FLOAT,
  				 NULL);
  
  		/* Set drive mode for output */
  		u300_gpio_config(&gpio->chip, offset,
  				 GPIO_U300_CONFIG_DRIVE_PUSH_PULL, NULL);
  
  		dev_dbg(gpio->dev, "set up pin %d as output, value: %d
  ",
  			offset, conf->outval);
  	} else {
  		u300_gpio_direction_input(&gpio->chip, offset);
  
  		/* Always set output low on input pins */
  		u300_gpio_set(&gpio->chip, offset, 0);
  
  		/* Set bias mode for input */
  		u300_gpio_config(&gpio->chip, offset, conf->bias_mode, NULL);
  
  		dev_dbg(gpio->dev, "set up pin %d as input, bias: %04x
  ",
  			offset, conf->bias_mode);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
678
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
679
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
680

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
  static void __init u300_gpio_init_coh901571(struct u300_gpio *gpio,
  				     struct u300_gpio_platform *plat)
  {
  	int i, j;
  
  	/* Write default config and values to all pins */
  	for (i = 0; i < plat->ports; i++) {
  		for (j = 0; j < 8; j++) {
  			const struct u300_gpio_confdata *conf;
  			int offset = (i*8) + j;
  
  			if (plat->variant == U300_GPIO_COH901571_3_BS335)
  				conf = &bs335_gpio_config[i][j];
  			else if (plat->variant == U300_GPIO_COH901571_3_BS365)
  				conf = &bs365_gpio_config[i][j];
  			else
  				break;
  
  			u300_gpio_init_pin(gpio, offset, conf);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
700
701
  		}
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
702
  }
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
703

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
704
705
706
707
708
709
710
711
712
713
  static inline void u300_gpio_free_ports(struct u300_gpio *gpio)
  {
  	struct u300_gpio_port *port;
  	struct list_head *p, *n;
  
  	list_for_each_safe(p, n, &gpio->port_list) {
  		port = list_entry(p, struct u300_gpio_port, node);
  		list_del(&port->node);
  		free_irq(port->irq, port);
  		kfree(port);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
714
  	}
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
715
  }
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
716
  static int __init u300_gpio_probe(struct platform_device *pdev)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
717
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
718
719
  	struct u300_gpio_platform *plat = dev_get_platdata(&pdev->dev);
  	struct u300_gpio *gpio;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
720
  	int err = 0;
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
721
722
723
  	int portno;
  	u32 val;
  	u32 ifr;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
724
  	int i;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
725

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
726
727
728
729
730
731
732
733
734
735
736
737
738
  	gpio = kzalloc(sizeof(struct u300_gpio), GFP_KERNEL);
  	if (gpio == NULL) {
  		dev_err(&pdev->dev, "failed to allocate memory
  ");
  		return -ENOMEM;
  	}
  
  	gpio->chip = u300_gpio_chip;
  	gpio->chip.ngpio = plat->ports * U300_GPIO_PINS_PER_PORT;
  	gpio->irq_base = plat->gpio_irq_base;
  	gpio->chip.dev = &pdev->dev;
  	gpio->chip.base = plat->gpio_base;
  	gpio->dev = &pdev->dev;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
739
740
  
  	/* Get GPIO clock */
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
741
742
743
744
745
  	gpio->clk = clk_get(gpio->dev, NULL);
  	if (IS_ERR(gpio->clk)) {
  		err = PTR_ERR(gpio->clk);
  		dev_err(gpio->dev, "could not get GPIO clock
  ");
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
746
747
  		goto err_no_clk;
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
748
  	err = clk_enable(gpio->clk);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
749
  	if (err) {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
750
751
  		dev_err(gpio->dev, "could not enable GPIO clock
  ");
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
752
753
  		goto err_no_clk_enable;
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
754
755
756
757
758
  	gpio->memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (!gpio->memres) {
  		dev_err(gpio->dev, "could not get GPIO memory resource
  ");
  		err = -ENODEV;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
759
  		goto err_no_resource;
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
760
  	}
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
761

cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
762
763
  	if (!request_mem_region(gpio->memres->start,
  				resource_size(gpio->memres),
28f65c11f   Joe Perches   treewide: Convert...
764
  				"GPIO Controller")) {
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
765
766
767
  		err = -ENODEV;
  		goto err_no_ioregion;
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
768
769
  	gpio->base = ioremap(gpio->memres->start, resource_size(gpio->memres));
  	if (!gpio->base) {
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
770
771
772
  		err = -ENOMEM;
  		goto err_no_ioremap;
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
  
  	if (plat->variant == U300_GPIO_COH901335) {
  		dev_info(gpio->dev,
  			 "initializing GPIO Controller COH 901 335
  ");
  		gpio->stride = U300_335_PORT_STRIDE;
  		gpio->pcr = U300_335_PXPCR;
  		gpio->dor = U300_335_PXPDOR;
  		gpio->dir = U300_335_PXPDIR;
  		gpio->per = U300_335_PXPER;
  		gpio->icr = U300_335_PXICR;
  		gpio->ien = U300_335_PXIEN;
  		gpio->iev = U300_335_PXIEV;
  		ifr = U300_335_PXIFR;
  
  		/* Turn on the GPIO block */
  		writel(U300_335_CR_BLOCK_CLOCK_ENABLE,
  		       gpio->base + U300_335_CR);
  	} else if (plat->variant == U300_GPIO_COH901571_3_BS335 ||
  		   plat->variant == U300_GPIO_COH901571_3_BS365) {
  		dev_info(gpio->dev,
  			 "initializing GPIO Controller COH 901 571/3
  ");
  		gpio->stride = U300_571_PORT_STRIDE;
  		gpio->pcr = U300_571_PXPCR;
  		gpio->dor = U300_571_PXPDOR;
  		gpio->dir = U300_571_PXPDIR;
  		gpio->per = U300_571_PXPER;
  		gpio->icr = U300_571_PXICR;
  		gpio->ien = U300_571_PXIEN;
  		gpio->iev = U300_571_PXIEV;
  		ifr = U300_571_PXIFR;
  
  		val = readl(gpio->base + U300_571_CR);
  		dev_info(gpio->dev, "COH901571/3 block version: %d, " \
  			 "number of cores: %d totalling %d pins
  ",
  			 ((val & 0x000001FC) >> 2),
  			 ((val & 0x0000FE00) >> 9),
  			 ((val & 0x0000FE00) >> 9) * 8);
  		writel(U300_571_CR_BLOCK_CLKRQ_ENABLE,
  		       gpio->base + U300_571_CR);
  		u300_gpio_init_coh901571(gpio, plat);
  	} else {
  		dev_err(gpio->dev, "unknown block variant
  ");
  		err = -ENODEV;
  		goto err_unknown_variant;
  	}
  
  	/* Add each port with its IRQ separately */
  	INIT_LIST_HEAD(&gpio->port_list);
  	for (portno = 0 ; portno < plat->ports; portno++) {
  		struct u300_gpio_port *port =
  			kmalloc(sizeof(struct u300_gpio_port), GFP_KERNEL);
  
  		if (!port) {
  			dev_err(gpio->dev, "out of memory
  ");
  			err = -ENOMEM;
  			goto err_no_port;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
834
  		}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
  
  		snprintf(port->name, 8, "gpio%d", portno);
  		port->number = portno;
  		port->gpio = gpio;
  
  		port->irq = platform_get_irq_byname(pdev,
  						    port->name);
  
  		dev_dbg(gpio->dev, "register IRQ %d for %s
  ", port->irq,
  			port->name);
  
  		irq_set_chained_handler(port->irq, u300_gpio_irq_handler);
  		irq_set_handler_data(port->irq, port);
  
  		/* For each GPIO pin set the unique IRQ handler */
  		for (i = 0; i < U300_GPIO_PINS_PER_PORT; i++) {
  			int irqno = gpio->irq_base + (portno << 3) + i;
  
  			dev_dbg(gpio->dev, "handler for IRQ %d on %s
  ",
  				irqno, port->name);
  			irq_set_chip_and_handler(irqno, &u300_gpio_irqchip,
  						 handle_simple_irq);
  			set_irq_flags(irqno, IRQF_VALID);
  			irq_set_chip_data(irqno, port);
  		}
  
  		/* Turns off irq force (test register) for this port */
  		writel(0x0, gpio->base + portno * gpio->stride + ifr);
  
  		list_add_tail(&port->node, &gpio->port_list);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
867
  	}
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
868
869
870
871
872
873
874
875
876
877
878
  	dev_dbg(gpio->dev, "initialized %d GPIO ports
  ", portno);
  
  	err = gpiochip_add(&gpio->chip);
  	if (err) {
  		dev_err(gpio->dev, "unable to add gpiochip: %d
  ", err);
  		goto err_no_chip;
  	}
  
  	platform_set_drvdata(pdev, gpio);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
879
880
  
  	return 0;
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
  err_no_chip:
  err_no_port:
  	u300_gpio_free_ports(gpio);
  err_unknown_variant:
  	iounmap(gpio->base);
  err_no_ioremap:
  	release_mem_region(gpio->memres->start, resource_size(gpio->memres));
  err_no_ioregion:
  err_no_resource:
  	clk_disable(gpio->clk);
  err_no_clk_enable:
  	clk_put(gpio->clk);
  err_no_clk:
  	kfree(gpio);
  	dev_info(&pdev->dev, "module ERROR:%d
  ", err);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
897
898
  	return err;
  }
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
899
  static int __exit u300_gpio_remove(struct platform_device *pdev)
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
900
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
901
902
903
  	struct u300_gpio_platform *plat = dev_get_platdata(&pdev->dev);
  	struct u300_gpio *gpio = platform_get_drvdata(pdev);
  	int err;
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
904
905
  
  	/* Turn off the GPIO block */
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
  	if (plat->variant == U300_GPIO_COH901335)
  		writel(0x00000000U, gpio->base + U300_335_CR);
  	if (plat->variant == U300_GPIO_COH901571_3_BS335 ||
  	    plat->variant == U300_GPIO_COH901571_3_BS365)
  		writel(0x00000000U, gpio->base + U300_571_CR);
  
  	err = gpiochip_remove(&gpio->chip);
  	if (err < 0) {
  		dev_err(gpio->dev, "unable to remove gpiochip: %d
  ", err);
  		return err;
  	}
  	u300_gpio_free_ports(gpio);
  	iounmap(gpio->base);
  	release_mem_region(gpio->memres->start,
  			   resource_size(gpio->memres));
  	clk_disable(gpio->clk);
  	clk_put(gpio->clk);
  	platform_set_drvdata(pdev, NULL);
  	kfree(gpio);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
926
927
  	return 0;
  }
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
928
  static struct platform_driver u300_gpio_driver = {
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
929
930
931
  	.driver		= {
  		.name	= "u300-gpio",
  	},
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
932
  	.remove		= __exit_p(u300_gpio_remove),
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
933
934
935
936
937
  };
  
  
  static int __init u300_gpio_init(void)
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
938
  	return platform_driver_probe(&u300_gpio_driver, u300_gpio_probe);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
939
940
941
942
  }
  
  static void __exit u300_gpio_exit(void)
  {
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
943
  	platform_driver_unregister(&u300_gpio_driver);
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
944
945
946
947
948
949
  }
  
  arch_initcall(u300_gpio_init);
  module_exit(u300_gpio_exit);
  
  MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
cc890cd78   Linus Walleij   ARM: 7083/1: rewr...
950
  MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335/COH 901 571/3 GPIO driver");
bd41b99d4   Linus Walleij   [ARM] 5471/2: U30...
951
  MODULE_LICENSE("GPL");