18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/drivers/mfd/ucb1x00-core.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2001 Russell King, All Rights Reserved. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * The UCB1x00 core driver provides basic services for handling IO, 88c2ecf20Sopenharmony_ci * the ADC, interrupts, and accessing registers. It is designed 98c2ecf20Sopenharmony_ci * such that everything goes through this layer, thereby providing 108c2ecf20Sopenharmony_ci * a consistent locking methodology, as well as allowing the drivers 118c2ecf20Sopenharmony_ci * to be used on other non-MCP-enabled hardware platforms. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * Note that all locks are private to this file. Nothing else may 148c2ecf20Sopenharmony_ci * touch them. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/kernel.h> 188c2ecf20Sopenharmony_ci#include <linux/sched.h> 198c2ecf20Sopenharmony_ci#include <linux/slab.h> 208c2ecf20Sopenharmony_ci#include <linux/init.h> 218c2ecf20Sopenharmony_ci#include <linux/errno.h> 228c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 238c2ecf20Sopenharmony_ci#include <linux/irq.h> 248c2ecf20Sopenharmony_ci#include <linux/device.h> 258c2ecf20Sopenharmony_ci#include <linux/mutex.h> 268c2ecf20Sopenharmony_ci#include <linux/mfd/ucb1x00.h> 278c2ecf20Sopenharmony_ci#include <linux/pm.h> 288c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(ucb1x00_mutex); 318c2ecf20Sopenharmony_cistatic LIST_HEAD(ucb1x00_drivers); 328c2ecf20Sopenharmony_cistatic LIST_HEAD(ucb1x00_devices); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/** 358c2ecf20Sopenharmony_ci * ucb1x00_io_set_dir - set IO direction 368c2ecf20Sopenharmony_ci * @ucb: UCB1x00 structure describing chip 378c2ecf20Sopenharmony_ci * @in: bitfield of IO pins to be set as inputs 388c2ecf20Sopenharmony_ci * @out: bitfield of IO pins to be set as outputs 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * Set the IO direction of the ten general purpose IO pins on 418c2ecf20Sopenharmony_ci * the UCB1x00 chip. The @in bitfield has priority over the 428c2ecf20Sopenharmony_ci * @out bitfield, in that if you specify a pin as both input 438c2ecf20Sopenharmony_ci * and output, it will end up as an input. 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * ucb1x00_enable must have been called to enable the comms 468c2ecf20Sopenharmony_ci * before using this function. 478c2ecf20Sopenharmony_ci * 488c2ecf20Sopenharmony_ci * This function takes a spinlock, disabling interrupts. 498c2ecf20Sopenharmony_ci */ 508c2ecf20Sopenharmony_civoid ucb1x00_io_set_dir(struct ucb1x00 *ucb, unsigned int in, unsigned int out) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci unsigned long flags; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci spin_lock_irqsave(&ucb->io_lock, flags); 558c2ecf20Sopenharmony_ci ucb->io_dir |= out; 568c2ecf20Sopenharmony_ci ucb->io_dir &= ~in; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 598c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ucb->io_lock, flags); 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/** 638c2ecf20Sopenharmony_ci * ucb1x00_io_write - set or clear IO outputs 648c2ecf20Sopenharmony_ci * @ucb: UCB1x00 structure describing chip 658c2ecf20Sopenharmony_ci * @set: bitfield of IO pins to set to logic '1' 668c2ecf20Sopenharmony_ci * @clear: bitfield of IO pins to set to logic '0' 678c2ecf20Sopenharmony_ci * 688c2ecf20Sopenharmony_ci * Set the IO output state of the specified IO pins. The value 698c2ecf20Sopenharmony_ci * is retained if the pins are subsequently configured as inputs. 708c2ecf20Sopenharmony_ci * The @clear bitfield has priority over the @set bitfield - 718c2ecf20Sopenharmony_ci * outputs will be cleared. 728c2ecf20Sopenharmony_ci * 738c2ecf20Sopenharmony_ci * ucb1x00_enable must have been called to enable the comms 748c2ecf20Sopenharmony_ci * before using this function. 758c2ecf20Sopenharmony_ci * 768c2ecf20Sopenharmony_ci * This function takes a spinlock, disabling interrupts. 778c2ecf20Sopenharmony_ci */ 788c2ecf20Sopenharmony_civoid ucb1x00_io_write(struct ucb1x00 *ucb, unsigned int set, unsigned int clear) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci unsigned long flags; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci spin_lock_irqsave(&ucb->io_lock, flags); 838c2ecf20Sopenharmony_ci ucb->io_out |= set; 848c2ecf20Sopenharmony_ci ucb->io_out &= ~clear; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 878c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ucb->io_lock, flags); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/** 918c2ecf20Sopenharmony_ci * ucb1x00_io_read - read the current state of the IO pins 928c2ecf20Sopenharmony_ci * @ucb: UCB1x00 structure describing chip 938c2ecf20Sopenharmony_ci * 948c2ecf20Sopenharmony_ci * Return a bitfield describing the logic state of the ten 958c2ecf20Sopenharmony_ci * general purpose IO pins. 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * ucb1x00_enable must have been called to enable the comms 988c2ecf20Sopenharmony_ci * before using this function. 998c2ecf20Sopenharmony_ci * 1008c2ecf20Sopenharmony_ci * This function does not take any mutexes or spinlocks. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ciunsigned int ucb1x00_io_read(struct ucb1x00 *ucb) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci return ucb1x00_reg_read(ucb, UCB_IO_DATA); 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic void ucb1x00_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = gpiochip_get_data(chip); 1108c2ecf20Sopenharmony_ci unsigned long flags; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci spin_lock_irqsave(&ucb->io_lock, flags); 1138c2ecf20Sopenharmony_ci if (value) 1148c2ecf20Sopenharmony_ci ucb->io_out |= 1 << offset; 1158c2ecf20Sopenharmony_ci else 1168c2ecf20Sopenharmony_ci ucb->io_out &= ~(1 << offset); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 1198c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 1208c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 1218c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ucb->io_lock, flags); 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = gpiochip_get_data(chip); 1278c2ecf20Sopenharmony_ci unsigned val; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 1308c2ecf20Sopenharmony_ci val = ucb1x00_reg_read(ucb, UCB_IO_DATA); 1318c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci return !!(val & (1 << offset)); 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = gpiochip_get_data(chip); 1398c2ecf20Sopenharmony_ci unsigned long flags; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci spin_lock_irqsave(&ucb->io_lock, flags); 1428c2ecf20Sopenharmony_ci ucb->io_dir &= ~(1 << offset); 1438c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 1448c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 1458c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 1468c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ucb->io_lock, flags); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return 0; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset 1528c2ecf20Sopenharmony_ci , int value) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = gpiochip_get_data(chip); 1558c2ecf20Sopenharmony_ci unsigned long flags; 1568c2ecf20Sopenharmony_ci unsigned old, mask = 1 << offset; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci spin_lock_irqsave(&ucb->io_lock, flags); 1598c2ecf20Sopenharmony_ci old = ucb->io_out; 1608c2ecf20Sopenharmony_ci if (value) 1618c2ecf20Sopenharmony_ci ucb->io_out |= mask; 1628c2ecf20Sopenharmony_ci else 1638c2ecf20Sopenharmony_ci ucb->io_out &= ~mask; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 1668c2ecf20Sopenharmony_ci if (old != ucb->io_out) 1678c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci if (!(ucb->io_dir & mask)) { 1708c2ecf20Sopenharmony_ci ucb->io_dir |= mask; 1718c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 1728c2ecf20Sopenharmony_ci } 1738c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 1748c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ucb->io_lock, flags); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return 0; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic int ucb1x00_to_irq(struct gpio_chip *chip, unsigned offset) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = gpiochip_get_data(chip); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci return ucb->irq_base > 0 ? ucb->irq_base + offset : -ENXIO; 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci/* 1878c2ecf20Sopenharmony_ci * UCB1300 data sheet says we must: 1888c2ecf20Sopenharmony_ci * 1. enable ADC => 5us (including reference startup time) 1898c2ecf20Sopenharmony_ci * 2. select input => 51*tsibclk => 4.3us 1908c2ecf20Sopenharmony_ci * 3. start conversion => 102*tsibclk => 8.5us 1918c2ecf20Sopenharmony_ci * (tsibclk = 1/11981000) 1928c2ecf20Sopenharmony_ci * Period between SIB 128-bit frames = 10.7us 1938c2ecf20Sopenharmony_ci */ 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci/** 1968c2ecf20Sopenharmony_ci * ucb1x00_adc_enable - enable the ADC converter 1978c2ecf20Sopenharmony_ci * @ucb: UCB1x00 structure describing chip 1988c2ecf20Sopenharmony_ci * 1998c2ecf20Sopenharmony_ci * Enable the ucb1x00 and ADC converter on the UCB1x00 for use. 2008c2ecf20Sopenharmony_ci * Any code wishing to use the ADC converter must call this 2018c2ecf20Sopenharmony_ci * function prior to using it. 2028c2ecf20Sopenharmony_ci * 2038c2ecf20Sopenharmony_ci * This function takes the ADC mutex to prevent two or more 2048c2ecf20Sopenharmony_ci * concurrent uses, and therefore may sleep. As a result, it 2058c2ecf20Sopenharmony_ci * can only be called from process context, not interrupt 2068c2ecf20Sopenharmony_ci * context. 2078c2ecf20Sopenharmony_ci * 2088c2ecf20Sopenharmony_ci * You should release the ADC as soon as possible using 2098c2ecf20Sopenharmony_ci * ucb1x00_adc_disable. 2108c2ecf20Sopenharmony_ci */ 2118c2ecf20Sopenharmony_civoid ucb1x00_adc_enable(struct ucb1x00 *ucb) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci mutex_lock(&ucb->adc_mutex); 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci ucb->adc_cr |= UCB_ADC_ENA; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 2188c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr); 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci/** 2228c2ecf20Sopenharmony_ci * ucb1x00_adc_read - read the specified ADC channel 2238c2ecf20Sopenharmony_ci * @ucb: UCB1x00 structure describing chip 2248c2ecf20Sopenharmony_ci * @adc_channel: ADC channel mask 2258c2ecf20Sopenharmony_ci * @sync: wait for syncronisation pulse. 2268c2ecf20Sopenharmony_ci * 2278c2ecf20Sopenharmony_ci * Start an ADC conversion and wait for the result. Note that 2288c2ecf20Sopenharmony_ci * synchronised ADC conversions (via the ADCSYNC pin) must wait 2298c2ecf20Sopenharmony_ci * until the trigger is asserted and the conversion is finished. 2308c2ecf20Sopenharmony_ci * 2318c2ecf20Sopenharmony_ci * This function currently spins waiting for the conversion to 2328c2ecf20Sopenharmony_ci * complete (2 frames max without sync). 2338c2ecf20Sopenharmony_ci * 2348c2ecf20Sopenharmony_ci * If called for a synchronised ADC conversion, it may sleep 2358c2ecf20Sopenharmony_ci * with the ADC mutex held. 2368c2ecf20Sopenharmony_ci */ 2378c2ecf20Sopenharmony_ciunsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync) 2388c2ecf20Sopenharmony_ci{ 2398c2ecf20Sopenharmony_ci unsigned int val; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci if (sync) 2428c2ecf20Sopenharmony_ci adc_channel |= UCB_ADC_SYNC_ENA; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel); 2458c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci for (;;) { 2488c2ecf20Sopenharmony_ci val = ucb1x00_reg_read(ucb, UCB_ADC_DATA); 2498c2ecf20Sopenharmony_ci if (val & UCB_ADC_DAT_VAL) 2508c2ecf20Sopenharmony_ci break; 2518c2ecf20Sopenharmony_ci /* yield to other processes */ 2528c2ecf20Sopenharmony_ci set_current_state(TASK_INTERRUPTIBLE); 2538c2ecf20Sopenharmony_ci schedule_timeout(1); 2548c2ecf20Sopenharmony_ci } 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci return UCB_ADC_DAT(val); 2578c2ecf20Sopenharmony_ci} 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci/** 2608c2ecf20Sopenharmony_ci * ucb1x00_adc_disable - disable the ADC converter 2618c2ecf20Sopenharmony_ci * @ucb: UCB1x00 structure describing chip 2628c2ecf20Sopenharmony_ci * 2638c2ecf20Sopenharmony_ci * Disable the ADC converter and release the ADC mutex. 2648c2ecf20Sopenharmony_ci */ 2658c2ecf20Sopenharmony_civoid ucb1x00_adc_disable(struct ucb1x00 *ucb) 2668c2ecf20Sopenharmony_ci{ 2678c2ecf20Sopenharmony_ci ucb->adc_cr &= ~UCB_ADC_ENA; 2688c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr); 2698c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci mutex_unlock(&ucb->adc_mutex); 2728c2ecf20Sopenharmony_ci} 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci/* 2758c2ecf20Sopenharmony_ci * UCB1x00 Interrupt handling. 2768c2ecf20Sopenharmony_ci * 2778c2ecf20Sopenharmony_ci * The UCB1x00 can generate interrupts when the SIBCLK is stopped. 2788c2ecf20Sopenharmony_ci * Since we need to read an internal register, we must re-enable 2798c2ecf20Sopenharmony_ci * SIBCLK to talk to the chip. We leave the clock running until 2808c2ecf20Sopenharmony_ci * we have finished processing all interrupts from the chip. 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_cistatic void ucb1x00_irq(struct irq_desc *desc) 2838c2ecf20Sopenharmony_ci{ 2848c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = irq_desc_get_handler_data(desc); 2858c2ecf20Sopenharmony_ci unsigned int isr, i; 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 2888c2ecf20Sopenharmony_ci isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS); 2898c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr); 2908c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci for (i = 0; i < 16 && isr; i++, isr >>= 1) 2938c2ecf20Sopenharmony_ci if (isr & 1) 2948c2ecf20Sopenharmony_ci generic_handle_irq(ucb->irq_base + i); 2958c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 2968c2ecf20Sopenharmony_ci} 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_cistatic void ucb1x00_irq_update(struct ucb1x00 *ucb, unsigned mask) 2998c2ecf20Sopenharmony_ci{ 3008c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 3018c2ecf20Sopenharmony_ci if (ucb->irq_ris_enbl & mask) 3028c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl & 3038c2ecf20Sopenharmony_ci ucb->irq_mask); 3048c2ecf20Sopenharmony_ci if (ucb->irq_fal_enbl & mask) 3058c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl & 3068c2ecf20Sopenharmony_ci ucb->irq_mask); 3078c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 3088c2ecf20Sopenharmony_ci} 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic void ucb1x00_irq_noop(struct irq_data *data) 3118c2ecf20Sopenharmony_ci{ 3128c2ecf20Sopenharmony_ci} 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_cistatic void ucb1x00_irq_mask(struct irq_data *data) 3158c2ecf20Sopenharmony_ci{ 3168c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = irq_data_get_irq_chip_data(data); 3178c2ecf20Sopenharmony_ci unsigned mask = 1 << (data->irq - ucb->irq_base); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci raw_spin_lock(&ucb->irq_lock); 3208c2ecf20Sopenharmony_ci ucb->irq_mask &= ~mask; 3218c2ecf20Sopenharmony_ci ucb1x00_irq_update(ucb, mask); 3228c2ecf20Sopenharmony_ci raw_spin_unlock(&ucb->irq_lock); 3238c2ecf20Sopenharmony_ci} 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_cistatic void ucb1x00_irq_unmask(struct irq_data *data) 3268c2ecf20Sopenharmony_ci{ 3278c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = irq_data_get_irq_chip_data(data); 3288c2ecf20Sopenharmony_ci unsigned mask = 1 << (data->irq - ucb->irq_base); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci raw_spin_lock(&ucb->irq_lock); 3318c2ecf20Sopenharmony_ci ucb->irq_mask |= mask; 3328c2ecf20Sopenharmony_ci ucb1x00_irq_update(ucb, mask); 3338c2ecf20Sopenharmony_ci raw_spin_unlock(&ucb->irq_lock); 3348c2ecf20Sopenharmony_ci} 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_cistatic int ucb1x00_irq_set_type(struct irq_data *data, unsigned int type) 3378c2ecf20Sopenharmony_ci{ 3388c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = irq_data_get_irq_chip_data(data); 3398c2ecf20Sopenharmony_ci unsigned mask = 1 << (data->irq - ucb->irq_base); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci raw_spin_lock(&ucb->irq_lock); 3428c2ecf20Sopenharmony_ci if (type & IRQ_TYPE_EDGE_RISING) 3438c2ecf20Sopenharmony_ci ucb->irq_ris_enbl |= mask; 3448c2ecf20Sopenharmony_ci else 3458c2ecf20Sopenharmony_ci ucb->irq_ris_enbl &= ~mask; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci if (type & IRQ_TYPE_EDGE_FALLING) 3488c2ecf20Sopenharmony_ci ucb->irq_fal_enbl |= mask; 3498c2ecf20Sopenharmony_ci else 3508c2ecf20Sopenharmony_ci ucb->irq_fal_enbl &= ~mask; 3518c2ecf20Sopenharmony_ci if (ucb->irq_mask & mask) { 3528c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl & 3538c2ecf20Sopenharmony_ci ucb->irq_mask); 3548c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl & 3558c2ecf20Sopenharmony_ci ucb->irq_mask); 3568c2ecf20Sopenharmony_ci } 3578c2ecf20Sopenharmony_ci raw_spin_unlock(&ucb->irq_lock); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci return 0; 3608c2ecf20Sopenharmony_ci} 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_cistatic int ucb1x00_irq_set_wake(struct irq_data *data, unsigned int on) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = irq_data_get_irq_chip_data(data); 3658c2ecf20Sopenharmony_ci struct ucb1x00_plat_data *pdata = ucb->mcp->attached_device.platform_data; 3668c2ecf20Sopenharmony_ci unsigned mask = 1 << (data->irq - ucb->irq_base); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci if (!pdata || !pdata->can_wakeup) 3698c2ecf20Sopenharmony_ci return -EINVAL; 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci raw_spin_lock(&ucb->irq_lock); 3728c2ecf20Sopenharmony_ci if (on) 3738c2ecf20Sopenharmony_ci ucb->irq_wake |= mask; 3748c2ecf20Sopenharmony_ci else 3758c2ecf20Sopenharmony_ci ucb->irq_wake &= ~mask; 3768c2ecf20Sopenharmony_ci raw_spin_unlock(&ucb->irq_lock); 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci return 0; 3798c2ecf20Sopenharmony_ci} 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_cistatic struct irq_chip ucb1x00_irqchip = { 3828c2ecf20Sopenharmony_ci .name = "ucb1x00", 3838c2ecf20Sopenharmony_ci .irq_ack = ucb1x00_irq_noop, 3848c2ecf20Sopenharmony_ci .irq_mask = ucb1x00_irq_mask, 3858c2ecf20Sopenharmony_ci .irq_unmask = ucb1x00_irq_unmask, 3868c2ecf20Sopenharmony_ci .irq_set_type = ucb1x00_irq_set_type, 3878c2ecf20Sopenharmony_ci .irq_set_wake = ucb1x00_irq_set_wake, 3888c2ecf20Sopenharmony_ci}; 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_cistatic int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv) 3918c2ecf20Sopenharmony_ci{ 3928c2ecf20Sopenharmony_ci struct ucb1x00_dev *dev; 3938c2ecf20Sopenharmony_ci int ret; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL); 3968c2ecf20Sopenharmony_ci if (!dev) 3978c2ecf20Sopenharmony_ci return -ENOMEM; 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci dev->ucb = ucb; 4008c2ecf20Sopenharmony_ci dev->drv = drv; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci ret = drv->add(dev); 4038c2ecf20Sopenharmony_ci if (ret) { 4048c2ecf20Sopenharmony_ci kfree(dev); 4058c2ecf20Sopenharmony_ci return ret; 4068c2ecf20Sopenharmony_ci } 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci list_add_tail(&dev->dev_node, &ucb->devs); 4098c2ecf20Sopenharmony_ci list_add_tail(&dev->drv_node, &drv->devs); 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci return ret; 4128c2ecf20Sopenharmony_ci} 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_cistatic void ucb1x00_remove_dev(struct ucb1x00_dev *dev) 4158c2ecf20Sopenharmony_ci{ 4168c2ecf20Sopenharmony_ci dev->drv->remove(dev); 4178c2ecf20Sopenharmony_ci list_del(&dev->dev_node); 4188c2ecf20Sopenharmony_ci list_del(&dev->drv_node); 4198c2ecf20Sopenharmony_ci kfree(dev); 4208c2ecf20Sopenharmony_ci} 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci/* 4238c2ecf20Sopenharmony_ci * Try to probe our interrupt, rather than relying on lots of 4248c2ecf20Sopenharmony_ci * hard-coded machine dependencies. For reference, the expected 4258c2ecf20Sopenharmony_ci * IRQ mappings are: 4268c2ecf20Sopenharmony_ci * 4278c2ecf20Sopenharmony_ci * Machine Default IRQ 4288c2ecf20Sopenharmony_ci * adsbitsy IRQ_GPCIN4 4298c2ecf20Sopenharmony_ci * cerf IRQ_GPIO_UCB1200_IRQ 4308c2ecf20Sopenharmony_ci * flexanet IRQ_GPIO_GUI 4318c2ecf20Sopenharmony_ci * freebird IRQ_GPIO_FREEBIRD_UCB1300_IRQ 4328c2ecf20Sopenharmony_ci * graphicsclient ADS_EXT_IRQ(8) 4338c2ecf20Sopenharmony_ci * graphicsmaster ADS_EXT_IRQ(8) 4348c2ecf20Sopenharmony_ci * lart LART_IRQ_UCB1200 4358c2ecf20Sopenharmony_ci * omnimeter IRQ_GPIO23 4368c2ecf20Sopenharmony_ci * pfs168 IRQ_GPIO_UCB1300_IRQ 4378c2ecf20Sopenharmony_ci * simpad IRQ_GPIO_UCB1300_IRQ 4388c2ecf20Sopenharmony_ci * shannon SHANNON_IRQ_GPIO_IRQ_CODEC 4398c2ecf20Sopenharmony_ci * yopy IRQ_GPIO_UCB1200_IRQ 4408c2ecf20Sopenharmony_ci */ 4418c2ecf20Sopenharmony_cistatic int ucb1x00_detect_irq(struct ucb1x00 *ucb) 4428c2ecf20Sopenharmony_ci{ 4438c2ecf20Sopenharmony_ci unsigned long mask; 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci mask = probe_irq_on(); 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci /* 4488c2ecf20Sopenharmony_ci * Enable the ADC interrupt. 4498c2ecf20Sopenharmony_ci */ 4508c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC); 4518c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC); 4528c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff); 4538c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci /* 4568c2ecf20Sopenharmony_ci * Cause an ADC interrupt. 4578c2ecf20Sopenharmony_ci */ 4588c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA); 4598c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START); 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci /* 4628c2ecf20Sopenharmony_ci * Wait for the conversion to complete. 4638c2ecf20Sopenharmony_ci */ 4648c2ecf20Sopenharmony_ci while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0); 4658c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_ADC_CR, 0); 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci /* 4688c2ecf20Sopenharmony_ci * Disable and clear interrupt. 4698c2ecf20Sopenharmony_ci */ 4708c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_RIS, 0); 4718c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_FAL, 0); 4728c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff); 4738c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci /* 4768c2ecf20Sopenharmony_ci * Read triggered interrupt. 4778c2ecf20Sopenharmony_ci */ 4788c2ecf20Sopenharmony_ci return probe_irq_off(mask); 4798c2ecf20Sopenharmony_ci} 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_cistatic void ucb1x00_release(struct device *dev) 4828c2ecf20Sopenharmony_ci{ 4838c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = classdev_to_ucb1x00(dev); 4848c2ecf20Sopenharmony_ci kfree(ucb); 4858c2ecf20Sopenharmony_ci} 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_cistatic struct class ucb1x00_class = { 4888c2ecf20Sopenharmony_ci .name = "ucb1x00", 4898c2ecf20Sopenharmony_ci .dev_release = ucb1x00_release, 4908c2ecf20Sopenharmony_ci}; 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic int ucb1x00_probe(struct mcp *mcp) 4938c2ecf20Sopenharmony_ci{ 4948c2ecf20Sopenharmony_ci struct ucb1x00_plat_data *pdata = mcp->attached_device.platform_data; 4958c2ecf20Sopenharmony_ci struct ucb1x00_driver *drv; 4968c2ecf20Sopenharmony_ci struct ucb1x00 *ucb; 4978c2ecf20Sopenharmony_ci unsigned id, i, irq_base; 4988c2ecf20Sopenharmony_ci int ret = -ENODEV; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci /* Tell the platform to deassert the UCB1x00 reset */ 5018c2ecf20Sopenharmony_ci if (pdata && pdata->reset) 5028c2ecf20Sopenharmony_ci pdata->reset(UCB_RST_PROBE); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci mcp_enable(mcp); 5058c2ecf20Sopenharmony_ci id = mcp_reg_read(mcp, UCB_ID); 5068c2ecf20Sopenharmony_ci mcp_disable(mcp); 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) { 5098c2ecf20Sopenharmony_ci printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id); 5108c2ecf20Sopenharmony_ci goto out; 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL); 5148c2ecf20Sopenharmony_ci ret = -ENOMEM; 5158c2ecf20Sopenharmony_ci if (!ucb) 5168c2ecf20Sopenharmony_ci goto out; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci device_initialize(&ucb->dev); 5198c2ecf20Sopenharmony_ci ucb->dev.class = &ucb1x00_class; 5208c2ecf20Sopenharmony_ci ucb->dev.parent = &mcp->attached_device; 5218c2ecf20Sopenharmony_ci dev_set_name(&ucb->dev, "ucb1x00"); 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci raw_spin_lock_init(&ucb->irq_lock); 5248c2ecf20Sopenharmony_ci spin_lock_init(&ucb->io_lock); 5258c2ecf20Sopenharmony_ci mutex_init(&ucb->adc_mutex); 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci ucb->id = id; 5288c2ecf20Sopenharmony_ci ucb->mcp = mcp; 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci ret = device_add(&ucb->dev); 5318c2ecf20Sopenharmony_ci if (ret) 5328c2ecf20Sopenharmony_ci goto err_dev_add; 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 5358c2ecf20Sopenharmony_ci ucb->irq = ucb1x00_detect_irq(ucb); 5368c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 5378c2ecf20Sopenharmony_ci if (!ucb->irq) { 5388c2ecf20Sopenharmony_ci dev_err(&ucb->dev, "IRQ probe failed\n"); 5398c2ecf20Sopenharmony_ci ret = -ENODEV; 5408c2ecf20Sopenharmony_ci goto err_no_irq; 5418c2ecf20Sopenharmony_ci } 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci ucb->gpio.base = -1; 5448c2ecf20Sopenharmony_ci irq_base = pdata ? pdata->irq_base : 0; 5458c2ecf20Sopenharmony_ci ucb->irq_base = irq_alloc_descs(-1, irq_base, 16, -1); 5468c2ecf20Sopenharmony_ci if (ucb->irq_base < 0) { 5478c2ecf20Sopenharmony_ci dev_err(&ucb->dev, "unable to allocate 16 irqs: %d\n", 5488c2ecf20Sopenharmony_ci ucb->irq_base); 5498c2ecf20Sopenharmony_ci ret = ucb->irq_base; 5508c2ecf20Sopenharmony_ci goto err_irq_alloc; 5518c2ecf20Sopenharmony_ci } 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci for (i = 0; i < 16; i++) { 5548c2ecf20Sopenharmony_ci unsigned irq = ucb->irq_base + i; 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci irq_set_chip_and_handler(irq, &ucb1x00_irqchip, handle_edge_irq); 5578c2ecf20Sopenharmony_ci irq_set_chip_data(irq, ucb); 5588c2ecf20Sopenharmony_ci irq_clear_status_flags(irq, IRQ_NOREQUEST); 5598c2ecf20Sopenharmony_ci } 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci irq_set_irq_type(ucb->irq, IRQ_TYPE_EDGE_RISING); 5628c2ecf20Sopenharmony_ci irq_set_chained_handler_and_data(ucb->irq, ucb1x00_irq, ucb); 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci if (pdata && pdata->gpio_base) { 5658c2ecf20Sopenharmony_ci ucb->gpio.label = dev_name(&ucb->dev); 5668c2ecf20Sopenharmony_ci ucb->gpio.parent = &ucb->dev; 5678c2ecf20Sopenharmony_ci ucb->gpio.owner = THIS_MODULE; 5688c2ecf20Sopenharmony_ci ucb->gpio.base = pdata->gpio_base; 5698c2ecf20Sopenharmony_ci ucb->gpio.ngpio = 10; 5708c2ecf20Sopenharmony_ci ucb->gpio.set = ucb1x00_gpio_set; 5718c2ecf20Sopenharmony_ci ucb->gpio.get = ucb1x00_gpio_get; 5728c2ecf20Sopenharmony_ci ucb->gpio.direction_input = ucb1x00_gpio_direction_input; 5738c2ecf20Sopenharmony_ci ucb->gpio.direction_output = ucb1x00_gpio_direction_output; 5748c2ecf20Sopenharmony_ci ucb->gpio.to_irq = ucb1x00_to_irq; 5758c2ecf20Sopenharmony_ci ret = gpiochip_add_data(&ucb->gpio, ucb); 5768c2ecf20Sopenharmony_ci if (ret) 5778c2ecf20Sopenharmony_ci goto err_gpio_add; 5788c2ecf20Sopenharmony_ci } else 5798c2ecf20Sopenharmony_ci dev_info(&ucb->dev, "gpio_base not set so no gpiolib support"); 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci mcp_set_drvdata(mcp, ucb); 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci if (pdata) 5848c2ecf20Sopenharmony_ci device_set_wakeup_capable(&ucb->dev, pdata->can_wakeup); 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&ucb->devs); 5878c2ecf20Sopenharmony_ci mutex_lock(&ucb1x00_mutex); 5888c2ecf20Sopenharmony_ci list_add_tail(&ucb->node, &ucb1x00_devices); 5898c2ecf20Sopenharmony_ci list_for_each_entry(drv, &ucb1x00_drivers, node) { 5908c2ecf20Sopenharmony_ci ucb1x00_add_dev(ucb, drv); 5918c2ecf20Sopenharmony_ci } 5928c2ecf20Sopenharmony_ci mutex_unlock(&ucb1x00_mutex); 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci return ret; 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci err_gpio_add: 5978c2ecf20Sopenharmony_ci irq_set_chained_handler(ucb->irq, NULL); 5988c2ecf20Sopenharmony_ci err_irq_alloc: 5998c2ecf20Sopenharmony_ci if (ucb->irq_base > 0) 6008c2ecf20Sopenharmony_ci irq_free_descs(ucb->irq_base, 16); 6018c2ecf20Sopenharmony_ci err_no_irq: 6028c2ecf20Sopenharmony_ci device_del(&ucb->dev); 6038c2ecf20Sopenharmony_ci err_dev_add: 6048c2ecf20Sopenharmony_ci put_device(&ucb->dev); 6058c2ecf20Sopenharmony_ci out: 6068c2ecf20Sopenharmony_ci if (pdata && pdata->reset) 6078c2ecf20Sopenharmony_ci pdata->reset(UCB_RST_PROBE_FAIL); 6088c2ecf20Sopenharmony_ci return ret; 6098c2ecf20Sopenharmony_ci} 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_cistatic void ucb1x00_remove(struct mcp *mcp) 6128c2ecf20Sopenharmony_ci{ 6138c2ecf20Sopenharmony_ci struct ucb1x00_plat_data *pdata = mcp->attached_device.platform_data; 6148c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = mcp_get_drvdata(mcp); 6158c2ecf20Sopenharmony_ci struct list_head *l, *n; 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci mutex_lock(&ucb1x00_mutex); 6188c2ecf20Sopenharmony_ci list_del(&ucb->node); 6198c2ecf20Sopenharmony_ci list_for_each_safe(l, n, &ucb->devs) { 6208c2ecf20Sopenharmony_ci struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node); 6218c2ecf20Sopenharmony_ci ucb1x00_remove_dev(dev); 6228c2ecf20Sopenharmony_ci } 6238c2ecf20Sopenharmony_ci mutex_unlock(&ucb1x00_mutex); 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_ci if (ucb->gpio.base != -1) 6268c2ecf20Sopenharmony_ci gpiochip_remove(&ucb->gpio); 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci irq_set_chained_handler(ucb->irq, NULL); 6298c2ecf20Sopenharmony_ci irq_free_descs(ucb->irq_base, 16); 6308c2ecf20Sopenharmony_ci device_unregister(&ucb->dev); 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci if (pdata && pdata->reset) 6338c2ecf20Sopenharmony_ci pdata->reset(UCB_RST_REMOVE); 6348c2ecf20Sopenharmony_ci} 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ciint ucb1x00_register_driver(struct ucb1x00_driver *drv) 6378c2ecf20Sopenharmony_ci{ 6388c2ecf20Sopenharmony_ci struct ucb1x00 *ucb; 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&drv->devs); 6418c2ecf20Sopenharmony_ci mutex_lock(&ucb1x00_mutex); 6428c2ecf20Sopenharmony_ci list_add_tail(&drv->node, &ucb1x00_drivers); 6438c2ecf20Sopenharmony_ci list_for_each_entry(ucb, &ucb1x00_devices, node) { 6448c2ecf20Sopenharmony_ci ucb1x00_add_dev(ucb, drv); 6458c2ecf20Sopenharmony_ci } 6468c2ecf20Sopenharmony_ci mutex_unlock(&ucb1x00_mutex); 6478c2ecf20Sopenharmony_ci return 0; 6488c2ecf20Sopenharmony_ci} 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_civoid ucb1x00_unregister_driver(struct ucb1x00_driver *drv) 6518c2ecf20Sopenharmony_ci{ 6528c2ecf20Sopenharmony_ci struct list_head *n, *l; 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci mutex_lock(&ucb1x00_mutex); 6558c2ecf20Sopenharmony_ci list_del(&drv->node); 6568c2ecf20Sopenharmony_ci list_for_each_safe(l, n, &drv->devs) { 6578c2ecf20Sopenharmony_ci struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node); 6588c2ecf20Sopenharmony_ci ucb1x00_remove_dev(dev); 6598c2ecf20Sopenharmony_ci } 6608c2ecf20Sopenharmony_ci mutex_unlock(&ucb1x00_mutex); 6618c2ecf20Sopenharmony_ci} 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 6648c2ecf20Sopenharmony_cistatic int ucb1x00_suspend(struct device *dev) 6658c2ecf20Sopenharmony_ci{ 6668c2ecf20Sopenharmony_ci struct ucb1x00_plat_data *pdata = dev_get_platdata(dev); 6678c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = dev_get_drvdata(dev); 6688c2ecf20Sopenharmony_ci struct ucb1x00_dev *udev; 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci mutex_lock(&ucb1x00_mutex); 6718c2ecf20Sopenharmony_ci list_for_each_entry(udev, &ucb->devs, dev_node) { 6728c2ecf20Sopenharmony_ci if (udev->drv->suspend) 6738c2ecf20Sopenharmony_ci udev->drv->suspend(udev); 6748c2ecf20Sopenharmony_ci } 6758c2ecf20Sopenharmony_ci mutex_unlock(&ucb1x00_mutex); 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci if (ucb->irq_wake) { 6788c2ecf20Sopenharmony_ci unsigned long flags; 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&ucb->irq_lock, flags); 6818c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 6828c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl & 6838c2ecf20Sopenharmony_ci ucb->irq_wake); 6848c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl & 6858c2ecf20Sopenharmony_ci ucb->irq_wake); 6868c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 6878c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&ucb->irq_lock, flags); 6888c2ecf20Sopenharmony_ci 6898c2ecf20Sopenharmony_ci enable_irq_wake(ucb->irq); 6908c2ecf20Sopenharmony_ci } else if (pdata && pdata->reset) 6918c2ecf20Sopenharmony_ci pdata->reset(UCB_RST_SUSPEND); 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_ci return 0; 6948c2ecf20Sopenharmony_ci} 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_cistatic int ucb1x00_resume(struct device *dev) 6978c2ecf20Sopenharmony_ci{ 6988c2ecf20Sopenharmony_ci struct ucb1x00_plat_data *pdata = dev_get_platdata(dev); 6998c2ecf20Sopenharmony_ci struct ucb1x00 *ucb = dev_get_drvdata(dev); 7008c2ecf20Sopenharmony_ci struct ucb1x00_dev *udev; 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_ci if (!ucb->irq_wake && pdata && pdata->reset) 7038c2ecf20Sopenharmony_ci pdata->reset(UCB_RST_RESUME); 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci ucb1x00_enable(ucb); 7068c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); 7078c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); 7088c2ecf20Sopenharmony_ci 7098c2ecf20Sopenharmony_ci if (ucb->irq_wake) { 7108c2ecf20Sopenharmony_ci unsigned long flags; 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&ucb->irq_lock, flags); 7138c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl & 7148c2ecf20Sopenharmony_ci ucb->irq_mask); 7158c2ecf20Sopenharmony_ci ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl & 7168c2ecf20Sopenharmony_ci ucb->irq_mask); 7178c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&ucb->irq_lock, flags); 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ci disable_irq_wake(ucb->irq); 7208c2ecf20Sopenharmony_ci } 7218c2ecf20Sopenharmony_ci ucb1x00_disable(ucb); 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci mutex_lock(&ucb1x00_mutex); 7248c2ecf20Sopenharmony_ci list_for_each_entry(udev, &ucb->devs, dev_node) { 7258c2ecf20Sopenharmony_ci if (udev->drv->resume) 7268c2ecf20Sopenharmony_ci udev->drv->resume(udev); 7278c2ecf20Sopenharmony_ci } 7288c2ecf20Sopenharmony_ci mutex_unlock(&ucb1x00_mutex); 7298c2ecf20Sopenharmony_ci return 0; 7308c2ecf20Sopenharmony_ci} 7318c2ecf20Sopenharmony_ci#endif 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(ucb1x00_pm_ops, ucb1x00_suspend, ucb1x00_resume); 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_cistatic struct mcp_driver ucb1x00_driver = { 7368c2ecf20Sopenharmony_ci .drv = { 7378c2ecf20Sopenharmony_ci .name = "ucb1x00", 7388c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 7398c2ecf20Sopenharmony_ci .pm = &ucb1x00_pm_ops, 7408c2ecf20Sopenharmony_ci }, 7418c2ecf20Sopenharmony_ci .probe = ucb1x00_probe, 7428c2ecf20Sopenharmony_ci .remove = ucb1x00_remove, 7438c2ecf20Sopenharmony_ci}; 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_cistatic int __init ucb1x00_init(void) 7468c2ecf20Sopenharmony_ci{ 7478c2ecf20Sopenharmony_ci int ret = class_register(&ucb1x00_class); 7488c2ecf20Sopenharmony_ci if (ret == 0) { 7498c2ecf20Sopenharmony_ci ret = mcp_driver_register(&ucb1x00_driver); 7508c2ecf20Sopenharmony_ci if (ret) 7518c2ecf20Sopenharmony_ci class_unregister(&ucb1x00_class); 7528c2ecf20Sopenharmony_ci } 7538c2ecf20Sopenharmony_ci return ret; 7548c2ecf20Sopenharmony_ci} 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_cistatic void __exit ucb1x00_exit(void) 7578c2ecf20Sopenharmony_ci{ 7588c2ecf20Sopenharmony_ci mcp_driver_unregister(&ucb1x00_driver); 7598c2ecf20Sopenharmony_ci class_unregister(&ucb1x00_class); 7608c2ecf20Sopenharmony_ci} 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_cimodule_init(ucb1x00_init); 7638c2ecf20Sopenharmony_cimodule_exit(ucb1x00_exit); 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ucb1x00_io_set_dir); 7668c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ucb1x00_io_write); 7678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ucb1x00_io_read); 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ucb1x00_adc_enable); 7708c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ucb1x00_adc_read); 7718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ucb1x00_adc_disable); 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ucb1x00_register_driver); 7748c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ucb1x00_unregister_driver); 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ciMODULE_ALIAS("mcp:ucb1x00"); 7778c2ecf20Sopenharmony_ciMODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); 7788c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("UCB1x00 core driver"); 7798c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 780