Repository navigation
adsp: add PCG driver - #3706
adsp: add PCG driver#3706sipraga wants to merge 9 commits into
Conversation
c5ddd65 to
95902da
Compare
nunojsa
left a comment
There was a problem hiding this comment.
Looks good! Nor much to say
| extN is an external input clock for the Nth PCG unit of this DAI, routed | ||
| from a DAI pin to PCG0_EXTCLKx_I via the SRU, where the units are A, B, | ||
| E, F on DAI0 and C, D, G, H on DAI1. Inputs which are not connected or not | ||
| used are given as 0. |
There was a problem hiding this comment.
What do you mean, "are given as 0"?
There was a problem hiding this comment.
It's the clocks entry which should be <0>, I'll clarify.
There was a problem hiding this comment.
clarified, have another look
|
|
||
| info = match->data; | ||
| if (adev->id >= info->num_dais) | ||
| return -EINVAL; |
There was a problem hiding this comment.
hm! thanks for the tip. maybe I can dispose of this "manual" matching I was doing for aux devices. let me play around with it :D
There was a problem hiding this comment.
Eh, auxiliary bus strikes again...
device_get_match_data
-> of_fwnode_device_get_match_data
-> of_device_get_match_data
-> of_match_device(dev->driver->of_match_table, dev)
const struct of_device_id *of_match_device(const struct of_device_id *matches,
const struct device *dev)
{
if (!matches || !dev->of_node || dev->of_node_reused) <-- !!
return NULL;
return of_match_node(matches, dev->of_node);
}
Above returns NULL if of_node_reused.
and dev->of_node_reused gets set to true by auxiliary bus:
struct auxiliary_device *auxiliary_device_create(struct device *dev,
const char *modname,
const char *devname,
void *platform_data,
int id)
{
struct auxiliary_device *auxdev;
...
device_set_of_node_from_dev(&auxdev->dev, dev);
}
void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
{
of_node_put(dev->of_node);
dev->of_node = of_node_get(dev2->of_node);
dev->of_node_reused = true; <-- !!
}
I was hoping I could do something like:
static const struct of_device_id adsp_pcg_of_match[] = {
{ .compatible = "adi,adsp-sc573-dai", .data = &adsp_sc573_pcg_info },
{ .compatible = "adi,adsp-sc589-dai", .data = &adsp_sc589_pcg_info },
{ .compatible = "adi,adsp-sc598-dai", .data = &adsp_sc598_pcg_info },
{ .compatible = "adi,adsp-sc846-dai", .data = &adsp_sc846_pcg_info },
{ }
};
static const struct auxiliary_device_id adsp_pcg_id_table[] = {
{ .name = "adsp_dai.pcg" },
{ }
};
MODULE_DEVICE_TABLE(auxiliary, adsp_pcg_id_table);
static struct auxiliary_driver adsp_pcg_driver = {
.probe = adsp_pcg_probe,
.id_table = adsp_pcg_id_table,
.driver = {
.of_match_table = adsp_pcg_of_match, <-- !!
},
};
module_auxiliary_driver(adsp_pcg_driver);
which is a bit cheating, but would've been nice.
The shared of node check is from this patch:
commit 9b22c17a3cc5f61b195da624cbb48634b4e42055
Author: Rob Herring <robh@kernel.org>
Date: Tue Jan 18 11:34:04 2022 -0600
of: Check 'of_node_reused' flag on of_match_device()
Commit 0f153a1b8193 ("usb: chipidea: Set the DT node on the child
device") caused the child device to match on the parent driver
instead of the child's driver since the child's DT node pointer matched.
The worst case result is a loop of the parent driver probing another
instance and creating yet another child device eventually exhausting the
stack. If the child driver happens to match first, then everything works
fine.
A device sharing the DT node should never do DT based driver matching,
so let's simply check of_node_reused in of_match_device() to prevent
that.
Fixes: 0f153a1b8193 ("usb: chipidea: Set the DT node on the child device")
Link: https://lore.kernel.org/all/20220114105620.GK18506@ediswmail.ad.cirrus.com/
I get the rationale but actually it doesn't apply for auxiliary bus, since the driver already matched and probed on an auxiliary device table. So maybe there's some improvement that can be made. But since this is not yet going upstream I would rather revisit it later.
What do you think?
There was a problem hiding this comment.
An alternative would be to put this soc data in adsp-dai.c, but it seems like a layering violation IMO
There was a problem hiding this comment.
Ahh crap!! I'm also better understanding what's going on... I don't really like much of what's being done in here tbh and I have vague memory about discussing this already. IMO, and assuming the driver creating the auxdevs have the same compatibles, we should get this kind of info with dev_get_platdata() (or at the least a magic/key/id to decide what to use in the auxdriver). Using of_id like this already seems to be like a violation tbh :)
But probably the above now would need more changes, not sure. So I won't be to grumpy over this
There was a problem hiding this comment.
there's precedent for what I'm doing upstream, I got the idea from /home/alvin/projects/vendor/linux-next/drivers/power/supply/qcom_battmgr.c and I see pinctrl-eyeq5.c does it too (although there he's stashing it in .driver.of_match_table, which I could also do to avoid a forward declaration)
I agree it's a bit a bit crude though
There was a problem hiding this comment.
@nunojsa I think this looks better, what say you?
diff --git a/drivers/clk/adi/clk-adsp-pcg.c b/drivers/clk/adi/clk-adsp-pcg.c
index 4722b41bccf0..a7920253a9c9 100644
--- a/drivers/clk/adi/clk-adsp-pcg.c
+++ b/drivers/clk/adi/clk-adsp-pcg.c
@@ -87,6 +87,42 @@
#define PCG_SYNC_CLK_FP1CLKSEL BIT(12)
#define PCG_SYNC_FS_FP1CLKSEL BIT(13)
+#define ADSP_PCG_MAX_DAIS 2
+
+/**
+ * struct adsp_pcg_unit_desc - static description of a PCG unit
+ *
+ * @ctl0: offset of the PCG_CTLx0 register for this unit
+ * @ctl1: offset of the PCG_CTLx1 register for this unit
+ * @sync: offset of the PCG_SYNCn register for this unit (shared among 2 units)
+ * @shift: bit offset of this unit's fields in PCG_SYNCn
+ * @letters: HRM letter of the unit on DAI0 and DAI1 (i.e. PCGA, PCGB, ...)
+ */
+struct adsp_pcg_unit_desc {
+ unsigned int ctl0;
+ unsigned int ctl1;
+ unsigned int sync;
+ unsigned int shift;
+ char letters[ADSP_PCG_MAX_DAIS];
+};
+
+#define ADSP_PCG_UNIT(_ctl0, _ctl1, _sync, _shift, _dai0, _dai1) \
+ { \
+ .ctl0 = _ctl0, \
+ .ctl1 = _ctl1, \
+ .sync = _sync, \
+ .shift = _shift, \
+ .letters = { _dai0, _dai1 }, \
+ }
+
+static const struct adsp_pcg_unit_desc adsp_pcg_unit_descs[] = {
+ /* ctl0 ctl1 sync shift DAI0 DAI1 */
+ ADSP_PCG_UNIT(0x300, 0x304, 0x314, 0, 'a', 'c'),
+ ADSP_PCG_UNIT(0x308, 0x30c, 0x314, 16, 'b', 'd'),
+ ADSP_PCG_UNIT(0x318, 0x31c, 0x32c, 0, 'e', 'g'),
+ ADSP_PCG_UNIT(0x320, 0x324, 0x32c, 16, 'f', 'h'),
+};
+
/*
* Possible clock parents for a PCG unit. SYS_CLKIN is for SoCs where the CLKIN
* of a PCG is fixed.
@@ -115,25 +151,6 @@ static const char *const adsp_pcg_src_names[ADSP_PCG_SRC_NUM] = {
[ADSP_PCG_SRC_FRACN1] = "fracnpll1",
};
-#define ADSP_PCG_MAX_DAIS 2
-
-/**
- * struct adsp_pcg_unit_desc - static description of a PCG unit
- *
- * @ctl0: offset of the PCG_CTLx0 register for this unit
- * @ctl1: offset of the PCG_CTLx1 register for this unit
- * @sync: offset of the PCG_SYNCn register for this unit (shared among 2 units)
- * @shift: bit offset of this unit's fields in PCG_SYNCn
- * @letters: HRM letter of the unit on DAI0 and DAI1 (i.e. PCGA, PCGB, ...)
- */
-struct adsp_pcg_unit_desc {
- unsigned int ctl0;
- unsigned int ctl1;
- unsigned int sync;
- unsigned int shift;
- char letters[ADSP_PCG_MAX_DAIS];
-};
-
/**
* struct adsp_pcg_info - per-SoC PCG information
*
@@ -649,9 +666,6 @@ static struct clk_hw *adsp_pcg_of_clk_get(struct of_phandle_args *clkspec,
}
}
-static const struct adsp_pcg_unit_desc adsp_pcg_unit_descs[];
-static const struct of_device_id adsp_pcg_of_match[];
-
static int adsp_pcg_probe(struct auxiliary_device *adev,
const struct auxiliary_device_id *id)
{
@@ -661,7 +675,7 @@ static int adsp_pcg_probe(struct auxiliary_device *adev,
struct adsp_pcg *pcg;
int ret;
- match = of_match_node(adsp_pcg_of_match, dev->of_node);
+ match = of_match_node(dev->driver->of_match_table, dev->of_node);
if (!match)
return -ENODEV;
@@ -698,23 +712,6 @@ static int adsp_pcg_probe(struct auxiliary_device *adev,
return devm_of_clk_add_hw_provider(dev, adsp_pcg_of_clk_get, pcg);
}
-#define ADSP_PCG_UNIT(_ctl0, _ctl1, _sync, _shift, _dai0, _dai1) \
- { \
- .ctl0 = _ctl0, \
- .ctl1 = _ctl1, \
- .sync = _sync, \
- .shift = _shift, \
- .letters = { _dai0, _dai1 }, \
- }
-
-static const struct adsp_pcg_unit_desc adsp_pcg_unit_descs[] = {
- /* ctl0 ctl1 sync shift DAI0 DAI1 */
- ADSP_PCG_UNIT(0x300, 0x304, 0x314, 0, 'a', 'c'),
- ADSP_PCG_UNIT(0x308, 0x30c, 0x314, 16, 'b', 'd'),
- ADSP_PCG_UNIT(0x318, 0x31c, 0x32c, 0, 'e', 'g'),
- ADSP_PCG_UNIT(0x320, 0x324, 0x32c, 16, 'f', 'h'),
-};
-
static const enum adsp_pcg_src adsp_sc573_pcg_srcs[] = {
ADSP_PCG_SRC_SYS_CLKIN0,
ADSP_PCG_SRC_SYS_CLKIN1,
@@ -790,6 +787,9 @@ MODULE_DEVICE_TABLE(auxiliary, adsp_pcg_id_table);
static struct auxiliary_driver adsp_pcg_driver = {
.probe = adsp_pcg_probe,
.id_table = adsp_pcg_id_table,
+ .driver = {
+ .of_match_table = adsp_pcg_of_match,
+ },
};
module_auxiliary_driver(adsp_pcg_driver);
| } | ||
|
|
||
| static const struct adsp_pcg_unit_desc adsp_pcg_unit_descs[]; | ||
| static const struct of_device_id adsp_pcg_of_match[]; |
There was a problem hiding this comment.
Do we need the above? (naturally assuming we move from of_match_node())
There was a problem hiding this comment.
I could put the match table and SoC data above the probe function (perhaps before all the function definitions of the driver). But I thought this was preferable, since we normally look for such information at the bottom. Thoughts?
| * PCGs are alphabetically named, the DT bindings expect a numerical | ||
| * "ext1" for DAI0's PCGB unit. | ||
| */ | ||
| snprintf(ext_name, sizeof(ext_name), "ext%u", unit->index); |
There was a problem hiding this comment.
We could support/honor clock-output-names
There was a problem hiding this comment.
What do you mean? These "extN"s are inputs to the PCG.
There was a problem hiding this comment.
Ahh yes! I just blindly looked but the comment still holds for the clocks we register! Not a big deal anyways
There was a problem hiding this comment.
I was once told by Stephen not to use clock-output-names, see https://lore.kernel.org/all/8e6fcde41671b0a1e8365214e6df4ec2.sboyd@kernel.org/
|
|
||
| /* | ||
| * Frame sync (FS) | ||
| */ |
There was a problem hiding this comment.
Seems to have more blank lines than needed
There was a problem hiding this comment.
I meant it to be a bit "fatter" so as to divide the file into sections
|
|
||
| /* | ||
| * Bit clock (CLK) | ||
| */ |
There was a problem hiding this comment.
But seems to be style choice :)
The DAI contains Precision Clock Generator (PCG) units which generate serial audio bit clocks and frame syncs. Make the DAI a clock provider with one clock cell, and add a header with clock IDs for the bit clock, frame sync and input source of each PCG unit. The units are named by their letter in the hardware reference manual, A to H, and each DAI only provides the clocks of its own units. Besides sclk, the PCG units can be clocked from the SYS_CLKIN pins, an external clock routed to the unit via the SRU, and (on SC84x) the fractional-N PLLs. Describe these as clock inputs in a fixed order per SoC, where inputs which are not connected are given as 0 and trailing ones may be omitted. As both DAIs of a SoC share a compatible, the external inputs are named by the index of the PCG unit within the DAI rather than by its letter. Also add missing copyright license message. Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
The Digital Audio Interfaces (DAIs) found on Analog Devices ADSP SoCs contain Precision Clock Generator (PCG) units, each of which generates a bit clock (CLK) and a frame sync (FS) for serial audio. Add a clock driver for them. It binds to the pcg auxiliary device of a DAI, accesses the PCG registers through the DAI regmap, and provides the clocks of the units of that DAI, named by their letter in the hardware reference manual. Each unit is modelled as a source mux, a gate, and two sibling dividers for CLK and FS. The mux programs the CLK and FS source selection together, and the gate sets CLKEN and FSEN in a single register write, as the hardware requires for the two outputs to be synchronized. The gate is implemented with prepare/unprepare, so that the clk core's prepare lock serializes all register accesses. As the clk core does not protect a divider against rate changes while only its sibling is prepared, the dividers refuse rate changes while the unit is running. The selectable sources are described per SoC: SYS_CLKIN, which is fixed per DAI on the SC58x, SCLK0, an external input per unit routed via the SRU, and on the SC84x the fractional-N PLLs. Enable the driver by default along with the DAI driver, so that defconfigs need not be updated. Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
Create a pcg auxiliary device for each DAI alongside the sru one, so that the PCG clock driver can bind to it and provide the clocks of the DAI's PCG units. Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
Make the DAI a clock provider for the clocks of its PCG units, and describe the SYS_CLKIN0 and SYS_CLKIN1 inputs of the PCG units. Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
Make the DAIs clock providers for the clocks of their PCG units, and describe the SYS_CLKIN input of the PCG units. On the SC58x, DAIn is hardwired to SYS_CLKINn. Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
Make the DAIs clock providers for the clocks of their PCG units, and describe the SYS_CLKIN0 and SYS_CLKIN1 inputs of the PCG units. Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
Make the DAIs clock providers for the clocks of their PCG units, and describe the SYS_CLKIN0 and SYS_CLKIN1 inputs of the PCG units. Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
Make the DAIs clock providers for the clocks of their PCG units, and describe the SYS_CLKIN0 and SYS_CLKIN1 inputs of the PCG units. The fractional-N PLL inputs are left out for now, since they aren't described in the devicetree. Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
Not sure how it became 1.1, but everything else in-tree uses 1.2. Fix. Also add copyright header. Fixes: 03a8e02 ("ASoC: dt-bindings: document Analog Devices ADSP SPORT") Signed-off-by: Alvin Šipraga <alvin.sipraga@analog.com>
95902da to
4922763
Compare
|
Only change: per Nuno's comment |
| @@ -0,0 +1,36 @@ | |||
| /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ | |||
| /* | |||
| * Clock IDs for the ADSP Precision Clock Generator (PCG) | |||
There was a problem hiding this comment.
Would change this to Device tree binding constants for the ADSP Precision Clock Generator (PCG) (as seen in many other upstream drivers).
| if (src == ADSP_PCG_SRC_EXT) | ||
| src_parents[i].fw_name = ext_name; | ||
| else | ||
| src_parents[i].fw_name = adsp_pcg_src_names[src]; |
There was a problem hiding this comment.
the fracN clocks aren't valid clocks right? (since we don't have a fracn-pll driver and so they aren't registered with CCF) so CCF basically thinks the parent clock is a non-existent clock (also none of the DTs currently have those fracpll clocks in clock-names) so why is this here right now? I think it might better to add those once we actually have the frac-N PLL driver in tbh.
Edit: I think the ext clock has the same issue?
| if (ret) | ||
| return ret; | ||
|
|
||
| ret = adsp_pcg_hw_register(dev, &unit->fs, &fs_init, unit->letter, |
There was a problem hiding this comment.
maybe return adsp_pcg_hw_register(...);? since it returns 0 on success anyways
| if (ret) | ||
| return ret; | ||
|
|
||
| return 0; |
There was a problem hiding this comment.
maybe return regmap_update_bits(...);, i think theres a few other places where this is applicable (adsp_pcg_fs_set_rate, adsp_pcg_clk_set_rate, etc.)
| * of a PCG is fixed. | ||
| */ | ||
| enum adsp_pcg_src { | ||
| ADSP_PCG_SRC_SYS_CLKIN, |
There was a problem hiding this comment.
ADSP_PCG_SRC_SYS_CLKIN
So on some SoCs each PCG can select between SYS_CLKIN0 and SYS_CLKIN1, but on SC589, SYS_CLKIN0 is a fixed input for the PCGs in DAI0 and SYS_CLKIN1 for the PCGs in DAI1? If that's the case, could we maybe elaborate a bit in the comment above the enum? Something like:
"SYS_CLKIN is used on SoCs where each PCG is hardwired to either SYS_CLKIN0 or SYS_CLKIN1, rather than being able to select between them."
I was initially a bit confused seeing SYS_CLKIN0/SYS_CLKIN1 alongside an unnumbered SYS_CLKIN =P
| /** | ||
| * struct adsp_pcg_info - per-SoC PCG information | ||
| * | ||
| * @num_units: number of units per PCG |
There was a problem hiding this comment.
units per PCG? shouldn't it be units per DAI? since a unit is a single instance of a PCG inside the DAI iiuc?
from the HRM for example:
There are four PCG units per DAI
|
|
||
| sync >>= unit->desc->shift; | ||
|
|
||
| for (u8 i = 0; i < info->num_srcs; i++) { |
There was a problem hiding this comment.
Within the clock callbacks we could replace uses of info->num_srcs with clk_hw_get_num_parents(hw). I generally prefer using existing CCF APIs wherever possible, and it makes it a tad easier to realise "ah, so he's iterating over the parents" when reading the call name, but your call
| } | ||
|
|
||
| err: | ||
| return info->num_srcs; |
There was a problem hiding this comment.
on the above, return clk_hw_get_num_parents(hw); for example. I did something very similar in my CDU driver, and there's upstream precedent for using this helper in clock callbacks too.
| }; | ||
|
|
||
| /* | ||
| * Gate |
There was a problem hiding this comment.
not really a fan of these small separator-like comments, I would drop personally. The rest of the kernel-doc I like a lot since they provide a bunch of context.
| enum adsp_pcg_src src = info->srcs[i]; | ||
|
|
||
| if (src == ADSP_PCG_SRC_EXT) | ||
| src_parents[i].fw_name = ext_name; |
There was a problem hiding this comment.
Right now you're using .fw_name, which is fine, but I wonder if .index might be cleaner? If we order the srcs[] arrays to match the ordering of the clocks property in DT, with ADSP_PCG_SRC_EXT last, then we could do something like the following in adsp_pcg_register_unit:
for (i = 0; i < info->num_srcs; i++)
src_parents[i].index = i;
src_parents[info->num_srcs - 1].index += unit->index;
The final += just adds the index from the loop in probe, so the EXT clock doesn't overlap with any other ext clock. Then I guess we could drop adsp_pcg_src_names[], and the snprintf() altogether. what do you think?
The DAIs contain a PCG block, consisting of multiple PCG units, each of which can generate a synchronous CLK/FS (bitclock/fsync) pair. I needed a driver for this so that I could have the chip generate an FSYNC independently of the audio state, which the A2B driver requires.
See the commits for more details.