stmpe.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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/gpio.h>
  10. #include <linux/export.h>
  11. #include <linux/kernel.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/pm.h>
  15. #include <linux/slab.h>
  16. #include <linux/mfd/core.h>
  17. #include "stmpe.h"
  18. static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
  19. {
  20. return stmpe->variant->enable(stmpe, blocks, true);
  21. }
  22. static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
  23. {
  24. return stmpe->variant->enable(stmpe, blocks, false);
  25. }
  26. static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
  27. {
  28. int ret;
  29. ret = stmpe->ci->read_byte(stmpe, reg);
  30. if (ret < 0)
  31. dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
  32. dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
  33. return ret;
  34. }
  35. static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
  36. {
  37. int ret;
  38. dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
  39. ret = stmpe->ci->write_byte(stmpe, reg, val);
  40. if (ret < 0)
  41. dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
  42. return ret;
  43. }
  44. static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
  45. {
  46. int ret;
  47. ret = __stmpe_reg_read(stmpe, reg);
  48. if (ret < 0)
  49. return ret;
  50. ret &= ~mask;
  51. ret |= val;
  52. return __stmpe_reg_write(stmpe, reg, ret);
  53. }
  54. static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
  55. u8 *values)
  56. {
  57. int ret;
  58. ret = stmpe->ci->read_block(stmpe, reg, length, values);
  59. if (ret < 0)
  60. dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
  61. dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
  62. stmpe_dump_bytes("stmpe rd: ", values, length);
  63. return ret;
  64. }
  65. static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
  66. const u8 *values)
  67. {
  68. int ret;
  69. dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
  70. stmpe_dump_bytes("stmpe wr: ", values, length);
  71. ret = stmpe->ci->write_block(stmpe, reg, length, values);
  72. if (ret < 0)
  73. dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
  74. return ret;
  75. }
  76. /**
  77. * stmpe_enable - enable blocks on an STMPE device
  78. * @stmpe: Device to work on
  79. * @blocks: Mask of blocks (enum stmpe_block values) to enable
  80. */
  81. int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
  82. {
  83. int ret;
  84. mutex_lock(&stmpe->lock);
  85. ret = __stmpe_enable(stmpe, blocks);
  86. mutex_unlock(&stmpe->lock);
  87. return ret;
  88. }
  89. EXPORT_SYMBOL_GPL(stmpe_enable);
  90. /**
  91. * stmpe_disable - disable blocks on an STMPE device
  92. * @stmpe: Device to work on
  93. * @blocks: Mask of blocks (enum stmpe_block values) to enable
  94. */
  95. int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
  96. {
  97. int ret;
  98. mutex_lock(&stmpe->lock);
  99. ret = __stmpe_disable(stmpe, blocks);
  100. mutex_unlock(&stmpe->lock);
  101. return ret;
  102. }
  103. EXPORT_SYMBOL_GPL(stmpe_disable);
  104. /**
  105. * stmpe_reg_read() - read a single STMPE register
  106. * @stmpe: Device to read from
  107. * @reg: Register to read
  108. */
  109. int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
  110. {
  111. int ret;
  112. mutex_lock(&stmpe->lock);
  113. ret = __stmpe_reg_read(stmpe, reg);
  114. mutex_unlock(&stmpe->lock);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL_GPL(stmpe_reg_read);
  118. /**
  119. * stmpe_reg_write() - write a single STMPE register
  120. * @stmpe: Device to write to
  121. * @reg: Register to write
  122. * @val: Value to write
  123. */
  124. int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
  125. {
  126. int ret;
  127. mutex_lock(&stmpe->lock);
  128. ret = __stmpe_reg_write(stmpe, reg, val);
  129. mutex_unlock(&stmpe->lock);
  130. return ret;
  131. }
  132. EXPORT_SYMBOL_GPL(stmpe_reg_write);
  133. /**
  134. * stmpe_set_bits() - set the value of a bitfield in a STMPE register
  135. * @stmpe: Device to write to
  136. * @reg: Register to write
  137. * @mask: Mask of bits to set
  138. * @val: Value to set
  139. */
  140. int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
  141. {
  142. int ret;
  143. mutex_lock(&stmpe->lock);
  144. ret = __stmpe_set_bits(stmpe, reg, mask, val);
  145. mutex_unlock(&stmpe->lock);
  146. return ret;
  147. }
  148. EXPORT_SYMBOL_GPL(stmpe_set_bits);
  149. /**
  150. * stmpe_block_read() - read multiple STMPE registers
  151. * @stmpe: Device to read from
  152. * @reg: First register
  153. * @length: Number of registers
  154. * @values: Buffer to write to
  155. */
  156. int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
  157. {
  158. int ret;
  159. mutex_lock(&stmpe->lock);
  160. ret = __stmpe_block_read(stmpe, reg, length, values);
  161. mutex_unlock(&stmpe->lock);
  162. return ret;
  163. }
  164. EXPORT_SYMBOL_GPL(stmpe_block_read);
  165. /**
  166. * stmpe_block_write() - write multiple STMPE registers
  167. * @stmpe: Device to write to
  168. * @reg: First register
  169. * @length: Number of registers
  170. * @values: Values to write
  171. */
  172. int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
  173. const u8 *values)
  174. {
  175. int ret;
  176. mutex_lock(&stmpe->lock);
  177. ret = __stmpe_block_write(stmpe, reg, length, values);
  178. mutex_unlock(&stmpe->lock);
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(stmpe_block_write);
  182. /**
  183. * stmpe_set_altfunc()- set the alternate function for STMPE pins
  184. * @stmpe: Device to configure
  185. * @pins: Bitmask of pins to affect
  186. * @block: block to enable alternate functions for
  187. *
  188. * @pins is assumed to have a bit set for each of the bits whose alternate
  189. * function is to be changed, numbered according to the GPIOXY numbers.
  190. *
  191. * If the GPIO module is not enabled, this function automatically enables it in
  192. * order to perform the change.
  193. */
  194. int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
  195. {
  196. struct stmpe_variant_info *variant = stmpe->variant;
  197. u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
  198. int af_bits = variant->af_bits;
  199. int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
  200. int mask = (1 << af_bits) - 1;
  201. u8 regs[numregs];
  202. int af, afperreg, ret;
  203. if (!variant->get_altfunc)
  204. return 0;
  205. afperreg = 8 / af_bits;
  206. mutex_lock(&stmpe->lock);
  207. ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
  208. if (ret < 0)
  209. goto out;
  210. ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
  211. if (ret < 0)
  212. goto out;
  213. af = variant->get_altfunc(stmpe, block);
  214. while (pins) {
  215. int pin = __ffs(pins);
  216. int regoffset = numregs - (pin / afperreg) - 1;
  217. int pos = (pin % afperreg) * (8 / afperreg);
  218. regs[regoffset] &= ~(mask << pos);
  219. regs[regoffset] |= af << pos;
  220. pins &= ~(1 << pin);
  221. }
  222. ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
  223. out:
  224. mutex_unlock(&stmpe->lock);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
  228. /*
  229. * GPIO (all variants)
  230. */
  231. static struct resource stmpe_gpio_resources[] = {
  232. /* Start and end filled dynamically */
  233. {
  234. .flags = IORESOURCE_IRQ,
  235. },
  236. };
  237. static struct mfd_cell stmpe_gpio_cell = {
  238. .name = "stmpe-gpio",
  239. .resources = stmpe_gpio_resources,
  240. .num_resources = ARRAY_SIZE(stmpe_gpio_resources),
  241. };
  242. static struct mfd_cell stmpe_gpio_cell_noirq = {
  243. .name = "stmpe-gpio",
  244. /* gpio cell resources consist of an irq only so no resources here */
  245. };
  246. /*
  247. * Keypad (1601, 2401, 2403)
  248. */
  249. static struct resource stmpe_keypad_resources[] = {
  250. {
  251. .name = "KEYPAD",
  252. .start = 0,
  253. .end = 0,
  254. .flags = IORESOURCE_IRQ,
  255. },
  256. {
  257. .name = "KEYPAD_OVER",
  258. .start = 1,
  259. .end = 1,
  260. .flags = IORESOURCE_IRQ,
  261. },
  262. };
  263. static struct mfd_cell stmpe_keypad_cell = {
  264. .name = "stmpe-keypad",
  265. .of_compatible = "st,stmpe-keypad",
  266. .resources = stmpe_keypad_resources,
  267. .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
  268. };
  269. /*
  270. * STMPE801
  271. */
  272. static const u8 stmpe801_regs[] = {
  273. [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
  274. [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
  275. [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
  276. [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  277. [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  278. [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
  279. [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
  280. [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
  281. };
  282. static struct stmpe_variant_block stmpe801_blocks[] = {
  283. {
  284. .cell = &stmpe_gpio_cell,
  285. .irq = 0,
  286. .block = STMPE_BLOCK_GPIO,
  287. },
  288. };
  289. static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
  290. {
  291. .cell = &stmpe_gpio_cell_noirq,
  292. .block = STMPE_BLOCK_GPIO,
  293. },
  294. };
  295. static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
  296. bool enable)
  297. {
  298. if (blocks & STMPE_BLOCK_GPIO)
  299. return 0;
  300. else
  301. return -EINVAL;
  302. }
  303. static struct stmpe_variant_info stmpe801 = {
  304. .name = "stmpe801",
  305. .id_val = STMPE801_ID,
  306. .id_mask = 0xffff,
  307. .num_gpios = 8,
  308. .regs = stmpe801_regs,
  309. .blocks = stmpe801_blocks,
  310. .num_blocks = ARRAY_SIZE(stmpe801_blocks),
  311. .num_irqs = STMPE801_NR_INTERNAL_IRQS,
  312. .enable = stmpe801_enable,
  313. };
  314. static struct stmpe_variant_info stmpe801_noirq = {
  315. .name = "stmpe801",
  316. .id_val = STMPE801_ID,
  317. .id_mask = 0xffff,
  318. .num_gpios = 8,
  319. .regs = stmpe801_regs,
  320. .blocks = stmpe801_blocks_noirq,
  321. .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
  322. .enable = stmpe801_enable,
  323. };
  324. /*
  325. * Touchscreen (STMPE811 or STMPE610)
  326. */
  327. static struct resource stmpe_ts_resources[] = {
  328. {
  329. .name = "TOUCH_DET",
  330. .start = 0,
  331. .end = 0,
  332. .flags = IORESOURCE_IRQ,
  333. },
  334. {
  335. .name = "FIFO_TH",
  336. .start = 1,
  337. .end = 1,
  338. .flags = IORESOURCE_IRQ,
  339. },
  340. };
  341. static 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 = STMPE24XX_IRQ_GPIOC,
  446. .block = STMPE_BLOCK_GPIO,
  447. },
  448. {
  449. .cell = &stmpe_keypad_cell,
  450. .irq = STMPE24XX_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. if (blocks & STMPE_BLOCK_KEYPAD)
  510. mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
  511. return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
  512. enable ? mask : 0);
  513. }
  514. static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  515. {
  516. switch (block) {
  517. case STMPE_BLOCK_PWM:
  518. return 2;
  519. case STMPE_BLOCK_KEYPAD:
  520. return 1;
  521. case STMPE_BLOCK_GPIO:
  522. default:
  523. return 0;
  524. }
  525. }
  526. static struct stmpe_variant_info stmpe1601 = {
  527. .name = "stmpe1601",
  528. .id_val = 0x0210,
  529. .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
  530. .num_gpios = 16,
  531. .af_bits = 2,
  532. .regs = stmpe1601_regs,
  533. .blocks = stmpe1601_blocks,
  534. .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
  535. .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
  536. .enable = stmpe1601_enable,
  537. .get_altfunc = stmpe1601_get_altfunc,
  538. .enable_autosleep = stmpe1601_autosleep,
  539. };
  540. /*
  541. * STMPE24XX
  542. */
  543. static const u8 stmpe24xx_regs[] = {
  544. [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
  545. [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
  546. [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
  547. [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
  548. [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
  549. [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
  550. [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
  551. [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
  552. [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
  553. [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
  554. [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
  555. [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
  556. [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
  557. [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
  558. };
  559. static struct stmpe_variant_block stmpe24xx_blocks[] = {
  560. {
  561. .cell = &stmpe_gpio_cell,
  562. .irq = STMPE24XX_IRQ_GPIOC,
  563. .block = STMPE_BLOCK_GPIO,
  564. },
  565. {
  566. .cell = &stmpe_keypad_cell,
  567. .irq = STMPE24XX_IRQ_KEYPAD,
  568. .block = STMPE_BLOCK_KEYPAD,
  569. },
  570. };
  571. static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
  572. bool enable)
  573. {
  574. unsigned int mask = 0;
  575. if (blocks & STMPE_BLOCK_GPIO)
  576. mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
  577. if (blocks & STMPE_BLOCK_KEYPAD)
  578. mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
  579. return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
  580. enable ? mask : 0);
  581. }
  582. static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  583. {
  584. switch (block) {
  585. case STMPE_BLOCK_ROTATOR:
  586. return 2;
  587. case STMPE_BLOCK_KEYPAD:
  588. return 1;
  589. case STMPE_BLOCK_GPIO:
  590. default:
  591. return 0;
  592. }
  593. }
  594. static struct stmpe_variant_info stmpe2401 = {
  595. .name = "stmpe2401",
  596. .id_val = 0x0101,
  597. .id_mask = 0xffff,
  598. .num_gpios = 24,
  599. .af_bits = 2,
  600. .regs = stmpe24xx_regs,
  601. .blocks = stmpe24xx_blocks,
  602. .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
  603. .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
  604. .enable = stmpe24xx_enable,
  605. .get_altfunc = stmpe24xx_get_altfunc,
  606. };
  607. static struct stmpe_variant_info stmpe2403 = {
  608. .name = "stmpe2403",
  609. .id_val = 0x0120,
  610. .id_mask = 0xffff,
  611. .num_gpios = 24,
  612. .af_bits = 2,
  613. .regs = stmpe24xx_regs,
  614. .blocks = stmpe24xx_blocks,
  615. .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
  616. .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
  617. .enable = stmpe24xx_enable,
  618. .get_altfunc = stmpe24xx_get_altfunc,
  619. .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
  620. };
  621. static struct stmpe_variant_info *stmpe_variant_info[STMPE_NBR_PARTS] = {
  622. [STMPE610] = &stmpe610,
  623. [STMPE801] = &stmpe801,
  624. [STMPE811] = &stmpe811,
  625. [STMPE1601] = &stmpe1601,
  626. [STMPE2401] = &stmpe2401,
  627. [STMPE2403] = &stmpe2403,
  628. };
  629. /*
  630. * These devices can be connected in a 'no-irq' configuration - the irq pin
  631. * is not used and the device cannot interrupt the CPU. Here we only list
  632. * devices which support this configuration - the driver will fail probing
  633. * for any devices not listed here which are configured in this way.
  634. */
  635. static struct stmpe_variant_info *stmpe_noirq_variant_info[STMPE_NBR_PARTS] = {
  636. [STMPE801] = &stmpe801_noirq,
  637. };
  638. static irqreturn_t stmpe_irq(int irq, void *data)
  639. {
  640. struct stmpe *stmpe = data;
  641. struct stmpe_variant_info *variant = stmpe->variant;
  642. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  643. u8 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
  644. u8 isr[num];
  645. int ret;
  646. int i;
  647. if (variant->id_val == STMPE801_ID) {
  648. handle_nested_irq(stmpe->irq_base);
  649. return IRQ_HANDLED;
  650. }
  651. ret = stmpe_block_read(stmpe, israddr, num, isr);
  652. if (ret < 0)
  653. return IRQ_NONE;
  654. for (i = 0; i < num; i++) {
  655. int bank = num - i - 1;
  656. u8 status = isr[i];
  657. u8 clear;
  658. status &= stmpe->ier[bank];
  659. if (!status)
  660. continue;
  661. clear = status;
  662. while (status) {
  663. int bit = __ffs(status);
  664. int line = bank * 8 + bit;
  665. handle_nested_irq(stmpe->irq_base + line);
  666. status &= ~(1 << bit);
  667. }
  668. stmpe_reg_write(stmpe, israddr + i, clear);
  669. }
  670. return IRQ_HANDLED;
  671. }
  672. static void stmpe_irq_lock(struct irq_data *data)
  673. {
  674. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  675. mutex_lock(&stmpe->irq_lock);
  676. }
  677. static void stmpe_irq_sync_unlock(struct irq_data *data)
  678. {
  679. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  680. struct stmpe_variant_info *variant = stmpe->variant;
  681. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  682. int i;
  683. for (i = 0; i < num; i++) {
  684. u8 new = stmpe->ier[i];
  685. u8 old = stmpe->oldier[i];
  686. if (new == old)
  687. continue;
  688. stmpe->oldier[i] = new;
  689. stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
  690. }
  691. mutex_unlock(&stmpe->irq_lock);
  692. }
  693. static void stmpe_irq_mask(struct irq_data *data)
  694. {
  695. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  696. int offset = data->irq - stmpe->irq_base;
  697. int regoffset = offset / 8;
  698. int mask = 1 << (offset % 8);
  699. stmpe->ier[regoffset] &= ~mask;
  700. }
  701. static void stmpe_irq_unmask(struct irq_data *data)
  702. {
  703. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  704. int offset = data->irq - stmpe->irq_base;
  705. int regoffset = offset / 8;
  706. int mask = 1 << (offset % 8);
  707. stmpe->ier[regoffset] |= mask;
  708. }
  709. static struct irq_chip stmpe_irq_chip = {
  710. .name = "stmpe",
  711. .irq_bus_lock = stmpe_irq_lock,
  712. .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
  713. .irq_mask = stmpe_irq_mask,
  714. .irq_unmask = stmpe_irq_unmask,
  715. };
  716. static int __devinit stmpe_irq_init(struct stmpe *stmpe)
  717. {
  718. struct irq_chip *chip = NULL;
  719. int num_irqs = stmpe->variant->num_irqs;
  720. int base = stmpe->irq_base;
  721. int irq;
  722. if (stmpe->variant->id_val != STMPE801_ID)
  723. chip = &stmpe_irq_chip;
  724. for (irq = base; irq < base + num_irqs; irq++) {
  725. irq_set_chip_data(irq, stmpe);
  726. irq_set_chip_and_handler(irq, chip, handle_edge_irq);
  727. irq_set_nested_thread(irq, 1);
  728. #ifdef CONFIG_ARM
  729. set_irq_flags(irq, IRQF_VALID);
  730. #else
  731. irq_set_noprobe(irq);
  732. #endif
  733. }
  734. return 0;
  735. }
  736. static void stmpe_irq_remove(struct stmpe *stmpe)
  737. {
  738. int num_irqs = stmpe->variant->num_irqs;
  739. int base = stmpe->irq_base;
  740. int irq;
  741. for (irq = base; irq < base + num_irqs; irq++) {
  742. #ifdef CONFIG_ARM
  743. set_irq_flags(irq, 0);
  744. #endif
  745. irq_set_chip_and_handler(irq, NULL, NULL);
  746. irq_set_chip_data(irq, NULL);
  747. }
  748. }
  749. static int __devinit stmpe_chip_init(struct stmpe *stmpe)
  750. {
  751. unsigned int irq_trigger = stmpe->pdata->irq_trigger;
  752. int autosleep_timeout = stmpe->pdata->autosleep_timeout;
  753. struct stmpe_variant_info *variant = stmpe->variant;
  754. u8 icr = 0;
  755. unsigned int id;
  756. u8 data[2];
  757. int ret;
  758. ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
  759. ARRAY_SIZE(data), data);
  760. if (ret < 0)
  761. return ret;
  762. id = (data[0] << 8) | data[1];
  763. if ((id & variant->id_mask) != variant->id_val) {
  764. dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
  765. return -EINVAL;
  766. }
  767. dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
  768. /* Disable all modules -- subdrivers should enable what they need. */
  769. ret = stmpe_disable(stmpe, ~0);
  770. if (ret)
  771. return ret;
  772. if (stmpe->irq >= 0) {
  773. if (id == STMPE801_ID)
  774. icr = STMPE801_REG_SYS_CTRL_INT_EN;
  775. else
  776. icr = STMPE_ICR_LSB_GIM;
  777. /* STMPE801 doesn't support Edge interrupts */
  778. if (id != STMPE801_ID) {
  779. if (irq_trigger == IRQF_TRIGGER_FALLING ||
  780. irq_trigger == IRQF_TRIGGER_RISING)
  781. icr |= STMPE_ICR_LSB_EDGE;
  782. }
  783. if (irq_trigger == IRQF_TRIGGER_RISING ||
  784. irq_trigger == IRQF_TRIGGER_HIGH) {
  785. if (id == STMPE801_ID)
  786. icr |= STMPE801_REG_SYS_CTRL_INT_HI;
  787. else
  788. icr |= STMPE_ICR_LSB_HIGH;
  789. }
  790. if (stmpe->pdata->irq_invert_polarity) {
  791. if (id == STMPE801_ID)
  792. icr ^= STMPE801_REG_SYS_CTRL_INT_HI;
  793. else
  794. icr ^= STMPE_ICR_LSB_HIGH;
  795. }
  796. }
  797. if (stmpe->pdata->autosleep) {
  798. ret = stmpe_autosleep(stmpe, autosleep_timeout);
  799. if (ret)
  800. return ret;
  801. }
  802. return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
  803. }
  804. static int __devinit stmpe_add_device(struct stmpe *stmpe,
  805. struct mfd_cell *cell, int irq)
  806. {
  807. return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
  808. NULL, stmpe->irq_base + irq, NULL);
  809. }
  810. static int __devinit stmpe_devices_init(struct stmpe *stmpe)
  811. {
  812. struct stmpe_variant_info *variant = stmpe->variant;
  813. unsigned int platform_blocks = stmpe->pdata->blocks;
  814. int ret = -EINVAL;
  815. int i;
  816. for (i = 0; i < variant->num_blocks; i++) {
  817. struct stmpe_variant_block *block = &variant->blocks[i];
  818. if (!(platform_blocks & block->block))
  819. continue;
  820. platform_blocks &= ~block->block;
  821. ret = stmpe_add_device(stmpe, block->cell, block->irq);
  822. if (ret)
  823. return ret;
  824. }
  825. if (platform_blocks)
  826. dev_warn(stmpe->dev,
  827. "platform wants blocks (%#x) not present on variant",
  828. platform_blocks);
  829. return ret;
  830. }
  831. /* Called from client specific probe routines */
  832. int __devinit stmpe_probe(struct stmpe_client_info *ci, int partnum)
  833. {
  834. struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
  835. struct stmpe *stmpe;
  836. int ret;
  837. if (!pdata)
  838. return -EINVAL;
  839. stmpe = kzalloc(sizeof(struct stmpe), GFP_KERNEL);
  840. if (!stmpe)
  841. return -ENOMEM;
  842. mutex_init(&stmpe->irq_lock);
  843. mutex_init(&stmpe->lock);
  844. stmpe->dev = ci->dev;
  845. stmpe->client = ci->client;
  846. stmpe->pdata = pdata;
  847. stmpe->irq_base = pdata->irq_base;
  848. stmpe->ci = ci;
  849. stmpe->partnum = partnum;
  850. stmpe->variant = stmpe_variant_info[partnum];
  851. stmpe->regs = stmpe->variant->regs;
  852. stmpe->num_gpios = stmpe->variant->num_gpios;
  853. dev_set_drvdata(stmpe->dev, stmpe);
  854. if (ci->init)
  855. ci->init(stmpe);
  856. if (pdata->irq_over_gpio) {
  857. ret = gpio_request_one(pdata->irq_gpio, GPIOF_DIR_IN, "stmpe");
  858. if (ret) {
  859. dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
  860. ret);
  861. goto out_free;
  862. }
  863. stmpe->irq = gpio_to_irq(pdata->irq_gpio);
  864. } else {
  865. stmpe->irq = ci->irq;
  866. }
  867. if (stmpe->irq < 0) {
  868. /* use alternate variant info for no-irq mode, if supported */
  869. dev_info(stmpe->dev,
  870. "%s configured in no-irq mode by platform data\n",
  871. stmpe->variant->name);
  872. if (!stmpe_noirq_variant_info[stmpe->partnum]) {
  873. dev_err(stmpe->dev,
  874. "%s does not support no-irq mode!\n",
  875. stmpe->variant->name);
  876. ret = -ENODEV;
  877. goto free_gpio;
  878. }
  879. stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
  880. }
  881. ret = stmpe_chip_init(stmpe);
  882. if (ret)
  883. goto free_gpio;
  884. if (stmpe->irq >= 0) {
  885. ret = stmpe_irq_init(stmpe);
  886. if (ret)
  887. goto free_gpio;
  888. ret = request_threaded_irq(stmpe->irq, NULL, stmpe_irq,
  889. pdata->irq_trigger | IRQF_ONESHOT,
  890. "stmpe", stmpe);
  891. if (ret) {
  892. dev_err(stmpe->dev, "failed to request IRQ: %d\n",
  893. ret);
  894. goto out_removeirq;
  895. }
  896. }
  897. ret = stmpe_devices_init(stmpe);
  898. if (ret) {
  899. dev_err(stmpe->dev, "failed to add children\n");
  900. goto out_removedevs;
  901. }
  902. return 0;
  903. out_removedevs:
  904. mfd_remove_devices(stmpe->dev);
  905. if (stmpe->irq >= 0)
  906. free_irq(stmpe->irq, stmpe);
  907. out_removeirq:
  908. if (stmpe->irq >= 0)
  909. stmpe_irq_remove(stmpe);
  910. free_gpio:
  911. if (pdata->irq_over_gpio)
  912. gpio_free(pdata->irq_gpio);
  913. out_free:
  914. kfree(stmpe);
  915. return ret;
  916. }
  917. int stmpe_remove(struct stmpe *stmpe)
  918. {
  919. mfd_remove_devices(stmpe->dev);
  920. if (stmpe->irq >= 0) {
  921. free_irq(stmpe->irq, stmpe);
  922. stmpe_irq_remove(stmpe);
  923. }
  924. if (stmpe->pdata->irq_over_gpio)
  925. gpio_free(stmpe->pdata->irq_gpio);
  926. kfree(stmpe);
  927. return 0;
  928. }
  929. #ifdef CONFIG_PM
  930. static int stmpe_suspend(struct device *dev)
  931. {
  932. struct stmpe *stmpe = dev_get_drvdata(dev);
  933. if (stmpe->irq >= 0 && device_may_wakeup(dev))
  934. enable_irq_wake(stmpe->irq);
  935. return 0;
  936. }
  937. static int stmpe_resume(struct device *dev)
  938. {
  939. struct stmpe *stmpe = dev_get_drvdata(dev);
  940. if (stmpe->irq >= 0 && device_may_wakeup(dev))
  941. disable_irq_wake(stmpe->irq);
  942. return 0;
  943. }
  944. const struct dev_pm_ops stmpe_dev_pm_ops = {
  945. .suspend = stmpe_suspend,
  946. .resume = stmpe_resume,
  947. };
  948. #endif