blob: 4a91f6771fb82da541868250a6f85ccd76d24abd [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
Vipul Kumar Samarac713cc2012-12-07 20:29:37 +053010#include <linux/err.h>
Viresh Kumar73de16d2011-11-08 09:44:06 +053011#include <linux/gpio.h>
Samuel Ortizdba61c82011-12-20 18:34:36 +010012#include <linux/export.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053013#include <linux/kernel.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053014#include <linux/interrupt.h>
15#include <linux/irq.h>
Lee Jones76f93992012-11-05 16:10:31 +010016#include <linux/irqdomain.h>
Randy Dunlap20d5c7d2012-11-12 09:20:49 -080017#include <linux/of.h>
Vipul Kumar Samarac713cc2012-12-07 20:29:37 +053018#include <linux/of_gpio.h>
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053019#include <linux/pm.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053020#include <linux/slab.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053021#include <linux/mfd/core.h>
Jean-Nicolas Graux230f13a2013-04-09 10:35:19 +020022#include <linux/delay.h>
Rabin Vincent27e34992010-07-02 16:52:08 +053023#include "stmpe.h"
24
25static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
26{
27 return stmpe->variant->enable(stmpe, blocks, true);
28}
29
30static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
31{
32 return stmpe->variant->enable(stmpe, blocks, false);
33}
34
35static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
36{
37 int ret;
38
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053039 ret = stmpe->ci->read_byte(stmpe, reg);
Rabin Vincent27e34992010-07-02 16:52:08 +053040 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053041 dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053042
43 dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
44
45 return ret;
46}
47
48static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
49{
50 int ret;
51
52 dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
53
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053054 ret = stmpe->ci->write_byte(stmpe, reg, val);
Rabin Vincent27e34992010-07-02 16:52:08 +053055 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053056 dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053057
58 return ret;
59}
60
61static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
62{
63 int ret;
64
65 ret = __stmpe_reg_read(stmpe, reg);
66 if (ret < 0)
67 return ret;
68
69 ret &= ~mask;
70 ret |= val;
71
72 return __stmpe_reg_write(stmpe, reg, ret);
73}
74
75static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
76 u8 *values)
77{
78 int ret;
79
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053080 ret = stmpe->ci->read_block(stmpe, reg, length, values);
Rabin Vincent27e34992010-07-02 16:52:08 +053081 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053082 dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +053083
84 dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
85 stmpe_dump_bytes("stmpe rd: ", values, length);
86
87 return ret;
88}
89
90static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
91 const u8 *values)
92{
93 int ret;
94
95 dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
96 stmpe_dump_bytes("stmpe wr: ", values, length);
97
Viresh Kumar1a6e4b72011-11-17 11:02:20 +053098 ret = stmpe->ci->write_block(stmpe, reg, length, values);
Rabin Vincent27e34992010-07-02 16:52:08 +053099 if (ret < 0)
Viresh Kumar1a6e4b72011-11-17 11:02:20 +0530100 dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
Rabin Vincent27e34992010-07-02 16:52:08 +0530101
102 return ret;
103}
104
105/**
106 * stmpe_enable - enable blocks on an STMPE device
107 * @stmpe: Device to work on
108 * @blocks: Mask of blocks (enum stmpe_block values) to enable
109 */
110int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
111{
112 int ret;
113
114 mutex_lock(&stmpe->lock);
115 ret = __stmpe_enable(stmpe, blocks);
116 mutex_unlock(&stmpe->lock);
117
118 return ret;
119}
120EXPORT_SYMBOL_GPL(stmpe_enable);
121
122/**
123 * stmpe_disable - disable blocks on an STMPE device
124 * @stmpe: Device to work on
125 * @blocks: Mask of blocks (enum stmpe_block values) to enable
126 */
127int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
128{
129 int ret;
130
131 mutex_lock(&stmpe->lock);
132 ret = __stmpe_disable(stmpe, blocks);
133 mutex_unlock(&stmpe->lock);
134
135 return ret;
136}
137EXPORT_SYMBOL_GPL(stmpe_disable);
138
139/**
140 * stmpe_reg_read() - read a single STMPE register
141 * @stmpe: Device to read from
142 * @reg: Register to read
143 */
144int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
145{
146 int ret;
147
148 mutex_lock(&stmpe->lock);
149 ret = __stmpe_reg_read(stmpe, reg);
150 mutex_unlock(&stmpe->lock);
151
152 return ret;
153}
154EXPORT_SYMBOL_GPL(stmpe_reg_read);
155
156/**
157 * stmpe_reg_write() - write a single STMPE register
158 * @stmpe: Device to write to
159 * @reg: Register to write
160 * @val: Value to write
161 */
162int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
163{
164 int ret;
165
166 mutex_lock(&stmpe->lock);
167 ret = __stmpe_reg_write(stmpe, reg, val);
168 mutex_unlock(&stmpe->lock);
169
170 return ret;
171}
172EXPORT_SYMBOL_GPL(stmpe_reg_write);
173
174/**
175 * stmpe_set_bits() - set the value of a bitfield in a STMPE register
176 * @stmpe: Device to write to
177 * @reg: Register to write
178 * @mask: Mask of bits to set
179 * @val: Value to set
180 */
181int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
182{
183 int ret;
184
185 mutex_lock(&stmpe->lock);
186 ret = __stmpe_set_bits(stmpe, reg, mask, val);
187 mutex_unlock(&stmpe->lock);
188
189 return ret;
190}
191EXPORT_SYMBOL_GPL(stmpe_set_bits);
192
193/**
194 * stmpe_block_read() - read multiple STMPE registers
195 * @stmpe: Device to read from
196 * @reg: First register
197 * @length: Number of registers
198 * @values: Buffer to write to
199 */
200int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
201{
202 int ret;
203
204 mutex_lock(&stmpe->lock);
205 ret = __stmpe_block_read(stmpe, reg, length, values);
206 mutex_unlock(&stmpe->lock);
207
208 return ret;
209}
210EXPORT_SYMBOL_GPL(stmpe_block_read);
211
212/**
213 * stmpe_block_write() - write multiple STMPE registers
214 * @stmpe: Device to write to
215 * @reg: First register
216 * @length: Number of registers
217 * @values: Values to write
218 */
219int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
220 const u8 *values)
221{
222 int ret;
223
224 mutex_lock(&stmpe->lock);
225 ret = __stmpe_block_write(stmpe, reg, length, values);
226 mutex_unlock(&stmpe->lock);
227
228 return ret;
229}
230EXPORT_SYMBOL_GPL(stmpe_block_write);
231
232/**
Om Prakash4dcaa6b2011-06-27 09:54:22 +0200233 * stmpe_set_altfunc()- set the alternate function for STMPE pins
Rabin Vincent27e34992010-07-02 16:52:08 +0530234 * @stmpe: Device to configure
235 * @pins: Bitmask of pins to affect
236 * @block: block to enable alternate functions for
237 *
238 * @pins is assumed to have a bit set for each of the bits whose alternate
239 * function is to be changed, numbered according to the GPIOXY numbers.
240 *
241 * If the GPIO module is not enabled, this function automatically enables it in
242 * order to perform the change.
243 */
244int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
245{
246 struct stmpe_variant_info *variant = stmpe->variant;
247 u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
248 int af_bits = variant->af_bits;
249 int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
Rabin Vincent27e34992010-07-02 16:52:08 +0530250 int mask = (1 << af_bits) - 1;
251 u8 regs[numregs];
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530252 int af, afperreg, ret;
Rabin Vincent27e34992010-07-02 16:52:08 +0530253
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530254 if (!variant->get_altfunc)
255 return 0;
256
257 afperreg = 8 / af_bits;
Rabin Vincent27e34992010-07-02 16:52:08 +0530258 mutex_lock(&stmpe->lock);
259
260 ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
261 if (ret < 0)
262 goto out;
263
264 ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
265 if (ret < 0)
266 goto out;
267
268 af = variant->get_altfunc(stmpe, block);
269
270 while (pins) {
271 int pin = __ffs(pins);
272 int regoffset = numregs - (pin / afperreg) - 1;
273 int pos = (pin % afperreg) * (8 / afperreg);
274
275 regs[regoffset] &= ~(mask << pos);
276 regs[regoffset] |= af << pos;
277
278 pins &= ~(1 << pin);
279 }
280
281 ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
282
283out:
284 mutex_unlock(&stmpe->lock);
285 return ret;
286}
287EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
288
289/*
290 * GPIO (all variants)
291 */
292
293static struct resource stmpe_gpio_resources[] = {
294 /* Start and end filled dynamically */
295 {
296 .flags = IORESOURCE_IRQ,
297 },
298};
299
Geert Uytterhoeven6bbb3c42013-11-18 14:33:01 +0100300static const struct mfd_cell stmpe_gpio_cell = {
Rabin Vincent27e34992010-07-02 16:52:08 +0530301 .name = "stmpe-gpio",
Vipul Kumar Samar86605cf2012-11-26 17:06:51 +0530302 .of_compatible = "st,stmpe-gpio",
Rabin Vincent27e34992010-07-02 16:52:08 +0530303 .resources = stmpe_gpio_resources,
304 .num_resources = ARRAY_SIZE(stmpe_gpio_resources),
305};
306
Geert Uytterhoeven6bbb3c42013-11-18 14:33:01 +0100307static const struct mfd_cell stmpe_gpio_cell_noirq = {
Chris Blaire31f9b82012-01-26 22:17:03 +0100308 .name = "stmpe-gpio",
Vipul Kumar Samar86605cf2012-11-26 17:06:51 +0530309 .of_compatible = "st,stmpe-gpio",
Chris Blaire31f9b82012-01-26 22:17:03 +0100310 /* gpio cell resources consist of an irq only so no resources here */
311};
312
Rabin Vincent27e34992010-07-02 16:52:08 +0530313/*
314 * Keypad (1601, 2401, 2403)
315 */
316
317static struct resource stmpe_keypad_resources[] = {
318 {
319 .name = "KEYPAD",
Rabin Vincent27e34992010-07-02 16:52:08 +0530320 .flags = IORESOURCE_IRQ,
321 },
322 {
323 .name = "KEYPAD_OVER",
Rabin Vincent27e34992010-07-02 16:52:08 +0530324 .flags = IORESOURCE_IRQ,
325 },
326};
327
Geert Uytterhoeven6bbb3c42013-11-18 14:33:01 +0100328static const struct mfd_cell stmpe_keypad_cell = {
Rabin Vincent27e34992010-07-02 16:52:08 +0530329 .name = "stmpe-keypad",
Dmitry Torokhov6ea32382012-11-14 08:55:21 -0800330 .of_compatible = "st,stmpe-keypad",
Rabin Vincent27e34992010-07-02 16:52:08 +0530331 .resources = stmpe_keypad_resources,
332 .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
333};
334
335/*
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530336 * STMPE801
337 */
338static const u8 stmpe801_regs[] = {
339 [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
340 [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
341 [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
342 [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
343 [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
344 [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
345 [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
346 [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
347
348};
349
350static struct stmpe_variant_block stmpe801_blocks[] = {
351 {
352 .cell = &stmpe_gpio_cell,
353 .irq = 0,
354 .block = STMPE_BLOCK_GPIO,
355 },
356};
357
Chris Blaire31f9b82012-01-26 22:17:03 +0100358static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
359 {
360 .cell = &stmpe_gpio_cell_noirq,
361 .block = STMPE_BLOCK_GPIO,
362 },
363};
364
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530365static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
366 bool enable)
367{
368 if (blocks & STMPE_BLOCK_GPIO)
369 return 0;
370 else
371 return -EINVAL;
372}
373
374static struct stmpe_variant_info stmpe801 = {
375 .name = "stmpe801",
376 .id_val = STMPE801_ID,
377 .id_mask = 0xffff,
378 .num_gpios = 8,
379 .regs = stmpe801_regs,
380 .blocks = stmpe801_blocks,
381 .num_blocks = ARRAY_SIZE(stmpe801_blocks),
382 .num_irqs = STMPE801_NR_INTERNAL_IRQS,
383 .enable = stmpe801_enable,
384};
385
Chris Blaire31f9b82012-01-26 22:17:03 +0100386static struct stmpe_variant_info stmpe801_noirq = {
387 .name = "stmpe801",
388 .id_val = STMPE801_ID,
389 .id_mask = 0xffff,
390 .num_gpios = 8,
391 .regs = stmpe801_regs,
392 .blocks = stmpe801_blocks_noirq,
393 .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
394 .enable = stmpe801_enable,
395};
396
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530397/*
Viresh Kumar1cda2392011-11-17 11:02:22 +0530398 * Touchscreen (STMPE811 or STMPE610)
Rabin Vincent27e34992010-07-02 16:52:08 +0530399 */
400
401static struct resource stmpe_ts_resources[] = {
402 {
403 .name = "TOUCH_DET",
Rabin Vincent27e34992010-07-02 16:52:08 +0530404 .flags = IORESOURCE_IRQ,
405 },
406 {
407 .name = "FIFO_TH",
Rabin Vincent27e34992010-07-02 16:52:08 +0530408 .flags = IORESOURCE_IRQ,
409 },
410};
411
Geert Uytterhoeven6bbb3c42013-11-18 14:33:01 +0100412static const struct mfd_cell stmpe_ts_cell = {
Rabin Vincent27e34992010-07-02 16:52:08 +0530413 .name = "stmpe-ts",
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800414 .of_compatible = "st,stmpe-ts",
Rabin Vincent27e34992010-07-02 16:52:08 +0530415 .resources = stmpe_ts_resources,
416 .num_resources = ARRAY_SIZE(stmpe_ts_resources),
417};
418
419/*
Viresh Kumar1cda2392011-11-17 11:02:22 +0530420 * STMPE811 or STMPE610
Rabin Vincent27e34992010-07-02 16:52:08 +0530421 */
422
423static const u8 stmpe811_regs[] = {
424 [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
425 [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
426 [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
427 [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
428 [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
429 [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
430 [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
431 [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
432 [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
433 [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
434 [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
435 [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
436 [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
437 [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
438};
439
440static struct stmpe_variant_block stmpe811_blocks[] = {
441 {
442 .cell = &stmpe_gpio_cell,
443 .irq = STMPE811_IRQ_GPIOC,
444 .block = STMPE_BLOCK_GPIO,
445 },
446 {
447 .cell = &stmpe_ts_cell,
448 .irq = STMPE811_IRQ_TOUCH_DET,
449 .block = STMPE_BLOCK_TOUCHSCREEN,
450 },
451};
452
453static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
454 bool enable)
455{
456 unsigned int mask = 0;
457
458 if (blocks & STMPE_BLOCK_GPIO)
459 mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
460
461 if (blocks & STMPE_BLOCK_ADC)
462 mask |= STMPE811_SYS_CTRL2_ADC_OFF;
463
464 if (blocks & STMPE_BLOCK_TOUCHSCREEN)
465 mask |= STMPE811_SYS_CTRL2_TSC_OFF;
466
467 return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
468 enable ? 0 : mask);
469}
470
471static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
472{
473 /* 0 for touchscreen, 1 for GPIO */
474 return block != STMPE_BLOCK_TOUCHSCREEN;
475}
476
477static struct stmpe_variant_info stmpe811 = {
478 .name = "stmpe811",
479 .id_val = 0x0811,
480 .id_mask = 0xffff,
481 .num_gpios = 8,
482 .af_bits = 1,
483 .regs = stmpe811_regs,
484 .blocks = stmpe811_blocks,
485 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
486 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
487 .enable = stmpe811_enable,
488 .get_altfunc = stmpe811_get_altfunc,
489};
490
Viresh Kumar1cda2392011-11-17 11:02:22 +0530491/* Similar to 811, except number of gpios */
492static struct stmpe_variant_info stmpe610 = {
493 .name = "stmpe610",
494 .id_val = 0x0811,
495 .id_mask = 0xffff,
496 .num_gpios = 6,
497 .af_bits = 1,
498 .regs = stmpe811_regs,
499 .blocks = stmpe811_blocks,
500 .num_blocks = ARRAY_SIZE(stmpe811_blocks),
501 .num_irqs = STMPE811_NR_INTERNAL_IRQS,
502 .enable = stmpe811_enable,
503 .get_altfunc = stmpe811_get_altfunc,
504};
505
Rabin Vincent27e34992010-07-02 16:52:08 +0530506/*
507 * STMPE1601
508 */
509
510static const u8 stmpe1601_regs[] = {
511 [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
512 [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
513 [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
514 [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
515 [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
516 [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
517 [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
518 [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
519 [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
520 [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
521 [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
522 [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
523 [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
524 [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
525};
526
527static struct stmpe_variant_block stmpe1601_blocks[] = {
528 {
529 .cell = &stmpe_gpio_cell,
Lee Jones5204e512012-11-05 16:10:32 +0100530 .irq = STMPE1601_IRQ_GPIOC,
Rabin Vincent27e34992010-07-02 16:52:08 +0530531 .block = STMPE_BLOCK_GPIO,
532 },
533 {
534 .cell = &stmpe_keypad_cell,
Lee Jones5204e512012-11-05 16:10:32 +0100535 .irq = STMPE1601_IRQ_KEYPAD,
Rabin Vincent27e34992010-07-02 16:52:08 +0530536 .block = STMPE_BLOCK_KEYPAD,
537 },
538};
539
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530540/* supported autosleep timeout delay (in msecs) */
541static const int stmpe_autosleep_delay[] = {
542 4, 16, 32, 64, 128, 256, 512, 1024,
543};
544
545static int stmpe_round_timeout(int timeout)
546{
547 int i;
548
549 for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
550 if (stmpe_autosleep_delay[i] >= timeout)
551 return i;
552 }
553
554 /*
555 * requests for delays longer than supported should not return the
556 * longest supported delay
557 */
558 return -EINVAL;
559}
560
561static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
562{
563 int ret;
564
565 if (!stmpe->variant->enable_autosleep)
566 return -ENOSYS;
567
568 mutex_lock(&stmpe->lock);
569 ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
570 mutex_unlock(&stmpe->lock);
571
572 return ret;
573}
574
575/*
576 * Both stmpe 1601/2403 support same layout for autosleep
577 */
578static int stmpe1601_autosleep(struct stmpe *stmpe,
579 int autosleep_timeout)
580{
581 int ret, timeout;
582
583 /* choose the best available timeout */
584 timeout = stmpe_round_timeout(autosleep_timeout);
585 if (timeout < 0) {
586 dev_err(stmpe->dev, "invalid timeout\n");
587 return timeout;
588 }
589
590 ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
591 STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
592 timeout);
593 if (ret < 0)
594 return ret;
595
596 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
597 STPME1601_AUTOSLEEP_ENABLE,
598 STPME1601_AUTOSLEEP_ENABLE);
599}
600
Rabin Vincent27e34992010-07-02 16:52:08 +0530601static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
602 bool enable)
603{
604 unsigned int mask = 0;
605
606 if (blocks & STMPE_BLOCK_GPIO)
607 mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
608
609 if (blocks & STMPE_BLOCK_KEYPAD)
610 mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
611
612 return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
613 enable ? mask : 0);
614}
615
616static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
617{
618 switch (block) {
619 case STMPE_BLOCK_PWM:
620 return 2;
621
622 case STMPE_BLOCK_KEYPAD:
623 return 1;
624
625 case STMPE_BLOCK_GPIO:
626 default:
627 return 0;
628 }
629}
630
631static struct stmpe_variant_info stmpe1601 = {
632 .name = "stmpe1601",
633 .id_val = 0x0210,
634 .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
635 .num_gpios = 16,
636 .af_bits = 2,
637 .regs = stmpe1601_regs,
638 .blocks = stmpe1601_blocks,
639 .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
640 .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
641 .enable = stmpe1601_enable,
642 .get_altfunc = stmpe1601_get_altfunc,
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530643 .enable_autosleep = stmpe1601_autosleep,
Rabin Vincent27e34992010-07-02 16:52:08 +0530644};
645
646/*
Jean-Nicolas Graux230f13a2013-04-09 10:35:19 +0200647 * STMPE1801
648 */
649static const u8 stmpe1801_regs[] = {
650 [STMPE_IDX_CHIP_ID] = STMPE1801_REG_CHIP_ID,
651 [STMPE_IDX_ICR_LSB] = STMPE1801_REG_INT_CTRL_LOW,
652 [STMPE_IDX_IER_LSB] = STMPE1801_REG_INT_EN_MASK_LOW,
653 [STMPE_IDX_ISR_LSB] = STMPE1801_REG_INT_STA_LOW,
654 [STMPE_IDX_GPMR_LSB] = STMPE1801_REG_GPIO_MP_LOW,
655 [STMPE_IDX_GPSR_LSB] = STMPE1801_REG_GPIO_SET_LOW,
656 [STMPE_IDX_GPCR_LSB] = STMPE1801_REG_GPIO_CLR_LOW,
657 [STMPE_IDX_GPDR_LSB] = STMPE1801_REG_GPIO_SET_DIR_LOW,
658 [STMPE_IDX_GPRER_LSB] = STMPE1801_REG_GPIO_RE_LOW,
659 [STMPE_IDX_GPFER_LSB] = STMPE1801_REG_GPIO_FE_LOW,
660 [STMPE_IDX_IEGPIOR_LSB] = STMPE1801_REG_INT_EN_GPIO_MASK_LOW,
661 [STMPE_IDX_ISGPIOR_LSB] = STMPE1801_REG_INT_STA_GPIO_LOW,
662};
663
664static struct stmpe_variant_block stmpe1801_blocks[] = {
665 {
666 .cell = &stmpe_gpio_cell,
667 .irq = STMPE1801_IRQ_GPIOC,
668 .block = STMPE_BLOCK_GPIO,
669 },
670 {
671 .cell = &stmpe_keypad_cell,
672 .irq = STMPE1801_IRQ_KEYPAD,
673 .block = STMPE_BLOCK_KEYPAD,
674 },
675};
676
677static int stmpe1801_enable(struct stmpe *stmpe, unsigned int blocks,
678 bool enable)
679{
680 unsigned int mask = 0;
681 if (blocks & STMPE_BLOCK_GPIO)
682 mask |= STMPE1801_MSK_INT_EN_GPIO;
683
684 if (blocks & STMPE_BLOCK_KEYPAD)
685 mask |= STMPE1801_MSK_INT_EN_KPC;
686
687 return __stmpe_set_bits(stmpe, STMPE1801_REG_INT_EN_MASK_LOW, mask,
688 enable ? mask : 0);
689}
690
691static int stmpe1801_reset(struct stmpe *stmpe)
692{
693 unsigned long timeout;
694 int ret = 0;
695
696 ret = __stmpe_set_bits(stmpe, STMPE1801_REG_SYS_CTRL,
697 STMPE1801_MSK_SYS_CTRL_RESET, STMPE1801_MSK_SYS_CTRL_RESET);
698 if (ret < 0)
699 return ret;
700
701 timeout = jiffies + msecs_to_jiffies(100);
702 while (time_before(jiffies, timeout)) {
703 ret = __stmpe_reg_read(stmpe, STMPE1801_REG_SYS_CTRL);
704 if (ret < 0)
705 return ret;
706 if (!(ret & STMPE1801_MSK_SYS_CTRL_RESET))
707 return 0;
708 usleep_range(100, 200);
Sachin Kamat52397fe2014-02-04 14:37:13 +0530709 }
Jean-Nicolas Graux230f13a2013-04-09 10:35:19 +0200710 return -EIO;
711}
712
713static struct stmpe_variant_info stmpe1801 = {
714 .name = "stmpe1801",
715 .id_val = STMPE1801_ID,
716 .id_mask = 0xfff0,
717 .num_gpios = 18,
718 .af_bits = 0,
719 .regs = stmpe1801_regs,
720 .blocks = stmpe1801_blocks,
721 .num_blocks = ARRAY_SIZE(stmpe1801_blocks),
722 .num_irqs = STMPE1801_NR_INTERNAL_IRQS,
723 .enable = stmpe1801_enable,
724 /* stmpe1801 do not have any gpio alternate function */
725 .get_altfunc = NULL,
726};
727
728/*
Rabin Vincent27e34992010-07-02 16:52:08 +0530729 * STMPE24XX
730 */
731
732static const u8 stmpe24xx_regs[] = {
733 [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
734 [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
735 [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
736 [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
737 [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
738 [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
739 [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
740 [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
741 [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
742 [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
743 [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
744 [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
745 [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
746 [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
747};
748
749static struct stmpe_variant_block stmpe24xx_blocks[] = {
750 {
751 .cell = &stmpe_gpio_cell,
752 .irq = STMPE24XX_IRQ_GPIOC,
753 .block = STMPE_BLOCK_GPIO,
754 },
755 {
756 .cell = &stmpe_keypad_cell,
757 .irq = STMPE24XX_IRQ_KEYPAD,
758 .block = STMPE_BLOCK_KEYPAD,
759 },
760};
761
762static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
763 bool enable)
764{
765 unsigned int mask = 0;
766
767 if (blocks & STMPE_BLOCK_GPIO)
768 mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
769
770 if (blocks & STMPE_BLOCK_KEYPAD)
771 mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
772
773 return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
774 enable ? mask : 0);
775}
776
777static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
778{
779 switch (block) {
780 case STMPE_BLOCK_ROTATOR:
781 return 2;
782
783 case STMPE_BLOCK_KEYPAD:
784 return 1;
785
786 case STMPE_BLOCK_GPIO:
787 default:
788 return 0;
789 }
790}
791
792static struct stmpe_variant_info stmpe2401 = {
793 .name = "stmpe2401",
794 .id_val = 0x0101,
795 .id_mask = 0xffff,
796 .num_gpios = 24,
797 .af_bits = 2,
798 .regs = stmpe24xx_regs,
799 .blocks = stmpe24xx_blocks,
800 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
801 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
802 .enable = stmpe24xx_enable,
803 .get_altfunc = stmpe24xx_get_altfunc,
804};
805
806static struct stmpe_variant_info stmpe2403 = {
807 .name = "stmpe2403",
808 .id_val = 0x0120,
809 .id_mask = 0xffff,
810 .num_gpios = 24,
811 .af_bits = 2,
812 .regs = stmpe24xx_regs,
813 .blocks = stmpe24xx_blocks,
814 .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
815 .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
816 .enable = stmpe24xx_enable,
817 .get_altfunc = stmpe24xx_get_altfunc,
Sundar R Iyer5981f4e2010-07-21 11:41:07 +0530818 .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
Rabin Vincent27e34992010-07-02 16:52:08 +0530819};
820
Chris Blaire31f9b82012-01-26 22:17:03 +0100821static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
Viresh Kumar1cda2392011-11-17 11:02:22 +0530822 [STMPE610] = &stmpe610,
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530823 [STMPE801] = &stmpe801,
Rabin Vincent27e34992010-07-02 16:52:08 +0530824 [STMPE811] = &stmpe811,
825 [STMPE1601] = &stmpe1601,
Jean-Nicolas Graux230f13a2013-04-09 10:35:19 +0200826 [STMPE1801] = &stmpe1801,
Rabin Vincent27e34992010-07-02 16:52:08 +0530827 [STMPE2401] = &stmpe2401,
828 [STMPE2403] = &stmpe2403,
829};
830
Chris Blaire31f9b82012-01-26 22:17:03 +0100831/*
832 * These devices can be connected in a 'no-irq' configuration - the irq pin
833 * is not used and the device cannot interrupt the CPU. Here we only list
834 * devices which support this configuration - the driver will fail probing
835 * for any devices not listed here which are configured in this way.
836 */
837static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
838 [STMPE801] = &stmpe801_noirq,
839};
840
Rabin Vincent27e34992010-07-02 16:52:08 +0530841static irqreturn_t stmpe_irq(int irq, void *data)
842{
843 struct stmpe *stmpe = data;
844 struct stmpe_variant_info *variant = stmpe->variant;
845 int num = DIV_ROUND_UP(variant->num_irqs, 8);
Jean-Nicolas Graux230f13a2013-04-09 10:35:19 +0200846 u8 israddr;
Rabin Vincent27e34992010-07-02 16:52:08 +0530847 u8 isr[num];
848 int ret;
849 int i;
850
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530851 if (variant->id_val == STMPE801_ID) {
Lee Jones76f93992012-11-05 16:10:31 +0100852 int base = irq_create_mapping(stmpe->domain, 0);
853
854 handle_nested_irq(base);
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530855 return IRQ_HANDLED;
856 }
857
Jean-Nicolas Graux230f13a2013-04-09 10:35:19 +0200858 if (variant->id_val == STMPE1801_ID)
859 israddr = stmpe->regs[STMPE_IDX_ISR_LSB];
860 else
861 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
862
Rabin Vincent27e34992010-07-02 16:52:08 +0530863 ret = stmpe_block_read(stmpe, israddr, num, isr);
864 if (ret < 0)
865 return IRQ_NONE;
866
867 for (i = 0; i < num; i++) {
868 int bank = num - i - 1;
869 u8 status = isr[i];
870 u8 clear;
871
872 status &= stmpe->ier[bank];
873 if (!status)
874 continue;
875
876 clear = status;
877 while (status) {
878 int bit = __ffs(status);
879 int line = bank * 8 + bit;
Lee Jones76f93992012-11-05 16:10:31 +0100880 int nestedirq = irq_create_mapping(stmpe->domain, line);
Rabin Vincent27e34992010-07-02 16:52:08 +0530881
Lee Jones76f93992012-11-05 16:10:31 +0100882 handle_nested_irq(nestedirq);
Rabin Vincent27e34992010-07-02 16:52:08 +0530883 status &= ~(1 << bit);
884 }
885
886 stmpe_reg_write(stmpe, israddr + i, clear);
887 }
888
889 return IRQ_HANDLED;
890}
891
Mark Brown43b8c082010-12-12 12:01:08 +0000892static void stmpe_irq_lock(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530893{
Mark Brown43b8c082010-12-12 12:01:08 +0000894 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Rabin Vincent27e34992010-07-02 16:52:08 +0530895
896 mutex_lock(&stmpe->irq_lock);
897}
898
Mark Brown43b8c082010-12-12 12:01:08 +0000899static void stmpe_irq_sync_unlock(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530900{
Mark Brown43b8c082010-12-12 12:01:08 +0000901 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Rabin Vincent27e34992010-07-02 16:52:08 +0530902 struct stmpe_variant_info *variant = stmpe->variant;
903 int num = DIV_ROUND_UP(variant->num_irqs, 8);
904 int i;
905
906 for (i = 0; i < num; i++) {
907 u8 new = stmpe->ier[i];
908 u8 old = stmpe->oldier[i];
909
910 if (new == old)
911 continue;
912
913 stmpe->oldier[i] = new;
914 stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
915 }
916
917 mutex_unlock(&stmpe->irq_lock);
918}
919
Mark Brown43b8c082010-12-12 12:01:08 +0000920static void stmpe_irq_mask(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530921{
Mark Brown43b8c082010-12-12 12:01:08 +0000922 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Lee Jones76f93992012-11-05 16:10:31 +0100923 int offset = data->hwirq;
Rabin Vincent27e34992010-07-02 16:52:08 +0530924 int regoffset = offset / 8;
925 int mask = 1 << (offset % 8);
926
927 stmpe->ier[regoffset] &= ~mask;
928}
929
Mark Brown43b8c082010-12-12 12:01:08 +0000930static void stmpe_irq_unmask(struct irq_data *data)
Rabin Vincent27e34992010-07-02 16:52:08 +0530931{
Mark Brown43b8c082010-12-12 12:01:08 +0000932 struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
Lee Jones76f93992012-11-05 16:10:31 +0100933 int offset = data->hwirq;
Rabin Vincent27e34992010-07-02 16:52:08 +0530934 int regoffset = offset / 8;
935 int mask = 1 << (offset % 8);
936
937 stmpe->ier[regoffset] |= mask;
938}
939
940static struct irq_chip stmpe_irq_chip = {
941 .name = "stmpe",
Mark Brown43b8c082010-12-12 12:01:08 +0000942 .irq_bus_lock = stmpe_irq_lock,
943 .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
944 .irq_mask = stmpe_irq_mask,
945 .irq_unmask = stmpe_irq_unmask,
Rabin Vincent27e34992010-07-02 16:52:08 +0530946};
947
Lee Jones76f93992012-11-05 16:10:31 +0100948static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
949 irq_hw_number_t hwirq)
Rabin Vincent27e34992010-07-02 16:52:08 +0530950{
Lee Jones76f93992012-11-05 16:10:31 +0100951 struct stmpe *stmpe = d->host_data;
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530952 struct irq_chip *chip = NULL;
Rabin Vincent27e34992010-07-02 16:52:08 +0530953
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530954 if (stmpe->variant->id_val != STMPE801_ID)
955 chip = &stmpe_irq_chip;
956
Lee Jones76f93992012-11-05 16:10:31 +0100957 irq_set_chip_data(virq, stmpe);
958 irq_set_chip_and_handler(virq, chip, handle_edge_irq);
959 irq_set_nested_thread(virq, 1);
Rabin Vincent27e34992010-07-02 16:52:08 +0530960#ifdef CONFIG_ARM
Lee Jones76f93992012-11-05 16:10:31 +0100961 set_irq_flags(virq, IRQF_VALID);
Rabin Vincent27e34992010-07-02 16:52:08 +0530962#else
Lee Jones76f93992012-11-05 16:10:31 +0100963 irq_set_noprobe(virq);
Rabin Vincent27e34992010-07-02 16:52:08 +0530964#endif
Rabin Vincent27e34992010-07-02 16:52:08 +0530965
966 return 0;
967}
968
Lee Jones76f93992012-11-05 16:10:31 +0100969static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
Rabin Vincent27e34992010-07-02 16:52:08 +0530970{
Rabin Vincent27e34992010-07-02 16:52:08 +0530971#ifdef CONFIG_ARM
Lee Jones76f93992012-11-05 16:10:31 +0100972 set_irq_flags(virq, 0);
Rabin Vincent27e34992010-07-02 16:52:08 +0530973#endif
Lee Jones76f93992012-11-05 16:10:31 +0100974 irq_set_chip_and_handler(virq, NULL, NULL);
975 irq_set_chip_data(virq, NULL);
976}
977
978static struct irq_domain_ops stmpe_irq_ops = {
979 .map = stmpe_irq_map,
980 .unmap = stmpe_irq_unmap,
981 .xlate = irq_domain_xlate_twocell,
982};
983
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800984static int stmpe_irq_init(struct stmpe *stmpe, struct device_node *np)
Lee Jones76f93992012-11-05 16:10:31 +0100985{
Lee Jonesb20a4372012-11-23 15:19:29 +0000986 int base = 0;
Lee Jones76f93992012-11-05 16:10:31 +0100987 int num_irqs = stmpe->variant->num_irqs;
988
Lee Jonesb20a4372012-11-23 15:19:29 +0000989 if (!np)
990 base = stmpe->irq_base;
Lee Jones76f93992012-11-05 16:10:31 +0100991
Lee Jonesb20a4372012-11-23 15:19:29 +0000992 stmpe->domain = irq_domain_add_simple(np, num_irqs, base,
993 &stmpe_irq_ops, stmpe);
Lee Jones76f93992012-11-05 16:10:31 +0100994 if (!stmpe->domain) {
995 dev_err(stmpe->dev, "Failed to create irqdomain\n");
996 return -ENOSYS;
Rabin Vincent27e34992010-07-02 16:52:08 +0530997 }
Lee Jones76f93992012-11-05 16:10:31 +0100998
999 return 0;
Rabin Vincent27e34992010-07-02 16:52:08 +05301000}
1001
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -08001002static int stmpe_chip_init(struct stmpe *stmpe)
Rabin Vincent27e34992010-07-02 16:52:08 +05301003{
1004 unsigned int irq_trigger = stmpe->pdata->irq_trigger;
Sundar R Iyer5981f4e2010-07-21 11:41:07 +05301005 int autosleep_timeout = stmpe->pdata->autosleep_timeout;
Rabin Vincent27e34992010-07-02 16:52:08 +05301006 struct stmpe_variant_info *variant = stmpe->variant;
Chris Blaire31f9b82012-01-26 22:17:03 +01001007 u8 icr = 0;
Rabin Vincent27e34992010-07-02 16:52:08 +05301008 unsigned int id;
1009 u8 data[2];
1010 int ret;
1011
1012 ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
1013 ARRAY_SIZE(data), data);
1014 if (ret < 0)
1015 return ret;
1016
1017 id = (data[0] << 8) | data[1];
1018 if ((id & variant->id_mask) != variant->id_val) {
1019 dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
1020 return -EINVAL;
1021 }
1022
1023 dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
1024
1025 /* Disable all modules -- subdrivers should enable what they need. */
1026 ret = stmpe_disable(stmpe, ~0);
1027 if (ret)
1028 return ret;
1029
Jean-Nicolas Graux230f13a2013-04-09 10:35:19 +02001030 if (id == STMPE1801_ID) {
1031 ret = stmpe1801_reset(stmpe);
1032 if (ret < 0)
1033 return ret;
1034 }
1035
Chris Blaire31f9b82012-01-26 22:17:03 +01001036 if (stmpe->irq >= 0) {
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +05301037 if (id == STMPE801_ID)
Chris Blaire31f9b82012-01-26 22:17:03 +01001038 icr = STMPE801_REG_SYS_CTRL_INT_EN;
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +05301039 else
Chris Blaire31f9b82012-01-26 22:17:03 +01001040 icr = STMPE_ICR_LSB_GIM;
Rabin Vincent27e34992010-07-02 16:52:08 +05301041
Chris Blaire31f9b82012-01-26 22:17:03 +01001042 /* STMPE801 doesn't support Edge interrupts */
1043 if (id != STMPE801_ID) {
1044 if (irq_trigger == IRQF_TRIGGER_FALLING ||
1045 irq_trigger == IRQF_TRIGGER_RISING)
1046 icr |= STMPE_ICR_LSB_EDGE;
1047 }
1048
1049 if (irq_trigger == IRQF_TRIGGER_RISING ||
1050 irq_trigger == IRQF_TRIGGER_HIGH) {
1051 if (id == STMPE801_ID)
1052 icr |= STMPE801_REG_SYS_CTRL_INT_HI;
1053 else
1054 icr |= STMPE_ICR_LSB_HIGH;
1055 }
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +05301056 }
Rabin Vincent27e34992010-07-02 16:52:08 +05301057
Sundar R Iyer5981f4e2010-07-21 11:41:07 +05301058 if (stmpe->pdata->autosleep) {
1059 ret = stmpe_autosleep(stmpe, autosleep_timeout);
1060 if (ret)
1061 return ret;
1062 }
1063
Rabin Vincent27e34992010-07-02 16:52:08 +05301064 return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
1065}
1066
Geert Uytterhoeven6bbb3c42013-11-18 14:33:01 +01001067static int stmpe_add_device(struct stmpe *stmpe, const struct mfd_cell *cell)
Rabin Vincent27e34992010-07-02 16:52:08 +05301068{
1069 return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
Lee Jones76f93992012-11-05 16:10:31 +01001070 NULL, stmpe->irq_base, stmpe->domain);
Rabin Vincent27e34992010-07-02 16:52:08 +05301071}
1072
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -08001073static int stmpe_devices_init(struct stmpe *stmpe)
Rabin Vincent27e34992010-07-02 16:52:08 +05301074{
1075 struct stmpe_variant_info *variant = stmpe->variant;
1076 unsigned int platform_blocks = stmpe->pdata->blocks;
1077 int ret = -EINVAL;
Lee Jones7da0cbf2012-11-05 16:10:30 +01001078 int i, j;
Rabin Vincent27e34992010-07-02 16:52:08 +05301079
1080 for (i = 0; i < variant->num_blocks; i++) {
1081 struct stmpe_variant_block *block = &variant->blocks[i];
1082
1083 if (!(platform_blocks & block->block))
1084 continue;
1085
Lee Jones7da0cbf2012-11-05 16:10:30 +01001086 for (j = 0; j < block->cell->num_resources; j++) {
1087 struct resource *res =
1088 (struct resource *) &block->cell->resources[j];
1089
1090 /* Dynamically fill in a variant's IRQ. */
1091 if (res->flags & IORESOURCE_IRQ)
1092 res->start = res->end = block->irq + j;
1093 }
1094
Rabin Vincent27e34992010-07-02 16:52:08 +05301095 platform_blocks &= ~block->block;
Lee Jones7da0cbf2012-11-05 16:10:30 +01001096 ret = stmpe_add_device(stmpe, block->cell);
Rabin Vincent27e34992010-07-02 16:52:08 +05301097 if (ret)
1098 return ret;
1099 }
1100
1101 if (platform_blocks)
1102 dev_warn(stmpe->dev,
1103 "platform wants blocks (%#x) not present on variant",
1104 platform_blocks);
1105
1106 return ret;
1107}
1108
Mark Browna9c40552013-07-28 15:03:48 +01001109static void stmpe_of_probe(struct stmpe_platform_data *pdata,
1110 struct device_node *np)
Lee Jones909582c2012-11-05 16:10:33 +01001111{
1112 struct device_node *child;
1113
Gabriel Fernandez408a3fa2013-03-01 13:07:26 +01001114 pdata->id = of_alias_get_id(np, "stmpe-i2c");
1115 if (pdata->id < 0)
1116 pdata->id = -1;
1117
Vipul Kumar Samarac713cc2012-12-07 20:29:37 +05301118 pdata->irq_trigger = IRQF_TRIGGER_NONE;
1119
Lee Jones909582c2012-11-05 16:10:33 +01001120 of_property_read_u32(np, "st,autosleep-timeout",
1121 &pdata->autosleep_timeout);
1122
1123 pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
1124
1125 for_each_child_of_node(np, child) {
1126 if (!strcmp(child->name, "stmpe_gpio")) {
1127 pdata->blocks |= STMPE_BLOCK_GPIO;
Vipul Kumar Samarac713cc2012-12-07 20:29:37 +05301128 } else if (!strcmp(child->name, "stmpe_keypad")) {
Lee Jones909582c2012-11-05 16:10:33 +01001129 pdata->blocks |= STMPE_BLOCK_KEYPAD;
Vipul Kumar Samarac713cc2012-12-07 20:29:37 +05301130 } else if (!strcmp(child->name, "stmpe_touchscreen")) {
Lee Jones909582c2012-11-05 16:10:33 +01001131 pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
Vipul Kumar Samarac713cc2012-12-07 20:29:37 +05301132 } else if (!strcmp(child->name, "stmpe_adc")) {
Lee Jones909582c2012-11-05 16:10:33 +01001133 pdata->blocks |= STMPE_BLOCK_ADC;
Vipul Kumar Samarac713cc2012-12-07 20:29:37 +05301134 } else if (!strcmp(child->name, "stmpe_pwm")) {
1135 pdata->blocks |= STMPE_BLOCK_PWM;
1136 } else if (!strcmp(child->name, "stmpe_rotator")) {
1137 pdata->blocks |= STMPE_BLOCK_ROTATOR;
Lee Jones909582c2012-11-05 16:10:33 +01001138 }
1139 }
1140}
1141
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301142/* Called from client specific probe routines */
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -08001143int stmpe_probe(struct stmpe_client_info *ci, int partnum)
Sundar Iyer208c4342010-09-15 10:30:54 +05301144{
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301145 struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
Lee Jones909582c2012-11-05 16:10:33 +01001146 struct device_node *np = ci->dev->of_node;
Rabin Vincent27e34992010-07-02 16:52:08 +05301147 struct stmpe *stmpe;
1148 int ret;
1149
Lee Jones909582c2012-11-05 16:10:33 +01001150 if (!pdata) {
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301151 if (!np)
Lee Jones909582c2012-11-05 16:10:33 +01001152 return -EINVAL;
Rabin Vincent27e34992010-07-02 16:52:08 +05301153
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301154 pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
1155 if (!pdata)
1156 return -ENOMEM;
1157
1158 stmpe_of_probe(pdata, np);
Gabriel Fernandeza200e322013-03-01 13:07:16 +01001159
1160 if (of_find_property(np, "interrupts", NULL) == NULL)
1161 ci->irq = -1;
Lee Jones909582c2012-11-05 16:10:33 +01001162 }
Rabin Vincent27e34992010-07-02 16:52:08 +05301163
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301164 stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
Rabin Vincent27e34992010-07-02 16:52:08 +05301165 if (!stmpe)
1166 return -ENOMEM;
1167
1168 mutex_init(&stmpe->irq_lock);
1169 mutex_init(&stmpe->lock);
1170
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301171 stmpe->dev = ci->dev;
1172 stmpe->client = ci->client;
Rabin Vincent27e34992010-07-02 16:52:08 +05301173 stmpe->pdata = pdata;
1174 stmpe->irq_base = pdata->irq_base;
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301175 stmpe->ci = ci;
1176 stmpe->partnum = partnum;
1177 stmpe->variant = stmpe_variant_info[partnum];
Rabin Vincent27e34992010-07-02 16:52:08 +05301178 stmpe->regs = stmpe->variant->regs;
1179 stmpe->num_gpios = stmpe->variant->num_gpios;
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301180 dev_set_drvdata(stmpe->dev, stmpe);
Rabin Vincent27e34992010-07-02 16:52:08 +05301181
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301182 if (ci->init)
1183 ci->init(stmpe);
Rabin Vincent27e34992010-07-02 16:52:08 +05301184
Viresh Kumar73de16d2011-11-08 09:44:06 +05301185 if (pdata->irq_over_gpio) {
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301186 ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
1187 GPIOF_DIR_IN, "stmpe");
Viresh Kumar73de16d2011-11-08 09:44:06 +05301188 if (ret) {
1189 dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
1190 ret);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301191 return ret;
Viresh Kumar73de16d2011-11-08 09:44:06 +05301192 }
1193
1194 stmpe->irq = gpio_to_irq(pdata->irq_gpio);
1195 } else {
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301196 stmpe->irq = ci->irq;
Viresh Kumar73de16d2011-11-08 09:44:06 +05301197 }
1198
Chris Blaire31f9b82012-01-26 22:17:03 +01001199 if (stmpe->irq < 0) {
1200 /* use alternate variant info for no-irq mode, if supported */
1201 dev_info(stmpe->dev,
1202 "%s configured in no-irq mode by platform data\n",
1203 stmpe->variant->name);
1204 if (!stmpe_noirq_variant_info[stmpe->partnum]) {
1205 dev_err(stmpe->dev,
1206 "%s does not support no-irq mode!\n",
1207 stmpe->variant->name);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301208 return -ENODEV;
Chris Blaire31f9b82012-01-26 22:17:03 +01001209 }
1210 stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
Vipul Kumar Samarac713cc2012-12-07 20:29:37 +05301211 } else if (pdata->irq_trigger == IRQF_TRIGGER_NONE) {
Javier Martinez Canillas1a5595c2013-06-14 18:40:46 +02001212 pdata->irq_trigger = irq_get_trigger_type(stmpe->irq);
Chris Blaire31f9b82012-01-26 22:17:03 +01001213 }
1214
Rabin Vincent27e34992010-07-02 16:52:08 +05301215 ret = stmpe_chip_init(stmpe);
1216 if (ret)
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301217 return ret;
Rabin Vincent27e34992010-07-02 16:52:08 +05301218
Chris Blaire31f9b82012-01-26 22:17:03 +01001219 if (stmpe->irq >= 0) {
Lee Jones909582c2012-11-05 16:10:33 +01001220 ret = stmpe_irq_init(stmpe, np);
Chris Blaire31f9b82012-01-26 22:17:03 +01001221 if (ret)
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301222 return ret;
Rabin Vincent27e34992010-07-02 16:52:08 +05301223
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301224 ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
1225 stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
Chris Blaire31f9b82012-01-26 22:17:03 +01001226 "stmpe", stmpe);
1227 if (ret) {
1228 dev_err(stmpe->dev, "failed to request IRQ: %d\n",
1229 ret);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301230 return ret;
Chris Blaire31f9b82012-01-26 22:17:03 +01001231 }
Rabin Vincent27e34992010-07-02 16:52:08 +05301232 }
1233
1234 ret = stmpe_devices_init(stmpe);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301235 if (!ret)
1236 return 0;
Rabin Vincent27e34992010-07-02 16:52:08 +05301237
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301238 dev_err(stmpe->dev, "failed to add children\n");
Rabin Vincent27e34992010-07-02 16:52:08 +05301239 mfd_remove_devices(stmpe->dev);
Viresh Kumarcb5faba2012-11-22 10:40:29 +05301240
Rabin Vincent27e34992010-07-02 16:52:08 +05301241 return ret;
1242}
1243
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301244int stmpe_remove(struct stmpe *stmpe)
Rabin Vincent27e34992010-07-02 16:52:08 +05301245{
Rabin Vincent27e34992010-07-02 16:52:08 +05301246 mfd_remove_devices(stmpe->dev);
1247
Rabin Vincent27e34992010-07-02 16:52:08 +05301248 return 0;
1249}
1250
Sundar Iyer208c4342010-09-15 10:30:54 +05301251#ifdef CONFIG_PM
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301252static int stmpe_suspend(struct device *dev)
1253{
1254 struct stmpe *stmpe = dev_get_drvdata(dev);
1255
Chris Blaire31f9b82012-01-26 22:17:03 +01001256 if (stmpe->irq >= 0 && device_may_wakeup(dev))
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301257 enable_irq_wake(stmpe->irq);
1258
1259 return 0;
1260}
1261
1262static int stmpe_resume(struct device *dev)
1263{
1264 struct stmpe *stmpe = dev_get_drvdata(dev);
1265
Chris Blaire31f9b82012-01-26 22:17:03 +01001266 if (stmpe->irq >= 0 && device_may_wakeup(dev))
Viresh Kumar1a6e4b72011-11-17 11:02:20 +05301267 disable_irq_wake(stmpe->irq);
1268
1269 return 0;
1270}
1271
1272const struct dev_pm_ops stmpe_dev_pm_ops = {
Sundar Iyer208c4342010-09-15 10:30:54 +05301273 .suspend = stmpe_suspend,
1274 .resume = stmpe_resume,
1275};
1276#endif