Example of driver module using SPI bus

Source: Internet
Author: User

Two days ago, I wrote a <I2C bus example for the driver module>. Because SPI is similar to I2C and there is idle time, the SPI module is implemented by referring to the previously written I2C module. The Code is as follows. This code has not been debugged. My current board does not use the SPI interface, but the overall architecture should be correct.

# Include <Linux/init. h> <br/> # include <Linux/kernel. h> <br/> # include <Linux/module. h> <br/> # include <Linux/init. h> <br/> # include <Linux/errno. h> <br/> # include <Linux/init. h> <br/> # include <Linux/list. h> <br/> # include <Linux/smp_lock.h> <br/> # include <Linux/SPI. h> </P> <p> struct spi_api {<br/> struct list_head list; <br/> struct spi_device * SPI; <br/> }; </P> <p> # define spi_minors 32 <br/> # define SPI _ Mode_mask (spi_cpha | spi_cpol | spi_cs_high/<br/> | spi_lsb_first | spi_3wire | spi_loop/<br/> | spi_no_cs | spi_ready) </P> <p> static list_head (spi_api_list); <br/> static define_spinlock (spi_api_list_lock); </P> <p> static struct spi_api * get_spi_api (INT bus_id) <br/>{< br/> struct spi_api * spi_api; </P> <p> spin_lock (& spi_api_list_lock); <br/> list_for_each_entry (spi_api, & spi_api_list, list) {<br/> If (spi_api-> SPI-> master-> bus_num = bus_id) <br/> goto found; <br/>}< br/> spi_api = NULL; </P> <p> found: <br/> spin_unlock (& spi_api_list_lock); <br/> return spi_api; <br/>}</P> <p> static struct spi_api * add_spi_api (struct spi_device * SPI) <br/>{< br/> struct spi_api * spi_api; </P> <p> If (SPI-> master-> bus_num> = spi_minors) {<br/> printk (kern_err "spi_api: Out of device minors (% d) /n ", <br/> SPI-> master-> Bu S_num); <br/> return NULL; <br/>}</P> <p> spi_api = kzarloc (sizeof (* spi_api), gfp_kernel ); <br/> If (! Spi_api) <br/> return NULL; <br/> spi_api-> SPI = SPI; </P> <p> spin_lock (& spi_api_list_lock ); <br/> list_add_tail (& spi_api-> list, & spi_api_list); <br/> spin_unlock (& spi_api_list_lock); <br/> return spi_api; <br/>}</P> <p> static void del_spi_api (struct spi_api * spi_api) <br/>{< br/> spin_lock (& spi_api_list_lock ); <br/> list_del (& spi_api-> list); <br/> spin_unlock (& spi_api_list_lock); <br/> kfree (spi_api); <br/>}< /P> <p> static int spi_api_do_set (INT bus_id, <br/> u8 mode, <br/> u8 bits_per_word, <br/> u8 max_speed_hz) <br/>{< br/> struct spi_device * SPI; <br/> struct spi_api * spi_api = get_spi_api (bus_id); <br/> If (! Spi_api) <br/> return-enodev; </P> <p> SPI = spi_api-> SPI; <br/> SPI-> mode & = ~ Spi_mode_mask; <br/> SPI-> mode | = mode; <br/> SPI-> bits_per_word = bits_per_word; <br/> SPI-> max_speed_hz = max_speed_hz; <br/> return spi_setup (SPI); <br/>}</P> <p> static int spi_api_do_write (INT bus_id, const u8 * Buf, size_t Len) <br/>{< br/> struct spi_api * spi_api = get_spi_api (bus_id); <br/> If (! Spi_api) <br/> return-enodev; <br/> return spi_write (spi_api-> SPI, Buf, Len ); <br/>}</P> <p> static int spi_api_do_read (INT bus_id, u8 * Buf, size_t Len) <br/>{< br/> struct spi_api * spi_api = get_spi_api (bus_id); <br/> If (! Spi_api) <br/> return-enodev; <br/> return spi_read (spi_api-> SPI, Buf, Len ); <br/>}</P> <p> static int spi_api_do_write_then_read (INT bus_id, <br/> const u8 * txbuf, unsigned n_tx, <br/> u8 * rxbuf, unsigned n_rx) <br/>{< br/> struct spi_api * spi_api = get_spi_api (bus_id); <br/> If (! Spi_api) <br/> return-enodev; <br/> return spi_write_then_read (spi_api-> SPI, txbuf, n_tx, rxbuf, n_rx ); <br/>}</P> <p> static int spi_api_probe (struct spi_device * SPI) <br/>{< br/> add_spi_api (SPI ); <br/> printk (kern_info "spi_api_probe device [% d]/n", SPI-> master-> bus_num); <br/> return 0; <br/>}</P> <p> static int spi_api_remove (struct spi_device * SPI) <br/>{< br/> struct spi_api * spi_api = get_spi_api (SPI-> master-> bus_num); <br/> If (spi_api) <br/> del_spi_api (spi_api); <br/> return 0; <br/>}</P> <p> static struct spi_driver spi_api_driver ={< br/>. driver ={< br/>. name = "SPI-API", <br/>. owner = this_module, <br/>}, <br/>. probe = spi_api_probe, <br/>. remove = spi_api_remove, <br/>}; </P> <p> static int _ init spi_api_init (void) <br/>{< br/> int ret = spi_register_driver (& spi_api_driver); </P> <p> If (RET) {<br/> printk (kern_err "[% s] driver regiled failed, module not inserted. /n ", _ FUNC _); <br/> return ret; <br/>}< br/> printk (" spi_api_init/N "); <br/> return 0; <br/>}</P> <p> static void _ exit spi_api_exit (void) <br/>{< br/> spi_unregister_driver (& spi_api_driver); <br/>}</P> <p> module_author ("loon, <sepnic@gmail.com> "); <br/> module_description ("SPI spi_api driver"); <br/> module_license ("GPL"); </P> <p> module_init (spi_api_init ); <br/> module_exit (spi_api_exit); </P> <p> export_symbol_gpl (signature); <br/> export_symbol_gpl (signature ); <br/> export_symbol_gpl (spi_api_do_write_then_read );

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.