linux内核iic驱动例程

#include "pcomc660.h"

MODULE_AUTHOR("Tan xujia");
MODULE_LICENSE("Dual BSD/GPL");

struct  pcomc660_dev {
    int major;
    int minor;
    dev_t dev_id;/*设备的主次设备号*/
    struct cdev cdev;
    struct class *class;
    struct device *device;
    void *private_data;
};

static struct pcomc660_dev pcomc660dev;

static int
pcomc660_read_regs(struct pcomc660_dev *dev, u8 reg, void *val, int len)
{
    int rc = 0;
    struct i2c_msg msg[2];
    struct i2c_client *client = (struct i2c_client *)dev->private_data;

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

    msg[1].addr = client->addr;
    msg[1].flags = I2C_M_RD;/*表示读操作*/
    msg[1].buf = val;
    msg[1].len = len;

    rc = i2c_transfer(client->adapter, msg, 2);
    if(rc == 2) {
        return 0;
    }else {
        return -EREMOTEIO;
    }
}

static int
pcomc660_write_regs(struct pcomc660_dev *dev, u8 reg, u8 *buf, u8 len)
{
    u8 data[256];
    struct i2c_msg msg;
    struct i2c_client *client = (struct i2c_client *)dev->private_data;

    data[0] = reg; /*要写的寄存器*/
    memcpy(&data[1], buf, len);/*要写的值*/

    msg.addr = client->addr;
    msg.flags = 0;/*表示写操作*/
    msg.buf = data;
    msg.len = len + 1;/*len以字节byte为单位*/
    
    return i2c_transfer(client->adapter, &msg, 1);
}

static unsigned char 
pcomc660_read_reg(struct pcomc660_dev *dev, u8 reg)
{
    u8 data = 0;
    pcomc660_read_regs(dev, reg, &data, 1);
    return data;
#if 0
    struct i2c_client *client = (struct i2c_client *)dev->private_data;
    return i2c_smbus_read_byte_data(client, reg);
#endif
}

static void 
pcomc660_write_reg(struct pcomc660_dev *dev, u8 reg, u8 data)
{
    u8 buf = 0;
    buf = data;
    pcomc660_write_regs(dev, reg, &buf, 1);
}

static int 
pcomc660_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &pcomc660dev;
    printk("pcomc660_open\n");

    //TODO:设备寄存器初始化
    return 0;
}

ssize_t 
pcomc660_read(struct file *filp,  char __user *buf, size_t count, loff_t *off)
{
    int rc = 0;
    struct pcomc660_dev *dev = (struct pcomc660_dev*)filp->private_data;
    //TODO:
    printk("pcomc660_read\n");
    return 0;
}

ssize_t 
pcomc660_write(struct file *filp, const char  __user *buf, size_t count, loff_t *fpos)
{
    int rc = 0;
    struct pcomc660_dev *dev = (struct pcomc660_dev*)filp->private_data;
    //TODO:
    printk("pcomc660_write\n");
    return 0;
}

static int
pcomc660_release(struct inode *inode, struct file *filp)
{
    struct pcomc660_dev *dev = (struct pcomc660_dev*)filp->private_data;
    printk("pcomc660_release\n");
    return 0;
}

static const struct file_operations pcomc660_fops = {
    .owner = THIS_MODULE,
    .open = pcomc660_open,
    .read = pcomc660_read,
    .write = pcomc660_write,
    .release = pcomc660_release,
};

static int 
pcomc660_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
    int rc = 0;
    printk("pcomc660_probe\n");
    rc = alloc_chrdev_region(&pcomc660dev.dev_id, 
        0, 1, "iic_dev");
    if(rc) {
        printk("alloc_chrdev_region fail\n");
        goto alloc_cdev_fail;
    }
        
    pcomc660dev.major = MAJOR(pcomc660dev.dev_id);
    pcomc660dev.minor = MINOR(pcomc660dev.dev_id);

    pcomc660dev.cdev.owner = THIS_MODULE;
    cdev_init(&pcomc660dev.cdev, &pcomc660_fops);
    rc = cdev_add(&pcomc660dev.cdev, pcomc660dev.dev_id, 1);
    if(rc) {
        printk("cdev_add fail\n");
        goto cdev_add_fail;
    }

    pcomc660dev.class = class_create(THIS_MODULE, "c_dev");
    if(IS_ERR(pcomc660dev.class)) {
        rc = PTR_ERR(pcomc660dev.class);
        goto class_fail;
    }
    pcomc660dev.device = device_create(pcomc660dev.class, NULL,
        pcomc660dev.dev_id, NULL, "c_dev");
    if(IS_ERR(pcomc660dev.device)) {
        rc = PTR_ERR(pcomc660dev.device);
        goto create_fail;
    }
    pcomc660dev.private_data = client;
create_fail:
    class_destroy(pcomc660dev.class);
class_fail:
    cdev_del(&pcomc660dev.cdev);
alloc_cdev_fail:
    unregister_chrdev_region(pcomc660dev.dev_id ,1);
cdev_add_fail:
    return rc;
}

static int
pcomc660_remove(struct i2c_client *client)
{
    device_destroy(pcomc660dev.class, pcomc660dev.dev_id);
    class_destroy(pcomc660dev.class);
    cdev_del(&pcomc660dev.cdev);
    unregister_chrdev_region(pcomc660dev.dev_id ,1);     
    return 0;
}

/*常规匹配*/
static struct i2c_device_id pcomc660_id[] = {
    {"pcomc660, 660", 0},
    {}
};

/*设备树匹配*/
static struct of_device_id pcomc660_of_match[] = {
    {.compatible = "pcomc660, 660"},
    {}
};

static struct i2c_driver pcomc660_driver = {
    .probe = pcomc660_probe,
    .remove = pcomc660_remove,
    .driver = {
        .name = "pcomc660",
        .owner = THIS_MODULE,
        .of_match_table = pcomc660_of_match,
    },
    .id_table = pcomc660_id,
};

static int 
__init pcomc660_init(void)
{
    return i2c_add_driver(&pcomc660_driver);
}

static void 
__exit pcomc660_exit(void)
{
    i2c_del_driver(&pcomc660_driver);
}

module_init(pcomc660_init);
module_exit(pcomc660_exit);
#ifndef _PCOMC660_H_
#define _PCOMC660_H_

#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/slab.h>
#include<linux/types.h>
#include<linux/delay.h>
#include<linux/errno.h>
#include<linux/gpio.h>
#include<linux/cdev.h>
#include<linux/device.h>
#include<linux/of.h>
#include<linux/of_address.h>
#include<linux/of_gpio.h>
#include<linux/semaphore.h>
#include<linux/timer.h>
#include<linux/of_irq.h>
#include<linux/irq.h>
#include<linux/input.h>
#include<linux/i2c.h>
#include<linux/delay.h>

/*参考
1:https://www.cnblogs.com/renjue/p/14019988.html
2:https://blog.csdn.net/weixin_57037095/article/details/123801283
3:https://www.cnblogs.com/lifexy/p/7816324.html
4://https://www.cnblogs.com/yuanqiangfei/p/15780448.html
*/
#endif
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)
obj-m	:=pcomc660.o
all:
	make -C $(KERNELDIR) M=$(PWD) modules
clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.* .tmp_versions *.mod *.order *.symvers *.dwo

//未完待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值