sst-atom-controls.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. * sst-atom-controls.c - Intel MID Platform driver DPCM ALSA controls for Mrfld
  3. *
  4. * Copyright (C) 2013-14 Intel Corp
  5. * Author: Omair Mohammed Abdullah <omair.m.abdullah@intel.com>
  6. * Vinod Koul <vinod.koul@intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * In the dpcm driver modelling when a particular FE/BE/Mixer/Pipe is active
  19. * we forward the settings and parameters, rest we keep the values in
  20. * driver and forward when DAPM enables them
  21. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/slab.h>
  25. #include <sound/soc.h>
  26. #include <sound/tlv.h>
  27. #include "sst-mfld-platform.h"
  28. #include "sst-atom-controls.h"
  29. static int sst_fill_byte_control(struct sst_data *drv,
  30. u8 ipc_msg, u8 block,
  31. u8 task_id, u8 pipe_id,
  32. u16 len, void *cmd_data)
  33. {
  34. struct snd_sst_bytes_v2 *byte_data = drv->byte_stream;
  35. byte_data->type = SST_CMD_BYTES_SET;
  36. byte_data->ipc_msg = ipc_msg;
  37. byte_data->block = block;
  38. byte_data->task_id = task_id;
  39. byte_data->pipe_id = pipe_id;
  40. if (len > SST_MAX_BIN_BYTES - sizeof(*byte_data)) {
  41. dev_err(&drv->pdev->dev, "command length too big (%u)", len);
  42. return -EINVAL;
  43. }
  44. byte_data->len = len;
  45. memcpy(byte_data->bytes, cmd_data, len);
  46. print_hex_dump_bytes("writing to lpe: ", DUMP_PREFIX_OFFSET,
  47. byte_data, len + sizeof(*byte_data));
  48. return 0;
  49. }
  50. static int sst_fill_and_send_cmd_unlocked(struct sst_data *drv,
  51. u8 ipc_msg, u8 block, u8 task_id, u8 pipe_id,
  52. void *cmd_data, u16 len)
  53. {
  54. int ret = 0;
  55. ret = sst_fill_byte_control(drv, ipc_msg,
  56. block, task_id, pipe_id, len, cmd_data);
  57. if (ret < 0)
  58. return ret;
  59. return sst->ops->send_byte_stream(sst->dev, drv->byte_stream);
  60. }
  61. /**
  62. * sst_fill_and_send_cmd - generate the IPC message and send it to the FW
  63. * @ipc_msg: type of IPC (CMD, SET_PARAMS, GET_PARAMS)
  64. * @cmd_data: the IPC payload
  65. */
  66. static int sst_fill_and_send_cmd(struct sst_data *drv,
  67. u8 ipc_msg, u8 block, u8 task_id, u8 pipe_id,
  68. void *cmd_data, u16 len)
  69. {
  70. int ret;
  71. mutex_lock(&drv->lock);
  72. ret = sst_fill_and_send_cmd_unlocked(drv, ipc_msg, block,
  73. task_id, pipe_id, cmd_data, len);
  74. mutex_unlock(&drv->lock);
  75. return ret;
  76. }
  77. /**
  78. * tx map value is a bitfield where each bit represents a FW channel
  79. *
  80. * 3 2 1 0 # 0 = codec0, 1 = codec1
  81. * RLRLRLRL # 3, 4 = reserved
  82. *
  83. * e.g. slot 0 rx map = 00001100b -> data from slot 0 goes into codec_in1 L,R
  84. */
  85. static u8 sst_ssp_tx_map[SST_MAX_TDM_SLOTS] = {
  86. 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, /* default rx map */
  87. };
  88. /**
  89. * rx map value is a bitfield where each bit represents a slot
  90. *
  91. * 76543210 # 0 = slot 0, 1 = slot 1
  92. *
  93. * e.g. codec1_0 tx map = 00000101b -> data from codec_out1_0 goes into slot 0, 2
  94. */
  95. static u8 sst_ssp_rx_map[SST_MAX_TDM_SLOTS] = {
  96. 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, /* default tx map */
  97. };
  98. /**
  99. * NOTE: this is invoked with lock held
  100. */
  101. static int sst_send_slot_map(struct sst_data *drv)
  102. {
  103. struct sst_param_sba_ssp_slot_map cmd;
  104. SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
  105. cmd.header.command_id = SBA_SET_SSP_SLOT_MAP;
  106. cmd.header.length = sizeof(struct sst_param_sba_ssp_slot_map)
  107. - sizeof(struct sst_dsp_header);
  108. cmd.param_id = SBA_SET_SSP_SLOT_MAP;
  109. cmd.param_len = sizeof(cmd.rx_slot_map) + sizeof(cmd.tx_slot_map)
  110. + sizeof(cmd.ssp_index);
  111. cmd.ssp_index = SSP_CODEC;
  112. memcpy(cmd.rx_slot_map, &sst_ssp_tx_map[0], sizeof(cmd.rx_slot_map));
  113. memcpy(cmd.tx_slot_map, &sst_ssp_rx_map[0], sizeof(cmd.tx_slot_map));
  114. return sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
  115. SST_FLAG_BLOCKED, SST_TASK_SBA, 0, &cmd,
  116. sizeof(cmd.header) + cmd.header.length);
  117. }
  118. int sst_slot_enum_info(struct snd_kcontrol *kcontrol,
  119. struct snd_ctl_elem_info *uinfo)
  120. {
  121. struct sst_enum *e = (struct sst_enum *)kcontrol->private_value;
  122. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  123. uinfo->count = 1;
  124. uinfo->value.enumerated.items = e->max;
  125. if (uinfo->value.enumerated.item > e->max - 1)
  126. uinfo->value.enumerated.item = e->max - 1;
  127. strcpy(uinfo->value.enumerated.name,
  128. e->texts[uinfo->value.enumerated.item]);
  129. return 0;
  130. }
  131. /**
  132. * sst_slot_get - get the status of the interleaver/deinterleaver control
  133. *
  134. * Searches the map where the control status is stored, and gets the
  135. * channel/slot which is currently set for this enumerated control. Since it is
  136. * an enumerated control, there is only one possible value.
  137. */
  138. static int sst_slot_get(struct snd_kcontrol *kcontrol,
  139. struct snd_ctl_elem_value *ucontrol)
  140. {
  141. struct sst_enum *e = (void *)kcontrol->private_value;
  142. struct snd_soc_component *c = snd_kcontrol_chip(kcontrol);
  143. struct sst_data *drv = snd_soc_component_get_drvdata(c);
  144. unsigned int ctl_no = e->reg;
  145. unsigned int is_tx = e->tx;
  146. unsigned int val, mux;
  147. u8 *map = is_tx ? sst_ssp_rx_map : sst_ssp_tx_map;
  148. mutex_lock(&drv->lock);
  149. val = 1 << ctl_no;
  150. /* search which slot/channel has this bit set - there should be only one */
  151. for (mux = e->max; mux > 0; mux--)
  152. if (map[mux - 1] & val)
  153. break;
  154. ucontrol->value.enumerated.item[0] = mux;
  155. mutex_unlock(&drv->lock);
  156. dev_dbg(c->dev, "%s - %s map = %#x\n",
  157. is_tx ? "tx channel" : "rx slot",
  158. e->texts[mux], mux ? map[mux - 1] : -1);
  159. return 0;
  160. }
  161. /* sst_check_and_send_slot_map - helper for checking power state and sending
  162. * slot map cmd
  163. *
  164. * called with lock held
  165. */
  166. static int sst_check_and_send_slot_map(struct sst_data *drv, struct snd_kcontrol *kcontrol)
  167. {
  168. struct sst_enum *e = (void *)kcontrol->private_value;
  169. int ret = 0;
  170. if (e->w && e->w->power)
  171. ret = sst_send_slot_map(drv);
  172. else
  173. dev_err(&drv->pdev->dev, "Slot control: %s doesn't have DAPM widget!!!\n",
  174. kcontrol->id.name);
  175. return ret;
  176. }
  177. /**
  178. * sst_slot_put - set the status of interleaver/deinterleaver control
  179. *
  180. * (de)interleaver controls are defined in opposite sense to be user-friendly
  181. *
  182. * Instead of the enum value being the value written to the register, it is the
  183. * register address; and the kcontrol number (register num) is the value written
  184. * to the register. This is so that there can be only one value for each
  185. * slot/channel since there is only one control for each slot/channel.
  186. *
  187. * This means that whenever an enum is set, we need to clear the bit
  188. * for that kcontrol_no for all the interleaver OR deinterleaver registers
  189. */
  190. static int sst_slot_put(struct snd_kcontrol *kcontrol,
  191. struct snd_ctl_elem_value *ucontrol)
  192. {
  193. struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
  194. struct sst_data *drv = snd_soc_component_get_drvdata(c);
  195. struct sst_enum *e = (void *)kcontrol->private_value;
  196. int i, ret = 0;
  197. unsigned int ctl_no = e->reg;
  198. unsigned int is_tx = e->tx;
  199. unsigned int slot_channel_no;
  200. unsigned int val, mux;
  201. u8 *map;
  202. map = is_tx ? sst_ssp_rx_map : sst_ssp_tx_map;
  203. val = 1 << ctl_no;
  204. mux = ucontrol->value.enumerated.item[0];
  205. if (mux > e->max - 1)
  206. return -EINVAL;
  207. mutex_lock(&drv->lock);
  208. /* first clear all registers of this bit */
  209. for (i = 0; i < e->max; i++)
  210. map[i] &= ~val;
  211. if (mux == 0) {
  212. /* kctl set to 'none' and we reset the bits so send IPC */
  213. ret = sst_check_and_send_slot_map(drv, kcontrol);
  214. mutex_unlock(&drv->lock);
  215. return ret;
  216. }
  217. /* offset by one to take "None" into account */
  218. slot_channel_no = mux - 1;
  219. map[slot_channel_no] |= val;
  220. dev_dbg(c->dev, "%s %s map = %#x\n",
  221. is_tx ? "tx channel" : "rx slot",
  222. e->texts[mux], map[slot_channel_no]);
  223. ret = sst_check_and_send_slot_map(drv, kcontrol);
  224. mutex_unlock(&drv->lock);
  225. return ret;
  226. }
  227. static int sst_send_algo_cmd(struct sst_data *drv,
  228. struct sst_algo_control *bc)
  229. {
  230. int len, ret = 0;
  231. struct sst_cmd_set_params *cmd;
  232. /*bc->max includes sizeof algos + length field*/
  233. len = sizeof(cmd->dst) + sizeof(cmd->command_id) + bc->max;
  234. cmd = kzalloc(len, GFP_KERNEL);
  235. if (cmd == NULL)
  236. return -ENOMEM;
  237. SST_FILL_DESTINATION(2, cmd->dst, bc->pipe_id, bc->module_id);
  238. cmd->command_id = bc->cmd_id;
  239. memcpy(cmd->params, bc->params, bc->max);
  240. ret = sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
  241. SST_FLAG_BLOCKED, bc->task_id, 0, cmd, len);
  242. kfree(cmd);
  243. return ret;
  244. }
  245. /**
  246. * sst_find_and_send_pipe_algo - send all the algo parameters for a pipe
  247. *
  248. * The algos which are in each pipeline are sent to the firmware one by one
  249. *
  250. * Called with lock held
  251. */
  252. static int sst_find_and_send_pipe_algo(struct sst_data *drv,
  253. const char *pipe, struct sst_ids *ids)
  254. {
  255. int ret = 0;
  256. struct sst_algo_control *bc;
  257. struct sst_module *algo = NULL;
  258. dev_dbg(&drv->pdev->dev, "Enter: widget=%s\n", pipe);
  259. list_for_each_entry(algo, &ids->algo_list, node) {
  260. bc = (void *)algo->kctl->private_value;
  261. dev_dbg(&drv->pdev->dev, "Found algo control name=%s pipe=%s\n",
  262. algo->kctl->id.name, pipe);
  263. ret = sst_send_algo_cmd(drv, bc);
  264. if (ret)
  265. return ret;
  266. }
  267. return ret;
  268. }
  269. static int sst_algo_bytes_ctl_info(struct snd_kcontrol *kcontrol,
  270. struct snd_ctl_elem_info *uinfo)
  271. {
  272. struct sst_algo_control *bc = (void *)kcontrol->private_value;
  273. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  274. uinfo->count = bc->max;
  275. return 0;
  276. }
  277. static int sst_algo_control_get(struct snd_kcontrol *kcontrol,
  278. struct snd_ctl_elem_value *ucontrol)
  279. {
  280. struct sst_algo_control *bc = (void *)kcontrol->private_value;
  281. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  282. switch (bc->type) {
  283. case SST_ALGO_PARAMS:
  284. memcpy(ucontrol->value.bytes.data, bc->params, bc->max);
  285. break;
  286. default:
  287. dev_err(component->dev, "Invalid Input- algo type:%d\n",
  288. bc->type);
  289. return -EINVAL;
  290. }
  291. return 0;
  292. }
  293. static int sst_algo_control_set(struct snd_kcontrol *kcontrol,
  294. struct snd_ctl_elem_value *ucontrol)
  295. {
  296. int ret = 0;
  297. struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
  298. struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
  299. struct sst_algo_control *bc = (void *)kcontrol->private_value;
  300. dev_dbg(cmpnt->dev, "control_name=%s\n", kcontrol->id.name);
  301. mutex_lock(&drv->lock);
  302. switch (bc->type) {
  303. case SST_ALGO_PARAMS:
  304. memcpy(bc->params, ucontrol->value.bytes.data, bc->max);
  305. break;
  306. default:
  307. mutex_unlock(&drv->lock);
  308. dev_err(cmpnt->dev, "Invalid Input- algo type:%d\n",
  309. bc->type);
  310. return -EINVAL;
  311. }
  312. /*if pipe is enabled, need to send the algo params from here*/
  313. if (bc->w && bc->w->power)
  314. ret = sst_send_algo_cmd(drv, bc);
  315. mutex_unlock(&drv->lock);
  316. return ret;
  317. }
  318. static int sst_gain_ctl_info(struct snd_kcontrol *kcontrol,
  319. struct snd_ctl_elem_info *uinfo)
  320. {
  321. struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
  322. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  323. uinfo->count = mc->stereo ? 2 : 1;
  324. uinfo->value.integer.min = mc->min;
  325. uinfo->value.integer.max = mc->max;
  326. return 0;
  327. }
  328. /**
  329. * sst_send_gain_cmd - send the gain algorithm IPC to the FW
  330. * @gv: the stored value of gain (also contains rampduration)
  331. * @mute: flag that indicates whether this was called from the
  332. * digital_mute callback or directly. If called from the
  333. * digital_mute callback, module will be muted/unmuted based on this
  334. * flag. The flag is always 0 if called directly.
  335. *
  336. * Called with sst_data.lock held
  337. *
  338. * The user-set gain value is sent only if the user-controllable 'mute' control
  339. * is OFF (indicated by gv->mute). Otherwise, the mute value (MIN value) is
  340. * sent.
  341. */
  342. static int sst_send_gain_cmd(struct sst_data *drv, struct sst_gain_value *gv,
  343. u16 task_id, u16 loc_id, u16 module_id, int mute)
  344. {
  345. struct sst_cmd_set_gain_dual cmd;
  346. dev_dbg(&drv->pdev->dev, "Enter\n");
  347. cmd.header.command_id = MMX_SET_GAIN;
  348. SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
  349. cmd.gain_cell_num = 1;
  350. if (mute || gv->mute) {
  351. cmd.cell_gains[0].cell_gain_left = SST_GAIN_MIN_VALUE;
  352. cmd.cell_gains[0].cell_gain_right = SST_GAIN_MIN_VALUE;
  353. } else {
  354. cmd.cell_gains[0].cell_gain_left = gv->l_gain;
  355. cmd.cell_gains[0].cell_gain_right = gv->r_gain;
  356. }
  357. SST_FILL_DESTINATION(2, cmd.cell_gains[0].dest,
  358. loc_id, module_id);
  359. cmd.cell_gains[0].gain_time_constant = gv->ramp_duration;
  360. cmd.header.length = sizeof(struct sst_cmd_set_gain_dual)
  361. - sizeof(struct sst_dsp_header);
  362. /* we are with lock held, so call the unlocked api to send */
  363. return sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
  364. SST_FLAG_BLOCKED, task_id, 0, &cmd,
  365. sizeof(cmd.header) + cmd.header.length);
  366. }
  367. static int sst_gain_get(struct snd_kcontrol *kcontrol,
  368. struct snd_ctl_elem_value *ucontrol)
  369. {
  370. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  371. struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
  372. struct sst_gain_value *gv = mc->gain_val;
  373. switch (mc->type) {
  374. case SST_GAIN_TLV:
  375. ucontrol->value.integer.value[0] = gv->l_gain;
  376. ucontrol->value.integer.value[1] = gv->r_gain;
  377. break;
  378. case SST_GAIN_MUTE:
  379. ucontrol->value.integer.value[0] = gv->mute ? 1 : 0;
  380. break;
  381. case SST_GAIN_RAMP_DURATION:
  382. ucontrol->value.integer.value[0] = gv->ramp_duration;
  383. break;
  384. default:
  385. dev_err(component->dev, "Invalid Input- gain type:%d\n",
  386. mc->type);
  387. return -EINVAL;
  388. }
  389. return 0;
  390. }
  391. static int sst_gain_put(struct snd_kcontrol *kcontrol,
  392. struct snd_ctl_elem_value *ucontrol)
  393. {
  394. int ret = 0;
  395. struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
  396. struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
  397. struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
  398. struct sst_gain_value *gv = mc->gain_val;
  399. mutex_lock(&drv->lock);
  400. switch (mc->type) {
  401. case SST_GAIN_TLV:
  402. gv->l_gain = ucontrol->value.integer.value[0];
  403. gv->r_gain = ucontrol->value.integer.value[1];
  404. dev_dbg(cmpnt->dev, "%s: Volume %d, %d\n",
  405. mc->pname, gv->l_gain, gv->r_gain);
  406. break;
  407. case SST_GAIN_MUTE:
  408. gv->mute = !!ucontrol->value.integer.value[0];
  409. dev_dbg(cmpnt->dev, "%s: Mute %d\n", mc->pname, gv->mute);
  410. break;
  411. case SST_GAIN_RAMP_DURATION:
  412. gv->ramp_duration = ucontrol->value.integer.value[0];
  413. dev_dbg(cmpnt->dev, "%s: Ramp Delay%d\n",
  414. mc->pname, gv->ramp_duration);
  415. break;
  416. default:
  417. mutex_unlock(&drv->lock);
  418. dev_err(cmpnt->dev, "Invalid Input- gain type:%d\n",
  419. mc->type);
  420. return -EINVAL;
  421. }
  422. if (mc->w && mc->w->power)
  423. ret = sst_send_gain_cmd(drv, gv, mc->task_id,
  424. mc->pipe_id | mc->instance_id, mc->module_id, 0);
  425. mutex_unlock(&drv->lock);
  426. return ret;
  427. }
  428. static int sst_set_pipe_gain(struct sst_ids *ids,
  429. struct sst_data *drv, int mute);
  430. static int sst_send_pipe_module_params(struct snd_soc_dapm_widget *w,
  431. struct snd_kcontrol *kcontrol)
  432. {
  433. struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
  434. struct sst_data *drv = snd_soc_component_get_drvdata(c);
  435. struct sst_ids *ids = w->priv;
  436. mutex_lock(&drv->lock);
  437. sst_find_and_send_pipe_algo(drv, w->name, ids);
  438. sst_set_pipe_gain(ids, drv, 0);
  439. mutex_unlock(&drv->lock);
  440. return 0;
  441. }
  442. static int sst_generic_modules_event(struct snd_soc_dapm_widget *w,
  443. struct snd_kcontrol *k, int event)
  444. {
  445. if (SND_SOC_DAPM_EVENT_ON(event))
  446. return sst_send_pipe_module_params(w, k);
  447. return 0;
  448. }
  449. static const DECLARE_TLV_DB_SCALE(sst_gain_tlv_common, SST_GAIN_MIN_VALUE * 10, 10, 0);
  450. /* Look up table to convert MIXER SW bit regs to SWM inputs */
  451. static const uint swm_mixer_input_ids[SST_SWM_INPUT_COUNT] = {
  452. [SST_IP_CODEC0] = SST_SWM_IN_CODEC0,
  453. [SST_IP_CODEC1] = SST_SWM_IN_CODEC1,
  454. [SST_IP_LOOP0] = SST_SWM_IN_SPROT_LOOP,
  455. [SST_IP_LOOP1] = SST_SWM_IN_MEDIA_LOOP1,
  456. [SST_IP_LOOP2] = SST_SWM_IN_MEDIA_LOOP2,
  457. [SST_IP_PCM0] = SST_SWM_IN_PCM0,
  458. [SST_IP_PCM1] = SST_SWM_IN_PCM1,
  459. [SST_IP_MEDIA0] = SST_SWM_IN_MEDIA0,
  460. [SST_IP_MEDIA1] = SST_SWM_IN_MEDIA1,
  461. [SST_IP_MEDIA2] = SST_SWM_IN_MEDIA2,
  462. [SST_IP_MEDIA3] = SST_SWM_IN_MEDIA3,
  463. };
  464. /**
  465. * fill_swm_input - fill in the SWM input ids given the register
  466. *
  467. * The register value is a bit-field inicated which mixer inputs are ON. Use the
  468. * lookup table to get the input-id and fill it in the structure.
  469. */
  470. static int fill_swm_input(struct snd_soc_component *cmpnt,
  471. struct swm_input_ids *swm_input, unsigned int reg)
  472. {
  473. uint i, is_set, nb_inputs = 0;
  474. u16 input_loc_id;
  475. dev_dbg(cmpnt->dev, "reg: %#x\n", reg);
  476. for (i = 0; i < SST_SWM_INPUT_COUNT; i++) {
  477. is_set = reg & BIT(i);
  478. if (!is_set)
  479. continue;
  480. input_loc_id = swm_mixer_input_ids[i];
  481. SST_FILL_DESTINATION(2, swm_input->input_id,
  482. input_loc_id, SST_DEFAULT_MODULE_ID);
  483. nb_inputs++;
  484. swm_input++;
  485. dev_dbg(cmpnt->dev, "input id: %#x, nb_inputs: %d\n",
  486. input_loc_id, nb_inputs);
  487. if (nb_inputs == SST_CMD_SWM_MAX_INPUTS) {
  488. dev_warn(cmpnt->dev, "SET_SWM cmd max inputs reached");
  489. break;
  490. }
  491. }
  492. return nb_inputs;
  493. }
  494. /**
  495. * called with lock held
  496. */
  497. static int sst_set_pipe_gain(struct sst_ids *ids,
  498. struct sst_data *drv, int mute)
  499. {
  500. int ret = 0;
  501. struct sst_gain_mixer_control *mc;
  502. struct sst_gain_value *gv;
  503. struct sst_module *gain = NULL;
  504. list_for_each_entry(gain, &ids->gain_list, node) {
  505. struct snd_kcontrol *kctl = gain->kctl;
  506. dev_dbg(&drv->pdev->dev, "control name=%s\n", kctl->id.name);
  507. mc = (void *)kctl->private_value;
  508. gv = mc->gain_val;
  509. ret = sst_send_gain_cmd(drv, gv, mc->task_id,
  510. mc->pipe_id | mc->instance_id, mc->module_id, mute);
  511. if (ret)
  512. return ret;
  513. }
  514. return ret;
  515. }
  516. static int sst_swm_mixer_event(struct snd_soc_dapm_widget *w,
  517. struct snd_kcontrol *k, int event)
  518. {
  519. struct sst_cmd_set_swm cmd;
  520. struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
  521. struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
  522. struct sst_ids *ids = w->priv;
  523. bool set_mixer = false;
  524. struct soc_mixer_control *mc;
  525. int val = 0;
  526. int i = 0;
  527. dev_dbg(cmpnt->dev, "widget = %s\n", w->name);
  528. /*
  529. * Identify which mixer input is on and send the bitmap of the
  530. * inputs as an IPC to the DSP.
  531. */
  532. for (i = 0; i < w->num_kcontrols; i++) {
  533. if (dapm_kcontrol_get_value(w->kcontrols[i])) {
  534. mc = (struct soc_mixer_control *)(w->kcontrols[i])->private_value;
  535. val |= 1 << mc->shift;
  536. }
  537. }
  538. dev_dbg(cmpnt->dev, "val = %#x\n", val);
  539. switch (event) {
  540. case SND_SOC_DAPM_PRE_PMU:
  541. case SND_SOC_DAPM_POST_PMD:
  542. set_mixer = true;
  543. break;
  544. case SND_SOC_DAPM_POST_REG:
  545. if (w->power)
  546. set_mixer = true;
  547. break;
  548. default:
  549. set_mixer = false;
  550. }
  551. if (set_mixer == false)
  552. return 0;
  553. if (SND_SOC_DAPM_EVENT_ON(event) ||
  554. event == SND_SOC_DAPM_POST_REG)
  555. cmd.switch_state = SST_SWM_ON;
  556. else
  557. cmd.switch_state = SST_SWM_OFF;
  558. SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
  559. /* MMX_SET_SWM == SBA_SET_SWM */
  560. cmd.header.command_id = SBA_SET_SWM;
  561. SST_FILL_DESTINATION(2, cmd.output_id,
  562. ids->location_id, SST_DEFAULT_MODULE_ID);
  563. cmd.nb_inputs = fill_swm_input(cmpnt, &cmd.input[0], val);
  564. cmd.header.length = offsetof(struct sst_cmd_set_swm, input)
  565. - sizeof(struct sst_dsp_header)
  566. + (cmd.nb_inputs * sizeof(cmd.input[0]));
  567. return sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
  568. ids->task_id, 0, &cmd,
  569. sizeof(cmd.header) + cmd.header.length);
  570. }
  571. /* SBA mixers - 16 inputs */
  572. #define SST_SBA_DECLARE_MIX_CONTROLS(kctl_name) \
  573. static const struct snd_kcontrol_new kctl_name[] = { \
  574. SOC_DAPM_SINGLE("codec_in0 Switch", SND_SOC_NOPM, SST_IP_CODEC0, 1, 0), \
  575. SOC_DAPM_SINGLE("codec_in1 Switch", SND_SOC_NOPM, SST_IP_CODEC1, 1, 0), \
  576. SOC_DAPM_SINGLE("sprot_loop_in Switch", SND_SOC_NOPM, SST_IP_LOOP0, 1, 0), \
  577. SOC_DAPM_SINGLE("media_loop1_in Switch", SND_SOC_NOPM, SST_IP_LOOP1, 1, 0), \
  578. SOC_DAPM_SINGLE("media_loop2_in Switch", SND_SOC_NOPM, SST_IP_LOOP2, 1, 0), \
  579. SOC_DAPM_SINGLE("pcm0_in Switch", SND_SOC_NOPM, SST_IP_PCM0, 1, 0), \
  580. SOC_DAPM_SINGLE("pcm1_in Switch", SND_SOC_NOPM, SST_IP_PCM1, 1, 0), \
  581. }
  582. #define SST_SBA_MIXER_GRAPH_MAP(mix_name) \
  583. { mix_name, "codec_in0 Switch", "codec_in0" }, \
  584. { mix_name, "codec_in1 Switch", "codec_in1" }, \
  585. { mix_name, "sprot_loop_in Switch", "sprot_loop_in" }, \
  586. { mix_name, "media_loop1_in Switch", "media_loop1_in" }, \
  587. { mix_name, "media_loop2_in Switch", "media_loop2_in" }, \
  588. { mix_name, "pcm0_in Switch", "pcm0_in" }, \
  589. { mix_name, "pcm1_in Switch", "pcm1_in" }
  590. #define SST_MMX_DECLARE_MIX_CONTROLS(kctl_name) \
  591. static const struct snd_kcontrol_new kctl_name[] = { \
  592. SOC_DAPM_SINGLE("media0_in Switch", SND_SOC_NOPM, SST_IP_MEDIA0, 1, 0), \
  593. SOC_DAPM_SINGLE("media1_in Switch", SND_SOC_NOPM, SST_IP_MEDIA1, 1, 0), \
  594. SOC_DAPM_SINGLE("media2_in Switch", SND_SOC_NOPM, SST_IP_MEDIA2, 1, 0), \
  595. SOC_DAPM_SINGLE("media3_in Switch", SND_SOC_NOPM, SST_IP_MEDIA3, 1, 0), \
  596. }
  597. SST_MMX_DECLARE_MIX_CONTROLS(sst_mix_media0_controls);
  598. SST_MMX_DECLARE_MIX_CONTROLS(sst_mix_media1_controls);
  599. /* 18 SBA mixers */
  600. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm0_controls);
  601. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm1_controls);
  602. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm2_controls);
  603. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_sprot_l0_controls);
  604. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_media_l1_controls);
  605. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_media_l2_controls);
  606. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_voip_controls);
  607. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_codec0_controls);
  608. SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_codec1_controls);
  609. /*
  610. * sst_handle_vb_timer - Start/Stop the DSP scheduler
  611. *
  612. * The DSP expects first cmd to be SBA_VB_START, so at first startup send
  613. * that.
  614. * DSP expects last cmd to be SBA_VB_IDLE, so at last shutdown send that.
  615. *
  616. * Do refcount internally so that we send command only at first start
  617. * and last end. Since SST driver does its own ref count, invoke sst's
  618. * power ops always!
  619. */
  620. int sst_handle_vb_timer(struct snd_soc_dai *dai, bool enable)
  621. {
  622. int ret = 0;
  623. struct sst_cmd_generic cmd;
  624. struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
  625. static int timer_usage;
  626. if (enable)
  627. cmd.header.command_id = SBA_VB_START;
  628. else
  629. cmd.header.command_id = SBA_IDLE;
  630. dev_dbg(dai->dev, "enable=%u, usage=%d\n", enable, timer_usage);
  631. SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
  632. cmd.header.length = 0;
  633. if (enable) {
  634. ret = sst->ops->power(sst->dev, true);
  635. if (ret < 0)
  636. return ret;
  637. }
  638. mutex_lock(&drv->lock);
  639. if (enable)
  640. timer_usage++;
  641. else
  642. timer_usage--;
  643. /*
  644. * Send the command only if this call is the first enable or last
  645. * disable
  646. */
  647. if ((enable && (timer_usage == 1)) ||
  648. (!enable && (timer_usage == 0))) {
  649. ret = sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_CMD,
  650. SST_FLAG_BLOCKED, SST_TASK_SBA, 0, &cmd,
  651. sizeof(cmd.header) + cmd.header.length);
  652. if (ret && enable) {
  653. timer_usage--;
  654. enable = false;
  655. }
  656. }
  657. mutex_unlock(&drv->lock);
  658. if (!enable)
  659. sst->ops->power(sst->dev, false);
  660. return ret;
  661. }
  662. /**
  663. * sst_ssp_config - contains SSP configuration for media UC
  664. */
  665. static const struct sst_ssp_config sst_ssp_configs = {
  666. .ssp_id = SSP_CODEC,
  667. .bits_per_slot = 24,
  668. .slots = 4,
  669. .ssp_mode = SSP_MODE_MASTER,
  670. .pcm_mode = SSP_PCM_MODE_NETWORK,
  671. .duplex = SSP_DUPLEX,
  672. .ssp_protocol = SSP_MODE_PCM,
  673. .fs_width = 1,
  674. .fs_frequency = SSP_FS_48_KHZ,
  675. .active_slot_map = 0xF,
  676. .start_delay = 0,
  677. };
  678. int send_ssp_cmd(struct snd_soc_dai *dai, const char *id, bool enable)
  679. {
  680. struct sst_cmd_sba_hw_set_ssp cmd;
  681. struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
  682. const struct sst_ssp_config *config;
  683. dev_info(dai->dev, "Enter: enable=%d port_name=%s\n", enable, id);
  684. SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
  685. cmd.header.command_id = SBA_HW_SET_SSP;
  686. cmd.header.length = sizeof(struct sst_cmd_sba_hw_set_ssp)
  687. - sizeof(struct sst_dsp_header);
  688. config = &sst_ssp_configs;
  689. dev_dbg(dai->dev, "ssp_id: %u\n", config->ssp_id);
  690. if (enable)
  691. cmd.switch_state = SST_SWITCH_ON;
  692. else
  693. cmd.switch_state = SST_SWITCH_OFF;
  694. cmd.selection = config->ssp_id;
  695. cmd.nb_bits_per_slots = config->bits_per_slot;
  696. cmd.nb_slots = config->slots;
  697. cmd.mode = config->ssp_mode | (config->pcm_mode << 1);
  698. cmd.duplex = config->duplex;
  699. cmd.active_tx_slot_map = config->active_slot_map;
  700. cmd.active_rx_slot_map = config->active_slot_map;
  701. cmd.frame_sync_frequency = config->fs_frequency;
  702. cmd.frame_sync_polarity = SSP_FS_ACTIVE_HIGH;
  703. cmd.data_polarity = 1;
  704. cmd.frame_sync_width = config->fs_width;
  705. cmd.ssp_protocol = config->ssp_protocol;
  706. cmd.start_delay = config->start_delay;
  707. cmd.reserved1 = cmd.reserved2 = 0xFF;
  708. return sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
  709. SST_TASK_SBA, 0, &cmd,
  710. sizeof(cmd.header) + cmd.header.length);
  711. }
  712. static int sst_set_be_modules(struct snd_soc_dapm_widget *w,
  713. struct snd_kcontrol *k, int event)
  714. {
  715. int ret = 0;
  716. struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
  717. struct sst_data *drv = snd_soc_component_get_drvdata(c);
  718. dev_dbg(c->dev, "Enter: widget=%s\n", w->name);
  719. if (SND_SOC_DAPM_EVENT_ON(event)) {
  720. ret = sst_send_slot_map(drv);
  721. if (ret)
  722. return ret;
  723. ret = sst_send_pipe_module_params(w, k);
  724. }
  725. return ret;
  726. }
  727. static int sst_set_media_path(struct snd_soc_dapm_widget *w,
  728. struct snd_kcontrol *k, int event)
  729. {
  730. int ret = 0;
  731. struct sst_cmd_set_media_path cmd;
  732. struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
  733. struct sst_data *drv = snd_soc_component_get_drvdata(c);
  734. struct sst_ids *ids = w->priv;
  735. dev_dbg(c->dev, "widget=%s\n", w->name);
  736. dev_dbg(c->dev, "task=%u, location=%#x\n",
  737. ids->task_id, ids->location_id);
  738. if (SND_SOC_DAPM_EVENT_ON(event))
  739. cmd.switch_state = SST_PATH_ON;
  740. else
  741. cmd.switch_state = SST_PATH_OFF;
  742. SST_FILL_DESTINATION(2, cmd.header.dst,
  743. ids->location_id, SST_DEFAULT_MODULE_ID);
  744. /* MMX_SET_MEDIA_PATH == SBA_SET_MEDIA_PATH */
  745. cmd.header.command_id = MMX_SET_MEDIA_PATH;
  746. cmd.header.length = sizeof(struct sst_cmd_set_media_path)
  747. - sizeof(struct sst_dsp_header);
  748. ret = sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
  749. ids->task_id, 0, &cmd,
  750. sizeof(cmd.header) + cmd.header.length);
  751. if (ret)
  752. return ret;
  753. if (SND_SOC_DAPM_EVENT_ON(event))
  754. ret = sst_send_pipe_module_params(w, k);
  755. return ret;
  756. }
  757. static int sst_set_media_loop(struct snd_soc_dapm_widget *w,
  758. struct snd_kcontrol *k, int event)
  759. {
  760. int ret = 0;
  761. struct sst_cmd_sba_set_media_loop_map cmd;
  762. struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
  763. struct sst_data *drv = snd_soc_component_get_drvdata(c);
  764. struct sst_ids *ids = w->priv;
  765. dev_dbg(c->dev, "Enter:widget=%s\n", w->name);
  766. if (SND_SOC_DAPM_EVENT_ON(event))
  767. cmd.switch_state = SST_SWITCH_ON;
  768. else
  769. cmd.switch_state = SST_SWITCH_OFF;
  770. SST_FILL_DESTINATION(2, cmd.header.dst,
  771. ids->location_id, SST_DEFAULT_MODULE_ID);
  772. cmd.header.command_id = SBA_SET_MEDIA_LOOP_MAP;
  773. cmd.header.length = sizeof(struct sst_cmd_sba_set_media_loop_map)
  774. - sizeof(struct sst_dsp_header);
  775. cmd.param.part.cfg.rate = 2; /* 48khz */
  776. cmd.param.part.cfg.format = ids->format; /* stereo/Mono */
  777. cmd.param.part.cfg.s_length = 1; /* 24bit left justified */
  778. cmd.map = 0; /* Algo sequence: Gain - DRP - FIR - IIR */
  779. ret = sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
  780. SST_TASK_SBA, 0, &cmd,
  781. sizeof(cmd.header) + cmd.header.length);
  782. if (ret)
  783. return ret;
  784. if (SND_SOC_DAPM_EVENT_ON(event))
  785. ret = sst_send_pipe_module_params(w, k);
  786. return ret;
  787. }
  788. static const struct snd_soc_dapm_widget sst_dapm_widgets[] = {
  789. SST_AIF_IN("codec_in0", sst_set_be_modules),
  790. SST_AIF_IN("codec_in1", sst_set_be_modules),
  791. SST_AIF_OUT("codec_out0", sst_set_be_modules),
  792. SST_AIF_OUT("codec_out1", sst_set_be_modules),
  793. /* Media Paths */
  794. /* MediaX IN paths are set via ALLOC, so no SET_MEDIA_PATH command */
  795. SST_PATH_INPUT("media0_in", SST_TASK_MMX, SST_SWM_IN_MEDIA0, sst_generic_modules_event),
  796. SST_PATH_INPUT("media1_in", SST_TASK_MMX, SST_SWM_IN_MEDIA1, NULL),
  797. SST_PATH_INPUT("media2_in", SST_TASK_MMX, SST_SWM_IN_MEDIA2, sst_set_media_path),
  798. SST_PATH_INPUT("media3_in", SST_TASK_MMX, SST_SWM_IN_MEDIA3, NULL),
  799. SST_PATH_OUTPUT("media0_out", SST_TASK_MMX, SST_SWM_OUT_MEDIA0, sst_set_media_path),
  800. SST_PATH_OUTPUT("media1_out", SST_TASK_MMX, SST_SWM_OUT_MEDIA1, sst_set_media_path),
  801. /* SBA PCM Paths */
  802. SST_PATH_INPUT("pcm0_in", SST_TASK_SBA, SST_SWM_IN_PCM0, sst_set_media_path),
  803. SST_PATH_INPUT("pcm1_in", SST_TASK_SBA, SST_SWM_IN_PCM1, sst_set_media_path),
  804. SST_PATH_OUTPUT("pcm0_out", SST_TASK_SBA, SST_SWM_OUT_PCM0, sst_set_media_path),
  805. SST_PATH_OUTPUT("pcm1_out", SST_TASK_SBA, SST_SWM_OUT_PCM1, sst_set_media_path),
  806. SST_PATH_OUTPUT("pcm2_out", SST_TASK_SBA, SST_SWM_OUT_PCM2, sst_set_media_path),
  807. /* SBA Loops */
  808. SST_PATH_INPUT("sprot_loop_in", SST_TASK_SBA, SST_SWM_IN_SPROT_LOOP, NULL),
  809. SST_PATH_INPUT("media_loop1_in", SST_TASK_SBA, SST_SWM_IN_MEDIA_LOOP1, NULL),
  810. SST_PATH_INPUT("media_loop2_in", SST_TASK_SBA, SST_SWM_IN_MEDIA_LOOP2, NULL),
  811. SST_PATH_MEDIA_LOOP_OUTPUT("sprot_loop_out", SST_TASK_SBA, SST_SWM_OUT_SPROT_LOOP, SST_FMT_MONO, sst_set_media_loop),
  812. SST_PATH_MEDIA_LOOP_OUTPUT("media_loop1_out", SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP1, SST_FMT_MONO, sst_set_media_loop),
  813. SST_PATH_MEDIA_LOOP_OUTPUT("media_loop2_out", SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP2, SST_FMT_STEREO, sst_set_media_loop),
  814. /* Media Mixers */
  815. SST_SWM_MIXER("media0_out mix 0", SND_SOC_NOPM, SST_TASK_MMX, SST_SWM_OUT_MEDIA0,
  816. sst_mix_media0_controls, sst_swm_mixer_event),
  817. SST_SWM_MIXER("media1_out mix 0", SND_SOC_NOPM, SST_TASK_MMX, SST_SWM_OUT_MEDIA1,
  818. sst_mix_media1_controls, sst_swm_mixer_event),
  819. /* SBA PCM mixers */
  820. SST_SWM_MIXER("pcm0_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM0,
  821. sst_mix_pcm0_controls, sst_swm_mixer_event),
  822. SST_SWM_MIXER("pcm1_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM1,
  823. sst_mix_pcm1_controls, sst_swm_mixer_event),
  824. SST_SWM_MIXER("pcm2_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM2,
  825. sst_mix_pcm2_controls, sst_swm_mixer_event),
  826. /* SBA Loop mixers */
  827. SST_SWM_MIXER("sprot_loop_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_SPROT_LOOP,
  828. sst_mix_sprot_l0_controls, sst_swm_mixer_event),
  829. SST_SWM_MIXER("media_loop1_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP1,
  830. sst_mix_media_l1_controls, sst_swm_mixer_event),
  831. SST_SWM_MIXER("media_loop2_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP2,
  832. sst_mix_media_l2_controls, sst_swm_mixer_event),
  833. /* SBA Backend mixers */
  834. SST_SWM_MIXER("codec_out0 mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_CODEC0,
  835. sst_mix_codec0_controls, sst_swm_mixer_event),
  836. SST_SWM_MIXER("codec_out1 mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_CODEC1,
  837. sst_mix_codec1_controls, sst_swm_mixer_event),
  838. };
  839. static const struct snd_soc_dapm_route intercon[] = {
  840. {"media0_in", NULL, "Compress Playback"},
  841. {"media1_in", NULL, "Headset Playback"},
  842. {"media2_in", NULL, "pcm0_out"},
  843. {"media0_out mix 0", "media0_in Switch", "media0_in"},
  844. {"media0_out mix 0", "media1_in Switch", "media1_in"},
  845. {"media0_out mix 0", "media2_in Switch", "media2_in"},
  846. {"media0_out mix 0", "media3_in Switch", "media3_in"},
  847. {"media1_out mix 0", "media0_in Switch", "media0_in"},
  848. {"media1_out mix 0", "media1_in Switch", "media1_in"},
  849. {"media1_out mix 0", "media2_in Switch", "media2_in"},
  850. {"media1_out mix 0", "media3_in Switch", "media3_in"},
  851. {"media0_out", NULL, "media0_out mix 0"},
  852. {"media1_out", NULL, "media1_out mix 0"},
  853. {"pcm0_in", NULL, "media0_out"},
  854. {"pcm1_in", NULL, "media1_out"},
  855. {"Headset Capture", NULL, "pcm1_out"},
  856. {"Headset Capture", NULL, "pcm2_out"},
  857. {"pcm0_out", NULL, "pcm0_out mix 0"},
  858. SST_SBA_MIXER_GRAPH_MAP("pcm0_out mix 0"),
  859. {"pcm1_out", NULL, "pcm1_out mix 0"},
  860. SST_SBA_MIXER_GRAPH_MAP("pcm1_out mix 0"),
  861. {"pcm2_out", NULL, "pcm2_out mix 0"},
  862. SST_SBA_MIXER_GRAPH_MAP("pcm2_out mix 0"),
  863. {"media_loop1_in", NULL, "media_loop1_out"},
  864. {"media_loop1_out", NULL, "media_loop1_out mix 0"},
  865. SST_SBA_MIXER_GRAPH_MAP("media_loop1_out mix 0"),
  866. {"media_loop2_in", NULL, "media_loop2_out"},
  867. {"media_loop2_out", NULL, "media_loop2_out mix 0"},
  868. SST_SBA_MIXER_GRAPH_MAP("media_loop2_out mix 0"),
  869. {"sprot_loop_in", NULL, "sprot_loop_out"},
  870. {"sprot_loop_out", NULL, "sprot_loop_out mix 0"},
  871. SST_SBA_MIXER_GRAPH_MAP("sprot_loop_out mix 0"),
  872. {"codec_out0", NULL, "codec_out0 mix 0"},
  873. SST_SBA_MIXER_GRAPH_MAP("codec_out0 mix 0"),
  874. {"codec_out1", NULL, "codec_out1 mix 0"},
  875. SST_SBA_MIXER_GRAPH_MAP("codec_out1 mix 0"),
  876. };
  877. static const char * const slot_names[] = {
  878. "none",
  879. "slot 0", "slot 1", "slot 2", "slot 3",
  880. "slot 4", "slot 5", "slot 6", "slot 7", /* not supported by FW */
  881. };
  882. static const char * const channel_names[] = {
  883. "none",
  884. "codec_out0_0", "codec_out0_1", "codec_out1_0", "codec_out1_1",
  885. "codec_out2_0", "codec_out2_1", "codec_out3_0", "codec_out3_1", /* not supported by FW */
  886. };
  887. #define SST_INTERLEAVER(xpname, slot_name, slotno) \
  888. SST_SSP_SLOT_CTL(xpname, "tx interleaver", slot_name, slotno, true, \
  889. channel_names, sst_slot_get, sst_slot_put)
  890. #define SST_DEINTERLEAVER(xpname, channel_name, channel_no) \
  891. SST_SSP_SLOT_CTL(xpname, "rx deinterleaver", channel_name, channel_no, false, \
  892. slot_names, sst_slot_get, sst_slot_put)
  893. static const struct snd_kcontrol_new sst_slot_controls[] = {
  894. SST_INTERLEAVER("codec_out", "slot 0", 0),
  895. SST_INTERLEAVER("codec_out", "slot 1", 1),
  896. SST_INTERLEAVER("codec_out", "slot 2", 2),
  897. SST_INTERLEAVER("codec_out", "slot 3", 3),
  898. SST_DEINTERLEAVER("codec_in", "codec_in0_0", 0),
  899. SST_DEINTERLEAVER("codec_in", "codec_in0_1", 1),
  900. SST_DEINTERLEAVER("codec_in", "codec_in1_0", 2),
  901. SST_DEINTERLEAVER("codec_in", "codec_in1_1", 3),
  902. };
  903. /* Gain helper with min/max set */
  904. #define SST_GAIN(name, path_id, task_id, instance, gain_var) \
  905. SST_GAIN_KCONTROLS(name, "Gain", SST_GAIN_MIN_VALUE, SST_GAIN_MAX_VALUE, \
  906. SST_GAIN_TC_MIN, SST_GAIN_TC_MAX, \
  907. sst_gain_get, sst_gain_put, \
  908. SST_MODULE_ID_GAIN_CELL, path_id, instance, task_id, \
  909. sst_gain_tlv_common, gain_var)
  910. #define SST_VOLUME(name, path_id, task_id, instance, gain_var) \
  911. SST_GAIN_KCONTROLS(name, "Volume", SST_GAIN_MIN_VALUE, SST_GAIN_MAX_VALUE, \
  912. SST_GAIN_TC_MIN, SST_GAIN_TC_MAX, \
  913. sst_gain_get, sst_gain_put, \
  914. SST_MODULE_ID_VOLUME, path_id, instance, task_id, \
  915. sst_gain_tlv_common, gain_var)
  916. static struct sst_gain_value sst_gains[];
  917. static const struct snd_kcontrol_new sst_gain_controls[] = {
  918. SST_GAIN("media0_in", SST_PATH_INDEX_MEDIA0_IN, SST_TASK_MMX, 0, &sst_gains[0]),
  919. SST_GAIN("media1_in", SST_PATH_INDEX_MEDIA1_IN, SST_TASK_MMX, 0, &sst_gains[1]),
  920. SST_GAIN("media2_in", SST_PATH_INDEX_MEDIA2_IN, SST_TASK_MMX, 0, &sst_gains[2]),
  921. SST_GAIN("media3_in", SST_PATH_INDEX_MEDIA3_IN, SST_TASK_MMX, 0, &sst_gains[3]),
  922. SST_GAIN("pcm0_in", SST_PATH_INDEX_PCM0_IN, SST_TASK_SBA, 0, &sst_gains[4]),
  923. SST_GAIN("pcm1_in", SST_PATH_INDEX_PCM1_IN, SST_TASK_SBA, 0, &sst_gains[5]),
  924. SST_GAIN("pcm1_out", SST_PATH_INDEX_PCM1_OUT, SST_TASK_SBA, 0, &sst_gains[6]),
  925. SST_GAIN("pcm2_out", SST_PATH_INDEX_PCM2_OUT, SST_TASK_SBA, 0, &sst_gains[7]),
  926. SST_GAIN("codec_in0", SST_PATH_INDEX_CODEC_IN0, SST_TASK_SBA, 0, &sst_gains[8]),
  927. SST_GAIN("codec_in1", SST_PATH_INDEX_CODEC_IN1, SST_TASK_SBA, 0, &sst_gains[9]),
  928. SST_GAIN("codec_out0", SST_PATH_INDEX_CODEC_OUT0, SST_TASK_SBA, 0, &sst_gains[10]),
  929. SST_GAIN("codec_out1", SST_PATH_INDEX_CODEC_OUT1, SST_TASK_SBA, 0, &sst_gains[11]),
  930. SST_GAIN("media_loop1_out", SST_PATH_INDEX_MEDIA_LOOP1_OUT, SST_TASK_SBA, 0, &sst_gains[12]),
  931. SST_GAIN("media_loop2_out", SST_PATH_INDEX_MEDIA_LOOP2_OUT, SST_TASK_SBA, 0, &sst_gains[13]),
  932. SST_GAIN("sprot_loop_out", SST_PATH_INDEX_SPROT_LOOP_OUT, SST_TASK_SBA, 0, &sst_gains[14]),
  933. SST_VOLUME("media0_in", SST_PATH_INDEX_MEDIA0_IN, SST_TASK_MMX, 0, &sst_gains[15]),
  934. };
  935. #define SST_GAIN_NUM_CONTROLS 3
  936. /* the SST_GAIN macro above will create three alsa controls for each
  937. * instance invoked, gain, mute and ramp duration, which use the same gain
  938. * cell sst_gain to keep track of data
  939. * To calculate number of gain cell instances we need to device by 3 in
  940. * below caulcation for gain cell memory.
  941. * This gets rid of static number and issues while adding new controls
  942. */
  943. static struct sst_gain_value sst_gains[ARRAY_SIZE(sst_gain_controls)/SST_GAIN_NUM_CONTROLS];
  944. static const struct snd_kcontrol_new sst_algo_controls[] = {
  945. SST_ALGO_KCONTROL_BYTES("media_loop1_out", "fir", 272, SST_MODULE_ID_FIR_24,
  946. SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_VB_SET_FIR),
  947. SST_ALGO_KCONTROL_BYTES("media_loop1_out", "iir", 300, SST_MODULE_ID_IIR_24,
  948. SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
  949. SST_ALGO_KCONTROL_BYTES("media_loop1_out", "mdrp", 286, SST_MODULE_ID_MDRP,
  950. SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_SET_MDRP),
  951. SST_ALGO_KCONTROL_BYTES("media_loop2_out", "fir", 272, SST_MODULE_ID_FIR_24,
  952. SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_VB_SET_FIR),
  953. SST_ALGO_KCONTROL_BYTES("media_loop2_out", "iir", 300, SST_MODULE_ID_IIR_24,
  954. SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
  955. SST_ALGO_KCONTROL_BYTES("media_loop2_out", "mdrp", 286, SST_MODULE_ID_MDRP,
  956. SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_SET_MDRP),
  957. SST_ALGO_KCONTROL_BYTES("sprot_loop_out", "lpro", 192, SST_MODULE_ID_SPROT,
  958. SST_PATH_INDEX_SPROT_LOOP_OUT, 0, SST_TASK_SBA, SBA_VB_LPRO),
  959. SST_ALGO_KCONTROL_BYTES("codec_in0", "dcr", 52, SST_MODULE_ID_FILT_DCR,
  960. SST_PATH_INDEX_CODEC_IN0, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
  961. SST_ALGO_KCONTROL_BYTES("codec_in1", "dcr", 52, SST_MODULE_ID_FILT_DCR,
  962. SST_PATH_INDEX_CODEC_IN1, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
  963. };
  964. static int sst_algo_control_init(struct device *dev)
  965. {
  966. int i = 0;
  967. struct sst_algo_control *bc;
  968. /*allocate space to cache the algo parameters in the driver*/
  969. for (i = 0; i < ARRAY_SIZE(sst_algo_controls); i++) {
  970. bc = (struct sst_algo_control *)sst_algo_controls[i].private_value;
  971. bc->params = devm_kzalloc(dev, bc->max, GFP_KERNEL);
  972. if (bc->params == NULL)
  973. return -ENOMEM;
  974. }
  975. return 0;
  976. }
  977. static bool is_sst_dapm_widget(struct snd_soc_dapm_widget *w)
  978. {
  979. switch (w->id) {
  980. case snd_soc_dapm_pga:
  981. case snd_soc_dapm_aif_in:
  982. case snd_soc_dapm_aif_out:
  983. case snd_soc_dapm_input:
  984. case snd_soc_dapm_output:
  985. case snd_soc_dapm_mixer:
  986. return true;
  987. default:
  988. return false;
  989. }
  990. }
  991. /**
  992. * sst_send_pipe_gains - send gains for the front-end DAIs
  993. *
  994. * The gains in the pipes connected to the front-ends are muted/unmuted
  995. * automatically via the digital_mute() DAPM callback. This function sends the
  996. * gains for the front-end pipes.
  997. */
  998. int sst_send_pipe_gains(struct snd_soc_dai *dai, int stream, int mute)
  999. {
  1000. struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
  1001. struct snd_soc_dapm_widget *w;
  1002. struct snd_soc_dapm_path *p = NULL;
  1003. dev_dbg(dai->dev, "enter, dai-name=%s dir=%d\n", dai->name, stream);
  1004. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1005. dev_dbg(dai->dev, "Stream name=%s\n",
  1006. dai->playback_widget->name);
  1007. w = dai->playback_widget;
  1008. list_for_each_entry(p, &w->sinks, list_source) {
  1009. if (p->connected && !p->connected(w, p->sink))
  1010. continue;
  1011. if (p->connect && p->sink->power &&
  1012. is_sst_dapm_widget(p->sink)) {
  1013. struct sst_ids *ids = p->sink->priv;
  1014. dev_dbg(dai->dev, "send gains for widget=%s\n",
  1015. p->sink->name);
  1016. mutex_lock(&drv->lock);
  1017. sst_set_pipe_gain(ids, drv, mute);
  1018. mutex_unlock(&drv->lock);
  1019. }
  1020. }
  1021. } else {
  1022. dev_dbg(dai->dev, "Stream name=%s\n",
  1023. dai->capture_widget->name);
  1024. w = dai->capture_widget;
  1025. list_for_each_entry(p, &w->sources, list_sink) {
  1026. if (p->connected && !p->connected(w, p->sink))
  1027. continue;
  1028. if (p->connect && p->source->power &&
  1029. is_sst_dapm_widget(p->source)) {
  1030. struct sst_ids *ids = p->source->priv;
  1031. dev_dbg(dai->dev, "send gain for widget=%s\n",
  1032. p->source->name);
  1033. mutex_lock(&drv->lock);
  1034. sst_set_pipe_gain(ids, drv, mute);
  1035. mutex_unlock(&drv->lock);
  1036. }
  1037. }
  1038. }
  1039. return 0;
  1040. }
  1041. /**
  1042. * sst_fill_module_list - populate the list of modules/gains for a pipe
  1043. *
  1044. *
  1045. * Fills the widget pointer in the kcontrol private data, and also fills the
  1046. * kcontrol pointer in the widget private data.
  1047. *
  1048. * Widget pointer is used to send the algo/gain in the .put() handler if the
  1049. * widget is powerd on.
  1050. *
  1051. * Kcontrol pointer is used to send the algo/gain in the widget power ON/OFF
  1052. * event handler. Each widget (pipe) has multiple algos stored in the algo_list.
  1053. */
  1054. static int sst_fill_module_list(struct snd_kcontrol *kctl,
  1055. struct snd_soc_dapm_widget *w, int type)
  1056. {
  1057. struct sst_module *module = NULL;
  1058. struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
  1059. struct sst_ids *ids = w->priv;
  1060. int ret = 0;
  1061. module = devm_kzalloc(c->dev, sizeof(*module), GFP_KERNEL);
  1062. if (!module)
  1063. return -ENOMEM;
  1064. if (type == SST_MODULE_GAIN) {
  1065. struct sst_gain_mixer_control *mc = (void *)kctl->private_value;
  1066. mc->w = w;
  1067. module->kctl = kctl;
  1068. list_add_tail(&module->node, &ids->gain_list);
  1069. } else if (type == SST_MODULE_ALGO) {
  1070. struct sst_algo_control *bc = (void *)kctl->private_value;
  1071. bc->w = w;
  1072. module->kctl = kctl;
  1073. list_add_tail(&module->node, &ids->algo_list);
  1074. } else {
  1075. dev_err(c->dev, "invoked for unknown type %d module %s",
  1076. type, kctl->id.name);
  1077. ret = -EINVAL;
  1078. }
  1079. return ret;
  1080. }
  1081. /**
  1082. * sst_fill_widget_module_info - fill list of gains/algos for the pipe
  1083. * @widget: pipe modelled as a DAPM widget
  1084. *
  1085. * Fill the list of gains/algos for the widget by looking at all the card
  1086. * controls and comparing the name of the widget with the first part of control
  1087. * name. First part of control name contains the pipe name (widget name).
  1088. */
  1089. static int sst_fill_widget_module_info(struct snd_soc_dapm_widget *w,
  1090. struct snd_soc_platform *platform)
  1091. {
  1092. struct snd_kcontrol *kctl;
  1093. int index, ret = 0;
  1094. struct snd_card *card = platform->component.card->snd_card;
  1095. char *idx;
  1096. down_read(&card->controls_rwsem);
  1097. list_for_each_entry(kctl, &card->controls, list) {
  1098. idx = strstr(kctl->id.name, " ");
  1099. if (idx == NULL)
  1100. continue;
  1101. index = strlen(kctl->id.name) - strlen(idx);
  1102. if (strstr(kctl->id.name, "Volume") &&
  1103. !strncmp(kctl->id.name, w->name, index))
  1104. ret = sst_fill_module_list(kctl, w, SST_MODULE_GAIN);
  1105. else if (strstr(kctl->id.name, "params") &&
  1106. !strncmp(kctl->id.name, w->name, index))
  1107. ret = sst_fill_module_list(kctl, w, SST_MODULE_ALGO);
  1108. else if (strstr(kctl->id.name, "Switch") &&
  1109. !strncmp(kctl->id.name, w->name, index) &&
  1110. strstr(kctl->id.name, "Gain")) {
  1111. struct sst_gain_mixer_control *mc =
  1112. (void *)kctl->private_value;
  1113. mc->w = w;
  1114. } else if (strstr(kctl->id.name, "interleaver") &&
  1115. !strncmp(kctl->id.name, w->name, index)) {
  1116. struct sst_enum *e = (void *)kctl->private_value;
  1117. e->w = w;
  1118. } else if (strstr(kctl->id.name, "deinterleaver") &&
  1119. !strncmp(kctl->id.name, w->name, index)) {
  1120. struct sst_enum *e = (void *)kctl->private_value;
  1121. e->w = w;
  1122. }
  1123. if (ret < 0) {
  1124. up_read(&card->controls_rwsem);
  1125. return ret;
  1126. }
  1127. }
  1128. up_read(&card->controls_rwsem);
  1129. return 0;
  1130. }
  1131. /**
  1132. * sst_fill_linked_widgets - fill the parent pointer for the linked widget
  1133. */
  1134. static void sst_fill_linked_widgets(struct snd_soc_platform *platform,
  1135. struct sst_ids *ids)
  1136. {
  1137. struct snd_soc_dapm_widget *w;
  1138. unsigned int len = strlen(ids->parent_wname);
  1139. list_for_each_entry(w, &platform->component.card->widgets, list) {
  1140. if (!strncmp(ids->parent_wname, w->name, len)) {
  1141. ids->parent_w = w;
  1142. break;
  1143. }
  1144. }
  1145. }
  1146. /**
  1147. * sst_map_modules_to_pipe - fill algo/gains list for all pipes
  1148. */
  1149. static int sst_map_modules_to_pipe(struct snd_soc_platform *platform)
  1150. {
  1151. struct snd_soc_dapm_widget *w;
  1152. int ret = 0;
  1153. list_for_each_entry(w, &platform->component.card->widgets, list) {
  1154. if (is_sst_dapm_widget(w) && (w->priv)) {
  1155. struct sst_ids *ids = w->priv;
  1156. dev_dbg(platform->dev, "widget type=%d name=%s\n",
  1157. w->id, w->name);
  1158. INIT_LIST_HEAD(&ids->algo_list);
  1159. INIT_LIST_HEAD(&ids->gain_list);
  1160. ret = sst_fill_widget_module_info(w, platform);
  1161. if (ret < 0)
  1162. return ret;
  1163. /* fill linked widgets */
  1164. if (ids->parent_wname != NULL)
  1165. sst_fill_linked_widgets(platform, ids);
  1166. }
  1167. }
  1168. return 0;
  1169. }
  1170. int sst_dsp_init_v2_dpcm(struct snd_soc_platform *platform)
  1171. {
  1172. int i, ret = 0;
  1173. struct snd_soc_dapm_context *dapm =
  1174. snd_soc_component_get_dapm(&platform->component);
  1175. struct sst_data *drv = snd_soc_platform_get_drvdata(platform);
  1176. unsigned int gains = ARRAY_SIZE(sst_gain_controls)/3;
  1177. drv->byte_stream = devm_kzalloc(platform->dev,
  1178. SST_MAX_BIN_BYTES, GFP_KERNEL);
  1179. if (!drv->byte_stream)
  1180. return -ENOMEM;
  1181. snd_soc_dapm_new_controls(dapm, sst_dapm_widgets,
  1182. ARRAY_SIZE(sst_dapm_widgets));
  1183. snd_soc_dapm_add_routes(dapm, intercon,
  1184. ARRAY_SIZE(intercon));
  1185. snd_soc_dapm_new_widgets(dapm->card);
  1186. for (i = 0; i < gains; i++) {
  1187. sst_gains[i].mute = SST_GAIN_MUTE_DEFAULT;
  1188. sst_gains[i].l_gain = SST_GAIN_VOLUME_DEFAULT;
  1189. sst_gains[i].r_gain = SST_GAIN_VOLUME_DEFAULT;
  1190. sst_gains[i].ramp_duration = SST_GAIN_RAMP_DURATION_DEFAULT;
  1191. }
  1192. ret = snd_soc_add_platform_controls(platform, sst_gain_controls,
  1193. ARRAY_SIZE(sst_gain_controls));
  1194. if (ret)
  1195. return ret;
  1196. /* Initialize algo control params */
  1197. ret = sst_algo_control_init(platform->dev);
  1198. if (ret)
  1199. return ret;
  1200. ret = snd_soc_add_platform_controls(platform, sst_algo_controls,
  1201. ARRAY_SIZE(sst_algo_controls));
  1202. if (ret)
  1203. return ret;
  1204. ret = snd_soc_add_platform_controls(platform, sst_slot_controls,
  1205. ARRAY_SIZE(sst_slot_controls));
  1206. if (ret)
  1207. return ret;
  1208. ret = sst_map_modules_to_pipe(platform);
  1209. return ret;
  1210. }