aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/clk-twl6040.c
blob: b8d1cfe5b0e1f50cd94686b331bb4cf2d66f3349 (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
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: GPL-2.0-only
/*
* TWL6040 clock module driver for OMAP4 McPDM functional clock
*
* Copyright (C) 2012 Texas Instruments Inc.
* Peter Ujfalusi <peter.ujfalusi@ti.com>
*/

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/mfd/twl6040.h>
#include <linux/clk-provider.h>

struct twl6040_pdmclk {
	struct twl6040 *twl6040;
	struct device *dev;
	struct clk_hw pdmclk_hw;
	int enabled;
};

static int twl6040_pdmclk_is_prepared(struct clk_hw *hw)
{
	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
						     pdmclk_hw);

	return pdmclk->enabled;
}

static int twl6040_pdmclk_reset_one_clock(struct twl6040_pdmclk *pdmclk,
					  unsigned int reg)
{
	const u8 reset_mask = TWL6040_HPLLRST;	/* Same for HPPLL and LPPLL */
	int ret;

	ret = twl6040_set_bits(pdmclk->twl6040, reg, reset_mask);
	if (ret < 0)
		return ret;

	ret = twl6040_clear_bits(pdmclk->twl6040, reg, reset_mask);
	if (ret < 0)
		return ret;

	return 0;
}

/*
 * TWL6040A2 Phoenix Audio IC erratum #6: "PDM Clock Generation Issue At
 * Cold Temperature". This affects cold boot and deeper idle states it
 * seems. The workaround consists of resetting HPPLL and LPPLL.
 */
static int twl6040_pdmclk_quirk_reset_clocks(struct twl6040_pdmclk *pdmclk)
{
	int ret;

	ret = twl6040_pdmclk_reset_one_clock(pdmclk, TWL6040_REG_HPPLLCTL);
	if (ret)
		return ret;

	ret = twl6040_pdmclk_reset_one_clock(pdmclk, TWL6040_REG_LPPLLCTL);
	if (ret)
		return ret;

	return 0;
}

static int twl6040_pdmclk_prepare(struct clk_hw *hw)
{
	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
						     pdmclk_hw);
	int ret;

	ret = twl6040_power(pdmclk->twl6040, 1);
	if (ret)
		return ret;

	ret = twl6040_pdmclk_quirk_reset_clocks(pdmclk);
	if (ret)
		goto out_err;

	pdmclk->enabled = 1;

	return 0;

out_err:
	dev_err(pdmclk->dev, "%s: error %i\n", __func__, ret);
	twl6040_power(pdmclk->twl6040, 0);

	return ret;
}

static void twl6040_pdmclk_unprepare(struct clk_hw *hw)
{
	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
						     pdmclk_hw);
	int ret;

	ret = twl6040_power(pdmclk->twl6040, 0);
	if (!ret)
		pdmclk->enabled = 0;

}

static unsigned long twl6040_pdmclk_recalc_rate(struct clk_hw *hw,
						unsigned long parent_rate)
{
	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
						     pdmclk_hw);

	return twl6040_get_sysclk(pdmclk->twl6040);
}

static const struct clk_ops twl6040_pdmclk_ops = {
	.is_prepared = twl6040_pdmclk_is_prepared,
	.prepare = twl6040_pdmclk_prepare,
	.unprepare = twl6040_pdmclk_unprepare,
	.recalc_rate = twl6040_pdmclk_recalc_rate,
};

static const struct clk_init_data twl6040_pdmclk_init = {
	.name = "pdmclk",
	.ops = &twl6040_pdmclk_ops,
	.flags = CLK_GET_RATE_NOCACHE,
};

static int twl6040_pdmclk_probe(struct platform_device *pdev)
{
	struct twl6040 *twl6040 = dev_get_drvdata(pdev->dev.parent);
	struct twl6040_pdmclk *clkdata;
	int ret;

	clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
	if (!clkdata)
		return -ENOMEM;

	clkdata->dev = &pdev->dev;
	clkdata->twl6040 = twl6040;

	clkdata->pdmclk_hw.init = &twl6040_pdmclk_init;
	ret = devm_clk_hw_register(&pdev->dev, &clkdata->pdmclk_hw);
	if (ret)
		return ret;

	platform_set_drvdata(pdev, clkdata);

	return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get,
					   &clkdata->pdmclk_hw);
}

static struct platform_driver twl6040_pdmclk_driver = {
	.driver = {
		.name = "twl6040-pdmclk",
	},
	.probe = twl6040_pdmclk_probe,
};

module_platform_driver(twl6040_pdmclk_driver);

MODULE_DESCRIPTION("TWL6040 clock driver for McPDM functional clock");
MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
MODULE_ALIAS("platform:twl6040-pdmclk");
MODULE_LICENSE("GPL");