blob: c08e7462ce681e3ebf305535dad187200a1d5699 [file] [log] [blame]
Banajit Goswamide8271c2017-01-18 00:28:59 -08001/*
2 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/debugfs.h>
18#include <linux/delay.h>
19#include <linux/bitops.h>
20#include <linux/spi/spi.h>
21#include <linux/regmap.h>
22#include <linux/component.h>
23#include <linux/ratelimit.h>
24#include <sound/wcd-dsp-mgr.h>
25#include <sound/wcd-spi.h>
26#include "wcd-spi-registers.h"
27
28/* Byte manipulations */
29#define SHIFT_1_BYTES (8)
30#define SHIFT_2_BYTES (16)
31#define SHIFT_3_BYTES (24)
32
33/* Command opcodes */
34#define WCD_SPI_CMD_NOP (0x00)
35#define WCD_SPI_CMD_WREN (0x06)
36#define WCD_SPI_CMD_CLKREQ (0xDA)
37#define WCD_SPI_CMD_RDSR (0x05)
38#define WCD_SPI_CMD_IRR (0x81)
39#define WCD_SPI_CMD_IRW (0x82)
40#define WCD_SPI_CMD_MIOR (0x83)
41#define WCD_SPI_CMD_FREAD (0x0B)
42#define WCD_SPI_CMD_MIOW (0x02)
43#define WCD_SPI_WRITE_FRAME_OPCODE \
44 (WCD_SPI_CMD_MIOW << SHIFT_3_BYTES)
45#define WCD_SPI_READ_FRAME_OPCODE \
46 (WCD_SPI_CMD_MIOR << SHIFT_3_BYTES)
47#define WCD_SPI_FREAD_FRAME_OPCODE \
48 (WCD_SPI_CMD_FREAD << SHIFT_3_BYTES)
49
50/* Command lengths */
51#define WCD_SPI_OPCODE_LEN (0x01)
52#define WCD_SPI_CMD_NOP_LEN (0x01)
53#define WCD_SPI_CMD_WREN_LEN (0x01)
54#define WCD_SPI_CMD_CLKREQ_LEN (0x04)
55#define WCD_SPI_CMD_IRR_LEN (0x04)
56#define WCD_SPI_CMD_IRW_LEN (0x06)
57#define WCD_SPI_WRITE_SINGLE_LEN (0x08)
58#define WCD_SPI_READ_SINGLE_LEN (0x13)
59#define WCD_SPI_CMD_FREAD_LEN (0x13)
60
61/* Command delays */
62#define WCD_SPI_CLKREQ_DELAY_USECS (500)
63#define WCD_SPI_CLK_OFF_TIMER_MS (3000)
64
65/* Command masks */
66#define WCD_CMD_ADDR_MASK \
67 (0xFF | \
68 (0xFF << SHIFT_1_BYTES) | \
69 (0xFF << SHIFT_2_BYTES))
70
71/* Clock ctrl request related */
72#define WCD_SPI_CLK_ENABLE true
73#define WCD_SPI_CLK_DISABLE false
74#define WCD_SPI_CLK_FLAG_DELAYED (1 << 0)
75#define WCD_SPI_CLK_FLAG_IMMEDIATE (1 << 1)
76
77/* Internal addresses */
78#define WCD_SPI_ADDR_IPC_CTL_HOST (0x012014)
79
80/* Word sizes and min/max lengths */
81#define WCD_SPI_WORD_BYTE_CNT (4)
82#define WCD_SPI_RW_MULTI_MIN_LEN (16)
83
84/* Max size is closest multiple of 16 less than 64Kbytes */
85#define WCD_SPI_RW_MULTI_MAX_LEN ((64 * 1024) - 16)
86
87/* Alignment requirements */
88#define WCD_SPI_RW_MIN_ALIGN WCD_SPI_WORD_BYTE_CNT
89#define WCD_SPI_RW_MULTI_ALIGN (16)
90
91/* Status mask bits */
92#define WCD_SPI_CLK_STATE_ENABLED BIT(0)
93
94/* Locking related */
95#define WCD_SPI_MUTEX_LOCK(spi, lock) \
96{ \
97 dev_vdbg(&spi->dev, "%s: mutex_lock(%s)\n", \
98 __func__, __stringify_1(lock)); \
99 mutex_lock(&lock); \
100}
101
102#define WCD_SPI_MUTEX_UNLOCK(spi, lock) \
103{ \
104 dev_vdbg(&spi->dev, "%s: mutex_unlock(%s)\n", \
105 __func__, __stringify_1(lock)); \
106 mutex_unlock(&lock); \
107}
108
109struct wcd_spi_debug_data {
110 struct dentry *dir;
111 u32 addr;
112 u32 size;
113};
114
115struct wcd_spi_priv {
116 struct spi_device *spi;
117 u32 mem_base_addr;
118
119 struct regmap *regmap;
120
121 /* Message for single transfer */
122 struct spi_message msg1;
123 struct spi_transfer xfer1;
124
125 /* Message for two transfers */
126 struct spi_message msg2;
127 struct spi_transfer xfer2[2];
128
129 /* Register access related */
130 u32 reg_bytes;
131 u32 val_bytes;
132
133 /* Clock requests related */
134 struct mutex clk_mutex;
135 int clk_users;
136 unsigned long status_mask;
137 struct delayed_work clk_dwork;
138
139 /* Transaction related */
140 struct mutex xfer_mutex;
141
142 struct device *m_dev;
143 struct wdsp_mgr_ops *m_ops;
144
145 /* Debugfs related information */
146 struct wcd_spi_debug_data debug_data;
147};
148
149enum xfer_request {
150 WCD_SPI_XFER_WRITE,
151 WCD_SPI_XFER_READ,
152};
153
154
155static char *wcd_spi_xfer_req_str(enum xfer_request req)
156{
157 if (req == WCD_SPI_XFER_WRITE)
158 return "xfer_write";
159 else if (req == WCD_SPI_XFER_READ)
160 return "xfer_read";
161 else
162 return "xfer_invalid";
163}
164
165static void wcd_spi_reinit_xfer(struct spi_transfer *xfer)
166{
167 xfer->tx_buf = NULL;
168 xfer->rx_buf = NULL;
169 xfer->delay_usecs = 0;
170 xfer->len = 0;
171}
172
173static int wcd_spi_read_single(struct spi_device *spi,
174 u32 remote_addr, u32 *val)
175{
176 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
177 struct spi_transfer *tx_xfer = &wcd_spi->xfer2[0];
178 struct spi_transfer *rx_xfer = &wcd_spi->xfer2[1];
179 u8 *tx_buf;
180 u32 frame = 0;
181 int ret;
182
183 dev_dbg(&spi->dev, "%s: remote_addr = 0x%x\n",
184 __func__, remote_addr);
185
186 tx_buf = kzalloc(WCD_SPI_READ_SINGLE_LEN,
187 GFP_KERNEL | GFP_DMA);
188 if (!tx_buf)
189 return -ENOMEM;
190
191 frame |= WCD_SPI_READ_FRAME_OPCODE;
192 frame |= remote_addr & WCD_CMD_ADDR_MASK;
193
194 wcd_spi_reinit_xfer(tx_xfer);
195 frame = cpu_to_be32(frame);
196 memcpy(tx_buf, &frame, sizeof(frame));
197 tx_xfer->tx_buf = tx_buf;
198 tx_xfer->len = WCD_SPI_READ_SINGLE_LEN;
199
200 wcd_spi_reinit_xfer(rx_xfer);
201 rx_xfer->rx_buf = val;
202 rx_xfer->len = sizeof(*val);
203
204 ret = spi_sync(spi, &wcd_spi->msg2);
205 kfree(tx_buf);
206
207 return ret;
208}
209
210static int wcd_spi_read_multi(struct spi_device *spi,
211 u32 remote_addr, u8 *data,
212 size_t len)
213{
214 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
215 struct spi_transfer *xfer = &wcd_spi->xfer1;
216 u8 *tx_buf;
217 u8 *rx_buf;
218 u32 frame = 0;
219 int ret;
220
221 dev_dbg(&spi->dev, "%s: addr 0x%x, len = %zd\n",
222 __func__, remote_addr, len);
223
224 frame |= WCD_SPI_FREAD_FRAME_OPCODE;
225 frame |= remote_addr & WCD_CMD_ADDR_MASK;
226
227 tx_buf = kzalloc(WCD_SPI_CMD_FREAD_LEN + len,
228 GFP_KERNEL | GFP_DMA);
229 if (!tx_buf)
230 return -ENOMEM;
231
232 rx_buf = kzalloc(WCD_SPI_CMD_FREAD_LEN + len,
233 GFP_KERNEL | GFP_DMA);
234 if (!rx_buf) {
235 kfree(tx_buf);
236 return -ENOMEM;
237 }
238
239 wcd_spi_reinit_xfer(xfer);
240 frame = cpu_to_be32(frame);
241 memcpy(tx_buf, &frame, sizeof(frame));
242 xfer->tx_buf = tx_buf;
243 xfer->rx_buf = rx_buf;
244 xfer->len = WCD_SPI_CMD_FREAD_LEN + len;
245
246 ret = spi_sync(spi, &wcd_spi->msg1);
247 if (ret) {
248 dev_err(&spi->dev, "%s: failed, err = %d\n",
249 __func__, ret);
250 goto done;
251 }
252
253 memcpy(data, rx_buf + WCD_SPI_CMD_FREAD_LEN, len);
254done:
255 kfree(tx_buf);
256 kfree(rx_buf);
257 return ret;
258}
259
260static int wcd_spi_write_single(struct spi_device *spi,
261 u32 remote_addr, u32 val)
262{
263 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
264 struct spi_transfer *xfer = &wcd_spi->xfer1;
265 u8 buf[WCD_SPI_WRITE_SINGLE_LEN];
266 u32 frame = 0;
267
268 dev_dbg(&spi->dev, "%s: remote_addr = 0x%x, val = 0x%x\n",
269 __func__, remote_addr, val);
270
271 memset(buf, 0, WCD_SPI_WRITE_SINGLE_LEN);
272 frame |= WCD_SPI_WRITE_FRAME_OPCODE;
273 frame |= (remote_addr & WCD_CMD_ADDR_MASK);
274
275 frame = cpu_to_be32(frame);
276 memcpy(buf, &frame, sizeof(frame));
277 memcpy(buf + sizeof(frame), &val, sizeof(val));
278
279 wcd_spi_reinit_xfer(xfer);
280 xfer->tx_buf = buf;
281 xfer->len = WCD_SPI_WRITE_SINGLE_LEN;
282
283 return spi_sync(spi, &wcd_spi->msg1);
284}
285
286static int wcd_spi_write_multi(struct spi_device *spi,
287 u32 remote_addr, u8 *data,
288 size_t len)
289{
290 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
291 struct spi_transfer *xfer = &wcd_spi->xfer1;
292 u32 frame = 0;
293 u8 *tx_buf;
294 int xfer_len, ret;
295
296 dev_dbg(&spi->dev, "%s: addr = 0x%x len = %zd\n",
297 __func__, remote_addr, len);
298
299 frame |= WCD_SPI_WRITE_FRAME_OPCODE;
300 frame |= (remote_addr & WCD_CMD_ADDR_MASK);
301
302 frame = cpu_to_be32(frame);
303 xfer_len = len + sizeof(frame);
304
305 tx_buf = kzalloc(xfer_len, GFP_KERNEL);
306 if (!tx_buf)
307 return -ENOMEM;
308
309 memcpy(tx_buf, &frame, sizeof(frame));
310 memcpy(tx_buf + sizeof(frame), data, len);
311
312 wcd_spi_reinit_xfer(xfer);
313 xfer->tx_buf = tx_buf;
314 xfer->len = xfer_len;
315
316 ret = spi_sync(spi, &wcd_spi->msg1);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800317 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800318 dev_err(&spi->dev,
319 "%s: Failed, addr = 0x%x, len = %zd\n",
320 __func__, remote_addr, len);
321 kfree(tx_buf);
322
323 return ret;
324}
325
326static int wcd_spi_transfer_split(struct spi_device *spi,
327 struct wcd_spi_msg *data_msg,
328 enum xfer_request xfer_req)
329{
330 u32 addr = data_msg->remote_addr;
331 u8 *data = data_msg->data;
332 int remain_size = data_msg->len;
333 int to_xfer, loop_cnt, ret = 0;
334
335 /* Perform single writes until multi word alignment is met */
336 loop_cnt = 1;
337 while (remain_size &&
338 !IS_ALIGNED(addr, WCD_SPI_RW_MULTI_ALIGN)) {
339 if (xfer_req == WCD_SPI_XFER_WRITE)
340 ret = wcd_spi_write_single(spi, addr,
341 (*(u32 *)data));
342 else
343 ret = wcd_spi_read_single(spi, addr,
344 (u32 *)data);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800345 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800346 dev_err(&spi->dev,
347 "%s: %s fail iter(%d) start-word addr (0x%x)\n",
348 __func__, wcd_spi_xfer_req_str(xfer_req),
349 loop_cnt, addr);
350 goto done;
351 }
352
353 addr += WCD_SPI_WORD_BYTE_CNT;
354 data += WCD_SPI_WORD_BYTE_CNT;
355 remain_size -= WCD_SPI_WORD_BYTE_CNT;
356 loop_cnt++;
357 }
358
359 /* Perform multi writes for max allowed multi writes */
360 loop_cnt = 1;
361 while (remain_size >= WCD_SPI_RW_MULTI_MAX_LEN) {
362 if (xfer_req == WCD_SPI_XFER_WRITE)
363 ret = wcd_spi_write_multi(spi, addr, data,
364 WCD_SPI_RW_MULTI_MAX_LEN);
365 else
366 ret = wcd_spi_read_multi(spi, addr, data,
367 WCD_SPI_RW_MULTI_MAX_LEN);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800368 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800369 dev_err(&spi->dev,
370 "%s: %s fail iter(%d) max-write addr (0x%x)\n",
371 __func__, wcd_spi_xfer_req_str(xfer_req),
372 loop_cnt, addr);
373 goto done;
374 }
375
376 addr += WCD_SPI_RW_MULTI_MAX_LEN;
377 data += WCD_SPI_RW_MULTI_MAX_LEN;
378 remain_size -= WCD_SPI_RW_MULTI_MAX_LEN;
379 loop_cnt++;
380 }
381
382 /*
383 * Perform write for max possible data that is multiple
384 * of the minimum size for multi-write commands.
385 */
386 to_xfer = remain_size - (remain_size % WCD_SPI_RW_MULTI_MIN_LEN);
387 if (remain_size >= WCD_SPI_RW_MULTI_MIN_LEN &&
388 to_xfer > 0) {
389 if (xfer_req == WCD_SPI_XFER_WRITE)
390 ret = wcd_spi_write_multi(spi, addr, data, to_xfer);
391 else
392 ret = wcd_spi_read_multi(spi, addr, data, to_xfer);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800393 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800394 dev_err(&spi->dev,
395 "%s: %s fail write addr (0x%x), size (0x%x)\n",
396 __func__, wcd_spi_xfer_req_str(xfer_req),
397 addr, to_xfer);
398 goto done;
399 }
400
401 addr += to_xfer;
402 data += to_xfer;
403 remain_size -= to_xfer;
404 }
405
406 /* Perform single writes for the last remaining data */
407 loop_cnt = 1;
408 while (remain_size > 0) {
409 if (xfer_req == WCD_SPI_XFER_WRITE)
410 ret = wcd_spi_write_single(spi, addr, (*((u32 *)data)));
411 else
412 ret = wcd_spi_read_single(spi, addr, (u32 *) data);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800413 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800414 dev_err(&spi->dev,
415 "%s: %s fail iter(%d) end-write addr (0x%x)\n",
416 __func__, wcd_spi_xfer_req_str(xfer_req),
417 loop_cnt, addr);
418 goto done;
419 }
420
421 addr += WCD_SPI_WORD_BYTE_CNT;
422 data += WCD_SPI_WORD_BYTE_CNT;
423 remain_size -= WCD_SPI_WORD_BYTE_CNT;
424 loop_cnt++;
425 }
426
427done:
428 return ret;
429}
430
431static int wcd_spi_cmd_nop(struct spi_device *spi)
432{
433 u8 nop = WCD_SPI_CMD_NOP;
434
435 return spi_write(spi, &nop, WCD_SPI_CMD_NOP_LEN);
436}
437
438static int wcd_spi_cmd_clkreq(struct spi_device *spi)
439{
440 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
441 struct spi_transfer *xfer = &wcd_spi->xfer1;
442 u8 cmd[WCD_SPI_CMD_CLKREQ_LEN] = {
443 WCD_SPI_CMD_CLKREQ,
444 0xBA, 0x80, 0x00};
445
446 wcd_spi_reinit_xfer(xfer);
447 xfer->tx_buf = cmd;
448 xfer->len = WCD_SPI_CMD_CLKREQ_LEN;
449 xfer->delay_usecs = WCD_SPI_CLKREQ_DELAY_USECS;
450
451 return spi_sync(spi, &wcd_spi->msg1);
452}
453
454static int wcd_spi_cmd_wr_en(struct spi_device *spi)
455{
456 u8 wr_en = WCD_SPI_CMD_WREN;
457
458 return spi_write(spi, &wr_en, WCD_SPI_CMD_WREN_LEN);
459}
460
461static int wcd_spi_cmd_rdsr(struct spi_device *spi,
462 u32 *rdsr_status)
463{
464 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
465 struct spi_transfer *tx_xfer = &wcd_spi->xfer2[0];
466 struct spi_transfer *rx_xfer = &wcd_spi->xfer2[1];
467 u8 rdsr_cmd;
468 u32 status;
469 int ret;
470
471 rdsr_cmd = WCD_SPI_CMD_RDSR;
472 wcd_spi_reinit_xfer(tx_xfer);
473 tx_xfer->tx_buf = &rdsr_cmd;
474 tx_xfer->len = sizeof(rdsr_cmd);
475
476
477 wcd_spi_reinit_xfer(rx_xfer);
478 rx_xfer->rx_buf = &status;
479 rx_xfer->len = sizeof(status);
480
481 ret = spi_sync(spi, &wcd_spi->msg2);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800482 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800483 dev_err(&spi->dev, "%s: RDSR failed, err = %d\n",
484 __func__, ret);
485 goto done;
486 }
487
488 *rdsr_status = be32_to_cpu(status);
489
490 dev_dbg(&spi->dev, "%s: RDSR success, value = 0x%x\n",
491 __func__, *rdsr_status);
492done:
493 return ret;
494}
495
496static int wcd_spi_clk_enable(struct spi_device *spi)
497{
498 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
499 int ret;
500 u32 rd_status;
501
502 ret = wcd_spi_cmd_nop(spi);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800503 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800504 dev_err(&spi->dev, "%s: NOP1 failed, err = %d\n",
505 __func__, ret);
506 goto done;
507 }
508
509 ret = wcd_spi_cmd_clkreq(spi);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800510 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800511 dev_err(&spi->dev, "%s: CLK_REQ failed, err = %d\n",
512 __func__, ret);
513 goto done;
514 }
515
516 ret = wcd_spi_cmd_nop(spi);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800517 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800518 dev_err(&spi->dev, "%s: NOP2 failed, err = %d\n",
519 __func__, ret);
520 goto done;
521 }
522 wcd_spi_cmd_rdsr(spi, &rd_status);
523 /*
524 * Read status zero means reads are not
525 * happenning on the bus, possibly because
526 * clock request failed.
527 */
528 if (rd_status) {
529 set_bit(WCD_SPI_CLK_STATE_ENABLED,
530 &wcd_spi->status_mask);
531 } else {
532 dev_err(&spi->dev, "%s: RDSR status is zero\n",
533 __func__);
534 ret = -EIO;
535 }
536done:
537 return ret;
538}
539
540static int wcd_spi_clk_disable(struct spi_device *spi)
541{
542 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
543 int ret;
544
545 ret = wcd_spi_write_single(spi, WCD_SPI_ADDR_IPC_CTL_HOST, 0x01);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800546 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800547 dev_err(&spi->dev, "%s: Failed, err = %d\n",
548 __func__, ret);
549 else
550 clear_bit(WCD_SPI_CLK_STATE_ENABLED, &wcd_spi->status_mask);
551
552 return ret;
553}
554
555static int wcd_spi_clk_ctrl(struct spi_device *spi,
556 bool request, u32 flags)
557{
558 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
559 int ret = 0;
560 const char *delay_str;
561
562 delay_str = (flags == WCD_SPI_CLK_FLAG_DELAYED) ?
563 "delayed" : "immediate";
564
565 WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
566
567 /* Reject any unbalanced disable request */
568 if (wcd_spi->clk_users < 0 ||
569 (!request && wcd_spi->clk_users == 0)) {
570 dev_err(&spi->dev, "%s: Unbalanced clk_users %d for %s\n",
571 __func__, wcd_spi->clk_users,
572 request ? "enable" : "disable");
573 ret = -EINVAL;
574
575 /* Reset the clk_users to 0 */
576 wcd_spi->clk_users = 0;
577
578 goto done;
579 }
580
581 if (request == WCD_SPI_CLK_ENABLE) {
582 /* Cancel the disable clk work */
583 WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
584 cancel_delayed_work_sync(&wcd_spi->clk_dwork);
585 WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
586
587 wcd_spi->clk_users++;
588
589 /*
590 * If clk state is already set,
591 * then clk wasnt really disabled
592 */
593 if (test_bit(WCD_SPI_CLK_STATE_ENABLED, &wcd_spi->status_mask))
594 goto done;
595 else if (wcd_spi->clk_users == 1)
596 ret = wcd_spi_clk_enable(spi);
597
598 } else {
599 wcd_spi->clk_users--;
600
601 /* Clock is still voted for */
602 if (wcd_spi->clk_users > 0)
603 goto done;
604
605 /*
606 * If we are here, clk_users must be 0 and needs
607 * to be disabled. Call the disable based on the
608 * flags.
609 */
610 if (flags == WCD_SPI_CLK_FLAG_DELAYED) {
611 schedule_delayed_work(&wcd_spi->clk_dwork,
612 msecs_to_jiffies(WCD_SPI_CLK_OFF_TIMER_MS));
613 } else {
614 ret = wcd_spi_clk_disable(spi);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800615 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800616 dev_err(&spi->dev,
617 "%s: Failed to disable clk err = %d\n",
618 __func__, ret);
619 }
620 }
621
622done:
623 dev_dbg(&spi->dev, "%s: updated clk_users = %d, request_%s %s\n",
624 __func__, wcd_spi->clk_users, request ? "enable" : "disable",
625 request ? "" : delay_str);
626 WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
627
628 return ret;
629}
630
631static int wcd_spi_init(struct spi_device *spi)
632{
633 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
634 int ret;
635
636 ret = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_ENABLE,
637 WCD_SPI_CLK_FLAG_IMMEDIATE);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800638 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800639 goto done;
640
641 ret = wcd_spi_cmd_wr_en(spi);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800642 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800643 goto err_wr_en;
644
645 /*
646 * In case spi_init is called after component deinit,
647 * it is possible hardware register state is also reset.
648 * Sync the regcache here so hardware state is updated
649 * to reflect the cache.
650 */
651 regcache_sync(wcd_spi->regmap);
652
653 regmap_write(wcd_spi->regmap, WCD_SPI_SLAVE_CONFIG,
654 0x0F3D0800);
655
656 /* Write the MTU to max allowed size */
657 regmap_update_bits(wcd_spi->regmap,
658 WCD_SPI_SLAVE_TRNS_LEN,
659 0xFFFF0000, 0xFFFF0000);
660err_wr_en:
661 wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_DISABLE,
662 WCD_SPI_CLK_FLAG_IMMEDIATE);
663done:
664 return ret;
665}
666
667static void wcd_spi_clk_work(struct work_struct *work)
668{
669 struct delayed_work *dwork;
670 struct wcd_spi_priv *wcd_spi;
671 struct spi_device *spi;
672 int ret;
673
674 dwork = to_delayed_work(work);
675 wcd_spi = container_of(dwork, struct wcd_spi_priv, clk_dwork);
676 spi = wcd_spi->spi;
677
678 WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
679 ret = wcd_spi_clk_disable(spi);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800680 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800681 dev_err(&spi->dev,
682 "%s: Failed to disable clk, err = %d\n",
683 __func__, ret);
684 WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
685}
686
687static int __wcd_spi_data_xfer(struct spi_device *spi,
688 struct wcd_spi_msg *msg,
689 enum xfer_request xfer_req)
690{
691 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
692 int ret;
693
694 /* Check for minimum alignment requirements */
695 if (!IS_ALIGNED(msg->remote_addr, WCD_SPI_RW_MIN_ALIGN)) {
696 dev_err(&spi->dev,
697 "%s addr 0x%x is not aligned to 0x%x\n",
698 __func__, msg->remote_addr, WCD_SPI_RW_MIN_ALIGN);
699 return -EINVAL;
700 } else if (msg->len % WCD_SPI_WORD_BYTE_CNT) {
701 dev_err(&spi->dev,
702 "%s len 0x%zx is not multiple of %d\n",
703 __func__, msg->len, WCD_SPI_WORD_BYTE_CNT);
704 return -EINVAL;
705 }
706
707 WCD_SPI_MUTEX_LOCK(spi, wcd_spi->xfer_mutex);
708 if (msg->len == WCD_SPI_WORD_BYTE_CNT) {
709 if (xfer_req == WCD_SPI_XFER_WRITE)
710 ret = wcd_spi_write_single(spi, msg->remote_addr,
711 (*((u32 *)msg->data)));
712 else
713 ret = wcd_spi_read_single(spi, msg->remote_addr,
714 (u32 *) msg->data);
715 } else {
716 ret = wcd_spi_transfer_split(spi, msg, xfer_req);
717 }
718 WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->xfer_mutex);
719
720 return ret;
721}
722
723static int wcd_spi_data_xfer(struct spi_device *spi,
724 struct wcd_spi_msg *msg,
725 enum xfer_request req)
726{
727 int ret, ret1;
728
729 if (msg->len <= 0) {
730 dev_err(&spi->dev, "%s: Invalid size %zd\n",
731 __func__, msg->len);
732 return -EINVAL;
733 }
734
735 /* Request for clock */
736 ret = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_ENABLE,
737 WCD_SPI_CLK_FLAG_IMMEDIATE);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800738 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -0800739 dev_err(&spi->dev, "%s: clk enable failed %d\n",
740 __func__, ret);
741 goto done;
742 }
743
744 /* Perform the transaction */
745 ret = __wcd_spi_data_xfer(spi, msg, req);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800746 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800747 dev_err(&spi->dev,
748 "%s: Failed %s, addr = 0x%x, size = 0x%zx, err = %d\n",
749 __func__, wcd_spi_xfer_req_str(req),
750 msg->remote_addr, msg->len, ret);
751
752 /* Release the clock even if xfer failed */
753 ret1 = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_DISABLE,
754 WCD_SPI_CLK_FLAG_DELAYED);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800755 if (ret1 < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800756 dev_err(&spi->dev, "%s: clk disable failed %d\n",
757 __func__, ret1);
758done:
759 return ret;
760}
761
762/*
763 * wcd_spi_data_write: Write data to WCD SPI
764 * @spi: spi_device struct
765 * @msg: msg that needs to be written to WCD
766 *
767 * This API writes length of data to address specified. These details
768 * about the write are encapsulated in @msg. Write size should be multiple
769 * of 4 bytes and write address should be 4-byte aligned.
770 */
771int wcd_spi_data_write(struct spi_device *spi,
772 struct wcd_spi_msg *msg)
773{
774 if (!spi || !msg) {
775 pr_err("%s: Invalid %s\n", __func__,
776 (!spi) ? "spi device" : "msg");
777 return -EINVAL;
778 }
779
780 dev_dbg_ratelimited(&spi->dev, "%s: addr = 0x%x, len = %zu\n",
781 __func__, msg->remote_addr, msg->len);
782 return wcd_spi_data_xfer(spi, msg, WCD_SPI_XFER_WRITE);
783}
784EXPORT_SYMBOL(wcd_spi_data_write);
785
786/*
787 * wcd_spi_data_read: Read data from WCD SPI
788 * @spi: spi_device struct
789 * @msg: msg that needs to be read from WCD
790 *
791 * This API reads length of data from address specified. These details
792 * about the read are encapsulated in @msg. Read size should be multiple
793 * of 4 bytes and read address should be 4-byte aligned.
794 */
795int wcd_spi_data_read(struct spi_device *spi,
796 struct wcd_spi_msg *msg)
797{
798 if (!spi || !msg) {
799 pr_err("%s: Invalid %s\n", __func__,
800 (!spi) ? "spi device" : "msg");
801 return -EINVAL;
802 }
803
804 dev_dbg_ratelimited(&spi->dev, "%s: addr = 0x%x,len = %zu\n",
805 __func__, msg->remote_addr, msg->len);
806 return wcd_spi_data_xfer(spi, msg, WCD_SPI_XFER_READ);
807}
808EXPORT_SYMBOL(wcd_spi_data_read);
809
810static int wdsp_spi_dload_section(struct spi_device *spi,
811 void *data)
812{
813 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
814 struct wdsp_img_section *sec = data;
815 struct wcd_spi_msg msg;
816 int ret;
817
818 dev_dbg(&spi->dev, "%s: addr = 0x%x, size = 0x%zx\n",
819 __func__, sec->addr, sec->size);
820
821 msg.remote_addr = sec->addr + wcd_spi->mem_base_addr;
822 msg.data = sec->data;
823 msg.len = sec->size;
824
825 ret = __wcd_spi_data_xfer(spi, &msg, WCD_SPI_XFER_WRITE);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800826 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800827 dev_err(&spi->dev, "%s: fail addr (0x%x) size (0x%zx)\n",
828 __func__, msg.remote_addr, msg.len);
829 return ret;
830}
831
832static int wdsp_spi_read_section(struct spi_device *spi, void *data)
833{
834 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
835 struct wdsp_img_section *sec = data;
836 struct wcd_spi_msg msg;
837 int ret;
838
839 msg.remote_addr = sec->addr + wcd_spi->mem_base_addr;
840 msg.data = sec->data;
841 msg.len = sec->size;
842
843 dev_dbg(&spi->dev, "%s: addr = 0x%x, size = 0x%zx\n",
844 __func__, msg.remote_addr, msg.len);
845
846 ret = wcd_spi_data_xfer(spi, &msg, WCD_SPI_XFER_READ);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800847 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800848 dev_err(&spi->dev, "%s: fail addr (0x%x) size (0x%zx)\n",
849 __func__, msg.remote_addr, msg.len);
850 return ret;
851}
852
853static int wdsp_spi_event_handler(struct device *dev, void *priv_data,
854 enum wdsp_event_type event,
855 void *data)
856{
857 struct spi_device *spi = to_spi_device(dev);
858 int ret = 0;
859
860 dev_dbg(&spi->dev, "%s: event type %d\n",
861 __func__, event);
862
863 switch (event) {
864 case WDSP_EVENT_PRE_DLOAD_CODE:
865 case WDSP_EVENT_PRE_DLOAD_DATA:
866 ret = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_ENABLE,
867 WCD_SPI_CLK_FLAG_IMMEDIATE);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800868 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800869 dev_err(&spi->dev, "%s: clk_req failed %d\n",
870 __func__, ret);
871 break;
872
873 case WDSP_EVENT_POST_DLOAD_CODE:
874 case WDSP_EVENT_POST_DLOAD_DATA:
875 case WDSP_EVENT_DLOAD_FAILED:
876
877 ret = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_DISABLE,
878 WCD_SPI_CLK_FLAG_IMMEDIATE);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -0800879 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -0800880 dev_err(&spi->dev, "%s: clk unvote failed %d\n",
881 __func__, ret);
882 break;
883
884 case WDSP_EVENT_DLOAD_SECTION:
885 ret = wdsp_spi_dload_section(spi, data);
886 break;
887
888 case WDSP_EVENT_READ_SECTION:
889 ret = wdsp_spi_read_section(spi, data);
890 break;
891
892 default:
893 dev_dbg(&spi->dev, "%s: Unhandled event %d\n",
894 __func__, event);
895 break;
896 }
897
898 return ret;
899}
900
901static int wcd_spi_bus_gwrite(void *context, const void *reg,
902 size_t reg_len, const void *val,
903 size_t val_len)
904{
905 struct device *dev = context;
906 struct spi_device *spi = to_spi_device(dev);
907 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
908 u8 tx_buf[WCD_SPI_CMD_IRW_LEN];
909
910 if (!reg || !val || reg_len != wcd_spi->reg_bytes ||
911 val_len != wcd_spi->val_bytes) {
912 dev_err(&spi->dev,
913 "%s: Invalid input, reg_len = %zd, val_len = %zd",
914 __func__, reg_len, val_len);
915 return -EINVAL;
916 }
917
918 tx_buf[0] = WCD_SPI_CMD_IRW;
919 tx_buf[1] = *((u8 *)reg);
920 memcpy(&tx_buf[WCD_SPI_OPCODE_LEN + reg_len],
921 val, val_len);
922
923 return spi_write(spi, tx_buf, WCD_SPI_CMD_IRW_LEN);
924}
925
926static int wcd_spi_bus_write(void *context, const void *data,
927 size_t count)
928{
929 struct device *dev = context;
930 struct spi_device *spi = to_spi_device(dev);
931 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
932
933 if (count < (wcd_spi->reg_bytes + wcd_spi->val_bytes)) {
934 dev_err(&spi->dev, "%s: Invalid size %zd\n",
935 __func__, count);
936 WARN_ON(1);
937 return -EINVAL;
938 }
939
940 return wcd_spi_bus_gwrite(context, data, wcd_spi->reg_bytes,
941 data + wcd_spi->reg_bytes,
942 count - wcd_spi->reg_bytes);
943}
944
945static int wcd_spi_bus_read(void *context, const void *reg,
946 size_t reg_len, void *val,
947 size_t val_len)
948{
949 struct device *dev = context;
950 struct spi_device *spi = to_spi_device(dev);
951 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
952 struct spi_transfer *tx_xfer = &wcd_spi->xfer2[0];
953 struct spi_transfer *rx_xfer = &wcd_spi->xfer2[1];
954 u8 tx_buf[WCD_SPI_CMD_IRR_LEN];
955
956 if (!reg || !val || reg_len != wcd_spi->reg_bytes ||
957 val_len != wcd_spi->val_bytes) {
958 dev_err(&spi->dev,
959 "%s: Invalid input, reg_len = %zd, val_len = %zd",
960 __func__, reg_len, val_len);
961 return -EINVAL;
962 }
963
964 memset(tx_buf, 0, WCD_SPI_OPCODE_LEN);
965 tx_buf[0] = WCD_SPI_CMD_IRR;
966 tx_buf[1] = *((u8 *)reg);
967
968 wcd_spi_reinit_xfer(tx_xfer);
969 tx_xfer->tx_buf = tx_buf;
970 tx_xfer->rx_buf = NULL;
971 tx_xfer->len = WCD_SPI_CMD_IRR_LEN;
972
973 wcd_spi_reinit_xfer(rx_xfer);
974 rx_xfer->tx_buf = NULL;
975 rx_xfer->rx_buf = val;
976 rx_xfer->len = val_len;
977
978 return spi_sync(spi, &wcd_spi->msg2);
979}
980
981static struct regmap_bus wcd_spi_regmap_bus = {
982 .write = wcd_spi_bus_write,
983 .gather_write = wcd_spi_bus_gwrite,
984 .read = wcd_spi_bus_read,
985 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
986 .val_format_endian_default = REGMAP_ENDIAN_BIG,
987};
988
989static int wcd_spi_state_show(struct seq_file *f, void *ptr)
990{
991 struct spi_device *spi = f->private;
992 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
993 const char *clk_state, *clk_mutex, *xfer_mutex;
994
995 if (test_bit(WCD_SPI_CLK_STATE_ENABLED, &wcd_spi->status_mask))
996 clk_state = "enabled";
997 else
998 clk_state = "disabled";
999
1000 clk_mutex = mutex_is_locked(&wcd_spi->clk_mutex) ?
1001 "locked" : "unlocked";
1002
1003 xfer_mutex = mutex_is_locked(&wcd_spi->xfer_mutex) ?
1004 "locked" : "unlocked";
1005
1006 seq_printf(f, "clk_state = %s\nclk_users = %d\n"
1007 "clk_mutex = %s\nxfer_mutex = %s\n",
1008 clk_state, wcd_spi->clk_users, clk_mutex,
1009 xfer_mutex);
1010 return 0;
1011}
1012
1013static int wcd_spi_state_open(struct inode *inode, struct file *file)
1014{
1015 return single_open(file, wcd_spi_state_show, inode->i_private);
1016}
1017
1018static const struct file_operations state_fops = {
1019 .open = wcd_spi_state_open,
1020 .read = seq_read,
1021 .llseek = seq_lseek,
1022 .release = single_release,
1023};
1024
1025static ssize_t wcd_spi_debugfs_mem_read(struct file *file, char __user *ubuf,
1026 size_t count, loff_t *ppos)
1027{
1028 struct spi_device *spi = file->private_data;
1029 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
1030 struct wcd_spi_debug_data *dbg_data = &wcd_spi->debug_data;
1031 struct wcd_spi_msg msg;
1032 ssize_t buf_size, read_count = 0;
1033 char *buf;
1034 int ret;
1035
1036 if (*ppos < 0 || !count)
1037 return -EINVAL;
1038
1039 if (dbg_data->size == 0 || dbg_data->addr == 0) {
1040 dev_err(&spi->dev,
1041 "%s: Invalid request, size = %u, addr = 0x%x\n",
1042 __func__, dbg_data->size, dbg_data->addr);
1043 return 0;
1044 }
1045
1046 buf_size = count < dbg_data->size ? count : dbg_data->size;
1047 buf = kzalloc(buf_size, GFP_KERNEL);
1048 if (!buf)
1049 return -ENOMEM;
1050
1051 msg.data = buf;
1052 msg.remote_addr = dbg_data->addr;
1053 msg.len = buf_size;
1054 msg.flags = 0;
1055
1056 ret = wcd_spi_data_read(spi, &msg);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -08001057 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -08001058 dev_err(&spi->dev,
1059 "%s: Failed to read %zu bytes from addr 0x%x\n",
1060 __func__, buf_size, msg.remote_addr);
1061 goto done;
1062 }
1063
1064 read_count = simple_read_from_buffer(ubuf, count, ppos, buf, buf_size);
1065
1066done:
1067 kfree(buf);
1068 if (ret < 0)
1069 return ret;
1070 else
1071 return read_count;
1072}
1073
1074static const struct file_operations mem_read_fops = {
1075 .open = simple_open,
1076 .read = wcd_spi_debugfs_mem_read,
1077};
1078
1079static int wcd_spi_debugfs_init(struct spi_device *spi)
1080{
1081 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
1082 struct wcd_spi_debug_data *dbg_data = &wcd_spi->debug_data;
1083 int rc = 0;
1084
1085 dbg_data->dir = debugfs_create_dir("wcd_spi", NULL);
1086 if (IS_ERR_OR_NULL(dbg_data->dir)) {
1087 dbg_data->dir = NULL;
1088 rc = -ENODEV;
1089 goto done;
1090 }
1091
1092 debugfs_create_file("state", 0444, dbg_data->dir, spi, &state_fops);
1093 debugfs_create_u32("addr", 0644, dbg_data->dir,
1094 &dbg_data->addr);
1095 debugfs_create_u32("size", 0644, dbg_data->dir,
1096 &dbg_data->size);
1097
1098 debugfs_create_file("mem_read", 0444, dbg_data->dir,
1099 spi, &mem_read_fops);
1100done:
1101 return rc;
1102}
1103
1104
1105static const struct reg_default wcd_spi_defaults[] = {
1106 {WCD_SPI_SLAVE_SANITY, 0xDEADBEEF},
1107 {WCD_SPI_SLAVE_DEVICE_ID, 0x00500000},
1108 {WCD_SPI_SLAVE_STATUS, 0x80100000},
1109 {WCD_SPI_SLAVE_CONFIG, 0x0F200808},
1110 {WCD_SPI_SLAVE_SW_RESET, 0x00000000},
1111 {WCD_SPI_SLAVE_IRQ_STATUS, 0x00000000},
1112 {WCD_SPI_SLAVE_IRQ_EN, 0x00000000},
1113 {WCD_SPI_SLAVE_IRQ_CLR, 0x00000000},
1114 {WCD_SPI_SLAVE_IRQ_FORCE, 0x00000000},
1115 {WCD_SPI_SLAVE_TX, 0x00000000},
1116 {WCD_SPI_SLAVE_TEST_BUS_DATA, 0x00000000},
1117 {WCD_SPI_SLAVE_TEST_BUS_CTRL, 0x00000000},
1118 {WCD_SPI_SLAVE_SW_RST_IRQ, 0x00000000},
1119 {WCD_SPI_SLAVE_CHAR_CFG, 0x00000000},
1120 {WCD_SPI_SLAVE_CHAR_DATA_MOSI, 0x00000000},
1121 {WCD_SPI_SLAVE_CHAR_DATA_CS_N, 0x00000000},
1122 {WCD_SPI_SLAVE_CHAR_DATA_MISO, 0x00000000},
1123 {WCD_SPI_SLAVE_TRNS_BYTE_CNT, 0x00000000},
1124 {WCD_SPI_SLAVE_TRNS_LEN, 0x00000000},
1125 {WCD_SPI_SLAVE_FIFO_LEVEL, 0x00000000},
1126 {WCD_SPI_SLAVE_GENERICS, 0x80000000},
1127 {WCD_SPI_SLAVE_EXT_BASE_ADDR, 0x00000000},
1128};
1129
1130static bool wcd_spi_is_volatile_reg(struct device *dev,
1131 unsigned int reg)
1132{
1133 switch (reg) {
1134 case WCD_SPI_SLAVE_SANITY:
1135 case WCD_SPI_SLAVE_STATUS:
1136 case WCD_SPI_SLAVE_IRQ_STATUS:
1137 case WCD_SPI_SLAVE_TX:
1138 case WCD_SPI_SLAVE_SW_RST_IRQ:
1139 case WCD_SPI_SLAVE_TRNS_BYTE_CNT:
1140 case WCD_SPI_SLAVE_FIFO_LEVEL:
1141 case WCD_SPI_SLAVE_GENERICS:
1142 return true;
1143 }
1144
1145 return false;
1146}
1147
1148static bool wcd_spi_is_readable_reg(struct device *dev,
1149 unsigned int reg)
1150{
1151 switch (reg) {
1152 case WCD_SPI_SLAVE_SW_RESET:
1153 case WCD_SPI_SLAVE_IRQ_CLR:
1154 case WCD_SPI_SLAVE_IRQ_FORCE:
1155 return false;
1156 }
1157
1158 return true;
1159}
1160
1161static struct regmap_config wcd_spi_regmap_cfg = {
1162 .reg_bits = 8,
1163 .val_bits = 32,
1164 .cache_type = REGCACHE_RBTREE,
1165 .reg_defaults = wcd_spi_defaults,
1166 .num_reg_defaults = ARRAY_SIZE(wcd_spi_defaults),
1167 .max_register = WCD_SPI_MAX_REGISTER,
1168 .volatile_reg = wcd_spi_is_volatile_reg,
1169 .readable_reg = wcd_spi_is_readable_reg,
1170};
1171
1172static int wdsp_spi_init(struct device *dev, void *priv_data)
1173{
1174 struct spi_device *spi = to_spi_device(dev);
1175 int ret;
1176
1177 ret = wcd_spi_init(spi);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -08001178 if (ret < 0)
Banajit Goswamide8271c2017-01-18 00:28:59 -08001179 dev_err(&spi->dev, "%s: Init failed, err = %d\n",
1180 __func__, ret);
1181 return ret;
1182}
1183
1184static int wdsp_spi_deinit(struct device *dev, void *priv_data)
1185{
1186 struct spi_device *spi = to_spi_device(dev);
1187 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
1188
1189 /*
1190 * Deinit means the hardware is reset. Mark the cache
1191 * as dirty here, so init will sync the cache
1192 */
1193 regcache_mark_dirty(wcd_spi->regmap);
1194
1195 return 0;
1196}
1197
1198static struct wdsp_cmpnt_ops wdsp_spi_ops = {
1199 .init = wdsp_spi_init,
1200 .deinit = wdsp_spi_deinit,
1201 .event_handler = wdsp_spi_event_handler,
1202};
1203
1204static int wcd_spi_component_bind(struct device *dev,
1205 struct device *master,
1206 void *data)
1207{
1208 struct spi_device *spi = to_spi_device(dev);
1209 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
1210 int ret = 0;
1211
1212 wcd_spi->m_dev = master;
1213 wcd_spi->m_ops = data;
1214
1215 if (wcd_spi->m_ops &&
1216 wcd_spi->m_ops->register_cmpnt_ops)
1217 ret = wcd_spi->m_ops->register_cmpnt_ops(master, dev,
1218 wcd_spi,
1219 &wdsp_spi_ops);
1220 if (ret) {
1221 dev_err(dev, "%s: register_cmpnt_ops failed, err = %d\n",
1222 __func__, ret);
1223 goto done;
1224 }
1225
1226 wcd_spi->reg_bytes = DIV_ROUND_UP(wcd_spi_regmap_cfg.reg_bits, 8);
1227 wcd_spi->val_bytes = DIV_ROUND_UP(wcd_spi_regmap_cfg.val_bits, 8);
1228
1229 wcd_spi->regmap = devm_regmap_init(&spi->dev, &wcd_spi_regmap_bus,
1230 &spi->dev, &wcd_spi_regmap_cfg);
1231 if (IS_ERR(wcd_spi->regmap)) {
1232 ret = PTR_ERR(wcd_spi->regmap);
1233 dev_err(&spi->dev, "%s: Failed to allocate regmap, err = %d\n",
1234 __func__, ret);
1235 goto done;
1236 }
1237
1238 if (wcd_spi_debugfs_init(spi))
1239 dev_err(&spi->dev, "%s: Failed debugfs init\n", __func__);
1240
1241 spi_message_init(&wcd_spi->msg1);
1242 spi_message_add_tail(&wcd_spi->xfer1, &wcd_spi->msg1);
1243
1244 spi_message_init(&wcd_spi->msg2);
1245 spi_message_add_tail(&wcd_spi->xfer2[0], &wcd_spi->msg2);
1246 spi_message_add_tail(&wcd_spi->xfer2[1], &wcd_spi->msg2);
1247done:
1248 return ret;
1249}
1250
1251static void wcd_spi_component_unbind(struct device *dev,
1252 struct device *master,
1253 void *data)
1254{
1255 struct spi_device *spi = to_spi_device(dev);
1256 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
1257
1258 wcd_spi->m_dev = NULL;
1259 wcd_spi->m_ops = NULL;
1260
1261 spi_transfer_del(&wcd_spi->xfer1);
1262 spi_transfer_del(&wcd_spi->xfer2[0]);
1263 spi_transfer_del(&wcd_spi->xfer2[1]);
1264}
1265
1266static const struct component_ops wcd_spi_component_ops = {
1267 .bind = wcd_spi_component_bind,
1268 .unbind = wcd_spi_component_unbind,
1269};
1270
1271static int wcd_spi_probe(struct spi_device *spi)
1272{
1273 struct wcd_spi_priv *wcd_spi;
1274 int ret = 0;
1275
1276 wcd_spi = devm_kzalloc(&spi->dev, sizeof(*wcd_spi),
1277 GFP_KERNEL);
1278 if (!wcd_spi)
1279 return -ENOMEM;
1280
1281 ret = of_property_read_u32(spi->dev.of_node,
1282 "qcom,mem-base-addr",
1283 &wcd_spi->mem_base_addr);
Xiaoyu Ye1a2d8bd92017-01-31 18:54:15 -08001284 if (ret < 0) {
Banajit Goswamide8271c2017-01-18 00:28:59 -08001285 dev_err(&spi->dev, "%s: Missing %s DT entry",
1286 __func__, "qcom,mem-base-addr");
1287 goto err_ret;
1288 }
1289
1290 dev_dbg(&spi->dev,
1291 "%s: mem_base_addr 0x%x\n", __func__, wcd_spi->mem_base_addr);
1292
1293 mutex_init(&wcd_spi->clk_mutex);
1294 mutex_init(&wcd_spi->xfer_mutex);
1295 INIT_DELAYED_WORK(&wcd_spi->clk_dwork, wcd_spi_clk_work);
1296
1297 wcd_spi->spi = spi;
1298 spi_set_drvdata(spi, wcd_spi);
1299
1300 ret = component_add(&spi->dev, &wcd_spi_component_ops);
1301 if (ret) {
1302 dev_err(&spi->dev, "%s: component_add failed err = %d\n",
1303 __func__, ret);
1304 goto err_component_add;
1305 }
1306
1307 return ret;
1308
1309err_component_add:
1310 mutex_destroy(&wcd_spi->clk_mutex);
1311 mutex_destroy(&wcd_spi->xfer_mutex);
1312err_ret:
1313 devm_kfree(&spi->dev, wcd_spi);
1314 spi_set_drvdata(spi, NULL);
1315 return ret;
1316}
1317
1318static int wcd_spi_remove(struct spi_device *spi)
1319{
1320 struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
1321
1322 component_del(&spi->dev, &wcd_spi_component_ops);
1323
1324 mutex_destroy(&wcd_spi->clk_mutex);
1325 mutex_destroy(&wcd_spi->xfer_mutex);
1326
1327 devm_kfree(&spi->dev, wcd_spi);
1328 spi_set_drvdata(spi, NULL);
1329
1330 return 0;
1331}
1332
1333static const struct of_device_id wcd_spi_of_match[] = {
1334 { .compatible = "qcom,wcd-spi-v2", },
1335 { }
1336};
1337MODULE_DEVICE_TABLE(of, wcd_spi_of_match);
1338
1339static struct spi_driver wcd_spi_driver = {
1340 .driver = {
1341 .name = "wcd-spi-v2",
1342 .of_match_table = wcd_spi_of_match,
1343 },
1344 .probe = wcd_spi_probe,
1345 .remove = wcd_spi_remove,
1346};
1347
1348module_spi_driver(wcd_spi_driver);
1349
1350MODULE_DESCRIPTION("WCD SPI driver");
1351MODULE_LICENSE("GPL v2");