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