²»¹ÜÔõÑù£¬ÏÈÁгöÔÚlinux2.6.32.2ÏÂ×î¼ò¶ÌµÄi2c³ÌÐò£¬ÒòΪÔÚ¸ú×ÙÄں˴úÂëµÄ¹ý³ÌÖÐÄã»á¹Ø×¢µ½Ëü¡£
Ò»¡¢×î¼òÇý¶¯
/* at24c08.c */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
static int at24c08_probe(struct i2c_client *client, const struct i2c_device_id *dev_id)
{
printk("at24c08_probe\n");
return 0;
}
static int at24c08_remove(struct i2c_client *client)
{
printk("at24c08_remove\n");
return 0;
}
static int at24c08_detect(struct i2c_client *client, int kind, struct i2c_board_info *bd_info)
{
strcpy(bd_info->type, "at24c08"); //Õâ¸ö±ØÐëÉèÖà //Point3
printk("at24c08_detect\n");
return 0;
}
static const struct i2c_device_id at24c08_id[] = {
{"at24c08", 0},
{}
};
static unsigned short ignore[] = { I2C_CLIENT_END };
static const unsigned short normal_i2c[] = {0x50, I2C_CLIENT_END};
static const struct i2c_client_address_data addr_data = {
.normal_i2c = normal_i2c,
.probe = ignore,
.ignore = ignore,
//.forces Point1
};
static struct i2c_driver at24c08_driver= {
.probe = at24c08_probe,
.remove = at24c08_remove,
.driver = {
.name = "at24c08",
},
.id_table = at24c08_id,
.detect = at24c08_detect,
.address_data = &addr_data,
.class = I2C_CLASS_HWMON | I2C_CLASS_SPD, //Point2
};
static int at24c08_init(void)
{
i2c_add_driver(&at24c08_driver);
return 0;
}
static void at24c08_exit(void)
{
i2c_del_driver(&at24c08_driver);
return ;
}
module_init(at24c08_init);
module_exit(at24c08_exit);
MODULE_LICENSE("GPL");
¶þ¡¢´úÂë·ÖÎö
ÊÊдÇý¶¯Ö®Ç°ÎÒÃÇÏÈ´Ói2c-s3c2410.c¿ªÊ¼·ÖÎö
ϵͳÆô¶¯³õʼ»¯ÔËÐÐi2c_adap_s3c_init
i2c_adap_s3c_init(i2c-s3c2410.c)
platform_driver_register (platform.c)
drv->driver.bus = &platform_bus_type; //ÉèÖÃÇý¶¯×ÜÏßµÄÀàÐÍΪƽ̨×ÜÏß
driver_register(&drv->driver) (driver.c)
bus_add_driver(drv) (bus.c)
driver_attach(drv) (db.c)
bus_for_each_dev(drv->bus, NULL, drv, __driver_attach)(bus.c)
fn(dev, data) (bus.c)
<=>__driver_attach (db.c)
driver_match_device(drv, dev) (base.h)//µ÷ÓÃÇý¶¯ËùÊô×ÜÏßµÄmatchº¯Êý¼´£ºplatform_match
driver_probe_device(drv, dev) (db.c)
really_probe(dev, drv) (db.c)
if (dev->bus->probe) { //²»´æÔÚ
ret = dev->bus->probe(dev);
...
} else if (drv->probe) { //»òÕßµ÷ÓÃÇý¶¯µÄprobeº¯Êý¼´£ºs3c24xx_i2c_probe
ret = drv->probe(dev);
..
}
½ÓÉÏ
drv->probe(dev)(db.c)
s3c24xx_i2c_probe (i2c-s3c2410.c)
i2c_add_numbered_adapter(&i2c->adap) (i2c-s3c2410.c)
i2c_register_adapter (i2c-core.c)
if (adap->nr < __i2c_first_dynamic_bus_num) //¾²Ì¬×¢²ái2c_board_infoµÄÊʺϲŻáÉèÖÃ__i2c_first_dynamic_bus_num£¬¶øÇÒadap->nrҲûÓÐÉèÖÃ
i2c_scan_static_board_info(adap); //²»»áÖ´ÐÐ
i2c_do_add_adapter (i2c-core.c)
i2c_detect (i2c-core.c)
if (!driver->detect || !address_data) (i2c-core.c)//Èç¹ûÎÒÃÇдµÄÇý¶¯ÀïûÓÐdetectº¯Êý»òÕßaddress_data¾ÍÖ±½Ó·µ»ØÁË
return 0; //ÒòΪsubsys_initcall(i2c_adap_s3c_init) (ÔÚi2c-s3c2410.c¿´µ½£¬ÊÇÔÚϵͳÆô¶¯µÄÊʺÏÔËÐеģ¬´Ëʱat24c08.ko»¹Î´¼ÓÔØ£¬Ö±½Ó·µ»Ø)
ÖÁ´Ë,i2c-s3c2410.c³õʼ»¯²¿·Ö½áÊø
µ±ÎÒÃǵÄat24c08.ko±»¼ÓÔغó:
at24c08_init (at24c08.c)
i2c_add_driver(&at24c08_driver) (at24c08.c)
i2c_register_driver (i2c.h)
driver->driver.bus = &i2c_bus_type;
driver_register(&driver->driver)
bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter) (bus.c)
fn(dev, data) (bus.c)
<=>__attach_adapter (i2c-core.c)
i2c_detect (i2c-core.c)
if (address_data->forces) { //forces²»´æÔÚ£¬¼ûPoint1
...
}
if (!(adapter->class & driver->class)) //Èç¹ûclassÒ²²»Æ¥Åä¾ÍÌø³öÁË,adapter->classÔÚs3c24xx_i2c_probeÀïÉèÖÃÁË
goto exit_free //ËùÒÔÔÚ×Ô¼ºÐ´µÄÇý¶¯³ÌÐòÀïÒ²Òª½«driverµÄclassÉèÖÃÏàͬ£¬¼ûPoint2ºÍs3c24xx_i2c_probe
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { //¼ì²éi2c_algorithmÀïµÄfunctionality
... //ÔÚi2c-s3c2410.cÒѾÉèÖ㬼ÌÐøÖ´ÐкóÃæ
}
for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { //ignore
...
}
for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { //Õý³£Èë¿Ú£¬³ý·Ç±»ºöÂÔ
...
i2c_detect_address(temp_client, -1, driver) (i2c-core.c)
if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,I2C_SMBUS_QUICK, NULL) < 0) //¼ì²éÕâ¸öµØÖ·ÊÇ·ñ¶Ô£¬²»¶Ô¾Í·µ»Ø
return 0;
driver->detect(temp_client, kind, &info) //Point5
<=>at24c08_detect (at24c08.c) //µ÷Óõ½×Ô¼ºÐ´µÄ̽²âº¯Êý
if (info.type[0] == '\0') { //Point3
...
} else { //ÒªÏëÖ´ÐÐelseÓï¾ä£¬±ØÐëÔÚ×Ô¶¨Òådetectº¯ÊýÀïÉèÖÃinfo.type×ֶΣ¬¼´client->name
struct i2c_client *client;
client = i2c_new_device(adapter, &info) (i2c-core.c)
//ÓÃinfoµÄÐÅÏ¢ÉèÖÃclient //Point4
client->dev.bus = &i2c_bus_type
device_register(&client->dev) (core.c)
device_add (core.c)
bus_probe_device (bus.c)
device_attach (bus.c)
device_attach (db.c)
bus_for_each_drv(dev->bus, NULL, dev, __device_attach) (bus.c)
fn(drv, data)
<=>__device_attach (db.c)
driver_match_device (base.h)
driver_probe_device (db.c)
really_probe (db.c)
if (dev->bus->probe) { //dev->busΪi2c_bus_type,Ôòµ÷ÓÃi2c_device_probe
ret = dev->bus->probe(dev);
driver->probe (i2c-core.c)
<=>at24c08_probe (at24c08.c) //×Ô¶¨ÒåµÄprobeº¯Êý±»µ÷ÓÃ
...
} else if (drv->probe) {
ret = drv->probe(dev);
...
}
list_add_tail(&client->detected, &driver->clients);
}
}
}
ËùÒÔ¼ÓÔØÉÏÃæ¼òµ¥µÄat24c08.ko,ÔËÐнá¹û¾ÍÊÇ£º
at24c08_detect
at24c08_probe
Èý¡¢±àд³ÌÐò
ÏÂÃæÎÒÃÇ¿´¿´ÔÚi2c_new_deviceÀïÃæÔõôÉèÖÃclient£¬ÎÒÃDZØÐëÏÈÔÚ×Ô¶¨Òådetectº¯ÊýÀïÉèÖÃ(Point4)
client->dev.platform_data = info->platform_data;
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
strlcpy(client->name, info->type, sizeof(client->name));
1.
at24c08_detectÀÎÒÃÇÖ»ÐèÒª¼ÓÉÏ
info->addr = client->addr;
2.
¹¹½¨×Ö·ûÉ豸Çý¶¯
ÔÚat24c08_probeÖÐÌí¼Ó
register_chrdev_region(major, 1, "at24c08");
cdev_init(&cdev, &at24c08_fops);
cdev_add(&cdev, MKDEV(major, 0), 1);
class_register(&at24c08_cls);
device_create(&at24c08_cls, NULL, MKDEV(major, 0), NULL, "at24c08_dev");
ÁíÍâ¼ÓÉÏ
#define major 155
static struct class at24c08_cls = {
.name = "at24c08_cls",
};
static struct file_operations at24c08_fops = {
.owner = THIS_MODULE,
};
3.ÍêÉÆat24c08_fops
¼ÓÉ϶Áдº¯Êý
.read = at24c08_read,
.write = at24c08_write,
i2cͨÐÅÊÇͨ¹ýi2c_msg´«ÊäµÄ,
¶Á£ºÏÈ´ÓÓû§¿Õ¼ä»ñµÃµØÖ·£¬ÓÃi2c_transferÏÈ´«µØÖ·£¬ÔÙ½ÓÊÕi2c·µ»ØµÄÊý¾Ý½»¸øÓû§
д£ºÏÈ´ÓÓû§¿Õ¼ä»ñµÃµØÖ·ºÍдÈëÖµ£¬ÓÃi2c_transfer´«Ò»¸ömsg¾Í¿ÉÒÔÁË¡£
ÔÚÕâÀïÓöµ½ÎÊÌ⣺i2c_tranferÐèÒªadapter²ÎÊý£¬²¢ÇÒmsgÐèҪĿµÄµØÖ·¡£
Õâ¸öʱºòÓ¦¸Ã¼ÇµÃdriver->detect(temp_client, kind, &info) Point5´«µ½×Ô¶¨ÒåµÄtemp_client,
Ò²¾ÍÊÇat24c08_detectµÄµÚÒ»¸ö²ÎÊýstruct i2c_client *client£¬ËüÀïÃæ¾ÍÓÐadapterºÍaddrµÄÐÅÏ¢£¬
ÄÇô¾ÍÔÚat24c08_detectÀï¼ÓÉÏat24c08_adapter = client->adapter; addr = bd_info->addr = client->addr;
ÐèÏȶ¨Òå
static struct i2c_adapter *at24c08_adapter;
static unsigned short addr;
ÍêÕûµÄÇý¶¯³ÌÐò¾ÍÓ¦¸ÃÊÇ£º
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <asm/uaccess.h>
#define major 155
static struct i2c_driver at24c08_driver;
static struct i2c_adapter *at24c08_adapter;
static unsigned short addr;
static struct class at24c08_cls = {
.name = "at24c08_cls",
};
ssize_t at24c08_read (struct file *filp, char __user *buf, size_t sz, loff_t *off)
{
struct i2c_msg msg[2];
unsigned char args, data;
if (sz != 1)
return -EINVAL;
copy_from_user((void *)&args, buf, 1);
/* ÏÈ´«¶ÁµØÖ· */
msg[0].addr = addr;
msg[0].buf = &args;
msg[0].len = 1;
msg[0].flags = 0;
/* ÔÙ ¶Á */
msg[1].addr = addr;
msg[1].buf = &data;
msg[1].len = 1;
msg[1].flags = 1; /* ¶Á */
if (2 == i2c_transfer(at24c08_adapter, msg, 2)) {
/* ¶Á³É¹¦ */
copy_to_user((void *)buf, &data, 1);
return 1;
}
else
return -EIO;
}
ssize_t at24c08_write (struct file *filp, const char __user *buf, size_t sz, loff_t *off)
{
struct i2c_msg msg;
unsigned char args[2];
copy_from_user((void *)&args, buf, 2);
/* args[0] = addr, args[1] = val */
msg.addr = addr;
msg.buf = args;
msg.len = 2;
msg.flags = 0; /* д */
if(1 == i2c_transfer(at24c08_adapter,&msg, 1))
return 2;
else
return -EIO;
}
static struct file_operations at24c08_fops = {
.owner = THIS_MODULE,
.read = at24c08_read,
.write = at24c08_write,
};
static struct cdev cdev;
static int at24c08_probe(struct i2c_client *client, const struct i2c_device_id *dev_id)
{
register_chrdev_region(major, 1, "at24c08");
cdev_init(&cdev, &at24c08_fops);
cdev_add(&cdev, MKDEV(major, 0), 1);
class_register(&at24c08_cls);
device_create(&at24c08_cls, NULL, MKDEV(major, 0), NULL, "at24c08_dev");
return 0;
}
static int at24c08_remove(struct i2c_client *client)
{
device_destroy(&at24c08_cls, MKDEV(major, 0));
class_destroy(&at24c08_cls);
cdev_del(&cdev);
unregister_chrdev_region(MKDEV(major, 0), 1);
return 0;
}
static int at24c08_detect(struct i2c_client *client, int kind, struct i2c_board_info *bd_info)
{
strcpy(bd_info->type, "at24c08");
addr = bd_info->addr = client->addr;
at24c08_adapter = client->adapter;
return 0;
}
static const struct i2c_device_id at24c08_id[] = {
{"at24c08", 0},
{}
};
static unsigned short ignore[] = { I2C_CLIENT_END };
static const unsigned short normal_i2c[] = {0x50, I2C_CLIENT_END};
static const struct i2c_client_address_data addr_data = {
.normal_i2c = normal_i2c,
.probe = ignore,
.ignore = ignore,
};
static struct i2c_driver at24c08_driver= {
.probe = at24c08_probe,
.remove = at24c08_remove,
.driver = {
.name = "at24c08",
},
.id_table = at24c08_id,
.detect = at24c08_detect,
.address_data = &addr_data,
.class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
};
static int at24c08_init(void)
{
i2c_add_driver(&at24c08_driver);
return 0;
}
static void at24c08_exit(void)
{
i2c_del_driver(&at24c08_driver);
return ;
}
module_init(at24c08_init);
module_exit(at24c08_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("100ask.net Young");
Ó¦ÓóÌÐò£º
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#define USAGE printf("./testapp r addr\n./testapp w addr data\n")
int main(int argc, char *argv[])
{
int fd;
unsigned char dat[2];
if ((argc!=3) && (argc!=4)) {
USAGE;
return -1;
}
fd = open("/dev/at24c08_dev", O_RDWR);
if (-1 == fd) {
printf("failed to open device.\n");
return -1;
}
if (!strcmp(argv[1], "r")) { /* ¶Á */
dat[0] = strtoul(argv[2], NULL, 10);
dat[1] = dat[0];
read(fd, dat,1);
printf("read addr%d: %c, %d, 0x%2x\n", dat[1], dat[0], dat[0], dat[0]);
}else if(!strcmp(argv[1], "w")) { /* д */
dat[0] = strtoul(argv[2], NULL, 10);
dat[1] = strtoul(argv[3], NULL, 10);
write(fd, dat, 2);
}else {
USAGE;
return -1;
}
return 0;
}
¾ÍÊÇÓÐÒ»¸ö±È½ÏÆæ¹ÖµÄÎÊÌ⣬Èç¹ûÖ±½ÓËæ±ã¶ÁÒ»¸öµØÖ·A£¬¶Áµ½µÄÖµÊÇ´«Ë͵ĵØÖ·A¡£Èç¹ûÏȽ«ÊýÖµBдÈëCµØÖ·£¬ÔÙ¶ÁCµØÖ·¾ÍÊǵõ½BÊýÖµÁË¡£
µ±ÎÒжÔØÄ£¿é¡¢ÖØÆô»úÆ÷Ö®ºóÔÙÀ´ÊÔÑ飬ÔÙ¶ÁCµØÖ·£¬ÊÇÕýÈ·µÄµÃµ½BÊýÖµ£¬ËùÒÔÇý¶¯ºÍÓû§³ÌÐòûÎÊÌâ¡£
Ò»¡¢×î¼òÇý¶¯
/* at24c08.c */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
static int at24c08_probe(struct i2c_client *client, const struct i2c_device_id *dev_id)
{
printk("at24c08_probe\n");
return 0;
}
static int at24c08_remove(struct i2c_client *client)
{
printk("at24c08_remove\n");
return 0;
}
static int at24c08_detect(struct i2c_client *client, int kind, struct i2c_board_info *bd_info)
{
strcpy(bd_info->type, "at24c08"); //Õâ¸ö±ØÐëÉèÖà //Point3
printk("at24c08_detect\n");
return 0;
}
static const struct i2c_device_id at24c08_id[] = {
{"at24c08", 0},
{}
};
static unsigned short ignore[] = { I2C_CLIENT_END };
static const unsigned short normal_i2c[] = {0x50, I2C_CLIENT_END};
static const struct i2c_client_address_data addr_data = {
.normal_i2c = normal_i2c,
.probe = ignore,
.ignore = ignore,
//.forces Point1
};
static struct i2c_driver at24c08_driver= {
.probe = at24c08_probe,
.remove = at24c08_remove,
.driver = {
.name = "at24c08",
},
.id_table = at24c08_id,
.detect = at24c08_detect,
.address_data = &addr_data,
.class = I2C_CLASS_HWMON | I2C_CLASS_SPD, //Point2
};
static int at24c08_init(void)
{
i2c_add_driver(&at24c08_driver);
return 0;
}
static void at24c08_exit(void)
{
i2c_del_driver(&at24c08_driver);
return ;
}
module_init(at24c08_init);
module_exit(at24c08_exit);
MODULE_LICENSE("GPL");
¶þ¡¢´úÂë·ÖÎö
ÊÊдÇý¶¯Ö®Ç°ÎÒÃÇÏÈ´Ói2c-s3c2410.c¿ªÊ¼·ÖÎö
ϵͳÆô¶¯³õʼ»¯ÔËÐÐi2c_adap_s3c_init
i2c_adap_s3c_init(i2c-s3c2410.c)
platform_driver_register (platform.c)
drv->driver.bus = &platform_bus_type; //ÉèÖÃÇý¶¯×ÜÏßµÄÀàÐÍΪƽ̨×ÜÏß
driver_register(&drv->driver) (driver.c)
bus_add_driver(drv) (bus.c)
driver_attach(drv) (db.c)
bus_for_each_dev(drv->bus, NULL, drv, __driver_attach)(bus.c)
fn(dev, data) (bus.c)
<=>__driver_attach (db.c)
driver_match_device(drv, dev) (base.h)//µ÷ÓÃÇý¶¯ËùÊô×ÜÏßµÄmatchº¯Êý¼´£ºplatform_match
driver_probe_device(drv, dev) (db.c)
really_probe(dev, drv) (db.c)
if (dev->bus->probe) { //²»´æÔÚ
ret = dev->bus->probe(dev);
...
} else if (drv->probe) { //»òÕßµ÷ÓÃÇý¶¯µÄprobeº¯Êý¼´£ºs3c24xx_i2c_probe
ret = drv->probe(dev);
..
}
½ÓÉÏ
drv->probe(dev)(db.c)
s3c24xx_i2c_probe (i2c-s3c2410.c)
i2c_add_numbered_adapter(&i2c->adap) (i2c-s3c2410.c)
i2c_register_adapter (i2c-core.c)
if (adap->nr < __i2c_first_dynamic_bus_num) //¾²Ì¬×¢²ái2c_board_infoµÄÊʺϲŻáÉèÖÃ__i2c_first_dynamic_bus_num£¬¶øÇÒadap->nrҲûÓÐÉèÖÃ
i2c_scan_static_board_info(adap); //²»»áÖ´ÐÐ
i2c_do_add_adapter (i2c-core.c)
i2c_detect (i2c-core.c)
if (!driver->detect || !address_data) (i2c-core.c)//Èç¹ûÎÒÃÇдµÄÇý¶¯ÀïûÓÐdetectº¯Êý»òÕßaddress_data¾ÍÖ±½Ó·µ»ØÁË
return 0; //ÒòΪsubsys_initcall(i2c_adap_s3c_init) (ÔÚi2c-s3c2410.c¿´µ½£¬ÊÇÔÚϵͳÆô¶¯µÄÊʺÏÔËÐеģ¬´Ëʱat24c08.ko»¹Î´¼ÓÔØ£¬Ö±½Ó·µ»Ø)
ÖÁ´Ë,i2c-s3c2410.c³õʼ»¯²¿·Ö½áÊø
µ±ÎÒÃǵÄat24c08.ko±»¼ÓÔغó:
at24c08_init (at24c08.c)
i2c_add_driver(&at24c08_driver) (at24c08.c)
i2c_register_driver (i2c.h)
driver->driver.bus = &i2c_bus_type;
driver_register(&driver->driver)
bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter) (bus.c)
fn(dev, data) (bus.c)
<=>__attach_adapter (i2c-core.c)
i2c_detect (i2c-core.c)
if (address_data->forces) { //forces²»´æÔÚ£¬¼ûPoint1
...
}
if (!(adapter->class & driver->class)) //Èç¹ûclassÒ²²»Æ¥Åä¾ÍÌø³öÁË,adapter->classÔÚs3c24xx_i2c_probeÀïÉèÖÃÁË
goto exit_free //ËùÒÔÔÚ×Ô¼ºÐ´µÄÇý¶¯³ÌÐòÀïÒ²Òª½«driverµÄclassÉèÖÃÏàͬ£¬¼ûPoint2ºÍs3c24xx_i2c_probe
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { //¼ì²éi2c_algorithmÀïµÄfunctionality
... //ÔÚi2c-s3c2410.cÒѾÉèÖ㬼ÌÐøÖ´ÐкóÃæ
}
for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { //ignore
...
}
for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { //Õý³£Èë¿Ú£¬³ý·Ç±»ºöÂÔ
...
i2c_detect_address(temp_client, -1, driver) (i2c-core.c)
if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,I2C_SMBUS_QUICK, NULL) < 0) //¼ì²éÕâ¸öµØÖ·ÊÇ·ñ¶Ô£¬²»¶Ô¾Í·µ»Ø
return 0;
driver->detect(temp_client, kind, &info) //Point5
<=>at24c08_detect (at24c08.c) //µ÷Óõ½×Ô¼ºÐ´µÄ̽²âº¯Êý
if (info.type[0] == '\0') { //Point3
...
} else { //ÒªÏëÖ´ÐÐelseÓï¾ä£¬±ØÐëÔÚ×Ô¶¨Òådetectº¯ÊýÀïÉèÖÃinfo.type×ֶΣ¬¼´client->name
struct i2c_client *client;
client = i2c_new_device(adapter, &info) (i2c-core.c)
//ÓÃinfoµÄÐÅÏ¢ÉèÖÃclient //Point4
client->dev.bus = &i2c_bus_type
device_register(&client->dev) (core.c)
device_add (core.c)
bus_probe_device (bus.c)
device_attach (bus.c)
device_attach (db.c)
bus_for_each_drv(dev->bus, NULL, dev, __device_attach) (bus.c)
fn(drv, data)
<=>__device_attach (db.c)
driver_match_device (base.h)
driver_probe_device (db.c)
really_probe (db.c)
if (dev->bus->probe) { //dev->busΪi2c_bus_type,Ôòµ÷ÓÃi2c_device_probe
ret = dev->bus->probe(dev);
driver->probe (i2c-core.c)
<=>at24c08_probe (at24c08.c) //×Ô¶¨ÒåµÄprobeº¯Êý±»µ÷ÓÃ
...
} else if (drv->probe) {
ret = drv->probe(dev);
...
}
list_add_tail(&client->detected, &driver->clients);
}
}
}
ËùÒÔ¼ÓÔØÉÏÃæ¼òµ¥µÄat24c08.ko,ÔËÐнá¹û¾ÍÊÇ£º
at24c08_detect
at24c08_probe
Èý¡¢±àд³ÌÐò
ÏÂÃæÎÒÃÇ¿´¿´ÔÚi2c_new_deviceÀïÃæÔõôÉèÖÃclient£¬ÎÒÃDZØÐëÏÈÔÚ×Ô¶¨Òådetectº¯ÊýÀïÉèÖÃ(Point4)
client->dev.platform_data = info->platform_data;
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
strlcpy(client->name, info->type, sizeof(client->name));
1.
at24c08_detectÀÎÒÃÇÖ»ÐèÒª¼ÓÉÏ
info->addr = client->addr;
2.
¹¹½¨×Ö·ûÉ豸Çý¶¯
ÔÚat24c08_probeÖÐÌí¼Ó
register_chrdev_region(major, 1, "at24c08");
cdev_init(&cdev, &at24c08_fops);
cdev_add(&cdev, MKDEV(major, 0), 1);
class_register(&at24c08_cls);
device_create(&at24c08_cls, NULL, MKDEV(major, 0), NULL, "at24c08_dev");
ÁíÍâ¼ÓÉÏ
#define major 155
static struct class at24c08_cls = {
.name = "at24c08_cls",
};
static struct file_operations at24c08_fops = {
.owner = THIS_MODULE,
};
3.ÍêÉÆat24c08_fops
¼ÓÉ϶Áдº¯Êý
.read = at24c08_read,
.write = at24c08_write,
i2cͨÐÅÊÇͨ¹ýi2c_msg´«ÊäµÄ,
¶Á£ºÏÈ´ÓÓû§¿Õ¼ä»ñµÃµØÖ·£¬ÓÃi2c_transferÏÈ´«µØÖ·£¬ÔÙ½ÓÊÕi2c·µ»ØµÄÊý¾Ý½»¸øÓû§
д£ºÏÈ´ÓÓû§¿Õ¼ä»ñµÃµØÖ·ºÍдÈëÖµ£¬ÓÃi2c_transfer´«Ò»¸ömsg¾Í¿ÉÒÔÁË¡£
ÔÚÕâÀïÓöµ½ÎÊÌ⣺i2c_tranferÐèÒªadapter²ÎÊý£¬²¢ÇÒmsgÐèҪĿµÄµØÖ·¡£
Õâ¸öʱºòÓ¦¸Ã¼ÇµÃdriver->detect(temp_client, kind, &info) Point5´«µ½×Ô¶¨ÒåµÄtemp_client,
Ò²¾ÍÊÇat24c08_detectµÄµÚÒ»¸ö²ÎÊýstruct i2c_client *client£¬ËüÀïÃæ¾ÍÓÐadapterºÍaddrµÄÐÅÏ¢£¬
ÄÇô¾ÍÔÚat24c08_detectÀï¼ÓÉÏat24c08_adapter = client->adapter; addr = bd_info->addr = client->addr;
ÐèÏȶ¨Òå
static struct i2c_adapter *at24c08_adapter;
static unsigned short addr;
ÍêÕûµÄÇý¶¯³ÌÐò¾ÍÓ¦¸ÃÊÇ£º
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <asm/uaccess.h>
#define major 155
static struct i2c_driver at24c08_driver;
static struct i2c_adapter *at24c08_adapter;
static unsigned short addr;
static struct class at24c08_cls = {
.name = "at24c08_cls",
};
ssize_t at24c08_read (struct file *filp, char __user *buf, size_t sz, loff_t *off)
{
struct i2c_msg msg[2];
unsigned char args, data;
if (sz != 1)
return -EINVAL;
copy_from_user((void *)&args, buf, 1);
/* ÏÈ´«¶ÁµØÖ· */
msg[0].addr = addr;
msg[0].buf = &args;
msg[0].len = 1;
msg[0].flags = 0;
/* ÔÙ ¶Á */
msg[1].addr = addr;
msg[1].buf = &data;
msg[1].len = 1;
msg[1].flags = 1; /* ¶Á */
if (2 == i2c_transfer(at24c08_adapter, msg, 2)) {
/* ¶Á³É¹¦ */
copy_to_user((void *)buf, &data, 1);
return 1;
}
else
return -EIO;
}
ssize_t at24c08_write (struct file *filp, const char __user *buf, size_t sz, loff_t *off)
{
struct i2c_msg msg;
unsigned char args[2];
copy_from_user((void *)&args, buf, 2);
/* args[0] = addr, args[1] = val */
msg.addr = addr;
msg.buf = args;
msg.len = 2;
msg.flags = 0; /* д */
if(1 == i2c_transfer(at24c08_adapter,&msg, 1))
return 2;
else
return -EIO;
}
static struct file_operations at24c08_fops = {
.owner = THIS_MODULE,
.read = at24c08_read,
.write = at24c08_write,
};
static struct cdev cdev;
static int at24c08_probe(struct i2c_client *client, const struct i2c_device_id *dev_id)
{
register_chrdev_region(major, 1, "at24c08");
cdev_init(&cdev, &at24c08_fops);
cdev_add(&cdev, MKDEV(major, 0), 1);
class_register(&at24c08_cls);
device_create(&at24c08_cls, NULL, MKDEV(major, 0), NULL, "at24c08_dev");
return 0;
}
static int at24c08_remove(struct i2c_client *client)
{
device_destroy(&at24c08_cls, MKDEV(major, 0));
class_destroy(&at24c08_cls);
cdev_del(&cdev);
unregister_chrdev_region(MKDEV(major, 0), 1);
return 0;
}
static int at24c08_detect(struct i2c_client *client, int kind, struct i2c_board_info *bd_info)
{
strcpy(bd_info->type, "at24c08");
addr = bd_info->addr = client->addr;
at24c08_adapter = client->adapter;
return 0;
}
static const struct i2c_device_id at24c08_id[] = {
{"at24c08", 0},
{}
};
static unsigned short ignore[] = { I2C_CLIENT_END };
static const unsigned short normal_i2c[] = {0x50, I2C_CLIENT_END};
static const struct i2c_client_address_data addr_data = {
.normal_i2c = normal_i2c,
.probe = ignore,
.ignore = ignore,
};
static struct i2c_driver at24c08_driver= {
.probe = at24c08_probe,
.remove = at24c08_remove,
.driver = {
.name = "at24c08",
},
.id_table = at24c08_id,
.detect = at24c08_detect,
.address_data = &addr_data,
.class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
};
static int at24c08_init(void)
{
i2c_add_driver(&at24c08_driver);
return 0;
}
static void at24c08_exit(void)
{
i2c_del_driver(&at24c08_driver);
return ;
}
module_init(at24c08_init);
module_exit(at24c08_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("100ask.net Young");
Ó¦ÓóÌÐò£º
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#define USAGE printf("./testapp r addr\n./testapp w addr data\n")
int main(int argc, char *argv[])
{
int fd;
unsigned char dat[2];
if ((argc!=3) && (argc!=4)) {
USAGE;
return -1;
}
fd = open("/dev/at24c08_dev", O_RDWR);
if (-1 == fd) {
printf("failed to open device.\n");
return -1;
}
if (!strcmp(argv[1], "r")) { /* ¶Á */
dat[0] = strtoul(argv[2], NULL, 10);
dat[1] = dat[0];
read(fd, dat,1);
printf("read addr%d: %c, %d, 0x%2x\n", dat[1], dat[0], dat[0], dat[0]);
}else if(!strcmp(argv[1], "w")) { /* д */
dat[0] = strtoul(argv[2], NULL, 10);
dat[1] = strtoul(argv[3], NULL, 10);
write(fd, dat, 2);
}else {
USAGE;
return -1;
}
return 0;
}
¾ÍÊÇÓÐÒ»¸ö±È½ÏÆæ¹ÖµÄÎÊÌ⣬Èç¹ûÖ±½ÓËæ±ã¶ÁÒ»¸öµØÖ·A£¬¶Áµ½µÄÖµÊÇ´«Ë͵ĵØÖ·A¡£Èç¹ûÏȽ«ÊýÖµBдÈëCµØÖ·£¬ÔÙ¶ÁCµØÖ·¾ÍÊǵõ½BÊýÖµÁË¡£
µ±ÎÒжÔØÄ£¿é¡¢ÖØÆô»úÆ÷Ö®ºóÔÙÀ´ÊÔÑ飬ÔÙ¶ÁCµØÖ·£¬ÊÇÕýÈ·µÄµÃµ½BÊýÖµ£¬ËùÒÔÇý¶¯ºÍÓû§³ÌÐòûÎÊÌâ¡£