1 // SPDX-License-Identifier: GPL-2.0-only
3 * Samsung LSI S5C73M3 8M pixel camera driver
5 * Copyright (C) 2012, Samsung Electronics, Co., Ltd.
6 * Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * Andrzej Hajda <a.hajda@samsung.com>
10 #include <linux/sizes.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/media.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/spi/spi.h>
20 #define S5C73M3_SPI_DRV_NAME "S5C73M3-SPI"
22 static const struct of_device_id s5c73m3_spi_ids[] = {
23 { .compatible = "samsung,s5c73m3" },
26 MODULE_DEVICE_TABLE(of, s5c73m3_spi_ids);
33 static int spi_xmit(struct spi_device *spi_dev, void *addr, const int len,
34 enum spi_direction dir)
36 struct spi_message msg;
38 struct spi_transfer xfer = {
42 if (dir == SPI_DIR_TX)
47 if (spi_dev == NULL) {
48 pr_err("SPI device is uninitialized\n");
52 spi_message_init(&msg);
53 spi_message_add_tail(&xfer, &msg);
55 r = spi_sync(spi_dev, &msg);
57 dev_err(&spi_dev->dev, "%s spi_sync failed %d\n", __func__, r);
62 int s5c73m3_spi_write(struct s5c73m3 *state, const void *addr,
63 const unsigned int len, const unsigned int tx_size)
65 struct spi_device *spi_dev = state->spi_dev;
66 u32 count = len / tx_size;
67 u32 extra = len % tx_size;
68 unsigned int i, j = 0;
72 memset(padding, 0, sizeof(padding));
74 for (i = 0; i < count; i++) {
75 r = spi_xmit(spi_dev, (void *)addr + j, tx_size, SPI_DIR_TX);
82 r = spi_xmit(spi_dev, (void *)addr + j, extra, SPI_DIR_TX);
87 return spi_xmit(spi_dev, padding, sizeof(padding), SPI_DIR_TX);
90 int s5c73m3_spi_read(struct s5c73m3 *state, void *addr,
91 const unsigned int len, const unsigned int tx_size)
93 struct spi_device *spi_dev = state->spi_dev;
94 u32 count = len / tx_size;
95 u32 extra = len % tx_size;
96 unsigned int i, j = 0;
99 for (i = 0; i < count; i++) {
100 r = spi_xmit(spi_dev, addr + j, tx_size, SPI_DIR_RX);
107 return spi_xmit(spi_dev, addr + j, extra, SPI_DIR_RX);
112 static int s5c73m3_spi_probe(struct spi_device *spi)
115 struct s5c73m3 *state = container_of(spi->dev.driver, struct s5c73m3,
117 spi->bits_per_word = 32;
121 dev_err(&spi->dev, "spi_setup() failed\n");
125 mutex_lock(&state->lock);
126 state->spi_dev = spi;
127 mutex_unlock(&state->lock);
129 v4l2_info(&state->sensor_sd, "S5C73M3 SPI probed successfully\n");
133 int s5c73m3_register_spi_driver(struct s5c73m3 *state)
135 struct spi_driver *spidrv = &state->spidrv;
137 spidrv->probe = s5c73m3_spi_probe;
138 spidrv->driver.name = S5C73M3_SPI_DRV_NAME;
139 spidrv->driver.of_match_table = s5c73m3_spi_ids;
141 return spi_register_driver(spidrv);
144 void s5c73m3_unregister_spi_driver(struct s5c73m3 *state)
146 spi_unregister_driver(&state->spidrv);