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/gpio.h>
11 #include <linux/export.h>
12 #include <linux/kernel.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/irqdomain.h>
18 #include <linux/slab.h>
19 #include <linux/mfd/core.h>
22 static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
24 return stmpe->variant->enable(stmpe, blocks, true);
27 static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
29 return stmpe->variant->enable(stmpe, blocks, false);
32 static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
36 ret = stmpe->ci->read_byte(stmpe, reg);
38 dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
40 dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
45 static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
49 dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
51 ret = stmpe->ci->write_byte(stmpe, reg, val);
53 dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
58 static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
62 ret = __stmpe_reg_read(stmpe, reg);
69 return __stmpe_reg_write(stmpe, reg, ret);
72 static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
77 ret = stmpe->ci->read_block(stmpe, reg, length, values);
79 dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
81 dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
82 stmpe_dump_bytes("stmpe rd: ", values, length);
87 static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
92 dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
93 stmpe_dump_bytes("stmpe wr: ", values, length);
95 ret = stmpe->ci->write_block(stmpe, reg, length, values);
97 dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
103 * stmpe_enable - enable blocks on an STMPE device
104 * @stmpe: Device to work on
105 * @blocks: Mask of blocks (enum stmpe_block values) to enable
107 int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
111 mutex_lock(&stmpe->lock);
112 ret = __stmpe_enable(stmpe, blocks);
113 mutex_unlock(&stmpe->lock);
117 EXPORT_SYMBOL_GPL(stmpe_enable);
120 * stmpe_disable - disable blocks on an STMPE device
121 * @stmpe: Device to work on
122 * @blocks: Mask of blocks (enum stmpe_block values) to enable
124 int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
128 mutex_lock(&stmpe->lock);
129 ret = __stmpe_disable(stmpe, blocks);
130 mutex_unlock(&stmpe->lock);
134 EXPORT_SYMBOL_GPL(stmpe_disable);
137 * stmpe_reg_read() - read a single STMPE register
138 * @stmpe: Device to read from
139 * @reg: Register to read
141 int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
145 mutex_lock(&stmpe->lock);
146 ret = __stmpe_reg_read(stmpe, reg);
147 mutex_unlock(&stmpe->lock);
151 EXPORT_SYMBOL_GPL(stmpe_reg_read);
154 * stmpe_reg_write() - write a single STMPE register
155 * @stmpe: Device to write to
156 * @reg: Register to write
157 * @val: Value to write
159 int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
163 mutex_lock(&stmpe->lock);
164 ret = __stmpe_reg_write(stmpe, reg, val);
165 mutex_unlock(&stmpe->lock);
169 EXPORT_SYMBOL_GPL(stmpe_reg_write);
172 * stmpe_set_bits() - set the value of a bitfield in a STMPE register
173 * @stmpe: Device to write to
174 * @reg: Register to write
175 * @mask: Mask of bits to set
178 int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
182 mutex_lock(&stmpe->lock);
183 ret = __stmpe_set_bits(stmpe, reg, mask, val);
184 mutex_unlock(&stmpe->lock);
188 EXPORT_SYMBOL_GPL(stmpe_set_bits);
191 * stmpe_block_read() - read multiple STMPE registers
192 * @stmpe: Device to read from
193 * @reg: First register
194 * @length: Number of registers
195 * @values: Buffer to write to
197 int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
201 mutex_lock(&stmpe->lock);
202 ret = __stmpe_block_read(stmpe, reg, length, values);
203 mutex_unlock(&stmpe->lock);
207 EXPORT_SYMBOL_GPL(stmpe_block_read);
210 * stmpe_block_write() - write multiple STMPE registers
211 * @stmpe: Device to write to
212 * @reg: First register
213 * @length: Number of registers
214 * @values: Values to write
216 int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
221 mutex_lock(&stmpe->lock);
222 ret = __stmpe_block_write(stmpe, reg, length, values);
223 mutex_unlock(&stmpe->lock);
227 EXPORT_SYMBOL_GPL(stmpe_block_write);
230 * stmpe_set_altfunc()- set the alternate function for STMPE pins
231 * @stmpe: Device to configure
232 * @pins: Bitmask of pins to affect
233 * @block: block to enable alternate functions for
235 * @pins is assumed to have a bit set for each of the bits whose alternate
236 * function is to be changed, numbered according to the GPIOXY numbers.
238 * If the GPIO module is not enabled, this function automatically enables it in
239 * order to perform the change.
241 int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
243 struct stmpe_variant_info *variant = stmpe->variant;
244 u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
245 int af_bits = variant->af_bits;
246 int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
247 int mask = (1 << af_bits) - 1;
249 int af, afperreg, ret;
251 if (!variant->get_altfunc)
254 afperreg = 8 / af_bits;
255 mutex_lock(&stmpe->lock);
257 ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
261 ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
265 af = variant->get_altfunc(stmpe, block);
268 int pin = __ffs(pins);
269 int regoffset = numregs - (pin / afperreg) - 1;
270 int pos = (pin % afperreg) * (8 / afperreg);
272 regs[regoffset] &= ~(mask << pos);
273 regs[regoffset] |= af << pos;
278 ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
281 mutex_unlock(&stmpe->lock);
284 EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
287 * GPIO (all variants)
290 static struct resource stmpe_gpio_resources[] = {
291 /* Start and end filled dynamically */
293 .flags = IORESOURCE_IRQ,
297 static struct mfd_cell stmpe_gpio_cell = {
298 .name = "stmpe-gpio",
299 .resources = stmpe_gpio_resources,
300 .num_resources = ARRAY_SIZE(stmpe_gpio_resources),
303 static struct mfd_cell stmpe_gpio_cell_noirq = {
304 .name = "stmpe-gpio",
305 /* gpio cell resources consist of an irq only so no resources here */
309 * Keypad (1601, 2401, 2403)
312 static struct resource stmpe_keypad_resources[] = {
315 .flags = IORESOURCE_IRQ,
318 .name = "KEYPAD_OVER",
319 .flags = IORESOURCE_IRQ,
323 static struct mfd_cell stmpe_keypad_cell = {
324 .name = "stmpe-keypad",
325 .resources = stmpe_keypad_resources,
326 .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
332 static const u8 stmpe801_regs[] = {
333 [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
334 [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
335 [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
336 [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
337 [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
338 [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
339 [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
340 [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
344 static struct stmpe_variant_block stmpe801_blocks[] = {
346 .cell = &stmpe_gpio_cell,
348 .block = STMPE_BLOCK_GPIO,
352 static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
354 .cell = &stmpe_gpio_cell_noirq,
355 .block = STMPE_BLOCK_GPIO,
359 static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
362 if (blocks & STMPE_BLOCK_GPIO)
368 static struct stmpe_variant_info stmpe801 = {
370 .id_val = STMPE801_ID,
373 .regs = stmpe801_regs,
374 .blocks = stmpe801_blocks,
375 .num_blocks = ARRAY_SIZE(stmpe801_blocks),
376 .num_irqs = STMPE801_NR_INTERNAL_IRQS,
377 .enable = stmpe801_enable,
380 static struct stmpe_variant_info stmpe801_noirq = {
382 .id_val = STMPE801_ID,
385 .regs = stmpe801_regs,
386 .blocks = stmpe801_blocks_noirq,
387 .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
388 .enable = stmpe801_enable,
392 * Touchscreen (STMPE811 or STMPE610)
395 static struct resource stmpe_ts_resources[] = {
398 .flags = IORESOURCE_IRQ,
402 .flags = IORESOURCE_IRQ,
406 static struct mfd_cell stmpe_ts_cell = {
408 .resources = stmpe_ts_resources,
409 .num_resources = ARRAY_SIZE(stmpe_ts_resources),
413 * STMPE811 or STMPE610
416 static const u8 stmpe811_regs[] = {
417 [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
418 [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
419 [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
420 [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
421 [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
422 [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
423 [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
424 [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
425 [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
426 [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
427 [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
428 [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
429 [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
430 [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
433 static struct stmpe_variant_block stmpe811_blocks[] = {
435 .cell = &stmpe_gpio_cell,
436 .irq = STMPE811_IRQ_GPIOC,
437 .block = STMPE_BLOCK_GPIO,
440 .cell = &stmpe_ts_cell,
441 .irq = STMPE811_IRQ_TOUCH_DET,
442 .block = STMPE_BLOCK_TOUCHSCREEN,
446 static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
449 unsigned int mask = 0;
451 if (blocks & STMPE_BLOCK_GPIO)
452 mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
454 if (blocks & STMPE_BLOCK_ADC)
455 mask |= STMPE811_SYS_CTRL2_ADC_OFF;
457 if (blocks & STMPE_BLOCK_TOUCHSCREEN)
458 mask |= STMPE811_SYS_CTRL2_TSC_OFF;
460 return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
464 static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
466 /* 0 for touchscreen, 1 for GPIO */
467 return block != STMPE_BLOCK_TOUCHSCREEN;
470 static struct stmpe_variant_info stmpe811 = {
476 .regs = stmpe811_regs,
477 .blocks = stmpe811_blocks,
478 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
479 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
480 .enable = stmpe811_enable,
481 .get_altfunc = stmpe811_get_altfunc,
484 /* Similar to 811, except number of gpios */
485 static struct stmpe_variant_info stmpe610 = {
491 .regs = stmpe811_regs,
492 .blocks = stmpe811_blocks,
493 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
494 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
495 .enable = stmpe811_enable,
496 .get_altfunc = stmpe811_get_altfunc,
503 static const u8 stmpe1601_regs[] = {
504 [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
505 [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
506 [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
507 [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
508 [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
509 [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
510 [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
511 [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
512 [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
513 [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
514 [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
515 [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
516 [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
517 [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
520 static struct stmpe_variant_block stmpe1601_blocks[] = {
522 .cell = &stmpe_gpio_cell,
523 .irq = STMPE1601_IRQ_GPIOC,
524 .block = STMPE_BLOCK_GPIO,
527 .cell = &stmpe_keypad_cell,
528 .irq = STMPE1601_IRQ_KEYPAD,
529 .block = STMPE_BLOCK_KEYPAD,
533 /* supported autosleep timeout delay (in msecs) */
534 static const int stmpe_autosleep_delay[] = {
535 4, 16, 32, 64, 128, 256, 512, 1024,
538 static int stmpe_round_timeout(int timeout)
542 for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
543 if (stmpe_autosleep_delay[i] >= timeout)
548 * requests for delays longer than supported should not return the
549 * longest supported delay
554 static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
558 if (!stmpe->variant->enable_autosleep)
561 mutex_lock(&stmpe->lock);
562 ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
563 mutex_unlock(&stmpe->lock);
569 * Both stmpe 1601/2403 support same layout for autosleep
571 static int stmpe1601_autosleep(struct stmpe *stmpe,
572 int autosleep_timeout)
576 /* choose the best available timeout */
577 timeout = stmpe_round_timeout(autosleep_timeout);
579 dev_err(stmpe->dev, "invalid timeout\n");
583 ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
584 STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
589 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
590 STPME1601_AUTOSLEEP_ENABLE,
591 STPME1601_AUTOSLEEP_ENABLE);
594 static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
597 unsigned int mask = 0;
599 if (blocks & STMPE_BLOCK_GPIO)
600 mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
602 if (blocks & STMPE_BLOCK_KEYPAD)
603 mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
605 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
609 static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
612 case STMPE_BLOCK_PWM:
615 case STMPE_BLOCK_KEYPAD:
618 case STMPE_BLOCK_GPIO:
624 static struct stmpe_variant_info stmpe1601 = {
627 .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
630 .regs = stmpe1601_regs,
631 .blocks = stmpe1601_blocks,
632 .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
633 .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
634 .enable = stmpe1601_enable,
635 .get_altfunc = stmpe1601_get_altfunc,
636 .enable_autosleep = stmpe1601_autosleep,
643 static const u8 stmpe24xx_regs[] = {
644 [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
645 [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
646 [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
647 [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
648 [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
649 [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
650 [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
651 [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
652 [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
653 [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
654 [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
655 [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
656 [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
657 [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
660 static struct stmpe_variant_block stmpe24xx_blocks[] = {
662 .cell = &stmpe_gpio_cell,
663 .irq = STMPE24XX_IRQ_GPIOC,
664 .block = STMPE_BLOCK_GPIO,
667 .cell = &stmpe_keypad_cell,
668 .irq = STMPE24XX_IRQ_KEYPAD,
669 .block = STMPE_BLOCK_KEYPAD,
673 static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
676 unsigned int mask = 0;
678 if (blocks & STMPE_BLOCK_GPIO)
679 mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
681 if (blocks & STMPE_BLOCK_KEYPAD)
682 mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
684 return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
688 static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
691 case STMPE_BLOCK_ROTATOR:
694 case STMPE_BLOCK_KEYPAD:
697 case STMPE_BLOCK_GPIO:
703 static struct stmpe_variant_info stmpe2401 = {
709 .regs = stmpe24xx_regs,
710 .blocks = stmpe24xx_blocks,
711 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
712 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
713 .enable = stmpe24xx_enable,
714 .get_altfunc = stmpe24xx_get_altfunc,
717 static struct stmpe_variant_info stmpe2403 = {
723 .regs = stmpe24xx_regs,
724 .blocks = stmpe24xx_blocks,
725 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
726 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
727 .enable = stmpe24xx_enable,
728 .get_altfunc = stmpe24xx_get_altfunc,
729 .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
732 static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
733 [STMPE610] = &stmpe610,
734 [STMPE801] = &stmpe801,
735 [STMPE811] = &stmpe811,
736 [STMPE1601] = &stmpe1601,
737 [STMPE2401] = &stmpe2401,
738 [STMPE2403] = &stmpe2403,
742 * These devices can be connected in a 'no-irq' configuration - the irq pin
743 * is not used and the device cannot interrupt the CPU. Here we only list
744 * devices which support this configuration - the driver will fail probing
745 * for any devices not listed here which are configured in this way.
747 static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
748 [STMPE801] = &stmpe801_noirq,
751 static irqreturn_t stmpe_irq(int irq, void *data)
753 struct stmpe *stmpe = data;
754 struct stmpe_variant_info *variant = stmpe->variant;
755 int num = DIV_ROUND_UP(variant->num_irqs, 8);
756 u8 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
761 if (variant->id_val == STMPE801_ID) {
762 int base = irq_create_mapping(stmpe->domain, 0);
764 handle_nested_irq(base);
768 ret = stmpe_block_read(stmpe, israddr, num, isr);
772 for (i = 0; i < num; i++) {
773 int bank = num - i - 1;
777 status &= stmpe->ier[bank];
783 int bit = __ffs(status);
784 int line = bank * 8 + bit;
785 int nestedirq = irq_create_mapping(stmpe->domain, line);
787 handle_nested_irq(nestedirq);
788 status &= ~(1 << bit);
791 stmpe_reg_write(stmpe, israddr + i, clear);
797 static void stmpe_irq_lock(struct irq_data *data)
799 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
801 mutex_lock(&stmpe->irq_lock);
804 static void stmpe_irq_sync_unlock(struct irq_data *data)
806 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
807 struct stmpe_variant_info *variant = stmpe->variant;
808 int num = DIV_ROUND_UP(variant->num_irqs, 8);
811 for (i = 0; i < num; i++) {
812 u8 new = stmpe->ier[i];
813 u8 old = stmpe->oldier[i];
818 stmpe->oldier[i] = new;
819 stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
822 mutex_unlock(&stmpe->irq_lock);
825 static void stmpe_irq_mask(struct irq_data *data)
827 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
828 int offset = data->hwirq;
829 int regoffset = offset / 8;
830 int mask = 1 << (offset % 8);
832 stmpe->ier[regoffset] &= ~mask;
835 static void stmpe_irq_unmask(struct irq_data *data)
837 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
838 int offset = data->hwirq;
839 int regoffset = offset / 8;
840 int mask = 1 << (offset % 8);
842 stmpe->ier[regoffset] |= mask;
845 static struct irq_chip stmpe_irq_chip = {
847 .irq_bus_lock = stmpe_irq_lock,
848 .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
849 .irq_mask = stmpe_irq_mask,
850 .irq_unmask = stmpe_irq_unmask,
853 static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
854 irq_hw_number_t hwirq)
856 struct stmpe *stmpe = d->host_data;
857 struct irq_chip *chip = NULL;
859 if (stmpe->variant->id_val != STMPE801_ID)
860 chip = &stmpe_irq_chip;
862 irq_set_chip_data(virq, stmpe);
863 irq_set_chip_and_handler(virq, chip, handle_edge_irq);
864 irq_set_nested_thread(virq, 1);
866 set_irq_flags(virq, IRQF_VALID);
868 irq_set_noprobe(virq);
874 static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
877 set_irq_flags(virq, 0);
879 irq_set_chip_and_handler(virq, NULL, NULL);
880 irq_set_chip_data(virq, NULL);
883 static struct irq_domain_ops stmpe_irq_ops = {
884 .map = stmpe_irq_map,
885 .unmap = stmpe_irq_unmap,
886 .xlate = irq_domain_xlate_twocell,
889 static int __devinit stmpe_irq_init(struct stmpe *stmpe,
890 struct device_node *np)
893 int num_irqs = stmpe->variant->num_irqs;
896 base = stmpe->irq_base;
898 stmpe->domain = irq_domain_add_simple(np, num_irqs, base,
899 &stmpe_irq_ops, stmpe);
900 if (!stmpe->domain) {
901 dev_err(stmpe->dev, "Failed to create irqdomain\n");
908 static int __devinit stmpe_chip_init(struct stmpe *stmpe)
910 unsigned int irq_trigger = stmpe->pdata->irq_trigger;
911 int autosleep_timeout = stmpe->pdata->autosleep_timeout;
912 struct stmpe_variant_info *variant = stmpe->variant;
918 ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
919 ARRAY_SIZE(data), data);
923 id = (data[0] << 8) | data[1];
924 if ((id & variant->id_mask) != variant->id_val) {
925 dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
929 dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
931 /* Disable all modules -- subdrivers should enable what they need. */
932 ret = stmpe_disable(stmpe, ~0);
936 if (stmpe->irq >= 0) {
937 if (id == STMPE801_ID)
938 icr = STMPE801_REG_SYS_CTRL_INT_EN;
940 icr = STMPE_ICR_LSB_GIM;
942 /* STMPE801 doesn't support Edge interrupts */
943 if (id != STMPE801_ID) {
944 if (irq_trigger == IRQF_TRIGGER_FALLING ||
945 irq_trigger == IRQF_TRIGGER_RISING)
946 icr |= STMPE_ICR_LSB_EDGE;
949 if (irq_trigger == IRQF_TRIGGER_RISING ||
950 irq_trigger == IRQF_TRIGGER_HIGH) {
951 if (id == STMPE801_ID)
952 icr |= STMPE801_REG_SYS_CTRL_INT_HI;
954 icr |= STMPE_ICR_LSB_HIGH;
957 if (stmpe->pdata->irq_invert_polarity) {
958 if (id == STMPE801_ID)
959 icr ^= STMPE801_REG_SYS_CTRL_INT_HI;
961 icr ^= STMPE_ICR_LSB_HIGH;
965 if (stmpe->pdata->autosleep) {
966 ret = stmpe_autosleep(stmpe, autosleep_timeout);
971 return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
974 static int __devinit stmpe_add_device(struct stmpe *stmpe,
975 struct mfd_cell *cell)
977 return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
978 NULL, stmpe->irq_base, stmpe->domain);
981 static int __devinit stmpe_devices_init(struct stmpe *stmpe)
983 struct stmpe_variant_info *variant = stmpe->variant;
984 unsigned int platform_blocks = stmpe->pdata->blocks;
988 for (i = 0; i < variant->num_blocks; i++) {
989 struct stmpe_variant_block *block = &variant->blocks[i];
991 if (!(platform_blocks & block->block))
994 for (j = 0; j < block->cell->num_resources; j++) {
995 struct resource *res =
996 (struct resource *) &block->cell->resources[j];
998 /* Dynamically fill in a variant's IRQ. */
999 if (res->flags & IORESOURCE_IRQ)
1000 res->start = res->end = block->irq + j;
1003 platform_blocks &= ~block->block;
1004 ret = stmpe_add_device(stmpe, block->cell);
1009 if (platform_blocks)
1010 dev_warn(stmpe->dev,
1011 "platform wants blocks (%#x) not present on variant",
1017 void __devinit stmpe_of_probe(struct stmpe_platform_data *pdata,
1018 struct device_node *np)
1020 struct device_node *child;
1022 of_property_read_u32(np, "st,autosleep-timeout",
1023 &pdata->autosleep_timeout);
1025 pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
1027 for_each_child_of_node(np, child) {
1028 if (!strcmp(child->name, "stmpe_gpio")) {
1029 pdata->blocks |= STMPE_BLOCK_GPIO;
1031 if (!strcmp(child->name, "stmpe_keypad")) {
1032 pdata->blocks |= STMPE_BLOCK_KEYPAD;
1034 if (!strcmp(child->name, "stmpe_touchscreen")) {
1035 pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
1037 if (!strcmp(child->name, "stmpe_adc")) {
1038 pdata->blocks |= STMPE_BLOCK_ADC;
1043 /* Called from client specific probe routines */
1044 int __devinit 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];
1111 ret = stmpe_chip_init(stmpe);
1115 if (stmpe->irq >= 0) {
1116 ret = stmpe_irq_init(stmpe, np);
1120 ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
1121 stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
1124 dev_err(stmpe->dev, "failed to request IRQ: %d\n",
1130 ret = stmpe_devices_init(stmpe);
1134 dev_err(stmpe->dev, "failed to add children\n");
1135 mfd_remove_devices(stmpe->dev);
1140 int stmpe_remove(struct stmpe *stmpe)
1142 mfd_remove_devices(stmpe->dev);
1148 static int stmpe_suspend(struct device *dev)
1150 struct stmpe *stmpe = dev_get_drvdata(dev);
1152 if (stmpe->irq >= 0 && device_may_wakeup(dev))
1153 enable_irq_wake(stmpe->irq);
1158 static int stmpe_resume(struct device *dev)
1160 struct stmpe *stmpe = dev_get_drvdata(dev);
1162 if (stmpe->irq >= 0 && device_may_wakeup(dev))
1163 disable_irq_wake(stmpe->irq);
1168 const struct dev_pm_ops stmpe_dev_pm_ops = {
1169 .suspend = stmpe_suspend,
1170 .resume = stmpe_resume,