lola_clock.c 7.4 KB

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