leds子系统-下

(本文内容涉及的代码全部取自linux-2.6.36内核中)

书接上文,洋洋洒洒的代码贴了那么多,不过好像并没有看到led子系统针对驱动开发用户的实际操作,下面就对gpio-led功能继续剖析:

 

1.  Gpio-led初始化

drivers/leds/leds-gpio.c(line317-line348)

 

static int __init gpio_led_init(void)

{

       intret;

 

#ifdef CONFIG_LEDS_GPIO_PLATFORM   

       ret= platform_driver_register(&gpio_led_driver);

       if(ret)

              returnret;

#endif

#ifdef CONFIG_LEDS_GPIO_OF

       ret= of_register_platform_driver(&of_gpio_leds_driver);

#endif

#ifdef CONFIG_LEDS_GPIO_PLATFORM   

       if(ret)

              platform_driver_unregister(&gpio_led_driver);

#endif

 

       returnret;

}

 

static void __exit gpio_led_exit(void)

{

#ifdef CONFIG_LEDS_GPIO_PLATFORM

       platform_driver_unregister(&gpio_led_driver);

#endif

#ifdef CONFIG_LEDS_GPIO_OF

       of_unregister_platform_driver(&of_gpio_leds_driver);

#endif

}

 

module_init(gpio_led_init);

module_exit(gpio_led_exit);

 

gpio_led_init才是对led设备的实际注册功能,这就是前面提到的,在leds子系统初始化时使用的是subsys_initcall而不是module_init的原因。

在gpio_led_init初始化提供了struct platform_driver和structof_platform_driver两种结构可以选择。

 

2.

drivers/leds/leds-gpio.c(line205-line212)

 

static struct platform_drivergpio_led_driver = {

       .probe           = gpio_led_probe,

       .remove        = __devexit_p(gpio_led_remove),

       .driver           = {

              .name    = "leds-gpio",

              .owner   = THIS_MODULE,

       },

};

 

3.

drivers/leds/leds-gpio.c(line155-line187)

 

static int __devinit gpio_led_probe(structplatform_device *pdev)

{

       structgpio_led_platform_data *pdata = pdev->dev.platform_data;

       structgpio_led_data *leds_data;

       inti, ret = 0;

 

       if(!pdata)

              return-EBUSY;

 

       leds_data= kzalloc(sizeof(struct gpio_led_data) * pdata->num_leds,

                            GFP_KERNEL);

       if(!leds_data)

              return-ENOMEM;

       //创建gpio-led设备

       for(i = 0; i < pdata->num_leds; i++) {

              ret= create_gpio_led(&pdata->leds[i],&leds_data[i],

                                  &pdev->dev,pdata->gpio_blink_set);

              if(ret < 0)

                     gotoerr;

       }

       //创建设备链表

       platform_set_drvdata(pdev,leds_data);

 

       return0;

 

err:

       for(i = i - 1; i >= 0; i--)

              delete_gpio_led(&leds_data[i]);

 

       kfree(leds_data);

 

       returnret;

}

 

4.再让我们看看create_gpio_led这个函数的功能,这个函数对gpio的硬件功能进行初始化,这个函数用到我前面提过的led_classdev_register函数,所以这个函数应该实现的功能包括申请gpio资源、初始化gpio、创建创建一个struct device并且注册sysfs,生成/sys/class/leds目录与硬件相关。代码内容如下

 

static int __devinit create_gpio_led(conststruct gpio_led *template,

       structgpio_led_data *led_dat, struct device *parent,

       int(*blink_set)(unsigned, int, unsigned long *, unsigned long *))

{

       intret, state;

 

       led_dat->gpio= -1;

 

       /*skip leds that aren't available */

       if(!gpio_is_valid(template->gpio)) {

              printk(KERN_INFO"Skipping unavailable LED gpio %d (%s)\n",

                            template->gpio,template->name);

              return0;

       }

 

       ret= gpio_request(template->gpio, template->name);

       if(ret < 0)

              returnret;

 

       led_dat->cdev.name= template->name;

       led_dat->cdev.default_trigger= template->default_trigger;

       led_dat->gpio= template->gpio;

       led_dat->can_sleep= gpio_cansleep(template->gpio);

       led_dat->active_low= template->active_low;

       led_dat->blinking= 0;

       if(blink_set) {

              led_dat->platform_gpio_blink_set= blink_set;

              led_dat->cdev.blink_set= gpio_blink_set;

       }

       led_dat->cdev.brightness_set= gpio_led_set;

       if(template->default_state == LEDS_GPIO_DEFSTATE_KEEP)

              state= !!gpio_get_value(led_dat->gpio) ^ led_dat->active_low;

       else

              state= (template->default_state == LEDS_GPIO_DEFSTATE_ON);

       led_dat->cdev.brightness= state ? LED_FULL : LED_OFF;

       if(!template->retain_state_suspended)

              led_dat->cdev.flags|= LED_CORE_SUSPENDRESUME;

 

       ret= gpio_direction_output(led_dat->gpio, led_dat->active_low ^ state);

       if(ret < 0)

              gotoerr;

             

       INIT_WORK(&led_dat->work,gpio_led_work);

 

       ret= led_classdev_register(parent,&led_dat->cdev);

       if(ret < 0)

              gotoerr;

 

       return0;

err:

       gpio_free(led_dat->gpio);

       returnret;

}

 

module_init执行完成之后,一个真正的leds设备就真正创建完成了。

个人水平有限,还有很多错误和不足之处,望指正。


你可能感兴趣的:(struct,Module,delete,output)