Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/spi/spi.h>
- #include <linux/platform_data/spi-omap2-mcspi.h>
- #include "spitest.h"
- #define SPI_BUFF_SIZE (512)
- const char this_driver_name[] = "spitest";
- static int spitest_probe(struct spi_device *pdev);
- static int spitest_remove(struct spi_device *pdev);
- static int spitest_do_read(struct spitest_driver_data *drv_data);
- static struct omap2_mcspi_device_config spitest_mcspi_config = {
- .turbo_mode = 1,
- };
- static const struct of_device_id spitest_of_ids[] = {
- {
- .compatible = "fluxeon,spitest",
- },
- { },
- };
- MODULE_DEVICE_TABLE(of, spitest_of_ids);
- static struct spi_driver spitest_driver =
- {
- .driver = {
- .name = this_driver_name,
- .owner = THIS_MODULE,
- .of_match_table = spitest_of_ids,
- },
- .probe = spitest_probe,
- .remove = spitest_remove,
- };
- /*******************
- * sysfs Fucntions *
- * ****************/
- static ssize_t sysfs_go(struct device *dev, struct device_attribute *attr, const char *buffer, size_t count)
- {
- unsigned long tmp;
- int ret;
- struct spitest_driver_data *drv_data = dev_get_drvdata(dev);
- ret = kstrtoul(buffer, 0, &tmp);
- if (!ret)
- {
- if (tmp)
- {
- spitest_do_read(drv_data);
- }
- }
- return count;
- }
- static DEVICE_ATTR(go, S_IWUSR, NULL, sysfs_go);
- /***********
- * Probing *
- * ********/
- static int spitest_probe(struct spi_device *spi)
- {
- struct spitest_driver_data *drv_data;
- // Allocate memory for the driver data struct
- drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
- // Allocate memory for the spi data
- drv_data->data_store = kzalloc(sizeof(*drv_data->data_store), GFP_KERNEL);
- // Allocate tx & rx buffers
- drv_data->data_store->rx_buff = kzalloc(SPI_BUFF_SIZE, GFP_KERNEL);
- drv_data->data_store->tx_buff = kzalloc(SPI_BUFF_SIZE, GFP_KERNEL);
- // Set the card spi pointer to the given spi_device
- drv_data->spi = spi;
- pr_debug("%s: SPI Bus %d probed\n", __func__, spi->master->bus_num);
- spi->bits_per_word = 16;
- spi->mode = SPI_MODE_0;
- spi->controller_data = &spitest_mcspi_config;
- spi_setup(drv_data->spi);
- // Save card struct location to driver data pointer for retrieval later
- spi_set_drvdata(spi, drv_data);
- device_create_file(&spi->dev, &dev_attr_go);
- pr_debug("%s: Finished probing\n", __func__);
- return 0;
- }
- static int spitest_remove(struct spi_device *spi)
- {
- struct spitest_driver_data *drv_data;
- pr_debug("%s: Removing\n", __func__);
- drv_data = spi_get_drvdata(spi);
- if (!IS_ERR(drv_data))
- {
- kfree(drv_data->data_store->rx_buff);
- kfree(drv_data->data_store->tx_buff);
- kfree(drv_data->data_store);
- device_remove_file(&spi->dev, &dev_attr_go);
- kfree(drv_data);
- }
- pr_debug("%s: Removed\n", __func__);
- return 0;
- }
- /****************
- * SPI Transfer *
- ****************/
- static int spitest_do_read(struct spitest_driver_data *drv_data)
- {
- int status;
- struct spi_data_store *data_store = drv_data->data_store;
- spi_message_init(&data_store->msg);
- data_store->transfer.tx_buf = NULL;
- data_store->transfer.rx_buf = data_store->rx_buff;
- data_store->transfer.len = SPI_BUFF_SIZE;
- data_store->transfer.delay_usecs = 0;
- spi_message_add_tail(&data_store->transfer, &data_store->msg);
- status = spi_sync(drv_data->spi, &data_store->msg);
- if (status < 0)
- {
- pr_debug("%s: SPI read failed: %d\n",__func__, status);
- }
- pr_info("SPI Read\n");
- return status;
- }
- /*****************************************
- * Module Intialisation/Deinitialisation *
- *****************************************/
- static int spitest_init(void)
- {
- pr_debug("%s: Inititialising Fluxeon SPI Test Driver\n", __func__);
- return spi_register_driver(&spitest_driver);
- }
- static void spitest_exit(void)
- {
- spi_unregister_driver(&spitest_driver);
- pr_debug("%s: Unregistering Fluxeon SPI Test Driver\n", __func__);
- }
- module_init(spitest_init);
- module_exit(spitest_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Jack Mitchell <jack@embed.me.uk>");
- MODULE_DESCRIPTION("Fluxeon SPI Test Driver");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement