bcm_iproc_tsc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (C) 2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/input.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/keyboard.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <asm/irq.h>
  23. #include <linux/io.h>
  24. #include <linux/clk.h>
  25. #include <linux/serio.h>
  26. #define IPROC_TS_NAME "iproc-ts"
  27. #define PEN_DOWN_STATUS 1
  28. #define PEN_UP_STATUS 0
  29. #define X_MIN 0
  30. #define Y_MIN 0
  31. #define X_MAX 0xFFF
  32. #define Y_MAX 0xFFF
  33. /* Value given by controller for invalid coordinate. */
  34. #define INVALID_COORD 0xFFFFFFFF
  35. /* Register offsets */
  36. #define REGCTL1 0x00
  37. #define REGCTL2 0x04
  38. #define INTERRUPT_THRES 0x08
  39. #define INTERRUPT_MASK 0x0c
  40. #define INTERRUPT_STATUS 0x10
  41. #define CONTROLLER_STATUS 0x14
  42. #define FIFO_DATA 0x18
  43. #define FIFO_DATA_X_Y_MASK 0xFFFF
  44. #define ANALOG_CONTROL 0x1c
  45. #define AUX_DATA 0x20
  46. #define DEBOUNCE_CNTR_STAT 0x24
  47. #define SCAN_CNTR_STAT 0x28
  48. #define REM_CNTR_STAT 0x2c
  49. #define SETTLING_TIMER_STAT 0x30
  50. #define SPARE_REG 0x34
  51. #define SOFT_BYPASS_CONTROL 0x38
  52. #define SOFT_BYPASS_DATA 0x3c
  53. /* Bit values for INTERRUPT_MASK and INTERRUPT_STATUS regs */
  54. #define TS_PEN_INTR_MASK BIT(0)
  55. #define TS_FIFO_INTR_MASK BIT(2)
  56. /* Bit values for CONTROLLER_STATUS reg1 */
  57. #define TS_PEN_DOWN BIT(0)
  58. /* Shift values for control reg1 */
  59. #define SCANNING_PERIOD_SHIFT 24
  60. #define DEBOUNCE_TIMEOUT_SHIFT 16
  61. #define SETTLING_TIMEOUT_SHIFT 8
  62. #define TOUCH_TIMEOUT_SHIFT 0
  63. /* Shift values for coordinates from fifo */
  64. #define X_COORD_SHIFT 0
  65. #define Y_COORD_SHIFT 16
  66. /* Bit values for REGCTL2 */
  67. #define TS_CONTROLLER_EN_BIT BIT(16)
  68. #define TS_CONTROLLER_AVGDATA_SHIFT 8
  69. #define TS_CONTROLLER_AVGDATA_MASK (0x7 << TS_CONTROLLER_AVGDATA_SHIFT)
  70. #define TS_CONTROLLER_PWR_LDO BIT(5)
  71. #define TS_CONTROLLER_PWR_ADC BIT(4)
  72. #define TS_CONTROLLER_PWR_BGP BIT(3)
  73. #define TS_CONTROLLER_PWR_TS BIT(2)
  74. #define TS_WIRE_MODE_BIT BIT(1)
  75. #define dbg_reg(dev, priv, reg) \
  76. dev_dbg(dev, "%20s= 0x%08x\n", #reg, readl((priv)->regs + reg))
  77. struct tsc_param {
  78. /* Each step is 1024 us. Valid 1-256 */
  79. u32 scanning_period;
  80. /* Each step is 512 us. Valid 0-255 */
  81. u32 debounce_timeout;
  82. /*
  83. * The settling duration (in ms) is the amount of time the tsc
  84. * waits to allow the voltage to settle after turning on the
  85. * drivers in detection mode. Valid values: 0-11
  86. * 0 = 0.008 ms
  87. * 1 = 0.01 ms
  88. * 2 = 0.02 ms
  89. * 3 = 0.04 ms
  90. * 4 = 0.08 ms
  91. * 5 = 0.16 ms
  92. * 6 = 0.32 ms
  93. * 7 = 0.64 ms
  94. * 8 = 1.28 ms
  95. * 9 = 2.56 ms
  96. * 10 = 5.12 ms
  97. * 11 = 10.24 ms
  98. */
  99. u32 settling_timeout;
  100. /* touch timeout in sample counts */
  101. u32 touch_timeout;
  102. /*
  103. * Number of data samples which are averaged before a final data point
  104. * is placed into the FIFO
  105. */
  106. u32 average_data;
  107. /* FIFO threshold */
  108. u32 fifo_threshold;
  109. /* Optional standard touchscreen properties. */
  110. u32 max_x;
  111. u32 max_y;
  112. u32 fuzz_x;
  113. u32 fuzz_y;
  114. bool invert_x;
  115. bool invert_y;
  116. };
  117. struct iproc_ts_priv {
  118. struct platform_device *pdev;
  119. struct input_dev *idev;
  120. void __iomem *regs;
  121. struct clk *tsc_clk;
  122. int pen_status;
  123. struct tsc_param cfg_params;
  124. };
  125. /*
  126. * Set default values the same as hardware reset values
  127. * except for fifo_threshold with is set to 1.
  128. */
  129. static const struct tsc_param iproc_default_config = {
  130. .scanning_period = 0x5, /* 1 to 256 */
  131. .debounce_timeout = 0x28, /* 0 to 255 */
  132. .settling_timeout = 0x7, /* 0 to 11 */
  133. .touch_timeout = 0xa, /* 0 to 255 */
  134. .average_data = 5, /* entry 5 = 32 pts */
  135. .fifo_threshold = 1, /* 0 to 31 */
  136. .max_x = X_MAX,
  137. .max_y = Y_MAX,
  138. };
  139. static void ts_reg_dump(struct iproc_ts_priv *priv)
  140. {
  141. struct device *dev = &priv->pdev->dev;
  142. dbg_reg(dev, priv, REGCTL1);
  143. dbg_reg(dev, priv, REGCTL2);
  144. dbg_reg(dev, priv, INTERRUPT_THRES);
  145. dbg_reg(dev, priv, INTERRUPT_MASK);
  146. dbg_reg(dev, priv, INTERRUPT_STATUS);
  147. dbg_reg(dev, priv, CONTROLLER_STATUS);
  148. dbg_reg(dev, priv, FIFO_DATA);
  149. dbg_reg(dev, priv, ANALOG_CONTROL);
  150. dbg_reg(dev, priv, AUX_DATA);
  151. dbg_reg(dev, priv, DEBOUNCE_CNTR_STAT);
  152. dbg_reg(dev, priv, SCAN_CNTR_STAT);
  153. dbg_reg(dev, priv, REM_CNTR_STAT);
  154. dbg_reg(dev, priv, SETTLING_TIMER_STAT);
  155. dbg_reg(dev, priv, SPARE_REG);
  156. dbg_reg(dev, priv, SOFT_BYPASS_CONTROL);
  157. dbg_reg(dev, priv, SOFT_BYPASS_DATA);
  158. }
  159. static irqreturn_t iproc_touchscreen_interrupt(int irq, void *data)
  160. {
  161. struct platform_device *pdev = data;
  162. struct iproc_ts_priv *priv = platform_get_drvdata(pdev);
  163. u32 intr_status;
  164. u32 raw_coordinate;
  165. u16 x;
  166. u16 y;
  167. int i;
  168. bool needs_sync = false;
  169. intr_status = readl(priv->regs + INTERRUPT_STATUS);
  170. intr_status &= TS_PEN_INTR_MASK | TS_FIFO_INTR_MASK;
  171. if (intr_status == 0)
  172. return IRQ_NONE;
  173. /* Clear all interrupt status bits, write-1-clear */
  174. writel(intr_status, priv->regs + INTERRUPT_STATUS);
  175. /* Pen up/down */
  176. if (intr_status & TS_PEN_INTR_MASK) {
  177. if (readl(priv->regs + CONTROLLER_STATUS) & TS_PEN_DOWN)
  178. priv->pen_status = PEN_DOWN_STATUS;
  179. else
  180. priv->pen_status = PEN_UP_STATUS;
  181. input_report_key(priv->idev, BTN_TOUCH, priv->pen_status);
  182. needs_sync = true;
  183. dev_dbg(&priv->pdev->dev,
  184. "pen up-down (%d)\n", priv->pen_status);
  185. }
  186. /* coordinates in FIFO exceed the theshold */
  187. if (intr_status & TS_FIFO_INTR_MASK) {
  188. for (i = 0; i < priv->cfg_params.fifo_threshold; i++) {
  189. raw_coordinate = readl(priv->regs + FIFO_DATA);
  190. if (raw_coordinate == INVALID_COORD)
  191. continue;
  192. /*
  193. * The x and y coordinate are 16 bits each
  194. * with the x in the lower 16 bits and y in the
  195. * upper 16 bits.
  196. */
  197. x = (raw_coordinate >> X_COORD_SHIFT) &
  198. FIFO_DATA_X_Y_MASK;
  199. y = (raw_coordinate >> Y_COORD_SHIFT) &
  200. FIFO_DATA_X_Y_MASK;
  201. /* We only want to retain the 12 msb of the 16 */
  202. x = (x >> 4) & 0x0FFF;
  203. y = (y >> 4) & 0x0FFF;
  204. /* adjust x y according to lcd tsc mount angle */
  205. if (priv->cfg_params.invert_x)
  206. x = priv->cfg_params.max_x - x;
  207. if (priv->cfg_params.invert_y)
  208. y = priv->cfg_params.max_y - y;
  209. input_report_abs(priv->idev, ABS_X, x);
  210. input_report_abs(priv->idev, ABS_Y, y);
  211. needs_sync = true;
  212. dev_dbg(&priv->pdev->dev, "xy (0x%x 0x%x)\n", x, y);
  213. }
  214. }
  215. if (needs_sync)
  216. input_sync(priv->idev);
  217. return IRQ_HANDLED;
  218. }
  219. static int iproc_ts_start(struct input_dev *idev)
  220. {
  221. struct iproc_ts_priv *priv = input_get_drvdata(idev);
  222. u32 val;
  223. int error;
  224. /* Enable clock */
  225. error = clk_prepare_enable(priv->tsc_clk);
  226. if (error) {
  227. dev_err(&priv->pdev->dev, "%s clk_prepare_enable failed %d\n",
  228. __func__, error);
  229. return error;
  230. }
  231. /*
  232. * Interrupt is generated when:
  233. * FIFO reaches the int_th value, and pen event(up/down)
  234. */
  235. val = TS_PEN_INTR_MASK | TS_FIFO_INTR_MASK;
  236. writel(val, priv->regs + INTERRUPT_MASK);
  237. writel(priv->cfg_params.fifo_threshold, priv->regs + INTERRUPT_THRES);
  238. /* Initialize control reg1 */
  239. val = 0;
  240. val |= priv->cfg_params.scanning_period << SCANNING_PERIOD_SHIFT;
  241. val |= priv->cfg_params.debounce_timeout << DEBOUNCE_TIMEOUT_SHIFT;
  242. val |= priv->cfg_params.settling_timeout << SETTLING_TIMEOUT_SHIFT;
  243. val |= priv->cfg_params.touch_timeout << TOUCH_TIMEOUT_SHIFT;
  244. writel(val, priv->regs + REGCTL1);
  245. /* Try to clear all interrupt status */
  246. val = readl(priv->regs + INTERRUPT_STATUS);
  247. val |= TS_FIFO_INTR_MASK | TS_PEN_INTR_MASK;
  248. writel(val, priv->regs + INTERRUPT_STATUS);
  249. /* Initialize control reg2 */
  250. val = readl(priv->regs + REGCTL2);
  251. val |= TS_CONTROLLER_EN_BIT | TS_WIRE_MODE_BIT;
  252. val &= ~TS_CONTROLLER_AVGDATA_MASK;
  253. val |= priv->cfg_params.average_data << TS_CONTROLLER_AVGDATA_SHIFT;
  254. val &= ~(TS_CONTROLLER_PWR_LDO | /* PWR up LDO */
  255. TS_CONTROLLER_PWR_ADC | /* PWR up ADC */
  256. TS_CONTROLLER_PWR_BGP | /* PWR up BGP */
  257. TS_CONTROLLER_PWR_TS); /* PWR up TS */
  258. writel(val, priv->regs + REGCTL2);
  259. ts_reg_dump(priv);
  260. return 0;
  261. }
  262. static void iproc_ts_stop(struct input_dev *dev)
  263. {
  264. u32 val;
  265. struct iproc_ts_priv *priv = input_get_drvdata(dev);
  266. writel(0, priv->regs + INTERRUPT_MASK); /* Disable all interrupts */
  267. /* Only power down touch screen controller */
  268. val = readl(priv->regs + REGCTL2);
  269. val |= TS_CONTROLLER_PWR_TS;
  270. writel(val, priv->regs + REGCTL2);
  271. clk_disable(priv->tsc_clk);
  272. }
  273. static int iproc_get_tsc_config(struct device *dev, struct iproc_ts_priv *priv)
  274. {
  275. struct device_node *np = dev->of_node;
  276. u32 val;
  277. priv->cfg_params = iproc_default_config;
  278. if (!np)
  279. return 0;
  280. if (of_property_read_u32(np, "scanning_period", &val) >= 0) {
  281. if (val < 1 || val > 256) {
  282. dev_err(dev, "scanning_period (%u) must be [1-256]\n",
  283. val);
  284. return -EINVAL;
  285. }
  286. priv->cfg_params.scanning_period = val;
  287. }
  288. if (of_property_read_u32(np, "debounce_timeout", &val) >= 0) {
  289. if (val > 255) {
  290. dev_err(dev, "debounce_timeout (%u) must be [0-255]\n",
  291. val);
  292. return -EINVAL;
  293. }
  294. priv->cfg_params.debounce_timeout = val;
  295. }
  296. if (of_property_read_u32(np, "settling_timeout", &val) >= 0) {
  297. if (val > 11) {
  298. dev_err(dev, "settling_timeout (%u) must be [0-11]\n",
  299. val);
  300. return -EINVAL;
  301. }
  302. priv->cfg_params.settling_timeout = val;
  303. }
  304. if (of_property_read_u32(np, "touch_timeout", &val) >= 0) {
  305. if (val > 255) {
  306. dev_err(dev, "touch_timeout (%u) must be [0-255]\n",
  307. val);
  308. return -EINVAL;
  309. }
  310. priv->cfg_params.touch_timeout = val;
  311. }
  312. if (of_property_read_u32(np, "average_data", &val) >= 0) {
  313. if (val > 8) {
  314. dev_err(dev, "average_data (%u) must be [0-8]\n", val);
  315. return -EINVAL;
  316. }
  317. priv->cfg_params.average_data = val;
  318. }
  319. if (of_property_read_u32(np, "fifo_threshold", &val) >= 0) {
  320. if (val > 31) {
  321. dev_err(dev, "fifo_threshold (%u)) must be [0-31]\n",
  322. val);
  323. return -EINVAL;
  324. }
  325. priv->cfg_params.fifo_threshold = val;
  326. }
  327. /* Parse optional properties. */
  328. of_property_read_u32(np, "touchscreen-size-x", &priv->cfg_params.max_x);
  329. of_property_read_u32(np, "touchscreen-size-y", &priv->cfg_params.max_y);
  330. of_property_read_u32(np, "touchscreen-fuzz-x",
  331. &priv->cfg_params.fuzz_x);
  332. of_property_read_u32(np, "touchscreen-fuzz-y",
  333. &priv->cfg_params.fuzz_y);
  334. priv->cfg_params.invert_x =
  335. of_property_read_bool(np, "touchscreen-inverted-x");
  336. priv->cfg_params.invert_y =
  337. of_property_read_bool(np, "touchscreen-inverted-y");
  338. return 0;
  339. }
  340. static int iproc_ts_probe(struct platform_device *pdev)
  341. {
  342. struct iproc_ts_priv *priv;
  343. struct input_dev *idev;
  344. struct resource *res;
  345. int irq;
  346. int error;
  347. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  348. if (!priv)
  349. return -ENOMEM;
  350. /* touchscreen controller memory mapped regs */
  351. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  352. priv->regs = devm_ioremap_resource(&pdev->dev, res);
  353. if (IS_ERR(priv->regs)) {
  354. error = PTR_ERR(priv->regs);
  355. dev_err(&pdev->dev, "unable to map I/O memory: %d\n", error);
  356. return error;
  357. }
  358. priv->tsc_clk = devm_clk_get(&pdev->dev, "tsc_clk");
  359. if (IS_ERR(priv->tsc_clk)) {
  360. error = PTR_ERR(priv->tsc_clk);
  361. dev_err(&pdev->dev,
  362. "failed getting clock tsc_clk: %d\n", error);
  363. return error;
  364. }
  365. priv->pdev = pdev;
  366. error = iproc_get_tsc_config(&pdev->dev, priv);
  367. if (error) {
  368. dev_err(&pdev->dev, "get_tsc_config failed: %d\n", error);
  369. return error;
  370. }
  371. idev = devm_input_allocate_device(&pdev->dev);
  372. if (!idev) {
  373. dev_err(&pdev->dev, "failed to allocate input device\n");
  374. return -ENOMEM;
  375. }
  376. priv->idev = idev;
  377. priv->pen_status = PEN_UP_STATUS;
  378. /* Set input device info */
  379. idev->name = IPROC_TS_NAME;
  380. idev->dev.parent = &pdev->dev;
  381. idev->id.bustype = BUS_HOST;
  382. idev->id.vendor = SERIO_UNKNOWN;
  383. idev->id.product = 0;
  384. idev->id.version = 0;
  385. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  386. __set_bit(BTN_TOUCH, idev->keybit);
  387. input_set_abs_params(idev, ABS_X, X_MIN, priv->cfg_params.max_x,
  388. priv->cfg_params.fuzz_x, 0);
  389. input_set_abs_params(idev, ABS_Y, Y_MIN, priv->cfg_params.max_y,
  390. priv->cfg_params.fuzz_y, 0);
  391. idev->open = iproc_ts_start;
  392. idev->close = iproc_ts_stop;
  393. input_set_drvdata(idev, priv);
  394. platform_set_drvdata(pdev, priv);
  395. /* get interrupt */
  396. irq = platform_get_irq(pdev, 0);
  397. if (irq < 0) {
  398. dev_err(&pdev->dev, "platform_get_irq failed: %d\n", irq);
  399. return irq;
  400. }
  401. error = devm_request_irq(&pdev->dev, irq,
  402. iproc_touchscreen_interrupt,
  403. IRQF_SHARED, IPROC_TS_NAME, pdev);
  404. if (error)
  405. return error;
  406. error = input_register_device(priv->idev);
  407. if (error) {
  408. dev_err(&pdev->dev,
  409. "failed to register input device: %d\n", error);
  410. return error;
  411. }
  412. return 0;
  413. }
  414. static const struct of_device_id iproc_ts_of_match[] = {
  415. {.compatible = "brcm,iproc-touchscreen", },
  416. { },
  417. };
  418. MODULE_DEVICE_TABLE(of, iproc_ts_of_match);
  419. static struct platform_driver iproc_ts_driver = {
  420. .probe = iproc_ts_probe,
  421. .driver = {
  422. .name = IPROC_TS_NAME,
  423. .of_match_table = of_match_ptr(iproc_ts_of_match),
  424. },
  425. };
  426. module_platform_driver(iproc_ts_driver);
  427. MODULE_DESCRIPTION("IPROC Touchscreen driver");
  428. MODULE_AUTHOR("Broadcom");
  429. MODULE_LICENSE("GPL v2");