Linux spi设备端驱动编写(基于linux4.9的内核)
Linux命令行基础操作教学 #生活知识# #编程教程#
目录
前序
1. 在dts中对应的设备树节点
2. 重要的数据结构与核心层API
3. 实例
前序
linux下写驱动是站在巨人的肩膀上做开发,不用完全从头做起,甚至你不需要懂SPI时序,照样能写出可用的驱动,原因是:控制器驱动一般芯片商早已写好(linux4.9中针对imx系列cpu是 drivers/spi/spi-imx.c),怎么发、怎么操作CPU寄存器这些细节都是控制器驱动干的事,设备端驱动不需要关心。我们所要做的便是用核心层提供的API注册进去,调用核心层发送函数,将数据发出去,收回来。linux4.9中核心层drivers/spi/spi.c。1. 在dts中对应的设备树节点
1. 管脚映射:
spi0的四根线可以根据板子硬件连接情况映射到不同的管脚上,针对ZYNQMP-zcu102这块CPU来说,与spi控制器的连接有两种方式:
使用PS域的MIO :mio[77:0]中某些引脚的复用功能是spi,但得成组配(设备树中pinctrl章节,有专用的格式);使用PL,bit文件中直接将某几个PL域的引脚连到SPI控制器的输出上。(我用的是这种方案,这样就不要进行引脚复用功能设置了,设备树中亦可不必指定pinctrl)引脚复用功能分组见drivers/pinctrl/pinctrl-zynqmp.c ; 设备树中与引脚相关的关键字解析以及定义见drivers/pinctrl/devicetree.c drivers/pinctrl/pinconf-generic.c;
&pinctrl0 {
status = "okay";
pinctrl_i2c1_default: i2c1-default {
mux {
groups = "i2c1_4_grp";
function = "i2c1";
};
conf {
groups = "i2c1_4_grp";
bias-pull-up;
slew-rate = <SLEW_RATE_SLOW>;
io-standard = <IO_STANDARD_LVCMOS18>;
};
};
pinctrl_i2c1_gpio: i2c1-gpio {
mux {
groups = "gpio0_16_grp", "gpio0_17_grp";
function = "gpio0";
};
conf {
groups = "gpio0_16_grp", "gpio0_17_grp";
slew-rate = <SLEW_RATE_SLOW>;
io-standard = <IO_STANDARD_LVCMOS18>;
};
};
pinctrl_uart0_default: uart0-default {
mux {
groups = "uart0_4_grp";
function = "uart0";
};
conf {
groups = "uart0_4_grp";
slew-rate = <SLEW_RATE_SLOW>;
io-standard = <IO_STANDARD_LVCMOS18>;
};
conf-rx {
pins = "MIO18";
bias-high-impedance;
};
conf-tx {
pins = "MIO19";
bias-disable;
};
};
pinctrl_spi0_default: spi0-default {
mux {
groups = "spi0_0_grp";//使用第几组引脚,查pinctrl-zynqmp.c
function = "spi0";
};
conf {
groups = "spi0_0_grp";//对上面选中的那组管脚做电气特性设置
bias-disable;
slew-rate = <SLEW_RATE_SLOW>;//速率
io-standard = <IO_STANDARD_LVCMOS18>;//电压类型
};
mux-cs {
groups = "spi0_0_ss0_grp", "spi0_0_ss1_grp", "spi0_0_ss2_grp";//spi0_cs_x选哪几个引脚用作spi0的cs
function = "spi0_ss";
};
conf-cs {
groups = "spi0_0_ss0_grp", "spi0_0_ss1_grp", "spi0_0_ss2_grp";
bias-disable;
};
};
};
amba: amba {
compatible = "simple-bus";
u-boot,dm-pre-reloc;
#address-cells = <2>;
#size-cells = <2>;
ranges;
此处省略n行...
spi0: spi@ff040000 {
compatible = "cdns,spi-r1p6";
status = "disabled";
interrupt-parent = <&gic>;
interrupts = <0 19 4>;
reg = <0x0 0xff040000 0x0 0x1000>;
clock-names = "ref_clk", "pclk";
#address-cells = <1>;
#size-cells = <0>;
num-cs = <1>;
power-domains = <&pd_spi0>;
};
此处省略n行...
};
更多关于pinctrl的分析见博客:http://blog.csdn.net/eastonwoo/article/details/51481312
2. 重要的数据结构与核心层API
2.1 spi_transfer
struct spi_transfer {
const void *tx_buf;
void *rx_buf;
unsigned len;
dma_addr_t tx_dma;
dma_addr_t rx_dma;
struct sg_table tx_sg;
struct sg_table rx_sg;
unsigned cs_change:1;
unsigned tx_nbits:3;
unsigned rx_nbits:3;
#define SPI_NBITS_SINGLE 0x01
#define SPI_NBITS_DUAL 0x02
#define SPI_NBITS_QUAD 0x04
u8 bits_per_word;
u16 delay_usecs;
u32 speed_hz;
u32 dummy;
struct list_head transfer_list;
};
2.2 spi_message
struct spi_message {
struct list_head transfers;
struct spi_device *spi;
unsigned is_dma_mapped:1;
void (*complete)(void *context);
void *context;
unsigned frame_length;
unsigned actual_length;
int status;
struct list_head queue;
void *state;
struct list_head resources;
};
2.3 spi_device
struct spi_device {
struct device dev;
struct spi_master *master;
u32 max_speed_hz;
u8 chip_select;
u8 bits_per_word;
u16 mode;
#define SPI_CPHA 0x01
#define SPI_CPOL 0x02
#define SPI_MODE_0 (0|0)
#define SPI_MODE_1 (0|SPI_CPHA)
#define SPI_MODE_2 (SPI_CPOL|0)
#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
#define SPI_CS_HIGH 0x04
#define SPI_LSB_FIRST 0x08
#define SPI_3WIRE 0x10
#define SPI_LOOP 0x20
#define SPI_NO_CS 0x40
#define SPI_READY 0x80
#define SPI_TX_DUAL 0x100
#define SPI_TX_QUAD 0x200
#define SPI_RX_DUAL 0x400
#define SPI_RX_QUAD 0x800
int irq;
void *controller_state;
void *controller_data;
char modalias[SPI_NAME_SIZE];
int cs_gpio;
struct spi_statistics statistics;
};
2.4 发送接收函数
int spi_write_then_read(struct spi_device *spi,
const void *txbuf, unsigned n_tx,
void *rxbuf, unsigned n_rx)
2.5 注册
module_spi_driver(dac37j84_spi_driver);
3. 实例
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/spi/spi.h>
#include <linux/spi/spidev.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/acpi.h>
#include <linux/miscdevice.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <asm/current.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/delay.h>
#define MASK_WRITE 0x80
#define MASK_READ 0x80
#define MASK_SEVE 0x60
#define MASK_ADDR_H 0x1F
#define SPI_SPEED_HZ 200000
#define LMK04828_MAGIC 'K'
#define GET_REG _IOR(LMK04828_MAGIC, 0,int)
#define SET_REG _IOW(LMK04828_MAGIC, 0, int)
struct lmk04828_t {
dev_t devt;
struct miscdevice misc_dev;
spinlock_t spi_lock;
struct spi_device *spi;
struct list_head device_entry;
struct mutex buf_lock;
unsigned users;
u8 *tx_buffer;
u8 *rx_buffer;
u32 speed_hz;
u32 cur_index;
void __iomem * pl_cs_addr;
u32 pl_cs_val;
};
static struct lmk04828_t *lmk04828;
void lmk04828spi_cs(void)
{
iowrite32( lmk04828->pl_cs_val, lmk04828->pl_cs_addr);
}
static ssize_t lmk04828spi_sync(struct lmk04828_t *spidev, struct spi_message *message)
{
DECLARE_COMPLETION_ONSTACK(done);
int status;
struct spi_device *spi;
spin_lock_irq(&spidev->spi_lock);
spi = spidev->spi;
spin_unlock_irq(&spidev->spi_lock);
lmk04828spi_cs();
if (spi == NULL)
status = -ESHUTDOWN;
else
status = spi_sync(spi, message);
if (status == 0)
status = message->actual_length;
return status;
}
static ssize_t lmk04828spi_sync_write(struct lmk04828_t *spidev, size_t len)
{
struct spi_transfer t = {
.tx_buf = spidev->tx_buffer,
.len = len,
.speed_hz = spidev->speed_hz,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return lmk04828spi_sync(spidev, &m);
}
static ssize_t lmk04828spi_sync_read(struct lmk04828_t *spidev, size_t len)
{
struct spi_transfer t = {
.rx_buf = spidev->rx_buffer,
.len = len,
.speed_hz = spidev->speed_hz,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return lmk04828spi_sync(spidev, &m);
}
int lmk04828_write_reg(int reg, unsigned char value)
{
unsigned char cmd[3]={0};
unsigned char addr_h = reg >> 8;
unsigned char addr_l = reg & 0xff;
cmd[0] = addr_h & MASK_ADDR_H;
cmd[0] &= ~ MASK_SEVE;
cmd[0] &= ~ MASK_WRITE;
cmd[1] = addr_l;
cmd[2] = value;
lmk04828->tx_buffer = cmd;
lmk04828->speed_hz = SPI_SPEED_HZ;
return lmk04828spi_sync_write(lmk04828, 3);
}
EXPORT_SYMBOL(lmk04828_write_reg);
int lmk04828_read_reg(int reg, unsigned char buff[1])
{
unsigned char cmd[3]={0};
unsigned char addr_h = reg >> 8;
unsigned char addr_l = reg & 0xff;
cmd[0] = addr_h & MASK_ADDR_H;
cmd[0] &= ~ MASK_SEVE;
cmd[0] |= MASK_READ;
cmd[1] = addr_l;
cmd[2] = 0;
lmk04828->tx_buffer = cmd;
lmk04828->rx_buffer = buff;
lmk04828->speed_hz = SPI_SPEED_HZ;
return spi_write_then_read(lmk04828->spi, cmd,2, buff, 1);
}
EXPORT_SYMBOL(lmk04828_read_reg);
int lmk04828_open(struct inode *node, struct file *pfile)
{
return 0;
}
int lmk04828_release(struct inode *node, struct file *pfile)
{
return 0;
}
loff_t lmk04828_llseek(struct file *pfile, loff_t off, int len)
{
lmk04828->cur_index = off;
return 0;
}
ssize_t lmk04828_read(struct file *pfile, char __user *buf, size_t size, loff_t *off)
{
unsigned char kbuf[1]={0};
mutex_lock(&lmk04828->buf_lock);
lmk04828_read_reg(lmk04828->cur_index, kbuf);
mutex_unlock(&lmk04828->buf_lock);
return copy_to_user(buf, kbuf, 1);
}
ssize_t lmk04828_write(struct file *pfile, const char __user *buf,
size_t size, loff_t *off)
{
unsigned char kbuf[1]={0};
int ret=0;
if ( 0 > copy_from_user(kbuf, buf, 1) ) {
printk(KERN_INFO "%s %s %d \n","copy to kbuf eer",__func__,__LINE__);
}
mutex_lock(&lmk04828->buf_lock);
ret = lmk04828_write_reg(lmk04828->cur_index,kbuf[0]);
mutex_unlock(&lmk04828->buf_lock);
return ret;
}
long lmk04828_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
switch(cmd) {
case GET_REG:
break;
case SET_REG:
break;
default:
printk("invalid argument\n");
return -EINVAL;
}
return 0;
}
int lmk04828_reg_pll2_n(char enable, unsigned int val)
{
int ret1,ret2,ret3;
if (enable == 0) {
ret1 = lmk04828_write_reg(0x168, val & 0xff);
ret2 = lmk04828_write_reg(0x167, (val >> 8) & 0xff);
ret3 = lmk04828_write_reg(0x166, (val >> 16) & 0x03 );
} else {
ret1 = lmk04828_write_reg(0x168, val & 0xff);
ret2 = lmk04828_write_reg(0x167, (val >> 8) & 0xff);
ret3 = lmk04828_write_reg(0x166, ((val >> 16) & 0x03) | 0x04 );
}
if (ret1 >=0 && ret2 >=0 && ret3 >=0) {
return 0;
} else {
return -1;
}
}
int lmk04828_reg_init(void)
{
unsigned char regval =0;
if (0 > lmk04828_write_reg(0, 0x90) ) {
return -1;
}
if (0 > lmk04828_write_reg(0, 0x10) ) {
return -1;
}
if (0 > lmk04828_write_reg(0x14A, 0X33)) {
return -1;
}
if (0 > lmk04828_write_reg(0x145, 0x7F)) {
return -1;
}
if (0 > lmk04828_write_reg(0x171, 0xAA)) {
return -1;
}
if (0 > lmk04828_write_reg(0x172, 0x02)) {
return -1;
}
if (0 > lmk04828_write_reg(0x17C, 21)) {
return -1;
}
if (0 > lmk04828_write_reg(0x17D, 51)) {
return -1;
}
if (0 > lmk04828_reg_pll2_n(1,0x1fff)) {
return -1;
}
return 0;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = lmk04828_open,
.release = lmk04828_release,
.read = lmk04828_read,
.write = lmk04828_write,
.llseek = lmk04828_llseek,
.unlocked_ioctl = lmk04828_ioctl,
};
static int lmk04828_spi_probe(struct spi_device *spi)
{
struct lmk04828_t *lmk04828_data;
struct device_node *np;
u32 addrtmp;
printk("entre prine \n");
lmk04828_data = kzalloc(sizeof(*lmk04828_data), GFP_KERNEL);
if (!lmk04828_data)
return -ENOMEM;
lmk04828_data->spi = spi;
lmk04828_data->speed_hz = SPI_SPEED_HZ;
spin_lock_init(&lmk04828_data->spi_lock);
mutex_init(&lmk04828_data->buf_lock);
INIT_LIST_HEAD(&lmk04828_data->device_entry);
lmk04828_data->misc_dev.fops = &fops;
lmk04828_data->misc_dev.name = "clk-lmk04828";
lmk04828_data->misc_dev.minor = MISC_DYNAMIC_MINOR;
misc_register(&lmk04828_data->misc_dev);
np = of_find_node_by_name(NULL, "lmk04828");
if (NULL == np) {
printk("node lmk04828 not find \n");
return -1;
}
if(0 > of_property_read_u32_index(np, "pl-cs-addr" , 0, &addrtmp) ) {
printk("pl-cs-addr property not find \n");
}
if(0 > of_property_read_u32_index(np, "pl-cs-val" , 0, &lmk04828_data->pl_cs_val)) {
printk("pl-cs-val property not find \n");
}
lmk04828_data->pl_cs_addr = ioremap(addrtmp, 4);
printk("val= %x, addrtmp = %x, ioremap-address= %x \n", lmk04828_data->pl_cs_val,
addrtmp, lmk04828_data->pl_cs_addr);
iowrite32( lmk04828_data->pl_cs_val, lmk04828_data->pl_cs_addr);
lmk04828 = lmk04828_data;
spi_set_drvdata(spi, lmk04828_data);
lmk04828_reg_init();
return 0;
}
static int lmk04828_spi_remove(struct spi_device *spi)
{
struct lmk04828_t *lmk04828_data = spi_get_drvdata(spi);
misc_deregister(&lmk04828_data->misc_dev);
kfree(lmk04828_data);
return 0;
}
static const struct of_device_id lmk04828_dt_ids[] = {
{ .compatible = "lmk04828" },
{},
};
static const struct spi_device_id lmk04828_spi_id[] = {
{"lmk04828"},
{}
};
MODULE_DEVICE_TABLE(spi, lmk04828_spi_id);
static struct spi_driver lmk04828_spi_driver = {
.driver = {
.name = "lmk04828",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(lmk04828_dt_ids),
},
.probe = lmk04828_spi_probe,
.remove = lmk04828_spi_remove,
.id_table = lmk04828_spi_id,
};
module_spi_driver(lmk04828_spi_driver);
MODULE_LICENSE("GPL");
MODULE_ALIAS("spi:04828");
网址:Linux spi设备端驱动编写(基于linux4.9的内核) https://www.yuejiaxmz.com/news/view/640583
相关内容
【TINY4412】LINUX移植笔记:(23)设备树LCD触摸屏驱动【ElfBoard】基于 Linux 的智能家居小项目——开源
基于Linux操作系统的 智能家居的设计
适用于 Linux 的六种优秀自动化和编排工具
事件驱动编程、消息驱动编程、数据驱动编程
spi 认知
基于无线传感器网络的家庭安防报警系统设计
基于STM32的智能家居环境监测系统设计
基于Linux的智能家居的设计(1)
【嵌入式Linux驱动开发】十七、深入学习异步通知,了解按键驱动的另一种方式