Guest User

Untitled

a guest
Apr 20th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.08 KB | None | 0 0
  1. #include <linux/delay.h>
  2. #include <linux/device.h>
  3. #include <linux/init.h>
  4. #include <linux/input.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/mfd/marimba-tsadc.h>
  10. #include <linux/pm.h>
  11. #include <linux/slab.h>
  12.  
  13. #if defined(CONFIG_HAS_EARLYSUSPEND)
  14. #include <linux/earlysuspend.h>
  15. #endif
  16.  
  17. #include <linux/input/msm_ts.h>
  18. #include <linux/jiffies.h>  
  19. #define TSSC_CTL            0x100
  20. #define     TSSC_CTL_PENUP_IRQ  (1 << 12)
  21. #define     TSSC_CTL_DATA_FLAG  (1 << 11)
  22. #define     TSSC_CTL_DEBOUNCE_EN    (1 << 6)
  23. #define     TSSC_CTL_EN_AVERAGE (1 << 5)
  24. #define     TSSC_CTL_MODE_MASTER    (3 << 3)
  25. #define     TSSC_CTL_SW_RESET   (1 << 2)
  26. #define     TSSC_CTL_ENABLE     (1 << 0)
  27. #define TSSC_OPN            0x104
  28. #define     TSSC_OPN_NOOP       0x00
  29. #define     TSSC_OPN_4WIRE_X    0x01
  30. #define     TSSC_OPN_4WIRE_Y    0x02
  31. #define     TSSC_OPN_4WIRE_Z1   0x03
  32. #define     TSSC_OPN_4WIRE_Z2   0x04
  33. #define TSSC_SAMPLING_INT       0x108
  34. #define TSSC_STATUS         0x10c
  35. #define TSSC_AVG_12         0x110
  36. #define TSSC_AVG_34         0x114
  37. #define TSSC_SAMPLE(op,samp)        ((0x118 + ((op & 0x3) * 0x20)) + \
  38.                      ((samp & 0x7) * 0x4))
  39. #define TSSC_TEST_1         0x198
  40.     #define TSSC_TEST_1_EN_GATE_DEBOUNCE (1 << 2)
  41. #define TSSC_TEST_2         0x19c
  42. #define TS_PENUP_TIMEOUT_MS 70
  43.  
  44. struct msm_ts {
  45.     struct msm_ts_platform_data *pdata;
  46.     struct input_dev        *input_dev;
  47.     void __iomem            *tssc_base;
  48.     uint32_t            ts_down:1;
  49.     struct ts_virt_key      *vkey_down;
  50.     unsigned int            sample_irq;
  51.     unsigned int            pen_up_irq;
  52.  
  53. #if defined(CONFIG_HAS_EARLYSUSPEND)
  54.     struct early_suspend        early_suspend;
  55. #endif
  56.     struct device           *dev;
  57. struct timer_list timer;
  58. };
  59.  
  60. static uint32_t msm_tsdebug;
  61. module_param_named(tsdebug, msm_tsdebug, uint, 0664);
  62.  
  63. #define tssc_readl(t, a)    (readl(((t)->tssc_base) + (a)))
  64. #define tssc_writel(t, v, a)    do {writel(v, ((t)->tssc_base) + (a));} while(0)
  65.  
  66. static void setup_next_sample(struct msm_ts *ts)
  67. {
  68.     uint32_t tmp;
  69.     tmp = ((7 << 7) | TSSC_CTL_DEBOUNCE_EN | TSSC_CTL_EN_AVERAGE |
  70.            TSSC_CTL_MODE_MASTER | TSSC_CTL_ENABLE);
  71.     tssc_writel(ts, tmp, TSSC_CTL);
  72. }
  73.  
  74.  
  75. static void ts_timer(unsigned long arg)
  76. {
  77.     struct msm_ts *ts = (struct msm_ts *)arg;
  78.     input_report_key(ts->input_dev, BTN_TOUCH, 0);
  79.     input_sync(ts->input_dev);
  80. }
  81.  
  82.  
  83. static irqreturn_t msm_ts_irq(int irq, void *dev_id)
  84. {
  85.     struct msm_ts *ts = dev_id;
  86.     struct msm_ts_platform_data *pdata = ts->pdata;
  87.  
  88.     uint32_t tssc_avg12, tssc_avg34, tssc_status, tssc_ctl;
  89.     int x, y, z1, z2;
  90.     int was_down;
  91.     int down;
  92.   int z=0;  
  93.  
  94.   del_timer_sync(&ts->timer);
  95.     tssc_ctl = tssc_readl(ts, TSSC_CTL);
  96.     tssc_status = tssc_readl(ts, TSSC_STATUS);
  97.     tssc_avg12 = tssc_readl(ts, TSSC_AVG_12);
  98.     tssc_avg34 = tssc_readl(ts, TSSC_AVG_34);
  99.  
  100.     setup_next_sample(ts);
  101.  
  102.     x = tssc_avg12 & 0xffff;
  103.     y = tssc_avg12 >> 16;
  104.     z1 = tssc_avg34 & 0xffff;
  105.     z2 = tssc_avg34 >> 16;
  106.  
  107.     down = !(tssc_ctl & TSSC_CTL_PENUP_IRQ);
  108.     was_down = ts->ts_down;
  109.     ts->ts_down = down;
  110.  
  111.     if (down && !(tssc_ctl & TSSC_CTL_DATA_FLAG))
  112.         return IRQ_HANDLED;
  113.  
  114.     if (msm_tsdebug & 2)
  115.         printk("%s: down=%d, x=%d, y=%d, z1=%d, z2=%d, status %x\n",
  116.                __func__, down, x, y, z1, z2, tssc_status);
  117.     if (down)
  118.         {
  119.             z = ( ( z2 - z1 - 2)*x) / ( z1 + 2 );
  120.             z = ( 2500 - z ) * 1000 / ( 2500 - 900 );
  121.             if( z<=0 ) z = 255;
  122.         }
  123.  
  124.     if (pdata->inv_x) x = pdata->inv_x - x;
  125.     if (pdata->inv_y) y = pdata->inv_y - y;
  126.  
  127.     if (x < 0) x = 0;
  128.     if (y < 0) y = 0;
  129.        
  130. #if defined(CONFIG_MACH_MOONCAKE)
  131.   //x = x*240/916;
  132.   //y = y*320/832;
  133. #elif defined(CONFIG_MACH_V9)
  134.   //x=x*480/13/73;
  135.   //y=y*800/6/149;
  136. #endif
  137.  
  138.  
  139.     if (down) {
  140.         input_report_abs(ts->input_dev, ABS_X, x);
  141.         input_report_abs(ts->input_dev, ABS_Y, y);
  142.             input_report_abs(ts->input_dev, ABS_PRESSURE, z);
  143.     }
  144.     input_report_key(ts->input_dev, BTN_TOUCH, down);
  145.     input_sync(ts->input_dev);
  146.  
  147.     if (30 == irq)mod_timer(&ts->timer,jiffies + msecs_to_jiffies(TS_PENUP_TIMEOUT_MS));
  148.     return IRQ_HANDLED;
  149. }
  150.  
  151. static int __devinit msm_ts_hw_init(struct msm_ts *ts)
  152. {
  153.     setup_next_sample(ts);
  154.     return 0;
  155. }
  156.  
  157. static void msm_ts_enable(struct msm_ts *ts, bool enable)
  158. {
  159.     uint32_t val;
  160.  
  161.     if (enable == true)
  162.         msm_ts_hw_init(ts);
  163.     else {
  164.         val = tssc_readl(ts, TSSC_CTL);
  165.         val &= ~TSSC_CTL_ENABLE;
  166.         tssc_writel(ts, val, TSSC_CTL);
  167.     }
  168. }
  169.  
  170. #ifdef CONFIG_PM
  171. static int
  172. msm_ts_suspend(struct device *dev)
  173. {
  174.     struct msm_ts *ts =  dev_get_drvdata(dev);
  175.  
  176.     if (device_may_wakeup(dev) &&
  177.             device_may_wakeup(dev->parent))
  178.         enable_irq_wake(ts->sample_irq);
  179.     else {
  180.         disable_irq(ts->sample_irq);
  181.         disable_irq(ts->pen_up_irq);
  182.         msm_ts_enable(ts, false);
  183.     }
  184.  
  185.     return 0;
  186. }
  187.  
  188. static int
  189. msm_ts_resume(struct device *dev)
  190. {
  191.     struct msm_ts *ts =  dev_get_drvdata(dev);
  192.  
  193.     if (device_may_wakeup(dev) &&
  194.             device_may_wakeup(dev->parent))
  195.         disable_irq_wake(ts->sample_irq);
  196.     else {
  197.         msm_ts_enable(ts, true);
  198.         enable_irq(ts->sample_irq);
  199.         enable_irq(ts->pen_up_irq);
  200.     }
  201.  
  202.     return 0;
  203. }
  204.  
  205. static struct dev_pm_ops msm_touchscreen_pm_ops = {
  206. #ifndef CONFIG_HAS_EARLYSUSPEND
  207.     .suspend    = msm_ts_suspend,
  208.     .resume     = msm_ts_resume,
  209. #endif
  210. };
  211. #endif
  212.  
  213. #ifdef CONFIG_HAS_EARLYSUSPEND
  214. static void msm_ts_early_suspend(struct early_suspend *h)
  215. {
  216.     struct msm_ts *ts = container_of(h, struct msm_ts, early_suspend);
  217.  
  218.     msm_ts_suspend(ts->dev);
  219. }
  220.  
  221. static void msm_ts_late_resume(struct early_suspend *h)
  222. {
  223.     struct msm_ts *ts = container_of(h, struct msm_ts, early_suspend);
  224.  
  225.     msm_ts_resume(ts->dev);
  226. }
  227. #endif
  228.  
  229. #if defined(CONFIG_TOUCHSCREEN_VIRTUAL_KEYS)
  230. #define virtualkeys virtualkeys.msm-touchscreen
  231. #if defined(CONFIG_MACH_MOONCAKE)
  232. static const char ts_keys_size[] = "0x01:102:40:340:60:50:0x01:139:120:340:60:50:0x01:158:200:340:60:50";
  233. #elif defined(CONFIG_MACH_V9)
  234. static const char ts_keys_size[] = "0x01:102:70:850:60:50:0x01:139:230:850:60:50:0x01:158:390:850:60:50";
  235. #endif
  236.  
  237.  
  238.  
  239. static ssize_t virtualkeys_show(struct device *dev,
  240.         struct device_attribute *attr, char *buf)
  241. {
  242.     sprintf(buf,"%s\n",ts_keys_size);
  243.     printk("wly:%s\n",__FUNCTION__);
  244.     return strlen(ts_keys_size)+2;
  245. }
  246. static DEVICE_ATTR(virtualkeys, 0444, virtualkeys_show, NULL);
  247. extern struct kobject *android_touch_kobj;
  248. static struct kobject * virtual_key_kobj;
  249. static int ts_key_report_init(void)
  250. {
  251.     int ret;
  252.     virtual_key_kobj = kobject_get(android_touch_kobj);
  253.     if (virtual_key_kobj == NULL) {
  254.         virtual_key_kobj = kobject_create_and_add("board_properties", NULL);
  255.         if (virtual_key_kobj == NULL) {
  256.             printk(KERN_ERR "%s: subsystem_register failed\n", __func__);
  257.             ret = -ENOMEM;
  258.             return ret;
  259.         }
  260.     }
  261.     ret = sysfs_create_file(virtual_key_kobj, &dev_attr_virtualkeys.attr);
  262.     if (ret) {
  263.         printk(KERN_ERR "%s: sysfs_create_file failed\n", __func__);
  264.         return ret;
  265.     }
  266.  
  267.     return 0;
  268. }
  269.  
  270. #endif 
  271.  
  272. static int __devinit msm_ts_probe(struct platform_device *pdev)
  273. {
  274.     struct msm_ts_platform_data *pdata = pdev->dev.platform_data;
  275.     struct msm_ts *ts;
  276.     struct resource *tssc_res;
  277.     struct resource *irq1_res;
  278.     struct resource *irq2_res;
  279.     int err = 0;
  280.  
  281.     tssc_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tssc");
  282.     irq1_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "tssc1");
  283.     irq2_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "tssc2");
  284.  
  285.     if (!tssc_res || !irq1_res || !irq2_res) {
  286.         pr_err("%s: required resources not defined\n", __func__);
  287.         return -ENODEV;
  288.     }
  289.  
  290.     if (pdata == NULL) {
  291.         pr_err("%s: missing platform_data\n", __func__);
  292.         return -ENODEV;
  293.     }
  294.  
  295.     ts = kzalloc(sizeof(struct msm_ts), GFP_KERNEL);
  296.     if (ts == NULL) {
  297.         pr_err("%s: No memory for struct msm_ts\n", __func__);
  298.         return -ENOMEM;
  299.     }
  300.     ts->pdata = pdata;
  301.     ts->dev   = &pdev->dev;
  302.  
  303.     ts->sample_irq = irq1_res->start;
  304.     ts->pen_up_irq = irq2_res->start;
  305.  
  306.     ts->tssc_base = ioremap(tssc_res->start, resource_size(tssc_res));
  307.     if (ts->tssc_base == NULL) {
  308.         pr_err("%s: Can't ioremap region (0x%08x - 0x%08x)\n", __func__,
  309.                (uint32_t)tssc_res->start, (uint32_t)tssc_res->end);
  310.         err = -ENOMEM;
  311.         goto err_ioremap_tssc;
  312.     }
  313.     ts->input_dev = input_allocate_device();
  314.     if (ts->input_dev == NULL) {
  315.         pr_err("failed to allocate touchscreen input device\n");
  316.         err = -ENOMEM;
  317.         goto err_alloc_input_dev;
  318.     }
  319.     ts->input_dev->name = "msm-touchscreen";
  320.     ts->input_dev->dev.parent = &pdev->dev;
  321.  
  322.     input_set_drvdata(ts->input_dev, ts);
  323.  
  324.     input_set_capability(ts->input_dev, EV_KEY, BTN_TOUCH);
  325.     set_bit(EV_ABS, ts->input_dev->evbit);
  326.    
  327. #if defined(CONFIG_TOUCHSCREEN_VIRTUAL_KEYS)
  328.     set_bit(EV_KEY, ts->input_dev->evbit);
  329.     set_bit(KEY_HOME, ts->input_dev->keybit);
  330.     set_bit(KEY_MENU, ts->input_dev->keybit);
  331.     set_bit(KEY_BACK, ts->input_dev->keybit);
  332. #endif
  333.  
  334.     input_set_abs_params(ts->input_dev, ABS_X, pdata->min_x, pdata->max_x,
  335.                  0, 0);
  336.     input_set_abs_params(ts->input_dev, ABS_Y, pdata->min_y, pdata->max_y,
  337.                  0, 0);
  338.     input_set_abs_params(ts->input_dev, ABS_PRESSURE, pdata->min_press,
  339.                  pdata->max_press, 0, 0);
  340.     err = input_register_device(ts->input_dev);
  341.     if (err != 0) {
  342.         pr_err("%s: failed to register input device\n", __func__);
  343.         goto err_input_dev_reg;
  344.     }
  345.  
  346.   setup_timer(&ts->timer, ts_timer, (unsigned long)ts);
  347.     msm_ts_hw_init(ts);
  348.  
  349.     err = request_irq(ts->sample_irq, msm_ts_irq,
  350.               (irq1_res->flags & ~IORESOURCE_IRQ) | IRQF_DISABLED,
  351.               "msm_touchscreen", ts);
  352.     if (err != 0) {
  353.         pr_err("%s: Cannot register irq1 (%d)\n", __func__, err);
  354.         goto err_request_irq1;
  355.     }
  356.  
  357.     err = request_irq(ts->pen_up_irq, msm_ts_irq,
  358.               (irq2_res->flags & ~IORESOURCE_IRQ) | IRQF_DISABLED,
  359.               "msm_touchscreen", ts);
  360.     if (err != 0) {
  361.         pr_err("%s: Cannot register irq2 (%d)\n", __func__, err);
  362.         goto err_request_irq2;
  363.     }
  364.  
  365.     platform_set_drvdata(pdev, ts);
  366.  
  367. #ifdef CONFIG_HAS_EARLYSUSPEND
  368.     ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
  369.                         TSSC_SUSPEND_LEVEL;
  370.     ts->early_suspend.suspend = msm_ts_early_suspend;
  371.     ts->early_suspend.resume = msm_ts_late_resume;
  372.     register_early_suspend(&ts->early_suspend);
  373. #endif
  374.  
  375.     device_init_wakeup(&pdev->dev, pdata->can_wakeup);
  376.     pr_info("%s: tssc_base=%p irq1=%d irq2=%d\n", __func__,
  377.         ts->tssc_base, (int)ts->sample_irq, (int)ts->pen_up_irq);
  378. //xiayc
  379. #if defined(CONFIG_TOUCHSCREEN_VIRTUAL_KEYS)
  380.     ts_key_report_init();
  381. #endif
  382. //  dump_tssc_regs(ts);
  383.     return 0;
  384.  
  385. err_request_irq2:
  386.     free_irq(ts->sample_irq, ts);
  387.  
  388. err_request_irq1:
  389.     /* disable the tssc */
  390.     tssc_writel(ts, TSSC_CTL_ENABLE, TSSC_CTL);
  391.  
  392. err_input_dev_reg:
  393.     input_set_drvdata(ts->input_dev, NULL);
  394.     input_free_device(ts->input_dev);
  395.  
  396. err_alloc_input_dev:
  397.     iounmap(ts->tssc_base);
  398.  
  399. err_ioremap_tssc:
  400.     kfree(ts);
  401.     return err;
  402. }
  403.  
  404. static struct platform_driver msm_touchscreen_driver = {
  405.     .driver = {
  406.         .name = "msm_touchscreen",
  407.         .owner = THIS_MODULE,
  408. #ifdef CONFIG_PM
  409.         .pm = &msm_touchscreen_pm_ops,
  410. #endif
  411.     },
  412.     .probe      = msm_ts_probe,
  413. };
  414.  
  415. static int __init msm_ts_init(void)
  416. {
  417.     return platform_driver_register(&msm_touchscreen_driver);
  418. }
  419.  
  420. static void __exit msm_ts_exit(void)
  421. {
  422.     platform_driver_unregister(&msm_touchscreen_driver);
  423. }
  424.  
  425. module_init(msm_ts_init);
  426. module_exit(msm_ts_exit);
  427. MODULE_DESCRIPTION("Qualcomm MSM/QSD Touchscreen controller driver");
  428. MODULE_LICENSE("GPL");
  429. MODULE_ALIAS("platform:msm_touchscreen");
Add Comment
Please, Sign In to add comment