1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com/
6 #include <linux/clk-provider.h>
7 #include <linux/mfd/syscon.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
12 struct ti_syscon_gate_clk_priv
{
14 struct regmap
*regmap
;
19 struct ti_syscon_gate_clk_data
{
26 ti_syscon_gate_clk_priv
*to_ti_syscon_gate_clk_priv(struct clk_hw
*hw
)
28 return container_of(hw
, struct ti_syscon_gate_clk_priv
, hw
);
31 static int ti_syscon_gate_clk_enable(struct clk_hw
*hw
)
33 struct ti_syscon_gate_clk_priv
*priv
= to_ti_syscon_gate_clk_priv(hw
);
35 return regmap_write_bits(priv
->regmap
, priv
->reg
, priv
->idx
,
39 static void ti_syscon_gate_clk_disable(struct clk_hw
*hw
)
41 struct ti_syscon_gate_clk_priv
*priv
= to_ti_syscon_gate_clk_priv(hw
);
43 regmap_write_bits(priv
->regmap
, priv
->reg
, priv
->idx
, 0);
46 static int ti_syscon_gate_clk_is_enabled(struct clk_hw
*hw
)
49 struct ti_syscon_gate_clk_priv
*priv
= to_ti_syscon_gate_clk_priv(hw
);
51 regmap_read(priv
->regmap
, priv
->reg
, &val
);
53 return !!(val
& priv
->idx
);
56 static const struct clk_ops ti_syscon_gate_clk_ops
= {
57 .enable
= ti_syscon_gate_clk_enable
,
58 .disable
= ti_syscon_gate_clk_disable
,
59 .is_enabled
= ti_syscon_gate_clk_is_enabled
,
63 *ti_syscon_gate_clk_register(struct device
*dev
, struct regmap
*regmap
,
64 const struct ti_syscon_gate_clk_data
*data
)
66 struct ti_syscon_gate_clk_priv
*priv
;
67 struct clk_init_data init
;
70 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
72 return ERR_PTR(-ENOMEM
);
74 init
.name
= data
->name
;
75 init
.ops
= &ti_syscon_gate_clk_ops
;
76 init
.parent_names
= NULL
;
80 priv
->regmap
= regmap
;
81 priv
->reg
= data
->offset
;
82 priv
->idx
= BIT(data
->bit_idx
);
83 priv
->hw
.init
= &init
;
85 ret
= devm_clk_hw_register(dev
, &priv
->hw
);
92 static int ti_syscon_gate_clk_probe(struct platform_device
*pdev
)
94 const struct ti_syscon_gate_clk_data
*data
, *p
;
95 struct clk_hw_onecell_data
*hw_data
;
96 struct device
*dev
= &pdev
->dev
;
97 struct regmap
*regmap
;
100 data
= device_get_match_data(dev
);
104 regmap
= syscon_node_to_regmap(dev
->of_node
);
105 if (IS_ERR(regmap
)) {
106 if (PTR_ERR(regmap
) == -EPROBE_DEFER
)
107 return -EPROBE_DEFER
;
108 dev_err(dev
, "failed to find parent regmap\n");
109 return PTR_ERR(regmap
);
113 for (p
= data
; p
->name
; p
++)
116 hw_data
= devm_kzalloc(dev
, struct_size(hw_data
, hws
, num_clks
),
121 hw_data
->num
= num_clks
;
123 for (i
= 0; i
< num_clks
; i
++) {
124 hw_data
->hws
[i
] = ti_syscon_gate_clk_register(dev
, regmap
,
126 if (IS_ERR(hw_data
->hws
[i
]))
127 dev_warn(dev
, "failed to register %s\n",
131 return devm_of_clk_add_hw_provider(dev
, of_clk_hw_onecell_get
,
135 #define TI_SYSCON_CLK_GATE(_name, _offset, _bit_idx) \
138 .offset = (_offset), \
139 .bit_idx = (_bit_idx), \
142 static const struct ti_syscon_gate_clk_data am654_clk_data
[] = {
143 TI_SYSCON_CLK_GATE("ehrpwm_tbclk0", 0x0, 0),
144 TI_SYSCON_CLK_GATE("ehrpwm_tbclk1", 0x4, 0),
145 TI_SYSCON_CLK_GATE("ehrpwm_tbclk2", 0x8, 0),
146 TI_SYSCON_CLK_GATE("ehrpwm_tbclk3", 0xc, 0),
147 TI_SYSCON_CLK_GATE("ehrpwm_tbclk4", 0x10, 0),
148 TI_SYSCON_CLK_GATE("ehrpwm_tbclk5", 0x14, 0),
152 static const struct of_device_id ti_syscon_gate_clk_ids
[] = {
154 .compatible
= "ti,am654-ehrpwm-tbclk",
155 .data
= &am654_clk_data
,
159 MODULE_DEVICE_TABLE(of
, ti_syscon_gate_clk_ids
);
161 static struct platform_driver ti_syscon_gate_clk_driver
= {
162 .probe
= ti_syscon_gate_clk_probe
,
164 .name
= "ti-syscon-gate-clk",
165 .of_match_table
= ti_syscon_gate_clk_ids
,
168 module_platform_driver(ti_syscon_gate_clk_driver
);
170 MODULE_AUTHOR("Vignesh Raghavendra <vigneshr@ti.com>");
171 MODULE_DESCRIPTION("Syscon backed gate-clock driver");
172 MODULE_LICENSE("GPL");