123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719 |
- // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
- // Copyright(c) 2015-18 Intel Corporation.
- /*
- * stream.c - SoundWire Bus stream operations.
- */
- #include <linux/delay.h>
- #include <linux/device.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/mod_devicetable.h>
- #include <linux/slab.h>
- #include <linux/soundwire/sdw_registers.h>
- #include <linux/soundwire/sdw.h>
- #include "bus.h"
- /*
- * Array of supported rows and columns as per MIPI SoundWire Specification 1.1
- *
- * The rows are arranged as per the array index value programmed
- * in register. The index 15 has dummy value 0 in order to fill hole.
- */
- int rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
- 96, 100, 120, 128, 150, 160, 250, 0,
- 192, 200, 240, 256, 72, 144, 90, 180};
- int cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};
- static int sdw_find_col_index(int col)
- {
- int i;
- for (i = 0; i < SDW_FRAME_COLS; i++) {
- if (cols[i] == col)
- return i;
- }
- pr_warn("Requested column not found, selecting lowest column no: 2\n");
- return 0;
- }
- static int sdw_find_row_index(int row)
- {
- int i;
- for (i = 0; i < SDW_FRAME_ROWS; i++) {
- if (rows[i] == row)
- return i;
- }
- pr_warn("Requested row not found, selecting lowest row no: 48\n");
- return 0;
- }
- static int _sdw_program_slave_port_params(struct sdw_bus *bus,
- struct sdw_slave *slave,
- struct sdw_transport_params *t_params,
- enum sdw_dpn_type type)
- {
- u32 addr1, addr2, addr3, addr4;
- int ret;
- u16 wbuf;
- if (bus->params.next_bank) {
- addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num);
- addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num);
- addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num);
- addr4 = SDW_DPN_HCTRL_B1(t_params->port_num);
- } else {
- addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num);
- addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num);
- addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num);
- addr4 = SDW_DPN_HCTRL_B0(t_params->port_num);
- }
- /* Program DPN_OffsetCtrl2 registers */
- ret = sdw_write(slave, addr1, t_params->offset2);
- if (ret < 0) {
- dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed");
- return ret;
- }
- /* Program DPN_BlockCtrl3 register */
- ret = sdw_write(slave, addr2, t_params->blk_pkg_mode);
- if (ret < 0) {
- dev_err(bus->dev, "DPN_BlockCtrl3 register write failed");
- return ret;
- }
- /*
- * Data ports are FULL, SIMPLE and REDUCED. This function handles
- * FULL and REDUCED only and and beyond this point only FULL is
- * handled, so bail out if we are not FULL data port type
- */
- if (type != SDW_DPN_FULL)
- return ret;
- /* Program DPN_SampleCtrl2 register */
- wbuf = (t_params->sample_interval - 1);
- wbuf &= SDW_DPN_SAMPLECTRL_HIGH;
- wbuf >>= SDW_REG_SHIFT(SDW_DPN_SAMPLECTRL_HIGH);
- ret = sdw_write(slave, addr3, wbuf);
- if (ret < 0) {
- dev_err(bus->dev, "DPN_SampleCtrl2 register write failed");
- return ret;
- }
- /* Program DPN_HCtrl register */
- wbuf = t_params->hstart;
- wbuf <<= SDW_REG_SHIFT(SDW_DPN_HCTRL_HSTART);
- wbuf |= t_params->hstop;
- ret = sdw_write(slave, addr4, wbuf);
- if (ret < 0)
- dev_err(bus->dev, "DPN_HCtrl register write failed");
- return ret;
- }
- static int sdw_program_slave_port_params(struct sdw_bus *bus,
- struct sdw_slave_runtime *s_rt,
- struct sdw_port_runtime *p_rt)
- {
- struct sdw_transport_params *t_params = &p_rt->transport_params;
- struct sdw_port_params *p_params = &p_rt->port_params;
- struct sdw_slave_prop *slave_prop = &s_rt->slave->prop;
- u32 addr1, addr2, addr3, addr4, addr5, addr6;
- struct sdw_dpn_prop *dpn_prop;
- int ret;
- u8 wbuf;
- dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
- s_rt->direction,
- t_params->port_num);
- if (!dpn_prop)
- return -EINVAL;
- addr1 = SDW_DPN_PORTCTRL(t_params->port_num);
- addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num);
- if (bus->params.next_bank) {
- addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num);
- addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num);
- addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num);
- addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num);
- } else {
- addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num);
- addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num);
- addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num);
- addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num);
- }
- /* Program DPN_PortCtrl register */
- wbuf = p_params->data_mode << SDW_REG_SHIFT(SDW_DPN_PORTCTRL_DATAMODE);
- wbuf |= p_params->flow_mode;
- ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf);
- if (ret < 0) {
- dev_err(&s_rt->slave->dev,
- "DPN_PortCtrl register write failed for port %d",
- t_params->port_num);
- return ret;
- }
- /* Program DPN_BlockCtrl1 register */
- ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1));
- if (ret < 0) {
- dev_err(&s_rt->slave->dev,
- "DPN_BlockCtrl1 register write failed for port %d",
- t_params->port_num);
- return ret;
- }
- /* Program DPN_SampleCtrl1 register */
- wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
- ret = sdw_write(s_rt->slave, addr3, wbuf);
- if (ret < 0) {
- dev_err(&s_rt->slave->dev,
- "DPN_SampleCtrl1 register write failed for port %d",
- t_params->port_num);
- return ret;
- }
- /* Program DPN_OffsetCtrl1 registers */
- ret = sdw_write(s_rt->slave, addr4, t_params->offset1);
- if (ret < 0) {
- dev_err(&s_rt->slave->dev,
- "DPN_OffsetCtrl1 register write failed for port %d",
- t_params->port_num);
- return ret;
- }
- /* Program DPN_BlockCtrl2 register*/
- if (t_params->blk_grp_ctrl_valid) {
- ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl);
- if (ret < 0) {
- dev_err(&s_rt->slave->dev,
- "DPN_BlockCtrl2 reg write failed for port %d",
- t_params->port_num);
- return ret;
- }
- }
- /* program DPN_LaneCtrl register */
- if (slave_prop->lane_control_support) {
- ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl);
- if (ret < 0) {
- dev_err(&s_rt->slave->dev,
- "DPN_LaneCtrl register write failed for port %d",
- t_params->port_num);
- return ret;
- }
- }
- if (dpn_prop->type != SDW_DPN_SIMPLE) {
- ret = _sdw_program_slave_port_params(bus, s_rt->slave,
- t_params, dpn_prop->type);
- if (ret < 0)
- dev_err(&s_rt->slave->dev,
- "Transport reg write failed for port: %d",
- t_params->port_num);
- }
- return ret;
- }
- static int sdw_program_master_port_params(struct sdw_bus *bus,
- struct sdw_port_runtime *p_rt)
- {
- int ret;
- /*
- * we need to set transport and port parameters for the port.
- * Transport parameters refers to the smaple interval, offsets and
- * hstart/stop etc of the data. Port parameters refers to word
- * length, flow mode etc of the port
- */
- ret = bus->port_ops->dpn_set_port_transport_params(bus,
- &p_rt->transport_params,
- bus->params.next_bank);
- if (ret < 0)
- return ret;
- return bus->port_ops->dpn_set_port_params(bus,
- &p_rt->port_params,
- bus->params.next_bank);
- }
- /**
- * sdw_program_port_params() - Programs transport parameters of Master(s)
- * and Slave(s)
- *
- * @m_rt: Master stream runtime
- */
- static int sdw_program_port_params(struct sdw_master_runtime *m_rt)
- {
- struct sdw_slave_runtime *s_rt = NULL;
- struct sdw_bus *bus = m_rt->bus;
- struct sdw_port_runtime *p_rt;
- int ret = 0;
- /* Program transport & port parameters for Slave(s) */
- list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
- list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
- ret = sdw_program_slave_port_params(bus, s_rt, p_rt);
- if (ret < 0)
- return ret;
- }
- }
- /* Program transport & port parameters for Master(s) */
- list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
- ret = sdw_program_master_port_params(bus, p_rt);
- if (ret < 0)
- return ret;
- }
- return 0;
- }
- /**
- * sdw_enable_disable_slave_ports: Enable/disable slave data port
- *
- * @bus: bus instance
- * @s_rt: slave runtime
- * @p_rt: port runtime
- * @en: enable or disable operation
- *
- * This function only sets the enable/disable bits in the relevant bank, the
- * actual enable/disable is done with a bank switch
- */
- static int sdw_enable_disable_slave_ports(struct sdw_bus *bus,
- struct sdw_slave_runtime *s_rt,
- struct sdw_port_runtime *p_rt, bool en)
- {
- struct sdw_transport_params *t_params = &p_rt->transport_params;
- u32 addr;
- int ret;
- if (bus->params.next_bank)
- addr = SDW_DPN_CHANNELEN_B1(p_rt->num);
- else
- addr = SDW_DPN_CHANNELEN_B0(p_rt->num);
- /*
- * Since bus doesn't support sharing a port across two streams,
- * it is safe to reset this register
- */
- if (en)
- ret = sdw_update(s_rt->slave, addr, 0xFF, p_rt->ch_mask);
- else
- ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);
- if (ret < 0)
- dev_err(&s_rt->slave->dev,
- "Slave chn_en reg write failed:%d port:%d",
- ret, t_params->port_num);
- return ret;
- }
- static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
- struct sdw_port_runtime *p_rt, bool en)
- {
- struct sdw_transport_params *t_params = &p_rt->transport_params;
- struct sdw_bus *bus = m_rt->bus;
- struct sdw_enable_ch enable_ch;
- int ret = 0;
- enable_ch.port_num = p_rt->num;
- enable_ch.ch_mask = p_rt->ch_mask;
- enable_ch.enable = en;
- /* Perform Master port channel(s) enable/disable */
- if (bus->port_ops->dpn_port_enable_ch) {
- ret = bus->port_ops->dpn_port_enable_ch(bus,
- &enable_ch, bus->params.next_bank);
- if (ret < 0) {
- dev_err(bus->dev,
- "Master chn_en write failed:%d port:%d",
- ret, t_params->port_num);
- return ret;
- }
- } else {
- dev_err(bus->dev,
- "dpn_port_enable_ch not supported, %s failed\n",
- en ? "enable" : "disable");
- return -EINVAL;
- }
- return 0;
- }
- /**
- * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
- * Slave(s)
- *
- * @m_rt: Master stream runtime
- * @en: mode (enable/disable)
- */
- static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en)
- {
- struct sdw_port_runtime *s_port, *m_port;
- struct sdw_slave_runtime *s_rt = NULL;
- int ret = 0;
- /* Enable/Disable Slave port(s) */
- list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
- list_for_each_entry(s_port, &s_rt->port_list, port_node) {
- ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt,
- s_port, en);
- if (ret < 0)
- return ret;
- }
- }
- /* Enable/Disable Master port(s) */
- list_for_each_entry(m_port, &m_rt->port_list, port_node) {
- ret = sdw_enable_disable_master_ports(m_rt, m_port, en);
- if (ret < 0)
- return ret;
- }
- return 0;
- }
- static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt,
- struct sdw_prepare_ch prep_ch, enum sdw_port_prep_ops cmd)
- {
- const struct sdw_slave_ops *ops = s_rt->slave->ops;
- int ret;
- if (ops->port_prep) {
- ret = ops->port_prep(s_rt->slave, &prep_ch, cmd);
- if (ret < 0) {
- dev_err(&s_rt->slave->dev,
- "Slave Port Prep cmd %d failed: %d", cmd, ret);
- return ret;
- }
- }
- return 0;
- }
- static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
- struct sdw_slave_runtime *s_rt,
- struct sdw_port_runtime *p_rt, bool prep)
- {
- struct completion *port_ready = NULL;
- struct sdw_dpn_prop *dpn_prop;
- struct sdw_prepare_ch prep_ch;
- unsigned int time_left;
- bool intr = false;
- int ret = 0, val;
- u32 addr;
- prep_ch.num = p_rt->num;
- prep_ch.ch_mask = p_rt->ch_mask;
- dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
- s_rt->direction,
- prep_ch.num);
- if (!dpn_prop) {
- dev_err(bus->dev,
- "Slave Port:%d properties not found", prep_ch.num);
- return -EINVAL;
- }
- prep_ch.prepare = prep;
- prep_ch.bank = bus->params.next_bank;
- if (dpn_prop->device_interrupts || !dpn_prop->simple_ch_prep_sm)
- intr = true;
- /*
- * Enable interrupt before Port prepare.
- * For Port de-prepare, it is assumed that port
- * was prepared earlier
- */
- if (prep && intr) {
- ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
- dpn_prop->device_interrupts);
- if (ret < 0)
- return ret;
- }
- /* Inform slave about the impending port prepare */
- sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_PRE_PREP);
- /* Prepare Slave port implementing CP_SM */
- if (!dpn_prop->simple_ch_prep_sm) {
- addr = SDW_DPN_PREPARECTRL(p_rt->num);
- if (prep)
- ret = sdw_update(s_rt->slave, addr,
- 0xFF, p_rt->ch_mask);
- else
- ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);
- if (ret < 0) {
- dev_err(&s_rt->slave->dev,
- "Slave prep_ctrl reg write failed");
- return ret;
- }
- /* Wait for completion on port ready */
- port_ready = &s_rt->slave->port_ready[prep_ch.num];
- time_left = wait_for_completion_timeout(port_ready,
- msecs_to_jiffies(dpn_prop->ch_prep_timeout));
- val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
- val &= p_rt->ch_mask;
- if (!time_left || val) {
- dev_err(&s_rt->slave->dev,
- "Chn prep failed for port:%d", prep_ch.num);
- return -ETIMEDOUT;
- }
- }
- /* Inform slaves about ports prepared */
- sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_POST_PREP);
- /* Disable interrupt after Port de-prepare */
- if (!prep && intr)
- ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
- dpn_prop->device_interrupts);
- return ret;
- }
- static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
- struct sdw_port_runtime *p_rt, bool prep)
- {
- struct sdw_transport_params *t_params = &p_rt->transport_params;
- struct sdw_bus *bus = m_rt->bus;
- const struct sdw_master_port_ops *ops = bus->port_ops;
- struct sdw_prepare_ch prep_ch;
- int ret = 0;
- prep_ch.num = p_rt->num;
- prep_ch.ch_mask = p_rt->ch_mask;
- prep_ch.prepare = prep; /* Prepare/De-prepare */
- prep_ch.bank = bus->params.next_bank;
- /* Pre-prepare/Pre-deprepare port(s) */
- if (ops->dpn_port_prep) {
- ret = ops->dpn_port_prep(bus, &prep_ch);
- if (ret < 0) {
- dev_err(bus->dev, "Port prepare failed for port:%d",
- t_params->port_num);
- return ret;
- }
- }
- return ret;
- }
- /**
- * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
- * Slave(s)
- *
- * @m_rt: Master runtime handle
- * @prep: Prepare or De-prepare
- */
- static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep)
- {
- struct sdw_slave_runtime *s_rt = NULL;
- struct sdw_port_runtime *p_rt;
- int ret = 0;
- /* Prepare/De-prepare Slave port(s) */
- list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
- list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
- ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt,
- p_rt, prep);
- if (ret < 0)
- return ret;
- }
- }
- /* Prepare/De-prepare Master port(s) */
- list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
- ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep);
- if (ret < 0)
- return ret;
- }
- return ret;
- }
- /**
- * sdw_notify_config() - Notify bus configuration
- *
- * @m_rt: Master runtime handle
- *
- * This function notifies the Master(s) and Slave(s) of the
- * new bus configuration.
- */
- static int sdw_notify_config(struct sdw_master_runtime *m_rt)
- {
- struct sdw_slave_runtime *s_rt;
- struct sdw_bus *bus = m_rt->bus;
- struct sdw_slave *slave;
- int ret = 0;
- if (bus->ops->set_bus_conf) {
- ret = bus->ops->set_bus_conf(bus, &bus->params);
- if (ret < 0)
- return ret;
- }
- list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
- slave = s_rt->slave;
- if (slave->ops->bus_config) {
- ret = slave->ops->bus_config(slave, &bus->params);
- if (ret < 0)
- dev_err(bus->dev, "Notify Slave: %d failed",
- slave->dev_num);
- return ret;
- }
- }
- return ret;
- }
- /**
- * sdw_program_params() - Program transport and port parameters for Master(s)
- * and Slave(s)
- *
- * @bus: SDW bus instance
- */
- static int sdw_program_params(struct sdw_bus *bus)
- {
- struct sdw_master_runtime *m_rt = NULL;
- int ret = 0;
- list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
- ret = sdw_program_port_params(m_rt);
- if (ret < 0) {
- dev_err(bus->dev,
- "Program transport params failed: %d", ret);
- return ret;
- }
- ret = sdw_notify_config(m_rt);
- if (ret < 0) {
- dev_err(bus->dev, "Notify bus config failed: %d", ret);
- return ret;
- }
- /* Enable port(s) on alternate bank for all active streams */
- if (m_rt->stream->state != SDW_STREAM_ENABLED)
- continue;
- ret = sdw_enable_disable_ports(m_rt, true);
- if (ret < 0) {
- dev_err(bus->dev, "Enable channel failed: %d", ret);
- return ret;
- }
- }
- return ret;
- }
- static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
- {
- int col_index, row_index;
- bool multi_link;
- struct sdw_msg *wr_msg;
- u8 *wbuf = NULL;
- int ret = 0;
- u16 addr;
- wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
- if (!wr_msg)
- return -ENOMEM;
- bus->defer_msg.msg = wr_msg;
- wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL);
- if (!wbuf) {
- ret = -ENOMEM;
- goto error_1;
- }
- /* Get row and column index to program register */
- col_index = sdw_find_col_index(bus->params.col);
- row_index = sdw_find_row_index(bus->params.row);
- wbuf[0] = col_index | (row_index << 3);
- if (bus->params.next_bank)
- addr = SDW_SCP_FRAMECTRL_B1;
- else
- addr = SDW_SCP_FRAMECTRL_B0;
- sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
- SDW_MSG_FLAG_WRITE, wbuf);
- wr_msg->ssp_sync = true;
- /*
- * Set the multi_link flag only when both the hardware supports
- * and there is a stream handled by multiple masters
- */
- multi_link = bus->multi_link && (m_rt_count > 1);
- if (multi_link)
- ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg);
- else
- ret = sdw_transfer(bus, wr_msg);
- if (ret < 0) {
- dev_err(bus->dev, "Slave frame_ctrl reg write failed");
- goto error;
- }
- if (!multi_link) {
- kfree(wr_msg);
- kfree(wbuf);
- bus->defer_msg.msg = NULL;
- bus->params.curr_bank = !bus->params.curr_bank;
- bus->params.next_bank = !bus->params.next_bank;
- }
- return 0;
- error:
- kfree(wbuf);
- error_1:
- kfree(wr_msg);
- return ret;
- }
- /**
- * sdw_ml_sync_bank_switch: Multilink register bank switch
- *
- * @bus: SDW bus instance
- *
- * Caller function should free the buffers on error
- */
- static int sdw_ml_sync_bank_switch(struct sdw_bus *bus)
- {
- unsigned long time_left;
- if (!bus->multi_link)
- return 0;
- /* Wait for completion of transfer */
- time_left = wait_for_completion_timeout(&bus->defer_msg.complete,
- bus->bank_switch_timeout);
- if (!time_left) {
- dev_err(bus->dev, "Controller Timed out on bank switch");
- return -ETIMEDOUT;
- }
- bus->params.curr_bank = !bus->params.curr_bank;
- bus->params.next_bank = !bus->params.next_bank;
- if (bus->defer_msg.msg) {
- kfree(bus->defer_msg.msg->buf);
- kfree(bus->defer_msg.msg);
- }
- return 0;
- }
- static int do_bank_switch(struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- const struct sdw_master_ops *ops;
- struct sdw_bus *bus = NULL;
- bool multi_link = false;
- int ret = 0;
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- ops = bus->ops;
- if (bus->multi_link) {
- multi_link = true;
- mutex_lock(&bus->msg_lock);
- }
- /* Pre-bank switch */
- if (ops->pre_bank_switch) {
- ret = ops->pre_bank_switch(bus);
- if (ret < 0) {
- dev_err(bus->dev,
- "Pre bank switch op failed: %d", ret);
- goto msg_unlock;
- }
- }
- /*
- * Perform Bank switch operation.
- * For multi link cases, the actual bank switch is
- * synchronized across all Masters and happens later as a
- * part of post_bank_switch ops.
- */
- ret = sdw_bank_switch(bus, stream->m_rt_count);
- if (ret < 0) {
- dev_err(bus->dev, "Bank switch failed: %d", ret);
- goto error;
- }
- }
- /*
- * For multi link cases, it is expected that the bank switch is
- * triggered by the post_bank_switch for the first Master in the list
- * and for the other Masters the post_bank_switch() should return doing
- * nothing.
- */
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- ops = bus->ops;
- /* Post-bank switch */
- if (ops->post_bank_switch) {
- ret = ops->post_bank_switch(bus);
- if (ret < 0) {
- dev_err(bus->dev,
- "Post bank switch op failed: %d", ret);
- goto error;
- }
- } else if (bus->multi_link && stream->m_rt_count > 1) {
- dev_err(bus->dev,
- "Post bank switch ops not implemented");
- goto error;
- }
- /* Set the bank switch timeout to default, if not set */
- if (!bus->bank_switch_timeout)
- bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT;
- /* Check if bank switch was successful */
- ret = sdw_ml_sync_bank_switch(bus);
- if (ret < 0) {
- dev_err(bus->dev,
- "multi link bank switch failed: %d", ret);
- goto error;
- }
- mutex_unlock(&bus->msg_lock);
- }
- return ret;
- error:
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- kfree(bus->defer_msg.msg->buf);
- kfree(bus->defer_msg.msg);
- }
- msg_unlock:
- if (multi_link) {
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- if (mutex_is_locked(&bus->msg_lock))
- mutex_unlock(&bus->msg_lock);
- }
- }
- return ret;
- }
- /**
- * sdw_release_stream() - Free the assigned stream runtime
- *
- * @stream: SoundWire stream runtime
- *
- * sdw_release_stream should be called only once per stream
- */
- void sdw_release_stream(struct sdw_stream_runtime *stream)
- {
- kfree(stream);
- }
- EXPORT_SYMBOL(sdw_release_stream);
- /**
- * sdw_alloc_stream() - Allocate and return stream runtime
- *
- * @stream_name: SoundWire stream name
- *
- * Allocates a SoundWire stream runtime instance.
- * sdw_alloc_stream should be called only once per stream. Typically
- * invoked from ALSA/ASoC machine/platform driver.
- */
- struct sdw_stream_runtime *sdw_alloc_stream(char *stream_name)
- {
- struct sdw_stream_runtime *stream;
- stream = kzalloc(sizeof(*stream), GFP_KERNEL);
- if (!stream)
- return NULL;
- stream->name = stream_name;
- INIT_LIST_HEAD(&stream->master_list);
- stream->state = SDW_STREAM_ALLOCATED;
- return stream;
- }
- EXPORT_SYMBOL(sdw_alloc_stream);
- static struct sdw_master_runtime
- *sdw_find_master_rt(struct sdw_bus *bus,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- /* Retrieve Bus handle if already available */
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- if (m_rt->bus == bus)
- return m_rt;
- }
- return NULL;
- }
- /**
- * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
- *
- * @bus: SDW bus instance
- * @stream_config: Stream configuration
- * @stream: Stream runtime handle.
- *
- * This function is to be called with bus_lock held.
- */
- static struct sdw_master_runtime
- *sdw_alloc_master_rt(struct sdw_bus *bus,
- struct sdw_stream_config *stream_config,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt;
- /*
- * check if Master is already allocated (as a result of Slave adding
- * it first), if so skip allocation and go to configure
- */
- m_rt = sdw_find_master_rt(bus, stream);
- if (m_rt)
- goto stream_config;
- m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
- if (!m_rt)
- return NULL;
- /* Initialization of Master runtime handle */
- INIT_LIST_HEAD(&m_rt->port_list);
- INIT_LIST_HEAD(&m_rt->slave_rt_list);
- list_add_tail(&m_rt->stream_node, &stream->master_list);
- list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
- stream_config:
- m_rt->ch_count = stream_config->ch_count;
- m_rt->bus = bus;
- m_rt->stream = stream;
- m_rt->direction = stream_config->direction;
- return m_rt;
- }
- /**
- * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
- *
- * @slave: Slave handle
- * @stream_config: Stream configuration
- * @stream: Stream runtime handle
- *
- * This function is to be called with bus_lock held.
- */
- static struct sdw_slave_runtime
- *sdw_alloc_slave_rt(struct sdw_slave *slave,
- struct sdw_stream_config *stream_config,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_slave_runtime *s_rt = NULL;
- s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
- if (!s_rt)
- return NULL;
- INIT_LIST_HEAD(&s_rt->port_list);
- s_rt->ch_count = stream_config->ch_count;
- s_rt->direction = stream_config->direction;
- s_rt->slave = slave;
- return s_rt;
- }
- static void sdw_master_port_release(struct sdw_bus *bus,
- struct sdw_master_runtime *m_rt)
- {
- struct sdw_port_runtime *p_rt, *_p_rt;
- list_for_each_entry_safe(p_rt, _p_rt,
- &m_rt->port_list, port_node) {
- list_del(&p_rt->port_node);
- kfree(p_rt);
- }
- }
- static void sdw_slave_port_release(struct sdw_bus *bus,
- struct sdw_slave *slave,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_port_runtime *p_rt, *_p_rt;
- struct sdw_master_runtime *m_rt;
- struct sdw_slave_runtime *s_rt;
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
- if (s_rt->slave != slave)
- continue;
- list_for_each_entry_safe(p_rt, _p_rt,
- &s_rt->port_list, port_node) {
- list_del(&p_rt->port_node);
- kfree(p_rt);
- }
- }
- }
- }
- /**
- * sdw_release_slave_stream() - Free Slave(s) runtime handle
- *
- * @slave: Slave handle.
- * @stream: Stream runtime handle.
- *
- * This function is to be called with bus_lock held.
- */
- static void sdw_release_slave_stream(struct sdw_slave *slave,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_slave_runtime *s_rt, *_s_rt;
- struct sdw_master_runtime *m_rt;
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- /* Retrieve Slave runtime handle */
- list_for_each_entry_safe(s_rt, _s_rt,
- &m_rt->slave_rt_list, m_rt_node) {
- if (s_rt->slave == slave) {
- list_del(&s_rt->m_rt_node);
- kfree(s_rt);
- return;
- }
- }
- }
- }
- /**
- * sdw_release_master_stream() - Free Master runtime handle
- *
- * @m_rt: Master runtime node
- * @stream: Stream runtime handle.
- *
- * This function is to be called with bus_lock held
- * It frees the Master runtime handle and associated Slave(s) runtime
- * handle. If this is called first then sdw_release_slave_stream() will have
- * no effect as Slave(s) runtime handle would already be freed up.
- */
- static void sdw_release_master_stream(struct sdw_master_runtime *m_rt,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_slave_runtime *s_rt, *_s_rt;
- list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
- sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream);
- sdw_release_slave_stream(s_rt->slave, stream);
- }
- list_del(&m_rt->stream_node);
- list_del(&m_rt->bus_node);
- kfree(m_rt);
- }
- /**
- * sdw_stream_remove_master() - Remove master from sdw_stream
- *
- * @bus: SDW Bus instance
- * @stream: SoundWire stream
- *
- * This removes and frees port_rt and master_rt from a stream
- */
- int sdw_stream_remove_master(struct sdw_bus *bus,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt, *_m_rt;
- mutex_lock(&bus->bus_lock);
- list_for_each_entry_safe(m_rt, _m_rt,
- &stream->master_list, stream_node) {
- if (m_rt->bus != bus)
- continue;
- sdw_master_port_release(bus, m_rt);
- sdw_release_master_stream(m_rt, stream);
- stream->m_rt_count--;
- }
- if (list_empty(&stream->master_list))
- stream->state = SDW_STREAM_RELEASED;
- mutex_unlock(&bus->bus_lock);
- return 0;
- }
- EXPORT_SYMBOL(sdw_stream_remove_master);
- /**
- * sdw_stream_remove_slave() - Remove slave from sdw_stream
- *
- * @slave: SDW Slave instance
- * @stream: SoundWire stream
- *
- * This removes and frees port_rt and slave_rt from a stream
- */
- int sdw_stream_remove_slave(struct sdw_slave *slave,
- struct sdw_stream_runtime *stream)
- {
- mutex_lock(&slave->bus->bus_lock);
- sdw_slave_port_release(slave->bus, slave, stream);
- sdw_release_slave_stream(slave, stream);
- mutex_unlock(&slave->bus->bus_lock);
- return 0;
- }
- EXPORT_SYMBOL(sdw_stream_remove_slave);
- /**
- * sdw_config_stream() - Configure the allocated stream
- *
- * @dev: SDW device
- * @stream: SoundWire stream
- * @stream_config: Stream configuration for audio stream
- * @is_slave: is API called from Slave or Master
- *
- * This function is to be called with bus_lock held.
- */
- static int sdw_config_stream(struct device *dev,
- struct sdw_stream_runtime *stream,
- struct sdw_stream_config *stream_config, bool is_slave)
- {
- /*
- * Update the stream rate, channel and bps based on data
- * source. For more than one data source (multilink),
- * match the rate, bps, stream type and increment number of channels.
- *
- * If rate/bps is zero, it means the values are not set, so skip
- * comparison and allow the value to be set and stored in stream
- */
- if (stream->params.rate &&
- stream->params.rate != stream_config->frame_rate) {
- dev_err(dev, "rate not matching, stream:%s", stream->name);
- return -EINVAL;
- }
- if (stream->params.bps &&
- stream->params.bps != stream_config->bps) {
- dev_err(dev, "bps not matching, stream:%s", stream->name);
- return -EINVAL;
- }
- stream->type = stream_config->type;
- stream->params.rate = stream_config->frame_rate;
- stream->params.bps = stream_config->bps;
- /* TODO: Update this check during Device-device support */
- if (is_slave)
- stream->params.ch_count += stream_config->ch_count;
- return 0;
- }
- static int sdw_is_valid_port_range(struct device *dev,
- struct sdw_port_runtime *p_rt)
- {
- if (!SDW_VALID_PORT_RANGE(p_rt->num)) {
- dev_err(dev,
- "SoundWire: Invalid port number :%d", p_rt->num);
- return -EINVAL;
- }
- return 0;
- }
- static struct sdw_port_runtime *sdw_port_alloc(struct device *dev,
- struct sdw_port_config *port_config,
- int port_index)
- {
- struct sdw_port_runtime *p_rt;
- p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
- if (!p_rt)
- return NULL;
- p_rt->ch_mask = port_config[port_index].ch_mask;
- p_rt->num = port_config[port_index].num;
- return p_rt;
- }
- static int sdw_master_port_config(struct sdw_bus *bus,
- struct sdw_master_runtime *m_rt,
- struct sdw_port_config *port_config,
- unsigned int num_ports)
- {
- struct sdw_port_runtime *p_rt;
- int i;
- /* Iterate for number of ports to perform initialization */
- for (i = 0; i < num_ports; i++) {
- p_rt = sdw_port_alloc(bus->dev, port_config, i);
- if (!p_rt)
- return -ENOMEM;
- /*
- * TODO: Check port capabilities for requested
- * configuration (audio mode support)
- */
- list_add_tail(&p_rt->port_node, &m_rt->port_list);
- }
- return 0;
- }
- static int sdw_slave_port_config(struct sdw_slave *slave,
- struct sdw_slave_runtime *s_rt,
- struct sdw_port_config *port_config,
- unsigned int num_config)
- {
- struct sdw_port_runtime *p_rt;
- int i, ret;
- /* Iterate for number of ports to perform initialization */
- for (i = 0; i < num_config; i++) {
- p_rt = sdw_port_alloc(&slave->dev, port_config, i);
- if (!p_rt)
- return -ENOMEM;
- /*
- * TODO: Check valid port range as defined by DisCo/
- * slave
- */
- ret = sdw_is_valid_port_range(&slave->dev, p_rt);
- if (ret < 0) {
- kfree(p_rt);
- return ret;
- }
- /*
- * TODO: Check port capabilities for requested
- * configuration (audio mode support)
- */
- list_add_tail(&p_rt->port_node, &s_rt->port_list);
- }
- return 0;
- }
- /**
- * sdw_stream_add_master() - Allocate and add master runtime to a stream
- *
- * @bus: SDW Bus instance
- * @stream_config: Stream configuration for audio stream
- * @port_config: Port configuration for audio stream
- * @num_ports: Number of ports
- * @stream: SoundWire stream
- */
- int sdw_stream_add_master(struct sdw_bus *bus,
- struct sdw_stream_config *stream_config,
- struct sdw_port_config *port_config,
- unsigned int num_ports,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- int ret;
- mutex_lock(&bus->bus_lock);
- /*
- * For multi link streams, add the second master only if
- * the bus supports it.
- * Check if bus->multi_link is set
- */
- if (!bus->multi_link && stream->m_rt_count > 0) {
- dev_err(bus->dev,
- "Multilink not supported, link %d", bus->link_id);
- ret = -EINVAL;
- goto unlock;
- }
- m_rt = sdw_alloc_master_rt(bus, stream_config, stream);
- if (!m_rt) {
- dev_err(bus->dev,
- "Master runtime config failed for stream:%s",
- stream->name);
- ret = -ENOMEM;
- goto unlock;
- }
- ret = sdw_config_stream(bus->dev, stream, stream_config, false);
- if (ret)
- goto stream_error;
- ret = sdw_master_port_config(bus, m_rt, port_config, num_ports);
- if (ret)
- goto stream_error;
- stream->m_rt_count++;
- goto unlock;
- stream_error:
- sdw_release_master_stream(m_rt, stream);
- unlock:
- mutex_unlock(&bus->bus_lock);
- return ret;
- }
- EXPORT_SYMBOL(sdw_stream_add_master);
- /**
- * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
- *
- * @slave: SDW Slave instance
- * @stream_config: Stream configuration for audio stream
- * @stream: SoundWire stream
- * @port_config: Port configuration for audio stream
- * @num_ports: Number of ports
- *
- * It is expected that Slave is added before adding Master
- * to the Stream.
- *
- */
- int sdw_stream_add_slave(struct sdw_slave *slave,
- struct sdw_stream_config *stream_config,
- struct sdw_port_config *port_config,
- unsigned int num_ports,
- struct sdw_stream_runtime *stream)
- {
- struct sdw_slave_runtime *s_rt;
- struct sdw_master_runtime *m_rt;
- int ret;
- mutex_lock(&slave->bus->bus_lock);
- /*
- * If this API is invoked by Slave first then m_rt is not valid.
- * So, allocate m_rt and add Slave to it.
- */
- m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream);
- if (!m_rt) {
- dev_err(&slave->dev,
- "alloc master runtime failed for stream:%s",
- stream->name);
- ret = -ENOMEM;
- goto error;
- }
- s_rt = sdw_alloc_slave_rt(slave, stream_config, stream);
- if (!s_rt) {
- dev_err(&slave->dev,
- "Slave runtime config failed for stream:%s",
- stream->name);
- ret = -ENOMEM;
- goto stream_error;
- }
- ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
- if (ret)
- goto stream_error;
- list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
- ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports);
- if (ret)
- goto stream_error;
- /*
- * Change stream state to CONFIGURED on first Slave add.
- * Bus is not aware of number of Slave(s) in a stream at this
- * point so cannot depend on all Slave(s) to be added in order to
- * change stream state to CONFIGURED.
- */
- stream->state = SDW_STREAM_CONFIGURED;
- goto error;
- stream_error:
- /*
- * we hit error so cleanup the stream, release all Slave(s) and
- * Master runtime
- */
- sdw_release_master_stream(m_rt, stream);
- error:
- mutex_unlock(&slave->bus->bus_lock);
- return ret;
- }
- EXPORT_SYMBOL(sdw_stream_add_slave);
- /**
- * sdw_get_slave_dpn_prop() - Get Slave port capabilities
- *
- * @slave: Slave handle
- * @direction: Data direction.
- * @port_num: Port number
- */
- struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
- enum sdw_data_direction direction,
- unsigned int port_num)
- {
- struct sdw_dpn_prop *dpn_prop;
- u8 num_ports;
- int i;
- if (direction == SDW_DATA_DIR_TX) {
- num_ports = hweight32(slave->prop.source_ports);
- dpn_prop = slave->prop.src_dpn_prop;
- } else {
- num_ports = hweight32(slave->prop.sink_ports);
- dpn_prop = slave->prop.sink_dpn_prop;
- }
- for (i = 0; i < num_ports; i++) {
- dpn_prop = &dpn_prop[i];
- if (dpn_prop->num == port_num)
- return &dpn_prop[i];
- }
- return NULL;
- }
- /**
- * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
- *
- * @stream: SoundWire stream
- *
- * Acquire bus_lock for each of the master runtime(m_rt) part of this
- * stream to reconfigure the bus.
- * NOTE: This function is called from SoundWire stream ops and is
- * expected that a global lock is held before acquiring bus_lock.
- */
- static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- struct sdw_bus *bus = NULL;
- /* Iterate for all Master(s) in Master list */
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- mutex_lock(&bus->bus_lock);
- }
- }
- /**
- * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
- *
- * @stream: SoundWire stream
- *
- * Release the previously held bus_lock after reconfiguring the bus.
- * NOTE: This function is called from SoundWire stream ops and is
- * expected that a global lock is held before releasing bus_lock.
- */
- static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- struct sdw_bus *bus = NULL;
- /* Iterate for all Master(s) in Master list */
- list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- mutex_unlock(&bus->bus_lock);
- }
- }
- static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- struct sdw_bus *bus = NULL;
- struct sdw_master_prop *prop = NULL;
- struct sdw_bus_params params;
- int ret;
- /* Prepare Master(s) and Slave(s) port(s) associated with stream */
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- prop = &bus->prop;
- memcpy(¶ms, &bus->params, sizeof(params));
- /* TODO: Support Asynchronous mode */
- if ((prop->max_freq % stream->params.rate) != 0) {
- dev_err(bus->dev, "Async mode not supported");
- return -EINVAL;
- }
- /* Increment cumulative bus bandwidth */
- /* TODO: Update this during Device-Device support */
- bus->params.bandwidth += m_rt->stream->params.rate *
- m_rt->ch_count * m_rt->stream->params.bps;
- /* Program params */
- ret = sdw_program_params(bus);
- if (ret < 0) {
- dev_err(bus->dev, "Program params failed: %d", ret);
- goto restore_params;
- }
- }
- ret = do_bank_switch(stream);
- if (ret < 0) {
- dev_err(bus->dev, "Bank switch failed: %d", ret);
- goto restore_params;
- }
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- /* Prepare port(s) on the new clock configuration */
- ret = sdw_prep_deprep_ports(m_rt, true);
- if (ret < 0) {
- dev_err(bus->dev, "Prepare port(s) failed ret = %d",
- ret);
- return ret;
- }
- }
- stream->state = SDW_STREAM_PREPARED;
- return ret;
- restore_params:
- memcpy(&bus->params, ¶ms, sizeof(params));
- return ret;
- }
- /**
- * sdw_prepare_stream() - Prepare SoundWire stream
- *
- * @stream: Soundwire stream
- *
- * Documentation/driver-api/soundwire/stream.rst explains this API in detail
- */
- int sdw_prepare_stream(struct sdw_stream_runtime *stream)
- {
- int ret = 0;
- if (!stream) {
- pr_err("SoundWire: Handle not found for stream");
- return -EINVAL;
- }
- sdw_acquire_bus_lock(stream);
- ret = _sdw_prepare_stream(stream);
- if (ret < 0)
- pr_err("Prepare for stream:%s failed: %d", stream->name, ret);
- sdw_release_bus_lock(stream);
- return ret;
- }
- EXPORT_SYMBOL(sdw_prepare_stream);
- static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- struct sdw_bus *bus = NULL;
- int ret;
- /* Enable Master(s) and Slave(s) port(s) associated with stream */
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- /* Program params */
- ret = sdw_program_params(bus);
- if (ret < 0) {
- dev_err(bus->dev, "Program params failed: %d", ret);
- return ret;
- }
- /* Enable port(s) */
- ret = sdw_enable_disable_ports(m_rt, true);
- if (ret < 0) {
- dev_err(bus->dev, "Enable port(s) failed ret: %d", ret);
- return ret;
- }
- }
- ret = do_bank_switch(stream);
- if (ret < 0) {
- dev_err(bus->dev, "Bank switch failed: %d", ret);
- return ret;
- }
- stream->state = SDW_STREAM_ENABLED;
- return 0;
- }
- /**
- * sdw_enable_stream() - Enable SoundWire stream
- *
- * @stream: Soundwire stream
- *
- * Documentation/driver-api/soundwire/stream.rst explains this API in detail
- */
- int sdw_enable_stream(struct sdw_stream_runtime *stream)
- {
- int ret = 0;
- if (!stream) {
- pr_err("SoundWire: Handle not found for stream");
- return -EINVAL;
- }
- sdw_acquire_bus_lock(stream);
- ret = _sdw_enable_stream(stream);
- if (ret < 0)
- pr_err("Enable for stream:%s failed: %d", stream->name, ret);
- sdw_release_bus_lock(stream);
- return ret;
- }
- EXPORT_SYMBOL(sdw_enable_stream);
- static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- struct sdw_bus *bus = NULL;
- int ret;
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- /* Disable port(s) */
- ret = sdw_enable_disable_ports(m_rt, false);
- if (ret < 0) {
- dev_err(bus->dev, "Disable port(s) failed: %d", ret);
- return ret;
- }
- }
- stream->state = SDW_STREAM_DISABLED;
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- /* Program params */
- ret = sdw_program_params(bus);
- if (ret < 0) {
- dev_err(bus->dev, "Program params failed: %d", ret);
- return ret;
- }
- }
- return do_bank_switch(stream);
- }
- /**
- * sdw_disable_stream() - Disable SoundWire stream
- *
- * @stream: Soundwire stream
- *
- * Documentation/driver-api/soundwire/stream.rst explains this API in detail
- */
- int sdw_disable_stream(struct sdw_stream_runtime *stream)
- {
- int ret = 0;
- if (!stream) {
- pr_err("SoundWire: Handle not found for stream");
- return -EINVAL;
- }
- sdw_acquire_bus_lock(stream);
- ret = _sdw_disable_stream(stream);
- if (ret < 0)
- pr_err("Disable for stream:%s failed: %d", stream->name, ret);
- sdw_release_bus_lock(stream);
- return ret;
- }
- EXPORT_SYMBOL(sdw_disable_stream);
- static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
- {
- struct sdw_master_runtime *m_rt = NULL;
- struct sdw_bus *bus = NULL;
- int ret = 0;
- list_for_each_entry(m_rt, &stream->master_list, stream_node) {
- bus = m_rt->bus;
- /* De-prepare port(s) */
- ret = sdw_prep_deprep_ports(m_rt, false);
- if (ret < 0) {
- dev_err(bus->dev, "De-prepare port(s) failed: %d", ret);
- return ret;
- }
- /* TODO: Update this during Device-Device support */
- bus->params.bandwidth -= m_rt->stream->params.rate *
- m_rt->ch_count * m_rt->stream->params.bps;
- /* Program params */
- ret = sdw_program_params(bus);
- if (ret < 0) {
- dev_err(bus->dev, "Program params failed: %d", ret);
- return ret;
- }
- }
- stream->state = SDW_STREAM_DEPREPARED;
- return do_bank_switch(stream);
- }
- /**
- * sdw_deprepare_stream() - Deprepare SoundWire stream
- *
- * @stream: Soundwire stream
- *
- * Documentation/driver-api/soundwire/stream.rst explains this API in detail
- */
- int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
- {
- int ret = 0;
- if (!stream) {
- pr_err("SoundWire: Handle not found for stream");
- return -EINVAL;
- }
- sdw_acquire_bus_lock(stream);
- ret = _sdw_deprepare_stream(stream);
- if (ret < 0)
- pr_err("De-prepare for stream:%d failed: %d", ret, ret);
- sdw_release_bus_lock(stream);
- return ret;
- }
- EXPORT_SYMBOL(sdw_deprepare_stream);
|