blob: f9f7de796029ecbf6a2f895f4a698b5a94b24411 [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>
Randy Dunlap20d5c7d2012-11-12 09:20:49 -080016#include <linux/of.h>
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053017#include <linux/pm.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053018#include <linux/slab.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053019#include <linux/mfd/core.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053020#include "stmpe.h"
21
22static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
23{
24 return stmpe->variant->enable(stmpe, blocks, true);
25}
26
27static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
28{
29 return stmpe->variant->enable(stmpe, blocks, false);
30}
31
32static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
33{
34 int ret;
35
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053036 ret = stmpe->ci->read_byte(stmpe, reg);
Rabin Vincent27e34992010-07-02 16:52:08 +053037 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053038 dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053039
40 dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
41
42 return ret;
43}
44
45static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
46{
47 int ret;
48
49 dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
50
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053051 ret = stmpe->ci->write_byte(stmpe, reg, val);
Rabin Vincent27e34992010-07-02 16:52:08 +053052 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053053 dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053054
55 return ret;
56}
57
58static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
59{
60 int ret;
61
62 ret = __stmpe_reg_read(stmpe, reg);
63 if (ret < 0)
64 return ret;
65
66 ret &= ~mask;
67 ret |= val;
68
69 return __stmpe_reg_write(stmpe, reg, ret);
70}
71
72static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
73 u8 *values)
74{
75 int ret;
76
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053077 ret = stmpe->ci->read_block(stmpe, reg, length, values);
Rabin Vincent27e34992010-07-02 16:52:08 +053078 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053079 dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053080
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
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053095 ret = stmpe->ci->write_block(stmpe, reg, length, values);
Rabin Vincent27e34992010-07-02 16:52:08 +053096 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053097 dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053098
99 return ret;
100}
101
102/**
103 * stmpe_enable - enable blocks on an STMPE device
104 * @stmpe: Device to work on
105 * @blocks: Mask of blocks (enum stmpe_block values) to enable
106 */
107int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
108{
109 int ret;
110
111 mutex_lock(&stmpe->lock);
112 ret = __stmpe_enable(stmpe, blocks);
113 mutex_unlock(&stmpe->lock);
114
115 return ret;
116}
117EXPORT_SYMBOL_GPL(stmpe_enable);
118
119/**
120 * stmpe_disable - disable blocks on an STMPE device
121 * @stmpe: Device to work on
122 * @blocks: Mask of blocks (enum stmpe_block values) to enable
123 */
124int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
125{
126 int ret;
127
128 mutex_lock(&stmpe->lock);
129 ret = __stmpe_disable(stmpe, blocks);
130 mutex_unlock(&stmpe->lock);
131
132 return ret;
133}
134EXPORT_SYMBOL_GPL(stmpe_disable);
135
136/**
137 * stmpe_reg_read() - read a single STMPE register
138 * @stmpe: Device to read from
139 * @reg: Register to read
140 */
141int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
142{
143 int ret;
144
145 mutex_lock(&stmpe->lock);
146 ret = __stmpe_reg_read(stmpe, reg);
147 mutex_unlock(&stmpe->lock);
148
149 return ret;
150}
151EXPORT_SYMBOL_GPL(stmpe_reg_read);
152
153/**
154 * stmpe_reg_write() - write a single STMPE register
155 * @stmpe: Device to write to
156 * @reg: Register to write
157 * @val: Value to write
158 */
159int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
160{
161 int ret;
162
163 mutex_lock(&stmpe->lock);
164 ret = __stmpe_reg_write(stmpe, reg, val);
165 mutex_unlock(&stmpe->lock);
166
167 return ret;
168}
169EXPORT_SYMBOL_GPL(stmpe_reg_write);
170
171/**
172 * stmpe_set_bits() - set the value of a bitfield in a STMPE register
173 * @stmpe: Device to write to
174 * @reg: Register to write
175 * @mask: Mask of bits to set
176 * @val: Value to set
177 */
178int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
179{
180 int ret;
181
182 mutex_lock(&stmpe->lock);
183 ret = __stmpe_set_bits(stmpe, reg, mask, val);
184 mutex_unlock(&stmpe->lock);
185
186 return ret;
187}
188EXPORT_SYMBOL_GPL(stmpe_set_bits);
189
190/**
191 * stmpe_block_read() - read multiple STMPE registers
192 * @stmpe: Device to read from
193 * @reg: First register
194 * @length: Number of registers
195 * @values: Buffer to write to
196 */
197int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
198{
199 int ret;
200
201 mutex_lock(&stmpe->lock);
202 ret = __stmpe_block_read(stmpe, reg, length, values);
203 mutex_unlock(&stmpe->lock);
204
205 return ret;
206}
207EXPORT_SYMBOL_GPL(stmpe_block_read);
208
209/**
210 * stmpe_block_write() - write multiple STMPE registers
211 * @stmpe: Device to write to
212 * @reg: First register
213 * @length: Number of registers
214 * @values: Values to write
215 */
216int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
217 const u8 *values)
218{
219 int ret;
220
221 mutex_lock(&stmpe->lock);
222 ret = __stmpe_block_write(stmpe, reg, length, values);
223 mutex_unlock(&stmpe->lock);
224
225 return ret;
226}
227EXPORT_SYMBOL_GPL(stmpe_block_write);
228
229/**
Om Prakash4dcaa6b2011-06-27 09:54:22 +0200230 * stmpe_set_altfunc()- set the alternate function for STMPE pins
Rabin Vincent27e34992010-07-02 16:52:08 +0530231 * @stmpe: Device to configure
232 * @pins: Bitmask of pins to affect
233 * @block: block to enable alternate functions for
234 *
235 * @pins is assumed to have a bit set for each of the bits whose alternate
236 * function is to be changed, numbered according to the GPIOXY numbers.
237 *
238 * If the GPIO module is not enabled, this function automatically enables it in
239 * order to perform the change.
240 */
241int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
242{
243 struct stmpe_variant_info *variant = stmpe->variant;
244 u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
245 int af_bits = variant->af_bits;
246 int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
Rabin Vincent27e34992010-07-02 16:52:08 +0530247 int mask = (1 << af_bits) - 1;
248 u8 regs[numregs];
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530249 int af, afperreg, ret;
Rabin Vincent27e34992010-07-02 16:52:08 +0530250
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530251 if (!variant->get_altfunc)
252 return 0;
253
254 afperreg = 8 / af_bits;
Rabin Vincent27e34992010-07-02 16:52:08 +0530255 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
Chris Blaire31f9b82012-01-26 22:17:03 +0100303static struct mfd_cell stmpe_gpio_cell_noirq = {
304 .name = "stmpe-gpio",
305 /* gpio cell resources consist of an irq only so no resources here */
306};
307
Rabin Vincent27e34992010-07-02 16:52:08 +0530308/*
309 * Keypad (1601, 2401, 2403)
310 */
311
312static struct resource stmpe_keypad_resources[] = {
313 {
314 .name = "KEYPAD",
Rabin Vincent27e34992010-07-02 16:52:08 +0530315 .flags = IORESOURCE_IRQ,
316 },
317 {
318 .name = "KEYPAD_OVER",
Rabin Vincent27e34992010-07-02 16:52:08 +0530319 .flags = IORESOURCE_IRQ,
320 },
321};
322
323static struct mfd_cell stmpe_keypad_cell = {
324 .name = "stmpe-keypad",
325 .resources = stmpe_keypad_resources,
326 .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
327};
328
329/*
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530330 * STMPE801
331 */
332static const u8 stmpe801_regs[] = {
333 [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
334 [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
335 [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
336 [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
337 [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
338 [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
339 [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
340 [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
341
342};
343
344static struct stmpe_variant_block stmpe801_blocks[] = {
345 {
346 .cell = &stmpe_gpio_cell,
347 .irq = 0,
348 .block = STMPE_BLOCK_GPIO,
349 },
350};
351
Chris Blaire31f9b82012-01-26 22:17:03 +0100352static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
353 {
354 .cell = &stmpe_gpio_cell_noirq,
355 .block = STMPE_BLOCK_GPIO,
356 },
357};
358
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530359static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
360 bool enable)
361{
362 if (blocks & STMPE_BLOCK_GPIO)
363 return 0;
364 else
365 return -EINVAL;
366}
367
368static struct stmpe_variant_info stmpe801 = {
369 .name = "stmpe801",
370 .id_val = STMPE801_ID,
371 .id_mask = 0xffff,
372 .num_gpios = 8,
373 .regs = stmpe801_regs,
374 .blocks = stmpe801_blocks,
375 .num_blocks = ARRAY_SIZE(stmpe801_blocks),
376 .num_irqs = STMPE801_NR_INTERNAL_IRQS,
377 .enable = stmpe801_enable,
378};
379
Chris Blaire31f9b82012-01-26 22:17:03 +0100380static struct stmpe_variant_info stmpe801_noirq = {
381 .name = "stmpe801",
382 .id_val = STMPE801_ID,
383 .id_mask = 0xffff,
384 .num_gpios = 8,
385 .regs = stmpe801_regs,
386 .blocks = stmpe801_blocks_noirq,
387 .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
388 .enable = stmpe801_enable,
389};
390
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530391/*
Viresh Kumar1cda2392011-11-17 11:02:22 +0530392 * Touchscreen (STMPE811 or STMPE610)
Rabin Vincent27e34992010-07-02 16:52:08 +0530393 */
394
395static struct resource stmpe_ts_resources[] = {
396 {
397 .name = "TOUCH_DET",
Rabin Vincent27e34992010-07-02 16:52:08 +0530398 .flags = IORESOURCE_IRQ,
399 },
400 {
401 .name = "FIFO_TH",
Rabin Vincent27e34992010-07-02 16:52:08 +0530402 .flags = IORESOURCE_IRQ,
403 },
404};
405
406static struct mfd_cell stmpe_ts_cell = {
407 .name = "stmpe-ts",
408 .resources = stmpe_ts_resources,
409 .num_resources = ARRAY_SIZE(stmpe_ts_resources),
410};
411
412/*
Viresh Kumar1cda2392011-11-17 11:02:22 +0530413 * STMPE811 or STMPE610
Rabin Vincent27e34992010-07-02 16:52:08 +0530414 */
415
416static const u8 stmpe811_regs[] = {
417 [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
418 [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
419 [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
420 [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
421 [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
422 [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
423 [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
424 [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
425 [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
426 [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
427 [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
428 [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
429 [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
430 [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
431};
432
433static struct stmpe_variant_block stmpe811_blocks[] = {
434 {
435 .cell = &stmpe_gpio_cell,
436 .irq = STMPE811_IRQ_GPIOC,
437 .block = STMPE_BLOCK_GPIO,
438 },
439 {
440 .cell = &stmpe_ts_cell,
441 .irq = STMPE811_IRQ_TOUCH_DET,
442 .block = STMPE_BLOCK_TOUCHSCREEN,
443 },
444};
445
446static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
447 bool enable)
448{
449 unsigned int mask = 0;
450
451 if (blocks & STMPE_BLOCK_GPIO)
452 mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
453
454 if (blocks & STMPE_BLOCK_ADC)
455 mask |= STMPE811_SYS_CTRL2_ADC_OFF;
456
457 if (blocks & STMPE_BLOCK_TOUCHSCREEN)
458 mask |= STMPE811_SYS_CTRL2_TSC_OFF;
459
460 return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
461 enable ? 0 : mask);
462}
463
464static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
465{
466 /* 0 for touchscreen, 1 for GPIO */
467 return block != STMPE_BLOCK_TOUCHSCREEN;
468}
469
470static struct stmpe_variant_info stmpe811 = {
471 .name = "stmpe811",
472 .id_val = 0x0811,
473 .id_mask = 0xffff,
474 .num_gpios = 8,
475 .af_bits = 1,
476 .regs = stmpe811_regs,
477 .blocks = stmpe811_blocks,
478 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
479 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
480 .enable = stmpe811_enable,
481 .get_altfunc = stmpe811_get_altfunc,
482};
483
Viresh Kumar1cda2392011-11-17 11:02:22 +0530484/* Similar to 811, except number of gpios */
485static struct stmpe_variant_info stmpe610 = {
486 .name = "stmpe610",
487 .id_val = 0x0811,
488 .id_mask = 0xffff,
489 .num_gpios = 6,
490 .af_bits = 1,
491 .regs = stmpe811_regs,
492 .blocks = stmpe811_blocks,
493 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
494 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
495 .enable = stmpe811_enable,
496 .get_altfunc = stmpe811_get_altfunc,
497};
498
Rabin Vincent27e34992010-07-02 16:52:08 +0530499/*
500 * STMPE1601
501 */
502
503static const u8 stmpe1601_regs[] = {
504 [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
505 [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
506 [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
507 [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
508 [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
509 [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
510 [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
511 [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
512 [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
513 [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
514 [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
515 [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
516 [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
517 [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
518};
519
520static struct stmpe_variant_block stmpe1601_blocks[] = {
521 {
522 .cell = &stmpe_gpio_cell,
Lee Jones5204e512012-11-05 16:10:32 +0100523 .irq = STMPE1601_IRQ_GPIOC,
Rabin Vincent27e34992010-07-02 16:52:08 +0530524 .block = STMPE_BLOCK_GPIO,
525 },
526 {
527 .cell = &stmpe_keypad_cell,
Lee Jones5204e512012-11-05 16:10:32 +0100528 .irq = STMPE1601_IRQ_KEYPAD,
Rabin Vincent27e34992010-07-02 16:52:08 +0530529 .block = STMPE_BLOCK_KEYPAD,
530 },
531};
532
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530533/* supported autosleep timeout delay (in msecs) */
534static const int stmpe_autosleep_delay[] = {
535 4, 16, 32, 64, 128, 256, 512, 1024,
536};
537
538static int stmpe_round_timeout(int timeout)
539{
540 int i;
541
542 for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
543 if (stmpe_autosleep_delay[i] >= timeout)
544 return i;
545 }
546
547 /*
548 * requests for delays longer than supported should not return the
549 * longest supported delay
550 */
551 return -EINVAL;
552}
553
554static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
555{
556 int ret;
557
558 if (!stmpe->variant->enable_autosleep)
559 return -ENOSYS;
560
561 mutex_lock(&stmpe->lock);
562 ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
563 mutex_unlock(&stmpe->lock);
564
565 return ret;
566}
567
568/*
569 * Both stmpe 1601/2403 support same layout for autosleep
570 */
571static int stmpe1601_autosleep(struct stmpe *stmpe,
572 int autosleep_timeout)
573{
574 int ret, timeout;
575
576 /* choose the best available timeout */
577 timeout = stmpe_round_timeout(autosleep_timeout);
578 if (timeout < 0) {
579 dev_err(stmpe->dev, "invalid timeout\n");
580 return timeout;
581 }
582
583 ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
584 STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
585 timeout);
586 if (ret < 0)
587 return ret;
588
589 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
590 STPME1601_AUTOSLEEP_ENABLE,
591 STPME1601_AUTOSLEEP_ENABLE);
592}
593
Rabin Vincent27e34992010-07-02 16:52:08 +0530594static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
595 bool enable)
596{
597 unsigned int mask = 0;
598
599 if (blocks & STMPE_BLOCK_GPIO)
600 mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
601
602 if (blocks & STMPE_BLOCK_KEYPAD)
603 mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
604
605 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
606 enable ? mask : 0);
607}
608
609static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
610{
611 switch (block) {
612 case STMPE_BLOCK_PWM:
613 return 2;
614
615 case STMPE_BLOCK_KEYPAD:
616 return 1;
617
618 case STMPE_BLOCK_GPIO:
619 default:
620 return 0;
621 }
622}
623
624static struct stmpe_variant_info stmpe1601 = {
625 .name = "stmpe1601",
626 .id_val = 0x0210,
627 .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
628 .num_gpios = 16,
629 .af_bits = 2,
630 .regs = stmpe1601_regs,
631 .blocks = stmpe1601_blocks,
632 .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
633 .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
634 .enable = stmpe1601_enable,
635 .get_altfunc = stmpe1601_get_altfunc,
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530636 .enable_autosleep = stmpe1601_autosleep,
Rabin Vincent27e34992010-07-02 16:52:08 +0530637};
638
639/*
640 * STMPE24XX
641 */
642
643static const u8 stmpe24xx_regs[] = {
644 [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
645 [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
646 [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
647 [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
648 [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
649 [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
650 [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
651 [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
652 [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
653 [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
654 [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
655 [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
656 [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
657 [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
658};
659
660static struct stmpe_variant_block stmpe24xx_blocks[] = {
661 {
662 .cell = &stmpe_gpio_cell,
663 .irq = STMPE24XX_IRQ_GPIOC,
664 .block = STMPE_BLOCK_GPIO,
665 },
666 {
667 .cell = &stmpe_keypad_cell,
668 .irq = STMPE24XX_IRQ_KEYPAD,
669 .block = STMPE_BLOCK_KEYPAD,
670 },
671};
672
673static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
674 bool enable)
675{
676 unsigned int mask = 0;
677
678 if (blocks & STMPE_BLOCK_GPIO)
679 mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
680
681 if (blocks & STMPE_BLOCK_KEYPAD)
682 mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
683
684 return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
685 enable ? mask : 0);
686}
687
688static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
689{
690 switch (block) {
691 case STMPE_BLOCK_ROTATOR:
692 return 2;
693
694 case STMPE_BLOCK_KEYPAD:
695 return 1;
696
697 case STMPE_BLOCK_GPIO:
698 default:
699 return 0;
700 }
701}
702
703static struct stmpe_variant_info stmpe2401 = {
704 .name = "stmpe2401",
705 .id_val = 0x0101,
706 .id_mask = 0xffff,
707 .num_gpios = 24,
708 .af_bits = 2,
709 .regs = stmpe24xx_regs,
710 .blocks = stmpe24xx_blocks,
711 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
712 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
713 .enable = stmpe24xx_enable,
714 .get_altfunc = stmpe24xx_get_altfunc,
715};
716
717static struct stmpe_variant_info stmpe2403 = {
718 .name = "stmpe2403",
719 .id_val = 0x0120,
720 .id_mask = 0xffff,
721 .num_gpios = 24,
722 .af_bits = 2,
723 .regs = stmpe24xx_regs,
724 .blocks = stmpe24xx_blocks,
725 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
726 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
727 .enable = stmpe24xx_enable,
728 .get_altfunc = stmpe24xx_get_altfunc,
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530729 .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
Rabin Vincent27e34992010-07-02 16:52:08 +0530730};
731
Chris Blaire31f9b82012-01-26 22:17:03 +0100732static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
Viresh Kumar1cda2392011-11-17 11:02:22 +0530733 [STMPE610] = &stmpe610,
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530734 [STMPE801] = &stmpe801,
Rabin Vincent27e34992010-07-02 16:52:08 +0530735 [STMPE811] = &stmpe811,
736 [STMPE1601] = &stmpe1601,
737 [STMPE2401] = &stmpe2401,
738 [STMPE2403] = &stmpe2403,
739};
740
Chris Blaire31f9b82012-01-26 22:17:03 +0100741/*
742 * These devices can be connected in a 'no-irq' configuration - the irq pin
743 * is not used and the device cannot interrupt the CPU. Here we only list
744 * devices which support this configuration - the driver will fail probing
745 * for any devices not listed here which are configured in this way.
746 */
747static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
748 [STMPE801] = &stmpe801_noirq,
749};
750
Rabin Vincent27e34992010-07-02 16:52:08 +0530751static irqreturn_t stmpe_irq(int irq, void *data)
752{
753 struct stmpe *stmpe = data;
754 struct stmpe_variant_info *variant = stmpe->variant;
755 int num = DIV_ROUND_UP(variant->num_irqs, 8);
756 u8 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
757 u8 isr[num];
758 int ret;
759 int i;
760
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530761 if (variant->id_val == STMPE801_ID) {
Lee Jones76f93992012-11-05 16:10:31 +0100762 int base = irq_create_mapping(stmpe->domain, 0);
763
764 handle_nested_irq(base);
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530765 return IRQ_HANDLED;
766 }
767
Rabin Vincent27e34992010-07-02 16:52:08 +0530768 ret = stmpe_block_read(stmpe, israddr, num, isr);
769 if (ret < 0)
770 return IRQ_NONE;
771
772 for (i = 0; i < num; i++) {
773 int bank = num - i - 1;
774 u8 status = isr[i];
775 u8 clear;
776
777 status &= stmpe->ier[bank];
778 if (!status)
779 continue;
780
781 clear = status;
782 while (status) {
783 int bit = __ffs(status);
784 int line = bank * 8 + bit;
Lee Jones76f93992012-11-05 16:10:31 +0100785 int nestedirq = irq_create_mapping(stmpe->domain, line);
Rabin Vincent27e34992010-07-02 16:52:08 +0530786
Lee Jones76f93992012-11-05 16:10:31 +0100787 handle_nested_irq(nestedirq);
Rabin Vincent27e34992010-07-02 16:52:08 +0530788 status &= ~(1 << bit);
789 }
790
791 stmpe_reg_write(stmpe, israddr + i, clear);
792 }
793
794 return IRQ_HANDLED;
795}
796
Mark Brown43b8c082010-12-12 12:01:08 +0000797static void stmpe_irq_lock(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530798{
Mark Brown43b8c082010-12-12 12:01:08 +0000799 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Rabin Vincent27e34992010-07-02 16:52:08 +0530800
801 mutex_lock(&stmpe->irq_lock);
802}
803
Mark Brown43b8c082010-12-12 12:01:08 +0000804static void stmpe_irq_sync_unlock(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530805{
Mark Brown43b8c082010-12-12 12:01:08 +0000806 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Rabin Vincent27e34992010-07-02 16:52:08 +0530807 struct stmpe_variant_info *variant = stmpe->variant;
808 int num = DIV_ROUND_UP(variant->num_irqs, 8);
809 int i;
810
811 for (i = 0; i < num; i++) {
812 u8 new = stmpe->ier[i];
813 u8 old = stmpe->oldier[i];
814
815 if (new == old)
816 continue;
817
818 stmpe->oldier[i] = new;
819 stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
820 }
821
822 mutex_unlock(&stmpe->irq_lock);
823}
824
Mark Brown43b8c082010-12-12 12:01:08 +0000825static void stmpe_irq_mask(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530826{
Mark Brown43b8c082010-12-12 12:01:08 +0000827 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Lee Jones76f93992012-11-05 16:10:31 +0100828 int offset = data->hwirq;
Rabin Vincent27e34992010-07-02 16:52:08 +0530829 int regoffset = offset / 8;
830 int mask = 1 << (offset % 8);
831
832 stmpe->ier[regoffset] &= ~mask;
833}
834
Mark Brown43b8c082010-12-12 12:01:08 +0000835static void stmpe_irq_unmask(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530836{
Mark Brown43b8c082010-12-12 12:01:08 +0000837 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Lee Jones76f93992012-11-05 16:10:31 +0100838 int offset = data->hwirq;
Rabin Vincent27e34992010-07-02 16:52:08 +0530839 int regoffset = offset / 8;
840 int mask = 1 << (offset % 8);
841
842 stmpe->ier[regoffset] |= mask;
843}
844
845static struct irq_chip stmpe_irq_chip = {
846 .name = "stmpe",
Mark Brown43b8c082010-12-12 12:01:08 +0000847 .irq_bus_lock = stmpe_irq_lock,
848 .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
849 .irq_mask = stmpe_irq_mask,
850 .irq_unmask = stmpe_irq_unmask,
Rabin Vincent27e34992010-07-02 16:52:08 +0530851};
852
Lee Jones76f93992012-11-05 16:10:31 +0100853static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
854 irq_hw_number_t hwirq)
Rabin Vincent27e34992010-07-02 16:52:08 +0530855{
Lee Jones76f93992012-11-05 16:10:31 +0100856 struct stmpe *stmpe = d->host_data;
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530857 struct irq_chip *chip = NULL;
Rabin Vincent27e34992010-07-02 16:52:08 +0530858
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530859 if (stmpe->variant->id_val != STMPE801_ID)
860 chip = &stmpe_irq_chip;
861
Lee Jones76f93992012-11-05 16:10:31 +0100862 irq_set_chip_data(virq, stmpe);
863 irq_set_chip_and_handler(virq, chip, handle_edge_irq);
864 irq_set_nested_thread(virq, 1);
Rabin Vincent27e34992010-07-02 16:52:08 +0530865#ifdef CONFIG_ARM
Lee Jones76f93992012-11-05 16:10:31 +0100866 set_irq_flags(virq, IRQF_VALID);
Rabin Vincent27e34992010-07-02 16:52:08 +0530867#else
Lee Jones76f93992012-11-05 16:10:31 +0100868 irq_set_noprobe(virq);
Rabin Vincent27e34992010-07-02 16:52:08 +0530869#endif
Rabin Vincent27e34992010-07-02 16:52:08 +0530870
871 return 0;
872}
873
Lee Jones76f93992012-11-05 16:10:31 +0100874static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
Rabin Vincent27e34992010-07-02 16:52:08 +0530875{
Rabin Vincent27e34992010-07-02 16:52:08 +0530876#ifdef CONFIG_ARM
Lee Jones76f93992012-11-05 16:10:31 +0100877 set_irq_flags(virq, 0);
Rabin Vincent27e34992010-07-02 16:52:08 +0530878#endif
Lee Jones76f93992012-11-05 16:10:31 +0100879 irq_set_chip_and_handler(virq, NULL, NULL);
880 irq_set_chip_data(virq, NULL);
881}
882
883static struct irq_domain_ops stmpe_irq_ops = {
884 .map = stmpe_irq_map,
885 .unmap = stmpe_irq_unmap,
886 .xlate = irq_domain_xlate_twocell,
887};
888
Lee Jones909582c2012-11-05 16:10:33 +0100889static int __devinit stmpe_irq_init(struct stmpe *stmpe,
890 struct device_node *np)
Lee Jones76f93992012-11-05 16:10:31 +0100891{
Lee Jonesb20a4372012-11-23 15:19:29 +0000892 int base = 0;
Lee Jones76f93992012-11-05 16:10:31 +0100893 int num_irqs = stmpe->variant->num_irqs;
894
Lee Jonesb20a4372012-11-23 15:19:29 +0000895 if (!np)
896 base = stmpe->irq_base;
Lee Jones76f93992012-11-05 16:10:31 +0100897
Lee Jonesb20a4372012-11-23 15:19:29 +0000898 stmpe->domain = irq_domain_add_simple(np, num_irqs, base,
899 &stmpe_irq_ops, stmpe);
Lee Jones76f93992012-11-05 16:10:31 +0100900 if (!stmpe->domain) {
901 dev_err(stmpe->dev, "Failed to create irqdomain\n");
902 return -ENOSYS;
903 }
904
905 return 0;
Rabin Vincent27e34992010-07-02 16:52:08 +0530906}
907
908static int __devinit stmpe_chip_init(struct stmpe *stmpe)
909{
910 unsigned int irq_trigger = stmpe->pdata->irq_trigger;
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530911 int autosleep_timeout = stmpe->pdata->autosleep_timeout;
Rabin Vincent27e34992010-07-02 16:52:08 +0530912 struct stmpe_variant_info *variant = stmpe->variant;
Chris Blaire31f9b82012-01-26 22:17:03 +0100913 u8 icr = 0;
Rabin Vincent27e34992010-07-02 16:52:08 +0530914 unsigned int id;
915 u8 data[2];
916 int ret;
917
918 ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
919 ARRAY_SIZE(data), data);
920 if (ret < 0)
921 return ret;
922
923 id = (data[0] << 8) | data[1];
924 if ((id & variant->id_mask) != variant->id_val) {
925 dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
926 return -EINVAL;
927 }
928
929 dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
930
931 /* Disable all modules -- subdrivers should enable what they need. */
932 ret = stmpe_disable(stmpe, ~0);
933 if (ret)
934 return ret;
935
Chris Blaire31f9b82012-01-26 22:17:03 +0100936 if (stmpe->irq >= 0) {
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530937 if (id == STMPE801_ID)
Chris Blaire31f9b82012-01-26 22:17:03 +0100938 icr = STMPE801_REG_SYS_CTRL_INT_EN;
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530939 else
Chris Blaire31f9b82012-01-26 22:17:03 +0100940 icr = STMPE_ICR_LSB_GIM;
Rabin Vincent27e34992010-07-02 16:52:08 +0530941
Chris Blaire31f9b82012-01-26 22:17:03 +0100942 /* STMPE801 doesn't support Edge interrupts */
943 if (id != STMPE801_ID) {
944 if (irq_trigger == IRQF_TRIGGER_FALLING ||
945 irq_trigger == IRQF_TRIGGER_RISING)
946 icr |= STMPE_ICR_LSB_EDGE;
947 }
948
949 if (irq_trigger == IRQF_TRIGGER_RISING ||
950 irq_trigger == IRQF_TRIGGER_HIGH) {
951 if (id == STMPE801_ID)
952 icr |= STMPE801_REG_SYS_CTRL_INT_HI;
953 else
954 icr |= STMPE_ICR_LSB_HIGH;
955 }
956
957 if (stmpe->pdata->irq_invert_polarity) {
958 if (id == STMPE801_ID)
959 icr ^= STMPE801_REG_SYS_CTRL_INT_HI;
960 else
961 icr ^= STMPE_ICR_LSB_HIGH;
962 }
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530963 }
Rabin Vincent27e34992010-07-02 16:52:08 +0530964
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530965 if (stmpe->pdata->autosleep) {
966 ret = stmpe_autosleep(stmpe, autosleep_timeout);
967 if (ret)
968 return ret;
969 }
970
Rabin Vincent27e34992010-07-02 16:52:08 +0530971 return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
972}
973
974static int __devinit stmpe_add_device(struct stmpe *stmpe,
Lee Jones7da0cbf2012-11-05 16:10:30 +0100975 struct mfd_cell *cell)
Rabin Vincent27e34992010-07-02 16:52:08 +0530976{
977 return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
Lee Jones76f93992012-11-05 16:10:31 +0100978 NULL, stmpe->irq_base, stmpe->domain);
Rabin Vincent27e34992010-07-02 16:52:08 +0530979}
980
981static int __devinit stmpe_devices_init(struct stmpe *stmpe)
982{
983 struct stmpe_variant_info *variant = stmpe->variant;
984 unsigned int platform_blocks = stmpe->pdata->blocks;
985 int ret = -EINVAL;
Lee Jones7da0cbf2012-11-05 16:10:30 +0100986 int i, j;
Rabin Vincent27e34992010-07-02 16:52:08 +0530987
988 for (i = 0; i < variant->num_blocks; i++) {
989 struct stmpe_variant_block *block = &variant->blocks[i];
990
991 if (!(platform_blocks & block->block))
992 continue;
993
Lee Jones7da0cbf2012-11-05 16:10:30 +0100994 for (j = 0; j < block->cell->num_resources; j++) {
995 struct resource *res =
996 (struct resource *) &block->cell->resources[j];
997
998 /* Dynamically fill in a variant's IRQ. */
999 if (res->flags & IORESOURCE_IRQ)
1000 res->start = res->end = block->irq + j;
1001 }
1002
Rabin Vincent27e34992010-07-02 16:52:08 +05301003 platform_blocks &= ~block->block;
Lee Jones7da0cbf2012-11-05 16:10:30 +01001004 ret = stmpe_add_device(stmpe, block->cell);
Rabin Vincent27e34992010-07-02 16:52:08 +05301005 if (ret)
1006 return ret;
1007 }
1008
1009 if (platform_blocks)
1010 dev_warn(stmpe->dev,
1011 "platform wants blocks (%#x) not present on variant",
1012 platform_blocks);
1013
1014 return ret;
1015}
1016
Lee Jones909582c2012-11-05 16:10:33 +01001017void __devinit stmpe_of_probe(struct stmpe_platform_data *pdata,
1018 struct device_node *np)
1019{
1020 struct device_node *child;
1021
1022 of_property_read_u32(np, "st,autosleep-timeout",
1023 &pdata->autosleep_timeout);
1024
1025 pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
1026
1027 for_each_child_of_node(np, child) {
1028 if (!strcmp(child->name, "stmpe_gpio")) {
1029 pdata->blocks |= STMPE_BLOCK_GPIO;
1030 }
1031 if (!strcmp(child->name, "stmpe_keypad")) {
1032 pdata->blocks |= STMPE_BLOCK_KEYPAD;
1033 }
1034 if (!strcmp(child->name, "stmpe_touchscreen")) {
1035 pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
1036 }
1037 if (!strcmp(child->name, "stmpe_adc")) {
1038 pdata->blocks |= STMPE_BLOCK_ADC;
1039 }
1040 }
1041}
1042
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301043/* Called from client specific probe routines */
Samuel Ortiz8ad1a972011-12-20 18:35:55 +01001044int __devinit stmpe_probe(struct stmpe_client_info *ci, int partnum)
Sundar Iyer208c4342010-09-15 10:30:54 +05301045{
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301046 struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
Lee Jones909582c2012-11-05 16:10:33 +01001047 struct device_node *np = ci->dev->of_node;
Rabin Vincent27e34992010-07-02 16:52:08 +05301048 struct stmpe *stmpe;
1049 int ret;
1050
Lee Jones909582c2012-11-05 16:10:33 +01001051 if (!pdata) {
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301052 if (!np)
Lee Jones909582c2012-11-05 16:10:33 +01001053 return -EINVAL;
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301054
1055 pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
1056 if (!pdata)
1057 return -ENOMEM;
1058
1059 stmpe_of_probe(pdata, np);
Lee Jones909582c2012-11-05 16:10:33 +01001060 }
Rabin Vincent27e34992010-07-02 16:52:08 +05301061
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301062 stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
Rabin Vincent27e34992010-07-02 16:52:08 +05301063 if (!stmpe)
1064 return -ENOMEM;
1065
1066 mutex_init(&stmpe->irq_lock);
1067 mutex_init(&stmpe->lock);
1068
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301069 stmpe->dev = ci->dev;
1070 stmpe->client = ci->client;
Rabin Vincent27e34992010-07-02 16:52:08 +05301071 stmpe->pdata = pdata;
1072 stmpe->irq_base = pdata->irq_base;
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301073 stmpe->ci = ci;
1074 stmpe->partnum = partnum;
1075 stmpe->variant = stmpe_variant_info[partnum];
Rabin Vincent27e34992010-07-02 16:52:08 +05301076 stmpe->regs = stmpe->variant->regs;
1077 stmpe->num_gpios = stmpe->variant->num_gpios;
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301078 dev_set_drvdata(stmpe->dev, stmpe);
Rabin Vincent27e34992010-07-02 16:52:08 +05301079
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301080 if (ci->init)
1081 ci->init(stmpe);
Rabin Vincent27e34992010-07-02 16:52:08 +05301082
Viresh Kumar73de16d2011-11-08 09:44:06 +05301083 if (pdata->irq_over_gpio) {
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301084 ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
1085 GPIOF_DIR_IN, "stmpe");
Viresh Kumar73de16d2011-11-08 09:44:06 +05301086 if (ret) {
1087 dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
1088 ret);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301089 return ret;
Viresh Kumar73de16d2011-11-08 09:44:06 +05301090 }
1091
1092 stmpe->irq = gpio_to_irq(pdata->irq_gpio);
1093 } else {
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301094 stmpe->irq = ci->irq;
Viresh Kumar73de16d2011-11-08 09:44:06 +05301095 }
1096
Chris Blaire31f9b82012-01-26 22:17:03 +01001097 if (stmpe->irq < 0) {
1098 /* use alternate variant info for no-irq mode, if supported */
1099 dev_info(stmpe->dev,
1100 "%s configured in no-irq mode by platform data\n",
1101 stmpe->variant->name);
1102 if (!stmpe_noirq_variant_info[stmpe->partnum]) {
1103 dev_err(stmpe->dev,
1104 "%s does not support no-irq mode!\n",
1105 stmpe->variant->name);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301106 return -ENODEV;
Chris Blaire31f9b82012-01-26 22:17:03 +01001107 }
1108 stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
1109 }
1110
Rabin Vincent27e34992010-07-02 16:52:08 +05301111 ret = stmpe_chip_init(stmpe);
1112 if (ret)
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301113 return ret;
Rabin Vincent27e34992010-07-02 16:52:08 +05301114
Chris Blaire31f9b82012-01-26 22:17:03 +01001115 if (stmpe->irq >= 0) {
Lee Jones909582c2012-11-05 16:10:33 +01001116 ret = stmpe_irq_init(stmpe, np);
Chris Blaire31f9b82012-01-26 22:17:03 +01001117 if (ret)
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301118 return ret;
Rabin Vincent27e34992010-07-02 16:52:08 +05301119
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301120 ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
1121 stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
Chris Blaire31f9b82012-01-26 22:17:03 +01001122 "stmpe", stmpe);
1123 if (ret) {
1124 dev_err(stmpe->dev, "failed to request IRQ: %d\n",
1125 ret);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301126 return ret;
Chris Blaire31f9b82012-01-26 22:17:03 +01001127 }
Rabin Vincent27e34992010-07-02 16:52:08 +05301128 }
1129
1130 ret = stmpe_devices_init(stmpe);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301131 if (!ret)
1132 return 0;
Rabin Vincent27e34992010-07-02 16:52:08 +05301133
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301134 dev_err(stmpe->dev, "failed to add children\n");
Rabin Vincent27e34992010-07-02 16:52:08 +05301135 mfd_remove_devices(stmpe->dev);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301136
Rabin Vincent27e34992010-07-02 16:52:08 +05301137 return ret;
1138}
1139
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301140int stmpe_remove(struct stmpe *stmpe)
Rabin Vincent27e34992010-07-02 16:52:08 +05301141{
Rabin Vincent27e34992010-07-02 16:52:08 +05301142 mfd_remove_devices(stmpe->dev);
1143
Rabin Vincent27e34992010-07-02 16:52:08 +05301144 return 0;
1145}
1146
Sundar Iyer208c4342010-09-15 10:30:54 +05301147#ifdef CONFIG_PM
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301148static int stmpe_suspend(struct device *dev)
1149{
1150 struct stmpe *stmpe = dev_get_drvdata(dev);
1151
Chris Blaire31f9b82012-01-26 22:17:03 +01001152 if (stmpe->irq >= 0 && device_may_wakeup(dev))
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301153 enable_irq_wake(stmpe->irq);
1154
1155 return 0;
1156}
1157
1158static int stmpe_resume(struct device *dev)
1159{
1160 struct stmpe *stmpe = dev_get_drvdata(dev);
1161
Chris Blaire31f9b82012-01-26 22:17:03 +01001162 if (stmpe->irq >= 0 && device_may_wakeup(dev))
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301163 disable_irq_wake(stmpe->irq);
1164
1165 return 0;
1166}
1167
1168const struct dev_pm_ops stmpe_dev_pm_ops = {
Sundar Iyer208c4342010-09-15 10:30:54 +05301169 .suspend = stmpe_suspend,
1170 .resume = stmpe_resume,
1171};
1172#endif