1038b892aSChris Zhong /* 2038b892aSChris Zhong * Clkout driver for Rockchip RK808 3038b892aSChris Zhong * 4038b892aSChris Zhong * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd 5038b892aSChris Zhong * 6038b892aSChris Zhong * Author:Chris Zhong <zyw@rock-chips.com> 7038b892aSChris Zhong * 8038b892aSChris Zhong * This program is free software; you can redistribute it and/or modify it 9038b892aSChris Zhong * under the terms and conditions of the GNU General Public License, 10038b892aSChris Zhong * version 2, as published by the Free Software Foundation. 11038b892aSChris Zhong * 12038b892aSChris Zhong * This program is distributed in the hope it will be useful, but WITHOUT 13038b892aSChris Zhong * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14038b892aSChris Zhong * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15038b892aSChris Zhong * more details. 16038b892aSChris Zhong */ 17038b892aSChris Zhong 18038b892aSChris Zhong #include <linux/clk-provider.h> 19038b892aSChris Zhong #include <linux/module.h> 20038b892aSChris Zhong #include <linux/slab.h> 21038b892aSChris Zhong #include <linux/platform_device.h> 22038b892aSChris Zhong #include <linux/mfd/rk808.h> 23038b892aSChris Zhong #include <linux/i2c.h> 24038b892aSChris Zhong 25038b892aSChris Zhong struct rk808_clkout { 26038b892aSChris Zhong struct rk808 *rk808; 27038b892aSChris Zhong struct clk_hw clkout1_hw; 28038b892aSChris Zhong struct clk_hw clkout2_hw; 29038b892aSChris Zhong }; 30038b892aSChris Zhong 31038b892aSChris Zhong static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw, 32038b892aSChris Zhong unsigned long parent_rate) 33038b892aSChris Zhong { 34038b892aSChris Zhong return 32768; 35038b892aSChris Zhong } 36038b892aSChris Zhong 37038b892aSChris Zhong static int rk808_clkout2_enable(struct clk_hw *hw, bool enable) 38038b892aSChris Zhong { 39038b892aSChris Zhong struct rk808_clkout *rk808_clkout = container_of(hw, 40038b892aSChris Zhong struct rk808_clkout, 41038b892aSChris Zhong clkout2_hw); 42038b892aSChris Zhong struct rk808 *rk808 = rk808_clkout->rk808; 43038b892aSChris Zhong 44038b892aSChris Zhong return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG, 45038b892aSChris Zhong CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0); 46038b892aSChris Zhong } 47038b892aSChris Zhong 48038b892aSChris Zhong static int rk808_clkout2_prepare(struct clk_hw *hw) 49038b892aSChris Zhong { 50038b892aSChris Zhong return rk808_clkout2_enable(hw, true); 51038b892aSChris Zhong } 52038b892aSChris Zhong 53038b892aSChris Zhong static void rk808_clkout2_unprepare(struct clk_hw *hw) 54038b892aSChris Zhong { 55038b892aSChris Zhong rk808_clkout2_enable(hw, false); 56038b892aSChris Zhong } 57038b892aSChris Zhong 58038b892aSChris Zhong static int rk808_clkout2_is_prepared(struct clk_hw *hw) 59038b892aSChris Zhong { 60038b892aSChris Zhong struct rk808_clkout *rk808_clkout = container_of(hw, 61038b892aSChris Zhong struct rk808_clkout, 62038b892aSChris Zhong clkout2_hw); 63038b892aSChris Zhong struct rk808 *rk808 = rk808_clkout->rk808; 64038b892aSChris Zhong uint32_t val; 65038b892aSChris Zhong 66038b892aSChris Zhong int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val); 67038b892aSChris Zhong 68038b892aSChris Zhong if (ret < 0) 69038b892aSChris Zhong return ret; 70038b892aSChris Zhong 71038b892aSChris Zhong return (val & CLK32KOUT2_EN) ? 1 : 0; 72038b892aSChris Zhong } 73038b892aSChris Zhong 74038b892aSChris Zhong static const struct clk_ops rk808_clkout1_ops = { 75038b892aSChris Zhong .recalc_rate = rk808_clkout_recalc_rate, 76038b892aSChris Zhong }; 77038b892aSChris Zhong 78038b892aSChris Zhong static const struct clk_ops rk808_clkout2_ops = { 79038b892aSChris Zhong .prepare = rk808_clkout2_prepare, 80038b892aSChris Zhong .unprepare = rk808_clkout2_unprepare, 81038b892aSChris Zhong .is_prepared = rk808_clkout2_is_prepared, 82038b892aSChris Zhong .recalc_rate = rk808_clkout_recalc_rate, 83038b892aSChris Zhong }; 84038b892aSChris Zhong 85a8b6e85dSStephen Boyd static struct clk_hw * 86a8b6e85dSStephen Boyd of_clk_rk808_get(struct of_phandle_args *clkspec, void *data) 87a8b6e85dSStephen Boyd { 88a8b6e85dSStephen Boyd struct rk808_clkout *rk808_clkout = data; 89a8b6e85dSStephen Boyd unsigned int idx = clkspec->args[0]; 90a8b6e85dSStephen Boyd 91a8b6e85dSStephen Boyd if (idx >= 2) { 92a8b6e85dSStephen Boyd pr_err("%s: invalid index %u\n", __func__, idx); 93a8b6e85dSStephen Boyd return ERR_PTR(-EINVAL); 94a8b6e85dSStephen Boyd } 95a8b6e85dSStephen Boyd 96a8b6e85dSStephen Boyd return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw; 97a8b6e85dSStephen Boyd } 98a8b6e85dSStephen Boyd 99*8ed14401STony Xie static int rk817_clkout2_enable(struct clk_hw *hw, bool enable) 100*8ed14401STony Xie { 101*8ed14401STony Xie struct rk808_clkout *rk808_clkout = container_of(hw, 102*8ed14401STony Xie struct rk808_clkout, 103*8ed14401STony Xie clkout2_hw); 104*8ed14401STony Xie struct rk808 *rk808 = rk808_clkout->rk808; 105*8ed14401STony Xie 106*8ed14401STony Xie return regmap_update_bits(rk808->regmap, RK817_SYS_CFG(1), 107*8ed14401STony Xie RK817_CLK32KOUT2_EN, 108*8ed14401STony Xie enable ? RK817_CLK32KOUT2_EN : 0); 109*8ed14401STony Xie } 110*8ed14401STony Xie 111*8ed14401STony Xie static int rk817_clkout2_prepare(struct clk_hw *hw) 112*8ed14401STony Xie { 113*8ed14401STony Xie return rk817_clkout2_enable(hw, true); 114*8ed14401STony Xie } 115*8ed14401STony Xie 116*8ed14401STony Xie static void rk817_clkout2_unprepare(struct clk_hw *hw) 117*8ed14401STony Xie { 118*8ed14401STony Xie rk817_clkout2_enable(hw, false); 119*8ed14401STony Xie } 120*8ed14401STony Xie 121*8ed14401STony Xie static int rk817_clkout2_is_prepared(struct clk_hw *hw) 122*8ed14401STony Xie { 123*8ed14401STony Xie struct rk808_clkout *rk808_clkout = container_of(hw, 124*8ed14401STony Xie struct rk808_clkout, 125*8ed14401STony Xie clkout2_hw); 126*8ed14401STony Xie struct rk808 *rk808 = rk808_clkout->rk808; 127*8ed14401STony Xie unsigned int val; 128*8ed14401STony Xie 129*8ed14401STony Xie int ret = regmap_read(rk808->regmap, RK817_SYS_CFG(1), &val); 130*8ed14401STony Xie 131*8ed14401STony Xie if (ret < 0) 132*8ed14401STony Xie return 0; 133*8ed14401STony Xie 134*8ed14401STony Xie return (val & RK817_CLK32KOUT2_EN) ? 1 : 0; 135*8ed14401STony Xie } 136*8ed14401STony Xie 137*8ed14401STony Xie static const struct clk_ops rk817_clkout2_ops = { 138*8ed14401STony Xie .prepare = rk817_clkout2_prepare, 139*8ed14401STony Xie .unprepare = rk817_clkout2_unprepare, 140*8ed14401STony Xie .is_prepared = rk817_clkout2_is_prepared, 141*8ed14401STony Xie .recalc_rate = rk808_clkout_recalc_rate, 142*8ed14401STony Xie }; 143*8ed14401STony Xie 144*8ed14401STony Xie static const struct clk_ops *rkpmic_get_ops(long variant) 145*8ed14401STony Xie { 146*8ed14401STony Xie switch (variant) { 147*8ed14401STony Xie case RK809_ID: 148*8ed14401STony Xie case RK817_ID: 149*8ed14401STony Xie return &rk817_clkout2_ops; 150*8ed14401STony Xie /* 151*8ed14401STony Xie * For the default case, it match the following PMIC type. 152*8ed14401STony Xie * RK805_ID 153*8ed14401STony Xie * RK808_ID 154*8ed14401STony Xie * RK818_ID 155*8ed14401STony Xie */ 156*8ed14401STony Xie default: 157*8ed14401STony Xie return &rk808_clkout2_ops; 158*8ed14401STony Xie } 159*8ed14401STony Xie } 160*8ed14401STony Xie 161038b892aSChris Zhong static int rk808_clkout_probe(struct platform_device *pdev) 162038b892aSChris Zhong { 163038b892aSChris Zhong struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent); 164038b892aSChris Zhong struct i2c_client *client = rk808->i2c; 165038b892aSChris Zhong struct device_node *node = client->dev.of_node; 166038b892aSChris Zhong struct clk_init_data init = {}; 167038b892aSChris Zhong struct rk808_clkout *rk808_clkout; 168a8b6e85dSStephen Boyd int ret; 169038b892aSChris Zhong 170038b892aSChris Zhong rk808_clkout = devm_kzalloc(&client->dev, 171038b892aSChris Zhong sizeof(*rk808_clkout), GFP_KERNEL); 172038b892aSChris Zhong if (!rk808_clkout) 173038b892aSChris Zhong return -ENOMEM; 174038b892aSChris Zhong 175038b892aSChris Zhong rk808_clkout->rk808 = rk808; 176038b892aSChris Zhong 177038b892aSChris Zhong init.parent_names = NULL; 178038b892aSChris Zhong init.num_parents = 0; 179038b892aSChris Zhong init.name = "rk808-clkout1"; 180038b892aSChris Zhong init.ops = &rk808_clkout1_ops; 181038b892aSChris Zhong rk808_clkout->clkout1_hw.init = &init; 182038b892aSChris Zhong 183038b892aSChris Zhong /* optional override of the clockname */ 184038b892aSChris Zhong of_property_read_string_index(node, "clock-output-names", 185038b892aSChris Zhong 0, &init.name); 186038b892aSChris Zhong 187a8b6e85dSStephen Boyd ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw); 188a8b6e85dSStephen Boyd if (ret) 189a8b6e85dSStephen Boyd return ret; 190038b892aSChris Zhong 191038b892aSChris Zhong init.name = "rk808-clkout2"; 192*8ed14401STony Xie init.ops = rkpmic_get_ops(rk808->variant); 193038b892aSChris Zhong rk808_clkout->clkout2_hw.init = &init; 194038b892aSChris Zhong 195038b892aSChris Zhong /* optional override of the clockname */ 196038b892aSChris Zhong of_property_read_string_index(node, "clock-output-names", 197038b892aSChris Zhong 1, &init.name); 198038b892aSChris Zhong 199a8b6e85dSStephen Boyd ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw); 200a8b6e85dSStephen Boyd if (ret) 201a8b6e85dSStephen Boyd return ret; 202038b892aSChris Zhong 20325224667SMatti Vaittinen return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rk808_get, 20425224667SMatti Vaittinen rk808_clkout); 205038b892aSChris Zhong } 206038b892aSChris Zhong 207038b892aSChris Zhong static struct platform_driver rk808_clkout_driver = { 208038b892aSChris Zhong .probe = rk808_clkout_probe, 209038b892aSChris Zhong .driver = { 210038b892aSChris Zhong .name = "rk808-clkout", 211038b892aSChris Zhong }, 212038b892aSChris Zhong }; 213038b892aSChris Zhong 214038b892aSChris Zhong module_platform_driver(rk808_clkout_driver); 215038b892aSChris Zhong 216038b892aSChris Zhong MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs"); 217038b892aSChris Zhong MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>"); 218038b892aSChris Zhong MODULE_LICENSE("GPL"); 219038b892aSChris Zhong MODULE_ALIAS("platform:rk808-clkout"); 220