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