sigmadsp.c 19 KB

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