world leader in high performance signal processing
Trace: » example_dma_spi

Sample SPI DMA Driver

This doc is under construction.

This driver creates a char device to access a AD7476A ADC ( Analog to Digital Converter ) ( see hardware_platform ) attached to the SPI controller.

The device is an Analog to Digital Converter and the device driver reads a number of samples from the ADC and then returned the samples to a user function.

This driver shows the usagage of the common spi framework. The kernel provide an interface for SPI drivers in linix-2.6.x/drivers/spi/. There are several drivers that can be used for reference: linix-2.6.x/drivers/char/bfin_spi_adc.c which working in DMA mode, and linux-2.6.x/drivers/spi/spidev.c or linux-2.6.x/drivers/mtd/devices/m25p80.c, which works in PIO mode.

Init Code

The first code sample to inspect is the init code for the driver.

file: drivers/char/bfin_spi_adc.c

scm failed with exit code 1:
file does not exist in git

“spi_register_driver()” regiser this driver to the spi bus driver.

file: drivers/char/bfin_spi_adc.c

scm failed with exit code 1:
file does not exist in git

FOPS Table

The next thing to look at is the file operations table Note the following

  • open, read, write, release functions as expected
  • IOCTL function
  • fasync notification function
static struct file_operations spi_fops = {
    owner:      THIS_MODULE,
    read:       adc_spi_read,
    write:      adc_spi_write,
    ioctl:      adc_spi_ioctl,
    open:       adc_spi_open,
    release:    adc_spi_release,
};

This provides the basic API for the driver.

Open Function

The spi_open function should be inspected next. Note the following in the driver:

  • There is a MINOR number check
  • only one user at a time

file: drivers/char/bfin_spi_adc.c

scm failed with exit code 1:
file does not exist in git

Read Function

In looking at this driver you can then proceed to the read but remember that there was an interrupt function attached to the DMA 5 completion.

Here is a section of the read function. Note the following:

  • use dma_alloc_coherent to allocate a buffer
  • a device structure is used to control the device
  • the spi device is set up

file: drivers/char/bfin_spi_adc.c

scm failed with exit code 1:
file does not exist in git

IOCTL

The IOCTL function provides a means for the user space program to modify the device function. Only part is produced here.

Each cmd is processed by a case statement

file: drivers/char/bfin_spi_adc.c

scm failed with exit code 1:
file does not exist in git

Conclusions

The basic data flow can be traced in this example.

The user code will open the device. Then the characteristics of the SPI device will be set up using IOCTL functions. Refer to the actual code for more details.