ctamixer.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 (NULL == 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 (NULL == 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 (NULL != input)
  104. input->ops->master(input);
  105. if (NULL != 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 (NULL != 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 (NULL != 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 (NULL != input)
  125. input->ops->master(input);
  126. if (NULL != 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 (NULL == amixer) {
  202. err = -ENOMEM;
  203. return err;
  204. }
  205. /* Check whether there are sufficient
  206. * amixer resources to meet request. */
  207. spin_lock_irqsave(&mgr->mgr_lock, flags);
  208. for (i = 0; i < desc->msr; i++) {
  209. err = mgr_get_resource(&mgr->mgr, 1, &idx);
  210. if (err)
  211. break;
  212. amixer->idx[i] = idx;
  213. }
  214. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  215. if (err) {
  216. printk(KERN_ERR "ctxfi: 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(void *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 (NULL == 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. *ramixer_mgr = amixer_mgr;
  259. return 0;
  260. error:
  261. kfree(amixer_mgr);
  262. return err;
  263. }
  264. int amixer_mgr_destroy(struct amixer_mgr *amixer_mgr)
  265. {
  266. rsc_mgr_uninit(&amixer_mgr->mgr);
  267. kfree(amixer_mgr);
  268. return 0;
  269. }
  270. /* SUM resource management */
  271. static int sum_master(struct rsc *rsc)
  272. {
  273. rsc->conj = 0;
  274. return rsc->idx = container_of(rsc, struct sum, rsc)->idx[0];
  275. }
  276. static int sum_next_conj(struct rsc *rsc)
  277. {
  278. rsc->conj++;
  279. return container_of(rsc, struct sum, rsc)->idx[rsc->conj];
  280. }
  281. static int sum_index(const struct rsc *rsc)
  282. {
  283. return container_of(rsc, struct sum, rsc)->idx[rsc->conj];
  284. }
  285. static int sum_output_slot(const struct rsc *rsc)
  286. {
  287. return (sum_index(rsc) << 4) + 0xc;
  288. }
  289. static struct rsc_ops sum_basic_rsc_ops = {
  290. .master = sum_master,
  291. .next_conj = sum_next_conj,
  292. .index = sum_index,
  293. .output_slot = sum_output_slot,
  294. };
  295. static int sum_rsc_init(struct sum *sum,
  296. const struct sum_desc *desc,
  297. struct sum_mgr *mgr)
  298. {
  299. int err;
  300. err = rsc_init(&sum->rsc, sum->idx[0], SUM, desc->msr, mgr->mgr.hw);
  301. if (err)
  302. return err;
  303. sum->rsc.ops = &sum_basic_rsc_ops;
  304. return 0;
  305. }
  306. static int sum_rsc_uninit(struct sum *sum)
  307. {
  308. rsc_uninit(&sum->rsc);
  309. return 0;
  310. }
  311. static int get_sum_rsc(struct sum_mgr *mgr,
  312. const struct sum_desc *desc,
  313. struct sum **rsum)
  314. {
  315. int err, i;
  316. unsigned int idx;
  317. struct sum *sum;
  318. unsigned long flags;
  319. *rsum = NULL;
  320. /* Allocate mem for sum resource */
  321. sum = kzalloc(sizeof(*sum), GFP_KERNEL);
  322. if (NULL == sum) {
  323. err = -ENOMEM;
  324. return err;
  325. }
  326. /* Check whether there are sufficient sum resources to meet request. */
  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. printk(KERN_ERR "ctxfi: Can't meet SUM resource request!\n");
  337. goto error;
  338. }
  339. err = sum_rsc_init(sum, desc, mgr);
  340. if (err)
  341. goto error;
  342. *rsum = sum;
  343. return 0;
  344. error:
  345. spin_lock_irqsave(&mgr->mgr_lock, flags);
  346. for (i--; i >= 0; i--)
  347. mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
  348. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  349. kfree(sum);
  350. return err;
  351. }
  352. static int put_sum_rsc(struct sum_mgr *mgr, struct sum *sum)
  353. {
  354. unsigned long flags;
  355. int i;
  356. spin_lock_irqsave(&mgr->mgr_lock, flags);
  357. for (i = 0; i < sum->rsc.msr; i++)
  358. mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
  359. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  360. sum_rsc_uninit(sum);
  361. kfree(sum);
  362. return 0;
  363. }
  364. int sum_mgr_create(void *hw, struct sum_mgr **rsum_mgr)
  365. {
  366. int err;
  367. struct sum_mgr *sum_mgr;
  368. *rsum_mgr = NULL;
  369. sum_mgr = kzalloc(sizeof(*sum_mgr), GFP_KERNEL);
  370. if (NULL == sum_mgr)
  371. return -ENOMEM;
  372. err = rsc_mgr_init(&sum_mgr->mgr, SUM, SUM_RESOURCE_NUM, hw);
  373. if (err)
  374. goto error;
  375. spin_lock_init(&sum_mgr->mgr_lock);
  376. sum_mgr->get_sum = get_sum_rsc;
  377. sum_mgr->put_sum = put_sum_rsc;
  378. *rsum_mgr = sum_mgr;
  379. return 0;
  380. error:
  381. kfree(sum_mgr);
  382. return err;
  383. }
  384. int sum_mgr_destroy(struct sum_mgr *sum_mgr)
  385. {
  386. rsc_mgr_uninit(&sum_mgr->mgr);
  387. kfree(sum_mgr);
  388. return 0;
  389. }