⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsc2003_linux.c

📁 the tsc2003 driver C code in linux os
💻 C
📖 第 1 页 / 共 2 页
字号:
351   ret = kernel_thread(tsc2003ts_thread, d, CLONE_KERNEL); 
352   if (ret >= 0) 
353   { 
354       wait_for_completion(&d->tstask_completion); 
355       ret = 0; 
356    } 
357  
358     
359  
360   up(&d->sem); 
361   return ret; 
362 } 
363  
364 static void tsc2003_idev_close (struct input_dev *idev) 
365 { 
366   struct tsc2003_data *d = idev->private; 
367   down_interruptible(&d->sem); 
368   if (d->tstask) 
369     { 
370       send_sig(SIGKILL, d->tstask, 1); 
371       wait_for_completion(&d->tstask_completion); 
372     } 
373  
374   if (timer_pending(&d->penirq_timer)) 
375     del_timer(&d->penirq_timer); 
376   up(&d->sem); 
377   return; 
378 } 
379  
380 #if defined(CONFIG_SYSFS) && defined(CONFIG_SENSORS_TSC2003_SYSFS) 
381 static ssize_t show_addr (struct device *dev, char *buf) 
382 { 
383   struct tsc2003_data *d = container_of(dev->driver, struct  
384 tsc2003_data, driver); 
385   return sprintf(buf, "%d\n", d->client.addr); 
386 } 
387 static DEVICE_ATTR(addr, S_IRUGO, show_addr, NULL); 
388  
389 static ssize_t show_vbat1 (struct device *dev, char *buf) 
390 { 
391   struct tsc2003_data *d = container_of(dev->driver, struct  
392 tsc2003_data, driver); 
393   return sprintf(buf, "%d\n", d->vbat1); 
394 } 
395 static DEVICE_ATTR(vbat1, S_IRUGO, show_vbat1, NULL); 
396  
397 static ssize_t show_vbat2 (struct device *dev, char *buf) 
398 { 
399   struct tsc2003_data *d = container_of(dev->driver, struct  
400 tsc2003_data, driver); 
401   return sprintf(buf, "%d\n", d->vbat2); 
402 } 
403 static DEVICE_ATTR(vbat2, S_IRUGO, show_vbat2, NULL); 
404  
405 static ssize_t show_in1 (struct device *dev, char *buf) 
406 { 
407   struct tsc2003_data *d = container_of(dev->driver, struct  
408 tsc2003_data, driver); 
409   return sprintf(buf, "%d\n", d->in1); 
410 } 
411 static DEVICE_ATTR(in1, S_IRUGO, show_in1, NULL); 
412  
413 static ssize_t show_in2 (struct device *dev, char *buf) 
414 { 
415   struct tsc2003_data *d = container_of(dev->driver, struct  
416 tsc2003_data, driver); 
417   return sprintf(buf, "%d\n", d->in2); 
418 } 
419 static DEVICE_ATTR(in2, S_IRUGO, show_in2, NULL); 
420  
421 static ssize_t show_temp0 (struct device *dev, char *buf) 
422 { 
423   struct tsc2003_data *d = container_of(dev->driver, struct  
424 tsc2003_data, driver); 
425   return sprintf(buf, "%d\n", d->temp0); 
426 } 
427 static DEVICE_ATTR(temp0, S_IRUGO, show_temp0, NULL); 
428  
429 static ssize_t show_temp1 (struct device *dev, char *buf) 
430 { 
431   struct tsc2003_data *d = container_of(dev->driver, struct  
432 tsc2003_data, driver); 
433   return sprintf(buf, "%d\n", d->temp1); 
434 } 
435 static DEVICE_ATTR(temp1, S_IRUGO, show_temp1, NULL); 
436  
437 #warning "TODO: this daemon sometimes hangs the touchscreen daemon" 
438 #warning "TODO: under periods of heavy touch screen activity." 
439 #warning "TODO: Use with caution until the bug is squashed." 
440 static int tsc2003s_thread (void *v) 
441 { 
442   struct tsc2003_data *d = v; 
443  
444   daemonize(DRIVER_NAME "sd"); 
445   allow_signal(SIGKILL); 
446  
447   printk(KERN_INFO "%s: address 0x%x\n", 
448          __FUNCTION__, d->client.addr); 
449  
450   while (!signal_pending(current)) 
451     { 
452       if (!down_interruptible(&d->sem)) 
453         { 
454           if (!timer_pending(&d->penirq_timer)) 
455             { 
456               tsc2003_read_vbat1(d, d->pd, &d->vbat1); 
457               tsc2003_read_vbat2(d, d->pd, &d->vbat2); 
458               tsc2003_read_in1(d, d->pd, &d->in1); 
459               tsc2003_read_in2(d, d->pd, &d->in2); 
460               tsc2003_read_temp0(d, d->pd, &d->temp0); 
461               tsc2003_read_temp1(d, d->pd, &d->temp1); 
462             } 
463           up(&d->sem); 
464         } 
465       set_task_state(current, TASK_INTERRUPTIBLE); 
466       schedule_timeout(5 * HZ); 
467     } 
468   do_exit(0); 
469 } 
470 #endif 
471  
472 static int tsc2003_detect_irq (struct tsc2003_data *d) 
473 { 
474         d->penirq = IRQ_GPIO(16);       //PWM0 GPIO 
475         return 0; 
476 } 
477  
478 /* 
479 static int tsc2003_probe (struct device *dev) 
480 { 
481   //struct platform_device *p = to_platform_device(dev);   
482   struct tsc2003_data *d = container_of(dev->driver, struct tsc2003_data, driver); 
483   int ret = 0;   
484   int error; 
485   
486   printk(KERN_ERR "TSC2003: tsc2003_probe probing...\n");  
487  
488   error = tsc2003_detect_irq(d); 
489    if(error) 
490    { 
491         printk(KERN_ERR "TSC2003: IRQ probe failed\n"); 
492    } 
493  
494    if (d->penirq) 
495    { 
496       ret = request_irq(d->penirq, tsc2003_penirq, IRQF_TRIGGER_FALLING, DRIVER_NAME, d); 
497       if (!ret) 
498         { 
499           printk(KERN_INFO "%s: irq %d\n", __FUNCTION__, d->penirq); 
500         } 
501        else 
502         { 
503           printk(KERN_INFO "%s: cannot grab irq %d\n", __FUNCTION__, d->penirq); 
504         } 
505     } 
506  
507 #if defined(CONFIG_SYSFS) && defined(CONFIG_SENSORS_TSC2003_SYSFS) 
508   ret = kernel_thread(tsc2003s_thread, d, CLONE_KERNEL); 
509   if (ret >= 0) 
510     ret = 0; 
511  
512   device_create_file(dev, &dev_attr_addr); 
513   device_create_file(dev, &dev_attr_vbat1); 
514   device_create_file(dev, &dev_attr_vbat2); 
515   device_create_file(dev, &dev_attr_in1); 
516   device_create_file(dev, &dev_attr_in2); 
517   device_create_file(dev, &dev_attr_temp0); 
518   device_create_file(dev, &dev_attr_temp1); 
519 #endif 
520  
521  
522   return ret; 
523 } 
524 */ 
525  
526 static int tsc2003_driver_register (struct tsc2003_data *data) 
527 { 
528   struct input_dev *idev; 
529   int ret = 0; 
530   int error;   
531  
532   init_MUTEX(&data->sem); 
533  
534   init_timer(&data->penirq_timer); 
535   data->penirq_timer.data = (unsigned long)data; 
536   data->penirq_timer.function = tsc2003_pen_up;   
537  
538   error = tsc2003_detect_irq(data); 
539    if(error) 
540    { 
541         printk(KERN_ERR "TSC2003: IRQ probe failed\n"); 
542    } 
543  
544    if (data->penirq) 
545    { 
546       ret = request_irq(data->penirq, tsc2003_penirq, IRQF_TRIGGER_FALLING, DRIVER_NAME, data); 
547       if (!ret) 
548         { 
549           printk(KERN_INFO "%s: irq %d\n", __FUNCTION__, data->penirq); 
550           init_completion(&data->tstask_completion); 
551           init_completion(&data->penirq_completion); 
552         } 
553        else 
554         { 
555           printk(KERN_ERR "%s: cannot grab irq %d\n", __FUNCTION__, data->penirq); 
556         } 
557     } 
558    
559   idev = input_allocate_device(); 
560   data->idev = idev; 
561  
562   idev->private = data; 
563   idev->name = DRIVER_NAME; 
564   idev->evbit[0] = BIT(EV_ABS); 
565   idev->open = tsc2003_idev_open; 
566   idev->close = tsc2003_idev_close; 
567   idev->absbit[LONG(ABS_X)] = BIT(ABS_X); 
568   idev->absbit[LONG(ABS_Y)] = BIT(ABS_Y); 
569   idev->absbit[LONG(ABS_PRESSURE)] = BIT(ABS_PRESSURE); 
570   input_set_abs_params(idev, ABS_X, 0, ADC_MAX, 0, 0); 
571   input_set_abs_params(idev, ABS_Y, 0, ADC_MAX, 0, 0); 
572   input_set_abs_params(idev, ABS_PRESSURE, 0, 0, 0, 0); 
573  
574   if (!ret) 
575   { 
576     input_register_device(idev); 
577         printk(KERN_INFO "tsc2003 - registering input device\n"); 
578   } 
579  
580   return ret; 
581 } 
582  
583 static int tsc2003_i2c_attach_adapter(struct i2c_adapter *adapter) 
584 { 
585   printk(KERN_INFO "tsc2003 i2c touch screen controller\n"); 
586   printk(KERN_INFO "Bill Gatliff <bgat at billgatliff.com\n"); 
587   printk(KERN_INFO "Nicholas Chen <nchen at cs.umd.edu>\n"); 
588   
589   return i2c_probe(adapter, &addr_data, tsc2003_i2c_detect); 
590 } 
591  
592 static int tsc2003_i2c_detach_client(struct i2c_client *client) 
593 { 
594   int err;   
595   struct tsc2003_data *d = i2c_get_clientdata(client); 
596  
597   free_irq(d->penirq,d); 
598   input_unregister_device(d->idev); 
599  
600   if ((err = i2c_detach_client(client))) { 
601     dev_err(&client->dev, "Client deregistration failed, " 
602             "client not detached.\n"); 
603     return err; 
604   } 
605  
606   return 0; 
607 } 
608  
609 static struct i2c_driver tsc2003_driver = { 
610   .driver = { 
611           .owner        = THIS_MODULE, 
612           .name         = DRIVER_NAME, 
613    }, 
614   .attach_adapter    = tsc2003_i2c_attach_adapter, 
615   .detach_client    = tsc2003_i2c_detach_client, 
616   //  .command              = tsc2003_command, 
617 }; 
618  
619 static int tsc2003_i2c_detect (struct i2c_adapter *adapter, int address,  
620 int kind) 
621 { 
622   struct i2c_client *new_client; 
623   struct tsc2003_data *data; 
624  
625   int err = 0; 
626   const char *name = ""; 
627  
628   if (!i2c_check_functionality(adapter, 
629                                I2C_FUNC_SMBUS_BYTE_DATA 
630                                | I2C_FUNC_I2C 
631                                | I2C_FUNC_SMBUS_WORD_DATA)) 
632     goto exit; 
633  
634   data = kcalloc(1, sizeof(*data), GFP_KERNEL); 
635   if (!data) { 
636     err = -ENOMEM; 
637     goto exit; 
638   } 
639  
640   new_client = &data->client; 
641   i2c_set_clientdata(new_client, data); 
642   new_client->addr = address; 
643   new_client->adapter = adapter; 
644   new_client->driver = &tsc2003_driver; 
645   new_client->flags = 0; 
646  
647   /* TODO: I'm pretty sure I'm not dealing with kind correctly */ 
648   if (kind == 0 /* identification */ || kind < 0 /* detection */) 
649       kind = tsc2003; 
650  
651   if (kind == tsc2003) 
652     name = DRIVER_NAME; 
653  
654   /* try a command, see if we get an ack; 
655      if we do, assume it's our device */ 
656   printk(KERN_INFO "%s: probing address 0x%x\n", 
657          __FUNCTION__, address); 
658   err = tsc2003_powerdown(data); 
659   if (err >= 0) 
660     { 
661       strlcpy(new_client->name, name, I2C_NAME_SIZE); 
662       err = i2c_attach_client(new_client); 
663       if (err) goto exit_free; 
664  
665       tsc2003_init_client(new_client); 
666  
667       err = tsc2003_driver_register(data); 
668       if (err < 0) goto exit_free; 
669       
670       printk(KERN_INFO "%s: device address 0x%x attached.\n", 
671              __FUNCTION__, address); 
672       return 0; 
673     } 
674   /* failure to detect when everything else is ok isn't an error */ 
675   else err = 0; 
676  
677  exit_free: 
678   kfree(new_client); 
679  exit: 
680   return err; 
681 } 
682  
683 static int __init tsc2003_init(void) 
684 { 
685   return i2c_add_driver(&tsc2003_driver); 
686 } 
687  
688 static void __exit tsc2003_exit(void) 
689 { 
690   i2c_del_driver(&tsc2003_driver); 
691 } 
692  
693 MODULE_AUTHOR("Bill Gatliff <bgat at billgatliff.com>"); 
694 MODULE_DESCRIPTION("TSC2003 Touch Screen Controller driver"); 
695 MODULE_LICENSE("GPL"); 
696  
697 module_init(tsc2003_init); 
698 module_exit(tsc2003_exit); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -