u014032613
xiefeng272
2018-09-07 07:00
采纳率: 66.7%
浏览 1.6k
已采纳

读写i2c设备寄存器返回-11

 #include <linux/module.h>
#include <linux/i2c.h>
#include <linux/version.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h> 
#include <linux/delay.h> 

#define MFI_I2C_NAME  "mfi_i2c_adapter"
/*Register Address*/
#define MFI_DEVICE_VERSION                  0x00     /* Device version */
#define MFI_FIRMWARE_VERSION                0x01     /* Firmware version */
/*Register Address*/

static struct class *mfi_class = NULL;
struct i2c_client* mfi_client;
static ssize_t mfi_read_register_show(struct class *cls, struct class_attribute *attr, char *_buf)
{
     int ret=i2c_smbus_read_byte_data(mfi_client,MFI_DEVICE_VERSION); 
     pr_info("test rewad value is : %d-------0x%x\n",ret,ret);
     ret = i2c_smbus_write_byte_data(mfi_client,MFI_SIGNATURE_DATA,1);
     pr_info("test write ret value is : %d-------0x%x\n",ret,ret);
     return sprintf(_buf,"%d\n",ret);
}

static CLASS_ATTR(mfi_read_register, 0660, mfi_read_register_show, NULL);

int mfi_calss_init(void)
{
     int ret ;
     mfi_class = class_create(THIS_MODULE, "mfi_test");
     if (IS_ERR(mfi_class)) 
     {
         pr_err("Create class mfi_class failed.\n");
         return -ENOMEM;
     }

     ret = class_create_file(mfi_class, &class_attr_mfi_read_register);

     if (ret)
     {
         pr_err("Fail to class mfi_class.\n");
     }

     return ret;
}

void mfi_calss_exit(void)
{
     class_remove_file(mfi_class, &class_attr_mfi_read_register);
     class_destroy(mfi_class);
}

static struct of_device_id mfi_i2c_of_match[] = {
    { .compatible = "fsl,mfi341s2313",}, { }
};

static int mfi_i2c_probe(struct i2c_client *client,
    const struct i2c_device_id *i2c_id)
{
    pr_info("IIC address is 0x%x\n",client->addr);
    mfi_client=client;
    struct device *dev = &client->dev;
    const struct of_device_id *match;
    struct device_node *mfi_node;
    int rst_gpio,rst_gpio_value;
    int test;

    if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
        dev_err(dev, "I2C functionality not Supported\n");
        return -EIO;
    }
    match = of_match_device(of_match_ptr(mfi_i2c_of_match), dev);
    if (!match) {
        pr_info("device does not match the id\n");
        return -EIO;
    }
    mfi_node=of_find_matching_node(NULL,mfi_i2c_of_match);
    rst_gpio=of_get_named_gpio(mfi_node,"rst-gpios",0);
    if (!gpio_is_valid(rst_gpio)) 
        pr_err("no rst pin available\n");
    else{
        pr_info("the rst_gpio number is : %d\n",rst_gpio);
        struct gpio_desc *rst_gpio_desc = gpio_to_desc(rst_gpio);
        rst_gpio_value=gpiod_get_value(rst_gpio_desc);
        pr_info("default rst_gpio_value value is : %d\n",rst_gpio_value);

        gpio_request(rst_gpio, "rst_gpio");
        gpio_direction_output(rst_gpio, 0);
        rst_gpio_value=gpiod_get_value(rst_gpio_desc);
        pr_info("active rst_gpio_value value is : %d\n",rst_gpio_value);

        gpio_direction_output(rst_gpio, 1);
        msleep(30);
        rst_gpio_value=gpiod_get_value(rst_gpio_desc);
        pr_info("after active rst_gpio_value value is : %d\n",rst_gpio_value);

        gpio_free(rst_gpio);

        test=i2c_smbus_read_byte_data(client,MFI_FIRMWARE_VERSION);

        if(test!=0)
            pr_info("the MFI_FIRMWARE_VERSION register value is :%d----- 0x%x\n",test,test);
        else
            pr_info("failed to get the MFI_FIRMWARE_VERSION register value\n");

        test=i2c_smbus_read_byte_data(client,MFI_DEVICE_VERSION);
        pr_info("the MFI_DEVICE_VERSION register value is :%d----- 0x%x\n",test,test);

    }

    mfi_calss_init();


    return 0;
}

static int mfi_i2c_remove(struct i2c_client *client)
{
    mfi_calss_exit();
    return 0;
}

static const struct i2c_device_id mfi_i2c_id[] = {
    { MFI_I2C_NAME, 0 },  
    { },
};
MODULE_DEVICE_TABLE(i2c, mfi_i2c_id);

static struct i2c_driver mfi_i2c_driver = {
    .driver = {
        .name = MFI_I2C_NAME,
        .owner = THIS_MODULE,
        .of_match_table = mfi_i2c_of_match,
    },
    .probe = mfi_i2c_probe,
    .remove = mfi_i2c_remove,
    .id_table = mfi_i2c_id,
};

static int __init mfi_i2c_init(void)
{
    int rc = i2c_add_driver(&mfi_i2c_driver);

    pr_info("%s: MFI I2C Driver (Built) rc=%d\n",__func__, rc);

    return rc;
}
module_init(mfi_i2c_init);

static void __exit mfi_i2c_exit(void)
{
    i2c_del_driver(&mfi_i2c_driver);
}
module_exit(mfi_i2c_exit);

代码如上,最后输出的日志如下
IIC address is 0x11
the rst_gpio number is : 132
default rst_gpio_value value is : 1
active rst_gpio_value value is : 0
after active rst_gpio_value value is : 1
enter i2c_smbus_xfer ......3358,adapter->algo->smbus_xfer=0x0
enter i2c_smbus_xfer_emulated.......3160
enter i2c_transfer.......2557
adap->algo->master_xfer=0xc0871354,num=2
enter i2c_imx_xfer......885
status=-11
the MFI_FIRMWARE_VERSION register value is :-11----- 0xfffffff5
enter i2c_smbus_xfer ......3358,adapter->algo->smbus_xfer=0x0
enter i2c_smbus_xfer_emulated.......3160
enter i2c_transfer.......2557
adap->algo->master_xfer=0xc0871354,num=2
enter i2c_imx_xfer......885
status=-11
the MFI_DEVICE_VERSION register value is :-11----- 0xfffffff5
mfi_i2c_init: MFI I2C Driver (Built) rc=0
就是读不到寄存器的数值,clk,sda有波形。。。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • zqbnqsdsmd
    zqbnqsdsmd 2018-09-07 16:01
    已采纳
    点赞 评论
  • u014032613
    xiefeng272 2018-09-13 02:21

    最后发现是时钟频率配置不对,认真查看相关模块数据手册

    点赞 评论

相关推荐