sigmadsp.c 20 KB

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