123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
- */
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/of.h>
- #include <linux/debugfs.h>
- #include <linux/delay.h>
- #include <linux/dma-mapping.h>
- #include <linux/bitops.h>
- #include <linux/spi/spi.h>
- #include <linux/regmap.h>
- #include <linux/component.h>
- #include <linux/ratelimit.h>
- #include <linux/platform_device.h>
- #include <sound/wcd-dsp-mgr.h>
- #include <sound/wcd-spi.h>
- #include <soc/wcd-spi-ac.h>
- #include "wcd-spi-registers.h"
- /* Byte manipulations */
- #define SHIFT_1_BYTES (8)
- #define SHIFT_2_BYTES (16)
- #define SHIFT_3_BYTES (24)
- /* Command opcodes */
- #define WCD_SPI_CMD_NOP (0x00)
- #define WCD_SPI_CMD_WREN (0x06)
- #define WCD_SPI_CMD_CLKREQ (0xDA)
- #define WCD_SPI_CMD_RDSR (0x05)
- #define WCD_SPI_CMD_IRR (0x81)
- #define WCD_SPI_CMD_IRW (0x82)
- #define WCD_SPI_CMD_MIOR (0x83)
- #define WCD_SPI_CMD_FREAD (0x0B)
- #define WCD_SPI_CMD_MIOW (0x02)
- #define WCD_SPI_WRITE_FRAME_OPCODE \
- (WCD_SPI_CMD_MIOW << SHIFT_3_BYTES)
- #define WCD_SPI_READ_FRAME_OPCODE \
- (WCD_SPI_CMD_MIOR << SHIFT_3_BYTES)
- #define WCD_SPI_FREAD_FRAME_OPCODE \
- (WCD_SPI_CMD_FREAD << SHIFT_3_BYTES)
- /* Command lengths */
- #define WCD_SPI_OPCODE_LEN (0x01)
- #define WCD_SPI_CMD_NOP_LEN (0x01)
- #define WCD_SPI_CMD_WREN_LEN (0x01)
- #define WCD_SPI_CMD_CLKREQ_LEN (0x04)
- #define WCD_SPI_CMD_IRR_LEN (0x04)
- #define WCD_SPI_CMD_IRW_LEN (0x06)
- #define WCD_SPI_WRITE_SINGLE_LEN (0x08)
- #define WCD_SPI_READ_SINGLE_LEN (0x13)
- #define WCD_SPI_CMD_FREAD_LEN (0x13)
- /* Command delays */
- #define WCD_SPI_CLKREQ_DELAY_USECS (500)
- #define WCD_SPI_CLK_OFF_TIMER_MS (500)
- #define WCD_SPI_RESUME_TIMEOUT_MS 100
- /* Command masks */
- #define WCD_CMD_ADDR_MASK \
- (0xFF | \
- (0xFF << SHIFT_1_BYTES) | \
- (0xFF << SHIFT_2_BYTES))
- /* Clock ctrl request related */
- #define WCD_SPI_CLK_ENABLE true
- #define WCD_SPI_CLK_DISABLE false
- #define WCD_SPI_CLK_FLAG_DELAYED (1 << 0)
- #define WCD_SPI_CLK_FLAG_IMMEDIATE (1 << 1)
- /* Internal addresses */
- #define WCD_SPI_ADDR_IPC_CTL_HOST (0x012014)
- /* Word sizes and min/max lengths */
- #define WCD_SPI_WORD_BYTE_CNT (4)
- #define WCD_SPI_RW_MULTI_MIN_LEN (16)
- /* Max size is 32 bytes less than 64Kbytes */
- #define WCD_SPI_RW_MULTI_MAX_LEN ((64 * 1024) - 32)
- /*
- * Max size for the pre-allocated buffers is the max
- * possible read/write length + 32 bytes for the SPI
- * read/write command header itself.
- */
- #define WCD_SPI_RW_MAX_BUF_SIZE (WCD_SPI_RW_MULTI_MAX_LEN + 32)
- /* Alignment requirements */
- #define WCD_SPI_RW_MIN_ALIGN WCD_SPI_WORD_BYTE_CNT
- #define WCD_SPI_RW_MULTI_ALIGN (16)
- /* Status mask bits */
- #define WCD_SPI_CLK_STATE_ENABLED BIT(0)
- #define WCD_SPI_IS_SUSPENDED BIT(1)
- /* Locking related */
- #define WCD_SPI_MUTEX_LOCK(spi, lock) \
- { \
- dev_vdbg(&spi->dev, "%s: mutex_lock(%s)\n", \
- __func__, __stringify_1(lock)); \
- mutex_lock(&lock); \
- }
- #define WCD_SPI_MUTEX_UNLOCK(spi, lock) \
- { \
- dev_vdbg(&spi->dev, "%s: mutex_unlock(%s)\n", \
- __func__, __stringify_1(lock)); \
- mutex_unlock(&lock); \
- }
- struct wcd_spi_debug_data {
- struct dentry *dir;
- u32 addr;
- u32 size;
- };
- struct wcd_spi_priv {
- struct spi_device *spi;
- u32 mem_base_addr;
- struct regmap *regmap;
- /* Message for single transfer */
- struct spi_message msg1;
- struct spi_transfer xfer1;
- /* Message for two transfers */
- struct spi_message msg2;
- struct spi_transfer xfer2[2];
- /* Register access related */
- u32 reg_bytes;
- u32 val_bytes;
- /* Clock requests related */
- struct mutex clk_mutex;
- int clk_users;
- unsigned long status_mask;
- struct delayed_work clk_dwork;
- /* Transaction related */
- struct mutex xfer_mutex;
- struct device *m_dev;
- struct wdsp_mgr_ops *m_ops;
- /* Debugfs related information */
- struct wcd_spi_debug_data debug_data;
- /* Completion object to indicate system resume completion */
- struct completion resume_comp;
- /* Buffers to hold memory used for transfers */
- void *tx_buf;
- void *rx_buf;
- /* DMA handles for transfer buffers */
- dma_addr_t tx_dma;
- dma_addr_t rx_dma;
- /* Handle to child (qmi client) device */
- struct device *ac_dev;
- };
- enum xfer_request {
- WCD_SPI_XFER_WRITE,
- WCD_SPI_XFER_READ,
- };
- static char *wcd_spi_xfer_req_str(enum xfer_request req)
- {
- if (req == WCD_SPI_XFER_WRITE)
- return "xfer_write";
- else if (req == WCD_SPI_XFER_READ)
- return "xfer_read";
- else
- return "xfer_invalid";
- }
- static void wcd_spi_reinit_xfer(struct spi_transfer *xfer)
- {
- xfer->tx_buf = NULL;
- xfer->rx_buf = NULL;
- xfer->delay_usecs = 0;
- xfer->len = 0;
- }
- static bool wcd_spi_is_suspended(struct wcd_spi_priv *wcd_spi)
- {
- return test_bit(WCD_SPI_IS_SUSPENDED, &wcd_spi->status_mask);
- }
- static bool wcd_spi_can_suspend(struct wcd_spi_priv *wcd_spi)
- {
- struct spi_device *spi = wcd_spi->spi;
- if (wcd_spi->clk_users > 0 ||
- test_bit(WCD_SPI_CLK_STATE_ENABLED, &wcd_spi->status_mask)) {
- dev_err(&spi->dev, "%s: cannot suspend, clk_users = %d\n",
- __func__, wcd_spi->clk_users);
- return false;
- }
- return true;
- }
- static int wcd_spi_wait_for_resume(struct wcd_spi_priv *wcd_spi)
- {
- struct spi_device *spi = wcd_spi->spi;
- int rc = 0;
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- /* If the system is already in resumed state, return right away */
- if (!wcd_spi_is_suspended(wcd_spi))
- goto done;
- /* If suspended then wait for resume to happen */
- reinit_completion(&wcd_spi->resume_comp);
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- rc = wait_for_completion_timeout(&wcd_spi->resume_comp,
- msecs_to_jiffies(WCD_SPI_RESUME_TIMEOUT_MS));
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- if (rc == 0) {
- dev_err(&spi->dev, "%s: failed to resume in %u msec\n",
- __func__, WCD_SPI_RESUME_TIMEOUT_MS);
- rc = -EIO;
- goto done;
- }
- dev_dbg(&spi->dev, "%s: resume successful\n", __func__);
- rc = 0;
- done:
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- return rc;
- }
- static int wcd_spi_read_single(struct spi_device *spi,
- u32 remote_addr, u32 *val)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct spi_transfer *tx_xfer = &wcd_spi->xfer2[0];
- struct spi_transfer *rx_xfer = &wcd_spi->xfer2[1];
- u8 *tx_buf = wcd_spi->tx_buf;
- u8 *rx_buf = wcd_spi->rx_buf;
- u32 frame = 0;
- int ret;
- dev_dbg(&spi->dev, "%s: remote_addr = 0x%x\n",
- __func__, remote_addr);
- if (!tx_buf) {
- dev_err(&spi->dev, "%s: tx_buf not allocated\n",
- __func__);
- return -ENOMEM;
- }
- frame |= WCD_SPI_READ_FRAME_OPCODE;
- frame |= remote_addr & WCD_CMD_ADDR_MASK;
- wcd_spi_reinit_xfer(tx_xfer);
- frame = cpu_to_be32(frame);
- memcpy(tx_buf, &frame, sizeof(frame));
- tx_xfer->tx_buf = tx_buf;
- tx_xfer->len = WCD_SPI_READ_SINGLE_LEN;
- wcd_spi_reinit_xfer(rx_xfer);
- rx_xfer->rx_buf = rx_buf;
- rx_xfer->len = sizeof(*val);
- ret = spi_sync(spi, &wcd_spi->msg2);
- if (ret)
- dev_err(&spi->dev, "%s: spi_sync failed, err %d\n",
- __func__, ret);
- else
- memcpy((u8*) val, rx_buf, sizeof(*val));
- return ret;
- }
- static int wcd_spi_read_multi(struct spi_device *spi,
- u32 remote_addr, u8 *data,
- size_t len)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct spi_transfer *xfer = &wcd_spi->xfer1;
- u8 *tx_buf = wcd_spi->tx_buf;
- u8 *rx_buf = wcd_spi->rx_buf;
- u32 frame = 0;
- int ret;
- dev_dbg(&spi->dev, "%s: addr 0x%x, len = %zd\n",
- __func__, remote_addr, len);
- frame |= WCD_SPI_FREAD_FRAME_OPCODE;
- frame |= remote_addr & WCD_CMD_ADDR_MASK;
- if (!tx_buf || !rx_buf) {
- dev_err(&spi->dev, "%s: %s not allocated\n", __func__,
- (!tx_buf) ? "tx_buf" : "rx_buf");
- return -ENOMEM;
- }
- wcd_spi_reinit_xfer(xfer);
- frame = cpu_to_be32(frame);
- memcpy(tx_buf, &frame, sizeof(frame));
- xfer->tx_buf = tx_buf;
- xfer->rx_buf = rx_buf;
- xfer->len = WCD_SPI_CMD_FREAD_LEN + len;
- ret = spi_sync(spi, &wcd_spi->msg1);
- if (ret) {
- dev_err(&spi->dev, "%s: failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- memcpy(data, rx_buf + WCD_SPI_CMD_FREAD_LEN, len);
- done:
- return ret;
- }
- static int wcd_spi_write_single(struct spi_device *spi,
- u32 remote_addr, u32 val)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct spi_transfer *xfer = &wcd_spi->xfer1;
- u8 *tx_buf = wcd_spi->tx_buf;
- u32 frame = 0;
- dev_dbg(&spi->dev, "%s: remote_addr = 0x%x, val = 0x%x\n",
- __func__, remote_addr, val);
- memset(tx_buf, 0, WCD_SPI_WRITE_SINGLE_LEN);
- frame |= WCD_SPI_WRITE_FRAME_OPCODE;
- frame |= (remote_addr & WCD_CMD_ADDR_MASK);
- frame = cpu_to_be32(frame);
- memcpy(tx_buf, &frame, sizeof(frame));
- memcpy(tx_buf + sizeof(frame), &val, sizeof(val));
- wcd_spi_reinit_xfer(xfer);
- xfer->tx_buf = tx_buf;
- xfer->len = WCD_SPI_WRITE_SINGLE_LEN;
- return spi_sync(spi, &wcd_spi->msg1);
- }
- static int wcd_spi_write_multi(struct spi_device *spi,
- u32 remote_addr, u8 *data,
- size_t len)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct spi_transfer *xfer = &wcd_spi->xfer1;
- u32 frame = 0;
- u8 *tx_buf = wcd_spi->tx_buf;
- int xfer_len, ret;
- dev_dbg(&spi->dev, "%s: addr = 0x%x len = %zd\n",
- __func__, remote_addr, len);
- frame |= WCD_SPI_WRITE_FRAME_OPCODE;
- frame |= (remote_addr & WCD_CMD_ADDR_MASK);
- frame = cpu_to_be32(frame);
- xfer_len = len + sizeof(frame);
- if (!tx_buf) {
- dev_err(&spi->dev, "%s: tx_buf not allocated\n",
- __func__);
- return -ENOMEM;
- }
- memcpy(tx_buf, &frame, sizeof(frame));
- memcpy(tx_buf + sizeof(frame), data, len);
- wcd_spi_reinit_xfer(xfer);
- xfer->tx_buf = tx_buf;
- xfer->len = xfer_len;
- ret = spi_sync(spi, &wcd_spi->msg1);
- if (ret < 0)
- dev_err(&spi->dev,
- "%s: Failed, addr = 0x%x, len = %zd\n",
- __func__, remote_addr, len);
- return ret;
- }
- static int wcd_spi_transfer_split(struct spi_device *spi,
- struct wcd_spi_msg *data_msg,
- enum xfer_request xfer_req)
- {
- u32 addr = data_msg->remote_addr;
- u8 *data = data_msg->data;
- int remain_size = data_msg->len;
- int to_xfer, loop_cnt, ret = 0;
- /* Perform single writes until multi word alignment is met */
- loop_cnt = 1;
- while (remain_size &&
- !IS_ALIGNED(addr, WCD_SPI_RW_MULTI_ALIGN)) {
- if (xfer_req == WCD_SPI_XFER_WRITE)
- ret = wcd_spi_write_single(spi, addr,
- (*(u32 *)data));
- else
- ret = wcd_spi_read_single(spi, addr,
- (u32 *)data);
- if (ret < 0) {
- dev_err(&spi->dev,
- "%s: %s fail iter(%d) start-word addr (0x%x)\n",
- __func__, wcd_spi_xfer_req_str(xfer_req),
- loop_cnt, addr);
- goto done;
- }
- addr += WCD_SPI_WORD_BYTE_CNT;
- data += WCD_SPI_WORD_BYTE_CNT;
- remain_size -= WCD_SPI_WORD_BYTE_CNT;
- loop_cnt++;
- }
- /* Perform multi writes for max allowed multi writes */
- loop_cnt = 1;
- while (remain_size >= WCD_SPI_RW_MULTI_MAX_LEN) {
- if (xfer_req == WCD_SPI_XFER_WRITE)
- ret = wcd_spi_write_multi(spi, addr, data,
- WCD_SPI_RW_MULTI_MAX_LEN);
- else
- ret = wcd_spi_read_multi(spi, addr, data,
- WCD_SPI_RW_MULTI_MAX_LEN);
- if (ret < 0) {
- dev_err(&spi->dev,
- "%s: %s fail iter(%d) max-write addr (0x%x)\n",
- __func__, wcd_spi_xfer_req_str(xfer_req),
- loop_cnt, addr);
- goto done;
- }
- addr += WCD_SPI_RW_MULTI_MAX_LEN;
- data += WCD_SPI_RW_MULTI_MAX_LEN;
- remain_size -= WCD_SPI_RW_MULTI_MAX_LEN;
- loop_cnt++;
- }
- /*
- * Perform write for max possible data that is multiple
- * of the minimum size for multi-write commands.
- */
- to_xfer = remain_size - (remain_size % WCD_SPI_RW_MULTI_MIN_LEN);
- if (remain_size >= WCD_SPI_RW_MULTI_MIN_LEN &&
- to_xfer > 0) {
- if (xfer_req == WCD_SPI_XFER_WRITE)
- ret = wcd_spi_write_multi(spi, addr, data, to_xfer);
- else
- ret = wcd_spi_read_multi(spi, addr, data, to_xfer);
- if (ret < 0) {
- dev_err(&spi->dev,
- "%s: %s fail write addr (0x%x), size (0x%x)\n",
- __func__, wcd_spi_xfer_req_str(xfer_req),
- addr, to_xfer);
- goto done;
- }
- addr += to_xfer;
- data += to_xfer;
- remain_size -= to_xfer;
- }
- /* Perform single writes for the last remaining data */
- loop_cnt = 1;
- while (remain_size > 0) {
- if (xfer_req == WCD_SPI_XFER_WRITE)
- ret = wcd_spi_write_single(spi, addr, (*((u32 *)data)));
- else
- ret = wcd_spi_read_single(spi, addr, (u32 *) data);
- if (ret < 0) {
- dev_err(&spi->dev,
- "%s: %s fail iter(%d) end-write addr (0x%x)\n",
- __func__, wcd_spi_xfer_req_str(xfer_req),
- loop_cnt, addr);
- goto done;
- }
- addr += WCD_SPI_WORD_BYTE_CNT;
- data += WCD_SPI_WORD_BYTE_CNT;
- remain_size -= WCD_SPI_WORD_BYTE_CNT;
- loop_cnt++;
- }
- done:
- return ret;
- }
- static int wcd_spi_cmd_nop(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- u8 *tx_buf = wcd_spi->tx_buf;
- tx_buf[0] = WCD_SPI_CMD_NOP;
- return spi_write(spi, tx_buf, WCD_SPI_CMD_NOP_LEN);
- }
- static int wcd_spi_cmd_clkreq(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct spi_transfer *xfer = &wcd_spi->xfer1;
- u8 *tx_buf = wcd_spi->tx_buf;
- u8 cmd[WCD_SPI_CMD_CLKREQ_LEN] = {
- WCD_SPI_CMD_CLKREQ,
- 0xBA, 0x80, 0x00};
- memcpy(tx_buf, cmd, WCD_SPI_CMD_CLKREQ_LEN);
- wcd_spi_reinit_xfer(xfer);
- xfer->tx_buf = tx_buf;
- xfer->len = WCD_SPI_CMD_CLKREQ_LEN;
- xfer->delay_usecs = WCD_SPI_CLKREQ_DELAY_USECS;
- return spi_sync(spi, &wcd_spi->msg1);
- }
- static int wcd_spi_cmd_wr_en(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- u8 *tx_buf = wcd_spi->tx_buf;
- tx_buf[0] = WCD_SPI_CMD_WREN;
- return spi_write(spi, tx_buf, WCD_SPI_CMD_WREN_LEN);
- }
- static int wcd_spi_cmd_rdsr(struct spi_device *spi,
- u32 *rdsr_status)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct spi_transfer *tx_xfer = &wcd_spi->xfer2[0];
- struct spi_transfer *rx_xfer = &wcd_spi->xfer2[1];
- u8 *tx_buf = wcd_spi->tx_buf;
- u8 *rx_buf = wcd_spi->rx_buf;
- int ret;
- tx_buf[0] = WCD_SPI_CMD_RDSR;
- wcd_spi_reinit_xfer(tx_xfer);
- tx_xfer->tx_buf = tx_buf;
- tx_xfer->len = WCD_SPI_OPCODE_LEN;
- memset(rx_buf, 0, sizeof(*rdsr_status));
- wcd_spi_reinit_xfer(rx_xfer);
- rx_xfer->rx_buf = rx_buf;
- rx_xfer->len = sizeof(*rdsr_status);
- ret = spi_sync(spi, &wcd_spi->msg2);
- if (ret < 0) {
- dev_err(&spi->dev, "%s: RDSR failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- *rdsr_status = be32_to_cpu(*((u32*)rx_buf));
- dev_dbg(&spi->dev, "%s: RDSR success, value = 0x%x\n",
- __func__, *rdsr_status);
- done:
- return ret;
- }
- static int wcd_spi_clk_enable(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- int ret;
- u32 rd_status = 0;
- /* Get the SPI access first */
- if (wcd_spi->ac_dev) {
- ret = wcd_spi_access_ctl(wcd_spi->ac_dev,
- WCD_SPI_ACCESS_REQUEST,
- WCD_SPI_AC_DATA_TRANSFER);
- if (ret) {
- dev_err(&spi->dev,
- "%s: Can't get spi access, err = %d\n",
- __func__, ret);
- return ret;
- }
- }
- ret = wcd_spi_cmd_nop(spi);
- if (ret < 0) {
- dev_err(&spi->dev, "%s: NOP1 failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- ret = wcd_spi_cmd_clkreq(spi);
- if (ret < 0) {
- dev_err(&spi->dev, "%s: CLK_REQ failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- ret = wcd_spi_cmd_nop(spi);
- if (ret < 0) {
- dev_err(&spi->dev, "%s: NOP2 failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- wcd_spi_cmd_rdsr(spi, &rd_status);
- /*
- * Read status zero means reads are not
- * happenning on the bus, possibly because
- * clock request failed.
- */
- if (rd_status) {
- set_bit(WCD_SPI_CLK_STATE_ENABLED,
- &wcd_spi->status_mask);
- } else {
- dev_err(&spi->dev, "%s: RDSR status is zero\n",
- __func__);
- ret = -EIO;
- }
- done:
- return ret;
- }
- static int wcd_spi_clk_disable(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- int ret;
- ret = wcd_spi_write_single(spi, WCD_SPI_ADDR_IPC_CTL_HOST, 0x01);
- if (ret < 0)
- dev_err(&spi->dev, "%s: Failed, err = %d\n",
- __func__, ret);
- /*
- * clear this bit even if clock disable failed
- * as the source clocks might get turned off.
- */
- clear_bit(WCD_SPI_CLK_STATE_ENABLED, &wcd_spi->status_mask);
- /* once the clock is released, SPI access can be released as well */
- if (wcd_spi->ac_dev) {
- ret = wcd_spi_access_ctl(wcd_spi->ac_dev,
- WCD_SPI_ACCESS_RELEASE,
- WCD_SPI_AC_DATA_TRANSFER);
- if (ret)
- dev_err(&spi->dev,
- "%s: SPI access release failed, err = %d\n",
- __func__, ret);
- }
- return ret;
- }
- static int wcd_spi_clk_ctrl(struct spi_device *spi,
- bool request, u32 flags)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- int ret = 0;
- const char *delay_str;
- delay_str = (flags == WCD_SPI_CLK_FLAG_DELAYED) ?
- "delayed" : "immediate";
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- /* Reject any unbalanced disable request */
- if (wcd_spi->clk_users < 0 ||
- (!request && wcd_spi->clk_users == 0)) {
- dev_err(&spi->dev, "%s: Unbalanced clk_users %d for %s\n",
- __func__, wcd_spi->clk_users,
- request ? "enable" : "disable");
- ret = -EINVAL;
- /* Reset the clk_users to 0 */
- wcd_spi->clk_users = 0;
- goto done;
- }
- if (request == WCD_SPI_CLK_ENABLE) {
- /*
- * If the SPI bus is suspended, then return error
- * as the transaction cannot be completed.
- */
- if (wcd_spi_is_suspended(wcd_spi)) {
- dev_err(&spi->dev,
- "%s: SPI suspended, cannot enable clk\n",
- __func__);
- ret = -EIO;
- goto done;
- }
- /* Cancel the disable clk work */
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- cancel_delayed_work_sync(&wcd_spi->clk_dwork);
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- wcd_spi->clk_users++;
- /*
- * If clk state is already set,
- * then clk wasnt really disabled
- */
- if (test_bit(WCD_SPI_CLK_STATE_ENABLED, &wcd_spi->status_mask))
- goto done;
- else if (wcd_spi->clk_users == 1)
- ret = wcd_spi_clk_enable(spi);
- } else {
- wcd_spi->clk_users--;
- /* Clock is still voted for */
- if (wcd_spi->clk_users > 0)
- goto done;
- /*
- * If we are here, clk_users must be 0 and needs
- * to be disabled. Call the disable based on the
- * flags.
- */
- if (flags == WCD_SPI_CLK_FLAG_DELAYED) {
- schedule_delayed_work(&wcd_spi->clk_dwork,
- msecs_to_jiffies(WCD_SPI_CLK_OFF_TIMER_MS));
- } else {
- ret = wcd_spi_clk_disable(spi);
- if (ret < 0)
- dev_err(&spi->dev,
- "%s: Failed to disable clk err = %d\n",
- __func__, ret);
- }
- }
- done:
- dev_dbg(&spi->dev, "%s: updated clk_users = %d, request_%s %s\n",
- __func__, wcd_spi->clk_users, request ? "enable" : "disable",
- request ? "" : delay_str);
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- return ret;
- }
- static int wcd_spi_init(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- int ret;
- ret = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_ENABLE,
- WCD_SPI_CLK_FLAG_IMMEDIATE);
- if (ret < 0)
- goto done;
- ret = wcd_spi_cmd_wr_en(spi);
- if (ret < 0)
- goto err_wr_en;
- /*
- * In case spi_init is called after component deinit,
- * it is possible hardware register state is also reset.
- * Sync the regcache here so hardware state is updated
- * to reflect the cache.
- */
- regcache_sync(wcd_spi->regmap);
- regmap_write(wcd_spi->regmap, WCD_SPI_SLAVE_CONFIG,
- 0x0F3D0800);
- /* Write the MTU to max allowed size */
- regmap_update_bits(wcd_spi->regmap,
- WCD_SPI_SLAVE_TRNS_LEN,
- 0xFFFF0000, 0xFFFF0000);
- err_wr_en:
- wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_DISABLE,
- WCD_SPI_CLK_FLAG_IMMEDIATE);
- done:
- return ret;
- }
- static void wcd_spi_clk_work(struct work_struct *work)
- {
- struct delayed_work *dwork;
- struct wcd_spi_priv *wcd_spi;
- struct spi_device *spi;
- int ret;
- dwork = to_delayed_work(work);
- wcd_spi = container_of(dwork, struct wcd_spi_priv, clk_dwork);
- spi = wcd_spi->spi;
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- ret = wcd_spi_clk_disable(spi);
- if (ret < 0)
- dev_err(&spi->dev,
- "%s: Failed to disable clk, err = %d\n",
- __func__, ret);
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- }
- static int __wcd_spi_data_xfer(struct spi_device *spi,
- struct wcd_spi_msg *msg,
- enum xfer_request xfer_req)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- int ret;
- /* Check for minimum alignment requirements */
- if (!IS_ALIGNED(msg->remote_addr, WCD_SPI_RW_MIN_ALIGN)) {
- dev_err(&spi->dev,
- "%s addr 0x%x is not aligned to 0x%x\n",
- __func__, msg->remote_addr, WCD_SPI_RW_MIN_ALIGN);
- return -EINVAL;
- } else if (msg->len % WCD_SPI_WORD_BYTE_CNT) {
- dev_err(&spi->dev,
- "%s len 0x%zx is not multiple of %d\n",
- __func__, msg->len, WCD_SPI_WORD_BYTE_CNT);
- return -EINVAL;
- }
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- if (wcd_spi_is_suspended(wcd_spi)) {
- dev_dbg(&spi->dev,
- "%s: SPI suspended, cannot perform transfer\n",
- __func__);
- ret = -EIO;
- goto done;
- }
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->xfer_mutex);
- if (msg->len == WCD_SPI_WORD_BYTE_CNT) {
- if (xfer_req == WCD_SPI_XFER_WRITE)
- ret = wcd_spi_write_single(spi, msg->remote_addr,
- (*((u32 *)msg->data)));
- else
- ret = wcd_spi_read_single(spi, msg->remote_addr,
- (u32 *) msg->data);
- } else {
- ret = wcd_spi_transfer_split(spi, msg, xfer_req);
- }
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->xfer_mutex);
- done:
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- return ret;
- }
- static int wcd_spi_data_xfer(struct spi_device *spi,
- struct wcd_spi_msg *msg,
- enum xfer_request req)
- {
- int ret, ret1;
- if (msg->len <= 0) {
- dev_err(&spi->dev, "%s: Invalid size %zd\n",
- __func__, msg->len);
- return -EINVAL;
- }
- /* Request for clock */
- ret = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_ENABLE,
- WCD_SPI_CLK_FLAG_IMMEDIATE);
- if (ret < 0) {
- dev_err(&spi->dev, "%s: clk enable failed %d\n",
- __func__, ret);
- goto done;
- }
- /* Perform the transaction */
- ret = __wcd_spi_data_xfer(spi, msg, req);
- if (ret < 0)
- dev_err(&spi->dev,
- "%s: Failed %s, addr = 0x%x, size = 0x%zx, err = %d\n",
- __func__, wcd_spi_xfer_req_str(req),
- msg->remote_addr, msg->len, ret);
- /* Release the clock even if xfer failed */
- ret1 = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_DISABLE,
- WCD_SPI_CLK_FLAG_DELAYED);
- if (ret1 < 0)
- dev_err(&spi->dev, "%s: clk disable failed %d\n",
- __func__, ret1);
- done:
- return ret;
- }
- /*
- * wcd_spi_data_write: Write data to WCD SPI
- * @spi: spi_device struct
- * @msg: msg that needs to be written to WCD
- *
- * This API writes length of data to address specified. These details
- * about the write are encapsulated in @msg. Write size should be multiple
- * of 4 bytes and write address should be 4-byte aligned.
- */
- static int wcd_spi_data_write(struct spi_device *spi,
- struct wcd_spi_msg *msg)
- {
- if (!spi || !msg) {
- pr_err("%s: Invalid %s\n", __func__,
- (!spi) ? "spi device" : "msg");
- return -EINVAL;
- }
- dev_dbg_ratelimited(&spi->dev, "%s: addr = 0x%x, len = %zu\n",
- __func__, msg->remote_addr, msg->len);
- return wcd_spi_data_xfer(spi, msg, WCD_SPI_XFER_WRITE);
- }
- /*
- * wcd_spi_data_read: Read data from WCD SPI
- * @spi: spi_device struct
- * @msg: msg that needs to be read from WCD
- *
- * This API reads length of data from address specified. These details
- * about the read are encapsulated in @msg. Read size should be multiple
- * of 4 bytes and read address should be 4-byte aligned.
- */
- static int wcd_spi_data_read(struct spi_device *spi,
- struct wcd_spi_msg *msg)
- {
- if (!spi || !msg) {
- pr_err("%s: Invalid %s\n", __func__,
- (!spi) ? "spi device" : "msg");
- return -EINVAL;
- }
- dev_dbg_ratelimited(&spi->dev, "%s: addr = 0x%x,len = %zu\n",
- __func__, msg->remote_addr, msg->len);
- return wcd_spi_data_xfer(spi, msg, WCD_SPI_XFER_READ);
- }
- static int wdsp_spi_dload_section(struct spi_device *spi,
- void *data)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct wdsp_img_section *sec = data;
- struct wcd_spi_msg msg;
- int ret;
- dev_dbg(&spi->dev, "%s: addr = 0x%x, size = 0x%zx\n",
- __func__, sec->addr, sec->size);
- msg.remote_addr = sec->addr + wcd_spi->mem_base_addr;
- msg.data = sec->data;
- msg.len = sec->size;
- ret = __wcd_spi_data_xfer(spi, &msg, WCD_SPI_XFER_WRITE);
- if (ret < 0)
- dev_err(&spi->dev, "%s: fail addr (0x%x) size (0x%zx)\n",
- __func__, msg.remote_addr, msg.len);
- return ret;
- }
- static int wdsp_spi_read_section(struct spi_device *spi, void *data)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct wdsp_img_section *sec = data;
- struct wcd_spi_msg msg;
- int ret;
- msg.remote_addr = sec->addr + wcd_spi->mem_base_addr;
- msg.data = sec->data;
- msg.len = sec->size;
- dev_dbg(&spi->dev, "%s: addr = 0x%x, size = 0x%zx\n",
- __func__, msg.remote_addr, msg.len);
- ret = wcd_spi_data_xfer(spi, &msg, WCD_SPI_XFER_READ);
- if (ret < 0)
- dev_err(&spi->dev, "%s: fail addr (0x%x) size (0x%zx)\n",
- __func__, msg.remote_addr, msg.len);
- return ret;
- }
- static int wdsp_spi_event_handler(struct device *dev, void *priv_data,
- enum wdsp_event_type event,
- void *data)
- {
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct wcd_spi_ops *spi_ops;
- int ret = 0;
- dev_dbg(&spi->dev, "%s: event type %d\n",
- __func__, event);
- switch (event) {
- case WDSP_EVENT_PRE_SHUTDOWN:
- if (wcd_spi->ac_dev) {
- ret = wcd_spi_access_ctl(wcd_spi->ac_dev,
- WCD_SPI_ACCESS_REQUEST,
- WCD_SPI_AC_REMOTE_DOWN);
- if (ret)
- dev_err(&spi->dev,
- "%s: request access failed %d\n",
- __func__, ret);
- }
- break;
- case WDSP_EVENT_POST_SHUTDOWN:
- cancel_delayed_work_sync(&wcd_spi->clk_dwork);
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- if (test_bit(WCD_SPI_CLK_STATE_ENABLED, &wcd_spi->status_mask))
- wcd_spi_clk_disable(spi);
- wcd_spi->clk_users = 0;
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- break;
- case WDSP_EVENT_POST_BOOTUP:
- if (wcd_spi->ac_dev) {
- ret = wcd_spi_access_ctl(wcd_spi->ac_dev,
- WCD_SPI_ACCESS_RELEASE,
- WCD_SPI_AC_REMOTE_DOWN);
- if (ret)
- dev_err(&spi->dev,
- "%s: release access failed %d\n",
- __func__, ret);
- }
- break;
- case WDSP_EVENT_PRE_DLOAD_CODE:
- case WDSP_EVENT_PRE_DLOAD_DATA:
- ret = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_ENABLE,
- WCD_SPI_CLK_FLAG_IMMEDIATE);
- if (ret < 0)
- dev_err(&spi->dev, "%s: clk_req failed %d\n",
- __func__, ret);
- break;
- case WDSP_EVENT_POST_DLOAD_CODE:
- case WDSP_EVENT_POST_DLOAD_DATA:
- case WDSP_EVENT_DLOAD_FAILED:
- ret = wcd_spi_clk_ctrl(spi, WCD_SPI_CLK_DISABLE,
- WCD_SPI_CLK_FLAG_IMMEDIATE);
- if (ret < 0)
- dev_err(&spi->dev, "%s: clk unvote failed %d\n",
- __func__, ret);
- break;
- case WDSP_EVENT_DLOAD_SECTION:
- ret = wdsp_spi_dload_section(spi, data);
- break;
- case WDSP_EVENT_READ_SECTION:
- ret = wdsp_spi_read_section(spi, data);
- break;
- case WDSP_EVENT_SUSPEND:
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- if (!wcd_spi_can_suspend(wcd_spi))
- ret = -EBUSY;
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- break;
- case WDSP_EVENT_RESUME:
- ret = wcd_spi_wait_for_resume(wcd_spi);
- break;
- case WDSP_EVENT_GET_DEVOPS:
- if (!data) {
- dev_err(&spi->dev, "%s: invalid data\n",
- __func__);
- ret = -EINVAL;
- break;
- }
- spi_ops = (struct wcd_spi_ops *) data;
- spi_ops->spi_dev = spi;
- spi_ops->read_dev = wcd_spi_data_read;
- spi_ops->write_dev = wcd_spi_data_write;
- break;
- default:
- dev_dbg(&spi->dev, "%s: Unhandled event %d\n",
- __func__, event);
- break;
- }
- return ret;
- }
- static int wcd_spi_bus_gwrite(void *context, const void *reg,
- size_t reg_len, const void *val,
- size_t val_len)
- {
- struct device *dev = context;
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- u8 *tx_buf = wcd_spi->tx_buf;
- if (!reg || !val || reg_len != wcd_spi->reg_bytes ||
- val_len != wcd_spi->val_bytes) {
- dev_err(&spi->dev,
- "%s: Invalid input, reg_len = %zd, val_len = %zd",
- __func__, reg_len, val_len);
- return -EINVAL;
- }
- memset(tx_buf, 0, WCD_SPI_CMD_IRW_LEN);
- tx_buf[0] = WCD_SPI_CMD_IRW;
- tx_buf[1] = *((u8 *)reg);
- memcpy(tx_buf + WCD_SPI_OPCODE_LEN + reg_len,
- val, val_len);
- return spi_write(spi, tx_buf, WCD_SPI_CMD_IRW_LEN);
- }
- static int wcd_spi_bus_write(void *context, const void *data,
- size_t count)
- {
- struct device *dev = context;
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- if (count < (wcd_spi->reg_bytes + wcd_spi->val_bytes)) {
- dev_err(&spi->dev, "%s: Invalid size %zd\n",
- __func__, count);
- WARN_ON(1);
- return -EINVAL;
- }
- return wcd_spi_bus_gwrite(context, data, wcd_spi->reg_bytes,
- data + wcd_spi->reg_bytes,
- count - wcd_spi->reg_bytes);
- }
- static int wcd_spi_bus_read(void *context, const void *reg,
- size_t reg_len, void *val,
- size_t val_len)
- {
- struct device *dev = context;
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct spi_transfer *tx_xfer = &wcd_spi->xfer2[0];
- struct spi_transfer *rx_xfer = &wcd_spi->xfer2[1];
- u8 *tx_buf = wcd_spi->tx_buf;
- u8 *rx_buf = wcd_spi->rx_buf;
- int ret = 0;
- if (!reg || !val || reg_len != wcd_spi->reg_bytes ||
- val_len != wcd_spi->val_bytes) {
- dev_err(&spi->dev,
- "%s: Invalid input, reg_len = %zd, val_len = %zd",
- __func__, reg_len, val_len);
- return -EINVAL;
- }
- memset(tx_buf, 0, WCD_SPI_CMD_IRR_LEN);
- tx_buf[0] = WCD_SPI_CMD_IRR;
- tx_buf[1] = *((u8 *)reg);
- wcd_spi_reinit_xfer(tx_xfer);
- tx_xfer->tx_buf = tx_buf;
- tx_xfer->rx_buf = NULL;
- tx_xfer->len = WCD_SPI_CMD_IRR_LEN;
- wcd_spi_reinit_xfer(rx_xfer);
- rx_xfer->tx_buf = NULL;
- rx_xfer->rx_buf = rx_buf;
- rx_xfer->len = val_len;
- ret = spi_sync(spi, &wcd_spi->msg2);
- if (ret) {
- dev_err(&spi->dev, "%s: spi_sync failed, err %d\n",
- __func__, ret);
- goto done;
- }
- memcpy(val, rx_buf, val_len);
- done:
- return ret;
- }
- static struct regmap_bus wcd_spi_regmap_bus = {
- .write = wcd_spi_bus_write,
- .gather_write = wcd_spi_bus_gwrite,
- .read = wcd_spi_bus_read,
- .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
- .val_format_endian_default = REGMAP_ENDIAN_BIG,
- };
- static int wcd_spi_state_show(struct seq_file *f, void *ptr)
- {
- struct spi_device *spi = f->private;
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- const char *clk_state, *clk_mutex, *xfer_mutex;
- if (test_bit(WCD_SPI_CLK_STATE_ENABLED, &wcd_spi->status_mask))
- clk_state = "enabled";
- else
- clk_state = "disabled";
- clk_mutex = mutex_is_locked(&wcd_spi->clk_mutex) ?
- "locked" : "unlocked";
- xfer_mutex = mutex_is_locked(&wcd_spi->xfer_mutex) ?
- "locked" : "unlocked";
- seq_printf(f, "clk_state = %s\nclk_users = %d\n"
- "clk_mutex = %s\nxfer_mutex = %s\n",
- clk_state, wcd_spi->clk_users, clk_mutex,
- xfer_mutex);
- return 0;
- }
- static int wcd_spi_state_open(struct inode *inode, struct file *file)
- {
- return single_open(file, wcd_spi_state_show, inode->i_private);
- }
- static const struct file_operations state_fops = {
- .open = wcd_spi_state_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static ssize_t wcd_spi_debugfs_mem_read(struct file *file, char __user *ubuf,
- size_t count, loff_t *ppos)
- {
- struct spi_device *spi = file->private_data;
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct wcd_spi_debug_data *dbg_data = &wcd_spi->debug_data;
- struct wcd_spi_msg msg;
- ssize_t buf_size, read_count = 0;
- char *buf;
- int ret;
- if (*ppos < 0 || !count)
- return -EINVAL;
- if (dbg_data->size == 0 || dbg_data->addr == 0) {
- dev_err(&spi->dev,
- "%s: Invalid request, size = %u, addr = 0x%x\n",
- __func__, dbg_data->size, dbg_data->addr);
- return 0;
- }
- buf_size = count < dbg_data->size ? count : dbg_data->size;
- buf = kzalloc(buf_size, GFP_KERNEL);
- if (!buf)
- return -ENOMEM;
- msg.data = buf;
- msg.remote_addr = dbg_data->addr;
- msg.len = buf_size;
- msg.flags = 0;
- ret = wcd_spi_data_read(spi, &msg);
- if (ret < 0) {
- dev_err(&spi->dev,
- "%s: Failed to read %zu bytes from addr 0x%x\n",
- __func__, buf_size, msg.remote_addr);
- goto done;
- }
- read_count = simple_read_from_buffer(ubuf, count, ppos, buf, buf_size);
- done:
- kfree(buf);
- if (ret < 0)
- return ret;
- else
- return read_count;
- }
- static const struct file_operations mem_read_fops = {
- .open = simple_open,
- .read = wcd_spi_debugfs_mem_read,
- };
- static int wcd_spi_debugfs_init(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct wcd_spi_debug_data *dbg_data = &wcd_spi->debug_data;
- int rc = 0;
- dbg_data->dir = debugfs_create_dir("wcd_spi", NULL);
- if (IS_ERR_OR_NULL(dbg_data->dir)) {
- dbg_data->dir = NULL;
- rc = -ENODEV;
- goto done;
- }
- debugfs_create_file("state", 0444, dbg_data->dir, spi, &state_fops);
- debugfs_create_u32("addr", 0644, dbg_data->dir,
- &dbg_data->addr);
- debugfs_create_u32("size", 0644, dbg_data->dir,
- &dbg_data->size);
- debugfs_create_file("mem_read", 0444, dbg_data->dir,
- spi, &mem_read_fops);
- done:
- return rc;
- }
- static const struct reg_default wcd_spi_defaults[] = {
- {WCD_SPI_SLAVE_SANITY, 0xDEADBEEF},
- {WCD_SPI_SLAVE_DEVICE_ID, 0x00500000},
- {WCD_SPI_SLAVE_STATUS, 0x80100000},
- {WCD_SPI_SLAVE_CONFIG, 0x0F200808},
- {WCD_SPI_SLAVE_SW_RESET, 0x00000000},
- {WCD_SPI_SLAVE_IRQ_STATUS, 0x00000000},
- {WCD_SPI_SLAVE_IRQ_EN, 0x00000000},
- {WCD_SPI_SLAVE_IRQ_CLR, 0x00000000},
- {WCD_SPI_SLAVE_IRQ_FORCE, 0x00000000},
- {WCD_SPI_SLAVE_TX, 0x00000000},
- {WCD_SPI_SLAVE_TEST_BUS_DATA, 0x00000000},
- {WCD_SPI_SLAVE_TEST_BUS_CTRL, 0x00000000},
- {WCD_SPI_SLAVE_SW_RST_IRQ, 0x00000000},
- {WCD_SPI_SLAVE_CHAR_CFG, 0x00000000},
- {WCD_SPI_SLAVE_CHAR_DATA_MOSI, 0x00000000},
- {WCD_SPI_SLAVE_CHAR_DATA_CS_N, 0x00000000},
- {WCD_SPI_SLAVE_CHAR_DATA_MISO, 0x00000000},
- {WCD_SPI_SLAVE_TRNS_BYTE_CNT, 0x00000000},
- {WCD_SPI_SLAVE_TRNS_LEN, 0x00000000},
- {WCD_SPI_SLAVE_FIFO_LEVEL, 0x00000000},
- {WCD_SPI_SLAVE_GENERICS, 0x80000000},
- {WCD_SPI_SLAVE_EXT_BASE_ADDR, 0x00000000},
- };
- static bool wcd_spi_is_volatile_reg(struct device *dev,
- unsigned int reg)
- {
- switch (reg) {
- case WCD_SPI_SLAVE_SANITY:
- case WCD_SPI_SLAVE_STATUS:
- case WCD_SPI_SLAVE_IRQ_STATUS:
- case WCD_SPI_SLAVE_TX:
- case WCD_SPI_SLAVE_SW_RST_IRQ:
- case WCD_SPI_SLAVE_TRNS_BYTE_CNT:
- case WCD_SPI_SLAVE_FIFO_LEVEL:
- case WCD_SPI_SLAVE_GENERICS:
- return true;
- }
- return false;
- }
- static bool wcd_spi_is_readable_reg(struct device *dev,
- unsigned int reg)
- {
- switch (reg) {
- case WCD_SPI_SLAVE_SW_RESET:
- case WCD_SPI_SLAVE_IRQ_CLR:
- case WCD_SPI_SLAVE_IRQ_FORCE:
- return false;
- }
- return true;
- }
- static struct regmap_config wcd_spi_regmap_cfg = {
- .reg_bits = 8,
- .val_bits = 32,
- .cache_type = REGCACHE_RBTREE,
- .reg_defaults = wcd_spi_defaults,
- .num_reg_defaults = ARRAY_SIZE(wcd_spi_defaults),
- .max_register = WCD_SPI_MAX_REGISTER,
- .volatile_reg = wcd_spi_is_volatile_reg,
- .readable_reg = wcd_spi_is_readable_reg,
- };
- static int wcd_spi_add_ac_dev(struct device *dev,
- struct device_node *node)
- {
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct platform_device *pdev;
- int ret = 0;
- pdev = platform_device_alloc("wcd-spi-ac", -1);
- if (IS_ERR_OR_NULL(pdev)) {
- ret = PTR_ERR(pdev);
- dev_err(dev, "%s: pdev alloc failed, ret = %d\n",
- __func__, ret);
- return ret;
- }
- pdev->dev.parent = dev;
- pdev->dev.of_node = node;
- ret = platform_device_add(pdev);
- if (ret) {
- dev_err(dev, "%s: pdev add failed, ret = %d\n",
- __func__, ret);
- goto dealloc_pdev;
- }
- wcd_spi->ac_dev = &pdev->dev;
- return 0;
- dealloc_pdev:
- platform_device_put(pdev);
- return ret;
- }
- static int wdsp_spi_init(struct device *dev, void *priv_data)
- {
- struct spi_device *spi = to_spi_device(dev);
- int ret;
- struct device_node *node;
- for_each_child_of_node(dev->of_node, node) {
- if (!strcmp(node->name, "wcd_spi_ac"))
- wcd_spi_add_ac_dev(dev, node);
- }
- ret = wcd_spi_init(spi);
- if (ret < 0)
- dev_err(&spi->dev, "%s: Init failed, err = %d\n",
- __func__, ret);
- return ret;
- }
- static int wdsp_spi_deinit(struct device *dev, void *priv_data)
- {
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- /*
- * Deinit means the hardware is reset. Mark the cache
- * as dirty here, so init will sync the cache
- */
- regcache_mark_dirty(wcd_spi->regmap);
- return 0;
- }
- static struct wdsp_cmpnt_ops wdsp_spi_ops = {
- .init = wdsp_spi_init,
- .deinit = wdsp_spi_deinit,
- .event_handler = wdsp_spi_event_handler,
- };
- static int wcd_spi_component_bind(struct device *dev,
- struct device *master,
- void *data)
- {
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- int ret = 0;
- wcd_spi->m_dev = master;
- wcd_spi->m_ops = data;
- if (wcd_spi->m_ops &&
- wcd_spi->m_ops->register_cmpnt_ops)
- ret = wcd_spi->m_ops->register_cmpnt_ops(master, dev,
- wcd_spi,
- &wdsp_spi_ops);
- if (ret) {
- dev_err(dev, "%s: register_cmpnt_ops failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- wcd_spi->reg_bytes = DIV_ROUND_UP(wcd_spi_regmap_cfg.reg_bits, 8);
- wcd_spi->val_bytes = DIV_ROUND_UP(wcd_spi_regmap_cfg.val_bits, 8);
- wcd_spi->regmap = devm_regmap_init(&spi->dev, &wcd_spi_regmap_bus,
- &spi->dev, &wcd_spi_regmap_cfg);
- if (IS_ERR(wcd_spi->regmap)) {
- ret = PTR_ERR(wcd_spi->regmap);
- dev_err(&spi->dev, "%s: Failed to allocate regmap, err = %d\n",
- __func__, ret);
- goto done;
- }
- if (wcd_spi_debugfs_init(spi))
- dev_err(&spi->dev, "%s: Failed debugfs init\n", __func__);
- spi_message_init(&wcd_spi->msg1);
- spi_message_add_tail(&wcd_spi->xfer1, &wcd_spi->msg1);
- spi_message_init(&wcd_spi->msg2);
- spi_message_add_tail(&wcd_spi->xfer2[0], &wcd_spi->msg2);
- spi_message_add_tail(&wcd_spi->xfer2[1], &wcd_spi->msg2);
- /* Pre-allocate the buffers */
- wcd_spi->tx_buf = dma_zalloc_coherent(&spi->dev,
- WCD_SPI_RW_MAX_BUF_SIZE,
- &wcd_spi->tx_dma, GFP_KERNEL);
- if (!wcd_spi->tx_buf) {
- ret = -ENOMEM;
- goto done;
- }
- wcd_spi->rx_buf = dma_zalloc_coherent(&spi->dev,
- WCD_SPI_RW_MAX_BUF_SIZE,
- &wcd_spi->rx_dma, GFP_KERNEL);
- if (!wcd_spi->rx_buf) {
- dma_free_coherent(&spi->dev, WCD_SPI_RW_MAX_BUF_SIZE,
- wcd_spi->tx_buf, wcd_spi->tx_dma);
- wcd_spi->tx_buf = NULL;
- ret = -ENOMEM;
- goto done;
- }
- done:
- return ret;
- }
- static void wcd_spi_component_unbind(struct device *dev,
- struct device *master,
- void *data)
- {
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- struct wcd_spi_debug_data *dbg_data = &wcd_spi->debug_data;
- debugfs_remove_recursive(dbg_data->dir);
- dbg_data->dir = NULL;
- wcd_spi->m_dev = NULL;
- wcd_spi->m_ops = NULL;
- spi_transfer_del(&wcd_spi->xfer1);
- spi_transfer_del(&wcd_spi->xfer2[0]);
- spi_transfer_del(&wcd_spi->xfer2[1]);
- dma_free_coherent(&spi->dev, WCD_SPI_RW_MAX_BUF_SIZE,
- wcd_spi->tx_buf, wcd_spi->tx_dma);
- dma_free_coherent(&spi->dev, WCD_SPI_RW_MAX_BUF_SIZE,
- wcd_spi->rx_buf, wcd_spi->rx_dma);
- wcd_spi->tx_buf = NULL;
- wcd_spi->rx_buf = NULL;
- }
- static const struct component_ops wcd_spi_component_ops = {
- .bind = wcd_spi_component_bind,
- .unbind = wcd_spi_component_unbind,
- };
- static int wcd_spi_probe(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi;
- int ret = 0;
- wcd_spi = devm_kzalloc(&spi->dev, sizeof(*wcd_spi),
- GFP_KERNEL);
- if (!wcd_spi)
- return -ENOMEM;
- ret = of_property_read_u32(spi->dev.of_node,
- "qcom,mem-base-addr",
- &wcd_spi->mem_base_addr);
- if (ret < 0) {
- dev_err(&spi->dev, "%s: Missing %s DT entry",
- __func__, "qcom,mem-base-addr");
- goto err_ret;
- }
- dev_dbg(&spi->dev,
- "%s: mem_base_addr 0x%x\n", __func__, wcd_spi->mem_base_addr);
- mutex_init(&wcd_spi->clk_mutex);
- mutex_init(&wcd_spi->xfer_mutex);
- INIT_DELAYED_WORK(&wcd_spi->clk_dwork, wcd_spi_clk_work);
- init_completion(&wcd_spi->resume_comp);
- arch_setup_dma_ops(&spi->dev, 0, 0, NULL, true);
- wcd_spi->spi = spi;
- spi_set_drvdata(spi, wcd_spi);
- ret = component_add(&spi->dev, &wcd_spi_component_ops);
- if (ret) {
- dev_err(&spi->dev, "%s: component_add failed err = %d\n",
- __func__, ret);
- goto err_component_add;
- }
- return ret;
- err_component_add:
- mutex_destroy(&wcd_spi->clk_mutex);
- mutex_destroy(&wcd_spi->xfer_mutex);
- err_ret:
- devm_kfree(&spi->dev, wcd_spi);
- spi_set_drvdata(spi, NULL);
- return ret;
- }
- static int wcd_spi_remove(struct spi_device *spi)
- {
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- component_del(&spi->dev, &wcd_spi_component_ops);
- mutex_destroy(&wcd_spi->clk_mutex);
- mutex_destroy(&wcd_spi->xfer_mutex);
- devm_kfree(&spi->dev, wcd_spi);
- spi_set_drvdata(spi, NULL);
- return 0;
- }
- #ifdef CONFIG_PM
- static int wcd_spi_suspend(struct device *dev)
- {
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- int rc = 0;
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- if (!wcd_spi_can_suspend(wcd_spi)) {
- rc = -EBUSY;
- goto done;
- }
- /*
- * If we are here, it is okay to let the suspend go
- * through for this driver. But, still need to notify
- * the master to make sure all other components can suspend
- * as well.
- */
- if (wcd_spi->m_dev && wcd_spi->m_ops &&
- wcd_spi->m_ops->suspend) {
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- rc = wcd_spi->m_ops->suspend(wcd_spi->m_dev);
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- }
- if (rc == 0)
- set_bit(WCD_SPI_IS_SUSPENDED, &wcd_spi->status_mask);
- else
- dev_dbg(&spi->dev, "%s: cannot suspend, err = %d\n",
- __func__, rc);
- done:
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- return rc;
- }
- static int wcd_spi_resume(struct device *dev)
- {
- struct spi_device *spi = to_spi_device(dev);
- struct wcd_spi_priv *wcd_spi = spi_get_drvdata(spi);
- WCD_SPI_MUTEX_LOCK(spi, wcd_spi->clk_mutex);
- clear_bit(WCD_SPI_IS_SUSPENDED, &wcd_spi->status_mask);
- complete(&wcd_spi->resume_comp);
- WCD_SPI_MUTEX_UNLOCK(spi, wcd_spi->clk_mutex);
- return 0;
- }
- static const struct dev_pm_ops wcd_spi_pm_ops = {
- .suspend = wcd_spi_suspend,
- .resume = wcd_spi_resume,
- };
- #endif
- static const struct of_device_id wcd_spi_of_match[] = {
- { .compatible = "qcom,wcd-spi-v2", },
- { }
- };
- MODULE_DEVICE_TABLE(of, wcd_spi_of_match);
- static struct spi_driver wcd_spi_driver = {
- .driver = {
- .name = "wcd-spi-v2",
- .of_match_table = wcd_spi_of_match,
- #ifdef CONFIG_PM
- .pm = &wcd_spi_pm_ops,
- #endif
- },
- .probe = wcd_spi_probe,
- .remove = wcd_spi_remove,
- };
- module_spi_driver(wcd_spi_driver);
- MODULE_DESCRIPTION("WCD SPI driver");
- MODULE_LICENSE("GPL v2");
|