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