qci_touchpad.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Quanta I2C Touchpad Driver
  2. *
  3. * Copyright (C) 2009 Quanta Computer Inc.
  4. * Author: Hsin Wu <hsin.wu@quantatw.com>
  5. * Author: Austin Lai <austin.lai@quantatw.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. /*
  18. *
  19. * The Driver with I/O communications via the I2C Interface for ON2 of AP BU.
  20. * And it is only working on the nuvoTon WPCE775x Embedded Controller.
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/i2c.h>
  29. #include <linux/mutex.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/input.h>
  32. #include <linux/keyboard.h>
  33. #include <linux/gpio.h>
  34. #include <linux/delay.h>
  35. #define TOUCHPAD_ID_NAME "qci-i2cpad"
  36. #define TOUCHPAD_NAME "PS2 Touchpad"
  37. #define TOUCHPAD_DEVICE "/i2c/input1"
  38. #define TOUCHPAD_CMD_ENABLE 0xF4
  39. #define TOUCHPAD_INIT_DELAY_MS 100
  40. static int __devinit qcitp_probe(struct i2c_client *client,
  41. const struct i2c_device_id *id);
  42. static int __devexit qcitp_remove(struct i2c_client *kbd);
  43. /* General structure to hold the driver data */
  44. struct i2ctpad_drv_data {
  45. struct i2c_client *ti2c_client;
  46. struct work_struct work;
  47. struct input_dev *qcitp_dev;
  48. struct kobject *tp_kobj;
  49. unsigned int qcitp_gpio;
  50. unsigned int qcitp_irq;
  51. char ecdata[8];
  52. };
  53. static int tp_sense_val = 10;
  54. static ssize_t tp_sensitive_show(struct kobject *kobj,
  55. struct kobj_attribute *attr, char * buf)
  56. {
  57. return sprintf(buf, "%d\n", tp_sense_val);
  58. }
  59. static ssize_t tp_sensitive_store(struct kobject *kobj,
  60. struct kobj_attribute *attr, const char* buf, size_t n)
  61. {
  62. unsigned int val = 0;
  63. sscanf(buf, "%d", &val);
  64. if (val >= 1 && val <= 10)
  65. tp_sense_val = val;
  66. else
  67. return -ENOSYS;
  68. return sizeof(buf);
  69. }
  70. static struct kobj_attribute tp_sensitivity = __ATTR(tp_sensitivity ,
  71. 0644 ,
  72. tp_sensitive_show ,
  73. tp_sensitive_store);
  74. static struct attribute *g_tp[] = {
  75. &tp_sensitivity.attr,
  76. NULL,
  77. };
  78. static struct attribute_group attr_group = {
  79. .attrs = g_tp,
  80. };
  81. /*-----------------------------------------------------------------------------
  82. * Driver functions
  83. *---------------------------------------------------------------------------*/
  84. #ifdef CONFIG_PM
  85. static int qcitp_suspend(struct device *dev)
  86. {
  87. return 0;
  88. }
  89. static int qcitp_resume(struct device *dev)
  90. {
  91. return 0;
  92. }
  93. #endif
  94. static const struct i2c_device_id qcitp_idtable[] = {
  95. { TOUCHPAD_ID_NAME, 0 },
  96. { }
  97. };
  98. MODULE_DEVICE_TABLE(i2c, qcitp_idtable);
  99. #ifdef CONFIG_PM
  100. static const struct dev_pm_ops qcitp_pm_ops = {
  101. .suspend = qcitp_suspend,
  102. .resume = qcitp_resume,
  103. };
  104. #endif
  105. static struct i2c_driver i2ctp_driver = {
  106. .driver = {
  107. .owner = THIS_MODULE,
  108. .name = TOUCHPAD_ID_NAME,
  109. #ifdef CONFIG_PM
  110. .pm = &qcitp_pm_ops,
  111. #endif
  112. },
  113. .probe = qcitp_probe,
  114. .remove = __devexit_p(qcitp_remove),
  115. .id_table = qcitp_idtable,
  116. };
  117. static void qcitp_fetch_data(struct i2c_client *tpad_client,
  118. char *ec_data)
  119. {
  120. struct i2c_msg tp_msg;
  121. int ret;
  122. tp_msg.addr = tpad_client->addr;
  123. tp_msg.flags = I2C_M_RD;
  124. tp_msg.len = 3;
  125. tp_msg.buf = (char *)&ec_data[0];
  126. ret = i2c_transfer(tpad_client->adapter, &tp_msg, 1);
  127. }
  128. static void qcitp_report_key(struct input_dev *tpad_dev, char *ec_data)
  129. {
  130. int dx = 0;
  131. int dy = 0;
  132. if (ec_data[1])
  133. dx = (int) ec_data[1] -
  134. (int) ((ec_data[0] << 4) & 0x100);
  135. if (ec_data[2])
  136. dy = (int) ((ec_data[0] << 3) & 0x100) -
  137. (int) ec_data[2];
  138. dx = (dx * tp_sense_val)/10;
  139. dy = (dy * tp_sense_val)/10;
  140. input_report_key(tpad_dev, BTN_LEFT, ec_data[0] & 0x01);
  141. input_report_key(tpad_dev, BTN_RIGHT, ec_data[0] & 0x02);
  142. input_report_key(tpad_dev, BTN_MIDDLE, ec_data[0] & 0x04);
  143. input_report_rel(tpad_dev, REL_X, dx);
  144. input_report_rel(tpad_dev, REL_Y, dy);
  145. input_sync(tpad_dev);
  146. }
  147. static void qcitp_work_handler(struct work_struct *_work)
  148. {
  149. struct i2ctpad_drv_data *itpad_drv_data =
  150. container_of(_work, struct i2ctpad_drv_data, work);
  151. struct i2c_client *itpad_client = itpad_drv_data->ti2c_client;
  152. struct input_dev *itpad_dev = itpad_drv_data->qcitp_dev;
  153. qcitp_fetch_data(itpad_client, itpad_drv_data->ecdata);
  154. qcitp_report_key(itpad_dev, itpad_drv_data->ecdata);
  155. }
  156. static irqreturn_t qcitp_interrupt(int irq, void *dev_id)
  157. {
  158. struct i2ctpad_drv_data *itpad_drv_data = dev_id;
  159. schedule_work(&itpad_drv_data->work);
  160. return IRQ_HANDLED;
  161. }
  162. static int __devinit qcitp_probe(struct i2c_client *client,
  163. const struct i2c_device_id *id)
  164. {
  165. int err = -ENOMEM;
  166. struct i2ctpad_drv_data *context = 0;
  167. context = kzalloc(sizeof(struct i2ctpad_drv_data), GFP_KERNEL);
  168. if (!context)
  169. return err;
  170. i2c_set_clientdata(client, context);
  171. context->ti2c_client = client;
  172. context->qcitp_gpio = client->irq;
  173. /* Enable mouse */
  174. i2c_smbus_write_byte(client, TOUCHPAD_CMD_ENABLE);
  175. msleep(TOUCHPAD_INIT_DELAY_MS);
  176. i2c_smbus_read_byte(client);
  177. /*allocate and register input device*/
  178. context->qcitp_dev = input_allocate_device();
  179. if (!context->qcitp_dev) {
  180. pr_err("[TouchPad] allocting memory fail\n");
  181. err = -ENOMEM;
  182. goto allocate_fail;
  183. }
  184. context->qcitp_dev->name = TOUCHPAD_NAME;
  185. context->qcitp_dev->phys = TOUCHPAD_DEVICE;
  186. context->qcitp_dev->id.bustype = BUS_I2C;
  187. context->qcitp_dev->id.vendor = 0x1050;
  188. context->qcitp_dev->id.product = 0x1;
  189. context->qcitp_dev->id.version = 0x1;
  190. context->qcitp_dev->evbit[0] = BIT_MASK(EV_KEY) |
  191. BIT_MASK(EV_REL);
  192. context->qcitp_dev->relbit[0] = BIT_MASK(REL_X) |
  193. BIT_MASK(REL_Y);
  194. context->qcitp_dev->keybit[BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) |
  195. BIT_MASK(BTN_MIDDLE) |
  196. BIT_MASK(BTN_RIGHT);
  197. input_set_drvdata(context->qcitp_dev, context);
  198. err = input_register_device(context->qcitp_dev);
  199. if (err) {
  200. pr_err("[TouchPad] register device fail\n");
  201. goto register_fail;
  202. }
  203. /*request intterrupt*/
  204. INIT_WORK(&context->work, qcitp_work_handler);
  205. err = gpio_request(context->qcitp_gpio, "qci-pad");
  206. if (err) {
  207. pr_err("[TouchPad]err gpio request\n");
  208. goto gpio_request_fail;
  209. }
  210. context->qcitp_irq = gpio_to_irq(context->qcitp_gpio);
  211. err = request_irq(context->qcitp_irq,
  212. qcitp_interrupt,
  213. IRQF_TRIGGER_FALLING,
  214. TOUCHPAD_ID_NAME,
  215. context);
  216. if (err) {
  217. pr_err("[TouchPad] unable to get IRQ\n");
  218. goto request_irq_fail;
  219. }
  220. /*create touchpad kobject*/
  221. context->tp_kobj = kobject_create_and_add("touchpad", NULL);
  222. err = sysfs_create_group(context->tp_kobj, &attr_group);
  223. if (err)
  224. pr_warning("[TouchPad] sysfs create fail\n");
  225. tp_sense_val = 10;
  226. return 0;
  227. request_irq_fail:
  228. gpio_free(context->qcitp_gpio);
  229. gpio_request_fail:
  230. input_unregister_device(context->qcitp_dev);
  231. register_fail:
  232. input_free_device(context->qcitp_dev);
  233. allocate_fail:
  234. i2c_set_clientdata(client, NULL);
  235. kfree(context);
  236. return err;
  237. }
  238. static int __devexit qcitp_remove(struct i2c_client *dev)
  239. {
  240. struct i2ctpad_drv_data *context = i2c_get_clientdata(dev);
  241. free_irq(context->qcitp_irq, context);
  242. gpio_free(context->qcitp_gpio);
  243. input_free_device(context->qcitp_dev);
  244. input_unregister_device(context->qcitp_dev);
  245. kfree(context);
  246. return 0;
  247. }
  248. static int __init qcitp_init(void)
  249. {
  250. return i2c_add_driver(&i2ctp_driver);
  251. }
  252. static void __exit qcitp_exit(void)
  253. {
  254. i2c_del_driver(&i2ctp_driver);
  255. }
  256. module_init(qcitp_init);
  257. module_exit(qcitp_exit);
  258. MODULE_AUTHOR("Quanta Computer Inc.");
  259. MODULE_DESCRIPTION("Quanta Embedded Controller I2C Touch Pad Driver");
  260. MODULE_LICENSE("GPL v2");