tdisc_vtd518_shinetsu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /* Copyright (c) 2010, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/input.h>
  16. #include <linux/delay.h>
  17. #include <linux/i2c.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/gpio.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/slab.h>
  24. #include <linux/input/tdisc_shinetsu.h>
  25. #if defined(CONFIG_HAS_EARLYSUSPEND)
  26. #include <linux/earlysuspend.h>
  27. /* Early-suspend level */
  28. #define TDISC_SUSPEND_LEVEL 1
  29. #endif
  30. MODULE_LICENSE("GPL v2");
  31. MODULE_VERSION("0.1");
  32. MODULE_DESCRIPTION("Shinetsu Touchdisc driver");
  33. MODULE_ALIAS("platform:tdisc-shinetsu");
  34. #define TDSIC_BLK_READ_CMD 0x00
  35. #define TDISC_READ_DELAY msecs_to_jiffies(25)
  36. #define X_MAX (32)
  37. #define X_MIN (-32)
  38. #define Y_MAX (32)
  39. #define Y_MIN (-32)
  40. #define PRESSURE_MAX (32)
  41. #define PRESSURE_MIN (0)
  42. #define TDISC_USER_ACTIVE_MASK 0x40
  43. #define TDISC_NORTH_SWITCH_MASK 0x20
  44. #define TDISC_SOUTH_SWITCH_MASK 0x10
  45. #define TDISC_EAST_SWITCH_MASK 0x08
  46. #define TDISC_WEST_SWITCH_MASK 0x04
  47. #define TDISC_CENTER_SWITCH 0x01
  48. #define TDISC_BUTTON_PRESS_MASK 0x3F
  49. #define DRIVER_NAME "tdisc-shinetsu"
  50. #define DEVICE_NAME "vtd518"
  51. #define TDISC_NAME "tdisc_shinetsu"
  52. #define TDISC_INT "tdisc_interrupt"
  53. struct tdisc_data {
  54. struct input_dev *tdisc_device;
  55. struct i2c_client *clientp;
  56. struct tdisc_platform_data *pdata;
  57. struct delayed_work tdisc_work;
  58. #if defined(CONFIG_HAS_EARLYSUSPEND)
  59. struct early_suspend tdisc_early_suspend;
  60. #endif
  61. };
  62. static void process_tdisc_data(struct tdisc_data *dd, u8 *data)
  63. {
  64. int i;
  65. static bool button_press;
  66. s8 x, y;
  67. /* Check if the user is actively navigating */
  68. if (!(data[7] & TDISC_USER_ACTIVE_MASK)) {
  69. pr_debug(" TDISC ! No Data to report ! False positive \n");
  70. return;
  71. }
  72. for (i = 0; i < 8 ; i++)
  73. pr_debug(" Data[%d] = %x\n", i, data[i]);
  74. /* Check if there is a button press */
  75. if (dd->pdata->tdisc_report_keys)
  76. if (data[7] & TDISC_BUTTON_PRESS_MASK || button_press == true) {
  77. input_report_key(dd->tdisc_device, KEY_UP,
  78. (data[7] & TDISC_NORTH_SWITCH_MASK));
  79. input_report_key(dd->tdisc_device, KEY_DOWN,
  80. (data[7] & TDISC_SOUTH_SWITCH_MASK));
  81. input_report_key(dd->tdisc_device, KEY_RIGHT,
  82. (data[7] & TDISC_EAST_SWITCH_MASK));
  83. input_report_key(dd->tdisc_device, KEY_LEFT,
  84. (data[7] & TDISC_WEST_SWITCH_MASK));
  85. input_report_key(dd->tdisc_device, KEY_ENTER,
  86. (data[7] & TDISC_CENTER_SWITCH));
  87. if (data[7] & TDISC_BUTTON_PRESS_MASK)
  88. button_press = true;
  89. else
  90. button_press = false;
  91. }
  92. if (dd->pdata->tdisc_report_relative) {
  93. /* Report relative motion values */
  94. x = (s8) data[0];
  95. y = (s8) data[1];
  96. if (dd->pdata->tdisc_reverse_x)
  97. x *= -1;
  98. if (dd->pdata->tdisc_reverse_y)
  99. y *= -1;
  100. input_report_rel(dd->tdisc_device, REL_X, x);
  101. input_report_rel(dd->tdisc_device, REL_Y, y);
  102. }
  103. if (dd->pdata->tdisc_report_absolute) {
  104. input_report_abs(dd->tdisc_device, ABS_X, data[2]);
  105. input_report_abs(dd->tdisc_device, ABS_Y, data[3]);
  106. input_report_abs(dd->tdisc_device, ABS_PRESSURE, data[4]);
  107. }
  108. if (dd->pdata->tdisc_report_wheel)
  109. input_report_rel(dd->tdisc_device, REL_WHEEL, (s8) data[6]);
  110. input_sync(dd->tdisc_device);
  111. }
  112. static void tdisc_work_f(struct work_struct *work)
  113. {
  114. int rc;
  115. u8 data[8];
  116. struct tdisc_data *dd =
  117. container_of(work, struct tdisc_data, tdisc_work.work);
  118. /*
  119. * Read the value of the interrupt pin. If low, perform
  120. * an I2C read of 8 bytes to get the touch values and then
  121. * reschedule the work after 25ms. If pin is high, exit
  122. * and wait for next interrupt.
  123. */
  124. rc = gpio_get_value_cansleep(dd->pdata->tdisc_gpio);
  125. if (rc < 0) {
  126. rc = pm_runtime_put_sync(&dd->clientp->dev);
  127. if (rc < 0)
  128. dev_dbg(&dd->clientp->dev, "%s: pm_runtime_put_sync"
  129. " failed\n", __func__);
  130. enable_irq(dd->clientp->irq);
  131. return;
  132. }
  133. pr_debug("%s: TDISC gpio_get_value = %d\n", __func__, rc);
  134. if (rc == 0) {
  135. /* We have data to read */
  136. rc = i2c_smbus_read_i2c_block_data(dd->clientp,
  137. TDSIC_BLK_READ_CMD, 8, data);
  138. if (rc < 0) {
  139. pr_debug("%s:I2C read failed,trying again\n", __func__);
  140. rc = i2c_smbus_read_i2c_block_data(dd->clientp,
  141. TDSIC_BLK_READ_CMD, 8, data);
  142. if (rc < 0) {
  143. pr_err("%s:I2C read failed again, exiting\n",
  144. __func__);
  145. goto fail_i2c_read;
  146. }
  147. }
  148. pr_debug("%s: TDISC: I2C read success\n", __func__);
  149. process_tdisc_data(dd, data);
  150. } else {
  151. /*
  152. * We have no data to read.
  153. * Enable the IRQ to receive further interrupts.
  154. */
  155. enable_irq(dd->clientp->irq);
  156. rc = pm_runtime_put_sync(&dd->clientp->dev);
  157. if (rc < 0)
  158. dev_dbg(&dd->clientp->dev, "%s: pm_runtime_put_sync"
  159. " failed\n", __func__);
  160. return;
  161. }
  162. fail_i2c_read:
  163. schedule_delayed_work(&dd->tdisc_work, TDISC_READ_DELAY);
  164. }
  165. static irqreturn_t tdisc_interrupt(int irq, void *dev_id)
  166. {
  167. /*
  168. * The touch disc intially generates an interrupt on any
  169. * touch. The interrupt line is pulled low and remains low
  170. * untill there are touch operations being performed. In case
  171. * there are no further touch operations, the line goes high. The
  172. * same process repeats again the next time,when the disc is touched.
  173. *
  174. * We do the following operations once we receive an interrupt.
  175. * 1. Disable the IRQ for any further interrutps.
  176. * 2. Schedule work every 25ms if the GPIO is still low.
  177. * 3. In the work queue do a I2C read to get the touch data.
  178. * 4. If the GPIO is pulled high, enable the IRQ and cancel the work.
  179. */
  180. struct tdisc_data *dd = dev_id;
  181. int rc;
  182. rc = pm_runtime_get(&dd->clientp->dev);
  183. if (rc < 0)
  184. dev_dbg(&dd->clientp->dev, "%s: pm_runtime_get"
  185. " failed\n", __func__);
  186. pr_debug("%s: TDISC IRQ ! :-)\n", __func__);
  187. /* Schedule the work immediately */
  188. disable_irq_nosync(dd->clientp->irq);
  189. schedule_delayed_work(&dd->tdisc_work, 0);
  190. return IRQ_HANDLED;
  191. }
  192. static int tdisc_open(struct input_dev *dev)
  193. {
  194. int rc;
  195. struct tdisc_data *dd = input_get_drvdata(dev);
  196. if (!dd->clientp) {
  197. /* Check if a valid i2c client is present */
  198. pr_err("%s: no i2c adapter present \n", __func__);
  199. return -ENODEV;
  200. }
  201. /* Enable the device */
  202. if (dd->pdata->tdisc_enable != NULL) {
  203. rc = dd->pdata->tdisc_enable();
  204. if (rc)
  205. goto fail_open;
  206. }
  207. rc = request_any_context_irq(dd->clientp->irq, tdisc_interrupt,
  208. IRQF_TRIGGER_FALLING, TDISC_INT, dd);
  209. if (rc < 0) {
  210. pr_err("%s: request IRQ failed\n", __func__);
  211. goto fail_irq_open;
  212. }
  213. return 0;
  214. fail_irq_open:
  215. if (dd->pdata->tdisc_disable != NULL)
  216. dd->pdata->tdisc_disable();
  217. fail_open:
  218. return rc;
  219. }
  220. static void tdisc_close(struct input_dev *dev)
  221. {
  222. struct tdisc_data *dd = input_get_drvdata(dev);
  223. free_irq(dd->clientp->irq, dd);
  224. cancel_delayed_work_sync(&dd->tdisc_work);
  225. if (dd->pdata->tdisc_disable != NULL)
  226. dd->pdata->tdisc_disable();
  227. }
  228. static int __devexit tdisc_remove(struct i2c_client *client)
  229. {
  230. struct tdisc_data *dd;
  231. pm_runtime_disable(&client->dev);
  232. dd = i2c_get_clientdata(client);
  233. #ifdef CONFIG_HAS_EARLYSUSPEND
  234. unregister_early_suspend(&dd->tdisc_early_suspend);
  235. #endif
  236. input_unregister_device(dd->tdisc_device);
  237. if (dd->pdata->tdisc_release != NULL)
  238. dd->pdata->tdisc_release();
  239. i2c_set_clientdata(client, NULL);
  240. kfree(dd);
  241. return 0;
  242. }
  243. #ifdef CONFIG_PM
  244. static int tdisc_suspend(struct device *dev)
  245. {
  246. int rc;
  247. struct tdisc_data *dd;
  248. dd = dev_get_drvdata(dev);
  249. if (device_may_wakeup(&dd->clientp->dev))
  250. enable_irq_wake(dd->clientp->irq);
  251. else {
  252. disable_irq(dd->clientp->irq);
  253. if (cancel_delayed_work_sync(&dd->tdisc_work))
  254. enable_irq(dd->clientp->irq);
  255. if (dd->pdata->tdisc_disable) {
  256. rc = dd->pdata->tdisc_disable();
  257. if (rc) {
  258. pr_err("%s: Suspend failed\n", __func__);
  259. return rc;
  260. }
  261. }
  262. }
  263. return 0;
  264. }
  265. static int tdisc_resume(struct device *dev)
  266. {
  267. int rc;
  268. struct tdisc_data *dd;
  269. dd = dev_get_drvdata(dev);
  270. if (device_may_wakeup(&dd->clientp->dev))
  271. disable_irq_wake(dd->clientp->irq);
  272. else {
  273. if (dd->pdata->tdisc_enable) {
  274. rc = dd->pdata->tdisc_enable();
  275. if (rc) {
  276. pr_err("%s: Resume failed\n", __func__);
  277. return rc;
  278. }
  279. }
  280. enable_irq(dd->clientp->irq);
  281. }
  282. return 0;
  283. }
  284. #ifdef CONFIG_HAS_EARLYSUSPEND
  285. static void tdisc_early_suspend(struct early_suspend *h)
  286. {
  287. struct tdisc_data *dd = container_of(h, struct tdisc_data,
  288. tdisc_early_suspend);
  289. tdisc_suspend(&dd->clientp->dev);
  290. }
  291. static void tdisc_late_resume(struct early_suspend *h)
  292. {
  293. struct tdisc_data *dd = container_of(h, struct tdisc_data,
  294. tdisc_early_suspend);
  295. tdisc_resume(&dd->clientp->dev);
  296. }
  297. #endif
  298. static struct dev_pm_ops tdisc_pm_ops = {
  299. #ifndef CONFIG_HAS_EARLYSUSPEND
  300. .suspend = tdisc_suspend,
  301. .resume = tdisc_resume,
  302. #endif
  303. };
  304. #endif
  305. static const struct i2c_device_id tdisc_id[] = {
  306. { DEVICE_NAME, 0 },
  307. { }
  308. };
  309. MODULE_DEVICE_TABLE(i2c, tdisc_id);
  310. static int __devinit tdisc_probe(struct i2c_client *client,
  311. const struct i2c_device_id *id)
  312. {
  313. int rc = -1;
  314. int x_max, x_min, y_max, y_min, pressure_min, pressure_max;
  315. struct tdisc_platform_data *pd;
  316. struct tdisc_data *dd;
  317. /* Check if the I2C adapter supports the BLOCK READ functionality */
  318. if (!i2c_check_functionality(client->adapter,
  319. I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  320. return -ENODEV;
  321. /* Enable runtime PM ops, start in ACTIVE mode */
  322. rc = pm_runtime_set_active(&client->dev);
  323. if (rc < 0)
  324. dev_dbg(&client->dev, "unable to set runtime pm state\n");
  325. pm_runtime_enable(&client->dev);
  326. dd = kzalloc(sizeof *dd, GFP_KERNEL);
  327. if (!dd) {
  328. rc = -ENOMEM;
  329. goto probe_exit;
  330. }
  331. i2c_set_clientdata(client, dd);
  332. dd->clientp = client;
  333. pd = client->dev.platform_data;
  334. if (!pd) {
  335. pr_err("%s: platform data not set \n", __func__);
  336. rc = -EFAULT;
  337. goto probe_free_exit;
  338. }
  339. dd->pdata = pd;
  340. dd->tdisc_device = input_allocate_device();
  341. if (!dd->tdisc_device) {
  342. rc = -ENOMEM;
  343. goto probe_free_exit;
  344. }
  345. input_set_drvdata(dd->tdisc_device, dd);
  346. dd->tdisc_device->open = tdisc_open;
  347. dd->tdisc_device->close = tdisc_close;
  348. dd->tdisc_device->name = TDISC_NAME;
  349. dd->tdisc_device->id.bustype = BUS_I2C;
  350. dd->tdisc_device->id.product = 1;
  351. dd->tdisc_device->id.version = 1;
  352. if (pd->tdisc_abs) {
  353. x_max = pd->tdisc_abs->x_max;
  354. x_min = pd->tdisc_abs->x_min;
  355. y_max = pd->tdisc_abs->y_max;
  356. y_min = pd->tdisc_abs->y_min;
  357. pressure_max = pd->tdisc_abs->pressure_max;
  358. pressure_min = pd->tdisc_abs->pressure_min;
  359. } else {
  360. x_max = X_MAX;
  361. x_min = X_MIN;
  362. y_max = Y_MAX;
  363. y_min = Y_MIN;
  364. pressure_max = PRESSURE_MAX;
  365. pressure_min = PRESSURE_MIN;
  366. }
  367. /* Device capablities for relative motion */
  368. input_set_capability(dd->tdisc_device, EV_REL, REL_X);
  369. input_set_capability(dd->tdisc_device, EV_REL, REL_Y);
  370. input_set_capability(dd->tdisc_device, EV_KEY, BTN_MOUSE);
  371. /* Device capablities for absolute motion */
  372. input_set_capability(dd->tdisc_device, EV_ABS, ABS_X);
  373. input_set_capability(dd->tdisc_device, EV_ABS, ABS_Y);
  374. input_set_capability(dd->tdisc_device, EV_ABS, ABS_PRESSURE);
  375. input_set_abs_params(dd->tdisc_device, ABS_X, x_min, x_max, 0, 0);
  376. input_set_abs_params(dd->tdisc_device, ABS_Y, y_min, y_max, 0, 0);
  377. input_set_abs_params(dd->tdisc_device, ABS_PRESSURE, pressure_min,
  378. pressure_max, 0, 0);
  379. /* Device capabilities for scroll and buttons */
  380. input_set_capability(dd->tdisc_device, EV_REL, REL_WHEEL);
  381. input_set_capability(dd->tdisc_device, EV_KEY, KEY_LEFT);
  382. input_set_capability(dd->tdisc_device, EV_KEY, KEY_RIGHT);
  383. input_set_capability(dd->tdisc_device, EV_KEY, KEY_UP);
  384. input_set_capability(dd->tdisc_device, EV_KEY, KEY_DOWN);
  385. input_set_capability(dd->tdisc_device, EV_KEY, KEY_ENTER);
  386. /* Setup the device for operation */
  387. if (dd->pdata->tdisc_setup != NULL) {
  388. rc = dd->pdata->tdisc_setup();
  389. if (rc) {
  390. pr_err("%s: Setup failed \n", __func__);
  391. goto probe_unreg_free_exit;
  392. }
  393. }
  394. /* Setup wakeup capability */
  395. device_init_wakeup(&dd->clientp->dev, dd->pdata->tdisc_wakeup);
  396. INIT_DELAYED_WORK(&dd->tdisc_work, tdisc_work_f);
  397. rc = input_register_device(dd->tdisc_device);
  398. if (rc) {
  399. pr_err("%s: input register device failed \n", __func__);
  400. rc = -EINVAL;
  401. goto probe_register_fail;
  402. }
  403. pm_runtime_set_suspended(&client->dev);
  404. #ifdef CONFIG_HAS_EARLYSUSPEND
  405. dd->tdisc_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
  406. TDISC_SUSPEND_LEVEL;
  407. dd->tdisc_early_suspend.suspend = tdisc_early_suspend;
  408. dd->tdisc_early_suspend.resume = tdisc_late_resume;
  409. register_early_suspend(&dd->tdisc_early_suspend);
  410. #endif
  411. return 0;
  412. probe_register_fail:
  413. if (dd->pdata->tdisc_release != NULL)
  414. dd->pdata->tdisc_release();
  415. probe_unreg_free_exit:
  416. input_free_device(dd->tdisc_device);
  417. probe_free_exit:
  418. i2c_set_clientdata(client, NULL);
  419. kfree(dd);
  420. probe_exit:
  421. pm_runtime_set_suspended(&client->dev);
  422. pm_runtime_disable(&client->dev);
  423. return rc;
  424. }
  425. static struct i2c_driver tdisc_driver = {
  426. .driver = {
  427. .name = DRIVER_NAME,
  428. .owner = THIS_MODULE,
  429. #ifdef CONFIG_PM
  430. .pm = &tdisc_pm_ops,
  431. #endif
  432. },
  433. .probe = tdisc_probe,
  434. .remove = __devexit_p(tdisc_remove),
  435. .id_table = tdisc_id,
  436. };
  437. static int __init tdisc_init(void)
  438. {
  439. int rc;
  440. rc = i2c_add_driver(&tdisc_driver);
  441. if (rc)
  442. pr_err("%s: i2c add driver failed \n", __func__);
  443. return rc;
  444. }
  445. static void __exit tdisc_exit(void)
  446. {
  447. i2c_del_driver(&tdisc_driver);
  448. }
  449. module_init(tdisc_init);
  450. module_exit(tdisc_exit);