linux pinctrl驅動

轉載地址:https://blog.csdn.net/chenliang0224/article/details/78777995

linux系統下采用pinctrl子系統管理所有的IO管腳,並對設備外圍管腳(如串口、I2C、spi、LCD)都有相應的配置模式,本博客以pinctrl子系統細說該驅動架構。

1. pinctrl設備註冊、退出

[cpp]  view plain  copy
  1. static int __init nuc970_pinctrl_init(void)  
  2. {  
  3.     return platform_driver_register(&nuc970_pinctrl_driver);  
  4. }  
  5. arch_initcall(nuc970_pinctrl_init);  
  6.   
  7. static void __exit nuc970_pinctrl_exit(void)  
  8. {  
  9.     platform_driver_unregister(&nuc970_pinctrl_driver);  
  10. }  
  11.   
  12. module_exit(nuc970_pinctrl_exit);  
[cpp]  view plain  copy
  1. static struct platform_driver nuc970_pinctrl_driver = {  
  2.     .driver = {  
  3.         .name = "pinctrl-nuc970",  
  4.         .owner = THIS_MODULE,  
  5.     },  
  6.     .probe = nuc970_pinctrl_probe,  
  7.     .remove = nuc970_pinctrl_remove,  
  8. };  
[cpp]  view plain  copy
  1. static int nuc970_pinctrl_probe(struct platform_device *pdev)  
  2. {  
  3.     struct pinctrl_dev *pctl;  
  4.   
  5.     pctl = pinctrl_register(&nuc970_pinctrl_desc, &pdev->dev, NULL);  
  6.     if (IS_ERR(pctl))  
  7.         pr_err("could not register NUC970 pin driver\n");  
  8.   
  9.     platform_set_drvdata(pdev, pctl);  
  10.   
  11.     return pinctrl_register_mappings(nuc970_pinmap, ARRAY_SIZE(nuc970_pinmap));  
  12.   
  13. }  
platform平臺驅動設備的註冊流程都是類似,具體註冊流程可參考: 點擊打開鏈接 ,而arch_initcall(...)系統接口函數將註冊該驅動,函數路徑:inux-3.10.x\include\linux\init.h。現在我們主要分析下結構體struct nuc970_pinctrl_desc管腳描述。

2. struct nuc970_pinctrl_desc

先看下struct pinctrl_desc結構體定義的具體內容:

[cpp]  view plain  copy
  1. struct pinctrl_desc {  
  2.     const char *name; //管腳控制名字  
  3.     struct pinctrl_pin_desc const *pins; //管腳描述,包括管腳編號和管腳字符串描述,見下面!  
  4.     unsigned int npins; //管腳個數  
  5.     const struct pinctrl_ops *pctlops; //管腳控制操作,見下面!  
  6.     const struct pinmux_ops *pmxops; //管腳複用操作,見下面!  
  7.     const struct pinconf_ops *confops; //管腳配置操作,見下面!  
  8.     struct module *owner;  
  9. };  

管腳描述符控制結構體:

[cpp]  view plain  copy
  1. struct pinctrl_pin_desc {  
  2.     unsigned number; //pin管腳值 如:PA01=0x00  
  3.     const char *name; //pin管腳名字 如:PA01="PA0"  
  4. };  

管腳操作結構體:
[cpp]  view plain  copy
  1. struct pinctrl_ops {  
  2.     int (*get_groups_count) (struct pinctrl_dev *pctldev); //一個驅動控制器由哪些管腳構成,這裏表示獲取多少個控制器設備,見結構體nuc970_pinctrl_groups[]!  
  3.     const char *(*get_group_name) (struct pinctrl_dev *pctldev,  
  4.                        unsigned selector); //每一組控制器都有對應的名字,如下面nuc970_pinctrl_groups[]結構體中第一個成員控制器的名字爲「emac0_grp」  
  5.     int (*get_group_pins) (struct pinctrl_dev *pctldev,  
  6.                    unsigned selector,  
  7.                    const unsigned **pins,  
  8.                    unsigned *num_pins); //獲取一個驅動控制器由哪些外圍管腳構成和管腳的個數,如"emac0_grp"是由「emac0_pins[]」數組構成  
  9.     void (*pin_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,  
  10.               unsigned offset);  
  11.     int (*dt_node_to_map) (struct pinctrl_dev *pctldev,  
  12.                    struct device_node *np_config,  
  13.                    struct pinctrl_map **map, unsigned *num_maps);  
  14.     void (*dt_free_map) (struct pinctrl_dev *pctldev,  
  15.                  struct pinctrl_map *map, unsigned num_maps);  
  16. };  

由於一個控制器設備存在複用管腳,如nuc972 nandflash設備有兩路管腳選擇,所以我們在實際使用過程中需要選擇任意一路,而struct pinmux就是對控制器設備複用選擇操作:

[cpp]  view plain  copy
  1. struct pinmux_ops {  
  2.     int (*request) (struct pinctrl_dev *pctldev, unsigned offset);  
  3.     int (*free) (struct pinctrl_dev *pctldev, unsigned offset);  
  4.     int (*get_functions_count) (struct pinctrl_dev *pctldev); //獲取管腳複用個數  
  5.     const char *(*get_function_name) (struct pinctrl_dev *pctldev,  
  6.                       unsigned selector); //獲取管腳複用名字,如「emac0」  
  7.     int (*get_function_groups) (struct pinctrl_dev *pctldev,  
  8.                   unsigned selector,  
  9.                   const char * const **groups,  
  10.                   unsigned * const num_groups); //獲取控制器有多少個複用,我們還是以「emac0」爲例,它只對應一個,即「emac0_grp」  
  11.     int (*enable) (struct pinctrl_dev *pctldev, unsigned func_selector,   
  12.                unsigned group_selector); //使能控制器,即配置控制器如果存在管腳複用,在實際使用中需選擇一路來運用,下面會介紹  
  13.     void (*disable) (struct pinctrl_dev *pctldev, unsigned func_selector,  
  14.              unsigned group_selector); //釋放管腳  
  15.     int (*gpio_request_enable) (struct pinctrl_dev *pctldev,  
  16.                     struct pinctrl_gpio_range *range,  
  17.                     unsigned offset);  
  18.     void (*gpio_disable_free) (struct pinctrl_dev *pctldev,  
  19.                    struct pinctrl_gpio_range *range,  
  20.                    unsigned offset);  
  21.     int (*gpio_set_direction) (struct pinctrl_dev *pctldev,  
  22.                    struct pinctrl_gpio_range *range,  
  23.                    unsigned offset,  
  24.                    bool input);  
  25. };  
管腳配置操作結構體:
[cpp]  view plain  copy
  1. struct pinconf_ops {  
  2. #ifdef CONFIG_GENERIC_PINCONF  
  3.     bool is_generic;  
  4. #endif    
  5.     int (*pin_config_get) (struct pinctrl_dev *pctldev,  
  6.                    unsigned pin,  
  7.                    unsigned long *config);  
  8.     int (*pin_config_set) (struct pinctrl_dev *pctldev,  
  9.                    unsigned pin,  
  10.                    unsigned long config);  
  11.     int (*pin_config_group_get) (struct pinctrl_dev *pctldev,  
  12.                      unsigned selector,  
  13.                      unsigned long *config);  
  14.     int (*pin_config_group_set) (struct pinctrl_dev *pctldev,  
  15.                      unsigned selector,  
  16.                      unsigned long config);  
  17.     int (*pin_config_dbg_parse_modify) (struct pinctrl_dev *pctldev,  
  18.                        const char *arg,  
  19.                        unsigned long *config);  
  20.     void (*pin_config_dbg_show) (struct pinctrl_dev *pctldev,  
  21.                      struct seq_file *s,  
  22.                      unsigned offset);  
  23.     void (*pin_config_group_dbg_show) (struct pinctrl_dev *pctldev,  
  24.                        struct seq_file *s,  
  25.                        unsigned selector);  
  26.     void (*pin_config_config_dbg_show) (struct pinctrl_dev *pctldev,  
  27.                         struct seq_file *s,  
  28.                         unsigned long config);  
  29. };  

上面主要介紹需要用到的結構體,下面我們將一步步分析pinctrl配置:
[cpp]  view plain  copy
  1. static struct pinctrl_desc nuc970_pinctrl_desc = {  
  2.     .name = "nuc970-pinctrl_desc"//見下面  
  3.     .pins = nuc970_pins,  
  4.     .npins = ARRAY_SIZE(nuc970_pins),  
  5.     .pctlops = &nuc970_pctrl_ops, //見下面   
  6.     .pmxops = &nuc970_pmxops,  
  7.     .owner = THIS_MODULE,  
  8. };  

"nuc970_pins"管腳映射結構體:

[cpp]  view plain  copy
  1. const struct pinctrl_pin_desc nuc970_pins[] = {  
  2.     PINCTRL_PIN(0x00, "PA0"),  
  3.     PINCTRL_PIN(0x01, "PA1"),  
  4.     //......  
  5.     PINCTRL_PIN(0x40, "PE0"),  
  6.     PINCTRL_PIN(0x41, "PE1"),  
  7.     PINCTRL_PIN(0x42, "PE2"),  
  8.     PINCTRL_PIN(0x43, "PE3"),  
  9.     PINCTRL_PIN(0x44, "PE4"),  
  10.     PINCTRL_PIN(0x45, "PE5"),  
  11.     PINCTRL_PIN(0x46, "PE6"),  
  12.     PINCTRL_PIN(0x47, "PE7"),  
  13.     PINCTRL_PIN(0x48, "PE8"),  
  14.     PINCTRL_PIN(0x49, "PE9"),  
  15.     PINCTRL_PIN(0x4A, "PE10"),  
  16.     PINCTRL_PIN(0x4B, "PE11"),  
  17.     PINCTRL_PIN(0x4C, "PE12"),  
  18.     PINCTRL_PIN(0x4D, "PE13"),  
  19.     PINCTRL_PIN(0x4E, "PE14"),  
  20.     PINCTRL_PIN(0x4F, "PE15"),  
  21.     PINCTRL_PIN(0x50, "PF0"),  
  22.     PINCTRL_PIN(0x51, "PF1"),  
  23.     PINCTRL_PIN(0x52, "PF2"),  
  24.     PINCTRL_PIN(0x53, "PF3"),  
  25.     PINCTRL_PIN(0x54, "PF4"),  
  26.     PINCTRL_PIN(0x55, "PF5"),  
  27.     PINCTRL_PIN(0x56, "PF6"),  
  28.     PINCTRL_PIN(0x57, "PF7"),  
  29.     PINCTRL_PIN(0x58, "PF8"),  
  30.     PINCTRL_PIN(0x59, "PF9"),  
  31.     PINCTRL_PIN(0x5A, "PF10"),  
  32.     PINCTRL_PIN(0x5B, "PF11"),  
  33.     PINCTRL_PIN(0x5C, "PF12"),  
  34.     PINCTRL_PIN(0x5D, "PF13"),  
  35.     PINCTRL_PIN(0x5E, "PF14"),  
  36.     PINCTRL_PIN(0x5F, "PF15"),  
  37.     //......  
  38. };  

[cpp]  view plain  copy
  1. static struct pinctrl_ops nuc970_pctrl_ops = {  
  2.     .get_groups_count = nuc970_get_groups_count,  
  3.     .get_group_name = nuc970_get_group_name,  
  4.     .get_group_pins = nuc970_get_group_pins,  
  5. };  
結構體的函數比較簡單,如下:
[cpp]  view plain  copy
  1. static int nuc970_get_groups_count(struct pinctrl_dev *pctldev)  
  2. {  
  3.     return ARRAY_SIZE(nuc970_pinctrl_groups);  
  4. }  
  5.   
  6. static const char *nuc970_get_group_name(struct pinctrl_dev *pctldev,  
  7.                        unsigned selector)  
  8. {;  
  9.     return nuc970_pinctrl_groups[selector].name;  
  10. }  
  11.   
  12. static int nuc970_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,  
  13.                    const unsigned ** pins,  
  14.                    unsigned * num_pins)  
  15. {  
  16.     *pins = (unsigned *) nuc970_pinctrl_groups[selector].pins;  
  17.     *num_pins = nuc970_pinctrl_groups[selector].num_pins;  
  18.     return 0;  
  19. }  
這裏主要分析上面折幾個函數內部調用的「nuc970_pinctrl_groups」結構體,這裏已「emac0_grp」爲例:
[cpp]  view plain  copy
  1. static const struct nuc970_pinctrl_group nuc970_pinctrl_groups[] = {  
  2.     {  
  3.         .name = "emac0_grp",  
  4.         .pins = emac0_pins,  
  5.         .num_pins = ARRAY_SIZE(emac0_pins),  
  6.         .func = 0x1, //通過查看datasheet 「emac0_pins」中的管腳是複用管腳,0x01表示選擇選擇emac0,如下截圖!  
  7.     },  
  8.     {  
  9.         .name = "emac1_grp",  
  10.         .pins = emac1_pins,  
  11.         .num_pins = ARRAY_SIZE(emac1_pins),  
  12.         .func = 0x1,  
  13.     },  
  14.     //......  
  15. }  

這裏以「emac0_pins」舉例,我們看如下的定義:

[cpp]  view plain  copy
  1. static const unsigned emac0_pins[] = {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59}; // Port F  
emac0_pins數組中內容16進制數表示對應的管腳,通過上面nuc970_pins結構體可以找到對應關係 0x50~0x59對用port F0~F7管腳。

emac0_pins結構體成員「.func=0x1」表示設置該管腳的寄存器值爲1,通過查看下面的截圖中紅框部分得知它們爲emac0的配置!

  

到這裏就分析完了「nuc970_pctrl_ops」結構體,接下來我們繼續回到「struct nuc970_pinctrl_desc」結構體來分析管腳複用成員「struct nuc970_pmxops」:

[cpp]  view plain  copy
  1. struct pinmux_ops nuc970_pmxops = {  
  2.     .get_functions_count = nuc970_get_functions_count,  
  3.     .get_function_name = nuc970_get_fname,  
  4.     .get_function_groups = nuc970_get_groups,  
  5.     .enable = nuc970_enable,  
  6.     .disable = nuc970_disable,  
  7. };  

nuc970_pmxops結構體中成員函數如下:

[cpp]  view plain  copy
  1. int nuc970_get_functions_count(struct pinctrl_dev *pctldev)  
  2. {  
  3.     return ARRAY_SIZE(nuc970_functions);  
  4. }  
  5.   
  6. const char *nuc970_get_fname(struct pinctrl_dev *pctldev, unsigned selector)  
  7. {  
  8.     return nuc970_functions[selector].name;  
  9. }  
  10.   
  11. static int nuc970_get_groups(struct pinctrl_dev *pctldev, unsigned selector,  
  12.               const char * const **groups,  
  13.               unsigned * const num_groups)  
  14. {  
  15.     *groups = nuc970_functions[selector].groups;  
  16.     *num_groups = nuc970_functions[selector].num_groups;  
  17.     return 0;  
  18. }  
  19.   
  20. /* 
  21.  * selector = data.nux.func, which is entry number in nuc970_functions, 
  22.  * and group = data.mux.group, which is entry number in nuc970_pmx_func 
  23.  * group is not used since some function use different setting between 
  24.  * different ports. for example UART.... 
  25.  */  
[cpp]  view plain  copy
  1.   
[cpp]  view plain  copy
  1. int nuc970_enable(struct pinctrl_dev *pctldev, unsigned selector,  
  2.         unsigned group)  
  3. {  
  4.     unsigned int i, j;  
  5.     unsigned int reg, offset;  
  6.   
  7.     //printk("enable =>%x %x\n", selector, group);  
  8.     for(i = 0; i < nuc970_pinctrl_groups[group].num_pins; i++) {  
  9.         j = nuc970_pinctrl_groups[group].pins[i];  
  10.         /* 
  11.             offset值要分兩層意思理解: 
  12.             1. (j >> 4) * 8  
  13.                 表示j/16*8,我們知道管腳PA0~PA15=0x00~0x0f,PB0~PB15=0x10~0x1f,.... 
  14.                 所以,(j >> 4)表示j屬於哪一組管腳,如j=0x08,即是PA,j=0x12,即是PB, 
  15.                 而*8呢?通過下面GPA_L,GPA_H,GPB_L,GPB_H可知,GPAL與GPAH相差0x4,而 
  16.                 GPAL與GPBL相差0x08,所以這裏的(j >> 4) * 8表示的是當前j屬於PA還是PB... 
  17.                  
  18.                 #define REG_MFP_GPA_L   (GCR_BA+0x070)   
  19.                 #define REG_MFP_GPA_H   (GCR_BA+0x074)   
  20.                 #define REG_MFP_GPB_L   (GCR_BA+0x078)   
  21.                 #define REG_MFP_GPB_H   (GCR_BA+0x07C)   
  22.             2. ((j & 0x8) ? 4 : 0) 
  23.                 由1.分析可知(j >> 4) * 8表示屬於哪類管腳(PA\PB\PC...), 而((j & 0x8) ? 4 : 0) 
  24.                 表示的是哪類管腳(PA\PB\PC...)的高低字節,即PA_L\PA_H,PB_L\PB_H, PC_L\PC_H 
  25.  
  26.             所以,通過(j >> 4) * 8 + ((j & 0x8) ? 4 : 0)我們可以計算出當前的j是位於哪個管腳配置 
  27.             寄存器EG_MFP_Px_y的哪種功能! 
  28.         */  
  29.         offset = (j >> 4) * 8 + ((j & 0x8) ? 4 : 0);  
  30.   
  31.         reg = __raw_readl(REG_MFP_GPA_L + offset);  
  32.         reg = (reg & ~(0xF << ((j & 0x7) * 4))) | (nuc970_pinctrl_groups[group].func << ((j & 0x7) * 4));  
  33.   
  34.         __raw_writel(reg, REG_MFP_GPA_L + offset);  
  35.     }  
  36.   
  37.     /* SD0 pin value is 0x6, SD1 PI pin value is 0x4, should set the correct value */  
  38.     if (strcmp(nuc970_pinctrl_groups[group].name, "sd01_0_grp") == 0)  
  39.     {  
  40.         for(i = 8; i < nuc970_pinctrl_groups[group].num_pins; i++) {  
  41.             j = nuc970_pinctrl_groups[group].pins[i];  
  42.             offset = (j >> 4) * 8 + ((j & 0x8) ? 4 : 0);  
  43.   
  44.             reg = __raw_readl(REG_MFP_GPA_L + offset);  
  45.             reg = (reg & ~(0xF << ((j & 0x7) * 4))) | (0x4 << ((j & 0x7) * 4));  
  46.   
  47.             __raw_writel(reg, REG_MFP_GPA_L + offset);  
  48.         }  
  49.     }  
  50.     return 0;  
  51. }  

 
 
[cpp]  view plain  copy
  1. /* 
  2.  * By disable a function, we'll switch it back to GPIO 
  3.  */  
  4. void nuc970_disable(struct pinctrl_dev *pctldev, unsigned selector,  
  5.         unsigned group)  
  6. {  
  7.   
  8.     unsigned int i, j;  
  9.     unsigned int reg, offset;  
  10.   
  11.     //printk("disable =>%x %x\n", selector, group);  
  12.     for(i = 0; i < nuc970_pinctrl_groups[group].num_pins; i++) {  
  13.         j = nuc970_pinctrl_groups[group].pins[i];  
  14.         offset = (j >> 4) * 8 + ((j & 0x8) ? 4 : 0); //獲取寄存器偏移位置  
  15.   
  16.         reg = __raw_readl(REG_MFP_GPA_L + offset);  
  17.         reg &= ~(0xF << ((j & 0x7) * 4));  
  18.         __raw_writel(reg, REG_MFP_GPA_L + offset);  
  19.     }  
  20.   
  21.     return;  
  22. }  
這裏我們主要分析上面函數中「struct nuc970_functions」結構體的作用, 這裏還是以「emac0」爲例:

[cpp]  view plain  copy
  1. static const struct nuc970_pmx_func nuc970_functions[] = {  
  2.     {  
  3.         .name = "emac0",  
  4.         .groups = emac0_groups,  
  5.         .num_groups = ARRAY_SIZE(emac0_groups),  
  6.     },  
  7.     {  
  8.         .name = "emac1",  
  9.         .groups = emac1_groups,  
  10.         .num_groups = ARRAY_SIZE(emac1_groups),  
  11.     },  
  12.     //.......  
  13. }  

[cpp]  view plain  copy
  1. static const char * const emac0_groups[] = {"emac0_grp"};  
可以看到emac0_groups中內容爲「emac0_grp」,這個就是對應我們上面分析到結構體nuc970_pinctrl_groups中的「emac0」,由於我們的「emac0」只有1個,所以emac0_groups中定義「emac0_grp」,例如nandflash有兩路,所以就會定義兩個,如下:

[cpp]  view plain  copy
  1. static const char * const nand_groups[] = {"nand_0_grp""nand_1_grp"};  

到這裏就介紹完了「nuc970_pinctrl_desc」結構體,下面將分析如何註冊該結構體。

3. pinctrl_register()

直接上代碼:

[cpp]  view plain  copy
  1. static int nuc970_pinctrl_probe(struct platform_device *pdev)  
  2. {  
  3.     struct pinctrl_dev *pctl;  
  4.   
  5.     pctl = pinctrl_register(&nuc970_pinctrl_desc, &pdev->dev, NULL);  
  6.     if (IS_ERR(pctl))  
  7.         pr_err("could not register NUC970 pin driver\n");  
  8.   
  9.     platform_set_drvdata(pdev, pctl);  
  10.   
  11.     return pinctrl_register_mappings(nuc970_pinmap, ARRAY_SIZE(nuc970_pinmap));  
  12.   
  13. }  
管腳註冊:
[cpp]  view plain  copy
  1. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,  
  2.                     struct device *dev, void *driver_data)  
  3. {  
  4.     struct pinctrl_dev *pctldev;  
  5.     int ret;  
  6.   
  7.     if (!pctldesc)  
  8.         return NULL;  
  9.     if (!pctldesc->name)  
  10.         return NULL;  
  11.   
  12.     pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL); //分配管腳設備內存  
  13.     if (pctldev == NULL) {  
  14.         dev_err(dev, "failed to alloc struct pinctrl_dev\n");  
  15.         return NULL;  
  16.     }  
  17.   
  18.     /* Initialize pin control device struct */  
  19.     pctldev->owner = pctldesc->owner;  
  20.     pctldev->desc = pctldesc; //將pctrldesc與管腳控制設備綁定  
  21.     pctldev->driver_data = driver_data;  
  22.     INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); //初始化「基數樹」,關於基數數可自行查相關資料  
  23.     INIT_LIST_HEAD(&pctldev->gpio_ranges); //初始化鏈表  
  24.     pctldev->dev = dev;  
  25.     mutex_init(&pctldev->mutex); //初始化互徹鎖  
  26.   
  27.     /* check core ops for sanity */  
  28.     if (pinctrl_check_ops(pctldev)) {  
  29.         dev_err(dev, "pinctrl ops lacks necessary functions\n");  
  30.         goto out_err;  
  31.     }  
  32.   
  33.     /* If we're implementing pinmuxing, check the ops for sanity */  
  34.     if (pctldesc->pmxops) {  
  35.         if (pinmux_check_ops(pctldev))  
  36.             goto out_err;  
  37.     }  
  38.   
  39.     /* If we're implementing pinconfig, check the ops for sanity */  
  40.     if (pctldesc->confops) {  
  41.         if (pinconf_check_ops(pctldev))  
  42.             goto out_err;  
  43.     }  
  44.   
  45.     /* Register all the pins */  
  46.     dev_dbg(dev, "try to register %d pins ...\n",  pctldesc->npins);  
  47.     ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); //註冊管腳,見下面  
  48.     if (ret) {  
  49.         dev_err(dev, "error during pin registration\n");  
  50.         pinctrl_free_pindescs(pctldev, pctldesc->pins,  
  51.                       pctldesc->npins);  
  52.         goto out_err;  
  53.     }  
  54.   
  55.     mutex_lock(&pinctrldev_list_mutex);  
  56.     list_add_tail(&pctldev->node, &pinctrldev_list); //pinctrldev_list爲全局鏈表,這裏將pctrldev設備以節點的形式加入到鏈表pinctrldev_list中  
  57.     mutex_unlock(&pinctrldev_list_mutex);  
  58.   
  59.     pctldev->p = pinctrl_get(pctldev->dev);  
  60.   
  61.     if (!IS_ERR(pctldev->p)) {  
  62.         pctldev->hog_default =  
  63.             pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);  
  64.         if (IS_ERR(pctldev->hog_default)) {  
  65.             dev_dbg(dev, "failed to lookup the default state\n");  
  66.         } else {  
  67.             if (pinctrl_select_state(pctldev->p,  
  68.                         pctldev->hog_default))  
  69.                 dev_err(dev,  
  70.                     "failed to select default state\n");  
  71.         }  
  72.   
  73.         pctldev->hog_sleep =  
  74.             pinctrl_lookup_state(pctldev->p,  
  75.                             PINCTRL_STATE_SLEEP);  
  76.         if (IS_ERR(pctldev->hog_sleep))  
  77.             dev_dbg(dev, "failed to lookup the sleep state\n");  
  78.     }  
  79.   
  80.     pinctrl_init_device_debugfs(pctldev);   
  81.   
  82.     return pctldev;  
  83.   
  84. out_err:  
  85.     mutex_destroy(&pctldev->mutex);  
  86.     kfree(pctldev);  
  87.     return NULL;  
  88. }  

[cpp]  view plain  copy
  1. static int pinctrl_register_pins(struct pinctrl_dev *pctldev,  
  2.                  struct pinctrl_pin_desc const *pins,  
  3.                  unsigned num_descs)  
  4. {  
  5.     unsigned i;  
  6.     int ret = 0;  
  7.   
  8.     for (i = 0; i < num_descs; i++) {  
  9.         ret = pinctrl_register_one_pin(pctldev,  
  10.                            pins[i].number, pins[i].name);  
  11.         if (ret)  
  12.             return ret;  
  13.     }  
  14.   
  15.     return 0;  
  16. }  
[cpp]  view plain  copy
  1. static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,  
  2.                     unsigned number, const char *name)  
  3. {  
  4.     struct pin_desc *pindesc;  
  5.   
  6.     pindesc = pin_desc_get(pctldev, number);    //��δ���忴?????add by cl 20170411 23:41  
  7.     if (pindesc != NULL) {  
  8.         pr_err("pin %d already registered on %s\n", number,  
  9.                pctldev->desc->name);  
  10.         return -EINVAL;  
  11.     }  
  12.   
  13.     pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);  
  14.     if (pindesc == NULL) {  
  15.         dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");  
  16.         return -ENOMEM;  
  17.     }  
  18.   
  19.     /* Set owner */  
  20.     pindesc->pctldev = pctldev;  
  21.   
  22.     /* Copy basic pin info */  
  23.     if (name) {  
  24.         pindesc->name = name;  
  25.     } else {  
  26.         pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);  
  27.             } else {  
  28.         pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);  
  29.         if (pindesc->name == NULL) {  
  30.             kfree(pindesc);  
  31.             return -ENOMEM;  
  32.         }  
  33.         pindesc->dynamic_name = true;  
  34.     }  
  35.   
  36.     radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc); //將管腳值和管腳名稱加入到「基數數」裏面  
  37.     pr_debug("registered pin %d (%s) on %s\n",  
  38.          number, pindesc->name, pctldev->desc->name);  
  39.     return 0;  
  40. }  
 
 

至此就完成了管腳的註冊,接下來分析管腳的映射「pinctrl_register_mappings」。

4. pinctrl_register_mappings()

[cpp] 
相關文章
相關標籤/搜索