stmpe.c 31 KB

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