stmpe.c 30 KB

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