stmpe.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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/kernel.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/pm.h>
  14. #include <linux/slab.h>
  15. #include <linux/mfd/core.h>
  16. #include "stmpe.h"
  17. static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
  18. {
  19. return stmpe->variant->enable(stmpe, blocks, true);
  20. }
  21. static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
  22. {
  23. return stmpe->variant->enable(stmpe, blocks, false);
  24. }
  25. static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
  26. {
  27. int ret;
  28. ret = stmpe->ci->read_byte(stmpe, reg);
  29. if (ret < 0)
  30. dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
  31. dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
  32. return ret;
  33. }
  34. static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
  35. {
  36. int ret;
  37. dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
  38. ret = stmpe->ci->write_byte(stmpe, reg, val);
  39. if (ret < 0)
  40. dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
  41. return ret;
  42. }
  43. static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
  44. {
  45. int ret;
  46. ret = __stmpe_reg_read(stmpe, reg);
  47. if (ret < 0)
  48. return ret;
  49. ret &= ~mask;
  50. ret |= val;
  51. return __stmpe_reg_write(stmpe, reg, ret);
  52. }
  53. static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
  54. u8 *values)
  55. {
  56. int ret;
  57. ret = stmpe->ci->read_block(stmpe, reg, length, values);
  58. if (ret < 0)
  59. dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
  60. dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
  61. stmpe_dump_bytes("stmpe rd: ", values, length);
  62. return ret;
  63. }
  64. static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
  65. const u8 *values)
  66. {
  67. int ret;
  68. dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
  69. stmpe_dump_bytes("stmpe wr: ", values, length);
  70. ret = stmpe->ci->write_block(stmpe, reg, length, values);
  71. if (ret < 0)
  72. dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
  73. return ret;
  74. }
  75. /**
  76. * stmpe_enable - enable blocks on an STMPE device
  77. * @stmpe: Device to work on
  78. * @blocks: Mask of blocks (enum stmpe_block values) to enable
  79. */
  80. int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
  81. {
  82. int ret;
  83. mutex_lock(&stmpe->lock);
  84. ret = __stmpe_enable(stmpe, blocks);
  85. mutex_unlock(&stmpe->lock);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL_GPL(stmpe_enable);
  89. /**
  90. * stmpe_disable - disable blocks on an STMPE device
  91. * @stmpe: Device to work on
  92. * @blocks: Mask of blocks (enum stmpe_block values) to enable
  93. */
  94. int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
  95. {
  96. int ret;
  97. mutex_lock(&stmpe->lock);
  98. ret = __stmpe_disable(stmpe, blocks);
  99. mutex_unlock(&stmpe->lock);
  100. return ret;
  101. }
  102. EXPORT_SYMBOL_GPL(stmpe_disable);
  103. /**
  104. * stmpe_reg_read() - read a single STMPE register
  105. * @stmpe: Device to read from
  106. * @reg: Register to read
  107. */
  108. int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
  109. {
  110. int ret;
  111. mutex_lock(&stmpe->lock);
  112. ret = __stmpe_reg_read(stmpe, reg);
  113. mutex_unlock(&stmpe->lock);
  114. return ret;
  115. }
  116. EXPORT_SYMBOL_GPL(stmpe_reg_read);
  117. /**
  118. * stmpe_reg_write() - write a single STMPE register
  119. * @stmpe: Device to write to
  120. * @reg: Register to write
  121. * @val: Value to write
  122. */
  123. int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
  124. {
  125. int ret;
  126. mutex_lock(&stmpe->lock);
  127. ret = __stmpe_reg_write(stmpe, reg, val);
  128. mutex_unlock(&stmpe->lock);
  129. return ret;
  130. }
  131. EXPORT_SYMBOL_GPL(stmpe_reg_write);
  132. /**
  133. * stmpe_set_bits() - set the value of a bitfield in a STMPE register
  134. * @stmpe: Device to write to
  135. * @reg: Register to write
  136. * @mask: Mask of bits to set
  137. * @val: Value to set
  138. */
  139. int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
  140. {
  141. int ret;
  142. mutex_lock(&stmpe->lock);
  143. ret = __stmpe_set_bits(stmpe, reg, mask, val);
  144. mutex_unlock(&stmpe->lock);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(stmpe_set_bits);
  148. /**
  149. * stmpe_block_read() - read multiple STMPE registers
  150. * @stmpe: Device to read from
  151. * @reg: First register
  152. * @length: Number of registers
  153. * @values: Buffer to write to
  154. */
  155. int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
  156. {
  157. int ret;
  158. mutex_lock(&stmpe->lock);
  159. ret = __stmpe_block_read(stmpe, reg, length, values);
  160. mutex_unlock(&stmpe->lock);
  161. return ret;
  162. }
  163. EXPORT_SYMBOL_GPL(stmpe_block_read);
  164. /**
  165. * stmpe_block_write() - write multiple STMPE registers
  166. * @stmpe: Device to write to
  167. * @reg: First register
  168. * @length: Number of registers
  169. * @values: Values to write
  170. */
  171. int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
  172. const u8 *values)
  173. {
  174. int ret;
  175. mutex_lock(&stmpe->lock);
  176. ret = __stmpe_block_write(stmpe, reg, length, values);
  177. mutex_unlock(&stmpe->lock);
  178. return ret;
  179. }
  180. EXPORT_SYMBOL_GPL(stmpe_block_write);
  181. /**
  182. * stmpe_set_altfunc()- set the alternate function for STMPE pins
  183. * @stmpe: Device to configure
  184. * @pins: Bitmask of pins to affect
  185. * @block: block to enable alternate functions for
  186. *
  187. * @pins is assumed to have a bit set for each of the bits whose alternate
  188. * function is to be changed, numbered according to the GPIOXY numbers.
  189. *
  190. * If the GPIO module is not enabled, this function automatically enables it in
  191. * order to perform the change.
  192. */
  193. int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
  194. {
  195. struct stmpe_variant_info *variant = stmpe->variant;
  196. u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
  197. int af_bits = variant->af_bits;
  198. int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
  199. int mask = (1 << af_bits) - 1;
  200. u8 regs[numregs];
  201. int af, afperreg, ret;
  202. if (!variant->get_altfunc)
  203. return 0;
  204. afperreg = 8 / af_bits;
  205. mutex_lock(&stmpe->lock);
  206. ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
  207. if (ret < 0)
  208. goto out;
  209. ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
  210. if (ret < 0)
  211. goto out;
  212. af = variant->get_altfunc(stmpe, block);
  213. while (pins) {
  214. int pin = __ffs(pins);
  215. int regoffset = numregs - (pin / afperreg) - 1;
  216. int pos = (pin % afperreg) * (8 / afperreg);
  217. regs[regoffset] &= ~(mask << pos);
  218. regs[regoffset] |= af << pos;
  219. pins &= ~(1 << pin);
  220. }
  221. ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
  222. out:
  223. mutex_unlock(&stmpe->lock);
  224. return ret;
  225. }
  226. EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
  227. /*
  228. * GPIO (all variants)
  229. */
  230. static struct resource stmpe_gpio_resources[] = {
  231. /* Start and end filled dynamically */
  232. {
  233. .flags = IORESOURCE_IRQ,
  234. },
  235. };
  236. static struct mfd_cell stmpe_gpio_cell = {
  237. .name = "stmpe-gpio",
  238. .resources = stmpe_gpio_resources,
  239. .num_resources = ARRAY_SIZE(stmpe_gpio_resources),
  240. };
  241. /*
  242. * Keypad (1601, 2401, 2403)
  243. */
  244. static struct resource stmpe_keypad_resources[] = {
  245. {
  246. .name = "KEYPAD",
  247. .start = 0,
  248. .end = 0,
  249. .flags = IORESOURCE_IRQ,
  250. },
  251. {
  252. .name = "KEYPAD_OVER",
  253. .start = 1,
  254. .end = 1,
  255. .flags = IORESOURCE_IRQ,
  256. },
  257. };
  258. static struct mfd_cell stmpe_keypad_cell = {
  259. .name = "stmpe-keypad",
  260. .resources = stmpe_keypad_resources,
  261. .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
  262. };
  263. /*
  264. * STMPE801
  265. */
  266. static const u8 stmpe801_regs[] = {
  267. [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
  268. [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
  269. [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
  270. [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  271. [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  272. [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
  273. [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
  274. [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
  275. };
  276. static struct stmpe_variant_block stmpe801_blocks[] = {
  277. {
  278. .cell = &stmpe_gpio_cell,
  279. .irq = 0,
  280. .block = STMPE_BLOCK_GPIO,
  281. },
  282. };
  283. static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
  284. bool enable)
  285. {
  286. if (blocks & STMPE_BLOCK_GPIO)
  287. return 0;
  288. else
  289. return -EINVAL;
  290. }
  291. static struct stmpe_variant_info stmpe801 = {
  292. .name = "stmpe801",
  293. .id_val = STMPE801_ID,
  294. .id_mask = 0xffff,
  295. .num_gpios = 8,
  296. .regs = stmpe801_regs,
  297. .blocks = stmpe801_blocks,
  298. .num_blocks = ARRAY_SIZE(stmpe801_blocks),
  299. .num_irqs = STMPE801_NR_INTERNAL_IRQS,
  300. .enable = stmpe801_enable,
  301. };
  302. /*
  303. * Touchscreen (STMPE811 or STMPE610)
  304. */
  305. static struct resource stmpe_ts_resources[] = {
  306. {
  307. .name = "TOUCH_DET",
  308. .start = 0,
  309. .end = 0,
  310. .flags = IORESOURCE_IRQ,
  311. },
  312. {
  313. .name = "FIFO_TH",
  314. .start = 1,
  315. .end = 1,
  316. .flags = IORESOURCE_IRQ,
  317. },
  318. };
  319. static struct mfd_cell stmpe_ts_cell = {
  320. .name = "stmpe-ts",
  321. .resources = stmpe_ts_resources,
  322. .num_resources = ARRAY_SIZE(stmpe_ts_resources),
  323. };
  324. /*
  325. * STMPE811 or STMPE610
  326. */
  327. static const u8 stmpe811_regs[] = {
  328. [STMPE_IDX_CHIP_ID] = STMPE811_REG_CHIP_ID,
  329. [STMPE_IDX_ICR_LSB] = STMPE811_REG_INT_CTRL,
  330. [STMPE_IDX_IER_LSB] = STMPE811_REG_INT_EN,
  331. [STMPE_IDX_ISR_MSB] = STMPE811_REG_INT_STA,
  332. [STMPE_IDX_GPMR_LSB] = STMPE811_REG_GPIO_MP_STA,
  333. [STMPE_IDX_GPSR_LSB] = STMPE811_REG_GPIO_SET_PIN,
  334. [STMPE_IDX_GPCR_LSB] = STMPE811_REG_GPIO_CLR_PIN,
  335. [STMPE_IDX_GPDR_LSB] = STMPE811_REG_GPIO_DIR,
  336. [STMPE_IDX_GPRER_LSB] = STMPE811_REG_GPIO_RE,
  337. [STMPE_IDX_GPFER_LSB] = STMPE811_REG_GPIO_FE,
  338. [STMPE_IDX_GPAFR_U_MSB] = STMPE811_REG_GPIO_AF,
  339. [STMPE_IDX_IEGPIOR_LSB] = STMPE811_REG_GPIO_INT_EN,
  340. [STMPE_IDX_ISGPIOR_MSB] = STMPE811_REG_GPIO_INT_STA,
  341. [STMPE_IDX_GPEDR_MSB] = STMPE811_REG_GPIO_ED,
  342. };
  343. static struct stmpe_variant_block stmpe811_blocks[] = {
  344. {
  345. .cell = &stmpe_gpio_cell,
  346. .irq = STMPE811_IRQ_GPIOC,
  347. .block = STMPE_BLOCK_GPIO,
  348. },
  349. {
  350. .cell = &stmpe_ts_cell,
  351. .irq = STMPE811_IRQ_TOUCH_DET,
  352. .block = STMPE_BLOCK_TOUCHSCREEN,
  353. },
  354. };
  355. static int stmpe811_enable(struct stmpe *stmpe, unsigned int blocks,
  356. bool enable)
  357. {
  358. unsigned int mask = 0;
  359. if (blocks & STMPE_BLOCK_GPIO)
  360. mask |= STMPE811_SYS_CTRL2_GPIO_OFF;
  361. if (blocks & STMPE_BLOCK_ADC)
  362. mask |= STMPE811_SYS_CTRL2_ADC_OFF;
  363. if (blocks & STMPE_BLOCK_TOUCHSCREEN)
  364. mask |= STMPE811_SYS_CTRL2_TSC_OFF;
  365. return __stmpe_set_bits(stmpe, STMPE811_REG_SYS_CTRL2, mask,
  366. enable ? 0 : mask);
  367. }
  368. static int stmpe811_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  369. {
  370. /* 0 for touchscreen, 1 for GPIO */
  371. return block != STMPE_BLOCK_TOUCHSCREEN;
  372. }
  373. static struct stmpe_variant_info stmpe811 = {
  374. .name = "stmpe811",
  375. .id_val = 0x0811,
  376. .id_mask = 0xffff,
  377. .num_gpios = 8,
  378. .af_bits = 1,
  379. .regs = stmpe811_regs,
  380. .blocks = stmpe811_blocks,
  381. .num_blocks = ARRAY_SIZE(stmpe811_blocks),
  382. .num_irqs = STMPE811_NR_INTERNAL_IRQS,
  383. .enable = stmpe811_enable,
  384. .get_altfunc = stmpe811_get_altfunc,
  385. };
  386. /* Similar to 811, except number of gpios */
  387. static struct stmpe_variant_info stmpe610 = {
  388. .name = "stmpe610",
  389. .id_val = 0x0811,
  390. .id_mask = 0xffff,
  391. .num_gpios = 6,
  392. .af_bits = 1,
  393. .regs = stmpe811_regs,
  394. .blocks = stmpe811_blocks,
  395. .num_blocks = ARRAY_SIZE(stmpe811_blocks),
  396. .num_irqs = STMPE811_NR_INTERNAL_IRQS,
  397. .enable = stmpe811_enable,
  398. .get_altfunc = stmpe811_get_altfunc,
  399. };
  400. /*
  401. * STMPE1601
  402. */
  403. static const u8 stmpe1601_regs[] = {
  404. [STMPE_IDX_CHIP_ID] = STMPE1601_REG_CHIP_ID,
  405. [STMPE_IDX_ICR_LSB] = STMPE1601_REG_ICR_LSB,
  406. [STMPE_IDX_IER_LSB] = STMPE1601_REG_IER_LSB,
  407. [STMPE_IDX_ISR_MSB] = STMPE1601_REG_ISR_MSB,
  408. [STMPE_IDX_GPMR_LSB] = STMPE1601_REG_GPIO_MP_LSB,
  409. [STMPE_IDX_GPSR_LSB] = STMPE1601_REG_GPIO_SET_LSB,
  410. [STMPE_IDX_GPCR_LSB] = STMPE1601_REG_GPIO_CLR_LSB,
  411. [STMPE_IDX_GPDR_LSB] = STMPE1601_REG_GPIO_SET_DIR_LSB,
  412. [STMPE_IDX_GPRER_LSB] = STMPE1601_REG_GPIO_RE_LSB,
  413. [STMPE_IDX_GPFER_LSB] = STMPE1601_REG_GPIO_FE_LSB,
  414. [STMPE_IDX_GPAFR_U_MSB] = STMPE1601_REG_GPIO_AF_U_MSB,
  415. [STMPE_IDX_IEGPIOR_LSB] = STMPE1601_REG_INT_EN_GPIO_MASK_LSB,
  416. [STMPE_IDX_ISGPIOR_MSB] = STMPE1601_REG_INT_STA_GPIO_MSB,
  417. [STMPE_IDX_GPEDR_MSB] = STMPE1601_REG_GPIO_ED_MSB,
  418. };
  419. static struct stmpe_variant_block stmpe1601_blocks[] = {
  420. {
  421. .cell = &stmpe_gpio_cell,
  422. .irq = STMPE24XX_IRQ_GPIOC,
  423. .block = STMPE_BLOCK_GPIO,
  424. },
  425. {
  426. .cell = &stmpe_keypad_cell,
  427. .irq = STMPE24XX_IRQ_KEYPAD,
  428. .block = STMPE_BLOCK_KEYPAD,
  429. },
  430. };
  431. /* supported autosleep timeout delay (in msecs) */
  432. static const int stmpe_autosleep_delay[] = {
  433. 4, 16, 32, 64, 128, 256, 512, 1024,
  434. };
  435. static int stmpe_round_timeout(int timeout)
  436. {
  437. int i;
  438. for (i = 0; i < ARRAY_SIZE(stmpe_autosleep_delay); i++) {
  439. if (stmpe_autosleep_delay[i] >= timeout)
  440. return i;
  441. }
  442. /*
  443. * requests for delays longer than supported should not return the
  444. * longest supported delay
  445. */
  446. return -EINVAL;
  447. }
  448. static int stmpe_autosleep(struct stmpe *stmpe, int autosleep_timeout)
  449. {
  450. int ret;
  451. if (!stmpe->variant->enable_autosleep)
  452. return -ENOSYS;
  453. mutex_lock(&stmpe->lock);
  454. ret = stmpe->variant->enable_autosleep(stmpe, autosleep_timeout);
  455. mutex_unlock(&stmpe->lock);
  456. return ret;
  457. }
  458. /*
  459. * Both stmpe 1601/2403 support same layout for autosleep
  460. */
  461. static int stmpe1601_autosleep(struct stmpe *stmpe,
  462. int autosleep_timeout)
  463. {
  464. int ret, timeout;
  465. /* choose the best available timeout */
  466. timeout = stmpe_round_timeout(autosleep_timeout);
  467. if (timeout < 0) {
  468. dev_err(stmpe->dev, "invalid timeout\n");
  469. return timeout;
  470. }
  471. ret = __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
  472. STMPE1601_AUTOSLEEP_TIMEOUT_MASK,
  473. timeout);
  474. if (ret < 0)
  475. return ret;
  476. return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL2,
  477. STPME1601_AUTOSLEEP_ENABLE,
  478. STPME1601_AUTOSLEEP_ENABLE);
  479. }
  480. static int stmpe1601_enable(struct stmpe *stmpe, unsigned int blocks,
  481. bool enable)
  482. {
  483. unsigned int mask = 0;
  484. if (blocks & STMPE_BLOCK_GPIO)
  485. mask |= STMPE1601_SYS_CTRL_ENABLE_GPIO;
  486. if (blocks & STMPE_BLOCK_KEYPAD)
  487. mask |= STMPE1601_SYS_CTRL_ENABLE_KPC;
  488. return __stmpe_set_bits(stmpe, STMPE1601_REG_SYS_CTRL, mask,
  489. enable ? mask : 0);
  490. }
  491. static int stmpe1601_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  492. {
  493. switch (block) {
  494. case STMPE_BLOCK_PWM:
  495. return 2;
  496. case STMPE_BLOCK_KEYPAD:
  497. return 1;
  498. case STMPE_BLOCK_GPIO:
  499. default:
  500. return 0;
  501. }
  502. }
  503. static struct stmpe_variant_info stmpe1601 = {
  504. .name = "stmpe1601",
  505. .id_val = 0x0210,
  506. .id_mask = 0xfff0, /* at least 0x0210 and 0x0212 */
  507. .num_gpios = 16,
  508. .af_bits = 2,
  509. .regs = stmpe1601_regs,
  510. .blocks = stmpe1601_blocks,
  511. .num_blocks = ARRAY_SIZE(stmpe1601_blocks),
  512. .num_irqs = STMPE1601_NR_INTERNAL_IRQS,
  513. .enable = stmpe1601_enable,
  514. .get_altfunc = stmpe1601_get_altfunc,
  515. .enable_autosleep = stmpe1601_autosleep,
  516. };
  517. /*
  518. * STMPE24XX
  519. */
  520. static const u8 stmpe24xx_regs[] = {
  521. [STMPE_IDX_CHIP_ID] = STMPE24XX_REG_CHIP_ID,
  522. [STMPE_IDX_ICR_LSB] = STMPE24XX_REG_ICR_LSB,
  523. [STMPE_IDX_IER_LSB] = STMPE24XX_REG_IER_LSB,
  524. [STMPE_IDX_ISR_MSB] = STMPE24XX_REG_ISR_MSB,
  525. [STMPE_IDX_GPMR_LSB] = STMPE24XX_REG_GPMR_LSB,
  526. [STMPE_IDX_GPSR_LSB] = STMPE24XX_REG_GPSR_LSB,
  527. [STMPE_IDX_GPCR_LSB] = STMPE24XX_REG_GPCR_LSB,
  528. [STMPE_IDX_GPDR_LSB] = STMPE24XX_REG_GPDR_LSB,
  529. [STMPE_IDX_GPRER_LSB] = STMPE24XX_REG_GPRER_LSB,
  530. [STMPE_IDX_GPFER_LSB] = STMPE24XX_REG_GPFER_LSB,
  531. [STMPE_IDX_GPAFR_U_MSB] = STMPE24XX_REG_GPAFR_U_MSB,
  532. [STMPE_IDX_IEGPIOR_LSB] = STMPE24XX_REG_IEGPIOR_LSB,
  533. [STMPE_IDX_ISGPIOR_MSB] = STMPE24XX_REG_ISGPIOR_MSB,
  534. [STMPE_IDX_GPEDR_MSB] = STMPE24XX_REG_GPEDR_MSB,
  535. };
  536. static struct stmpe_variant_block stmpe24xx_blocks[] = {
  537. {
  538. .cell = &stmpe_gpio_cell,
  539. .irq = STMPE24XX_IRQ_GPIOC,
  540. .block = STMPE_BLOCK_GPIO,
  541. },
  542. {
  543. .cell = &stmpe_keypad_cell,
  544. .irq = STMPE24XX_IRQ_KEYPAD,
  545. .block = STMPE_BLOCK_KEYPAD,
  546. },
  547. };
  548. static int stmpe24xx_enable(struct stmpe *stmpe, unsigned int blocks,
  549. bool enable)
  550. {
  551. unsigned int mask = 0;
  552. if (blocks & STMPE_BLOCK_GPIO)
  553. mask |= STMPE24XX_SYS_CTRL_ENABLE_GPIO;
  554. if (blocks & STMPE_BLOCK_KEYPAD)
  555. mask |= STMPE24XX_SYS_CTRL_ENABLE_KPC;
  556. return __stmpe_set_bits(stmpe, STMPE24XX_REG_SYS_CTRL, mask,
  557. enable ? mask : 0);
  558. }
  559. static int stmpe24xx_get_altfunc(struct stmpe *stmpe, enum stmpe_block block)
  560. {
  561. switch (block) {
  562. case STMPE_BLOCK_ROTATOR:
  563. return 2;
  564. case STMPE_BLOCK_KEYPAD:
  565. return 1;
  566. case STMPE_BLOCK_GPIO:
  567. default:
  568. return 0;
  569. }
  570. }
  571. static struct stmpe_variant_info stmpe2401 = {
  572. .name = "stmpe2401",
  573. .id_val = 0x0101,
  574. .id_mask = 0xffff,
  575. .num_gpios = 24,
  576. .af_bits = 2,
  577. .regs = stmpe24xx_regs,
  578. .blocks = stmpe24xx_blocks,
  579. .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
  580. .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
  581. .enable = stmpe24xx_enable,
  582. .get_altfunc = stmpe24xx_get_altfunc,
  583. };
  584. static struct stmpe_variant_info stmpe2403 = {
  585. .name = "stmpe2403",
  586. .id_val = 0x0120,
  587. .id_mask = 0xffff,
  588. .num_gpios = 24,
  589. .af_bits = 2,
  590. .regs = stmpe24xx_regs,
  591. .blocks = stmpe24xx_blocks,
  592. .num_blocks = ARRAY_SIZE(stmpe24xx_blocks),
  593. .num_irqs = STMPE24XX_NR_INTERNAL_IRQS,
  594. .enable = stmpe24xx_enable,
  595. .get_altfunc = stmpe24xx_get_altfunc,
  596. .enable_autosleep = stmpe1601_autosleep, /* same as stmpe1601 */
  597. };
  598. static struct stmpe_variant_info *stmpe_variant_info[] = {
  599. [STMPE610] = &stmpe610,
  600. [STMPE801] = &stmpe801,
  601. [STMPE811] = &stmpe811,
  602. [STMPE1601] = &stmpe1601,
  603. [STMPE2401] = &stmpe2401,
  604. [STMPE2403] = &stmpe2403,
  605. };
  606. static irqreturn_t stmpe_irq(int irq, void *data)
  607. {
  608. struct stmpe *stmpe = data;
  609. struct stmpe_variant_info *variant = stmpe->variant;
  610. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  611. u8 israddr = stmpe->regs[STMPE_IDX_ISR_MSB];
  612. u8 isr[num];
  613. int ret;
  614. int i;
  615. if (variant->id_val == STMPE801_ID) {
  616. handle_nested_irq(stmpe->irq_base);
  617. return IRQ_HANDLED;
  618. }
  619. ret = stmpe_block_read(stmpe, israddr, num, isr);
  620. if (ret < 0)
  621. return IRQ_NONE;
  622. for (i = 0; i < num; i++) {
  623. int bank = num - i - 1;
  624. u8 status = isr[i];
  625. u8 clear;
  626. status &= stmpe->ier[bank];
  627. if (!status)
  628. continue;
  629. clear = status;
  630. while (status) {
  631. int bit = __ffs(status);
  632. int line = bank * 8 + bit;
  633. handle_nested_irq(stmpe->irq_base + line);
  634. status &= ~(1 << bit);
  635. }
  636. stmpe_reg_write(stmpe, israddr + i, clear);
  637. }
  638. return IRQ_HANDLED;
  639. }
  640. static void stmpe_irq_lock(struct irq_data *data)
  641. {
  642. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  643. mutex_lock(&stmpe->irq_lock);
  644. }
  645. static void stmpe_irq_sync_unlock(struct irq_data *data)
  646. {
  647. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  648. struct stmpe_variant_info *variant = stmpe->variant;
  649. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  650. int i;
  651. for (i = 0; i < num; i++) {
  652. u8 new = stmpe->ier[i];
  653. u8 old = stmpe->oldier[i];
  654. if (new == old)
  655. continue;
  656. stmpe->oldier[i] = new;
  657. stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
  658. }
  659. mutex_unlock(&stmpe->irq_lock);
  660. }
  661. static void stmpe_irq_mask(struct irq_data *data)
  662. {
  663. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  664. int offset = data->irq - stmpe->irq_base;
  665. int regoffset = offset / 8;
  666. int mask = 1 << (offset % 8);
  667. stmpe->ier[regoffset] &= ~mask;
  668. }
  669. static void stmpe_irq_unmask(struct irq_data *data)
  670. {
  671. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  672. int offset = data->irq - stmpe->irq_base;
  673. int regoffset = offset / 8;
  674. int mask = 1 << (offset % 8);
  675. stmpe->ier[regoffset] |= mask;
  676. }
  677. static struct irq_chip stmpe_irq_chip = {
  678. .name = "stmpe",
  679. .irq_bus_lock = stmpe_irq_lock,
  680. .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
  681. .irq_mask = stmpe_irq_mask,
  682. .irq_unmask = stmpe_irq_unmask,
  683. };
  684. static int __devinit stmpe_irq_init(struct stmpe *stmpe)
  685. {
  686. struct irq_chip *chip = NULL;
  687. int num_irqs = stmpe->variant->num_irqs;
  688. int base = stmpe->irq_base;
  689. int irq;
  690. if (stmpe->variant->id_val != STMPE801_ID)
  691. chip = &stmpe_irq_chip;
  692. for (irq = base; irq < base + num_irqs; irq++) {
  693. irq_set_chip_data(irq, stmpe);
  694. irq_set_chip_and_handler(irq, chip, handle_edge_irq);
  695. irq_set_nested_thread(irq, 1);
  696. #ifdef CONFIG_ARM
  697. set_irq_flags(irq, IRQF_VALID);
  698. #else
  699. irq_set_noprobe(irq);
  700. #endif
  701. }
  702. return 0;
  703. }
  704. static void stmpe_irq_remove(struct stmpe *stmpe)
  705. {
  706. int num_irqs = stmpe->variant->num_irqs;
  707. int base = stmpe->irq_base;
  708. int irq;
  709. for (irq = base; irq < base + num_irqs; irq++) {
  710. #ifdef CONFIG_ARM
  711. set_irq_flags(irq, 0);
  712. #endif
  713. irq_set_chip_and_handler(irq, NULL, NULL);
  714. irq_set_chip_data(irq, NULL);
  715. }
  716. }
  717. static int __devinit stmpe_chip_init(struct stmpe *stmpe)
  718. {
  719. unsigned int irq_trigger = stmpe->pdata->irq_trigger;
  720. int autosleep_timeout = stmpe->pdata->autosleep_timeout;
  721. struct stmpe_variant_info *variant = stmpe->variant;
  722. u8 icr;
  723. unsigned int id;
  724. u8 data[2];
  725. int ret;
  726. ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
  727. ARRAY_SIZE(data), data);
  728. if (ret < 0)
  729. return ret;
  730. id = (data[0] << 8) | data[1];
  731. if ((id & variant->id_mask) != variant->id_val) {
  732. dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
  733. return -EINVAL;
  734. }
  735. dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
  736. /* Disable all modules -- subdrivers should enable what they need. */
  737. ret = stmpe_disable(stmpe, ~0);
  738. if (ret)
  739. return ret;
  740. if (id == STMPE801_ID)
  741. icr = STMPE801_REG_SYS_CTRL_INT_EN;
  742. else
  743. icr = STMPE_ICR_LSB_GIM;
  744. /* STMPE801 doesn't support Edge interrupts */
  745. if (id != STMPE801_ID) {
  746. if (irq_trigger == IRQF_TRIGGER_FALLING ||
  747. irq_trigger == IRQF_TRIGGER_RISING)
  748. icr |= STMPE_ICR_LSB_EDGE;
  749. }
  750. if (irq_trigger == IRQF_TRIGGER_RISING ||
  751. irq_trigger == IRQF_TRIGGER_HIGH) {
  752. if (id == STMPE801_ID)
  753. icr |= STMPE801_REG_SYS_CTRL_INT_HI;
  754. else
  755. icr |= STMPE_ICR_LSB_HIGH;
  756. }
  757. if (stmpe->pdata->irq_invert_polarity) {
  758. if (id == STMPE801_ID)
  759. icr ^= STMPE801_REG_SYS_CTRL_INT_HI;
  760. else
  761. icr ^= STMPE_ICR_LSB_HIGH;
  762. }
  763. if (stmpe->pdata->autosleep) {
  764. ret = stmpe_autosleep(stmpe, autosleep_timeout);
  765. if (ret)
  766. return ret;
  767. }
  768. return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
  769. }
  770. static int __devinit stmpe_add_device(struct stmpe *stmpe,
  771. struct mfd_cell *cell, int irq)
  772. {
  773. return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
  774. NULL, stmpe->irq_base + irq);
  775. }
  776. static int __devinit stmpe_devices_init(struct stmpe *stmpe)
  777. {
  778. struct stmpe_variant_info *variant = stmpe->variant;
  779. unsigned int platform_blocks = stmpe->pdata->blocks;
  780. int ret = -EINVAL;
  781. int i;
  782. for (i = 0; i < variant->num_blocks; i++) {
  783. struct stmpe_variant_block *block = &variant->blocks[i];
  784. if (!(platform_blocks & block->block))
  785. continue;
  786. platform_blocks &= ~block->block;
  787. ret = stmpe_add_device(stmpe, block->cell, block->irq);
  788. if (ret)
  789. return ret;
  790. }
  791. if (platform_blocks)
  792. dev_warn(stmpe->dev,
  793. "platform wants blocks (%#x) not present on variant",
  794. platform_blocks);
  795. return ret;
  796. }
  797. /* Called from client specific probe routines */
  798. int stmpe_probe(struct stmpe_client_info *ci, int partnum)
  799. {
  800. struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
  801. struct stmpe *stmpe;
  802. int ret;
  803. if (!pdata)
  804. return -EINVAL;
  805. stmpe = kzalloc(sizeof(struct stmpe), GFP_KERNEL);
  806. if (!stmpe)
  807. return -ENOMEM;
  808. mutex_init(&stmpe->irq_lock);
  809. mutex_init(&stmpe->lock);
  810. stmpe->dev = ci->dev;
  811. stmpe->client = ci->client;
  812. stmpe->pdata = pdata;
  813. stmpe->irq_base = pdata->irq_base;
  814. stmpe->ci = ci;
  815. stmpe->partnum = partnum;
  816. stmpe->variant = stmpe_variant_info[partnum];
  817. stmpe->regs = stmpe->variant->regs;
  818. stmpe->num_gpios = stmpe->variant->num_gpios;
  819. dev_set_drvdata(stmpe->dev, stmpe);
  820. if (ci->init)
  821. ci->init(stmpe);
  822. if (pdata->irq_over_gpio) {
  823. ret = gpio_request_one(pdata->irq_gpio, GPIOF_DIR_IN, "stmpe");
  824. if (ret) {
  825. dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
  826. ret);
  827. goto out_free;
  828. }
  829. stmpe->irq = gpio_to_irq(pdata->irq_gpio);
  830. } else {
  831. stmpe->irq = ci->irq;
  832. }
  833. ret = stmpe_chip_init(stmpe);
  834. if (ret)
  835. goto free_gpio;
  836. ret = stmpe_irq_init(stmpe);
  837. if (ret)
  838. goto free_gpio;
  839. ret = request_threaded_irq(stmpe->irq, NULL, stmpe_irq,
  840. pdata->irq_trigger | IRQF_ONESHOT, "stmpe", stmpe);
  841. if (ret) {
  842. dev_err(stmpe->dev, "failed to request IRQ: %d\n", ret);
  843. goto out_removeirq;
  844. }
  845. ret = stmpe_devices_init(stmpe);
  846. if (ret) {
  847. dev_err(stmpe->dev, "failed to add children\n");
  848. goto out_removedevs;
  849. }
  850. return 0;
  851. out_removedevs:
  852. mfd_remove_devices(stmpe->dev);
  853. free_irq(stmpe->irq, stmpe);
  854. out_removeirq:
  855. stmpe_irq_remove(stmpe);
  856. free_gpio:
  857. if (pdata->irq_over_gpio)
  858. gpio_free(pdata->irq_gpio);
  859. out_free:
  860. kfree(stmpe);
  861. return ret;
  862. }
  863. int stmpe_remove(struct stmpe *stmpe)
  864. {
  865. mfd_remove_devices(stmpe->dev);
  866. free_irq(stmpe->irq, stmpe);
  867. stmpe_irq_remove(stmpe);
  868. if (stmpe->pdata->irq_over_gpio)
  869. gpio_free(stmpe->pdata->irq_gpio);
  870. kfree(stmpe);
  871. return 0;
  872. }
  873. #ifdef CONFIG_PM
  874. static int stmpe_suspend(struct device *dev)
  875. {
  876. struct stmpe *stmpe = dev_get_drvdata(dev);
  877. if (device_may_wakeup(dev))
  878. enable_irq_wake(stmpe->irq);
  879. return 0;
  880. }
  881. static int stmpe_resume(struct device *dev)
  882. {
  883. struct stmpe *stmpe = dev_get_drvdata(dev);
  884. if (device_may_wakeup(dev))
  885. disable_irq_wake(stmpe->irq);
  886. return 0;
  887. }
  888. const struct dev_pm_ops stmpe_dev_pm_ops = {
  889. .suspend = stmpe_suspend,
  890. .resume = stmpe_resume,
  891. };
  892. #endif