stmpe.c 36 KB

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