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