s5c73m3-spi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Samsung LSI S5C73M3 8M pixel camera driver
  3. *
  4. * Copyright (C) 2012, Samsung Electronics, Co., Ltd.
  5. * Sylwester Nawrocki <s.nawrocki@samsung.com>
  6. * Andrzej Hajda <a.hajda@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/sizes.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/media.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/spi/spi.h>
  24. #include "s5c73m3.h"
  25. #define S5C73M3_SPI_DRV_NAME "S5C73M3-SPI"
  26. static const struct of_device_id s5c73m3_spi_ids[] = {
  27. { .compatible = "samsung,s5c73m3" },
  28. { }
  29. };
  30. MODULE_DEVICE_TABLE(of, s5c73m3_spi_ids);
  31. enum spi_direction {
  32. SPI_DIR_RX,
  33. SPI_DIR_TX
  34. };
  35. static int spi_xmit(struct spi_device *spi_dev, void *addr, const int len,
  36. enum spi_direction dir)
  37. {
  38. struct spi_message msg;
  39. int r;
  40. struct spi_transfer xfer = {
  41. .len = len,
  42. };
  43. if (dir == SPI_DIR_TX)
  44. xfer.tx_buf = addr;
  45. else
  46. xfer.rx_buf = addr;
  47. if (spi_dev == NULL) {
  48. pr_err("SPI device is uninitialized\n");
  49. return -ENODEV;
  50. }
  51. spi_message_init(&msg);
  52. spi_message_add_tail(&xfer, &msg);
  53. r = spi_sync(spi_dev, &msg);
  54. if (r < 0)
  55. dev_err(&spi_dev->dev, "%s spi_sync failed %d\n", __func__, r);
  56. return r;
  57. }
  58. int s5c73m3_spi_write(struct s5c73m3 *state, const void *addr,
  59. const unsigned int len, const unsigned int tx_size)
  60. {
  61. struct spi_device *spi_dev = state->spi_dev;
  62. u32 count = len / tx_size;
  63. u32 extra = len % tx_size;
  64. unsigned int i, j = 0;
  65. u8 padding[32];
  66. int r = 0;
  67. memset(padding, 0, sizeof(padding));
  68. for (i = 0; i < count; i++) {
  69. r = spi_xmit(spi_dev, (void *)addr + j, tx_size, SPI_DIR_TX);
  70. if (r < 0)
  71. return r;
  72. j += tx_size;
  73. }
  74. if (extra > 0) {
  75. r = spi_xmit(spi_dev, (void *)addr + j, extra, SPI_DIR_TX);
  76. if (r < 0)
  77. return r;
  78. }
  79. return spi_xmit(spi_dev, padding, sizeof(padding), SPI_DIR_TX);
  80. }
  81. int s5c73m3_spi_read(struct s5c73m3 *state, void *addr,
  82. const unsigned int len, const unsigned int tx_size)
  83. {
  84. struct spi_device *spi_dev = state->spi_dev;
  85. u32 count = len / tx_size;
  86. u32 extra = len % tx_size;
  87. unsigned int i, j = 0;
  88. int r = 0;
  89. for (i = 0; i < count; i++) {
  90. r = spi_xmit(spi_dev, addr + j, tx_size, SPI_DIR_RX);
  91. if (r < 0)
  92. return r;
  93. j += tx_size;
  94. }
  95. if (extra > 0)
  96. return spi_xmit(spi_dev, addr + j, extra, SPI_DIR_RX);
  97. return 0;
  98. }
  99. static int s5c73m3_spi_probe(struct spi_device *spi)
  100. {
  101. int r;
  102. struct s5c73m3 *state = container_of(spi->dev.driver, struct s5c73m3,
  103. spidrv.driver);
  104. spi->bits_per_word = 32;
  105. r = spi_setup(spi);
  106. if (r < 0) {
  107. dev_err(&spi->dev, "spi_setup() failed\n");
  108. return r;
  109. }
  110. mutex_lock(&state->lock);
  111. state->spi_dev = spi;
  112. mutex_unlock(&state->lock);
  113. v4l2_info(&state->sensor_sd, "S5C73M3 SPI probed successfully\n");
  114. return 0;
  115. }
  116. static int s5c73m3_spi_remove(struct spi_device *spi)
  117. {
  118. return 0;
  119. }
  120. int s5c73m3_register_spi_driver(struct s5c73m3 *state)
  121. {
  122. struct spi_driver *spidrv = &state->spidrv;
  123. spidrv->remove = s5c73m3_spi_remove;
  124. spidrv->probe = s5c73m3_spi_probe;
  125. spidrv->driver.name = S5C73M3_SPI_DRV_NAME;
  126. spidrv->driver.of_match_table = s5c73m3_spi_ids;
  127. return spi_register_driver(spidrv);
  128. }
  129. void s5c73m3_unregister_spi_driver(struct s5c73m3 *state)
  130. {
  131. spi_unregister_driver(&state->spidrv);
  132. }