Linux下SPI和IIC驅動免在裝置樹上新增裝置資訊的編寫方法
阿新 • • 發佈:2018-12-22
編寫i2c或spi驅動時,一般需要往裝置樹上(或者板級檔案)新增節點資訊,這裡提供一種直接在驅動中新增裝置資訊的方法,使驅動更方便移植。
i2c的驅動模板如下
#include <linux/module.h> #include <linux/i2c.h> #define SENSOR_BUS_NUM 0 //該裝置在第幾路iic上,根據硬體具體修改 #define SENSOR_SLAVE_ADDRESS 0x3e//裝置地址,注意是7位地址(非8位地址,linux中會自動新增讀寫位) #define SENSOR_NAME "sensor" //裝置的名字 struct i2c_client *sensor_client=NULL; static int sensor_i2c_write(char *buf,int length) { #if 1 int ret; ret=i2c_master_send(sensor_client, buf, length); if(ret!=length) { pr_err("sensor_i2c_write: i2c_master_send error\n"); return -EIO; } else return 0; #endif #if 0 struct i2c_msg msg= { .addr = sensor_client->addr, .flags = 0, .buf = buf, .len = length }; if(i2c_transfer(sensor_client->adapter, &msg, 1)<0) { pr_err("sensor_i2c_write: transfer error\n"); return -EIO; } else return 0; #endif } static int sensor_i2c_read(char *buf,int length) { #if 1 int ret; ret=i2c_master_recv(sensor_client,buf,length); if(ret!=length) { pr_err("sensor_i2c_read: i2c_master_recv error\n"); return -EIO; } else return 0; #endif #if 0 struct i2c_msg msg[] = { { .addr = sensor_client->addr, .flags = 0, .buf = buf, .len = 1 }, { .addr = sensor_client->addr, .flags = I2C_M_RD, .buf = buf, .len = 1 } }; if(i2c_transfer(sensor_client->adapter, msg, 2)<0) { pr_err("sensor_i2c_read: transfer error\n"); return -EIO; } else return 0; #endif } void sensor_config(void) { char buf[10]; int ret; buf[0]=0x28; buf[1]=0x88; ret=sensor_i2c_write(buf,1); if(ret==0) printk("write reg %d value %d ok.",buf[0],buf[1]); buf[0]=0x28; sensor_i2c_read(buf+1,1); printk("reg 0x%x value is 0x%x",buf[0],buf[1]); } static int sensor_probe(struct i2c_client *client,const struct i2c_device_id *id) { sensor_client=client; sensor_config(); return 0; } static int sensor_remove(struct i2c_client *client) { return 0; } static const struct i2c_device_id sensor_id[] = { {SENSOR_NAME, 0}, { } }; MODULE_DEVICE_TABLE(i2c, sensor_id); #ifdef CONFIG_PM static int sensor_suspend(struct device *dev) { return 0; } static int sensor_resume(struct device *dev) { return 0; } #endif static const struct dev_pm_ops sensor_ops = { .suspend = sensor_suspend, .resume = sensor_resume, }; static struct i2c_driver sensor_driver = { .driver = { .name = SENSOR_NAME, .owner = THIS_MODULE, #ifdef CONFIG_PM .pm = &sensor_ops, #endif }, .probe = sensor_probe, .remove = sensor_remove, .id_table = sensor_id, }; static struct i2c_board_info sensor_device = { I2C_BOARD_INFO(SENSOR_NAME, SENSOR_SLAVE_ADDRESS), }; static int __init sensor_init(void) { struct i2c_adapter *adap; struct i2c_client *client; adap = i2c_get_adapter(SENSOR_BUS_NUM); if (!adap) { printk("i2c adapter %d\n",SENSOR_BUS_NUM); return -ENODEV; } else { printk("get i2c adapter %d ok\n", SENSOR_BUS_NUM); client = i2c_new_device(adap, &sensor_device); } if (!client) { printk("get i2c client %s @ 0x%02x fail!\n", sensor_device.type, sensor_device.addr); return -ENODEV; } else { printk("get i2c client ok!\n"); } i2c_put_adapter(adap); i2c_add_driver(&sensor_driver); printk("sensor init success!\n"); return 0; } static void __exit sensor_exit(void) { i2c_del_driver(&sensor_driver); if(sensor_client!=NULL) i2c_unregister_device(sensor_client); printk("Module removed\n"); } module_init(sensor_init); module_exit(sensor_exit); MODULE_AUTHOR("GPL"); MODULE_LICENSE("GPL");
spi的驅動模板如下
#include <linux/module.h> #include <linux/spi/spi.h> #define SENSOR_NAME "sensor" #define SENSOR_SPI_BUS 0 #define SPI_MODE 0 #define SENSOR_MAX_SPEED 2*1000*1000 struct spi_device *sensor_spi=NULL; int sensor_spi_write(unsigned int addr, unsigned int val, size_t len) { int status; unsigned char write_buf[2]; write_buf[0] = addr; write_buf[1] = val; status = spi_write(sensor_spi, write_buf,2); if (status) dev_err(&sensor_spi->dev, "%s error %d\n", __FUNCTION__, status); return status; } int sensor_spi_read(unsigned int addr, unsigned int *val, size_t len) { struct spi_message message; struct spi_transfer x[1]; int status; unsigned char write_buf[10]; unsigned char read_buf[10]; write_buf[0] =addr; spi_message_init(&message); memset(x, 0, sizeof(x)); x[0].len = len; x[0].tx_buf = write_buf; x[0].rx_buf = read_buf; spi_message_add_tail(&x[0], &message); status = spi_sync(sensor_spi, &message); if(status!=0) dev_err(&sensor_spi->dev, "%s error %d\n", __FUNCTION__, status); else *val =read_buf[4]; return status; } static const struct spi_device_id sensor_spi_id[] = { { SENSOR_NAME, 0 }, { } }; MODULE_DEVICE_TABLE(spi, sensor_spi_id); static int sensor_probe(struct spi_device *spi) { sensor_spi=spi; return 0; } static int sensor_remove(struct spi_device *spi) { return 0; } #ifdef CONFIG_PM static int sensor_suspend(struct device *dev) { return 0; } static int sensor_resume(struct device *dev) { return 0; } static const struct dev_pm_ops sensor_ops = { .suspend = sensor_suspend, .resume = sensor_resume, }; #endif static struct spi_driver sensor_driver = { .driver = { .name = SENSOR_NAME, .owner = THIS_MODULE, #ifdef CONFIG_PM .pm = &sensor_ops, #endif }, .probe = sensor_probe, .remove = sensor_remove, .id_table = sensor_spi_id, }; static __init int sensor_spi_init(void) { int status=-1; struct spi_master *master; struct spi_device *spi; struct spi_board_info chip = { .modalias = SENSOR_NAME, .mode = SPI_MODE, .bus_num = SENSOR_SPI_BUS, .chip_select = 0, .max_speed_hz = SENSOR_MAX_SPEED, }; spi_register_driver(&sensor_driver); if (status<0) { pr_err("%s: spi_register_driver spi_driver failure. status = %d\n", __func__, status); return status; } pr_err("%s: spi_register_driver spi_driver success. status = %d\n", __func__, status); master = spi_busnum_to_master(SENSOR_SPI_BUS); if (!master) { status = -ENODEV; goto error_busnum; } spi = spi_new_device(master, &chip); if (!spi) { status = -EBUSY; goto error_mem; } return status; error_mem: error_busnum: printk("register spi device err!\n"); spi_unregister_driver(&sensor_driver); return status; } static __exit void sensor_spi_exit(void) { spi_unregister_driver(&sensor_driver); if(sensor_spi!=NULL) spi_unregister_device(sensor_spi); } module_init(sensor_spi_init); module_exit(sensor_spi_exit); MODULE_LICENSE("GPL v2");