stmpe.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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[numregs];
  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[num];
  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_trigger = IRQF_TRIGGER_NONE;
  941. of_property_read_u32(np, "st,autosleep-timeout",
  942. &pdata->autosleep_timeout);
  943. pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
  944. for_each_child_of_node(np, child) {
  945. if (!strcmp(child->name, "stmpe_gpio")) {
  946. pdata->blocks |= STMPE_BLOCK_GPIO;
  947. } else if (!strcmp(child->name, "stmpe_keypad")) {
  948. pdata->blocks |= STMPE_BLOCK_KEYPAD;
  949. } else if (!strcmp(child->name, "stmpe_touchscreen")) {
  950. pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
  951. } else if (!strcmp(child->name, "stmpe_adc")) {
  952. pdata->blocks |= STMPE_BLOCK_ADC;
  953. } else if (!strcmp(child->name, "stmpe_pwm")) {
  954. pdata->blocks |= STMPE_BLOCK_PWM;
  955. } else if (!strcmp(child->name, "stmpe_rotator")) {
  956. pdata->blocks |= STMPE_BLOCK_ROTATOR;
  957. }
  958. }
  959. }
  960. /* Called from client specific probe routines */
  961. int stmpe_probe(struct stmpe_client_info *ci, int partnum)
  962. {
  963. struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
  964. struct device_node *np = ci->dev->of_node;
  965. struct stmpe *stmpe;
  966. int ret;
  967. if (!pdata) {
  968. if (!np)
  969. return -EINVAL;
  970. pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
  971. if (!pdata)
  972. return -ENOMEM;
  973. stmpe_of_probe(pdata, np);
  974. if (of_find_property(np, "interrupts", NULL) == NULL)
  975. ci->irq = -1;
  976. }
  977. stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
  978. if (!stmpe)
  979. return -ENOMEM;
  980. mutex_init(&stmpe->irq_lock);
  981. mutex_init(&stmpe->lock);
  982. stmpe->dev = ci->dev;
  983. stmpe->client = ci->client;
  984. stmpe->pdata = pdata;
  985. stmpe->ci = ci;
  986. stmpe->partnum = partnum;
  987. stmpe->variant = stmpe_variant_info[partnum];
  988. stmpe->regs = stmpe->variant->regs;
  989. stmpe->num_gpios = stmpe->variant->num_gpios;
  990. stmpe->vcc = devm_regulator_get_optional(ci->dev, "vcc");
  991. if (!IS_ERR(stmpe->vcc)) {
  992. ret = regulator_enable(stmpe->vcc);
  993. if (ret)
  994. dev_warn(ci->dev, "failed to enable VCC supply\n");
  995. }
  996. stmpe->vio = devm_regulator_get_optional(ci->dev, "vio");
  997. if (!IS_ERR(stmpe->vio)) {
  998. ret = regulator_enable(stmpe->vio);
  999. if (ret)
  1000. dev_warn(ci->dev, "failed to enable VIO supply\n");
  1001. }
  1002. dev_set_drvdata(stmpe->dev, stmpe);
  1003. if (ci->init)
  1004. ci->init(stmpe);
  1005. if (pdata->irq_over_gpio) {
  1006. ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
  1007. GPIOF_DIR_IN, "stmpe");
  1008. if (ret) {
  1009. dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
  1010. ret);
  1011. return ret;
  1012. }
  1013. stmpe->irq = gpio_to_irq(pdata->irq_gpio);
  1014. } else {
  1015. stmpe->irq = ci->irq;
  1016. }
  1017. if (stmpe->irq < 0) {
  1018. /* use alternate variant info for no-irq mode, if supported */
  1019. dev_info(stmpe->dev,
  1020. "%s configured in no-irq mode by platform data\n",
  1021. stmpe->variant->name);
  1022. if (!stmpe_noirq_variant_info[stmpe->partnum]) {
  1023. dev_err(stmpe->dev,
  1024. "%s does not support no-irq mode!\n",
  1025. stmpe->variant->name);
  1026. return -ENODEV;
  1027. }
  1028. stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
  1029. } else if (pdata->irq_trigger == IRQF_TRIGGER_NONE) {
  1030. pdata->irq_trigger = irq_get_trigger_type(stmpe->irq);
  1031. }
  1032. ret = stmpe_chip_init(stmpe);
  1033. if (ret)
  1034. return ret;
  1035. if (stmpe->irq >= 0) {
  1036. ret = stmpe_irq_init(stmpe, np);
  1037. if (ret)
  1038. return ret;
  1039. ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
  1040. stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
  1041. "stmpe", stmpe);
  1042. if (ret) {
  1043. dev_err(stmpe->dev, "failed to request IRQ: %d\n",
  1044. ret);
  1045. return ret;
  1046. }
  1047. }
  1048. ret = stmpe_devices_init(stmpe);
  1049. if (!ret)
  1050. return 0;
  1051. dev_err(stmpe->dev, "failed to add children\n");
  1052. mfd_remove_devices(stmpe->dev);
  1053. return ret;
  1054. }
  1055. int stmpe_remove(struct stmpe *stmpe)
  1056. {
  1057. if (!IS_ERR(stmpe->vio))
  1058. regulator_disable(stmpe->vio);
  1059. if (!IS_ERR(stmpe->vcc))
  1060. regulator_disable(stmpe->vcc);
  1061. mfd_remove_devices(stmpe->dev);
  1062. return 0;
  1063. }
  1064. #ifdef CONFIG_PM
  1065. static int stmpe_suspend(struct device *dev)
  1066. {
  1067. struct stmpe *stmpe = dev_get_drvdata(dev);
  1068. if (stmpe->irq >= 0 && device_may_wakeup(dev))
  1069. enable_irq_wake(stmpe->irq);
  1070. return 0;
  1071. }
  1072. static int stmpe_resume(struct device *dev)
  1073. {
  1074. struct stmpe *stmpe = dev_get_drvdata(dev);
  1075. if (stmpe->irq >= 0 && device_may_wakeup(dev))
  1076. disable_irq_wake(stmpe->irq);
  1077. return 0;
  1078. }
  1079. const struct dev_pm_ops stmpe_dev_pm_ops = {
  1080. .suspend = stmpe_suspend,
  1081. .resume = stmpe_resume,
  1082. };
  1083. #endif