blob: c6408a62d95e3ab791bb3a303ba717ec1563b591 [file] [log] [blame]
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -08001/*
2 * driver/mfd/asic3.c
3 *
4 * Compaq ASIC3 support.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Copyright 2001 Compaq Computer Corporation.
11 * Copyright 2004-2005 Phil Blundell
Samuel Ortiz6f2384c2008-06-20 11:02:19 +020012 * Copyright 2007-2008 OpenedHand Ltd.
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080013 *
14 * Authors: Phil Blundell <pb@handhelds.org>,
15 * Samuel Ortiz <sameo@openedhand.com>
16 *
17 */
18
19#include <linux/version.h>
20#include <linux/kernel.h>
21#include <linux/irq.h>
Samuel Ortiz6f2384c2008-06-20 11:02:19 +020022#include <linux/gpio.h>
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080023#include <linux/io.h>
24#include <linux/spinlock.h>
25#include <linux/platform_device.h>
26
27#include <linux/mfd/asic3.h>
28
Samuel Ortiz6f2384c2008-06-20 11:02:19 +020029struct asic3 {
30 void __iomem *mapping;
31 unsigned int bus_shift;
32 unsigned int irq_nr;
33 unsigned int irq_base;
34 spinlock_t lock;
35 u16 irq_bothedge[4];
36 struct gpio_chip gpio;
37 struct device *dev;
38};
39
40static int asic3_gpio_get(struct gpio_chip *chip, unsigned offset);
41
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080042static inline void asic3_write_register(struct asic3 *asic,
43 unsigned int reg, u32 value)
44{
Al Virob32661e2008-03-29 03:10:58 +000045 iowrite16(value, asic->mapping +
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080046 (reg >> asic->bus_shift));
47}
48
49static inline u32 asic3_read_register(struct asic3 *asic,
50 unsigned int reg)
51{
Al Virob32661e2008-03-29 03:10:58 +000052 return ioread16(asic->mapping +
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080053 (reg >> asic->bus_shift));
54}
55
56/* IRQs */
57#define MAX_ASIC_ISR_LOOPS 20
Samuel Ortiz3b8139f2008-06-20 11:12:21 +020058#define ASIC3_GPIO_BASE_INCR \
59 (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE)
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080060
61static void asic3_irq_flip_edge(struct asic3 *asic,
62 u32 base, int bit)
63{
64 u16 edge;
65 unsigned long flags;
66
67 spin_lock_irqsave(&asic->lock, flags);
68 edge = asic3_read_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +020069 base + ASIC3_GPIO_EDGE_TRIGGER);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080070 edge ^= bit;
71 asic3_write_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +020072 base + ASIC3_GPIO_EDGE_TRIGGER, edge);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080073 spin_unlock_irqrestore(&asic->lock, flags);
74}
75
76static void asic3_irq_demux(unsigned int irq, struct irq_desc *desc)
77{
78 int iter, i;
79 unsigned long flags;
80 struct asic3 *asic;
81
82 desc->chip->ack(irq);
83
84 asic = desc->handler_data;
85
86 for (iter = 0 ; iter < MAX_ASIC_ISR_LOOPS; iter++) {
87 u32 status;
88 int bank;
89
90 spin_lock_irqsave(&asic->lock, flags);
91 status = asic3_read_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +020092 ASIC3_OFFSET(INTR, P_INT_STAT));
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -080093 spin_unlock_irqrestore(&asic->lock, flags);
94
95 /* Check all ten register bits */
96 if ((status & 0x3ff) == 0)
97 break;
98
99 /* Handle GPIO IRQs */
100 for (bank = 0; bank < ASIC3_NUM_GPIO_BANKS; bank++) {
101 if (status & (1 << bank)) {
102 unsigned long base, istat;
103
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200104 base = ASIC3_GPIO_A_BASE
105 + bank * ASIC3_GPIO_BASE_INCR;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800106
107 spin_lock_irqsave(&asic->lock, flags);
108 istat = asic3_read_register(asic,
109 base +
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200110 ASIC3_GPIO_INT_STATUS);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800111 /* Clearing IntStatus */
112 asic3_write_register(asic,
113 base +
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200114 ASIC3_GPIO_INT_STATUS, 0);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800115 spin_unlock_irqrestore(&asic->lock, flags);
116
117 for (i = 0; i < ASIC3_GPIOS_PER_BANK; i++) {
118 int bit = (1 << i);
119 unsigned int irqnr;
120
121 if (!(istat & bit))
122 continue;
123
124 irqnr = asic->irq_base +
125 (ASIC3_GPIOS_PER_BANK * bank)
126 + i;
127 desc = irq_desc + irqnr;
128 desc->handle_irq(irqnr, desc);
129 if (asic->irq_bothedge[bank] & bit)
130 asic3_irq_flip_edge(asic, base,
131 bit);
132 }
133 }
134 }
135
136 /* Handle remaining IRQs in the status register */
137 for (i = ASIC3_NUM_GPIOS; i < ASIC3_NR_IRQS; i++) {
138 /* They start at bit 4 and go up */
139 if (status & (1 << (i - ASIC3_NUM_GPIOS + 4))) {
Philipp Zabelde0d23c2008-07-03 11:08:27 +0200140 desc = irq_desc + asic->irq_base + i;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800141 desc->handle_irq(asic->irq_base + i,
142 desc);
143 }
144 }
145 }
146
147 if (iter >= MAX_ASIC_ISR_LOOPS)
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200148 dev_err(asic->dev, "interrupt processing overrun\n");
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800149}
150
151static inline int asic3_irq_to_bank(struct asic3 *asic, int irq)
152{
153 int n;
154
155 n = (irq - asic->irq_base) >> 4;
156
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200157 return (n * (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE));
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800158}
159
160static inline int asic3_irq_to_index(struct asic3 *asic, int irq)
161{
162 return (irq - asic->irq_base) & 0xf;
163}
164
165static void asic3_mask_gpio_irq(unsigned int irq)
166{
167 struct asic3 *asic = get_irq_chip_data(irq);
168 u32 val, bank, index;
169 unsigned long flags;
170
171 bank = asic3_irq_to_bank(asic, irq);
172 index = asic3_irq_to_index(asic, irq);
173
174 spin_lock_irqsave(&asic->lock, flags);
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200175 val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800176 val |= 1 << index;
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200177 asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800178 spin_unlock_irqrestore(&asic->lock, flags);
179}
180
181static void asic3_mask_irq(unsigned int irq)
182{
183 struct asic3 *asic = get_irq_chip_data(irq);
184 int regval;
185 unsigned long flags;
186
187 spin_lock_irqsave(&asic->lock, flags);
188 regval = asic3_read_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200189 ASIC3_INTR_BASE +
190 ASIC3_INTR_INT_MASK);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800191
192 regval &= ~(ASIC3_INTMASK_MASK0 <<
193 (irq - (asic->irq_base + ASIC3_NUM_GPIOS)));
194
195 asic3_write_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200196 ASIC3_INTR_BASE +
197 ASIC3_INTR_INT_MASK,
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800198 regval);
199 spin_unlock_irqrestore(&asic->lock, flags);
200}
201
202static void asic3_unmask_gpio_irq(unsigned int irq)
203{
204 struct asic3 *asic = get_irq_chip_data(irq);
205 u32 val, bank, index;
206 unsigned long flags;
207
208 bank = asic3_irq_to_bank(asic, irq);
209 index = asic3_irq_to_index(asic, irq);
210
211 spin_lock_irqsave(&asic->lock, flags);
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200212 val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800213 val &= ~(1 << index);
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200214 asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800215 spin_unlock_irqrestore(&asic->lock, flags);
216}
217
218static void asic3_unmask_irq(unsigned int irq)
219{
220 struct asic3 *asic = get_irq_chip_data(irq);
221 int regval;
222 unsigned long flags;
223
224 spin_lock_irqsave(&asic->lock, flags);
225 regval = asic3_read_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200226 ASIC3_INTR_BASE +
227 ASIC3_INTR_INT_MASK);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800228
229 regval |= (ASIC3_INTMASK_MASK0 <<
230 (irq - (asic->irq_base + ASIC3_NUM_GPIOS)));
231
232 asic3_write_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200233 ASIC3_INTR_BASE +
234 ASIC3_INTR_INT_MASK,
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800235 regval);
236 spin_unlock_irqrestore(&asic->lock, flags);
237}
238
239static int asic3_gpio_irq_type(unsigned int irq, unsigned int type)
240{
241 struct asic3 *asic = get_irq_chip_data(irq);
242 u32 bank, index;
243 u16 trigger, level, edge, bit;
244 unsigned long flags;
245
246 bank = asic3_irq_to_bank(asic, irq);
247 index = asic3_irq_to_index(asic, irq);
248 bit = 1<<index;
249
250 spin_lock_irqsave(&asic->lock, flags);
251 level = asic3_read_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200252 bank + ASIC3_GPIO_LEVEL_TRIGGER);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800253 edge = asic3_read_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200254 bank + ASIC3_GPIO_EDGE_TRIGGER);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800255 trigger = asic3_read_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200256 bank + ASIC3_GPIO_TRIGGER_TYPE);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800257 asic->irq_bothedge[(irq - asic->irq_base) >> 4] &= ~bit;
258
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100259 if (type == IRQ_TYPE_EDGE_RISING) {
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800260 trigger |= bit;
261 edge |= bit;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100262 } else if (type == IRQ_TYPE_EDGE_FALLING) {
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800263 trigger |= bit;
264 edge &= ~bit;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100265 } else if (type == IRQ_TYPE_EDGE_BOTH) {
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800266 trigger |= bit;
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200267 if (asic3_gpio_get(&asic->gpio, irq - asic->irq_base))
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800268 edge &= ~bit;
269 else
270 edge |= bit;
271 asic->irq_bothedge[(irq - asic->irq_base) >> 4] |= bit;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100272 } else if (type == IRQ_TYPE_LEVEL_LOW) {
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800273 trigger &= ~bit;
274 level &= ~bit;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100275 } else if (type == IRQ_TYPE_LEVEL_HIGH) {
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800276 trigger &= ~bit;
277 level |= bit;
278 } else {
279 /*
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100280 * if type == IRQ_TYPE_NONE, we should mask interrupts, but
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800281 * be careful to not unmask them if mask was also called.
282 * Probably need internal state for mask.
283 */
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200284 dev_notice(asic->dev, "irq type not changed\n");
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800285 }
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200286 asic3_write_register(asic, bank + ASIC3_GPIO_LEVEL_TRIGGER,
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800287 level);
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200288 asic3_write_register(asic, bank + ASIC3_GPIO_EDGE_TRIGGER,
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800289 edge);
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200290 asic3_write_register(asic, bank + ASIC3_GPIO_TRIGGER_TYPE,
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800291 trigger);
292 spin_unlock_irqrestore(&asic->lock, flags);
293 return 0;
294}
295
296static struct irq_chip asic3_gpio_irq_chip = {
297 .name = "ASIC3-GPIO",
298 .ack = asic3_mask_gpio_irq,
299 .mask = asic3_mask_gpio_irq,
300 .unmask = asic3_unmask_gpio_irq,
301 .set_type = asic3_gpio_irq_type,
302};
303
304static struct irq_chip asic3_irq_chip = {
305 .name = "ASIC3",
306 .ack = asic3_mask_irq,
307 .mask = asic3_mask_irq,
308 .unmask = asic3_unmask_irq,
309};
310
Philipp Zabel065032f2008-06-21 00:51:38 +0200311static int __init asic3_irq_probe(struct platform_device *pdev)
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800312{
313 struct asic3 *asic = platform_get_drvdata(pdev);
314 unsigned long clksel = 0;
315 unsigned int irq, irq_base;
Philipp Zabel99cdb0c2008-07-10 02:17:02 +0200316 int map_size;
Roel Kluinc491b2f2008-07-25 19:44:41 -0700317 int ret;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800318
Roel Kluinc491b2f2008-07-25 19:44:41 -0700319 ret = platform_get_irq(pdev, 0);
320 if (ret < 0)
321 return ret;
322 asic->irq_nr = ret;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800323
324 /* turn on clock to IRQ controller */
325 clksel |= CLOCK_SEL_CX;
326 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL),
327 clksel);
328
329 irq_base = asic->irq_base;
330
331 for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
332 if (irq < asic->irq_base + ASIC3_NUM_GPIOS)
333 set_irq_chip(irq, &asic3_gpio_irq_chip);
334 else
335 set_irq_chip(irq, &asic3_irq_chip);
336
337 set_irq_chip_data(irq, asic);
338 set_irq_handler(irq, handle_level_irq);
339 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
340 }
341
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200342 asic3_write_register(asic, ASIC3_OFFSET(INTR, INT_MASK),
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800343 ASIC3_INTMASK_GINTMASK);
344
345 set_irq_chained_handler(asic->irq_nr, asic3_irq_demux);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100346 set_irq_type(asic->irq_nr, IRQ_TYPE_EDGE_RISING);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800347 set_irq_data(asic->irq_nr, asic);
348
349 return 0;
350}
351
352static void asic3_irq_remove(struct platform_device *pdev)
353{
354 struct asic3 *asic = platform_get_drvdata(pdev);
355 unsigned int irq, irq_base;
356
357 irq_base = asic->irq_base;
358
359 for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
360 set_irq_flags(irq, 0);
361 set_irq_handler(irq, NULL);
362 set_irq_chip(irq, NULL);
363 set_irq_chip_data(irq, NULL);
364 }
365 set_irq_chained_handler(asic->irq_nr, NULL);
366}
367
368/* GPIOs */
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200369static int asic3_gpio_direction(struct gpio_chip *chip,
370 unsigned offset, int out)
371{
372 u32 mask = ASIC3_GPIO_TO_MASK(offset), out_reg;
373 unsigned int gpio_base;
374 unsigned long flags;
375 struct asic3 *asic;
376
377 asic = container_of(chip, struct asic3, gpio);
378 gpio_base = ASIC3_GPIO_TO_BASE(offset);
379
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200380 if (gpio_base > ASIC3_GPIO_D_BASE) {
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200381 dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
382 gpio_base, offset);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200383 return -EINVAL;
384 }
385
386 spin_lock_irqsave(&asic->lock, flags);
387
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200388 out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_DIRECTION);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200389
390 /* Input is 0, Output is 1 */
391 if (out)
392 out_reg |= mask;
393 else
394 out_reg &= ~mask;
395
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200396 asic3_write_register(asic, gpio_base + ASIC3_GPIO_DIRECTION, out_reg);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200397
398 spin_unlock_irqrestore(&asic->lock, flags);
399
400 return 0;
401
402}
403
404static int asic3_gpio_direction_input(struct gpio_chip *chip,
405 unsigned offset)
406{
407 return asic3_gpio_direction(chip, offset, 0);
408}
409
410static int asic3_gpio_direction_output(struct gpio_chip *chip,
411 unsigned offset, int value)
412{
413 return asic3_gpio_direction(chip, offset, 1);
414}
415
416static int asic3_gpio_get(struct gpio_chip *chip,
417 unsigned offset)
418{
419 unsigned int gpio_base;
420 u32 mask = ASIC3_GPIO_TO_MASK(offset);
421 struct asic3 *asic;
422
423 asic = container_of(chip, struct asic3, gpio);
424 gpio_base = ASIC3_GPIO_TO_BASE(offset);
425
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200426 if (gpio_base > ASIC3_GPIO_D_BASE) {
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200427 dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
428 gpio_base, offset);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200429 return -EINVAL;
430 }
431
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200432 return asic3_read_register(asic, gpio_base + ASIC3_GPIO_STATUS) & mask;
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200433}
434
435static void asic3_gpio_set(struct gpio_chip *chip,
436 unsigned offset, int value)
437{
438 u32 mask, out_reg;
439 unsigned int gpio_base;
440 unsigned long flags;
441 struct asic3 *asic;
442
443 asic = container_of(chip, struct asic3, gpio);
444 gpio_base = ASIC3_GPIO_TO_BASE(offset);
445
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200446 if (gpio_base > ASIC3_GPIO_D_BASE) {
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200447 dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
448 gpio_base, offset);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200449 return;
450 }
451
452 mask = ASIC3_GPIO_TO_MASK(offset);
453
454 spin_lock_irqsave(&asic->lock, flags);
455
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200456 out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_OUT);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200457
458 if (value)
459 out_reg |= mask;
460 else
461 out_reg &= ~mask;
462
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200463 asic3_write_register(asic, gpio_base + ASIC3_GPIO_OUT, out_reg);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200464
465 spin_unlock_irqrestore(&asic->lock, flags);
466
467 return;
468}
469
Philipp Zabel065032f2008-06-21 00:51:38 +0200470static __init int asic3_gpio_probe(struct platform_device *pdev,
471 u16 *gpio_config, int num)
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800472{
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800473 struct asic3 *asic = platform_get_drvdata(pdev);
Samuel Ortiz3b26bf12008-06-20 11:09:51 +0200474 u16 alt_reg[ASIC3_NUM_GPIO_BANKS];
475 u16 out_reg[ASIC3_NUM_GPIO_BANKS];
476 u16 dir_reg[ASIC3_NUM_GPIO_BANKS];
477 int i;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800478
Philipp Zabel786bef32008-06-24 01:31:24 +0200479 memzero(alt_reg, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
480 memzero(out_reg, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
481 memzero(dir_reg, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
Samuel Ortiz3b26bf12008-06-20 11:09:51 +0200482
483 /* Enable all GPIOs */
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200484 asic3_write_register(asic, ASIC3_GPIO_OFFSET(A, MASK), 0xffff);
485 asic3_write_register(asic, ASIC3_GPIO_OFFSET(B, MASK), 0xffff);
486 asic3_write_register(asic, ASIC3_GPIO_OFFSET(C, MASK), 0xffff);
487 asic3_write_register(asic, ASIC3_GPIO_OFFSET(D, MASK), 0xffff);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800488
Samuel Ortiz3b26bf12008-06-20 11:09:51 +0200489 for (i = 0; i < num; i++) {
490 u8 alt, pin, dir, init, bank_num, bit_num;
491 u16 config = gpio_config[i];
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800492
Samuel Ortiz3b26bf12008-06-20 11:09:51 +0200493 pin = ASIC3_CONFIG_GPIO_PIN(config);
494 alt = ASIC3_CONFIG_GPIO_ALT(config);
495 dir = ASIC3_CONFIG_GPIO_DIR(config);
496 init = ASIC3_CONFIG_GPIO_INIT(config);
497
498 bank_num = ASIC3_GPIO_TO_BANK(pin);
499 bit_num = ASIC3_GPIO_TO_BIT(pin);
500
501 alt_reg[bank_num] |= (alt << bit_num);
502 out_reg[bank_num] |= (init << bit_num);
503 dir_reg[bank_num] |= (dir << bit_num);
504 }
505
506 for (i = 0; i < ASIC3_NUM_GPIO_BANKS; i++) {
507 asic3_write_register(asic,
508 ASIC3_BANK_TO_BASE(i) +
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200509 ASIC3_GPIO_DIRECTION,
Samuel Ortiz3b26bf12008-06-20 11:09:51 +0200510 dir_reg[i]);
511 asic3_write_register(asic,
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200512 ASIC3_BANK_TO_BASE(i) + ASIC3_GPIO_OUT,
Samuel Ortiz3b26bf12008-06-20 11:09:51 +0200513 out_reg[i]);
514 asic3_write_register(asic,
515 ASIC3_BANK_TO_BASE(i) +
Samuel Ortiz3b8139f2008-06-20 11:12:21 +0200516 ASIC3_GPIO_ALT_FUNCTION,
Samuel Ortiz3b26bf12008-06-20 11:09:51 +0200517 alt_reg[i]);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800518 }
519
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200520 return gpiochip_add(&asic->gpio);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800521}
522
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200523static int asic3_gpio_remove(struct platform_device *pdev)
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800524{
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200525 struct asic3 *asic = platform_get_drvdata(pdev);
526
527 return gpiochip_remove(&asic->gpio);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800528}
529
530
531/* Core */
Philipp Zabel065032f2008-06-21 00:51:38 +0200532static int __init asic3_probe(struct platform_device *pdev)
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800533{
534 struct asic3_platform_data *pdata = pdev->dev.platform_data;
535 struct asic3 *asic;
536 struct resource *mem;
537 unsigned long clksel;
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200538 int ret = 0;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800539
540 asic = kzalloc(sizeof(struct asic3), GFP_KERNEL);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200541 if (asic == NULL) {
542 printk(KERN_ERR "kzalloc failed\n");
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800543 return -ENOMEM;
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200544 }
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800545
546 spin_lock_init(&asic->lock);
547 platform_set_drvdata(pdev, asic);
548 asic->dev = &pdev->dev;
549
550 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
551 if (!mem) {
552 ret = -ENOMEM;
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200553 dev_err(asic->dev, "no MEM resource\n");
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200554 goto out_free;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800555 }
556
Philipp Zabel99cdb0c2008-07-10 02:17:02 +0200557 map_size = mem->end - mem->start + 1;
558 asic->mapping = ioremap(mem->start, map_size);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800559 if (!asic->mapping) {
560 ret = -ENOMEM;
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200561 dev_err(asic->dev, "Couldn't ioremap\n");
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200562 goto out_free;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800563 }
564
565 asic->irq_base = pdata->irq_base;
566
Philipp Zabel99cdb0c2008-07-10 02:17:02 +0200567 /* calculate bus shift from mem resource */
568 asic->bus_shift = 2 - (map_size >> 12);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800569
570 clksel = 0;
571 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), clksel);
572
573 ret = asic3_irq_probe(pdev);
574 if (ret < 0) {
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200575 dev_err(asic->dev, "Couldn't probe IRQs\n");
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200576 goto out_unmap;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800577 }
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200578
579 asic->gpio.base = pdata->gpio_base;
580 asic->gpio.ngpio = ASIC3_NUM_GPIOS;
581 asic->gpio.get = asic3_gpio_get;
582 asic->gpio.set = asic3_gpio_set;
583 asic->gpio.direction_input = asic3_gpio_direction_input;
584 asic->gpio.direction_output = asic3_gpio_direction_output;
585
Samuel Ortiz3b26bf12008-06-20 11:09:51 +0200586 ret = asic3_gpio_probe(pdev,
587 pdata->gpio_config,
588 pdata->gpio_config_num);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200589 if (ret < 0) {
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200590 dev_err(asic->dev, "GPIO probe failed\n");
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200591 goto out_irq;
592 }
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800593
Samuel Ortiz24f4f2e2008-06-20 11:11:19 +0200594 dev_info(asic->dev, "ASIC3 Core driver\n");
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800595
596 return 0;
597
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200598 out_irq:
599 asic3_irq_remove(pdev);
600
601 out_unmap:
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800602 iounmap(asic->mapping);
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200603
604 out_free:
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800605 kfree(asic);
606
607 return ret;
608}
609
610static int asic3_remove(struct platform_device *pdev)
611{
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200612 int ret;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800613 struct asic3 *asic = platform_get_drvdata(pdev);
614
Samuel Ortiz6f2384c2008-06-20 11:02:19 +0200615 ret = asic3_gpio_remove(pdev);
616 if (ret < 0)
617 return ret;
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800618 asic3_irq_remove(pdev);
619
620 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), 0);
621
622 iounmap(asic->mapping);
623
624 kfree(asic);
625
626 return 0;
627}
628
629static void asic3_shutdown(struct platform_device *pdev)
630{
631}
632
633static struct platform_driver asic3_device_driver = {
634 .driver = {
635 .name = "asic3",
636 },
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800637 .remove = __devexit_p(asic3_remove),
638 .shutdown = asic3_shutdown,
639};
640
641static int __init asic3_init(void)
642{
643 int retval = 0;
Philipp Zabel065032f2008-06-21 00:51:38 +0200644 retval = platform_driver_probe(&asic3_device_driver, asic3_probe);
Samuel Ortizfa9ff4b2008-02-07 00:14:49 -0800645 return retval;
646}
647
648subsys_initcall(asic3_init);