1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Clkout driver for Rockchip RK808 4 * 5 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd 6 * 7 * Author:Chris Zhong <zyw@rock-chips.com> 8 */ 9 10 #include <linux/clk-provider.h> 11 #include <linux/module.h> 12 #include <linux/slab.h> 13 #include <linux/platform_device.h> 14 #include <linux/mfd/rk808.h> 15 #include <linux/i2c.h> 16 17 struct rk808_clkout { 18 struct rk808 *rk808; 19 struct clk_hw clkout1_hw; 20 struct clk_hw clkout2_hw; 21 }; 22 23 static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw, 24 unsigned long parent_rate) 25 { 26 return 32768; 27 } 28 29 static int rk808_clkout2_enable(struct clk_hw *hw, bool enable) 30 { 31 struct rk808_clkout *rk808_clkout = container_of(hw, 32 struct rk808_clkout, 33 clkout2_hw); 34 struct rk808 *rk808 = rk808_clkout->rk808; 35 36 return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG, 37 CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0); 38 } 39 40 static int rk808_clkout2_prepare(struct clk_hw *hw) 41 { 42 return rk808_clkout2_enable(hw, true); 43 } 44 45 static void rk808_clkout2_unprepare(struct clk_hw *hw) 46 { 47 rk808_clkout2_enable(hw, false); 48 } 49 50 static int rk808_clkout2_is_prepared(struct clk_hw *hw) 51 { 52 struct rk808_clkout *rk808_clkout = container_of(hw, 53 struct rk808_clkout, 54 clkout2_hw); 55 struct rk808 *rk808 = rk808_clkout->rk808; 56 uint32_t val; 57 58 int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val); 59 60 if (ret < 0) 61 return ret; 62 63 return (val & CLK32KOUT2_EN) ? 1 : 0; 64 } 65 66 static const struct clk_ops rk808_clkout1_ops = { 67 .recalc_rate = rk808_clkout_recalc_rate, 68 }; 69 70 static const struct clk_ops rk808_clkout2_ops = { 71 .prepare = rk808_clkout2_prepare, 72 .unprepare = rk808_clkout2_unprepare, 73 .is_prepared = rk808_clkout2_is_prepared, 74 .recalc_rate = rk808_clkout_recalc_rate, 75 }; 76 77 static struct clk_hw * 78 of_clk_rk808_get(struct of_phandle_args *clkspec, void *data) 79 { 80 struct rk808_clkout *rk808_clkout = data; 81 unsigned int idx = clkspec->args[0]; 82 83 if (idx >= 2) { 84 pr_err("%s: invalid index %u\n", __func__, idx); 85 return ERR_PTR(-EINVAL); 86 } 87 88 return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw; 89 } 90 91 static int rk808_clkout_probe(struct platform_device *pdev) 92 { 93 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent); 94 struct i2c_client *client = rk808->i2c; 95 struct device_node *node = client->dev.of_node; 96 struct clk_init_data init = {}; 97 struct rk808_clkout *rk808_clkout; 98 int ret; 99 100 rk808_clkout = devm_kzalloc(&client->dev, 101 sizeof(*rk808_clkout), GFP_KERNEL); 102 if (!rk808_clkout) 103 return -ENOMEM; 104 105 rk808_clkout->rk808 = rk808; 106 107 init.parent_names = NULL; 108 init.num_parents = 0; 109 init.name = "rk808-clkout1"; 110 init.ops = &rk808_clkout1_ops; 111 rk808_clkout->clkout1_hw.init = &init; 112 113 /* optional override of the clockname */ 114 of_property_read_string_index(node, "clock-output-names", 115 0, &init.name); 116 117 ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw); 118 if (ret) 119 return ret; 120 121 init.name = "rk808-clkout2"; 122 init.ops = &rk808_clkout2_ops; 123 rk808_clkout->clkout2_hw.init = &init; 124 125 /* optional override of the clockname */ 126 of_property_read_string_index(node, "clock-output-names", 127 1, &init.name); 128 129 ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw); 130 if (ret) 131 return ret; 132 133 return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rk808_get, 134 rk808_clkout); 135 } 136 137 static struct platform_driver rk808_clkout_driver = { 138 .probe = rk808_clkout_probe, 139 .driver = { 140 .name = "rk808-clkout", 141 }, 142 }; 143 144 module_platform_driver(rk808_clkout_driver); 145 146 MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs"); 147 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>"); 148 MODULE_LICENSE("GPL"); 149 MODULE_ALIAS("platform:rk808-clkout"); 150