stmpe.c 27 KB

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