ctamixer.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. printk(KERN_ERR "ctxfi: Can't meet AMIXER resource request!\n");
  216. goto error;
  217. }
  218. err = amixer_rsc_init(amixer, desc, mgr);
  219. if (err)
  220. goto error;
  221. *ramixer = amixer;
  222. return 0;
  223. error:
  224. spin_lock_irqsave(&mgr->mgr_lock, flags);
  225. for (i--; i >= 0; i--)
  226. mgr_put_resource(&mgr->mgr, 1, amixer->idx[i]);
  227. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  228. kfree(amixer);
  229. return err;
  230. }
  231. static int put_amixer_rsc(struct amixer_mgr *mgr, struct amixer *amixer)
  232. {
  233. unsigned long flags;
  234. int i;
  235. spin_lock_irqsave(&mgr->mgr_lock, flags);
  236. for (i = 0; i < amixer->rsc.msr; i++)
  237. mgr_put_resource(&mgr->mgr, 1, amixer->idx[i]);
  238. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  239. amixer_rsc_uninit(amixer);
  240. kfree(amixer);
  241. return 0;
  242. }
  243. int amixer_mgr_create(void *hw, struct amixer_mgr **ramixer_mgr)
  244. {
  245. int err;
  246. struct amixer_mgr *amixer_mgr;
  247. *ramixer_mgr = NULL;
  248. amixer_mgr = kzalloc(sizeof(*amixer_mgr), GFP_KERNEL);
  249. if (!amixer_mgr)
  250. return -ENOMEM;
  251. err = rsc_mgr_init(&amixer_mgr->mgr, AMIXER, AMIXER_RESOURCE_NUM, hw);
  252. if (err)
  253. goto error;
  254. spin_lock_init(&amixer_mgr->mgr_lock);
  255. amixer_mgr->get_amixer = get_amixer_rsc;
  256. amixer_mgr->put_amixer = put_amixer_rsc;
  257. *ramixer_mgr = amixer_mgr;
  258. return 0;
  259. error:
  260. kfree(amixer_mgr);
  261. return err;
  262. }
  263. int amixer_mgr_destroy(struct amixer_mgr *amixer_mgr)
  264. {
  265. rsc_mgr_uninit(&amixer_mgr->mgr);
  266. kfree(amixer_mgr);
  267. return 0;
  268. }
  269. /* SUM resource management */
  270. static int sum_master(struct rsc *rsc)
  271. {
  272. rsc->conj = 0;
  273. return rsc->idx = container_of(rsc, struct sum, rsc)->idx[0];
  274. }
  275. static int sum_next_conj(struct rsc *rsc)
  276. {
  277. rsc->conj++;
  278. return container_of(rsc, struct sum, rsc)->idx[rsc->conj];
  279. }
  280. static int sum_index(const struct rsc *rsc)
  281. {
  282. return container_of(rsc, struct sum, rsc)->idx[rsc->conj];
  283. }
  284. static int sum_output_slot(const struct rsc *rsc)
  285. {
  286. return (sum_index(rsc) << 4) + 0xc;
  287. }
  288. static struct rsc_ops sum_basic_rsc_ops = {
  289. .master = sum_master,
  290. .next_conj = sum_next_conj,
  291. .index = sum_index,
  292. .output_slot = sum_output_slot,
  293. };
  294. static int sum_rsc_init(struct sum *sum,
  295. const struct sum_desc *desc,
  296. struct sum_mgr *mgr)
  297. {
  298. int err;
  299. err = rsc_init(&sum->rsc, sum->idx[0], SUM, desc->msr, mgr->mgr.hw);
  300. if (err)
  301. return err;
  302. sum->rsc.ops = &sum_basic_rsc_ops;
  303. return 0;
  304. }
  305. static int sum_rsc_uninit(struct sum *sum)
  306. {
  307. rsc_uninit(&sum->rsc);
  308. return 0;
  309. }
  310. static int get_sum_rsc(struct sum_mgr *mgr,
  311. const struct sum_desc *desc,
  312. struct sum **rsum)
  313. {
  314. int err, i;
  315. unsigned int idx;
  316. struct sum *sum;
  317. unsigned long flags;
  318. *rsum = NULL;
  319. /* Allocate mem for sum resource */
  320. sum = kzalloc(sizeof(*sum), GFP_KERNEL);
  321. if (!sum)
  322. return -ENOMEM;
  323. /* Check whether there are sufficient sum resources to meet request. */
  324. err = 0;
  325. spin_lock_irqsave(&mgr->mgr_lock, flags);
  326. for (i = 0; i < desc->msr; i++) {
  327. err = mgr_get_resource(&mgr->mgr, 1, &idx);
  328. if (err)
  329. break;
  330. sum->idx[i] = idx;
  331. }
  332. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  333. if (err) {
  334. printk(KERN_ERR "ctxfi: Can't meet SUM resource request!\n");
  335. goto error;
  336. }
  337. err = sum_rsc_init(sum, desc, mgr);
  338. if (err)
  339. goto error;
  340. *rsum = sum;
  341. return 0;
  342. error:
  343. spin_lock_irqsave(&mgr->mgr_lock, flags);
  344. for (i--; i >= 0; i--)
  345. mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
  346. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  347. kfree(sum);
  348. return err;
  349. }
  350. static int put_sum_rsc(struct sum_mgr *mgr, struct sum *sum)
  351. {
  352. unsigned long flags;
  353. int i;
  354. spin_lock_irqsave(&mgr->mgr_lock, flags);
  355. for (i = 0; i < sum->rsc.msr; i++)
  356. mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
  357. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  358. sum_rsc_uninit(sum);
  359. kfree(sum);
  360. return 0;
  361. }
  362. int sum_mgr_create(void *hw, struct sum_mgr **rsum_mgr)
  363. {
  364. int err;
  365. struct sum_mgr *sum_mgr;
  366. *rsum_mgr = NULL;
  367. sum_mgr = kzalloc(sizeof(*sum_mgr), GFP_KERNEL);
  368. if (!sum_mgr)
  369. return -ENOMEM;
  370. err = rsc_mgr_init(&sum_mgr->mgr, SUM, SUM_RESOURCE_NUM, hw);
  371. if (err)
  372. goto error;
  373. spin_lock_init(&sum_mgr->mgr_lock);
  374. sum_mgr->get_sum = get_sum_rsc;
  375. sum_mgr->put_sum = put_sum_rsc;
  376. *rsum_mgr = sum_mgr;
  377. return 0;
  378. error:
  379. kfree(sum_mgr);
  380. return err;
  381. }
  382. int sum_mgr_destroy(struct sum_mgr *sum_mgr)
  383. {
  384. rsc_mgr_uninit(&sum_mgr->mgr);
  385. kfree(sum_mgr);
  386. return 0;
  387. }