linux pinctrl驱动

32 篇文章 8 订阅

https://blog.csdn.net/chenliang0224/article/details/78777995

 

前言:

linux系统下采用pinctrl子系统管理所有的IO管脚,并对设备外围管脚(如串口、I2C、spi、LCD)都有相应的配置模式,本博客以pinctrl子系统细说该驱动架构。

1. pinctrl设备注册、退出

 
  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);

 
  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. };

 
  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结构体定义的具体内容:

 
  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. };

 

管脚描述符控制结构体:

 
  1. struct pinctrl_pin_desc {

  2. unsigned number; //pin管脚值 如:PA01=0x00

  3. const char *name; //pin管脚名字 如:PA01="PA0"

  4. };


管脚操作结构体:

 
  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就是对控制器设备复用选择操作:

 
  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. };

管脚配置操作结构体:

 
  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配置:

 
  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"管脚映射结构体:

 
  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. };

 

 
  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. };

结构体的函数比较简单,如下:

 
  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”为例:

 
  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”举例,我们看如下的定义:

 

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”:

 

 
  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结构体中成员函数如下:

 
  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. */

 
int nuc970_enable(struct pinctrl_dev *pctldev, unsigned selector, unsigned group) { unsigned int i, j; unsigned int reg, offset; //printk("enable =>%x %x\n", selector, group); for(i = 0; i < nuc970_pinctrl_groups[group].num_pins; i++) { j = nuc970_pinctrl_groups[group].pins[i]; /* offset值要分两层意思理解: 1. (j >> 4) * 8 表示j/16*8,我们知道管脚PA0~PA15=0x00~0x0f,PB0~PB15=0x10~0x1f,.... 所以,(j >> 4)表示j属于哪一组管脚,如j=0x08,即是PA,j=0x12,即是PB, 而*8呢?通过下面GPA_L,GPA_H,GPB_L,GPB_H可知,GPAL与GPAH相差0x4,而 GPAL与GPBL相差0x08,所以这里的(j >> 4) * 8表示的是当前j属于PA还是PB... #define REG_MFP_GPA_L (GCR_BA+0x070) #define REG_MFP_GPA_H (GCR_BA+0x074) #define REG_MFP_GPB_L (GCR_BA+0x078) #define REG_MFP_GPB_H (GCR_BA+0x07C) 2. ((j & 0x8) ? 4 : 0) 由1.分析可知(j >> 4) * 8表示属于哪类管脚(PA\PB\PC...), 而((j & 0x8) ? 4 : 0) 表示的是哪类管脚(PA\PB\PC...)的高低字节,即PA_L\PA_H,PB_L\PB_H, PC_L\PC_H 所以,通过(j >> 4) * 8 + ((j & 0x8) ? 4 : 0)我们可以计算出当前的j是位于哪个管脚配置 寄存器EG_MFP_Px_y的哪种功能! */ offset = (j >> 4) * 8 + ((j & 0x8) ? 4 : 0); reg = __raw_readl(REG_MFP_GPA_L + offset); reg = (reg & ~(0xF << ((j & 0x7) * 4))) | (nuc970_pinctrl_groups[group].func << ((j & 0x7) * 4)); __raw_writel(reg, REG_MFP_GPA_L + offset); } /* SD0 pin value is 0x6, SD1 PI pin value is 0x4, should set the correct value */ if (strcmp(nuc970_pinctrl_groups[group].name, "sd01_0_grp") == 0) { for(i = 8; i < nuc970_pinctrl_groups[group].num_pins; i++) { j = nuc970_pinctrl_groups[group].pins[i]; offset = (j >> 4) * 8 + ((j & 0x8) ? 4 : 0); reg = __raw_readl(REG_MFP_GPA_L + offset); reg = (reg & ~(0xF << ((j & 0x7) * 4))) | (0x4 << ((j & 0x7) * 4)); __raw_writel(reg, REG_MFP_GPA_L + offset); } } return 0; }

 

 

 

 
  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”为例:

 

 

 
  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. }

 

 

static const char * const emac0_groups[] = {"emac0_grp"};

可以看到emac0_groups中内容为“emac0_grp”,这个就是对应我们上面分析到结构体nuc970_pinctrl_groups中的“emac0”,由于我们的“emac0”只有1个,所以emac0_groups中定义“emac0_grp”,例如nandflash有两路,所以就会定义两个,如下:

 

static const char * const nand_groups[] = {"nand_0_grp", "nand_1_grp"};

 

到这里就介绍完了“nuc970_pinctrl_desc”结构体,下面将分析如何注册该结构体。

3. pinctrl_register()

直接上代码:

 

 
  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. }

管脚注册:

 
  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. }

 

 

 
  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. }

 
  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. if (pindesc->name == NULL) {

  28. kfree(pindesc);

  29. return -ENOMEM;

  30. }

  31. pindesc->dynamic_name = true;

  32. }

  33.  
  34. radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc); //将管脚值和管脚名称加入到“基数数”里面

  35. pr_debug("registered pin %d (%s) on %s\n",

  36. number, pindesc->name, pctldev->desc->name);

  37. return 0;

  38. }

 

至此就完成了管脚的注册,接下来分析管脚的映射“pinctrl_register_mappings”。

4. pinctrl_register_mappings()

return pinctrl_register_mappings(nuc970_pinmap, ARRAY_SIZE(nuc970_pinmap));

 

这里我们主要分析nuc970_pinmap[]这个结构体,先来看下这个结构体内部都有哪些成员:

 

 
  1. enum pinctrl_map_type {

  2. PIN_MAP_TYPE_INVALID,

  3. PIN_MAP_TYPE_DUMMY_STATE,

  4. PIN_MAP_TYPE_MUX_GROUP,

  5. PIN_MAP_TYPE_CONFIGS_PIN,

  6. PIN_MAP_TYPE_CONFIGS_GROUP,

  7. };

  8.  
  9. /**

  10. * struct pinctrl_map_mux - mapping table content for MAP_TYPE_MUX_GROUP

  11. * @group: the name of the group whose mux function is to be configured. This

  12. * field may be left NULL, and the first applicable group for the function

  13. * will be used.

  14. * @function: the mux function to select for the group

  15. */

  16. struct pinctrl_map_mux {

  17. const char *group;

  18. const char *function;

  19. };

  20.  
  21. /**

  22. * struct pinctrl_map_configs - mapping table content for MAP_TYPE_CONFIGS_*

  23. * @group_or_pin: the name of the pin or group whose configuration parameters

  24. * are to be configured.

  25. * @configs: a pointer to an array of config parameters/values to program into

  26. * hardware. Each individual pin controller defines the format and meaning

  27. * of config parameters.

  28. * @num_configs: the number of entries in array @configs

  29. */

  30. struct pinctrl_map_configs {

  31. const char *group_or_pin;

  32. unsigned long *configs;

  33. unsigned num_configs;

  34. };

  35.  
  36. /**

  37. * struct pinctrl_map - boards/machines shall provide this map for devices

  38. * @dev_name: the name of the device using this specific mapping, the name

  39. * must be the same as in your struct device*. If this name is set to the

  40. * same name as the pin controllers own dev_name(), the map entry will be

  41. * hogged by the driver itself upon registration

  42. * @name: the name of this specific map entry for the particular machine.

  43. * This is the parameter passed to pinmux_lookup_state()

  44. * @type: the type of mapping table entry

  45. * @ctrl_dev_name: the name of the device controlling this specific mapping,

  46. * the name must be the same as in your struct device*. This field is not

  47. * used for PIN_MAP_TYPE_DUMMY_STATE

  48. * @data: Data specific to the mapping type

  49. */

  50. struct pinctrl_map {

  51. const char *dev_name;

  52. const char *name;

  53. enum pinctrl_map_type type;

  54. const char *ctrl_dev_name;

  55. union {

  56. struct pinctrl_map_mux mux;

  57. struct pinctrl_map_configs configs;

  58. } data;

  59. };

  60.  

 

我们在来看下nuc970_pinmap[]这个结构体的成员:

 

 
  1. static const struct pinctrl_map nuc970_pinmap[] = {

  2. {

  3. .dev_name = "nuc970-emac0", //设备驱动名称

  4. .name = PINCTRL_STATE_DEFAULT,

  5. .type = PIN_MAP_TYPE_MUX_GROUP,

  6. .ctrl_dev_name = "pinctrl-nuc970",//控制设备名称

  7. .data.mux.function = "emac0", //管脚复用功能名称,对应上面分析到的nuc970_functions[]结构体

  8. .data.mux.group = "emac0_grp", //管脚复用所属组,对应上面分析到的nuc970_pinctrl_groups[]结构体

  9. },

  10. {

  11. .dev_name = "nuc970-emac1",

  12. .name = PINCTRL_STATE_DEFAULT,

  13. .type = PIN_MAP_TYPE_MUX_GROUP,

  14. .ctrl_dev_name = "pinctrl-nuc970",

  15. .data.mux.function = "emac1",

  16. .data.mux.group = "emac1_grp",

  17. },

  18. {

  19. .dev_name = "nuc970-emac0",

  20. .name = "pps0",

  21. .type = PIN_MAP_TYPE_MUX_GROUP,

  22. .ctrl_dev_name = "pinctrl-nuc970",

  23. .data.mux.function = "pps0",

  24. .data.mux.group = "pps0_grp",

  25. },

  26. {

  27. .dev_name = "nuc970-emac1",

  28. .name = "pps1",

  29. .type = PIN_MAP_TYPE_MUX_GROUP,

  30. .ctrl_dev_name = "pinctrl-nuc970",

  31. .data.mux.function = "pps1",

  32. .data.mux.group = "pps1_grp",

  33. },

  34. //......

  35. }

 

再回到“pinctrl_register_mappings”函数,源码如下:

 
  1. int pinctrl_register_mappings(struct pinctrl_map const *maps,

  2. unsigned num_maps)

  3. {

  4. return pinctrl_register_map(maps, num_maps, true, false);

  5. }

  6. int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,

  7. bool dup, bool locked)

  8. {

  9. int i, ret;

  10. struct pinctrl_maps *maps_node;

  11.  
  12. pr_debug("add %d pinmux maps\n", num_maps);

  13.  
  14. /* First sanity check the new mapping */

  15. for (i = 0; i < num_maps; i++) {

  16. if (!maps[i].dev_name) {

  17. pr_err("failed to register map %s (%d): no device given\n",

  18. maps[i].name, i);

  19. return -EINVAL;

  20. }

  21.  
  22. if (!maps[i].name) {

  23. pr_err("failed to register map %d: no map name given\n",

  24. i);

  25. return -EINVAL;

  26. }

  27.  
  28. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&

  29. !maps[i].ctrl_dev_name) {

  30. pr_err("failed to register map %s (%d): no pin control device given\n",

  31. maps[i].name, i);

  32. return -EINVAL;

  33. }

  34.  
  35. switch (maps[i].type) {

  36. case PIN_MAP_TYPE_DUMMY_STATE:

  37. break;

  38. case PIN_MAP_TYPE_MUX_GROUP:

  39. ret = pinmux_validate_map(&maps[i], i);

  40. if (ret < 0)

  41. return ret;

  42. break;

  43. case PIN_MAP_TYPE_CONFIGS_PIN:

  44. case PIN_MAP_TYPE_CONFIGS_GROUP:

  45. ret = pinconf_validate_map(&maps[i], i);

  46. if (ret < 0)

  47. return ret;

  48. break;

  49. default:

  50. pr_err("failed to register map %s (%d): invalid type given\n",

  51. maps[i].name, i);

  52. return -EINVAL;

  53. }

  54. }

  55.  
  56. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);

  57. if (!maps_node) {

  58. pr_err("failed to alloc struct pinctrl_maps\n");

  59. return -ENOMEM;

  60. }

  61.  
  62. maps_node->num_maps = num_maps;

  63. if (dup) {

  64. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,

  65. GFP_KERNEL);

  66. if (!maps_node->maps) {

  67. pr_err("failed to duplicate mapping table\n");

  68. kfree(maps_node);

  69. return -ENOMEM;

  70. }

  71. } else {

  72. maps_node->maps = maps;

  73. }

  74.  
  75. if (!locked)

  76. mutex_lock(&pinctrl_maps_mutex);

  77. list_add_tail(&maps_node->node, &pinctrl_maps); //将管脚映射加入到链表pinctrl_maps中

  78. if (!locked)

  79. mutex_unlock(&pinctrl_maps_mutex);

  80.  
  81. return 0;

  82. }

到这里就完成了管脚映射加入到内核链表中的操作,下面我们以UART2为例的说明驱动在注册时是如何获取到对应的管脚及管脚的寄存器的配置。

5. devm_pinctrl_get_select()

以下是截取nuc970_pinmap[]、nuc970_pinctrl_groups[]结构体的部分内容:

 

 
  1. static const struct pinctrl_map nuc970_pinmap[] = {

  2. {

  3. .dev_name = "nuc970-uart.2",//驱动名称,由串口驱动程序注册时自动添加串口编号nuc970serial_driver.driver.name="nuc970-uart"

  4. .name = "uart2_fc", //UART串口驱动注册时内部调用 devm_pinctrl_get_select(&pdev->dev, "uart2_fc");

  5. .type = PIN_MAP_TYPE_MUX_GROUP,

  6. .ctrl_dev_name = "pinctrl-nuc970",

  7. .data.mux.function = "uart2",

  8. .data.mux.group = "uart2_0_grp",

  9. },

  10. {

  11. .dev_name = "nuc970-uart.2",

  12. .name = "uart2_fc",

  13. .type = PIN_MAP_TYPE_MUX_GROUP,

  14. .ctrl_dev_name = "pinctrl-nuc970",

  15. .data.mux.function = "uart2_fc",

  16. .data.mux.group = "uart2_1_grp",

  17. },

  18. }

  19. static const unsigned uart2_0_pins[] = {0x5B, 0x5C}; // tx, rx

  20. static const unsigned uart2_1_pins[] = {0x5D, 0x5E}; // rts, cts

  21. static const struct nuc970_pinctrl_group nuc970_pinctrl_groups[] = {

  22. {

  23. .name = "uart2_0_grp",

  24. .pins = uart2_0_pins,

  25. .num_pins = ARRAY_SIZE(uart2_0_pins),

  26. .func = 0x9,

  27. },

  28. {

  29. .name = "uart2_1_grp",

  30. .pins = uart2_1_pins,

  31. .num_pins = ARRAY_SIZE(uart2_1_pins),

  32. .func = 0x9,

  33. },

  34. }

  35. static const char * const uart2_groups[] = {"uart2_0_grp"};

  36. static const char * const uart2_fc_groups[] = {"uart2_1_grp"};

  37. static const struct nuc970_pmx_func nuc970_functions[] = {

  38. {

  39. .name = "uart2",

  40. .groups = uart2_groups,

  41. .num_groups = ARRAY_SIZE(uart2_groups),

  42. },

  43. {

  44. .name = "uart2_fc",

  45. .groups = uart2_fc_groups,

  46. .num_groups = ARRAY_SIZE(uart2_fc_groups),

  47. },

  48. }

 

 

 
  1. static const struct pinctrl_map nuc970_pinmap[] = {

  2. {

  3. .dev_name = "nuc970-uart.2",

  4. .name = "uart2",

  5. .type = PIN_MAP_TYPE_MUX_GROUP,

  6. .ctrl_dev_name = "pinctrl-nuc970",

  7. .data.mux.function = "uart2",

  8. .data.mux.group = "uart2_0_grp",

  9. },

  10. {

  11. .dev_name = "nuc970-uart.2",

  12. .name = "uart2_fc",

  13. .type = PIN_MAP_TYPE_MUX_GROUP,

  14. .ctrl_dev_name = "pinctrl-nuc970",

  15. .data.mux.function = "uart2",

  16. .data.mux.group = "uart2_0_grp",

  17. },

  18. {

  19. .dev_name = "nuc970-uart.2",

  20. .name = "uart2_fc",

  21. .type = PIN_MAP_TYPE_MUX_GROUP,

  22. .ctrl_dev_name = "pinctrl-nuc970",

  23. .data.mux.function = "uart2_fc",

  24. .data.mux.group = "uart2_1_grp",

  25. },

  26. }

 

 

 

 

 

直接上源码:

devm_pinctrl_get_select(&pdev->dev, "uart2_fc");

 

 
  1. static inline struct pinctrl * __must_check devm_pinctrl_get_select(

  2. struct device *dev, const char *name)

  3. {

  4. struct pinctrl *p;

  5. struct pinctrl_state *s;

  6. int ret;

  7.  
  8. p = devm_pinctrl_get(dev); //获取pinctrl,见下面分析

  9. if (IS_ERR(p))

  10. return p;

  11.  
  12. s = pinctrl_lookup_state(p, name);

  13. if (IS_ERR(s)) {

  14. devm_pinctrl_put(p);

  15. return ERR_CAST(s);

  16. }

  17.  
  18. ret = pinctrl_select_state(p, s);

  19. if (ret < 0) {

  20. devm_pinctrl_put(p);

  21. return ERR_PTR(ret);

  22. }

  23.  
  24. return p;

  25. }

 

 

 
  1. struct pinctrl *devm_pinctrl_get(struct device *dev)

  2. {

  3. struct pinctrl **ptr, *p;

  4.  
  5. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);

  6. if (!ptr)

  7. return ERR_PTR(-ENOMEM);

  8.  
  9. p = pinctrl_get(dev); //获取管脚

  10. if (!IS_ERR(p)) {

  11. *ptr = p;

  12. devres_add(dev, ptr);

  13. } else {

  14. devres_free(ptr);

  15. }

  16.  
  17. return p;

  18. }

 

 

 
  1. struct pinctrl *pinctrl_get(struct device *dev)

  2. {

  3. struct pinctrl *p;

  4.  
  5. if (WARN_ON(!dev))

  6. return ERR_PTR(-EINVAL);

  7.  
  8. /*

  9. * See if somebody else (such as the device core) has already

  10. * obtained a handle to the pinctrl for this device. In that case,

  11. * return another pointer to it.

  12. */

  13. //如果已经有其他模块get了,那么pinctrl肯定已经创建好了,直接返回吧,函数内部会遍历pinctrl_list链表,比对该设备(uart)的管脚控制是否已经配置?

  14. p = find_pinctrl(dev);

  15. if (p != NULL) {

  16. dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");

  17. kref_get(&p->users);

  18. return p;

  19. }

  20.  
  21. //否则,创建一个pinctrl用于管理该设备本身的pin信息

  22. return create_pinctrl(dev); //见下面的分析!

  23. }

 

 
  1. static struct pinctrl *find_pinctrl(struct device *dev)

  2. {

  3. struct pinctrl *p;

  4.  
  5. mutex_lock(&pinctrl_list_mutex);

  6. list_for_each_entry(p, &pinctrl_list, node) //确定当前UART-2设备是否已经加入到pinctrl_list链表中,有就返回对应的pinctrl指向pinctrl_list链表指针

  7. if (p->dev == dev) {

  8. mutex_unlock(&pinctrl_list_mutex);

  9. return p;

  10. }

  11.  
  12. mutex_unlock(&pinctrl_list_mutex);

  13. return NULL;

  14. }

create_pinctrl()函数内嵌套太深,这里只给出函数嵌套路径,由于水平有限,仅贴出关键源代码:

 
  1. static struct pinctrl *create_pinctrl(struct device *dev)

  2. {

  3. struct pinctrl *p;

  4. const char *devname;

  5. struct pinctrl_maps *maps_node;

  6. int i;

  7. struct pinctrl_map const *map;

  8. int ret;

  9.  
  10. /*

  11. * create the state cookie holder struct pinctrl for each

  12. * mapping, this is what consumers will get when requesting

  13. * a pin control handle with pinctrl_get()

  14. */

  15. p = kzalloc(sizeof(*p), GFP_KERNEL);

  16. if (p == NULL) {

  17. dev_err(dev, "failed to alloc struct pinctrl\n");

  18. return ERR_PTR(-ENOMEM);

  19. }

  20. p->dev = dev;

  21. INIT_LIST_HEAD(&p->states);

  22. INIT_LIST_HEAD(&p->dt_maps);

  23.  
  24. ret = pinctrl_dt_to_map(p); //很重要,见下面的分析

  25. if (ret < 0) {

  26. kfree(p);

  27. return ERR_PTR(ret);

  28. }

  29.  
  30. devname = dev_name(dev);

  31.  
  32. mutex_lock(&pinctrl_maps_mutex);

  33. /* Iterate over the pin control maps to locate the right ones */

  34. for_each_maps(maps_node, i, map) {

  35. /* Map must be for this device */

  36. if (strcmp(map->dev_name, devname))

  37. continue;

  38.  
  39. ret = add_setting(p, map); //很重要,见下面分析

  40. /*

  41. * At this point the adding of a setting may:

  42. *

  43. * - Defer, if the pinctrl device is not yet available

  44. * - Fail, if the pinctrl device is not yet available,

  45. * AND the setting is a hog. We cannot defer that, since

  46. * the hog will kick in immediately after the device

  47. * is registered.

  48. *

  49. * If the error returned was not -EPROBE_DEFER then we

  50. * accumulate the errors to see if we end up with

  51. * an -EPROBE_DEFER later, as that is the worst case.

  52. */

  53. if (ret == -EPROBE_DEFER) {

  54. pinctrl_free(p, false);

  55. mutex_unlock(&pinctrl_maps_mutex);

  56. return ERR_PTR(ret);

  57. }

  58. }

  59. mutex_unlock(&pinctrl_maps_mutex);

  60.  
  61. if (ret < 0) {

  62. /* If some other error than deferral occured, return here */

  63. pinctrl_free(p, false);

  64. return ERR_PTR(ret);

  65. }

  66.  
  67. kref_init(&p->users);

  68.  
  69. /* Add the pinctrl handle to the global list */

  70. mutex_lock(&pinctrl_list_mutex);

  71. list_add_tail(&p->node, &pinctrl_list);

  72. mutex_unlock(&pinctrl_list_mutex);

  73.  
  74. return p;

  75. }

 

 
  1. //管脚到设备树的映射

  2. int pinctrl_dt_to_map(struct pinctrl *p)

  3. {

  4. struct device_node *np = p->dev->of_node; //关联的设备树节点,这里应该是pinctrl-nuc970注册时绑定的???

  5. int state, ret;

  6. char *propname;

  7. struct property *prop;

  8. const char *statename;

  9. const __be32 *list;

  10. int size, config;

  11. phandle phandle;

  12. struct device_node *np_config;

  13.  
  14. /* CONFIG_OF enabled, p->dev not instantiated from DT */

  15. if (!np) {

  16. dev_dbg(p->dev, "no of_node; not parsing pinctrl DT\n");

  17. return 0;

  18. }

  19.  
  20. /* We may store pointers to property names within the node */

  21. of_node_get(np);

  22.  
  23. /* For each defined state ID */

  24. for (state = 0; ; state++) {

  25. /* Retrieve the pinctrl-* property */

  26. propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);

  27. prop = of_find_property(np, propname, &size); //匹配pinctrl-x名称

  28. kfree(propname);

  29. if (!prop)

  30. break;

  31. list = prop->value;

  32. size /= sizeof(*list);

  33.  
  34. /* Determine whether pinctrl-names property names the state */

  35. ret = of_property_read_string_index(np, "pinctrl-names",

  36. state, &statename);

  37. /*

  38. * If not, statename is just the integer state ID. But rather

  39. * than dynamically allocate it and have to free it later,

  40. * just point part way into the property name for the string.

  41. */

  42. if (ret < 0) {

  43. /* strlen("pinctrl-") == 8 */

  44. statename = prop->name + 8;

  45. }

  46.  
  47. /* For every referenced pin configuration node in it */

  48. for (config = 0; config < size; config++) {

  49. phandle = be32_to_cpup(list++);

  50.  
  51. /* Look up the pin configuration node */

  52. np_config = of_find_node_by_phandle(phandle);

  53. if (!np_config) {

  54. dev_err(p->dev,

  55. "prop %s index %i invalid phandle\n",

  56. prop->name, config);

  57. ret = -EINVAL;

  58. goto err;

  59. }

  60.  
  61. /* Parse the node */

  62. ret = dt_to_map_one_config(p, statename, np_config); //很重要!!!

  63. of_node_put(np_config);

  64. if (ret < 0)

  65. goto err;

  66. }

  67.  
  68. /* No entries in DT? Generate a dummy state table entry */

  69. if (!size) {

  70. ret = dt_remember_dummy_state(p, statename);

  71. if (ret < 0)

  72. goto err;

  73. }

  74. }

  75.  
  76. return 0;

  77.  
  78. err:

  79. pinctrl_dt_free_maps(p);

  80. return ret;

  81. }

 

 
  1. static int dt_to_map_one_config(struct pinctrl *p, const char *statename,

  2. struct device_node *np_config)

  3. {

  4. struct device_node *np_pctldev;

  5. struct pinctrl_dev *pctldev;

  6. const struct pinctrl_ops *ops;

  7. int ret;

  8. struct pinctrl_map *map;

  9. unsigned num_maps;

  10.  
  11. /* Find the pin controller containing np_config */

  12. np_pctldev = of_node_get(np_config);

  13. for (;;) {

  14. np_pctldev = of_get_next_parent(np_pctldev);

  15. if (!np_pctldev || of_node_is_root(np_pctldev)) {

  16. dev_info(p->dev, "could not find pctldev for node %s, deferring probe\n",

  17. np_config->full_name);

  18. of_node_put(np_pctldev);

  19. /* OK let's just assume this will appear later then */

  20. return -EPROBE_DEFER;

  21. }

  22. //这个函数很重要,内部会遍历pinctrldev_list全局链表,而这个链表就是我们在pinctrl-nuc970.c设备驱动里注册的管脚!!!

  23. pctldev = get_pinctrl_dev_from_of_node(np_pctldev);

  24. if (pctldev)

  25. break;

  26. /* Do not defer probing of hogs (circular loop) */

  27. if (np_pctldev == p->dev->of_node) {

  28. of_node_put(np_pctldev);

  29. return -ENODEV;

  30. }

  31. }

  32. of_node_put(np_pctldev);

  33.  
  34. /*

  35. * Call pinctrl driver to parse device tree node, and

  36. * generate mapping table entries

  37. */

  38. ops = pctldev->desc->pctlops;

  39. if (!ops->dt_node_to_map) {

  40. dev_err(p->dev, "pctldev %s doesn't support DT\n",

  41. dev_name(pctldev->dev));

  42. return -ENODEV;

  43. }

  44. ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);//返回map映射个数汗操作函数,这里应该是对应nuc970_pinmap[]

  45. if (ret < 0)

  46. return ret;

  47.  
  48. /* Stash the mapping table chunk away for later use */

  49. return dt_remember_or_free_map(p, statename, pctldev, map, num_maps); //很重要,见下面分析

  50. }

 

 
  1. static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,

  2. struct pinctrl_dev *pctldev,

  3. struct pinctrl_map *map, unsigned num_maps)

  4. {

  5. int i;

  6. struct pinctrl_dt_map *dt_map;

  7.  
  8. /* Initialize common mapping table entry fields */

  9. for (i = 0; i < num_maps; i++) {

  10. map[i].dev_name = dev_name(p->dev);

  11. map[i].name = statename;

  12. if (pctldev)

  13. map[i].ctrl_dev_name = dev_name(pctldev->dev);

  14. }

  15.  
  16. /* Remember the converted mapping table entries */

  17. dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);

  18. if (!dt_map) {

  19. dev_err(p->dev, "failed to alloc struct pinctrl_dt_map\n");

  20. dt_free_map(pctldev, map, num_maps);

  21. return -ENOMEM;

  22. }

  23.  
  24. dt_map->pctldev = pctldev;

  25. dt_map->map = map;

  26. dt_map->num_maps = num_maps;

  27. list_add_tail(&dt_map->node, &p->dt_maps);

  28.  
  29. return pinctrl_register_map(map, num_maps, false, true); //很重要,见下面

  30. }

 

这里要理解一点,maps[i]就是指向nuc970_pinmap[]结构体:

 

 
  1. int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,

  2. bool dup, bool locked)

  3. {

  4. int i, ret;

  5. struct pinctrl_maps *maps_node;

  6.  
  7. pr_debug("add %d pinmux maps\n", num_maps);

  8.  
  9. /* First sanity check the new mapping */

  10. for (i = 0; i < num_maps; i++) { //遍历nuc970_pinmap[]成员个数

  11. if (!maps[i].dev_name) {

  12. pr_err("failed to register map %s (%d): no device given\n",

  13. maps[i].name, i);

  14. return -EINVAL;

  15. }

  16.  
  17. if (!maps[i].name) {

  18. pr_err("failed to register map %d: no map name given\n",

  19. i);

  20. return -EINVAL;

  21. }

  22.  
  23. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&

  24. !maps[i].ctrl_dev_name) {

  25. pr_err("failed to register map %s (%d): no pin control device given\n",

  26. maps[i].name, i);

  27. return -EINVAL;

  28. }

  29.  
  30. switch (maps[i].type) { //管脚类型

  31. case PIN_MAP_TYPE_DUMMY_STATE:

  32. break;

  33. case PIN_MAP_TYPE_MUX_GROUP: //复用管脚

  34. ret = pinmux_validate_map(&maps[i], i); //判读复用功能是否有效

  35. if (ret < 0)

  36. return ret;

  37. break;

  38. case PIN_MAP_TYPE_CONFIGS_PIN:

  39. case PIN_MAP_TYPE_CONFIGS_GROUP:

  40. ret = pinconf_validate_map(&maps[i], i); //判读配置功能是否有效

  41. if (ret < 0)

  42. return ret;

  43. break;

  44. default:

  45. pr_err("failed to register map %s (%d): invalid type given\n",

  46. maps[i].name, i);

  47. return -EINVAL;

  48. }

  49. }

  50.  
  51. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);

  52. if (!maps_node) {

  53. pr_err("failed to alloc struct pinctrl_maps\n");

  54. return -ENOMEM;

  55. }

  56.  
  57. maps_node->num_maps = num_maps;

  58. if (dup) {

  59. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,

  60. GFP_KERNEL);

  61. if (!maps_node->maps) {

  62. pr_err("failed to duplicate mapping table\n");

  63. kfree(maps_node);

  64. return -ENOMEM;

  65. }

  66. } else {

  67. maps_node->maps = maps;

  68. }

  69.  
  70. if (!locked)

  71. mutex_lock(&pinctrl_maps_mutex);

  72. list_add_tail(&maps_node->node, &pinctrl_maps); //加入到全局链表中pinctrl_maps中,这里很重要,下面会用到!又见链表!!!

  73. if (!locked)

  74. mutex_unlock(&pinctrl_maps_mutex);

  75.  
  76. return 0;

  77. }


到现在我们在回到create_pinctrl(...)函数:

 

 

 
  1. static struct pinctrl *create_pinctrl(struct device *dev)

  2. {

  3. struct pinctrl *p;

  4. const char *devname;

  5. struct pinctrl_maps *maps_node;

  6. int i;

  7. struct pinctrl_map const *map;

  8. int ret;

  9.  
  10. /*

  11. * create the state cookie holder struct pinctrl for each

  12. * mapping, this is what consumers will get when requesting

  13. * a pin control handle with pinctrl_get()

  14. */

  15. p = kzalloc(sizeof(*p), GFP_KERNEL);

  16. if (p == NULL) {

  17. dev_err(dev, "failed to alloc struct pinctrl\n");

  18. return ERR_PTR(-ENOMEM);

  19. }

  20. p->dev = dev;

  21. INIT_LIST_HEAD(&p->states);

  22. INIT_LIST_HEAD(&p->dt_maps);

  23.  
  24. ret = pinctrl_dt_to_map(p); //从设备树device tree设备找到对应的映射,上面已简单分析,水太深,我也还没有完全理解!!!!

  25. if (ret < 0) {

  26. kfree(p);

  27. return ERR_PTR(ret);

  28. }

  29.  
  30. devname = dev_name(dev);

  31.  
  32. mutex_lock(&pinctrl_maps_mutex);

  33. /* Iterate over the pin control maps to locate the right ones */

  34. //for_each_maps(...)遍历的是上面分析到的pinctrl_maps链表,所以下面的操作就明白了,就是

  35. //查找与链表中相同的设备名字,而devname="nuc970-uart.2",在结构体nuc970_pinmap[]中有三个

  36. //"nuc970-uart.2"相同的成员,这里是如何选择一个还没有找到原因?

  37. for_each_maps(maps_node, i, map) {

  38. /* Map must be for this device */

  39. if (strcmp(map->dev_name, devname))

  40. continue;

  41.  
  42. ret = add_setting(p, map); //很重要,管脚功能选择、配置就是通过该函数,见下面!!

  43. /*

  44. * At this point the adding of a setting may:

  45. *

  46. * - Defer, if the pinctrl device is not yet available

  47. * - Fail, if the pinctrl device is not yet available,

  48. * AND the setting is a hog. We cannot defer that, since

  49. * the hog will kick in immediately after the device

  50. * is registered.

  51. *

  52. * If the error returned was not -EPROBE_DEFER then we

  53. * accumulate the errors to see if we end up with

  54. * an -EPROBE_DEFER later, as that is the worst case.

  55. */

  56. if (ret == -EPROBE_DEFER) {

  57. pinctrl_free(p, false);

  58. mutex_unlock(&pinctrl_maps_mutex);

  59. return ERR_PTR(ret);

  60. }

  61. }

  62. mutex_unlock(&pinctrl_maps_mutex);

  63.  
  64. if (ret < 0) {

  65. /* If some other error than deferral occured, return here */

  66. pinctrl_free(p, false);

  67. return ERR_PTR(ret);

  68. }

  69.  
  70. kref_init(&p->users);

  71.  
  72. /* Add the pinctrl handle to the global list */

  73. mutex_lock(&pinctrl_list_mutex);

  74. list_add_tail(&p->node, &pinctrl_list);

  75. mutex_unlock(&pinctrl_list_mutex);

  76.  
  77. return p;

  78. }

 

 
  1. static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)

  2. {

  3. struct pinctrl_state *state;

  4. struct pinctrl_setting *setting;

  5. int ret;

  6.  
  7. //确定串口名字是否有相同, map->name="uart2"或“uart2-fc”

  8. state = find_state(p, map->name);

  9. if (!state)

  10. state = create_state(p, map->name);

  11. if (IS_ERR(state))

  12. return PTR_ERR(state);

  13.  
  14. if (map->type == PIN_MAP_TYPE_DUMMY_STATE)

  15. return 0;

  16.  
  17. setting = kzalloc(sizeof(*setting), GFP_KERNEL);

  18. if (setting == NULL) {

  19. dev_err(p->dev,

  20. "failed to alloc struct pinctrl_setting\n");

  21. return -ENOMEM;

  22. }

  23.  
  24. setting->type = map->type;

  25.  
  26. //函数会遍历全局链表pinctrldev_list,而pinctrl-nuc970.c管脚是加入到了该链表中。

  27. setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);

  28. if (setting->pctldev == NULL) {

  29. kfree(setting);

  30. /* Do not defer probing of hogs (circular loop) */

  31. if (!strcmp(map->ctrl_dev_name, map->dev_name))

  32. return -ENODEV;

  33. /*

  34. * OK let us guess that the driver is not there yet, and

  35. * let's defer obtaining this pinctrl handle to later...

  36. */

  37. dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",

  38. map->ctrl_dev_name);

  39. return -EPROBE_DEFER;

  40. }

  41.  
  42. setting->dev_name = map->dev_name;

  43.  
  44. switch (map->type) {

  45. case PIN_MAP_TYPE_MUX_GROUP:

  46. ret = pinmux_map_to_setting(map, setting); //复用管脚设置,内部有详解!

  47. break;

  48. case PIN_MAP_TYPE_CONFIGS_PIN:

  49. case PIN_MAP_TYPE_CONFIGS_GROUP:

  50. ret = pinconf_map_to_setting(map, setting); //管脚配置

  51. break;

  52. default:

  53. ret = -EINVAL;

  54. break;

  55. }

  56. if (ret < 0) {

  57. kfree(setting);

  58. return ret;

  59. }

  60.  
  61. list_add_tail(&setting->node, &state->settings); //又见链表!!!

  62.  
  63. return 0;

  64. }

 

 
  1. int pinmux_map_to_setting(struct pinctrl_map const *map,

  2. struct pinctrl_setting *setting)

  3. {

  4. struct pinctrl_dev *pctldev = setting->pctldev;

  5. const struct pinmux_ops *pmxops = pctldev->desc->pmxops; //获取nuc970_pmxops 管脚复用操作结构体

  6. char const * const *groups;

  7. unsigned num_groups;

  8. int ret;

  9. const char *group;

  10. int i;

  11.  
  12. if (!pmxops) {

  13. dev_err(pctldev->dev, "does not support mux function\n");

  14. return -EINVAL;

  15. }

  16.  
  17. //1. 在nuc970_functions[]中查找与“uart2_fc”匹配的成员,同时返回该成员在nuc970_functions[]的偏移位置!!!

  18. ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);

  19. if (ret < 0) {

  20. dev_err(pctldev->dev, "invalid function %s in map table\n",

  21. map->data.mux.function);

  22. return ret;

  23. }

  24. setting->data.mux.func = ret;

  25.  
  26. //2. 由1.中返回的索引,确定“uart2_fc”有多少组复用的管脚配置,这里uart2_fc_groups[] = {"uart2_1_grp"}

  27. // 即只有一组,同时返回uart2_fc_groups和个数

  28. ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,

  29. &groups, &num_groups);

  30. if (ret < 0) {

  31. dev_err(pctldev->dev, "can't query groups for function %s\n",

  32. map->data.mux.function);

  33. return ret;

  34. }

  35. if (!num_groups) {

  36. dev_err(pctldev->dev,

  37. "function %s can't be selected on any group\n",

  38. map->data.mux.function);

  39. return -EINVAL;

  40. }

  41. if (map->data.mux.group) {

  42. bool found = false;

  43. group = map->data.mux.group; //????????????怎么来的?!

  44. for (i = 0; i < num_groups; i++) {

  45. if (!strcmp(group, groups[i])) { //确定组名“uart2_1_grp”是否匹配!!!

  46. found = true;

  47. break;

  48. }

  49. }

  50. if (!found) {

  51. dev_err(pctldev->dev,

  52. "invalid group \"%s\" for function \"%s\"\n",

  53. group, map->data.mux.function);

  54. return -EINVAL;

  55. }

  56. } else {

  57. group = groups[0];

  58. }

  59.  
  60. //3. 根据选中的组名“uart2_1_grp”,确定“uart2_1_grp”对应的管家选择,这里返回“uart2_1_grp”在

  61. // nuc970_pinctrl_groups[]中的偏移!!!

  62. ret = pinctrl_get_group_selector(pctldev, group);

  63. if (ret < 0) {

  64. dev_err(pctldev->dev, "invalid group %s in map table\n",

  65. map->data.mux.group);

  66. return ret;

  67. }

  68. setting->data.mux.group = ret;

  69.  
  70. return 0;

  71. }

 

 
  1. int pinconf_map_to_setting(struct pinctrl_map const *map,

  2. struct pinctrl_setting *setting)

  3. {

  4. struct pinctrl_dev *pctldev = setting->pctldev;

  5. int pin;

  6.  
  7. switch (setting->type) {

  8. case PIN_MAP_TYPE_CONFIGS_PIN:

  9. pin = pin_get_from_name(pctldev,

  10. map->data.configs.group_or_pin);

  11. if (pin < 0) {

  12. dev_err(pctldev->dev, "could not map pin config for \"%s\"",

  13. map->data.configs.group_or_pin);

  14. return pin;

  15. }

  16. setting->data.configs.group_or_pin = pin;

  17. break;

  18. case PIN_MAP_TYPE_CONFIGS_GROUP:

  19. pin = pinctrl_get_group_selector(pctldev,

  20. map->data.configs.group_or_pin);

  21. if (pin < 0) {

  22. dev_err(pctldev->dev, "could not map group config for \"%s\"",

  23. map->data.configs.group_or_pin);

  24. return pin;

  25. }

  26. setting->data.configs.group_or_pin = pin;

  27. break;

  28. default:

  29. return -EINVAL;

  30. }

  31.  
  32. setting->data.configs.num_configs = map->data.configs.num_configs;

  33. setting->data.configs.configs = map->data.configs.configs;

  34.  
  35. return 0;

  36. }


再次回到devm_pinctrl_get_select(...)函数,

 

 

 
  1. static inline struct pinctrl * __must_check devm_pinctrl_get_select(

  2. struct device *dev, const char *name)

  3. {

  4. struct pinctrl *p;

  5. struct pinctrl_state *s;

  6. int ret;

  7.  
  8. p = devm_pinctrl_get(dev); //获取pinctrl

  9. if (IS_ERR(p))

  10. return p;

  11.  
  12. //查找管脚的状态setting,已在devm_pinctrl_get(...)函数内部有提到

  13. s = pinctrl_lookup_state(p, name);

  14. if (IS_ERR(s)) {

  15. devm_pinctrl_put(p);

  16. return ERR_CAST(s);

  17. }

  18.  
  19. //根据管脚控制,选择一种状态,即使能“uart-fs”串口的配置

  20. ret = pinctrl_select_state(p, s);

  21. if (ret < 0) {

  22. devm_pinctrl_put(p);

  23. return ERR_PTR(ret);

  24. }

  25.  
  26. return p;

  27. }

 

 
  1. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)

  2. {

  3. struct pinctrl_setting *setting, *setting2;

  4. struct pinctrl_state *old_state = p->state;

  5. int ret;

  6.  
  7. if (p->state == state)

  8. return 0;

  9.  
  10. if (p->state) {

  11. /*

  12. * The set of groups with a mux configuration in the old state

  13. * may not be identical to the set of groups with a mux setting

  14. * in the new state. While this might be unusual, it's entirely

  15. * possible for the "user"-supplied mapping table to be written

  16. * that way. For each group that was configured in the old state

  17. * but not in the new state, this code puts that group into a

  18. * safe/disabled state.

  19. */

  20. list_for_each_entry(setting, &p->state->settings, node) {

  21. bool found = false;

  22. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)

  23. continue;

  24. list_for_each_entry(setting2, &state->settings, node) {

  25. if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)

  26. continue;

  27. if (setting2->data.mux.group ==

  28. setting->data.mux.group) {

  29. found = true;

  30. break;

  31. }

  32. }

  33. if (!found)

  34. pinmux_disable_setting(setting);

  35. }

  36. }

  37.  
  38. p->state = NULL;

  39.  
  40. /* Apply all the settings for the new state */

  41. //到这里,终于看到了对pin管脚的操作函数,真的不容易!!!!

  42. list_for_each_entry(setting, &state->settings, node) {

  43. switch (setting->type) {

  44. case PIN_MAP_TYPE_MUX_GROUP:

  45. ret = pinmux_enable_setting(setting); //见下面

  46. break;

  47. case PIN_MAP_TYPE_CONFIGS_PIN:

  48. case PIN_MAP_TYPE_CONFIGS_GROUP:

  49. ret = pinconf_apply_setting(setting); //见下面

  50. break;

  51. default:

  52. ret = -EINVAL;

  53. break;

  54. }

  55.  
  56. if (ret < 0) {

  57. goto unapply_new_state;

  58. }

  59. }

  60.  
  61. p->state = state;

  62.  
  63. return 0;

  64.  
  65. unapply_new_state:

  66. dev_err(p->dev, "Error applying setting, reverse things back\n");

  67.  
  68. list_for_each_entry(setting2, &state->settings, node) {

  69. if (&setting2->node == &setting->node)

  70. break;

  71. /*

  72. * All we can do here is pinmux_disable_setting.

  73. * That means that some pins are muxed differently now

  74. * than they were before applying the setting (We can't

  75. * "unmux a pin"!), but it's not a big deal since the pins

  76. * are free to be muxed by another apply_setting.

  77. */

  78. if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)

  79. pinmux_disable_setting(setting2);

  80. }

  81.  
  82. /* There's no infinite recursive loop here because p->state is NULL */

  83. if (old_state)

  84. pinctrl_select_state(p, old_state);

  85.  
  86. return ret;

  87. }

 

 
  1. int pinmux_enable_setting(struct pinctrl_setting const *setting)

  2. {

  3. struct pinctrl_dev *pctldev = setting->pctldev;

  4. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;

  5. const struct pinmux_ops *ops = pctldev->desc->pmxops;

  6. int ret;

  7. const unsigned *pins;

  8. unsigned num_pins;

  9. int i;

  10. struct pin_desc *desc;

  11.  
  12. //获取管脚的数量和数组,即“uart-fs”的配置管脚

  13. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,

  14. &pins, &num_pins);

  15. if (ret) {

  16. /* errors only affect debug data, so just warn */

  17. dev_warn(pctldev->dev,

  18. "could not get pins for group selector %d\n",

  19. setting->data.mux.group);

  20. num_pins = 0;

  21. }

  22.  
  23. //申请管脚使用,即“uart-fs”的配置管脚

  24. /* Try to allocate all pins in this group, one by one */

  25. for (i = 0; i < num_pins; i++) {

  26. ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);

  27. if (ret) {

  28. dev_err(pctldev->dev,

  29. "could not request pin %d on device %s\n",

  30. pins[i], pinctrl_dev_get_name(pctldev));

  31. goto err_pin_request;

  32. }

  33. }

  34.  
  35. /* Now that we have acquired the pins, encode the mux setting */

  36. for (i = 0; i < num_pins; i++) {

  37. desc = pin_desc_get(pctldev, pins[i]);

  38. if (desc == NULL) {

  39. dev_warn(pctldev->dev,

  40. "could not get pin desc for pin %d\n",

  41. pins[i]);

  42. continue;

  43. }

  44. desc->mux_setting = &(setting->data.mux);

  45. }

  46.  
  47. //使能管脚,到这里就是我们最终要找的根源,到这里也就完成了管脚的选择、配置!!!

  48. ret = ops->enable(pctldev, setting->data.mux.func,

  49. setting->data.mux.group);

  50.  
  51. if (ret)

  52. goto err_enable;

  53.  
  54. return 0;

  55.  
  56. err_enable:

  57. for (i = 0; i < num_pins; i++) {

  58. desc = pin_desc_get(pctldev, pins[i]);

  59. if (desc)

  60. desc->mux_setting = NULL;

  61. }

  62. err_pin_request:

  63. /* On error release all taken pins */

  64. while (--i >= 0)

  65. pin_free(pctldev, pins[i], NULL);

  66.  
  67. return ret;

  68. }

 

 

 

 
  1. int pinconf_apply_setting(struct pinctrl_setting const *setting)

  2. {

  3. struct pinctrl_dev *pctldev = setting->pctldev;

  4. const struct pinconf_ops *ops = pctldev->desc->confops;

  5. int i, ret;

  6.  
  7. if (!ops) {

  8. dev_err(pctldev->dev, "missing confops\n");

  9. return -EINVAL;

  10. }

  11.  
  12. switch (setting->type) {

  13. case PIN_MAP_TYPE_CONFIGS_PIN:

  14. if (!ops->pin_config_set) {

  15. dev_err(pctldev->dev, "missing pin_config_set op\n");

  16. return -EINVAL;

  17. }

  18. for (i = 0; i < setting->data.configs.num_configs; i++) {

  19. ret = ops->pin_config_set(pctldev,

  20. setting->data.configs.group_or_pin,

  21. setting->data.configs.configs[i]);

  22. if (ret < 0) {

  23. dev_err(pctldev->dev,

  24. "pin_config_set op failed for pin %d config %08lx\n",

  25. setting->data.configs.group_or_pin,

  26. setting->data.configs.configs[i]);

  27. return ret;

  28. }

  29. }

  30. break;

  31. case PIN_MAP_TYPE_CONFIGS_GROUP:

  32. if (!ops->pin_config_group_set) {

  33. dev_err(pctldev->dev,

  34. "missing pin_config_group_set op\n");

  35. return -EINVAL;

  36. }

  37. for (i = 0; i < setting->data.configs.num_configs; i++) {

  38. ret = ops->pin_config_group_set(pctldev,

  39. setting->data.configs.group_or_pin,

  40. setting->data.configs.configs[i]);

  41. if (ret < 0) {

  42. dev_err(pctldev->dev,

  43. "pin_config_group_set op failed for group %d config %08lx\n",

  44. setting->data.configs.group_or_pin,

  45. setting->data.configs.configs[i]);

  46. return ret;

  47. }

  48. }

  49. break;

  50. default:

  51. return -EINVAL;

  52. }

  53.  
  54. return 0;

  55. }

 

 

ret = ops->enable(pctldev, setting->data.mux.func, setting->data.mux.group);这里调用的是结构体nuc970_pmxops中的成员nuc970_enable函数,至此上面讲了一大推的铺垫也就是为了找到这个管脚配置的源头!!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值