Linux下常用的spi flash驱动程序demo
时间: 2024-02-28 18:35:03 浏览: 188
demo_driver.rar_DEMO_内核_内核移植
以下是一个Linux下常用的SPI Flash驱动程序demo,供参考:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/spi/spi.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#define SPINOR_OP_RDID 0x9f
struct spinor_chip {
struct spi_device *spi;
struct mtd_info *mtd;
};
static int spinor_read_id(struct spinor_chip *chip, u8 *id)
{
struct spi_transfer t = {
.tx_buf = &SPINOR_OP_RDID,
.rx_buf = id,
.len = 3,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spi_sync(chip->spi, &m);
}
static int spinor_probe(struct spi_device *spi)
{
struct spinor_chip *chip;
int ret;
u8 id[3];
chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
chip->spi = spi;
ret = spinor_read_id(chip, id);
if (ret)
return ret;
spi_set_drvdata(spi, chip);
chip->mtd = mtd_device_parse_register(&spi->dev, NULL, NULL, NULL, 0, NULL);
if (IS_ERR(chip->mtd)) {
dev_err(&spi->dev, "failed to register MTD device\n");
return PTR_ERR(chip->mtd);
}
return 0;
}
static int spinor_remove(struct spi_device *spi)
{
struct spinor_chip *chip = spi_get_drvdata(spi);
mtd_device_unregister(chip->mtd);
return 0;
}
static const struct of_device_id spinor_of_match[] = {
{ .compatible = "spi-flash" },
{ },
};
MODULE_DEVICE_TABLE(of, spinor_of_match);
static struct spi_driver spinor_driver = {
.driver = {
.name = "spinor",
.owner = THIS_MODULE,
.of_match_table = spinor_of_match,
},
.probe = spinor_probe,
.remove = spinor_remove,
};
module_spi_driver(spinor_driver);
MODULE_AUTHOR("Your Name");
MODULE_LICENSE("GPL");
阅读全文