clk-s2mps11.c 7.74 KB
Newer Older
1 2 3
/*
 * clk-s2mps11.c - Clock driver for S2MPS11.
 *
4
 * Copyright (C) 2013,2014 Samsung Electornics
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/module.h>
#include <linux/err.h>
#include <linux/of.h>
#include <linux/clkdev.h>
#include <linux/regmap.h>
#include <linux/clk-provider.h>
#include <linux/platform_device.h>
#include <linux/mfd/samsung/s2mps11.h>
26
#include <linux/mfd/samsung/s2mps13.h>
27
#include <linux/mfd/samsung/s2mps14.h>
28
#include <linux/mfd/samsung/s5m8767.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <linux/mfd/samsung/core.h>

#define s2mps11_name(a) (a->hw.init->name)

static struct clk **clk_table;
static struct clk_onecell_data clk_data;

enum {
	S2MPS11_CLK_AP = 0,
	S2MPS11_CLK_CP,
	S2MPS11_CLK_BT,
	S2MPS11_CLKS_NUM,
};

struct s2mps11_clk {
	struct sec_pmic_dev *iodev;
45
	struct device_node *clk_np;
46 47 48 49
	struct clk_hw hw;
	struct clk *clk;
	struct clk_lookup *lookup;
	u32 mask;
50
	unsigned int reg;
51 52 53 54 55 56 57 58 59 60 61
};

static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw)
{
	return container_of(hw, struct s2mps11_clk, hw);
}

static int s2mps11_clk_prepare(struct clk_hw *hw)
{
	struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);

62
	return regmap_update_bits(s2mps11->iodev->regmap_pmic,
63
				 s2mps11->reg,
64 65 66 67 68 69 70
				 s2mps11->mask, s2mps11->mask);
}

static void s2mps11_clk_unprepare(struct clk_hw *hw)
{
	struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);

71
	regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg,
72 73 74
			   s2mps11->mask, ~s2mps11->mask);
}

Karol Wrona's avatar
Karol Wrona committed
75
static int s2mps11_clk_is_prepared(struct clk_hw *hw)
76
{
Karol Wrona's avatar
Karol Wrona committed
77 78
	int ret;
	u32 val;
79 80
	struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);

Karol Wrona's avatar
Karol Wrona committed
81 82 83 84 85 86
	ret = regmap_read(s2mps11->iodev->regmap_pmic,
				s2mps11->reg, &val);
	if (ret < 0)
		return -EINVAL;

	return val & s2mps11->mask;
87 88 89 90 91
}

static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw,
					     unsigned long parent_rate)
{
Karol Wrona's avatar
Karol Wrona committed
92
	return 32768;
93 94 95 96 97
}

static struct clk_ops s2mps11_clk_ops = {
	.prepare	= s2mps11_clk_prepare,
	.unprepare	= s2mps11_clk_unprepare,
Karol Wrona's avatar
Karol Wrona committed
98
	.is_prepared	= s2mps11_clk_is_prepared,
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	.recalc_rate	= s2mps11_clk_recalc_rate,
};

static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = {
	[S2MPS11_CLK_AP] = {
		.name = "s2mps11_ap",
		.ops = &s2mps11_clk_ops,
		.flags = CLK_IS_ROOT,
	},
	[S2MPS11_CLK_CP] = {
		.name = "s2mps11_cp",
		.ops = &s2mps11_clk_ops,
		.flags = CLK_IS_ROOT,
	},
	[S2MPS11_CLK_BT] = {
		.name = "s2mps11_bt",
		.ops = &s2mps11_clk_ops,
		.flags = CLK_IS_ROOT,
	},
};

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static struct clk_init_data s2mps13_clks_init[S2MPS11_CLKS_NUM] = {
	[S2MPS11_CLK_AP] = {
		.name = "s2mps13_ap",
		.ops = &s2mps11_clk_ops,
		.flags = CLK_IS_ROOT,
	},
	[S2MPS11_CLK_CP] = {
		.name = "s2mps13_cp",
		.ops = &s2mps11_clk_ops,
		.flags = CLK_IS_ROOT,
	},
	[S2MPS11_CLK_BT] = {
		.name = "s2mps13_bt",
		.ops = &s2mps11_clk_ops,
		.flags = CLK_IS_ROOT,
	},
};

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
static struct clk_init_data s2mps14_clks_init[S2MPS11_CLKS_NUM] = {
	[S2MPS11_CLK_AP] = {
		.name = "s2mps14_ap",
		.ops = &s2mps11_clk_ops,
		.flags = CLK_IS_ROOT,
	},
	[S2MPS11_CLK_BT] = {
		.name = "s2mps14_bt",
		.ops = &s2mps11_clk_ops,
		.flags = CLK_IS_ROOT,
	},
};

static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev,
		struct clk_init_data *clks_init)
153 154 155 156 157 158
{
	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
	struct device_node *clk_np;
	int i;

	if (!iodev->dev->of_node)
159
		return ERR_PTR(-EINVAL);
160

161
	clk_np = of_get_child_by_name(iodev->dev->of_node, "clocks");
162 163 164 165 166
	if (!clk_np) {
		dev_err(&pdev->dev, "could not find clock sub-node\n");
		return ERR_PTR(-EINVAL);
	}

167 168 169
	for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
		if (!clks_init[i].name)
			continue; /* Skip clocks not present in some devices */
170
		of_property_read_string_index(clk_np, "clock-output-names", i,
171 172
				&clks_init[i].name);
	}
173 174 175 176 177 178 179 180

	return clk_np;
}

static int s2mps11_clk_probe(struct platform_device *pdev)
{
	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
	struct s2mps11_clk *s2mps11_clks, *s2mps11_clk;
181
	unsigned int s2mps11_reg;
182
	struct clk_init_data *clks_init;
183 184
	int i, ret = 0;

185 186
	s2mps11_clks = devm_kcalloc(&pdev->dev, S2MPS11_CLKS_NUM,
				sizeof(*s2mps11_clk), GFP_KERNEL);
187 188 189 190 191
	if (!s2mps11_clks)
		return -ENOMEM;

	s2mps11_clk = s2mps11_clks;

192 193
	clk_table = devm_kcalloc(&pdev->dev, S2MPS11_CLKS_NUM,
				sizeof(struct clk *), GFP_KERNEL);
194 195 196
	if (!clk_table)
		return -ENOMEM;

197 198 199
	switch(platform_get_device_id(pdev)->driver_data) {
	case S2MPS11X:
		s2mps11_reg = S2MPS11_REG_RTC_CTRL;
200 201
		clks_init = s2mps11_clks_init;
		break;
202 203 204 205
	case S2MPS13X:
		s2mps11_reg = S2MPS13_REG_RTCCTRL;
		clks_init = s2mps13_clks_init;
		break;
206 207 208
	case S2MPS14X:
		s2mps11_reg = S2MPS14_REG_RTCCTRL;
		clks_init = s2mps14_clks_init;
209
		break;
210 211
	case S5M8767X:
		s2mps11_reg = S5M8767_REG_CTRL1;
212
		clks_init = s2mps11_clks_init;
213
		break;
214 215 216
	default:
		dev_err(&pdev->dev, "Invalid device type\n");
		return -EINVAL;
217
	}
218

219 220 221 222 223
	/* Store clocks of_node in first element of s2mps11_clks array */
	s2mps11_clks->clk_np = s2mps11_clk_parse_dt(pdev, clks_init);
	if (IS_ERR(s2mps11_clks->clk_np))
		return PTR_ERR(s2mps11_clks->clk_np);

224
	for (i = 0; i < S2MPS11_CLKS_NUM; i++, s2mps11_clk++) {
225 226
		if (!clks_init[i].name)
			continue; /* Skip clocks not present in some devices */
227
		s2mps11_clk->iodev = iodev;
228
		s2mps11_clk->hw.init = &clks_init[i];
229
		s2mps11_clk->mask = 1 << i;
230
		s2mps11_clk->reg = s2mps11_reg;
231 232 233 234 235 236 237 238 239 240

		s2mps11_clk->clk = devm_clk_register(&pdev->dev,
							&s2mps11_clk->hw);
		if (IS_ERR(s2mps11_clk->clk)) {
			dev_err(&pdev->dev, "Fail to register : %s\n",
						s2mps11_name(s2mps11_clk));
			ret = PTR_ERR(s2mps11_clk->clk);
			goto err_reg;
		}

241
		s2mps11_clk->lookup = clkdev_create(s2mps11_clk->clk,
242
					s2mps11_name(s2mps11_clk), NULL);
243 244
		if (!s2mps11_clk->lookup) {
			ret = -ENOMEM;
245
			goto err_reg;
246 247 248
		}
	}

249 250 251 252
	for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
		/* Skip clocks not present on S2MPS14 */
		if (!clks_init[i].name)
			continue;
253
		clk_table[i] = s2mps11_clks[i].clk;
254
	}
255

256 257 258 259
	clk_data.clks = clk_table;
	clk_data.clk_num = S2MPS11_CLKS_NUM;
	of_clk_add_provider(s2mps11_clks->clk_np, of_clk_src_onecell_get,
			&clk_data);
260 261 262 263

	platform_set_drvdata(pdev, s2mps11_clks);

	return ret;
264

265
err_reg:
266 267
	while (--i >= 0)
		clkdev_drop(s2mps11_clks[i].lookup);
268 269 270 271 272 273 274 275 276

	return ret;
}

static int s2mps11_clk_remove(struct platform_device *pdev)
{
	struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
	int i;

277 278 279 280
	of_clk_del_provider(s2mps11_clks[0].clk_np);
	/* Drop the reference obtained in s2mps11_clk_parse_dt */
	of_node_put(s2mps11_clks[0].clk_np);

281 282 283 284
	for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
		/* Skip clocks not present on S2MPS14 */
		if (!s2mps11_clks[i].lookup)
			continue;
285
		clkdev_drop(s2mps11_clks[i].lookup);
286
	}
287 288 289 290 291

	return 0;
}

static const struct platform_device_id s2mps11_clk_id[] = {
292
	{ "s2mps11-clk", S2MPS11X},
293
	{ "s2mps13-clk", S2MPS13X},
294
	{ "s2mps14-clk", S2MPS14X},
295
	{ "s5m8767-clk", S5M8767X},
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	{ },
};
MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);

static struct platform_driver s2mps11_clk_driver = {
	.driver = {
		.name  = "s2mps11-clk",
	},
	.probe = s2mps11_clk_probe,
	.remove = s2mps11_clk_remove,
	.id_table = s2mps11_clk_id,
};

static int __init s2mps11_clk_init(void)
{
	return platform_driver_register(&s2mps11_clk_driver);
}
subsys_initcall(s2mps11_clk_init);

315
static void __exit s2mps11_clk_cleanup(void)
316 317 318 319 320 321 322 323
{
	platform_driver_unregister(&s2mps11_clk_driver);
}
module_exit(s2mps11_clk_cleanup);

MODULE_DESCRIPTION("S2MPS11 Clock Driver");
MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
MODULE_LICENSE("GPL");