增加 SPI 设备驱动示例.

Signed-off-by: rick.chan <chenyang@autoai.com>
This commit is contained in:
rick.chan 2020-12-16 15:12:52 +08:00
parent 12048b9dd9
commit c524f83562
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,14 @@
obj-m:= \
demospi.o
demoplatform-objs:= \
demo_spi.o
EXTRA_CFLAGS += \
-I$(PWD)
all:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

View File

@ -0,0 +1,68 @@
#include <linux/spi/spi.h>
#include <linux/of.h>
#include <linux/regmap.h>
struct demo_spi_private {
struct regmap *regmap;
};
static const struct regmap_config demo_regmap = {
.reg_bits = 8,
.val_bits = 8,
};
static int demo_spi_probe(struct spi_device *spi)
{
struct demo_spi_private *demo_spi;
int ret;
demo_spi = devm_kzalloc(&spi->dev, sizeof(struct demo_spi_private),
GFP_KERNEL);
if (demo_spi == NULL)
return -ENOMEM;
spi->bits_per_word = 8;
spi->mode = SPI_MODE_0;
spi->max_speed_hz = 1000000;
demo_spi->regmap = devm_regmap_init_spi(spi, &demo_regmap);
if (IS_ERR(demo_spi->regmap))
return PTR_ERR(demo_spi->regmap);
spi_set_drvdata(spi, demo_spi);
return 0;
}
static int demo_spi_remove(struct spi_device *spi)
{
return 0;
}
static const struct of_device_id demo_of_match[] = {
{ .compatible = "spi,demo", },
{ }
};
MODULE_DEVICE_TABLE(of, demo_of_match);
static const struct spi_device_id demo_id_table[] = {
{ "spidemo", 0 },
{ }
};
MODULE_DEVICE_TABLE(spi, demo_id_table);
static struct spi_driver demo_spi_driver = {
.driver = {
.name = "demo_spi",
.of_match_table = demo_of_match,
},
.id_table = demo_id_table,
.probe = demo_spi_probe,
.remove = demo_spi_remove,
};
module_spi_driver(demo_spi_driver);
MODULE_AUTHOR("Rick Chan <cy187lion@sina.com>");
MODULE_DESCRIPTION("SPI driver demo");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.0");