0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/clk-provider.h>
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/mfd/rk808.h>
0015 #include <linux/i2c.h>
0016
0017 struct rk808_clkout {
0018 struct rk808 *rk808;
0019 struct clk_hw clkout1_hw;
0020 struct clk_hw clkout2_hw;
0021 };
0022
0023 static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
0024 unsigned long parent_rate)
0025 {
0026 return 32768;
0027 }
0028
0029 static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
0030 {
0031 struct rk808_clkout *rk808_clkout = container_of(hw,
0032 struct rk808_clkout,
0033 clkout2_hw);
0034 struct rk808 *rk808 = rk808_clkout->rk808;
0035
0036 return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
0037 CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
0038 }
0039
0040 static int rk808_clkout2_prepare(struct clk_hw *hw)
0041 {
0042 return rk808_clkout2_enable(hw, true);
0043 }
0044
0045 static void rk808_clkout2_unprepare(struct clk_hw *hw)
0046 {
0047 rk808_clkout2_enable(hw, false);
0048 }
0049
0050 static int rk808_clkout2_is_prepared(struct clk_hw *hw)
0051 {
0052 struct rk808_clkout *rk808_clkout = container_of(hw,
0053 struct rk808_clkout,
0054 clkout2_hw);
0055 struct rk808 *rk808 = rk808_clkout->rk808;
0056 uint32_t val;
0057
0058 int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
0059
0060 if (ret < 0)
0061 return ret;
0062
0063 return (val & CLK32KOUT2_EN) ? 1 : 0;
0064 }
0065
0066 static const struct clk_ops rk808_clkout1_ops = {
0067 .recalc_rate = rk808_clkout_recalc_rate,
0068 };
0069
0070 static const struct clk_ops rk808_clkout2_ops = {
0071 .prepare = rk808_clkout2_prepare,
0072 .unprepare = rk808_clkout2_unprepare,
0073 .is_prepared = rk808_clkout2_is_prepared,
0074 .recalc_rate = rk808_clkout_recalc_rate,
0075 };
0076
0077 static struct clk_hw *
0078 of_clk_rk808_get(struct of_phandle_args *clkspec, void *data)
0079 {
0080 struct rk808_clkout *rk808_clkout = data;
0081 unsigned int idx = clkspec->args[0];
0082
0083 if (idx >= 2) {
0084 pr_err("%s: invalid index %u\n", __func__, idx);
0085 return ERR_PTR(-EINVAL);
0086 }
0087
0088 return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw;
0089 }
0090
0091 static int rk817_clkout2_enable(struct clk_hw *hw, bool enable)
0092 {
0093 struct rk808_clkout *rk808_clkout = container_of(hw,
0094 struct rk808_clkout,
0095 clkout2_hw);
0096 struct rk808 *rk808 = rk808_clkout->rk808;
0097
0098 return regmap_update_bits(rk808->regmap, RK817_SYS_CFG(1),
0099 RK817_CLK32KOUT2_EN,
0100 enable ? RK817_CLK32KOUT2_EN : 0);
0101 }
0102
0103 static int rk817_clkout2_prepare(struct clk_hw *hw)
0104 {
0105 return rk817_clkout2_enable(hw, true);
0106 }
0107
0108 static void rk817_clkout2_unprepare(struct clk_hw *hw)
0109 {
0110 rk817_clkout2_enable(hw, false);
0111 }
0112
0113 static int rk817_clkout2_is_prepared(struct clk_hw *hw)
0114 {
0115 struct rk808_clkout *rk808_clkout = container_of(hw,
0116 struct rk808_clkout,
0117 clkout2_hw);
0118 struct rk808 *rk808 = rk808_clkout->rk808;
0119 unsigned int val;
0120
0121 int ret = regmap_read(rk808->regmap, RK817_SYS_CFG(1), &val);
0122
0123 if (ret < 0)
0124 return 0;
0125
0126 return (val & RK817_CLK32KOUT2_EN) ? 1 : 0;
0127 }
0128
0129 static const struct clk_ops rk817_clkout2_ops = {
0130 .prepare = rk817_clkout2_prepare,
0131 .unprepare = rk817_clkout2_unprepare,
0132 .is_prepared = rk817_clkout2_is_prepared,
0133 .recalc_rate = rk808_clkout_recalc_rate,
0134 };
0135
0136 static const struct clk_ops *rkpmic_get_ops(long variant)
0137 {
0138 switch (variant) {
0139 case RK809_ID:
0140 case RK817_ID:
0141 return &rk817_clkout2_ops;
0142
0143
0144
0145
0146
0147
0148 default:
0149 return &rk808_clkout2_ops;
0150 }
0151 }
0152
0153 static int rk808_clkout_probe(struct platform_device *pdev)
0154 {
0155 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
0156 struct i2c_client *client = rk808->i2c;
0157 struct device_node *node = client->dev.of_node;
0158 struct clk_init_data init = {};
0159 struct rk808_clkout *rk808_clkout;
0160 int ret;
0161
0162 rk808_clkout = devm_kzalloc(&client->dev,
0163 sizeof(*rk808_clkout), GFP_KERNEL);
0164 if (!rk808_clkout)
0165 return -ENOMEM;
0166
0167 rk808_clkout->rk808 = rk808;
0168
0169 init.parent_names = NULL;
0170 init.num_parents = 0;
0171 init.name = "rk808-clkout1";
0172 init.ops = &rk808_clkout1_ops;
0173 rk808_clkout->clkout1_hw.init = &init;
0174
0175
0176 of_property_read_string_index(node, "clock-output-names",
0177 0, &init.name);
0178
0179 ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw);
0180 if (ret)
0181 return ret;
0182
0183 init.name = "rk808-clkout2";
0184 init.ops = rkpmic_get_ops(rk808->variant);
0185 rk808_clkout->clkout2_hw.init = &init;
0186
0187
0188 of_property_read_string_index(node, "clock-output-names",
0189 1, &init.name);
0190
0191 ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw);
0192 if (ret)
0193 return ret;
0194
0195 return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rk808_get,
0196 rk808_clkout);
0197 }
0198
0199 static struct platform_driver rk808_clkout_driver = {
0200 .probe = rk808_clkout_probe,
0201 .driver = {
0202 .name = "rk808-clkout",
0203 },
0204 };
0205
0206 module_platform_driver(rk808_clkout_driver);
0207
0208 MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
0209 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
0210 MODULE_LICENSE("GPL");
0211 MODULE_ALIAS("platform:rk808-clkout");