2020/11/6

add read/write function in i2c_client driver function

snd_soc_codec_driver
裡面有:
  • write : i2c_write
  • read : i2c_read
如果沒有指定,就會用 platform 內的。

所以如果想要 debug snd_soc_code driver,知道該 driver 對 i2c 的動作。
可以自己寫 i2c_read, i2c_write 放進去。
static unsigned int my_i2c_read(struct snd_soc_codec *codec, unsigned int reg)
{
    struct my_priv *mypriv = snd_soc_codec_get_drvdata(codec);
    int ret = -1; 
    unsigned char tx[1], rx[1];

    struct i2c_msg xfer[2];
    struct i2c_client *client = mypriv->i2c;

    tx[0] = reg;
    rx[0] = 0;

    /* Write register */
    xfer[0].addr = client->addr;
    xfer[0].flags = 0;
    xfer[0].len = 1;
    xfer[0].buf = tx;

    /* Read data */
    xfer[1].addr = client->addr;
    xfer[1].flags = I2C_M_RD;
    xfer[1].len = 1;
    xfer[1].buf = rx;

    ret = i2c_transfer(client->adapter, xfer, 2);

    if (ret != 2)
        printk("\t%s error ret = %d\n", __func__, ret);

    return (unsigned int)rx[0];

}

static int my_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
                unsigned int value)
{
    struct my_priv *mypriv = snd_soc_codec_get_drvdata(codec);
    int ret = -1;
    unsigned char tx[2];
    size_t  len = 2;

    if(reg==1)
        value |= 0x02; // don't power off spk 

    tx[0] = reg;
    tx[1] = value;

    printk("\t %s: (addr,data)=(%x, %x)\n", __func__, reg, value);

    ret = i2c_master_send(mypriv->i2c, tx, len);

    if (ret != len)
        return -EIO;

    return 0;
}
但是要struct my_priv 要把 probe 時,register 的 i2c client 田進變數
struct my_priv {
    struct i2c_client *i2c;
     ...
然後 module probe function...
static int my_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
{
    struct my_priv *mypriv;
    mypriv = devm_kzalloc(&i2c->dev. sizeof(struct my_priv), GFP_KERNEL);
    
    i2c_set_client_data(i2c, mypriv);
    mypriv->i2c = i2c;
    ...
這樣,這個 driver call snd_soc_read/write 時,就會 call 到 自己的 i2c_read/write function.
然後舊可以加 debug message...
但是這樣舊沒用到 regmap,所以 sys/kernel/debug/regmap/XXXX/registers 的內容不會顯示。

沒有留言:

張貼留言