这里拿linux/drivers/leds/leds-ss4200.c来做模板参考学习
注册struct pci_driver
static struct pci_driver nas_gpio_pci_driver = {
.name = KBUILD_MODNAME,
.id_table = ich7_lpc_pci_id,
.probe = ich7_lpc_probe,
.remove = ich7_lpc_remove,
};
static int __init nas_gpio_init(void)
{
ret = pci_register_driver(&nas_gpio_pci_driver);
}
static void __exit nas_gpio_exit(void)
{
pci_unregister_driver(&nas_gpio_pci_driver);
}
module_init(nas_gpio_init);
module_exit(nas_gpio_exit);
probe
使能PCI设备,读取PCI设备对应在CPU总线上的寄存器地址
/* ICH7 LPC/GPIO PCI Config register offsets */
#define PMBASE 0x040
#define GPIO_BASE 0x048
#define GPIO_CTRL 0x04c
#define GPIO_EN 0x010
/* The ICH7 GPIO register block is 64 bytes in size. */
#define ICH7_GPIO_SIZE 64
static int ich7_lpc_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
status = pci_enable_device(dev);
status = pci_read_config_dword(dev, PMBASE, &g_pm_io_base);
status = pci_read_config_dword(dev, GPIO_CTRL, &gc);
status = pci_read_config_dword(dev, GPIO_BASE, &nas_gpio_io_base);
gp_gpio_resource = request_region(nas_gpio_io_base, ICH7_GPIO_SIZE, KBUILD_MODNAME);
}
除了可以用pci_read_config_word读取pci设备的访问地址之外,一般是通过pci_resource_start(pci_dev, 0),然后再通过request_region申请访问这块地址。
操作PCI设备
通过从PCI设备配置信息中读取的nas_gpio_io_base寄存器地址操作led
static void __nasgpio_led_set_attr(struct led_classdev *led_cdev, u32 port, u32 value)
{
struct nasgpio_led *led = led_classdev_to_nasgpio_led(led_cdev);
u32 gpio_out;
gpio_out = inl(nas_gpio_io_base + port);
if (value)
gpio_out |= (1<<led->gpio_bit);
else
gpio_out &= ~(1<<led->gpio_bit);
outl(gpio_out, nas_gpio_io_base + port);
}