stmpe.c 31 KB

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