tegra_asoc_utils.c 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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>
24
#include <linux/device.h>
25 26
#include <linux/err.h>
#include <linux/kernel.h>
27
#include <linux/module.h>
28 29 30

#include "tegra_asoc_utils.h"

31
int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate,
32
			      int mclk)
33 34
{
	int new_baseclock;
35
	bool clk_change;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	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;
	}

57
	clk_change = ((new_baseclock != data->set_baseclock) ||
58
			(mclk != data->set_mclk));
59 60
	if (!clk_change)
		return 0;
61

62 63
	data->set_baseclock = 0;
	data->set_mclk = 0;
64

65 66 67
	clk_disable(data->clk_cdev1);
	clk_disable(data->clk_pll_a_out0);
	clk_disable(data->clk_pll_a);
68

69
	err = clk_set_rate(data->clk_pll_a, new_baseclock);
70
	if (err) {
71
		dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
72 73 74
		return err;
	}

75
	err = clk_set_rate(data->clk_pll_a_out0, mclk);
76
	if (err) {
77
		dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
78 79 80 81 82
		return err;
	}

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

83
	err = clk_enable(data->clk_pll_a);
84
	if (err) {
85
		dev_err(data->dev, "Can't enable pll_a: %d\n", err);
86 87 88
		return err;
	}

89
	err = clk_enable(data->clk_pll_a_out0);
90
	if (err) {
91
		dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
92 93 94
		return err;
	}

95
	err = clk_enable(data->clk_cdev1);
96
	if (err) {
97
		dev_err(data->dev, "Can't enable cdev1: %d\n", err);
98 99 100
		return err;
	}

101 102
	data->set_baseclock = new_baseclock;
	data->set_mclk = mclk;
103 104 105

	return 0;
}
106
EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
107

108 109
int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
			  struct device *dev)
110 111 112
{
	int ret;

113 114 115 116 117 118
	data->dev = dev;

	data->clk_pll_a = clk_get_sys(NULL, "pll_a");
	if (IS_ERR(data->clk_pll_a)) {
		dev_err(data->dev, "Can't retrieve clk pll_a\n");
		ret = PTR_ERR(data->clk_pll_a);
119 120 121
		goto err;
	}

122 123 124 125
	data->clk_pll_a_out0 = clk_get_sys(NULL, "pll_a_out0");
	if (IS_ERR(data->clk_pll_a_out0)) {
		dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
		ret = PTR_ERR(data->clk_pll_a_out0);
126
		goto err_put_pll_a;
127 128
	}

129 130 131 132
	data->clk_cdev1 = clk_get_sys(NULL, "cdev1");
	if (IS_ERR(data->clk_cdev1)) {
		dev_err(data->dev, "Can't retrieve clk cdev1\n");
		ret = PTR_ERR(data->clk_cdev1);
133
		goto err_put_pll_a_out0;
134 135 136 137
	}

	return 0;

138
err_put_pll_a_out0:
139
	clk_put(data->clk_pll_a_out0);
140
err_put_pll_a:
141
	clk_put(data->clk_pll_a);
142 143 144
err:
	return ret;
}
145
EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
146

147
void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data)
148
{
149 150 151
	clk_put(data->clk_cdev1);
	clk_put(data->clk_pll_a_out0);
	clk_put(data->clk_pll_a);
152
}
153
EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini);
154

155 156 157
MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
MODULE_DESCRIPTION("Tegra ASoC utility code");
MODULE_LICENSE("GPL");