Advertisement
Guest User

Untitled

a guest
Mar 18th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3.  
  4. #include <linux/spi/spi.h>
  5. #include <linux/platform_data/spi-omap2-mcspi.h>
  6.  
  7. #include "spitest.h"
  8.  
  9. #define SPI_BUFF_SIZE (512)
  10.  
  11. const char this_driver_name[] = "spitest";
  12.  
  13. static int spitest_probe(struct spi_device *pdev);
  14. static int spitest_remove(struct spi_device *pdev);
  15.  
  16. static int spitest_do_read(struct spitest_driver_data *drv_data);
  17.  
  18. static struct omap2_mcspi_device_config spitest_mcspi_config = {
  19. .turbo_mode = 1,
  20. };
  21.  
  22. static const struct of_device_id spitest_of_ids[] = {
  23. {
  24. .compatible = "fluxeon,spitest",
  25. },
  26. { },
  27. };
  28. MODULE_DEVICE_TABLE(of, spitest_of_ids);
  29.  
  30. static struct spi_driver spitest_driver =
  31. {
  32. .driver = {
  33. .name = this_driver_name,
  34. .owner = THIS_MODULE,
  35. .of_match_table = spitest_of_ids,
  36. },
  37. .probe = spitest_probe,
  38. .remove = spitest_remove,
  39. };
  40.  
  41. /*******************
  42. * sysfs Fucntions *
  43. * ****************/
  44.  
  45. static ssize_t sysfs_go(struct device *dev, struct device_attribute *attr, const char *buffer, size_t count)
  46. {
  47. unsigned long tmp;
  48. int ret;
  49.  
  50. struct spitest_driver_data *drv_data = dev_get_drvdata(dev);
  51.  
  52. ret = kstrtoul(buffer, 0, &tmp);
  53.  
  54. if (!ret)
  55. {
  56. if (tmp)
  57. {
  58. spitest_do_read(drv_data);
  59. }
  60. }
  61.  
  62. return count;
  63.  
  64. }
  65.  
  66. static DEVICE_ATTR(go, S_IWUSR, NULL, sysfs_go);
  67.  
  68. /***********
  69. * Probing *
  70. * ********/
  71.  
  72. static int spitest_probe(struct spi_device *spi)
  73. {
  74. struct spitest_driver_data *drv_data;
  75.  
  76. // Allocate memory for the driver data struct
  77. drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
  78.  
  79. // Allocate memory for the spi data
  80. drv_data->data_store = kzalloc(sizeof(*drv_data->data_store), GFP_KERNEL);
  81.  
  82. // Allocate tx & rx buffers
  83. drv_data->data_store->rx_buff = kzalloc(SPI_BUFF_SIZE, GFP_KERNEL);
  84. drv_data->data_store->tx_buff = kzalloc(SPI_BUFF_SIZE, GFP_KERNEL);
  85.  
  86. // Set the card spi pointer to the given spi_device
  87. drv_data->spi = spi;
  88.  
  89. pr_debug("%s: SPI Bus %d probed\n", __func__, spi->master->bus_num);
  90.  
  91. spi->bits_per_word = 16;
  92. spi->mode = SPI_MODE_0;
  93. spi->controller_data = &spitest_mcspi_config;
  94.  
  95. spi_setup(drv_data->spi);
  96.  
  97. // Save card struct location to driver data pointer for retrieval later
  98. spi_set_drvdata(spi, drv_data);
  99.  
  100. device_create_file(&spi->dev, &dev_attr_go);
  101.  
  102. pr_debug("%s: Finished probing\n", __func__);
  103.  
  104. return 0;
  105. }
  106.  
  107. static int spitest_remove(struct spi_device *spi)
  108. {
  109. struct spitest_driver_data *drv_data;
  110.  
  111. pr_debug("%s: Removing\n", __func__);
  112.  
  113. drv_data = spi_get_drvdata(spi);
  114.  
  115. if (!IS_ERR(drv_data))
  116. {
  117.  
  118. kfree(drv_data->data_store->rx_buff);
  119. kfree(drv_data->data_store->tx_buff);
  120. kfree(drv_data->data_store);
  121.  
  122. device_remove_file(&spi->dev, &dev_attr_go);
  123.  
  124. kfree(drv_data);
  125. }
  126.  
  127. pr_debug("%s: Removed\n", __func__);
  128.  
  129. return 0;
  130. }
  131.  
  132. /****************
  133. * SPI Transfer *
  134. ****************/
  135.  
  136. static int spitest_do_read(struct spitest_driver_data *drv_data)
  137. {
  138.  
  139. int status;
  140. struct spi_data_store *data_store = drv_data->data_store;
  141.  
  142. spi_message_init(&data_store->msg);
  143.  
  144. data_store->transfer.tx_buf = NULL;
  145. data_store->transfer.rx_buf = data_store->rx_buff;
  146. data_store->transfer.len = SPI_BUFF_SIZE;
  147. data_store->transfer.delay_usecs = 0;
  148.  
  149. spi_message_add_tail(&data_store->transfer, &data_store->msg);
  150.  
  151. status = spi_sync(drv_data->spi, &data_store->msg);
  152.  
  153. if (status < 0)
  154. {
  155. pr_debug("%s: SPI read failed: %d\n",__func__, status);
  156. }
  157.  
  158. pr_info("SPI Read\n");
  159.  
  160.  
  161. return status;
  162.  
  163. }
  164.  
  165. /*****************************************
  166. * Module Intialisation/Deinitialisation *
  167. *****************************************/
  168.  
  169. static int spitest_init(void)
  170. {
  171. pr_debug("%s: Inititialising Fluxeon SPI Test Driver\n", __func__);
  172.  
  173. return spi_register_driver(&spitest_driver);
  174. }
  175.  
  176. static void spitest_exit(void)
  177. {
  178. spi_unregister_driver(&spitest_driver);
  179.  
  180. pr_debug("%s: Unregistering Fluxeon SPI Test Driver\n", __func__);
  181. }
  182.  
  183. module_init(spitest_init);
  184. module_exit(spitest_exit);
  185.  
  186. MODULE_LICENSE("GPL");
  187. MODULE_AUTHOR("Jack Mitchell <jack@embed.me.uk>");
  188. MODULE_DESCRIPTION("Fluxeon SPI Test Driver");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement