1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Samsung LSI S5C73M3 8M pixel camera driver
4 *
5 * Copyright (C) 2012, Samsung Electronics, Co., Ltd.
6 * Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * Andrzej Hajda <a.hajda@samsung.com>
8 */
9
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>
17
18 #include "s5c73m3.h"
19
20 #define S5C73M3_SPI_DRV_NAME "S5C73M3-SPI"
21
22 static const struct of_device_id s5c73m3_spi_ids[] = {
23 { .compatible = "samsung,s5c73m3" },
24 { }
25 };
26 MODULE_DEVICE_TABLE(of, s5c73m3_spi_ids);
27
28 enum spi_direction {
29 SPI_DIR_RX,
30 SPI_DIR_TX
31 };
32
spi_xmit(struct spi_device * spi_dev,void * addr,const int len,enum spi_direction dir)33 static int spi_xmit(struct spi_device *spi_dev, void *addr, const int len,
34 enum spi_direction dir)
35 {
36 struct spi_message msg;
37 int r;
38 struct spi_transfer xfer = {
39 .len = len,
40 };
41
42 if (dir == SPI_DIR_TX)
43 xfer.tx_buf = addr;
44 else
45 xfer.rx_buf = addr;
46
47 if (spi_dev == NULL) {
48 pr_err("SPI device is uninitialized\n");
49 return -ENODEV;
50 }
51
52 spi_message_init(&msg);
53 spi_message_add_tail(&xfer, &msg);
54
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);
58
59 return r;
60 }
61
s5c73m3_spi_write(struct s5c73m3 * state,const void * addr,const unsigned int len,const unsigned int tx_size)62 int s5c73m3_spi_write(struct s5c73m3 *state, const void *addr,
63 const unsigned int len, const unsigned int tx_size)
64 {
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;
71
72 memset(padding, 0, sizeof(padding));
73
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;
79 }
80
81 if (extra > 0) {
82 r = spi_xmit(spi_dev, (void *)addr + j, extra, SPI_DIR_TX);
83 if (r < 0)
84 return r;
85 }
86
87 return spi_xmit(spi_dev, padding, sizeof(padding), SPI_DIR_TX);
88 }
89
s5c73m3_spi_read(struct s5c73m3 * state,void * addr,const unsigned int len,const unsigned int tx_size)90 int s5c73m3_spi_read(struct s5c73m3 *state, void *addr,
91 const unsigned int len, const unsigned int tx_size)
92 {
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;
98
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;
104 }
105
106 if (extra > 0)
107 return spi_xmit(spi_dev, addr + j, extra, SPI_DIR_RX);
108
109 return 0;
110 }
111
s5c73m3_spi_probe(struct spi_device * spi)112 static int s5c73m3_spi_probe(struct spi_device *spi)
113 {
114 int r;
115 struct s5c73m3 *state = container_of(spi->dev.driver, struct s5c73m3,
116 spidrv.driver);
117 spi->bits_per_word = 32;
118
119 r = spi_setup(spi);
120 if (r < 0) {
121 dev_err(&spi->dev, "spi_setup() failed\n");
122 return r;
123 }
124
125 mutex_lock(&state->lock);
126 state->spi_dev = spi;
127 mutex_unlock(&state->lock);
128
129 v4l2_info(&state->sensor_sd, "S5C73M3 SPI probed successfully\n");
130 return 0;
131 }
132
s5c73m3_register_spi_driver(struct s5c73m3 * state)133 int s5c73m3_register_spi_driver(struct s5c73m3 *state)
134 {
135 struct spi_driver *spidrv = &state->spidrv;
136
137 spidrv->probe = s5c73m3_spi_probe;
138 spidrv->driver.name = S5C73M3_SPI_DRV_NAME;
139 spidrv->driver.of_match_table = s5c73m3_spi_ids;
140
141 return spi_register_driver(spidrv);
142 }
143
s5c73m3_unregister_spi_driver(struct s5c73m3 * state)144 void s5c73m3_unregister_spi_driver(struct s5c73m3 *state)
145 {
146 spi_unregister_driver(&state->spidrv);
147 }
148