设备树:
leds:leds{
compatible = "xx,xx-led";
};
驱动:
static unsigned int led = 0;
static ssize_t led_status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%s:%d.n", "led", led);
}
static ssize_t led_status_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%d", &led);
return count;
}
static DEVICE_ATTR(led_status, 0600, led_status_show, led_status_store);
static struct attribute *led_attributes[]={
&dev_attr_led_status.attr,
NULL,
};
static const struct attribute_group led_attrs={
.attrs = led_attributes,
};
static int xx_led_probe(struct platform_device *pdev)
{
sysfs_create_group(&pdev- >dev.kobj, &led_attrs);
return 0;
}
static int xx_led_remove(struct platform_device *pdev)
{
sysfs_remove_group(&pdev- >dev.kobj, &led_attrs);
return 0;
}
static const struct of_device_id xx_led_of_match[] = {
{.compatible = "xx,xx-led"},
};
static struct platform_driver xx_led_driver = {
.probe = xx_led_probe,
.remove = xx_led_remove,
.driver = {
.name = "xx-led",
.owner = THIS_MODULE,
.of_match_table = xx_led_of_match,
},
};
static int __init xx_led_init(void)
{
return platform_driver_register(&xx_led_driver );
}
static void __exit xx_led_exit(void)
{
platform_driver_unregister(&xx_led_driver);
}
module_init(xx_led_init);
module_exit(xx_led_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("xx led driver");
MODULE_AUTHOR("Vincent");
MODULE_VERSION("V1.0.00");
驱动加载后,就可以在linux终端中,使用cat
和echo
命令来查看和修改驱动中led
变量的值。例如:
//查看led变量的值
cat /sys/devices/platform/leds/led_status
led:0.
//修改led变量的值为9
echo 9 > /sys/devices/platform/leds/led_status
//查看
cat /sys/devices/platform/leds/led_status
led:9.
全部0条评论
快来发表一下你的评论吧 !