1.0 struct gpio_chip
struct gpio_chip代表一个gpio控制器。用一个全局数组保存所有的gpio控制器。
90 struct gpio_chip {
91 >---const char>->---*label;
92 >---struct device>-->---*dev;
93 >---struct device>-->---*cdev;
94 >---struct module>-->---*owner;
95 >---struct list_head list;
96
97 >---int>>--->---(*request)(struct gpio_chip *chip,
98 >--->--->--->--->--->---unsigned offset);//请求gpio
99 >---void>--->--->---(*free)(struct gpio_chip *chip,
100 >--->--->--->--->--->---unsigned offset);//释放
101 >---int>>--->---(*get_direction)(struct gpio_chip *chip,
102 >--->--->--->--->--->---unsigned offset);
103 >---int>>--->---(*direction_input)(struct gpio_chip *chip,
104 >--->--->--->--->--->---unsigned offset);//配置gpio为输入
105 >---int>>--->---(*direction_output)(struct gpio_chip *chip,
106 >--->--->--->--->--->---unsigned offset, int value);//配置gpio为输出,设置为value
107 >---int>>--->---(*get)(struct gpio_chip *chip,
108 >--->--->--->--->--->---unsigned offset);
109 >---void>--->--->---(*set)(struct gpio_chip *chip,
110 >--->--->--->--->--->---unsigned offset, int value);//设置gpio为value
111 >---void>--->--->---(*set_multiple)(struct gpio_chip *chip,
112 >--->--->--->--->--->---unsigned long *mask,
113 >--->--->--->--->--->---unsigned long *bits);
114 >---int>>--->---(*set_debounce)(struct gpio_chip *chip,
115 >--->--->--->--->--->---unsigned offset,
116 >--->--->--->--->--->---unsigned debounce);
117
118 >---int>>--->---(*to_irq)(struct gpio_chip *chip,
119 >--->--->--->--->--->---unsigned offset);//gpio转为中断号
120
121 >---void>--->--->---(*dbg_show)(struct seq_file *s,
122 >--->--->--->--->--->---struct gpio_chip *chip);
123 >---int>>--->---base;//这个gpio控制器的gpio开始编号
124 >---u16>>--->---ngpio;//控制的gpio数
125 >---struct gpio_desc>---*desc;//gpio descriptor数组
126 >---const char>->---*const *names;
127 >---bool>--->--->---can_sleep;
128 >---bool>--->--->---irq_not_threaded;
...
}
1.1 struct gpio_desc
//表示一个gpio口,含对应的gpio_chip.
//对于每一个gpio,都有一个gpio描述符,这个描述符包含了这个gpio所属的控制器即chip和一些标志,label等。
86 struct gpio_desc {
87 >---struct gpio_chip>---*chip;//所属gpio控制器
88 >---unsigned long>-->---flags;
89 /* flag symbols are bit numbers */
90 #define FLAG_REQUESTED>-0
91 #define FLAG_IS_OUT>1
92 #define FLAG_EXPORT>2>--/* protected by sysfs_lock */
93 #define FLAG_SYSFS>-3>--/* exported via /sys/class/gpio/control */
94 #define FLAG_ACTIVE_LOW>6>--/* value has active low */
95 #define FLAG_OPEN_DRAIN>7>--/* Gpio is open drain type */
96 #define FLAG_OPEN_SOURCE 8>-/* Gpio is open source type */
97 #define FLAG_USED_AS_IRQ 9>-/* GPIO is connected to an IRQ */
98 #define FLAG_IS_HOGGED>-11>-/* GPIO is hogged */
99
100 >---/* Connection label */
101 >---const char>->---*label;
102 >---/* Name of the GPIO */
103 >---const char>->---*name;
104 };
由gpio号获得gpio_desc
68 struct gpio_desc *gpio_to_desc(unsigned gpio)
69 {
70 >---struct gpio_chip *chip;
71 >---unsigned long flags;
72
73 >---spin_lock_irqsave(&gpio_lock, flags);
74
75 >---list_for_each_entry(chip, &gpio_chips, list) {
76 >--->---if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
77 >--->--->---spin_unlock_irqrestore(&gpio_lock, flags);
78 >--->--->---return &chip->desc[gpio - chip->base];
79 >--->---}
80 >---}
81
82 >---spin_unlock_irqrestore(&gpio_lock, flags);
83
84 >---if (!gpio_is_valid(gpio))
85 >--->---WARN(1, "invalid GPIO %d\n", gpio);
86
87 >---return NULL;
88 }
int of_get_named_gpio(struct device_node *np, const char *propname, int index)
of_get_named_gpio() - Get a GPIO number to use with GPIO API
@np:>>---device node to get GPIO from@propname:>--Name of property containing gpio specifier(s)
@index:>-index of the GPIO
bool gpio_is_valid(int number)//检查gpio号
int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
devm_gpio_request - request a GPIO for a managed device
@dev: device to request the GPIO for
@gpio: GPIO to allocate
@label: the name of the requested GPIO
//gpio配置为输出,并初始化为value
int gpio_direction_output(unsigned gpio, int value)
void gpio_set_value(unsigned gpio, int value)//gpio配置为输出时可用
int gpio_direction_input(unsigned gpio)//gpio配置为输入