Linux Audio

Check our new training course

Loading...
v5.9
  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
 
 
 17struct rk808_clkout {
 18	struct rk808 *rk808;
 
 19	struct clk_hw		clkout1_hw;
 20	struct clk_hw		clkout2_hw;
 21};
 22
 23static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
 24					      unsigned long parent_rate)
 25{
 26	return 32768;
 27}
 28
 29static 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
 40static int rk808_clkout2_prepare(struct clk_hw *hw)
 41{
 42	return rk808_clkout2_enable(hw, true);
 43}
 44
 45static void rk808_clkout2_unprepare(struct clk_hw *hw)
 46{
 47	rk808_clkout2_enable(hw, false);
 48}
 49
 50static 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
 66static const struct clk_ops rk808_clkout1_ops = {
 67	.recalc_rate = rk808_clkout_recalc_rate,
 68};
 69
 70static 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
 77static struct clk_hw *
 78of_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
 91static int rk817_clkout2_enable(struct clk_hw *hw, bool enable)
 92{
 93	struct rk808_clkout *rk808_clkout = container_of(hw,
 94							 struct rk808_clkout,
 95							 clkout2_hw);
 96	struct rk808 *rk808 = rk808_clkout->rk808;
 97
 98	return regmap_update_bits(rk808->regmap, RK817_SYS_CFG(1),
 99				  RK817_CLK32KOUT2_EN,
100				  enable ? RK817_CLK32KOUT2_EN : 0);
101}
102
103static int rk817_clkout2_prepare(struct clk_hw *hw)
104{
105	return rk817_clkout2_enable(hw, true);
106}
107
108static void rk817_clkout2_unprepare(struct clk_hw *hw)
109{
110	rk817_clkout2_enable(hw, false);
111}
112
113static int rk817_clkout2_is_prepared(struct clk_hw *hw)
114{
115	struct rk808_clkout *rk808_clkout = container_of(hw,
116							 struct rk808_clkout,
117							 clkout2_hw);
118	struct rk808 *rk808 = rk808_clkout->rk808;
119	unsigned int val;
120
121	int ret = regmap_read(rk808->regmap, RK817_SYS_CFG(1), &val);
122
123	if (ret < 0)
124		return 0;
125
126	return (val & RK817_CLK32KOUT2_EN) ? 1 : 0;
127}
128
129static const struct clk_ops rk817_clkout2_ops = {
130	.prepare = rk817_clkout2_prepare,
131	.unprepare = rk817_clkout2_unprepare,
132	.is_prepared = rk817_clkout2_is_prepared,
133	.recalc_rate = rk808_clkout_recalc_rate,
134};
135
136static const struct clk_ops *rkpmic_get_ops(long variant)
137{
138	switch (variant) {
139	case RK809_ID:
140	case RK817_ID:
141		return &rk817_clkout2_ops;
142	/*
143	 * For the default case, it match the following PMIC type.
144	 * RK805_ID
145	 * RK808_ID
146	 * RK818_ID
147	 */
148	default:
149		return &rk808_clkout2_ops;
150	}
151}
152
153static int rk808_clkout_probe(struct platform_device *pdev)
154{
155	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
156	struct i2c_client *client = rk808->i2c;
157	struct device_node *node = client->dev.of_node;
158	struct clk_init_data init = {};
 
159	struct rk808_clkout *rk808_clkout;
160	int ret;
161
162	rk808_clkout = devm_kzalloc(&client->dev,
163				    sizeof(*rk808_clkout), GFP_KERNEL);
164	if (!rk808_clkout)
165		return -ENOMEM;
166
167	rk808_clkout->rk808 = rk808;
168
 
 
 
 
 
 
169	init.parent_names = NULL;
170	init.num_parents = 0;
171	init.name = "rk808-clkout1";
172	init.ops = &rk808_clkout1_ops;
173	rk808_clkout->clkout1_hw.init = &init;
174
175	/* optional override of the clockname */
176	of_property_read_string_index(node, "clock-output-names",
177				      0, &init.name);
178
179	ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw);
180	if (ret)
181		return ret;
 
182
183	init.name = "rk808-clkout2";
184	init.ops = rkpmic_get_ops(rk808->variant);
185	rk808_clkout->clkout2_hw.init = &init;
186
187	/* optional override of the clockname */
188	of_property_read_string_index(node, "clock-output-names",
189				      1, &init.name);
190
191	ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw);
192	if (ret)
193		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
195	return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rk808_get,
196					   rk808_clkout);
197}
198
199static struct platform_driver rk808_clkout_driver = {
200	.probe = rk808_clkout_probe,
 
201	.driver		= {
202		.name	= "rk808-clkout",
203	},
204};
205
206module_platform_driver(rk808_clkout_driver);
207
208MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
209MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
210MODULE_LICENSE("GPL");
211MODULE_ALIAS("platform:rk808-clkout");
v4.6
 
  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
 25#define RK808_NR_OUTPUT 2
 26
 27struct rk808_clkout {
 28	struct rk808 *rk808;
 29	struct clk_onecell_data clk_data;
 30	struct clk_hw		clkout1_hw;
 31	struct clk_hw		clkout2_hw;
 32};
 33
 34static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
 35					      unsigned long parent_rate)
 36{
 37	return 32768;
 38}
 39
 40static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
 41{
 42	struct rk808_clkout *rk808_clkout = container_of(hw,
 43							 struct rk808_clkout,
 44							 clkout2_hw);
 45	struct rk808 *rk808 = rk808_clkout->rk808;
 46
 47	return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
 48				  CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
 49}
 50
 51static int rk808_clkout2_prepare(struct clk_hw *hw)
 52{
 53	return rk808_clkout2_enable(hw, true);
 54}
 55
 56static void rk808_clkout2_unprepare(struct clk_hw *hw)
 57{
 58	rk808_clkout2_enable(hw, false);
 59}
 60
 61static int rk808_clkout2_is_prepared(struct clk_hw *hw)
 62{
 63	struct rk808_clkout *rk808_clkout = container_of(hw,
 64							 struct rk808_clkout,
 65							 clkout2_hw);
 66	struct rk808 *rk808 = rk808_clkout->rk808;
 67	uint32_t val;
 68
 69	int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
 70
 71	if (ret < 0)
 72		return ret;
 73
 74	return (val & CLK32KOUT2_EN) ? 1 : 0;
 75}
 76
 77static const struct clk_ops rk808_clkout1_ops = {
 78	.recalc_rate = rk808_clkout_recalc_rate,
 79};
 80
 81static const struct clk_ops rk808_clkout2_ops = {
 82	.prepare = rk808_clkout2_prepare,
 83	.unprepare = rk808_clkout2_unprepare,
 84	.is_prepared = rk808_clkout2_is_prepared,
 85	.recalc_rate = rk808_clkout_recalc_rate,
 86};
 87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 88static int rk808_clkout_probe(struct platform_device *pdev)
 89{
 90	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
 91	struct i2c_client *client = rk808->i2c;
 92	struct device_node *node = client->dev.of_node;
 93	struct clk_init_data init = {};
 94	struct clk **clk_table;
 95	struct rk808_clkout *rk808_clkout;
 
 96
 97	rk808_clkout = devm_kzalloc(&client->dev,
 98				    sizeof(*rk808_clkout), GFP_KERNEL);
 99	if (!rk808_clkout)
100		return -ENOMEM;
101
102	rk808_clkout->rk808 = rk808;
103
104	clk_table = devm_kcalloc(&client->dev, RK808_NR_OUTPUT,
105				 sizeof(struct clk *), GFP_KERNEL);
106	if (!clk_table)
107		return -ENOMEM;
108
109	init.flags = CLK_IS_ROOT;
110	init.parent_names = NULL;
111	init.num_parents = 0;
112	init.name = "rk808-clkout1";
113	init.ops = &rk808_clkout1_ops;
114	rk808_clkout->clkout1_hw.init = &init;
115
116	/* optional override of the clockname */
117	of_property_read_string_index(node, "clock-output-names",
118				      0, &init.name);
119
120	clk_table[0] = devm_clk_register(&client->dev,
121					 &rk808_clkout->clkout1_hw);
122	if (IS_ERR(clk_table[0]))
123		return PTR_ERR(clk_table[0]);
124
125	init.name = "rk808-clkout2";
126	init.ops = &rk808_clkout2_ops;
127	rk808_clkout->clkout2_hw.init = &init;
128
129	/* optional override of the clockname */
130	of_property_read_string_index(node, "clock-output-names",
131				      1, &init.name);
132
133	clk_table[1] = devm_clk_register(&client->dev,
134					 &rk808_clkout->clkout2_hw);
135	if (IS_ERR(clk_table[1]))
136		return PTR_ERR(clk_table[1]);
137
138	rk808_clkout->clk_data.clks = clk_table;
139	rk808_clkout->clk_data.clk_num = RK808_NR_OUTPUT;
140
141	return of_clk_add_provider(node, of_clk_src_onecell_get,
142				   &rk808_clkout->clk_data);
143}
144
145static int rk808_clkout_remove(struct platform_device *pdev)
146{
147	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
148	struct i2c_client *client = rk808->i2c;
149	struct device_node *node = client->dev.of_node;
150
151	of_clk_del_provider(node);
152
153	return 0;
 
154}
155
156static struct platform_driver rk808_clkout_driver = {
157	.probe = rk808_clkout_probe,
158	.remove = rk808_clkout_remove,
159	.driver		= {
160		.name	= "rk808-clkout",
161	},
162};
163
164module_platform_driver(rk808_clkout_driver);
165
166MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
167MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
168MODULE_LICENSE("GPL");
169MODULE_ALIAS("platform:rk808-clkout");