catalogue
Four important analytical functions
origin
Linus Torvalds declared "this whole ARM thing is a f*cking pain in the ass" in the ARM Linux mailing list on March 17, 2011, which triggered an earthquake in the ARM Linux community, and then the ARM community made a series of major amendments. In the past ARM Linux, arch / ARM / plat XXX and arch / ARM / Mach XXX were filled with a large amount of garbage code. A considerable number of codes only described board level details, which were just garbage for the kernel, such as platform devices, resource s and I2C on the board_ board_ info,spi_board_info and various hardware platforms_ data. If you are interested, you can count the common board level directories such as s3c2410 and s3c6410, with tens of thousands of lines of code.
The community must change this situation, so the flatted Device Tree (FDT) already used under other architectures such as PowerPC has entered the vision of the ARM community. Device Tree is a data structure describing hardware, which originated from OpenFirmware (OF). In Linux 2.6, the board hardware details of ARM architecture are too hard coded in arch / ARM / plat XXX and arch / ARM / Mach XXX. After using Device Tree, many hardware details can be directly transmitted to Linux through it without a lot of redundant coding in the kernel.
The Device Tree consists of a series of named nodes and properties, and the node itself can contain child nodes. The so-called properties are actually paired names and value s. In the Device Tree, descriptive information includes (most of these information was hard code d into the kernel):
The following is an example of dtsi
#include <dt-bindings/interrupt-controller/irq.h> #include <dt-bindings/interrupt-controller/arm-gic.h> #include <dt-bindings/soc/rockchip,boot-mode.h> / { #address-cells = <1>; #size-cells = <1>; interrupt-parent = <&gic>; aliases { ethernet0 = &emac; i2c0 = &i2c0; i2c1 = &i2c1; mshc0 = &emmc; mshc1 = &mmc0; serial0 = &uart0; serial1 = &uart1; spi0 = &spi0; spi1 = &spi1; }; amba { compatible = "simple-bus"; #address-cells = <1>; #size-cells = <1>; ranges; dmac1_s: dma-controller@20018000 { compatible = "arm,pl330", "arm,primecell"; reg = <0x20018000 0x4000>; interrupts = <GIC_SPI 0 IRQ_TYPE_LEVEL_HIGH>, <GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>; #dma-cells = <1>; arm,pl330-broken-no-flushp; clocks = <&cru ACLK_DMA1>; clock-names = "apb_pclk"; }; dmac1_ns: dma-controller@2001c000 { compatible = "arm,pl330", "arm,primecell"; reg = <0x2001c000 0x4000>; interrupts = <GIC_SPI 0 IRQ_TYPE_LEVEL_HIGH>, <GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>; #dma-cells = <1>; arm,pl330-broken-no-flushp; clocks = <&cru ACLK_DMA1>; clock-names = "apb_pclk"; status = "disabled"; }; dmac2: dma-controller@20078000 { compatible = "arm,pl330", "arm,primecell"; reg = <0x20078000 0x4000>; interrupts = <GIC_SPI 2 IRQ_TYPE_LEVEL_HIGH>, <GIC_SPI 3 IRQ_TYPE_LEVEL_HIGH>; #dma-cells = <1>; arm,pl330-broken-no-flushp; clocks = <&cru ACLK_DMA2>; clock-names = "apb_pclk"; }; }; uart0: serial@10124000 { compatible = "snps,dw-apb-uart"; reg = <0x10124000 0x400>; interrupts = <GIC_SPI 34 IRQ_TYPE_LEVEL_HIGH>; reg-shift = <2>; reg-io-width = <1>; clock-names = "baudclk", "apb_pclk"; clocks = <&cru SCLK_UART0>, <&cru PCLK_UART0>; status = "disabled"; }; uart1: serial@10126000 { compatible = "snps,dw-apb-uart"; reg = <0x10126000 0x400>; interrupts = <GIC_SPI 35 IRQ_TYPE_LEVEL_HIGH>; reg-shift = <2>; reg-io-width = <1>; clock-names = "baudclk", "apb_pclk"; clocks = <&cru SCLK_UART1>, <&cru PCLK_UART1>; status = "disabled"; }; usb_otg: usb@10180000 { compatible = "rockchip,rk3066-usb", "snps,dwc2"; reg = <0x10180000 0x40000>; interrupts = <GIC_SPI 16 IRQ_TYPE_LEVEL_HIGH>; clocks = <&cru HCLK_OTG0>; clock-names = "otg"; dr_mode = "otg"; g-np-tx-fifo-size = <16>; g-rx-fifo-size = <275>; g-tx-fifo-size = <256 128 128 64 64 32>; phys = <&usbphy0>; phy-names = "usb2-phy"; status = "disabled"; }; usb_host: usb@101c0000 { compatible = "snps,dwc2"; reg = <0x101c0000 0x40000>; interrupts = <GIC_SPI 17 IRQ_TYPE_LEVEL_HIGH>; clocks = <&cru HCLK_OTG1>; clock-names = "otg"; dr_mode = "host"; phys = <&usbphy1>; phy-names = "usb2-phy"; status = "disabled"; }; emac: ethernet@10204000 { compatible = "snps,arc-emac"; reg = <0x10204000 0x3c>; interrupts = <GIC_SPI 19 IRQ_TYPE_LEVEL_HIGH>; #address-cells = <1>; #size-cells = <0>; rockchip,grf = <&grf>; clocks = <&cru HCLK_EMAC>, <&cru SCLK_MAC>; clock-names = "hclk", "macref"; max-speed = <100>; phy-mode = "rmii"; status = "disabled"; }; emmc: dwmmc@1021c000 { compatible = "rockchip,rk2928-dw-mshc"; reg = <0x1021c000 0x1000>; interrupts = <GIC_SPI 25 IRQ_TYPE_LEVEL_HIGH>; clocks = <&cru HCLK_EMMC>, <&cru SCLK_EMMC>; clock-names = "biu", "ciu"; dmas = <&dmac2 4>; dma-names = "rx-tx"; fifo-depth = <256>; resets = <&cru SRST_EMMC>; reset-names = "reset"; status = "disabled"; }; pmu: pmu@20004000 { compatible = "rockchip,rk3066-pmu", "syscon", "simple-mfd"; reg = <0x20004000 0x100>; reboot-mode { compatible = "syscon-reboot-mode"; offset = <0x40>; mode-normal = <BOOT_NORMAL>; mode-recovery = <BOOT_RECOVERY>; mode-bootloader = <BOOT_FASTBOOT>; mode-loader = <BOOT_BL_DOWNLOAD>; }; }; i2c0: i2c@2002d000 { compatible = "rockchip,rk3066-i2c"; reg = <0x2002d000 0x1000>; interrupts = <GIC_SPI 40 IRQ_TYPE_LEVEL_HIGH>; #address-cells = <1>; #size-cells = <0>; rockchip,grf = <&grf>; clock-names = "i2c"; clocks = <&cru PCLK_I2C0>; status = "disabled"; }; wdt: watchdog@2004c000 { compatible = "snps,dw-wdt"; reg = <0x2004c000 0x100>; clocks = <&cru PCLK_WDT>; interrupts = <GIC_SPI 51 IRQ_TYPE_LEVEL_HIGH>; status = "disabled"; }; saradc: saradc@2006c000 { compatible = "rockchip,saradc"; reg = <0x2006c000 0x100>; interrupts = <GIC_SPI 26 IRQ_TYPE_LEVEL_HIGH>; #io-channel-cells = <1>; clocks = <&cru SCLK_SARADC>, <&cru PCLK_SARADC>; clock-names = "saradc", "apb_pclk"; resets = <&cru SRST_SARADC>; reset-names = "saradc-apb"; status = "disabled"; }; spi0: spi@20070000 { compatible = "rockchip,rk3066-spi"; clocks = <&cru SCLK_SPI0>, <&cru PCLK_SPI0>; clock-names = "spiclk", "apb_pclk"; interrupts = <GIC_SPI 38 IRQ_TYPE_LEVEL_HIGH>; reg = <0x20070000 0x1000>; #address-cells = <1>; #size-cells = <0>; dmas = <&dmac2 10>, <&dmac2 11>; dma-names = "tx", "rx"; status = "disabled"; }; lradc: lradc@20080000 { compatible = "test,adc-keys"; reg = <0x01c22800 0x100>; interrupts = <31>; vref-supply = <®_vcc3v0>; button@191 { label = "Volume Up"; linux,code = <KEY_VOLUMEUP>; channel = <0>; voltage = <191274>; }; button@392 { label = "Volume Down"; linux,code = <KEY_VOLUMEDOWN>; channel = <0>; voltage = <392644>; }; button@601 { label = "Menu"; linux,code = <KEY_MENU>; channel = <0>; voltage = <601151>; }; button@795 { label = "Enter"; linux,code = <KEY_ENTER>; channel = <0>; voltage = <795090>; }; button@987 { label = "Home"; linux,code = <KEY_HOMEPAGE>; channel = <0>; voltage = <987387>; }; }; };
The above involves usb, uart, IIC, EMMC, spi, etc., and basically covers the common driver types of Linux. Through comparison, it is found that they all have compatible and status, which are also essential. Compatible means that after the driver is used as a platform device, it is matched when the kernel is loaded. If the string in compatible is different from that in the driver you write, it will fail to load; Status is usually disabled or OK. If you use this device, set it to OK. If you don't use it, set it to disabled.
Driver code parsing
driver loading
Let's take an adc key driver as an example. It is loaded in the last driver of the dtsi file above
static const struct of_device_id adc_of_match[] = { { .compatible = "test,adc-keys", }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, adc_of_match); static struct platform_driver adc_driver = { .driver = { .name = "adc-keys", .of_match_table = of_match_ptr(adc_of_match), }, .probe = adc_probe, }; module_platform_driver(adc_driver);
The string of the above compatible field must be consistent with the string of the dtsi file before it can be loaded. After matching, the probe function can be executed
static int adc_probe(struct platform_device *pdev) { struct adc_data *lradc; struct device *dev = &pdev->dev; int i; int error; lradc = devm_kzalloc(dev, sizeof(struct adc_data), GFP_KERNEL); if (!lradc) return -ENOMEM; error = adc_load_dt_keymap(dev, lradc); if (error) return error; lradc->vref_supply = devm_regulator_get(dev, "vref"); if (IS_ERR(lradc->vref_supply)) return PTR_ERR(lradc->vref_supply); lradc->dev = dev; lradc->input = devm_input_allocate_device(dev); if (!lradc->input) return -ENOMEM; lradc->input->name = pdev->name; lradc->input->phys = "adc/input0"; lradc->input->open = adc_open; lradc->input->close = adc_close; lradc->input->id.bustype = BUS_HOST; lradc->input->id.vendor = 0x0001; lradc->input->id.product = 0x0001; lradc->input->id.version = 0x0100; __set_bit(EV_KEY, lradc->input->evbit); for (i = 0; i < lradc->chan0_map_count; i++) __set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit); input_set_drvdata(lradc->input, lradc); lradc->base = devm_ioremap_resource(dev, platform_get_resource(pdev, IORESOURCE_MEM, 0)); if (IS_ERR(lradc->base)) return PTR_ERR(lradc->base); error = devm_request_irq(dev, platform_get_irq(pdev, 0), adc_irq, 0, "adc-keys", lradc); if (error) return error; error = input_register_device(lradc->input); if (error) return error; return 0; }
dtsi file content parsing
The probe function above runs to adc_load_dt_keymap will read the key configuration information from dtsi and save it to ADC_ In the data corresponding structure, let's focus on the analysis of dtsi files
static int adc_load_dt_keymap(struct device *dev, struct adc_data *lradc) { struct device_node *np, *pp; int i; int error; np = dev->of_node; if (!np) return -EINVAL; lradc->chan0_map_count = of_get_child_count(np); if (lradc->chan0_map_count == 0) { dev_err(dev, "keymap is missing in device tree\n"); return -EINVAL; } lradc->chan0_map = devm_kmalloc_array(dev, lradc->chan0_map_count, sizeof(struct adc_keymap), GFP_KERNEL); if (!lradc->chan0_map) return -ENOMEM; i = 0; for_each_child_of_node(np, pp) { struct adc_keymap *map = &lradc->chan0_map[i]; u32 channel; error = of_property_read_u32(pp, "channel", &channel); if (error || channel != 0) { dev_err(dev, "%s: Inval channel prop\n", pp->name); return -EINVAL; } error = of_property_read_u32(pp, "voltage", &map->voltage); if (error) { dev_err(dev, "%s: Inval voltage prop\n", pp->name); return -EINVAL; } error = of_property_read_u32(pp, "linux,code", &map->keycode); if (error) { dev_err(dev, "%s: Inval linux,code prop\n", pp->name); return -EINVAL; } i++; } return 0; }
Four important analytical functions
of_get_child_count obtains the number of nodes from dtsi. The specific code implementation is as follows. It can be seen from the implementation that it calls for_each_child_of_node
static inline int of_get_child_count(const struct device_node *np) { struct device_node *child; int num = 0; for_each_child_of_node(np, child) num++; return num; }
devm_kmalloc_array allocates space according to the number of nodes. In this example, a block memory space is allocated according to the device defined by us and the number of nodes obtained above for filling after finding relevant keywords from dsti. Array malloc is used here, and devm is also used in the end_ kmalloc
static inline void *devm_kmalloc_array(struct device *dev, size_t n, size_t size, gfp_t flags) { if (size != 0 && n > SIZE_MAX / size) return NULL; return devm_kmalloc(dev, n * size, flags); }
for_each_child_of_node polls nodes, using a macro to define circular lookup
#define for_each_child_of_node(parent, child) \ for (child = of_get_next_child(parent, NULL); child != NULL; \ child = of_get_next_child(parent, child))
of_property_read_u32 according to the device node device_node lookup string
static inline int of_property_read_u32(const struct device_node *np, const char *propname, u32 *out_value) { return of_property_read_u32_array(np, propname, out_value, 1); } static inline int of_property_read_u32_array(const struct device_node *np, const char *propname, u32 *out_values, size_t sz) { int ret = of_property_read_variable_u32_array(np, propname, out_values, sz, 0); if (ret >= 0) return 0; else return ret; } int of_property_read_variable_u32_array(const struct device_node *np, const char *propname, u32 *out_values, size_t sz_min, size_t sz_max) { size_t sz, count; const __be32 *val = of_find_property_value_of_size(np, propname, (sz_min * sizeof(*out_values)), (sz_max * sizeof(*out_values)), &sz); if (IS_ERR(val)) return PTR_ERR(val); if (!sz_max) sz = sz_min; else sz /= sizeof(*out_values); count = sz; while (count--) *out_values++ = be32_to_cpup(val++); return sz; }
The above of related codes can be found through the Linux source code. Interested readers can continue to dig into how the code is implemented.
The complete code is as follows
#include <linux/err.h> #include <linux/init.h> #include <linux/input.h> #include <linux/interrupt.h> #include <linux/io.h> #include <linux/module.h> #include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/regulator/consumer.h> #include <linux/slab.h> #define LRADC_CTRL 0x00 #define LRADC_INTC 0x04 #define LRADC_INTS 0x08 #define LRADC_DATA0 0x0c #define LRADC_DATA1 0x10 /* LRADC_CTRL bits */ #define FIRST_CONVERT_DLY(x) ((x) << 24) /* 8 bits */ #define CHAN_SELECT(x) ((x) << 22) /* 2 bits */ #define CONTINUE_TIME_SEL(x) ((x) << 16) /* 4 bits */ #define KEY_MODE_SEL(x) ((x) << 12) /* 2 bits */ #define LEVELA_B_CNT(x) ((x) << 8) /* 4 bits */ #define HOLD_EN(x) ((x) << 6) #define LEVELB_VOL(x) ((x) << 4) /* 2 bits */ #define SAMPLE_RATE(x) ((x) << 2) /* 2 bits */ #define ENABLE(x) ((x) << 0) /* LRADC_INTC and LRADC_INTS bits */ #define CHAN1_KEYUP_IRQ BIT(12) #define CHAN1_ALRDY_HOLD_IRQ BIT(11) #define CHAN1_HOLD_IRQ BIT(10) #define CHAN1_KEYDOWN_IRQ BIT(9) #define CHAN1_DATA_IRQ BIT(8) #define CHAN0_KEYUP_IRQ BIT(4) #define CHAN0_ALRDY_HOLD_IRQ BIT(3) #define CHAN0_HOLD_IRQ BIT(2) #define CHAN0_KEYDOWN_IRQ BIT(1) #define CHAN0_DATA_IRQ BIT(0) struct adc_keymap { u32 voltage; u32 keycode; }; struct adc_data { struct device *dev; struct input_dev *input; void __iomem *base; struct regulator *vref_supply; struct adc_keymap *chan0_map; u32 chan0_map_count; u32 chan0_keycode; u32 vref; }; static irqreturn_t adc_irq(int irq, void *dev_id) { struct adc_data *lradc = dev_id; u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff; ints = readl(lradc->base + LRADC_INTS); /* * lradc supports only one keypress at a time, release does not give * any info as to which key was released, so we cache the keycode. */ if (ints & CHAN0_KEYUP_IRQ) { input_report_key(lradc->input, lradc->chan0_keycode, 0); lradc->chan0_keycode = 0; } if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) { val = readl(lradc->base + LRADC_DATA0) & 0x3f; voltage = val * lradc->vref / 63; for (i = 0; i < lradc->chan0_map_count; i++) { diff = abs(lradc->chan0_map[i].voltage - voltage); if (diff < closest) { closest = diff; keycode = lradc->chan0_map[i].keycode; } } lradc->chan0_keycode = keycode; input_report_key(lradc->input, lradc->chan0_keycode, 1); } input_sync(lradc->input); writel(ints, lradc->base + LRADC_INTS); return IRQ_HANDLED; } static int adc_open(struct input_dev *dev) { struct adc_data *lradc = input_get_drvdata(dev); int error; error = regulator_enable(lradc->vref_supply); if (error) return error; /* lradc Vref internally is divided by 2/3 */ lradc->vref = regulator_get_voltage(lradc->vref_supply) * 2 / 3; /* * Set sample time to 4 ms / 250 Hz. Wait 2 * 4 ms for key to * stabilize on press, wait (1 + 1) * 4 ms for key release */ writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) | SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL); writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC); return 0; } static void adc_close(struct input_dev *dev) { struct adc_data *lradc = input_get_drvdata(dev); /* Disable lradc, leave other settings unchanged */ writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) | SAMPLE_RATE(2), lradc->base + LRADC_CTRL); writel(0, lradc->base + LRADC_INTC); regulator_disable(lradc->vref_supply); } static int adc_load_dt_keymap(struct device *dev, struct adc_data *lradc) { struct device_node *np, *pp; int i; int error; np = dev->of_node; if (!np) return -EINVAL; lradc->chan0_map_count = of_get_child_count(np); if (lradc->chan0_map_count == 0) { dev_err(dev, "keymap is missing in device tree\n"); return -EINVAL; } lradc->chan0_map = devm_kmalloc_array(dev, lradc->chan0_map_count, sizeof(struct adc_keymap), GFP_KERNEL); if (!lradc->chan0_map) return -ENOMEM; i = 0; for_each_child_of_node(np, pp) { struct adc_keymap *map = &lradc->chan0_map[i]; u32 channel; error = of_property_read_u32(pp, "channel", &channel); if (error || channel != 0) { dev_err(dev, "%s: Inval channel prop\n", pp->name); return -EINVAL; } error = of_property_read_u32(pp, "voltage", &map->voltage); if (error) { dev_err(dev, "%s: Inval voltage prop\n", pp->name); return -EINVAL; } error = of_property_read_u32(pp, "linux,code", &map->keycode); if (error) { dev_err(dev, "%s: Inval linux,code prop\n", pp->name); return -EINVAL; } i++; } return 0; } static int adc_probe(struct platform_device *pdev) { struct adc_data *lradc; struct device *dev = &pdev->dev; int i; int error; lradc = devm_kzalloc(dev, sizeof(struct adc_data), GFP_KERNEL); if (!lradc) return -ENOMEM; error = adc_load_dt_keymap(dev, lradc); if (error) return error; lradc->vref_supply = devm_regulator_get(dev, "vref"); if (IS_ERR(lradc->vref_supply)) return PTR_ERR(lradc->vref_supply); lradc->dev = dev; lradc->input = devm_input_allocate_device(dev); if (!lradc->input) return -ENOMEM; lradc->input->name = pdev->name; lradc->input->phys = "adc/input0"; lradc->input->open = adc_open; lradc->input->close = adc_close; lradc->input->id.bustype = BUS_HOST; lradc->input->id.vendor = 0x0001; lradc->input->id.product = 0x0001; lradc->input->id.version = 0x0100; __set_bit(EV_KEY, lradc->input->evbit); for (i = 0; i < lradc->chan0_map_count; i++) __set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit); input_set_drvdata(lradc->input, lradc); lradc->base = devm_ioremap_resource(dev, platform_get_resource(pdev, IORESOURCE_MEM, 0)); if (IS_ERR(lradc->base)) return PTR_ERR(lradc->base); error = devm_request_irq(dev, platform_get_irq(pdev, 0), adc_irq, 0, "adc-keys", lradc); if (error) return error; error = input_register_device(lradc->input); if (error) return error; return 0; } static const struct of_device_id adc_of_match[] = { { .compatible = "test,adc-keys", }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, adc_of_match); static struct platform_driver adc_driver = { .driver = { .name = "adc-keys", .of_match_table = of_match_ptr(adc_of_match), }, .probe = adc_probe, }; module_platform_driver(adc_driver); MODULE_DESCRIPTION("adc attached tablet keys driver"); MODULE_LICENSE("GPL");