添加at24 eeprom 驱动

4 篇文章 1 订阅
2 篇文章 0 订阅

MSM8909平台添加at24 eeprom 驱动

关键词:android USB充电 androidboot.mode charger

平台信息:

平台:MSM8909
内核:linux3.10.49   
系统:android/android5.1

平台:QCM2150
内核:linux4.9.217   
系统:android/android10

kernel/arch/arm/configs/msm8909-1gb-perf_APOS_A8_defconfig

 CONFIG_MSM_CSID=y
 CONFIG_MSM_EEPROM=y
+CONFIG_EEPROM_AT24=y
 CONFIG_MSM_ISPIF=y
 CONFIG_MSM_VIDC_V4L2=y

kernel/arch/arm/boot/dts/apos/P960_msm8909-camera-sensor-skue.dtsi

&i2c_3 {

     eeprom@50 {
                compatible = "atmel,24c02";
                reg = <0x50>;
            };

    mipic0: hsm,mipic@07 {                    /* MIPI converter chip */
        compatible = "toshiba, tc358746";
        reg = <0x07>;
        gpios = <&msm_gpio 56 0>;
        hsm,gpio-mipi-reset = <0 1 0>;        /* index to gpios; init value; inverted */
    };

.....

.....

};

kernel/drivers/misc/eeprom/at24.c

#include <linux/gpio.h>
#include <linux/of_gpio.h>

/*
 * This parameter is to help this driver avoid blocking other drivers out
 * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
 * clock, one 256 byte read takes about 1/43 second which is excessive;
 * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
 * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
 *
 * This value is forced to be a power of two so that writes align on pages.
 */
static unsigned io_limit = 128;
module_param(io_limit, uint, 0);
MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");

此参数用于帮助该驱动程序避免在I2C之外阻塞其他驱动程序,以避免潜在的麻烦时间。对于100 kHz I2C时钟,一个256字节的读取大约需要1/43秒,这是多余的;但在400千赫时所需的1/170秒可能是相当合理的;在1MHz(Fm+)频率下,1/430秒的延迟很容易不可见。

static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
    struct at24_platform_data chip;
    bool writable;
    int use_smbus = 0;
    struct at24_data *at24;
    int err;
    unsigned i, num_addresses;
    kernel_ulong_t magic;

    if (client->dev.platform_data) {
        chip = *(struct at24_platform_data *)client->dev.platform_data;
    } else {
        if (!id->driver_data) {
            err = -ENODEV;
            goto err_out;
        }
        magic = id->driver_data;
        chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
        magic >>= AT24_SIZE_BYTELEN;
        chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
        /*
         * This is slow, but we can't know all eeproms, so we better
         * play safe. Specifying custom eeprom-types via platform_data
         * is recommended anyhow.
         */
        chip.page_size = 1;

        /* update chipdata if OF is present */
        at24_get_ofdata(client, &chip);

        chip.setup = NULL;
        chip.context = NULL;
    }

    if (!is_power_of_2(chip.byte_len))
        dev_warn(&client->dev,
            "byte_len looks suspicious (no power of 2)!\n");
    if (!chip.page_size) {
        dev_err(&client->dev, "page_size must not be 0!\n");
        err = -EINVAL;
        goto err_out;
    }
    if (!is_power_of_2(chip.page_size))
        dev_warn(&client->dev,
            "page_size looks suspicious (no power of 2)!\n");

    /* Use I2C operations unless we're stuck with SMBus extensions. */
    if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
        if (chip.flags & AT24_FLAG_ADDR16) {
            err = -EPFNOSUPPORT;
            goto err_out;
        }
        if (i2c_check_functionality(client->adapter,
                I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
            use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
        } else if (i2c_check_functionality(client->adapter,
                I2C_FUNC_SMBUS_READ_WORD_DATA)) {
            use_smbus = I2C_SMBUS_WORD_DATA;
        } else if (i2c_check_functionality(client->adapter,
                I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
            use_smbus = I2C_SMBUS_BYTE_DATA;
        } else {
            err = -EPFNOSUPPORT;
            goto err_out;
        }
    }

    if (chip.flags & AT24_FLAG_TAKE8ADDR)
        num_addresses = 8;
    else
        num_addresses =    DIV_ROUND_UP(chip.byte_len,
            (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);

    at24 = kzalloc(sizeof(struct at24_data) +
        num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
    if (!at24) {
        err = -ENOMEM;
        goto err_out;
    }

    mutex_init(&at24->lock);
    at24->use_smbus = use_smbus;
    at24->chip = chip;
    at24->num_addresses = num_addresses;

    /*
     * Export the EEPROM bytes through sysfs, since that's convenient.
     * By default, only root should see the data (maybe passwords etc)
     */
    sysfs_bin_attr_init(&at24->bin);
    at24->bin.attr.name = "eeprom";
    //at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
    at24->bin.attr.mode = S_IRWXU | S_IRWXG | S_IRWXO;

    at24->bin.read = at24_bin_read;
    at24->bin.size = chip.byte_len;

    at24->macc.read = at24_macc_read;

    writable = !(chip.flags & AT24_FLAG_READONLY);
    if (writable) {
        if (!use_smbus || i2c_check_functionality(client->adapter,
                I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {

            unsigned write_max = chip.page_size;

            at24->macc.write = at24_macc_write;

            at24->bin.write = at24_bin_write;
            at24->bin.attr.mode |= S_IWUSR;

            if (write_max > io_limit)
                write_max = io_limit;
            if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
                write_max = I2C_SMBUS_BLOCK_MAX;
            at24->write_max = write_max;

            /* buffer (data + address at the beginning) */
            at24->writebuf = kmalloc(write_max + 2, GFP_KERNEL);
            if (!at24->writebuf) {
                err = -ENOMEM;
                goto err_struct;
            }
        } else {
            dev_warn(&client->dev,
                "cannot write due to controller restrictions.");
        }
    }

    at24->client[0] = client;

    /* use dummy devices for multiple-address chips */
    for (i = 1; i < num_addresses; i++) {
        at24->client[i] = i2c_new_dummy(client->adapter,
                    client->addr + i);
        if (!at24->client[i]) {
            dev_err(&client->dev, "address 0x%02x unavailable\n",
                    client->addr + i);
            err = -EADDRINUSE;
            goto err_clients;
        }
    }

    err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
    if (err)
        goto err_clients;

    i2c_set_clientdata(client, at24);

    dev_info(&client->dev, "%zu byte %s EEPROM, %s, %u bytes/write\n",
        at24->bin.size, client->name,
        writable ? "writable" : "read-only", at24->write_max);
    if (use_smbus == I2C_SMBUS_WORD_DATA ||
        use_smbus == I2C_SMBUS_BYTE_DATA) {
        dev_notice(&client->dev, "Falling back to %s reads, "
               "performance will suffer\n", use_smbus ==
               I2C_SMBUS_WORD_DATA ? "word" : "byte");
    }

    /* export data to kernel code */
    if (chip.setup)
        chip.setup(&at24->macc, chip.context);

    return 0;

err_clients:
    for (i = 1; i < num_addresses; i++)
        if (at24->client[i])
            i2c_unregister_device(at24->client[i]);

    kfree(at24->writebuf);
err_struct:
    kfree(at24);
err_out:
    dev_dbg(&client->dev, "probe error %d\n", err);
    return err;
}

static int __init at24_init(void)
{
    if (!io_limit) {
        pr_err("at24: io_limit must not be 0!\n");
        return -EINVAL;
    }

    //@qq.com, added  start for eeprom 24c02
    //gpio_direction_output(33+911, 1);
    //gpio_direction_output(36+911, 1);
    //msleep(5);

//@qq.com, added  end for eeprom 24c02

    io_limit = rounddown_pow_of_two(io_limit);
    return i2c_add_driver(&at24_driver);
}
module_init(at24_init);

gpio_set_value 和 gpio_direction_output 的区别

        在linux驱动中常常会碰到gpio_set_value(port_num,0/1)或gpio_direction_output (port_num,0/1) 这两者有什么关系呢

gpio_set_value(port_num,0/1) 一般只是在这个GPIO口的寄存器上写上某个值,至于这个端口是否设置为输出,它就管不了!而gpio_direction_output (port_num,0/1),在某个GPIO口写上某个值之后,还会把这个端口设置为输出模式。 因此,有人也许就会建议,把gpio_set_value这个函数直接去掉不用,是否可以,显然是可以的。 但是为什么系统还要用呢, 我个人分析是, 系统开发人员在要结合这两者来使用,以便提高效率。 一般某个端口设置好了输入与输出模式后,最好不要经常变动。 首先要调用gpio_direction_output(),以后要设置高低电平时,直接使用gpio_set_value()就可以了,这样可以省却再 次调用设置输出模式的操作,从而提高运行效率。

gpio_direction_output 接口里面有mutes锁,会引起休眠,相比gpio_set_value接口做的事更多。

rounddown_pow_of_two 讲解

\kernel\include\linux\log2.h

rounddown_pow_of_two 该函数是取数的最高二进制阶数,即将给定值四舍五入到最接近的二次方

rounddown_pow_of_two 第一种处理

/**
 * roundup_pow_of_two - round the given value up to nearest power of two
 * @n - parameter
 *
 * round the given value up to the nearest power of two
 * - the result is undefined when n == 0
 * - this can be used to initialise global variables from constant data
 */
#define roundup_pow_of_two(n)            \
(                        \
    __builtin_constant_p(n) ? (        \
        (n == 1) ? 1 :            \
        (1UL << (ilog2((n) - 1) + 1))    \
                   ) :        \
    __roundup_pow_of_two(n)            \
 )

如果n 是常数则调用ilog2,这是因为宏是在编译(不仅在预处理阶段,而且在编译阶段)中处理的,不会影响到执行效率。

其中,宏ilog2的定义如下:

/**
 * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value
 * @n - parameter
 *
 * constant-capable log of base 2 calculation
 * - this can be used to initialise global variables from constant data, hence
 *   the massive ternary operator construction
 *
 * selects the appropriately-sized optimised version depending on sizeof(n)
 */
#define ilog2(n)                \
(                        \
    __builtin_constant_p(n) ? (        \
        (n) < 1 ? ____ilog2_NaN() :    \
        (n) & (1ULL << 63) ? 63 :    \
        (n) & (1ULL << 62) ? 62 :    \
        (n) & (1ULL << 61) ? 61 :    \
        (n) & (1ULL << 60) ? 60 :    \
        (n) & (1ULL << 59) ? 59 :    \
        (n) & (1ULL << 58) ? 58 :    \
        (n) & (1ULL << 57) ? 57 :    \
        (n) & (1ULL << 56) ? 56 :    \
        (n) & (1ULL << 55) ? 55 :    \
        (n) & (1ULL << 54) ? 54 :    \
        (n) & (1ULL << 53) ? 53 :    \
        (n) & (1ULL << 52) ? 52 :    \
        (n) & (1ULL << 51) ? 51 :    \
        (n) & (1ULL << 50) ? 50 :    \
        (n) & (1ULL << 49) ? 49 :    \
        (n) & (1ULL << 48) ? 48 :    \
        (n) & (1ULL << 47) ? 47 :    \
        (n) & (1ULL << 46) ? 46 :    \
        (n) & (1ULL << 45) ? 45 :    \
        (n) & (1ULL << 44) ? 44 :    \
        (n) & (1ULL << 43) ? 43 :    \
        (n) & (1ULL << 42) ? 42 :    \
        (n) & (1ULL << 41) ? 41 :    \
        (n) & (1ULL << 40) ? 40 :    \
        (n) & (1ULL << 39) ? 39 :    \
        (n) & (1ULL << 38) ? 38 :    \
        (n) & (1ULL << 37) ? 37 :    \
        (n) & (1ULL << 36) ? 36 :    \
        (n) & (1ULL << 35) ? 35 :    \
        (n) & (1ULL << 34) ? 34 :    \
        (n) & (1ULL << 33) ? 33 :    \
        (n) & (1ULL << 32) ? 32 :    \
        (n) & (1ULL << 31) ? 31 :    \
        (n) & (1ULL << 30) ? 30 :    \
        (n) & (1ULL << 29) ? 29 :    \
        (n) & (1ULL << 28) ? 28 :    \
        (n) & (1ULL << 27) ? 27 :    \
        (n) & (1ULL << 26) ? 26 :    \
        (n) & (1ULL << 25) ? 25 :    \
        (n) & (1ULL << 24) ? 24 :    \
        (n) & (1ULL << 23) ? 23 :    \
        (n) & (1ULL << 22) ? 22 :    \
        (n) & (1ULL << 21) ? 21 :    \
        (n) & (1ULL << 20) ? 20 :    \
        (n) & (1ULL << 19) ? 19 :    \
        (n) & (1ULL << 18) ? 18 :    \
        (n) & (1ULL << 17) ? 17 :    \
        (n) & (1ULL << 16) ? 16 :    \
        (n) & (1ULL << 15) ? 15 :    \
        (n) & (1ULL << 14) ? 14 :    \
        (n) & (1ULL << 13) ? 13 :    \
        (n) & (1ULL << 12) ? 12 :    \
        (n) & (1ULL << 11) ? 11 :    \
        (n) & (1ULL << 10) ? 10 :    \
        (n) & (1ULL <<  9) ?  9 :    \
        (n) & (1ULL <<  8) ?  8 :    \
        (n) & (1ULL <<  7) ?  7 :    \
        (n) & (1ULL <<  6) ?  6 :    \
        (n) & (1ULL <<  5) ?  5 :    \
        (n) & (1ULL <<  4) ?  4 :    \
        (n) & (1ULL <<  3) ?  3 :    \
        (n) & (1ULL <<  2) ?  2 :    \
        (n) & (1ULL <<  1) ?  1 :    \
        (n) & (1ULL <<  0) ?  0 :    \
        ____ilog2_NaN()            \
                   ) :        \
    (sizeof(n) <= 4) ?            \
    __ilog2_u32(n) :            \
    __ilog2_u64(n)                \
 )

可见如果n不是常数,不能再编译阶段处理的话,这种处理方式不可取,于是有了第二种处理方式。

rounddown_pow_of_two 第二种处理

如果n不是常数,则用汇编处理

/*
 * round up to nearest power of two
 */
static inline __attribute__((const))
unsigned long __roundup_pow_of_two(unsigned long n)
{
    return 1UL << fls_long(n - 1);
}

\kernel\include\linux\bitops.h

static inline unsigned fls_long(unsigned long l)
{
    if (sizeof(l) == 4)
        return fls(l);
    return fls64(l);
}

\kernel\arch\frv\include\asm\bitops.h

/**
 * fls - find last bit set
 * @x: the word to search
 *
 * This is defined the same way as ffs:
 * - return 32..1 to indicate bit 31..0 most significant bit set
 * - return 0 to indicate no bits set
 */
#define fls(x)                        \
({                            \
    int bit;                    \
                            \
    asm("    subcc    %1,gr0,gr0,icc0        \n"    \
        "    ckne    icc0,cc4        \n"    \
        "    cscan.p    %1,gr0,%0    ,cc4,#1    \n"    \
        "    csub    %0,%0,%0    ,cc4,#0    \n"    \
        "   csub    %2,%0,%0    ,cc4,#1    \n"    \
        : "=&r"(bit)                \
        : "r"(x), "r"(32)                \
        : "icc0", "cc4"                \
        );                        \
                            \
    bit;                        \
})

/**
 * fls64 - find last bit set in a 64-bit value
 * @n: the value to search
 *
 * This is defined the same way as ffs:
 * - return 64..1 to indicate bit 63..0 most significant bit set
 * - return 0 to indicate no bits set
 */
static inline __attribute__((const))
int fls64(u64 n)
{
    union {
        u64 ll;
        struct { u32 h, l; };
    } _;
    int bit, x, y;

    _.ll = n;

    asm("    subcc.p        %3,gr0,gr0,icc0        \n"
        "    subcc        %4,gr0,gr0,icc1        \n"
        "    ckne        icc0,cc4        \n"
        "    ckne        icc1,cc5        \n"
        "    norcr        cc4,cc5,cc6        \n"
        "    csub.p        %0,%0,%0    ,cc6,1    \n"
        "    orcr        cc5,cc4,cc4        \n"
        "    andcr        cc4,cc5,cc4        \n"
        "    cscan.p        %3,gr0,%0    ,cc4,0    \n"
        "   setlos        #64,%1            \n"
        "    cscan.p        %4,gr0,%0    ,cc4,1    \n"
        "   setlos        #32,%2            \n"
        "    csub.p        %1,%0,%0    ,cc4,0    \n"
        "    csub        %2,%0,%0    ,cc4,1    \n"
        : "=&r"(bit), "=r"(x), "=r"(y)
        : "0r"(_.h), "r"(_.l)
        : "icc0", "icc1", "cc4", "cc5", "cc6"
        );
    return bit;

}

为了追求效率,可见用心。

QCM2150平台添加at24 eeprom 驱动

kernel/msm-4.9/arch/arm/configs/msm8937-perf_defconfig

 CONFIG_CPUIDINFO_OPS=y
 CONFIG_EPAY_ENABLED=y
 CONFIG_ROOT_DETECTOR=y
+CONFIG_NVMEM=y

kernel/msm-4.9/arch/arm/configs/msm8937_defconfig

 CONFIG_EPAY_ENABLED=y
 CONFIG_CPUIDINFO_OPS=y
 #CONFIG_ROOT_DETECTOR=y
+CONFIG_NVMEM=y

kernel/msm-4.9/arch/arm64/boot/dts/qcom/qm215-qrd.dtsi

&i2c_5 { /* BLSP2 QUP1 (NFC) */
    #address-cells = <1>;
    #size-cells = <0>;

    status = "ok";

......

......

//@qq.com: add at24cs0x eeprom;
    at24@50 {
        compatible = "24cs02";
        reg = <0x50>;
        vboot-offset = <0x9e>;
        batt-type-offset = <0x18>;
    };
    //@qq.com: add end

......

};

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值