2 * linux/drivers/mfd/ucb1x00-core.c
4 * Copyright (C) 2001 Russell King, All Rights Reserved.
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.
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.
16 * Note that all locks are private to this file. Nothing else may
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/errno.h>
25 #include <linux/interrupt.h>
26 #include <linux/device.h>
27 #include <linux/mutex.h>
28 #include <linux/mfd/ucb1x00.h>
29 #include <linux/gpio.h>
32 #include <mach/hardware.h>
34 static DEFINE_MUTEX(ucb1x00_mutex);
35 static LIST_HEAD(ucb1x00_drivers);
36 static LIST_HEAD(ucb1x00_devices);
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
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.
49 * ucb1x00_enable must have been called to enable the comms
50 * before using this function.
52 * This function takes a spinlock, disabling interrupts.
54 void ucb1x00_io_set_dir(struct ucb1x00 *ucb, unsigned int in, unsigned int out)
58 spin_lock_irqsave(&ucb->io_lock, flags);
62 ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
63 spin_unlock_irqrestore(&ucb->io_lock, flags);
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'
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.
77 * ucb1x00_enable must have been called to enable the comms
78 * before using this function.
80 * This function takes a spinlock, disabling interrupts.
82 void ucb1x00_io_write(struct ucb1x00 *ucb, unsigned int set, unsigned int clear)
86 spin_lock_irqsave(&ucb->io_lock, flags);
88 ucb->io_out &= ~clear;
90 ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
91 spin_unlock_irqrestore(&ucb->io_lock, flags);
95 * ucb1x00_io_read - read the current state of the IO pins
96 * @ucb: UCB1x00 structure describing chip
98 * Return a bitfield describing the logic state of the ten
99 * general purpose IO pins.
101 * ucb1x00_enable must have been called to enable the comms
102 * before using this function.
104 * This function does not take any semaphores or spinlocks.
106 unsigned int ucb1x00_io_read(struct ucb1x00 *ucb)
108 return ucb1x00_reg_read(ucb, UCB_IO_DATA);
111 static void ucb1x00_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
113 struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
116 spin_lock_irqsave(&ucb->io_lock, flags);
118 ucb->io_out |= 1 << offset;
120 ucb->io_out &= ~(1 << offset);
122 ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
123 spin_unlock_irqrestore(&ucb->io_lock, flags);
126 static int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset)
128 struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
129 return ucb1x00_reg_read(ucb, UCB_IO_DATA) & (1 << offset);
132 static int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
134 struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
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);
145 static int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset
148 struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
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);
156 ucb->io_out |= 1 << offset;
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);
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
175 * ucb1x00_adc_enable - enable the ADC converter
176 * @ucb: UCB1x00 structure describing chip
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.
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
187 * You should release the ADC as soon as possible using
188 * ucb1x00_adc_disable.
190 void ucb1x00_adc_enable(struct ucb1x00 *ucb)
194 ucb->adc_cr |= UCB_ADC_ENA;
197 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
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.
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.
210 * This function currently spins waiting for the conversion to
211 * complete (2 frames max without sync).
213 * If called for a synchronised ADC conversion, it may sleep
214 * with the ADC semaphore held.
216 unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync)
221 adc_channel |= UCB_ADC_SYNC_ENA;
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);
227 val = ucb1x00_reg_read(ucb, UCB_ADC_DATA);
228 if (val & UCB_ADC_DAT_VAL)
230 /* yield to other processes */
231 set_current_state(TASK_INTERRUPTIBLE);
235 return UCB_ADC_DAT(val);
239 * ucb1x00_adc_disable - disable the ADC converter
240 * @ucb: UCB1x00 structure describing chip
242 * Disable the ADC converter and release the ADC semaphore.
244 void ucb1x00_adc_disable(struct ucb1x00 *ucb)
246 ucb->adc_cr &= ~UCB_ADC_ENA;
247 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
248 ucb1x00_disable(ucb);
254 * UCB1x00 Interrupt handling.
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.
261 static irqreturn_t ucb1x00_irq(int irqnr, void *devid)
263 struct ucb1x00 *ucb = devid;
264 struct ucb1x00_irq *irq;
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);
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);
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
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.
291 * Interrupt handlers will be called with other interrupts enabled.
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
297 int ucb1x00_hook_irq(struct ucb1x00 *ucb, unsigned int idx, void (*fn)(int, void *), void *devid)
299 struct ucb1x00_irq *irq;
303 irq = ucb->irq_handler + idx;
306 spin_lock_irq(&ucb->lock);
307 if (irq->fn == NULL) {
312 spin_unlock_irq(&ucb->lock);
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
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.
327 void ucb1x00_enable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
332 spin_lock_irqsave(&ucb->lock, flags);
335 if (edges & UCB_RISING) {
336 ucb->irq_ris_enbl |= 1 << idx;
337 ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
339 if (edges & UCB_FALLING) {
340 ucb->irq_fal_enbl |= 1 << idx;
341 ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
343 ucb1x00_disable(ucb);
344 spin_unlock_irqrestore(&ucb->lock, flags);
349 * ucb1x00_disable_irq - disable an UCB1x00 interrupt source
350 * @ucb: UCB1x00 structure describing chip
351 * @edges: interrupt edges to disable
353 * Disable the specified interrupt triggering on the specified
354 * (%UCB_RISING, %UCB_FALLING or both) edges.
356 void ucb1x00_disable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
361 spin_lock_irqsave(&ucb->lock, flags);
364 if (edges & UCB_RISING) {
365 ucb->irq_ris_enbl &= ~(1 << idx);
366 ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
368 if (edges & UCB_FALLING) {
369 ucb->irq_fal_enbl &= ~(1 << idx);
370 ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
372 ucb1x00_disable(ucb);
373 spin_unlock_irqrestore(&ucb->lock, flags);
378 * ucb1x00_free_irq - disable and free the specified UCB1x00 interrupt
379 * @ucb: UCB1x00 structure describing chip
380 * @idx: interrupt index
383 * Disable the interrupt source and remove the handler. devid must
384 * match the devid passed when hooking the interrupt.
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
390 int ucb1x00_free_irq(struct ucb1x00 *ucb, unsigned int idx, void *devid)
392 struct ucb1x00_irq *irq;
398 irq = ucb->irq_handler + idx;
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);
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);
415 spin_unlock_irq(&ucb->lock);
419 printk(KERN_ERR "Freeing bad UCB1x00 irq %d\n", idx);
423 static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv)
425 struct ucb1x00_dev *dev;
428 dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL);
436 list_add(&dev->dev_node, &ucb->devs);
437 list_add(&dev->drv_node, &drv->devs);
445 static void ucb1x00_remove_dev(struct ucb1x00_dev *dev)
447 dev->drv->remove(dev);
448 list_del(&dev->dev_node);
449 list_del(&dev->drv_node);
454 * Try to probe our interrupt, rather than relying on lots of
455 * hard-coded machine dependencies. For reference, the expected
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
472 static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
476 mask = probe_irq_on();
483 * Enable the ADC interrupt.
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);
491 * Cause an ADC interrupt.
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);
497 * Wait for the conversion to complete.
499 while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0);
500 ucb1x00_reg_write(ucb, UCB_ADC_CR, 0);
503 * Disable and clear interrupt.
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);
511 * Read triggered interrupt.
513 return probe_irq_off(mask);
516 static void ucb1x00_release(struct device *dev)
518 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
522 static struct class ucb1x00_class = {
524 .dev_release = ucb1x00_release,
527 static int ucb1x00_probe(struct mcp *mcp)
530 struct ucb1x00_driver *drv;
536 id = mcp_reg_read(mcp, UCB_ID);
538 if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) {
539 printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id);
543 ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL);
549 ucb->dev.class = &ucb1x00_class;
550 ucb->dev.parent = &mcp->attached_device;
551 dev_set_name(&ucb->dev, "ucb1x00");
553 spin_lock_init(&ucb->lock);
554 spin_lock_init(&ucb->io_lock);
555 sema_init(&ucb->adc_sem, 1);
559 ucb->irq = ucb1x00_detect_irq(ucb);
560 if (ucb->irq == NO_IRQ) {
561 printk(KERN_ERR "UCB1x00: IRQ probe failed\n");
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);
579 dev_info(&ucb->dev, "gpio_base not set so no gpiolib support");
581 ret = request_irq(ucb->irq, ucb1x00_irq, IRQF_TRIGGER_RISING,
584 printk(KERN_ERR "ucb1x00: unable to grab irq%d: %d\n",
589 mcp_set_drvdata(mcp, ucb);
591 ret = device_register(&ucb->dev);
596 INIT_LIST_HEAD(&ucb->devs);
597 mutex_lock(&ucb1x00_mutex);
598 list_add(&ucb->node, &ucb1x00_devices);
599 list_for_each_entry(drv, &ucb1x00_drivers, node) {
600 ucb1x00_add_dev(ucb, drv);
602 mutex_unlock(&ucb1x00_mutex);
607 free_irq(ucb->irq, ucb);
609 if (ucb->gpio.base != -1)
610 temp = gpiochip_remove(&ucb->gpio);
619 static void ucb1x00_remove(struct mcp *mcp)
621 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
622 struct list_head *l, *n;
625 mutex_lock(&ucb1x00_mutex);
626 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);
631 mutex_unlock(&ucb1x00_mutex);
633 if (ucb->gpio.base != -1) {
634 ret = gpiochip_remove(&ucb->gpio);
636 dev_err(&ucb->dev, "Can't remove gpio chip: %d\n", ret);
639 free_irq(ucb->irq, ucb);
640 device_unregister(&ucb->dev);
643 int ucb1x00_register_driver(struct ucb1x00_driver *drv)
647 INIT_LIST_HEAD(&drv->devs);
648 mutex_lock(&ucb1x00_mutex);
649 list_add(&drv->node, &ucb1x00_drivers);
650 list_for_each_entry(ucb, &ucb1x00_devices, node) {
651 ucb1x00_add_dev(ucb, drv);
653 mutex_unlock(&ucb1x00_mutex);
657 void ucb1x00_unregister_driver(struct ucb1x00_driver *drv)
659 struct list_head *n, *l;
661 mutex_lock(&ucb1x00_mutex);
662 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);
667 mutex_unlock(&ucb1x00_mutex);
670 static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state)
672 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
673 struct ucb1x00_dev *dev;
675 mutex_lock(&ucb1x00_mutex);
676 list_for_each_entry(dev, &ucb->devs, dev_node) {
677 if (dev->drv->suspend)
678 dev->drv->suspend(dev, state);
680 mutex_unlock(&ucb1x00_mutex);
684 static int ucb1x00_resume(struct mcp *mcp)
686 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
687 struct ucb1x00_dev *dev;
689 ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
690 mutex_lock(&ucb1x00_mutex);
691 list_for_each_entry(dev, &ucb->devs, dev_node) {
692 if (dev->drv->resume)
693 dev->drv->resume(dev);
695 mutex_unlock(&ucb1x00_mutex);
699 static struct mcp_driver ucb1x00_driver = {
703 .probe = ucb1x00_probe,
704 .remove = ucb1x00_remove,
705 .suspend = ucb1x00_suspend,
706 .resume = ucb1x00_resume,
709 static int __init ucb1x00_init(void)
711 int ret = class_register(&ucb1x00_class);
713 ret = mcp_driver_register(&ucb1x00_driver);
715 class_unregister(&ucb1x00_class);
720 static void __exit ucb1x00_exit(void)
722 mcp_driver_unregister(&ucb1x00_driver);
723 class_unregister(&ucb1x00_class);
726 module_init(ucb1x00_init);
727 module_exit(ucb1x00_exit);
729 EXPORT_SYMBOL(ucb1x00_io_set_dir);
730 EXPORT_SYMBOL(ucb1x00_io_write);
731 EXPORT_SYMBOL(ucb1x00_io_read);
733 EXPORT_SYMBOL(ucb1x00_adc_enable);
734 EXPORT_SYMBOL(ucb1x00_adc_read);
735 EXPORT_SYMBOL(ucb1x00_adc_disable);
737 EXPORT_SYMBOL(ucb1x00_hook_irq);
738 EXPORT_SYMBOL(ucb1x00_free_irq);
739 EXPORT_SYMBOL(ucb1x00_enable_irq);
740 EXPORT_SYMBOL(ucb1x00_disable_irq);
742 EXPORT_SYMBOL(ucb1x00_register_driver);
743 EXPORT_SYMBOL(ucb1x00_unregister_driver);
745 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
746 MODULE_DESCRIPTION("UCB1x00 core driver");
747 MODULE_LICENSE("GPL");