[IMX6ULL driver development and learning] 11. SPI driver of Linux

Reference : Driver Development: SPI Device Driver_spi Driver_Deng Jiawen 007's Blog-CSDN Blog

Table of contents

1. Introduction to SPI driver

1.1 Overview of SPI architecture

1.2 SPI adapter (controller) data structure

1.2 SPI device data structure

1.3 SIP device driver

1.4 Interface functions

 2. SPI driver template


1. Introduction to SPI driver

The SPI driver framework is very similar to the I2C driver framework. The difference is that SPI selects the slave device through the chip select pin, so SPI no longer needs to perform an addressing operation (query the slave address) like I2C. Then perform data interaction of the corresponding registers, and SPI is full-duplex communication, and the communication rate is much higher than I2C.

However, SPI obviously occupies more hardware resources than I2C, and SPI does not have the specified flow control (such as start and stop signals) like I2C and no response mechanism like I2C (leading to the inability to confirm whether the data has been received).

1.1 Overview of SPI architecture

The SPI architecture of Linux can be divided into 3 components:

  • spi core (SPI Core): SPI Core is the core part of the Linux kernel used to maintain and manage spi. SPI Core provides operation interface functions that allow a spi master, spi driver and spi device to register in the SPI Core during initialization and exit. Log out.
  • spi controller driver or adapter driver (SPI Master Driver): SPI Master implements hardware access operations of the spi bus for different types of spi controller hardware. SPI Master registers a controller with SPI Core through the interface function.
  • spi Device Driver: SPI Driver is a driver corresponding to the spi device. It registers with the SPI Core through the interface function. The function of the SPI Driver is to connect the spi device to the spi bus.

The software architecture diagram of Linux is shown in the figure below: 

1.2 SPI adapter (controller) data structure

Refer to the kernel file: include/linux/spi/spi.h

In Linux, the spi_master structure is used to describe the SPI controller, the most important member of which is transferthe function pointer:

The transfer function, like the master_xfer function in i2c_algorithm, is the data transfer function of the controller.
The transfer_one_message function is also used for SPI data sending. It is used to send a spi_message. The SPI data will be packaged into spi_message and then sent out in a queue.

1.2 SPI device data structure

Refer to the kernel file: include/linux/spi/spi.h

In Linux, the spi_device structure is used to describe the SPI device, which records the device's chip select pin, frequency, and which SPI controller it is connected to:

1.3 SIP device driver

Refer to the kernel file: include/linux/spi/spi.h

In Linux, use the spi_driver structure to describe the SPI device driver:

It can be seen that spi_driver is basically the same as i2c_driver and platform_driver, and the probe function will be executed when the SPI device and driver are successfully matched. 

For example: there are two devices connected to spi1 (with two chip select signals), we can put the device into the child node. The child node will be parsed by the kernel and converted into a spi_device. After matching with a certain spi_driver, the child node will be The probe function is called, and we can register the character device driver in the probe function.

1.4 Interface functions

Function prototype:

  • Simple function
/**
 * SPI同步写
 * @spi: 写哪个设备
 * @buf: 数据buffer
 * @len: 长度
 * 这个函数可以休眠
 *
 * 返回值: 0-成功, 负数-失败码
 */
static inline int
spi_write(struct spi_device *spi, const void *buf, size_t len);

/**
 * SPI同步读
 * @spi: 读哪个设备
 * @buf: 数据buffer
 * @len: 长度
 * 这个函数可以休眠
 *
 * 返回值: 0-成功, 负数-失败码
 */
static inline int
spi_read(struct spi_device *spi, void *buf, size_t len);


/**
 * spi_write_then_read : 先写再读, 这是一个同步函数
 * @spi: 读写哪个设备
 * @txbuf: 发送buffer
 * @n_tx: 发送多少字节
 * @rxbuf: 接收buffer
 * @n_rx: 接收多少字节
 * 这个函数可以休眠
 * 
 * 这个函数执行的是半双工的操作: 先发送txbuf中的数据,在读数据,读到的数据存入rxbuf
 *
 * 这个函数用来传输少量数据(建议不要操作32字节), 它的效率不高
 * 如果想进行高效的SPI传输,请使用spi_{async,sync}(这些函数使用DMA buffer)
 *
 * 返回值: 0-成功, 负数-失败码
 */
extern int spi_write_then_read(struct spi_device *spi,
		const void *txbuf, unsigned n_tx,
		void *rxbuf, unsigned n_rx);

/**
 * spi_w8r8 - 同步函数,先写8位数据,再读8位数据
 * @spi: 读写哪个设备
 * @cmd: 要写的数据
 * 这个函数可以休眠
 *
 *
 * 返回值: 成功的话返回一个8位数据(unsigned), 负数表示失败码
 */
static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd);

/**
 * spi_w8r16 - 同步函数,先写8位数据,再读16位数据
 * @spi: 读写哪个设备
 * @cmd: 要写的数据
 * 这个函数可以休眠
 *
 * 读到的16位数据: 
 *     低地址对应读到的第1个字节(MSB),高地址对应读到的第2个字节(LSB)
 *     这是一个big-endian的数据
 *
 * 返回值: 成功的话返回一个16位数据(unsigned), 负数表示失败码
 */
static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd);

/**
 * spi_w8r16be - 同步函数,先写8位数据,再读16位数据,
 *               读到的16位数据被当做big-endian,然后转换为CPU使用的字节序
 * @spi: 读写哪个设备
 * @cmd: 要写的数据
 * 这个函数可以休眠
 *
 * 这个函数跟spi_w8r16类似,差别在于它读到16位数据后,会把它转换为"native endianness"
 *
 * 返回值: 成功的话返回一个16位数据(unsigned, 被转换为本地字节序), 负数表示失败码
 */
static inline ssize_t spi_w8r16be(struct spi_device *spi, u8 cmd);
  •  complex function
/**
 * spi_async - 异步SPI传输函数,简单地说就是这个函数即刻返回,它返回后SPI传输不一定已经完成
 * @spi: 读写哪个设备
 * @message: 用来描述数据传输,里面含有完成时的回调函数(completion callback)
 * 上下文: 任意上下文都可以使用,中断中也可以使用
 *
 * 这个函数不会休眠,它可以在中断上下文使用(无法休眠的上下文),也可以在任务上下文使用(可以休眠的上下文) 
 *
 * 完成SPI传输后,回调函数被调用,它是在"无法休眠的上下文"中被调用的,所以回调函数里不能有休眠操作。
 * 在回调函数被调用前message->statuss是未定义的值,没有意义。
 * 当回调函数被调用时,就可以根据message->status判断结果: 0-成功,负数表示失败码
 * 当回调函数执行完后,驱动程序要认为message等结构体已经被释放,不能再使用它们。
 *
 * 在传输过程中一旦发生错误,整个message传输都会中止,对spi设备的片选被取消。
 *
 * 返回值: 0-成功(只是表示启动的异步传输,并不表示已经传输成功), 负数-失败码
 */
extern int spi_async(struct spi_device *spi, struct spi_message *message);

/**
 * spi_sync - 同步的、阻塞的SPI传输函数,简单地说就是这个函数返回时,SPI传输要么成功要么失败
 * @spi: 读写哪个设备
 * @message: 用来描述数据传输,里面含有完成时的回调函数(completion callback)
 * 上下文: 能休眠的上下文才可以使用这个函数
 *
 * 这个函数的message参数中,使用的buffer是DMA buffer
 *
 * 返回值: 0-成功, 负数-失败码
 */
extern int spi_sync(struct spi_device *spi, struct spi_message *message);


/**
 * spi_sync_transfer - 同步的SPI传输函数
 * @spi: 读写哪个设备
 * @xfers: spi_transfers数组,用来描述传输
 * @num_xfers: 数组项个数
 * 上下文: 能休眠的上下文才可以使用这个函数
 *
 * 返回值: 0-成功, 负数-失败码
 */
static inline int
spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
	unsigned int num_xfers);

 2. SPI driver template

spi_drv.c

#include <linux/spi/spi.h>
#include <linux/module.h>
#include <linux/poll.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>

/* 主设备号                                                                 */
static int major = 0;
static struct class *my_spi_class;

static struct spi_device *g_spi;

static DECLARE_WAIT_QUEUE_HEAD(gpio_wait);
struct fasync_struct *spi_fasync;


/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t spi_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	// int err;

	// struct spi_transfer msgs[2];

	/* 初始化 spi_transfer */

	// static inline int
    //   spi_sync_transfer(struct   spi_device *spi, struct spi_transfer *xfers,
	//   unsigned int num_xfers);

	/* copy_to_user  */
	
	return 0;
}

static ssize_t spi_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	//int err;

	/* copy_from_user  */


	// struct spi_transfer msgs[2];

	/* 初始化 spi_transfer */

	// static inline int
    //   spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
	//   unsigned int num_xfers);

	
	return 0;    
}


static unsigned int spi_drv_poll(struct file *fp, poll_table * wait)
{
	//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	poll_wait(fp, &gpio_wait, wait);
	//return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
	return 0;
}

static int spi_drv_fasync(int fd, struct file *file, int on)
{
	if (fasync_helper(fd, file, on, &spi_fasync) >= 0)
		return 0;
	else
		return -EIO;
}


/* 定义自己的file_operations结构体                                              */
static struct file_operations spi_drv_fops = {
	.owner	 = THIS_MODULE,
	.read    = spi_drv_read,
	.write   = spi_drv_write,
	.poll    = spi_drv_poll,
	.fasync  = spi_drv_fasync,
};


static int spi_drv_probe(struct spi_device *spi)
{
	// struct device_node *np = client->dev.of_node;

	/* 记录spi_device */
	g_spi = spi;

	/* 注册字符设备 */
	/* 注册file_operations 	*/
	major = register_chrdev(0, "100ask_spi", &spi_drv_fops);  /* /dev/gpio_desc */

	my_spi_class = class_create(THIS_MODULE, "100ask_spi_class");
	if (IS_ERR(my_spi_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "100ask_spi");
		return PTR_ERR(my_spi_class);
	}

	device_create(my_spi_class, NULL, MKDEV(major, 0), NULL, "myspi"); /* /dev/myspi */
	
	return 0;
}

static int spi_drv_remove(struct spi_device *spi)
{
	/* 反注册字符设备 */
	device_destroy(my_spi_class, MKDEV(major, 0));
	class_destroy(my_spi_class);
	unregister_chrdev(major, "100ask_spi");

	return 0;
}

static const struct of_device_id myspi_dt_match[] = {
	{ .compatible = "100ask,spidev" },
	{},
};
static struct spi_driver my_spi_driver = {
	.driver = {
		   .name = "100ask_spi_drv",
		   .owner = THIS_MODULE,
		   .of_match_table = myspi_dt_match,
	},
	.probe = spi_drv_probe,
	.remove = spi_drv_remove,
};


static int __init spi_drv_init(void)
{
	/* 注册spi_driver */
	return spi_register_driver(&my_spi_driver);
}

static void __exit spi_drv_exit(void)
{
	/* 反注册spi_driver */
	spi_unregister_driver(&my_spi_driver);
}

/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(spi_drv_init);
module_exit(spi_drv_exit);

MODULE_LICENSE("GPL");


Guess you like

Origin blog.csdn.net/qq_43460230/article/details/132502516