stmpe.c 27 KB

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