stmpe.c 26 KB

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