Skip to content

Commit 185ea05

Browse files
author
Stuart MacLean
committed
Add support for the HiFiBerry DAC+ Pro.
The HiFiBerry DAC+ and DAC+ Pro products both use the existing bcm sound driver with the DAC+ Pro having a special clock device driver representing the two high precision oscillators. An addition bug fix is included for the PCM512x codec where by the physical size of the sample frame is used in the calculation of the LRCK divisor as it was found to be wrong when using 24-bit depth sample contained in a little endian 4-byte sample frame.
1 parent 21c2c0a commit 185ea05

File tree

5 files changed

+396
-27
lines changed

5 files changed

+396
-27
lines changed

arch/arm/boot/dts/overlays/hifiberry-dacplus-overlay.dts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
compatible = "brcm,bcm2708";
77

88
fragment@0 {
9+
target-path = "/clocks";
10+
__overlay__ {
11+
dacpro_osc: dacpro_osc {
12+
compatible = "hifiberry,dacpro-clk";
13+
#clock-cells = <0>;
14+
};
15+
};
16+
};
17+
18+
fragment@1 {
919
target = <&sound>;
1020
__overlay__ {
1121
compatible = "hifiberry,hifiberry-dacplus";
@@ -14,14 +24,14 @@
1424
};
1525
};
1626

17-
fragment@1 {
27+
fragment@2 {
1828
target = <&i2s>;
1929
__overlay__ {
2030
status = "okay";
2131
};
2232
};
2333

24-
fragment@2 {
34+
fragment@3 {
2535
target = <&i2c1>;
2636
__overlay__ {
2737
#address-cells = <1>;
@@ -32,6 +42,7 @@
3242
#sound-dai-cells = <0>;
3343
compatible = "ti,pcm5122";
3444
reg = <0x4d>;
45+
clocks = <&dacpro_osc>;
3546
status = "okay";
3647
};
3748
};

drivers/clk/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ obj-$(CONFIG_COMMON_CLK_CDCE706) += clk-cdce706.o
2424
obj-$(CONFIG_ARCH_CLPS711X) += clk-clps711x.o
2525
obj-$(CONFIG_ARCH_EFM32) += clk-efm32gg.o
2626
obj-$(CONFIG_ARCH_HIGHBANK) += clk-highbank.o
27+
obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += clk-hifiberry-dacpro.o
2728
obj-$(CONFIG_MACH_LOONGSON1) += clk-ls1x.o
2829
obj-$(CONFIG_COMMON_CLK_MAX_GEN) += clk-max-gen.o
2930
obj-$(CONFIG_COMMON_CLK_MAX77686) += clk-max77686.o

drivers/clk/clk-hifiberry-dacpro.c

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Clock Driver for HiFiBerry DAC Pro
3+
*
4+
* Author: Stuart MacLean
5+
* Copyright 2015
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* version 2 as published by the Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* General Public License for more details.
15+
*/
16+
17+
#include <linux/clk-provider.h>
18+
#include <linux/clkdev.h>
19+
#include <linux/kernel.h>
20+
#include <linux/module.h>
21+
#include <linux/of.h>
22+
#include <linux/slab.h>
23+
#include <linux/platform_device.h>
24+
25+
/* Clock rate of CLK44EN attached to GPIO6 pin */
26+
#define CLK_44EN_RATE 22579200UL
27+
/* Clock rate of CLK48EN attached to GPIO3 pin */
28+
#define CLK_48EN_RATE 24576000UL
29+
30+
/**
31+
* struct hifiberry_dacpro_clk - Common struct to the HiFiBerry DAC Pro
32+
* @hw: clk_hw for the common clk framework
33+
* @mode: 0 => CLK44EN, 1 => CLK48EN
34+
*/
35+
struct clk_hifiberry_hw {
36+
struct clk_hw hw;
37+
uint8_t mode;
38+
};
39+
40+
#define to_hifiberry_clk(_hw) container_of(_hw, struct clk_hifiberry_hw, hw)
41+
42+
static const struct of_device_id clk_hifiberry_dacpro_dt_ids[] = {
43+
{ .compatible = "hifiberry,dacpro-clk",},
44+
{ }
45+
};
46+
MODULE_DEVICE_TABLE(of, clk_hifiberry_dacpro_dt_ids);
47+
48+
static unsigned long clk_hifiberry_dacpro_recalc_rate(struct clk_hw *hw,
49+
unsigned long parent_rate)
50+
{
51+
return (to_hifiberry_clk(hw)->mode == 0) ? CLK_44EN_RATE :
52+
CLK_48EN_RATE;
53+
}
54+
55+
static long clk_hifiberry_dacpro_round_rate(struct clk_hw *hw,
56+
unsigned long rate, unsigned long *parent_rate)
57+
{
58+
long actual_rate;
59+
60+
if (rate <= CLK_44EN_RATE) {
61+
actual_rate = (long)CLK_44EN_RATE;
62+
} else if (rate >= CLK_48EN_RATE) {
63+
actual_rate = (long)CLK_48EN_RATE;
64+
} else {
65+
long diff44Rate = (long)(rate - CLK_44EN_RATE);
66+
long diff48Rate = (long)(CLK_48EN_RATE - rate);
67+
68+
if (diff44Rate < diff48Rate)
69+
actual_rate = (long)CLK_44EN_RATE;
70+
else
71+
actual_rate = (long)CLK_48EN_RATE;
72+
}
73+
return actual_rate;
74+
}
75+
76+
77+
static int clk_hifiberry_dacpro_set_rate(struct clk_hw *hw,
78+
unsigned long rate, unsigned long parent_rate)
79+
{
80+
unsigned long actual_rate;
81+
struct clk_hifiberry_hw *clk = to_hifiberry_clk(hw);
82+
83+
actual_rate = (unsigned long)clk_hifiberry_dacpro_round_rate(hw, rate,
84+
&parent_rate);
85+
clk->mode = (actual_rate == CLK_44EN_RATE) ? 0 : 1;
86+
return 0;
87+
}
88+
89+
90+
const struct clk_ops clk_hifiberry_dacpro_rate_ops = {
91+
.recalc_rate = clk_hifiberry_dacpro_recalc_rate,
92+
.round_rate = clk_hifiberry_dacpro_round_rate,
93+
.set_rate = clk_hifiberry_dacpro_set_rate,
94+
};
95+
96+
static int clk_hifiberry_dacpro_probe(struct platform_device *pdev)
97+
{
98+
int ret;
99+
struct clk_hifiberry_hw *proclk;
100+
struct clk *clk;
101+
struct device *dev;
102+
struct clk_init_data init;
103+
104+
dev = &pdev->dev;
105+
106+
proclk = kzalloc(sizeof(struct clk_hifiberry_hw), GFP_KERNEL);
107+
if (!proclk)
108+
return -ENOMEM;
109+
110+
init.name = "clk-hifiberry-dacpro";
111+
init.ops = &clk_hifiberry_dacpro_rate_ops;
112+
init.flags = CLK_IS_ROOT | CLK_IS_BASIC;
113+
init.parent_names = NULL;
114+
init.num_parents = 0;
115+
116+
proclk->mode = 0;
117+
proclk->hw.init = &init;
118+
119+
clk = devm_clk_register(dev, &proclk->hw);
120+
if (!IS_ERR(clk)) {
121+
ret = of_clk_add_provider(dev->of_node, of_clk_src_simple_get,
122+
clk);
123+
} else {
124+
dev_err(dev, "Fail to register clock driver\n");
125+
kfree(proclk);
126+
ret = PTR_ERR(clk);
127+
}
128+
return ret;
129+
}
130+
131+
static int clk_hifiberry_dacpro_remove(struct platform_device *pdev)
132+
{
133+
of_clk_del_provider(pdev->dev.of_node);
134+
return 0;
135+
}
136+
137+
static struct platform_driver clk_hifiberry_dacpro_driver = {
138+
.probe = clk_hifiberry_dacpro_probe,
139+
.remove = clk_hifiberry_dacpro_remove,
140+
.driver = {
141+
.name = "clk-hifiberry-dacpro",
142+
.of_match_table = clk_hifiberry_dacpro_dt_ids,
143+
},
144+
};
145+
146+
static int __init clk_hifiberry_dacpro_init(void)
147+
{
148+
return platform_driver_register(&clk_hifiberry_dacpro_driver);
149+
}
150+
core_initcall(clk_hifiberry_dacpro_init);
151+
152+
static void __exit clk_hifiberry_dacpro_exit(void)
153+
{
154+
platform_driver_unregister(&clk_hifiberry_dacpro_driver);
155+
}
156+
module_exit(clk_hifiberry_dacpro_exit);
157+
158+
MODULE_DESCRIPTION("HiFiBerry DAC Pro clock driver");
159+
MODULE_LICENSE("GPL v2");
160+
MODULE_ALIAS("platform:clk-hifiberry-dacpro");

0 commit comments

Comments
 (0)