lola_clock.c 8.0 KB

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