stmpe.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. * STMPE801
  275. */
  276. static const u8 stmpe801_regs[] = {
  277. [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
  278. [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
  279. [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
  280. [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  281. [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  282. [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
  283. [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
  284. [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
  285. };
  286. static struct stmpe_variant_block stmpe801_blocks[] = {
  287. {
  288. .cell = &stmpe_gpio_cell,
  289. .irq = 0,
  290. .block = STMPE_BLOCK_GPIO,
  291. },
  292. };
  293. static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
  294. {
  295. .cell = &stmpe_gpio_cell_noirq,
  296. .block = STMPE_BLOCK_GPIO,
  297. },
  298. };
  299. static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
  300. bool enable)
  301. {
  302. if (blocks & STMPE_BLOCK_GPIO)
  303. return 0;
  304. else
  305. return -EINVAL;
  306. }
  307. static struct stmpe_variant_info stmpe801 = {
  308. .name = "stmpe801",
  309. .id_val = STMPE801_ID,
  310. .id_mask = 0xffff,
  311. .num_gpios = 8,
  312. .regs = stmpe801_regs,
  313. .blocks = stmpe801_blocks,
  314. .num_blocks = ARRAY_SIZE(stmpe801_blocks),
  315. .num_irqs = STMPE801_NR_INTERNAL_IRQS,
  316. .enable = stmpe801_enable,
  317. };
  318. static struct stmpe_variant_info stmpe801_noirq = {
  319. .name = "stmpe801",
  320. .id_val = STMPE801_ID,
  321. .id_mask = 0xffff,
  322. .num_gpios = 8,
  323. .regs = stmpe801_regs,
  324. .blocks = stmpe801_blocks_noirq,
  325. .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
  326. .enable = stmpe801_enable,
  327. };
  328. /*
  329. * Touchscreen (STMPE811 or STMPE610)
  330. */
  331. static struct resource stmpe_ts_resources[] = {
  332. {
  333. .name = "TOUCH_DET",
  334. .flags = IORESOURCE_IRQ,
  335. },
  336. {
  337. .name = "FIFO_TH",
  338. .flags = IORESOURCE_IRQ,
  339. },
  340. };
  341. static const struct mfd_cell stmpe_ts_cell = {
  342. .name = "stmpe-ts",
  343. .of_compatible = "st,stmpe-ts",
  344. .resources = stmpe_ts_resources,
  345. .num_resources = ARRAY_SIZE(stmpe_ts_resources),
  346. };
  347. /*
  348. * STMPE811 or STMPE610
  349. */
  350. static const u8 stmpe811_regs[] = {
  351. [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
  352. [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
  353. [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
  354. [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
  355. [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
  356. [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
  357. [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
  358. [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
  359. [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
  360. [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
  361. [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
  362. [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
  363. [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
  364. [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
  365. };
  366. static struct stmpe_variant_block stmpe811_blocks[] = {
  367. {
  368. .cell = &stmpe_gpio_cell,
  369. .irq = STMPE811_IRQ_GPIOC,
  370. .block = STMPE_BLOCK_GPIO,
  371. },
  372. {
  373. .cell = &stmpe_ts_cell,
  374. .irq = STMPE811_IRQ_TOUCH_DET,
  375. .block = STMPE_BLOCK_TOUCHSCREEN,
  376. },
  377. };
  378. static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
  379. bool enable)
  380. {
  381. unsigned int mask = 0;
  382. if (blocks & STMPE_BLOCK_GPIO)
  383. mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
  384. if (blocks & STMPE_BLOCK_ADC)
  385. mask |= STMPE811_SYS_CTRL2_ADC_OFF;
  386. if (blocks & STMPE_BLOCK_TOUCHSCREEN)
  387. mask |= STMPE811_SYS_CTRL2_TSC_OFF;
  388. return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
  389. enable ? 0 : mask);
  390. }
  391. static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  392. {
  393. /* 0 for touchscreen, 1 for GPIO */
  394. return block != STMPE_BLOCK_TOUCHSCREEN;
  395. }
  396. static struct stmpe_variant_info stmpe811 = {
  397. .name = "stmpe811",
  398. .id_val = 0x0811,
  399. .id_mask = 0xffff,
  400. .num_gpios = 8,
  401. .af_bits = 1,
  402. .regs = stmpe811_regs,
  403. .blocks = stmpe811_blocks,
  404. .num_blocks = ARRAY_SIZE(stmpe811_blocks),
  405. .num_irqs = STMPE811_NR_INTERNAL_IRQS,
  406. .enable = stmpe811_enable,
  407. .get_altfunc = stmpe811_get_altfunc,
  408. };
  409. /* Similar to 811, except number of gpios */
  410. static struct stmpe_variant_info stmpe610 = {
  411. .name = "stmpe610",
  412. .id_val = 0x0811,
  413. .id_mask = 0xffff,
  414. .num_gpios = 6,
  415. .af_bits = 1,
  416. .regs = stmpe811_regs,
  417. .blocks = stmpe811_blocks,
  418. .num_blocks = ARRAY_SIZE(stmpe811_blocks),
  419. .num_irqs = STMPE811_NR_INTERNAL_IRQS,
  420. .enable = stmpe811_enable,
  421. .get_altfunc = stmpe811_get_altfunc,
  422. };
  423. /*
  424. * STMPE1601
  425. */
  426. static const u8 stmpe1601_regs[] = {
  427. [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
  428. [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
  429. [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
  430. [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
  431. [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
  432. [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
  433. [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
  434. [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
  435. [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
  436. [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
  437. [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
  438. [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
  439. [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
  440. [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
  441. };
  442. static struct stmpe_variant_block stmpe1601_blocks[] = {
  443. {
  444. .cell = &stmpe_gpio_cell,
  445. .irq = STMPE1601_IRQ_GPIOC,
  446. .block = STMPE_BLOCK_GPIO,
  447. },
  448. {
  449. .cell = &stmpe_keypad_cell,
  450. .irq = STMPE1601_IRQ_KEYPAD,
  451. .block = STMPE_BLOCK_KEYPAD,
  452. },
  453. };
  454. /* supported autosleep timeout delay (in msecs) */
  455. static const int stmpe_autosleep_delay[] = {
  456. 4, 16, 32, 64, 128, 256, 512, 1024,
  457. };
  458. static int stmpe_round_timeout(int timeout)
  459. {
  460. int i;
  461. for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
  462. if (stmpe_autosleep_delay[i] >= timeout)
  463. return i;
  464. }
  465. /*
  466. * requests for delays longer than supported should not return the
  467. * longest supported delay
  468. */
  469. return -EINVAL;
  470. }
  471. static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
  472. {
  473. int ret;
  474. if (!stmpe->variant->enable_autosleep)
  475. return -ENOSYS;
  476. mutex_lock(&stmpe->lock);
  477. ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
  478. mutex_unlock(&stmpe->lock);
  479. return ret;
  480. }
  481. /*
  482. * Both stmpe 1601/2403 support same layout for autosleep
  483. */
  484. static int stmpe1601_autosleep(struct stmpe *stmpe,
  485. int autosleep_timeout)
  486. {
  487. int ret, timeout;
  488. /* choose the best available timeout */
  489. timeout = stmpe_round_timeout(autosleep_timeout);
  490. if (timeout < 0) {
  491. dev_err(stmpe->dev, "invalid timeout\n");
  492. return timeout;
  493. }
  494. ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
  495. STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
  496. timeout);
  497. if (ret < 0)
  498. return ret;
  499. return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
  500. STPME1601_AUTOSLEEP_ENABLE,
  501. STPME1601_AUTOSLEEP_ENABLE);
  502. }
  503. static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
  504. bool enable)
  505. {
  506. unsigned int mask = 0;
  507. if (blocks & STMPE_BLOCK_GPIO)
  508. mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
  509. else
  510. mask &= ~STMPE1601_SYS_CTRL_ENABLE_GPIO;
  511. if (blocks & STMPE_BLOCK_KEYPAD)
  512. mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
  513. else
  514. mask &= ~STMPE1601_SYS_CTRL_ENABLE_KPC;
  515. if (blocks & STMPE_BLOCK_PWM)
  516. mask |= STMPE1601_SYS_CTRL_ENABLE_SPWM;
  517. else
  518. mask &= ~STMPE1601_SYS_CTRL_ENABLE_SPWM;
  519. return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
  520. enable ? mask : 0);
  521. }
  522. static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  523. {
  524. switch (block) {
  525. case STMPE_BLOCK_PWM:
  526. return 2;
  527. case STMPE_BLOCK_KEYPAD:
  528. return 1;
  529. case STMPE_BLOCK_GPIO:
  530. default:
  531. return 0;
  532. }
  533. }
  534. static struct stmpe_variant_info stmpe1601 = {
  535. .name = "stmpe1601",
  536. .id_val = 0x0210,
  537. .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
  538. .num_gpios = 16,
  539. .af_bits = 2,
  540. .regs = stmpe1601_regs,
  541. .blocks = stmpe1601_blocks,
  542. .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
  543. .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
  544. .enable = stmpe1601_enable,
  545. .get_altfunc = stmpe1601_get_altfunc,
  546. .enable_autosleep = stmpe1601_autosleep,
  547. };
  548. /*
  549. * STMPE1801
  550. */
  551. static const u8 stmpe1801_regs[] = {
  552. [STMPE_IDX_CHIP_ID] = STMPE1801_REG_CHIP_ID,
  553. [STMPE_IDX_ICR_LSB] = STMPE1801_REG_INT_CTRL_LOW,
  554. [STMPE_IDX_IER_LSB] = STMPE1801_REG_INT_EN_MASK_LOW,
  555. [STMPE_IDX_ISR_LSB] = STMPE1801_REG_INT_STA_LOW,
  556. [STMPE_IDX_GPMR_LSB] = STMPE1801_REG_GPIO_MP_LOW,
  557. [STMPE_IDX_GPSR_LSB] = STMPE1801_REG_GPIO_SET_LOW,
  558. [STMPE_IDX_GPCR_LSB] = STMPE1801_REG_GPIO_CLR_LOW,
  559. [STMPE_IDX_GPDR_LSB] = STMPE1801_REG_GPIO_SET_DIR_LOW,
  560. [STMPE_IDX_GPRER_LSB] = STMPE1801_REG_GPIO_RE_LOW,
  561. [STMPE_IDX_GPFER_LSB] = STMPE1801_REG_GPIO_FE_LOW,
  562. [STMPE_IDX_IEGPIOR_LSB] = STMPE1801_REG_INT_EN_GPIO_MASK_LOW,
  563. [STMPE_IDX_ISGPIOR_LSB] = STMPE1801_REG_INT_STA_GPIO_LOW,
  564. };
  565. static struct stmpe_variant_block stmpe1801_blocks[] = {
  566. {
  567. .cell = &stmpe_gpio_cell,
  568. .irq = STMPE1801_IRQ_GPIOC,
  569. .block = STMPE_BLOCK_GPIO,
  570. },
  571. {
  572. .cell = &stmpe_keypad_cell,
  573. .irq = STMPE1801_IRQ_KEYPAD,
  574. .block = STMPE_BLOCK_KEYPAD,
  575. },
  576. };
  577. static int stmpe1801_enable(struct stmpe *stmpe, unsigned int blocks,
  578. bool enable)
  579. {
  580. unsigned int mask = 0;
  581. if (blocks & STMPE_BLOCK_GPIO)
  582. mask |= STMPE1801_MSK_INT_EN_GPIO;
  583. if (blocks & STMPE_BLOCK_KEYPAD)
  584. mask |= STMPE1801_MSK_INT_EN_KPC;
  585. return __stmpe_set_bits(stmpe, STMPE1801_REG_INT_EN_MASK_LOW, mask,
  586. enable ? mask : 0);
  587. }
  588. static int stmpe1801_reset(struct stmpe *stmpe)
  589. {
  590. unsigned long timeout;
  591. int ret = 0;
  592. ret = __stmpe_set_bits(stmpe, STMPE1801_REG_SYS_CTRL,
  593. STMPE1801_MSK_SYS_CTRL_RESET, STMPE1801_MSK_SYS_CTRL_RESET);
  594. if (ret < 0)
  595. return ret;
  596. timeout = jiffies + msecs_to_jiffies(100);
  597. while (time_before(jiffies, timeout)) {
  598. ret = __stmpe_reg_read(stmpe, STMPE1801_REG_SYS_CTRL);
  599. if (ret < 0)
  600. return ret;
  601. if (!(ret & STMPE1801_MSK_SYS_CTRL_RESET))
  602. return 0;
  603. usleep_range(100, 200);
  604. }
  605. return -EIO;
  606. }
  607. static struct stmpe_variant_info stmpe1801 = {
  608. .name = "stmpe1801",
  609. .id_val = STMPE1801_ID,
  610. .id_mask = 0xfff0,
  611. .num_gpios = 18,
  612. .af_bits = 0,
  613. .regs = stmpe1801_regs,
  614. .blocks = stmpe1801_blocks,
  615. .num_blocks = ARRAY_SIZE(stmpe1801_blocks),
  616. .num_irqs = STMPE1801_NR_INTERNAL_IRQS,
  617. .enable = stmpe1801_enable,
  618. /* stmpe1801 do not have any gpio alternate function */
  619. .get_altfunc = NULL,
  620. };
  621. /*
  622. * STMPE24XX
  623. */
  624. static const u8 stmpe24xx_regs[] = {
  625. [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
  626. [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
  627. [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
  628. [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
  629. [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
  630. [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
  631. [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
  632. [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
  633. [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
  634. [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
  635. [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
  636. [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
  637. [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
  638. [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
  639. };
  640. static struct stmpe_variant_block stmpe24xx_blocks[] = {
  641. {
  642. .cell = &stmpe_gpio_cell,
  643. .irq = STMPE24XX_IRQ_GPIOC,
  644. .block = STMPE_BLOCK_GPIO,
  645. },
  646. {
  647. .cell = &stmpe_keypad_cell,
  648. .irq = STMPE24XX_IRQ_KEYPAD,
  649. .block = STMPE_BLOCK_KEYPAD,
  650. },
  651. };
  652. static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
  653. bool enable)
  654. {
  655. unsigned int mask = 0;
  656. if (blocks & STMPE_BLOCK_GPIO)
  657. mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
  658. if (blocks & STMPE_BLOCK_KEYPAD)
  659. mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
  660. return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
  661. enable ? mask : 0);
  662. }
  663. static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  664. {
  665. switch (block) {
  666. case STMPE_BLOCK_ROTATOR:
  667. return 2;
  668. case STMPE_BLOCK_KEYPAD:
  669. return 1;
  670. case STMPE_BLOCK_GPIO:
  671. default:
  672. return 0;
  673. }
  674. }
  675. static struct stmpe_variant_info stmpe2401 = {
  676. .name = "stmpe2401",
  677. .id_val = 0x0101,
  678. .id_mask = 0xffff,
  679. .num_gpios = 24,
  680. .af_bits = 2,
  681. .regs = stmpe24xx_regs,
  682. .blocks = stmpe24xx_blocks,
  683. .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
  684. .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
  685. .enable = stmpe24xx_enable,
  686. .get_altfunc = stmpe24xx_get_altfunc,
  687. };
  688. static struct stmpe_variant_info stmpe2403 = {
  689. .name = "stmpe2403",
  690. .id_val = 0x0120,
  691. .id_mask = 0xffff,
  692. .num_gpios = 24,
  693. .af_bits = 2,
  694. .regs = stmpe24xx_regs,
  695. .blocks = stmpe24xx_blocks,
  696. .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
  697. .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
  698. .enable = stmpe24xx_enable,
  699. .get_altfunc = stmpe24xx_get_altfunc,
  700. .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
  701. };
  702. static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
  703. [STMPE610] = &stmpe610,
  704. [STMPE801] = &stmpe801,
  705. [STMPE811] = &stmpe811,
  706. [STMPE1601] = &stmpe1601,
  707. [STMPE1801] = &stmpe1801,
  708. [STMPE2401] = &stmpe2401,
  709. [STMPE2403] = &stmpe2403,
  710. };
  711. /*
  712. * These devices can be connected in a 'no-irq' configuration - the irq pin
  713. * is not used and the device cannot interrupt the CPU. Here we only list
  714. * devices which support this configuration - the driver will fail probing
  715. * for any devices not listed here which are configured in this way.
  716. */
  717. static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
  718. [STMPE801] = &stmpe801_noirq,
  719. };
  720. static irqreturn_t stmpe_irq(int irq, void *data)
  721. {
  722. struct stmpe *stmpe = data;
  723. struct stmpe_variant_info *variant = stmpe->variant;
  724. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  725. u8 israddr;
  726. u8 isr[3];
  727. int ret;
  728. int i;
  729. if (variant->id_val == STMPE801_ID) {
  730. int base = irq_create_mapping(stmpe->domain, 0);
  731. handle_nested_irq(base);
  732. return IRQ_HANDLED;
  733. }
  734. if (variant->id_val == STMPE1801_ID)
  735. israddr = stmpe->regs[STMPE_IDX_ISR_LSB];
  736. else
  737. israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
  738. ret = stmpe_block_read(stmpe, israddr, num, isr);
  739. if (ret < 0)
  740. return IRQ_NONE;
  741. for (i = 0; i < num; i++) {
  742. int bank = num - i - 1;
  743. u8 status = isr[i];
  744. u8 clear;
  745. status &= stmpe->ier[bank];
  746. if (!status)
  747. continue;
  748. clear = status;
  749. while (status) {
  750. int bit = __ffs(status);
  751. int line = bank * 8 + bit;
  752. int nestedirq = irq_create_mapping(stmpe->domain, line);
  753. handle_nested_irq(nestedirq);
  754. status &= ~(1 << bit);
  755. }
  756. stmpe_reg_write(stmpe, israddr + i, clear);
  757. }
  758. return IRQ_HANDLED;
  759. }
  760. static void stmpe_irq_lock(struct irq_data *data)
  761. {
  762. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  763. mutex_lock(&stmpe->irq_lock);
  764. }
  765. static void stmpe_irq_sync_unlock(struct irq_data *data)
  766. {
  767. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  768. struct stmpe_variant_info *variant = stmpe->variant;
  769. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  770. int i;
  771. for (i = 0; i < num; i++) {
  772. u8 new = stmpe->ier[i];
  773. u8 old = stmpe->oldier[i];
  774. if (new == old)
  775. continue;
  776. stmpe->oldier[i] = new;
  777. stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
  778. }
  779. mutex_unlock(&stmpe->irq_lock);
  780. }
  781. static void stmpe_irq_mask(struct irq_data *data)
  782. {
  783. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  784. int offset = data->hwirq;
  785. int regoffset = offset / 8;
  786. int mask = 1 << (offset % 8);
  787. stmpe->ier[regoffset] &= ~mask;
  788. }
  789. static void stmpe_irq_unmask(struct irq_data *data)
  790. {
  791. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  792. int offset = data->hwirq;
  793. int regoffset = offset / 8;
  794. int mask = 1 << (offset % 8);
  795. stmpe->ier[regoffset] |= mask;
  796. }
  797. static struct irq_chip stmpe_irq_chip = {
  798. .name = "stmpe",
  799. .irq_bus_lock = stmpe_irq_lock,
  800. .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
  801. .irq_mask = stmpe_irq_mask,
  802. .irq_unmask = stmpe_irq_unmask,
  803. };
  804. static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
  805. irq_hw_number_t hwirq)
  806. {
  807. struct stmpe *stmpe = d->host_data;
  808. struct irq_chip *chip = NULL;
  809. if (stmpe->variant->id_val != STMPE801_ID)
  810. chip = &stmpe_irq_chip;
  811. irq_set_chip_data(virq, stmpe);
  812. irq_set_chip_and_handler(virq, chip, handle_edge_irq);
  813. irq_set_nested_thread(virq, 1);
  814. #ifdef CONFIG_ARM
  815. set_irq_flags(virq, IRQF_VALID);
  816. #else
  817. irq_set_noprobe(virq);
  818. #endif
  819. return 0;
  820. }
  821. static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
  822. {
  823. #ifdef CONFIG_ARM
  824. set_irq_flags(virq, 0);
  825. #endif
  826. irq_set_chip_and_handler(virq, NULL, NULL);
  827. irq_set_chip_data(virq, NULL);
  828. }
  829. static struct irq_domain_ops stmpe_irq_ops = {
  830. .map = stmpe_irq_map,
  831. .unmap = stmpe_irq_unmap,
  832. .xlate = irq_domain_xlate_twocell,
  833. };
  834. static int stmpe_irq_init(struct stmpe *stmpe, struct device_node *np)
  835. {
  836. int base = 0;
  837. int num_irqs = stmpe->variant->num_irqs;
  838. stmpe->domain = irq_domain_add_simple(np, num_irqs, base,
  839. &stmpe_irq_ops, stmpe);
  840. if (!stmpe->domain) {
  841. dev_err(stmpe->dev, "Failed to create irqdomain\n");
  842. return -ENOSYS;
  843. }
  844. return 0;
  845. }
  846. static int stmpe_chip_init(struct stmpe *stmpe)
  847. {
  848. unsigned int irq_trigger = stmpe->pdata->irq_trigger;
  849. int autosleep_timeout = stmpe->pdata->autosleep_timeout;
  850. struct stmpe_variant_info *variant = stmpe->variant;
  851. u8 icr = 0;
  852. unsigned int id;
  853. u8 data[2];
  854. int ret;
  855. ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
  856. ARRAY_SIZE(data), data);
  857. if (ret < 0)
  858. return ret;
  859. id = (data[0] << 8) | data[1];
  860. if ((id & variant->id_mask) != variant->id_val) {
  861. dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
  862. return -EINVAL;
  863. }
  864. dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
  865. /* Disable all modules -- subdrivers should enable what they need. */
  866. ret = stmpe_disable(stmpe, ~0);
  867. if (ret)
  868. return ret;
  869. if (id == STMPE1801_ID) {
  870. ret = stmpe1801_reset(stmpe);
  871. if (ret < 0)
  872. return ret;
  873. }
  874. if (stmpe->irq >= 0) {
  875. if (id == STMPE801_ID)
  876. icr = STMPE801_REG_SYS_CTRL_INT_EN;
  877. else
  878. icr = STMPE_ICR_LSB_GIM;
  879. /* STMPE801 doesn't support Edge interrupts */
  880. if (id != STMPE801_ID) {
  881. if (irq_trigger == IRQF_TRIGGER_FALLING ||
  882. irq_trigger == IRQF_TRIGGER_RISING)
  883. icr |= STMPE_ICR_LSB_EDGE;
  884. }
  885. if (irq_trigger == IRQF_TRIGGER_RISING ||
  886. irq_trigger == IRQF_TRIGGER_HIGH) {
  887. if (id == STMPE801_ID)
  888. icr |= STMPE801_REG_SYS_CTRL_INT_HI;
  889. else
  890. icr |= STMPE_ICR_LSB_HIGH;
  891. }
  892. }
  893. if (stmpe->pdata->autosleep) {
  894. ret = stmpe_autosleep(stmpe, autosleep_timeout);
  895. if (ret)
  896. return ret;
  897. }
  898. return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
  899. }
  900. static int stmpe_add_device(struct stmpe *stmpe, const struct mfd_cell *cell)
  901. {
  902. return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
  903. NULL, 0, stmpe->domain);
  904. }
  905. static int stmpe_devices_init(struct stmpe *stmpe)
  906. {
  907. struct stmpe_variant_info *variant = stmpe->variant;
  908. unsigned int platform_blocks = stmpe->pdata->blocks;
  909. int ret = -EINVAL;
  910. int i, j;
  911. for (i = 0; i < variant->num_blocks; i++) {
  912. struct stmpe_variant_block *block = &variant->blocks[i];
  913. if (!(platform_blocks & block->block))
  914. continue;
  915. for (j = 0; j < block->cell->num_resources; j++) {
  916. struct resource *res =
  917. (struct resource *) &block->cell->resources[j];
  918. /* Dynamically fill in a variant's IRQ. */
  919. if (res->flags & IORESOURCE_IRQ)
  920. res->start = res->end = block->irq + j;
  921. }
  922. platform_blocks &= ~block->block;
  923. ret = stmpe_add_device(stmpe, block->cell);
  924. if (ret)
  925. return ret;
  926. }
  927. if (platform_blocks)
  928. dev_warn(stmpe->dev,
  929. "platform wants blocks (%#x) not present on variant",
  930. platform_blocks);
  931. return ret;
  932. }
  933. static void stmpe_of_probe(struct stmpe_platform_data *pdata,
  934. struct device_node *np)
  935. {
  936. struct device_node *child;
  937. pdata->id = of_alias_get_id(np, "stmpe-i2c");
  938. if (pdata->id < 0)
  939. pdata->id = -1;
  940. pdata->irq_gpio = of_get_named_gpio_flags(np, "irq-gpio", 0,
  941. &pdata->irq_trigger);
  942. if (gpio_is_valid(pdata->irq_gpio))
  943. pdata->irq_over_gpio = 1;
  944. else
  945. pdata->irq_trigger = IRQF_TRIGGER_NONE;
  946. of_property_read_u32(np, "st,autosleep-timeout",
  947. &pdata->autosleep_timeout);
  948. pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
  949. for_each_child_of_node(np, child) {
  950. if (!strcmp(child->name, "stmpe_gpio")) {
  951. pdata->blocks |= STMPE_BLOCK_GPIO;
  952. } else if (!strcmp(child->name, "stmpe_keypad")) {
  953. pdata->blocks |= STMPE_BLOCK_KEYPAD;
  954. } else if (!strcmp(child->name, "stmpe_touchscreen")) {
  955. pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
  956. } else if (!strcmp(child->name, "stmpe_adc")) {
  957. pdata->blocks |= STMPE_BLOCK_ADC;
  958. } else if (!strcmp(child->name, "stmpe_pwm")) {
  959. pdata->blocks |= STMPE_BLOCK_PWM;
  960. } else if (!strcmp(child->name, "stmpe_rotator")) {
  961. pdata->blocks |= STMPE_BLOCK_ROTATOR;
  962. }
  963. }
  964. }
  965. /* Called from client specific probe routines */
  966. int stmpe_probe(struct stmpe_client_info *ci, enum stmpe_partnum partnum)
  967. {
  968. struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
  969. struct device_node *np = ci->dev->of_node;
  970. struct stmpe *stmpe;
  971. int ret;
  972. if (!pdata) {
  973. if (!np)
  974. return -EINVAL;
  975. pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
  976. if (!pdata)
  977. return -ENOMEM;
  978. stmpe_of_probe(pdata, np);
  979. if (of_find_property(np, "interrupts", NULL) == NULL)
  980. ci->irq = -1;
  981. }
  982. stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
  983. if (!stmpe)
  984. return -ENOMEM;
  985. mutex_init(&stmpe->irq_lock);
  986. mutex_init(&stmpe->lock);
  987. stmpe->dev = ci->dev;
  988. stmpe->client = ci->client;
  989. stmpe->pdata = pdata;
  990. stmpe->ci = ci;
  991. stmpe->partnum = partnum;
  992. stmpe->variant = stmpe_variant_info[partnum];
  993. stmpe->regs = stmpe->variant->regs;
  994. stmpe->num_gpios = stmpe->variant->num_gpios;
  995. stmpe->vcc = devm_regulator_get_optional(ci->dev, "vcc");
  996. if (!IS_ERR(stmpe->vcc)) {
  997. ret = regulator_enable(stmpe->vcc);
  998. if (ret)
  999. dev_warn(ci->dev, "failed to enable VCC supply\n");
  1000. }
  1001. stmpe->vio = devm_regulator_get_optional(ci->dev, "vio");
  1002. if (!IS_ERR(stmpe->vio)) {
  1003. ret = regulator_enable(stmpe->vio);
  1004. if (ret)
  1005. dev_warn(ci->dev, "failed to enable VIO supply\n");
  1006. }
  1007. dev_set_drvdata(stmpe->dev, stmpe);
  1008. if (ci->init)
  1009. ci->init(stmpe);
  1010. if (pdata->irq_over_gpio) {
  1011. ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
  1012. GPIOF_DIR_IN, "stmpe");
  1013. if (ret) {
  1014. dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
  1015. ret);
  1016. return ret;
  1017. }
  1018. stmpe->irq = gpio_to_irq(pdata->irq_gpio);
  1019. } else {
  1020. stmpe->irq = ci->irq;
  1021. }
  1022. if (stmpe->irq < 0) {
  1023. /* use alternate variant info for no-irq mode, if supported */
  1024. dev_info(stmpe->dev,
  1025. "%s configured in no-irq mode by platform data\n",
  1026. stmpe->variant->name);
  1027. if (!stmpe_noirq_variant_info[stmpe->partnum]) {
  1028. dev_err(stmpe->dev,
  1029. "%s does not support no-irq mode!\n",
  1030. stmpe->variant->name);
  1031. return -ENODEV;
  1032. }
  1033. stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
  1034. } else if (pdata->irq_trigger == IRQF_TRIGGER_NONE) {
  1035. pdata->irq_trigger = irq_get_trigger_type(stmpe->irq);
  1036. }
  1037. ret = stmpe_chip_init(stmpe);
  1038. if (ret)
  1039. return ret;
  1040. if (stmpe->irq >= 0) {
  1041. ret = stmpe_irq_init(stmpe, np);
  1042. if (ret)
  1043. return ret;
  1044. ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
  1045. stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
  1046. "stmpe", stmpe);
  1047. if (ret) {
  1048. dev_err(stmpe->dev, "failed to request IRQ: %d\n",
  1049. ret);
  1050. return ret;
  1051. }
  1052. }
  1053. ret = stmpe_devices_init(stmpe);
  1054. if (!ret)
  1055. return 0;
  1056. dev_err(stmpe->dev, "failed to add children\n");
  1057. mfd_remove_devices(stmpe->dev);
  1058. return ret;
  1059. }
  1060. int stmpe_remove(struct stmpe *stmpe)
  1061. {
  1062. if (!IS_ERR(stmpe->vio))
  1063. regulator_disable(stmpe->vio);
  1064. if (!IS_ERR(stmpe->vcc))
  1065. regulator_disable(stmpe->vcc);
  1066. mfd_remove_devices(stmpe->dev);
  1067. return 0;
  1068. }
  1069. #ifdef CONFIG_PM
  1070. static int stmpe_suspend(struct device *dev)
  1071. {
  1072. struct stmpe *stmpe = dev_get_drvdata(dev);
  1073. if (stmpe->irq >= 0 && device_may_wakeup(dev))
  1074. enable_irq_wake(stmpe->irq);
  1075. return 0;
  1076. }
  1077. static int stmpe_resume(struct device *dev)
  1078. {
  1079. struct stmpe *stmpe = dev_get_drvdata(dev);
  1080. if (stmpe->irq >= 0 && device_may_wakeup(dev))
  1081. disable_irq_wake(stmpe->irq);
  1082. return 0;
  1083. }
  1084. const struct dev_pm_ops stmpe_dev_pm_ops = {
  1085. .suspend = stmpe_suspend,
  1086. .resume = stmpe_resume,
  1087. };
  1088. #endif