2 * ST Microelectronics MFD: stmpe's driver
4 * Copyright (C) ST-Ericsson SA 2010
6 * License Terms: GNU General Public License, version 2
7 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
10 #include <linux/err.h>
11 #include <linux/gpio.h>
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
18 #include <linux/of_gpio.h>
20 #include <linux/slab.h>
21 #include <linux/mfd/core.h>
24 static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
26 return stmpe->variant->enable(stmpe, blocks, true);
29 static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
31 return stmpe->variant->enable(stmpe, blocks, false);
34 static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
38 ret = stmpe->ci->read_byte(stmpe, reg);
40 dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
42 dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
47 static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
51 dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
53 ret = stmpe->ci->write_byte(stmpe, reg, val);
55 dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
60 static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
64 ret = __stmpe_reg_read(stmpe, reg);
71 return __stmpe_reg_write(stmpe, reg, ret);
74 static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
79 ret = stmpe->ci->read_block(stmpe, reg, length, values);
81 dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
83 dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
84 stmpe_dump_bytes("stmpe rd: ", values, length);
89 static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
94 dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
95 stmpe_dump_bytes("stmpe wr: ", values, length);
97 ret = stmpe->ci->write_block(stmpe, reg, length, values);
99 dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
105 * stmpe_enable - enable blocks on an STMPE device
106 * @stmpe: Device to work on
107 * @blocks: Mask of blocks (enum stmpe_block values) to enable
109 int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
113 mutex_lock(&stmpe->lock);
114 ret = __stmpe_enable(stmpe, blocks);
115 mutex_unlock(&stmpe->lock);
119 EXPORT_SYMBOL_GPL(stmpe_enable);
122 * stmpe_disable - disable blocks on an STMPE device
123 * @stmpe: Device to work on
124 * @blocks: Mask of blocks (enum stmpe_block values) to enable
126 int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
130 mutex_lock(&stmpe->lock);
131 ret = __stmpe_disable(stmpe, blocks);
132 mutex_unlock(&stmpe->lock);
136 EXPORT_SYMBOL_GPL(stmpe_disable);
139 * stmpe_reg_read() - read a single STMPE register
140 * @stmpe: Device to read from
141 * @reg: Register to read
143 int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
147 mutex_lock(&stmpe->lock);
148 ret = __stmpe_reg_read(stmpe, reg);
149 mutex_unlock(&stmpe->lock);
153 EXPORT_SYMBOL_GPL(stmpe_reg_read);
156 * stmpe_reg_write() - write a single STMPE register
157 * @stmpe: Device to write to
158 * @reg: Register to write
159 * @val: Value to write
161 int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
165 mutex_lock(&stmpe->lock);
166 ret = __stmpe_reg_write(stmpe, reg, val);
167 mutex_unlock(&stmpe->lock);
171 EXPORT_SYMBOL_GPL(stmpe_reg_write);
174 * stmpe_set_bits() - set the value of a bitfield in a STMPE register
175 * @stmpe: Device to write to
176 * @reg: Register to write
177 * @mask: Mask of bits to set
180 int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
184 mutex_lock(&stmpe->lock);
185 ret = __stmpe_set_bits(stmpe, reg, mask, val);
186 mutex_unlock(&stmpe->lock);
190 EXPORT_SYMBOL_GPL(stmpe_set_bits);
193 * stmpe_block_read() - read multiple STMPE registers
194 * @stmpe: Device to read from
195 * @reg: First register
196 * @length: Number of registers
197 * @values: Buffer to write to
199 int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
203 mutex_lock(&stmpe->lock);
204 ret = __stmpe_block_read(stmpe, reg, length, values);
205 mutex_unlock(&stmpe->lock);
209 EXPORT_SYMBOL_GPL(stmpe_block_read);
212 * stmpe_block_write() - write multiple STMPE registers
213 * @stmpe: Device to write to
214 * @reg: First register
215 * @length: Number of registers
216 * @values: Values to write
218 int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
223 mutex_lock(&stmpe->lock);
224 ret = __stmpe_block_write(stmpe, reg, length, values);
225 mutex_unlock(&stmpe->lock);
229 EXPORT_SYMBOL_GPL(stmpe_block_write);
232 * stmpe_set_altfunc()- set the alternate function for STMPE pins
233 * @stmpe: Device to configure
234 * @pins: Bitmask of pins to affect
235 * @block: block to enable alternate functions for
237 * @pins is assumed to have a bit set for each of the bits whose alternate
238 * function is to be changed, numbered according to the GPIOXY numbers.
240 * If the GPIO module is not enabled, this function automatically enables it in
241 * order to perform the change.
243 int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
245 struct stmpe_variant_info *variant = stmpe->variant;
246 u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
247 int af_bits = variant->af_bits;
248 int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
249 int mask = (1 << af_bits) - 1;
251 int af, afperreg, ret;
253 if (!variant->get_altfunc)
256 afperreg = 8 / af_bits;
257 mutex_lock(&stmpe->lock);
259 ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
263 ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
267 af = variant->get_altfunc(stmpe, block);
270 int pin = __ffs(pins);
271 int regoffset = numregs - (pin / afperreg) - 1;
272 int pos = (pin % afperreg) * (8 / afperreg);
274 regs[regoffset] &= ~(mask << pos);
275 regs[regoffset] |= af << pos;
280 ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
283 mutex_unlock(&stmpe->lock);
286 EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
289 * GPIO (all variants)
292 static struct resource stmpe_gpio_resources[] = {
293 /* Start and end filled dynamically */
295 .flags = IORESOURCE_IRQ,
299 static struct mfd_cell stmpe_gpio_cell = {
300 .name = "stmpe-gpio",
301 .of_compatible = "st,stmpe-gpio",
302 .resources = stmpe_gpio_resources,
303 .num_resources = ARRAY_SIZE(stmpe_gpio_resources),
306 static struct mfd_cell stmpe_gpio_cell_noirq = {
307 .name = "stmpe-gpio",
308 .of_compatible = "st,stmpe-gpio",
309 /* gpio cell resources consist of an irq only so no resources here */
313 * Keypad (1601, 2401, 2403)
316 static struct resource stmpe_keypad_resources[] = {
319 .flags = IORESOURCE_IRQ,
322 .name = "KEYPAD_OVER",
323 .flags = IORESOURCE_IRQ,
327 static struct mfd_cell stmpe_keypad_cell = {
328 .name = "stmpe-keypad",
329 .of_compatible = "st,stmpe-keypad",
330 .resources = stmpe_keypad_resources,
331 .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
337 static const u8 stmpe801_regs[] = {
338 [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
339 [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
340 [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
341 [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
342 [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
343 [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
344 [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
345 [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
349 static struct stmpe_variant_block stmpe801_blocks[] = {
351 .cell = &stmpe_gpio_cell,
353 .block = STMPE_BLOCK_GPIO,
357 static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
359 .cell = &stmpe_gpio_cell_noirq,
360 .block = STMPE_BLOCK_GPIO,
364 static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
367 if (blocks & STMPE_BLOCK_GPIO)
373 static struct stmpe_variant_info stmpe801 = {
375 .id_val = STMPE801_ID,
378 .regs = stmpe801_regs,
379 .blocks = stmpe801_blocks,
380 .num_blocks = ARRAY_SIZE(stmpe801_blocks),
381 .num_irqs = STMPE801_NR_INTERNAL_IRQS,
382 .enable = stmpe801_enable,
385 static struct stmpe_variant_info stmpe801_noirq = {
387 .id_val = STMPE801_ID,
390 .regs = stmpe801_regs,
391 .blocks = stmpe801_blocks_noirq,
392 .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
393 .enable = stmpe801_enable,
397 * Touchscreen (STMPE811 or STMPE610)
400 static struct resource stmpe_ts_resources[] = {
403 .flags = IORESOURCE_IRQ,
407 .flags = IORESOURCE_IRQ,
411 static struct mfd_cell stmpe_ts_cell = {
413 .of_compatible = "st,stmpe-ts",
414 .resources = stmpe_ts_resources,
415 .num_resources = ARRAY_SIZE(stmpe_ts_resources),
419 * STMPE811 or STMPE610
422 static const u8 stmpe811_regs[] = {
423 [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
424 [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
425 [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
426 [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
427 [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
428 [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
429 [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
430 [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
431 [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
432 [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
433 [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
434 [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
435 [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
436 [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
439 static struct stmpe_variant_block stmpe811_blocks[] = {
441 .cell = &stmpe_gpio_cell,
442 .irq = STMPE811_IRQ_GPIOC,
443 .block = STMPE_BLOCK_GPIO,
446 .cell = &stmpe_ts_cell,
447 .irq = STMPE811_IRQ_TOUCH_DET,
448 .block = STMPE_BLOCK_TOUCHSCREEN,
452 static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
455 unsigned int mask = 0;
457 if (blocks & STMPE_BLOCK_GPIO)
458 mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
460 if (blocks & STMPE_BLOCK_ADC)
461 mask |= STMPE811_SYS_CTRL2_ADC_OFF;
463 if (blocks & STMPE_BLOCK_TOUCHSCREEN)
464 mask |= STMPE811_SYS_CTRL2_TSC_OFF;
466 return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
470 static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
472 /* 0 for touchscreen, 1 for GPIO */
473 return block != STMPE_BLOCK_TOUCHSCREEN;
476 static struct stmpe_variant_info stmpe811 = {
482 .regs = stmpe811_regs,
483 .blocks = stmpe811_blocks,
484 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
485 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
486 .enable = stmpe811_enable,
487 .get_altfunc = stmpe811_get_altfunc,
490 /* Similar to 811, except number of gpios */
491 static struct stmpe_variant_info stmpe610 = {
497 .regs = stmpe811_regs,
498 .blocks = stmpe811_blocks,
499 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
500 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
501 .enable = stmpe811_enable,
502 .get_altfunc = stmpe811_get_altfunc,
509 static const u8 stmpe1601_regs[] = {
510 [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
511 [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
512 [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
513 [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
514 [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
515 [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
516 [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
517 [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
518 [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
519 [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
520 [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
521 [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
522 [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
523 [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
526 static struct stmpe_variant_block stmpe1601_blocks[] = {
528 .cell = &stmpe_gpio_cell,
529 .irq = STMPE1601_IRQ_GPIOC,
530 .block = STMPE_BLOCK_GPIO,
533 .cell = &stmpe_keypad_cell,
534 .irq = STMPE1601_IRQ_KEYPAD,
535 .block = STMPE_BLOCK_KEYPAD,
539 /* supported autosleep timeout delay (in msecs) */
540 static const int stmpe_autosleep_delay[] = {
541 4, 16, 32, 64, 128, 256, 512, 1024,
544 static int stmpe_round_timeout(int timeout)
548 for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
549 if (stmpe_autosleep_delay[i] >= timeout)
554 * requests for delays longer than supported should not return the
555 * longest supported delay
560 static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
564 if (!stmpe->variant->enable_autosleep)
567 mutex_lock(&stmpe->lock);
568 ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
569 mutex_unlock(&stmpe->lock);
575 * Both stmpe 1601/2403 support same layout for autosleep
577 static int stmpe1601_autosleep(struct stmpe *stmpe,
578 int autosleep_timeout)
582 /* choose the best available timeout */
583 timeout = stmpe_round_timeout(autosleep_timeout);
585 dev_err(stmpe->dev, "invalid timeout\n");
589 ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
590 STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
595 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
596 STPME1601_AUTOSLEEP_ENABLE,
597 STPME1601_AUTOSLEEP_ENABLE);
600 static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
603 unsigned int mask = 0;
605 if (blocks & STMPE_BLOCK_GPIO)
606 mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
608 if (blocks & STMPE_BLOCK_KEYPAD)
609 mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
611 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
615 static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
618 case STMPE_BLOCK_PWM:
621 case STMPE_BLOCK_KEYPAD:
624 case STMPE_BLOCK_GPIO:
630 static struct stmpe_variant_info stmpe1601 = {
633 .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
636 .regs = stmpe1601_regs,
637 .blocks = stmpe1601_blocks,
638 .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
639 .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
640 .enable = stmpe1601_enable,
641 .get_altfunc = stmpe1601_get_altfunc,
642 .enable_autosleep = stmpe1601_autosleep,
649 static const u8 stmpe24xx_regs[] = {
650 [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
651 [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
652 [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
653 [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
654 [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
655 [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
656 [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
657 [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
658 [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
659 [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
660 [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
661 [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
662 [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
663 [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
666 static struct stmpe_variant_block stmpe24xx_blocks[] = {
668 .cell = &stmpe_gpio_cell,
669 .irq = STMPE24XX_IRQ_GPIOC,
670 .block = STMPE_BLOCK_GPIO,
673 .cell = &stmpe_keypad_cell,
674 .irq = STMPE24XX_IRQ_KEYPAD,
675 .block = STMPE_BLOCK_KEYPAD,
679 static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
682 unsigned int mask = 0;
684 if (blocks & STMPE_BLOCK_GPIO)
685 mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
687 if (blocks & STMPE_BLOCK_KEYPAD)
688 mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
690 return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
694 static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
697 case STMPE_BLOCK_ROTATOR:
700 case STMPE_BLOCK_KEYPAD:
703 case STMPE_BLOCK_GPIO:
709 static struct stmpe_variant_info stmpe2401 = {
715 .regs = stmpe24xx_regs,
716 .blocks = stmpe24xx_blocks,
717 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
718 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
719 .enable = stmpe24xx_enable,
720 .get_altfunc = stmpe24xx_get_altfunc,
723 static struct stmpe_variant_info stmpe2403 = {
729 .regs = stmpe24xx_regs,
730 .blocks = stmpe24xx_blocks,
731 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
732 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
733 .enable = stmpe24xx_enable,
734 .get_altfunc = stmpe24xx_get_altfunc,
735 .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
738 static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
739 [STMPE610] = &stmpe610,
740 [STMPE801] = &stmpe801,
741 [STMPE811] = &stmpe811,
742 [STMPE1601] = &stmpe1601,
743 [STMPE2401] = &stmpe2401,
744 [STMPE2403] = &stmpe2403,
748 * These devices can be connected in a 'no-irq' configuration - the irq pin
749 * is not used and the device cannot interrupt the CPU. Here we only list
750 * devices which support this configuration - the driver will fail probing
751 * for any devices not listed here which are configured in this way.
753 static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
754 [STMPE801] = &stmpe801_noirq,
757 static irqreturn_t stmpe_irq(int irq, void *data)
759 struct stmpe *stmpe = data;
760 struct stmpe_variant_info *variant = stmpe->variant;
761 int num = DIV_ROUND_UP(variant->num_irqs, 8);
762 u8 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
767 if (variant->id_val == STMPE801_ID) {
768 int base = irq_create_mapping(stmpe->domain, 0);
770 handle_nested_irq(base);
774 ret = stmpe_block_read(stmpe, israddr, num, isr);
778 for (i = 0; i < num; i++) {
779 int bank = num - i - 1;
783 status &= stmpe->ier[bank];
789 int bit = __ffs(status);
790 int line = bank * 8 + bit;
791 int nestedirq = irq_create_mapping(stmpe->domain, line);
793 handle_nested_irq(nestedirq);
794 status &= ~(1 << bit);
797 stmpe_reg_write(stmpe, israddr + i, clear);
803 static void stmpe_irq_lock(struct irq_data *data)
805 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
807 mutex_lock(&stmpe->irq_lock);
810 static void stmpe_irq_sync_unlock(struct irq_data *data)
812 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
813 struct stmpe_variant_info *variant = stmpe->variant;
814 int num = DIV_ROUND_UP(variant->num_irqs, 8);
817 for (i = 0; i < num; i++) {
818 u8 new = stmpe->ier[i];
819 u8 old = stmpe->oldier[i];
824 stmpe->oldier[i] = new;
825 stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
828 mutex_unlock(&stmpe->irq_lock);
831 static void stmpe_irq_mask(struct irq_data *data)
833 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
834 int offset = data->hwirq;
835 int regoffset = offset / 8;
836 int mask = 1 << (offset % 8);
838 stmpe->ier[regoffset] &= ~mask;
841 static void stmpe_irq_unmask(struct irq_data *data)
843 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
844 int offset = data->hwirq;
845 int regoffset = offset / 8;
846 int mask = 1 << (offset % 8);
848 stmpe->ier[regoffset] |= mask;
851 static struct irq_chip stmpe_irq_chip = {
853 .irq_bus_lock = stmpe_irq_lock,
854 .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
855 .irq_mask = stmpe_irq_mask,
856 .irq_unmask = stmpe_irq_unmask,
859 static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
860 irq_hw_number_t hwirq)
862 struct stmpe *stmpe = d->host_data;
863 struct irq_chip *chip = NULL;
865 if (stmpe->variant->id_val != STMPE801_ID)
866 chip = &stmpe_irq_chip;
868 irq_set_chip_data(virq, stmpe);
869 irq_set_chip_and_handler(virq, chip, handle_edge_irq);
870 irq_set_nested_thread(virq, 1);
872 set_irq_flags(virq, IRQF_VALID);
874 irq_set_noprobe(virq);
880 static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
883 set_irq_flags(virq, 0);
885 irq_set_chip_and_handler(virq, NULL, NULL);
886 irq_set_chip_data(virq, NULL);
889 static struct irq_domain_ops stmpe_irq_ops = {
890 .map = stmpe_irq_map,
891 .unmap = stmpe_irq_unmap,
892 .xlate = irq_domain_xlate_twocell,
895 static int stmpe_irq_init(struct stmpe *stmpe, struct device_node *np)
898 int num_irqs = stmpe->variant->num_irqs;
901 base = stmpe->irq_base;
903 stmpe->domain = irq_domain_add_simple(np, num_irqs, base,
904 &stmpe_irq_ops, stmpe);
905 if (!stmpe->domain) {
906 dev_err(stmpe->dev, "Failed to create irqdomain\n");
913 static int stmpe_chip_init(struct stmpe *stmpe)
915 unsigned int irq_trigger = stmpe->pdata->irq_trigger;
916 int autosleep_timeout = stmpe->pdata->autosleep_timeout;
917 struct stmpe_variant_info *variant = stmpe->variant;
923 ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
924 ARRAY_SIZE(data), data);
928 id = (data[0] << 8) | data[1];
929 if ((id & variant->id_mask) != variant->id_val) {
930 dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
934 dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
936 /* Disable all modules -- subdrivers should enable what they need. */
937 ret = stmpe_disable(stmpe, ~0);
941 if (stmpe->irq >= 0) {
942 if (id == STMPE801_ID)
943 icr = STMPE801_REG_SYS_CTRL_INT_EN;
945 icr = STMPE_ICR_LSB_GIM;
947 /* STMPE801 doesn't support Edge interrupts */
948 if (id != STMPE801_ID) {
949 if (irq_trigger == IRQF_TRIGGER_FALLING ||
950 irq_trigger == IRQF_TRIGGER_RISING)
951 icr |= STMPE_ICR_LSB_EDGE;
954 if (irq_trigger == IRQF_TRIGGER_RISING ||
955 irq_trigger == IRQF_TRIGGER_HIGH) {
956 if (id == STMPE801_ID)
957 icr |= STMPE801_REG_SYS_CTRL_INT_HI;
959 icr |= STMPE_ICR_LSB_HIGH;
963 if (stmpe->pdata->autosleep) {
964 ret = stmpe_autosleep(stmpe, autosleep_timeout);
969 return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
972 static int stmpe_add_device(struct stmpe *stmpe, struct mfd_cell *cell)
974 return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
975 NULL, stmpe->irq_base, stmpe->domain);
978 static int stmpe_devices_init(struct stmpe *stmpe)
980 struct stmpe_variant_info *variant = stmpe->variant;
981 unsigned int platform_blocks = stmpe->pdata->blocks;
985 for (i = 0; i < variant->num_blocks; i++) {
986 struct stmpe_variant_block *block = &variant->blocks[i];
988 if (!(platform_blocks & block->block))
991 for (j = 0; j < block->cell->num_resources; j++) {
992 struct resource *res =
993 (struct resource *) &block->cell->resources[j];
995 /* Dynamically fill in a variant's IRQ. */
996 if (res->flags & IORESOURCE_IRQ)
997 res->start = res->end = block->irq + j;
1000 platform_blocks &= ~block->block;
1001 ret = stmpe_add_device(stmpe, block->cell);
1006 if (platform_blocks)
1007 dev_warn(stmpe->dev,
1008 "platform wants blocks (%#x) not present on variant",
1014 void stmpe_of_probe(struct stmpe_platform_data *pdata, struct device_node *np)
1016 struct device_node *child;
1019 pdata->irq_trigger = IRQF_TRIGGER_NONE;
1021 of_property_read_u32(np, "st,autosleep-timeout",
1022 &pdata->autosleep_timeout);
1024 pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
1026 for_each_child_of_node(np, child) {
1027 if (!strcmp(child->name, "stmpe_gpio")) {
1028 pdata->blocks |= STMPE_BLOCK_GPIO;
1029 } else if (!strcmp(child->name, "stmpe_keypad")) {
1030 pdata->blocks |= STMPE_BLOCK_KEYPAD;
1031 } else if (!strcmp(child->name, "stmpe_touchscreen")) {
1032 pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
1033 } else if (!strcmp(child->name, "stmpe_adc")) {
1034 pdata->blocks |= STMPE_BLOCK_ADC;
1035 } else if (!strcmp(child->name, "stmpe_pwm")) {
1036 pdata->blocks |= STMPE_BLOCK_PWM;
1037 } else if (!strcmp(child->name, "stmpe_rotator")) {
1038 pdata->blocks |= STMPE_BLOCK_ROTATOR;
1043 /* Called from client specific probe routines */
1044 int stmpe_probe(struct stmpe_client_info *ci, int partnum)
1046 struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
1047 struct device_node *np = ci->dev->of_node;
1048 struct stmpe *stmpe;
1055 pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
1059 stmpe_of_probe(pdata, np);
1062 stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
1066 mutex_init(&stmpe->irq_lock);
1067 mutex_init(&stmpe->lock);
1069 stmpe->dev = ci->dev;
1070 stmpe->client = ci->client;
1071 stmpe->pdata = pdata;
1072 stmpe->irq_base = pdata->irq_base;
1074 stmpe->partnum = partnum;
1075 stmpe->variant = stmpe_variant_info[partnum];
1076 stmpe->regs = stmpe->variant->regs;
1077 stmpe->num_gpios = stmpe->variant->num_gpios;
1078 dev_set_drvdata(stmpe->dev, stmpe);
1083 if (pdata->irq_over_gpio) {
1084 ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
1085 GPIOF_DIR_IN, "stmpe");
1087 dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
1092 stmpe->irq = gpio_to_irq(pdata->irq_gpio);
1094 stmpe->irq = ci->irq;
1097 if (stmpe->irq < 0) {
1098 /* use alternate variant info for no-irq mode, if supported */
1099 dev_info(stmpe->dev,
1100 "%s configured in no-irq mode by platform data\n",
1101 stmpe->variant->name);
1102 if (!stmpe_noirq_variant_info[stmpe->partnum]) {
1104 "%s does not support no-irq mode!\n",
1105 stmpe->variant->name);
1108 stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
1109 } else if (pdata->irq_trigger == IRQF_TRIGGER_NONE) {
1110 pdata->irq_trigger =
1111 irqd_get_trigger_type(irq_get_irq_data(stmpe->irq));
1114 ret = stmpe_chip_init(stmpe);
1118 if (stmpe->irq >= 0) {
1119 ret = stmpe_irq_init(stmpe, np);
1123 ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
1124 stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
1127 dev_err(stmpe->dev, "failed to request IRQ: %d\n",
1133 ret = stmpe_devices_init(stmpe);
1137 dev_err(stmpe->dev, "failed to add children\n");
1138 mfd_remove_devices(stmpe->dev);
1143 int stmpe_remove(struct stmpe *stmpe)
1145 mfd_remove_devices(stmpe->dev);
1151 static int stmpe_suspend(struct device *dev)
1153 struct stmpe *stmpe = dev_get_drvdata(dev);
1155 if (stmpe->irq >= 0 && device_may_wakeup(dev))
1156 enable_irq_wake(stmpe->irq);
1161 static int stmpe_resume(struct device *dev)
1163 struct stmpe *stmpe = dev_get_drvdata(dev);
1165 if (stmpe->irq >= 0 && device_may_wakeup(dev))
1166 disable_irq_wake(stmpe->irq);
1171 const struct dev_pm_ops stmpe_dev_pm_ops = {
1172 .suspend = stmpe_suspend,
1173 .resume = stmpe_resume,