lola_clock.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Support for Digigram Lola PCI-e boards
  3. *
  4. * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include "lola.h"
  25. unsigned int lola_sample_rate_convert(unsigned int coded)
  26. {
  27. unsigned int freq;
  28. /* base frequency */
  29. switch (coded & 0x3) {
  30. case 0: freq = 48000; break;
  31. case 1: freq = 44100; break;
  32. case 2: freq = 32000; break;
  33. default: return 0; /* error */
  34. }
  35. /* multiplier / devisor */
  36. switch (coded & 0x1c) {
  37. case (0 << 2): break;
  38. case (4 << 2): break;
  39. case (1 << 2): freq *= 2; break;
  40. case (2 << 2): freq *= 4; break;
  41. case (5 << 2): freq /= 2; break;
  42. case (6 << 2): freq /= 4; break;
  43. default: return 0; /* error */
  44. }
  45. /* ajustement */
  46. switch (coded & 0x60) {
  47. case (0 << 5): break;
  48. case (1 << 5): freq = (freq * 999) / 1000; break;
  49. case (2 << 5): freq = (freq * 1001) / 1000; break;
  50. default: return 0; /* error */
  51. }
  52. return freq;
  53. }
  54. /*
  55. * Granualrity
  56. */
  57. #define LOLA_MAXFREQ_AT_GRANULARITY_MIN 48000
  58. #define LOLA_MAXFREQ_AT_GRANULARITY_BELOW_MAX 96000
  59. static bool check_gran_clock_compatibility(struct lola *chip,
  60. unsigned int val,
  61. unsigned int freq)
  62. {
  63. if (!chip->granularity)
  64. return true;
  65. if (val < LOLA_GRANULARITY_MIN || val > LOLA_GRANULARITY_MAX ||
  66. (val % LOLA_GRANULARITY_STEP) != 0)
  67. return false;
  68. if (val == LOLA_GRANULARITY_MIN) {
  69. if (freq > LOLA_MAXFREQ_AT_GRANULARITY_MIN)
  70. return false;
  71. } else if (val < LOLA_GRANULARITY_MAX) {
  72. if (freq > LOLA_MAXFREQ_AT_GRANULARITY_BELOW_MAX)
  73. return false;
  74. }
  75. return true;
  76. }
  77. int lola_set_granularity(struct lola *chip, unsigned int val, bool force)
  78. {
  79. int err;
  80. if (!force) {
  81. if (val == chip->granularity)
  82. return 0;
  83. #if 0
  84. /* change Gran only if there are no streams allocated ! */
  85. if (chip->audio_in_alloc_mask || chip->audio_out_alloc_mask)
  86. return -EBUSY;
  87. #endif
  88. if (!check_gran_clock_compatibility(chip, val,
  89. chip->clock.cur_freq))
  90. return -EINVAL;
  91. }
  92. chip->granularity = val;
  93. val /= LOLA_GRANULARITY_STEP;
  94. /* audio function group */
  95. err = lola_codec_write(chip, 1, LOLA_VERB_SET_GRANULARITY_STEPS,
  96. val, 0);
  97. if (err < 0)
  98. return err;
  99. /* this can be a very slow function !!! */
  100. usleep_range(400 * val, 20000);
  101. return lola_codec_flush(chip);
  102. }
  103. /*
  104. * Clock widget handling
  105. */
  106. int __devinit lola_init_clock_widget(struct lola *chip, int nid)
  107. {
  108. unsigned int val;
  109. int i, j, nitems, nb_verbs, idx, idx_list;
  110. int err;
  111. err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
  112. if (err < 0) {
  113. printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
  114. return err;
  115. }
  116. if ((val & 0xfff00000) != 0x01f00000) { /* test SubType and Type */
  117. snd_printdd("No valid clock widget\n");
  118. return 0;
  119. }
  120. chip->clock.nid = nid;
  121. chip->clock.items = val & 0xff;
  122. snd_printdd("clock_list nid=%x, entries=%d\n", nid,
  123. chip->clock.items);
  124. if (chip->clock.items > MAX_SAMPLE_CLOCK_COUNT) {
  125. printk(KERN_ERR SFX "CLOCK_LIST too big: %d\n",
  126. chip->clock.items);
  127. return -EINVAL;
  128. }
  129. nitems = chip->clock.items;
  130. nb_verbs = (nitems + 3) / 4;
  131. idx = 0;
  132. idx_list = 0;
  133. for (i = 0; i < nb_verbs; i++) {
  134. unsigned int res_ex;
  135. unsigned short items[4];
  136. err = lola_codec_read(chip, nid, LOLA_VERB_GET_CLOCK_LIST,
  137. idx, 0, &val, &res_ex);
  138. if (err < 0) {
  139. printk(KERN_ERR SFX "Can't read CLOCK_LIST\n");
  140. return -EINVAL;
  141. }
  142. items[0] = val & 0xfff;
  143. items[1] = (val >> 16) & 0xfff;
  144. items[2] = res_ex & 0xfff;
  145. items[3] = (res_ex >> 16) & 0xfff;
  146. for (j = 0; j < 4; j++) {
  147. unsigned char type = items[j] >> 8;
  148. unsigned int freq = items[j] & 0xff;
  149. int format = LOLA_CLOCK_FORMAT_NONE;
  150. bool add_clock = true;
  151. if (type == LOLA_CLOCK_TYPE_INTERNAL) {
  152. freq = lola_sample_rate_convert(freq);
  153. if (freq < chip->sample_rate_min)
  154. add_clock = false;
  155. else if (freq == 48000) {
  156. chip->clock.cur_index = idx_list;
  157. chip->clock.cur_freq = 48000;
  158. chip->clock.cur_valid = true;
  159. }
  160. } else if (type == LOLA_CLOCK_TYPE_VIDEO) {
  161. freq = lola_sample_rate_convert(freq);
  162. if (freq < chip->sample_rate_min)
  163. add_clock = false;
  164. /* video clock has a format (0:NTSC, 1:PAL)*/
  165. if (items[j] & 0x80)
  166. format = LOLA_CLOCK_FORMAT_NTSC;
  167. else
  168. format = LOLA_CLOCK_FORMAT_PAL;
  169. }
  170. if (add_clock) {
  171. struct lola_sample_clock *sc;
  172. sc = &chip->clock.sample_clock[idx_list];
  173. sc->type = type;
  174. sc->format = format;
  175. sc->freq = freq;
  176. /* keep the index used with the board */
  177. chip->clock.idx_lookup[idx_list] = idx;
  178. idx_list++;
  179. } else {
  180. chip->clock.items--;
  181. }
  182. if (++idx >= nitems)
  183. break;
  184. }
  185. }
  186. return 0;
  187. }
  188. /* enable unsolicited events of the clock widget */
  189. int lola_enable_clock_events(struct lola *chip)
  190. {
  191. unsigned int res;
  192. int err;
  193. err = lola_codec_read(chip, chip->clock.nid,
  194. LOLA_VERB_SET_UNSOLICITED_ENABLE,
  195. LOLA_UNSOLICITED_ENABLE | LOLA_UNSOLICITED_TAG,
  196. 0, &res, NULL);
  197. if (err < 0)
  198. return err;
  199. if (res) {
  200. printk(KERN_WARNING SFX "error in enable_clock_events %d\n",
  201. res);
  202. return -EINVAL;
  203. }
  204. return 0;
  205. }
  206. int lola_set_clock_index(struct lola *chip, unsigned int idx)
  207. {
  208. unsigned int res;
  209. int err;
  210. err = lola_codec_read(chip, chip->clock.nid,
  211. LOLA_VERB_SET_CLOCK_SELECT,
  212. chip->clock.idx_lookup[idx],
  213. 0, &res, NULL);
  214. if (err < 0)
  215. return err;
  216. if (res) {
  217. printk(KERN_WARNING SFX "error in set_clock %d\n", res);
  218. return -EINVAL;
  219. }
  220. return 0;
  221. }
  222. bool lola_update_ext_clock_freq(struct lola *chip, unsigned int val)
  223. {
  224. unsigned int tag;
  225. /* the current EXTERNAL clock information gets updated by interrupt
  226. * with an unsolicited response
  227. */
  228. if (!val)
  229. return false;
  230. tag = (val >> LOLA_UNSOL_RESP_TAG_OFFSET) & LOLA_UNSOLICITED_TAG_MASK;
  231. if (tag != LOLA_UNSOLICITED_TAG)
  232. return false;
  233. /* only for current = external clocks */
  234. if (chip->clock.sample_clock[chip->clock.cur_index].type !=
  235. LOLA_CLOCK_TYPE_INTERNAL) {
  236. chip->clock.cur_freq = lola_sample_rate_convert(val & 0x7f);
  237. chip->clock.cur_valid = (val & 0x100) != 0;
  238. }
  239. return true;
  240. }
  241. int lola_set_clock(struct lola *chip, int idx)
  242. {
  243. int freq = 0;
  244. bool valid = false;
  245. if (idx == chip->clock.cur_index) {
  246. /* current clock is allowed */
  247. freq = chip->clock.cur_freq;
  248. valid = chip->clock.cur_valid;
  249. } else if (chip->clock.sample_clock[idx].type ==
  250. LOLA_CLOCK_TYPE_INTERNAL) {
  251. /* internal clocks allowed */
  252. freq = chip->clock.sample_clock[idx].freq;
  253. valid = true;
  254. }
  255. if (!freq || !valid)
  256. return -EINVAL;
  257. if (!check_gran_clock_compatibility(chip, chip->granularity, freq))
  258. return -EINVAL;
  259. if (idx != chip->clock.cur_index) {
  260. int err = lola_set_clock_index(chip, idx);
  261. if (err < 0)
  262. return err;
  263. /* update new settings */
  264. chip->clock.cur_index = idx;
  265. chip->clock.cur_freq = freq;
  266. chip->clock.cur_valid = true;
  267. }
  268. return 0;
  269. }
  270. int lola_set_sample_rate(struct lola *chip, int rate)
  271. {
  272. int i;
  273. if (chip->clock.cur_freq == rate && chip->clock.cur_valid)
  274. return 0;
  275. /* search for new dwClockIndex */
  276. for (i = 0; i < chip->clock.items; i++) {
  277. if (chip->clock.sample_clock[i].type == LOLA_CLOCK_TYPE_INTERNAL &&
  278. chip->clock.sample_clock[i].freq == rate)
  279. break;
  280. }
  281. if (i >= chip->clock.items)
  282. return -EINVAL;
  283. return lola_set_clock(chip, i);
  284. }