Linux Audio

Check our new training course

Loading...
v6.13.7
  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
 16struct rk808_clkout {
 17	struct regmap		*regmap;
 18	struct clk_hw		clkout1_hw;
 19	struct clk_hw		clkout2_hw;
 20};
 21
 22static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
 23					      unsigned long parent_rate)
 24{
 25	return 32768;
 26}
 27
 28static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
 29{
 30	struct rk808_clkout *rk808_clkout = container_of(hw,
 31							 struct rk808_clkout,
 32							 clkout2_hw);
 
 33
 34	return regmap_update_bits(rk808_clkout->regmap, RK808_CLK32OUT_REG,
 35				  CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
 36}
 37
 38static int rk808_clkout2_prepare(struct clk_hw *hw)
 39{
 40	return rk808_clkout2_enable(hw, true);
 41}
 42
 43static void rk808_clkout2_unprepare(struct clk_hw *hw)
 44{
 45	rk808_clkout2_enable(hw, false);
 46}
 47
 48static int rk808_clkout2_is_prepared(struct clk_hw *hw)
 49{
 50	struct rk808_clkout *rk808_clkout = container_of(hw,
 51							 struct rk808_clkout,
 52							 clkout2_hw);
 
 53	uint32_t val;
 54
 55	int ret = regmap_read(rk808_clkout->regmap, RK808_CLK32OUT_REG, &val);
 56
 57	if (ret < 0)
 58		return ret;
 59
 60	return (val & CLK32KOUT2_EN) ? 1 : 0;
 61}
 62
 63static const struct clk_ops rk808_clkout1_ops = {
 64	.recalc_rate = rk808_clkout_recalc_rate,
 65};
 66
 67static const struct clk_ops rk808_clkout2_ops = {
 68	.prepare = rk808_clkout2_prepare,
 69	.unprepare = rk808_clkout2_unprepare,
 70	.is_prepared = rk808_clkout2_is_prepared,
 71	.recalc_rate = rk808_clkout_recalc_rate,
 72};
 73
 74static struct clk_hw *
 75of_clk_rk808_get(struct of_phandle_args *clkspec, void *data)
 76{
 77	struct rk808_clkout *rk808_clkout = data;
 78	unsigned int idx = clkspec->args[0];
 79
 80	if (idx >= 2) {
 81		pr_err("%s: invalid index %u\n", __func__, idx);
 82		return ERR_PTR(-EINVAL);
 83	}
 84
 85	return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw;
 86}
 87
 88static int rk817_clkout2_enable(struct clk_hw *hw, bool enable)
 89{
 90	struct rk808_clkout *rk808_clkout = container_of(hw,
 91							 struct rk808_clkout,
 92							 clkout2_hw);
 93
 94	return regmap_update_bits(rk808_clkout->regmap, RK817_SYS_CFG(1),
 95				  RK817_CLK32KOUT2_EN,
 96				  enable ? RK817_CLK32KOUT2_EN : 0);
 97}
 98
 99static int rk817_clkout2_prepare(struct clk_hw *hw)
100{
101	return rk817_clkout2_enable(hw, true);
102}
103
104static void rk817_clkout2_unprepare(struct clk_hw *hw)
105{
106	rk817_clkout2_enable(hw, false);
107}
108
109static int rk817_clkout2_is_prepared(struct clk_hw *hw)
110{
111	struct rk808_clkout *rk808_clkout = container_of(hw,
112							 struct rk808_clkout,
113							 clkout2_hw);
114	unsigned int val;
115
116	int ret = regmap_read(rk808_clkout->regmap, RK817_SYS_CFG(1), &val);
117
118	if (ret < 0)
119		return 0;
120
121	return (val & RK817_CLK32KOUT2_EN) ? 1 : 0;
122}
123
124static const struct clk_ops rk817_clkout2_ops = {
125	.prepare = rk817_clkout2_prepare,
126	.unprepare = rk817_clkout2_unprepare,
127	.is_prepared = rk817_clkout2_is_prepared,
128	.recalc_rate = rk808_clkout_recalc_rate,
129};
130
131static const struct clk_ops *rkpmic_get_ops(long variant)
132{
133	switch (variant) {
134	case RK809_ID:
135	case RK817_ID:
136		return &rk817_clkout2_ops;
137	/*
138	 * For the default case, it match the following PMIC type.
139	 * RK805_ID
140	 * RK808_ID
141	 * RK818_ID
142	 */
143	default:
144		return &rk808_clkout2_ops;
145	}
146}
147
148static int rk808_clkout_probe(struct platform_device *pdev)
149{
150	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
151	struct device *dev = &pdev->dev;
 
152	struct clk_init_data init = {};
153	struct rk808_clkout *rk808_clkout;
154	int ret;
155
156	dev->of_node = pdev->dev.parent->of_node;
157
158	rk808_clkout = devm_kzalloc(dev,
159				    sizeof(*rk808_clkout), GFP_KERNEL);
160	if (!rk808_clkout)
161		return -ENOMEM;
162
163	rk808_clkout->regmap = dev_get_regmap(pdev->dev.parent, NULL);
164	if (!rk808_clkout->regmap)
165		return -ENODEV;
166
167	init.parent_names = NULL;
168	init.num_parents = 0;
169	init.name = "rk808-clkout1";
170	init.ops = &rk808_clkout1_ops;
171	rk808_clkout->clkout1_hw.init = &init;
172
173	/* optional override of the clockname */
174	of_property_read_string_index(dev->of_node, "clock-output-names",
175				      0, &init.name);
176
177	ret = devm_clk_hw_register(dev, &rk808_clkout->clkout1_hw);
178	if (ret)
179		return ret;
180
181	init.name = "rk808-clkout2";
182	init.ops = rkpmic_get_ops(rk808->variant);
183	rk808_clkout->clkout2_hw.init = &init;
184
185	/* optional override of the clockname */
186	of_property_read_string_index(dev->of_node, "clock-output-names",
187				      1, &init.name);
188
189	ret = devm_clk_hw_register(dev, &rk808_clkout->clkout2_hw);
190	if (ret)
191		return ret;
192
193	return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rk808_get,
194					   rk808_clkout);
 
 
 
 
 
 
 
 
 
 
195}
196
197static struct platform_driver rk808_clkout_driver = {
198	.probe = rk808_clkout_probe,
 
199	.driver		= {
200		.name	= "rk808-clkout",
201	},
202};
203
204module_platform_driver(rk808_clkout_driver);
205
206MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
207MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
208MODULE_LICENSE("GPL");
209MODULE_ALIAS("platform:rk808-clkout");
v4.10.11
 
  1/*
  2 * Clkout driver for Rockchip RK808
  3 *
  4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  5 *
  6 * Author:Chris Zhong <zyw@rock-chips.com>
  7 *
  8 * This program is free software; you can redistribute it and/or modify it
  9 * under the terms and conditions of the GNU General Public License,
 10 * version 2, as published by the Free Software Foundation.
 11 *
 12 * This program is distributed in the hope it will be useful, but WITHOUT
 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 15 * more details.
 16 */
 17
 18#include <linux/clk-provider.h>
 19#include <linux/module.h>
 20#include <linux/slab.h>
 21#include <linux/platform_device.h>
 22#include <linux/mfd/rk808.h>
 23#include <linux/i2c.h>
 24
 25struct rk808_clkout {
 26	struct rk808 *rk808;
 27	struct clk_hw		clkout1_hw;
 28	struct clk_hw		clkout2_hw;
 29};
 30
 31static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
 32					      unsigned long parent_rate)
 33{
 34	return 32768;
 35}
 36
 37static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
 38{
 39	struct rk808_clkout *rk808_clkout = container_of(hw,
 40							 struct rk808_clkout,
 41							 clkout2_hw);
 42	struct rk808 *rk808 = rk808_clkout->rk808;
 43
 44	return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
 45				  CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
 46}
 47
 48static int rk808_clkout2_prepare(struct clk_hw *hw)
 49{
 50	return rk808_clkout2_enable(hw, true);
 51}
 52
 53static void rk808_clkout2_unprepare(struct clk_hw *hw)
 54{
 55	rk808_clkout2_enable(hw, false);
 56}
 57
 58static int rk808_clkout2_is_prepared(struct clk_hw *hw)
 59{
 60	struct rk808_clkout *rk808_clkout = container_of(hw,
 61							 struct rk808_clkout,
 62							 clkout2_hw);
 63	struct rk808 *rk808 = rk808_clkout->rk808;
 64	uint32_t val;
 65
 66	int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
 67
 68	if (ret < 0)
 69		return ret;
 70
 71	return (val & CLK32KOUT2_EN) ? 1 : 0;
 72}
 73
 74static const struct clk_ops rk808_clkout1_ops = {
 75	.recalc_rate = rk808_clkout_recalc_rate,
 76};
 77
 78static const struct clk_ops rk808_clkout2_ops = {
 79	.prepare = rk808_clkout2_prepare,
 80	.unprepare = rk808_clkout2_unprepare,
 81	.is_prepared = rk808_clkout2_is_prepared,
 82	.recalc_rate = rk808_clkout_recalc_rate,
 83};
 84
 85static struct clk_hw *
 86of_clk_rk808_get(struct of_phandle_args *clkspec, void *data)
 87{
 88	struct rk808_clkout *rk808_clkout = data;
 89	unsigned int idx = clkspec->args[0];
 90
 91	if (idx >= 2) {
 92		pr_err("%s: invalid index %u\n", __func__, idx);
 93		return ERR_PTR(-EINVAL);
 94	}
 95
 96	return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw;
 97}
 98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 99static int rk808_clkout_probe(struct platform_device *pdev)
100{
101	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
102	struct i2c_client *client = rk808->i2c;
103	struct device_node *node = client->dev.of_node;
104	struct clk_init_data init = {};
105	struct rk808_clkout *rk808_clkout;
106	int ret;
107
108	rk808_clkout = devm_kzalloc(&client->dev,
 
 
109				    sizeof(*rk808_clkout), GFP_KERNEL);
110	if (!rk808_clkout)
111		return -ENOMEM;
112
113	rk808_clkout->rk808 = rk808;
 
 
114
115	init.parent_names = NULL;
116	init.num_parents = 0;
117	init.name = "rk808-clkout1";
118	init.ops = &rk808_clkout1_ops;
119	rk808_clkout->clkout1_hw.init = &init;
120
121	/* optional override of the clockname */
122	of_property_read_string_index(node, "clock-output-names",
123				      0, &init.name);
124
125	ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw);
126	if (ret)
127		return ret;
128
129	init.name = "rk808-clkout2";
130	init.ops = &rk808_clkout2_ops;
131	rk808_clkout->clkout2_hw.init = &init;
132
133	/* optional override of the clockname */
134	of_property_read_string_index(node, "clock-output-names",
135				      1, &init.name);
136
137	ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw);
138	if (ret)
139		return ret;
140
141	return of_clk_add_hw_provider(node, of_clk_rk808_get, rk808_clkout);
142}
143
144static int rk808_clkout_remove(struct platform_device *pdev)
145{
146	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
147	struct i2c_client *client = rk808->i2c;
148	struct device_node *node = client->dev.of_node;
149
150	of_clk_del_provider(node);
151
152	return 0;
153}
154
155static struct platform_driver rk808_clkout_driver = {
156	.probe = rk808_clkout_probe,
157	.remove = rk808_clkout_remove,
158	.driver		= {
159		.name	= "rk808-clkout",
160	},
161};
162
163module_platform_driver(rk808_clkout_driver);
164
165MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
166MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
167MODULE_LICENSE("GPL");
168MODULE_ALIAS("platform:rk808-clkout");