您的位置:首页 > 运维架构 > Linux

Linux 内核设备驱动之GPIO驱动之GPIO sysfs支持

2017-03-10 09:09 686 查看
需要内核配置CONFIG_GPIO_SYSFS

intgpiochip_sysfs_register(structgpio_device*gdev)
{
structdevice*dev;
structdevice*parent;
structgpio_chip*chip=gdev->chip;
/*
*ManysystemsaddgpiochipsforSOCsupportveryearly,
*beforedrivermodelsupportisavailable.Inthosecaseswe
*registerlater,ingpiolib_sysfs_init()...herewejust
*verifythat_some_fieldofgpio_classgotinitialized.
*/
if(!gpio_class.p)
return0;
/*
*Forsysfsbackwardcompatibilityweneedtopreservethis
*preferredparentingtothegpio_chipparentfield,ifset.
*/
if(chip->parent)
parent=chip->parent;
else
parent=&gdev->dev;
/*usechip->basefortheID;it'salreadyknowntobeunique*/
dev=device_create_with_groups(&gpio_class,parent,
MKDEV(0,0),
chip,gpiochip_groups,
"gpiochip%d",chip->base);
if(IS_ERR(dev))
returnPTR_ERR(dev);
mutex_lock(&sysfs_lock);
gdev->mockdev=dev;
mutex_unlock(&sysfs_lock);
return0;
}

voidgpiochip_sysfs_unregister(structgpio_device*gdev)
{
structgpio_desc*desc;
structgpio_chip*chip=gdev->chip;
unsignedinti;
if(!gdev->mockdev)
return;
device_unregister(gdev->mockdev);
/*preventfurthergpiodexports*/
mutex_lock(&sysfs_lock);
gdev->mockdev=NULL;
mutex_unlock(&sysfs_lock);
/*unregistergpiodclassdevicesownedbysysfs*/
for(i=0;i<chip->ngpio;i++){
desc=&gdev->descs[i];
if(test_and_clear_bit(FLAG_SYSFS,&desc->flags))
gpiod_free(desc);
}
}
staticint__initgpiolib_sysfs_init(void)
{
intstatus;
unsignedlongflags;
structgpio_device*gdev;
status=class_register(&gpio_class);
if(status<0)
returnstatus;
/*Scanandregisterthegpio_chipswhichregisteredvery
*early(e.g.beforetheclass_registerabovewascalled).
*
*Werunbeforearch_initcall()sochip->devnodescanhave
*registered,andsoarch_initcall()canalwaysgpio_export().
*/
spin_lock_irqsave(&gpio_lock,flags);
list_for_each_entry(gdev,&gpio_devices,list){
if(gdev->mockdev)
continue;
/*
*TODOweyieldgpio_lockherebecause
*gpiochip_sysfs_register()acquiresamutex.Thisisunsafe
*andneedstobefixed.
*
*Alsoitwouldbenicetousegpiochip_find()heresowe
*cankeepgpio_chipslocaltogpiolib.c,buttheyieldof
*gpio_lockpreventsusfromdoingthis.
*/
spin_unlock_irqrestore(&gpio_lock,flags);
status=gpiochip_sysfs_register(gdev);
spin_lock_irqsave(&gpio_lock,flags);
}
spin_unlock_irqrestore(&gpio_lock,flags);
returnstatus;
}
postcore_initcall(gpiolib_sysfs_init);


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: