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 18 #include <linux/sizes.h> 19 #include <linux/delay.h> 20 #include <linux/init.h> 21 #include <linux/media.h> 22 #include <linux/module.h> 23 #include <linux/slab.h> 24 #include <linux/spi/spi.h> 25 26 #include "s5c73m3.h" 27 28 #define S5C73M3_SPI_DRV_NAME "S5C73M3-SPI" 29 30 static const struct of_device_id s5c73m3_spi_ids[] = { 31 { .compatible = "samsung,s5c73m3" }, 32 { } 33 }; 34 MODULE_DEVICE_TABLE(of, s5c73m3_spi_ids); 35 36 enum spi_direction { 37 SPI_DIR_RX, 38 SPI_DIR_TX 39 }; 40 MODULE_DEVICE_TABLE(of, s5c73m3_spi_ids); 41 42 static int spi_xmit(struct spi_device *spi_dev, void *addr, const int len, 43 enum spi_direction dir) 44 { 45 struct spi_message msg; 46 int r; 47 struct spi_transfer xfer = { 48 .len = len, 49 }; 50 51 if (dir == SPI_DIR_TX) 52 xfer.tx_buf = addr; 53 else 54 xfer.rx_buf = addr; 55 56 if (spi_dev == NULL) { 57 pr_err("SPI device is uninitialized\n"); 58 return -ENODEV; 59 } 60 61 spi_message_init(&msg); 62 spi_message_add_tail(&xfer, &msg); 63 64 r = spi_sync(spi_dev, &msg); 65 if (r < 0) 66 dev_err(&spi_dev->dev, "%s spi_sync failed %d\n", __func__, r); 67 68 return r; 69 } 70 71 int s5c73m3_spi_write(struct s5c73m3 *state, const void *addr, 72 const unsigned int len, const unsigned int tx_size) 73 { 74 struct spi_device *spi_dev = state->spi_dev; 75 u32 count = len / tx_size; 76 u32 extra = len % tx_size; 77 unsigned int i, j = 0; 78 u8 padding[32]; 79 int r = 0; 80 81 memset(padding, 0, sizeof(padding)); 82 83 for (i = 0; i < count; i++) { 84 r = spi_xmit(spi_dev, (void *)addr + j, tx_size, SPI_DIR_TX); 85 if (r < 0) 86 return r; 87 j += tx_size; 88 } 89 90 if (extra > 0) { 91 r = spi_xmit(spi_dev, (void *)addr + j, extra, SPI_DIR_TX); 92 if (r < 0) 93 return r; 94 } 95 96 return spi_xmit(spi_dev, padding, sizeof(padding), SPI_DIR_TX); 97 } 98 99 int s5c73m3_spi_read(struct s5c73m3 *state, void *addr, 100 const unsigned int len, const unsigned int tx_size) 101 { 102 struct spi_device *spi_dev = state->spi_dev; 103 u32 count = len / tx_size; 104 u32 extra = len % tx_size; 105 unsigned int i, j = 0; 106 int r = 0; 107 108 for (i = 0; i < count; i++) { 109 r = spi_xmit(spi_dev, addr + j, tx_size, SPI_DIR_RX); 110 if (r < 0) 111 return r; 112 j += tx_size; 113 } 114 115 if (extra > 0) 116 return spi_xmit(spi_dev, addr + j, extra, SPI_DIR_RX); 117 118 return 0; 119 } 120 121 static int s5c73m3_spi_probe(struct spi_device *spi) 122 { 123 int r; 124 struct s5c73m3 *state = container_of(spi->dev.driver, struct s5c73m3, 125 spidrv.driver); 126 spi->bits_per_word = 32; 127 128 r = spi_setup(spi); 129 if (r < 0) { 130 dev_err(&spi->dev, "spi_setup() failed\n"); 131 return r; 132 } 133 134 mutex_lock(&state->lock); 135 state->spi_dev = spi; 136 mutex_unlock(&state->lock); 137 138 v4l2_info(&state->sensor_sd, "S5C73M3 SPI probed successfully\n"); 139 return 0; 140 } 141 142 static int s5c73m3_spi_remove(struct spi_device *spi) 143 { 144 return 0; 145 } 146 147 int s5c73m3_register_spi_driver(struct s5c73m3 *state) 148 { 149 struct spi_driver *spidrv = &state->spidrv; 150 151 spidrv->remove = s5c73m3_spi_remove; 152 spidrv->probe = s5c73m3_spi_probe; 153 spidrv->driver.name = S5C73M3_SPI_DRV_NAME; 154 spidrv->driver.of_match_table = s5c73m3_spi_ids; 155 156 return spi_register_driver(spidrv); 157 } 158 159 void s5c73m3_unregister_spi_driver(struct s5c73m3 *state) 160 { 161 spi_unregister_driver(&state->spidrv); 162 } 163