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