基于tiny4412的Linux内核移植 -- MMA7660驱动移植(九)

2023-06-21  

平台简介

开发板:tiny4412ADK + S700 + 4GB Flash

要移植的内核版本:Linux-4.4.0 (支持device tree)

u-boot版本:友善之臂自带的 U-Boot 2010.12 (为支持uImage启动,做了少许改动)

busybox版本:busybox 1.25

交叉编译工具链: arm-none-linux-gnueabi-gcc

      (gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-29))

摘要

    MMA7660是一个三轴加速度传感器,跟exynos4412之间使用I2C接口进行通信,同时MMA7660可以向exynos4412发起外部中断。

    移植MMA7660驱动会涉及到device tree、I2C驱动、中断、输入子系统等几个部分,tiny4412自带的MMA7660驱动程序是不支持设备树的,同时I2C驱动也没有采用设备树,所以主要的工作量就是将MMA7660和I2C驱动程序从非设备树形式转变为设备树的形式。同时借此机会,学习一下有设备树的情况下的设备驱动(MMA7660和I2C)和中断。

移植

一、原理图

下面是MMA7660的在底板原理图:

image

     可以看到,使用的是第3个I2C控制器。

下面是核心板:

  I2C:

image

   XEINT25:

image

二、tiny4412自带的驱动

    tiny4412自带的mma7660驱动并不是采用设备树,但是可以作为我们的参考,在arch/arm/mach-exynos/mach-tiny4412.c中包含了mma7660的板级信息。

MMA7660的板级信息:

   1: #include

   2: static struct mma7660_platform_data mma7660_pdata = {

   3:     .irq            = IRQ_EINT(25),

   4:     .poll_interval    = 100,

   5:     .input_fuzz        = 4,

   6:     .input_flat        = 4,

   7: };

   8:  

   9: static struct s3c2410_platform_i2c tiny4412_i2c3_data __initdata = {

  10:     .flags            = 0,

  11:     .bus_num        = 3,

  12:     .slave_addr        = 0x10,

  13:     .frequency        = 200*1000,

  14:     .sda_delay        = 100,

  15: };

  16:  

  17: static struct i2c_board_info i2c_devs3[] __initdata = {

  18:     {

  19:         I2C_BOARD_INFO("mma7660", 0x4c),

  20:         .platform_data = &mma7660_pdata,

  21:     },

  22: };

  23:  

  24: static void __init smdk4x12_machine_init(void)

  25: {

  26:     ... ...

  27:     s3c_i2c3_set_platdata(&tiny4412_i2c3_data);

  28:     i2c_register_board_info(3, i2c_devs3, ARRAY_SIZE(i2c_devs3));   // 注册板级信息

  29:     ... ...

  30: }

其中,


从上面的信息我们可以知道:


MMA7660的器件地址是0x4c,I2C3的CLK信号新的频率为200KHz。这两个信息比较重要。MMA7660的驱动程序是linux-3.0.86/drivers/hwmon/mma7660.c。


I2C的板级信息:


在arch/arm/plat-samsung/dev-i2c3.c中:


   1: /* linux/arch/arm/plat-samsung/dev-i2c3.c

   2:  *

   3:  * Copyright (c) 2010 Samsung Electronics Co., Ltd.

   4:  *        http://www.samsung.com/

   5:  *

   6:  * S5P series device definition for i2c device 3

   7:  *

   8:  * This program is free software; you can redistribute it and/or modify

   9:  * it under the terms of the GNU General Public License version 2 as

  10:  * published by the Free Software Foundation.

  11:  */

  12:  

  13: #include

  14: #include

  15: #include

  16: #include

  17:  

  18: #include

  19: #include

  20:  

  21: #include

  22: #include

  23: #include

  24: #include

  25:  

  26: static struct resource s3c_i2c_resource[] = {

  27:     [0] = {

  28:         .start    = S3C_PA_IIC3,

  29:         .end    = S3C_PA_IIC3 + SZ_4K - 1,

  30:         .flags    = IORESOURCE_MEM,

  31:     },

  32:     [1] = {

  33:         .start    = IRQ_IIC3,

  34:         .end    = IRQ_IIC3,

  35:         .flags    = IORESOURCE_IRQ,

  36:     },

  37: };

  38:  

  39: struct platform_device s3c_device_i2c3 = {

  40:     .name        = "s3c2440-i2c",

  41:     .id        = 3,

  42:     .num_resources    = ARRAY_SIZE(s3c_i2c_resource),

  43:     .resource    = s3c_i2c_resource,

  44: };

  45:  

  46: void __init s3c_i2c3_set_platdata(struct s3c2410_platform_i2c *pd)

  47: {

  48:     struct s3c2410_platform_i2c *npd;

  49:  

  50:     if (!pd) {

  51:         pd = &default_i2c_data;

  52:         pd->bus_num = 3;

  53:     }

  54:  

  55:     npd = s3c_set_platdata(pd, sizeof(struct s3c2410_platform_i2c),

  56:                    &s3c_device_i2c3);

  57:  

  58:     if (!npd->cfg_gpio)

  59:         npd->cfg_gpio = s3c_i2c3_cfg_gpio;

  60: }

然后会在arch/arm/mach-exynos/mach-tiny4412.c中注册:


   1: static struct platform_device *smdk4x12_devices[] __initdata = {

   2:     ... ...

   3:     &s3c_device_i2c3,

   4:     ... ...

   5: }

   6:  

   7: static void __init smdk4x12_machine_init(void)

   8: {

   9:     ... ...

  10:     platform_add_devices(smdk4x12_devices, ARRAY_SIZE(smdk4x12_devices));

  11:     ... ...

  12: }

I2C控制器对应的驱动是linux-3.0.86/drivers/i2c/busses/i2c-s3c2410.c。


三、移植


1、首先把MMA7660和I2C控制器的板级信息转化为设备树的形式,修改arch/arm/boot/dts/exynos4412-tiny4412.dts,添加MMA7660和I2C的硬件信息,可以参考内核文档:Documentation/devicetree/bindings/i2c/i2c.txt和Documentation/devicetree/bindings/i2c/i2c-s3c2410.txt,中断资源的填写可以参考内核文档Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt。


   1: /* MMA7660FC  */

   2: &i2c_3 {

   3:     samsung,i2c-sda-delay = <100>;

   4:     samsung,i2c-slave-addr = <0x10>;

   5:     samsung,i2c-max-bus-freq = <200000>;

   6:     pinctrl-0 = ;

   7:     pinctrl-names = "default";

   8:     status = "okay";

   9:  

  10:     mma7660@4c {

  11:         compatible = "freescale,mma7660";

  12:         reg = <0x4c>;

  13:         interrupt-parent = ;

  14:         interrupts = <1 2>;

  15:         poll_interval = <100>;

  16:         input_fuzz = <4>;

  17:         input_flat = <4>;

  18:         status = "okay";

  19:     };

  20: };

上面的信息基本上是把原来的板级信息搬过来。


第13行和第14行是设置中断资源,参考Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt:


External GPIO and Wakeup Interrupts:

 

The controller supports two types of external interrupts over gpio. The first

is the external gpio interrupt and second is the external wakeup interrupts.

The difference between the two is that the external wakeup interrupts can be

used as system wakeup events.

 

A. External GPIO Interrupts: For supporting external gpio interrupts, the

   following properties should be specified in the pin-controller device node.

 

   - interrupt-parent: phandle of the interrupt parent to which the external

     GPIO interrupts are forwarded to.

   - interrupts: interrupt specifier for the controller. The format and value of

     the interrupt specifier depends on the interrupt parent for the controller.

 

   In addition, following properties must be present in node of every bank

   of pins supporting GPIO interrupts:

 

   - interrupt-controller: identifies the controller node as interrupt-parent.

   - #interrupt-cells: the value of this property should be 2.

     - First Cell: represents the external gpio interrupt number local to the

       external gpio interrupt space of the controller.

     - Second Cell: flags to identify the type of the interrupt

       - 1 = rising edge triggered

       - 2 = falling edge triggered

       - 3 = rising and falling edge triggered

       - 4 = high level triggered

       - 8 = low level triggered

对于interrupts = <1 2>,其中1表示GPX3_1,2表示的是下降沿触发。


第2行的i2c_3是一个标号,i2c3的其他信息是在arch/arm/boot/dts/exynos4.dtsi中:


   1: i2c_3: i2c@13890000 {

   2:     #address-cells = <1>;

   3:     #size-cells = <0>;

   4:     compatible = "samsung,s3c2440-i2c";

   5:     reg = <0x13890000 0x100>;

   6:     interrupts = <0 61 0>;

   7:     clocks = ;

文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。