Skip to content

Commit 00f1cb1

Browse files
esmilConchuOD
authored andcommitted
clk: starfive: Factor out common JH7100 and JH7110 code
The clock control registers on the StarFive JH7100 and JH7110 work identically, so factor out the code then drivers for the two SoCs can share it without depending on each other. No functional change. Tested-by: Tommaso Merciai <tomm.merciai@gmail.com> Reviewed-by: Conor Dooley <conor.dooley@microchip.com> Reviewed-by: Emil Renner Berthing <emil.renner.berthing@canonical.com> Signed-off-by: Emil Renner Berthing <kernel@esmil.dk> Signed-off-by: Hal Feng <hal.feng@starfivetech.com> Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
1 parent c61f19e commit 00f1cb1

5 files changed

Lines changed: 342 additions & 326 deletions

File tree

drivers/clk/starfive/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3+
config CLK_STARFIVE_JH71X0
4+
bool
5+
36
config CLK_STARFIVE_JH7100
47
bool "StarFive JH7100 clock support"
58
depends on ARCH_STARFIVE || COMPILE_TEST
9+
select CLK_STARFIVE_JH71X0
610
default ARCH_STARFIVE
711
help
812
Say yes here to support the clock controller on the StarFive JH7100
@@ -11,6 +15,7 @@ config CLK_STARFIVE_JH7100
1115
config CLK_STARFIVE_JH7100_AUDIO
1216
tristate "StarFive JH7100 audio clock support"
1317
depends on CLK_STARFIVE_JH7100
18+
select CLK_STARFIVE_JH71X0
1419
default m if ARCH_STARFIVE
1520
help
1621
Say Y or M here to support the audio clocks on the StarFive JH7100

drivers/clk/starfive/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0
2-
# StarFive Clock
2+
obj-$(CONFIG_CLK_STARFIVE_JH71X0) += clk-starfive-jh71x0.o
3+
34
obj-$(CONFIG_CLK_STARFIVE_JH7100) += clk-starfive-jh7100.o
45
obj-$(CONFIG_CLK_STARFIVE_JH7100_AUDIO) += clk-starfive-jh7100-audio.o

drivers/clk/starfive/clk-starfive-jh7100.c

Lines changed: 0 additions & 325 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
* Copyright (C) 2021 Emil Renner Berthing <kernel@esmil.dk>
88
*/
99

10-
#include <linux/bits.h>
1110
#include <linux/clk-provider.h>
12-
#include <linux/debugfs.h>
1311
#include <linux/device.h>
1412
#include <linux/init.h>
15-
#include <linux/io.h>
16-
#include <linux/kernel.h>
1713
#include <linux/mod_devicetable.h>
18-
#include <linux/module.h>
1914
#include <linux/platform_device.h>
2015

2116
#include <dt-bindings/clock/starfive-jh7100.h>
@@ -269,326 +264,6 @@ static const struct jh7100_clk_data jh7100_clk_data[] __initconst = {
269264
JH7100_GATE(JH7100_CLK_SYSERR_APB, "syserr_apb", 0, JH7100_CLK_APB2_BUS),
270265
};
271266

272-
static struct jh7100_clk *jh7100_clk_from(struct clk_hw *hw)
273-
{
274-
return container_of(hw, struct jh7100_clk, hw);
275-
}
276-
277-
static struct jh7100_clk_priv *jh7100_priv_from(struct jh7100_clk *clk)
278-
{
279-
return container_of(clk, struct jh7100_clk_priv, reg[clk->idx]);
280-
}
281-
282-
static u32 jh7100_clk_reg_get(struct jh7100_clk *clk)
283-
{
284-
struct jh7100_clk_priv *priv = jh7100_priv_from(clk);
285-
void __iomem *reg = priv->base + 4 * clk->idx;
286-
287-
return readl_relaxed(reg);
288-
}
289-
290-
static void jh7100_clk_reg_rmw(struct jh7100_clk *clk, u32 mask, u32 value)
291-
{
292-
struct jh7100_clk_priv *priv = jh7100_priv_from(clk);
293-
void __iomem *reg = priv->base + 4 * clk->idx;
294-
unsigned long flags;
295-
296-
spin_lock_irqsave(&priv->rmw_lock, flags);
297-
value |= readl_relaxed(reg) & ~mask;
298-
writel_relaxed(value, reg);
299-
spin_unlock_irqrestore(&priv->rmw_lock, flags);
300-
}
301-
302-
static int jh7100_clk_enable(struct clk_hw *hw)
303-
{
304-
struct jh7100_clk *clk = jh7100_clk_from(hw);
305-
306-
jh7100_clk_reg_rmw(clk, JH7100_CLK_ENABLE, JH7100_CLK_ENABLE);
307-
return 0;
308-
}
309-
310-
static void jh7100_clk_disable(struct clk_hw *hw)
311-
{
312-
struct jh7100_clk *clk = jh7100_clk_from(hw);
313-
314-
jh7100_clk_reg_rmw(clk, JH7100_CLK_ENABLE, 0);
315-
}
316-
317-
static int jh7100_clk_is_enabled(struct clk_hw *hw)
318-
{
319-
struct jh7100_clk *clk = jh7100_clk_from(hw);
320-
321-
return !!(jh7100_clk_reg_get(clk) & JH7100_CLK_ENABLE);
322-
}
323-
324-
static unsigned long jh7100_clk_recalc_rate(struct clk_hw *hw,
325-
unsigned long parent_rate)
326-
{
327-
struct jh7100_clk *clk = jh7100_clk_from(hw);
328-
u32 div = jh7100_clk_reg_get(clk) & JH7100_CLK_DIV_MASK;
329-
330-
return div ? parent_rate / div : 0;
331-
}
332-
333-
static int jh7100_clk_determine_rate(struct clk_hw *hw,
334-
struct clk_rate_request *req)
335-
{
336-
struct jh7100_clk *clk = jh7100_clk_from(hw);
337-
unsigned long parent = req->best_parent_rate;
338-
unsigned long rate = clamp(req->rate, req->min_rate, req->max_rate);
339-
unsigned long div = min_t(unsigned long, DIV_ROUND_UP(parent, rate), clk->max_div);
340-
unsigned long result = parent / div;
341-
342-
/*
343-
* we want the result clamped by min_rate and max_rate if possible:
344-
* case 1: div hits the max divider value, which means it's less than
345-
* parent / rate, so the result is greater than rate and min_rate in
346-
* particular. we can't do anything about result > max_rate because the
347-
* divider doesn't go any further.
348-
* case 2: div = DIV_ROUND_UP(parent, rate) which means the result is
349-
* always lower or equal to rate and max_rate. however the result may
350-
* turn out lower than min_rate, but then the next higher rate is fine:
351-
* div - 1 = ceil(parent / rate) - 1 < parent / rate
352-
* and thus
353-
* min_rate <= rate < parent / (div - 1)
354-
*/
355-
if (result < req->min_rate && div > 1)
356-
result = parent / (div - 1);
357-
358-
req->rate = result;
359-
return 0;
360-
}
361-
362-
static int jh7100_clk_set_rate(struct clk_hw *hw,
363-
unsigned long rate,
364-
unsigned long parent_rate)
365-
{
366-
struct jh7100_clk *clk = jh7100_clk_from(hw);
367-
unsigned long div = clamp(DIV_ROUND_CLOSEST(parent_rate, rate),
368-
1UL, (unsigned long)clk->max_div);
369-
370-
jh7100_clk_reg_rmw(clk, JH7100_CLK_DIV_MASK, div);
371-
return 0;
372-
}
373-
374-
static unsigned long jh7100_clk_frac_recalc_rate(struct clk_hw *hw,
375-
unsigned long parent_rate)
376-
{
377-
struct jh7100_clk *clk = jh7100_clk_from(hw);
378-
u32 reg = jh7100_clk_reg_get(clk);
379-
unsigned long div100 = 100 * (reg & JH7100_CLK_INT_MASK) +
380-
((reg & JH7100_CLK_FRAC_MASK) >> JH7100_CLK_FRAC_SHIFT);
381-
382-
return (div100 >= JH7100_CLK_FRAC_MIN) ? 100 * parent_rate / div100 : 0;
383-
}
384-
385-
static int jh7100_clk_frac_determine_rate(struct clk_hw *hw,
386-
struct clk_rate_request *req)
387-
{
388-
unsigned long parent100 = 100 * req->best_parent_rate;
389-
unsigned long rate = clamp(req->rate, req->min_rate, req->max_rate);
390-
unsigned long div100 = clamp(DIV_ROUND_CLOSEST(parent100, rate),
391-
JH7100_CLK_FRAC_MIN, JH7100_CLK_FRAC_MAX);
392-
unsigned long result = parent100 / div100;
393-
394-
/* clamp the result as in jh7100_clk_determine_rate() above */
395-
if (result > req->max_rate && div100 < JH7100_CLK_FRAC_MAX)
396-
result = parent100 / (div100 + 1);
397-
if (result < req->min_rate && div100 > JH7100_CLK_FRAC_MIN)
398-
result = parent100 / (div100 - 1);
399-
400-
req->rate = result;
401-
return 0;
402-
}
403-
404-
static int jh7100_clk_frac_set_rate(struct clk_hw *hw,
405-
unsigned long rate,
406-
unsigned long parent_rate)
407-
{
408-
struct jh7100_clk *clk = jh7100_clk_from(hw);
409-
unsigned long div100 = clamp(DIV_ROUND_CLOSEST(100 * parent_rate, rate),
410-
JH7100_CLK_FRAC_MIN, JH7100_CLK_FRAC_MAX);
411-
u32 value = ((div100 % 100) << JH7100_CLK_FRAC_SHIFT) | (div100 / 100);
412-
413-
jh7100_clk_reg_rmw(clk, JH7100_CLK_DIV_MASK, value);
414-
return 0;
415-
}
416-
417-
static u8 jh7100_clk_get_parent(struct clk_hw *hw)
418-
{
419-
struct jh7100_clk *clk = jh7100_clk_from(hw);
420-
u32 value = jh7100_clk_reg_get(clk);
421-
422-
return (value & JH7100_CLK_MUX_MASK) >> JH7100_CLK_MUX_SHIFT;
423-
}
424-
425-
static int jh7100_clk_set_parent(struct clk_hw *hw, u8 index)
426-
{
427-
struct jh7100_clk *clk = jh7100_clk_from(hw);
428-
u32 value = (u32)index << JH7100_CLK_MUX_SHIFT;
429-
430-
jh7100_clk_reg_rmw(clk, JH7100_CLK_MUX_MASK, value);
431-
return 0;
432-
}
433-
434-
static int jh7100_clk_mux_determine_rate(struct clk_hw *hw,
435-
struct clk_rate_request *req)
436-
{
437-
return clk_mux_determine_rate_flags(hw, req, 0);
438-
}
439-
440-
static int jh7100_clk_get_phase(struct clk_hw *hw)
441-
{
442-
struct jh7100_clk *clk = jh7100_clk_from(hw);
443-
u32 value = jh7100_clk_reg_get(clk);
444-
445-
return (value & JH7100_CLK_INVERT) ? 180 : 0;
446-
}
447-
448-
static int jh7100_clk_set_phase(struct clk_hw *hw, int degrees)
449-
{
450-
struct jh7100_clk *clk = jh7100_clk_from(hw);
451-
u32 value;
452-
453-
if (degrees == 0)
454-
value = 0;
455-
else if (degrees == 180)
456-
value = JH7100_CLK_INVERT;
457-
else
458-
return -EINVAL;
459-
460-
jh7100_clk_reg_rmw(clk, JH7100_CLK_INVERT, value);
461-
return 0;
462-
}
463-
464-
#ifdef CONFIG_DEBUG_FS
465-
static void jh7100_clk_debug_init(struct clk_hw *hw, struct dentry *dentry)
466-
{
467-
static const struct debugfs_reg32 jh7100_clk_reg = {
468-
.name = "CTRL",
469-
.offset = 0,
470-
};
471-
struct jh7100_clk *clk = jh7100_clk_from(hw);
472-
struct jh7100_clk_priv *priv = jh7100_priv_from(clk);
473-
struct debugfs_regset32 *regset;
474-
475-
regset = devm_kzalloc(priv->dev, sizeof(*regset), GFP_KERNEL);
476-
if (!regset)
477-
return;
478-
479-
regset->regs = &jh7100_clk_reg;
480-
regset->nregs = 1;
481-
regset->base = priv->base + 4 * clk->idx;
482-
483-
debugfs_create_regset32("registers", 0400, dentry, regset);
484-
}
485-
#else
486-
#define jh7100_clk_debug_init NULL
487-
#endif
488-
489-
static const struct clk_ops jh7100_clk_gate_ops = {
490-
.enable = jh7100_clk_enable,
491-
.disable = jh7100_clk_disable,
492-
.is_enabled = jh7100_clk_is_enabled,
493-
.debug_init = jh7100_clk_debug_init,
494-
};
495-
496-
static const struct clk_ops jh7100_clk_div_ops = {
497-
.recalc_rate = jh7100_clk_recalc_rate,
498-
.determine_rate = jh7100_clk_determine_rate,
499-
.set_rate = jh7100_clk_set_rate,
500-
.debug_init = jh7100_clk_debug_init,
501-
};
502-
503-
static const struct clk_ops jh7100_clk_fdiv_ops = {
504-
.recalc_rate = jh7100_clk_frac_recalc_rate,
505-
.determine_rate = jh7100_clk_frac_determine_rate,
506-
.set_rate = jh7100_clk_frac_set_rate,
507-
.debug_init = jh7100_clk_debug_init,
508-
};
509-
510-
static const struct clk_ops jh7100_clk_gdiv_ops = {
511-
.enable = jh7100_clk_enable,
512-
.disable = jh7100_clk_disable,
513-
.is_enabled = jh7100_clk_is_enabled,
514-
.recalc_rate = jh7100_clk_recalc_rate,
515-
.determine_rate = jh7100_clk_determine_rate,
516-
.set_rate = jh7100_clk_set_rate,
517-
.debug_init = jh7100_clk_debug_init,
518-
};
519-
520-
static const struct clk_ops jh7100_clk_mux_ops = {
521-
.determine_rate = jh7100_clk_mux_determine_rate,
522-
.set_parent = jh7100_clk_set_parent,
523-
.get_parent = jh7100_clk_get_parent,
524-
.debug_init = jh7100_clk_debug_init,
525-
};
526-
527-
static const struct clk_ops jh7100_clk_gmux_ops = {
528-
.enable = jh7100_clk_enable,
529-
.disable = jh7100_clk_disable,
530-
.is_enabled = jh7100_clk_is_enabled,
531-
.determine_rate = jh7100_clk_mux_determine_rate,
532-
.set_parent = jh7100_clk_set_parent,
533-
.get_parent = jh7100_clk_get_parent,
534-
.debug_init = jh7100_clk_debug_init,
535-
};
536-
537-
static const struct clk_ops jh7100_clk_mdiv_ops = {
538-
.recalc_rate = jh7100_clk_recalc_rate,
539-
.determine_rate = jh7100_clk_determine_rate,
540-
.get_parent = jh7100_clk_get_parent,
541-
.set_parent = jh7100_clk_set_parent,
542-
.set_rate = jh7100_clk_set_rate,
543-
.debug_init = jh7100_clk_debug_init,
544-
};
545-
546-
static const struct clk_ops jh7100_clk_gmd_ops = {
547-
.enable = jh7100_clk_enable,
548-
.disable = jh7100_clk_disable,
549-
.is_enabled = jh7100_clk_is_enabled,
550-
.recalc_rate = jh7100_clk_recalc_rate,
551-
.determine_rate = jh7100_clk_determine_rate,
552-
.get_parent = jh7100_clk_get_parent,
553-
.set_parent = jh7100_clk_set_parent,
554-
.set_rate = jh7100_clk_set_rate,
555-
.debug_init = jh7100_clk_debug_init,
556-
};
557-
558-
static const struct clk_ops jh7100_clk_inv_ops = {
559-
.get_phase = jh7100_clk_get_phase,
560-
.set_phase = jh7100_clk_set_phase,
561-
.debug_init = jh7100_clk_debug_init,
562-
};
563-
564-
const struct clk_ops *starfive_jh7100_clk_ops(u32 max)
565-
{
566-
if (max & JH7100_CLK_DIV_MASK) {
567-
if (max & JH7100_CLK_MUX_MASK) {
568-
if (max & JH7100_CLK_ENABLE)
569-
return &jh7100_clk_gmd_ops;
570-
return &jh7100_clk_mdiv_ops;
571-
}
572-
if (max & JH7100_CLK_ENABLE)
573-
return &jh7100_clk_gdiv_ops;
574-
if (max == JH7100_CLK_FRAC_MAX)
575-
return &jh7100_clk_fdiv_ops;
576-
return &jh7100_clk_div_ops;
577-
}
578-
579-
if (max & JH7100_CLK_MUX_MASK) {
580-
if (max & JH7100_CLK_ENABLE)
581-
return &jh7100_clk_gmux_ops;
582-
return &jh7100_clk_mux_ops;
583-
}
584-
585-
if (max & JH7100_CLK_ENABLE)
586-
return &jh7100_clk_gate_ops;
587-
588-
return &jh7100_clk_inv_ops;
589-
}
590-
EXPORT_SYMBOL_GPL(starfive_jh7100_clk_ops);
591-
592267
static struct clk_hw *jh7100_clk_get(struct of_phandle_args *clkspec, void *data)
593268
{
594269
struct jh7100_clk_priv *priv = data;

drivers/clk/starfive/clk-starfive-jh7100.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <linux/bits.h>
66
#include <linux/clk-provider.h>
7+
#include <linux/device.h>
8+
#include <linux/spinlock.h>
79

810
/* register fields */
911
#define JH7100_CLK_ENABLE BIT(31)

0 commit comments

Comments
 (0)