stmpe.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. /*
  243. * Keypad (1601, 2401, 2403)
  244. */
  245. static struct resource stmpe_keypad_resources[] = {
  246. {
  247. .name = "KEYPAD",
  248. .start = 0,
  249. .end = 0,
  250. .flags = IORESOURCE_IRQ,
  251. },
  252. {
  253. .name = "KEYPAD_OVER",
  254. .start = 1,
  255. .end = 1,
  256. .flags = IORESOURCE_IRQ,
  257. },
  258. };
  259. static struct mfd_cell stmpe_keypad_cell = {
  260. .name = "stmpe-keypad",
  261. .resources = stmpe_keypad_resources,
  262. .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
  263. };
  264. /*
  265. * STMPE801
  266. */
  267. static const u8 stmpe801_regs[] = {
  268. [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
  269. [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
  270. [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
  271. [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  272. [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  273. [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
  274. [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
  275. [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
  276. };
  277. static struct stmpe_variant_block stmpe801_blocks[] = {
  278. {
  279. .cell = &stmpe_gpio_cell,
  280. .irq = 0,
  281. .block = STMPE_BLOCK_GPIO,
  282. },
  283. };
  284. static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
  285. bool enable)
  286. {
  287. if (blocks & STMPE_BLOCK_GPIO)
  288. return 0;
  289. else
  290. return -EINVAL;
  291. }
  292. static struct stmpe_variant_info stmpe801 = {
  293. .name = "stmpe801",
  294. .id_val = STMPE801_ID,
  295. .id_mask = 0xffff,
  296. .num_gpios = 8,
  297. .regs = stmpe801_regs,
  298. .blocks = stmpe801_blocks,
  299. .num_blocks = ARRAY_SIZE(stmpe801_blocks),
  300. .num_irqs = STMPE801_NR_INTERNAL_IRQS,
  301. .enable = stmpe801_enable,
  302. };
  303. /*
  304. * Touchscreen (STMPE811 or STMPE610)
  305. */
  306. static struct resource stmpe_ts_resources[] = {
  307. {
  308. .name = "TOUCH_DET",
  309. .start = 0,
  310. .end = 0,
  311. .flags = IORESOURCE_IRQ,
  312. },
  313. {
  314. .name = "FIFO_TH",
  315. .start = 1,
  316. .end = 1,
  317. .flags = IORESOURCE_IRQ,
  318. },
  319. };
  320. static struct mfd_cell stmpe_ts_cell = {
  321. .name = "stmpe-ts",
  322. .resources = stmpe_ts_resources,
  323. .num_resources = ARRAY_SIZE(stmpe_ts_resources),
  324. };
  325. /*
  326. * STMPE811 or STMPE610
  327. */
  328. static const u8 stmpe811_regs[] = {
  329. [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
  330. [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
  331. [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
  332. [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
  333. [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
  334. [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
  335. [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
  336. [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
  337. [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
  338. [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
  339. [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
  340. [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
  341. [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
  342. [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
  343. };
  344. static struct stmpe_variant_block stmpe811_blocks[] = {
  345. {
  346. .cell = &stmpe_gpio_cell,
  347. .irq = STMPE811_IRQ_GPIOC,
  348. .block = STMPE_BLOCK_GPIO,
  349. },
  350. {
  351. .cell = &stmpe_ts_cell,
  352. .irq = STMPE811_IRQ_TOUCH_DET,
  353. .block = STMPE_BLOCK_TOUCHSCREEN,
  354. },
  355. };
  356. static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
  357. bool enable)
  358. {
  359. unsigned int mask = 0;
  360. if (blocks & STMPE_BLOCK_GPIO)
  361. mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
  362. if (blocks & STMPE_BLOCK_ADC)
  363. mask |= STMPE811_SYS_CTRL2_ADC_OFF;
  364. if (blocks & STMPE_BLOCK_TOUCHSCREEN)
  365. mask |= STMPE811_SYS_CTRL2_TSC_OFF;
  366. return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
  367. enable ? 0 : mask);
  368. }
  369. static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  370. {
  371. /* 0 for touchscreen, 1 for GPIO */
  372. return block != STMPE_BLOCK_TOUCHSCREEN;
  373. }
  374. static struct stmpe_variant_info stmpe811 = {
  375. .name = "stmpe811",
  376. .id_val = 0x0811,
  377. .id_mask = 0xffff,
  378. .num_gpios = 8,
  379. .af_bits = 1,
  380. .regs = stmpe811_regs,
  381. .blocks = stmpe811_blocks,
  382. .num_blocks = ARRAY_SIZE(stmpe811_blocks),
  383. .num_irqs = STMPE811_NR_INTERNAL_IRQS,
  384. .enable = stmpe811_enable,
  385. .get_altfunc = stmpe811_get_altfunc,
  386. };
  387. /* Similar to 811, except number of gpios */
  388. static struct stmpe_variant_info stmpe610 = {
  389. .name = "stmpe610",
  390. .id_val = 0x0811,
  391. .id_mask = 0xffff,
  392. .num_gpios = 6,
  393. .af_bits = 1,
  394. .regs = stmpe811_regs,
  395. .blocks = stmpe811_blocks,
  396. .num_blocks = ARRAY_SIZE(stmpe811_blocks),
  397. .num_irqs = STMPE811_NR_INTERNAL_IRQS,
  398. .enable = stmpe811_enable,
  399. .get_altfunc = stmpe811_get_altfunc,
  400. };
  401. /*
  402. * STMPE1601
  403. */
  404. static const u8 stmpe1601_regs[] = {
  405. [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
  406. [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
  407. [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
  408. [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
  409. [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
  410. [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
  411. [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
  412. [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
  413. [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
  414. [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
  415. [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
  416. [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
  417. [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
  418. [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
  419. };
  420. static struct stmpe_variant_block stmpe1601_blocks[] = {
  421. {
  422. .cell = &stmpe_gpio_cell,
  423. .irq = STMPE24XX_IRQ_GPIOC,
  424. .block = STMPE_BLOCK_GPIO,
  425. },
  426. {
  427. .cell = &stmpe_keypad_cell,
  428. .irq = STMPE24XX_IRQ_KEYPAD,
  429. .block = STMPE_BLOCK_KEYPAD,
  430. },
  431. };
  432. /* supported autosleep timeout delay (in msecs) */
  433. static const int stmpe_autosleep_delay[] = {
  434. 4, 16, 32, 64, 128, 256, 512, 1024,
  435. };
  436. static int stmpe_round_timeout(int timeout)
  437. {
  438. int i;
  439. for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
  440. if (stmpe_autosleep_delay[i] >= timeout)
  441. return i;
  442. }
  443. /*
  444. * requests for delays longer than supported should not return the
  445. * longest supported delay
  446. */
  447. return -EINVAL;
  448. }
  449. static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
  450. {
  451. int ret;
  452. if (!stmpe->variant->enable_autosleep)
  453. return -ENOSYS;
  454. mutex_lock(&stmpe->lock);
  455. ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
  456. mutex_unlock(&stmpe->lock);
  457. return ret;
  458. }
  459. /*
  460. * Both stmpe 1601/2403 support same layout for autosleep
  461. */
  462. static int stmpe1601_autosleep(struct stmpe *stmpe,
  463. int autosleep_timeout)
  464. {
  465. int ret, timeout;
  466. /* choose the best available timeout */
  467. timeout = stmpe_round_timeout(autosleep_timeout);
  468. if (timeout < 0) {
  469. dev_err(stmpe->dev, "invalid timeout\n");
  470. return timeout;
  471. }
  472. ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
  473. STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
  474. timeout);
  475. if (ret < 0)
  476. return ret;
  477. return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
  478. STPME1601_AUTOSLEEP_ENABLE,
  479. STPME1601_AUTOSLEEP_ENABLE);
  480. }
  481. static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
  482. bool enable)
  483. {
  484. unsigned int mask = 0;
  485. if (blocks & STMPE_BLOCK_GPIO)
  486. mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
  487. if (blocks & STMPE_BLOCK_KEYPAD)
  488. mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
  489. return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
  490. enable ? mask : 0);
  491. }
  492. static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  493. {
  494. switch (block) {
  495. case STMPE_BLOCK_PWM:
  496. return 2;
  497. case STMPE_BLOCK_KEYPAD:
  498. return 1;
  499. case STMPE_BLOCK_GPIO:
  500. default:
  501. return 0;
  502. }
  503. }
  504. static struct stmpe_variant_info stmpe1601 = {
  505. .name = "stmpe1601",
  506. .id_val = 0x0210,
  507. .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
  508. .num_gpios = 16,
  509. .af_bits = 2,
  510. .regs = stmpe1601_regs,
  511. .blocks = stmpe1601_blocks,
  512. .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
  513. .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
  514. .enable = stmpe1601_enable,
  515. .get_altfunc = stmpe1601_get_altfunc,
  516. .enable_autosleep = stmpe1601_autosleep,
  517. };
  518. /*
  519. * STMPE24XX
  520. */
  521. static const u8 stmpe24xx_regs[] = {
  522. [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
  523. [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
  524. [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
  525. [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
  526. [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
  527. [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
  528. [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
  529. [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
  530. [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
  531. [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
  532. [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
  533. [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
  534. [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
  535. [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
  536. };
  537. static struct stmpe_variant_block stmpe24xx_blocks[] = {
  538. {
  539. .cell = &stmpe_gpio_cell,
  540. .irq = STMPE24XX_IRQ_GPIOC,
  541. .block = STMPE_BLOCK_GPIO,
  542. },
  543. {
  544. .cell = &stmpe_keypad_cell,
  545. .irq = STMPE24XX_IRQ_KEYPAD,
  546. .block = STMPE_BLOCK_KEYPAD,
  547. },
  548. };
  549. static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
  550. bool enable)
  551. {
  552. unsigned int mask = 0;
  553. if (blocks & STMPE_BLOCK_GPIO)
  554. mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
  555. if (blocks & STMPE_BLOCK_KEYPAD)
  556. mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
  557. return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
  558. enable ? mask : 0);
  559. }
  560. static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  561. {
  562. switch (block) {
  563. case STMPE_BLOCK_ROTATOR:
  564. return 2;
  565. case STMPE_BLOCK_KEYPAD:
  566. return 1;
  567. case STMPE_BLOCK_GPIO:
  568. default:
  569. return 0;
  570. }
  571. }
  572. static struct stmpe_variant_info stmpe2401 = {
  573. .name = "stmpe2401",
  574. .id_val = 0x0101,
  575. .id_mask = 0xffff,
  576. .num_gpios = 24,
  577. .af_bits = 2,
  578. .regs = stmpe24xx_regs,
  579. .blocks = stmpe24xx_blocks,
  580. .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
  581. .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
  582. .enable = stmpe24xx_enable,
  583. .get_altfunc = stmpe24xx_get_altfunc,
  584. };
  585. static struct stmpe_variant_info stmpe2403 = {
  586. .name = "stmpe2403",
  587. .id_val = 0x0120,
  588. .id_mask = 0xffff,
  589. .num_gpios = 24,
  590. .af_bits = 2,
  591. .regs = stmpe24xx_regs,
  592. .blocks = stmpe24xx_blocks,
  593. .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
  594. .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
  595. .enable = stmpe24xx_enable,
  596. .get_altfunc = stmpe24xx_get_altfunc,
  597. .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
  598. };
  599. static struct stmpe_variant_info *stmpe_variant_info[] = {
  600. [STMPE610] = &stmpe610,
  601. [STMPE801] = &stmpe801,
  602. [STMPE811] = &stmpe811,
  603. [STMPE1601] = &stmpe1601,
  604. [STMPE2401] = &stmpe2401,
  605. [STMPE2403] = &stmpe2403,
  606. };
  607. static irqreturn_t stmpe_irq(int irq, void *data)
  608. {
  609. struct stmpe *stmpe = data;
  610. struct stmpe_variant_info *variant = stmpe->variant;
  611. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  612. u8 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
  613. u8 isr[num];
  614. int ret;
  615. int i;
  616. if (variant->id_val == STMPE801_ID) {
  617. handle_nested_irq(stmpe->irq_base);
  618. return IRQ_HANDLED;
  619. }
  620. ret = stmpe_block_read(stmpe, israddr, num, isr);
  621. if (ret < 0)
  622. return IRQ_NONE;
  623. for (i = 0; i < num; i++) {
  624. int bank = num - i - 1;
  625. u8 status = isr[i];
  626. u8 clear;
  627. status &= stmpe->ier[bank];
  628. if (!status)
  629. continue;
  630. clear = status;
  631. while (status) {
  632. int bit = __ffs(status);
  633. int line = bank * 8 + bit;
  634. handle_nested_irq(stmpe->irq_base + line);
  635. status &= ~(1 << bit);
  636. }
  637. stmpe_reg_write(stmpe, israddr + i, clear);
  638. }
  639. return IRQ_HANDLED;
  640. }
  641. static void stmpe_irq_lock(struct irq_data *data)
  642. {
  643. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  644. mutex_lock(&stmpe->irq_lock);
  645. }
  646. static void stmpe_irq_sync_unlock(struct irq_data *data)
  647. {
  648. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  649. struct stmpe_variant_info *variant = stmpe->variant;
  650. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  651. int i;
  652. for (i = 0; i < num; i++) {
  653. u8 new = stmpe->ier[i];
  654. u8 old = stmpe->oldier[i];
  655. if (new == old)
  656. continue;
  657. stmpe->oldier[i] = new;
  658. stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
  659. }
  660. mutex_unlock(&stmpe->irq_lock);
  661. }
  662. static void stmpe_irq_mask(struct irq_data *data)
  663. {
  664. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  665. int offset = data->irq - stmpe->irq_base;
  666. int regoffset = offset / 8;
  667. int mask = 1 << (offset % 8);
  668. stmpe->ier[regoffset] &= ~mask;
  669. }
  670. static void stmpe_irq_unmask(struct irq_data *data)
  671. {
  672. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  673. int offset = data->irq - stmpe->irq_base;
  674. int regoffset = offset / 8;
  675. int mask = 1 << (offset % 8);
  676. stmpe->ier[regoffset] |= mask;
  677. }
  678. static struct irq_chip stmpe_irq_chip = {
  679. .name = "stmpe",
  680. .irq_bus_lock = stmpe_irq_lock,
  681. .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
  682. .irq_mask = stmpe_irq_mask,
  683. .irq_unmask = stmpe_irq_unmask,
  684. };
  685. static int __devinit stmpe_irq_init(struct stmpe *stmpe)
  686. {
  687. struct irq_chip *chip = NULL;
  688. int num_irqs = stmpe->variant->num_irqs;
  689. int base = stmpe->irq_base;
  690. int irq;
  691. if (stmpe->variant->id_val != STMPE801_ID)
  692. chip = &stmpe_irq_chip;
  693. for (irq = base; irq < base + num_irqs; irq++) {
  694. irq_set_chip_data(irq, stmpe);
  695. irq_set_chip_and_handler(irq, chip, handle_edge_irq);
  696. irq_set_nested_thread(irq, 1);
  697. #ifdef CONFIG_ARM
  698. set_irq_flags(irq, IRQF_VALID);
  699. #else
  700. irq_set_noprobe(irq);
  701. #endif
  702. }
  703. return 0;
  704. }
  705. static void stmpe_irq_remove(struct stmpe *stmpe)
  706. {
  707. int num_irqs = stmpe->variant->num_irqs;
  708. int base = stmpe->irq_base;
  709. int irq;
  710. for (irq = base; irq < base + num_irqs; irq++) {
  711. #ifdef CONFIG_ARM
  712. set_irq_flags(irq, 0);
  713. #endif
  714. irq_set_chip_and_handler(irq, NULL, NULL);
  715. irq_set_chip_data(irq, NULL);
  716. }
  717. }
  718. static int __devinit stmpe_chip_init(struct stmpe *stmpe)
  719. {
  720. unsigned int irq_trigger = stmpe->pdata->irq_trigger;
  721. int autosleep_timeout = stmpe->pdata->autosleep_timeout;
  722. struct stmpe_variant_info *variant = stmpe->variant;
  723. u8 icr;
  724. unsigned int id;
  725. u8 data[2];
  726. int ret;
  727. ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
  728. ARRAY_SIZE(data), data);
  729. if (ret < 0)
  730. return ret;
  731. id = (data[0] << 8) | data[1];
  732. if ((id & variant->id_mask) != variant->id_val) {
  733. dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
  734. return -EINVAL;
  735. }
  736. dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
  737. /* Disable all modules -- subdrivers should enable what they need. */
  738. ret = stmpe_disable(stmpe, ~0);
  739. if (ret)
  740. return ret;
  741. if (id == STMPE801_ID)
  742. icr = STMPE801_REG_SYS_CTRL_INT_EN;
  743. else
  744. icr = STMPE_ICR_LSB_GIM;
  745. /* STMPE801 doesn't support Edge interrupts */
  746. if (id != STMPE801_ID) {
  747. if (irq_trigger == IRQF_TRIGGER_FALLING ||
  748. irq_trigger == IRQF_TRIGGER_RISING)
  749. icr |= STMPE_ICR_LSB_EDGE;
  750. }
  751. if (irq_trigger == IRQF_TRIGGER_RISING ||
  752. irq_trigger == IRQF_TRIGGER_HIGH) {
  753. if (id == STMPE801_ID)
  754. icr |= STMPE801_REG_SYS_CTRL_INT_HI;
  755. else
  756. icr |= STMPE_ICR_LSB_HIGH;
  757. }
  758. if (stmpe->pdata->irq_invert_polarity) {
  759. if (id == STMPE801_ID)
  760. icr ^= STMPE801_REG_SYS_CTRL_INT_HI;
  761. else
  762. icr ^= STMPE_ICR_LSB_HIGH;
  763. }
  764. if (stmpe->pdata->autosleep) {
  765. ret = stmpe_autosleep(stmpe, autosleep_timeout);
  766. if (ret)
  767. return ret;
  768. }
  769. return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
  770. }
  771. static int __devinit stmpe_add_device(struct stmpe *stmpe,
  772. struct mfd_cell *cell, int irq)
  773. {
  774. return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
  775. NULL, stmpe->irq_base + irq);
  776. }
  777. static int __devinit stmpe_devices_init(struct stmpe *stmpe)
  778. {
  779. struct stmpe_variant_info *variant = stmpe->variant;
  780. unsigned int platform_blocks = stmpe->pdata->blocks;
  781. int ret = -EINVAL;
  782. int i;
  783. for (i = 0; i < variant->num_blocks; i++) {
  784. struct stmpe_variant_block *block = &variant->blocks[i];
  785. if (!(platform_blocks & block->block))
  786. continue;
  787. platform_blocks &= ~block->block;
  788. ret = stmpe_add_device(stmpe, block->cell, block->irq);
  789. if (ret)
  790. return ret;
  791. }
  792. if (platform_blocks)
  793. dev_warn(stmpe->dev,
  794. "platform wants blocks (%#x) not present on variant",
  795. platform_blocks);
  796. return ret;
  797. }
  798. /* Called from client specific probe routines */
  799. int __devinit stmpe_probe(struct stmpe_client_info *ci, int partnum)
  800. {
  801. struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
  802. struct stmpe *stmpe;
  803. int ret;
  804. if (!pdata)
  805. return -EINVAL;
  806. stmpe = kzalloc(sizeof(struct stmpe), GFP_KERNEL);
  807. if (!stmpe)
  808. return -ENOMEM;
  809. mutex_init(&stmpe->irq_lock);
  810. mutex_init(&stmpe->lock);
  811. stmpe->dev = ci->dev;
  812. stmpe->client = ci->client;
  813. stmpe->pdata = pdata;
  814. stmpe->irq_base = pdata->irq_base;
  815. stmpe->ci = ci;
  816. stmpe->partnum = partnum;
  817. stmpe->variant = stmpe_variant_info[partnum];
  818. stmpe->regs = stmpe->variant->regs;
  819. stmpe->num_gpios = stmpe->variant->num_gpios;
  820. dev_set_drvdata(stmpe->dev, stmpe);
  821. if (ci->init)
  822. ci->init(stmpe);
  823. if (pdata->irq_over_gpio) {
  824. ret = gpio_request_one(pdata->irq_gpio, GPIOF_DIR_IN, "stmpe");
  825. if (ret) {
  826. dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
  827. ret);
  828. goto out_free;
  829. }
  830. stmpe->irq = gpio_to_irq(pdata->irq_gpio);
  831. } else {
  832. stmpe->irq = ci->irq;
  833. }
  834. ret = stmpe_chip_init(stmpe);
  835. if (ret)
  836. goto free_gpio;
  837. ret = stmpe_irq_init(stmpe);
  838. if (ret)
  839. goto free_gpio;
  840. ret = request_threaded_irq(stmpe->irq, NULL, stmpe_irq,
  841. pdata->irq_trigger | IRQF_ONESHOT, "stmpe", stmpe);
  842. if (ret) {
  843. dev_err(stmpe->dev, "failed to request IRQ: %d\n", ret);
  844. goto out_removeirq;
  845. }
  846. ret = stmpe_devices_init(stmpe);
  847. if (ret) {
  848. dev_err(stmpe->dev, "failed to add children\n");
  849. goto out_removedevs;
  850. }
  851. return 0;
  852. out_removedevs:
  853. mfd_remove_devices(stmpe->dev);
  854. free_irq(stmpe->irq, stmpe);
  855. out_removeirq:
  856. stmpe_irq_remove(stmpe);
  857. free_gpio:
  858. if (pdata->irq_over_gpio)
  859. gpio_free(pdata->irq_gpio);
  860. out_free:
  861. kfree(stmpe);
  862. return ret;
  863. }
  864. int stmpe_remove(struct stmpe *stmpe)
  865. {
  866. mfd_remove_devices(stmpe->dev);
  867. free_irq(stmpe->irq, stmpe);
  868. stmpe_irq_remove(stmpe);
  869. if (stmpe->pdata->irq_over_gpio)
  870. gpio_free(stmpe->pdata->irq_gpio);
  871. kfree(stmpe);
  872. return 0;
  873. }
  874. #ifdef CONFIG_PM
  875. static int stmpe_suspend(struct device *dev)
  876. {
  877. struct stmpe *stmpe = dev_get_drvdata(dev);
  878. if (device_may_wakeup(dev))
  879. enable_irq_wake(stmpe->irq);
  880. return 0;
  881. }
  882. static int stmpe_resume(struct device *dev)
  883. {
  884. struct stmpe *stmpe = dev_get_drvdata(dev);
  885. if (device_may_wakeup(dev))
  886. disable_irq_wake(stmpe->irq);
  887. return 0;
  888. }
  889. const struct dev_pm_ops stmpe_dev_pm_ops = {
  890. .suspend = stmpe_suspend,
  891. .resume = stmpe_resume,
  892. };
  893. #endif