I2C驱动分为3层:
1. I2C BUS驱动

2. I2C核心层(协议相关)

3. I2C chip

I2C BUS 驱动分析:drivers\i2c\busses\i2c-s3c2410.c
1. 分配一个i2c_adapter
2. 设置: 实现发出I2C命令、数据的函数
3. 注册:i2c_add_adapter
4. 硬件相关的设置

I2C chip驱动分析:drivers\i2c\chips\ds1337.c
ds1337_init
    i2c_add_driver(&ds1337_driver);  /* 添加driver */
1. 分配i2c_driver
2. 设置: i2c_driver里有一个attach_adapter,用来识别是否存在I2C设备
3. 注册: i2c_add_driver

static struct i2c_driver ds1337_driver = {
    .driver = {
        .name   = "ds1337",
    },
    .attach_adapter = ds1337_attach_adapter,
    .detach_client  = ds1337_detach_client,
    .command    = ds1337_command,
};

drivers\i2c\busses\i2c-s3c2410.c:

i2c_add_adapter => i2c_register_adapter => i2c_driver.attach_adapter
   
    i2c_register_driver => i2c_driver.attach_adapter
       
       
       
       
            i2c_driver.attach_adapter  //ds1337_attach_adapter
                i2c_probe(adapter, &addr_data, ds1337_detect);
                    ds1337_detect
                        i2c_attach_client  /* 添加设备 */

分析drivers\i2c\chips\ds1337.c
ds1337_init
    i2c_add_driver(&ds1337_driver);
        i2c_register_driver
            // 把i2c_drvier放入键表
           
            // 对每一个adapter(发出I2C信息的能力),调用attach_adapter
            list_for_each_entry(adapter, &adapters, list) {
                driver->attach_adapter(adapter);
                        ds1337_attach_adapter // 识别I2C设备
                       
                            // i2c-core.c
                            i2c_probe(adapter, &addr_data, ds1337_detect);
                                i2c_probe_address(adapter, addr, kind, found_proc)
                                    err = found_proc(adapter, addr, kind);
                                            ds1337_detect(adapter, 0x68, xxxxx)
                                                // 分配一个i2c_client
                                                // 设置
                                               
                                                // 读I2C设备,确定该设备确实存在
                                                ds1337_read(new_client, DS1337_REG_STATUS, &data)
                                                    i2c_smbus_read_byte_data
                                                        i2c_smbus_xfer
                                                            // s3c24xx_i2c_algorithm
                                                            adapter->algo->smbus_xfer

/*
 *  linux/drivers/i2c/chips/ds1337.c
 *
 *  Copyright (C) 2005 James Chapman <jchapman@katalix.com>
 *
 * based on linux/drivers/acorn/char/pcf8583.c
 *  Copyright (C) 2000 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Driver for Dallas Semiconductor DS1337 and DS1339 real time clock chip
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/string.h>
#include <linux/rtc.h>  /* get the user-level API */
#include <linux/bcd.h>
#include <linux/list.h>

/* Device registers */
#define DS1337_REG_HOUR  2
#define DS1337_REG_DAY  3
#define DS1337_REG_DATE  4
#define DS1337_REG_MONTH 5
#define DS1337_REG_CONTROL 14
#define DS1337_REG_STATUS 15

/* FIXME - how do we export these interface constants? */
#define DS1337_GET_DATE  0
#define DS1337_SET_DATE  1

/*
 * Functions declaration
 */
static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };

I2C_CLIENT_INSMOD_1(ds1337);

static int ds1337_attach_adapter(struct i2c_adapter *adapter);
static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind);
static void ds1337_init_client(struct i2c_client *client);
static int ds1337_detach_client(struct i2c_client *client);
static int ds1337_command(struct i2c_client *client, unsigned int cmd,
     void *arg);

/*
 * Driver data (common to all clients)
 */
static struct i2c_driver ds1337_driver = {
 .driver = {
  .name = "ds1337",
 },
 .attach_adapter = ds1337_attach_adapter,
 .detach_client = ds1337_detach_client,
 .command = ds1337_command,
};

/*
 * Client data (each client gets its own)
 */
struct ds1337_data {
 struct i2c_client client;
 struct list_head list;
};

/*
 * Internal variables
 */
static LIST_HEAD(ds1337_clients);

static inline int ds1337_read(struct i2c_client *client, u8 reg, u8 *value)
{
 s32 tmp = i2c_smbus_read_byte_data(client, reg);

if (tmp < 0)
  return -EIO;

*value = tmp;

return 0;
}

/*
 * Chip access functions
 */
static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt)
{
 int result;
 u8 buf[7];
 u8 val;
 struct i2c_msg msg[2];
 u8 offs = 0;

if (!dt) {
  dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
  return -EINVAL;
 }

msg[0].addr = client->addr;
 msg[0].flags = 0;
 msg[0].len = 1;
 msg[0].buf = &offs;

msg[1].addr = client->addr;
 msg[1].flags = I2C_M_RD;
 msg[1].len = sizeof(buf);
 msg[1].buf = &buf[0];

result = i2c_transfer(client->adapter, msg, 2);

dev_dbg(&client->dev, "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n",
  __FUNCTION__, result, buf[0], buf[1], buf[2], buf[3],
  buf[4], buf[5], buf[6]);

if (result == 2) {
  dt->tm_sec = BCD2BIN(buf[0]);
  dt->tm_min = BCD2BIN(buf[1]);
  val = buf[2] & 0x3f;
  dt->tm_hour = BCD2BIN(val);
  dt->tm_wday = BCD2BIN(buf[3]) - 1;
  dt->tm_mday = BCD2BIN(buf[4]);
  val = buf[5] & 0x7f;
  dt->tm_mon = BCD2BIN(val) - 1;
  dt->tm_year = BCD2BIN(buf[6]);
  if (buf[5] & 0x80)
   dt->tm_year += 100;

dev_dbg(&client->dev, "%s: secs=%d, mins=%d, "
   "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
   __FUNCTION__, dt->tm_sec, dt->tm_min,
   dt->tm_hour, dt->tm_mday,
   dt->tm_mon, dt->tm_year, dt->tm_wday);

return 0;
 }

dev_err(&client->dev, "error reading data! %d\n", result);
 return -EIO;
}

static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt)
{
 int result;
 u8 buf[8];
 u8 val;
 struct i2c_msg msg[1];

if (!dt) {
  dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
  return -EINVAL;
 }

dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  "mday=%d, mon=%d, year=%d, wday=%d\n", __FUNCTION__,
  dt->tm_sec, dt->tm_min, dt->tm_hour,
  dt->tm_mday, dt->tm_mon, dt->tm_year, dt->tm_wday);

buf[0] = 0;  /* reg offset */
 buf[1] = BIN2BCD(dt->tm_sec);
 buf[2] = BIN2BCD(dt->tm_min);
 buf[3] = BIN2BCD(dt->tm_hour);
 buf[4] = BIN2BCD(dt->tm_wday + 1);
 buf[5] = BIN2BCD(dt->tm_mday);
 buf[6] = BIN2BCD(dt->tm_mon + 1);
 val = dt->tm_year;
 if (val >= 100) {
  val -= 100;
  buf[6] |= (1 << 7);
 }
 buf[7] = BIN2BCD(val);

msg[0].addr = client->addr;
 msg[0].flags = 0;
 msg[0].len = sizeof(buf);
 msg[0].buf = &buf[0];

result = i2c_transfer(client->adapter, msg, 1);
 if (result == 1)
  return 0;

dev_err(&client->dev, "error writing data! %d\n", result);
 return -EIO;
}

static int ds1337_command(struct i2c_client *client, unsigned int cmd,
     void *arg)
{
 dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);

switch (cmd) {
 case DS1337_GET_DATE:
  return ds1337_get_datetime(client, arg);

case DS1337_SET_DATE:
  return ds1337_set_datetime(client, arg);

default:
  return -EINVAL;
 }
}

/*
 * Public API for access to specific device. Useful for low-level
 * RTC access from kernel code.
 */
int ds1337_do_command(int bus, int cmd, void *arg)
{
 struct list_head *walk;
 struct list_head *tmp;
 struct ds1337_data *data;

list_for_each_safe(walk, tmp, &ds1337_clients) {
  data = list_entry(walk, struct ds1337_data, list);
  if (data->client.adapter->nr == bus)
   return ds1337_command(&data->client, cmd, arg);
 }

return -ENODEV;
}

static int ds1337_attach_adapter(struct i2c_adapter *adapter)
{
 return i2c_probe(adapter, &addr_data, ds1337_detect);
}

/*
 * The following function does more than just detection. If detection
 * succeeds, it also registers the new chip.
 */
static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind)
{
 struct i2c_client *new_client;
 struct ds1337_data *data;
 int err = 0;
 const char *name = "";

if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
         I2C_FUNC_I2C))
  goto exit;

if (!(data = kzalloc(sizeof(struct ds1337_data), GFP_KERNEL))) {
  err = -ENOMEM;
  goto exit;
 }
 INIT_LIST_HEAD(&data->list);

/* The common I2C client data is placed right before the
  * DS1337-specific data.
  */
 new_client = &data->client;
 i2c_set_clientdata(new_client, data);
 new_client->addr = address;
 new_client->adapter = adapter;
 new_client->driver = &ds1337_driver;
 new_client->flags = 0;

/*
  * Now we do the remaining detection. A negative kind means that
  * the driver was loaded with no force parameter (default), so we
  * must both detect and identify the chip. A zero kind means that
  * the driver was loaded with the force parameter, the detection
  * step shall be skipped. A positive kind means that the driver
  * was loaded with the force parameter and a given kind of chip is
  * requested, so both the detection and the identification steps
  * are skipped.
  *
  * For detection, we read registers that are most likely to cause
  * detection failure, i.e. those that have more bits with fixed
  * or reserved values.
  */

/* Default to an DS1337 if forced */
 if (kind == 0)
  kind = ds1337;

if (kind < 0) {  /* detection and identification */
  u8 data;

/* Check that status register bits 6-2 are zero */
  if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) ||
      (data & 0x7c))
   goto exit_free;

/* Check for a valid day register value */
  if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) ||
      (data == 0) || (data & 0xf8))
   goto exit_free;

/* Check for a valid date register value */
  if ((ds1337_read(new_client, DS1337_REG_DATE, &data) < 0) ||
      (data == 0) || (data & 0xc0) || ((data & 0x0f) > 9) ||
      (data >= 0x32))
   goto exit_free;

/* Check for a valid month register value */
  if ((ds1337_read(new_client, DS1337_REG_MONTH, &data) < 0) ||
      (data == 0) || (data & 0x60) || ((data & 0x0f) > 9) ||
      ((data >= 0x13) && (data <= 0x19)))
   goto exit_free;

/* Check that control register bits 6-5 are zero */
  if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) ||
      (data & 0x60))
   goto exit_free;

kind = ds1337;
 }

if (kind == ds1337)
  name = "ds1337";

/* We can fill in the remaining client fields */
 strlcpy(new_client->name, name, I2C_NAME_SIZE);

/* Tell the I2C layer a new client has arrived */
 if ((err = i2c_attach_client(new_client)))
  goto exit_free;

/* Initialize the DS1337 chip */
 ds1337_init_client(new_client);

/* Add client to local list */
 list_add(&data->list, &ds1337_clients);

return 0;

exit_free:
 kfree(data);
exit:
 return err;
}

static void ds1337_init_client(struct i2c_client *client)
{
 u8 status, control;

/* On some boards, the RTC isn't configured by boot firmware.
  * Handle that case by starting/configuring the RTC now.
  */
 status = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);

if ((status & 0x80) || (control & 0x80)) {
  /* RTC not running */
  u8 buf[1+16]; /* First byte is interpreted as address */
  struct i2c_msg msg[1];

dev_dbg(&client->dev, "%s: RTC not running!\n", __FUNCTION__);

/* Initialize all, including STATUS and CONTROL to zero */
  memset(buf, 0, sizeof(buf));

/* Write valid values in the date/time registers */
  buf[1+DS1337_REG_DAY] = 1;
  buf[1+DS1337_REG_DATE] = 1;
  buf[1+DS1337_REG_MONTH] = 1;

msg[0].addr = client->addr;
  msg[0].flags = 0;
  msg[0].len = sizeof(buf);
  msg[0].buf = &buf[0];

i2c_transfer(client->adapter, msg, 1);
 } else {
  /* Running: ensure that device is set in 24-hour mode */
  s32 val;

val = i2c_smbus_read_byte_data(client, DS1337_REG_HOUR);
  if ((val >= 0) && (val & (1 << 6)))
   i2c_smbus_write_byte_data(client, DS1337_REG_HOUR,
        val & 0x3f);
 }
}

static int ds1337_detach_client(struct i2c_client *client)
{
 int err;
 struct ds1337_data *data = i2c_get_clientdata(client);

if ((err = i2c_detach_client(client)))
  return err;

list_del(&data->list);
 kfree(data);
 return 0;
}

static int __init ds1337_init(void)
{
 return i2c_add_driver(&ds1337_driver);
}

static void __exit ds1337_exit(void)
{
 i2c_del_driver(&ds1337_driver);
}

MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
MODULE_DESCRIPTION("DS1337 RTC driver");
MODULE_LICENSE("GPL");

EXPORT_SYMBOL_GPL(ds1337_do_command);

module_init(ds1337_init);
module_exit(ds1337_exit);

i2c设备驱动(ds1337实例:内核代码)相关推荐

  1. Linux I2C设备驱动编写(二)

    I2C对外API I2C client的注册 i2c_register_board_info具体实现 i2c_new_device I2C driver 关于I2C设备驱动的小总结 I2C adapt ...

  2. i2c设备驱动实例 ds1307为例

    i2c设备驱动实例 ds1307为例 http://blog.csdn.net/airk000/article/details/21345457 http://blog.csdn.net/creazy ...

  3. Linux 设备驱动篇之I2c设备驱动

    ******************************************************************************************** 装载声明:希望 ...

  4. Linux设备驱动篇——[I2C设备驱动-1]

    Linux 设备驱动篇之I2c设备驱动 fulinux 一.I2C驱动体系 虽然I2C硬件体系结构和协议都很容易理解,但是Linux I2C驱动体系结构却有相当的复杂度,它主要由3部分组成,即I2C设 ...

  5. linux下i2c设备驱动程序,Linux I2C 设备驱动

    I2C 设备驱动要使用 i2c_driver 和 i2c_client 数据结构并填充其中的成员函数.i2c_client 一般被包含在设备的私有信息结构体yyy_data 中,而 i2c_drive ...

  6. 用户空间访问I2C设备驱动

    2012-01-11 15:33:43 标签:Linux I2C 字符设备 设备驱动 用户空间 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. ...

  7. 手把手教你写Linux I2C设备驱动

    手把手教你写Linux I2C设备驱动 标签:Linux 设备 驱动 详解 i2c 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http:/ ...

  8. Linux的I2C 设备驱动 -- mini2440 上i2c接口触摸屏驱动

    本篇记录在友善之臂 mini2440 平台上挂载I2C接口触摸屏的驱动开发过程. 内核版本linux-2.6.32.2, 平台是ARM9 S3C2440+I2C接口的触摸屏 如上篇 Linux的I2C ...

  9. linux探测i2c设备连接状态,手把手教你写Linux I2C设备驱动

    Linux I2C驱动是嵌入式Linux驱动开发人员经常需要编写的一种驱动,因为凡是系统中使用到的I2C设备,几乎都需要编写相应的I2C驱动去配置和控制它,例如 RTC实时时钟芯片.音视频采集芯片.音 ...

  10. 手把手教你写Linux I2C设备驱动 tvp5158

    Linux I2C驱动是嵌入式Linux驱动开发人员经常需要编写的一种驱动,因为凡是系统中使用到的I2C设备,几乎都需要编写相应的I2C驱动去配置和控制它,例如 RTC实时时钟芯片.音视频采集芯片.音 ...

最新文章

  1. ARM7+PROTEUS调试(转)
  2. C++ Primer 5th笔记(7)chapter7 类:类的静态成员
  3. 几篇关于Hadoop+Hive数据仓库的入门文章
  4. Window Server 2008中开启Window Media Player功能
  5. 用css、html编写一个两列布局的网页,名称为css.html ,要求左侧宽度为200px ,右侧自动扩展...
  6. 《汇编语言》王爽—第五章实验三详解
  7. 网络层地址解析协议ARP
  8. React antD 使用Select 进阶功能 远程搜索,防抖控制,加载状态
  9. PHP实现限制域名从而保护源代码不被拷贝
  10. [原创]超轻量级Web安全漏洞扫描工具Netsparker使用教程介绍
  11. rbf神经网络参数设置_基于梯度下降法的RBF神经网络(04)
  12. 手机linux发短信的命令,在Linux中使用飞信发送手机短信
  13. python制作音乐相册_用Python制作音乐海报
  14. datealive软件最新_约会大作战手游官网版下载-约会大作战正版手游下载地址v3.79_86PS软件园...
  15. resample按时间聚合
  16. 设计模式08—模板方法模式
  17. arcgis 属性表 汇总_最常用的GIS数据汇总
  18. 《迅雷链精品课》第四课:区块链技术的发展趋势
  19. 12.pandas 读取与写入文件
  20. 【RL系列】Multi-Armed Bandit问题笔记

热门文章

  1. 12-21数据增强与数据集寻找方法
  2. optimizer(二) RMSProp
  3. 搞笑四格漫画 - 阿吉先生
  4. Java集合之Stack(出自Java知识体系)
  5. 我们应该知道的java位运算
  6. 将各种网盘挂载到本地,可以使用磁盘操作(建议收藏)
  7. 030 星际争霸网页版!!
  8. ActionBar、TitleBar、ToolBar的联系和区别
  9. VC++利用微软mstscax.dll控件集成远程桌面
  10. (一) Ubuntu下Qt引入FFmpeg