stmpe.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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/err.h>
  10. #include <linux/gpio.h>
  11. #include <linux/export.h>
  12. #include <linux/kernel.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/of.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/pm.h>
  19. #include <linux/slab.h>
  20. #include <linux/mfd/core.h>
  21. #include "stmpe.h"
  22. static int __stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
  23. {
  24. return stmpe->variant->enable(stmpe, blocks, true);
  25. }
  26. static int __stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
  27. {
  28. return stmpe->variant->enable(stmpe, blocks, false);
  29. }
  30. static int __stmpe_reg_read(struct stmpe *stmpe, u8 reg)
  31. {
  32. int ret;
  33. ret = stmpe->ci->read_byte(stmpe, reg);
  34. if (ret < 0)
  35. dev_err(stmpe->dev, "failed to read reg %#x: %d\n", reg, ret);
  36. dev_vdbg(stmpe->dev, "rd: reg %#x => data %#x\n", reg, ret);
  37. return ret;
  38. }
  39. static int __stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
  40. {
  41. int ret;
  42. dev_vdbg(stmpe->dev, "wr: reg %#x <= %#x\n", reg, val);
  43. ret = stmpe->ci->write_byte(stmpe, reg, val);
  44. if (ret < 0)
  45. dev_err(stmpe->dev, "failed to write reg %#x: %d\n", reg, ret);
  46. return ret;
  47. }
  48. static int __stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
  49. {
  50. int ret;
  51. ret = __stmpe_reg_read(stmpe, reg);
  52. if (ret < 0)
  53. return ret;
  54. ret &= ~mask;
  55. ret |= val;
  56. return __stmpe_reg_write(stmpe, reg, ret);
  57. }
  58. static int __stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length,
  59. u8 *values)
  60. {
  61. int ret;
  62. ret = stmpe->ci->read_block(stmpe, reg, length, values);
  63. if (ret < 0)
  64. dev_err(stmpe->dev, "failed to read regs %#x: %d\n", reg, ret);
  65. dev_vdbg(stmpe->dev, "rd: reg %#x (%d) => ret %#x\n", reg, length, ret);
  66. stmpe_dump_bytes("stmpe rd: ", values, length);
  67. return ret;
  68. }
  69. static int __stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
  70. const u8 *values)
  71. {
  72. int ret;
  73. dev_vdbg(stmpe->dev, "wr: regs %#x (%d)\n", reg, length);
  74. stmpe_dump_bytes("stmpe wr: ", values, length);
  75. ret = stmpe->ci->write_block(stmpe, reg, length, values);
  76. if (ret < 0)
  77. dev_err(stmpe->dev, "failed to write regs %#x: %d\n", reg, ret);
  78. return ret;
  79. }
  80. /**
  81. * stmpe_enable - enable blocks on an STMPE device
  82. * @stmpe: Device to work on
  83. * @blocks: Mask of blocks (enum stmpe_block values) to enable
  84. */
  85. int stmpe_enable(struct stmpe *stmpe, unsigned int blocks)
  86. {
  87. int ret;
  88. mutex_lock(&stmpe->lock);
  89. ret = __stmpe_enable(stmpe, blocks);
  90. mutex_unlock(&stmpe->lock);
  91. return ret;
  92. }
  93. EXPORT_SYMBOL_GPL(stmpe_enable);
  94. /**
  95. * stmpe_disable - disable blocks on an STMPE device
  96. * @stmpe: Device to work on
  97. * @blocks: Mask of blocks (enum stmpe_block values) to enable
  98. */
  99. int stmpe_disable(struct stmpe *stmpe, unsigned int blocks)
  100. {
  101. int ret;
  102. mutex_lock(&stmpe->lock);
  103. ret = __stmpe_disable(stmpe, blocks);
  104. mutex_unlock(&stmpe->lock);
  105. return ret;
  106. }
  107. EXPORT_SYMBOL_GPL(stmpe_disable);
  108. /**
  109. * stmpe_reg_read() - read a single STMPE register
  110. * @stmpe: Device to read from
  111. * @reg: Register to read
  112. */
  113. int stmpe_reg_read(struct stmpe *stmpe, u8 reg)
  114. {
  115. int ret;
  116. mutex_lock(&stmpe->lock);
  117. ret = __stmpe_reg_read(stmpe, reg);
  118. mutex_unlock(&stmpe->lock);
  119. return ret;
  120. }
  121. EXPORT_SYMBOL_GPL(stmpe_reg_read);
  122. /**
  123. * stmpe_reg_write() - write a single STMPE register
  124. * @stmpe: Device to write to
  125. * @reg: Register to write
  126. * @val: Value to write
  127. */
  128. int stmpe_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
  129. {
  130. int ret;
  131. mutex_lock(&stmpe->lock);
  132. ret = __stmpe_reg_write(stmpe, reg, val);
  133. mutex_unlock(&stmpe->lock);
  134. return ret;
  135. }
  136. EXPORT_SYMBOL_GPL(stmpe_reg_write);
  137. /**
  138. * stmpe_set_bits() - set the value of a bitfield in a STMPE register
  139. * @stmpe: Device to write to
  140. * @reg: Register to write
  141. * @mask: Mask of bits to set
  142. * @val: Value to set
  143. */
  144. int stmpe_set_bits(struct stmpe *stmpe, u8 reg, u8 mask, u8 val)
  145. {
  146. int ret;
  147. mutex_lock(&stmpe->lock);
  148. ret = __stmpe_set_bits(stmpe, reg, mask, val);
  149. mutex_unlock(&stmpe->lock);
  150. return ret;
  151. }
  152. EXPORT_SYMBOL_GPL(stmpe_set_bits);
  153. /**
  154. * stmpe_block_read() - read multiple STMPE registers
  155. * @stmpe: Device to read from
  156. * @reg: First register
  157. * @length: Number of registers
  158. * @values: Buffer to write to
  159. */
  160. int stmpe_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
  161. {
  162. int ret;
  163. mutex_lock(&stmpe->lock);
  164. ret = __stmpe_block_read(stmpe, reg, length, values);
  165. mutex_unlock(&stmpe->lock);
  166. return ret;
  167. }
  168. EXPORT_SYMBOL_GPL(stmpe_block_read);
  169. /**
  170. * stmpe_block_write() - write multiple STMPE registers
  171. * @stmpe: Device to write to
  172. * @reg: First register
  173. * @length: Number of registers
  174. * @values: Values to write
  175. */
  176. int stmpe_block_write(struct stmpe *stmpe, u8 reg, u8 length,
  177. const u8 *values)
  178. {
  179. int ret;
  180. mutex_lock(&stmpe->lock);
  181. ret = __stmpe_block_write(stmpe, reg, length, values);
  182. mutex_unlock(&stmpe->lock);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL_GPL(stmpe_block_write);
  186. /**
  187. * stmpe_set_altfunc()- set the alternate function for STMPE pins
  188. * @stmpe: Device to configure
  189. * @pins: Bitmask of pins to affect
  190. * @block: block to enable alternate functions for
  191. *
  192. * @pins is assumed to have a bit set for each of the bits whose alternate
  193. * function is to be changed, numbered according to the GPIOXY numbers.
  194. *
  195. * If the GPIO module is not enabled, this function automatically enables it in
  196. * order to perform the change.
  197. */
  198. int stmpe_set_altfunc(struct stmpe *stmpe, u32 pins, enum stmpe_block block)
  199. {
  200. struct stmpe_variant_info *variant = stmpe->variant;
  201. u8 regaddr = stmpe->regs[STMPE_IDX_GPAFR_U_MSB];
  202. int af_bits = variant->af_bits;
  203. int numregs = DIV_ROUND_UP(stmpe->num_gpios * af_bits, 8);
  204. int mask = (1 << af_bits) - 1;
  205. u8 regs[numregs];
  206. int af, afperreg, ret;
  207. if (!variant->get_altfunc)
  208. return 0;
  209. afperreg = 8 / af_bits;
  210. mutex_lock(&stmpe->lock);
  211. ret = __stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
  212. if (ret < 0)
  213. goto out;
  214. ret = __stmpe_block_read(stmpe, regaddr, numregs, regs);
  215. if (ret < 0)
  216. goto out;
  217. af = variant->get_altfunc(stmpe, block);
  218. while (pins) {
  219. int pin = __ffs(pins);
  220. int regoffset = numregs - (pin / afperreg) - 1;
  221. int pos = (pin % afperreg) * (8 / afperreg);
  222. regs[regoffset] &= ~(mask << pos);
  223. regs[regoffset] |= af << pos;
  224. pins &= ~(1 << pin);
  225. }
  226. ret = __stmpe_block_write(stmpe, regaddr, numregs, regs);
  227. out:
  228. mutex_unlock(&stmpe->lock);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_GPL(stmpe_set_altfunc);
  232. /*
  233. * GPIO (all variants)
  234. */
  235. static struct resource stmpe_gpio_resources[] = {
  236. /* Start and end filled dynamically */
  237. {
  238. .flags = IORESOURCE_IRQ,
  239. },
  240. };
  241. static struct mfd_cell stmpe_gpio_cell = {
  242. .name = "stmpe-gpio",
  243. .of_compatible = "st,stmpe-gpio",
  244. .resources = stmpe_gpio_resources,
  245. .num_resources = ARRAY_SIZE(stmpe_gpio_resources),
  246. };
  247. static struct mfd_cell stmpe_gpio_cell_noirq = {
  248. .name = "stmpe-gpio",
  249. .of_compatible = "st,stmpe-gpio",
  250. /* gpio cell resources consist of an irq only so no resources here */
  251. };
  252. /*
  253. * Keypad (1601, 2401, 2403)
  254. */
  255. static struct resource stmpe_keypad_resources[] = {
  256. {
  257. .name = "KEYPAD",
  258. .flags = IORESOURCE_IRQ,
  259. },
  260. {
  261. .name = "KEYPAD_OVER",
  262. .flags = IORESOURCE_IRQ,
  263. },
  264. };
  265. static struct mfd_cell stmpe_keypad_cell = {
  266. .name = "stmpe-keypad",
  267. .of_compatible = "st,stmpe-keypad",
  268. .resources = stmpe_keypad_resources,
  269. .num_resources = ARRAY_SIZE(stmpe_keypad_resources),
  270. };
  271. /*
  272. * STMPE801
  273. */
  274. static const u8 stmpe801_regs[] = {
  275. [STMPE_IDX_CHIP_ID] = STMPE801_REG_CHIP_ID,
  276. [STMPE_IDX_ICR_LSB] = STMPE801_REG_SYS_CTRL,
  277. [STMPE_IDX_GPMR_LSB] = STMPE801_REG_GPIO_MP_STA,
  278. [STMPE_IDX_GPSR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  279. [STMPE_IDX_GPCR_LSB] = STMPE801_REG_GPIO_SET_PIN,
  280. [STMPE_IDX_GPDR_LSB] = STMPE801_REG_GPIO_DIR,
  281. [STMPE_IDX_IEGPIOR_LSB] = STMPE801_REG_GPIO_INT_EN,
  282. [STMPE_IDX_ISGPIOR_MSB] = STMPE801_REG_GPIO_INT_STA,
  283. };
  284. static struct stmpe_variant_block stmpe801_blocks[] = {
  285. {
  286. .cell = &stmpe_gpio_cell,
  287. .irq = 0,
  288. .block = STMPE_BLOCK_GPIO,
  289. },
  290. };
  291. static struct stmpe_variant_block stmpe801_blocks_noirq[] = {
  292. {
  293. .cell = &stmpe_gpio_cell_noirq,
  294. .block = STMPE_BLOCK_GPIO,
  295. },
  296. };
  297. static int stmpe801_enable(struct stmpe *stmpe, unsigned int blocks,
  298. bool enable)
  299. {
  300. if (blocks & STMPE_BLOCK_GPIO)
  301. return 0;
  302. else
  303. return -EINVAL;
  304. }
  305. static struct stmpe_variant_info stmpe801 = {
  306. .name = "stmpe801",
  307. .id_val = STMPE801_ID,
  308. .id_mask = 0xffff,
  309. .num_gpios = 8,
  310. .regs = stmpe801_regs,
  311. .blocks = stmpe801_blocks,
  312. .num_blocks = ARRAY_SIZE(stmpe801_blocks),
  313. .num_irqs = STMPE801_NR_INTERNAL_IRQS,
  314. .enable = stmpe801_enable,
  315. };
  316. static struct stmpe_variant_info stmpe801_noirq = {
  317. .name = "stmpe801",
  318. .id_val = STMPE801_ID,
  319. .id_mask = 0xffff,
  320. .num_gpios = 8,
  321. .regs = stmpe801_regs,
  322. .blocks = stmpe801_blocks_noirq,
  323. .num_blocks = ARRAY_SIZE(stmpe801_blocks_noirq),
  324. .enable = stmpe801_enable,
  325. };
  326. /*
  327. * Touchscreen (STMPE811 or STMPE610)
  328. */
  329. static struct resource stmpe_ts_resources[] = {
  330. {
  331. .name = "TOUCH_DET",
  332. .flags = IORESOURCE_IRQ,
  333. },
  334. {
  335. .name = "FIFO_TH",
  336. .flags = IORESOURCE_IRQ,
  337. },
  338. };
  339. static struct mfd_cell stmpe_ts_cell = {
  340. .name = "stmpe-ts",
  341. .of_compatible = "st,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 = STMPE1601_IRQ_GPIOC,
  444. .block = STMPE_BLOCK_GPIO,
  445. },
  446. {
  447. .cell = &stmpe_keypad_cell,
  448. .irq = STMPE1601_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. int base = irq_create_mapping(stmpe->domain, 0);
  647. handle_nested_irq(base);
  648. return IRQ_HANDLED;
  649. }
  650. ret = stmpe_block_read(stmpe, israddr, num, isr);
  651. if (ret < 0)
  652. return IRQ_NONE;
  653. for (i = 0; i < num; i++) {
  654. int bank = num - i - 1;
  655. u8 status = isr[i];
  656. u8 clear;
  657. status &= stmpe->ier[bank];
  658. if (!status)
  659. continue;
  660. clear = status;
  661. while (status) {
  662. int bit = __ffs(status);
  663. int line = bank * 8 + bit;
  664. int nestedirq = irq_create_mapping(stmpe->domain, line);
  665. handle_nested_irq(nestedirq);
  666. status &= ~(1 << bit);
  667. }
  668. stmpe_reg_write(stmpe, israddr + i, clear);
  669. }
  670. return IRQ_HANDLED;
  671. }
  672. static void stmpe_irq_lock(struct irq_data *data)
  673. {
  674. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  675. mutex_lock(&stmpe->irq_lock);
  676. }
  677. static void stmpe_irq_sync_unlock(struct irq_data *data)
  678. {
  679. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  680. struct stmpe_variant_info *variant = stmpe->variant;
  681. int num = DIV_ROUND_UP(variant->num_irqs, 8);
  682. int i;
  683. for (i = 0; i < num; i++) {
  684. u8 new = stmpe->ier[i];
  685. u8 old = stmpe->oldier[i];
  686. if (new == old)
  687. continue;
  688. stmpe->oldier[i] = new;
  689. stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_IER_LSB] - i, new);
  690. }
  691. mutex_unlock(&stmpe->irq_lock);
  692. }
  693. static void stmpe_irq_mask(struct irq_data *data)
  694. {
  695. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  696. int offset = data->hwirq;
  697. int regoffset = offset / 8;
  698. int mask = 1 << (offset % 8);
  699. stmpe->ier[regoffset] &= ~mask;
  700. }
  701. static void stmpe_irq_unmask(struct irq_data *data)
  702. {
  703. struct stmpe *stmpe = irq_data_get_irq_chip_data(data);
  704. int offset = data->hwirq;
  705. int regoffset = offset / 8;
  706. int mask = 1 << (offset % 8);
  707. stmpe->ier[regoffset] |= mask;
  708. }
  709. static struct irq_chip stmpe_irq_chip = {
  710. .name = "stmpe",
  711. .irq_bus_lock = stmpe_irq_lock,
  712. .irq_bus_sync_unlock = stmpe_irq_sync_unlock,
  713. .irq_mask = stmpe_irq_mask,
  714. .irq_unmask = stmpe_irq_unmask,
  715. };
  716. static int stmpe_irq_map(struct irq_domain *d, unsigned int virq,
  717. irq_hw_number_t hwirq)
  718. {
  719. struct stmpe *stmpe = d->host_data;
  720. struct irq_chip *chip = NULL;
  721. if (stmpe->variant->id_val != STMPE801_ID)
  722. chip = &stmpe_irq_chip;
  723. irq_set_chip_data(virq, stmpe);
  724. irq_set_chip_and_handler(virq, chip, handle_edge_irq);
  725. irq_set_nested_thread(virq, 1);
  726. #ifdef CONFIG_ARM
  727. set_irq_flags(virq, IRQF_VALID);
  728. #else
  729. irq_set_noprobe(virq);
  730. #endif
  731. return 0;
  732. }
  733. static void stmpe_irq_unmap(struct irq_domain *d, unsigned int virq)
  734. {
  735. #ifdef CONFIG_ARM
  736. set_irq_flags(virq, 0);
  737. #endif
  738. irq_set_chip_and_handler(virq, NULL, NULL);
  739. irq_set_chip_data(virq, NULL);
  740. }
  741. static struct irq_domain_ops stmpe_irq_ops = {
  742. .map = stmpe_irq_map,
  743. .unmap = stmpe_irq_unmap,
  744. .xlate = irq_domain_xlate_twocell,
  745. };
  746. static int stmpe_irq_init(struct stmpe *stmpe, struct device_node *np)
  747. {
  748. int base = 0;
  749. int num_irqs = stmpe->variant->num_irqs;
  750. if (!np)
  751. base = stmpe->irq_base;
  752. stmpe->domain = irq_domain_add_simple(np, num_irqs, base,
  753. &stmpe_irq_ops, stmpe);
  754. if (!stmpe->domain) {
  755. dev_err(stmpe->dev, "Failed to create irqdomain\n");
  756. return -ENOSYS;
  757. }
  758. return 0;
  759. }
  760. static int stmpe_chip_init(struct stmpe *stmpe)
  761. {
  762. unsigned int irq_trigger = stmpe->pdata->irq_trigger;
  763. int autosleep_timeout = stmpe->pdata->autosleep_timeout;
  764. struct stmpe_variant_info *variant = stmpe->variant;
  765. u8 icr = 0;
  766. unsigned int id;
  767. u8 data[2];
  768. int ret;
  769. ret = stmpe_block_read(stmpe, stmpe->regs[STMPE_IDX_CHIP_ID],
  770. ARRAY_SIZE(data), data);
  771. if (ret < 0)
  772. return ret;
  773. id = (data[0] << 8) | data[1];
  774. if ((id & variant->id_mask) != variant->id_val) {
  775. dev_err(stmpe->dev, "unknown chip id: %#x\n", id);
  776. return -EINVAL;
  777. }
  778. dev_info(stmpe->dev, "%s detected, chip id: %#x\n", variant->name, id);
  779. /* Disable all modules -- subdrivers should enable what they need. */
  780. ret = stmpe_disable(stmpe, ~0);
  781. if (ret)
  782. return ret;
  783. if (stmpe->irq >= 0) {
  784. if (id == STMPE801_ID)
  785. icr = STMPE801_REG_SYS_CTRL_INT_EN;
  786. else
  787. icr = STMPE_ICR_LSB_GIM;
  788. /* STMPE801 doesn't support Edge interrupts */
  789. if (id != STMPE801_ID) {
  790. if (irq_trigger == IRQF_TRIGGER_FALLING ||
  791. irq_trigger == IRQF_TRIGGER_RISING)
  792. icr |= STMPE_ICR_LSB_EDGE;
  793. }
  794. if (irq_trigger == IRQF_TRIGGER_RISING ||
  795. irq_trigger == IRQF_TRIGGER_HIGH) {
  796. if (id == STMPE801_ID)
  797. icr |= STMPE801_REG_SYS_CTRL_INT_HI;
  798. else
  799. icr |= STMPE_ICR_LSB_HIGH;
  800. }
  801. }
  802. if (stmpe->pdata->autosleep) {
  803. ret = stmpe_autosleep(stmpe, autosleep_timeout);
  804. if (ret)
  805. return ret;
  806. }
  807. return stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_ICR_LSB], icr);
  808. }
  809. static int stmpe_add_device(struct stmpe *stmpe, struct mfd_cell *cell)
  810. {
  811. return mfd_add_devices(stmpe->dev, stmpe->pdata->id, cell, 1,
  812. NULL, stmpe->irq_base, stmpe->domain);
  813. }
  814. static int stmpe_devices_init(struct stmpe *stmpe)
  815. {
  816. struct stmpe_variant_info *variant = stmpe->variant;
  817. unsigned int platform_blocks = stmpe->pdata->blocks;
  818. int ret = -EINVAL;
  819. int i, j;
  820. for (i = 0; i < variant->num_blocks; i++) {
  821. struct stmpe_variant_block *block = &variant->blocks[i];
  822. if (!(platform_blocks & block->block))
  823. continue;
  824. for (j = 0; j < block->cell->num_resources; j++) {
  825. struct resource *res =
  826. (struct resource *) &block->cell->resources[j];
  827. /* Dynamically fill in a variant's IRQ. */
  828. if (res->flags & IORESOURCE_IRQ)
  829. res->start = res->end = block->irq + j;
  830. }
  831. platform_blocks &= ~block->block;
  832. ret = stmpe_add_device(stmpe, block->cell);
  833. if (ret)
  834. return ret;
  835. }
  836. if (platform_blocks)
  837. dev_warn(stmpe->dev,
  838. "platform wants blocks (%#x) not present on variant",
  839. platform_blocks);
  840. return ret;
  841. }
  842. void stmpe_of_probe(struct stmpe_platform_data *pdata, struct device_node *np)
  843. {
  844. struct device_node *child;
  845. pdata->id = -1;
  846. pdata->irq_trigger = IRQF_TRIGGER_NONE;
  847. of_property_read_u32(np, "st,autosleep-timeout",
  848. &pdata->autosleep_timeout);
  849. pdata->autosleep = (pdata->autosleep_timeout) ? true : false;
  850. for_each_child_of_node(np, child) {
  851. if (!strcmp(child->name, "stmpe_gpio")) {
  852. pdata->blocks |= STMPE_BLOCK_GPIO;
  853. } else if (!strcmp(child->name, "stmpe_keypad")) {
  854. pdata->blocks |= STMPE_BLOCK_KEYPAD;
  855. } else if (!strcmp(child->name, "stmpe_touchscreen")) {
  856. pdata->blocks |= STMPE_BLOCK_TOUCHSCREEN;
  857. } else if (!strcmp(child->name, "stmpe_adc")) {
  858. pdata->blocks |= STMPE_BLOCK_ADC;
  859. } else if (!strcmp(child->name, "stmpe_pwm")) {
  860. pdata->blocks |= STMPE_BLOCK_PWM;
  861. } else if (!strcmp(child->name, "stmpe_rotator")) {
  862. pdata->blocks |= STMPE_BLOCK_ROTATOR;
  863. }
  864. }
  865. }
  866. /* Called from client specific probe routines */
  867. int stmpe_probe(struct stmpe_client_info *ci, int partnum)
  868. {
  869. struct stmpe_platform_data *pdata = dev_get_platdata(ci->dev);
  870. struct device_node *np = ci->dev->of_node;
  871. struct stmpe *stmpe;
  872. int ret;
  873. if (!pdata) {
  874. if (!np)
  875. return -EINVAL;
  876. pdata = devm_kzalloc(ci->dev, sizeof(*pdata), GFP_KERNEL);
  877. if (!pdata)
  878. return -ENOMEM;
  879. stmpe_of_probe(pdata, np);
  880. }
  881. stmpe = devm_kzalloc(ci->dev, sizeof(struct stmpe), GFP_KERNEL);
  882. if (!stmpe)
  883. return -ENOMEM;
  884. mutex_init(&stmpe->irq_lock);
  885. mutex_init(&stmpe->lock);
  886. stmpe->dev = ci->dev;
  887. stmpe->client = ci->client;
  888. stmpe->pdata = pdata;
  889. stmpe->irq_base = pdata->irq_base;
  890. stmpe->ci = ci;
  891. stmpe->partnum = partnum;
  892. stmpe->variant = stmpe_variant_info[partnum];
  893. stmpe->regs = stmpe->variant->regs;
  894. stmpe->num_gpios = stmpe->variant->num_gpios;
  895. dev_set_drvdata(stmpe->dev, stmpe);
  896. if (ci->init)
  897. ci->init(stmpe);
  898. if (pdata->irq_over_gpio) {
  899. ret = devm_gpio_request_one(ci->dev, pdata->irq_gpio,
  900. GPIOF_DIR_IN, "stmpe");
  901. if (ret) {
  902. dev_err(stmpe->dev, "failed to request IRQ GPIO: %d\n",
  903. ret);
  904. return ret;
  905. }
  906. stmpe->irq = gpio_to_irq(pdata->irq_gpio);
  907. } else {
  908. stmpe->irq = ci->irq;
  909. }
  910. if (stmpe->irq < 0) {
  911. /* use alternate variant info for no-irq mode, if supported */
  912. dev_info(stmpe->dev,
  913. "%s configured in no-irq mode by platform data\n",
  914. stmpe->variant->name);
  915. if (!stmpe_noirq_variant_info[stmpe->partnum]) {
  916. dev_err(stmpe->dev,
  917. "%s does not support no-irq mode!\n",
  918. stmpe->variant->name);
  919. return -ENODEV;
  920. }
  921. stmpe->variant = stmpe_noirq_variant_info[stmpe->partnum];
  922. } else if (pdata->irq_trigger == IRQF_TRIGGER_NONE) {
  923. pdata->irq_trigger =
  924. irqd_get_trigger_type(irq_get_irq_data(stmpe->irq));
  925. }
  926. ret = stmpe_chip_init(stmpe);
  927. if (ret)
  928. return ret;
  929. if (stmpe->irq >= 0) {
  930. ret = stmpe_irq_init(stmpe, np);
  931. if (ret)
  932. return ret;
  933. ret = devm_request_threaded_irq(ci->dev, stmpe->irq, NULL,
  934. stmpe_irq, pdata->irq_trigger | IRQF_ONESHOT,
  935. "stmpe", stmpe);
  936. if (ret) {
  937. dev_err(stmpe->dev, "failed to request IRQ: %d\n",
  938. ret);
  939. return ret;
  940. }
  941. }
  942. ret = stmpe_devices_init(stmpe);
  943. if (!ret)
  944. return 0;
  945. dev_err(stmpe->dev, "failed to add children\n");
  946. mfd_remove_devices(stmpe->dev);
  947. return ret;
  948. }
  949. int stmpe_remove(struct stmpe *stmpe)
  950. {
  951. mfd_remove_devices(stmpe->dev);
  952. return 0;
  953. }
  954. #ifdef CONFIG_PM
  955. static int stmpe_suspend(struct device *dev)
  956. {
  957. struct stmpe *stmpe = dev_get_drvdata(dev);
  958. if (stmpe->irq >= 0 && device_may_wakeup(dev))
  959. enable_irq_wake(stmpe->irq);
  960. return 0;
  961. }
  962. static int stmpe_resume(struct device *dev)
  963. {
  964. struct stmpe *stmpe = dev_get_drvdata(dev);
  965. if (stmpe->irq >= 0 && device_may_wakeup(dev))
  966. disable_irq_wake(stmpe->irq);
  967. return 0;
  968. }
  969. const struct dev_pm_ops stmpe_dev_pm_ops = {
  970. .suspend = stmpe_suspend,
  971. .resume = stmpe_resume,
  972. };
  973. #endif