Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[pandora-kernel.git] / drivers / mfd / stmpe.c
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  *
4  * License Terms: GNU General Public License, version 2
5  * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/mfd/core.h>
15 #include <linux/mfd/stmpe.h>
16 #include "stmpe.h"
17
18 static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
19 {
20         return stmpe->variant->enable(stmpe, blocks, true);
21 }
22
23 static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
24 {
25         return stmpe->variant->enable(stmpe, blocks, false);
26 }
27
28 static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
29 {
30         int ret;
31
32         ret = i2c_smbus_read_byte_data(stmpe->i2c, reg);
33         if (ret < 0)
34                 dev_err(stmpe->dev, "failed to read reg %#x: %d\n",
35                         reg, ret);
36
37         dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
38
39         return ret;
40 }
41
42 static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
43 {
44         int ret;
45
46         dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
47
48         ret = i2c_smbus_write_byte_data(stmpe->i2c, reg, val);
49         if (ret < 0)
50                 dev_err(stmpe->dev, "failed to write reg %#x: %d\n",
51                         reg, ret);
52
53         return ret;
54 }
55
56 static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
57 {
58         int ret;
59
60         ret = __stmpe_reg_read(stmpe, reg);
61         if (ret < 0)
62                 return ret;
63
64         ret &= ~mask;
65         ret |= val;
66
67         return __stmpe_reg_write(stmpe, reg, ret);
68 }
69
70 static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
71                               u8 *values)
72 {
73         int ret;
74
75         ret = i2c_smbus_read_i2c_block_data(stmpe->i2c, reg, length, values);
76         if (ret < 0)
77                 dev_err(stmpe->dev, "failed to read regs %#x: %d\n",
78                         reg, ret);
79
80         dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
81         stmpe_dump_bytes("stmpe rd: ", values, length);
82
83         return ret;
84 }
85
86 static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
87                         const u8 *values)
88 {
89         int ret;
90
91         dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
92         stmpe_dump_bytes("stmpe wr: ", values, length);
93
94         ret = i2c_smbus_write_i2c_block_data(stmpe->i2c, reg, length,
95                                              values);
96         if (ret < 0)
97                 dev_err(stmpe->dev, "failed to write regs %#x: %d\n",
98                         reg, ret);
99
100         return ret;
101 }
102
103 /**
104  * stmpe_enable - enable blocks on an STMPE device
105  * @stmpe:      Device to work on
106  * @blocks:     Mask of blocks (enum stmpe_block values) to enable
107  */
108 int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
109 {
110         int ret;
111
112         mutex_lock(&stmpe->lock);
113         ret = __stmpe_enable(stmpe, blocks);
114         mutex_unlock(&stmpe->lock);
115
116         return ret;
117 }
118 EXPORT_SYMBOL_GPL(stmpe_enable);
119
120 /**
121  * stmpe_disable - disable blocks on an STMPE device
122  * @stmpe:      Device to work on
123  * @blocks:     Mask of blocks (enum stmpe_block values) to enable
124  */
125 int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
126 {
127         int ret;
128
129         mutex_lock(&stmpe->lock);
130         ret = __stmpe_disable(stmpe, blocks);
131         mutex_unlock(&stmpe->lock);
132
133         return ret;
134 }
135 EXPORT_SYMBOL_GPL(stmpe_disable);
136
137 /**
138  * stmpe_reg_read() - read a single STMPE register
139  * @stmpe:      Device to read from
140  * @reg:        Register to read
141  */
142 int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
143 {
144         int ret;
145
146         mutex_lock(&stmpe->lock);
147         ret = __stmpe_reg_read(stmpe, reg);
148         mutex_unlock(&stmpe->lock);
149
150         return ret;
151 }
152 EXPORT_SYMBOL_GPL(stmpe_reg_read);
153
154 /**
155  * stmpe_reg_write() - write a single STMPE register
156  * @stmpe:      Device to write to
157  * @reg:        Register to write
158  * @val:        Value to write
159  */
160 int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
161 {
162         int ret;
163
164         mutex_lock(&stmpe->lock);
165         ret = __stmpe_reg_write(stmpe, reg, val);
166         mutex_unlock(&stmpe->lock);
167
168         return ret;
169 }
170 EXPORT_SYMBOL_GPL(stmpe_reg_write);
171
172 /**
173  * stmpe_set_bits() - set the value of a bitfield in a STMPE register
174  * @stmpe:      Device to write to
175  * @reg:        Register to write
176  * @mask:       Mask of bits to set
177  * @val:        Value to set
178  */
179 int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
180 {
181         int ret;
182
183         mutex_lock(&stmpe->lock);
184         ret = __stmpe_set_bits(stmpe, reg, mask, val);
185         mutex_unlock(&stmpe->lock);
186
187         return ret;
188 }
189 EXPORT_SYMBOL_GPL(stmpe_set_bits);
190
191 /**
192  * stmpe_block_read() - read multiple STMPE registers
193  * @stmpe:      Device to read from
194  * @reg:        First register
195  * @length:     Number of registers
196  * @values:     Buffer to write to
197  */
198 int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
199 {
200         int ret;
201
202         mutex_lock(&stmpe->lock);
203         ret = __stmpe_block_read(stmpe, reg, length, values);
204         mutex_unlock(&stmpe->lock);
205
206         return ret;
207 }
208 EXPORT_SYMBOL_GPL(stmpe_block_read);
209
210 /**
211  * stmpe_block_write() - write multiple STMPE registers
212  * @stmpe:      Device to write to
213  * @reg:        First register
214  * @length:     Number of registers
215  * @values:     Values to write
216  */
217 int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
218                       const u8 *values)
219 {
220         int ret;
221
222         mutex_lock(&stmpe->lock);
223         ret = __stmpe_block_write(stmpe, reg, length, values);
224         mutex_unlock(&stmpe->lock);
225
226         return ret;
227 }
228 EXPORT_SYMBOL_GPL(stmpe_block_write);
229
230 /**
231  * stmpe_set_altfunc()- set the alternate function for STMPE pins
232  * @stmpe:      Device to configure
233  * @pins:       Bitmask of pins to affect
234  * @block:      block to enable alternate functions for
235  *
236  * @pins is assumed to have a bit set for each of the bits whose alternate
237  * function is to be changed, numbered according to the GPIOXY numbers.
238  *
239  * If the GPIO module is not enabled, this function automatically enables it in
240  * order to perform the change.
241  */
242 int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
243 {
244         struct stmpe_variant_info *variant = stmpe->variant;
245         u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
246         int af_bits = variant->af_bits;
247         int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
248         int afperreg = 8 / af_bits;
249         int mask = (1 << af_bits) - 1;
250         u8 regs[numregs];
251         int af;
252         int ret;
253
254         mutex_lock(&stmpe->lock);
255
256         ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
257         if (ret < 0)
258                 goto out;
259
260         ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
261         if (ret < 0)
262                 goto out;
263
264         af = variant->get_altfunc(stmpe, block);
265
266         while (pins) {
267                 int pin = __ffs(pins);
268                 int regoffset = numregs - (pin / afperreg) - 1;
269                 int pos = (pin % afperreg) * (8 / afperreg);
270
271                 regs[regoffset] &= ~(mask << pos);
272                 regs[regoffset] |= af << pos;
273
274                 pins &= ~(1 << pin);
275         }
276
277         ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
278
279 out:
280         mutex_unlock(&stmpe->lock);
281         return ret;
282 }
283 EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
284
285 /*
286  * GPIO (all variants)
287  */
288
289 static struct resource stmpe_gpio_resources[] = {
290         /* Start and end filled dynamically */
291         {
292                 .flags  = IORESOURCE_IRQ,
293         },
294 };
295
296 static struct mfd_cell stmpe_gpio_cell = {
297         .name           = "stmpe-gpio",
298         .resources      = stmpe_gpio_resources,
299         .num_resources  = ARRAY_SIZE(stmpe_gpio_resources),
300 };
301
302 /*
303  * Keypad (1601, 2401, 2403)
304  */
305
306 static struct resource stmpe_keypad_resources[] = {
307         {
308                 .name   = "KEYPAD",
309                 .start  = 0,
310                 .end    = 0,
311                 .flags  = IORESOURCE_IRQ,
312         },
313         {
314                 .name   = "KEYPAD_OVER",
315                 .start  = 1,
316                 .end    = 1,
317                 .flags  = IORESOURCE_IRQ,
318         },
319 };
320
321 static struct mfd_cell stmpe_keypad_cell = {
322         .name           = "stmpe-keypad",
323         .resources      = stmpe_keypad_resources,
324         .num_resources  = ARRAY_SIZE(stmpe_keypad_resources),
325 };
326
327 /*
328  * Touchscreen (STMPE811)
329  */
330
331 static struct resource stmpe_ts_resources[] = {
332         {
333                 .name   = "TOUCH_DET",
334                 .start  = 0,
335                 .end    = 0,
336                 .flags  = IORESOURCE_IRQ,
337         },
338         {
339                 .name   = "FIFO_TH",
340                 .start  = 1,
341                 .end    = 1,
342                 .flags  = IORESOURCE_IRQ,
343         },
344 };
345
346 static struct mfd_cell stmpe_ts_cell = {
347         .name           = "stmpe-ts",
348         .resources      = stmpe_ts_resources,
349         .num_resources  = ARRAY_SIZE(stmpe_ts_resources),
350 };
351
352 /*
353  * STMPE811
354  */
355
356 static const u8 stmpe811_regs[] = {
357         [STMPE_IDX_CHIP_ID]     = STMPE811_REG_CHIP_ID,
358         [STMPE_IDX_ICR_LSB]     = STMPE811_REG_INT_CTRL,
359         [STMPE_IDX_IER_LSB]     = STMPE811_REG_INT_EN,
360         [STMPE_IDX_ISR_MSB]     = STMPE811_REG_INT_STA,
361         [STMPE_IDX_GPMR_LSB]    = STMPE811_REG_GPIO_MP_STA,
362         [STMPE_IDX_GPSR_LSB]    = STMPE811_REG_GPIO_SET_PIN,
363         [STMPE_IDX_GPCR_LSB]    = STMPE811_REG_GPIO_CLR_PIN,
364         [STMPE_IDX_GPDR_LSB]    = STMPE811_REG_GPIO_DIR,
365         [STMPE_IDX_GPRER_LSB]   = STMPE811_REG_GPIO_RE,
366         [STMPE_IDX_GPFER_LSB]   = STMPE811_REG_GPIO_FE,
367         [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
368         [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
369         [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
370         [STMPE_IDX_GPEDR_MSB]   = STMPE811_REG_GPIO_ED,
371 };
372
373 static struct stmpe_variant_block stmpe811_blocks[] = {
374         {
375                 .cell   = &stmpe_gpio_cell,
376                 .irq    = STMPE811_IRQ_GPIOC,
377                 .block  = STMPE_BLOCK_GPIO,
378         },
379         {
380                 .cell   = &stmpe_ts_cell,
381                 .irq    = STMPE811_IRQ_TOUCH_DET,
382                 .block  = STMPE_BLOCK_TOUCHSCREEN,
383         },
384 };
385
386 static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
387                            bool enable)
388 {
389         unsigned int mask = 0;
390
391         if (blocks & STMPE_BLOCK_GPIO)
392                 mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
393
394         if (blocks & STMPE_BLOCK_ADC)
395                 mask |= STMPE811_SYS_CTRL2_ADC_OFF;
396
397         if (blocks & STMPE_BLOCK_TOUCHSCREEN)
398                 mask |= STMPE811_SYS_CTRL2_TSC_OFF;
399
400         return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
401                                 enable ? 0 : mask);
402 }
403
404 static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
405 {
406         /* 0 for touchscreen, 1 for GPIO */
407         return block != STMPE_BLOCK_TOUCHSCREEN;
408 }
409
410 static struct stmpe_variant_info stmpe811 = {
411         .name           = "stmpe811",
412         .id_val         = 0x0811,
413         .id_mask        = 0xffff,
414         .num_gpios      = 8,
415         .af_bits        = 1,
416         .regs           = stmpe811_regs,
417         .blocks         = stmpe811_blocks,
418         .num_blocks     = ARRAY_SIZE(stmpe811_blocks),
419         .num_irqs       = STMPE811_NR_INTERNAL_IRQS,
420         .enable         = stmpe811_enable,
421         .get_altfunc    = stmpe811_get_altfunc,
422 };
423
424 /*
425  * STMPE1601
426  */
427
428 static const u8 stmpe1601_regs[] = {
429         [STMPE_IDX_CHIP_ID]     = STMPE1601_REG_CHIP_ID,
430         [STMPE_IDX_ICR_LSB]     = STMPE1601_REG_ICR_LSB,
431         [STMPE_IDX_IER_LSB]     = STMPE1601_REG_IER_LSB,
432         [STMPE_IDX_ISR_MSB]     = STMPE1601_REG_ISR_MSB,
433         [STMPE_IDX_GPMR_LSB]    = STMPE1601_REG_GPIO_MP_LSB,
434         [STMPE_IDX_GPSR_LSB]    = STMPE1601_REG_GPIO_SET_LSB,
435         [STMPE_IDX_GPCR_LSB]    = STMPE1601_REG_GPIO_CLR_LSB,
436         [STMPE_IDX_GPDR_LSB]    = STMPE1601_REG_GPIO_SET_DIR_LSB,
437         [STMPE_IDX_GPRER_LSB]   = STMPE1601_REG_GPIO_RE_LSB,
438         [STMPE_IDX_GPFER_LSB]   = STMPE1601_REG_GPIO_FE_LSB,
439         [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
440         [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
441         [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
442         [STMPE_IDX_GPEDR_MSB]   = STMPE1601_REG_GPIO_ED_MSB,
443 };
444
445 static struct stmpe_variant_block stmpe1601_blocks[] = {
446         {
447                 .cell   = &stmpe_gpio_cell,
448                 .irq    = STMPE24XX_IRQ_GPIOC,
449                 .block  = STMPE_BLOCK_GPIO,
450         },
451         {
452                 .cell   = &stmpe_keypad_cell,
453                 .irq    = STMPE24XX_IRQ_KEYPAD,
454                 .block  = STMPE_BLOCK_KEYPAD,
455         },
456 };
457
458 /* supported autosleep timeout delay (in msecs) */
459 static const int stmpe_autosleep_delay[] = {
460         4, 16, 32, 64, 128, 256, 512, 1024,
461 };
462
463 static int stmpe_round_timeout(int timeout)
464 {
465         int i;
466
467         for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
468                 if (stmpe_autosleep_delay[i] >= timeout)
469                         return i;
470         }
471
472         /*
473          * requests for delays longer than supported should not return the
474          * longest supported delay
475          */
476         return -EINVAL;
477 }
478
479 static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
480 {
481         int ret;
482
483         if (!stmpe->variant->enable_autosleep)
484                 return -ENOSYS;
485
486         mutex_lock(&stmpe->lock);
487         ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
488         mutex_unlock(&stmpe->lock);
489
490         return ret;
491 }
492
493 /*
494  * Both stmpe 1601/2403 support same layout for autosleep
495  */
496 static int stmpe1601_autosleep(struct stmpe *stmpe,
497                 int autosleep_timeout)
498 {
499         int ret, timeout;
500
501         /* choose the best available timeout */
502         timeout = stmpe_round_timeout(autosleep_timeout);
503         if (timeout < 0) {
504                 dev_err(stmpe->dev, "invalid timeout\n");
505                 return timeout;
506         }
507
508         ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
509                         STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
510                         timeout);
511         if (ret < 0)
512                 return ret;
513
514         return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
515                         STPME1601_AUTOSLEEP_ENABLE,
516                         STPME1601_AUTOSLEEP_ENABLE);
517 }
518
519 static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
520                             bool enable)
521 {
522         unsigned int mask = 0;
523
524         if (blocks & STMPE_BLOCK_GPIO)
525                 mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
526
527         if (blocks & STMPE_BLOCK_KEYPAD)
528                 mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
529
530         return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
531                                 enable ? mask : 0);
532 }
533
534 static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
535 {
536         switch (block) {
537         case STMPE_BLOCK_PWM:
538                 return 2;
539
540         case STMPE_BLOCK_KEYPAD:
541                 return 1;
542
543         case STMPE_BLOCK_GPIO:
544         default:
545                 return 0;
546         }
547 }
548
549 static struct stmpe_variant_info stmpe1601 = {
550         .name           = "stmpe1601",
551         .id_val         = 0x0210,
552         .id_mask        = 0xfff0,       /* at least 0x0210 and 0x0212 */
553         .num_gpios      = 16,
554         .af_bits        = 2,
555         .regs           = stmpe1601_regs,
556         .blocks         = stmpe1601_blocks,
557         .num_blocks     = ARRAY_SIZE(stmpe1601_blocks),
558         .num_irqs       = STMPE1601_NR_INTERNAL_IRQS,
559         .enable         = stmpe1601_enable,
560         .get_altfunc    = stmpe1601_get_altfunc,
561         .enable_autosleep       = stmpe1601_autosleep,
562 };
563
564 /*
565  * STMPE24XX
566  */
567
568 static const u8 stmpe24xx_regs[] = {
569         [STMPE_IDX_CHIP_ID]     = STMPE24XX_REG_CHIP_ID,
570         [STMPE_IDX_ICR_LSB]     = STMPE24XX_REG_ICR_LSB,
571         [STMPE_IDX_IER_LSB]     = STMPE24XX_REG_IER_LSB,
572         [STMPE_IDX_ISR_MSB]     = STMPE24XX_REG_ISR_MSB,
573         [STMPE_IDX_GPMR_LSB]    = STMPE24XX_REG_GPMR_LSB,
574         [STMPE_IDX_GPSR_LSB]    = STMPE24XX_REG_GPSR_LSB,
575         [STMPE_IDX_GPCR_LSB]    = STMPE24XX_REG_GPCR_LSB,
576         [STMPE_IDX_GPDR_LSB]    = STMPE24XX_REG_GPDR_LSB,
577         [STMPE_IDX_GPRER_LSB]   = STMPE24XX_REG_GPRER_LSB,
578         [STMPE_IDX_GPFER_LSB]   = STMPE24XX_REG_GPFER_LSB,
579         [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
580         [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
581         [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
582         [STMPE_IDX_GPEDR_MSB]   = STMPE24XX_REG_GPEDR_MSB,
583 };
584
585 static struct stmpe_variant_block stmpe24xx_blocks[] = {
586         {
587                 .cell   = &stmpe_gpio_cell,
588                 .irq    = STMPE24XX_IRQ_GPIOC,
589                 .block  = STMPE_BLOCK_GPIO,
590         },
591         {
592                 .cell   = &stmpe_keypad_cell,
593                 .irq    = STMPE24XX_IRQ_KEYPAD,
594                 .block  = STMPE_BLOCK_KEYPAD,
595         },
596 };
597
598 static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
599                             bool enable)
600 {
601         unsigned int mask = 0;
602
603         if (blocks & STMPE_BLOCK_GPIO)
604                 mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
605
606         if (blocks & STMPE_BLOCK_KEYPAD)
607                 mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
608
609         return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
610                                 enable ? mask : 0);
611 }
612
613 static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
614 {
615         switch (block) {
616         case STMPE_BLOCK_ROTATOR:
617                 return 2;
618
619         case STMPE_BLOCK_KEYPAD:
620                 return 1;
621
622         case STMPE_BLOCK_GPIO:
623         default:
624                 return 0;
625         }
626 }
627
628 static struct stmpe_variant_info stmpe2401 = {
629         .name           = "stmpe2401",
630         .id_val         = 0x0101,
631         .id_mask        = 0xffff,
632         .num_gpios      = 24,
633         .af_bits        = 2,
634         .regs           = stmpe24xx_regs,
635         .blocks         = stmpe24xx_blocks,
636         .num_blocks     = ARRAY_SIZE(stmpe24xx_blocks),
637         .num_irqs       = STMPE24XX_NR_INTERNAL_IRQS,
638         .enable         = stmpe24xx_enable,
639         .get_altfunc    = stmpe24xx_get_altfunc,
640 };
641
642 static struct stmpe_variant_info stmpe2403 = {
643         .name           = "stmpe2403",
644         .id_val         = 0x0120,
645         .id_mask        = 0xffff,
646         .num_gpios      = 24,
647         .af_bits        = 2,
648         .regs           = stmpe24xx_regs,
649         .blocks         = stmpe24xx_blocks,
650         .num_blocks     = ARRAY_SIZE(stmpe24xx_blocks),
651         .num_irqs       = STMPE24XX_NR_INTERNAL_IRQS,
652         .enable         = stmpe24xx_enable,
653         .get_altfunc    = stmpe24xx_get_altfunc,
654         .enable_autosleep       = stmpe1601_autosleep, /* same as stmpe1601 */
655 };
656
657 static struct stmpe_variant_info *stmpe_variant_info[] = {
658         [STMPE811]      = &stmpe811,
659         [STMPE1601]     = &stmpe1601,
660         [STMPE2401]     = &stmpe2401,
661         [STMPE2403]     = &stmpe2403,
662 };
663
664 static irqreturn_t stmpe_irq(int irq, void *data)
665 {
666         struct stmpe *stmpe = data;
667         struct stmpe_variant_info *variant = stmpe->variant;
668         int num = DIV_ROUND_UP(variant->num_irqs, 8);
669         u8 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
670         u8 isr[num];
671         int ret;
672         int i;
673
674         ret = stmpe_block_read(stmpe, israddr, num, isr);
675         if (ret < 0)
676                 return IRQ_NONE;
677
678         for (i = 0; i < num; i++) {
679                 int bank = num - i - 1;
680                 u8 status = isr[i];
681                 u8 clear;
682
683                 status &= stmpe->ier[bank];
684                 if (!status)
685                         continue;
686
687                 clear = status;
688                 while (status) {
689                         int bit = __ffs(status);
690                         int line = bank * 8 + bit;
691
692                         handle_nested_irq(stmpe->irq_base + line);
693                         status &= ~(1 << bit);
694                 }
695
696                 stmpe_reg_write(stmpe, israddr + i, clear);
697         }
698
699         return IRQ_HANDLED;
700 }
701
702 static void stmpe_irq_lock(struct irq_data *data)
703 {
704         struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
705
706         mutex_lock(&stmpe->irq_lock);
707 }
708
709 static void stmpe_irq_sync_unlock(struct irq_data *data)
710 {
711         struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
712         struct stmpe_variant_info *variant = stmpe->variant;
713         int num = DIV_ROUND_UP(variant->num_irqs, 8);
714         int i;
715
716         for (i = 0; i < num; i++) {
717                 u8 new = stmpe->ier[i];
718                 u8 old = stmpe->oldier[i];
719
720                 if (new == old)
721                         continue;
722
723                 stmpe->oldier[i] = new;
724                 stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
725         }
726
727         mutex_unlock(&stmpe->irq_lock);
728 }
729
730 static void stmpe_irq_mask(struct irq_data *data)
731 {
732         struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
733         int offset = data->irq - stmpe->irq_base;
734         int regoffset = offset / 8;
735         int mask = 1 << (offset % 8);
736
737         stmpe->ier[regoffset] &= ~mask;
738 }
739
740 static void stmpe_irq_unmask(struct irq_data *data)
741 {
742         struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
743         int offset = data->irq - stmpe->irq_base;
744         int regoffset = offset / 8;
745         int mask = 1 << (offset % 8);
746
747         stmpe->ier[regoffset] |= mask;
748 }
749
750 static struct irq_chip stmpe_irq_chip = {
751         .name                   = "stmpe",
752         .irq_bus_lock           = stmpe_irq_lock,
753         .irq_bus_sync_unlock    = stmpe_irq_sync_unlock,
754         .irq_mask               = stmpe_irq_mask,
755         .irq_unmask             = stmpe_irq_unmask,
756 };
757
758 static int __devinit stmpe_irq_init(struct stmpe *stmpe)
759 {
760         int num_irqs = stmpe->variant->num_irqs;
761         int base = stmpe->irq_base;
762         int irq;
763
764         for (irq = base; irq < base + num_irqs; irq++) {
765                 irq_set_chip_data(irq, stmpe);
766                 irq_set_chip_and_handler(irq, &stmpe_irq_chip,
767                                          handle_edge_irq);
768                 irq_set_nested_thread(irq, 1);
769 #ifdef CONFIG_ARM
770                 set_irq_flags(irq, IRQF_VALID);
771 #else
772                 irq_set_noprobe(irq);
773 #endif
774         }
775
776         return 0;
777 }
778
779 static void stmpe_irq_remove(struct stmpe *stmpe)
780 {
781         int num_irqs = stmpe->variant->num_irqs;
782         int base = stmpe->irq_base;
783         int irq;
784
785         for (irq = base; irq < base + num_irqs; irq++) {
786 #ifdef CONFIG_ARM
787                 set_irq_flags(irq, 0);
788 #endif
789                 irq_set_chip_and_handler(irq, NULL, NULL);
790                 irq_set_chip_data(irq, NULL);
791         }
792 }
793
794 static int __devinit stmpe_chip_init(struct stmpe *stmpe)
795 {
796         unsigned int irq_trigger = stmpe->pdata->irq_trigger;
797         int autosleep_timeout = stmpe->pdata->autosleep_timeout;
798         struct stmpe_variant_info *variant = stmpe->variant;
799         u8 icr = STMPE_ICR_LSB_GIM;
800         unsigned int id;
801         u8 data[2];
802         int ret;
803
804         ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
805                                ARRAY_SIZE(data), data);
806         if (ret < 0)
807                 return ret;
808
809         id = (data[0] << 8) | data[1];
810         if ((id & variant->id_mask) != variant->id_val) {
811                 dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
812                 return -EINVAL;
813         }
814
815         dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
816
817         /* Disable all modules -- subdrivers should enable what they need. */
818         ret = stmpe_disable(stmpe, ~0);
819         if (ret)
820                 return ret;
821
822         if (irq_trigger == IRQF_TRIGGER_FALLING ||
823             irq_trigger == IRQF_TRIGGER_RISING)
824                 icr |= STMPE_ICR_LSB_EDGE;
825
826         if (irq_trigger == IRQF_TRIGGER_RISING ||
827             irq_trigger == IRQF_TRIGGER_HIGH)
828                 icr |= STMPE_ICR_LSB_HIGH;
829
830         if (stmpe->pdata->irq_invert_polarity)
831                 icr ^= STMPE_ICR_LSB_HIGH;
832
833         if (stmpe->pdata->autosleep) {
834                 ret = stmpe_autosleep(stmpe, autosleep_timeout);
835                 if (ret)
836                         return ret;
837         }
838
839         return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
840 }
841
842 static int __devinit stmpe_add_device(struct stmpe *stmpe,
843                                       struct mfd_cell *cell, int irq)
844 {
845         return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
846                                NULL, stmpe->irq_base + irq);
847 }
848
849 static int __devinit stmpe_devices_init(struct stmpe *stmpe)
850 {
851         struct stmpe_variant_info *variant = stmpe->variant;
852         unsigned int platform_blocks = stmpe->pdata->blocks;
853         int ret = -EINVAL;
854         int i;
855
856         for (i = 0; i < variant->num_blocks; i++) {
857                 struct stmpe_variant_block *block = &variant->blocks[i];
858
859                 if (!(platform_blocks & block->block))
860                         continue;
861
862                 platform_blocks &= ~block->block;
863                 ret = stmpe_add_device(stmpe, block->cell, block->irq);
864                 if (ret)
865                         return ret;
866         }
867
868         if (platform_blocks)
869                 dev_warn(stmpe->dev,
870                          "platform wants blocks (%#x) not present on variant",
871                          platform_blocks);
872
873         return ret;
874 }
875
876 #ifdef CONFIG_PM
877 static int stmpe_suspend(struct device *dev)
878 {
879         struct i2c_client *i2c = to_i2c_client(dev);
880
881         if (device_may_wakeup(&i2c->dev))
882                 enable_irq_wake(i2c->irq);
883
884         return 0;
885 }
886
887 static int stmpe_resume(struct device *dev)
888 {
889         struct i2c_client *i2c = to_i2c_client(dev);
890
891         if (device_may_wakeup(&i2c->dev))
892                 disable_irq_wake(i2c->irq);
893
894         return 0;
895 }
896 #endif
897
898 static int __devinit stmpe_probe(struct i2c_client *i2c,
899                                  const struct i2c_device_id *id)
900 {
901         struct stmpe_platform_data *pdata = i2c->dev.platform_data;
902         struct stmpe *stmpe;
903         int ret;
904
905         if (!pdata)
906                 return -EINVAL;
907
908         stmpe = kzalloc(sizeof(struct stmpe), GFP_KERNEL);
909         if (!stmpe)
910                 return -ENOMEM;
911
912         mutex_init(&stmpe->irq_lock);
913         mutex_init(&stmpe->lock);
914
915         stmpe->dev = &i2c->dev;
916         stmpe->i2c = i2c;
917
918         stmpe->pdata = pdata;
919         stmpe->irq_base = pdata->irq_base;
920
921         stmpe->partnum = id->driver_data;
922         stmpe->variant = stmpe_variant_info[stmpe->partnum];
923         stmpe->regs = stmpe->variant->regs;
924         stmpe->num_gpios = stmpe->variant->num_gpios;
925
926         i2c_set_clientdata(i2c, stmpe);
927
928         ret = stmpe_chip_init(stmpe);
929         if (ret)
930                 goto out_free;
931
932         ret = stmpe_irq_init(stmpe);
933         if (ret)
934                 goto out_free;
935
936         ret = request_threaded_irq(stmpe->i2c->irq, NULL, stmpe_irq,
937                                    pdata->irq_trigger | IRQF_ONESHOT,
938                                    "stmpe", stmpe);
939         if (ret) {
940                 dev_err(stmpe->dev, "failed to request IRQ: %d\n", ret);
941                 goto out_removeirq;
942         }
943
944         ret = stmpe_devices_init(stmpe);
945         if (ret) {
946                 dev_err(stmpe->dev, "failed to add children\n");
947                 goto out_removedevs;
948         }
949
950         return 0;
951
952 out_removedevs:
953         mfd_remove_devices(stmpe->dev);
954         free_irq(stmpe->i2c->irq, stmpe);
955 out_removeirq:
956         stmpe_irq_remove(stmpe);
957 out_free:
958         kfree(stmpe);
959         return ret;
960 }
961
962 static int __devexit stmpe_remove(struct i2c_client *client)
963 {
964         struct stmpe *stmpe = i2c_get_clientdata(client);
965
966         mfd_remove_devices(stmpe->dev);
967
968         free_irq(stmpe->i2c->irq, stmpe);
969         stmpe_irq_remove(stmpe);
970
971         kfree(stmpe);
972
973         return 0;
974 }
975
976 static const struct i2c_device_id stmpe_id[] = {
977         { "stmpe811", STMPE811 },
978         { "stmpe1601", STMPE1601 },
979         { "stmpe2401", STMPE2401 },
980         { "stmpe2403", STMPE2403 },
981         { }
982 };
983 MODULE_DEVICE_TABLE(i2c, stmpe_id);
984
985 #ifdef CONFIG_PM
986 static const struct dev_pm_ops stmpe_dev_pm_ops = {
987         .suspend        = stmpe_suspend,
988         .resume         = stmpe_resume,
989 };
990 #endif
991
992 static struct i2c_driver stmpe_driver = {
993         .driver.name    = "stmpe",
994         .driver.owner   = THIS_MODULE,
995 #ifdef CONFIG_PM
996         .driver.pm      = &stmpe_dev_pm_ops,
997 #endif
998         .probe          = stmpe_probe,
999         .remove         = __devexit_p(stmpe_remove),
1000         .id_table       = stmpe_id,
1001 };
1002
1003 static int __init stmpe_init(void)
1004 {
1005         return i2c_add_driver(&stmpe_driver);
1006 }
1007 subsys_initcall(stmpe_init);
1008
1009 static void __exit stmpe_exit(void)
1010 {
1011         i2c_del_driver(&stmpe_driver);
1012 }
1013 module_exit(stmpe_exit);
1014
1015 MODULE_LICENSE("GPL v2");
1016 MODULE_DESCRIPTION("STMPE MFD core driver");
1017 MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");