轉載於: http://blog.csdn.net/viewsky11/article/details/53402536
GPIO相關的dvice tree設置和interrupt設置
gpoi號以及gpio相關的屬性設置
以tsp的proxy_en端口為例:
i2c@78b6000 { /* BLSP1 QUP2 */ compatible = "qcom,i2c-msm-v2"; ... tmd3782@39 { compatible = "taos,tmd3782"; ... taos,en = <&msm_gpio 8 0x1>; // ... }
在相應的驅動里邊,取gpio編號以及設置輸入或者輸入
//在tsp驅動里邊,可以通過of_get_named_gpio()來取相應的gpio號 pdata->enable = of_get_named_gpio(np, "taos,en", 0); gpio_direction_output(pdata->enable, 1);
那這個gpio的active的時候和sleep的時候的PULL_DOWN
,PULL_UP
,NO_PULL
等屬性在哪里設置呢?
其實是在pinctrl相關的dtsi文件里邊設置的
tlmm_pinmux: pinctrl@1000000 { compatible = "qcom,msm-tlmm-8916";//看一下相應的驅動 reg = <0x1000000 0x300000>; interrupts = <0 208 0>; //gp: general purpose pins //此外還有兩種pin type: //sdc : SDC pins //qdsc: QDSC pins /*General purpose pins*/ gp: gp { qcom,pin-type-gp; qcom,num-pins = <122>; //這個pin type里包含的pin的個數 #qcom,pin-cells = <1>; msm_gpio: msm_gpio { compatible = "qcom,msm-tlmm-gp"; gpio-controller;//指定當前msm_gpio為一個gpio-controller #gpio-cells = <2>; //#gpio-cells的值指定每個msm_gpio后面跟幾個數來表示一個gpio。 //如果是2的話,就要像上面tsp一樣寫成taos,en = <&msm_gpio 8 0x1>; //前面的8指定gpio號,但后面的還不是很清楚,應該是表示輸入輸出等,這里0x1表示輸出 interrupt-controller; //表示可以用作中斷控制器 #interrupt-cells = <2>;//兩個的話,前面的表示irq號,后面的一個optional flags num_irqs = <122>;//表示可以被用作中斷源的pin的個數 }; }; //定義sdc pin type /* Sdc pin type */ sdc: sdc { qcom,num-pins = <6>; #qcom,pin-cells = <1>; }; prox_sensor_power { qcom,pins = <&gp 8>; //gpio 8的上拉,下拉或者no pull等屬性 qcom,pin-func = <0>; qcom,num-grp-pins = <1>; //qcom,num-grp-pins,這個表示一共有幾個pin //msm-pinctrl.txt里邊寫的是number of pins in the group. label = "prox_sensor_power"; //label: name to identify the pin group to be used by a client. //以下是pinctrl的時候的設置,下面有pinctrl的說明 prox_power_active: prox_power_active { drive-strength = <2>;//2MA bias-disable; /* No PULL *///也可以寫成 bias-disable = <0>; //這里可以把bias-disable替換成bias-pull-up;或者bias-pull-down; }; prox_power_suspend: prox_power_suspend { drive-strength = <2>; //2MA bias-disable; /* No PULL */ }; };
在dts里邊定義gpio相關的中斷
以hall id相關的dtsi定義為例
hall { status = "okay"; compatible = "hall"; interrupt-parent = <&msm_gpio>;//表示當前的中斷控制器用的哪個,高通平台的dts文件中, //msm_gpio表示msm_tlmm_irq中斷控制器。 interrupts = <52 0>; //第一個52是指中斷號,后面的0不知道表示什么,根據文檔,這個值有以下意義 //1: low-to-high edge triggered //2: high-to-low edge triggered //3: active high-level-sensitive //4: active low-level-sensitive //Documentation/devicetree/bindings/arm/gic.txt hall,gpio_flip_cover = <&msm_gpio 52 0>; };
如果定義了一個以上的interrupts號該怎么弄呢?
比如:
hall { ... interrupts = <52 0> <62 0>; ... }
這種可以通過,platform_get_irq(pdev,0)
來取第一個irq號,platform_get_irq(pdev,1)
來取第二個irq號。
reg相關的設置
以下是reg
, address-cells
,size-cells
的解釋,但還不知道從哪里讀出來這些並設置??
-reg
- #address-cells
-#size-cells
其中reg的組織形式為 reg = <address1 length1 [address2 length2] [address3 length3] ... >
,
其中的每一組address length表明了設備使用的一個地址范圍。address為1個或多個32位的
整型(即cell),而length則為cell的列表或者為空(若\#size-cells = 0
)。
address 和 length 字段是可變長的,父結點的#address-cells
和#size-cells
分別決定了子結點的
reg屬性的address和length字段的長度。
在本例中,root結點的#address-cells = <1>;
和#size-cells = <1>;
決定了serial、gpio、spi等結點的address和length字段的長度分別為1。
cpus 結點的#address-cells = <1>;
和#size-cells = <0>;
決定了2個cpu子結
點的address為1,而length為空,於是形成了2個cpu的reg = <0>;
和reg = <1>;
。
external-bus結點的#address-cells = <2>
和#size-cells = <1>;
決定了其下的
ethernet、i2c、flash的reg字段形如reg = <0 0 0x1000>;
、reg = <1 0 0x1000>;
和reg = <2 0 0x4000000>;
。
其中,address字段長度為0,開始的第一個cell(0、1、2)
是對應的片選,第2個cell(0,0,0)
是相對該片選的基地址,第3個cell(0x1000、0x1000、0x4000000)
為length。
特別要留意的是i2c結點中定義的 #address-cells = <1>;
和#size-cells = <0>;
pinctrl相關的設置
pinctrl相關的的設置到底有什么用呢?在驅動里邊常常碰到驅動相關的一個或者幾個gpio,在
醒來或者睡眠的時候需要設置成不同的類型,不如醒來的時候是i2c端口,但睡眠的時候可能要
設置成GPIO並把輸出設置成0等。
這個時候pinctrl就派上用場了,這個可以大大簡化驅動的編寫,因為這個可以像下面這樣根據
active和suspend來設置要配置的gpio的管腳配置,然后在驅動里邊調用
devm_pinctrl_get_select(dev,"tlmm_motor_active");
來實現pinctrl-0
和pinctrl-1
里對應tlmm_motor_active
和tlmm_motor_suspend
的配置!!像下面的device tree配置的話,如果是devm_pinctrl_get_select(dev,”tlmm_motor_active”);
則應該就是把pinctrl-0
里邊的gpio相關配置都配置上去。如果是devm_pinctrl_get_select(dev,”tlmm_motor_suspend”);
的話就把pinctrl-1
里邊的設置都配置上去。
具體devm_pinctrl_xxx
這種接口說明也可以參考kernel下面的Documentation/pintrl.txt文件
&soc { xxx,vibrator { compatible = "haptic_vib"; //下面的pinctrl的設置,好像是沒有在驅動里邊讀取並進行設置, //不知道什么用~~ pinctrl-names = "tlmm_motor_active","tlmm_motor_suspend"; pinctrl-0 = <&motor_en_active &motor_pwm_active>; pinctrl-1 = <&motor_en_suspend &motor_pwm_suspend>; //下面en,pwm的設置前面已經講過 xxx,vib_en = <&msm_gpio 76 0x1>; xxx,vib_pwm = <&msm_gpio 50 0x1>; xxx,vib_model = <1>; xxx,is_pmic_vib_pwm = <0>; xxx,pwm_period_us = <40>; xxx,duty_us = <36>; status = "ok"; }; //這里muic_i2c_active,muic_int_pin ,muic_chg_det這種都可以在gpio controller那里找到相應 //的設置,例如下面這樣 tlmm_motor_en { qcom,pins = <&gp 76>; qcom,pin-func = <0>; qcom,num-grp-pins = <1>; label = "tlmm_motor_en"; motor_en_active: motor_en_active { drive-strength = <2>; bias-disable = <0>; /* No PULL */ }; motor_en_suspend: motor_en_suspend { drive-strength = <2>; bias-disable = <0>; /* No PULL */ }; };
在dts里邊定義gpio和中斷的文檔
還有像gpio-ranges這種沒有說明,可以再Documentation/devicetree/bindings/gpio/gpio.txt里邊找到說明。
還有一個是高通的gpio說明,在Documentation/devicetree/bindings/pintrl/msm-pintrl.txt。
在gpio和中斷debug方法
在debug目錄下,可以查到每個gpio的輸入輸出設置,以及當前的值。
#cat /d/gpio //這個命令只會顯示AP設置的GPIO信息,不顯示Modem設置的GPIO信息
如果想看更詳細的GPIO設置的話
#cat /d/gpiomux //顯示AP,CP所有的GPIO的信息
例:
//開始操作GPIO的時候必須要先執行 #echo 30 > /sys/class/gpio/export //設置GPIO 30的輸入輸出 #echo "out" > /sys/class/gpio/gpio30/direction #echo "in" > /sys/class/gpio/gpio30/direction //改變GPIO 30的值 #echo 1 > /sys/class/gpio/gpio30/value #echo 0 > /sys/class/gpio/gpio30/value //操作完畢需要執行如下命令 #echo 30 > /sys/class/gpio/unexport
查找Wakeup IRQ等
#echo 1 > /sys/module/msm_show_resume_irq/parameters/debug_mask. //這樣輸入完之后,如果被中斷喚醒就會輸出如下log [ 75.0xxx] pm8xxx_show_resume_irq_chip: 479 triggered [ 75.0xxx] msm_gpio_show_resume_irq: 392 triggered [ 75.0xxx] gic_show_resume_irq: 48 triggered [ 75.0xxx] gic_show_resume_irq: 52 triggered
顯示整個中斷設置情況
#cat /proc/interrupts
GIC 中斷控制器
GIC中斷控制器的device tree定義的例子如下:
intc:interrupt-controller@F9000000{ compitable = "qcom,msm-qgic2"; interrupt-controller;//聲明這個為一個中斷控制器 #interrup-cells = <3>;//高通的這里是3,具體看芯片的GIC中斷控制器 reg = <0xF9000000 0x1000> , <0xF900200 0x1000>; }
這里interrupt-controller
的意思跟上面解釋的一樣,但#interrupt-cells
的值必須是3。
高通的是這樣的,當然其他芯片的還要具體看GIC控制器驅動。
申請中斷的例子:
device1@f991f000{ compatible = "qcom,msm-device-v1"; reg = <0xf991f000 0x1000>; interrupt-parent = <&intc>; //指定中斷控制器 interrupts = <0 131 0>, <0 179 0>; interrupt-names = "irq" ,"otg_irq"; };
這里interrupts的3個數中,后面兩個和前面說的一樣的,分別是中斷號和中斷類型。
第三個數指定的中斷類型:
//1: low-to-high edge triggered //2: high-to-low edge triggered //3: active high-level-sensitive //4: active low-level-sensitive //Documentation/devicetree/bindings/arm/gic.txt
那第一個數表示什么呢?第一個數表示中斷GIC的中斷類型。
0 表示:shared processor interrupts (SPI)
1 表示:Private Pripheral Interrupts (PPI)
還有interrupt mapping內容看一下Linux Device Tree GPIO文檔。
以下是打印的某個高通平台的/proc/interrupts的內容
GIC的中斷號有些和msmxxx.dtsi
里邊設置的終端號不一致,一般有一定的偏移量。之前看到過的是32,還沒找在哪里設置的。
root@gtelltevzw:/proc # cat interrupts cat interrupts CPU0 CPU1 CPU2 CPU3 20: 11501064 2634910 1450801 1172471 GIC arch_timer 35: 0 0 0 0 GIC apps_wdog_bark 39: 5643824 2599019 1701936 1316131 GIC arch_mem_timer 47: 52981 0 0 0 GIC cpr 56: 0 0 0 0 GIC modem 57: 1527948 0 0 0 GIC qcom,smd-modem 58: 5 0 0 0 GIC qcom,smsm-modem 59: 5 0 0 0 GIC smp2p 61: 10 0 0 0 GIC sps 65: 23838 0 0 0 GIC kgsl-3d0 75: 0 0 0 0 GIC msm_iommu_global_cfg_irq, msm_iommu_global_cfg_irq 76: 420 0 0 0 GIC msm_vidc 82: 10 0 0 0 GIC cci 83: 2 0 0 0 GIC csid 84: 2 0 0 0 GIC csid 89: 2 0 0 0 GIC 102: 0 0 0 0 GIC msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_secure_irq, msm_iommu_secure_irq, msm_iommu_secure_irq, msm_iommu_secure_irq, msm_iommu_secure_irq, msm_iommu_secure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq, msm_iommu_nonsecure_irq 104: 650484 0 0 0 GIC MDSS 110: 0 0 0 0 GIC csiphy 111: 0 0 0 0 GIC csiphy 127: 0 0 0 0 GIC i2c-msm-v2-irq 128: 7585 0 0 0 GIC i2c-msm-v2-irq 130: 0 0 0 0 GIC i2c-msm-v2-irq 131: 0 0 0 0 GIC i2c-msm-v2-irq 132: 0 0 0 0 GIC i2c-msm-v2-irq 140: 384648 0 0 0 GIC msm_serial_hsl0 155: 12681898 0 0 0 GIC mmc0 157: 0 0 0 0 GIC mmc1 166: 1761 0 0 0 GIC msm_otg, msm_hsusb 170: 263953 0 0 0 GIC 7824900.sdhci 172: 0 0 0 0 GIC msm_otg 174: 207 0 0 0 GIC qcom,smd-wcnss 175: 5 0 0 0 GIC smp2p 176: 0 0 0 0 GIC qcom,smsm-wcnss 181: 0 0 0 0 GIC wcnss 200: 8461599 379482 175887 113247 GIC qcom,smd-rpm 203: 932778 390645 274255 170499 GIC 601d0.qcom,mpm 216: 0 0 0 0 GIC tsens_interrupt 222: 7 0 0 0 GIC 200f000.qcom,spmi 239: 0 0 0 0 GIC sps 240: 946 0 0 0 GIC 1000000.pinctrl 253: 2 0 0 0 GIC 7864900.sdhci 273: 0 0 0 0 GIC msm_iommu_nonsecure_irq 274: 0 0 0 0 GIC msm_iommu_nonsecure_irq 280: 1 0 0 0 GIC mobicore 288: 3 0 0 0 msm_tlmm_irq sm5703 290: 0 0 0 0 msm_tlmm_irq 7864900.sdhci cd 291: 6 0 0