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; 151c23bb602SRussell King unsigned old, mask = 1 << offset; 1529ca3dc80SThomas Kunze 1539ca3dc80SThomas Kunze spin_lock_irqsave(&ucb->io_lock, flags); 154c23bb602SRussell King old = ucb->io_out; 1559ca3dc80SThomas Kunze if (value) 156c23bb602SRussell King ucb->io_out |= mask; 1579ca3dc80SThomas Kunze else 158c23bb602SRussell King ucb->io_out &= ~mask; 159c23bb602SRussell King 160c23bb602SRussell King if (old != ucb->io_out) 1619ca3dc80SThomas Kunze ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 162c23bb602SRussell King 163c23bb602SRussell King if (!(ucb->io_dir & mask)) { 164c23bb602SRussell King ucb->io_dir |= mask; 165c23bb602SRussell King ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 166c23bb602SRussell King } 1679ca3dc80SThomas Kunze spin_unlock_irqrestore(&ucb->io_lock, flags); 1689ca3dc80SThomas Kunze 1699ca3dc80SThomas Kunze return 0; 1709ca3dc80SThomas Kunze } 1719ca3dc80SThomas Kunze 17205c45ca9SRussell King /* 17305c45ca9SRussell King * UCB1300 data sheet says we must: 17405c45ca9SRussell King * 1. enable ADC => 5us (including reference startup time) 17505c45ca9SRussell King * 2. select input => 51*tsibclk => 4.3us 17605c45ca9SRussell King * 3. start conversion => 102*tsibclk => 8.5us 17705c45ca9SRussell King * (tsibclk = 1/11981000) 17805c45ca9SRussell King * Period between SIB 128-bit frames = 10.7us 17905c45ca9SRussell King */ 18005c45ca9SRussell King 18105c45ca9SRussell King /** 18205c45ca9SRussell King * ucb1x00_adc_enable - enable the ADC converter 18305c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 18405c45ca9SRussell King * 18505c45ca9SRussell King * Enable the ucb1x00 and ADC converter on the UCB1x00 for use. 18605c45ca9SRussell King * Any code wishing to use the ADC converter must call this 18705c45ca9SRussell King * function prior to using it. 18805c45ca9SRussell King * 18905c45ca9SRussell King * This function takes the ADC semaphore to prevent two or more 19005c45ca9SRussell King * concurrent uses, and therefore may sleep. As a result, it 19105c45ca9SRussell King * can only be called from process context, not interrupt 19205c45ca9SRussell King * context. 19305c45ca9SRussell King * 19405c45ca9SRussell King * You should release the ADC as soon as possible using 19505c45ca9SRussell King * ucb1x00_adc_disable. 19605c45ca9SRussell King */ 19705c45ca9SRussell King void ucb1x00_adc_enable(struct ucb1x00 *ucb) 19805c45ca9SRussell King { 19905c45ca9SRussell King down(&ucb->adc_sem); 20005c45ca9SRussell King 20105c45ca9SRussell King ucb->adc_cr |= UCB_ADC_ENA; 20205c45ca9SRussell King 20305c45ca9SRussell King ucb1x00_enable(ucb); 20405c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr); 20505c45ca9SRussell King } 20605c45ca9SRussell King 20705c45ca9SRussell King /** 20805c45ca9SRussell King * ucb1x00_adc_read - read the specified ADC channel 20905c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 21005c45ca9SRussell King * @adc_channel: ADC channel mask 21105c45ca9SRussell King * @sync: wait for syncronisation pulse. 21205c45ca9SRussell King * 21305c45ca9SRussell King * Start an ADC conversion and wait for the result. Note that 21405c45ca9SRussell King * synchronised ADC conversions (via the ADCSYNC pin) must wait 21505c45ca9SRussell King * until the trigger is asserted and the conversion is finished. 21605c45ca9SRussell King * 21705c45ca9SRussell King * This function currently spins waiting for the conversion to 21805c45ca9SRussell King * complete (2 frames max without sync). 21905c45ca9SRussell King * 22005c45ca9SRussell King * If called for a synchronised ADC conversion, it may sleep 22105c45ca9SRussell King * with the ADC semaphore held. 22205c45ca9SRussell King */ 22305c45ca9SRussell King unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync) 22405c45ca9SRussell King { 22505c45ca9SRussell King unsigned int val; 22605c45ca9SRussell King 22705c45ca9SRussell King if (sync) 22805c45ca9SRussell King adc_channel |= UCB_ADC_SYNC_ENA; 22905c45ca9SRussell King 23005c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel); 23105c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START); 23205c45ca9SRussell King 23305c45ca9SRussell King for (;;) { 23405c45ca9SRussell King val = ucb1x00_reg_read(ucb, UCB_ADC_DATA); 23505c45ca9SRussell King if (val & UCB_ADC_DAT_VAL) 23605c45ca9SRussell King break; 23705c45ca9SRussell King /* yield to other processes */ 23805c45ca9SRussell King set_current_state(TASK_INTERRUPTIBLE); 23905c45ca9SRussell King schedule_timeout(1); 24005c45ca9SRussell King } 24105c45ca9SRussell King 24205c45ca9SRussell King return UCB_ADC_DAT(val); 24305c45ca9SRussell King } 24405c45ca9SRussell King 24505c45ca9SRussell King /** 24605c45ca9SRussell King * ucb1x00_adc_disable - disable the ADC converter 24705c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 24805c45ca9SRussell King * 24905c45ca9SRussell King * Disable the ADC converter and release the ADC semaphore. 25005c45ca9SRussell King */ 25105c45ca9SRussell King void ucb1x00_adc_disable(struct ucb1x00 *ucb) 25205c45ca9SRussell King { 25305c45ca9SRussell King ucb->adc_cr &= ~UCB_ADC_ENA; 25405c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr); 25505c45ca9SRussell King ucb1x00_disable(ucb); 25605c45ca9SRussell King 25705c45ca9SRussell King up(&ucb->adc_sem); 25805c45ca9SRussell King } 25905c45ca9SRussell King 26005c45ca9SRussell King /* 26105c45ca9SRussell King * UCB1x00 Interrupt handling. 26205c45ca9SRussell King * 26305c45ca9SRussell King * The UCB1x00 can generate interrupts when the SIBCLK is stopped. 26405c45ca9SRussell King * Since we need to read an internal register, we must re-enable 26505c45ca9SRussell King * SIBCLK to talk to the chip. We leave the clock running until 26605c45ca9SRussell King * we have finished processing all interrupts from the chip. 26705c45ca9SRussell King */ 2687d12e780SDavid Howells static irqreturn_t ucb1x00_irq(int irqnr, void *devid) 26905c45ca9SRussell King { 27005c45ca9SRussell King struct ucb1x00 *ucb = devid; 27105c45ca9SRussell King struct ucb1x00_irq *irq; 27205c45ca9SRussell King unsigned int isr, i; 27305c45ca9SRussell King 27405c45ca9SRussell King ucb1x00_enable(ucb); 27505c45ca9SRussell King isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS); 27605c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr); 27705c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 27805c45ca9SRussell King 27905c45ca9SRussell King for (i = 0, irq = ucb->irq_handler; i < 16 && isr; i++, isr >>= 1, irq++) 28005c45ca9SRussell King if (isr & 1 && irq->fn) 28105c45ca9SRussell King irq->fn(i, irq->devid); 28205c45ca9SRussell King ucb1x00_disable(ucb); 28305c45ca9SRussell King 28405c45ca9SRussell King return IRQ_HANDLED; 28505c45ca9SRussell King } 28605c45ca9SRussell King 28705c45ca9SRussell King /** 28805c45ca9SRussell King * ucb1x00_hook_irq - hook a UCB1x00 interrupt 28905c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 29005c45ca9SRussell King * @idx: interrupt index 29105c45ca9SRussell King * @fn: function to call when interrupt is triggered 29205c45ca9SRussell King * @devid: device id to pass to interrupt handler 29305c45ca9SRussell King * 29405c45ca9SRussell King * Hook the specified interrupt. You can only register one handler 29505c45ca9SRussell King * for each interrupt source. The interrupt source is not enabled 29605c45ca9SRussell King * by this function; use ucb1x00_enable_irq instead. 29705c45ca9SRussell King * 29805c45ca9SRussell King * Interrupt handlers will be called with other interrupts enabled. 29905c45ca9SRussell King * 30005c45ca9SRussell King * Returns zero on success, or one of the following errors: 30105c45ca9SRussell King * -EINVAL if the interrupt index is invalid 30205c45ca9SRussell King * -EBUSY if the interrupt has already been hooked 30305c45ca9SRussell King */ 30405c45ca9SRussell King int ucb1x00_hook_irq(struct ucb1x00 *ucb, unsigned int idx, void (*fn)(int, void *), void *devid) 30505c45ca9SRussell King { 30605c45ca9SRussell King struct ucb1x00_irq *irq; 30705c45ca9SRussell King int ret = -EINVAL; 30805c45ca9SRussell King 30905c45ca9SRussell King if (idx < 16) { 31005c45ca9SRussell King irq = ucb->irq_handler + idx; 31105c45ca9SRussell King ret = -EBUSY; 31205c45ca9SRussell King 31305c45ca9SRussell King spin_lock_irq(&ucb->lock); 31405c45ca9SRussell King if (irq->fn == NULL) { 31505c45ca9SRussell King irq->devid = devid; 31605c45ca9SRussell King irq->fn = fn; 31705c45ca9SRussell King ret = 0; 31805c45ca9SRussell King } 31905c45ca9SRussell King spin_unlock_irq(&ucb->lock); 32005c45ca9SRussell King } 32105c45ca9SRussell King return ret; 32205c45ca9SRussell King } 32305c45ca9SRussell King 32405c45ca9SRussell King /** 32505c45ca9SRussell King * ucb1x00_enable_irq - enable an UCB1x00 interrupt source 32605c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 32705c45ca9SRussell King * @idx: interrupt index 32805c45ca9SRussell King * @edges: interrupt edges to enable 32905c45ca9SRussell King * 33005c45ca9SRussell King * Enable the specified interrupt to trigger on %UCB_RISING, 33105c45ca9SRussell King * %UCB_FALLING or both edges. The interrupt should have been 33205c45ca9SRussell King * hooked by ucb1x00_hook_irq. 33305c45ca9SRussell King */ 33405c45ca9SRussell King void ucb1x00_enable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges) 33505c45ca9SRussell King { 33605c45ca9SRussell King unsigned long flags; 33705c45ca9SRussell King 33805c45ca9SRussell King if (idx < 16) { 33905c45ca9SRussell King spin_lock_irqsave(&ucb->lock, flags); 34005c45ca9SRussell King 34105c45ca9SRussell King ucb1x00_enable(ucb); 34205c45ca9SRussell King if (edges & UCB_RISING) { 34305c45ca9SRussell King ucb->irq_ris_enbl |= 1 << idx; 34405c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); 34505c45ca9SRussell King } 34605c45ca9SRussell King if (edges & UCB_FALLING) { 34705c45ca9SRussell King ucb->irq_fal_enbl |= 1 << idx; 34805c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); 34905c45ca9SRussell King } 35005c45ca9SRussell King ucb1x00_disable(ucb); 35105c45ca9SRussell King spin_unlock_irqrestore(&ucb->lock, flags); 35205c45ca9SRussell King } 35305c45ca9SRussell King } 35405c45ca9SRussell King 35505c45ca9SRussell King /** 35605c45ca9SRussell King * ucb1x00_disable_irq - disable an UCB1x00 interrupt source 35705c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 35805c45ca9SRussell King * @edges: interrupt edges to disable 35905c45ca9SRussell King * 36005c45ca9SRussell King * Disable the specified interrupt triggering on the specified 36105c45ca9SRussell King * (%UCB_RISING, %UCB_FALLING or both) edges. 36205c45ca9SRussell King */ 36305c45ca9SRussell King void ucb1x00_disable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges) 36405c45ca9SRussell King { 36505c45ca9SRussell King unsigned long flags; 36605c45ca9SRussell King 36705c45ca9SRussell King if (idx < 16) { 36805c45ca9SRussell King spin_lock_irqsave(&ucb->lock, flags); 36905c45ca9SRussell King 37005c45ca9SRussell King ucb1x00_enable(ucb); 37105c45ca9SRussell King if (edges & UCB_RISING) { 37205c45ca9SRussell King ucb->irq_ris_enbl &= ~(1 << idx); 37305c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); 37405c45ca9SRussell King } 37505c45ca9SRussell King if (edges & UCB_FALLING) { 37605c45ca9SRussell King ucb->irq_fal_enbl &= ~(1 << idx); 37705c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); 37805c45ca9SRussell King } 37905c45ca9SRussell King ucb1x00_disable(ucb); 38005c45ca9SRussell King spin_unlock_irqrestore(&ucb->lock, flags); 38105c45ca9SRussell King } 38205c45ca9SRussell King } 38305c45ca9SRussell King 38405c45ca9SRussell King /** 38505c45ca9SRussell King * ucb1x00_free_irq - disable and free the specified UCB1x00 interrupt 38605c45ca9SRussell King * @ucb: UCB1x00 structure describing chip 38705c45ca9SRussell King * @idx: interrupt index 38805c45ca9SRussell King * @devid: device id. 38905c45ca9SRussell King * 39005c45ca9SRussell King * Disable the interrupt source and remove the handler. devid must 39105c45ca9SRussell King * match the devid passed when hooking the interrupt. 39205c45ca9SRussell King * 39305c45ca9SRussell King * Returns zero on success, or one of the following errors: 39405c45ca9SRussell King * -EINVAL if the interrupt index is invalid 39505c45ca9SRussell King * -ENOENT if devid does not match 39605c45ca9SRussell King */ 39705c45ca9SRussell King int ucb1x00_free_irq(struct ucb1x00 *ucb, unsigned int idx, void *devid) 39805c45ca9SRussell King { 39905c45ca9SRussell King struct ucb1x00_irq *irq; 40005c45ca9SRussell King int ret; 40105c45ca9SRussell King 40205c45ca9SRussell King if (idx >= 16) 40305c45ca9SRussell King goto bad; 40405c45ca9SRussell King 40505c45ca9SRussell King irq = ucb->irq_handler + idx; 40605c45ca9SRussell King ret = -ENOENT; 40705c45ca9SRussell King 40805c45ca9SRussell King spin_lock_irq(&ucb->lock); 40905c45ca9SRussell King if (irq->devid == devid) { 41005c45ca9SRussell King ucb->irq_ris_enbl &= ~(1 << idx); 41105c45ca9SRussell King ucb->irq_fal_enbl &= ~(1 << idx); 41205c45ca9SRussell King 41305c45ca9SRussell King ucb1x00_enable(ucb); 41405c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); 41505c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); 41605c45ca9SRussell King ucb1x00_disable(ucb); 41705c45ca9SRussell King 41805c45ca9SRussell King irq->fn = NULL; 41905c45ca9SRussell King irq->devid = NULL; 42005c45ca9SRussell King ret = 0; 42105c45ca9SRussell King } 42205c45ca9SRussell King spin_unlock_irq(&ucb->lock); 42305c45ca9SRussell King return ret; 42405c45ca9SRussell King 42505c45ca9SRussell King bad: 42605c45ca9SRussell King printk(KERN_ERR "Freeing bad UCB1x00 irq %d\n", idx); 42705c45ca9SRussell King return -EINVAL; 42805c45ca9SRussell King } 42905c45ca9SRussell King 43005c45ca9SRussell King static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv) 43105c45ca9SRussell King { 43205c45ca9SRussell King struct ucb1x00_dev *dev; 43305c45ca9SRussell King int ret = -ENOMEM; 43405c45ca9SRussell King 43505c45ca9SRussell King dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL); 43605c45ca9SRussell King if (dev) { 43705c45ca9SRussell King dev->ucb = ucb; 43805c45ca9SRussell King dev->drv = drv; 43905c45ca9SRussell King 44005c45ca9SRussell King ret = drv->add(dev); 44105c45ca9SRussell King 44205c45ca9SRussell King if (ret == 0) { 44305c45ca9SRussell King list_add(&dev->dev_node, &ucb->devs); 44405c45ca9SRussell King list_add(&dev->drv_node, &drv->devs); 44505c45ca9SRussell King } else { 44605c45ca9SRussell King kfree(dev); 44705c45ca9SRussell King } 44805c45ca9SRussell King } 44905c45ca9SRussell King return ret; 45005c45ca9SRussell King } 45105c45ca9SRussell King 45205c45ca9SRussell King static void ucb1x00_remove_dev(struct ucb1x00_dev *dev) 45305c45ca9SRussell King { 45405c45ca9SRussell King dev->drv->remove(dev); 45505c45ca9SRussell King list_del(&dev->dev_node); 45605c45ca9SRussell King list_del(&dev->drv_node); 45705c45ca9SRussell King kfree(dev); 45805c45ca9SRussell King } 45905c45ca9SRussell King 46005c45ca9SRussell King /* 46105c45ca9SRussell King * Try to probe our interrupt, rather than relying on lots of 46205c45ca9SRussell King * hard-coded machine dependencies. For reference, the expected 46305c45ca9SRussell King * IRQ mappings are: 46405c45ca9SRussell King * 46505c45ca9SRussell King * Machine Default IRQ 46605c45ca9SRussell King * adsbitsy IRQ_GPCIN4 46705c45ca9SRussell King * cerf IRQ_GPIO_UCB1200_IRQ 46805c45ca9SRussell King * flexanet IRQ_GPIO_GUI 46905c45ca9SRussell King * freebird IRQ_GPIO_FREEBIRD_UCB1300_IRQ 47005c45ca9SRussell King * graphicsclient ADS_EXT_IRQ(8) 47105c45ca9SRussell King * graphicsmaster ADS_EXT_IRQ(8) 47205c45ca9SRussell King * lart LART_IRQ_UCB1200 47305c45ca9SRussell King * omnimeter IRQ_GPIO23 47405c45ca9SRussell King * pfs168 IRQ_GPIO_UCB1300_IRQ 47505c45ca9SRussell King * simpad IRQ_GPIO_UCB1300_IRQ 47605c45ca9SRussell King * shannon SHANNON_IRQ_GPIO_IRQ_CODEC 47705c45ca9SRussell King * yopy IRQ_GPIO_UCB1200_IRQ 47805c45ca9SRussell King */ 47905c45ca9SRussell King static int ucb1x00_detect_irq(struct ucb1x00 *ucb) 48005c45ca9SRussell King { 48105c45ca9SRussell King unsigned long mask; 48205c45ca9SRussell King 48305c45ca9SRussell King mask = probe_irq_on(); 484cfc73656SIngo Molnar if (!mask) { 485cfc73656SIngo Molnar probe_irq_off(mask); 48605c45ca9SRussell King return NO_IRQ; 487cfc73656SIngo Molnar } 48805c45ca9SRussell King 48905c45ca9SRussell King /* 49005c45ca9SRussell King * Enable the ADC interrupt. 49105c45ca9SRussell King */ 49205c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC); 49305c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC); 49405c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff); 49505c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 49605c45ca9SRussell King 49705c45ca9SRussell King /* 49805c45ca9SRussell King * Cause an ADC interrupt. 49905c45ca9SRussell King */ 50005c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA); 50105c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START); 50205c45ca9SRussell King 50305c45ca9SRussell King /* 50405c45ca9SRussell King * Wait for the conversion to complete. 50505c45ca9SRussell King */ 50605c45ca9SRussell King while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0); 50705c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_ADC_CR, 0); 50805c45ca9SRussell King 50905c45ca9SRussell King /* 51005c45ca9SRussell King * Disable and clear interrupt. 51105c45ca9SRussell King */ 51205c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_RIS, 0); 51305c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_FAL, 0); 51405c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff); 51505c45ca9SRussell King ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 51605c45ca9SRussell King 51705c45ca9SRussell King /* 51805c45ca9SRussell King * Read triggered interrupt. 51905c45ca9SRussell King */ 52005c45ca9SRussell King return probe_irq_off(mask); 52105c45ca9SRussell King } 52205c45ca9SRussell King 5230c55445fSTony Jones static void ucb1x00_release(struct device *dev) 524585f5457SNicolas Pitre { 525585f5457SNicolas Pitre struct ucb1x00 *ucb = classdev_to_ucb1x00(dev); 526585f5457SNicolas Pitre kfree(ucb); 527585f5457SNicolas Pitre } 528585f5457SNicolas Pitre 529585f5457SNicolas Pitre static struct class ucb1x00_class = { 530585f5457SNicolas Pitre .name = "ucb1x00", 5310c55445fSTony Jones .dev_release = ucb1x00_release, 532585f5457SNicolas Pitre }; 533585f5457SNicolas Pitre 53405c45ca9SRussell King static int ucb1x00_probe(struct mcp *mcp) 53505c45ca9SRussell King { 53605c45ca9SRussell King struct ucb1x00 *ucb; 53705c45ca9SRussell King struct ucb1x00_driver *drv; 53805c45ca9SRussell King unsigned int id; 53905c45ca9SRussell King int ret = -ENODEV; 5409ca3dc80SThomas Kunze int temp; 54105c45ca9SRussell King 54205c45ca9SRussell King mcp_enable(mcp); 54305c45ca9SRussell King id = mcp_reg_read(mcp, UCB_ID); 54405c45ca9SRussell King 54565f2e753SRussell King if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) { 54665f2e753SRussell King printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id); 54705c45ca9SRussell King goto err_disable; 54805c45ca9SRussell King } 54905c45ca9SRussell King 550dd00cc48SYoann Padioleau ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL); 55105c45ca9SRussell King ret = -ENOMEM; 55205c45ca9SRussell King if (!ucb) 55305c45ca9SRussell King goto err_disable; 55405c45ca9SRussell King 55565f2e753SRussell King 5560c55445fSTony Jones ucb->dev.class = &ucb1x00_class; 5570c55445fSTony Jones ucb->dev.parent = &mcp->attached_device; 55865f2e753SRussell King dev_set_name(&ucb->dev, "ucb1x00"); 55905c45ca9SRussell King 56005c45ca9SRussell King spin_lock_init(&ucb->lock); 56105c45ca9SRussell King spin_lock_init(&ucb->io_lock); 56205c45ca9SRussell King sema_init(&ucb->adc_sem, 1); 56305c45ca9SRussell King 56465f2e753SRussell King ucb->id = id; 56505c45ca9SRussell King ucb->mcp = mcp; 56605c45ca9SRussell King ucb->irq = ucb1x00_detect_irq(ucb); 56705c45ca9SRussell King if (ucb->irq == NO_IRQ) { 56865f2e753SRussell King printk(KERN_ERR "UCB1x00: IRQ probe failed\n"); 56905c45ca9SRussell King ret = -ENODEV; 57005c45ca9SRussell King goto err_free; 57105c45ca9SRussell King } 57205c45ca9SRussell King 5739ca3dc80SThomas Kunze ucb->gpio.base = -1; 57465f2e753SRussell King if (mcp->gpio_base != 0) { 5759ca3dc80SThomas Kunze ucb->gpio.label = dev_name(&ucb->dev); 57665f2e753SRussell King ucb->gpio.base = mcp->gpio_base; 5779ca3dc80SThomas Kunze ucb->gpio.ngpio = 10; 5789ca3dc80SThomas Kunze ucb->gpio.set = ucb1x00_gpio_set; 5799ca3dc80SThomas Kunze ucb->gpio.get = ucb1x00_gpio_get; 5809ca3dc80SThomas Kunze ucb->gpio.direction_input = ucb1x00_gpio_direction_input; 5819ca3dc80SThomas Kunze ucb->gpio.direction_output = ucb1x00_gpio_direction_output; 5829ca3dc80SThomas Kunze ret = gpiochip_add(&ucb->gpio); 5839ca3dc80SThomas Kunze if (ret) 5849ca3dc80SThomas Kunze goto err_free; 5859ca3dc80SThomas Kunze } else 5869ca3dc80SThomas Kunze dev_info(&ucb->dev, "gpio_base not set so no gpiolib support"); 5879ca3dc80SThomas Kunze 588dace1453SThomas Gleixner ret = request_irq(ucb->irq, ucb1x00_irq, IRQF_TRIGGER_RISING, 58965f2e753SRussell King "UCB1x00", ucb); 59005c45ca9SRussell King if (ret) { 59165f2e753SRussell King printk(KERN_ERR "ucb1x00: unable to grab irq%d: %d\n", 59265f2e753SRussell King ucb->irq, ret); 5939ca3dc80SThomas Kunze goto err_gpio; 59405c45ca9SRussell King } 59505c45ca9SRussell King 59605c45ca9SRussell King mcp_set_drvdata(mcp, ucb); 59705c45ca9SRussell King 5980c55445fSTony Jones ret = device_register(&ucb->dev); 59905c45ca9SRussell King if (ret) 60005c45ca9SRussell King goto err_irq; 60105c45ca9SRussell King 6029ca3dc80SThomas Kunze 60305c45ca9SRussell King INIT_LIST_HEAD(&ucb->devs); 604a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 60505c45ca9SRussell King list_add(&ucb->node, &ucb1x00_devices); 60605c45ca9SRussell King list_for_each_entry(drv, &ucb1x00_drivers, node) { 60705c45ca9SRussell King ucb1x00_add_dev(ucb, drv); 60805c45ca9SRussell King } 609a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 6109ca3dc80SThomas Kunze 61105c45ca9SRussell King goto out; 61205c45ca9SRussell King 61305c45ca9SRussell King err_irq: 61405c45ca9SRussell King free_irq(ucb->irq, ucb); 6159ca3dc80SThomas Kunze err_gpio: 6169ca3dc80SThomas Kunze if (ucb->gpio.base != -1) 6179ca3dc80SThomas Kunze temp = gpiochip_remove(&ucb->gpio); 61805c45ca9SRussell King err_free: 61905c45ca9SRussell King kfree(ucb); 62005c45ca9SRussell King err_disable: 62105c45ca9SRussell King mcp_disable(mcp); 62205c45ca9SRussell King out: 62305c45ca9SRussell King return ret; 62405c45ca9SRussell King } 62505c45ca9SRussell King 62605c45ca9SRussell King static void ucb1x00_remove(struct mcp *mcp) 62705c45ca9SRussell King { 62805c45ca9SRussell King struct ucb1x00 *ucb = mcp_get_drvdata(mcp); 62905c45ca9SRussell King struct list_head *l, *n; 6309ca3dc80SThomas Kunze int ret; 63105c45ca9SRussell King 632a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 63305c45ca9SRussell King list_del(&ucb->node); 63405c45ca9SRussell King list_for_each_safe(l, n, &ucb->devs) { 63505c45ca9SRussell King struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node); 63605c45ca9SRussell King ucb1x00_remove_dev(dev); 63705c45ca9SRussell King } 638a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 63905c45ca9SRussell King 6409ca3dc80SThomas Kunze if (ucb->gpio.base != -1) { 6419ca3dc80SThomas Kunze ret = gpiochip_remove(&ucb->gpio); 6429ca3dc80SThomas Kunze if (ret) 6439ca3dc80SThomas Kunze dev_err(&ucb->dev, "Can't remove gpio chip: %d\n", ret); 6449ca3dc80SThomas Kunze } 6459ca3dc80SThomas Kunze 64605c45ca9SRussell King free_irq(ucb->irq, ucb); 6470c55445fSTony Jones device_unregister(&ucb->dev); 64805c45ca9SRussell King } 64905c45ca9SRussell King 65005c45ca9SRussell King int ucb1x00_register_driver(struct ucb1x00_driver *drv) 65105c45ca9SRussell King { 65205c45ca9SRussell King struct ucb1x00 *ucb; 65305c45ca9SRussell King 65405c45ca9SRussell King INIT_LIST_HEAD(&drv->devs); 655a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 65605c45ca9SRussell King list_add(&drv->node, &ucb1x00_drivers); 65705c45ca9SRussell King list_for_each_entry(ucb, &ucb1x00_devices, node) { 65805c45ca9SRussell King ucb1x00_add_dev(ucb, drv); 65905c45ca9SRussell King } 660a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 66105c45ca9SRussell King return 0; 66205c45ca9SRussell King } 66305c45ca9SRussell King 66405c45ca9SRussell King void ucb1x00_unregister_driver(struct ucb1x00_driver *drv) 66505c45ca9SRussell King { 66605c45ca9SRussell King struct list_head *n, *l; 66705c45ca9SRussell King 668a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 66905c45ca9SRussell King list_del(&drv->node); 67005c45ca9SRussell King list_for_each_safe(l, n, &drv->devs) { 67105c45ca9SRussell King struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node); 67205c45ca9SRussell King ucb1x00_remove_dev(dev); 67305c45ca9SRussell King } 674a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 67505c45ca9SRussell King } 67605c45ca9SRussell King 67705c45ca9SRussell King static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state) 67805c45ca9SRussell King { 67905c45ca9SRussell King struct ucb1x00 *ucb = mcp_get_drvdata(mcp); 68005c45ca9SRussell King struct ucb1x00_dev *dev; 68105c45ca9SRussell King 682a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 68305c45ca9SRussell King list_for_each_entry(dev, &ucb->devs, dev_node) { 68405c45ca9SRussell King if (dev->drv->suspend) 68505c45ca9SRussell King dev->drv->suspend(dev, state); 68605c45ca9SRussell King } 687a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 68805c45ca9SRussell King return 0; 68905c45ca9SRussell King } 69005c45ca9SRussell King 69105c45ca9SRussell King static int ucb1x00_resume(struct mcp *mcp) 69205c45ca9SRussell King { 69305c45ca9SRussell King struct ucb1x00 *ucb = mcp_get_drvdata(mcp); 69405c45ca9SRussell King struct ucb1x00_dev *dev; 69505c45ca9SRussell King 6962e95e51eSRussell King ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 6979ca3dc80SThomas Kunze ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 698a621aaedSArjan van de Ven mutex_lock(&ucb1x00_mutex); 69905c45ca9SRussell King list_for_each_entry(dev, &ucb->devs, dev_node) { 70005c45ca9SRussell King if (dev->drv->resume) 70105c45ca9SRussell King dev->drv->resume(dev); 70205c45ca9SRussell King } 703a621aaedSArjan van de Ven mutex_unlock(&ucb1x00_mutex); 70405c45ca9SRussell King return 0; 70505c45ca9SRussell King } 70605c45ca9SRussell King 70705c45ca9SRussell King static struct mcp_driver ucb1x00_driver = { 70805c45ca9SRussell King .drv = { 70905c45ca9SRussell King .name = "ucb1x00", 71005c45ca9SRussell King }, 71105c45ca9SRussell King .probe = ucb1x00_probe, 71205c45ca9SRussell King .remove = ucb1x00_remove, 71305c45ca9SRussell King .suspend = ucb1x00_suspend, 71405c45ca9SRussell King .resume = ucb1x00_resume, 71505c45ca9SRussell King }; 71605c45ca9SRussell King 71705c45ca9SRussell King static int __init ucb1x00_init(void) 71805c45ca9SRussell King { 71905c45ca9SRussell King int ret = class_register(&ucb1x00_class); 72005c45ca9SRussell King if (ret == 0) { 72105c45ca9SRussell King ret = mcp_driver_register(&ucb1x00_driver); 72205c45ca9SRussell King if (ret) 72305c45ca9SRussell King class_unregister(&ucb1x00_class); 72405c45ca9SRussell King } 72505c45ca9SRussell King return ret; 72605c45ca9SRussell King } 72705c45ca9SRussell King 72805c45ca9SRussell King static void __exit ucb1x00_exit(void) 72905c45ca9SRussell King { 73005c45ca9SRussell King mcp_driver_unregister(&ucb1x00_driver); 73105c45ca9SRussell King class_unregister(&ucb1x00_class); 73205c45ca9SRussell King } 73305c45ca9SRussell King 73405c45ca9SRussell King module_init(ucb1x00_init); 73505c45ca9SRussell King module_exit(ucb1x00_exit); 73605c45ca9SRussell King 73705c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_io_set_dir); 73805c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_io_write); 73905c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_io_read); 74005c45ca9SRussell King 74105c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_adc_enable); 74205c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_adc_read); 74305c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_adc_disable); 74405c45ca9SRussell King 74505c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_hook_irq); 74605c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_free_irq); 74705c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_enable_irq); 74805c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_disable_irq); 74905c45ca9SRussell King 75005c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_register_driver); 75105c45ca9SRussell King EXPORT_SYMBOL(ucb1x00_unregister_driver); 75205c45ca9SRussell King 75305c45ca9SRussell King MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); 75405c45ca9SRussell King MODULE_DESCRIPTION("UCB1x00 core driver"); 75505c45ca9SRussell King MODULE_LICENSE("GPL"); 756