blob: 252b74188ec20a7fbf55ee95410c9edfc41dc5ec [file] [log] [blame]
Russell King05c45ca2005-09-11 10:26:31 +01001/*
2 * linux/drivers/mfd/ucb1x00-core.c
3 *
4 * Copyright (C) 2001 Russell King, All Rights Reserved.
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 as published by
8 * the Free Software Foundation; either version 2 of the License.
9 *
10 * The UCB1x00 core driver provides basic services for handling IO,
11 * the ADC, interrupts, and accessing registers. It is designed
12 * such that everything goes through this layer, thereby providing
13 * a consistent locking methodology, as well as allowing the drivers
14 * to be used on other non-MCP-enabled hardware platforms.
15 *
16 * Note that all locks are private to this file. Nothing else may
17 * touch them.
18 */
Russell King05c45ca2005-09-11 10:26:31 +010019#include <linux/module.h>
20#include <linux/kernel.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040021#include <linux/sched.h>
Russell King05c45ca2005-09-11 10:26:31 +010022#include <linux/slab.h>
23#include <linux/init.h>
24#include <linux/errno.h>
25#include <linux/interrupt.h>
26#include <linux/device.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000027#include <linux/mutex.h>
Thomas Kunzec8602ed2009-02-10 14:54:57 +010028#include <linux/mfd/ucb1x00.h>
Thomas Kunze9ca3dc82009-02-10 14:50:56 +010029#include <linux/gpio.h>
Russell King05c45ca2005-09-11 10:26:31 +010030
Russell Kingdcea83a2008-11-29 11:40:28 +000031#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010032#include <mach/hardware.h>
Russell King05c45ca2005-09-11 10:26:31 +010033
Arjan van de Vena621aae2006-01-12 18:43:35 +000034static DEFINE_MUTEX(ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +010035static LIST_HEAD(ucb1x00_drivers);
36static LIST_HEAD(ucb1x00_devices);
37
38/**
39 * ucb1x00_io_set_dir - set IO direction
40 * @ucb: UCB1x00 structure describing chip
41 * @in: bitfield of IO pins to be set as inputs
42 * @out: bitfield of IO pins to be set as outputs
43 *
44 * Set the IO direction of the ten general purpose IO pins on
45 * the UCB1x00 chip. The @in bitfield has priority over the
46 * @out bitfield, in that if you specify a pin as both input
47 * and output, it will end up as an input.
48 *
49 * ucb1x00_enable must have been called to enable the comms
50 * before using this function.
51 *
52 * This function takes a spinlock, disabling interrupts.
53 */
54void ucb1x00_io_set_dir(struct ucb1x00 *ucb, unsigned int in, unsigned int out)
55{
56 unsigned long flags;
57
58 spin_lock_irqsave(&ucb->io_lock, flags);
59 ucb->io_dir |= out;
60 ucb->io_dir &= ~in;
61
62 ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
63 spin_unlock_irqrestore(&ucb->io_lock, flags);
64}
65
66/**
67 * ucb1x00_io_write - set or clear IO outputs
68 * @ucb: UCB1x00 structure describing chip
69 * @set: bitfield of IO pins to set to logic '1'
70 * @clear: bitfield of IO pins to set to logic '0'
71 *
72 * Set the IO output state of the specified IO pins. The value
73 * is retained if the pins are subsequently configured as inputs.
74 * The @clear bitfield has priority over the @set bitfield -
75 * outputs will be cleared.
76 *
77 * ucb1x00_enable must have been called to enable the comms
78 * before using this function.
79 *
80 * This function takes a spinlock, disabling interrupts.
81 */
82void ucb1x00_io_write(struct ucb1x00 *ucb, unsigned int set, unsigned int clear)
83{
84 unsigned long flags;
85
86 spin_lock_irqsave(&ucb->io_lock, flags);
87 ucb->io_out |= set;
88 ucb->io_out &= ~clear;
89
90 ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
91 spin_unlock_irqrestore(&ucb->io_lock, flags);
92}
93
94/**
95 * ucb1x00_io_read - read the current state of the IO pins
96 * @ucb: UCB1x00 structure describing chip
97 *
98 * Return a bitfield describing the logic state of the ten
99 * general purpose IO pins.
100 *
101 * ucb1x00_enable must have been called to enable the comms
102 * before using this function.
103 *
104 * This function does not take any semaphores or spinlocks.
105 */
106unsigned int ucb1x00_io_read(struct ucb1x00 *ucb)
107{
108 return ucb1x00_reg_read(ucb, UCB_IO_DATA);
109}
110
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100111static void ucb1x00_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
112{
113 struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
114 unsigned long flags;
115
116 spin_lock_irqsave(&ucb->io_lock, flags);
117 if (value)
118 ucb->io_out |= 1 << offset;
119 else
120 ucb->io_out &= ~(1 << offset);
121
122 ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
123 spin_unlock_irqrestore(&ucb->io_lock, flags);
124}
125
126static int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset)
127{
128 struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
129 return ucb1x00_reg_read(ucb, UCB_IO_DATA) & (1 << offset);
130}
131
132static int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
133{
134 struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
135 unsigned long flags;
136
137 spin_lock_irqsave(&ucb->io_lock, flags);
138 ucb->io_dir &= ~(1 << offset);
139 ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
140 spin_unlock_irqrestore(&ucb->io_lock, flags);
141
142 return 0;
143}
144
145static int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset
146 , int value)
147{
148 struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
149 unsigned long flags;
150
151 spin_lock_irqsave(&ucb->io_lock, flags);
152 ucb->io_dir |= (1 << offset);
153 ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
154
155 if (value)
156 ucb->io_out |= 1 << offset;
157 else
158 ucb->io_out &= ~(1 << offset);
159 ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
160 spin_unlock_irqrestore(&ucb->io_lock, flags);
161
162 return 0;
163}
164
Russell King05c45ca2005-09-11 10:26:31 +0100165/*
166 * UCB1300 data sheet says we must:
167 * 1. enable ADC => 5us (including reference startup time)
168 * 2. select input => 51*tsibclk => 4.3us
169 * 3. start conversion => 102*tsibclk => 8.5us
170 * (tsibclk = 1/11981000)
171 * Period between SIB 128-bit frames = 10.7us
172 */
173
174/**
175 * ucb1x00_adc_enable - enable the ADC converter
176 * @ucb: UCB1x00 structure describing chip
177 *
178 * Enable the ucb1x00 and ADC converter on the UCB1x00 for use.
179 * Any code wishing to use the ADC converter must call this
180 * function prior to using it.
181 *
182 * This function takes the ADC semaphore to prevent two or more
183 * concurrent uses, and therefore may sleep. As a result, it
184 * can only be called from process context, not interrupt
185 * context.
186 *
187 * You should release the ADC as soon as possible using
188 * ucb1x00_adc_disable.
189 */
190void ucb1x00_adc_enable(struct ucb1x00 *ucb)
191{
192 down(&ucb->adc_sem);
193
194 ucb->adc_cr |= UCB_ADC_ENA;
195
196 ucb1x00_enable(ucb);
197 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
198}
199
200/**
201 * ucb1x00_adc_read - read the specified ADC channel
202 * @ucb: UCB1x00 structure describing chip
203 * @adc_channel: ADC channel mask
204 * @sync: wait for syncronisation pulse.
205 *
206 * Start an ADC conversion and wait for the result. Note that
207 * synchronised ADC conversions (via the ADCSYNC pin) must wait
208 * until the trigger is asserted and the conversion is finished.
209 *
210 * This function currently spins waiting for the conversion to
211 * complete (2 frames max without sync).
212 *
213 * If called for a synchronised ADC conversion, it may sleep
214 * with the ADC semaphore held.
215 */
216unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync)
217{
218 unsigned int val;
219
220 if (sync)
221 adc_channel |= UCB_ADC_SYNC_ENA;
222
223 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel);
224 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START);
225
226 for (;;) {
227 val = ucb1x00_reg_read(ucb, UCB_ADC_DATA);
228 if (val & UCB_ADC_DAT_VAL)
229 break;
230 /* yield to other processes */
231 set_current_state(TASK_INTERRUPTIBLE);
232 schedule_timeout(1);
233 }
234
235 return UCB_ADC_DAT(val);
236}
237
238/**
239 * ucb1x00_adc_disable - disable the ADC converter
240 * @ucb: UCB1x00 structure describing chip
241 *
242 * Disable the ADC converter and release the ADC semaphore.
243 */
244void ucb1x00_adc_disable(struct ucb1x00 *ucb)
245{
246 ucb->adc_cr &= ~UCB_ADC_ENA;
247 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
248 ucb1x00_disable(ucb);
249
250 up(&ucb->adc_sem);
251}
252
253/*
254 * UCB1x00 Interrupt handling.
255 *
256 * The UCB1x00 can generate interrupts when the SIBCLK is stopped.
257 * Since we need to read an internal register, we must re-enable
258 * SIBCLK to talk to the chip. We leave the clock running until
259 * we have finished processing all interrupts from the chip.
260 */
David Howells7d12e782006-10-05 14:55:46 +0100261static irqreturn_t ucb1x00_irq(int irqnr, void *devid)
Russell King05c45ca2005-09-11 10:26:31 +0100262{
263 struct ucb1x00 *ucb = devid;
264 struct ucb1x00_irq *irq;
265 unsigned int isr, i;
266
267 ucb1x00_enable(ucb);
268 isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS);
269 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr);
270 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
271
272 for (i = 0, irq = ucb->irq_handler; i < 16 && isr; i++, isr >>= 1, irq++)
273 if (isr & 1 && irq->fn)
274 irq->fn(i, irq->devid);
275 ucb1x00_disable(ucb);
276
277 return IRQ_HANDLED;
278}
279
280/**
281 * ucb1x00_hook_irq - hook a UCB1x00 interrupt
282 * @ucb: UCB1x00 structure describing chip
283 * @idx: interrupt index
284 * @fn: function to call when interrupt is triggered
285 * @devid: device id to pass to interrupt handler
286 *
287 * Hook the specified interrupt. You can only register one handler
288 * for each interrupt source. The interrupt source is not enabled
289 * by this function; use ucb1x00_enable_irq instead.
290 *
291 * Interrupt handlers will be called with other interrupts enabled.
292 *
293 * Returns zero on success, or one of the following errors:
294 * -EINVAL if the interrupt index is invalid
295 * -EBUSY if the interrupt has already been hooked
296 */
297int ucb1x00_hook_irq(struct ucb1x00 *ucb, unsigned int idx, void (*fn)(int, void *), void *devid)
298{
299 struct ucb1x00_irq *irq;
300 int ret = -EINVAL;
301
302 if (idx < 16) {
303 irq = ucb->irq_handler + idx;
304 ret = -EBUSY;
305
306 spin_lock_irq(&ucb->lock);
307 if (irq->fn == NULL) {
308 irq->devid = devid;
309 irq->fn = fn;
310 ret = 0;
311 }
312 spin_unlock_irq(&ucb->lock);
313 }
314 return ret;
315}
316
317/**
318 * ucb1x00_enable_irq - enable an UCB1x00 interrupt source
319 * @ucb: UCB1x00 structure describing chip
320 * @idx: interrupt index
321 * @edges: interrupt edges to enable
322 *
323 * Enable the specified interrupt to trigger on %UCB_RISING,
324 * %UCB_FALLING or both edges. The interrupt should have been
325 * hooked by ucb1x00_hook_irq.
326 */
327void ucb1x00_enable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
328{
329 unsigned long flags;
330
331 if (idx < 16) {
332 spin_lock_irqsave(&ucb->lock, flags);
333
334 ucb1x00_enable(ucb);
335 if (edges & UCB_RISING) {
336 ucb->irq_ris_enbl |= 1 << idx;
337 ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
338 }
339 if (edges & UCB_FALLING) {
340 ucb->irq_fal_enbl |= 1 << idx;
341 ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
342 }
343 ucb1x00_disable(ucb);
344 spin_unlock_irqrestore(&ucb->lock, flags);
345 }
346}
347
348/**
349 * ucb1x00_disable_irq - disable an UCB1x00 interrupt source
350 * @ucb: UCB1x00 structure describing chip
351 * @edges: interrupt edges to disable
352 *
353 * Disable the specified interrupt triggering on the specified
354 * (%UCB_RISING, %UCB_FALLING or both) edges.
355 */
356void ucb1x00_disable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
357{
358 unsigned long flags;
359
360 if (idx < 16) {
361 spin_lock_irqsave(&ucb->lock, flags);
362
363 ucb1x00_enable(ucb);
364 if (edges & UCB_RISING) {
365 ucb->irq_ris_enbl &= ~(1 << idx);
366 ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
367 }
368 if (edges & UCB_FALLING) {
369 ucb->irq_fal_enbl &= ~(1 << idx);
370 ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
371 }
372 ucb1x00_disable(ucb);
373 spin_unlock_irqrestore(&ucb->lock, flags);
374 }
375}
376
377/**
378 * ucb1x00_free_irq - disable and free the specified UCB1x00 interrupt
379 * @ucb: UCB1x00 structure describing chip
380 * @idx: interrupt index
381 * @devid: device id.
382 *
383 * Disable the interrupt source and remove the handler. devid must
384 * match the devid passed when hooking the interrupt.
385 *
386 * Returns zero on success, or one of the following errors:
387 * -EINVAL if the interrupt index is invalid
388 * -ENOENT if devid does not match
389 */
390int ucb1x00_free_irq(struct ucb1x00 *ucb, unsigned int idx, void *devid)
391{
392 struct ucb1x00_irq *irq;
393 int ret;
394
395 if (idx >= 16)
396 goto bad;
397
398 irq = ucb->irq_handler + idx;
399 ret = -ENOENT;
400
401 spin_lock_irq(&ucb->lock);
402 if (irq->devid == devid) {
403 ucb->irq_ris_enbl &= ~(1 << idx);
404 ucb->irq_fal_enbl &= ~(1 << idx);
405
406 ucb1x00_enable(ucb);
407 ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
408 ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
409 ucb1x00_disable(ucb);
410
411 irq->fn = NULL;
412 irq->devid = NULL;
413 ret = 0;
414 }
415 spin_unlock_irq(&ucb->lock);
416 return ret;
417
418bad:
419 printk(KERN_ERR "Freeing bad UCB1x00 irq %d\n", idx);
420 return -EINVAL;
421}
422
423static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv)
424{
425 struct ucb1x00_dev *dev;
426 int ret = -ENOMEM;
427
428 dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL);
429 if (dev) {
430 dev->ucb = ucb;
431 dev->drv = drv;
432
433 ret = drv->add(dev);
434
435 if (ret == 0) {
436 list_add(&dev->dev_node, &ucb->devs);
437 list_add(&dev->drv_node, &drv->devs);
438 } else {
439 kfree(dev);
440 }
441 }
442 return ret;
443}
444
445static void ucb1x00_remove_dev(struct ucb1x00_dev *dev)
446{
447 dev->drv->remove(dev);
448 list_del(&dev->dev_node);
449 list_del(&dev->drv_node);
450 kfree(dev);
451}
452
453/*
454 * Try to probe our interrupt, rather than relying on lots of
455 * hard-coded machine dependencies. For reference, the expected
456 * IRQ mappings are:
457 *
458 * Machine Default IRQ
459 * adsbitsy IRQ_GPCIN4
460 * cerf IRQ_GPIO_UCB1200_IRQ
461 * flexanet IRQ_GPIO_GUI
462 * freebird IRQ_GPIO_FREEBIRD_UCB1300_IRQ
463 * graphicsclient ADS_EXT_IRQ(8)
464 * graphicsmaster ADS_EXT_IRQ(8)
465 * lart LART_IRQ_UCB1200
466 * omnimeter IRQ_GPIO23
467 * pfs168 IRQ_GPIO_UCB1300_IRQ
468 * simpad IRQ_GPIO_UCB1300_IRQ
469 * shannon SHANNON_IRQ_GPIO_IRQ_CODEC
470 * yopy IRQ_GPIO_UCB1200_IRQ
471 */
472static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
473{
474 unsigned long mask;
475
476 mask = probe_irq_on();
Ingo Molnarcfc73652006-06-26 15:26:13 +0100477 if (!mask) {
478 probe_irq_off(mask);
Russell King05c45ca2005-09-11 10:26:31 +0100479 return NO_IRQ;
Ingo Molnarcfc73652006-06-26 15:26:13 +0100480 }
Russell King05c45ca2005-09-11 10:26:31 +0100481
482 /*
483 * Enable the ADC interrupt.
484 */
485 ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC);
486 ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC);
487 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
488 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
489
490 /*
491 * Cause an ADC interrupt.
492 */
493 ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA);
494 ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START);
495
496 /*
497 * Wait for the conversion to complete.
498 */
499 while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0);
500 ucb1x00_reg_write(ucb, UCB_ADC_CR, 0);
501
502 /*
503 * Disable and clear interrupt.
504 */
505 ucb1x00_reg_write(ucb, UCB_IE_RIS, 0);
506 ucb1x00_reg_write(ucb, UCB_IE_FAL, 0);
507 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
508 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
509
510 /*
511 * Read triggered interrupt.
512 */
513 return probe_irq_off(mask);
514}
515
Tony Jones0c554452007-09-25 02:03:03 +0200516static void ucb1x00_release(struct device *dev)
Nicolas Pitre585f5452005-10-10 18:22:17 +0100517{
518 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
519 kfree(ucb);
520}
521
522static struct class ucb1x00_class = {
523 .name = "ucb1x00",
Tony Jones0c554452007-09-25 02:03:03 +0200524 .dev_release = ucb1x00_release,
Nicolas Pitre585f5452005-10-10 18:22:17 +0100525};
526
Russell King05c45ca2005-09-11 10:26:31 +0100527static int ucb1x00_probe(struct mcp *mcp)
528{
529 struct ucb1x00 *ucb;
530 struct ucb1x00_driver *drv;
531 unsigned int id;
532 int ret = -ENODEV;
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100533 int temp;
Russell King05c45ca2005-09-11 10:26:31 +0100534
535 mcp_enable(mcp);
536 id = mcp_reg_read(mcp, UCB_ID);
537
Pavel Machekb94ea6c2006-07-11 22:54:15 +0100538 if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) {
Russell King05c45ca2005-09-11 10:26:31 +0100539 printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id);
540 goto err_disable;
541 }
542
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700543 ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL);
Russell King05c45ca2005-09-11 10:26:31 +0100544 ret = -ENOMEM;
545 if (!ucb)
546 goto err_disable;
547
Russell King05c45ca2005-09-11 10:26:31 +0100548
Tony Jones0c554452007-09-25 02:03:03 +0200549 ucb->dev.class = &ucb1x00_class;
550 ucb->dev.parent = &mcp->attached_device;
Kay Sieversb2bf61f2009-03-24 16:38:23 -0700551 dev_set_name(&ucb->dev, "ucb1x00");
Russell King05c45ca2005-09-11 10:26:31 +0100552
553 spin_lock_init(&ucb->lock);
554 spin_lock_init(&ucb->io_lock);
555 sema_init(&ucb->adc_sem, 1);
556
557 ucb->id = id;
558 ucb->mcp = mcp;
559 ucb->irq = ucb1x00_detect_irq(ucb);
560 if (ucb->irq == NO_IRQ) {
561 printk(KERN_ERR "UCB1x00: IRQ probe failed\n");
562 ret = -ENODEV;
563 goto err_free;
564 }
565
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100566 ucb->gpio.base = -1;
567 if (mcp->gpio_base != 0) {
568 ucb->gpio.label = dev_name(&ucb->dev);
569 ucb->gpio.base = mcp->gpio_base;
570 ucb->gpio.ngpio = 10;
571 ucb->gpio.set = ucb1x00_gpio_set;
572 ucb->gpio.get = ucb1x00_gpio_get;
573 ucb->gpio.direction_input = ucb1x00_gpio_direction_input;
574 ucb->gpio.direction_output = ucb1x00_gpio_direction_output;
575 ret = gpiochip_add(&ucb->gpio);
576 if (ret)
577 goto err_free;
578 } else
579 dev_info(&ucb->dev, "gpio_base not set so no gpiolib support");
580
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700581 ret = request_irq(ucb->irq, ucb1x00_irq, IRQF_TRIGGER_RISING,
Russell King9ded96f2006-01-08 01:02:07 -0800582 "UCB1x00", ucb);
Russell King05c45ca2005-09-11 10:26:31 +0100583 if (ret) {
584 printk(KERN_ERR "ucb1x00: unable to grab irq%d: %d\n",
585 ucb->irq, ret);
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100586 goto err_gpio;
Russell King05c45ca2005-09-11 10:26:31 +0100587 }
588
Russell King05c45ca2005-09-11 10:26:31 +0100589 mcp_set_drvdata(mcp, ucb);
590
Tony Jones0c554452007-09-25 02:03:03 +0200591 ret = device_register(&ucb->dev);
Russell King05c45ca2005-09-11 10:26:31 +0100592 if (ret)
593 goto err_irq;
594
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100595
Russell King05c45ca2005-09-11 10:26:31 +0100596 INIT_LIST_HEAD(&ucb->devs);
Arjan van de Vena621aae2006-01-12 18:43:35 +0000597 mutex_lock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100598 list_add(&ucb->node, &ucb1x00_devices);
599 list_for_each_entry(drv, &ucb1x00_drivers, node) {
600 ucb1x00_add_dev(ucb, drv);
601 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000602 mutex_unlock(&ucb1x00_mutex);
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100603
Russell King05c45ca2005-09-11 10:26:31 +0100604 goto out;
605
606 err_irq:
607 free_irq(ucb->irq, ucb);
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100608 err_gpio:
609 if (ucb->gpio.base != -1)
610 temp = gpiochip_remove(&ucb->gpio);
Russell King05c45ca2005-09-11 10:26:31 +0100611 err_free:
612 kfree(ucb);
613 err_disable:
614 mcp_disable(mcp);
615 out:
616 return ret;
617}
618
619static void ucb1x00_remove(struct mcp *mcp)
620{
621 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
622 struct list_head *l, *n;
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100623 int ret;
Russell King05c45ca2005-09-11 10:26:31 +0100624
Arjan van de Vena621aae2006-01-12 18:43:35 +0000625 mutex_lock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100626 list_del(&ucb->node);
627 list_for_each_safe(l, n, &ucb->devs) {
628 struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node);
629 ucb1x00_remove_dev(dev);
630 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000631 mutex_unlock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100632
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100633 if (ucb->gpio.base != -1) {
634 ret = gpiochip_remove(&ucb->gpio);
635 if (ret)
636 dev_err(&ucb->dev, "Can't remove gpio chip: %d\n", ret);
637 }
638
Russell King05c45ca2005-09-11 10:26:31 +0100639 free_irq(ucb->irq, ucb);
Tony Jones0c554452007-09-25 02:03:03 +0200640 device_unregister(&ucb->dev);
Russell King05c45ca2005-09-11 10:26:31 +0100641}
642
Russell King05c45ca2005-09-11 10:26:31 +0100643int ucb1x00_register_driver(struct ucb1x00_driver *drv)
644{
645 struct ucb1x00 *ucb;
646
647 INIT_LIST_HEAD(&drv->devs);
Arjan van de Vena621aae2006-01-12 18:43:35 +0000648 mutex_lock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100649 list_add(&drv->node, &ucb1x00_drivers);
650 list_for_each_entry(ucb, &ucb1x00_devices, node) {
651 ucb1x00_add_dev(ucb, drv);
652 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000653 mutex_unlock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100654 return 0;
655}
656
657void ucb1x00_unregister_driver(struct ucb1x00_driver *drv)
658{
659 struct list_head *n, *l;
660
Arjan van de Vena621aae2006-01-12 18:43:35 +0000661 mutex_lock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100662 list_del(&drv->node);
663 list_for_each_safe(l, n, &drv->devs) {
664 struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node);
665 ucb1x00_remove_dev(dev);
666 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000667 mutex_unlock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100668}
669
670static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state)
671{
672 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
673 struct ucb1x00_dev *dev;
674
Arjan van de Vena621aae2006-01-12 18:43:35 +0000675 mutex_lock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100676 list_for_each_entry(dev, &ucb->devs, dev_node) {
677 if (dev->drv->suspend)
678 dev->drv->suspend(dev, state);
679 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000680 mutex_unlock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100681 return 0;
682}
683
684static int ucb1x00_resume(struct mcp *mcp)
685{
686 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
687 struct ucb1x00_dev *dev;
688
Thomas Kunze9ca3dc82009-02-10 14:50:56 +0100689 ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
Arjan van de Vena621aae2006-01-12 18:43:35 +0000690 mutex_lock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100691 list_for_each_entry(dev, &ucb->devs, dev_node) {
692 if (dev->drv->resume)
693 dev->drv->resume(dev);
694 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000695 mutex_unlock(&ucb1x00_mutex);
Russell King05c45ca2005-09-11 10:26:31 +0100696 return 0;
697}
698
699static struct mcp_driver ucb1x00_driver = {
700 .drv = {
701 .name = "ucb1x00",
702 },
703 .probe = ucb1x00_probe,
704 .remove = ucb1x00_remove,
705 .suspend = ucb1x00_suspend,
706 .resume = ucb1x00_resume,
707};
708
709static int __init ucb1x00_init(void)
710{
711 int ret = class_register(&ucb1x00_class);
712 if (ret == 0) {
713 ret = mcp_driver_register(&ucb1x00_driver);
714 if (ret)
715 class_unregister(&ucb1x00_class);
716 }
717 return ret;
718}
719
720static void __exit ucb1x00_exit(void)
721{
722 mcp_driver_unregister(&ucb1x00_driver);
723 class_unregister(&ucb1x00_class);
724}
725
726module_init(ucb1x00_init);
727module_exit(ucb1x00_exit);
728
Russell King05c45ca2005-09-11 10:26:31 +0100729EXPORT_SYMBOL(ucb1x00_io_set_dir);
730EXPORT_SYMBOL(ucb1x00_io_write);
731EXPORT_SYMBOL(ucb1x00_io_read);
732
733EXPORT_SYMBOL(ucb1x00_adc_enable);
734EXPORT_SYMBOL(ucb1x00_adc_read);
735EXPORT_SYMBOL(ucb1x00_adc_disable);
736
737EXPORT_SYMBOL(ucb1x00_hook_irq);
738EXPORT_SYMBOL(ucb1x00_free_irq);
739EXPORT_SYMBOL(ucb1x00_enable_irq);
740EXPORT_SYMBOL(ucb1x00_disable_irq);
741
742EXPORT_SYMBOL(ucb1x00_register_driver);
743EXPORT_SYMBOL(ucb1x00_unregister_driver);
744
745MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
746MODULE_DESCRIPTION("UCB1x00 core driver");
747MODULE_LICENSE("GPL");