1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/input.h>
14#include <linux/spi/spi.h>
15#include <linux/device.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/interrupt.h>
19#include <linux/irq.h>
20#include <linux/slab.h>
21#include <linux/sched.h>
22#include <linux/uaccess.h>
23#include <linux/regulator/consumer.h>
24#include <linux/pm_qos.h>
25#include <linux/sysfs.h>
26#include <linux/clk.h>
27#include <linux/firmware.h>
28#include <linux/acpi.h>
29
30#include "rt5677-spi.h"
31
32#define RT5677_SPI_BURST_LEN 240
33#define RT5677_SPI_HEADER 5
34#define RT5677_SPI_FREQ 6000000
35
36
37
38
39
40
41
42#define RT5677_SPI_WRITE_BURST 0x5
43#define RT5677_SPI_READ_BURST 0x4
44#define RT5677_SPI_WRITE_32 0x3
45#define RT5677_SPI_READ_32 0x2
46#define RT5677_SPI_WRITE_16 0x1
47#define RT5677_SPI_READ_16 0x0
48
49static struct spi_device *g_spi;
50static DEFINE_MUTEX(spi_mutex);
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80static u8 rt5677_spi_select_cmd(bool read, u32 align, u32 remain, u32 *len)
81{
82 u8 cmd;
83
84 if (align == 4 || remain <= 4) {
85 cmd = RT5677_SPI_READ_32;
86 *len = 4;
87 } else {
88 cmd = RT5677_SPI_READ_BURST;
89 *len = (((remain - 1) >> 3) + 1) << 3;
90 *len = min_t(u32, *len, RT5677_SPI_BURST_LEN);
91 }
92 return read ? cmd : cmd + 1;
93}
94
95
96
97
98static void rt5677_spi_reverse(u8 *dst, u32 dstlen, const u8 *src, u32 srclen)
99{
100 u32 w, i, si;
101 u32 word_size = min_t(u32, dstlen, 8);
102
103 for (w = 0; w < dstlen; w += word_size) {
104 for (i = 0; i < word_size; i++) {
105 si = w + word_size - i - 1;
106 dst[w + i] = si < srclen ? src[si] : 0;
107 }
108 }
109}
110
111
112int rt5677_spi_read(u32 addr, void *rxbuf, size_t len)
113{
114 u32 offset;
115 int status = 0;
116 struct spi_transfer t[2];
117 struct spi_message m;
118
119 u8 header[RT5677_SPI_HEADER + 4];
120 u8 body[RT5677_SPI_BURST_LEN];
121 u8 spi_cmd;
122 u8 *cb = rxbuf;
123
124 if (!g_spi)
125 return -ENODEV;
126
127 if ((addr & 3) || (len & 3)) {
128 dev_err(&g_spi->dev, "Bad read align 0x%x(%zu)\n", addr, len);
129 return -EACCES;
130 }
131
132 memset(t, 0, sizeof(t));
133 t[0].tx_buf = header;
134 t[0].len = sizeof(header);
135 t[0].speed_hz = RT5677_SPI_FREQ;
136 t[1].rx_buf = body;
137 t[1].speed_hz = RT5677_SPI_FREQ;
138 spi_message_init_with_transfers(&m, t, ARRAY_SIZE(t));
139
140 for (offset = 0; offset < len; offset += t[1].len) {
141 spi_cmd = rt5677_spi_select_cmd(true, (addr + offset) & 7,
142 len - offset, &t[1].len);
143
144
145 header[0] = spi_cmd;
146 header[1] = ((addr + offset) & 0xff000000) >> 24;
147 header[2] = ((addr + offset) & 0x00ff0000) >> 16;
148 header[3] = ((addr + offset) & 0x0000ff00) >> 8;
149 header[4] = ((addr + offset) & 0x000000ff) >> 0;
150
151 mutex_lock(&spi_mutex);
152 status |= spi_sync(g_spi, &m);
153 mutex_unlock(&spi_mutex);
154
155
156 rt5677_spi_reverse(cb + offset, t[1].len, body, t[1].len);
157 }
158 return status;
159}
160EXPORT_SYMBOL_GPL(rt5677_spi_read);
161
162
163
164
165
166int rt5677_spi_write(u32 addr, const void *txbuf, size_t len)
167{
168 u32 offset;
169 int status = 0;
170 struct spi_transfer t;
171 struct spi_message m;
172
173 u8 buf[RT5677_SPI_HEADER + RT5677_SPI_BURST_LEN + 1];
174 u8 *body = buf + RT5677_SPI_HEADER;
175 u8 spi_cmd;
176 const u8 *cb = txbuf;
177
178 if (!g_spi)
179 return -ENODEV;
180
181 if (addr & 3) {
182 dev_err(&g_spi->dev, "Bad write align 0x%x(%zu)\n", addr, len);
183 return -EACCES;
184 }
185
186 memset(&t, 0, sizeof(t));
187 t.tx_buf = buf;
188 t.speed_hz = RT5677_SPI_FREQ;
189 spi_message_init_with_transfers(&m, &t, 1);
190
191 for (offset = 0; offset < len;) {
192 spi_cmd = rt5677_spi_select_cmd(false, (addr + offset) & 7,
193 len - offset, &t.len);
194
195
196 buf[0] = spi_cmd;
197 buf[1] = ((addr + offset) & 0xff000000) >> 24;
198 buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
199 buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
200 buf[4] = ((addr + offset) & 0x000000ff) >> 0;
201
202
203 rt5677_spi_reverse(body, t.len, cb + offset, len - offset);
204 offset += t.len;
205 t.len += RT5677_SPI_HEADER + 1;
206
207 mutex_lock(&spi_mutex);
208 status |= spi_sync(g_spi, &m);
209 mutex_unlock(&spi_mutex);
210 }
211 return status;
212}
213EXPORT_SYMBOL_GPL(rt5677_spi_write);
214
215int rt5677_spi_write_firmware(u32 addr, const struct firmware *fw)
216{
217 return rt5677_spi_write(addr, fw->data, fw->size);
218}
219EXPORT_SYMBOL_GPL(rt5677_spi_write_firmware);
220
221static int rt5677_spi_probe(struct spi_device *spi)
222{
223 g_spi = spi;
224 return 0;
225}
226
227static const struct acpi_device_id rt5677_spi_acpi_id[] = {
228 { "RT5677AA", 0 },
229 { }
230};
231MODULE_DEVICE_TABLE(acpi, rt5677_spi_acpi_id);
232
233static struct spi_driver rt5677_spi_driver = {
234 .driver = {
235 .name = "rt5677",
236 .acpi_match_table = ACPI_PTR(rt5677_spi_acpi_id),
237 },
238 .probe = rt5677_spi_probe,
239};
240module_spi_driver(rt5677_spi_driver);
241
242MODULE_DESCRIPTION("ASoC RT5677 SPI driver");
243MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
244MODULE_LICENSE("GPL v2");
245