ctamixer.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
  3. *
  4. * This source file is released under GPL v2 license (no other versions).
  5. * See the COPYING file included in the main directory of this source
  6. * distribution for the license terms and conditions.
  7. *
  8. * @File ctamixer.c
  9. *
  10. * @Brief
  11. * This file contains the implementation of the Audio Mixer
  12. * resource management object.
  13. *
  14. * @Author Liu Chun
  15. * @Date May 21 2008
  16. *
  17. */
  18. #include "ctamixer.h"
  19. #include "cthardware.h"
  20. #include <linux/slab.h>
  21. #define AMIXER_RESOURCE_NUM 256
  22. #define SUM_RESOURCE_NUM 256
  23. #define AMIXER_Y_IMMEDIATE 1
  24. #define BLANK_SLOT 4094
  25. static int amixer_master(struct rsc *rsc)
  26. {
  27. rsc->conj = 0;
  28. return rsc->idx = container_of(rsc, struct amixer, rsc)->idx[0];
  29. }
  30. static int amixer_next_conj(struct rsc *rsc)
  31. {
  32. rsc->conj++;
  33. return container_of(rsc, struct amixer, rsc)->idx[rsc->conj];
  34. }
  35. static int amixer_index(const struct rsc *rsc)
  36. {
  37. return container_of(rsc, struct amixer, rsc)->idx[rsc->conj];
  38. }
  39. static int amixer_output_slot(const struct rsc *rsc)
  40. {
  41. return (amixer_index(rsc) << 4) + 0x4;
  42. }
  43. static struct rsc_ops amixer_basic_rsc_ops = {
  44. .master = amixer_master,
  45. .next_conj = amixer_next_conj,
  46. .index = amixer_index,
  47. .output_slot = amixer_output_slot,
  48. };
  49. static int amixer_set_input(struct amixer *amixer, struct rsc *rsc)
  50. {
  51. struct hw *hw;
  52. hw = amixer->rsc.hw;
  53. hw->amixer_set_mode(amixer->rsc.ctrl_blk, AMIXER_Y_IMMEDIATE);
  54. amixer->input = rsc;
  55. if (!rsc)
  56. hw->amixer_set_x(amixer->rsc.ctrl_blk, BLANK_SLOT);
  57. else
  58. hw->amixer_set_x(amixer->rsc.ctrl_blk,
  59. rsc->ops->output_slot(rsc));
  60. return 0;
  61. }
  62. /* y is a 14-bit immediate constant */
  63. static int amixer_set_y(struct amixer *amixer, unsigned int y)
  64. {
  65. struct hw *hw;
  66. hw = amixer->rsc.hw;
  67. hw->amixer_set_y(amixer->rsc.ctrl_blk, y);
  68. return 0;
  69. }
  70. static int amixer_set_invalid_squash(struct amixer *amixer, unsigned int iv)
  71. {
  72. struct hw *hw;
  73. hw = amixer->rsc.hw;
  74. hw->amixer_set_iv(amixer->rsc.ctrl_blk, iv);
  75. return 0;
  76. }
  77. static int amixer_set_sum(struct amixer *amixer, struct sum *sum)
  78. {
  79. struct hw *hw;
  80. hw = amixer->rsc.hw;
  81. amixer->sum = sum;
  82. if (!sum) {
  83. hw->amixer_set_se(amixer->rsc.ctrl_blk, 0);
  84. } else {
  85. hw->amixer_set_se(amixer->rsc.ctrl_blk, 1);
  86. hw->amixer_set_sadr(amixer->rsc.ctrl_blk,
  87. sum->rsc.ops->index(&sum->rsc));
  88. }
  89. return 0;
  90. }
  91. static int amixer_commit_write(struct amixer *amixer)
  92. {
  93. struct hw *hw;
  94. unsigned int index;
  95. int i;
  96. struct rsc *input;
  97. struct sum *sum;
  98. hw = amixer->rsc.hw;
  99. input = amixer->input;
  100. sum = amixer->sum;
  101. /* Program master and conjugate resources */
  102. amixer->rsc.ops->master(&amixer->rsc);
  103. if (input)
  104. input->ops->master(input);
  105. if (sum)
  106. sum->rsc.ops->master(&sum->rsc);
  107. for (i = 0; i < amixer->rsc.msr; i++) {
  108. hw->amixer_set_dirty_all(amixer->rsc.ctrl_blk);
  109. if (input) {
  110. hw->amixer_set_x(amixer->rsc.ctrl_blk,
  111. input->ops->output_slot(input));
  112. input->ops->next_conj(input);
  113. }
  114. if (sum) {
  115. hw->amixer_set_sadr(amixer->rsc.ctrl_blk,
  116. sum->rsc.ops->index(&sum->rsc));
  117. sum->rsc.ops->next_conj(&sum->rsc);
  118. }
  119. index = amixer->rsc.ops->output_slot(&amixer->rsc);
  120. hw->amixer_commit_write(hw, index, amixer->rsc.ctrl_blk);
  121. amixer->rsc.ops->next_conj(&amixer->rsc);
  122. }
  123. amixer->rsc.ops->master(&amixer->rsc);
  124. if (input)
  125. input->ops->master(input);
  126. if (sum)
  127. sum->rsc.ops->master(&sum->rsc);
  128. return 0;
  129. }
  130. static int amixer_commit_raw_write(struct amixer *amixer)
  131. {
  132. struct hw *hw;
  133. unsigned int index;
  134. hw = amixer->rsc.hw;
  135. index = amixer->rsc.ops->output_slot(&amixer->rsc);
  136. hw->amixer_commit_write(hw, index, amixer->rsc.ctrl_blk);
  137. return 0;
  138. }
  139. static int amixer_get_y(struct amixer *amixer)
  140. {
  141. struct hw *hw;
  142. hw = amixer->rsc.hw;
  143. return hw->amixer_get_y(amixer->rsc.ctrl_blk);
  144. }
  145. static int amixer_setup(struct amixer *amixer, struct rsc *input,
  146. unsigned int scale, struct sum *sum)
  147. {
  148. amixer_set_input(amixer, input);
  149. amixer_set_y(amixer, scale);
  150. amixer_set_sum(amixer, sum);
  151. amixer_commit_write(amixer);
  152. return 0;
  153. }
  154. static struct amixer_rsc_ops amixer_ops = {
  155. .set_input = amixer_set_input,
  156. .set_invalid_squash = amixer_set_invalid_squash,
  157. .set_scale = amixer_set_y,
  158. .set_sum = amixer_set_sum,
  159. .commit_write = amixer_commit_write,
  160. .commit_raw_write = amixer_commit_raw_write,
  161. .setup = amixer_setup,
  162. .get_scale = amixer_get_y,
  163. };
  164. static int amixer_rsc_init(struct amixer *amixer,
  165. const struct amixer_desc *desc,
  166. struct amixer_mgr *mgr)
  167. {
  168. int err;
  169. err = rsc_init(&amixer->rsc, amixer->idx[0],
  170. AMIXER, desc->msr, mgr->mgr.hw);
  171. if (err)
  172. return err;
  173. /* Set amixer specific operations */
  174. amixer->rsc.ops = &amixer_basic_rsc_ops;
  175. amixer->ops = &amixer_ops;
  176. amixer->input = NULL;
  177. amixer->sum = NULL;
  178. amixer_setup(amixer, NULL, 0, NULL);
  179. return 0;
  180. }
  181. static int amixer_rsc_uninit(struct amixer *amixer)
  182. {
  183. amixer_setup(amixer, NULL, 0, NULL);
  184. rsc_uninit(&amixer->rsc);
  185. amixer->ops = NULL;
  186. amixer->input = NULL;
  187. amixer->sum = NULL;
  188. return 0;
  189. }
  190. static int get_amixer_rsc(struct amixer_mgr *mgr,
  191. const struct amixer_desc *desc,
  192. struct amixer **ramixer)
  193. {
  194. int err, i;
  195. unsigned int idx;
  196. struct amixer *amixer;
  197. unsigned long flags;
  198. *ramixer = NULL;
  199. /* Allocate mem for amixer resource */
  200. amixer = kzalloc(sizeof(*amixer), GFP_KERNEL);
  201. if (!amixer)
  202. return -ENOMEM;
  203. /* Check whether there are sufficient
  204. * amixer resources to meet request. */
  205. err = 0;
  206. spin_lock_irqsave(&mgr->mgr_lock, flags);
  207. for (i = 0; i < desc->msr; i++) {
  208. err = mgr_get_resource(&mgr->mgr, 1, &idx);
  209. if (err)
  210. break;
  211. amixer->idx[i] = idx;
  212. }
  213. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  214. if (err) {
  215. dev_err(mgr->card->dev,
  216. "Can't meet AMIXER resource request!\n");
  217. goto error;
  218. }
  219. err = amixer_rsc_init(amixer, desc, mgr);
  220. if (err)
  221. goto error;
  222. *ramixer = amixer;
  223. return 0;
  224. error:
  225. spin_lock_irqsave(&mgr->mgr_lock, flags);
  226. for (i--; i >= 0; i--)
  227. mgr_put_resource(&mgr->mgr, 1, amixer->idx[i]);
  228. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  229. kfree(amixer);
  230. return err;
  231. }
  232. static int put_amixer_rsc(struct amixer_mgr *mgr, struct amixer *amixer)
  233. {
  234. unsigned long flags;
  235. int i;
  236. spin_lock_irqsave(&mgr->mgr_lock, flags);
  237. for (i = 0; i < amixer->rsc.msr; i++)
  238. mgr_put_resource(&mgr->mgr, 1, amixer->idx[i]);
  239. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  240. amixer_rsc_uninit(amixer);
  241. kfree(amixer);
  242. return 0;
  243. }
  244. int amixer_mgr_create(struct hw *hw, struct amixer_mgr **ramixer_mgr)
  245. {
  246. int err;
  247. struct amixer_mgr *amixer_mgr;
  248. *ramixer_mgr = NULL;
  249. amixer_mgr = kzalloc(sizeof(*amixer_mgr), GFP_KERNEL);
  250. if (!amixer_mgr)
  251. return -ENOMEM;
  252. err = rsc_mgr_init(&amixer_mgr->mgr, AMIXER, AMIXER_RESOURCE_NUM, hw);
  253. if (err)
  254. goto error;
  255. spin_lock_init(&amixer_mgr->mgr_lock);
  256. amixer_mgr->get_amixer = get_amixer_rsc;
  257. amixer_mgr->put_amixer = put_amixer_rsc;
  258. amixer_mgr->card = hw->card;
  259. *ramixer_mgr = amixer_mgr;
  260. return 0;
  261. error:
  262. kfree(amixer_mgr);
  263. return err;
  264. }
  265. int amixer_mgr_destroy(struct amixer_mgr *amixer_mgr)
  266. {
  267. rsc_mgr_uninit(&amixer_mgr->mgr);
  268. kfree(amixer_mgr);
  269. return 0;
  270. }
  271. /* SUM resource management */
  272. static int sum_master(struct rsc *rsc)
  273. {
  274. rsc->conj = 0;
  275. return rsc->idx = container_of(rsc, struct sum, rsc)->idx[0];
  276. }
  277. static int sum_next_conj(struct rsc *rsc)
  278. {
  279. rsc->conj++;
  280. return container_of(rsc, struct sum, rsc)->idx[rsc->conj];
  281. }
  282. static int sum_index(const struct rsc *rsc)
  283. {
  284. return container_of(rsc, struct sum, rsc)->idx[rsc->conj];
  285. }
  286. static int sum_output_slot(const struct rsc *rsc)
  287. {
  288. return (sum_index(rsc) << 4) + 0xc;
  289. }
  290. static struct rsc_ops sum_basic_rsc_ops = {
  291. .master = sum_master,
  292. .next_conj = sum_next_conj,
  293. .index = sum_index,
  294. .output_slot = sum_output_slot,
  295. };
  296. static int sum_rsc_init(struct sum *sum,
  297. const struct sum_desc *desc,
  298. struct sum_mgr *mgr)
  299. {
  300. int err;
  301. err = rsc_init(&sum->rsc, sum->idx[0], SUM, desc->msr, mgr->mgr.hw);
  302. if (err)
  303. return err;
  304. sum->rsc.ops = &sum_basic_rsc_ops;
  305. return 0;
  306. }
  307. static int sum_rsc_uninit(struct sum *sum)
  308. {
  309. rsc_uninit(&sum->rsc);
  310. return 0;
  311. }
  312. static int get_sum_rsc(struct sum_mgr *mgr,
  313. const struct sum_desc *desc,
  314. struct sum **rsum)
  315. {
  316. int err, i;
  317. unsigned int idx;
  318. struct sum *sum;
  319. unsigned long flags;
  320. *rsum = NULL;
  321. /* Allocate mem for sum resource */
  322. sum = kzalloc(sizeof(*sum), GFP_KERNEL);
  323. if (!sum)
  324. return -ENOMEM;
  325. /* Check whether there are sufficient sum resources to meet request. */
  326. err = 0;
  327. spin_lock_irqsave(&mgr->mgr_lock, flags);
  328. for (i = 0; i < desc->msr; i++) {
  329. err = mgr_get_resource(&mgr->mgr, 1, &idx);
  330. if (err)
  331. break;
  332. sum->idx[i] = idx;
  333. }
  334. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  335. if (err) {
  336. dev_err(mgr->card->dev,
  337. "Can't meet SUM resource request!\n");
  338. goto error;
  339. }
  340. err = sum_rsc_init(sum, desc, mgr);
  341. if (err)
  342. goto error;
  343. *rsum = sum;
  344. return 0;
  345. error:
  346. spin_lock_irqsave(&mgr->mgr_lock, flags);
  347. for (i--; i >= 0; i--)
  348. mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
  349. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  350. kfree(sum);
  351. return err;
  352. }
  353. static int put_sum_rsc(struct sum_mgr *mgr, struct sum *sum)
  354. {
  355. unsigned long flags;
  356. int i;
  357. spin_lock_irqsave(&mgr->mgr_lock, flags);
  358. for (i = 0; i < sum->rsc.msr; i++)
  359. mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
  360. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  361. sum_rsc_uninit(sum);
  362. kfree(sum);
  363. return 0;
  364. }
  365. int sum_mgr_create(struct hw *hw, struct sum_mgr **rsum_mgr)
  366. {
  367. int err;
  368. struct sum_mgr *sum_mgr;
  369. *rsum_mgr = NULL;
  370. sum_mgr = kzalloc(sizeof(*sum_mgr), GFP_KERNEL);
  371. if (!sum_mgr)
  372. return -ENOMEM;
  373. err = rsc_mgr_init(&sum_mgr->mgr, SUM, SUM_RESOURCE_NUM, hw);
  374. if (err)
  375. goto error;
  376. spin_lock_init(&sum_mgr->mgr_lock);
  377. sum_mgr->get_sum = get_sum_rsc;
  378. sum_mgr->put_sum = put_sum_rsc;
  379. sum_mgr->card = hw->card;
  380. *rsum_mgr = sum_mgr;
  381. return 0;
  382. error:
  383. kfree(sum_mgr);
  384. return err;
  385. }
  386. int sum_mgr_destroy(struct sum_mgr *sum_mgr)
  387. {
  388. rsc_mgr_uninit(&sum_mgr->mgr);
  389. kfree(sum_mgr);
  390. return 0;
  391. }