The SPI and IIC drivers under Linux are not programmed to add device information on the device tree

Source: Internet
Author: User
Tags goto

When writing drivers, it is generally necessary to add node information to the device tree, which provides a way to add device information directly to the drive.

The drive template for I²c is as follows

#include <linux/module.h> #include <linux/i2c.h> #define Sensor_bus_num 0#define sensor_slave_address 0x3e #define SENSOR_NAME "SENSOR" struct i2c_client *sensor_client=null;static int sensor_probe (struct i2c_client *client, const struct I2C_DEVICE_ID *id) {    sensor_client=client;    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 (I²c, sensor_id); static struct I2c_driver sensor_driver = {   . Driver = {             . Name = sensor_name,   },     .probe    = sensor_probe,    .remove   = sensor_remove,    . id_table = sensor_id,};static struct I2c_board_info sensor_device = {    I2c_board_info (" HMC5883L-I2C ", 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 ("I²c adapter%d\n", Sensor_bus_num);         return-enodev;   } else {        PRINTK ("Get Ii2 Adapter%d ok\n", sensor_bus_num);        client = I2c_new_device (ADAP, & Amp;sensor_device);   }    if (!client) {        PRINTK ("Get i²c client%s @ 0x%02x fail!\n", sensor_device.type,                 sensor_device.addr);        return -enodev;   } else {        printk ("GET i²c client ok!\n ");   }    i2c_put_adapter (ADAP);    i2c_add_driver (&sensor_driver);   &NBSP;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); Odule_author ("GPL"); Odule_license ("GPL");


The SPI driver template is as follows

#include <linux/module.h> #include <linux/spi/spi.h> #define DEVICE_NAME "sensor" #define Sensor_spi_bus 0struct spi_device *sensor_spi=null;int sensor_spi_write (void) {return 0;} int Sensor_spi_read (void) {return 0;} static const struct SPI_DEVICE_ID sensor_spi_id[] = {{device_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;} static struct Spi_driver sensor_driver = {. Driver = {. Name = Device_name,.owner = This_module,},.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 = device_name,. Mode = 0x00,. Bus_num = 0,. Chip_select = 0,. max_speed_hz = 2000000,};spi_register_driver (&sens Or_driver), if (status<0) {Pr_err ("%s:spi_register_driver spi_driver failure. Status =%d\n ", __func__, 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: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");

The SPI and IIC drivers under Linux are not programmed to add device information on the device tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.