]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/mfd/stmpe.c
mfd: stmpe: Probe properly from the Device Tree
[mirror_ubuntu-artful-kernel.git] / drivers / mfd / stmpe.c
CommitLineData
27e34995 1/*
1a6e4b74
VK
2 * ST Microelectronics MFD: stmpe's driver
3 *
27e34995
RV
4 * Copyright (C) ST-Ericsson SA 2010
5 *
6 * License Terms: GNU General Public License, version 2
7 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
8 */
9
ac713cc9 10#include <linux/err.h>
73de16db 11#include <linux/gpio.h>
dba61c8f 12#include <linux/export.h>
27e34995 13#include <linux/kernel.h>
27e34995
RV
14#include <linux/interrupt.h>
15#include <linux/irq.h>
76f93992 16#include <linux/irqdomain.h>
20d5c7de 17#include <linux/of.h>
ac713cc9 18#include <linux/of_gpio.h>
1a6e4b74 19#include <linux/pm.h>
27e34995 20#include <linux/slab.h>
27e34995 21#include <linux/mfd/core.h>
230f13a5 22#include <linux/delay.h>
27e34995
RV
23#include "stmpe.h"
24
25static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
26{
27 return stmpe->variant->enable(stmpe, blocks, true);
28}
29
30static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
31{
32 return stmpe->variant->enable(stmpe, blocks, false);
33}
34
35static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
36{
37 int ret;
38
1a6e4b74 39 ret = stmpe->ci->read_byte(stmpe, reg);
27e34995 40 if (ret < 0)
1a6e4b74 41 dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
27e34995
RV
42
43 dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
44
45 return ret;
46}
47
48static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
49{
50 int ret;
51
52 dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
53
1a6e4b74 54 ret = stmpe->ci->write_byte(stmpe, reg, val);
27e34995 55 if (ret < 0)
1a6e4b74 56 dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
27e34995
RV
57
58 return ret;
59}
60
61static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
62{
63 int ret;
64
65 ret = __stmpe_reg_read(stmpe, reg);
66 if (ret < 0)
67 return ret;
68
69 ret &= ~mask;
70 ret |= val;
71
72 return __stmpe_reg_write(stmpe, reg, ret);
73}
74
75static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
76 u8 *values)
77{
78 int ret;
79
1a6e4b74 80 ret = stmpe->ci->read_block(stmpe, reg, length, values);
27e34995 81 if (ret < 0)
1a6e4b74 82 dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
27e34995
RV
83
84 dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
85 stmpe_dump_bytes("stmpe rd: ", values, length);
86
87 return ret;
88}
89
90static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
91 const u8 *values)
92{
93 int ret;
94
95 dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
96 stmpe_dump_bytes("stmpe wr: ", values, length);
97
1a6e4b74 98 ret = stmpe->ci->write_block(stmpe, reg, length, values);
27e34995 99 if (ret < 0)
1a6e4b74 100 dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
27e34995
RV
101
102 return ret;
103}
104
105/**
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
109 */
110int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
111{
112 int ret;
113
114 mutex_lock(&stmpe->lock);
115 ret = __stmpe_enable(stmpe, blocks);
116 mutex_unlock(&stmpe->lock);
117
118 return ret;
119}
120EXPORT_SYMBOL_GPL(stmpe_enable);
121
122/**
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
126 */
127int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
128{
129 int ret;
130
131 mutex_lock(&stmpe->lock);
132 ret = __stmpe_disable(stmpe, blocks);
133 mutex_unlock(&stmpe->lock);
134
135 return ret;
136}
137EXPORT_SYMBOL_GPL(stmpe_disable);
138
139/**
140 * stmpe_reg_read() - read a single STMPE register
141 * @stmpe: Device to read from
142 * @reg: Register to read
143 */
144int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
145{
146 int ret;
147
148 mutex_lock(&stmpe->lock);
149 ret = __stmpe_reg_read(stmpe, reg);
150 mutex_unlock(&stmpe->lock);
151
152 return ret;
153}
154EXPORT_SYMBOL_GPL(stmpe_reg_read);
155
156/**
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
161 */
162int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
163{
164 int ret;
165
166 mutex_lock(&stmpe->lock);
167 ret = __stmpe_reg_write(stmpe, reg, val);
168 mutex_unlock(&stmpe->lock);
169
170 return ret;
171}
172EXPORT_SYMBOL_GPL(stmpe_reg_write);
173
174/**
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
180 */
181int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
182{
183 int ret;
184
185 mutex_lock(&stmpe->lock);
186 ret = __stmpe_set_bits(stmpe, reg, mask, val);
187 mutex_unlock(&stmpe->lock);
188
189 return ret;
190}
191EXPORT_SYMBOL_GPL(stmpe_set_bits);
192
193/**
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
199 */
200int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
201{
202 int ret;
203
204 mutex_lock(&stmpe->lock);
205 ret = __stmpe_block_read(stmpe, reg, length, values);
206 mutex_unlock(&stmpe->lock);
207
208 return ret;
209}
210EXPORT_SYMBOL_GPL(stmpe_block_read);
211
212/**
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
218 */
219int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
220 const u8 *values)
221{
222 int ret;
223
224 mutex_lock(&stmpe->lock);
225 ret = __stmpe_block_write(stmpe, reg, length, values);
226 mutex_unlock(&stmpe->lock);
227
228 return ret;
229}
230EXPORT_SYMBOL_GPL(stmpe_block_write);
231
232/**
4dcaa6b6 233 * stmpe_set_altfunc()- set the alternate function for STMPE pins
27e34995
RV
234 * @stmpe: Device to configure
235 * @pins: Bitmask of pins to affect
236 * @block: block to enable alternate functions for
237 *
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.
240 *
241 * If the GPIO module is not enabled, this function automatically enables it in
242 * order to perform the change.
243 */
244int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
245{
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);
27e34995
RV
250 int mask = (1 << af_bits) - 1;
251 u8 regs[numregs];
7f7f4ea1
VK
252 int af, afperreg, ret;
253
254 if (!variant->get_altfunc)
255 return 0;
27e34995 256
7f7f4ea1 257 afperreg = 8 / af_bits;
27e34995
RV
258 mutex_lock(&stmpe->lock);
259
260 ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
261 if (ret < 0)
262 goto out;
263
264 ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
265 if (ret < 0)
266 goto out;
267
268 af = variant->get_altfunc(stmpe, block);
269
270 while (pins) {
271 int pin = __ffs(pins);
272 int regoffset = numregs - (pin / afperreg) - 1;
273 int pos = (pin % afperreg) * (8 / afperreg);
274
275 regs[regoffset] &= ~(mask << pos);
276 regs[regoffset] |= af << pos;
277
278 pins &= ~(1 << pin);
279 }
280
281 ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
282
283out:
284 mutex_unlock(&stmpe->lock);
285 return ret;
286}
287EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
288
289/*
290 * GPIO (all variants)
291 */
292
293static struct resource stmpe_gpio_resources[] = {
294 /* Start and end filled dynamically */
295 {
296 .flags = IORESOURCE_IRQ,
297 },
298};
299
6bbb3c4c 300static const struct mfd_cell stmpe_gpio_cell = {
27e34995 301 .name = "stmpe-gpio",
86605cfe 302 .of_compatible = "st,stmpe-gpio",
27e34995
RV
303 .resources = stmpe_gpio_resources,
304 .num_resources = ARRAY_SIZE(stmpe_gpio_resources),
305};
306
6bbb3c4c 307static const struct mfd_cell stmpe_gpio_cell_noirq = {
e31f9b82 308 .name = "stmpe-gpio",
86605cfe 309 .of_compatible = "st,stmpe-gpio",
e31f9b82
CB
310 /* gpio cell resources consist of an irq only so no resources here */
311};
312
27e34995
RV
313/*
314 * Keypad (1601, 2401, 2403)
315 */
316
317static struct resource stmpe_keypad_resources[] = {
318 {
319 .name = "KEYPAD",
27e34995
RV
320 .flags = IORESOURCE_IRQ,
321 },
322 {
323 .name = "KEYPAD_OVER",
27e34995
RV
324 .flags = IORESOURCE_IRQ,
325 },
326};
327
6bbb3c4c 328static const struct mfd_cell stmpe_keypad_cell = {
27e34995 329 .name = "stmpe-keypad",
6ea32387 330 .of_compatible = "st,stmpe-keypad",
27e34995
RV
331 .resources = stmpe_keypad_resources,
332 .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
333};
334
7f7f4ea1
VK
335/*
336 * STMPE801
337 */
338static 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,
347
348};
349
350static struct stmpe_variant_block stmpe801_blocks[] = {
351 {
352 .cell = &stmpe_gpio_cell,
353 .irq = 0,
354 .block = STMPE_BLOCK_GPIO,
355 },
356};
357
e31f9b82
CB
358static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
359 {
360 .cell = &stmpe_gpio_cell_noirq,
361 .block = STMPE_BLOCK_GPIO,
362 },
363};
364
7f7f4ea1
VK
365static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
366 bool enable)
367{
368 if (blocks & STMPE_BLOCK_GPIO)
369 return 0;
370 else
371 return -EINVAL;
372}
373
374static 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,
384};
385
e31f9b82
CB
386static 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,
395};
396
27e34995 397/*
1cda2394 398 * Touchscreen (STMPE811 or STMPE610)
27e34995
RV
399 */
400
401static struct resource stmpe_ts_resources[] = {
402 {
403 .name = "TOUCH_DET",
27e34995
RV
404 .flags = IORESOURCE_IRQ,
405 },
406 {
407 .name = "FIFO_TH",
27e34995
RV
408 .flags = IORESOURCE_IRQ,
409 },
410};
411
6bbb3c4c 412static const struct mfd_cell stmpe_ts_cell = {
27e34995 413 .name = "stmpe-ts",
037db524 414 .of_compatible = "st,stmpe-ts",
27e34995
RV
415 .resources = stmpe_ts_resources,
416 .num_resources = ARRAY_SIZE(stmpe_ts_resources),
417};
418
419/*
1cda2394 420 * STMPE811 or STMPE610
27e34995
RV
421 */
422
423static 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,
438};
439
440static struct stmpe_variant_block stmpe811_blocks[] = {
441 {
442 .cell = &stmpe_gpio_cell,
443 .irq = STMPE811_IRQ_GPIOC,
444 .block = STMPE_BLOCK_GPIO,
445 },
446 {
447 .cell = &stmpe_ts_cell,
448 .irq = STMPE811_IRQ_TOUCH_DET,
449 .block = STMPE_BLOCK_TOUCHSCREEN,
450 },
451};
452
453static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
454 bool enable)
455{
456 unsigned int mask = 0;
457
458 if (blocks & STMPE_BLOCK_GPIO)
459 mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
460
461 if (blocks & STMPE_BLOCK_ADC)
462 mask |= STMPE811_SYS_CTRL2_ADC_OFF;
463
464 if (blocks & STMPE_BLOCK_TOUCHSCREEN)
465 mask |= STMPE811_SYS_CTRL2_TSC_OFF;
466
467 return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
468 enable ? 0 : mask);
469}
470
471static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
472{
473 /* 0 for touchscreen, 1 for GPIO */
474 return block != STMPE_BLOCK_TOUCHSCREEN;
475}
476
477static 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,
489};
490
1cda2394
VK
491/* Similar to 811, except number of gpios */
492static 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,
504};
505
27e34995
RV
506/*
507 * STMPE1601
508 */
509
510static 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,
525};
526
527static struct stmpe_variant_block stmpe1601_blocks[] = {
528 {
529 .cell = &stmpe_gpio_cell,
5204e51d 530 .irq = STMPE1601_IRQ_GPIOC,
27e34995
RV
531 .block = STMPE_BLOCK_GPIO,
532 },
533 {
534 .cell = &stmpe_keypad_cell,
5204e51d 535 .irq = STMPE1601_IRQ_KEYPAD,
27e34995
RV
536 .block = STMPE_BLOCK_KEYPAD,
537 },
538};
539
5981f4e6
SI
540/* supported autosleep timeout delay (in msecs) */
541static const int stmpe_autosleep_delay[] = {
542 4, 16, 32, 64, 128, 256, 512, 1024,
543};
544
545static int stmpe_round_timeout(int timeout)
546{
547 int i;
548
549 for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
550 if (stmpe_autosleep_delay[i] >= timeout)
551 return i;
552 }
553
554 /*
555 * requests for delays longer than supported should not return the
556 * longest supported delay
557 */
558 return -EINVAL;
559}
560
561static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
562{
563 int ret;
564
565 if (!stmpe->variant->enable_autosleep)
566 return -ENOSYS;
567
568 mutex_lock(&stmpe->lock);
569 ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
570 mutex_unlock(&stmpe->lock);
571
572 return ret;
573}
574
575/*
576 * Both stmpe 1601/2403 support same layout for autosleep
577 */
578static int stmpe1601_autosleep(struct stmpe *stmpe,
579 int autosleep_timeout)
580{
581 int ret, timeout;
582
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;
588 }
589
590 ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
591 STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
592 timeout);
593 if (ret < 0)
594 return ret;
595
596 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
597 STPME1601_AUTOSLEEP_ENABLE,
598 STPME1601_AUTOSLEEP_ENABLE);
599}
600
27e34995
RV
601static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
602 bool enable)
603{
604 unsigned int mask = 0;
605
606 if (blocks & STMPE_BLOCK_GPIO)
607 mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
b69d2ad6
LW
608 else
609 mask &= ~STMPE1601_SYS_CTRL_ENABLE_GPIO;
27e34995
RV
610
611 if (blocks & STMPE_BLOCK_KEYPAD)
612 mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
b69d2ad6
LW
613 else
614 mask &= ~STMPE1601_SYS_CTRL_ENABLE_KPC;
615
616 if (blocks & STMPE_BLOCK_PWM)
617 mask |= STMPE1601_SYS_CTRL_ENABLE_SPWM;
618 else
619 mask &= ~STMPE1601_SYS_CTRL_ENABLE_SPWM;
27e34995
RV
620
621 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
622 enable ? mask : 0);
623}
624
625static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
626{
627 switch (block) {
628 case STMPE_BLOCK_PWM:
629 return 2;
630
631 case STMPE_BLOCK_KEYPAD:
632 return 1;
633
634 case STMPE_BLOCK_GPIO:
635 default:
636 return 0;
637 }
638}
639
640static struct stmpe_variant_info stmpe1601 = {
641 .name = "stmpe1601",
642 .id_val = 0x0210,
643 .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
644 .num_gpios = 16,
645 .af_bits = 2,
646 .regs = stmpe1601_regs,
647 .blocks = stmpe1601_blocks,
648 .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
649 .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
650 .enable = stmpe1601_enable,
651 .get_altfunc = stmpe1601_get_altfunc,
5981f4e6 652 .enable_autosleep = stmpe1601_autosleep,
27e34995
RV
653};
654
230f13a5
JNG
655/*
656 * STMPE1801
657 */
658static const u8 stmpe1801_regs[] = {
659 [STMPE_IDX_CHIP_ID] = STMPE1801_REG_CHIP_ID,
660 [STMPE_IDX_ICR_LSB] = STMPE1801_REG_INT_CTRL_LOW,
661 [STMPE_IDX_IER_LSB] = STMPE1801_REG_INT_EN_MASK_LOW,
662 [STMPE_IDX_ISR_LSB] = STMPE1801_REG_INT_STA_LOW,
663 [STMPE_IDX_GPMR_LSB] = STMPE1801_REG_GPIO_MP_LOW,
664 [STMPE_IDX_GPSR_LSB] = STMPE1801_REG_GPIO_SET_LOW,
665 [STMPE_IDX_GPCR_LSB] = STMPE1801_REG_GPIO_CLR_LOW,
666 [STMPE_IDX_GPDR_LSB] = STMPE1801_REG_GPIO_SET_DIR_LOW,
667 [STMPE_IDX_GPRER_LSB] = STMPE1801_REG_GPIO_RE_LOW,
668 [STMPE_IDX_GPFER_LSB] = STMPE1801_REG_GPIO_FE_LOW,
669 [STMPE_IDX_IEGPIOR_LSB] = STMPE1801_REG_INT_EN_GPIO_MASK_LOW,
670 [STMPE_IDX_ISGPIOR_LSB] = STMPE1801_REG_INT_STA_GPIO_LOW,
671};
672
673static struct stmpe_variant_block stmpe1801_blocks[] = {
674 {
675 .cell = &stmpe_gpio_cell,
676 .irq = STMPE1801_IRQ_GPIOC,
677 .block = STMPE_BLOCK_GPIO,
678 },
679 {
680 .cell = &stmpe_keypad_cell,
681 .irq = STMPE1801_IRQ_KEYPAD,
682 .block = STMPE_BLOCK_KEYPAD,
683 },
684};
685
686static int stmpe1801_enable(struct stmpe *stmpe, unsigned int blocks,
687 bool enable)
688{
689 unsigned int mask = 0;
690 if (blocks & STMPE_BLOCK_GPIO)
691 mask |= STMPE1801_MSK_INT_EN_GPIO;
692
693 if (blocks & STMPE_BLOCK_KEYPAD)
694 mask |= STMPE1801_MSK_INT_EN_KPC;
695
696 return __stmpe_set_bits(stmpe, STMPE1801_REG_INT_EN_MASK_LOW, mask,
697 enable ? mask : 0);
698}
699
700static int stmpe1801_reset(struct stmpe *stmpe)
701{
702 unsigned long timeout;
703 int ret = 0;
704
705 ret = __stmpe_set_bits(stmpe, STMPE1801_REG_SYS_CTRL,
706 STMPE1801_MSK_SYS_CTRL_RESET, STMPE1801_MSK_SYS_CTRL_RESET);
707 if (ret < 0)
708 return ret;
709
710 timeout = jiffies + msecs_to_jiffies(100);
711 while (time_before(jiffies, timeout)) {
712 ret = __stmpe_reg_read(stmpe, STMPE1801_REG_SYS_CTRL);
713 if (ret < 0)
714 return ret;
715 if (!(ret & STMPE1801_MSK_SYS_CTRL_RESET))
716 return 0;
717 usleep_range(100, 200);
52397fe1 718 }
230f13a5
JNG
719 return -EIO;
720}
721
722static struct stmpe_variant_info stmpe1801 = {
723 .name = "stmpe1801",
724 .id_val = STMPE1801_ID,
725 .id_mask = 0xfff0,
726 .num_gpios = 18,
727 .af_bits = 0,
728 .regs = stmpe1801_regs,
729 .blocks = stmpe1801_blocks,
730 .num_blocks = ARRAY_SIZE(stmpe1801_blocks),
731 .num_irqs = STMPE1801_NR_INTERNAL_IRQS,
732 .enable = stmpe1801_enable,
733 /* stmpe1801 do not have any gpio alternate function */
734 .get_altfunc = NULL,
735};
736
27e34995
RV
737/*
738 * STMPE24XX
739 */
740
741static const u8 stmpe24xx_regs[] = {
742 [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
743 [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
744 [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
745 [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
746 [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
747 [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
748 [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
749 [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
750 [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
751 [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
752 [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
753 [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
754 [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
755 [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
756};
757
758static struct stmpe_variant_block stmpe24xx_blocks[] = {
759 {
760 .cell = &stmpe_gpio_cell,
761 .irq = STMPE24XX_IRQ_GPIOC,
762 .block = STMPE_BLOCK_GPIO,
763 },
764 {
765 .cell = &stmpe_keypad_cell,
766 .irq = STMPE24XX_IRQ_KEYPAD,
767 .block = STMPE_BLOCK_KEYPAD,
768 },
769};
770
771static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
772 bool enable)
773{
774 unsigned int mask = 0;
775
776 if (blocks & STMPE_BLOCK_GPIO)
777 mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
778
779 if (blocks & STMPE_BLOCK_KEYPAD)
780 mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
781
782 return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
783 enable ? mask : 0);
784}
785
786static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
787{
788 switch (block) {
789 case STMPE_BLOCK_ROTATOR:
790 return 2;
791
792 case STMPE_BLOCK_KEYPAD:
793 return 1;
794
795 case STMPE_BLOCK_GPIO:
796 default:
797 return 0;
798 }
799}
800
801static struct stmpe_variant_info stmpe2401 = {
802 .name = "stmpe2401",
803 .id_val = 0x0101,
804 .id_mask = 0xffff,
805 .num_gpios = 24,
806 .af_bits = 2,
807 .regs = stmpe24xx_regs,
808 .blocks = stmpe24xx_blocks,
809 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
810 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
811 .enable = stmpe24xx_enable,
812 .get_altfunc = stmpe24xx_get_altfunc,
813};
814
815static struct stmpe_variant_info stmpe2403 = {
816 .name = "stmpe2403",
817 .id_val = 0x0120,
818 .id_mask = 0xffff,
819 .num_gpios = 24,
820 .af_bits = 2,
821 .regs = stmpe24xx_regs,
822 .blocks = stmpe24xx_blocks,
823 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
824 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
825 .enable = stmpe24xx_enable,
826 .get_altfunc = stmpe24xx_get_altfunc,
5981f4e6 827 .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
27e34995
RV
828};
829
e31f9b82 830static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
1cda2394 831 [STMPE610] = &stmpe610,
7f7f4ea1 832 [STMPE801] = &stmpe801,
27e34995
RV
833 [STMPE811] = &stmpe811,
834 [STMPE1601] = &stmpe1601,
230f13a5 835 [STMPE1801] = &stmpe1801,
27e34995
RV
836 [STMPE2401] = &stmpe2401,
837 [STMPE2403] = &stmpe2403,
838};
839
e31f9b82
CB
840/*
841 * These devices can be connected in a 'no-irq' configuration - the irq pin
842 * is not used and the device cannot interrupt the CPU. Here we only list
843 * devices which support this configuration - the driver will fail probing
844 * for any devices not listed here which are configured in this way.
845 */
846static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
847 [STMPE801] = &stmpe801_noirq,
848};
849
27e34995
RV
850static irqreturn_t stmpe_irq(int irq, void *data)
851{
852 struct stmpe *stmpe = data;
853 struct stmpe_variant_info *variant = stmpe->variant;
854 int num = DIV_ROUND_UP(variant->num_irqs, 8);
230f13a5 855 u8 israddr;
27e34995
RV
856 u8 isr[num];
857 int ret;
858 int i;
859
7f7f4ea1 860 if (variant->id_val == STMPE801_ID) {
76f93992
LJ
861 int base = irq_create_mapping(stmpe->domain, 0);
862
863 handle_nested_irq(base);
7f7f4ea1
VK
864 return IRQ_HANDLED;
865 }
866
230f13a5
JNG
867 if (variant->id_val == STMPE1801_ID)
868 israddr = stmpe->regs[STMPE_IDX_ISR_LSB];
869 else
870 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
871
27e34995
RV
872 ret = stmpe_block_read(stmpe, israddr, num, isr);
873 if (ret < 0)
874 return IRQ_NONE;
875
876 for (i = 0; i < num; i++) {
877 int bank = num - i - 1;
878 u8 status = isr[i];
879 u8 clear;
880
881 status &= stmpe->ier[bank];
882 if (!status)
883 continue;
884
885 clear = status;
886 while (status) {
887 int bit = __ffs(status);
888 int line = bank * 8 + bit;
76f93992 889 int nestedirq = irq_create_mapping(stmpe->domain, line);
27e34995 890
76f93992 891 handle_nested_irq(nestedirq);
27e34995
RV
892 status &= ~(1 << bit);
893 }
894
895 stmpe_reg_write(stmpe, israddr + i, clear);
896 }
897
898 return IRQ_HANDLED;
899}
900
43b8c084 901static void stmpe_irq_lock(struct irq_data *data)
27e34995 902{
43b8c084 903 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
27e34995
RV
904
905 mutex_lock(&stmpe->irq_lock);
906}
907
43b8c084 908static void stmpe_irq_sync_unlock(struct irq_data *data)
27e34995 909{
43b8c084 910 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
27e34995
RV
911 struct stmpe_variant_info *variant = stmpe->variant;
912 int num = DIV_ROUND_UP(variant->num_irqs, 8);
913 int i;
914
915 for (i = 0; i < num; i++) {
916 u8 new = stmpe->ier[i];
917 u8 old = stmpe->oldier[i];
918
919 if (new == old)
920 continue;
921
922 stmpe->oldier[i] = new;
923 stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
924 }
925
926 mutex_unlock(&stmpe->irq_lock);
927}
928
43b8c084 929static void stmpe_irq_mask(struct irq_data *data)
27e34995 930{
43b8c084 931 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
76f93992 932 int offset = data->hwirq;
27e34995
RV
933 int regoffset = offset / 8;
934 int mask = 1 << (offset % 8);
935
936 stmpe->ier[regoffset] &= ~mask;
937}
938
43b8c084 939static void stmpe_irq_unmask(struct irq_data *data)
27e34995 940{
43b8c084 941 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
76f93992 942 int offset = data->hwirq;
27e34995
RV
943 int regoffset = offset / 8;
944 int mask = 1 << (offset % 8);
945
946 stmpe->ier[regoffset] |= mask;
947}
948
949static struct irq_chip stmpe_irq_chip = {
950 .name = "stmpe",
43b8c084
MB
951 .irq_bus_lock = stmpe_irq_lock,
952 .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
953 .irq_mask = stmpe_irq_mask,
954 .irq_unmask = stmpe_irq_unmask,
27e34995
RV
955};
956
76f93992
LJ
957static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
958 irq_hw_number_t hwirq)
27e34995 959{
76f93992 960 struct stmpe *stmpe = d->host_data;
7f7f4ea1 961 struct irq_chip *chip = NULL;
27e34995 962
7f7f4ea1
VK
963 if (stmpe->variant->id_val != STMPE801_ID)
964 chip = &stmpe_irq_chip;
965
76f93992
LJ
966 irq_set_chip_data(virq, stmpe);
967 irq_set_chip_and_handler(virq, chip, handle_edge_irq);
968 irq_set_nested_thread(virq, 1);
27e34995 969#ifdef CONFIG_ARM
76f93992 970 set_irq_flags(virq, IRQF_VALID);
27e34995 971#else
76f93992 972 irq_set_noprobe(virq);
27e34995 973#endif
27e34995
RV
974
975 return 0;
976}
977
76f93992 978static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
27e34995 979{
27e34995 980#ifdef CONFIG_ARM
76f93992 981 set_irq_flags(virq, 0);
27e34995 982#endif
76f93992
LJ
983 irq_set_chip_and_handler(virq, NULL, NULL);
984 irq_set_chip_data(virq, NULL);
985}
986
987static struct irq_domain_ops stmpe_irq_ops = {
988 .map = stmpe_irq_map,
989 .unmap = stmpe_irq_unmap,
990 .xlate = irq_domain_xlate_twocell,
991};
992
612b95cd 993static int stmpe_irq_init(struct stmpe *stmpe, struct device_node *np)
76f93992 994{
b20a4371 995 int base = 0;
76f93992
LJ
996 int num_irqs = stmpe->variant->num_irqs;
997
b20a4371
LJ
998 if (!np)
999 base = stmpe->irq_base;
76f93992 1000
b20a4371
LJ
1001 stmpe->domain = irq_domain_add_simple(np, num_irqs, base,
1002 &stmpe_irq_ops, stmpe);
76f93992
LJ
1003 if (!stmpe->domain) {
1004 dev_err(stmpe->dev, "Failed to create irqdomain\n");
1005 return -ENOSYS;
27e34995 1006 }
76f93992
LJ
1007
1008 return 0;
27e34995
RV
1009}
1010
612b95cd 1011static int stmpe_chip_init(struct stmpe *stmpe)
27e34995
RV
1012{
1013 unsigned int irq_trigger = stmpe->pdata->irq_trigger;
5981f4e6 1014 int autosleep_timeout = stmpe->pdata->autosleep_timeout;
27e34995 1015 struct stmpe_variant_info *variant = stmpe->variant;
e31f9b82 1016 u8 icr = 0;
27e34995
RV
1017 unsigned int id;
1018 u8 data[2];
1019 int ret;
1020
1021 ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
1022 ARRAY_SIZE(data), data);
1023 if (ret < 0)
1024 return ret;
1025
1026 id = (data[0] << 8) | data[1];
1027 if ((id & variant->id_mask) != variant->id_val) {
1028 dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
1029 return -EINVAL;
1030 }
1031
1032 dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
1033
1034 /* Disable all modules -- subdrivers should enable what they need. */
1035 ret = stmpe_disable(stmpe, ~0);
1036 if (ret)
1037 return ret;
1038
230f13a5
JNG
1039 if (id == STMPE1801_ID) {
1040 ret = stmpe1801_reset(stmpe);
1041 if (ret < 0)
1042 return ret;
1043 }
1044
e31f9b82 1045 if (stmpe->irq >= 0) {
7f7f4ea1 1046 if (id == STMPE801_ID)
e31f9b82 1047 icr = STMPE801_REG_SYS_CTRL_INT_EN;
7f7f4ea1 1048 else
e31f9b82 1049 icr = STMPE_ICR_LSB_GIM;
27e34995 1050
e31f9b82
CB
1051 /* STMPE801 doesn't support Edge interrupts */
1052 if (id != STMPE801_ID) {
1053 if (irq_trigger == IRQF_TRIGGER_FALLING ||
1054 irq_trigger == IRQF_TRIGGER_RISING)
1055 icr |= STMPE_ICR_LSB_EDGE;
1056 }
1057
1058 if (irq_trigger == IRQF_TRIGGER_RISING ||
1059 irq_trigger == IRQF_TRIGGER_HIGH) {
1060 if (id == STMPE801_ID)
1061 icr |= STMPE801_REG_SYS_CTRL_INT_HI;
1062 else
1063 icr |= STMPE_ICR_LSB_HIGH;
1064 }
7f7f4ea1 1065 }
27e34995 1066
5981f4e6
SI
1067 if (stmpe->pdata->autosleep) {
1068 ret = stmpe_autosleep(stmpe, autosleep_timeout);
1069 if (ret)
1070 return ret;
1071 }
1072
27e34995
RV
1073 return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
1074}
1075
6bbb3c4c 1076static int stmpe_add_device(struct stmpe *stmpe, const struct mfd_cell *cell)
27e34995
RV
1077{
1078 return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
76f93992 1079 NULL, stmpe->irq_base, stmpe->domain);
27e34995
RV
1080}
1081
612b95cd 1082static int stmpe_devices_init(struct stmpe *stmpe)
27e34995
RV
1083{
1084 struct stmpe_variant_info *variant = stmpe->variant;
1085 unsigned int platform_blocks = stmpe->pdata->blocks;
1086 int ret = -EINVAL;
7da0cbfc 1087 int i, j;
27e34995
RV
1088
1089 for (i = 0; i < variant->num_blocks; i++) {
1090 struct stmpe_variant_block *block = &variant->blocks[i];
1091
1092 if (!(platform_blocks & block->block))
1093 continue;
1094
7da0cbfc
LJ
1095 for (j = 0; j < block->cell->num_resources; j++) {
1096 struct resource *res =
1097 (struct resource *) &block->cell->resources[j];
1098
1099 /* Dynamically fill in a variant's IRQ. */
1100 if (res->flags & IORESOURCE_IRQ)
1101 res->start = res->end = block->irq + j;
1102 }
1103
27e34995 1104 platform_blocks &= ~block->block;
7da0cbfc 1105 ret = stmpe_add_device(stmpe, block->cell);
27e34995
RV
1106 if (ret)
1107 return ret;
1108 }
1109
1110 if (platform_blocks)
1111 dev_warn(stmpe->dev,
1112 "platform wants blocks (%#x) not present on variant",
1113 platform_blocks);
1114
1115 return ret;
1116}
1117
a9c4055d
MB
1118static void stmpe_of_probe(struct stmpe_platform_data *pdata,
1119 struct device_node *np)
909582ca
LJ
1120{
1121 struct device_node *child;
1122
408a3fa8
GF
1123 pdata->id = of_alias_get_id(np, "stmpe-i2c");
1124 if (pdata->id < 0)
1125 pdata->id = -1;
1126
ac713cc9
VKS
1127 pdata->irq_trigger = IRQF_TRIGGER_NONE;
1128
909582ca
LJ
1129 of_property_read_u32(np, "st,autosleep-timeout",
1130 &pdata->autosleep_timeout);
1131
1132 pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
1133
1134 for_each_child_of_node(np, child) {
1135 if (!strcmp(child->name, "stmpe_gpio")) {
1136 pdata->blocks |= STMPE_BLOCK_GPIO;
ac713cc9 1137 } else if (!strcmp(child->name, "stmpe_keypad")) {
909582ca 1138 pdata->blocks |= STMPE_BLOCK_KEYPAD;
ac713cc9 1139 } else if (!strcmp(child->name, "stmpe_touchscreen")) {
909582ca 1140 pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
ac713cc9 1141 } else if (!strcmp(child->name, "stmpe_adc")) {
909582ca 1142 pdata->blocks |= STMPE_BLOCK_ADC;
ac713cc9
VKS
1143 } else if (!strcmp(child->name, "stmpe_pwm")) {
1144 pdata->blocks |= STMPE_BLOCK_PWM;
1145 } else if (!strcmp(child->name, "stmpe_rotator")) {
1146 pdata->blocks |= STMPE_BLOCK_ROTATOR;
909582ca
LJ
1147 }
1148 }
1149}
1150
1a6e4b74 1151/* Called from client specific probe routines */
612b95cd 1152int stmpe_probe(struct stmpe_client_info *ci, int partnum)
208c4343 1153{
1a6e4b74 1154 struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
909582ca 1155 struct device_node *np = ci->dev->of_node;
27e34995
RV
1156 struct stmpe *stmpe;
1157 int ret;
1158
909582ca 1159 if (!pdata) {
cb5faba9 1160 if (!np)
909582ca 1161 return -EINVAL;
cb5faba9
VK
1162
1163 pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
1164 if (!pdata)
1165 return -ENOMEM;
1166
1167 stmpe_of_probe(pdata, np);
a200e320
GF
1168
1169 if (of_find_property(np, "interrupts", NULL) == NULL)
1170 ci->irq = -1;
909582ca 1171 }
27e34995 1172
cb5faba9 1173 stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
27e34995
RV
1174 if (!stmpe)
1175 return -ENOMEM;
1176
1177 mutex_init(&stmpe->irq_lock);
1178 mutex_init(&stmpe->lock);
1179
1a6e4b74
VK
1180 stmpe->dev = ci->dev;
1181 stmpe->client = ci->client;
27e34995
RV
1182 stmpe->pdata = pdata;
1183 stmpe->irq_base = pdata->irq_base;
1a6e4b74
VK
1184 stmpe->ci = ci;
1185 stmpe->partnum = partnum;
1186 stmpe->variant = stmpe_variant_info[partnum];
27e34995
RV
1187 stmpe->regs = stmpe->variant->regs;
1188 stmpe->num_gpios = stmpe->variant->num_gpios;
1a6e4b74 1189 dev_set_drvdata(stmpe->dev, stmpe);
27e34995 1190
1a6e4b74
VK
1191 if (ci->init)
1192 ci->init(stmpe);
27e34995 1193
73de16db 1194 if (pdata->irq_over_gpio) {
cb5faba9
VK
1195 ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
1196 GPIOF_DIR_IN, "stmpe");
73de16db
VK
1197 if (ret) {
1198 dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
1199 ret);
cb5faba9 1200 return ret;
73de16db
VK
1201 }
1202
1203 stmpe->irq = gpio_to_irq(pdata->irq_gpio);
1204 } else {
1a6e4b74 1205 stmpe->irq = ci->irq;
73de16db
VK
1206 }
1207
e31f9b82
CB
1208 if (stmpe->irq < 0) {
1209 /* use alternate variant info for no-irq mode, if supported */
1210 dev_info(stmpe->dev,
1211 "%s configured in no-irq mode by platform data\n",
1212 stmpe->variant->name);
1213 if (!stmpe_noirq_variant_info[stmpe->partnum]) {
1214 dev_err(stmpe->dev,
1215 "%s does not support no-irq mode!\n",
1216 stmpe->variant->name);
cb5faba9 1217 return -ENODEV;
e31f9b82
CB
1218 }
1219 stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
ac713cc9 1220 } else if (pdata->irq_trigger == IRQF_TRIGGER_NONE) {
1a5595cb 1221 pdata->irq_trigger = irq_get_trigger_type(stmpe->irq);
e31f9b82
CB
1222 }
1223
27e34995
RV
1224 ret = stmpe_chip_init(stmpe);
1225 if (ret)
cb5faba9 1226 return ret;
27e34995 1227
e31f9b82 1228 if (stmpe->irq >= 0) {
909582ca 1229 ret = stmpe_irq_init(stmpe, np);
e31f9b82 1230 if (ret)
cb5faba9 1231 return ret;
27e34995 1232
cb5faba9
VK
1233 ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
1234 stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
e31f9b82
CB
1235 "stmpe", stmpe);
1236 if (ret) {
1237 dev_err(stmpe->dev, "failed to request IRQ: %d\n",
1238 ret);
cb5faba9 1239 return ret;
e31f9b82 1240 }
27e34995
RV
1241 }
1242
1243 ret = stmpe_devices_init(stmpe);
cb5faba9
VK
1244 if (!ret)
1245 return 0;
27e34995 1246
cb5faba9 1247 dev_err(stmpe->dev, "failed to add children\n");
27e34995 1248 mfd_remove_devices(stmpe->dev);
cb5faba9 1249
27e34995
RV
1250 return ret;
1251}
1252
1a6e4b74 1253int stmpe_remove(struct stmpe *stmpe)
27e34995 1254{
27e34995
RV
1255 mfd_remove_devices(stmpe->dev);
1256
27e34995
RV
1257 return 0;
1258}
1259
208c4343 1260#ifdef CONFIG_PM
1a6e4b74
VK
1261static int stmpe_suspend(struct device *dev)
1262{
1263 struct stmpe *stmpe = dev_get_drvdata(dev);
208c4343 1264
e31f9b82 1265 if (stmpe->irq >= 0 && device_may_wakeup(dev))
1a6e4b74 1266 enable_irq_wake(stmpe->irq);
27e34995 1267
1a6e4b74 1268 return 0;
27e34995 1269}
27e34995 1270
1a6e4b74 1271static int stmpe_resume(struct device *dev)
27e34995 1272{
1a6e4b74
VK
1273 struct stmpe *stmpe = dev_get_drvdata(dev);
1274
e31f9b82 1275 if (stmpe->irq >= 0 && device_may_wakeup(dev))
1a6e4b74
VK
1276 disable_irq_wake(stmpe->irq);
1277
1278 return 0;
27e34995 1279}
27e34995 1280
1a6e4b74
VK
1281const struct dev_pm_ops stmpe_dev_pm_ops = {
1282 .suspend = stmpe_suspend,
1283 .resume = stmpe_resume,
1284};
1285#endif