sigmadsp.c 19 KB

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