2022-01-08

struct ap3216c_dev {
    dev_t devid;            /* 设备号   */
    struct cdev cdev;       /* cdev     */
    struct class *class;    /* 类        */
    struct device *device;  /* 设备    */
    struct device_node  *nd; /* 设备节点 */
    int major;          /* 主设备号 */
    void *private_data; /* 私有数据 */
    unsigned short ir, als, ps;     /* 三个光传感器数据 */
};

static struct ap3216c_dev ap3216cdev;

static int ap3216c_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &ap3216cdev;
}
static ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
static int ap3216c_release(struct inode *inode, struct file *filp)

static const struct file_operations ap3216c_ops = {
    .owner = THIS_MODULE,
    .open = ap3216c_open,
    .read = ap3216c_read,
    .release = ap3216c_release,
};
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
    /* 1、构建设备号 */
    if (ap3216cdev.major) {
        ap3216cdev.devid = MKDEV(ap3216cdev.major, 0);
        register_chrdev_region(ap3216cdev.devid, AP3216C_CNT, AP3216C_NAME);
    } else {
        alloc_chrdev_region(&ap3216cdev.devid, 0, AP3216C_CNT, AP3216C_NAME);
        ap3216cdev.major = MAJOR(ap3216cdev.devid);
    }

    /* 2、注册设备 */
    cdev_init(&ap3216cdev.cdev, &ap3216c_ops);
    cdev_add(&ap3216cdev.cdev, ap3216cdev.devid, AP3216C_CNT);

    /* 3、创建类 */
    ap3216cdev.class = class_create(THIS_MODULE, AP3216C_NAME);
    if (IS_ERR(ap3216cdev.class)) {
        return PTR_ERR(ap3216cdev.class);
    }

    /* 4、创建设备 */
    ap3216cdev.device = device_create(ap3216cdev.class, NULL, ap3216cdev.devid, NULL, AP3216C_NAME);
    if (IS_ERR(ap3216cdev.device)) {
        return PTR_ERR(ap3216cdev.device);
    }

    ap3216cdev.private_data = client;

    return 0;
}

static int ap3216c_remove(struct i2c_client *client)
{
    /* 删除设备 */
    cdev_del(&ap3216cdev.cdev);
    unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);

    /* 注销掉类和设备 */
    device_destroy(ap3216cdev.class, ap3216cdev.devid);
    class_destroy(ap3216cdev.class);
    return 0;
}


/* i2c驱动结构体 */  
static struct i2c_driver ap3216c_driver = {
    .probe = ap3216c_probe,
    .remove = ap3216c_remove,
    .driver = {
            .owner = THIS_MODULE,
            .name = "ap3216c",
            .of_match_table = ap3216c_of_match, 
           },
    .id_table = ap3216c_id,
};
           
static int __init ap3216c_init(void)
{
    int ret = 0;

    ret = i2c_add_driver(&ap3216c_driver);
    return ret;
}

static void __exit ap3216c_exit(void)
{
    i2c_del_driver(&ap3216c_driver);
}

module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第九章 至上知识和大奥秘 关于至上者本性的知识是最大的奥秘 要知道,一切众生都在我之中,没有任何可见的接触,也不产...
    莹火虫S阅读 575评论 0 1
  • 2022年的第一一周已经来了,周末,也对自己的一些对2022年的展望做了一些梳理。最重要的是目标的设定,然后是怎么...
    东方郭杨阅读 409评论 0 1
  • 日进六功 三自省:①5:15分按时起床打卡,以前是醒来在被窝,一直想阅读自己买的书,拿着书到厨房大声朗读起来,为自...
    文娟love阅读 213评论 0 1
  • 人间一趟,积极向上, 不念过往,不畏将来, 保持对生活的热爱, 把每一天都过得热气腾腾。 健康就是存款,快乐就是利...
    远岫晴空阅读 520评论 0 1
  • 是喜欢好看的皮囊还是喜欢有趣的灵魂
    认清自我阅读 144评论 7 2