Andrzej Hajda | cac47f1 | 2012-11-22 11:39:18 -0300 | [diff] [blame] | 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 | enum spi_direction { |
| 31 | SPI_DIR_RX, |
| 32 | SPI_DIR_TX |
| 33 | }; |
| 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 | |
| 44 | if (dir == SPI_DIR_TX) |
| 45 | xfer.tx_buf = addr; |
| 46 | else |
| 47 | xfer.rx_buf = addr; |
| 48 | |
| 49 | if (spi_dev == NULL) { |
| 50 | dev_err(&spi_dev->dev, "SPI device is uninitialized\n"); |
| 51 | return -ENODEV; |
| 52 | } |
| 53 | |
| 54 | spi_message_init(&msg); |
| 55 | spi_message_add_tail(&xfer, &msg); |
| 56 | |
| 57 | r = spi_sync(spi_dev, &msg); |
| 58 | if (r < 0) |
| 59 | dev_err(&spi_dev->dev, "%s spi_sync failed %d\n", __func__, r); |
| 60 | |
| 61 | return r; |
| 62 | } |
| 63 | |
| 64 | int s5c73m3_spi_write(struct s5c73m3 *state, const void *addr, |
| 65 | const unsigned int len, const unsigned int tx_size) |
| 66 | { |
| 67 | struct spi_device *spi_dev = state->spi_dev; |
| 68 | u32 count = len / tx_size; |
| 69 | u32 extra = len % tx_size; |
| 70 | unsigned int i, j = 0; |
| 71 | u8 padding[32]; |
| 72 | int r = 0; |
| 73 | |
| 74 | memset(padding, 0, sizeof(padding)); |
| 75 | |
Sachin Kamat | 8638a46 | 2013-04-30 05:04:09 -0300 | [diff] [blame^] | 76 | for (i = 0; i < count; i++) { |
Andrzej Hajda | cac47f1 | 2012-11-22 11:39:18 -0300 | [diff] [blame] | 77 | r = spi_xmit(spi_dev, (void *)addr + j, tx_size, SPI_DIR_TX); |
| 78 | if (r < 0) |
| 79 | return r; |
| 80 | j += tx_size; |
| 81 | } |
| 82 | |
| 83 | if (extra > 0) { |
| 84 | r = spi_xmit(spi_dev, (void *)addr + j, extra, SPI_DIR_TX); |
| 85 | if (r < 0) |
| 86 | return r; |
| 87 | } |
| 88 | |
| 89 | return spi_xmit(spi_dev, padding, sizeof(padding), SPI_DIR_TX); |
| 90 | } |
| 91 | |
| 92 | int s5c73m3_spi_read(struct s5c73m3 *state, void *addr, |
| 93 | const unsigned int len, const unsigned int tx_size) |
| 94 | { |
| 95 | struct spi_device *spi_dev = state->spi_dev; |
| 96 | u32 count = len / tx_size; |
| 97 | u32 extra = len % tx_size; |
| 98 | unsigned int i, j = 0; |
| 99 | int r = 0; |
| 100 | |
Sachin Kamat | 8638a46 | 2013-04-30 05:04:09 -0300 | [diff] [blame^] | 101 | for (i = 0; i < count; i++) { |
Andrzej Hajda | cac47f1 | 2012-11-22 11:39:18 -0300 | [diff] [blame] | 102 | r = spi_xmit(spi_dev, addr + j, tx_size, SPI_DIR_RX); |
| 103 | if (r < 0) |
| 104 | return r; |
| 105 | j += tx_size; |
| 106 | } |
| 107 | |
| 108 | if (extra > 0) |
| 109 | return spi_xmit(spi_dev, addr + j, extra, SPI_DIR_RX); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Sylwester Nawrocki | c2668c0 | 2013-02-06 17:35:41 -0300 | [diff] [blame] | 114 | static int s5c73m3_spi_probe(struct spi_device *spi) |
Andrzej Hajda | cac47f1 | 2012-11-22 11:39:18 -0300 | [diff] [blame] | 115 | { |
| 116 | int r; |
| 117 | struct s5c73m3 *state = container_of(spi->dev.driver, struct s5c73m3, |
| 118 | spidrv.driver); |
| 119 | spi->bits_per_word = 32; |
| 120 | |
| 121 | r = spi_setup(spi); |
| 122 | if (r < 0) { |
| 123 | dev_err(&spi->dev, "spi_setup() failed\n"); |
| 124 | return r; |
| 125 | } |
| 126 | |
| 127 | mutex_lock(&state->lock); |
| 128 | state->spi_dev = spi; |
| 129 | mutex_unlock(&state->lock); |
| 130 | |
| 131 | v4l2_info(&state->sensor_sd, "S5C73M3 SPI probed successfully\n"); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Sylwester Nawrocki | c2668c0 | 2013-02-06 17:35:41 -0300 | [diff] [blame] | 135 | static int s5c73m3_spi_remove(struct spi_device *spi) |
Andrzej Hajda | cac47f1 | 2012-11-22 11:39:18 -0300 | [diff] [blame] | 136 | { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int s5c73m3_register_spi_driver(struct s5c73m3 *state) |
| 141 | { |
| 142 | struct spi_driver *spidrv = &state->spidrv; |
| 143 | |
Sylwester Nawrocki | c2668c0 | 2013-02-06 17:35:41 -0300 | [diff] [blame] | 144 | spidrv->remove = s5c73m3_spi_remove; |
Andrzej Hajda | cac47f1 | 2012-11-22 11:39:18 -0300 | [diff] [blame] | 145 | spidrv->probe = s5c73m3_spi_probe; |
| 146 | spidrv->driver.name = S5C73M3_SPI_DRV_NAME; |
| 147 | spidrv->driver.bus = &spi_bus_type; |
| 148 | spidrv->driver.owner = THIS_MODULE; |
| 149 | |
| 150 | return spi_register_driver(spidrv); |
| 151 | } |
| 152 | |
| 153 | void s5c73m3_unregister_spi_driver(struct s5c73m3 *state) |
| 154 | { |
| 155 | spi_unregister_driver(&state->spidrv); |
| 156 | } |