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);
相关文章推荐
- Linux 内核设备驱动之GPIO驱动之GPIO GPIO描述符到GPIO CHIP
- Linux 内核设备驱动之GPIO驱动之GPIO 框架初始化
- Linux 内核设备驱动之GPIO驱动之GPIO 控制器描述
- Linux 内核设备驱动之GPIO驱动之GPIO 获取GPIO方向
- Linux 内核设备驱动之GPIO驱动之GPIO 管脚描述
- Linux 内核设备驱动之GPIO驱动之GPIO 控制器设备描述
- Linux 内核设备驱动之GPIO驱动之GPIO 获取GPIO描述符
- Linux 内核设备驱动之GPIO驱动之GPIO API
- Linux 内核设备驱动之GPIO驱动之GPIO 由名字到GPIO描述符
- 编写LED混杂设备驱动:静态映射,如何用Linux内核里的gpio_request(),gpio_set_value()等函数,ioctl函数
- Linux 内核设备驱动之GPIO驱动之GPIO GPIO字符设备初始化
- Linux 内核设备驱动之GPIO驱动之GPIO GPIO描述符到GPIO号
- Linux 内核设备驱动之GPIO驱动之GPIO 管脚添加
- linux驱动开发之字符设备--内核和用户空间数据的交换(sysfs)
- Linux 内核设备驱动之GPIO驱动之GPIO gpiochip注册
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响(一)
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响 -zt
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
- 编写Linux设备驱动时内核版本号的判断