treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / i2c / s5c73m3 / s5c73m3-spi.c
blobc102c6bbc118f7be20206e50d12da629c6f96474
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
8 */
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>
18 #include "s5c73m3.h"
20 #define S5C73M3_SPI_DRV_NAME "S5C73M3-SPI"
22 static const struct of_device_id s5c73m3_spi_ids[] = {
23 { .compatible = "samsung,s5c73m3" },
24 { }
26 MODULE_DEVICE_TABLE(of, s5c73m3_spi_ids);
28 enum spi_direction {
29 SPI_DIR_RX,
30 SPI_DIR_TX
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;
37 int r;
38 struct spi_transfer xfer = {
39 .len = len,
42 if (dir == SPI_DIR_TX)
43 xfer.tx_buf = addr;
44 else
45 xfer.rx_buf = addr;
47 if (spi_dev == NULL) {
48 pr_err("SPI device is uninitialized\n");
49 return -ENODEV;
52 spi_message_init(&msg);
53 spi_message_add_tail(&xfer, &msg);
55 r = spi_sync(spi_dev, &msg);
56 if (r < 0)
57 dev_err(&spi_dev->dev, "%s spi_sync failed %d\n", __func__, r);
59 return 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;
69 u8 padding[32];
70 int r = 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);
76 if (r < 0)
77 return r;
78 j += tx_size;
81 if (extra > 0) {
82 r = spi_xmit(spi_dev, (void *)addr + j, extra, SPI_DIR_TX);
83 if (r < 0)
84 return r;
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;
97 int r = 0;
99 for (i = 0; i < count; i++) {
100 r = spi_xmit(spi_dev, addr + j, tx_size, SPI_DIR_RX);
101 if (r < 0)
102 return r;
103 j += tx_size;
106 if (extra > 0)
107 return spi_xmit(spi_dev, addr + j, extra, SPI_DIR_RX);
109 return 0;
112 static int s5c73m3_spi_probe(struct spi_device *spi)
114 int r;
115 struct s5c73m3 *state = container_of(spi->dev.driver, struct s5c73m3,
116 spidrv.driver);
117 spi->bits_per_word = 32;
119 r = spi_setup(spi);
120 if (r < 0) {
121 dev_err(&spi->dev, "spi_setup() failed\n");
122 return r;
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");
130 return 0;
133 static int s5c73m3_spi_remove(struct spi_device *spi)
135 return 0;
138 int s5c73m3_register_spi_driver(struct s5c73m3 *state)
140 struct spi_driver *spidrv = &state->spidrv;
142 spidrv->remove = s5c73m3_spi_remove;
143 spidrv->probe = s5c73m3_spi_probe;
144 spidrv->driver.name = S5C73M3_SPI_DRV_NAME;
145 spidrv->driver.of_match_table = s5c73m3_spi_ids;
147 return spi_register_driver(spidrv);
150 void s5c73m3_unregister_spi_driver(struct s5c73m3 *state)
152 spi_unregister_driver(&state->spidrv);