Zynq-Linux移植學習筆記之13-i2c驅動配置
1、 背景介紹
板子上通過I2C匯流排與zynq相連的是三片1848
如上圖所示,zynq通過I2C匯流排與3片CPS-1848交換晶片相連,3片1848晶片的I2C地址分別為2,4,8.
目前zynq上linux I2C驅動採用的是i2c-cadence(drivers/i2c/buses),對應於i2c驅動中的bus driver(匯流排驅動,也稱為介面卡驅動)。需要實現的是i2c驅動中的裝置驅動,類似於eeprom驅動(drivers/misc/eeprom)。
2、devicetree配置
706的devicetree中關於I2C的部分如下:
[email protected] {
compatible = "cdns,i2c-r1p10";
status = "okay";
clocks = <0x1 0x26>;
interrupt-parent = <0x3>;
interrupts = <0x0 0x19 0x4>;
reg = <0xe0004000 0x1000>;
#address-cells = <0x1>;
#size-cells = <0x0>;
clock-frequency = <0x61a80>;
pinctrl-names = "default";
pinctrl-0 = <0xb>;
[email protected] {
compatible = "nxp,pca9548";
#address-cells = <0x1>;
#size-cells = <0x0>;
reg = <0x74>;
[email protected] {
#address-cells = <0x1>;
#size-cells = <0x0>;
reg = <0x0>;
[email protected] {
#clock-cells = <0x0>;
compatible = "silabs,si570";
temperature-stability = <0x32>;
reg = <0x5d>;
factory-fout = <0x9502f90>;
clock-frequency = <0x8d9ee20>;
};
};
[email protected] {
#address-cells = <0x1>;
#size-cells = <0x0>;
reg = <0x1>;
[email protected] {
compatible = "adi,adv7511";
reg = <0x39>;
adi,input-depth = <0x8>;
adi,input-colorspace = "yuv422";
adi,input-clock = "1x";
adi,input-style = <0x3>;
adi,input-justification = "evenly";
};
};
[email protected] {
#address-cells = <0x1>;
#size-cells = <0x0>;
reg = <0x2>;
[email protected] {
compatible = "at,24c08";
reg = <0x54>;
};
};
[email protected] {
#address-cells = <0x1>;
#size-cells = <0x0>;
reg = <0x3>;
[email protected] {
compatible = "ti,tca6416";
reg = <0x21>;
gpio-controller;
#gpio-cells = <0x2>;
};
};
[email protected] {
#address-cells = <0x1>;
#size-cells = <0x0>;
reg = <0x4>;
[email protected] {
compatible = "nxp,pcf8563";
reg = <0x51>;
};
};
[email protected] {
#address-cells = <0x1>;
#size-cells = <0x0>;
reg = <0x7>;
[email protected] {
compatible = "ti,ucd90120";
reg = <0x65>;
};
};
};
};
參考706中的devicetree,通過i2c控制三片1848, devicetree格式修改如下:
[email protected] {
compatible = "cdns,i2c-r1p10";
status = "okay";
clocks = <0x1 0x26>;
interrupt-parent = <0x3>;
interrupts = <0x0 0x19 0x4>;
reg = <0xe0004000 0x1000>;
#address-cells = <0x1>;
#size-cells = <0x0>;
clock-frequency = <0x61a80>;
[email protected] {
compatible = "cps1848";
reg = <0x2>;
};
[email protected] {
compatible = "cps1848";
reg = <0x4>;
};
[email protected] {
compatible = "cps1848";
reg = <0x8>;
};
};
如上就配置好了三個地址分別為2,4,8的裝置,暫時給它們起名叫cps1848.
3、 kernel配置
kernel中xilinx已經實現了i2c的bus driver,我們只需要實現device driver。由於eeprom為標準i2c裝置,可以將eeprom為模板實現1848的裝置驅動。修改過程中注意匹配裝置的name和驅動id_table中的name,裝置name就是devicetree中的cps1848.
實現後的cps1848程式碼如下:
/*
* CPS1848 bus driver
*
* Copyright (C) 2014 CGT Corp.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#define DEBUG
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/serial_core.h>
/* Each client has this additional data */
#define USER_EEPROM_SIZE 0xFFFF48
#define USER_XFER_MAX_COUNT 0x8
/* Addresses to scan */
static const unsigned short cps1848_i2c[] = { 0x3, I2C_CLIENT_END };
static unsigned read_timeout = 25;
module_param(read_timeout, uint, 0);
MODULE_PARM_DESC(read_timeout, "Time (in ms) to try reads (default 25)");
static unsigned write_timeout = 25;
module_param(write_timeout, uint, 0);
MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
struct cps1848_data {
struct mutex lock;
u8 *data;
};
static ssize_t cps1848_eeprom_read( struct i2c_client *client,
char *buf, unsigned offset, size_t count)
{
struct i2c_msg msg[2];
u8 msgbuf[4];
unsigned long timeout, transfer_time;
int status;
memset(msg, 0, sizeof(msg));
msgbuf[0] = (u8)((offset >> 18) & 0x3f);
msgbuf[1] = (u8)((offset >> 10) & 0xff);
msgbuf[2] = (u8)((offset >> 2) & 0xff);
msg[0].addr = client->addr;
msg[0].buf = msgbuf;
msg[0].len = 3;
msg[1].addr = client->addr;
msg[1].flags = I2C_M_RD;
msg[1].buf = buf;
msg[1].len = count;
/*
* Reads fail if the previous write didn't complete yet. We may
* loop a few times until this one succeeds, waiting at least
* long enough for one entire page write to work.
*/
timeout = jiffies + msecs_to_jiffies(read_timeout);
do {
transfer_time = jiffies;
status = i2c_transfer(client->adapter, msg, 2);
if (status == 2)
status = count;
dev_dbg(&client->dev, "read %[email protected]%lx --> %d (%ld)\n",
count, (unsigned long)offset, status, jiffies);
if (status == count)
return count;
/* REVISIT: at HZ=100, this is sloooow */
msleep(1);
} while (time_before(transfer_time, timeout));
return -ETIMEDOUT;
}
static ssize_t cps1848_read(struct file *filp, struct kobject *kobj,
struct bin_attribute *bin_attr,
char *buf, loff_t offset, size_t count)
{
struct i2c_client *client = kobj_to_i2c_client(kobj);
struct cps1848_data *data = i2c_get_clientdata(client);
ssize_t retval = 0;
if (offset > USER_EEPROM_SIZE)
return 0;
if (offset + count > USER_EEPROM_SIZE)
count = USER_EEPROM_SIZE - offset;
mutex_lock(&data->lock);
dev_dbg(&client->dev, "cps1848 start read %[email protected]%lx ..\n", count, (unsigned long)offset);
while (count > 0) {
ssize_t status = count>USER_XFER_MAX_COUNT?USER_XFER_MAX_COUNT:count;
status = cps1848_eeprom_read(client, buf, offset, status);
if (status <= 0) {
if (retval == 0)
retval = status;
break;
}
buf += status;
offset += status;
count -= status;
retval += status;
}
dev_dbg(&client->dev, "cps1848 end read %[email protected]%lx !\n", retval, (unsigned long)offset);
mutex_unlock(&data->lock);
return retval;
}
static ssize_t cps1848_eeprom_write(
struct i2c_client *client,
struct cps1848_data *data,
char *buf, unsigned offset, size_t count)
{
struct i2c_msg msg[1];
u8 *msgbuf;
unsigned long timeout, transfer_time;
int status;
memset(msg, 0, sizeof(msg));
msgbuf = data->data;
msgbuf[0] = (u8)((offset >> 18) & 0x3f);
msgbuf[1] = (u8)((offset >> 10) & 0xff);
msgbuf[2] = (u8)((offset >> 2) & 0xff);
memcpy(msgbuf+3, buf, count);
msg[0].addr = client->addr;
msg[0].buf = msgbuf;
msg[0].len = 3 + count;
/*
* Reads fail if the previous write didn't complete yet. We may
* loop a few times until this one succeeds, waiting at least
* long enough for one entire page write to work.
*/
timeout = jiffies + msecs_to_jiffies(write_timeout);
do {
transfer_time = jiffies;
status = i2c_transfer(client->adapter, msg, 1);
if (status == 1)
status = count;
dev_dbg(&client->dev, "write %[email protected]%lx --> %d (%ld)\n",
count, (unsigned long)offset, status, jiffies);
if (status == count)
return count;
/* REVISIT: at HZ=100, this is sloooow */
msleep(1);
} while (time_before(transfer_time, timeout));
return -ETIMEDOUT;
}
static ssize_t cps1848_write(struct file *filp, struct kobject *kobj,
struct bin_attribute *bin_attr,
char *buf, loff_t offset, size_t count)
{
struct i2c_client *client = kobj_to_i2c_client(kobj);
struct cps1848_data *data = i2c_get_clientdata(client);
ssize_t retval = 0;
if (offset > USER_EEPROM_SIZE)
return 0;
if (offset + count > USER_EEPROM_SIZE)
count = USER_EEPROM_SIZE - offset;
mutex_lock(&data->lock);
dev_dbg(&client->dev, "cps1848 start write %[email protected]%lx ..\n", count, (unsigned long)offset);
while (count > 0) {
ssize_t status = count>USER_XFER_MAX_COUNT?USER_XFER_MAX_COUNT:count;
status = cps1848_eeprom_write(client, data, buf, offset, status);
if (status <= 0) {
if (retval == 0)
retval = status;
break;
}
buf += status;
offset += status;
count -= status;
retval += status;
}
dev_dbg(&client->dev, "cps1848 end write %[email protected]%lx !\n", retval, (unsigned long)offset);
mutex_unlock(&data->lock);
return retval;
}
static struct bin_attribute user_eeprom_attr = {
.attr = {
.name = "eeprom",
.mode = (S_IRUSR | S_IWUSR),
},
.size = USER_EEPROM_SIZE,
.read = cps1848_read,
.write = cps1848_write,
};
/* Return 0 if detection is successful, -ENODEV otherwise */
static int cps1848_detect(struct i2c_client *client, struct i2c_board_info *info)
{
struct i2c_adapter *adapter = client->adapter;
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
dev_dbg(&client->dev, "cps1848 detect error for BYTE access !\n");
return -ENODEV;
}
strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
return 0;
}
static int cps1848_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct i2c_adapter *adapter = client->adapter;
struct cps1848_data *data;
int err ;
dev_notice(&client->dev, "CPS1848 driver: " __DATE__ " " __TIME__ " \n" );
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
dev_err(&client->dev, "CPS1848 driver: BYTE DATA not supported! \n" );
return -ENODEV;
}
if (!(data = kzalloc(sizeof(struct cps1848_data), GFP_KERNEL))) {
dev_err(&client->dev, "CPS1848 driver: Memory alloc error ! \n" );
return -ENOMEM;
}
/* alloc buffer */
data->data = devm_kzalloc(&client->dev, USER_XFER_MAX_COUNT + 8, GFP_KERNEL);
if (!data->data) {
dev_err(&client->dev, "CPS1848 driver: Memory alloc error ! \n" );
err = -ENOMEM;
goto exit_kfree;
}
/* Init real i2c_client */
i2c_set_clientdata(client, data);
mutex_init(&data->lock);
err = sysfs_create_bin_file(&client->dev.kobj, &user_eeprom_attr);
if (err) {
dev_err(&client->dev, "CPS1848 driver: sysfs create error ! \n" );
goto exit_kfree;
}
return 0;
exit_kfree:
if(data->data)
kfree(data->data);
kfree(data);
return err;
}
static int cps1848_remove(struct i2c_client *client)
{
struct cps1848_data *data = i2c_get_clientdata(client);
sysfs_remove_bin_file(&client->dev.kobj, &user_eeprom_attr);
if(data->data)
kfree(data->data);
kfree(data);
return 0;
}
static const struct i2c_device_id cps1848_id[] = {
{ "cps1848", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, cps1848_id);
static struct i2c_driver cps1848_driver = {
.driver = {
.name = "cps1848",
},
.probe = cps1848_probe,
.remove = cps1848_remove,
.id_table = cps1848_id,
.class = I2C_CLASS_SPD,
.detect = cps1848_detect,
.address_list = cps1848_i2c,
};
module_i2c_driver(cps1848_driver);
MODULE_AUTHOR("RobinLee");
MODULE_DESCRIPTION("CPS1848 driver");
MODULE_LICENSE("GPL");
將該程式碼命名為i2c-1848放在drivers/i2c/muxes下
修改muxes的Kconfig檔案以及Makefile檔案,加入針對1848的配置選項
在編譯核心選單中能看到新增加配置選項
選擇以後進行編譯,這樣kernel配置就完成了。
4、 i2c驅動測試
系統上電啟動,載入devicetree,kernel,uramdisk,能看到kernel啟動時已經載入了cps1848驅動。
上圖中列出了系統檢測到的三個i2c裝置,名稱為cps1848,地址為0002,0004,0008.
為了針對cps1848進行測試,首先要知道三片1848在系統中的位置(在linux中所有裝置都是以檔案形式掛載)。最終在sys/class/i2c-dev/i2c-0/device下找到了三個裝置。
app-cps1848/cps1848/app/cps1848.c
編譯測試程,得到cps1848可執行檔案
將cps1848放進uramdisk中,參考:rootfs修改
產生新的uramdisk.image.gz,重新載入linux
在/usr/sbin下執行cps1848,進入cps1848控制介面
輸入get 0,獲取1848地址為0暫存器的值,該值為0x38007403
檢視cps1848的datasheet,發現值確實是這個,大小端顛倒一下。
重複上述測試過程,再測試0002和0004位置的1848,最終實現對1848驅動的測試。