summaryrefslogtreecommitdiffstats
path: root/sound/soc/tegra/tegra_asoc_utils.c
blob: cfe2ea890dc0fc4e77b35305f9b7e5ec2d935f6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * tegra_asoc_utils.c - Harmony machine ASoC driver
 *
 * Author: Stephen Warren <swarren@nvidia.com>
 * Copyright (C) 2010 - NVIDIA, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/clk.h>
#include <linux/err.h>
#include <linux/kernel.h>

#include "tegra_asoc_utils.h"

#define PREFIX "ASoC Tegra: "

static struct clk *clk_pll_a;
static struct clk *clk_pll_a_out0;
static struct clk *clk_cdev1;

static int set_baseclock, set_mclk;

int tegra_asoc_utils_set_rate(int srate, int mclk, int *mclk_change)
{
	int new_baseclock;
	int err;

	switch (srate) {
	case 11025:
	case 22050:
	case 44100:
	case 88200:
		new_baseclock = 56448000;
		break;
	case 8000:
	case 16000:
	case 32000:
	case 48000:
	case 64000:
	case 96000:
		new_baseclock = 73728000;
		break;
	default:
		return -EINVAL;
	}

	*mclk_change = ((new_baseclock != set_baseclock) ||
			(mclk != set_mclk));
	if (!*mclk_change)
	    return 0;

	set_baseclock = 0;
	set_mclk = 0;

	clk_disable(clk_cdev1);
	clk_disable(clk_pll_a_out0);
	clk_disable(clk_pll_a);

	err = clk_set_rate(clk_pll_a, new_baseclock);
	if (err) {
		pr_err(PREFIX "Can't set pll_a rate: %d\n", err);
		return err;
	}

	err = clk_set_rate(clk_pll_a_out0, mclk);
	if (err) {
		pr_err(PREFIX "Can't set pll_a_out0 rate: %d\n", err);
		return err;
	}

	/* Don't set cdev1 rate; its locked to pll_a_out0 */

	err = clk_enable(clk_pll_a);
	if (err) {
		pr_err(PREFIX "Can't enable pll_a: %d\n", err);
		return err;
	}

	err = clk_enable(clk_pll_a_out0);
	if (err) {
		pr_err(PREFIX "Can't enable pll_a_out0: %d\n", err);
		return err;
	}

	err = clk_enable(clk_cdev1);
	if (err) {
		pr_err(PREFIX "Can't enable cdev1: %d\n", err);
		return err;
	}

	set_baseclock = new_baseclock;
	set_mclk = mclk;

	return 0;
}

int tegra_asoc_utils_init(void)
{
	int ret;

	clk_pll_a = clk_get_sys(NULL, "pll_a");
	if (IS_ERR(clk_pll_a)) {
		pr_err(PREFIX "Can't retrieve clk pll_a\n");
		ret = PTR_ERR(clk_pll_a);
		goto err;
	}

	clk_pll_a_out0 = clk_get_sys(NULL, "pll_a_out0");
	if (IS_ERR(clk_pll_a_out0)) {
		pr_err(PREFIX "Can't retrieve clk pll_a_out0\n");
		ret = PTR_ERR(clk_pll_a_out0);
		goto err_put_pll_a;
	}

	clk_cdev1 = clk_get_sys(NULL, "cdev1");
	if (IS_ERR(clk_cdev1)) {
		pr_err(PREFIX "Can't retrieve clk cdev1\n");
		ret = PTR_ERR(clk_cdev1);
		goto err_put_pll_a_out0;
	}

	return 0;

err_put_pll_a_out0:
	clk_put(clk_pll_a_out0);
err_put_pll_a:
	clk_put(clk_pll_a);
err:
	return ret;
}

void tegra_asoc_utils_fini(void)
{
	clk_put(clk_cdev1);
	clk_put(clk_pll_a_out0);
	clk_put(clk_pll_a);
}