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