bcm_iproc_tsc.c 13 KB

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