stmpe.c 27 KB

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