105c45ca9SRussell King /* 205c45ca9SRussell King * linux/drivers/mfd/ucb1x00-core.c 305c45ca9SRussell King * 405c45ca9SRussell King * Copyright (C) 2001 Russell King, All Rights Reserved. 505c45ca9SRussell King * 605c45ca9SRussell King * This program is free software; you can redistribute it and/or modify 705c45ca9SRussell King * it under the terms of the GNU General Public License as published by 805c45ca9SRussell King * the Free Software Foundation; either version 2 of the License. 905c45ca9SRussell King * 1005c45ca9SRussell King * The UCB1x00 core driver provides basic services for handling IO, 1105c45ca9SRussell King * the ADC, interrupts, and accessing registers. It is designed 1205c45ca9SRussell King * such that everything goes through this layer, thereby providing 1305c45ca9SRussell King * a consistent locking methodology, as well as allowing the drivers 1405c45ca9SRussell King * to be used on other non-MCP-enabled hardware platforms. 1505c45ca9SRussell King * 1605c45ca9SRussell King * Note that all locks are private to this file. Nothing else may 1705c45ca9SRussell King * touch them. 1805c45ca9SRussell King */ 1905c45ca9SRussell King #include <linux/module.h> 2005c45ca9SRussell King #include <linux/kernel.h> 21d43c36dcSAlexey Dobriyan #include <linux/sched.h> 2205c45ca9SRussell King #include <linux/slab.h> 2305c45ca9SRussell King #include <linux/init.h> 2405c45ca9SRussell King #include <linux/errno.h> 2505c45ca9SRussell King #include <linux/interrupt.h> 2605c45ca9SRussell King #include <linux/device.h> 27a621aaedSArjan van de Ven #include <linux/mutex.h> 28c8602edfSThomas Kunze #include <linux/mfd/ucb1x00.h> 299ca3dc80SThomas Kunze #include <linux/gpio.h> 302c08583cSPeter Huewe #include <linux/semaphore.h> 3105c45ca9SRussell King 32dcea83adSRussell King #include <mach/dma.h> 33a09e64fbSRussell King #include <mach/hardware.h> 3405c45ca9SRussell King 35a621aaedSArjan van de Ven static DEFINE_MUTEX(ucb1x00_mutex); 3605c45ca9SRussell King static LIST_HEAD(ucb1x00_drivers); 3705c45ca9SRussell King static LIST_HEAD(ucb1x00_devices); 3805c45ca9SRussell King 3905c45ca9SRussell King /** 4005c45ca9SRussell King * ucb1x00_io_set_dir - set IO direction 4105c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 4205c45ca9SRussell King * @in: bitfield of IO pins to be set as inputs 4305c45ca9SRussell King * @out: bitfield of IO pins to be set as outputs 4405c45ca9SRussell King * 4505c45ca9SRussell King * Set the IO direction of the ten general purpose IO pins on 4605c45ca9SRussell King * the UCB1x00 chip. The @in bitfield has priority over the 4705c45ca9SRussell King * @out bitfield, in that if you specify a pin as both input 4805c45ca9SRussell King * and output, it will end up as an input. 4905c45ca9SRussell King * 5005c45ca9SRussell King * ucb1x00_enable must have been called to enable the comms 5105c45ca9SRussell King * before using this function. 5205c45ca9SRussell King * 5305c45ca9SRussell King * This function takes a spinlock, disabling interrupts. 5405c45ca9SRussell King */ 5505c45ca9SRussell King void ucb1x00_io_set_dir(struct ucb1x00 *ucb, unsigned int in, unsigned int out) 5605c45ca9SRussell King { 5705c45ca9SRussell King unsigned long flags; 5805c45ca9SRussell King 5905c45ca9SRussell King spin_lock_irqsave(&ucb->io_lock, flags); 6005c45ca9SRussell King ucb->io_dir |= out; 6105c45ca9SRussell King ucb->io_dir &= ~in; 6205c45ca9SRussell King 6305c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 6405c45ca9SRussell King spin_unlock_irqrestore(&ucb->io_lock, flags); 6505c45ca9SRussell King } 6605c45ca9SRussell King 6705c45ca9SRussell King /** 6805c45ca9SRussell King * ucb1x00_io_write - set or clear IO outputs 6905c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 7005c45ca9SRussell King * @set: bitfield of IO pins to set to logic '1' 7105c45ca9SRussell King * @clear: bitfield of IO pins to set to logic '0' 7205c45ca9SRussell King * 7305c45ca9SRussell King * Set the IO output state of the specified IO pins. The value 7405c45ca9SRussell King * is retained if the pins are subsequently configured as inputs. 7505c45ca9SRussell King * The @clear bitfield has priority over the @set bitfield - 7605c45ca9SRussell King * outputs will be cleared. 7705c45ca9SRussell King * 7805c45ca9SRussell King * ucb1x00_enable must have been called to enable the comms 7905c45ca9SRussell King * before using this function. 8005c45ca9SRussell King * 8105c45ca9SRussell King * This function takes a spinlock, disabling interrupts. 8205c45ca9SRussell King */ 8305c45ca9SRussell King void ucb1x00_io_write(struct ucb1x00 *ucb, unsigned int set, unsigned int clear) 8405c45ca9SRussell King { 8505c45ca9SRussell King unsigned long flags; 8605c45ca9SRussell King 8705c45ca9SRussell King spin_lock_irqsave(&ucb->io_lock, flags); 8805c45ca9SRussell King ucb->io_out |= set; 8905c45ca9SRussell King ucb->io_out &= ~clear; 9005c45ca9SRussell King 9105c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 9205c45ca9SRussell King spin_unlock_irqrestore(&ucb->io_lock, flags); 9305c45ca9SRussell King } 9405c45ca9SRussell King 9505c45ca9SRussell King /** 9605c45ca9SRussell King * ucb1x00_io_read - read the current state of the IO pins 9705c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 9805c45ca9SRussell King * 9905c45ca9SRussell King * Return a bitfield describing the logic state of the ten 10005c45ca9SRussell King * general purpose IO pins. 10105c45ca9SRussell King * 10205c45ca9SRussell King * ucb1x00_enable must have been called to enable the comms 10305c45ca9SRussell King * before using this function. 10405c45ca9SRussell King * 10505c45ca9SRussell King * This function does not take any semaphores or spinlocks. 10605c45ca9SRussell King */ 10705c45ca9SRussell King unsigned int ucb1x00_io_read(struct ucb1x00 *ucb) 10805c45ca9SRussell King { 10905c45ca9SRussell King return ucb1x00_reg_read(ucb, UCB_IO_DATA); 11005c45ca9SRussell King } 11105c45ca9SRussell King 1129ca3dc80SThomas Kunze static void ucb1x00_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 1139ca3dc80SThomas Kunze { 1149ca3dc80SThomas Kunze struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio); 1159ca3dc80SThomas Kunze unsigned long flags; 1169ca3dc80SThomas Kunze 1179ca3dc80SThomas Kunze spin_lock_irqsave(&ucb->io_lock, flags); 1189ca3dc80SThomas Kunze if (value) 1199ca3dc80SThomas Kunze ucb->io_out |= 1 << offset; 1209ca3dc80SThomas Kunze else 1219ca3dc80SThomas Kunze ucb->io_out &= ~(1 << offset); 1229ca3dc80SThomas Kunze 1239ca3dc80SThomas Kunze ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 1249ca3dc80SThomas Kunze spin_unlock_irqrestore(&ucb->io_lock, flags); 1259ca3dc80SThomas Kunze } 1269ca3dc80SThomas Kunze 1279ca3dc80SThomas Kunze static int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset) 1289ca3dc80SThomas Kunze { 1299ca3dc80SThomas Kunze struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio); 1309ca3dc80SThomas Kunze return ucb1x00_reg_read(ucb, UCB_IO_DATA) & (1 << offset); 1319ca3dc80SThomas Kunze } 1329ca3dc80SThomas Kunze 1339ca3dc80SThomas Kunze static int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 1349ca3dc80SThomas Kunze { 1359ca3dc80SThomas Kunze struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio); 1369ca3dc80SThomas Kunze unsigned long flags; 1379ca3dc80SThomas Kunze 1389ca3dc80SThomas Kunze spin_lock_irqsave(&ucb->io_lock, flags); 1399ca3dc80SThomas Kunze ucb->io_dir &= ~(1 << offset); 1409ca3dc80SThomas Kunze ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 1419ca3dc80SThomas Kunze spin_unlock_irqrestore(&ucb->io_lock, flags); 1429ca3dc80SThomas Kunze 1439ca3dc80SThomas Kunze return 0; 1449ca3dc80SThomas Kunze } 1459ca3dc80SThomas Kunze 1469ca3dc80SThomas Kunze static int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset 1479ca3dc80SThomas Kunze , int value) 1489ca3dc80SThomas Kunze { 1499ca3dc80SThomas Kunze struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio); 1509ca3dc80SThomas Kunze unsigned long flags; 1519ca3dc80SThomas Kunze 1529ca3dc80SThomas Kunze spin_lock_irqsave(&ucb->io_lock, flags); 1539ca3dc80SThomas Kunze ucb->io_dir |= (1 << offset); 1549ca3dc80SThomas Kunze ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 1559ca3dc80SThomas Kunze 1569ca3dc80SThomas Kunze if (value) 1579ca3dc80SThomas Kunze ucb->io_out |= 1 << offset; 1589ca3dc80SThomas Kunze else 1599ca3dc80SThomas Kunze ucb->io_out &= ~(1 << offset); 1609ca3dc80SThomas Kunze ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 1619ca3dc80SThomas Kunze spin_unlock_irqrestore(&ucb->io_lock, flags); 1629ca3dc80SThomas Kunze 1639ca3dc80SThomas Kunze return 0; 1649ca3dc80SThomas Kunze } 1659ca3dc80SThomas Kunze 16605c45ca9SRussell King /* 16705c45ca9SRussell King * UCB1300 data sheet says we must: 16805c45ca9SRussell King * 1. enable ADC => 5us (including reference startup time) 16905c45ca9SRussell King * 2. select input => 51*tsibclk => 4.3us 17005c45ca9SRussell King * 3. start conversion => 102*tsibclk => 8.5us 17105c45ca9SRussell King * (tsibclk = 1/11981000) 17205c45ca9SRussell King * Period between SIB 128-bit frames = 10.7us 17305c45ca9SRussell King */ 17405c45ca9SRussell King 17505c45ca9SRussell King /** 17605c45ca9SRussell King * ucb1x00_adc_enable - enable the ADC converter 17705c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 17805c45ca9SRussell King * 17905c45ca9SRussell King * Enable the ucb1x00 and ADC converter on the UCB1x00 for use. 18005c45ca9SRussell King * Any code wishing to use the ADC converter must call this 18105c45ca9SRussell King * function prior to using it. 18205c45ca9SRussell King * 18305c45ca9SRussell King * This function takes the ADC semaphore to prevent two or more 18405c45ca9SRussell King * concurrent uses, and therefore may sleep. As a result, it 18505c45ca9SRussell King * can only be called from process context, not interrupt 18605c45ca9SRussell King * context. 18705c45ca9SRussell King * 18805c45ca9SRussell King * You should release the ADC as soon as possible using 18905c45ca9SRussell King * ucb1x00_adc_disable. 19005c45ca9SRussell King */ 19105c45ca9SRussell King void ucb1x00_adc_enable(struct ucb1x00 *ucb) 19205c45ca9SRussell King { 19305c45ca9SRussell King down(&ucb->adc_sem); 19405c45ca9SRussell King 19505c45ca9SRussell King ucb->adc_cr |= UCB_ADC_ENA; 19605c45ca9SRussell King 19705c45ca9SRussell King ucb1x00_enable(ucb); 19805c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr); 19905c45ca9SRussell King } 20005c45ca9SRussell King 20105c45ca9SRussell King /** 20205c45ca9SRussell King * ucb1x00_adc_read - read the specified ADC channel 20305c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 20405c45ca9SRussell King * @adc_channel: ADC channel mask 20505c45ca9SRussell King * @sync: wait for syncronisation pulse. 20605c45ca9SRussell King * 20705c45ca9SRussell King * Start an ADC conversion and wait for the result. Note that 20805c45ca9SRussell King * synchronised ADC conversions (via the ADCSYNC pin) must wait 20905c45ca9SRussell King * until the trigger is asserted and the conversion is finished. 21005c45ca9SRussell King * 21105c45ca9SRussell King * This function currently spins waiting for the conversion to 21205c45ca9SRussell King * complete (2 frames max without sync). 21305c45ca9SRussell King * 21405c45ca9SRussell King * If called for a synchronised ADC conversion, it may sleep 21505c45ca9SRussell King * with the ADC semaphore held. 21605c45ca9SRussell King */ 21705c45ca9SRussell King unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync) 21805c45ca9SRussell King { 21905c45ca9SRussell King unsigned int val; 22005c45ca9SRussell King 22105c45ca9SRussell King if (sync) 22205c45ca9SRussell King adc_channel |= UCB_ADC_SYNC_ENA; 22305c45ca9SRussell King 22405c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel); 22505c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START); 22605c45ca9SRussell King 22705c45ca9SRussell King for (;;) { 22805c45ca9SRussell King val = ucb1x00_reg_read(ucb, UCB_ADC_DATA); 22905c45ca9SRussell King if (val & UCB_ADC_DAT_VAL) 23005c45ca9SRussell King break; 23105c45ca9SRussell King /* yield to other processes */ 23205c45ca9SRussell King set_current_state(TASK_INTERRUPTIBLE); 23305c45ca9SRussell King schedule_timeout(1); 23405c45ca9SRussell King } 23505c45ca9SRussell King 23605c45ca9SRussell King return UCB_ADC_DAT(val); 23705c45ca9SRussell King } 23805c45ca9SRussell King 23905c45ca9SRussell King /** 24005c45ca9SRussell King * ucb1x00_adc_disable - disable the ADC converter 24105c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 24205c45ca9SRussell King * 24305c45ca9SRussell King * Disable the ADC converter and release the ADC semaphore. 24405c45ca9SRussell King */ 24505c45ca9SRussell King void ucb1x00_adc_disable(struct ucb1x00 *ucb) 24605c45ca9SRussell King { 24705c45ca9SRussell King ucb->adc_cr &= ~UCB_ADC_ENA; 24805c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr); 24905c45ca9SRussell King ucb1x00_disable(ucb); 25005c45ca9SRussell King 25105c45ca9SRussell King up(&ucb->adc_sem); 25205c45ca9SRussell King } 25305c45ca9SRussell King 25405c45ca9SRussell King /* 25505c45ca9SRussell King * UCB1x00 Interrupt handling. 25605c45ca9SRussell King * 25705c45ca9SRussell King * The UCB1x00 can generate interrupts when the SIBCLK is stopped. 25805c45ca9SRussell King * Since we need to read an internal register, we must re-enable 25905c45ca9SRussell King * SIBCLK to talk to the chip. We leave the clock running until 26005c45ca9SRussell King * we have finished processing all interrupts from the chip. 26105c45ca9SRussell King */ 2627d12e780SDavid Howells static irqreturn_t ucb1x00_irq(int irqnr, void *devid) 26305c45ca9SRussell King { 26405c45ca9SRussell King struct ucb1x00 *ucb = devid; 26505c45ca9SRussell King struct ucb1x00_irq *irq; 26605c45ca9SRussell King unsigned int isr, i; 26705c45ca9SRussell King 26805c45ca9SRussell King ucb1x00_enable(ucb); 26905c45ca9SRussell King isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS); 27005c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr); 27105c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 27205c45ca9SRussell King 27305c45ca9SRussell King for (i = 0, irq = ucb->irq_handler; i < 16 && isr; i++, isr >>= 1, irq++) 27405c45ca9SRussell King if (isr & 1 && irq->fn) 27505c45ca9SRussell King irq->fn(i, irq->devid); 27605c45ca9SRussell King ucb1x00_disable(ucb); 27705c45ca9SRussell King 27805c45ca9SRussell King return IRQ_HANDLED; 27905c45ca9SRussell King } 28005c45ca9SRussell King 28105c45ca9SRussell King /** 28205c45ca9SRussell King * ucb1x00_hook_irq - hook a UCB1x00 interrupt 28305c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 28405c45ca9SRussell King * @idx: interrupt index 28505c45ca9SRussell King * @fn: function to call when interrupt is triggered 28605c45ca9SRussell King * @devid: device id to pass to interrupt handler 28705c45ca9SRussell King * 28805c45ca9SRussell King * Hook the specified interrupt. You can only register one handler 28905c45ca9SRussell King * for each interrupt source. The interrupt source is not enabled 29005c45ca9SRussell King * by this function; use ucb1x00_enable_irq instead. 29105c45ca9SRussell King * 29205c45ca9SRussell King * Interrupt handlers will be called with other interrupts enabled. 29305c45ca9SRussell King * 29405c45ca9SRussell King * Returns zero on success, or one of the following errors: 29505c45ca9SRussell King * -EINVAL if the interrupt index is invalid 29605c45ca9SRussell King * -EBUSY if the interrupt has already been hooked 29705c45ca9SRussell King */ 29805c45ca9SRussell King int ucb1x00_hook_irq(struct ucb1x00 *ucb, unsigned int idx, void (*fn)(int, void *), void *devid) 29905c45ca9SRussell King { 30005c45ca9SRussell King struct ucb1x00_irq *irq; 30105c45ca9SRussell King int ret = -EINVAL; 30205c45ca9SRussell King 30305c45ca9SRussell King if (idx < 16) { 30405c45ca9SRussell King irq = ucb->irq_handler + idx; 30505c45ca9SRussell King ret = -EBUSY; 30605c45ca9SRussell King 30705c45ca9SRussell King spin_lock_irq(&ucb->lock); 30805c45ca9SRussell King if (irq->fn == NULL) { 30905c45ca9SRussell King irq->devid = devid; 31005c45ca9SRussell King irq->fn = fn; 31105c45ca9SRussell King ret = 0; 31205c45ca9SRussell King } 31305c45ca9SRussell King spin_unlock_irq(&ucb->lock); 31405c45ca9SRussell King } 31505c45ca9SRussell King return ret; 31605c45ca9SRussell King } 31705c45ca9SRussell King 31805c45ca9SRussell King /** 31905c45ca9SRussell King * ucb1x00_enable_irq - enable an UCB1x00 interrupt source 32005c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 32105c45ca9SRussell King * @idx: interrupt index 32205c45ca9SRussell King * @edges: interrupt edges to enable 32305c45ca9SRussell King * 32405c45ca9SRussell King * Enable the specified interrupt to trigger on %UCB_RISING, 32505c45ca9SRussell King * %UCB_FALLING or both edges. The interrupt should have been 32605c45ca9SRussell King * hooked by ucb1x00_hook_irq. 32705c45ca9SRussell King */ 32805c45ca9SRussell King void ucb1x00_enable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges) 32905c45ca9SRussell King { 33005c45ca9SRussell King unsigned long flags; 33105c45ca9SRussell King 33205c45ca9SRussell King if (idx < 16) { 33305c45ca9SRussell King spin_lock_irqsave(&ucb->lock, flags); 33405c45ca9SRussell King 33505c45ca9SRussell King ucb1x00_enable(ucb); 33605c45ca9SRussell King if (edges & UCB_RISING) { 33705c45ca9SRussell King ucb->irq_ris_enbl |= 1 << idx; 33805c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); 33905c45ca9SRussell King } 34005c45ca9SRussell King if (edges & UCB_FALLING) { 34105c45ca9SRussell King ucb->irq_fal_enbl |= 1 << idx; 34205c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); 34305c45ca9SRussell King } 34405c45ca9SRussell King ucb1x00_disable(ucb); 34505c45ca9SRussell King spin_unlock_irqrestore(&ucb->lock, flags); 34605c45ca9SRussell King } 34705c45ca9SRussell King } 34805c45ca9SRussell King 34905c45ca9SRussell King /** 35005c45ca9SRussell King * ucb1x00_disable_irq - disable an UCB1x00 interrupt source 35105c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 35205c45ca9SRussell King * @edges: interrupt edges to disable 35305c45ca9SRussell King * 35405c45ca9SRussell King * Disable the specified interrupt triggering on the specified 35505c45ca9SRussell King * (%UCB_RISING, %UCB_FALLING or both) edges. 35605c45ca9SRussell King */ 35705c45ca9SRussell King void ucb1x00_disable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges) 35805c45ca9SRussell King { 35905c45ca9SRussell King unsigned long flags; 36005c45ca9SRussell King 36105c45ca9SRussell King if (idx < 16) { 36205c45ca9SRussell King spin_lock_irqsave(&ucb->lock, flags); 36305c45ca9SRussell King 36405c45ca9SRussell King ucb1x00_enable(ucb); 36505c45ca9SRussell King if (edges & UCB_RISING) { 36605c45ca9SRussell King ucb->irq_ris_enbl &= ~(1 << idx); 36705c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); 36805c45ca9SRussell King } 36905c45ca9SRussell King if (edges & UCB_FALLING) { 37005c45ca9SRussell King ucb->irq_fal_enbl &= ~(1 << idx); 37105c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); 37205c45ca9SRussell King } 37305c45ca9SRussell King ucb1x00_disable(ucb); 37405c45ca9SRussell King spin_unlock_irqrestore(&ucb->lock, flags); 37505c45ca9SRussell King } 37605c45ca9SRussell King } 37705c45ca9SRussell King 37805c45ca9SRussell King /** 37905c45ca9SRussell King * ucb1x00_free_irq - disable and free the specified UCB1x00 interrupt 38005c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 38105c45ca9SRussell King * @idx: interrupt index 38205c45ca9SRussell King * @devid: device id. 38305c45ca9SRussell King * 38405c45ca9SRussell King * Disable the interrupt source and remove the handler. devid must 38505c45ca9SRussell King * match the devid passed when hooking the interrupt. 38605c45ca9SRussell King * 38705c45ca9SRussell King * Returns zero on success, or one of the following errors: 38805c45ca9SRussell King * -EINVAL if the interrupt index is invalid 38905c45ca9SRussell King * -ENOENT if devid does not match 39005c45ca9SRussell King */ 39105c45ca9SRussell King int ucb1x00_free_irq(struct ucb1x00 *ucb, unsigned int idx, void *devid) 39205c45ca9SRussell King { 39305c45ca9SRussell King struct ucb1x00_irq *irq; 39405c45ca9SRussell King int ret; 39505c45ca9SRussell King 39605c45ca9SRussell King if (idx >= 16) 39705c45ca9SRussell King goto bad; 39805c45ca9SRussell King 39905c45ca9SRussell King irq = ucb->irq_handler + idx; 40005c45ca9SRussell King ret = -ENOENT; 40105c45ca9SRussell King 40205c45ca9SRussell King spin_lock_irq(&ucb->lock); 40305c45ca9SRussell King if (irq->devid == devid) { 40405c45ca9SRussell King ucb->irq_ris_enbl &= ~(1 << idx); 40505c45ca9SRussell King ucb->irq_fal_enbl &= ~(1 << idx); 40605c45ca9SRussell King 40705c45ca9SRussell King ucb1x00_enable(ucb); 40805c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); 40905c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); 41005c45ca9SRussell King ucb1x00_disable(ucb); 41105c45ca9SRussell King 41205c45ca9SRussell King irq->fn = NULL; 41305c45ca9SRussell King irq->devid = NULL; 41405c45ca9SRussell King ret = 0; 41505c45ca9SRussell King } 41605c45ca9SRussell King spin_unlock_irq(&ucb->lock); 41705c45ca9SRussell King return ret; 41805c45ca9SRussell King 41905c45ca9SRussell King bad: 42005c45ca9SRussell King printk(KERN_ERR "Freeing bad UCB1x00 irq %d\n", idx); 42105c45ca9SRussell King return -EINVAL; 42205c45ca9SRussell King } 42305c45ca9SRussell King 42405c45ca9SRussell King static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv) 42505c45ca9SRussell King { 42605c45ca9SRussell King struct ucb1x00_dev *dev; 42705c45ca9SRussell King int ret = -ENOMEM; 42805c45ca9SRussell King 42905c45ca9SRussell King dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL); 43005c45ca9SRussell King if (dev) { 43105c45ca9SRussell King dev->ucb = ucb; 43205c45ca9SRussell King dev->drv = drv; 43305c45ca9SRussell King 43405c45ca9SRussell King ret = drv->add(dev); 43505c45ca9SRussell King 43605c45ca9SRussell King if (ret == 0) { 43705c45ca9SRussell King list_add(&dev->dev_node, &ucb->devs); 43805c45ca9SRussell King list_add(&dev->drv_node, &drv->devs); 43905c45ca9SRussell King } else { 44005c45ca9SRussell King kfree(dev); 44105c45ca9SRussell King } 44205c45ca9SRussell King } 44305c45ca9SRussell King return ret; 44405c45ca9SRussell King } 44505c45ca9SRussell King 44605c45ca9SRussell King static void ucb1x00_remove_dev(struct ucb1x00_dev *dev) 44705c45ca9SRussell King { 44805c45ca9SRussell King dev->drv->remove(dev); 44905c45ca9SRussell King list_del(&dev->dev_node); 45005c45ca9SRussell King list_del(&dev->drv_node); 45105c45ca9SRussell King kfree(dev); 45205c45ca9SRussell King } 45305c45ca9SRussell King 45405c45ca9SRussell King /* 45505c45ca9SRussell King * Try to probe our interrupt, rather than relying on lots of 45605c45ca9SRussell King * hard-coded machine dependencies. For reference, the expected 45705c45ca9SRussell King * IRQ mappings are: 45805c45ca9SRussell King * 45905c45ca9SRussell King * Machine Default IRQ 46005c45ca9SRussell King * adsbitsy IRQ_GPCIN4 46105c45ca9SRussell King * cerf IRQ_GPIO_UCB1200_IRQ 46205c45ca9SRussell King * flexanet IRQ_GPIO_GUI 46305c45ca9SRussell King * freebird IRQ_GPIO_FREEBIRD_UCB1300_IRQ 46405c45ca9SRussell King * graphicsclient ADS_EXT_IRQ(8) 46505c45ca9SRussell King * graphicsmaster ADS_EXT_IRQ(8) 46605c45ca9SRussell King * lart LART_IRQ_UCB1200 46705c45ca9SRussell King * omnimeter IRQ_GPIO23 46805c45ca9SRussell King * pfs168 IRQ_GPIO_UCB1300_IRQ 46905c45ca9SRussell King * simpad IRQ_GPIO_UCB1300_IRQ 47005c45ca9SRussell King * shannon SHANNON_IRQ_GPIO_IRQ_CODEC 47105c45ca9SRussell King * yopy IRQ_GPIO_UCB1200_IRQ 47205c45ca9SRussell King */ 47305c45ca9SRussell King static int ucb1x00_detect_irq(struct ucb1x00 *ucb) 47405c45ca9SRussell King { 47505c45ca9SRussell King unsigned long mask; 47605c45ca9SRussell King 47705c45ca9SRussell King mask = probe_irq_on(); 478cfc73656SIngo Molnar if (!mask) { 479cfc73656SIngo Molnar probe_irq_off(mask); 48005c45ca9SRussell King return NO_IRQ; 481cfc73656SIngo Molnar } 48205c45ca9SRussell King 48305c45ca9SRussell King /* 48405c45ca9SRussell King * Enable the ADC interrupt. 48505c45ca9SRussell King */ 48605c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC); 48705c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC); 48805c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff); 48905c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 49005c45ca9SRussell King 49105c45ca9SRussell King /* 49205c45ca9SRussell King * Cause an ADC interrupt. 49305c45ca9SRussell King */ 49405c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA); 49505c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START); 49605c45ca9SRussell King 49705c45ca9SRussell King /* 49805c45ca9SRussell King * Wait for the conversion to complete. 49905c45ca9SRussell King */ 50005c45ca9SRussell King while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0); 50105c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, 0); 50205c45ca9SRussell King 50305c45ca9SRussell King /* 50405c45ca9SRussell King * Disable and clear interrupt. 50505c45ca9SRussell King */ 50605c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, 0); 50705c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, 0); 50805c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff); 50905c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 51005c45ca9SRussell King 51105c45ca9SRussell King /* 51205c45ca9SRussell King * Read triggered interrupt. 51305c45ca9SRussell King */ 51405c45ca9SRussell King return probe_irq_off(mask); 51505c45ca9SRussell King } 51605c45ca9SRussell King 5170c55445fSTony Jones static void ucb1x00_release(struct device *dev) 518585f5457SNicolas Pitre { 519585f5457SNicolas Pitre struct ucb1x00 *ucb = classdev_to_ucb1x00(dev); 520585f5457SNicolas Pitre kfree(ucb); 521585f5457SNicolas Pitre } 522585f5457SNicolas Pitre 523585f5457SNicolas Pitre static struct class ucb1x00_class = { 524585f5457SNicolas Pitre .name = "ucb1x00", 5250c55445fSTony Jones .dev_release = ucb1x00_release, 526585f5457SNicolas Pitre }; 527585f5457SNicolas Pitre 52805c45ca9SRussell King static int ucb1x00_probe(struct mcp *mcp) 52905c45ca9SRussell King { 53005c45ca9SRussell King struct ucb1x00 *ucb; 53105c45ca9SRussell King struct ucb1x00_driver *drv; 53205c45ca9SRussell King unsigned int id; 53305c45ca9SRussell King int ret = -ENODEV; 5349ca3dc80SThomas Kunze int temp; 53505c45ca9SRussell King 53605c45ca9SRussell King mcp_enable(mcp); 53705c45ca9SRussell King id = mcp_reg_read(mcp, UCB_ID); 53805c45ca9SRussell King 539b94ea6c0SPavel Machek if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) { 54005c45ca9SRussell King printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id); 54105c45ca9SRussell King goto err_disable; 54205c45ca9SRussell King } 54305c45ca9SRussell King 544dd00cc48SYoann Padioleau ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL); 54505c45ca9SRussell King ret = -ENOMEM; 54605c45ca9SRussell King if (!ucb) 54705c45ca9SRussell King goto err_disable; 54805c45ca9SRussell King 54905c45ca9SRussell King 5500c55445fSTony Jones ucb->dev.class = &ucb1x00_class; 5510c55445fSTony Jones ucb->dev.parent = &mcp->attached_device; 552b2bf61f2SKay Sievers dev_set_name(&ucb->dev, "ucb1x00"); 55305c45ca9SRussell King 55405c45ca9SRussell King spin_lock_init(&ucb->lock); 55505c45ca9SRussell King spin_lock_init(&ucb->io_lock); 55605c45ca9SRussell King sema_init(&ucb->adc_sem, 1); 55705c45ca9SRussell King 55805c45ca9SRussell King ucb->id = id; 55905c45ca9SRussell King ucb->mcp = mcp; 56005c45ca9SRussell King ucb->irq = ucb1x00_detect_irq(ucb); 56105c45ca9SRussell King if (ucb->irq == NO_IRQ) { 56205c45ca9SRussell King printk(KERN_ERR "UCB1x00: IRQ probe failed\n"); 56305c45ca9SRussell King ret = -ENODEV; 56405c45ca9SRussell King goto err_free; 56505c45ca9SRussell King } 56605c45ca9SRussell King 5679ca3dc80SThomas Kunze ucb->gpio.base = -1; 5689ca3dc80SThomas Kunze if (mcp->gpio_base != 0) { 5699ca3dc80SThomas Kunze ucb->gpio.label = dev_name(&ucb->dev); 5709ca3dc80SThomas Kunze ucb->gpio.base = mcp->gpio_base; 5719ca3dc80SThomas Kunze ucb->gpio.ngpio = 10; 5729ca3dc80SThomas Kunze ucb->gpio.set = ucb1x00_gpio_set; 5739ca3dc80SThomas Kunze ucb->gpio.get = ucb1x00_gpio_get; 5749ca3dc80SThomas Kunze ucb->gpio.direction_input = ucb1x00_gpio_direction_input; 5759ca3dc80SThomas Kunze ucb->gpio.direction_output = ucb1x00_gpio_direction_output; 5769ca3dc80SThomas Kunze ret = gpiochip_add(&ucb->gpio); 5779ca3dc80SThomas Kunze if (ret) 5789ca3dc80SThomas Kunze goto err_free; 5799ca3dc80SThomas Kunze } else 5809ca3dc80SThomas Kunze dev_info(&ucb->dev, "gpio_base not set so no gpiolib support"); 5819ca3dc80SThomas Kunze 582dace1453SThomas Gleixner ret = request_irq(ucb->irq, ucb1x00_irq, IRQF_TRIGGER_RISING, 5839ded96f2SRussell King "UCB1x00", ucb); 58405c45ca9SRussell King if (ret) { 58505c45ca9SRussell King printk(KERN_ERR "ucb1x00: unable to grab irq%d: %d\n", 58605c45ca9SRussell King ucb->irq, ret); 5879ca3dc80SThomas Kunze goto err_gpio; 58805c45ca9SRussell King } 58905c45ca9SRussell King 59005c45ca9SRussell King mcp_set_drvdata(mcp, ucb); 59105c45ca9SRussell King 5920c55445fSTony Jones ret = device_register(&ucb->dev); 59305c45ca9SRussell King if (ret) 59405c45ca9SRussell King goto err_irq; 59505c45ca9SRussell King 5969ca3dc80SThomas Kunze 59705c45ca9SRussell King INIT_LIST_HEAD(&ucb->devs); 598a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 59905c45ca9SRussell King list_add(&ucb->node, &ucb1x00_devices); 60005c45ca9SRussell King list_for_each_entry(drv, &ucb1x00_drivers, node) { 60105c45ca9SRussell King ucb1x00_add_dev(ucb, drv); 60205c45ca9SRussell King } 603a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 6049ca3dc80SThomas Kunze 60505c45ca9SRussell King goto out; 60605c45ca9SRussell King 60705c45ca9SRussell King err_irq: 60805c45ca9SRussell King free_irq(ucb->irq, ucb); 6099ca3dc80SThomas Kunze err_gpio: 6109ca3dc80SThomas Kunze if (ucb->gpio.base != -1) 6119ca3dc80SThomas Kunze temp = gpiochip_remove(&ucb->gpio); 61205c45ca9SRussell King err_free: 61305c45ca9SRussell King kfree(ucb); 61405c45ca9SRussell King err_disable: 61505c45ca9SRussell King mcp_disable(mcp); 61605c45ca9SRussell King out: 61705c45ca9SRussell King return ret; 61805c45ca9SRussell King } 61905c45ca9SRussell King 62005c45ca9SRussell King static void ucb1x00_remove(struct mcp *mcp) 62105c45ca9SRussell King { 62205c45ca9SRussell King struct ucb1x00 *ucb = mcp_get_drvdata(mcp); 62305c45ca9SRussell King struct list_head *l, *n; 6249ca3dc80SThomas Kunze int ret; 62505c45ca9SRussell King 626a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 62705c45ca9SRussell King list_del(&ucb->node); 62805c45ca9SRussell King list_for_each_safe(l, n, &ucb->devs) { 62905c45ca9SRussell King struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node); 63005c45ca9SRussell King ucb1x00_remove_dev(dev); 63105c45ca9SRussell King } 632a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 63305c45ca9SRussell King 6349ca3dc80SThomas Kunze if (ucb->gpio.base != -1) { 6359ca3dc80SThomas Kunze ret = gpiochip_remove(&ucb->gpio); 6369ca3dc80SThomas Kunze if (ret) 6379ca3dc80SThomas Kunze dev_err(&ucb->dev, "Can't remove gpio chip: %d\n", ret); 6389ca3dc80SThomas Kunze } 6399ca3dc80SThomas Kunze 64005c45ca9SRussell King free_irq(ucb->irq, ucb); 6410c55445fSTony Jones device_unregister(&ucb->dev); 64205c45ca9SRussell King } 64305c45ca9SRussell King 64405c45ca9SRussell King int ucb1x00_register_driver(struct ucb1x00_driver *drv) 64505c45ca9SRussell King { 64605c45ca9SRussell King struct ucb1x00 *ucb; 64705c45ca9SRussell King 64805c45ca9SRussell King INIT_LIST_HEAD(&drv->devs); 649a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 65005c45ca9SRussell King list_add(&drv->node, &ucb1x00_drivers); 65105c45ca9SRussell King list_for_each_entry(ucb, &ucb1x00_devices, node) { 65205c45ca9SRussell King ucb1x00_add_dev(ucb, drv); 65305c45ca9SRussell King } 654a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 65505c45ca9SRussell King return 0; 65605c45ca9SRussell King } 65705c45ca9SRussell King 65805c45ca9SRussell King void ucb1x00_unregister_driver(struct ucb1x00_driver *drv) 65905c45ca9SRussell King { 66005c45ca9SRussell King struct list_head *n, *l; 66105c45ca9SRussell King 662a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 66305c45ca9SRussell King list_del(&drv->node); 66405c45ca9SRussell King list_for_each_safe(l, n, &drv->devs) { 66505c45ca9SRussell King struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node); 66605c45ca9SRussell King ucb1x00_remove_dev(dev); 66705c45ca9SRussell King } 668a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 66905c45ca9SRussell King } 67005c45ca9SRussell King 67105c45ca9SRussell King static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state) 67205c45ca9SRussell King { 67305c45ca9SRussell King struct ucb1x00 *ucb = mcp_get_drvdata(mcp); 67405c45ca9SRussell King struct ucb1x00_dev *dev; 67505c45ca9SRussell King 676a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 67705c45ca9SRussell King list_for_each_entry(dev, &ucb->devs, dev_node) { 67805c45ca9SRussell King if (dev->drv->suspend) 67905c45ca9SRussell King dev->drv->suspend(dev, state); 68005c45ca9SRussell King } 681a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 68205c45ca9SRussell King return 0; 68305c45ca9SRussell King } 68405c45ca9SRussell King 68505c45ca9SRussell King static int ucb1x00_resume(struct mcp *mcp) 68605c45ca9SRussell King { 68705c45ca9SRussell King struct ucb1x00 *ucb = mcp_get_drvdata(mcp); 68805c45ca9SRussell King struct ucb1x00_dev *dev; 68905c45ca9SRussell King 6909ca3dc80SThomas Kunze ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 691a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 69205c45ca9SRussell King list_for_each_entry(dev, &ucb->devs, dev_node) { 69305c45ca9SRussell King if (dev->drv->resume) 69405c45ca9SRussell King dev->drv->resume(dev); 69505c45ca9SRussell King } 696a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 69705c45ca9SRussell King return 0; 69805c45ca9SRussell King } 69905c45ca9SRussell King 70005c45ca9SRussell King static struct mcp_driver ucb1x00_driver = { 70105c45ca9SRussell King .drv = { 70205c45ca9SRussell King .name = "ucb1x00", 70305c45ca9SRussell King }, 70405c45ca9SRussell King .probe = ucb1x00_probe, 70505c45ca9SRussell King .remove = ucb1x00_remove, 70605c45ca9SRussell King .suspend = ucb1x00_suspend, 70705c45ca9SRussell King .resume = ucb1x00_resume, 70805c45ca9SRussell King }; 70905c45ca9SRussell King 71005c45ca9SRussell King static int __init ucb1x00_init(void) 71105c45ca9SRussell King { 71205c45ca9SRussell King int ret = class_register(&ucb1x00_class); 71305c45ca9SRussell King if (ret == 0) { 71405c45ca9SRussell King ret = mcp_driver_register(&ucb1x00_driver); 71505c45ca9SRussell King if (ret) 71605c45ca9SRussell King class_unregister(&ucb1x00_class); 71705c45ca9SRussell King } 71805c45ca9SRussell King return ret; 71905c45ca9SRussell King } 72005c45ca9SRussell King 72105c45ca9SRussell King static void __exit ucb1x00_exit(void) 72205c45ca9SRussell King { 72305c45ca9SRussell King mcp_driver_unregister(&ucb1x00_driver); 72405c45ca9SRussell King class_unregister(&ucb1x00_class); 72505c45ca9SRussell King } 72605c45ca9SRussell King 72705c45ca9SRussell King module_init(ucb1x00_init); 72805c45ca9SRussell King module_exit(ucb1x00_exit); 72905c45ca9SRussell King 73005c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_io_set_dir); 73105c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_io_write); 73205c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_io_read); 73305c45ca9SRussell King 73405c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_adc_enable); 73505c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_adc_read); 73605c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_adc_disable); 73705c45ca9SRussell King 73805c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_hook_irq); 73905c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_free_irq); 74005c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_enable_irq); 74105c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_disable_irq); 74205c45ca9SRussell King 74305c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_register_driver); 74405c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_unregister_driver); 74505c45ca9SRussell King 74605c45ca9SRussell King MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); 74705c45ca9SRussell King MODULE_DESCRIPTION("UCB1x00 core driver"); 74805c45ca9SRussell King MODULE_LICENSE("GPL"); 749