xref: /openbmc/linux/sound/soc/codecs/adau-utils.c (revision 1c2dd16a)
1 /*
2  * Shared helper functions for devices from the ADAU family
3  *
4  * Copyright 2011-2016 Analog Devices Inc.
5  * Author: Lars-Peter Clausen <lars@metafoo.de>
6  *
7  * Licensed under the GPL-2 or later.
8  */
9 
10 #include <linux/gcd.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 
14 #include "adau-utils.h"
15 
16 int adau_calc_pll_cfg(unsigned int freq_in, unsigned int freq_out,
17 	uint8_t regs[5])
18 {
19 	unsigned int r, n, m, i, j;
20 	unsigned int div;
21 
22 	if (!freq_out) {
23 		r = 0;
24 		n = 0;
25 		m = 0;
26 		div = 0;
27 	} else {
28 		if (freq_out % freq_in != 0) {
29 			div = DIV_ROUND_UP(freq_in, 13500000);
30 			freq_in /= div;
31 			r = freq_out / freq_in;
32 			i = freq_out % freq_in;
33 			j = gcd(i, freq_in);
34 			n = i / j;
35 			m = freq_in / j;
36 			div--;
37 		} else {
38 			r = freq_out / freq_in;
39 			n = 0;
40 			m = 0;
41 			div = 0;
42 		}
43 		if (n > 0xffff || m > 0xffff || div > 3 || r > 8 || r < 2)
44 			return -EINVAL;
45 	}
46 
47 	regs[0] = m >> 8;
48 	regs[1] = m & 0xff;
49 	regs[2] = n >> 8;
50 	regs[3] = n & 0xff;
51 	regs[4] = (r << 3) | (div << 1);
52 	if (m != 0)
53 		regs[4] |= 1; /* Fractional mode */
54 
55 	return 0;
56 }
57 EXPORT_SYMBOL_GPL(adau_calc_pll_cfg);
58 
59 MODULE_DESCRIPTION("ASoC ADAU audio CODECs shared helper functions");
60 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
61 MODULE_LICENSE("GPL v2");
62