x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / mfd / stmpe.c
blobfff63a41862cf6bbb4acfac38496447a4137ad0a
1 /*
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
8 */
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>
17 #include <linux/of.h>
18 #include <linux/of_gpio.h>
19 #include <linux/pm.h>
20 #include <linux/slab.h>
21 #include <linux/mfd/core.h>
22 #include <linux/delay.h>
23 #include "stmpe.h"
25 static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
27 return stmpe->variant->enable(stmpe, blocks, true);
30 static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
32 return stmpe->variant->enable(stmpe, blocks, false);
35 static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
37 int ret;
39 ret = stmpe->ci->read_byte(stmpe, reg);
40 if (ret < 0)
41 dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
43 dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
45 return ret;
48 static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
50 int ret;
52 dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
54 ret = stmpe->ci->write_byte(stmpe, reg, val);
55 if (ret < 0)
56 dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
58 return ret;
61 static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
63 int ret;
65 ret = __stmpe_reg_read(stmpe, reg);
66 if (ret < 0)
67 return ret;
69 ret &= ~mask;
70 ret |= val;
72 return __stmpe_reg_write(stmpe, reg, ret);
75 static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
76 u8 *values)
78 int ret;
80 ret = stmpe->ci->read_block(stmpe, reg, length, values);
81 if (ret < 0)
82 dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
84 dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
85 stmpe_dump_bytes("stmpe rd: ", values, length);
87 return ret;
90 static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
91 const u8 *values)
93 int ret;
95 dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
96 stmpe_dump_bytes("stmpe wr: ", values, length);
98 ret = stmpe->ci->write_block(stmpe, reg, length, values);
99 if (ret < 0)
100 dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
102 return ret;
106 * stmpe_enable - enable blocks on an STMPE device
107 * @stmpe: Device to work on
108 * @blocks: Mask of blocks (enum stmpe_block values) to enable
110 int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
112 int ret;
114 mutex_lock(&stmpe->lock);
115 ret = __stmpe_enable(stmpe, blocks);
116 mutex_unlock(&stmpe->lock);
118 return ret;
120 EXPORT_SYMBOL_GPL(stmpe_enable);
123 * stmpe_disable - disable blocks on an STMPE device
124 * @stmpe: Device to work on
125 * @blocks: Mask of blocks (enum stmpe_block values) to enable
127 int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
129 int ret;
131 mutex_lock(&stmpe->lock);
132 ret = __stmpe_disable(stmpe, blocks);
133 mutex_unlock(&stmpe->lock);
135 return ret;
137 EXPORT_SYMBOL_GPL(stmpe_disable);
140 * stmpe_reg_read() - read a single STMPE register
141 * @stmpe: Device to read from
142 * @reg: Register to read
144 int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
146 int ret;
148 mutex_lock(&stmpe->lock);
149 ret = __stmpe_reg_read(stmpe, reg);
150 mutex_unlock(&stmpe->lock);
152 return ret;
154 EXPORT_SYMBOL_GPL(stmpe_reg_read);
157 * stmpe_reg_write() - write a single STMPE register
158 * @stmpe: Device to write to
159 * @reg: Register to write
160 * @val: Value to write
162 int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
164 int ret;
166 mutex_lock(&stmpe->lock);
167 ret = __stmpe_reg_write(stmpe, reg, val);
168 mutex_unlock(&stmpe->lock);
170 return ret;
172 EXPORT_SYMBOL_GPL(stmpe_reg_write);
175 * stmpe_set_bits() - set the value of a bitfield in a STMPE register
176 * @stmpe: Device to write to
177 * @reg: Register to write
178 * @mask: Mask of bits to set
179 * @val: Value to set
181 int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
183 int ret;
185 mutex_lock(&stmpe->lock);
186 ret = __stmpe_set_bits(stmpe, reg, mask, val);
187 mutex_unlock(&stmpe->lock);
189 return ret;
191 EXPORT_SYMBOL_GPL(stmpe_set_bits);
194 * stmpe_block_read() - read multiple STMPE registers
195 * @stmpe: Device to read from
196 * @reg: First register
197 * @length: Number of registers
198 * @values: Buffer to write to
200 int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
202 int ret;
204 mutex_lock(&stmpe->lock);
205 ret = __stmpe_block_read(stmpe, reg, length, values);
206 mutex_unlock(&stmpe->lock);
208 return ret;
210 EXPORT_SYMBOL_GPL(stmpe_block_read);
213 * stmpe_block_write() - write multiple STMPE registers
214 * @stmpe: Device to write to
215 * @reg: First register
216 * @length: Number of registers
217 * @values: Values to write
219 int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
220 const u8 *values)
222 int ret;
224 mutex_lock(&stmpe->lock);
225 ret = __stmpe_block_write(stmpe, reg, length, values);
226 mutex_unlock(&stmpe->lock);
228 return ret;
230 EXPORT_SYMBOL_GPL(stmpe_block_write);
233 * stmpe_set_altfunc()- set the alternate function for STMPE pins
234 * @stmpe: Device to configure
235 * @pins: Bitmask of pins to affect
236 * @block: block to enable alternate functions for
238 * @pins is assumed to have a bit set for each of the bits whose alternate
239 * function is to be changed, numbered according to the GPIOXY numbers.
241 * If the GPIO module is not enabled, this function automatically enables it in
242 * order to perform the change.
244 int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
246 struct stmpe_variant_info *variant = stmpe->variant;
247 u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
248 int af_bits = variant->af_bits;
249 int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
250 int mask = (1 << af_bits) - 1;
251 u8 regs[numregs];
252 int af, afperreg, ret;
254 if (!variant->get_altfunc)
255 return 0;
257 afperreg = 8 / af_bits;
258 mutex_lock(&stmpe->lock);
260 ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
261 if (ret < 0)
262 goto out;
264 ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
265 if (ret < 0)
266 goto out;
268 af = variant->get_altfunc(stmpe, block);
270 while (pins) {
271 int pin = __ffs(pins);
272 int regoffset = numregs - (pin / afperreg) - 1;
273 int pos = (pin % afperreg) * (8 / afperreg);
275 regs[regoffset] &= ~(mask << pos);
276 regs[regoffset] |= af << pos;
278 pins &= ~(1 << pin);
281 ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
283 out:
284 mutex_unlock(&stmpe->lock);
285 return ret;
287 EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
290 * GPIO (all variants)
293 static struct resource stmpe_gpio_resources[] = {
294 /* Start and end filled dynamically */
296 .flags = IORESOURCE_IRQ,
300 static struct mfd_cell stmpe_gpio_cell = {
301 .name = "stmpe-gpio",
302 .of_compatible = "st,stmpe-gpio",
303 .resources = stmpe_gpio_resources,
304 .num_resources = ARRAY_SIZE(stmpe_gpio_resources),
307 static struct mfd_cell stmpe_gpio_cell_noirq = {
308 .name = "stmpe-gpio",
309 .of_compatible = "st,stmpe-gpio",
310 /* gpio cell resources consist of an irq only so no resources here */
314 * Keypad (1601, 2401, 2403)
317 static struct resource stmpe_keypad_resources[] = {
319 .name = "KEYPAD",
320 .flags = IORESOURCE_IRQ,
323 .name = "KEYPAD_OVER",
324 .flags = IORESOURCE_IRQ,
328 static struct mfd_cell stmpe_keypad_cell = {
329 .name = "stmpe-keypad",
330 .of_compatible = "st,stmpe-keypad",
331 .resources = stmpe_keypad_resources,
332 .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
336 * STMPE801
338 static const u8 stmpe801_regs[] = {
339 [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
340 [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
341 [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
342 [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
343 [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
344 [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
345 [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
346 [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
350 static struct stmpe_variant_block stmpe801_blocks[] = {
352 .cell = &stmpe_gpio_cell,
353 .irq = 0,
354 .block = STMPE_BLOCK_GPIO,
358 static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
360 .cell = &stmpe_gpio_cell_noirq,
361 .block = STMPE_BLOCK_GPIO,
365 static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
366 bool enable)
368 if (blocks & STMPE_BLOCK_GPIO)
369 return 0;
370 else
371 return -EINVAL;
374 static struct stmpe_variant_info stmpe801 = {
375 .name = "stmpe801",
376 .id_val = STMPE801_ID,
377 .id_mask = 0xffff,
378 .num_gpios = 8,
379 .regs = stmpe801_regs,
380 .blocks = stmpe801_blocks,
381 .num_blocks = ARRAY_SIZE(stmpe801_blocks),
382 .num_irqs = STMPE801_NR_INTERNAL_IRQS,
383 .enable = stmpe801_enable,
386 static struct stmpe_variant_info stmpe801_noirq = {
387 .name = "stmpe801",
388 .id_val = STMPE801_ID,
389 .id_mask = 0xffff,
390 .num_gpios = 8,
391 .regs = stmpe801_regs,
392 .blocks = stmpe801_blocks_noirq,
393 .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
394 .enable = stmpe801_enable,
398 * Touchscreen (STMPE811 or STMPE610)
401 static struct resource stmpe_ts_resources[] = {
403 .name = "TOUCH_DET",
404 .flags = IORESOURCE_IRQ,
407 .name = "FIFO_TH",
408 .flags = IORESOURCE_IRQ,
412 static struct mfd_cell stmpe_ts_cell = {
413 .name = "stmpe-ts",
414 .of_compatible = "st,stmpe-ts",
415 .resources = stmpe_ts_resources,
416 .num_resources = ARRAY_SIZE(stmpe_ts_resources),
420 * STMPE811 or STMPE610
423 static const u8 stmpe811_regs[] = {
424 [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
425 [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
426 [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
427 [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
428 [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
429 [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
430 [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
431 [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
432 [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
433 [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
434 [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
435 [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
436 [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
437 [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
440 static struct stmpe_variant_block stmpe811_blocks[] = {
442 .cell = &stmpe_gpio_cell,
443 .irq = STMPE811_IRQ_GPIOC,
444 .block = STMPE_BLOCK_GPIO,
447 .cell = &stmpe_ts_cell,
448 .irq = STMPE811_IRQ_TOUCH_DET,
449 .block = STMPE_BLOCK_TOUCHSCREEN,
453 static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
454 bool enable)
456 unsigned int mask = 0;
458 if (blocks & STMPE_BLOCK_GPIO)
459 mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
461 if (blocks & STMPE_BLOCK_ADC)
462 mask |= STMPE811_SYS_CTRL2_ADC_OFF;
464 if (blocks & STMPE_BLOCK_TOUCHSCREEN)
465 mask |= STMPE811_SYS_CTRL2_TSC_OFF;
467 return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
468 enable ? 0 : mask);
471 static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
473 /* 0 for touchscreen, 1 for GPIO */
474 return block != STMPE_BLOCK_TOUCHSCREEN;
477 static struct stmpe_variant_info stmpe811 = {
478 .name = "stmpe811",
479 .id_val = 0x0811,
480 .id_mask = 0xffff,
481 .num_gpios = 8,
482 .af_bits = 1,
483 .regs = stmpe811_regs,
484 .blocks = stmpe811_blocks,
485 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
486 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
487 .enable = stmpe811_enable,
488 .get_altfunc = stmpe811_get_altfunc,
491 /* Similar to 811, except number of gpios */
492 static struct stmpe_variant_info stmpe610 = {
493 .name = "stmpe610",
494 .id_val = 0x0811,
495 .id_mask = 0xffff,
496 .num_gpios = 6,
497 .af_bits = 1,
498 .regs = stmpe811_regs,
499 .blocks = stmpe811_blocks,
500 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
501 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
502 .enable = stmpe811_enable,
503 .get_altfunc = stmpe811_get_altfunc,
507 * STMPE1601
510 static const u8 stmpe1601_regs[] = {
511 [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
512 [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
513 [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
514 [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
515 [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
516 [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
517 [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
518 [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
519 [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
520 [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
521 [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
522 [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
523 [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
524 [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
527 static struct stmpe_variant_block stmpe1601_blocks[] = {
529 .cell = &stmpe_gpio_cell,
530 .irq = STMPE1601_IRQ_GPIOC,
531 .block = STMPE_BLOCK_GPIO,
534 .cell = &stmpe_keypad_cell,
535 .irq = STMPE1601_IRQ_KEYPAD,
536 .block = STMPE_BLOCK_KEYPAD,
540 /* supported autosleep timeout delay (in msecs) */
541 static const int stmpe_autosleep_delay[] = {
542 4, 16, 32, 64, 128, 256, 512, 1024,
545 static int stmpe_round_timeout(int timeout)
547 int i;
549 for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
550 if (stmpe_autosleep_delay[i] >= timeout)
551 return i;
555 * requests for delays longer than supported should not return the
556 * longest supported delay
558 return -EINVAL;
561 static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
563 int ret;
565 if (!stmpe->variant->enable_autosleep)
566 return -ENOSYS;
568 mutex_lock(&stmpe->lock);
569 ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
570 mutex_unlock(&stmpe->lock);
572 return ret;
576 * Both stmpe 1601/2403 support same layout for autosleep
578 static int stmpe1601_autosleep(struct stmpe *stmpe,
579 int autosleep_timeout)
581 int ret, timeout;
583 /* choose the best available timeout */
584 timeout = stmpe_round_timeout(autosleep_timeout);
585 if (timeout < 0) {
586 dev_err(stmpe->dev, "invalid timeout\n");
587 return timeout;
590 ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
591 STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
592 timeout);
593 if (ret < 0)
594 return ret;
596 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
597 STPME1601_AUTOSLEEP_ENABLE,
598 STPME1601_AUTOSLEEP_ENABLE);
601 static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
602 bool enable)
604 unsigned int mask = 0;
606 if (blocks & STMPE_BLOCK_GPIO)
607 mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
609 if (blocks & STMPE_BLOCK_KEYPAD)
610 mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
612 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
613 enable ? mask : 0);
616 static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
618 switch (block) {
619 case STMPE_BLOCK_PWM:
620 return 2;
622 case STMPE_BLOCK_KEYPAD:
623 return 1;
625 case STMPE_BLOCK_GPIO:
626 default:
627 return 0;
631 static struct stmpe_variant_info stmpe1601 = {
632 .name = "stmpe1601",
633 .id_val = 0x0210,
634 .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
635 .num_gpios = 16,
636 .af_bits = 2,
637 .regs = stmpe1601_regs,
638 .blocks = stmpe1601_blocks,
639 .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
640 .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
641 .enable = stmpe1601_enable,
642 .get_altfunc = stmpe1601_get_altfunc,
643 .enable_autosleep = stmpe1601_autosleep,
647 * STMPE1801
649 static const u8 stmpe1801_regs[] = {
650 [STMPE_IDX_CHIP_ID] = STMPE1801_REG_CHIP_ID,
651 [STMPE_IDX_ICR_LSB] = STMPE1801_REG_INT_CTRL_LOW,
652 [STMPE_IDX_IER_LSB] = STMPE1801_REG_INT_EN_MASK_LOW,
653 [STMPE_IDX_ISR_LSB] = STMPE1801_REG_INT_STA_LOW,
654 [STMPE_IDX_GPMR_LSB] = STMPE1801_REG_GPIO_MP_LOW,
655 [STMPE_IDX_GPSR_LSB] = STMPE1801_REG_GPIO_SET_LOW,
656 [STMPE_IDX_GPCR_LSB] = STMPE1801_REG_GPIO_CLR_LOW,
657 [STMPE_IDX_GPDR_LSB] = STMPE1801_REG_GPIO_SET_DIR_LOW,
658 [STMPE_IDX_GPRER_LSB] = STMPE1801_REG_GPIO_RE_LOW,
659 [STMPE_IDX_GPFER_LSB] = STMPE1801_REG_GPIO_FE_LOW,
660 [STMPE_IDX_IEGPIOR_LSB] = STMPE1801_REG_INT_EN_GPIO_MASK_LOW,
661 [STMPE_IDX_ISGPIOR_LSB] = STMPE1801_REG_INT_STA_GPIO_LOW,
664 static struct stmpe_variant_block stmpe1801_blocks[] = {
666 .cell = &stmpe_gpio_cell,
667 .irq = STMPE1801_IRQ_GPIOC,
668 .block = STMPE_BLOCK_GPIO,
671 .cell = &stmpe_keypad_cell,
672 .irq = STMPE1801_IRQ_KEYPAD,
673 .block = STMPE_BLOCK_KEYPAD,
677 static int stmpe1801_enable(struct stmpe *stmpe, unsigned int blocks,
678 bool enable)
680 unsigned int mask = 0;
681 if (blocks & STMPE_BLOCK_GPIO)
682 mask |= STMPE1801_MSK_INT_EN_GPIO;
684 if (blocks & STMPE_BLOCK_KEYPAD)
685 mask |= STMPE1801_MSK_INT_EN_KPC;
687 return __stmpe_set_bits(stmpe, STMPE1801_REG_INT_EN_MASK_LOW, mask,
688 enable ? mask : 0);
691 static int stmpe1801_reset(struct stmpe *stmpe)
693 unsigned long timeout;
694 int ret = 0;
696 ret = __stmpe_set_bits(stmpe, STMPE1801_REG_SYS_CTRL,
697 STMPE1801_MSK_SYS_CTRL_RESET, STMPE1801_MSK_SYS_CTRL_RESET);
698 if (ret < 0)
699 return ret;
701 timeout = jiffies + msecs_to_jiffies(100);
702 while (time_before(jiffies, timeout)) {
703 ret = __stmpe_reg_read(stmpe, STMPE1801_REG_SYS_CTRL);
704 if (ret < 0)
705 return ret;
706 if (!(ret & STMPE1801_MSK_SYS_CTRL_RESET))
707 return 0;
708 usleep_range(100, 200);
710 return -EIO;
713 static struct stmpe_variant_info stmpe1801 = {
714 .name = "stmpe1801",
715 .id_val = STMPE1801_ID,
716 .id_mask = 0xfff0,
717 .num_gpios = 18,
718 .af_bits = 0,
719 .regs = stmpe1801_regs,
720 .blocks = stmpe1801_blocks,
721 .num_blocks = ARRAY_SIZE(stmpe1801_blocks),
722 .num_irqs = STMPE1801_NR_INTERNAL_IRQS,
723 .enable = stmpe1801_enable,
724 /* stmpe1801 do not have any gpio alternate function */
725 .get_altfunc = NULL,
729 * STMPE24XX
732 static const u8 stmpe24xx_regs[] = {
733 [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
734 [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
735 [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
736 [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
737 [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
738 [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
739 [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
740 [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
741 [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
742 [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
743 [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
744 [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
745 [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
746 [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
749 static struct stmpe_variant_block stmpe24xx_blocks[] = {
751 .cell = &stmpe_gpio_cell,
752 .irq = STMPE24XX_IRQ_GPIOC,
753 .block = STMPE_BLOCK_GPIO,
756 .cell = &stmpe_keypad_cell,
757 .irq = STMPE24XX_IRQ_KEYPAD,
758 .block = STMPE_BLOCK_KEYPAD,
762 static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
763 bool enable)
765 unsigned int mask = 0;
767 if (blocks & STMPE_BLOCK_GPIO)
768 mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
770 if (blocks & STMPE_BLOCK_KEYPAD)
771 mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
773 return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
774 enable ? mask : 0);
777 static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
779 switch (block) {
780 case STMPE_BLOCK_ROTATOR:
781 return 2;
783 case STMPE_BLOCK_KEYPAD:
784 return 1;
786 case STMPE_BLOCK_GPIO:
787 default:
788 return 0;
792 static struct stmpe_variant_info stmpe2401 = {
793 .name = "stmpe2401",
794 .id_val = 0x0101,
795 .id_mask = 0xffff,
796 .num_gpios = 24,
797 .af_bits = 2,
798 .regs = stmpe24xx_regs,
799 .blocks = stmpe24xx_blocks,
800 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
801 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
802 .enable = stmpe24xx_enable,
803 .get_altfunc = stmpe24xx_get_altfunc,
806 static struct stmpe_variant_info stmpe2403 = {
807 .name = "stmpe2403",
808 .id_val = 0x0120,
809 .id_mask = 0xffff,
810 .num_gpios = 24,
811 .af_bits = 2,
812 .regs = stmpe24xx_regs,
813 .blocks = stmpe24xx_blocks,
814 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
815 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
816 .enable = stmpe24xx_enable,
817 .get_altfunc = stmpe24xx_get_altfunc,
818 .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
821 static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
822 [STMPE610] = &stmpe610,
823 [STMPE801] = &stmpe801,
824 [STMPE811] = &stmpe811,
825 [STMPE1601] = &stmpe1601,
826 [STMPE1801] = &stmpe1801,
827 [STMPE2401] = &stmpe2401,
828 [STMPE2403] = &stmpe2403,
832 * These devices can be connected in a 'no-irq' configuration - the irq pin
833 * is not used and the device cannot interrupt the CPU. Here we only list
834 * devices which support this configuration - the driver will fail probing
835 * for any devices not listed here which are configured in this way.
837 static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
838 [STMPE801] = &stmpe801_noirq,
841 static irqreturn_t stmpe_irq(int irq, void *data)
843 struct stmpe *stmpe = data;
844 struct stmpe_variant_info *variant = stmpe->variant;
845 int num = DIV_ROUND_UP(variant->num_irqs, 8);
846 u8 israddr;
847 u8 isr[num];
848 int ret;
849 int i;
851 if (variant->id_val == STMPE801_ID) {
852 int base = irq_create_mapping(stmpe->domain, 0);
854 handle_nested_irq(base);
855 return IRQ_HANDLED;
858 if (variant->id_val == STMPE1801_ID)
859 israddr = stmpe->regs[STMPE_IDX_ISR_LSB];
860 else
861 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
863 ret = stmpe_block_read(stmpe, israddr, num, isr);
864 if (ret < 0)
865 return IRQ_NONE;
867 for (i = 0; i < num; i++) {
868 int bank = num - i - 1;
869 u8 status = isr[i];
870 u8 clear;
872 status &= stmpe->ier[bank];
873 if (!status)
874 continue;
876 clear = status;
877 while (status) {
878 int bit = __ffs(status);
879 int line = bank * 8 + bit;
880 int nestedirq = irq_create_mapping(stmpe->domain, line);
882 handle_nested_irq(nestedirq);
883 status &= ~(1 << bit);
886 stmpe_reg_write(stmpe, israddr + i, clear);
889 return IRQ_HANDLED;
892 static void stmpe_irq_lock(struct irq_data *data)
894 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
896 mutex_lock(&stmpe->irq_lock);
899 static void stmpe_irq_sync_unlock(struct irq_data *data)
901 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
902 struct stmpe_variant_info *variant = stmpe->variant;
903 int num = DIV_ROUND_UP(variant->num_irqs, 8);
904 int i;
906 for (i = 0; i < num; i++) {
907 u8 new = stmpe->ier[i];
908 u8 old = stmpe->oldier[i];
910 if (new == old)
911 continue;
913 stmpe->oldier[i] = new;
914 stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
917 mutex_unlock(&stmpe->irq_lock);
920 static void stmpe_irq_mask(struct irq_data *data)
922 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
923 int offset = data->hwirq;
924 int regoffset = offset / 8;
925 int mask = 1 << (offset % 8);
927 stmpe->ier[regoffset] &= ~mask;
930 static void stmpe_irq_unmask(struct irq_data *data)
932 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
933 int offset = data->hwirq;
934 int regoffset = offset / 8;
935 int mask = 1 << (offset % 8);
937 stmpe->ier[regoffset] |= mask;
940 static struct irq_chip stmpe_irq_chip = {
941 .name = "stmpe",
942 .irq_bus_lock = stmpe_irq_lock,
943 .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
944 .irq_mask = stmpe_irq_mask,
945 .irq_unmask = stmpe_irq_unmask,
948 static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
949 irq_hw_number_t hwirq)
951 struct stmpe *stmpe = d->host_data;
952 struct irq_chip *chip = NULL;
954 if (stmpe->variant->id_val != STMPE801_ID)
955 chip = &stmpe_irq_chip;
957 irq_set_chip_data(virq, stmpe);
958 irq_set_chip_and_handler(virq, chip, handle_edge_irq);
959 irq_set_nested_thread(virq, 1);
960 #ifdef CONFIG_ARM
961 set_irq_flags(virq, IRQF_VALID);
962 #else
963 irq_set_noprobe(virq);
964 #endif
966 return 0;
969 static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
971 #ifdef CONFIG_ARM
972 set_irq_flags(virq, 0);
973 #endif
974 irq_set_chip_and_handler(virq, NULL, NULL);
975 irq_set_chip_data(virq, NULL);
978 static struct irq_domain_ops stmpe_irq_ops = {
979 .map = stmpe_irq_map,
980 .unmap = stmpe_irq_unmap,
981 .xlate = irq_domain_xlate_twocell,
984 static int stmpe_irq_init(struct stmpe *stmpe, struct device_node *np)
986 int base = 0;
987 int num_irqs = stmpe->variant->num_irqs;
989 if (!np)
990 base = stmpe->irq_base;
992 stmpe->domain = irq_domain_add_simple(np, num_irqs, base,
993 &stmpe_irq_ops, stmpe);
994 if (!stmpe->domain) {
995 dev_err(stmpe->dev, "Failed to create irqdomain\n");
996 return -ENOSYS;
999 return 0;
1002 static int stmpe_chip_init(struct stmpe *stmpe)
1004 unsigned int irq_trigger = stmpe->pdata->irq_trigger;
1005 int autosleep_timeout = stmpe->pdata->autosleep_timeout;
1006 struct stmpe_variant_info *variant = stmpe->variant;
1007 u8 icr = 0;
1008 unsigned int id;
1009 u8 data[2];
1010 int ret;
1012 ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
1013 ARRAY_SIZE(data), data);
1014 if (ret < 0)
1015 return ret;
1017 id = (data[0] << 8) | data[1];
1018 if ((id & variant->id_mask) != variant->id_val) {
1019 dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
1020 return -EINVAL;
1023 dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
1025 /* Disable all modules -- subdrivers should enable what they need. */
1026 ret = stmpe_disable(stmpe, ~0);
1027 if (ret)
1028 return ret;
1030 if (id == STMPE1801_ID) {
1031 ret = stmpe1801_reset(stmpe);
1032 if (ret < 0)
1033 return ret;
1036 if (stmpe->irq >= 0) {
1037 if (id == STMPE801_ID)
1038 icr = STMPE801_REG_SYS_CTRL_INT_EN;
1039 else
1040 icr = STMPE_ICR_LSB_GIM;
1042 /* STMPE801 doesn't support Edge interrupts */
1043 if (id != STMPE801_ID) {
1044 if (irq_trigger == IRQF_TRIGGER_FALLING ||
1045 irq_trigger == IRQF_TRIGGER_RISING)
1046 icr |= STMPE_ICR_LSB_EDGE;
1049 if (irq_trigger == IRQF_TRIGGER_RISING ||
1050 irq_trigger == IRQF_TRIGGER_HIGH) {
1051 if (id == STMPE801_ID)
1052 icr |= STMPE801_REG_SYS_CTRL_INT_HI;
1053 else
1054 icr |= STMPE_ICR_LSB_HIGH;
1058 if (stmpe->pdata->autosleep) {
1059 ret = stmpe_autosleep(stmpe, autosleep_timeout);
1060 if (ret)
1061 return ret;
1064 return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
1067 static int stmpe_add_device(struct stmpe *stmpe, struct mfd_cell *cell)
1069 return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
1070 NULL, stmpe->irq_base, stmpe->domain);
1073 static int stmpe_devices_init(struct stmpe *stmpe)
1075 struct stmpe_variant_info *variant = stmpe->variant;
1076 unsigned int platform_blocks = stmpe->pdata->blocks;
1077 int ret = -EINVAL;
1078 int i, j;
1080 for (i = 0; i < variant->num_blocks; i++) {
1081 struct stmpe_variant_block *block = &variant->blocks[i];
1083 if (!(platform_blocks & block->block))
1084 continue;
1086 for (j = 0; j < block->cell->num_resources; j++) {
1087 struct resource *res =
1088 (struct resource *) &block->cell->resources[j];
1090 /* Dynamically fill in a variant's IRQ. */
1091 if (res->flags & IORESOURCE_IRQ)
1092 res->start = res->end = block->irq + j;
1095 platform_blocks &= ~block->block;
1096 ret = stmpe_add_device(stmpe, block->cell);
1097 if (ret)
1098 return ret;
1101 if (platform_blocks)
1102 dev_warn(stmpe->dev,
1103 "platform wants blocks (%#x) not present on variant",
1104 platform_blocks);
1106 return ret;
1109 static void stmpe_of_probe(struct stmpe_platform_data *pdata,
1110 struct device_node *np)
1112 struct device_node *child;
1114 pdata->id = of_alias_get_id(np, "stmpe-i2c");
1115 if (pdata->id < 0)
1116 pdata->id = -1;
1118 pdata->irq_trigger = IRQF_TRIGGER_NONE;
1120 of_property_read_u32(np, "st,autosleep-timeout",
1121 &pdata->autosleep_timeout);
1123 pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
1125 for_each_child_of_node(np, child) {
1126 if (!strcmp(child->name, "stmpe_gpio")) {
1127 pdata->blocks |= STMPE_BLOCK_GPIO;
1128 } else if (!strcmp(child->name, "stmpe_keypad")) {
1129 pdata->blocks |= STMPE_BLOCK_KEYPAD;
1130 } else if (!strcmp(child->name, "stmpe_touchscreen")) {
1131 pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
1132 } else if (!strcmp(child->name, "stmpe_adc")) {
1133 pdata->blocks |= STMPE_BLOCK_ADC;
1134 } else if (!strcmp(child->name, "stmpe_pwm")) {
1135 pdata->blocks |= STMPE_BLOCK_PWM;
1136 } else if (!strcmp(child->name, "stmpe_rotator")) {
1137 pdata->blocks |= STMPE_BLOCK_ROTATOR;
1142 /* Called from client specific probe routines */
1143 int stmpe_probe(struct stmpe_client_info *ci, int partnum)
1145 struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
1146 struct device_node *np = ci->dev->of_node;
1147 struct stmpe *stmpe;
1148 int ret;
1150 if (!pdata) {
1151 if (!np)
1152 return -EINVAL;
1154 pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
1155 if (!pdata)
1156 return -ENOMEM;
1158 stmpe_of_probe(pdata, np);
1160 if (of_find_property(np, "interrupts", NULL) == NULL)
1161 ci->irq = -1;
1164 stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
1165 if (!stmpe)
1166 return -ENOMEM;
1168 mutex_init(&stmpe->irq_lock);
1169 mutex_init(&stmpe->lock);
1171 stmpe->dev = ci->dev;
1172 stmpe->client = ci->client;
1173 stmpe->pdata = pdata;
1174 stmpe->irq_base = pdata->irq_base;
1175 stmpe->ci = ci;
1176 stmpe->partnum = partnum;
1177 stmpe->variant = stmpe_variant_info[partnum];
1178 stmpe->regs = stmpe->variant->regs;
1179 stmpe->num_gpios = stmpe->variant->num_gpios;
1180 dev_set_drvdata(stmpe->dev, stmpe);
1182 if (ci->init)
1183 ci->init(stmpe);
1185 if (pdata->irq_over_gpio) {
1186 ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
1187 GPIOF_DIR_IN, "stmpe");
1188 if (ret) {
1189 dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
1190 ret);
1191 return ret;
1194 stmpe->irq = gpio_to_irq(pdata->irq_gpio);
1195 } else {
1196 stmpe->irq = ci->irq;
1199 if (stmpe->irq < 0) {
1200 /* use alternate variant info for no-irq mode, if supported */
1201 dev_info(stmpe->dev,
1202 "%s configured in no-irq mode by platform data\n",
1203 stmpe->variant->name);
1204 if (!stmpe_noirq_variant_info[stmpe->partnum]) {
1205 dev_err(stmpe->dev,
1206 "%s does not support no-irq mode!\n",
1207 stmpe->variant->name);
1208 return -ENODEV;
1210 stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
1211 } else if (pdata->irq_trigger == IRQF_TRIGGER_NONE) {
1212 pdata->irq_trigger = irq_get_trigger_type(stmpe->irq);
1215 ret = stmpe_chip_init(stmpe);
1216 if (ret)
1217 return ret;
1219 if (stmpe->irq >= 0) {
1220 ret = stmpe_irq_init(stmpe, np);
1221 if (ret)
1222 return ret;
1224 ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
1225 stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
1226 "stmpe", stmpe);
1227 if (ret) {
1228 dev_err(stmpe->dev, "failed to request IRQ: %d\n",
1229 ret);
1230 return ret;
1234 ret = stmpe_devices_init(stmpe);
1235 if (!ret)
1236 return 0;
1238 dev_err(stmpe->dev, "failed to add children\n");
1239 mfd_remove_devices(stmpe->dev);
1241 return ret;
1244 int stmpe_remove(struct stmpe *stmpe)
1246 mfd_remove_devices(stmpe->dev);
1248 return 0;
1251 #ifdef CONFIG_PM
1252 static int stmpe_suspend(struct device *dev)
1254 struct stmpe *stmpe = dev_get_drvdata(dev);
1256 if (stmpe->irq >= 0 && device_may_wakeup(dev))
1257 enable_irq_wake(stmpe->irq);
1259 return 0;
1262 static int stmpe_resume(struct device *dev)
1264 struct stmpe *stmpe = dev_get_drvdata(dev);
1266 if (stmpe->irq >= 0 && device_may_wakeup(dev))
1267 disable_irq_wake(stmpe->irq);
1269 return 0;
1272 const struct dev_pm_ops stmpe_dev_pm_ops = {
1273 .suspend = stmpe_suspend,
1274 .resume = stmpe_resume,
1276 #endif