Linux Audio

Check our new training course

Loading...
v3.15
  1/*
  2 * clk-max77686.c - Clock driver for Maxim 77686
  3 *
  4 * Copyright (C) 2012 Samsung Electornics
  5 * Jonghwa Lee <jonghwa3.lee@samsung.com>
  6 *
  7 * This program is free software; you can redistribute  it and/or modify it
  8 * under  the terms of  the GNU General  Public License as published by the
  9 * Free Software Foundation;  either version 2 of the  License, or (at your
 10 * option) any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20 *
 21 */
 22
 23#include <linux/kernel.h>
 24#include <linux/slab.h>
 25#include <linux/err.h>
 
 26#include <linux/platform_device.h>
 
 27#include <linux/mfd/max77686.h>
 28#include <linux/mfd/max77686-private.h>
 29#include <linux/clk-provider.h>
 30#include <linux/mutex.h>
 31#include <linux/clkdev.h>
 
 
 32
 33enum {
 34	MAX77686_CLK_AP = 0,
 35	MAX77686_CLK_CP,
 36	MAX77686_CLK_PMIC,
 37	MAX77686_CLKS_NUM,
 
 
 
 
 
 38};
 39
 40struct max77686_clk {
 41	struct max77686_dev *iodev;
 42	u32 mask;
 
 
 
 
 
 
 43	struct clk_hw hw;
 44	struct clk_lookup *lookup;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 45};
 46
 47static struct max77686_clk *to_max77686_clk(struct clk_hw *hw)
 
 48{
 49	return container_of(hw, struct max77686_clk, hw);
 50}
 51
 52static int max77686_clk_prepare(struct clk_hw *hw)
 53{
 54	struct max77686_clk *max77686 = to_max77686_clk(hw);
 55
 56	return regmap_update_bits(max77686->iodev->regmap,
 57				  MAX77686_REG_32KHZ, max77686->mask,
 58				  max77686->mask);
 59}
 60
 61static void max77686_clk_unprepare(struct clk_hw *hw)
 62{
 63	struct max77686_clk *max77686 = to_max77686_clk(hw);
 64
 65	regmap_update_bits(max77686->iodev->regmap,
 66		MAX77686_REG_32KHZ, max77686->mask, ~max77686->mask);
 
 67}
 68
 69static int max77686_clk_is_prepared(struct clk_hw *hw)
 70{
 71	struct max77686_clk *max77686 = to_max77686_clk(hw);
 72	int ret;
 73	u32 val;
 74
 75	ret = regmap_read(max77686->iodev->regmap,
 76				MAX77686_REG_32KHZ, &val);
 77
 78	if (ret < 0)
 79		return -EINVAL;
 80
 81	return val & max77686->mask;
 82}
 83
 84static unsigned long max77686_recalc_rate(struct clk_hw *hw,
 85					  unsigned long parent_rate)
 86{
 87	return 32768;
 88}
 89
 90static struct clk_ops max77686_clk_ops = {
 91	.prepare	= max77686_clk_prepare,
 92	.unprepare	= max77686_clk_unprepare,
 93	.is_prepared	= max77686_clk_is_prepared,
 94	.recalc_rate	= max77686_recalc_rate,
 95};
 96
 97static struct clk_init_data max77686_clks_init[MAX77686_CLKS_NUM] = {
 98	[MAX77686_CLK_AP] = {
 99		.name = "32khz_ap",
100		.ops = &max77686_clk_ops,
101		.flags = CLK_IS_ROOT,
102	},
103	[MAX77686_CLK_CP] = {
104		.name = "32khz_cp",
105		.ops = &max77686_clk_ops,
106		.flags = CLK_IS_ROOT,
107	},
108	[MAX77686_CLK_PMIC] = {
109		.name = "32khz_pmic",
110		.ops = &max77686_clk_ops,
111		.flags = CLK_IS_ROOT,
112	},
113};
114
115static struct clk *max77686_clk_register(struct device *dev,
116				struct max77686_clk *max77686)
117{
118	struct clk *clk;
119	struct clk_hw *hw = &max77686->hw;
120
121	clk = clk_register(dev, hw);
122	if (IS_ERR(clk))
123		return clk;
124
125	max77686->lookup = kzalloc(sizeof(struct clk_lookup), GFP_KERNEL);
126	if (!max77686->lookup)
127		return ERR_PTR(-ENOMEM);
128
129	max77686->lookup->con_id = hw->init->name;
130	max77686->lookup->clk = clk;
131
132	clkdev_add(max77686->lookup);
133
134	return clk;
135}
136
137static int max77686_clk_probe(struct platform_device *pdev)
138{
139	struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
140	struct max77686_clk *max77686_clks[MAX77686_CLKS_NUM];
141	struct clk **clocks;
142	int i, ret;
143
144	clocks = devm_kzalloc(&pdev->dev, sizeof(struct clk *)
145					* MAX77686_CLKS_NUM, GFP_KERNEL);
146	if (!clocks)
 
 
147		return -ENOMEM;
148
149	for (i = 0; i < MAX77686_CLKS_NUM; i++) {
150		max77686_clks[i] = devm_kzalloc(&pdev->dev,
151					sizeof(struct max77686_clk), GFP_KERNEL);
152		if (!max77686_clks[i])
153			return -ENOMEM;
154	}
155
156	for (i = 0; i < MAX77686_CLKS_NUM; i++) {
157		max77686_clks[i]->iodev = iodev;
158		max77686_clks[i]->mask = 1 << i;
159		max77686_clks[i]->hw.init = &max77686_clks_init[i];
160
161		clocks[i] = max77686_clk_register(&pdev->dev, max77686_clks[i]);
162		if (IS_ERR(clocks[i])) {
163			ret = PTR_ERR(clocks[i]);
164			dev_err(&pdev->dev, "failed to register %s\n",
165				max77686_clks[i]->hw.init->name);
166			goto err_clocks;
167		}
168	}
169
170	platform_set_drvdata(pdev, clocks);
171
172	if (iodev->dev->of_node) {
173		struct clk_onecell_data *of_data;
174
175		of_data = devm_kzalloc(&pdev->dev,
176					sizeof(*of_data), GFP_KERNEL);
177		if (!of_data) {
178			ret = -ENOMEM;
179			goto err_clocks;
180		}
 
 
 
 
 
 
 
 
 
181
182		of_data->clks = clocks;
183		of_data->clk_num = MAX77686_CLKS_NUM;
184		ret = of_clk_add_provider(iodev->dev->of_node,
185					of_clk_src_onecell_get, of_data);
186		if (ret) {
187			dev_err(&pdev->dev, "failed to register OF clock provider\n");
188			goto err_clocks;
189		}
190	}
191
192	return 0;
 
 
 
 
 
193
194err_clocks:
195	for (--i; i >= 0; --i) {
196		clkdev_drop(max77686_clks[i]->lookup);
197		clk_unregister(max77686_clks[i]->hw.clk);
198	}
 
 
 
 
 
 
 
 
 
 
 
 
 
199
200	return ret;
201}
202
203static int max77686_clk_remove(struct platform_device *pdev)
204{
205	struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
206	struct clk **clocks = platform_get_drvdata(pdev);
207	int i;
208
209	if (iodev->dev->of_node)
210		of_clk_del_provider(iodev->dev->of_node);
 
 
 
 
 
 
211
212	for (i = 0; i < MAX77686_CLKS_NUM; i++) {
213		struct clk_hw *hw = __clk_get_hw(clocks[i]);
214		struct max77686_clk *max77686 = to_max77686_clk(hw);
 
 
 
 
 
 
 
215
216		clkdev_drop(max77686->lookup);
217		clk_unregister(clocks[i]);
 
 
 
 
 
 
 
218	}
 
219	return 0;
220}
221
222static const struct platform_device_id max77686_clk_id[] = {
223	{ "max77686-clk", 0},
224	{ },
 
 
225};
226MODULE_DEVICE_TABLE(platform, max77686_clk_id);
227
228static struct platform_driver max77686_clk_driver = {
229	.driver = {
230		.name  = "max77686-clk",
231		.owner = THIS_MODULE,
232	},
233	.probe = max77686_clk_probe,
234	.remove = max77686_clk_remove,
235	.id_table = max77686_clk_id,
236};
237
238static int __init max77686_clk_init(void)
239{
240	return platform_driver_register(&max77686_clk_driver);
241}
242subsys_initcall(max77686_clk_init);
243
244static void __init max77686_clk_cleanup(void)
245{
246	platform_driver_unregister(&max77686_clk_driver);
247}
248module_exit(max77686_clk_cleanup);
249
250MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
251MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
252MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2//
  3// clk-max77686.c - Clock driver for Maxim 77686/MAX77802
  4//
  5// Copyright (C) 2012 Samsung Electornics
  6// Jonghwa Lee <jonghwa3.lee@samsung.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  7
  8#include <linux/kernel.h>
  9#include <linux/slab.h>
 10#include <linux/err.h>
 11#include <linux/module.h>
 12#include <linux/platform_device.h>
 13#include <linux/mfd/max77620.h>
 14#include <linux/mfd/max77686.h>
 15#include <linux/mfd/max77686-private.h>
 16#include <linux/clk-provider.h>
 17#include <linux/mutex.h>
 18#include <linux/clkdev.h>
 19#include <linux/of.h>
 20#include <linux/regmap.h>
 21
 22#include <dt-bindings/clock/maxim,max77686.h>
 23#include <dt-bindings/clock/maxim,max77802.h>
 24#include <dt-bindings/clock/maxim,max77620.h>
 25
 26#define MAX77802_CLOCK_LOW_JITTER_SHIFT 0x3
 27
 28enum max77686_chip_name {
 29	CHIP_MAX77686,
 30	CHIP_MAX77802,
 31	CHIP_MAX77620,
 32};
 33
 34struct max77686_hw_clk_info {
 35	const char *name;
 36	u32 clk_reg;
 37	u32 clk_enable_mask;
 38	u32 flags;
 39};
 40
 41struct max77686_clk_init_data {
 42	struct regmap *regmap;
 43	struct clk_hw hw;
 44	struct clk_init_data clk_idata;
 45	const struct max77686_hw_clk_info *clk_info;
 46};
 47
 48struct max77686_clk_driver_data {
 49	enum max77686_chip_name chip;
 50	struct max77686_clk_init_data *max_clk_data;
 51	size_t num_clks;
 52};
 53
 54static const struct
 55max77686_hw_clk_info max77686_hw_clks_info[MAX77686_CLKS_NUM] = {
 56	[MAX77686_CLK_AP] = {
 57		.name = "32khz_ap",
 58		.clk_reg = MAX77686_REG_32KHZ,
 59		.clk_enable_mask = BIT(MAX77686_CLK_AP),
 60	},
 61	[MAX77686_CLK_CP] = {
 62		.name = "32khz_cp",
 63		.clk_reg = MAX77686_REG_32KHZ,
 64		.clk_enable_mask = BIT(MAX77686_CLK_CP),
 65	},
 66	[MAX77686_CLK_PMIC] = {
 67		.name = "32khz_pmic",
 68		.clk_reg = MAX77686_REG_32KHZ,
 69		.clk_enable_mask = BIT(MAX77686_CLK_PMIC),
 70	},
 71};
 72
 73static const struct
 74max77686_hw_clk_info max77802_hw_clks_info[MAX77802_CLKS_NUM] = {
 75	[MAX77802_CLK_32K_AP] = {
 76		.name = "32khz_ap",
 77		.clk_reg = MAX77802_REG_32KHZ,
 78		.clk_enable_mask = BIT(MAX77802_CLK_32K_AP),
 79	},
 80	[MAX77802_CLK_32K_CP] = {
 81		.name = "32khz_cp",
 82		.clk_reg = MAX77802_REG_32KHZ,
 83		.clk_enable_mask = BIT(MAX77802_CLK_32K_CP),
 84	},
 85};
 86
 87static const struct
 88max77686_hw_clk_info max77620_hw_clks_info[MAX77620_CLKS_NUM] = {
 89	[MAX77620_CLK_32K_OUT0] = {
 90		.name = "32khz_out0",
 91		.clk_reg = MAX77620_REG_CNFG1_32K,
 92		.clk_enable_mask = MAX77620_CNFG1_32K_OUT0_EN,
 93	},
 94};
 95
 96static struct max77686_clk_init_data *to_max77686_clk_init_data(
 97				struct clk_hw *hw)
 98{
 99	return container_of(hw, struct max77686_clk_init_data, hw);
100}
101
102static int max77686_clk_prepare(struct clk_hw *hw)
103{
104	struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
105
106	return regmap_update_bits(max77686->regmap, max77686->clk_info->clk_reg,
107				  max77686->clk_info->clk_enable_mask,
108				  max77686->clk_info->clk_enable_mask);
109}
110
111static void max77686_clk_unprepare(struct clk_hw *hw)
112{
113	struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
114
115	regmap_update_bits(max77686->regmap, max77686->clk_info->clk_reg,
116			   max77686->clk_info->clk_enable_mask,
117			   ~max77686->clk_info->clk_enable_mask);
118}
119
120static int max77686_clk_is_prepared(struct clk_hw *hw)
121{
122	struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
123	int ret;
124	u32 val;
125
126	ret = regmap_read(max77686->regmap, max77686->clk_info->clk_reg, &val);
 
127
128	if (ret < 0)
129		return -EINVAL;
130
131	return val & max77686->clk_info->clk_enable_mask;
132}
133
134static unsigned long max77686_recalc_rate(struct clk_hw *hw,
135					  unsigned long parent_rate)
136{
137	return 32768;
138}
139
140static const struct clk_ops max77686_clk_ops = {
141	.prepare	= max77686_clk_prepare,
142	.unprepare	= max77686_clk_unprepare,
143	.is_prepared	= max77686_clk_is_prepared,
144	.recalc_rate	= max77686_recalc_rate,
145};
146
147static struct clk_hw *
148of_clk_max77686_get(struct of_phandle_args *clkspec, void *data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149{
150	struct max77686_clk_driver_data *drv_data = data;
151	unsigned int idx = clkspec->args[0];
 
 
 
 
152
153	if (idx >= drv_data->num_clks) {
154		pr_err("%s: invalid index %u\n", __func__, idx);
155		return ERR_PTR(-EINVAL);
156	}
 
 
 
 
157
158	return &drv_data->max_clk_data[idx].hw;
159}
160
161static int max77686_clk_probe(struct platform_device *pdev)
162{
163	struct device *dev = &pdev->dev;
164	struct device *parent = dev->parent;
165	const struct platform_device_id *id = platform_get_device_id(pdev);
166	struct max77686_clk_driver_data *drv_data;
167	const struct max77686_hw_clk_info *hw_clks;
168	struct regmap *regmap;
169	int i, ret, num_clks;
170
171	drv_data = devm_kzalloc(dev, sizeof(*drv_data), GFP_KERNEL);
172	if (!drv_data)
173		return -ENOMEM;
174
175	regmap = dev_get_regmap(parent, NULL);
176	if (!regmap) {
177		dev_err(dev, "Failed to get rtc regmap\n");
178		return -ENODEV;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179	}
180
181	drv_data->chip = id->driver_data;
 
 
 
182
183	switch (drv_data->chip) {
184	case CHIP_MAX77686:
185		num_clks = MAX77686_CLKS_NUM;
186		hw_clks = max77686_hw_clks_info;
187		break;
188
189	case CHIP_MAX77802:
190		num_clks = MAX77802_CLKS_NUM;
191		hw_clks = max77802_hw_clks_info;
192		break;
193
194	case CHIP_MAX77620:
195		num_clks = MAX77620_CLKS_NUM;
196		hw_clks = max77620_hw_clks_info;
197		break;
198
199	default:
200		dev_err(dev, "Unknown Chip ID\n");
201		return -EINVAL;
 
 
 
 
 
202	}
203
204	drv_data->num_clks = num_clks;
205	drv_data->max_clk_data = devm_kcalloc(dev, num_clks,
206					      sizeof(*drv_data->max_clk_data),
207					      GFP_KERNEL);
208	if (!drv_data->max_clk_data)
209		return -ENOMEM;
210
211	for (i = 0; i < num_clks; i++) {
212		struct max77686_clk_init_data *max_clk_data;
213		const char *clk_name;
214
215		max_clk_data = &drv_data->max_clk_data[i];
216
217		max_clk_data->regmap = regmap;
218		max_clk_data->clk_info = &hw_clks[i];
219		max_clk_data->clk_idata.flags = hw_clks[i].flags;
220		max_clk_data->clk_idata.ops = &max77686_clk_ops;
221
222		if (parent->of_node &&
223		    !of_property_read_string_index(parent->of_node,
224						   "clock-output-names",
225						   i, &clk_name))
226			max_clk_data->clk_idata.name = clk_name;
227		else
228			max_clk_data->clk_idata.name = hw_clks[i].name;
229
230		max_clk_data->hw.init = &max_clk_data->clk_idata;
 
231
232		ret = devm_clk_hw_register(dev, &max_clk_data->hw);
233		if (ret) {
234			dev_err(dev, "Failed to clock register: %d\n", ret);
235			return ret;
236		}
237
238		ret = devm_clk_hw_register_clkdev(dev, &max_clk_data->hw,
239						  max_clk_data->clk_idata.name,
240						  NULL);
241		if (ret < 0) {
242			dev_err(dev, "Failed to clkdev register: %d\n", ret);
243			return ret;
244		}
245	}
246
247	if (parent->of_node) {
248		ret = devm_of_clk_add_hw_provider(dev, of_clk_max77686_get,
249						  drv_data);
250
251		if (ret < 0) {
252			dev_err(dev, "Failed to register OF clock provider: %d\n",
253				ret);
254			return ret;
255		}
256	}
257
258	/* MAX77802: Enable low-jitter mode on the 32khz clocks. */
259	if (drv_data->chip == CHIP_MAX77802) {
260		ret = regmap_update_bits(regmap, MAX77802_REG_32KHZ,
261					 1 << MAX77802_CLOCK_LOW_JITTER_SHIFT,
262					 1 << MAX77802_CLOCK_LOW_JITTER_SHIFT);
263		if (ret < 0) {
264			dev_err(dev, "Failed to config low-jitter: %d\n", ret);
265			return ret;
266		}
267	}
268
269	return 0;
270}
271
272static const struct platform_device_id max77686_clk_id[] = {
273	{ "max77686-clk", .driver_data = CHIP_MAX77686, },
274	{ "max77802-clk", .driver_data = CHIP_MAX77802, },
275	{ "max77620-clock", .driver_data = CHIP_MAX77620, },
276	{},
277};
278MODULE_DEVICE_TABLE(platform, max77686_clk_id);
279
280static struct platform_driver max77686_clk_driver = {
281	.driver = {
282		.name  = "max77686-clk",
 
283	},
284	.probe = max77686_clk_probe,
 
285	.id_table = max77686_clk_id,
286};
287
288module_platform_driver(max77686_clk_driver);
 
 
 
 
 
 
 
 
 
 
289
290MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
291MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
292MODULE_LICENSE("GPL");