linux_driver/cst816d_driver/cst816d.c
2024-11-19 14:24:47 +08:00

153 lines
3.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
/*------------------字符设备内容----------------------*/
#define DEV_NAME "cst816d"
#define DEV_CNT (1)
struct class *class_cst816d; //保存创建的类
struct device *device; // 保存创建的设备
struct device_node *cst816d_device_node; //cst816d的设备树节点
int io_cst816d_reset; // 保存获取得到引脚编号
struct cst816x_priv {
struct i2c_client *client;
struct gpio_desc *reset;
struct input_dev *input;
// struct cst816x_event_map event_map[16];
};
// static void cst816x_reset(struct cst816x_priv *priv)
// {
// gpiod_set_value_cansleep(priv->reset, 1);
// msleep(50);
// gpiod_set_value_cansleep(priv->reset, 0);
// msleep(100);
// }
/*----------------平台驱动函数集-----------------*/
static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct cst816x_priv *priv;
struct device *dev = &client->dev;
// int ret = 0; //用于保存申请设备号的结果
printk("match successed\n");
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->client = client;
/*获取RGB的设备树节点*/
cst816d_device_node = of_find_node_by_path("/cst816t@15");
if(cst816d_device_node == NULL)
{
printk(KERN_EMERG "\t get cst816t fail! \n");
}
io_cst816d_reset = of_get_named_gpio(cst816d_device_node, "reset-gpios", 0);
printk("cst816d = %d \n",io_cst816d_reset);
/*设置gpio输出高电平*/
gpio_direction_output(io_cst816d_reset, 1);
// if (priv->reset)
// cst816x_reset(priv);
/*---------------------注册 字符设备部分-----------------*/
// //第一步
// //采用动态分配的方式,获取设备编号,次设备号为0,
// //DEV_CNT为1,当前只申请一个设备编号
// ret = alloc_chrdev_region(&cst816d_devno, 0, DEV_CNT, DEV_NAME);
// if(ret < 0){
// printk("fail to alloc cst816d_devno\n");
// goto alloc_err;
// }
// //第二步
// //关联字符设备结构体cdev与文件操作结构体file_operations
// cst816d_chr_dev.owner = THIS_MODULE;
// cdev_init(&cst816d_chr_dev, &cst816d_chr_dev_fops);
// //第三步
// //添加设备至cdev_map散列表中
// ret = cdev_add(&cst816d_chr_dev, cst816d_devno, DEV_CNT);
// if(ret < 0)
// {
// printk("fail to add cdev\n");
// goto add_err;
// }
// //第四步
// /*创建类 */
// class_cst816d = class_create(THIS_MODULE, DEV_NAME);
// /*创建设备*/
// device = device_create(class_cst816d, NULL, cst816d_devno, NULL, DEV_NAME);
// return 0;
// add_err:
// //添加设备失败时,需要注销设备号
// unregister_chrdev_region(cst816d_devno, DEV_CNT);
// printk("\n error! \n");
// alloc_err:
return -1;
}
static const struct of_device_id cst816x_of_match[] = {
{ .compatible = "Yura,cst816t"},
{ /* sentinel */ }
};
static const struct i2c_device_id cst816x_id[] = {
{ .name = "cst816s", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, cst816x_id);
/*定义平台设备结构体*/
static struct i2c_driver cst816d_driver = {
.driver = {
.name = "cst816x",
.of_match_table = cst816x_of_match,
},
.id_table = cst816x_id,
.probe = cst816d_probe,
};
module_i2c_driver(cst816d_driver);
MODULE_LICENSE("GPL");