1*1f67b599SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 227e34995SRabin Vincent /* 31a6e4b74SViresh Kumar * ST Microelectronics MFD: stmpe's driver 41a6e4b74SViresh Kumar * 527e34995SRabin Vincent * Copyright (C) ST-Ericsson SA 2010 627e34995SRabin Vincent * 727e34995SRabin Vincent * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson 827e34995SRabin Vincent */ 927e34995SRabin Vincent 10ac713cc9SVipul Kumar Samar #include <linux/err.h> 1173de16dbSViresh Kumar #include <linux/gpio.h> 12dba61c8fSSamuel Ortiz #include <linux/export.h> 1327e34995SRabin Vincent #include <linux/kernel.h> 1427e34995SRabin Vincent #include <linux/interrupt.h> 1527e34995SRabin Vincent #include <linux/irq.h> 1676f93992SLee Jones #include <linux/irqdomain.h> 1720d5c7deSRandy Dunlap #include <linux/of.h> 18ac713cc9SVipul Kumar Samar #include <linux/of_gpio.h> 191a6e4b74SViresh Kumar #include <linux/pm.h> 2027e34995SRabin Vincent #include <linux/slab.h> 2127e34995SRabin Vincent #include <linux/mfd/core.h> 22230f13a5SJean-Nicolas Graux #include <linux/delay.h> 239c9e3214SLinus Walleij #include <linux/regulator/consumer.h> 2427e34995SRabin Vincent #include "stmpe.h" 2527e34995SRabin Vincent 26fc1882dcSLinus Walleij /** 27fc1882dcSLinus Walleij * struct stmpe_platform_data - STMPE platform data 28fc1882dcSLinus Walleij * @id: device id to distinguish between multiple STMPEs on the same board 29fc1882dcSLinus Walleij * @blocks: bitmask of blocks to enable (use STMPE_BLOCK_*) 30fc1882dcSLinus Walleij * @irq_trigger: IRQ trigger to use for the interrupt to the host 31fc1882dcSLinus Walleij * @autosleep: bool to enable/disable stmpe autosleep 32fc1882dcSLinus Walleij * @autosleep_timeout: inactivity timeout in milliseconds for autosleep 33fc1882dcSLinus Walleij * @irq_over_gpio: true if gpio is used to get irq 34fc1882dcSLinus Walleij * @irq_gpio: gpio number over which irq will be requested (significant only if 35fc1882dcSLinus Walleij * irq_over_gpio is true) 36fc1882dcSLinus Walleij */ 37fc1882dcSLinus Walleij struct stmpe_platform_data { 38fc1882dcSLinus Walleij int id; 39fc1882dcSLinus Walleij unsigned int blocks; 40fc1882dcSLinus Walleij unsigned int irq_trigger; 41fc1882dcSLinus Walleij bool autosleep; 42fc1882dcSLinus Walleij bool irq_over_gpio; 43fc1882dcSLinus Walleij int irq_gpio; 44fc1882dcSLinus Walleij int autosleep_timeout; 45fc1882dcSLinus Walleij }; 46fc1882dcSLinus Walleij 4727e34995SRabin Vincent static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks) 4827e34995SRabin Vincent { 4927e34995SRabin Vincent return stmpe->variant->enable(stmpe, blocks, true); 5027e34995SRabin Vincent } 5127e34995SRabin Vincent 5227e34995SRabin Vincent static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks) 5327e34995SRabin Vincent { 5427e34995SRabin Vincent return stmpe->variant->enable(stmpe, blocks, false); 5527e34995SRabin Vincent } 5627e34995SRabin Vincent 5727e34995SRabin Vincent static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg) 5827e34995SRabin Vincent { 5927e34995SRabin Vincent int ret; 6027e34995SRabin Vincent 611a6e4b74SViresh Kumar ret = stmpe->ci->read_byte(stmpe, reg); 6227e34995SRabin Vincent if (ret < 0) 631a6e4b74SViresh Kumar dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret); 6427e34995SRabin Vincent 6527e34995SRabin Vincent dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret); 6627e34995SRabin Vincent 6727e34995SRabin Vincent return ret; 6827e34995SRabin Vincent } 6927e34995SRabin Vincent 7027e34995SRabin Vincent static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val) 7127e34995SRabin Vincent { 7227e34995SRabin Vincent int ret; 7327e34995SRabin Vincent 7427e34995SRabin Vincent dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val); 7527e34995SRabin Vincent 761a6e4b74SViresh Kumar ret = stmpe->ci->write_byte(stmpe, reg, val); 7727e34995SRabin Vincent if (ret < 0) 781a6e4b74SViresh Kumar dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret); 7927e34995SRabin Vincent 8027e34995SRabin Vincent return ret; 8127e34995SRabin Vincent } 8227e34995SRabin Vincent 8327e34995SRabin Vincent static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val) 8427e34995SRabin Vincent { 8527e34995SRabin Vincent int ret; 8627e34995SRabin Vincent 8727e34995SRabin Vincent ret = __stmpe_reg_read(stmpe, reg); 8827e34995SRabin Vincent if (ret < 0) 8927e34995SRabin Vincent return ret; 9027e34995SRabin Vincent 9127e34995SRabin Vincent ret &= ~mask; 9227e34995SRabin Vincent ret |= val; 9327e34995SRabin Vincent 9427e34995SRabin Vincent return __stmpe_reg_write(stmpe, reg, ret); 9527e34995SRabin Vincent } 9627e34995SRabin Vincent 9727e34995SRabin Vincent static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, 9827e34995SRabin Vincent u8 *values) 9927e34995SRabin Vincent { 10027e34995SRabin Vincent int ret; 10127e34995SRabin Vincent 1021a6e4b74SViresh Kumar ret = stmpe->ci->read_block(stmpe, reg, length, values); 10327e34995SRabin Vincent if (ret < 0) 1041a6e4b74SViresh Kumar dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret); 10527e34995SRabin Vincent 10627e34995SRabin Vincent dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret); 10727e34995SRabin Vincent stmpe_dump_bytes("stmpe rd: ", values, length); 10827e34995SRabin Vincent 10927e34995SRabin Vincent return ret; 11027e34995SRabin Vincent } 11127e34995SRabin Vincent 11227e34995SRabin Vincent static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length, 11327e34995SRabin Vincent const u8 *values) 11427e34995SRabin Vincent { 11527e34995SRabin Vincent int ret; 11627e34995SRabin Vincent 11727e34995SRabin Vincent dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length); 11827e34995SRabin Vincent stmpe_dump_bytes("stmpe wr: ", values, length); 11927e34995SRabin Vincent 1201a6e4b74SViresh Kumar ret = stmpe->ci->write_block(stmpe, reg, length, values); 12127e34995SRabin Vincent if (ret < 0) 1221a6e4b74SViresh Kumar dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret); 12327e34995SRabin Vincent 12427e34995SRabin Vincent return ret; 12527e34995SRabin Vincent } 12627e34995SRabin Vincent 12727e34995SRabin Vincent /** 12827e34995SRabin Vincent * stmpe_enable - enable blocks on an STMPE device 12927e34995SRabin Vincent * @stmpe: Device to work on 13027e34995SRabin Vincent * @blocks: Mask of blocks (enum stmpe_block values) to enable 13127e34995SRabin Vincent */ 13227e34995SRabin Vincent int stmpe_enable(struct stmpe *stmpe, unsigned int blocks) 13327e34995SRabin Vincent { 13427e34995SRabin Vincent int ret; 13527e34995SRabin Vincent 13627e34995SRabin Vincent mutex_lock(&stmpe->lock); 13727e34995SRabin Vincent ret = __stmpe_enable(stmpe, blocks); 13827e34995SRabin Vincent mutex_unlock(&stmpe->lock); 13927e34995SRabin Vincent 14027e34995SRabin Vincent return ret; 14127e34995SRabin Vincent } 14227e34995SRabin Vincent EXPORT_SYMBOL_GPL(stmpe_enable); 14327e34995SRabin Vincent 14427e34995SRabin Vincent /** 14527e34995SRabin Vincent * stmpe_disable - disable blocks on an STMPE device 14627e34995SRabin Vincent * @stmpe: Device to work on 14727e34995SRabin Vincent * @blocks: Mask of blocks (enum stmpe_block values) to enable 14827e34995SRabin Vincent */ 14927e34995SRabin Vincent int stmpe_disable(struct stmpe *stmpe, unsigned int blocks) 15027e34995SRabin Vincent { 15127e34995SRabin Vincent int ret; 15227e34995SRabin Vincent 15327e34995SRabin Vincent mutex_lock(&stmpe->lock); 15427e34995SRabin Vincent ret = __stmpe_disable(stmpe, blocks); 15527e34995SRabin Vincent mutex_unlock(&stmpe->lock); 15627e34995SRabin Vincent 15727e34995SRabin Vincent return ret; 15827e34995SRabin Vincent } 15927e34995SRabin Vincent EXPORT_SYMBOL_GPL(stmpe_disable); 16027e34995SRabin Vincent 16127e34995SRabin Vincent /** 16227e34995SRabin Vincent * stmpe_reg_read() - read a single STMPE register 16327e34995SRabin Vincent * @stmpe: Device to read from 16427e34995SRabin Vincent * @reg: Register to read 16527e34995SRabin Vincent */ 16627e34995SRabin Vincent int stmpe_reg_read(struct stmpe *stmpe, u8 reg) 16727e34995SRabin Vincent { 16827e34995SRabin Vincent int ret; 16927e34995SRabin Vincent 17027e34995SRabin Vincent mutex_lock(&stmpe->lock); 17127e34995SRabin Vincent ret = __stmpe_reg_read(stmpe, reg); 17227e34995SRabin Vincent mutex_unlock(&stmpe->lock); 17327e34995SRabin Vincent 17427e34995SRabin Vincent return ret; 17527e34995SRabin Vincent } 17627e34995SRabin Vincent EXPORT_SYMBOL_GPL(stmpe_reg_read); 17727e34995SRabin Vincent 17827e34995SRabin Vincent /** 17927e34995SRabin Vincent * stmpe_reg_write() - write a single STMPE register 18027e34995SRabin Vincent * @stmpe: Device to write to 18127e34995SRabin Vincent * @reg: Register to write 18227e34995SRabin Vincent * @val: Value to write 18327e34995SRabin Vincent */ 18427e34995SRabin Vincent int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val) 18527e34995SRabin Vincent { 18627e34995SRabin Vincent int ret; 18727e34995SRabin Vincent 18827e34995SRabin Vincent mutex_lock(&stmpe->lock); 18927e34995SRabin Vincent ret = __stmpe_reg_write(stmpe, reg, val); 19027e34995SRabin Vincent mutex_unlock(&stmpe->lock); 19127e34995SRabin Vincent 19227e34995SRabin Vincent return ret; 19327e34995SRabin Vincent } 19427e34995SRabin Vincent EXPORT_SYMBOL_GPL(stmpe_reg_write); 19527e34995SRabin Vincent 19627e34995SRabin Vincent /** 19727e34995SRabin Vincent * stmpe_set_bits() - set the value of a bitfield in a STMPE register 19827e34995SRabin Vincent * @stmpe: Device to write to 19927e34995SRabin Vincent * @reg: Register to write 20027e34995SRabin Vincent * @mask: Mask of bits to set 20127e34995SRabin Vincent * @val: Value to set 20227e34995SRabin Vincent */ 20327e34995SRabin Vincent int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val) 20427e34995SRabin Vincent { 20527e34995SRabin Vincent int ret; 20627e34995SRabin Vincent 20727e34995SRabin Vincent mutex_lock(&stmpe->lock); 20827e34995SRabin Vincent ret = __stmpe_set_bits(stmpe, reg, mask, val); 20927e34995SRabin Vincent mutex_unlock(&stmpe->lock); 21027e34995SRabin Vincent 21127e34995SRabin Vincent return ret; 21227e34995SRabin Vincent } 21327e34995SRabin Vincent EXPORT_SYMBOL_GPL(stmpe_set_bits); 21427e34995SRabin Vincent 21527e34995SRabin Vincent /** 21627e34995SRabin Vincent * stmpe_block_read() - read multiple STMPE registers 21727e34995SRabin Vincent * @stmpe: Device to read from 21827e34995SRabin Vincent * @reg: First register 21927e34995SRabin Vincent * @length: Number of registers 22027e34995SRabin Vincent * @values: Buffer to write to 22127e34995SRabin Vincent */ 22227e34995SRabin Vincent int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values) 22327e34995SRabin Vincent { 22427e34995SRabin Vincent int ret; 22527e34995SRabin Vincent 22627e34995SRabin Vincent mutex_lock(&stmpe->lock); 22727e34995SRabin Vincent ret = __stmpe_block_read(stmpe, reg, length, values); 22827e34995SRabin Vincent mutex_unlock(&stmpe->lock); 22927e34995SRabin Vincent 23027e34995SRabin Vincent return ret; 23127e34995SRabin Vincent } 23227e34995SRabin Vincent EXPORT_SYMBOL_GPL(stmpe_block_read); 23327e34995SRabin Vincent 23427e34995SRabin Vincent /** 23527e34995SRabin Vincent * stmpe_block_write() - write multiple STMPE registers 23627e34995SRabin Vincent * @stmpe: Device to write to 23727e34995SRabin Vincent * @reg: First register 23827e34995SRabin Vincent * @length: Number of registers 23927e34995SRabin Vincent * @values: Values to write 24027e34995SRabin Vincent */ 24127e34995SRabin Vincent int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length, 24227e34995SRabin Vincent const u8 *values) 24327e34995SRabin Vincent { 24427e34995SRabin Vincent int ret; 24527e34995SRabin Vincent 24627e34995SRabin Vincent mutex_lock(&stmpe->lock); 24727e34995SRabin Vincent ret = __stmpe_block_write(stmpe, reg, length, values); 24827e34995SRabin Vincent mutex_unlock(&stmpe->lock); 24927e34995SRabin Vincent 25027e34995SRabin Vincent return ret; 25127e34995SRabin Vincent } 25227e34995SRabin Vincent EXPORT_SYMBOL_GPL(stmpe_block_write); 25327e34995SRabin Vincent 25427e34995SRabin Vincent /** 2554dcaa6b6SOm Prakash * stmpe_set_altfunc()- set the alternate function for STMPE pins 25627e34995SRabin Vincent * @stmpe: Device to configure 25727e34995SRabin Vincent * @pins: Bitmask of pins to affect 25827e34995SRabin Vincent * @block: block to enable alternate functions for 25927e34995SRabin Vincent * 26027e34995SRabin Vincent * @pins is assumed to have a bit set for each of the bits whose alternate 26127e34995SRabin Vincent * function is to be changed, numbered according to the GPIOXY numbers. 26227e34995SRabin Vincent * 26327e34995SRabin Vincent * If the GPIO module is not enabled, this function automatically enables it in 26427e34995SRabin Vincent * order to perform the change. 26527e34995SRabin Vincent */ 26627e34995SRabin Vincent int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block) 26727e34995SRabin Vincent { 26827e34995SRabin Vincent struct stmpe_variant_info *variant = stmpe->variant; 26927e34995SRabin Vincent u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB]; 27027e34995SRabin Vincent int af_bits = variant->af_bits; 27127e34995SRabin Vincent int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8); 27227e34995SRabin Vincent int mask = (1 << af_bits) - 1; 2737929fa77SLee Jones u8 regs[8]; 2747f7f4ea1SViresh Kumar int af, afperreg, ret; 27527e34995SRabin Vincent 2767f7f4ea1SViresh Kumar if (!variant->get_altfunc) 2777f7f4ea1SViresh Kumar return 0; 2787f7f4ea1SViresh Kumar 2797f7f4ea1SViresh Kumar afperreg = 8 / af_bits; 28027e34995SRabin Vincent mutex_lock(&stmpe->lock); 28127e34995SRabin Vincent 28227e34995SRabin Vincent ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO); 28327e34995SRabin Vincent if (ret < 0) 28427e34995SRabin Vincent goto out; 28527e34995SRabin Vincent 28627e34995SRabin Vincent ret = __stmpe_block_read(stmpe, regaddr, numregs, regs); 28727e34995SRabin Vincent if (ret < 0) 28827e34995SRabin Vincent goto out; 28927e34995SRabin Vincent 29027e34995SRabin Vincent af = variant->get_altfunc(stmpe, block); 29127e34995SRabin Vincent 29227e34995SRabin Vincent while (pins) { 29327e34995SRabin Vincent int pin = __ffs(pins); 29427e34995SRabin Vincent int regoffset = numregs - (pin / afperreg) - 1; 29527e34995SRabin Vincent int pos = (pin % afperreg) * (8 / afperreg); 29627e34995SRabin Vincent 29727e34995SRabin Vincent regs[regoffset] &= ~(mask << pos); 29827e34995SRabin Vincent regs[regoffset] |= af << pos; 29927e34995SRabin Vincent 30027e34995SRabin Vincent pins &= ~(1 << pin); 30127e34995SRabin Vincent } 30227e34995SRabin Vincent 30327e34995SRabin Vincent ret = __stmpe_block_write(stmpe, regaddr, numregs, regs); 30427e34995SRabin Vincent 30527e34995SRabin Vincent out: 30627e34995SRabin Vincent mutex_unlock(&stmpe->lock); 30727e34995SRabin Vincent return ret; 30827e34995SRabin Vincent } 30927e34995SRabin Vincent EXPORT_SYMBOL_GPL(stmpe_set_altfunc); 31027e34995SRabin Vincent 31127e34995SRabin Vincent /* 31227e34995SRabin Vincent * GPIO (all variants) 31327e34995SRabin Vincent */ 31427e34995SRabin Vincent 31527e34995SRabin Vincent static struct resource stmpe_gpio_resources[] = { 31627e34995SRabin Vincent /* Start and end filled dynamically */ 31727e34995SRabin Vincent { 31827e34995SRabin Vincent .flags = IORESOURCE_IRQ, 31927e34995SRabin Vincent }, 32027e34995SRabin Vincent }; 32127e34995SRabin Vincent 3226bbb3c4cSGeert Uytterhoeven static const struct mfd_cell stmpe_gpio_cell = { 32327e34995SRabin Vincent .name = "stmpe-gpio", 32486605cfeSVipul Kumar Samar .of_compatible = "st,stmpe-gpio", 32527e34995SRabin Vincent .resources = stmpe_gpio_resources, 32627e34995SRabin Vincent .num_resources = ARRAY_SIZE(stmpe_gpio_resources), 32727e34995SRabin Vincent }; 32827e34995SRabin Vincent 3296bbb3c4cSGeert Uytterhoeven static const struct mfd_cell stmpe_gpio_cell_noirq = { 330e31f9b82SChris Blair .name = "stmpe-gpio", 33186605cfeSVipul Kumar Samar .of_compatible = "st,stmpe-gpio", 332e31f9b82SChris Blair /* gpio cell resources consist of an irq only so no resources here */ 333e31f9b82SChris Blair }; 334e31f9b82SChris Blair 33527e34995SRabin Vincent /* 33627e34995SRabin Vincent * Keypad (1601, 2401, 2403) 33727e34995SRabin Vincent */ 33827e34995SRabin Vincent 33927e34995SRabin Vincent static struct resource stmpe_keypad_resources[] = { 34027e34995SRabin Vincent { 34127e34995SRabin Vincent .name = "KEYPAD", 34227e34995SRabin Vincent .flags = IORESOURCE_IRQ, 34327e34995SRabin Vincent }, 34427e34995SRabin Vincent { 34527e34995SRabin Vincent .name = "KEYPAD_OVER", 34627e34995SRabin Vincent .flags = IORESOURCE_IRQ, 34727e34995SRabin Vincent }, 34827e34995SRabin Vincent }; 34927e34995SRabin Vincent 3506bbb3c4cSGeert Uytterhoeven static const struct mfd_cell stmpe_keypad_cell = { 35127e34995SRabin Vincent .name = "stmpe-keypad", 3526ea32387SDmitry Torokhov .of_compatible = "st,stmpe-keypad", 35327e34995SRabin Vincent .resources = stmpe_keypad_resources, 35427e34995SRabin Vincent .num_resources = ARRAY_SIZE(stmpe_keypad_resources), 35527e34995SRabin Vincent }; 35627e34995SRabin Vincent 35727e34995SRabin Vincent /* 358b273c5e0SLinus Walleij * PWM (1601, 2401, 2403) 359b273c5e0SLinus Walleij */ 360b273c5e0SLinus Walleij static struct resource stmpe_pwm_resources[] = { 361b273c5e0SLinus Walleij { 362b273c5e0SLinus Walleij .name = "PWM0", 363b273c5e0SLinus Walleij .flags = IORESOURCE_IRQ, 364b273c5e0SLinus Walleij }, 365b273c5e0SLinus Walleij { 366b273c5e0SLinus Walleij .name = "PWM1", 367b273c5e0SLinus Walleij .flags = IORESOURCE_IRQ, 368b273c5e0SLinus Walleij }, 369b273c5e0SLinus Walleij { 370b273c5e0SLinus Walleij .name = "PWM2", 371b273c5e0SLinus Walleij .flags = IORESOURCE_IRQ, 372b273c5e0SLinus Walleij }, 373b273c5e0SLinus Walleij }; 374b273c5e0SLinus Walleij 375b273c5e0SLinus Walleij static const struct mfd_cell stmpe_pwm_cell = { 376b273c5e0SLinus Walleij .name = "stmpe-pwm", 377b273c5e0SLinus Walleij .of_compatible = "st,stmpe-pwm", 378b273c5e0SLinus Walleij .resources = stmpe_pwm_resources, 379b273c5e0SLinus Walleij .num_resources = ARRAY_SIZE(stmpe_pwm_resources), 380b273c5e0SLinus Walleij }; 381b273c5e0SLinus Walleij 382b273c5e0SLinus Walleij /* 3837f7f4ea1SViresh Kumar * STMPE801 3847f7f4ea1SViresh Kumar */ 3857f7f4ea1SViresh Kumar static const u8 stmpe801_regs[] = { 3867f7f4ea1SViresh Kumar [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID, 3877f7f4ea1SViresh Kumar [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL, 3887f7f4ea1SViresh Kumar [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA, 3897f7f4ea1SViresh Kumar [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN, 3907f7f4ea1SViresh Kumar [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN, 3917f7f4ea1SViresh Kumar [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR, 3927f7f4ea1SViresh Kumar [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN, 3937f7f4ea1SViresh Kumar [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA, 3947f7f4ea1SViresh Kumar 3957f7f4ea1SViresh Kumar }; 3967f7f4ea1SViresh Kumar 3977f7f4ea1SViresh Kumar static struct stmpe_variant_block stmpe801_blocks[] = { 3987f7f4ea1SViresh Kumar { 3997f7f4ea1SViresh Kumar .cell = &stmpe_gpio_cell, 4007f7f4ea1SViresh Kumar .irq = 0, 4017f7f4ea1SViresh Kumar .block = STMPE_BLOCK_GPIO, 4027f7f4ea1SViresh Kumar }, 4037f7f4ea1SViresh Kumar }; 4047f7f4ea1SViresh Kumar 405e31f9b82SChris Blair static struct stmpe_variant_block stmpe801_blocks_noirq[] = { 406e31f9b82SChris Blair { 407e31f9b82SChris Blair .cell = &stmpe_gpio_cell_noirq, 408e31f9b82SChris Blair .block = STMPE_BLOCK_GPIO, 409e31f9b82SChris Blair }, 410e31f9b82SChris Blair }; 411e31f9b82SChris Blair 4127f7f4ea1SViresh Kumar static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks, 4137f7f4ea1SViresh Kumar bool enable) 4147f7f4ea1SViresh Kumar { 4157f7f4ea1SViresh Kumar if (blocks & STMPE_BLOCK_GPIO) 4167f7f4ea1SViresh Kumar return 0; 4177f7f4ea1SViresh Kumar else 4187f7f4ea1SViresh Kumar return -EINVAL; 4197f7f4ea1SViresh Kumar } 4207f7f4ea1SViresh Kumar 4217f7f4ea1SViresh Kumar static struct stmpe_variant_info stmpe801 = { 4227f7f4ea1SViresh Kumar .name = "stmpe801", 4237f7f4ea1SViresh Kumar .id_val = STMPE801_ID, 4247f7f4ea1SViresh Kumar .id_mask = 0xffff, 4257f7f4ea1SViresh Kumar .num_gpios = 8, 4267f7f4ea1SViresh Kumar .regs = stmpe801_regs, 4277f7f4ea1SViresh Kumar .blocks = stmpe801_blocks, 4287f7f4ea1SViresh Kumar .num_blocks = ARRAY_SIZE(stmpe801_blocks), 4297f7f4ea1SViresh Kumar .num_irqs = STMPE801_NR_INTERNAL_IRQS, 4307f7f4ea1SViresh Kumar .enable = stmpe801_enable, 4317f7f4ea1SViresh Kumar }; 4327f7f4ea1SViresh Kumar 433e31f9b82SChris Blair static struct stmpe_variant_info stmpe801_noirq = { 434e31f9b82SChris Blair .name = "stmpe801", 435e31f9b82SChris Blair .id_val = STMPE801_ID, 436e31f9b82SChris Blair .id_mask = 0xffff, 437e31f9b82SChris Blair .num_gpios = 8, 438e31f9b82SChris Blair .regs = stmpe801_regs, 439e31f9b82SChris Blair .blocks = stmpe801_blocks_noirq, 440e31f9b82SChris Blair .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq), 441e31f9b82SChris Blair .enable = stmpe801_enable, 442e31f9b82SChris Blair }; 443e31f9b82SChris Blair 4447f7f4ea1SViresh Kumar /* 4451cda2394SViresh Kumar * Touchscreen (STMPE811 or STMPE610) 44627e34995SRabin Vincent */ 44727e34995SRabin Vincent 44827e34995SRabin Vincent static struct resource stmpe_ts_resources[] = { 44927e34995SRabin Vincent { 45027e34995SRabin Vincent .name = "TOUCH_DET", 45127e34995SRabin Vincent .flags = IORESOURCE_IRQ, 45227e34995SRabin Vincent }, 45327e34995SRabin Vincent { 45427e34995SRabin Vincent .name = "FIFO_TH", 45527e34995SRabin Vincent .flags = IORESOURCE_IRQ, 45627e34995SRabin Vincent }, 45727e34995SRabin Vincent }; 45827e34995SRabin Vincent 4596bbb3c4cSGeert Uytterhoeven static const struct mfd_cell stmpe_ts_cell = { 46027e34995SRabin Vincent .name = "stmpe-ts", 461037db524SVipul Kumar Samar .of_compatible = "st,stmpe-ts", 46227e34995SRabin Vincent .resources = stmpe_ts_resources, 46327e34995SRabin Vincent .num_resources = ARRAY_SIZE(stmpe_ts_resources), 46427e34995SRabin Vincent }; 46527e34995SRabin Vincent 46627e34995SRabin Vincent /* 4676377cfa3SStefan Agner * ADC (STMPE811) 4686377cfa3SStefan Agner */ 4696377cfa3SStefan Agner 4706377cfa3SStefan Agner static struct resource stmpe_adc_resources[] = { 4716377cfa3SStefan Agner { 4726377cfa3SStefan Agner .name = "STMPE_TEMP_SENS", 4736377cfa3SStefan Agner .flags = IORESOURCE_IRQ, 4746377cfa3SStefan Agner }, 4756377cfa3SStefan Agner { 4766377cfa3SStefan Agner .name = "STMPE_ADC", 4776377cfa3SStefan Agner .flags = IORESOURCE_IRQ, 4786377cfa3SStefan Agner }, 4796377cfa3SStefan Agner }; 4806377cfa3SStefan Agner 4816377cfa3SStefan Agner static const struct mfd_cell stmpe_adc_cell = { 4826377cfa3SStefan Agner .name = "stmpe-adc", 4836377cfa3SStefan Agner .of_compatible = "st,stmpe-adc", 4846377cfa3SStefan Agner .resources = stmpe_adc_resources, 4856377cfa3SStefan Agner .num_resources = ARRAY_SIZE(stmpe_adc_resources), 4866377cfa3SStefan Agner }; 4876377cfa3SStefan Agner 4886377cfa3SStefan Agner /* 4891cda2394SViresh Kumar * STMPE811 or STMPE610 49027e34995SRabin Vincent */ 49127e34995SRabin Vincent 49227e34995SRabin Vincent static const u8 stmpe811_regs[] = { 49327e34995SRabin Vincent [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID, 4940f4be8cfSPatrice Chotard [STMPE_IDX_SYS_CTRL] = STMPE811_REG_SYS_CTRL, 4950f4be8cfSPatrice Chotard [STMPE_IDX_SYS_CTRL2] = STMPE811_REG_SYS_CTRL2, 49627e34995SRabin Vincent [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL, 49727e34995SRabin Vincent [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN, 49827e34995SRabin Vincent [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA, 49927e34995SRabin Vincent [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA, 50027e34995SRabin Vincent [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN, 50127e34995SRabin Vincent [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN, 50227e34995SRabin Vincent [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR, 50327e34995SRabin Vincent [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE, 50427e34995SRabin Vincent [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE, 50527e34995SRabin Vincent [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF, 50627e34995SRabin Vincent [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN, 50727e34995SRabin Vincent [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA, 508897ac667SPatrice Chotard [STMPE_IDX_GPEDR_LSB] = STMPE811_REG_GPIO_ED, 50927e34995SRabin Vincent }; 51027e34995SRabin Vincent 51127e34995SRabin Vincent static struct stmpe_variant_block stmpe811_blocks[] = { 51227e34995SRabin Vincent { 51327e34995SRabin Vincent .cell = &stmpe_gpio_cell, 51427e34995SRabin Vincent .irq = STMPE811_IRQ_GPIOC, 51527e34995SRabin Vincent .block = STMPE_BLOCK_GPIO, 51627e34995SRabin Vincent }, 51727e34995SRabin Vincent { 51827e34995SRabin Vincent .cell = &stmpe_ts_cell, 51927e34995SRabin Vincent .irq = STMPE811_IRQ_TOUCH_DET, 52027e34995SRabin Vincent .block = STMPE_BLOCK_TOUCHSCREEN, 52127e34995SRabin Vincent }, 5226377cfa3SStefan Agner { 5236377cfa3SStefan Agner .cell = &stmpe_adc_cell, 5246377cfa3SStefan Agner .irq = STMPE811_IRQ_TEMP_SENS, 5256377cfa3SStefan Agner .block = STMPE_BLOCK_ADC, 5266377cfa3SStefan Agner }, 52727e34995SRabin Vincent }; 52827e34995SRabin Vincent 52927e34995SRabin Vincent static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks, 53027e34995SRabin Vincent bool enable) 53127e34995SRabin Vincent { 53227e34995SRabin Vincent unsigned int mask = 0; 53327e34995SRabin Vincent 53427e34995SRabin Vincent if (blocks & STMPE_BLOCK_GPIO) 53527e34995SRabin Vincent mask |= STMPE811_SYS_CTRL2_GPIO_OFF; 53627e34995SRabin Vincent 53727e34995SRabin Vincent if (blocks & STMPE_BLOCK_ADC) 53827e34995SRabin Vincent mask |= STMPE811_SYS_CTRL2_ADC_OFF; 53927e34995SRabin Vincent 54027e34995SRabin Vincent if (blocks & STMPE_BLOCK_TOUCHSCREEN) 54127e34995SRabin Vincent mask |= STMPE811_SYS_CTRL2_TSC_OFF; 54227e34995SRabin Vincent 5430f4be8cfSPatrice Chotard return __stmpe_set_bits(stmpe, stmpe->regs[STMPE_IDX_SYS_CTRL2], mask, 54427e34995SRabin Vincent enable ? 0 : mask); 54527e34995SRabin Vincent } 54627e34995SRabin Vincent 5476377cfa3SStefan Agner int stmpe811_adc_common_init(struct stmpe *stmpe) 5486377cfa3SStefan Agner { 5496377cfa3SStefan Agner int ret; 5506377cfa3SStefan Agner u8 adc_ctrl1, adc_ctrl1_mask; 5516377cfa3SStefan Agner 5526377cfa3SStefan Agner adc_ctrl1 = STMPE_SAMPLE_TIME(stmpe->sample_time) | 5536377cfa3SStefan Agner STMPE_MOD_12B(stmpe->mod_12b) | 5546377cfa3SStefan Agner STMPE_REF_SEL(stmpe->ref_sel); 5556377cfa3SStefan Agner adc_ctrl1_mask = STMPE_SAMPLE_TIME(0xff) | STMPE_MOD_12B(0xff) | 5566377cfa3SStefan Agner STMPE_REF_SEL(0xff); 5576377cfa3SStefan Agner 5586377cfa3SStefan Agner ret = stmpe_set_bits(stmpe, STMPE811_REG_ADC_CTRL1, 5596377cfa3SStefan Agner adc_ctrl1_mask, adc_ctrl1); 5606377cfa3SStefan Agner if (ret) { 5616377cfa3SStefan Agner dev_err(stmpe->dev, "Could not setup ADC\n"); 5626377cfa3SStefan Agner return ret; 5636377cfa3SStefan Agner } 5646377cfa3SStefan Agner 5656377cfa3SStefan Agner ret = stmpe_set_bits(stmpe, STMPE811_REG_ADC_CTRL2, 5666377cfa3SStefan Agner STMPE_ADC_FREQ(0xff), STMPE_ADC_FREQ(stmpe->adc_freq)); 5676377cfa3SStefan Agner if (ret) { 5686377cfa3SStefan Agner dev_err(stmpe->dev, "Could not setup ADC\n"); 5696377cfa3SStefan Agner return ret; 5706377cfa3SStefan Agner } 5716377cfa3SStefan Agner 5726377cfa3SStefan Agner return 0; 5736377cfa3SStefan Agner } 5746377cfa3SStefan Agner EXPORT_SYMBOL_GPL(stmpe811_adc_common_init); 5756377cfa3SStefan Agner 57627e34995SRabin Vincent static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block) 57727e34995SRabin Vincent { 57827e34995SRabin Vincent /* 0 for touchscreen, 1 for GPIO */ 57927e34995SRabin Vincent return block != STMPE_BLOCK_TOUCHSCREEN; 58027e34995SRabin Vincent } 58127e34995SRabin Vincent 58227e34995SRabin Vincent static struct stmpe_variant_info stmpe811 = { 58327e34995SRabin Vincent .name = "stmpe811", 58427e34995SRabin Vincent .id_val = 0x0811, 58527e34995SRabin Vincent .id_mask = 0xffff, 58627e34995SRabin Vincent .num_gpios = 8, 58727e34995SRabin Vincent .af_bits = 1, 58827e34995SRabin Vincent .regs = stmpe811_regs, 58927e34995SRabin Vincent .blocks = stmpe811_blocks, 59027e34995SRabin Vincent .num_blocks = ARRAY_SIZE(stmpe811_blocks), 59127e34995SRabin Vincent .num_irqs = STMPE811_NR_INTERNAL_IRQS, 59227e34995SRabin Vincent .enable = stmpe811_enable, 59327e34995SRabin Vincent .get_altfunc = stmpe811_get_altfunc, 59427e34995SRabin Vincent }; 59527e34995SRabin Vincent 5961cda2394SViresh Kumar /* Similar to 811, except number of gpios */ 5971cda2394SViresh Kumar static struct stmpe_variant_info stmpe610 = { 5981cda2394SViresh Kumar .name = "stmpe610", 5991cda2394SViresh Kumar .id_val = 0x0811, 6001cda2394SViresh Kumar .id_mask = 0xffff, 6011cda2394SViresh Kumar .num_gpios = 6, 6021cda2394SViresh Kumar .af_bits = 1, 6031cda2394SViresh Kumar .regs = stmpe811_regs, 6041cda2394SViresh Kumar .blocks = stmpe811_blocks, 6051cda2394SViresh Kumar .num_blocks = ARRAY_SIZE(stmpe811_blocks), 6061cda2394SViresh Kumar .num_irqs = STMPE811_NR_INTERNAL_IRQS, 6071cda2394SViresh Kumar .enable = stmpe811_enable, 6081cda2394SViresh Kumar .get_altfunc = stmpe811_get_altfunc, 6091cda2394SViresh Kumar }; 6101cda2394SViresh Kumar 61127e34995SRabin Vincent /* 6126bb9f0d9SPatrice Chotard * STMPE1600 6136bb9f0d9SPatrice Chotard * Compared to all others STMPE variant, LSB and MSB regs are located in this 6146bb9f0d9SPatrice Chotard * order : LSB addr 6156bb9f0d9SPatrice Chotard * MSB addr + 1 6166bb9f0d9SPatrice Chotard * As there is only 2 * 8bits registers for GPMR/GPSR/IEGPIOPR, CSB index is MSB registers 6176bb9f0d9SPatrice Chotard */ 6186bb9f0d9SPatrice Chotard 6196bb9f0d9SPatrice Chotard static const u8 stmpe1600_regs[] = { 6206bb9f0d9SPatrice Chotard [STMPE_IDX_CHIP_ID] = STMPE1600_REG_CHIP_ID, 6216bb9f0d9SPatrice Chotard [STMPE_IDX_SYS_CTRL] = STMPE1600_REG_SYS_CTRL, 6226bb9f0d9SPatrice Chotard [STMPE_IDX_ICR_LSB] = STMPE1600_REG_SYS_CTRL, 6236bb9f0d9SPatrice Chotard [STMPE_IDX_GPMR_LSB] = STMPE1600_REG_GPMR_LSB, 6246bb9f0d9SPatrice Chotard [STMPE_IDX_GPMR_CSB] = STMPE1600_REG_GPMR_MSB, 6256bb9f0d9SPatrice Chotard [STMPE_IDX_GPSR_LSB] = STMPE1600_REG_GPSR_LSB, 6266bb9f0d9SPatrice Chotard [STMPE_IDX_GPSR_CSB] = STMPE1600_REG_GPSR_MSB, 627b97eef5dSHugues Fruchet [STMPE_IDX_GPCR_LSB] = STMPE1600_REG_GPSR_LSB, 628b97eef5dSHugues Fruchet [STMPE_IDX_GPCR_CSB] = STMPE1600_REG_GPSR_MSB, 6296bb9f0d9SPatrice Chotard [STMPE_IDX_GPDR_LSB] = STMPE1600_REG_GPDR_LSB, 6306bb9f0d9SPatrice Chotard [STMPE_IDX_GPDR_CSB] = STMPE1600_REG_GPDR_MSB, 6316bb9f0d9SPatrice Chotard [STMPE_IDX_IEGPIOR_LSB] = STMPE1600_REG_IEGPIOR_LSB, 6326bb9f0d9SPatrice Chotard [STMPE_IDX_IEGPIOR_CSB] = STMPE1600_REG_IEGPIOR_MSB, 6336bb9f0d9SPatrice Chotard [STMPE_IDX_ISGPIOR_LSB] = STMPE1600_REG_ISGPIOR_LSB, 6346bb9f0d9SPatrice Chotard }; 6356bb9f0d9SPatrice Chotard 6366bb9f0d9SPatrice Chotard static struct stmpe_variant_block stmpe1600_blocks[] = { 6376bb9f0d9SPatrice Chotard { 6386bb9f0d9SPatrice Chotard .cell = &stmpe_gpio_cell, 6396bb9f0d9SPatrice Chotard .irq = 0, 6406bb9f0d9SPatrice Chotard .block = STMPE_BLOCK_GPIO, 6416bb9f0d9SPatrice Chotard }, 6426bb9f0d9SPatrice Chotard }; 6436bb9f0d9SPatrice Chotard 6446bb9f0d9SPatrice Chotard static int stmpe1600_enable(struct stmpe *stmpe, unsigned int blocks, 6456bb9f0d9SPatrice Chotard bool enable) 6466bb9f0d9SPatrice Chotard { 6476bb9f0d9SPatrice Chotard if (blocks & STMPE_BLOCK_GPIO) 6486bb9f0d9SPatrice Chotard return 0; 6496bb9f0d9SPatrice Chotard else 6506bb9f0d9SPatrice Chotard return -EINVAL; 6516bb9f0d9SPatrice Chotard } 6526bb9f0d9SPatrice Chotard 6536bb9f0d9SPatrice Chotard static struct stmpe_variant_info stmpe1600 = { 6546bb9f0d9SPatrice Chotard .name = "stmpe1600", 6556bb9f0d9SPatrice Chotard .id_val = STMPE1600_ID, 6566bb9f0d9SPatrice Chotard .id_mask = 0xffff, 6576bb9f0d9SPatrice Chotard .num_gpios = 16, 6586bb9f0d9SPatrice Chotard .af_bits = 0, 6596bb9f0d9SPatrice Chotard .regs = stmpe1600_regs, 6606bb9f0d9SPatrice Chotard .blocks = stmpe1600_blocks, 6616bb9f0d9SPatrice Chotard .num_blocks = ARRAY_SIZE(stmpe1600_blocks), 6626bb9f0d9SPatrice Chotard .num_irqs = STMPE1600_NR_INTERNAL_IRQS, 6636bb9f0d9SPatrice Chotard .enable = stmpe1600_enable, 6646bb9f0d9SPatrice Chotard }; 6656bb9f0d9SPatrice Chotard 6666bb9f0d9SPatrice Chotard /* 66727e34995SRabin Vincent * STMPE1601 66827e34995SRabin Vincent */ 66927e34995SRabin Vincent 67027e34995SRabin Vincent static const u8 stmpe1601_regs[] = { 67127e34995SRabin Vincent [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID, 6720f4be8cfSPatrice Chotard [STMPE_IDX_SYS_CTRL] = STMPE1601_REG_SYS_CTRL, 6730f4be8cfSPatrice Chotard [STMPE_IDX_SYS_CTRL2] = STMPE1601_REG_SYS_CTRL2, 67427e34995SRabin Vincent [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB, 675897ac667SPatrice Chotard [STMPE_IDX_IER_MSB] = STMPE1601_REG_IER_MSB, 67627e34995SRabin Vincent [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB, 67727e34995SRabin Vincent [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB, 67827e34995SRabin Vincent [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB, 679897ac667SPatrice Chotard [STMPE_IDX_GPMR_CSB] = STMPE1601_REG_GPIO_MP_MSB, 68027e34995SRabin Vincent [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB, 681897ac667SPatrice Chotard [STMPE_IDX_GPSR_CSB] = STMPE1601_REG_GPIO_SET_MSB, 68227e34995SRabin Vincent [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB, 683897ac667SPatrice Chotard [STMPE_IDX_GPCR_CSB] = STMPE1601_REG_GPIO_CLR_MSB, 68427e34995SRabin Vincent [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB, 685897ac667SPatrice Chotard [STMPE_IDX_GPDR_CSB] = STMPE1601_REG_GPIO_SET_DIR_MSB, 686897ac667SPatrice Chotard [STMPE_IDX_GPEDR_LSB] = STMPE1601_REG_GPIO_ED_LSB, 687897ac667SPatrice Chotard [STMPE_IDX_GPEDR_CSB] = STMPE1601_REG_GPIO_ED_MSB, 68827e34995SRabin Vincent [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB, 689897ac667SPatrice Chotard [STMPE_IDX_GPRER_CSB] = STMPE1601_REG_GPIO_RE_MSB, 69027e34995SRabin Vincent [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB, 691897ac667SPatrice Chotard [STMPE_IDX_GPFER_CSB] = STMPE1601_REG_GPIO_FE_MSB, 69280e1dd82SLinus Walleij [STMPE_IDX_GPPUR_LSB] = STMPE1601_REG_GPIO_PU_LSB, 69327e34995SRabin Vincent [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB, 69427e34995SRabin Vincent [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB, 695897ac667SPatrice Chotard [STMPE_IDX_IEGPIOR_CSB] = STMPE1601_REG_INT_EN_GPIO_MASK_MSB, 69627e34995SRabin Vincent [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB, 69727e34995SRabin Vincent }; 69827e34995SRabin Vincent 69927e34995SRabin Vincent static struct stmpe_variant_block stmpe1601_blocks[] = { 70027e34995SRabin Vincent { 70127e34995SRabin Vincent .cell = &stmpe_gpio_cell, 7025204e51dSLee Jones .irq = STMPE1601_IRQ_GPIOC, 70327e34995SRabin Vincent .block = STMPE_BLOCK_GPIO, 70427e34995SRabin Vincent }, 70527e34995SRabin Vincent { 70627e34995SRabin Vincent .cell = &stmpe_keypad_cell, 7075204e51dSLee Jones .irq = STMPE1601_IRQ_KEYPAD, 70827e34995SRabin Vincent .block = STMPE_BLOCK_KEYPAD, 70927e34995SRabin Vincent }, 710b273c5e0SLinus Walleij { 711b273c5e0SLinus Walleij .cell = &stmpe_pwm_cell, 712b273c5e0SLinus Walleij .irq = STMPE1601_IRQ_PWM0, 713b273c5e0SLinus Walleij .block = STMPE_BLOCK_PWM, 714b273c5e0SLinus Walleij }, 71527e34995SRabin Vincent }; 71627e34995SRabin Vincent 7175981f4e6SSundar R Iyer /* supported autosleep timeout delay (in msecs) */ 7185981f4e6SSundar R Iyer static const int stmpe_autosleep_delay[] = { 7195981f4e6SSundar R Iyer 4, 16, 32, 64, 128, 256, 512, 1024, 7205981f4e6SSundar R Iyer }; 7215981f4e6SSundar R Iyer 7225981f4e6SSundar R Iyer static int stmpe_round_timeout(int timeout) 7235981f4e6SSundar R Iyer { 7245981f4e6SSundar R Iyer int i; 7255981f4e6SSundar R Iyer 7265981f4e6SSundar R Iyer for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) { 7275981f4e6SSundar R Iyer if (stmpe_autosleep_delay[i] >= timeout) 7285981f4e6SSundar R Iyer return i; 7295981f4e6SSundar R Iyer } 7305981f4e6SSundar R Iyer 7315981f4e6SSundar R Iyer /* 7325981f4e6SSundar R Iyer * requests for delays longer than supported should not return the 7335981f4e6SSundar R Iyer * longest supported delay 7345981f4e6SSundar R Iyer */ 7355981f4e6SSundar R Iyer return -EINVAL; 7365981f4e6SSundar R Iyer } 7375981f4e6SSundar R Iyer 7385981f4e6SSundar R Iyer static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout) 7395981f4e6SSundar R Iyer { 7405981f4e6SSundar R Iyer int ret; 7415981f4e6SSundar R Iyer 7425981f4e6SSundar R Iyer if (!stmpe->variant->enable_autosleep) 7435981f4e6SSundar R Iyer return -ENOSYS; 7445981f4e6SSundar R Iyer 7455981f4e6SSundar R Iyer mutex_lock(&stmpe->lock); 7465981f4e6SSundar R Iyer ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout); 7475981f4e6SSundar R Iyer mutex_unlock(&stmpe->lock); 7485981f4e6SSundar R Iyer 7495981f4e6SSundar R Iyer return ret; 7505981f4e6SSundar R Iyer } 7515981f4e6SSundar R Iyer 7525981f4e6SSundar R Iyer /* 7535981f4e6SSundar R Iyer * Both stmpe 1601/2403 support same layout for autosleep 7545981f4e6SSundar R Iyer */ 7555981f4e6SSundar R Iyer static int stmpe1601_autosleep(struct stmpe *stmpe, 7565981f4e6SSundar R Iyer int autosleep_timeout) 7575981f4e6SSundar R Iyer { 7585981f4e6SSundar R Iyer int ret, timeout; 7595981f4e6SSundar R Iyer 7605981f4e6SSundar R Iyer /* choose the best available timeout */ 7615981f4e6SSundar R Iyer timeout = stmpe_round_timeout(autosleep_timeout); 7625981f4e6SSundar R Iyer if (timeout < 0) { 7635981f4e6SSundar R Iyer dev_err(stmpe->dev, "invalid timeout\n"); 7645981f4e6SSundar R Iyer return timeout; 7655981f4e6SSundar R Iyer } 7665981f4e6SSundar R Iyer 7670f4be8cfSPatrice Chotard ret = __stmpe_set_bits(stmpe, stmpe->regs[STMPE_IDX_SYS_CTRL2], 7685981f4e6SSundar R Iyer STMPE1601_AUTOSLEEP_TIMEOUT_MASK, 7695981f4e6SSundar R Iyer timeout); 7705981f4e6SSundar R Iyer if (ret < 0) 7715981f4e6SSundar R Iyer return ret; 7725981f4e6SSundar R Iyer 7730f4be8cfSPatrice Chotard return __stmpe_set_bits(stmpe, stmpe->regs[STMPE_IDX_SYS_CTRL2], 7745981f4e6SSundar R Iyer STPME1601_AUTOSLEEP_ENABLE, 7755981f4e6SSundar R Iyer STPME1601_AUTOSLEEP_ENABLE); 7765981f4e6SSundar R Iyer } 7775981f4e6SSundar R Iyer 77827e34995SRabin Vincent static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks, 77927e34995SRabin Vincent bool enable) 78027e34995SRabin Vincent { 78127e34995SRabin Vincent unsigned int mask = 0; 78227e34995SRabin Vincent 78327e34995SRabin Vincent if (blocks & STMPE_BLOCK_GPIO) 78427e34995SRabin Vincent mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO; 785b69d2ad6SLinus Walleij else 786b69d2ad6SLinus Walleij mask &= ~STMPE1601_SYS_CTRL_ENABLE_GPIO; 78727e34995SRabin Vincent 78827e34995SRabin Vincent if (blocks & STMPE_BLOCK_KEYPAD) 78927e34995SRabin Vincent mask |= STMPE1601_SYS_CTRL_ENABLE_KPC; 790b69d2ad6SLinus Walleij else 791b69d2ad6SLinus Walleij mask &= ~STMPE1601_SYS_CTRL_ENABLE_KPC; 792b69d2ad6SLinus Walleij 793b69d2ad6SLinus Walleij if (blocks & STMPE_BLOCK_PWM) 794b69d2ad6SLinus Walleij mask |= STMPE1601_SYS_CTRL_ENABLE_SPWM; 795b69d2ad6SLinus Walleij else 796b69d2ad6SLinus Walleij mask &= ~STMPE1601_SYS_CTRL_ENABLE_SPWM; 79727e34995SRabin Vincent 7980f4be8cfSPatrice Chotard return __stmpe_set_bits(stmpe, stmpe->regs[STMPE_IDX_SYS_CTRL], mask, 79927e34995SRabin Vincent enable ? mask : 0); 80027e34995SRabin Vincent } 80127e34995SRabin Vincent 80227e34995SRabin Vincent static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block) 80327e34995SRabin Vincent { 80427e34995SRabin Vincent switch (block) { 80527e34995SRabin Vincent case STMPE_BLOCK_PWM: 80627e34995SRabin Vincent return 2; 80727e34995SRabin Vincent 80827e34995SRabin Vincent case STMPE_BLOCK_KEYPAD: 80927e34995SRabin Vincent return 1; 81027e34995SRabin Vincent 81127e34995SRabin Vincent case STMPE_BLOCK_GPIO: 81227e34995SRabin Vincent default: 81327e34995SRabin Vincent return 0; 81427e34995SRabin Vincent } 81527e34995SRabin Vincent } 81627e34995SRabin Vincent 81727e34995SRabin Vincent static struct stmpe_variant_info stmpe1601 = { 81827e34995SRabin Vincent .name = "stmpe1601", 81927e34995SRabin Vincent .id_val = 0x0210, 82027e34995SRabin Vincent .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */ 82127e34995SRabin Vincent .num_gpios = 16, 82227e34995SRabin Vincent .af_bits = 2, 82327e34995SRabin Vincent .regs = stmpe1601_regs, 82427e34995SRabin Vincent .blocks = stmpe1601_blocks, 82527e34995SRabin Vincent .num_blocks = ARRAY_SIZE(stmpe1601_blocks), 82627e34995SRabin Vincent .num_irqs = STMPE1601_NR_INTERNAL_IRQS, 82727e34995SRabin Vincent .enable = stmpe1601_enable, 82827e34995SRabin Vincent .get_altfunc = stmpe1601_get_altfunc, 8295981f4e6SSundar R Iyer .enable_autosleep = stmpe1601_autosleep, 83027e34995SRabin Vincent }; 83127e34995SRabin Vincent 83227e34995SRabin Vincent /* 833230f13a5SJean-Nicolas Graux * STMPE1801 834230f13a5SJean-Nicolas Graux */ 835230f13a5SJean-Nicolas Graux static const u8 stmpe1801_regs[] = { 836230f13a5SJean-Nicolas Graux [STMPE_IDX_CHIP_ID] = STMPE1801_REG_CHIP_ID, 8370f4be8cfSPatrice Chotard [STMPE_IDX_SYS_CTRL] = STMPE1801_REG_SYS_CTRL, 838230f13a5SJean-Nicolas Graux [STMPE_IDX_ICR_LSB] = STMPE1801_REG_INT_CTRL_LOW, 839230f13a5SJean-Nicolas Graux [STMPE_IDX_IER_LSB] = STMPE1801_REG_INT_EN_MASK_LOW, 840230f13a5SJean-Nicolas Graux [STMPE_IDX_ISR_LSB] = STMPE1801_REG_INT_STA_LOW, 841230f13a5SJean-Nicolas Graux [STMPE_IDX_GPMR_LSB] = STMPE1801_REG_GPIO_MP_LOW, 842897ac667SPatrice Chotard [STMPE_IDX_GPMR_CSB] = STMPE1801_REG_GPIO_MP_MID, 843897ac667SPatrice Chotard [STMPE_IDX_GPMR_MSB] = STMPE1801_REG_GPIO_MP_HIGH, 844230f13a5SJean-Nicolas Graux [STMPE_IDX_GPSR_LSB] = STMPE1801_REG_GPIO_SET_LOW, 845897ac667SPatrice Chotard [STMPE_IDX_GPSR_CSB] = STMPE1801_REG_GPIO_SET_MID, 846897ac667SPatrice Chotard [STMPE_IDX_GPSR_MSB] = STMPE1801_REG_GPIO_SET_HIGH, 847230f13a5SJean-Nicolas Graux [STMPE_IDX_GPCR_LSB] = STMPE1801_REG_GPIO_CLR_LOW, 848897ac667SPatrice Chotard [STMPE_IDX_GPCR_CSB] = STMPE1801_REG_GPIO_CLR_MID, 849897ac667SPatrice Chotard [STMPE_IDX_GPCR_MSB] = STMPE1801_REG_GPIO_CLR_HIGH, 850230f13a5SJean-Nicolas Graux [STMPE_IDX_GPDR_LSB] = STMPE1801_REG_GPIO_SET_DIR_LOW, 851897ac667SPatrice Chotard [STMPE_IDX_GPDR_CSB] = STMPE1801_REG_GPIO_SET_DIR_MID, 852897ac667SPatrice Chotard [STMPE_IDX_GPDR_MSB] = STMPE1801_REG_GPIO_SET_DIR_HIGH, 853230f13a5SJean-Nicolas Graux [STMPE_IDX_GPRER_LSB] = STMPE1801_REG_GPIO_RE_LOW, 854897ac667SPatrice Chotard [STMPE_IDX_GPRER_CSB] = STMPE1801_REG_GPIO_RE_MID, 855897ac667SPatrice Chotard [STMPE_IDX_GPRER_MSB] = STMPE1801_REG_GPIO_RE_HIGH, 856230f13a5SJean-Nicolas Graux [STMPE_IDX_GPFER_LSB] = STMPE1801_REG_GPIO_FE_LOW, 857897ac667SPatrice Chotard [STMPE_IDX_GPFER_CSB] = STMPE1801_REG_GPIO_FE_MID, 858897ac667SPatrice Chotard [STMPE_IDX_GPFER_MSB] = STMPE1801_REG_GPIO_FE_HIGH, 85980e1dd82SLinus Walleij [STMPE_IDX_GPPUR_LSB] = STMPE1801_REG_GPIO_PULL_UP_LOW, 860230f13a5SJean-Nicolas Graux [STMPE_IDX_IEGPIOR_LSB] = STMPE1801_REG_INT_EN_GPIO_MASK_LOW, 861897ac667SPatrice Chotard [STMPE_IDX_IEGPIOR_CSB] = STMPE1801_REG_INT_EN_GPIO_MASK_MID, 862897ac667SPatrice Chotard [STMPE_IDX_IEGPIOR_MSB] = STMPE1801_REG_INT_EN_GPIO_MASK_HIGH, 863897ac667SPatrice Chotard [STMPE_IDX_ISGPIOR_MSB] = STMPE1801_REG_INT_STA_GPIO_HIGH, 864230f13a5SJean-Nicolas Graux }; 865230f13a5SJean-Nicolas Graux 866230f13a5SJean-Nicolas Graux static struct stmpe_variant_block stmpe1801_blocks[] = { 867230f13a5SJean-Nicolas Graux { 868230f13a5SJean-Nicolas Graux .cell = &stmpe_gpio_cell, 869230f13a5SJean-Nicolas Graux .irq = STMPE1801_IRQ_GPIOC, 870230f13a5SJean-Nicolas Graux .block = STMPE_BLOCK_GPIO, 871230f13a5SJean-Nicolas Graux }, 872230f13a5SJean-Nicolas Graux { 873230f13a5SJean-Nicolas Graux .cell = &stmpe_keypad_cell, 874230f13a5SJean-Nicolas Graux .irq = STMPE1801_IRQ_KEYPAD, 875230f13a5SJean-Nicolas Graux .block = STMPE_BLOCK_KEYPAD, 876230f13a5SJean-Nicolas Graux }, 877230f13a5SJean-Nicolas Graux }; 878230f13a5SJean-Nicolas Graux 879230f13a5SJean-Nicolas Graux static int stmpe1801_enable(struct stmpe *stmpe, unsigned int blocks, 880230f13a5SJean-Nicolas Graux bool enable) 881230f13a5SJean-Nicolas Graux { 882230f13a5SJean-Nicolas Graux unsigned int mask = 0; 883230f13a5SJean-Nicolas Graux if (blocks & STMPE_BLOCK_GPIO) 884230f13a5SJean-Nicolas Graux mask |= STMPE1801_MSK_INT_EN_GPIO; 885230f13a5SJean-Nicolas Graux 886230f13a5SJean-Nicolas Graux if (blocks & STMPE_BLOCK_KEYPAD) 887230f13a5SJean-Nicolas Graux mask |= STMPE1801_MSK_INT_EN_KPC; 888230f13a5SJean-Nicolas Graux 889230f13a5SJean-Nicolas Graux return __stmpe_set_bits(stmpe, STMPE1801_REG_INT_EN_MASK_LOW, mask, 890230f13a5SJean-Nicolas Graux enable ? mask : 0); 891230f13a5SJean-Nicolas Graux } 892230f13a5SJean-Nicolas Graux 893c4dd1ba3SPatrice Chotard static int stmpe_reset(struct stmpe *stmpe) 894230f13a5SJean-Nicolas Graux { 895c4dd1ba3SPatrice Chotard u16 id_val = stmpe->variant->id_val; 896230f13a5SJean-Nicolas Graux unsigned long timeout; 897230f13a5SJean-Nicolas Graux int ret = 0; 898c4dd1ba3SPatrice Chotard u8 reset_bit; 899c4dd1ba3SPatrice Chotard 900c4dd1ba3SPatrice Chotard if (id_val == STMPE811_ID) 901c4dd1ba3SPatrice Chotard /* STMPE801 and STMPE610 use bit 1 of SYS_CTRL register */ 902c4dd1ba3SPatrice Chotard reset_bit = STMPE811_SYS_CTRL_RESET; 903c4dd1ba3SPatrice Chotard else 904c4dd1ba3SPatrice Chotard /* all other STMPE variant use bit 7 of SYS_CTRL register */ 905c4dd1ba3SPatrice Chotard reset_bit = STMPE_SYS_CTRL_RESET; 906230f13a5SJean-Nicolas Graux 9070f4be8cfSPatrice Chotard ret = __stmpe_set_bits(stmpe, stmpe->regs[STMPE_IDX_SYS_CTRL], 908c4dd1ba3SPatrice Chotard reset_bit, reset_bit); 909230f13a5SJean-Nicolas Graux if (ret < 0) 910230f13a5SJean-Nicolas Graux return ret; 911230f13a5SJean-Nicolas Graux 912f4058420SLinus Walleij msleep(10); 913f4058420SLinus Walleij 914230f13a5SJean-Nicolas Graux timeout = jiffies + msecs_to_jiffies(100); 915230f13a5SJean-Nicolas Graux while (time_before(jiffies, timeout)) { 9160f4be8cfSPatrice Chotard ret = __stmpe_reg_read(stmpe, stmpe->regs[STMPE_IDX_SYS_CTRL]); 917230f13a5SJean-Nicolas Graux if (ret < 0) 918230f13a5SJean-Nicolas Graux return ret; 919c4dd1ba3SPatrice Chotard if (!(ret & reset_bit)) 920230f13a5SJean-Nicolas Graux return 0; 921230f13a5SJean-Nicolas Graux usleep_range(100, 200); 92252397fe1SSachin Kamat } 923230f13a5SJean-Nicolas Graux return -EIO; 924230f13a5SJean-Nicolas Graux } 925230f13a5SJean-Nicolas Graux 926230f13a5SJean-Nicolas Graux static struct stmpe_variant_info stmpe1801 = { 927230f13a5SJean-Nicolas Graux .name = "stmpe1801", 928230f13a5SJean-Nicolas Graux .id_val = STMPE1801_ID, 929230f13a5SJean-Nicolas Graux .id_mask = 0xfff0, 930230f13a5SJean-Nicolas Graux .num_gpios = 18, 931230f13a5SJean-Nicolas Graux .af_bits = 0, 932230f13a5SJean-Nicolas Graux .regs = stmpe1801_regs, 933230f13a5SJean-Nicolas Graux .blocks = stmpe1801_blocks, 934230f13a5SJean-Nicolas Graux .num_blocks = ARRAY_SIZE(stmpe1801_blocks), 935230f13a5SJean-Nicolas Graux .num_irqs = STMPE1801_NR_INTERNAL_IRQS, 936230f13a5SJean-Nicolas Graux .enable = stmpe1801_enable, 937230f13a5SJean-Nicolas Graux /* stmpe1801 do not have any gpio alternate function */ 938230f13a5SJean-Nicolas Graux .get_altfunc = NULL, 939230f13a5SJean-Nicolas Graux }; 940230f13a5SJean-Nicolas Graux 941230f13a5SJean-Nicolas Graux /* 94227e34995SRabin Vincent * STMPE24XX 94327e34995SRabin Vincent */ 94427e34995SRabin Vincent 94527e34995SRabin Vincent static const u8 stmpe24xx_regs[] = { 94627e34995SRabin Vincent [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID, 9470f4be8cfSPatrice Chotard [STMPE_IDX_SYS_CTRL] = STMPE24XX_REG_SYS_CTRL, 9480f4be8cfSPatrice Chotard [STMPE_IDX_SYS_CTRL2] = STMPE24XX_REG_SYS_CTRL2, 94927e34995SRabin Vincent [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB, 950897ac667SPatrice Chotard [STMPE_IDX_IER_MSB] = STMPE24XX_REG_IER_MSB, 95127e34995SRabin Vincent [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB, 95227e34995SRabin Vincent [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB, 95327e34995SRabin Vincent [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB, 954897ac667SPatrice Chotard [STMPE_IDX_GPMR_CSB] = STMPE24XX_REG_GPMR_CSB, 955897ac667SPatrice Chotard [STMPE_IDX_GPMR_MSB] = STMPE24XX_REG_GPMR_MSB, 95627e34995SRabin Vincent [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB, 957897ac667SPatrice Chotard [STMPE_IDX_GPSR_CSB] = STMPE24XX_REG_GPSR_CSB, 958897ac667SPatrice Chotard [STMPE_IDX_GPSR_MSB] = STMPE24XX_REG_GPSR_MSB, 95927e34995SRabin Vincent [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB, 960897ac667SPatrice Chotard [STMPE_IDX_GPCR_CSB] = STMPE24XX_REG_GPCR_CSB, 961897ac667SPatrice Chotard [STMPE_IDX_GPCR_MSB] = STMPE24XX_REG_GPCR_MSB, 96227e34995SRabin Vincent [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB, 963897ac667SPatrice Chotard [STMPE_IDX_GPDR_CSB] = STMPE24XX_REG_GPDR_CSB, 964897ac667SPatrice Chotard [STMPE_IDX_GPDR_MSB] = STMPE24XX_REG_GPDR_MSB, 96527e34995SRabin Vincent [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB, 966897ac667SPatrice Chotard [STMPE_IDX_GPRER_CSB] = STMPE24XX_REG_GPRER_CSB, 967897ac667SPatrice Chotard [STMPE_IDX_GPRER_MSB] = STMPE24XX_REG_GPRER_MSB, 96827e34995SRabin Vincent [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB, 969897ac667SPatrice Chotard [STMPE_IDX_GPFER_CSB] = STMPE24XX_REG_GPFER_CSB, 970897ac667SPatrice Chotard [STMPE_IDX_GPFER_MSB] = STMPE24XX_REG_GPFER_MSB, 97180e1dd82SLinus Walleij [STMPE_IDX_GPPUR_LSB] = STMPE24XX_REG_GPPUR_LSB, 97280e1dd82SLinus Walleij [STMPE_IDX_GPPDR_LSB] = STMPE24XX_REG_GPPDR_LSB, 97327e34995SRabin Vincent [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB, 97427e34995SRabin Vincent [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB, 975897ac667SPatrice Chotard [STMPE_IDX_IEGPIOR_CSB] = STMPE24XX_REG_IEGPIOR_CSB, 976897ac667SPatrice Chotard [STMPE_IDX_IEGPIOR_MSB] = STMPE24XX_REG_IEGPIOR_MSB, 97727e34995SRabin Vincent [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB, 978897ac667SPatrice Chotard [STMPE_IDX_GPEDR_LSB] = STMPE24XX_REG_GPEDR_LSB, 979897ac667SPatrice Chotard [STMPE_IDX_GPEDR_CSB] = STMPE24XX_REG_GPEDR_CSB, 98027e34995SRabin Vincent [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB, 98127e34995SRabin Vincent }; 98227e34995SRabin Vincent 98327e34995SRabin Vincent static struct stmpe_variant_block stmpe24xx_blocks[] = { 98427e34995SRabin Vincent { 98527e34995SRabin Vincent .cell = &stmpe_gpio_cell, 98627e34995SRabin Vincent .irq = STMPE24XX_IRQ_GPIOC, 98727e34995SRabin Vincent .block = STMPE_BLOCK_GPIO, 98827e34995SRabin Vincent }, 98927e34995SRabin Vincent { 99027e34995SRabin Vincent .cell = &stmpe_keypad_cell, 99127e34995SRabin Vincent .irq = STMPE24XX_IRQ_KEYPAD, 99227e34995SRabin Vincent .block = STMPE_BLOCK_KEYPAD, 99327e34995SRabin Vincent }, 994b273c5e0SLinus Walleij { 995b273c5e0SLinus Walleij .cell = &stmpe_pwm_cell, 996b273c5e0SLinus Walleij .irq = STMPE24XX_IRQ_PWM0, 997b273c5e0SLinus Walleij .block = STMPE_BLOCK_PWM, 998b273c5e0SLinus Walleij }, 99927e34995SRabin Vincent }; 100027e34995SRabin Vincent 100127e34995SRabin Vincent static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks, 100227e34995SRabin Vincent bool enable) 100327e34995SRabin Vincent { 100427e34995SRabin Vincent unsigned int mask = 0; 100527e34995SRabin Vincent 100627e34995SRabin Vincent if (blocks & STMPE_BLOCK_GPIO) 100727e34995SRabin Vincent mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO; 100827e34995SRabin Vincent 100927e34995SRabin Vincent if (blocks & STMPE_BLOCK_KEYPAD) 101027e34995SRabin Vincent mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC; 101127e34995SRabin Vincent 10120f4be8cfSPatrice Chotard return __stmpe_set_bits(stmpe, stmpe->regs[STMPE_IDX_SYS_CTRL], mask, 101327e34995SRabin Vincent enable ? mask : 0); 101427e34995SRabin Vincent } 101527e34995SRabin Vincent 101627e34995SRabin Vincent static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block) 101727e34995SRabin Vincent { 101827e34995SRabin Vincent switch (block) { 101927e34995SRabin Vincent case STMPE_BLOCK_ROTATOR: 102027e34995SRabin Vincent return 2; 102127e34995SRabin Vincent 102227e34995SRabin Vincent case STMPE_BLOCK_KEYPAD: 1023f6d10341SLinus Walleij case STMPE_BLOCK_PWM: 102427e34995SRabin Vincent return 1; 102527e34995SRabin Vincent 102627e34995SRabin Vincent case STMPE_BLOCK_GPIO: 102727e34995SRabin Vincent default: 102827e34995SRabin Vincent return 0; 102927e34995SRabin Vincent } 103027e34995SRabin Vincent } 103127e34995SRabin Vincent 103227e34995SRabin Vincent static struct stmpe_variant_info stmpe2401 = { 103327e34995SRabin Vincent .name = "stmpe2401", 103427e34995SRabin Vincent .id_val = 0x0101, 103527e34995SRabin Vincent .id_mask = 0xffff, 103627e34995SRabin Vincent .num_gpios = 24, 103727e34995SRabin Vincent .af_bits = 2, 103827e34995SRabin Vincent .regs = stmpe24xx_regs, 103927e34995SRabin Vincent .blocks = stmpe24xx_blocks, 104027e34995SRabin Vincent .num_blocks = ARRAY_SIZE(stmpe24xx_blocks), 104127e34995SRabin Vincent .num_irqs = STMPE24XX_NR_INTERNAL_IRQS, 104227e34995SRabin Vincent .enable = stmpe24xx_enable, 104327e34995SRabin Vincent .get_altfunc = stmpe24xx_get_altfunc, 104427e34995SRabin Vincent }; 104527e34995SRabin Vincent 104627e34995SRabin Vincent static struct stmpe_variant_info stmpe2403 = { 104727e34995SRabin Vincent .name = "stmpe2403", 104827e34995SRabin Vincent .id_val = 0x0120, 104927e34995SRabin Vincent .id_mask = 0xffff, 105027e34995SRabin Vincent .num_gpios = 24, 105127e34995SRabin Vincent .af_bits = 2, 105227e34995SRabin Vincent .regs = stmpe24xx_regs, 105327e34995SRabin Vincent .blocks = stmpe24xx_blocks, 105427e34995SRabin Vincent .num_blocks = ARRAY_SIZE(stmpe24xx_blocks), 105527e34995SRabin Vincent .num_irqs = STMPE24XX_NR_INTERNAL_IRQS, 105627e34995SRabin Vincent .enable = stmpe24xx_enable, 105727e34995SRabin Vincent .get_altfunc = stmpe24xx_get_altfunc, 10585981f4e6SSundar R Iyer .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */ 105927e34995SRabin Vincent }; 106027e34995SRabin Vincent 1061e31f9b82SChris Blair static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = { 10621cda2394SViresh Kumar [STMPE610] = &stmpe610, 10637f7f4ea1SViresh Kumar [STMPE801] = &stmpe801, 106427e34995SRabin Vincent [STMPE811] = &stmpe811, 10656bb9f0d9SPatrice Chotard [STMPE1600] = &stmpe1600, 106627e34995SRabin Vincent [STMPE1601] = &stmpe1601, 1067230f13a5SJean-Nicolas Graux [STMPE1801] = &stmpe1801, 106827e34995SRabin Vincent [STMPE2401] = &stmpe2401, 106927e34995SRabin Vincent [STMPE2403] = &stmpe2403, 107027e34995SRabin Vincent }; 107127e34995SRabin Vincent 1072e31f9b82SChris Blair /* 1073e31f9b82SChris Blair * These devices can be connected in a 'no-irq' configuration - the irq pin 1074e31f9b82SChris Blair * is not used and the device cannot interrupt the CPU. Here we only list 1075e31f9b82SChris Blair * devices which support this configuration - the driver will fail probing 1076e31f9b82SChris Blair * for any devices not listed here which are configured in this way. 1077e31f9b82SChris Blair */ 1078e31f9b82SChris Blair static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = { 1079e31f9b82SChris Blair [STMPE801] = &stmpe801_noirq, 1080e31f9b82SChris Blair }; 1081e31f9b82SChris Blair 108227e34995SRabin Vincent static irqreturn_t stmpe_irq(int irq, void *data) 108327e34995SRabin Vincent { 108427e34995SRabin Vincent struct stmpe *stmpe = data; 108527e34995SRabin Vincent struct stmpe_variant_info *variant = stmpe->variant; 108627e34995SRabin Vincent int num = DIV_ROUND_UP(variant->num_irqs, 8); 1087230f13a5SJean-Nicolas Graux u8 israddr; 10887929fa77SLee Jones u8 isr[3]; 108927e34995SRabin Vincent int ret; 109027e34995SRabin Vincent int i; 109127e34995SRabin Vincent 10926bb9f0d9SPatrice Chotard if (variant->id_val == STMPE801_ID || 10936bb9f0d9SPatrice Chotard variant->id_val == STMPE1600_ID) { 109476f93992SLee Jones int base = irq_create_mapping(stmpe->domain, 0); 109576f93992SLee Jones 109676f93992SLee Jones handle_nested_irq(base); 10977f7f4ea1SViresh Kumar return IRQ_HANDLED; 10987f7f4ea1SViresh Kumar } 10997f7f4ea1SViresh Kumar 1100230f13a5SJean-Nicolas Graux if (variant->id_val == STMPE1801_ID) 1101230f13a5SJean-Nicolas Graux israddr = stmpe->regs[STMPE_IDX_ISR_LSB]; 1102230f13a5SJean-Nicolas Graux else 1103230f13a5SJean-Nicolas Graux israddr = stmpe->regs[STMPE_IDX_ISR_MSB]; 1104230f13a5SJean-Nicolas Graux 110527e34995SRabin Vincent ret = stmpe_block_read(stmpe, israddr, num, isr); 110627e34995SRabin Vincent if (ret < 0) 110727e34995SRabin Vincent return IRQ_NONE; 110827e34995SRabin Vincent 110927e34995SRabin Vincent for (i = 0; i < num; i++) { 111027e34995SRabin Vincent int bank = num - i - 1; 111127e34995SRabin Vincent u8 status = isr[i]; 111227e34995SRabin Vincent u8 clear; 111327e34995SRabin Vincent 111427e34995SRabin Vincent status &= stmpe->ier[bank]; 111527e34995SRabin Vincent if (!status) 111627e34995SRabin Vincent continue; 111727e34995SRabin Vincent 111827e34995SRabin Vincent clear = status; 111927e34995SRabin Vincent while (status) { 112027e34995SRabin Vincent int bit = __ffs(status); 112127e34995SRabin Vincent int line = bank * 8 + bit; 112276f93992SLee Jones int nestedirq = irq_create_mapping(stmpe->domain, line); 112327e34995SRabin Vincent 112476f93992SLee Jones handle_nested_irq(nestedirq); 112527e34995SRabin Vincent status &= ~(1 << bit); 112627e34995SRabin Vincent } 112727e34995SRabin Vincent 112827e34995SRabin Vincent stmpe_reg_write(stmpe, israddr + i, clear); 112927e34995SRabin Vincent } 113027e34995SRabin Vincent 113127e34995SRabin Vincent return IRQ_HANDLED; 113227e34995SRabin Vincent } 113327e34995SRabin Vincent 113443b8c084SMark Brown static void stmpe_irq_lock(struct irq_data *data) 113527e34995SRabin Vincent { 113643b8c084SMark Brown struct stmpe *stmpe = irq_data_get_irq_chip_data(data); 113727e34995SRabin Vincent 113827e34995SRabin Vincent mutex_lock(&stmpe->irq_lock); 113927e34995SRabin Vincent } 114027e34995SRabin Vincent 114143b8c084SMark Brown static void stmpe_irq_sync_unlock(struct irq_data *data) 114227e34995SRabin Vincent { 114343b8c084SMark Brown struct stmpe *stmpe = irq_data_get_irq_chip_data(data); 114427e34995SRabin Vincent struct stmpe_variant_info *variant = stmpe->variant; 114527e34995SRabin Vincent int num = DIV_ROUND_UP(variant->num_irqs, 8); 114627e34995SRabin Vincent int i; 114727e34995SRabin Vincent 114827e34995SRabin Vincent for (i = 0; i < num; i++) { 114927e34995SRabin Vincent u8 new = stmpe->ier[i]; 115027e34995SRabin Vincent u8 old = stmpe->oldier[i]; 115127e34995SRabin Vincent 115227e34995SRabin Vincent if (new == old) 115327e34995SRabin Vincent continue; 115427e34995SRabin Vincent 115527e34995SRabin Vincent stmpe->oldier[i] = new; 1156897ac667SPatrice Chotard stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB + i], new); 115727e34995SRabin Vincent } 115827e34995SRabin Vincent 115927e34995SRabin Vincent mutex_unlock(&stmpe->irq_lock); 116027e34995SRabin Vincent } 116127e34995SRabin Vincent 116243b8c084SMark Brown static void stmpe_irq_mask(struct irq_data *data) 116327e34995SRabin Vincent { 116443b8c084SMark Brown struct stmpe *stmpe = irq_data_get_irq_chip_data(data); 116576f93992SLee Jones int offset = data->hwirq; 116627e34995SRabin Vincent int regoffset = offset / 8; 116727e34995SRabin Vincent int mask = 1 << (offset % 8); 116827e34995SRabin Vincent 116927e34995SRabin Vincent stmpe->ier[regoffset] &= ~mask; 117027e34995SRabin Vincent } 117127e34995SRabin Vincent 117243b8c084SMark Brown static void stmpe_irq_unmask(struct irq_data *data) 117327e34995SRabin Vincent { 117443b8c084SMark Brown struct stmpe *stmpe = irq_data_get_irq_chip_data(data); 117576f93992SLee Jones int offset = data->hwirq; 117627e34995SRabin Vincent int regoffset = offset / 8; 117727e34995SRabin Vincent int mask = 1 << (offset % 8); 117827e34995SRabin Vincent 117927e34995SRabin Vincent stmpe->ier[regoffset] |= mask; 118027e34995SRabin Vincent } 118127e34995SRabin Vincent 118227e34995SRabin Vincent static struct irq_chip stmpe_irq_chip = { 118327e34995SRabin Vincent .name = "stmpe", 118443b8c084SMark Brown .irq_bus_lock = stmpe_irq_lock, 118543b8c084SMark Brown .irq_bus_sync_unlock = stmpe_irq_sync_unlock, 118643b8c084SMark Brown .irq_mask = stmpe_irq_mask, 118743b8c084SMark Brown .irq_unmask = stmpe_irq_unmask, 118827e34995SRabin Vincent }; 118927e34995SRabin Vincent 119076f93992SLee Jones static int stmpe_irq_map(struct irq_domain *d, unsigned int virq, 119176f93992SLee Jones irq_hw_number_t hwirq) 119227e34995SRabin Vincent { 119376f93992SLee Jones struct stmpe *stmpe = d->host_data; 11947f7f4ea1SViresh Kumar struct irq_chip *chip = NULL; 119527e34995SRabin Vincent 11967f7f4ea1SViresh Kumar if (stmpe->variant->id_val != STMPE801_ID) 11977f7f4ea1SViresh Kumar chip = &stmpe_irq_chip; 11987f7f4ea1SViresh Kumar 119976f93992SLee Jones irq_set_chip_data(virq, stmpe); 120076f93992SLee Jones irq_set_chip_and_handler(virq, chip, handle_edge_irq); 120176f93992SLee Jones irq_set_nested_thread(virq, 1); 120276f93992SLee Jones irq_set_noprobe(virq); 120327e34995SRabin Vincent 120427e34995SRabin Vincent return 0; 120527e34995SRabin Vincent } 120627e34995SRabin Vincent 120776f93992SLee Jones static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq) 120827e34995SRabin Vincent { 120976f93992SLee Jones irq_set_chip_and_handler(virq, NULL, NULL); 121076f93992SLee Jones irq_set_chip_data(virq, NULL); 121127e34995SRabin Vincent } 121276f93992SLee Jones 12137ce7b26fSKrzysztof Kozlowski static const struct irq_domain_ops stmpe_irq_ops = { 121476f93992SLee Jones .map = stmpe_irq_map, 121576f93992SLee Jones .unmap = stmpe_irq_unmap, 121676f93992SLee Jones .xlate = irq_domain_xlate_twocell, 121776f93992SLee Jones }; 121876f93992SLee Jones 1219612b95cdSGreg Kroah-Hartman static int stmpe_irq_init(struct stmpe *stmpe, struct device_node *np) 122076f93992SLee Jones { 1221b20a4371SLee Jones int base = 0; 122276f93992SLee Jones int num_irqs = stmpe->variant->num_irqs; 122376f93992SLee Jones 1224b20a4371SLee Jones stmpe->domain = irq_domain_add_simple(np, num_irqs, base, 1225b20a4371SLee Jones &stmpe_irq_ops, stmpe); 122676f93992SLee Jones if (!stmpe->domain) { 122776f93992SLee Jones dev_err(stmpe->dev, "Failed to create irqdomain\n"); 122876f93992SLee Jones return -ENOSYS; 122976f93992SLee Jones } 123076f93992SLee Jones 123176f93992SLee Jones return 0; 123227e34995SRabin Vincent } 123327e34995SRabin Vincent 1234612b95cdSGreg Kroah-Hartman static int stmpe_chip_init(struct stmpe *stmpe) 123527e34995SRabin Vincent { 123627e34995SRabin Vincent unsigned int irq_trigger = stmpe->pdata->irq_trigger; 12375981f4e6SSundar R Iyer int autosleep_timeout = stmpe->pdata->autosleep_timeout; 123827e34995SRabin Vincent struct stmpe_variant_info *variant = stmpe->variant; 1239e31f9b82SChris Blair u8 icr = 0; 124027e34995SRabin Vincent unsigned int id; 124127e34995SRabin Vincent u8 data[2]; 124227e34995SRabin Vincent int ret; 124327e34995SRabin Vincent 124427e34995SRabin Vincent ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID], 124527e34995SRabin Vincent ARRAY_SIZE(data), data); 124627e34995SRabin Vincent if (ret < 0) 124727e34995SRabin Vincent return ret; 124827e34995SRabin Vincent 124927e34995SRabin Vincent id = (data[0] << 8) | data[1]; 125027e34995SRabin Vincent if ((id & variant->id_mask) != variant->id_val) { 125127e34995SRabin Vincent dev_err(stmpe->dev, "unknown chip id: %#x\n", id); 125227e34995SRabin Vincent return -EINVAL; 125327e34995SRabin Vincent } 125427e34995SRabin Vincent 125527e34995SRabin Vincent dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id); 125627e34995SRabin Vincent 125727e34995SRabin Vincent /* Disable all modules -- subdrivers should enable what they need. */ 125827e34995SRabin Vincent ret = stmpe_disable(stmpe, ~0); 125927e34995SRabin Vincent if (ret) 126027e34995SRabin Vincent return ret; 126127e34995SRabin Vincent 1262c4dd1ba3SPatrice Chotard ret = stmpe_reset(stmpe); 1263230f13a5SJean-Nicolas Graux if (ret < 0) 1264230f13a5SJean-Nicolas Graux return ret; 1265230f13a5SJean-Nicolas Graux 1266e31f9b82SChris Blair if (stmpe->irq >= 0) { 12676bb9f0d9SPatrice Chotard if (id == STMPE801_ID || id == STMPE1600_ID) 1268c16bee78SPatrice Chotard icr = STMPE_SYS_CTRL_INT_EN; 12697f7f4ea1SViresh Kumar else 12707f7f4ea1SViresh Kumar icr = STMPE_ICR_LSB_GIM; 12717f7f4ea1SViresh Kumar 12726bb9f0d9SPatrice Chotard /* STMPE801 and STMPE1600 don't support Edge interrupts */ 12736bb9f0d9SPatrice Chotard if (id != STMPE801_ID && id != STMPE1600_ID) { 127427e34995SRabin Vincent if (irq_trigger == IRQF_TRIGGER_FALLING || 127527e34995SRabin Vincent irq_trigger == IRQF_TRIGGER_RISING) 127627e34995SRabin Vincent icr |= STMPE_ICR_LSB_EDGE; 12777f7f4ea1SViresh Kumar } 127827e34995SRabin Vincent 127927e34995SRabin Vincent if (irq_trigger == IRQF_TRIGGER_RISING || 12807f7f4ea1SViresh Kumar irq_trigger == IRQF_TRIGGER_HIGH) { 12816bb9f0d9SPatrice Chotard if (id == STMPE801_ID || id == STMPE1600_ID) 1282c16bee78SPatrice Chotard icr |= STMPE_SYS_CTRL_INT_HI; 12837f7f4ea1SViresh Kumar else 128427e34995SRabin Vincent icr |= STMPE_ICR_LSB_HIGH; 12857f7f4ea1SViresh Kumar } 1286e31f9b82SChris Blair } 128727e34995SRabin Vincent 12885981f4e6SSundar R Iyer if (stmpe->pdata->autosleep) { 12895981f4e6SSundar R Iyer ret = stmpe_autosleep(stmpe, autosleep_timeout); 12905981f4e6SSundar R Iyer if (ret) 12915981f4e6SSundar R Iyer return ret; 12925981f4e6SSundar R Iyer } 12935981f4e6SSundar R Iyer 129427e34995SRabin Vincent return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr); 129527e34995SRabin Vincent } 129627e34995SRabin Vincent 12976bbb3c4cSGeert Uytterhoeven static int stmpe_add_device(struct stmpe *stmpe, const struct mfd_cell *cell) 129827e34995SRabin Vincent { 129927e34995SRabin Vincent return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1, 13009e9dc7d9SLinus Walleij NULL, 0, stmpe->domain); 130127e34995SRabin Vincent } 130227e34995SRabin Vincent 1303612b95cdSGreg Kroah-Hartman static int stmpe_devices_init(struct stmpe *stmpe) 130427e34995SRabin Vincent { 130527e34995SRabin Vincent struct stmpe_variant_info *variant = stmpe->variant; 130627e34995SRabin Vincent unsigned int platform_blocks = stmpe->pdata->blocks; 130727e34995SRabin Vincent int ret = -EINVAL; 13087da0cbfcSLee Jones int i, j; 130927e34995SRabin Vincent 131027e34995SRabin Vincent for (i = 0; i < variant->num_blocks; i++) { 131127e34995SRabin Vincent struct stmpe_variant_block *block = &variant->blocks[i]; 131227e34995SRabin Vincent 131327e34995SRabin Vincent if (!(platform_blocks & block->block)) 131427e34995SRabin Vincent continue; 131527e34995SRabin Vincent 13167da0cbfcSLee Jones for (j = 0; j < block->cell->num_resources; j++) { 13177da0cbfcSLee Jones struct resource *res = 13187da0cbfcSLee Jones (struct resource *) &block->cell->resources[j]; 13197da0cbfcSLee Jones 13207da0cbfcSLee Jones /* Dynamically fill in a variant's IRQ. */ 13217da0cbfcSLee Jones if (res->flags & IORESOURCE_IRQ) 13227da0cbfcSLee Jones res->start = res->end = block->irq + j; 13237da0cbfcSLee Jones } 13247da0cbfcSLee Jones 132527e34995SRabin Vincent platform_blocks &= ~block->block; 13267da0cbfcSLee Jones ret = stmpe_add_device(stmpe, block->cell); 132727e34995SRabin Vincent if (ret) 132827e34995SRabin Vincent return ret; 132927e34995SRabin Vincent } 133027e34995SRabin Vincent 133127e34995SRabin Vincent if (platform_blocks) 133227e34995SRabin Vincent dev_warn(stmpe->dev, 133327e34995SRabin Vincent "platform wants blocks (%#x) not present on variant", 133427e34995SRabin Vincent platform_blocks); 133527e34995SRabin Vincent 133627e34995SRabin Vincent return ret; 133727e34995SRabin Vincent } 133827e34995SRabin Vincent 1339a9c4055dSMark Brown static void stmpe_of_probe(struct stmpe_platform_data *pdata, 1340a9c4055dSMark Brown struct device_node *np) 1341909582caSLee Jones { 1342909582caSLee Jones struct device_node *child; 1343909582caSLee Jones 1344408a3fa8SGabriel Fernandez pdata->id = of_alias_get_id(np, "stmpe-i2c"); 1345408a3fa8SGabriel Fernandez if (pdata->id < 0) 1346ac713cc9SVipul Kumar Samar pdata->id = -1; 1347408a3fa8SGabriel Fernandez 1348851ec596SSean Cross pdata->irq_gpio = of_get_named_gpio_flags(np, "irq-gpio", 0, 1349851ec596SSean Cross &pdata->irq_trigger); 1350851ec596SSean Cross if (gpio_is_valid(pdata->irq_gpio)) 1351851ec596SSean Cross pdata->irq_over_gpio = 1; 1352851ec596SSean Cross else 1353ac713cc9SVipul Kumar Samar pdata->irq_trigger = IRQF_TRIGGER_NONE; 1354ac713cc9SVipul Kumar Samar 1355909582caSLee Jones of_property_read_u32(np, "st,autosleep-timeout", 1356909582caSLee Jones &pdata->autosleep_timeout); 1357909582caSLee Jones 1358909582caSLee Jones pdata->autosleep = (pdata->autosleep_timeout) ? true : false; 1359909582caSLee Jones 1360909582caSLee Jones for_each_child_of_node(np, child) { 136138df91ccSRob Herring if (of_node_name_eq(child, "stmpe_gpio")) { 1362909582caSLee Jones pdata->blocks |= STMPE_BLOCK_GPIO; 136338df91ccSRob Herring } else if (of_node_name_eq(child, "stmpe_keypad")) { 1364909582caSLee Jones pdata->blocks |= STMPE_BLOCK_KEYPAD; 136538df91ccSRob Herring } else if (of_node_name_eq(child, "stmpe_touchscreen")) { 1366909582caSLee Jones pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN; 136738df91ccSRob Herring } else if (of_node_name_eq(child, "stmpe_adc")) { 1368909582caSLee Jones pdata->blocks |= STMPE_BLOCK_ADC; 136938df91ccSRob Herring } else if (of_node_name_eq(child, "stmpe_pwm")) { 1370ac713cc9SVipul Kumar Samar pdata->blocks |= STMPE_BLOCK_PWM; 137138df91ccSRob Herring } else if (of_node_name_eq(child, "stmpe_rotator")) { 1372ac713cc9SVipul Kumar Samar pdata->blocks |= STMPE_BLOCK_ROTATOR; 1373909582caSLee Jones } 1374909582caSLee Jones } 1375909582caSLee Jones } 1376909582caSLee Jones 13771a6e4b74SViresh Kumar /* Called from client specific probe routines */ 1378c00572bcSLee Jones int stmpe_probe(struct stmpe_client_info *ci, enum stmpe_partnum partnum) 1379208c4343SSundar Iyer { 1380fc1882dcSLinus Walleij struct stmpe_platform_data *pdata; 1381909582caSLee Jones struct device_node *np = ci->dev->of_node; 138227e34995SRabin Vincent struct stmpe *stmpe; 138327e34995SRabin Vincent int ret; 13846377cfa3SStefan Agner u32 val; 138527e34995SRabin Vincent 1386cb5faba9SViresh Kumar pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL); 138727e34995SRabin Vincent if (!pdata) 1388909582caSLee Jones return -ENOMEM; 1389909582caSLee Jones 1390909582caSLee Jones stmpe_of_probe(pdata, np); 1391a200e320SGabriel Fernandez 1392a200e320SGabriel Fernandez if (of_find_property(np, "interrupts", NULL) == NULL) 1393a200e320SGabriel Fernandez ci->irq = -1; 139427e34995SRabin Vincent 1395cb5faba9SViresh Kumar stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL); 139627e34995SRabin Vincent if (!stmpe) 139727e34995SRabin Vincent return -ENOMEM; 139827e34995SRabin Vincent 139927e34995SRabin Vincent mutex_init(&stmpe->irq_lock); 140027e34995SRabin Vincent mutex_init(&stmpe->lock); 140127e34995SRabin Vincent 14026377cfa3SStefan Agner if (!of_property_read_u32(np, "st,sample-time", &val)) 14036377cfa3SStefan Agner stmpe->sample_time = val; 14046377cfa3SStefan Agner if (!of_property_read_u32(np, "st,mod-12b", &val)) 14056377cfa3SStefan Agner stmpe->mod_12b = val; 14066377cfa3SStefan Agner if (!of_property_read_u32(np, "st,ref-sel", &val)) 14076377cfa3SStefan Agner stmpe->ref_sel = val; 14086377cfa3SStefan Agner if (!of_property_read_u32(np, "st,adc-freq", &val)) 14096377cfa3SStefan Agner stmpe->adc_freq = val; 14106377cfa3SStefan Agner 14111a6e4b74SViresh Kumar stmpe->dev = ci->dev; 14121a6e4b74SViresh Kumar stmpe->client = ci->client; 141327e34995SRabin Vincent stmpe->pdata = pdata; 14141a6e4b74SViresh Kumar stmpe->ci = ci; 14151a6e4b74SViresh Kumar stmpe->partnum = partnum; 14161a6e4b74SViresh Kumar stmpe->variant = stmpe_variant_info[partnum]; 141727e34995SRabin Vincent stmpe->regs = stmpe->variant->regs; 141827e34995SRabin Vincent stmpe->num_gpios = stmpe->variant->num_gpios; 14199c9e3214SLinus Walleij stmpe->vcc = devm_regulator_get_optional(ci->dev, "vcc"); 14209c9e3214SLinus Walleij if (!IS_ERR(stmpe->vcc)) { 14219c9e3214SLinus Walleij ret = regulator_enable(stmpe->vcc); 14229c9e3214SLinus Walleij if (ret) 14239c9e3214SLinus Walleij dev_warn(ci->dev, "failed to enable VCC supply\n"); 14249c9e3214SLinus Walleij } 14259c9e3214SLinus Walleij stmpe->vio = devm_regulator_get_optional(ci->dev, "vio"); 14269c9e3214SLinus Walleij if (!IS_ERR(stmpe->vio)) { 14279c9e3214SLinus Walleij ret = regulator_enable(stmpe->vio); 14289c9e3214SLinus Walleij if (ret) 14299c9e3214SLinus Walleij dev_warn(ci->dev, "failed to enable VIO supply\n"); 14309c9e3214SLinus Walleij } 14311a6e4b74SViresh Kumar dev_set_drvdata(stmpe->dev, stmpe); 143227e34995SRabin Vincent 14331a6e4b74SViresh Kumar if (ci->init) 14341a6e4b74SViresh Kumar ci->init(stmpe); 143527e34995SRabin Vincent 143673de16dbSViresh Kumar if (pdata->irq_over_gpio) { 1437cb5faba9SViresh Kumar ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio, 1438cb5faba9SViresh Kumar GPIOF_DIR_IN, "stmpe"); 143973de16dbSViresh Kumar if (ret) { 144073de16dbSViresh Kumar dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n", 144173de16dbSViresh Kumar ret); 1442cb5faba9SViresh Kumar return ret; 144373de16dbSViresh Kumar } 144473de16dbSViresh Kumar 144573de16dbSViresh Kumar stmpe->irq = gpio_to_irq(pdata->irq_gpio); 144673de16dbSViresh Kumar } else { 14471a6e4b74SViresh Kumar stmpe->irq = ci->irq; 144873de16dbSViresh Kumar } 144973de16dbSViresh Kumar 1450e31f9b82SChris Blair if (stmpe->irq < 0) { 1451e31f9b82SChris Blair /* use alternate variant info for no-irq mode, if supported */ 1452e31f9b82SChris Blair dev_info(stmpe->dev, 1453e31f9b82SChris Blair "%s configured in no-irq mode by platform data\n", 1454e31f9b82SChris Blair stmpe->variant->name); 1455e31f9b82SChris Blair if (!stmpe_noirq_variant_info[stmpe->partnum]) { 1456e31f9b82SChris Blair dev_err(stmpe->dev, 1457e31f9b82SChris Blair "%s does not support no-irq mode!\n", 1458e31f9b82SChris Blair stmpe->variant->name); 1459cb5faba9SViresh Kumar return -ENODEV; 1460e31f9b82SChris Blair } 1461e31f9b82SChris Blair stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum]; 1462ac713cc9SVipul Kumar Samar } else if (pdata->irq_trigger == IRQF_TRIGGER_NONE) { 14631a5595cbSJavier Martinez Canillas pdata->irq_trigger = irq_get_trigger_type(stmpe->irq); 1464e31f9b82SChris Blair } 1465e31f9b82SChris Blair 146627e34995SRabin Vincent ret = stmpe_chip_init(stmpe); 146727e34995SRabin Vincent if (ret) 1468cb5faba9SViresh Kumar return ret; 146927e34995SRabin Vincent 1470e31f9b82SChris Blair if (stmpe->irq >= 0) { 1471909582caSLee Jones ret = stmpe_irq_init(stmpe, np); 147227e34995SRabin Vincent if (ret) 1473cb5faba9SViresh Kumar return ret; 147427e34995SRabin Vincent 1475cb5faba9SViresh Kumar ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL, 1476cb5faba9SViresh Kumar stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT, 1477e31f9b82SChris Blair "stmpe", stmpe); 147827e34995SRabin Vincent if (ret) { 1479e31f9b82SChris Blair dev_err(stmpe->dev, "failed to request IRQ: %d\n", 1480e31f9b82SChris Blair ret); 1481cb5faba9SViresh Kumar return ret; 148227e34995SRabin Vincent } 1483e31f9b82SChris Blair } 148427e34995SRabin Vincent 148527e34995SRabin Vincent ret = stmpe_devices_init(stmpe); 1486cb5faba9SViresh Kumar if (!ret) 148727e34995SRabin Vincent return 0; 148827e34995SRabin Vincent 1489cb5faba9SViresh Kumar dev_err(stmpe->dev, "failed to add children\n"); 149027e34995SRabin Vincent mfd_remove_devices(stmpe->dev); 1491cb5faba9SViresh Kumar 149227e34995SRabin Vincent return ret; 149327e34995SRabin Vincent } 149427e34995SRabin Vincent 14951a6e4b74SViresh Kumar int stmpe_remove(struct stmpe *stmpe) 149627e34995SRabin Vincent { 14979c9e3214SLinus Walleij if (!IS_ERR(stmpe->vio)) 14989c9e3214SLinus Walleij regulator_disable(stmpe->vio); 14999c9e3214SLinus Walleij if (!IS_ERR(stmpe->vcc)) 15009c9e3214SLinus Walleij regulator_disable(stmpe->vcc); 15019c9e3214SLinus Walleij 15026377cfa3SStefan Agner __stmpe_disable(stmpe, STMPE_BLOCK_ADC); 15036377cfa3SStefan Agner 150427e34995SRabin Vincent mfd_remove_devices(stmpe->dev); 150527e34995SRabin Vincent 150627e34995SRabin Vincent return 0; 150727e34995SRabin Vincent } 150827e34995SRabin Vincent 1509208c4343SSundar Iyer #ifdef CONFIG_PM 15101a6e4b74SViresh Kumar static int stmpe_suspend(struct device *dev) 15111a6e4b74SViresh Kumar { 15121a6e4b74SViresh Kumar struct stmpe *stmpe = dev_get_drvdata(dev); 15131a6e4b74SViresh Kumar 1514e31f9b82SChris Blair if (stmpe->irq >= 0 && device_may_wakeup(dev)) 15151a6e4b74SViresh Kumar enable_irq_wake(stmpe->irq); 15161a6e4b74SViresh Kumar 15171a6e4b74SViresh Kumar return 0; 15181a6e4b74SViresh Kumar } 15191a6e4b74SViresh Kumar 15201a6e4b74SViresh Kumar static int stmpe_resume(struct device *dev) 15211a6e4b74SViresh Kumar { 15221a6e4b74SViresh Kumar struct stmpe *stmpe = dev_get_drvdata(dev); 15231a6e4b74SViresh Kumar 1524e31f9b82SChris Blair if (stmpe->irq >= 0 && device_may_wakeup(dev)) 15251a6e4b74SViresh Kumar disable_irq_wake(stmpe->irq); 15261a6e4b74SViresh Kumar 15271a6e4b74SViresh Kumar return 0; 15281a6e4b74SViresh Kumar } 15291a6e4b74SViresh Kumar 15301a6e4b74SViresh Kumar const struct dev_pm_ops stmpe_dev_pm_ops = { 1531208c4343SSundar Iyer .suspend = stmpe_suspend, 1532208c4343SSundar Iyer .resume = stmpe_resume, 1533208c4343SSundar Iyer }; 1534208c4343SSundar Iyer #endif 1535