]>
Commit | Line | Data |
---|---|---|
27e34995 | 1 | /* |
1a6e4b74 VK |
2 | * ST Microelectronics MFD: stmpe's driver |
3 | * | |
27e34995 RV |
4 | * Copyright (C) ST-Ericsson SA 2010 |
5 | * | |
6 | * License Terms: GNU General Public License, version 2 | |
7 | * Author: Rabin Vincent <[email protected]> for ST-Ericsson | |
8 | */ | |
9 | ||
73de16db | 10 | #include <linux/gpio.h> |
dba61c8f | 11 | #include <linux/export.h> |
27e34995 | 12 | #include <linux/kernel.h> |
27e34995 RV |
13 | #include <linux/interrupt.h> |
14 | #include <linux/irq.h> | |
76f93992 | 15 | #include <linux/irqdomain.h> |
1a6e4b74 | 16 | #include <linux/pm.h> |
27e34995 | 17 | #include <linux/slab.h> |
27e34995 | 18 | #include <linux/mfd/core.h> |
27e34995 RV |
19 | #include "stmpe.h" |
20 | ||
21 | static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks) | |
22 | { | |
23 | return stmpe->variant->enable(stmpe, blocks, true); | |
24 | } | |
25 | ||
26 | static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks) | |
27 | { | |
28 | return stmpe->variant->enable(stmpe, blocks, false); | |
29 | } | |
30 | ||
31 | static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg) | |
32 | { | |
33 | int ret; | |
34 | ||
1a6e4b74 | 35 | ret = stmpe->ci->read_byte(stmpe, reg); |
27e34995 | 36 | if (ret < 0) |
1a6e4b74 | 37 | dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret); |
27e34995 RV |
38 | |
39 | dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret); | |
40 | ||
41 | return ret; | |
42 | } | |
43 | ||
44 | static 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 | ||
1a6e4b74 | 50 | ret = stmpe->ci->write_byte(stmpe, reg, val); |
27e34995 | 51 | if (ret < 0) |
1a6e4b74 | 52 | dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret); |
27e34995 RV |
53 | |
54 | return ret; | |
55 | } | |
56 | ||
57 | static 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 | ||
71 | static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, | |
72 | u8 *values) | |
73 | { | |
74 | int ret; | |
75 | ||
1a6e4b74 | 76 | ret = stmpe->ci->read_block(stmpe, reg, length, values); |
27e34995 | 77 | if (ret < 0) |
1a6e4b74 | 78 | dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret); |
27e34995 RV |
79 | |
80 | dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret); | |
81 | stmpe_dump_bytes("stmpe rd: ", values, length); | |
82 | ||
83 | return ret; | |
84 | } | |
85 | ||
86 | static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length, | |
87 | const u8 *values) | |
88 | { | |
89 | int ret; | |
90 | ||
91 | dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length); | |
92 | stmpe_dump_bytes("stmpe wr: ", values, length); | |
93 | ||
1a6e4b74 | 94 | ret = stmpe->ci->write_block(stmpe, reg, length, values); |
27e34995 | 95 | if (ret < 0) |
1a6e4b74 | 96 | dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret); |
27e34995 RV |
97 | |
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 | */ | |
106 | int 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 | } | |
116 | EXPORT_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 | */ | |
123 | int 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 | } | |
133 | EXPORT_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 | */ | |
140 | int 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 | } | |
150 | EXPORT_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 | */ | |
158 | int 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 | } | |
168 | EXPORT_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 | */ | |
177 | int 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 | } | |
187 | EXPORT_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 | */ | |
196 | int 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 | } | |
206 | EXPORT_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 | */ | |
215 | int 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 | } | |
226 | EXPORT_SYMBOL_GPL(stmpe_block_write); | |
227 | ||
228 | /** | |
4dcaa6b6 | 229 | * stmpe_set_altfunc()- set the alternate function for STMPE pins |
27e34995 RV |
230 | * @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 | */ | |
240 | int 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); | |
27e34995 RV |
246 | int mask = (1 << af_bits) - 1; |
247 | u8 regs[numregs]; | |
7f7f4ea1 VK |
248 | int af, afperreg, ret; |
249 | ||
250 | if (!variant->get_altfunc) | |
251 | return 0; | |
27e34995 | 252 | |
7f7f4ea1 | 253 | afperreg = 8 / af_bits; |
27e34995 RV |
254 | mutex_lock(&stmpe->lock); |
255 | ||
256 | ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO); | |
257 | if (ret < 0) | |
258 | goto out; | |
259 | ||
260 | ret = __stmpe_block_read(stmpe, regaddr, numregs, regs); | |
261 | if (ret < 0) | |
262 | goto out; | |
263 | ||
264 | af = variant->get_altfunc(stmpe, block); | |
265 | ||
266 | while (pins) { | |
267 | int pin = __ffs(pins); | |
268 | int regoffset = numregs - (pin / afperreg) - 1; | |
269 | int pos = (pin % afperreg) * (8 / afperreg); | |
270 | ||
271 | regs[regoffset] &= ~(mask << pos); | |
272 | regs[regoffset] |= af << pos; | |
273 | ||
274 | pins &= ~(1 << pin); | |
275 | } | |
276 | ||
277 | ret = __stmpe_block_write(stmpe, regaddr, numregs, regs); | |
278 | ||
279 | out: | |
280 | mutex_unlock(&stmpe->lock); | |
281 | return ret; | |
282 | } | |
283 | EXPORT_SYMBOL_GPL(stmpe_set_altfunc); | |
284 | ||
285 | /* | |
286 | * GPIO (all variants) | |
287 | */ | |
288 | ||
289 | static struct resource stmpe_gpio_resources[] = { | |
290 | /* Start and end filled dynamically */ | |
291 | { | |
292 | .flags = IORESOURCE_IRQ, | |
293 | }, | |
294 | }; | |
295 | ||
296 | static struct mfd_cell stmpe_gpio_cell = { | |
297 | .name = "stmpe-gpio", | |
298 | .resources = stmpe_gpio_resources, | |
299 | .num_resources = ARRAY_SIZE(stmpe_gpio_resources), | |
300 | }; | |
301 | ||
e31f9b82 CB |
302 | static 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 | ||
27e34995 RV |
307 | /* |
308 | * Keypad (1601, 2401, 2403) | |
309 | */ | |
310 | ||
311 | static struct resource stmpe_keypad_resources[] = { | |
312 | { | |
313 | .name = "KEYPAD", | |
27e34995 RV |
314 | .flags = IORESOURCE_IRQ, |
315 | }, | |
316 | { | |
317 | .name = "KEYPAD_OVER", | |
27e34995 RV |
318 | .flags = IORESOURCE_IRQ, |
319 | }, | |
320 | }; | |
321 | ||
322 | static 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 | ||
7f7f4ea1 VK |
328 | /* |
329 | * STMPE801 | |
330 | */ | |
331 | static 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 | ||
343 | static struct stmpe_variant_block stmpe801_blocks[] = { | |
344 | { | |
345 | .cell = &stmpe_gpio_cell, | |
346 | .irq = 0, | |
347 | .block = STMPE_BLOCK_GPIO, | |
348 | }, | |
349 | }; | |
350 | ||
e31f9b82 CB |
351 | static struct stmpe_variant_block stmpe801_blocks_noirq[] = { |
352 | { | |
353 | .cell = &stmpe_gpio_cell_noirq, | |
354 | .block = STMPE_BLOCK_GPIO, | |
355 | }, | |
356 | }; | |
357 | ||
7f7f4ea1 VK |
358 | static 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 | ||
367 | static 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 | ||
e31f9b82 CB |
379 | static 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 | ||
27e34995 | 390 | /* |
1cda2394 | 391 | * Touchscreen (STMPE811 or STMPE610) |
27e34995 RV |
392 | */ |
393 | ||
394 | static struct resource stmpe_ts_resources[] = { | |
395 | { | |
396 | .name = "TOUCH_DET", | |
27e34995 RV |
397 | .flags = IORESOURCE_IRQ, |
398 | }, | |
399 | { | |
400 | .name = "FIFO_TH", | |
27e34995 RV |
401 | .flags = IORESOURCE_IRQ, |
402 | }, | |
403 | }; | |
404 | ||
405 | static 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 | /* | |
1cda2394 | 412 | * STMPE811 or STMPE610 |
27e34995 RV |
413 | */ |
414 | ||
415 | static 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 | ||
432 | static 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 | ||
445 | static 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 | ||
463 | static 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 | ||
469 | static 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 | ||
1cda2394 VK |
483 | /* Similar to 811, except number of gpios */ |
484 | static 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 | ||
27e34995 RV |
498 | /* |
499 | * STMPE1601 | |
500 | */ | |
501 | ||
502 | static 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 | ||
519 | static struct stmpe_variant_block stmpe1601_blocks[] = { | |
520 | { | |
521 | .cell = &stmpe_gpio_cell, | |
5204e51d | 522 | .irq = STMPE1601_IRQ_GPIOC, |
27e34995 RV |
523 | .block = STMPE_BLOCK_GPIO, |
524 | }, | |
525 | { | |
526 | .cell = &stmpe_keypad_cell, | |
5204e51d | 527 | .irq = STMPE1601_IRQ_KEYPAD, |
27e34995 RV |
528 | .block = STMPE_BLOCK_KEYPAD, |
529 | }, | |
530 | }; | |
531 | ||
5981f4e6 SI |
532 | /* supported autosleep timeout delay (in msecs) */ |
533 | static const int stmpe_autosleep_delay[] = { | |
534 | 4, 16, 32, 64, 128, 256, 512, 1024, | |
535 | }; | |
536 | ||
537 | static 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 | ||
553 | static 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 | */ | |
570 | static 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 | ||
27e34995 RV |
593 | static 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 | ||
608 | static 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 | ||
623 | static 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, | |
5981f4e6 | 635 | .enable_autosleep = stmpe1601_autosleep, |
27e34995 RV |
636 | }; |
637 | ||
638 | /* | |
639 | * STMPE24XX | |
640 | */ | |
641 | ||
642 | static 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 | ||
659 | static 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 | ||
672 | static 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 | ||
687 | static 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 | ||
702 | static 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 | ||
716 | static 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, | |
5981f4e6 | 728 | .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */ |
27e34995 RV |
729 | }; |
730 | ||
e31f9b82 | 731 | static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = { |
1cda2394 | 732 | [STMPE610] = &stmpe610, |
7f7f4ea1 | 733 | [STMPE801] = &stmpe801, |
27e34995 RV |
734 | [STMPE811] = &stmpe811, |
735 | [STMPE1601] = &stmpe1601, | |
736 | [STMPE2401] = &stmpe2401, | |
737 | [STMPE2403] = &stmpe2403, | |
738 | }; | |
739 | ||
e31f9b82 CB |
740 | /* |
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 | */ | |
746 | static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = { | |
747 | [STMPE801] = &stmpe801_noirq, | |
748 | }; | |
749 | ||
27e34995 RV |
750 | static 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 | ||
7f7f4ea1 | 760 | if (variant->id_val == STMPE801_ID) { |
76f93992 LJ |
761 | int base = irq_create_mapping(stmpe->domain, 0); |
762 | ||
763 | handle_nested_irq(base); | |
7f7f4ea1 VK |
764 | return IRQ_HANDLED; |
765 | } | |
766 | ||
27e34995 RV |
767 | 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; | |
76f93992 | 784 | int nestedirq = irq_create_mapping(stmpe->domain, line); |
27e34995 | 785 | |
76f93992 | 786 | handle_nested_irq(nestedirq); |
27e34995 RV |
787 | status &= ~(1 << bit); |
788 | } | |
789 | ||
790 | stmpe_reg_write(stmpe, israddr + i, clear); | |
791 | } | |
792 | ||
793 | return IRQ_HANDLED; | |
794 | } | |
795 | ||
43b8c084 | 796 | static void stmpe_irq_lock(struct irq_data *data) |
27e34995 | 797 | { |
43b8c084 | 798 | struct stmpe *stmpe = irq_data_get_irq_chip_data(data); |
27e34995 RV |
799 | |
800 | mutex_lock(&stmpe->irq_lock); | |
801 | } | |
802 | ||
43b8c084 | 803 | static void stmpe_irq_sync_unlock(struct irq_data *data) |
27e34995 | 804 | { |
43b8c084 | 805 | struct stmpe *stmpe = irq_data_get_irq_chip_data(data); |
27e34995 RV |
806 | 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 | ||
43b8c084 | 824 | static void stmpe_irq_mask(struct irq_data *data) |
27e34995 | 825 | { |
43b8c084 | 826 | struct stmpe *stmpe = irq_data_get_irq_chip_data(data); |
76f93992 | 827 | int offset = data->hwirq; |
27e34995 RV |
828 | int regoffset = offset / 8; |
829 | int mask = 1 << (offset % 8); | |
830 | ||
831 | stmpe->ier[regoffset] &= ~mask; | |
832 | } | |
833 | ||
43b8c084 | 834 | static void stmpe_irq_unmask(struct irq_data *data) |
27e34995 | 835 | { |
43b8c084 | 836 | struct stmpe *stmpe = irq_data_get_irq_chip_data(data); |
76f93992 | 837 | int offset = data->hwirq; |
27e34995 RV |
838 | int regoffset = offset / 8; |
839 | int mask = 1 << (offset % 8); | |
840 | ||
841 | stmpe->ier[regoffset] |= mask; | |
842 | } | |
843 | ||
844 | static struct irq_chip stmpe_irq_chip = { | |
845 | .name = "stmpe", | |
43b8c084 MB |
846 | .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, | |
27e34995 RV |
850 | }; |
851 | ||
76f93992 LJ |
852 | static int stmpe_irq_map(struct irq_domain *d, unsigned int virq, |
853 | irq_hw_number_t hwirq) | |
27e34995 | 854 | { |
76f93992 | 855 | struct stmpe *stmpe = d->host_data; |
7f7f4ea1 | 856 | struct irq_chip *chip = NULL; |
27e34995 | 857 | |
7f7f4ea1 VK |
858 | if (stmpe->variant->id_val != STMPE801_ID) |
859 | chip = &stmpe_irq_chip; | |
860 | ||
76f93992 LJ |
861 | irq_set_chip_data(virq, stmpe); |
862 | irq_set_chip_and_handler(virq, chip, handle_edge_irq); | |
863 | irq_set_nested_thread(virq, 1); | |
27e34995 | 864 | #ifdef CONFIG_ARM |
76f93992 | 865 | set_irq_flags(virq, IRQF_VALID); |
27e34995 | 866 | #else |
76f93992 | 867 | irq_set_noprobe(virq); |
27e34995 | 868 | #endif |
27e34995 RV |
869 | |
870 | return 0; | |
871 | } | |
872 | ||
76f93992 | 873 | static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq) |
27e34995 | 874 | { |
27e34995 | 875 | #ifdef CONFIG_ARM |
76f93992 | 876 | set_irq_flags(virq, 0); |
27e34995 | 877 | #endif |
76f93992 LJ |
878 | irq_set_chip_and_handler(virq, NULL, NULL); |
879 | irq_set_chip_data(virq, NULL); | |
880 | } | |
881 | ||
882 | static 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 | ||
909582ca LJ |
888 | static int __devinit stmpe_irq_init(struct stmpe *stmpe, |
889 | struct device_node *np) | |
76f93992 LJ |
890 | { |
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( | |
909582ca | 896 | np, num_irqs, base, 0, &stmpe_irq_ops, stmpe); |
76f93992 LJ |
897 | } |
898 | else { | |
899 | stmpe->domain = irq_domain_add_linear( | |
909582ca | 900 | np, num_irqs, &stmpe_irq_ops, stmpe); |
76f93992 LJ |
901 | } |
902 | ||
903 | if (!stmpe->domain) { | |
904 | dev_err(stmpe->dev, "Failed to create irqdomain\n"); | |
905 | return -ENOSYS; | |
27e34995 | 906 | } |
76f93992 LJ |
907 | |
908 | return 0; | |
27e34995 RV |
909 | } |
910 | ||
911 | static int __devinit stmpe_chip_init(struct stmpe *stmpe) | |
912 | { | |
913 | unsigned int irq_trigger = stmpe->pdata->irq_trigger; | |
5981f4e6 | 914 | int autosleep_timeout = stmpe->pdata->autosleep_timeout; |
27e34995 | 915 | struct stmpe_variant_info *variant = stmpe->variant; |
e31f9b82 | 916 | u8 icr = 0; |
27e34995 RV |
917 | 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 | ||
e31f9b82 | 939 | if (stmpe->irq >= 0) { |
7f7f4ea1 | 940 | if (id == STMPE801_ID) |
e31f9b82 | 941 | icr = STMPE801_REG_SYS_CTRL_INT_EN; |
7f7f4ea1 | 942 | else |
e31f9b82 | 943 | icr = STMPE_ICR_LSB_GIM; |
27e34995 | 944 | |
e31f9b82 CB |
945 | /* 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 | } | |
7f7f4ea1 | 966 | } |
27e34995 | 967 | |
5981f4e6 SI |
968 | if (stmpe->pdata->autosleep) { |
969 | ret = stmpe_autosleep(stmpe, autosleep_timeout); | |
970 | if (ret) | |
971 | return ret; | |
972 | } | |
973 | ||
27e34995 RV |
974 | return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr); |
975 | } | |
976 | ||
977 | static int __devinit stmpe_add_device(struct stmpe *stmpe, | |
7da0cbfc | 978 | struct mfd_cell *cell) |
27e34995 RV |
979 | { |
980 | return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1, | |
76f93992 | 981 | NULL, stmpe->irq_base, stmpe->domain); |
27e34995 RV |
982 | } |
983 | ||
984 | static 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; | |
7da0cbfc | 989 | int i, j; |
27e34995 RV |
990 | |
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 | ||
7da0cbfc LJ |
997 | 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 | ||
27e34995 | 1006 | platform_blocks &= ~block->block; |
7da0cbfc | 1007 | ret = stmpe_add_device(stmpe, block->cell); |
27e34995 RV |
1008 | 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 | ||
909582ca LJ |
1020 | void __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 | ||
1a6e4b74 | 1046 | /* Called from client specific probe routines */ |
8ad1a973 | 1047 | int __devinit stmpe_probe(struct stmpe_client_info *ci, int partnum) |
208c4343 | 1048 | { |
1a6e4b74 | 1049 | struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev); |
909582ca | 1050 | struct device_node *np = ci->dev->of_node; |
27e34995 RV |
1051 | struct stmpe *stmpe; |
1052 | int ret; | |
1053 | ||
909582ca | 1054 | if (!pdata) { |
cb5faba9 | 1055 | if (!np) |
909582ca | 1056 | return -EINVAL; |
cb5faba9 VK |
1057 | |
1058 | pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL); | |
1059 | if (!pdata) | |
1060 | return -ENOMEM; | |
1061 | ||
1062 | stmpe_of_probe(pdata, np); | |
909582ca | 1063 | } |
27e34995 | 1064 | |
cb5faba9 | 1065 | stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL); |
27e34995 RV |
1066 | if (!stmpe) |
1067 | return -ENOMEM; | |
1068 | ||
1069 | mutex_init(&stmpe->irq_lock); | |
1070 | mutex_init(&stmpe->lock); | |
1071 | ||
1a6e4b74 VK |
1072 | stmpe->dev = ci->dev; |
1073 | stmpe->client = ci->client; | |
27e34995 RV |
1074 | stmpe->pdata = pdata; |
1075 | stmpe->irq_base = pdata->irq_base; | |
1a6e4b74 VK |
1076 | stmpe->ci = ci; |
1077 | stmpe->partnum = partnum; | |
1078 | stmpe->variant = stmpe_variant_info[partnum]; | |
27e34995 RV |
1079 | stmpe->regs = stmpe->variant->regs; |
1080 | stmpe->num_gpios = stmpe->variant->num_gpios; | |
1a6e4b74 | 1081 | dev_set_drvdata(stmpe->dev, stmpe); |
27e34995 | 1082 | |
1a6e4b74 VK |
1083 | if (ci->init) |
1084 | ci->init(stmpe); | |
27e34995 | 1085 | |
73de16db | 1086 | if (pdata->irq_over_gpio) { |
cb5faba9 VK |
1087 | ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio, |
1088 | GPIOF_DIR_IN, "stmpe"); | |
73de16db VK |
1089 | if (ret) { |
1090 | dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n", | |
1091 | ret); | |
cb5faba9 | 1092 | return ret; |
73de16db VK |
1093 | } |
1094 | ||
1095 | stmpe->irq = gpio_to_irq(pdata->irq_gpio); | |
1096 | } else { | |
1a6e4b74 | 1097 | stmpe->irq = ci->irq; |
73de16db VK |
1098 | } |
1099 | ||
e31f9b82 CB |
1100 | 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); | |
cb5faba9 | 1109 | return -ENODEV; |
e31f9b82 CB |
1110 | } |
1111 | stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum]; | |
1112 | } | |
1113 | ||
27e34995 RV |
1114 | ret = stmpe_chip_init(stmpe); |
1115 | if (ret) | |
cb5faba9 | 1116 | return ret; |
27e34995 | 1117 | |
e31f9b82 | 1118 | if (stmpe->irq >= 0) { |
909582ca | 1119 | ret = stmpe_irq_init(stmpe, np); |
e31f9b82 | 1120 | if (ret) |
cb5faba9 | 1121 | return ret; |
27e34995 | 1122 | |
cb5faba9 VK |
1123 | ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL, |
1124 | stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT, | |
e31f9b82 CB |
1125 | "stmpe", stmpe); |
1126 | if (ret) { | |
1127 | dev_err(stmpe->dev, "failed to request IRQ: %d\n", | |
1128 | ret); | |
cb5faba9 | 1129 | return ret; |
e31f9b82 | 1130 | } |
27e34995 RV |
1131 | } |
1132 | ||
1133 | ret = stmpe_devices_init(stmpe); | |
cb5faba9 VK |
1134 | if (!ret) |
1135 | return 0; | |
27e34995 | 1136 | |
cb5faba9 | 1137 | dev_err(stmpe->dev, "failed to add children\n"); |
27e34995 | 1138 | mfd_remove_devices(stmpe->dev); |
cb5faba9 | 1139 | |
27e34995 RV |
1140 | return ret; |
1141 | } | |
1142 | ||
1a6e4b74 | 1143 | int stmpe_remove(struct stmpe *stmpe) |
27e34995 | 1144 | { |
27e34995 RV |
1145 | mfd_remove_devices(stmpe->dev); |
1146 | ||
27e34995 RV |
1147 | return 0; |
1148 | } | |
1149 | ||
208c4343 | 1150 | #ifdef CONFIG_PM |
1a6e4b74 VK |
1151 | static int stmpe_suspend(struct device *dev) |
1152 | { | |
1153 | struct stmpe *stmpe = dev_get_drvdata(dev); | |
208c4343 | 1154 | |
e31f9b82 | 1155 | if (stmpe->irq >= 0 && device_may_wakeup(dev)) |
1a6e4b74 | 1156 | enable_irq_wake(stmpe->irq); |
27e34995 | 1157 | |
1a6e4b74 | 1158 | return 0; |
27e34995 | 1159 | } |
27e34995 | 1160 | |
1a6e4b74 | 1161 | static int stmpe_resume(struct device *dev) |
27e34995 | 1162 | { |
1a6e4b74 VK |
1163 | struct stmpe *stmpe = dev_get_drvdata(dev); |
1164 | ||
e31f9b82 | 1165 | if (stmpe->irq >= 0 && device_may_wakeup(dev)) |
1a6e4b74 VK |
1166 | disable_irq_wake(stmpe->irq); |
1167 | ||
1168 | return 0; | |
27e34995 | 1169 | } |
27e34995 | 1170 | |
1a6e4b74 VK |
1171 | const struct dev_pm_ops stmpe_dev_pm_ops = { |
1172 | .suspend = stmpe_suspend, | |
1173 | .resume = stmpe_resume, | |
1174 | }; | |
1175 | #endif |