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