sigmadsp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Load Analog Devices SigmaStudio firmware files
  4. *
  5. * Copyright 2009-2014 Analog Devices Inc.
  6. */
  7. #include <linux/crc32.h>
  8. #include <linux/firmware.h>
  9. #include <linux/kernel.h>
  10. #include <linux/i2c.h>
  11. #include <linux/regmap.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <sound/control.h>
  15. #include <sound/soc.h>
  16. #include "sigmadsp.h"
  17. #define SIGMA_MAGIC "ADISIGM"
  18. #define SIGMA_FW_CHUNK_TYPE_DATA 0
  19. #define SIGMA_FW_CHUNK_TYPE_CONTROL 1
  20. #define SIGMA_FW_CHUNK_TYPE_SAMPLERATES 2
  21. struct sigmadsp_control {
  22. struct list_head head;
  23. uint32_t samplerates;
  24. unsigned int addr;
  25. unsigned int num_bytes;
  26. const char *name;
  27. struct snd_kcontrol *kcontrol;
  28. bool cached;
  29. uint8_t cache[];
  30. };
  31. struct sigmadsp_data {
  32. struct list_head head;
  33. uint32_t samplerates;
  34. unsigned int addr;
  35. unsigned int length;
  36. uint8_t data[];
  37. };
  38. struct sigma_fw_chunk {
  39. __le32 length;
  40. __le32 tag;
  41. __le32 samplerates;
  42. } __packed;
  43. struct sigma_fw_chunk_data {
  44. struct sigma_fw_chunk chunk;
  45. __le16 addr;
  46. uint8_t data[];
  47. } __packed;
  48. struct sigma_fw_chunk_control {
  49. struct sigma_fw_chunk chunk;
  50. __le16 type;
  51. __le16 addr;
  52. __le16 num_bytes;
  53. const char name[];
  54. } __packed;
  55. struct sigma_fw_chunk_samplerate {
  56. struct sigma_fw_chunk chunk;
  57. __le32 samplerates[];
  58. } __packed;
  59. struct sigma_firmware_header {
  60. unsigned char magic[7];
  61. u8 version;
  62. __le32 crc;
  63. } __packed;
  64. enum {
  65. SIGMA_ACTION_WRITEXBYTES = 0,
  66. SIGMA_ACTION_WRITESINGLE,
  67. SIGMA_ACTION_WRITESAFELOAD,
  68. SIGMA_ACTION_END,
  69. };
  70. struct sigma_action {
  71. u8 instr;
  72. u8 len_hi;
  73. __le16 len;
  74. __be16 addr;
  75. unsigned char payload[];
  76. } __packed;
  77. static int sigmadsp_write(struct sigmadsp *sigmadsp, unsigned int addr,
  78. const uint8_t data[], size_t len)
  79. {
  80. return sigmadsp->write(sigmadsp->control_data, addr, data, len);
  81. }
  82. static int sigmadsp_read(struct sigmadsp *sigmadsp, unsigned int addr,
  83. uint8_t data[], size_t len)
  84. {
  85. return sigmadsp->read(sigmadsp->control_data, addr, data, len);
  86. }
  87. static int sigmadsp_ctrl_info(struct snd_kcontrol *kcontrol,
  88. struct snd_ctl_elem_info *info)
  89. {
  90. struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
  91. info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  92. info->count = ctrl->num_bytes;
  93. return 0;
  94. }
  95. static int sigmadsp_ctrl_write(struct sigmadsp *sigmadsp,
  96. struct sigmadsp_control *ctrl, void *data)
  97. {
  98. /* safeload loads up to 20 bytes in a atomic operation */
  99. if (ctrl->num_bytes <= 20 && sigmadsp->ops && sigmadsp->ops->safeload)
  100. return sigmadsp->ops->safeload(sigmadsp, ctrl->addr, data,
  101. ctrl->num_bytes);
  102. else
  103. return sigmadsp_write(sigmadsp, ctrl->addr, data,
  104. ctrl->num_bytes);
  105. }
  106. static int sigmadsp_ctrl_put(struct snd_kcontrol *kcontrol,
  107. struct snd_ctl_elem_value *ucontrol)
  108. {
  109. struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
  110. struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
  111. uint8_t *data;
  112. int ret = 0;
  113. mutex_lock(&sigmadsp->lock);
  114. data = ucontrol->value.bytes.data;
  115. if (!(kcontrol->vd[0].access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
  116. ret = sigmadsp_ctrl_write(sigmadsp, ctrl, data);
  117. if (ret == 0) {
  118. memcpy(ctrl->cache, data, ctrl->num_bytes);
  119. ctrl->cached = true;
  120. }
  121. mutex_unlock(&sigmadsp->lock);
  122. return ret;
  123. }
  124. static int sigmadsp_ctrl_get(struct snd_kcontrol *kcontrol,
  125. struct snd_ctl_elem_value *ucontrol)
  126. {
  127. struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
  128. struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
  129. int ret = 0;
  130. mutex_lock(&sigmadsp->lock);
  131. if (!ctrl->cached) {
  132. ret = sigmadsp_read(sigmadsp, ctrl->addr, ctrl->cache,
  133. ctrl->num_bytes);
  134. }
  135. if (ret == 0) {
  136. ctrl->cached = true;
  137. memcpy(ucontrol->value.bytes.data, ctrl->cache,
  138. ctrl->num_bytes);
  139. }
  140. mutex_unlock(&sigmadsp->lock);
  141. return ret;
  142. }
  143. static void sigmadsp_control_free(struct snd_kcontrol *kcontrol)
  144. {
  145. struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
  146. ctrl->kcontrol = NULL;
  147. }
  148. static bool sigma_fw_validate_control_name(const char *name, unsigned int len)
  149. {
  150. unsigned int i;
  151. for (i = 0; i < len; i++) {
  152. /* Normal ASCII characters are valid */
  153. if (name[i] < ' ' || name[i] > '~')
  154. return false;
  155. }
  156. return true;
  157. }
  158. static int sigma_fw_load_control(struct sigmadsp *sigmadsp,
  159. const struct sigma_fw_chunk *chunk, unsigned int length)
  160. {
  161. const struct sigma_fw_chunk_control *ctrl_chunk;
  162. struct sigmadsp_control *ctrl;
  163. unsigned int num_bytes;
  164. size_t name_len;
  165. char *name;
  166. int ret;
  167. if (length <= sizeof(*ctrl_chunk))
  168. return -EINVAL;
  169. ctrl_chunk = (const struct sigma_fw_chunk_control *)chunk;
  170. name_len = length - sizeof(*ctrl_chunk);
  171. if (name_len >= SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  172. name_len = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1;
  173. /* Make sure there are no non-displayable characaters in the string */
  174. if (!sigma_fw_validate_control_name(ctrl_chunk->name, name_len))
  175. return -EINVAL;
  176. num_bytes = le16_to_cpu(ctrl_chunk->num_bytes);
  177. ctrl = kzalloc(sizeof(*ctrl) + num_bytes, GFP_KERNEL);
  178. if (!ctrl)
  179. return -ENOMEM;
  180. name = kzalloc(name_len + 1, GFP_KERNEL);
  181. if (!name) {
  182. ret = -ENOMEM;
  183. goto err_free_ctrl;
  184. }
  185. memcpy(name, ctrl_chunk->name, name_len);
  186. name[name_len] = '\0';
  187. ctrl->name = name;
  188. ctrl->addr = le16_to_cpu(ctrl_chunk->addr);
  189. ctrl->num_bytes = num_bytes;
  190. ctrl->samplerates = le32_to_cpu(chunk->samplerates);
  191. list_add_tail(&ctrl->head, &sigmadsp->ctrl_list);
  192. return 0;
  193. err_free_ctrl:
  194. kfree(ctrl);
  195. return ret;
  196. }
  197. static int sigma_fw_load_data(struct sigmadsp *sigmadsp,
  198. const struct sigma_fw_chunk *chunk, unsigned int length)
  199. {
  200. const struct sigma_fw_chunk_data *data_chunk;
  201. struct sigmadsp_data *data;
  202. if (length <= sizeof(*data_chunk))
  203. return -EINVAL;
  204. data_chunk = (struct sigma_fw_chunk_data *)chunk;
  205. length -= sizeof(*data_chunk);
  206. data = kzalloc(sizeof(*data) + length, GFP_KERNEL);
  207. if (!data)
  208. return -ENOMEM;
  209. data->addr = le16_to_cpu(data_chunk->addr);
  210. data->length = length;
  211. data->samplerates = le32_to_cpu(chunk->samplerates);
  212. memcpy(data->data, data_chunk->data, length);
  213. list_add_tail(&data->head, &sigmadsp->data_list);
  214. return 0;
  215. }
  216. static int sigma_fw_load_samplerates(struct sigmadsp *sigmadsp,
  217. const struct sigma_fw_chunk *chunk, unsigned int length)
  218. {
  219. const struct sigma_fw_chunk_samplerate *rate_chunk;
  220. unsigned int num_rates;
  221. unsigned int *rates;
  222. unsigned int i;
  223. rate_chunk = (const struct sigma_fw_chunk_samplerate *)chunk;
  224. num_rates = (length - sizeof(*rate_chunk)) / sizeof(__le32);
  225. if (num_rates > 32 || num_rates == 0)
  226. return -EINVAL;
  227. /* We only allow one samplerates block per file */
  228. if (sigmadsp->rate_constraints.count)
  229. return -EINVAL;
  230. rates = kcalloc(num_rates, sizeof(*rates), GFP_KERNEL);
  231. if (!rates)
  232. return -ENOMEM;
  233. for (i = 0; i < num_rates; i++)
  234. rates[i] = le32_to_cpu(rate_chunk->samplerates[i]);
  235. sigmadsp->rate_constraints.count = num_rates;
  236. sigmadsp->rate_constraints.list = rates;
  237. return 0;
  238. }
  239. static int sigmadsp_fw_load_v2(struct sigmadsp *sigmadsp,
  240. const struct firmware *fw)
  241. {
  242. struct sigma_fw_chunk *chunk;
  243. unsigned int length, pos;
  244. int ret;
  245. /*
  246. * Make sure that there is at least one chunk to avoid integer
  247. * underflows later on. Empty firmware is still valid though.
  248. */
  249. if (fw->size < sizeof(*chunk) + sizeof(struct sigma_firmware_header))
  250. return 0;
  251. pos = sizeof(struct sigma_firmware_header);
  252. while (pos < fw->size - sizeof(*chunk)) {
  253. chunk = (struct sigma_fw_chunk *)(fw->data + pos);
  254. length = le32_to_cpu(chunk->length);
  255. if (length > fw->size - pos || length < sizeof(*chunk))
  256. return -EINVAL;
  257. switch (le32_to_cpu(chunk->tag)) {
  258. case SIGMA_FW_CHUNK_TYPE_DATA:
  259. ret = sigma_fw_load_data(sigmadsp, chunk, length);
  260. break;
  261. case SIGMA_FW_CHUNK_TYPE_CONTROL:
  262. ret = sigma_fw_load_control(sigmadsp, chunk, length);
  263. break;
  264. case SIGMA_FW_CHUNK_TYPE_SAMPLERATES:
  265. ret = sigma_fw_load_samplerates(sigmadsp, chunk, length);
  266. break;
  267. default:
  268. dev_warn(sigmadsp->dev, "Unknown chunk type: %d\n",
  269. chunk->tag);
  270. ret = 0;
  271. break;
  272. }
  273. if (ret)
  274. return ret;
  275. /*
  276. * This can not overflow since if length is larger than the
  277. * maximum firmware size (0x4000000) we'll error out earilier.
  278. */
  279. pos += ALIGN(length, sizeof(__le32));
  280. }
  281. return 0;
  282. }
  283. static inline u32 sigma_action_len(struct sigma_action *sa)
  284. {
  285. return (sa->len_hi << 16) | le16_to_cpu(sa->len);
  286. }
  287. static size_t sigma_action_size(struct sigma_action *sa)
  288. {
  289. size_t payload = 0;
  290. switch (sa->instr) {
  291. case SIGMA_ACTION_WRITEXBYTES:
  292. case SIGMA_ACTION_WRITESINGLE:
  293. case SIGMA_ACTION_WRITESAFELOAD:
  294. payload = sigma_action_len(sa);
  295. break;
  296. default:
  297. break;
  298. }
  299. payload = ALIGN(payload, 2);
  300. return payload + sizeof(struct sigma_action);
  301. }
  302. /*
  303. * Returns a negative error value in case of an error, 0 if processing of
  304. * the firmware should be stopped after this action, 1 otherwise.
  305. */
  306. static int process_sigma_action(struct sigmadsp *sigmadsp,
  307. struct sigma_action *sa)
  308. {
  309. size_t len = sigma_action_len(sa);
  310. struct sigmadsp_data *data;
  311. pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
  312. sa->instr, sa->addr, len);
  313. switch (sa->instr) {
  314. case SIGMA_ACTION_WRITEXBYTES:
  315. case SIGMA_ACTION_WRITESINGLE:
  316. case SIGMA_ACTION_WRITESAFELOAD:
  317. if (len < 3)
  318. return -EINVAL;
  319. data = kzalloc(sizeof(*data) + len - 2, GFP_KERNEL);
  320. if (!data)
  321. return -ENOMEM;
  322. data->addr = be16_to_cpu(sa->addr);
  323. data->length = len - 2;
  324. memcpy(data->data, sa->payload, data->length);
  325. list_add_tail(&data->head, &sigmadsp->data_list);
  326. break;
  327. case SIGMA_ACTION_END:
  328. return 0;
  329. default:
  330. return -EINVAL;
  331. }
  332. return 1;
  333. }
  334. static int sigmadsp_fw_load_v1(struct sigmadsp *sigmadsp,
  335. const struct firmware *fw)
  336. {
  337. struct sigma_action *sa;
  338. size_t size, pos;
  339. int ret;
  340. pos = sizeof(struct sigma_firmware_header);
  341. while (pos + sizeof(*sa) <= fw->size) {
  342. sa = (struct sigma_action *)(fw->data + pos);
  343. size = sigma_action_size(sa);
  344. pos += size;
  345. if (pos > fw->size || size == 0)
  346. break;
  347. ret = process_sigma_action(sigmadsp, sa);
  348. pr_debug("%s: action returned %i\n", __func__, ret);
  349. if (ret <= 0)
  350. return ret;
  351. }
  352. if (pos != fw->size)
  353. return -EINVAL;
  354. return 0;
  355. }
  356. static void sigmadsp_firmware_release(struct sigmadsp *sigmadsp)
  357. {
  358. struct sigmadsp_control *ctrl, *_ctrl;
  359. struct sigmadsp_data *data, *_data;
  360. list_for_each_entry_safe(ctrl, _ctrl, &sigmadsp->ctrl_list, head) {
  361. kfree(ctrl->name);
  362. kfree(ctrl);
  363. }
  364. list_for_each_entry_safe(data, _data, &sigmadsp->data_list, head)
  365. kfree(data);
  366. INIT_LIST_HEAD(&sigmadsp->ctrl_list);
  367. INIT_LIST_HEAD(&sigmadsp->data_list);
  368. }
  369. static void devm_sigmadsp_release(struct device *dev, void *res)
  370. {
  371. sigmadsp_firmware_release((struct sigmadsp *)res);
  372. }
  373. static int sigmadsp_firmware_load(struct sigmadsp *sigmadsp, const char *name)
  374. {
  375. const struct sigma_firmware_header *ssfw_head;
  376. const struct firmware *fw;
  377. int ret;
  378. u32 crc;
  379. /* first load the blob */
  380. ret = request_firmware(&fw, name, sigmadsp->dev);
  381. if (ret) {
  382. pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
  383. goto done;
  384. }
  385. /* then verify the header */
  386. ret = -EINVAL;
  387. /*
  388. * Reject too small or unreasonable large files. The upper limit has been
  389. * chosen a bit arbitrarily, but it should be enough for all practical
  390. * purposes and having the limit makes it easier to avoid integer
  391. * overflows later in the loading process.
  392. */
  393. if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
  394. dev_err(sigmadsp->dev, "Failed to load firmware: Invalid size\n");
  395. goto done;
  396. }
  397. ssfw_head = (void *)fw->data;
  398. if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic))) {
  399. dev_err(sigmadsp->dev, "Failed to load firmware: Invalid magic\n");
  400. goto done;
  401. }
  402. crc = crc32(0, fw->data + sizeof(*ssfw_head),
  403. fw->size - sizeof(*ssfw_head));
  404. pr_debug("%s: crc=%x\n", __func__, crc);
  405. if (crc != le32_to_cpu(ssfw_head->crc)) {
  406. dev_err(sigmadsp->dev, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
  407. le32_to_cpu(ssfw_head->crc), crc);
  408. goto done;
  409. }
  410. switch (ssfw_head->version) {
  411. case 1:
  412. ret = sigmadsp_fw_load_v1(sigmadsp, fw);
  413. break;
  414. case 2:
  415. ret = sigmadsp_fw_load_v2(sigmadsp, fw);
  416. break;
  417. default:
  418. dev_err(sigmadsp->dev,
  419. "Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
  420. ssfw_head->version);
  421. ret = -EINVAL;
  422. break;
  423. }
  424. if (ret)
  425. sigmadsp_firmware_release(sigmadsp);
  426. done:
  427. release_firmware(fw);
  428. return ret;
  429. }
  430. static int sigmadsp_init(struct sigmadsp *sigmadsp, struct device *dev,
  431. const struct sigmadsp_ops *ops, const char *firmware_name)
  432. {
  433. sigmadsp->ops = ops;
  434. sigmadsp->dev = dev;
  435. INIT_LIST_HEAD(&sigmadsp->ctrl_list);
  436. INIT_LIST_HEAD(&sigmadsp->data_list);
  437. mutex_init(&sigmadsp->lock);
  438. return sigmadsp_firmware_load(sigmadsp, firmware_name);
  439. }
  440. /**
  441. * devm_sigmadsp_init() - Initialize SigmaDSP instance
  442. * @dev: The parent device
  443. * @ops: The sigmadsp_ops to use for this instance
  444. * @firmware_name: Name of the firmware file to load
  445. *
  446. * Allocates a SigmaDSP instance and loads the specified firmware file.
  447. *
  448. * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
  449. */
  450. struct sigmadsp *devm_sigmadsp_init(struct device *dev,
  451. const struct sigmadsp_ops *ops, const char *firmware_name)
  452. {
  453. struct sigmadsp *sigmadsp;
  454. int ret;
  455. sigmadsp = devres_alloc(devm_sigmadsp_release, sizeof(*sigmadsp),
  456. GFP_KERNEL);
  457. if (!sigmadsp)
  458. return ERR_PTR(-ENOMEM);
  459. ret = sigmadsp_init(sigmadsp, dev, ops, firmware_name);
  460. if (ret) {
  461. devres_free(sigmadsp);
  462. return ERR_PTR(ret);
  463. }
  464. devres_add(dev, sigmadsp);
  465. return sigmadsp;
  466. }
  467. EXPORT_SYMBOL_GPL(devm_sigmadsp_init);
  468. static int sigmadsp_rate_to_index(struct sigmadsp *sigmadsp, unsigned int rate)
  469. {
  470. unsigned int i;
  471. for (i = 0; i < sigmadsp->rate_constraints.count; i++) {
  472. if (sigmadsp->rate_constraints.list[i] == rate)
  473. return i;
  474. }
  475. return -EINVAL;
  476. }
  477. static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp *sigmadsp,
  478. unsigned int samplerate)
  479. {
  480. int samplerate_index;
  481. if (samplerate == 0)
  482. return 0;
  483. if (sigmadsp->rate_constraints.count) {
  484. samplerate_index = sigmadsp_rate_to_index(sigmadsp, samplerate);
  485. if (samplerate_index < 0)
  486. return 0;
  487. return BIT(samplerate_index);
  488. } else {
  489. return ~0;
  490. }
  491. }
  492. static bool sigmadsp_samplerate_valid(unsigned int supported,
  493. unsigned int requested)
  494. {
  495. /* All samplerates are supported */
  496. if (!supported)
  497. return true;
  498. return supported & requested;
  499. }
  500. static int sigmadsp_alloc_control(struct sigmadsp *sigmadsp,
  501. struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
  502. {
  503. struct snd_kcontrol_new template;
  504. struct snd_kcontrol *kcontrol;
  505. memset(&template, 0, sizeof(template));
  506. template.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  507. template.name = ctrl->name;
  508. template.info = sigmadsp_ctrl_info;
  509. template.get = sigmadsp_ctrl_get;
  510. template.put = sigmadsp_ctrl_put;
  511. template.private_value = (unsigned long)ctrl;
  512. template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  513. if (!sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask))
  514. template.access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  515. kcontrol = snd_ctl_new1(&template, sigmadsp);
  516. if (!kcontrol)
  517. return -ENOMEM;
  518. kcontrol->private_free = sigmadsp_control_free;
  519. ctrl->kcontrol = kcontrol;
  520. return snd_ctl_add(sigmadsp->component->card->snd_card, kcontrol);
  521. }
  522. static void sigmadsp_activate_ctrl(struct sigmadsp *sigmadsp,
  523. struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
  524. {
  525. struct snd_card *card = sigmadsp->component->card->snd_card;
  526. struct snd_kcontrol_volatile *vd;
  527. struct snd_ctl_elem_id id;
  528. bool active;
  529. bool changed = false;
  530. active = sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask);
  531. down_write(&card->controls_rwsem);
  532. if (!ctrl->kcontrol) {
  533. up_write(&card->controls_rwsem);
  534. return;
  535. }
  536. id = ctrl->kcontrol->id;
  537. vd = &ctrl->kcontrol->vd[0];
  538. if (active == (bool)(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)) {
  539. vd->access ^= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  540. changed = true;
  541. }
  542. up_write(&card->controls_rwsem);
  543. if (active && changed) {
  544. mutex_lock(&sigmadsp->lock);
  545. if (ctrl->cached)
  546. sigmadsp_ctrl_write(sigmadsp, ctrl, ctrl->cache);
  547. mutex_unlock(&sigmadsp->lock);
  548. }
  549. if (changed)
  550. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, &id);
  551. }
  552. /**
  553. * sigmadsp_attach() - Attach a sigmadsp instance to a ASoC component
  554. * @sigmadsp: The sigmadsp instance to attach
  555. * @component: The component to attach to
  556. *
  557. * Typically called in the components probe callback.
  558. *
  559. * Note, once this function has been called the firmware must not be released
  560. * until after the ALSA snd_card that the component belongs to has been
  561. * disconnected, even if sigmadsp_attach() returns an error.
  562. */
  563. int sigmadsp_attach(struct sigmadsp *sigmadsp,
  564. struct snd_soc_component *component)
  565. {
  566. struct sigmadsp_control *ctrl;
  567. unsigned int samplerate_mask;
  568. int ret;
  569. sigmadsp->component = component;
  570. samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp,
  571. sigmadsp->current_samplerate);
  572. list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head) {
  573. ret = sigmadsp_alloc_control(sigmadsp, ctrl, samplerate_mask);
  574. if (ret)
  575. return ret;
  576. }
  577. return 0;
  578. }
  579. EXPORT_SYMBOL_GPL(sigmadsp_attach);
  580. /**
  581. * sigmadsp_setup() - Setup the DSP for the specified samplerate
  582. * @sigmadsp: The sigmadsp instance to configure
  583. * @samplerate: The samplerate the DSP should be configured for
  584. *
  585. * Loads the appropriate firmware program and parameter memory (if not already
  586. * loaded) and enables the controls for the specified samplerate. Any control
  587. * parameter changes that have been made previously will be restored.
  588. *
  589. * Returns 0 on success, a negative error code otherwise.
  590. */
  591. int sigmadsp_setup(struct sigmadsp *sigmadsp, unsigned int samplerate)
  592. {
  593. struct sigmadsp_control *ctrl;
  594. unsigned int samplerate_mask;
  595. struct sigmadsp_data *data;
  596. int ret;
  597. if (sigmadsp->current_samplerate == samplerate)
  598. return 0;
  599. samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp, samplerate);
  600. if (samplerate_mask == 0)
  601. return -EINVAL;
  602. list_for_each_entry(data, &sigmadsp->data_list, head) {
  603. if (!sigmadsp_samplerate_valid(data->samplerates,
  604. samplerate_mask))
  605. continue;
  606. ret = sigmadsp_write(sigmadsp, data->addr, data->data,
  607. data->length);
  608. if (ret)
  609. goto err;
  610. }
  611. list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
  612. sigmadsp_activate_ctrl(sigmadsp, ctrl, samplerate_mask);
  613. sigmadsp->current_samplerate = samplerate;
  614. return 0;
  615. err:
  616. sigmadsp_reset(sigmadsp);
  617. return ret;
  618. }
  619. EXPORT_SYMBOL_GPL(sigmadsp_setup);
  620. /**
  621. * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
  622. * @sigmadsp: The sigmadsp instance to reset
  623. *
  624. * Should be called whenever the DSP has been reset and parameter and program
  625. * memory need to be re-loaded.
  626. */
  627. void sigmadsp_reset(struct sigmadsp *sigmadsp)
  628. {
  629. struct sigmadsp_control *ctrl;
  630. list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
  631. sigmadsp_activate_ctrl(sigmadsp, ctrl, false);
  632. sigmadsp->current_samplerate = 0;
  633. }
  634. EXPORT_SYMBOL_GPL(sigmadsp_reset);
  635. /**
  636. * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
  637. * @sigmadsp: The sigmadsp instance
  638. * @substream: The substream to restrict
  639. *
  640. * Applies samplerate constraints that may be required by the firmware Should
  641. * typically be called from the CODEC/component drivers startup callback.
  642. *
  643. * Returns 0 on success, a negative error code otherwise.
  644. */
  645. int sigmadsp_restrict_params(struct sigmadsp *sigmadsp,
  646. struct snd_pcm_substream *substream)
  647. {
  648. if (sigmadsp->rate_constraints.count == 0)
  649. return 0;
  650. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  651. SNDRV_PCM_HW_PARAM_RATE, &sigmadsp->rate_constraints);
  652. }
  653. EXPORT_SYMBOL_GPL(sigmadsp_restrict_params);
  654. MODULE_LICENSE("GPL");