1/*
2 * sky81452-regulator.c	SKY81452 regulator driver
3 *
4 * Copyright 2014 Skyworks Solutions Inc.
5 * Author : Gyungoh Yoo <jack.yoo@skyworksinc.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, or (at your option) any
10 * later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/platform_device.h>
24#include <linux/init.h>
25#include <linux/err.h>
26#include <linux/of.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/of_regulator.h>
29
30/* registers */
31#define SKY81452_REG1	0x01
32#define SKY81452_REG3	0x03
33
34/* bit mask */
35#define SKY81452_LEN	0x40
36#define SKY81452_LOUT	0x1F
37
38static struct regulator_ops sky81452_reg_ops = {
39	.list_voltage = regulator_list_voltage_linear_range,
40	.map_voltage = regulator_map_voltage_linear_range,
41	.get_voltage_sel = regulator_get_voltage_sel_regmap,
42	.set_voltage_sel = regulator_set_voltage_sel_regmap,
43	.enable = regulator_enable_regmap,
44	.disable = regulator_disable_regmap,
45	.is_enabled = regulator_is_enabled_regmap,
46};
47
48static const struct regulator_linear_range sky81452_reg_ranges[] = {
49	REGULATOR_LINEAR_RANGE(4500000, 0, 14, 250000),
50	REGULATOR_LINEAR_RANGE(9000000, 15, 31, 1000000),
51};
52
53static const struct regulator_desc sky81452_reg = {
54	.name = "LOUT",
55	.ops = &sky81452_reg_ops,
56	.type = REGULATOR_VOLTAGE,
57	.owner = THIS_MODULE,
58	.n_voltages = SKY81452_LOUT + 1,
59	.linear_ranges = sky81452_reg_ranges,
60	.n_linear_ranges = ARRAY_SIZE(sky81452_reg_ranges),
61	.vsel_reg = SKY81452_REG3,
62	.vsel_mask = SKY81452_LOUT,
63	.enable_reg = SKY81452_REG1,
64	.enable_mask = SKY81452_LEN,
65};
66
67#ifdef CONFIG_OF
68static struct regulator_init_data *sky81452_reg_parse_dt(struct device *dev)
69{
70	struct regulator_init_data *init_data;
71	struct device_node *np;
72
73	np = of_get_child_by_name(dev->parent->of_node, "regulator");
74	if (unlikely(!np)) {
75		dev_err(dev, "regulator node not found");
76		return NULL;
77	}
78
79	init_data = of_get_regulator_init_data(dev, np);
80
81	of_node_put(np);
82	return init_data;
83}
84#else
85static struct regulator_init_data *sky81452_reg_parse_dt(struct device *dev)
86{
87	return ERR_PTR(-EINVAL);
88}
89#endif
90
91static int sky81452_reg_probe(struct platform_device *pdev)
92{
93	struct device *dev = &pdev->dev;
94	const struct regulator_init_data *init_data = dev_get_platdata(dev);
95	struct regulator_config config = { };
96	struct regulator_dev *rdev;
97
98	if (!init_data) {
99		init_data = sky81452_reg_parse_dt(dev);
100		if (IS_ERR(init_data))
101			return PTR_ERR(init_data);
102	}
103
104	config.dev = dev;
105	config.init_data = init_data;
106	config.of_node = dev->of_node;
107	config.regmap = dev_get_drvdata(dev->parent);
108
109	rdev = devm_regulator_register(dev, &sky81452_reg, &config);
110	if (IS_ERR(rdev))
111		return PTR_ERR(rdev);
112
113	platform_set_drvdata(pdev, rdev);
114
115	return 0;
116}
117
118static struct platform_driver sky81452_reg_driver = {
119	.driver = {
120		.name = "sky81452-regulator",
121	},
122	.probe = sky81452_reg_probe,
123};
124
125module_platform_driver(sky81452_reg_driver);
126
127MODULE_DESCRIPTION("Skyworks SKY81452 Regulator driver");
128MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@skyworksinc.com>");
129MODULE_LICENSE("GPL");
130MODULE_VERSION("1.0");
131