Linux GPIO 的註冊與申請

來源:互聯網
上載者:User

標籤:linux kernel   gpio   arm   

Linux GPIO 的註冊與申請

Linux Kernel, GPIO, ARM

在Linux kernel代碼中,經常會使用 GPIO 來作為一個特殊的訊號,如作為晶片的片選訊號等。
GPIO 申請的函數,我們經常用到,如 gpio_request ,那麼 GPIO 是何時,以及如何註冊的,本文就來探索一下。
基於的平台上 freesccale 的 i.MX6

先從函數 gpio_request 的實現開始。

/* These "optional" allocation calls help prevent drivers from stomping * on each other, and help provide better diagnostics in debugfs. * They're called even less than the "set direction" calls. */int gpio_request(unsigned gpio, const char *label){struct gpio_desc*desc;struct gpio_chip*chip;intstatus = -EINVAL;unsigned longflags;spin_lock_irqsave(&gpio_lock, flags);if (!gpio_is_valid(gpio))goto done;// 這兒從 gpio_desc 數組中取了一個 gpio_desc 結構體// 後面的代碼基本上都是基於這個結構體進行的操作// 我們是從數組中取了一個 gpio 的描述,這個描述應該是在 gpio 註冊的時候添加到這個數組的// 以這個數組為線索,看看 gpio 是如何註冊的desc = &gpio_desc[gpio];chip = desc->chip;if (chip == NULL)goto done;if (!try_module_get(chip->owner))goto done;/* NOTE:  gpio_request() can be called in early boot, * before IRQs are enabled, for non-sleeping (SOC) GPIOs. */if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {desc_set_label(desc, label ? : "?");status = 0;} else {status = -EBUSY;module_put(chip->owner);goto done;}if (chip->request) {/* chip->request may sleep */spin_unlock_irqrestore(&gpio_lock, flags);status = chip->request(chip, gpio - chip->base);spin_lock_irqsave(&gpio_lock, flags);if (status < 0) {desc_set_label(desc, NULL);module_put(chip->owner);clear_bit(FLAG_REQUESTED, &desc->flags);}}done:if (status)pr_debug("gpio_request: gpio-%d (%s) status %d\n",gpio, label ? : "?", status);spin_unlock_irqrestore(&gpio_lock, flags);return status;}


以數組 gpio_desc 為線索。
既然我們申請 GPIO 的時候是從這個數字中取資料,那麼註冊 GPIO 的時候就應該往這個數字中添加資料了。
反過來,往這個數組添加資料的地方應該也就是註冊 GPIO 的地方了。
這個數組定義在 Gpiolib.c 檔案中:

static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];


搜尋發現, gpiochip_add 函數中有給數組 gpio_desc 賦值。
 
看看誰調用了函數 gpiochip_add 。
平台相關目錄下的 Gpio.c 檔案中的 mxc_gpio_init 函數調用了 gpiochip_add :

if (!initialed)/* its a serious configuration bug when it fails */BUG_ON(gpiochip_add(&port[i].chip) < 0);


繼續往上找,平台相關目錄下的 Devices.c 有如下函數:

int mx6q_register_gpios(void){/* 7 ports for Mx6 */return mxc_gpio_init(mxc_gpio_ports, 7);}


mxc_gpio_ports 的定義:

static struct mxc_gpio_port mxc_gpio_ports[] = {{.chip.label = "gpio-0",.base = IO_ADDRESS(GPIO1_BASE_ADDR),.irq = MXC_INT_GPIO1_INT15_0_NUM,.irq_high = MXC_INT_GPIO1_INT31_16_NUM,.virtual_irq_start = MXC_GPIO_IRQ_START},{.chip.label = "gpio-1",.base = IO_ADDRESS(GPIO2_BASE_ADDR),.irq = MXC_INT_GPIO2_INT15_0_NUM,.irq_high = MXC_INT_GPIO2_INT31_16_NUM,.virtual_irq_start = MXC_GPIO_IRQ_START + 32 * 1},{.chip.label = "gpio-2",.base = IO_ADDRESS(GPIO3_BASE_ADDR),.irq = MXC_INT_GPIO3_INT15_0_NUM,.irq_high = MXC_INT_GPIO3_INT31_16_NUM,.virtual_irq_start = MXC_GPIO_IRQ_START + 32 * 2},{.chip.label = "gpio-3",.base = IO_ADDRESS(GPIO4_BASE_ADDR),.irq = MXC_INT_GPIO4_INT15_0_NUM,.irq_high = MXC_INT_GPIO4_INT31_16_NUM,.virtual_irq_start = MXC_GPIO_IRQ_START + 32 * 3},{.chip.label = "gpio-4",.base = IO_ADDRESS(GPIO5_BASE_ADDR),.irq = MXC_INT_GPIO5_INT15_0_NUM,.irq_high = MXC_INT_GPIO5_INT31_16_NUM,.virtual_irq_start = MXC_GPIO_IRQ_START + 32 * 4},{.chip.label = "gpio-5",.base = IO_ADDRESS(GPIO6_BASE_ADDR),.irq = MXC_INT_GPIO6_INT15_0_NUM,.irq_high = MXC_INT_GPIO6_INT31_16_NUM,.virtual_irq_start = MXC_GPIO_IRQ_START + 32 * 5},{.chip.label = "gpio-6",.base = IO_ADDRESS(GPIO7_BASE_ADDR),.irq = MXC_INT_GPIO7_INT15_0_NUM,.irq_high = MXC_INT_GPIO7_INT31_16_NUM,.virtual_irq_start = MXC_GPIO_IRQ_START + 32 * 6},};


繼續往上,找到了同目錄下 Irq.c 檔案中的 mx6_init_irq 函數調用了 mx6q_register_gpios 。
board.c 檔案中將 mx6_init_irq 函數賦值給了 machine_desc 結構體的 init_irq 函數:

MACHINE_START(MX6XXXX, "Freescale i.MX 6 Board").boot_params= MX6_PHYS_OFFSET + 0x100,.fixup= fixup_mxc_board,.map_io= mx6_map_io,.init_irq= mx6_init_irq,.init_machine= mx6_board_init,.timer= &mxc_timer,.reserve= mx6q_reserve,MACHINE_END


arch/arm/kernel/irq.c 檔案中有以下函數:

void __init init_IRQ(void){machine_desc->init_irq();}


 

init/main.c 檔案中的 start_kernel 函數調用了 init_IRQ 。

至於 start_kernel 函數何時被調用,有時間再作研究。

總結一下 GPIO 的註冊過程:
start_kernel 函數會調用 init_IRQ 函數。
init_IRQ 函數調用了 machine_desc 結構體的 init_irq 函數。
machine_desc 結構體在 board.c 檔案中定義,其中 init_irq 被賦值為 mx6_init_irq 。
mx6_init_irq 函數中調用了 mx6q_register_gpios 函數。
mx6q_register_gpios 函數的定義見前文,其中調用了函數 mxc_gpio_init 。

函數 mxc_gpio_init 的實現:

int mxc_gpio_init(struct mxc_gpio_port *port, int cnt){int i, j;static bool initialed;/* save for local usage */// port 是前面定義的數組 mxc_gpio_ports , cnt 是數組中元素的個數mxc_gpio_ports = port;gpio_table_size = cnt;printk(KERN_INFO "MXC GPIO hardware\n");for (i = 0; i < cnt; i++) {/* disable the interrupt and clear the status */__raw_writel(0, port[i].base + GPIO_IMR);__raw_writel(~0, port[i].base + GPIO_ISR);for (j = port[i].virtual_irq_start;j < port[i].virtual_irq_start + 32; j++) {irq_set_lockdep_class(j, &gpio_lock_class);/*static struct irq_chip gpio_irq_chip = {.name = "GPIO",.irq_ack = gpio_ack_irq,.irq_mask = gpio_mask_irq,.irq_unmask = gpio_unmask_irq,.irq_set_type = gpio_set_irq_type,.irq_set_wake = gpio_set_wake_irq,};*//** *handle_level_irq - Level type irq handler *@irq:the interrupt number *@desc:the interrupt description structure for this irq * *Level type interrupts are active as long as the hardware line has *the active level. This may require to mask the interrupt and unmask *it after the associated handler has acknowledged the device, so the *interrupt line is back to inactive. */irq_set_chip_and_handler(j, &gpio_irq_chip, handle_level_irq);set_irq_flags(j, IRQF_VALID);}/* register gpio chip */// mxc_gpio_direction_input 將對應 gpio 設定為輸入, mxc_gpio_direction_output 將對應 gpio 設定為輸出,並會設定一個初始值// 這兒的輸入/輸出是對 cpu 來說的port[i].chip.direction_input = mxc_gpio_direction_input;port[i].chip.direction_output = mxc_gpio_direction_output;// 擷取/設定 gpio 狀態port[i].chip.get = mxc_gpio_get;port[i].chip.set = mxc_gpio_set;port[i].chip.base = i * 32;port[i].chip.ngpio = 32;spin_lock_init(&port[i].lock);if (!initialed)/* its a serious configuration bug when it fails */// 添加 gpio chip , 調用的是我們前面用到的一個線索函數, 該函數中有給 gpio_desc 數組賦值BUG_ON(gpiochip_add(&port[i].chip) < 0);if (cpu_is_mx1() || cpu_is_mx3() || cpu_is_mx25() ||cpu_is_mx51() || cpu_is_mx53() || cpu_is_mx6q() ||cpu_is_mx6dl() || cpu_is_mx6sl()) {/* setup one handler for each entry */irq_set_chained_handler(port[i].irq,mx3_gpio_irq_handler);irq_set_handler_data(port[i].irq, &port[i]);if (port[i].irq_high) {/* setup handler for GPIO 16 to 31 */irq_set_chained_handler(port[i].irq_high,mx3_gpio_irq_handler);irq_set_handler_data(port[i].irq_high,     &port[i]);}}}initialed = true;if (cpu_is_mx2()) {/* setup one handler for all GPIO interrupts */irq_set_chained_handler(port[0].irq, mx2_gpio_irq_handler);irq_set_handler_data(port[0].irq, port);}return 0;}


gpiochip_add 函數的實現:

/** * gpiochip_add() - register a gpio_chip * @chip: the chip to register, with chip->base initialized * Context: potentially before irqs or kmalloc will work * * Returns a negative errno if the chip can't be registered, such as * because the chip->base is invalid or already associated with a * different chip.  Otherwise it returns zero as a success code. * * When gpiochip_add() is called very early during boot, so that GPIOs * can be freely used, the chip->dev device must be registered before * the gpio framework's arch_initcall().  Otherwise sysfs initialization * for GPIOs will fail rudely. * * If chip->base is negative, this requests dynamic assignment of * a range of valid GPIOs. */int gpiochip_add(struct gpio_chip *chip){unsigned longflags;intstatus = 0;unsignedid;intbase = chip->base;if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))&& base >= 0) {status = -EINVAL;goto fail;}spin_lock_irqsave(&gpio_lock, flags);if (base < 0) {base = gpiochip_find_base(chip->ngpio);if (base < 0) {status = base;goto unlock;}chip->base = base;}/* these GPIO numbers must not be managed by another gpio_chip */for (id = base; id < base + chip->ngpio; id++) {if (gpio_desc[id].chip != NULL) {status = -EBUSY;break;}}if (status == 0) {for (id = base; id < base + chip->ngpio; id++) {// 發現這兒只是賦值了 gpio_desc 成員的 chip 成員gpio_desc[id].chip = chip;/* REVISIT:  most hardware initializes GPIOs as * inputs (often with pullups enabled) so power * usage is minimized.  Linux code should set the * gpio direction first thing; but until it does, * we may expose the wrong direction in sysfs. */gpio_desc[id].flags = !chip->direction_input? (1 << FLAG_IS_OUT): 0;}}of_gpiochip_add(chip);unlock:spin_unlock_irqrestore(&gpio_lock, flags);if (status)goto fail;// 建立裝置, 並添加對應的 sysfsstatus = gpiochip_export(chip);if (status)goto fail;return 0;fail:/* failures here can mean systems won't boot... */pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",chip->base, chip->base + chip->ngpio - 1,chip->label ? : "generic");return status;}


gpio_desc 結構體的定義:

struct gpio_desc {struct gpio_chip*chip;unsigned longflags;/* flag symbols are bit numbers */#define FLAG_REQUESTED0#define FLAG_IS_OUT1#define FLAG_RESERVED2#define FLAG_EXPORT3/* protected by sysfs_lock */#define FLAG_SYSFS4/* exported via /sys/class/gpio/control */#define FLAG_TRIG_FALL5/* trigger on falling edge */#define FLAG_TRIG_RISE6/* trigger on rising edge */#define FLAG_ACTIVE_LOW7/* sysfs value has active low */#define ID_SHIFT16/* add new flags before this one */#define GPIO_FLAGS_MASK((1 << ID_SHIFT) - 1)#define GPIO_TRIGGER_MASK(BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))#ifdef CONFIG_DEBUG_FSconst char*label;#endif};


 

Linux GPIO 的註冊與申請

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.