blob: c0df4b9d51c3393510f4276a68a85707c5c69f9d [file] [log] [blame]
Rabin Vincent27e34992010-07-02 16:52:08 +05301/*
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05302 * ST Microelectronics MFD: stmpe's driver
3 *
Rabin Vincent27e34992010-07-02 16:52:08 +05304 * 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
Viresh Kumar73de16d2011-11-08 09:44:06 +053010#include <linux/gpio.h>
Samuel Ortizdba61c82011-12-20 18:34:36 +010011#include <linux/export.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053012#include <linux/kernel.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053013#include <linux/interrupt.h>
14#include <linux/irq.h>
Lee Jones76f93992012-11-05 16:10:31 +010015#include <linux/irqdomain.h>
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053016#include <linux/pm.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053017#include <linux/slab.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053018#include <linux/mfd/core.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053019#include "stmpe.h"
20
21static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
22{
23 return stmpe->variant->enable(stmpe, blocks, true);
24}
25
26static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
27{
28 return stmpe->variant->enable(stmpe, blocks, false);
29}
30
31static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
32{
33 int ret;
34
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053035 ret = stmpe->ci->read_byte(stmpe, reg);
Rabin Vincent27e34992010-07-02 16:52:08 +053036 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053037 dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053038
39 dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
40
41 return ret;
42}
43
44static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
45{
46 int ret;
47
48 dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
49
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053050 ret = stmpe->ci->write_byte(stmpe, reg, val);
Rabin Vincent27e34992010-07-02 16:52:08 +053051 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053052 dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053053
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
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053076 ret = stmpe->ci->read_block(stmpe, reg, length, values);
Rabin Vincent27e34992010-07-02 16:52:08 +053077 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053078 dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053079
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
86static 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
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053094 ret = stmpe->ci->write_block(stmpe, reg, length, values);
Rabin Vincent27e34992010-07-02 16:52:08 +053095 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053096 dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053097
98 return ret;
99}
100
101/**
102 * stmpe_enable - enable blocks on an STMPE device
103 * @stmpe: Device to work on
104 * @blocks: Mask of blocks (enum stmpe_block values) to enable
105 */
106int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
107{
108 int ret;
109
110 mutex_lock(&stmpe->lock);
111 ret = __stmpe_enable(stmpe, blocks);
112 mutex_unlock(&stmpe->lock);
113
114 return ret;
115}
116EXPORT_SYMBOL_GPL(stmpe_enable);
117
118/**
119 * stmpe_disable - disable blocks on an STMPE device
120 * @stmpe: Device to work on
121 * @blocks: Mask of blocks (enum stmpe_block values) to enable
122 */
123int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
124{
125 int ret;
126
127 mutex_lock(&stmpe->lock);
128 ret = __stmpe_disable(stmpe, blocks);
129 mutex_unlock(&stmpe->lock);
130
131 return ret;
132}
133EXPORT_SYMBOL_GPL(stmpe_disable);
134
135/**
136 * stmpe_reg_read() - read a single STMPE register
137 * @stmpe: Device to read from
138 * @reg: Register to read
139 */
140int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
141{
142 int ret;
143
144 mutex_lock(&stmpe->lock);
145 ret = __stmpe_reg_read(stmpe, reg);
146 mutex_unlock(&stmpe->lock);
147
148 return ret;
149}
150EXPORT_SYMBOL_GPL(stmpe_reg_read);
151
152/**
153 * stmpe_reg_write() - write a single STMPE register
154 * @stmpe: Device to write to
155 * @reg: Register to write
156 * @val: Value to write
157 */
158int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
159{
160 int ret;
161
162 mutex_lock(&stmpe->lock);
163 ret = __stmpe_reg_write(stmpe, reg, val);
164 mutex_unlock(&stmpe->lock);
165
166 return ret;
167}
168EXPORT_SYMBOL_GPL(stmpe_reg_write);
169
170/**
171 * stmpe_set_bits() - set the value of a bitfield in a STMPE register
172 * @stmpe: Device to write to
173 * @reg: Register to write
174 * @mask: Mask of bits to set
175 * @val: Value to set
176 */
177int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
178{
179 int ret;
180
181 mutex_lock(&stmpe->lock);
182 ret = __stmpe_set_bits(stmpe, reg, mask, val);
183 mutex_unlock(&stmpe->lock);
184
185 return ret;
186}
187EXPORT_SYMBOL_GPL(stmpe_set_bits);
188
189/**
190 * stmpe_block_read() - read multiple STMPE registers
191 * @stmpe: Device to read from
192 * @reg: First register
193 * @length: Number of registers
194 * @values: Buffer to write to
195 */
196int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
197{
198 int ret;
199
200 mutex_lock(&stmpe->lock);
201 ret = __stmpe_block_read(stmpe, reg, length, values);
202 mutex_unlock(&stmpe->lock);
203
204 return ret;
205}
206EXPORT_SYMBOL_GPL(stmpe_block_read);
207
208/**
209 * stmpe_block_write() - write multiple STMPE registers
210 * @stmpe: Device to write to
211 * @reg: First register
212 * @length: Number of registers
213 * @values: Values to write
214 */
215int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
216 const u8 *values)
217{
218 int ret;
219
220 mutex_lock(&stmpe->lock);
221 ret = __stmpe_block_write(stmpe, reg, length, values);
222 mutex_unlock(&stmpe->lock);
223
224 return ret;
225}
226EXPORT_SYMBOL_GPL(stmpe_block_write);
227
228/**
Om Prakash4dcaa6b2011-06-27 09:54:22 +0200229 * stmpe_set_altfunc()- set the alternate function for STMPE pins
Rabin Vincent27e34992010-07-02 16:52:08 +0530230 * @stmpe: Device to configure
231 * @pins: Bitmask of pins to affect
232 * @block: block to enable alternate functions for
233 *
234 * @pins is assumed to have a bit set for each of the bits whose alternate
235 * function is to be changed, numbered according to the GPIOXY numbers.
236 *
237 * If the GPIO module is not enabled, this function automatically enables it in
238 * order to perform the change.
239 */
240int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
241{
242 struct stmpe_variant_info *variant = stmpe->variant;
243 u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
244 int af_bits = variant->af_bits;
245 int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
Rabin Vincent27e34992010-07-02 16:52:08 +0530246 int mask = (1 << af_bits) - 1;
247 u8 regs[numregs];
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530248 int af, afperreg, ret;
Rabin Vincent27e34992010-07-02 16:52:08 +0530249
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530250 if (!variant->get_altfunc)
251 return 0;
252
253 afperreg = 8 / af_bits;
Rabin Vincent27e34992010-07-02 16:52:08 +0530254 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
279out:
280 mutex_unlock(&stmpe->lock);
281 return ret;
282}
283EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
284
285/*
286 * GPIO (all variants)
287 */
288
289static struct resource stmpe_gpio_resources[] = {
290 /* Start and end filled dynamically */
291 {
292 .flags = IORESOURCE_IRQ,
293 },
294};
295
296static 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
Chris Blaire31f9b82012-01-26 22:17:03 +0100302static struct mfd_cell stmpe_gpio_cell_noirq = {
303 .name = "stmpe-gpio",
304 /* gpio cell resources consist of an irq only so no resources here */
305};
306
Rabin Vincent27e34992010-07-02 16:52:08 +0530307/*
308 * Keypad (1601, 2401, 2403)
309 */
310
311static struct resource stmpe_keypad_resources[] = {
312 {
313 .name = "KEYPAD",
Rabin Vincent27e34992010-07-02 16:52:08 +0530314 .flags = IORESOURCE_IRQ,
315 },
316 {
317 .name = "KEYPAD_OVER",
Rabin Vincent27e34992010-07-02 16:52:08 +0530318 .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/*
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530329 * STMPE801
330 */
331static const u8 stmpe801_regs[] = {
332 [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
333 [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
334 [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
335 [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
336 [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
337 [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
338 [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
339 [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
340
341};
342
343static struct stmpe_variant_block stmpe801_blocks[] = {
344 {
345 .cell = &stmpe_gpio_cell,
346 .irq = 0,
347 .block = STMPE_BLOCK_GPIO,
348 },
349};
350
Chris Blaire31f9b82012-01-26 22:17:03 +0100351static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
352 {
353 .cell = &stmpe_gpio_cell_noirq,
354 .block = STMPE_BLOCK_GPIO,
355 },
356};
357
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530358static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
359 bool enable)
360{
361 if (blocks & STMPE_BLOCK_GPIO)
362 return 0;
363 else
364 return -EINVAL;
365}
366
367static struct stmpe_variant_info stmpe801 = {
368 .name = "stmpe801",
369 .id_val = STMPE801_ID,
370 .id_mask = 0xffff,
371 .num_gpios = 8,
372 .regs = stmpe801_regs,
373 .blocks = stmpe801_blocks,
374 .num_blocks = ARRAY_SIZE(stmpe801_blocks),
375 .num_irqs = STMPE801_NR_INTERNAL_IRQS,
376 .enable = stmpe801_enable,
377};
378
Chris Blaire31f9b82012-01-26 22:17:03 +0100379static struct stmpe_variant_info stmpe801_noirq = {
380 .name = "stmpe801",
381 .id_val = STMPE801_ID,
382 .id_mask = 0xffff,
383 .num_gpios = 8,
384 .regs = stmpe801_regs,
385 .blocks = stmpe801_blocks_noirq,
386 .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
387 .enable = stmpe801_enable,
388};
389
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530390/*
Viresh Kumar1cda2392011-11-17 11:02:22 +0530391 * Touchscreen (STMPE811 or STMPE610)
Rabin Vincent27e34992010-07-02 16:52:08 +0530392 */
393
394static struct resource stmpe_ts_resources[] = {
395 {
396 .name = "TOUCH_DET",
Rabin Vincent27e34992010-07-02 16:52:08 +0530397 .flags = IORESOURCE_IRQ,
398 },
399 {
400 .name = "FIFO_TH",
Rabin Vincent27e34992010-07-02 16:52:08 +0530401 .flags = IORESOURCE_IRQ,
402 },
403};
404
405static struct mfd_cell stmpe_ts_cell = {
406 .name = "stmpe-ts",
407 .resources = stmpe_ts_resources,
408 .num_resources = ARRAY_SIZE(stmpe_ts_resources),
409};
410
411/*
Viresh Kumar1cda2392011-11-17 11:02:22 +0530412 * STMPE811 or STMPE610
Rabin Vincent27e34992010-07-02 16:52:08 +0530413 */
414
415static const u8 stmpe811_regs[] = {
416 [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
417 [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
418 [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
419 [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
420 [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
421 [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
422 [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
423 [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
424 [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
425 [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
426 [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
427 [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
428 [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
429 [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
430};
431
432static struct stmpe_variant_block stmpe811_blocks[] = {
433 {
434 .cell = &stmpe_gpio_cell,
435 .irq = STMPE811_IRQ_GPIOC,
436 .block = STMPE_BLOCK_GPIO,
437 },
438 {
439 .cell = &stmpe_ts_cell,
440 .irq = STMPE811_IRQ_TOUCH_DET,
441 .block = STMPE_BLOCK_TOUCHSCREEN,
442 },
443};
444
445static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
446 bool enable)
447{
448 unsigned int mask = 0;
449
450 if (blocks & STMPE_BLOCK_GPIO)
451 mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
452
453 if (blocks & STMPE_BLOCK_ADC)
454 mask |= STMPE811_SYS_CTRL2_ADC_OFF;
455
456 if (blocks & STMPE_BLOCK_TOUCHSCREEN)
457 mask |= STMPE811_SYS_CTRL2_TSC_OFF;
458
459 return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
460 enable ? 0 : mask);
461}
462
463static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
464{
465 /* 0 for touchscreen, 1 for GPIO */
466 return block != STMPE_BLOCK_TOUCHSCREEN;
467}
468
469static struct stmpe_variant_info stmpe811 = {
470 .name = "stmpe811",
471 .id_val = 0x0811,
472 .id_mask = 0xffff,
473 .num_gpios = 8,
474 .af_bits = 1,
475 .regs = stmpe811_regs,
476 .blocks = stmpe811_blocks,
477 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
478 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
479 .enable = stmpe811_enable,
480 .get_altfunc = stmpe811_get_altfunc,
481};
482
Viresh Kumar1cda2392011-11-17 11:02:22 +0530483/* Similar to 811, except number of gpios */
484static struct stmpe_variant_info stmpe610 = {
485 .name = "stmpe610",
486 .id_val = 0x0811,
487 .id_mask = 0xffff,
488 .num_gpios = 6,
489 .af_bits = 1,
490 .regs = stmpe811_regs,
491 .blocks = stmpe811_blocks,
492 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
493 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
494 .enable = stmpe811_enable,
495 .get_altfunc = stmpe811_get_altfunc,
496};
497
Rabin Vincent27e34992010-07-02 16:52:08 +0530498/*
499 * STMPE1601
500 */
501
502static const u8 stmpe1601_regs[] = {
503 [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
504 [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
505 [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
506 [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
507 [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
508 [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
509 [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
510 [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
511 [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
512 [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
513 [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
514 [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
515 [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
516 [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
517};
518
519static struct stmpe_variant_block stmpe1601_blocks[] = {
520 {
521 .cell = &stmpe_gpio_cell,
Lee Jones5204e512012-11-05 16:10:32 +0100522 .irq = STMPE1601_IRQ_GPIOC,
Rabin Vincent27e34992010-07-02 16:52:08 +0530523 .block = STMPE_BLOCK_GPIO,
524 },
525 {
526 .cell = &stmpe_keypad_cell,
Lee Jones5204e512012-11-05 16:10:32 +0100527 .irq = STMPE1601_IRQ_KEYPAD,
Rabin Vincent27e34992010-07-02 16:52:08 +0530528 .block = STMPE_BLOCK_KEYPAD,
529 },
530};
531
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530532/* supported autosleep timeout delay (in msecs) */
533static const int stmpe_autosleep_delay[] = {
534 4, 16, 32, 64, 128, 256, 512, 1024,
535};
536
537static int stmpe_round_timeout(int timeout)
538{
539 int i;
540
541 for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
542 if (stmpe_autosleep_delay[i] >= timeout)
543 return i;
544 }
545
546 /*
547 * requests for delays longer than supported should not return the
548 * longest supported delay
549 */
550 return -EINVAL;
551}
552
553static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
554{
555 int ret;
556
557 if (!stmpe->variant->enable_autosleep)
558 return -ENOSYS;
559
560 mutex_lock(&stmpe->lock);
561 ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
562 mutex_unlock(&stmpe->lock);
563
564 return ret;
565}
566
567/*
568 * Both stmpe 1601/2403 support same layout for autosleep
569 */
570static int stmpe1601_autosleep(struct stmpe *stmpe,
571 int autosleep_timeout)
572{
573 int ret, timeout;
574
575 /* choose the best available timeout */
576 timeout = stmpe_round_timeout(autosleep_timeout);
577 if (timeout < 0) {
578 dev_err(stmpe->dev, "invalid timeout\n");
579 return timeout;
580 }
581
582 ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
583 STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
584 timeout);
585 if (ret < 0)
586 return ret;
587
588 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
589 STPME1601_AUTOSLEEP_ENABLE,
590 STPME1601_AUTOSLEEP_ENABLE);
591}
592
Rabin Vincent27e34992010-07-02 16:52:08 +0530593static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
594 bool enable)
595{
596 unsigned int mask = 0;
597
598 if (blocks & STMPE_BLOCK_GPIO)
599 mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
600
601 if (blocks & STMPE_BLOCK_KEYPAD)
602 mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
603
604 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
605 enable ? mask : 0);
606}
607
608static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
609{
610 switch (block) {
611 case STMPE_BLOCK_PWM:
612 return 2;
613
614 case STMPE_BLOCK_KEYPAD:
615 return 1;
616
617 case STMPE_BLOCK_GPIO:
618 default:
619 return 0;
620 }
621}
622
623static struct stmpe_variant_info stmpe1601 = {
624 .name = "stmpe1601",
625 .id_val = 0x0210,
626 .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
627 .num_gpios = 16,
628 .af_bits = 2,
629 .regs = stmpe1601_regs,
630 .blocks = stmpe1601_blocks,
631 .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
632 .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
633 .enable = stmpe1601_enable,
634 .get_altfunc = stmpe1601_get_altfunc,
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530635 .enable_autosleep = stmpe1601_autosleep,
Rabin Vincent27e34992010-07-02 16:52:08 +0530636};
637
638/*
639 * STMPE24XX
640 */
641
642static const u8 stmpe24xx_regs[] = {
643 [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
644 [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
645 [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
646 [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
647 [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
648 [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
649 [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
650 [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
651 [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
652 [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
653 [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
654 [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
655 [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
656 [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
657};
658
659static struct stmpe_variant_block stmpe24xx_blocks[] = {
660 {
661 .cell = &stmpe_gpio_cell,
662 .irq = STMPE24XX_IRQ_GPIOC,
663 .block = STMPE_BLOCK_GPIO,
664 },
665 {
666 .cell = &stmpe_keypad_cell,
667 .irq = STMPE24XX_IRQ_KEYPAD,
668 .block = STMPE_BLOCK_KEYPAD,
669 },
670};
671
672static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
673 bool enable)
674{
675 unsigned int mask = 0;
676
677 if (blocks & STMPE_BLOCK_GPIO)
678 mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
679
680 if (blocks & STMPE_BLOCK_KEYPAD)
681 mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
682
683 return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
684 enable ? mask : 0);
685}
686
687static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
688{
689 switch (block) {
690 case STMPE_BLOCK_ROTATOR:
691 return 2;
692
693 case STMPE_BLOCK_KEYPAD:
694 return 1;
695
696 case STMPE_BLOCK_GPIO:
697 default:
698 return 0;
699 }
700}
701
702static struct stmpe_variant_info stmpe2401 = {
703 .name = "stmpe2401",
704 .id_val = 0x0101,
705 .id_mask = 0xffff,
706 .num_gpios = 24,
707 .af_bits = 2,
708 .regs = stmpe24xx_regs,
709 .blocks = stmpe24xx_blocks,
710 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
711 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
712 .enable = stmpe24xx_enable,
713 .get_altfunc = stmpe24xx_get_altfunc,
714};
715
716static struct stmpe_variant_info stmpe2403 = {
717 .name = "stmpe2403",
718 .id_val = 0x0120,
719 .id_mask = 0xffff,
720 .num_gpios = 24,
721 .af_bits = 2,
722 .regs = stmpe24xx_regs,
723 .blocks = stmpe24xx_blocks,
724 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
725 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
726 .enable = stmpe24xx_enable,
727 .get_altfunc = stmpe24xx_get_altfunc,
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530728 .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
Rabin Vincent27e34992010-07-02 16:52:08 +0530729};
730
Chris Blaire31f9b82012-01-26 22:17:03 +0100731static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
Viresh Kumar1cda2392011-11-17 11:02:22 +0530732 [STMPE610] = &stmpe610,
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530733 [STMPE801] = &stmpe801,
Rabin Vincent27e34992010-07-02 16:52:08 +0530734 [STMPE811] = &stmpe811,
735 [STMPE1601] = &stmpe1601,
736 [STMPE2401] = &stmpe2401,
737 [STMPE2403] = &stmpe2403,
738};
739
Chris Blaire31f9b82012-01-26 22:17:03 +0100740/*
741 * These devices can be connected in a 'no-irq' configuration - the irq pin
742 * is not used and the device cannot interrupt the CPU. Here we only list
743 * devices which support this configuration - the driver will fail probing
744 * for any devices not listed here which are configured in this way.
745 */
746static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
747 [STMPE801] = &stmpe801_noirq,
748};
749
Rabin Vincent27e34992010-07-02 16:52:08 +0530750static irqreturn_t stmpe_irq(int irq, void *data)
751{
752 struct stmpe *stmpe = data;
753 struct stmpe_variant_info *variant = stmpe->variant;
754 int num = DIV_ROUND_UP(variant->num_irqs, 8);
755 u8 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
756 u8 isr[num];
757 int ret;
758 int i;
759
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530760 if (variant->id_val == STMPE801_ID) {
Lee Jones76f93992012-11-05 16:10:31 +0100761 int base = irq_create_mapping(stmpe->domain, 0);
762
763 handle_nested_irq(base);
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530764 return IRQ_HANDLED;
765 }
766
Rabin Vincent27e34992010-07-02 16:52:08 +0530767 ret = stmpe_block_read(stmpe, israddr, num, isr);
768 if (ret < 0)
769 return IRQ_NONE;
770
771 for (i = 0; i < num; i++) {
772 int bank = num - i - 1;
773 u8 status = isr[i];
774 u8 clear;
775
776 status &= stmpe->ier[bank];
777 if (!status)
778 continue;
779
780 clear = status;
781 while (status) {
782 int bit = __ffs(status);
783 int line = bank * 8 + bit;
Lee Jones76f93992012-11-05 16:10:31 +0100784 int nestedirq = irq_create_mapping(stmpe->domain, line);
Rabin Vincent27e34992010-07-02 16:52:08 +0530785
Lee Jones76f93992012-11-05 16:10:31 +0100786 handle_nested_irq(nestedirq);
Rabin Vincent27e34992010-07-02 16:52:08 +0530787 status &= ~(1 << bit);
788 }
789
790 stmpe_reg_write(stmpe, israddr + i, clear);
791 }
792
793 return IRQ_HANDLED;
794}
795
Mark Brown43b8c082010-12-12 12:01:08 +0000796static void stmpe_irq_lock(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530797{
Mark Brown43b8c082010-12-12 12:01:08 +0000798 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Rabin Vincent27e34992010-07-02 16:52:08 +0530799
800 mutex_lock(&stmpe->irq_lock);
801}
802
Mark Brown43b8c082010-12-12 12:01:08 +0000803static void stmpe_irq_sync_unlock(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530804{
Mark Brown43b8c082010-12-12 12:01:08 +0000805 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Rabin Vincent27e34992010-07-02 16:52:08 +0530806 struct stmpe_variant_info *variant = stmpe->variant;
807 int num = DIV_ROUND_UP(variant->num_irqs, 8);
808 int i;
809
810 for (i = 0; i < num; i++) {
811 u8 new = stmpe->ier[i];
812 u8 old = stmpe->oldier[i];
813
814 if (new == old)
815 continue;
816
817 stmpe->oldier[i] = new;
818 stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
819 }
820
821 mutex_unlock(&stmpe->irq_lock);
822}
823
Mark Brown43b8c082010-12-12 12:01:08 +0000824static void stmpe_irq_mask(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530825{
Mark Brown43b8c082010-12-12 12:01:08 +0000826 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Lee Jones76f93992012-11-05 16:10:31 +0100827 int offset = data->hwirq;
Rabin Vincent27e34992010-07-02 16:52:08 +0530828 int regoffset = offset / 8;
829 int mask = 1 << (offset % 8);
830
831 stmpe->ier[regoffset] &= ~mask;
832}
833
Mark Brown43b8c082010-12-12 12:01:08 +0000834static void stmpe_irq_unmask(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530835{
Mark Brown43b8c082010-12-12 12:01:08 +0000836 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Lee Jones76f93992012-11-05 16:10:31 +0100837 int offset = data->hwirq;
Rabin Vincent27e34992010-07-02 16:52:08 +0530838 int regoffset = offset / 8;
839 int mask = 1 << (offset % 8);
840
841 stmpe->ier[regoffset] |= mask;
842}
843
844static struct irq_chip stmpe_irq_chip = {
845 .name = "stmpe",
Mark Brown43b8c082010-12-12 12:01:08 +0000846 .irq_bus_lock = stmpe_irq_lock,
847 .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
848 .irq_mask = stmpe_irq_mask,
849 .irq_unmask = stmpe_irq_unmask,
Rabin Vincent27e34992010-07-02 16:52:08 +0530850};
851
Lee Jones76f93992012-11-05 16:10:31 +0100852static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
853 irq_hw_number_t hwirq)
Rabin Vincent27e34992010-07-02 16:52:08 +0530854{
Lee Jones76f93992012-11-05 16:10:31 +0100855 struct stmpe *stmpe = d->host_data;
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530856 struct irq_chip *chip = NULL;
Rabin Vincent27e34992010-07-02 16:52:08 +0530857
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530858 if (stmpe->variant->id_val != STMPE801_ID)
859 chip = &stmpe_irq_chip;
860
Lee Jones76f93992012-11-05 16:10:31 +0100861 irq_set_chip_data(virq, stmpe);
862 irq_set_chip_and_handler(virq, chip, handle_edge_irq);
863 irq_set_nested_thread(virq, 1);
Rabin Vincent27e34992010-07-02 16:52:08 +0530864#ifdef CONFIG_ARM
Lee Jones76f93992012-11-05 16:10:31 +0100865 set_irq_flags(virq, IRQF_VALID);
Rabin Vincent27e34992010-07-02 16:52:08 +0530866#else
Lee Jones76f93992012-11-05 16:10:31 +0100867 irq_set_noprobe(virq);
Rabin Vincent27e34992010-07-02 16:52:08 +0530868#endif
Rabin Vincent27e34992010-07-02 16:52:08 +0530869
870 return 0;
871}
872
Lee Jones76f93992012-11-05 16:10:31 +0100873static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
Rabin Vincent27e34992010-07-02 16:52:08 +0530874{
Rabin Vincent27e34992010-07-02 16:52:08 +0530875#ifdef CONFIG_ARM
Lee Jones76f93992012-11-05 16:10:31 +0100876 set_irq_flags(virq, 0);
Rabin Vincent27e34992010-07-02 16:52:08 +0530877#endif
Lee Jones76f93992012-11-05 16:10:31 +0100878 irq_set_chip_and_handler(virq, NULL, NULL);
879 irq_set_chip_data(virq, NULL);
880}
881
882static struct irq_domain_ops stmpe_irq_ops = {
883 .map = stmpe_irq_map,
884 .unmap = stmpe_irq_unmap,
885 .xlate = irq_domain_xlate_twocell,
886};
887
Lee Jones909582c2012-11-05 16:10:33 +0100888static int __devinit stmpe_irq_init(struct stmpe *stmpe,
889 struct device_node *np)
Lee Jones76f93992012-11-05 16:10:31 +0100890{
891 int base = stmpe->irq_base;
892 int num_irqs = stmpe->variant->num_irqs;
893
894 if (base) {
895 stmpe->domain = irq_domain_add_legacy(
Lee Jones909582c2012-11-05 16:10:33 +0100896 np, num_irqs, base, 0, &stmpe_irq_ops, stmpe);
Rabin Vincent27e34992010-07-02 16:52:08 +0530897 }
Lee Jones76f93992012-11-05 16:10:31 +0100898 else {
899 stmpe->domain = irq_domain_add_linear(
Lee Jones909582c2012-11-05 16:10:33 +0100900 np, num_irqs, &stmpe_irq_ops, stmpe);
Lee Jones76f93992012-11-05 16:10:31 +0100901 }
902
903 if (!stmpe->domain) {
904 dev_err(stmpe->dev, "Failed to create irqdomain\n");
905 return -ENOSYS;
906 }
907
908 return 0;
Rabin Vincent27e34992010-07-02 16:52:08 +0530909}
910
911static int __devinit stmpe_chip_init(struct stmpe *stmpe)
912{
913 unsigned int irq_trigger = stmpe->pdata->irq_trigger;
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530914 int autosleep_timeout = stmpe->pdata->autosleep_timeout;
Rabin Vincent27e34992010-07-02 16:52:08 +0530915 struct stmpe_variant_info *variant = stmpe->variant;
Chris Blaire31f9b82012-01-26 22:17:03 +0100916 u8 icr = 0;
Rabin Vincent27e34992010-07-02 16:52:08 +0530917 unsigned int id;
918 u8 data[2];
919 int ret;
920
921 ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
922 ARRAY_SIZE(data), data);
923 if (ret < 0)
924 return ret;
925
926 id = (data[0] << 8) | data[1];
927 if ((id & variant->id_mask) != variant->id_val) {
928 dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
929 return -EINVAL;
930 }
931
932 dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
933
934 /* Disable all modules -- subdrivers should enable what they need. */
935 ret = stmpe_disable(stmpe, ~0);
936 if (ret)
937 return ret;
938
Chris Blaire31f9b82012-01-26 22:17:03 +0100939 if (stmpe->irq >= 0) {
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530940 if (id == STMPE801_ID)
Chris Blaire31f9b82012-01-26 22:17:03 +0100941 icr = STMPE801_REG_SYS_CTRL_INT_EN;
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530942 else
Chris Blaire31f9b82012-01-26 22:17:03 +0100943 icr = STMPE_ICR_LSB_GIM;
Rabin Vincent27e34992010-07-02 16:52:08 +0530944
Chris Blaire31f9b82012-01-26 22:17:03 +0100945 /* STMPE801 doesn't support Edge interrupts */
946 if (id != STMPE801_ID) {
947 if (irq_trigger == IRQF_TRIGGER_FALLING ||
948 irq_trigger == IRQF_TRIGGER_RISING)
949 icr |= STMPE_ICR_LSB_EDGE;
950 }
951
952 if (irq_trigger == IRQF_TRIGGER_RISING ||
953 irq_trigger == IRQF_TRIGGER_HIGH) {
954 if (id == STMPE801_ID)
955 icr |= STMPE801_REG_SYS_CTRL_INT_HI;
956 else
957 icr |= STMPE_ICR_LSB_HIGH;
958 }
959
960 if (stmpe->pdata->irq_invert_polarity) {
961 if (id == STMPE801_ID)
962 icr ^= STMPE801_REG_SYS_CTRL_INT_HI;
963 else
964 icr ^= STMPE_ICR_LSB_HIGH;
965 }
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530966 }
Rabin Vincent27e34992010-07-02 16:52:08 +0530967
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530968 if (stmpe->pdata->autosleep) {
969 ret = stmpe_autosleep(stmpe, autosleep_timeout);
970 if (ret)
971 return ret;
972 }
973
Rabin Vincent27e34992010-07-02 16:52:08 +0530974 return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
975}
976
977static int __devinit stmpe_add_device(struct stmpe *stmpe,
Lee Jones7da0cbf2012-11-05 16:10:30 +0100978 struct mfd_cell *cell)
Rabin Vincent27e34992010-07-02 16:52:08 +0530979{
980 return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
Lee Jones76f93992012-11-05 16:10:31 +0100981 NULL, stmpe->irq_base, stmpe->domain);
Rabin Vincent27e34992010-07-02 16:52:08 +0530982}
983
984static int __devinit stmpe_devices_init(struct stmpe *stmpe)
985{
986 struct stmpe_variant_info *variant = stmpe->variant;
987 unsigned int platform_blocks = stmpe->pdata->blocks;
988 int ret = -EINVAL;
Lee Jones7da0cbf2012-11-05 16:10:30 +0100989 int i, j;
Rabin Vincent27e34992010-07-02 16:52:08 +0530990
991 for (i = 0; i < variant->num_blocks; i++) {
992 struct stmpe_variant_block *block = &variant->blocks[i];
993
994 if (!(platform_blocks & block->block))
995 continue;
996
Lee Jones7da0cbf2012-11-05 16:10:30 +0100997 for (j = 0; j < block->cell->num_resources; j++) {
998 struct resource *res =
999 (struct resource *) &block->cell->resources[j];
1000
1001 /* Dynamically fill in a variant's IRQ. */
1002 if (res->flags & IORESOURCE_IRQ)
1003 res->start = res->end = block->irq + j;
1004 }
1005
Rabin Vincent27e34992010-07-02 16:52:08 +05301006 platform_blocks &= ~block->block;
Lee Jones7da0cbf2012-11-05 16:10:30 +01001007 ret = stmpe_add_device(stmpe, block->cell);
Rabin Vincent27e34992010-07-02 16:52:08 +05301008 if (ret)
1009 return ret;
1010 }
1011
1012 if (platform_blocks)
1013 dev_warn(stmpe->dev,
1014 "platform wants blocks (%#x) not present on variant",
1015 platform_blocks);
1016
1017 return ret;
1018}
1019
Lee Jones909582c2012-11-05 16:10:33 +01001020void __devinit stmpe_of_probe(struct stmpe_platform_data *pdata,
1021 struct device_node *np)
1022{
1023 struct device_node *child;
1024
1025 of_property_read_u32(np, "st,autosleep-timeout",
1026 &pdata->autosleep_timeout);
1027
1028 pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
1029
1030 for_each_child_of_node(np, child) {
1031 if (!strcmp(child->name, "stmpe_gpio")) {
1032 pdata->blocks |= STMPE_BLOCK_GPIO;
1033 }
1034 if (!strcmp(child->name, "stmpe_keypad")) {
1035 pdata->blocks |= STMPE_BLOCK_KEYPAD;
1036 }
1037 if (!strcmp(child->name, "stmpe_touchscreen")) {
1038 pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
1039 }
1040 if (!strcmp(child->name, "stmpe_adc")) {
1041 pdata->blocks |= STMPE_BLOCK_ADC;
1042 }
1043 }
1044}
1045
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301046/* Called from client specific probe routines */
Samuel Ortiz8ad1a972011-12-20 18:35:55 +01001047int __devinit stmpe_probe(struct stmpe_client_info *ci, int partnum)
Sundar Iyer208c4342010-09-15 10:30:54 +05301048{
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301049 struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
Lee Jones909582c2012-11-05 16:10:33 +01001050 struct device_node *np = ci->dev->of_node;
Rabin Vincent27e34992010-07-02 16:52:08 +05301051 struct stmpe *stmpe;
1052 int ret;
1053
Lee Jones909582c2012-11-05 16:10:33 +01001054 if (!pdata) {
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301055 if (!np)
Lee Jones909582c2012-11-05 16:10:33 +01001056 return -EINVAL;
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301057
1058 pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
1059 if (!pdata)
1060 return -ENOMEM;
1061
1062 stmpe_of_probe(pdata, np);
Lee Jones909582c2012-11-05 16:10:33 +01001063 }
Rabin Vincent27e34992010-07-02 16:52:08 +05301064
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301065 stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
Rabin Vincent27e34992010-07-02 16:52:08 +05301066 if (!stmpe)
1067 return -ENOMEM;
1068
1069 mutex_init(&stmpe->irq_lock);
1070 mutex_init(&stmpe->lock);
1071
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301072 stmpe->dev = ci->dev;
1073 stmpe->client = ci->client;
Rabin Vincent27e34992010-07-02 16:52:08 +05301074 stmpe->pdata = pdata;
1075 stmpe->irq_base = pdata->irq_base;
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301076 stmpe->ci = ci;
1077 stmpe->partnum = partnum;
1078 stmpe->variant = stmpe_variant_info[partnum];
Rabin Vincent27e34992010-07-02 16:52:08 +05301079 stmpe->regs = stmpe->variant->regs;
1080 stmpe->num_gpios = stmpe->variant->num_gpios;
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301081 dev_set_drvdata(stmpe->dev, stmpe);
Rabin Vincent27e34992010-07-02 16:52:08 +05301082
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301083 if (ci->init)
1084 ci->init(stmpe);
Rabin Vincent27e34992010-07-02 16:52:08 +05301085
Viresh Kumar73de16d2011-11-08 09:44:06 +05301086 if (pdata->irq_over_gpio) {
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301087 ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
1088 GPIOF_DIR_IN, "stmpe");
Viresh Kumar73de16d2011-11-08 09:44:06 +05301089 if (ret) {
1090 dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
1091 ret);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301092 return ret;
Viresh Kumar73de16d2011-11-08 09:44:06 +05301093 }
1094
1095 stmpe->irq = gpio_to_irq(pdata->irq_gpio);
1096 } else {
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301097 stmpe->irq = ci->irq;
Viresh Kumar73de16d2011-11-08 09:44:06 +05301098 }
1099
Chris Blaire31f9b82012-01-26 22:17:03 +01001100 if (stmpe->irq < 0) {
1101 /* use alternate variant info for no-irq mode, if supported */
1102 dev_info(stmpe->dev,
1103 "%s configured in no-irq mode by platform data\n",
1104 stmpe->variant->name);
1105 if (!stmpe_noirq_variant_info[stmpe->partnum]) {
1106 dev_err(stmpe->dev,
1107 "%s does not support no-irq mode!\n",
1108 stmpe->variant->name);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301109 return -ENODEV;
Chris Blaire31f9b82012-01-26 22:17:03 +01001110 }
1111 stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
1112 }
1113
Rabin Vincent27e34992010-07-02 16:52:08 +05301114 ret = stmpe_chip_init(stmpe);
1115 if (ret)
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301116 return ret;
Rabin Vincent27e34992010-07-02 16:52:08 +05301117
Chris Blaire31f9b82012-01-26 22:17:03 +01001118 if (stmpe->irq >= 0) {
Lee Jones909582c2012-11-05 16:10:33 +01001119 ret = stmpe_irq_init(stmpe, np);
Chris Blaire31f9b82012-01-26 22:17:03 +01001120 if (ret)
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301121 return ret;
Rabin Vincent27e34992010-07-02 16:52:08 +05301122
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301123 ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
1124 stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
Chris Blaire31f9b82012-01-26 22:17:03 +01001125 "stmpe", stmpe);
1126 if (ret) {
1127 dev_err(stmpe->dev, "failed to request IRQ: %d\n",
1128 ret);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301129 return ret;
Chris Blaire31f9b82012-01-26 22:17:03 +01001130 }
Rabin Vincent27e34992010-07-02 16:52:08 +05301131 }
1132
1133 ret = stmpe_devices_init(stmpe);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301134 if (!ret)
1135 return 0;
Rabin Vincent27e34992010-07-02 16:52:08 +05301136
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301137 dev_err(stmpe->dev, "failed to add children\n");
Rabin Vincent27e34992010-07-02 16:52:08 +05301138 mfd_remove_devices(stmpe->dev);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301139
Rabin Vincent27e34992010-07-02 16:52:08 +05301140 return ret;
1141}
1142
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301143int stmpe_remove(struct stmpe *stmpe)
Rabin Vincent27e34992010-07-02 16:52:08 +05301144{
Rabin Vincent27e34992010-07-02 16:52:08 +05301145 mfd_remove_devices(stmpe->dev);
1146
Rabin Vincent27e34992010-07-02 16:52:08 +05301147 return 0;
1148}
1149
Sundar Iyer208c4342010-09-15 10:30:54 +05301150#ifdef CONFIG_PM
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301151static int stmpe_suspend(struct device *dev)
1152{
1153 struct stmpe *stmpe = dev_get_drvdata(dev);
1154
Chris Blaire31f9b82012-01-26 22:17:03 +01001155 if (stmpe->irq >= 0 && device_may_wakeup(dev))
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301156 enable_irq_wake(stmpe->irq);
1157
1158 return 0;
1159}
1160
1161static int stmpe_resume(struct device *dev)
1162{
1163 struct stmpe *stmpe = dev_get_drvdata(dev);
1164
Chris Blaire31f9b82012-01-26 22:17:03 +01001165 if (stmpe->irq >= 0 && device_may_wakeup(dev))
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301166 disable_irq_wake(stmpe->irq);
1167
1168 return 0;
1169}
1170
1171const struct dev_pm_ops stmpe_dev_pm_ops = {
Sundar Iyer208c4342010-09-15 10:30:54 +05301172 .suspend = stmpe_suspend,
1173 .resume = stmpe_resume,
1174};
1175#endif