blob: 0281c31ae2edc0abd22ac44e4407d2c3fd9d6751 [file] [log] [blame]
Yong Shen167e3d82010-12-14 14:00:54 +08001/*
2 * Regulator Driver for Freescale MC13xxx PMIC
3 *
4 * Copyright 2010 Yong Shen <yong.shen@linaro.org>
5 *
6 * Based on mc13783 regulator driver :
7 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
8 * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file
15 * from freescale
16 */
17
18#include <linux/mfd/mc13xxx.h>
19#include <linux/regulator/machine.h>
20#include <linux/regulator/driver.h>
Shawn Guo93bcb232011-12-21 23:00:46 +080021#include <linux/regulator/of_regulator.h>
Yong Shen167e3d82010-12-14 14:00:54 +080022#include <linux/platform_device.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/err.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040027#include <linux/module.h>
Shawn Guo93bcb232011-12-21 23:00:46 +080028#include <linux/of.h>
Yong Shen167e3d82010-12-14 14:00:54 +080029#include "mc13xxx.h"
30
31static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
32{
33 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
34 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
35 int id = rdev_get_id(rdev);
Yong Shen167e3d82010-12-14 14:00:54 +080036
37 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
38
Alexander Shiyan7dd33c12014-06-08 15:26:29 +040039 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
40 mc13xxx_regulators[id].enable_bit,
41 mc13xxx_regulators[id].enable_bit);
Yong Shen167e3d82010-12-14 14:00:54 +080042}
43
44static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
45{
46 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
47 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
48 int id = rdev_get_id(rdev);
Yong Shen167e3d82010-12-14 14:00:54 +080049
50 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
51
Alexander Shiyan7dd33c12014-06-08 15:26:29 +040052 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
53 mc13xxx_regulators[id].enable_bit, 0);
Yong Shen167e3d82010-12-14 14:00:54 +080054}
55
56static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
57{
58 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
59 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
60 int ret, id = rdev_get_id(rdev);
61 unsigned int val;
62
Yong Shen167e3d82010-12-14 14:00:54 +080063 ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
Yong Shen167e3d82010-12-14 14:00:54 +080064 if (ret)
65 return ret;
66
67 return (val & mc13xxx_regulators[id].enable_bit) != 0;
68}
69
Axel Lin939b62d2012-03-20 10:46:35 +080070static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev,
71 unsigned selector)
Yong Shen167e3d82010-12-14 14:00:54 +080072{
73 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
74 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
Axel Lin939b62d2012-03-20 10:46:35 +080075 int id = rdev_get_id(rdev);
Yong Shen167e3d82010-12-14 14:00:54 +080076
Alexander Shiyan7dd33c12014-06-08 15:26:29 +040077 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
78 mc13xxx_regulators[id].vsel_mask,
79 selector << mc13xxx_regulators[id].vsel_shift);
Yong Shen167e3d82010-12-14 14:00:54 +080080}
81
82static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
83{
84 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
85 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
86 int ret, id = rdev_get_id(rdev);
87 unsigned int val;
88
89 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
90
Yong Shen167e3d82010-12-14 14:00:54 +080091 ret = mc13xxx_reg_read(priv->mc13xxx,
92 mc13xxx_regulators[id].vsel_reg, &val);
Yong Shen167e3d82010-12-14 14:00:54 +080093 if (ret)
94 return ret;
95
96 val = (val & mc13xxx_regulators[id].vsel_mask)
97 >> mc13xxx_regulators[id].vsel_shift;
98
99 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
100
Axel Lin3c240192011-05-18 20:56:45 +0800101 BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages);
Yong Shen167e3d82010-12-14 14:00:54 +0800102
Axel Lin34e74f32012-06-08 15:41:48 +0800103 return rdev->desc->volt_table[val];
Yong Shen167e3d82010-12-14 14:00:54 +0800104}
105
106struct regulator_ops mc13xxx_regulator_ops = {
107 .enable = mc13xxx_regulator_enable,
108 .disable = mc13xxx_regulator_disable,
109 .is_enabled = mc13xxx_regulator_is_enabled,
Axel Lin34e74f32012-06-08 15:41:48 +0800110 .list_voltage = regulator_list_voltage_table,
Axel Lin939b62d2012-03-20 10:46:35 +0800111 .set_voltage_sel = mc13xxx_regulator_set_voltage_sel,
Yong Shen167e3d82010-12-14 14:00:54 +0800112 .get_voltage = mc13xxx_regulator_get_voltage,
113};
Mark Brown4d7071f2010-12-15 14:10:25 +0000114EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
Yong Shen167e3d82010-12-14 14:00:54 +0800115
116int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
117 int max_uV, unsigned *selector)
118{
Yong Shen167e3d82010-12-14 14:00:54 +0800119 int id = rdev_get_id(rdev);
120
121 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
122 __func__, id, min_uV, max_uV);
123
Axel Lin34e74f32012-06-08 15:41:48 +0800124 if (min_uV <= rdev->desc->volt_table[0] &&
Axel Lin13407ea2012-07-13 23:01:14 +0800125 rdev->desc->volt_table[0] <= max_uV) {
126 *selector = 0;
Yong Shen167e3d82010-12-14 14:00:54 +0800127 return 0;
Axel Lin13407ea2012-07-13 23:01:14 +0800128 } else {
Yong Shen167e3d82010-12-14 14:00:54 +0800129 return -EINVAL;
Axel Lin13407ea2012-07-13 23:01:14 +0800130 }
Yong Shen167e3d82010-12-14 14:00:54 +0800131}
Mark Brown4d7071f2010-12-15 14:10:25 +0000132EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
Yong Shen167e3d82010-12-14 14:00:54 +0800133
Yong Shen167e3d82010-12-14 14:00:54 +0800134struct regulator_ops mc13xxx_fixed_regulator_ops = {
135 .enable = mc13xxx_regulator_enable,
136 .disable = mc13xxx_regulator_disable,
137 .is_enabled = mc13xxx_regulator_is_enabled,
Axel Lin34e74f32012-06-08 15:41:48 +0800138 .list_voltage = regulator_list_voltage_table,
Yong Shen167e3d82010-12-14 14:00:54 +0800139 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
Yong Shen167e3d82010-12-14 14:00:54 +0800140};
Mark Brown4d7071f2010-12-15 14:10:25 +0000141EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
Yong Shen167e3d82010-12-14 14:00:54 +0800142
Shawn Guo93bcb232011-12-21 23:00:46 +0800143#ifdef CONFIG_OF
Bill Pembertona5023572012-11-19 13:22:22 -0500144int mc13xxx_get_num_regulators_dt(struct platform_device *pdev)
Shawn Guo93bcb232011-12-21 23:00:46 +0800145{
Axel Lin86f66732013-01-30 20:54:49 +0800146 struct device_node *parent;
147 int num;
Shawn Guo93bcb232011-12-21 23:00:46 +0800148
Alexander Shiyanb431e692014-03-02 11:44:35 +0400149 if (!pdev->dev.parent->of_node)
Sachin Kamatbf7f8822014-02-25 16:49:27 +0530150 return -ENODEV;
151
Alexander Shiyanb431e692014-03-02 11:44:35 +0400152 parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
Shawn Guo93bcb232011-12-21 23:00:46 +0800153 if (!parent)
154 return -ENODEV;
155
Axel Lin86f66732013-01-30 20:54:49 +0800156 num = of_get_child_count(parent);
Axel Linc92f5dd2013-01-27 21:16:56 +0800157 of_node_put(parent);
Shawn Guo93bcb232011-12-21 23:00:46 +0800158 return num;
159}
David Miller53269162012-02-09 16:43:01 -0500160EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt);
Shawn Guo93bcb232011-12-21 23:00:46 +0800161
Bill Pembertona5023572012-11-19 13:22:22 -0500162struct mc13xxx_regulator_init_data *mc13xxx_parse_regulators_dt(
Shawn Guo93bcb232011-12-21 23:00:46 +0800163 struct platform_device *pdev, struct mc13xxx_regulator *regulators,
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400164 int num_regulators)
Shawn Guo93bcb232011-12-21 23:00:46 +0800165{
166 struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
167 struct mc13xxx_regulator_init_data *data, *p;
168 struct device_node *parent, *child;
Matt Sealey2c8a5dc2013-01-21 12:25:45 -0600169 int i, parsed = 0;
170
Alexander Shiyanb431e692014-03-02 11:44:35 +0400171 if (!pdev->dev.parent->of_node)
Sachin Kamatbf7f8822014-02-25 16:49:27 +0530172 return NULL;
173
Alexander Shiyanb431e692014-03-02 11:44:35 +0400174 parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
Shawn Guo93bcb232011-12-21 23:00:46 +0800175 if (!parent)
176 return NULL;
177
178 data = devm_kzalloc(&pdev->dev, sizeof(*data) * priv->num_regulators,
179 GFP_KERNEL);
Axel Linc92f5dd2013-01-27 21:16:56 +0800180 if (!data) {
181 of_node_put(parent);
Shawn Guo93bcb232011-12-21 23:00:46 +0800182 return NULL;
Axel Linc92f5dd2013-01-27 21:16:56 +0800183 }
184
Shawn Guo93bcb232011-12-21 23:00:46 +0800185 p = data;
186
187 for_each_child_of_node(parent, child) {
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400188 int found = 0;
189
Shawn Guo93bcb232011-12-21 23:00:46 +0800190 for (i = 0; i < num_regulators; i++) {
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400191 if (!regulators[i].desc.name)
192 continue;
Shawn Guo93bcb232011-12-21 23:00:46 +0800193 if (!of_node_cmp(child->name,
194 regulators[i].desc.name)) {
195 p->id = i;
196 p->init_data = of_get_regulator_init_data(
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100197 &pdev->dev, child,
198 &regulators[i].desc);
Shawn Guo93bcb232011-12-21 23:00:46 +0800199 p->node = child;
200 p++;
Matt Sealey2c8a5dc2013-01-21 12:25:45 -0600201
202 parsed++;
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400203 found = 1;
Shawn Guo93bcb232011-12-21 23:00:46 +0800204 break;
205 }
206 }
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400207
208 if (!found)
209 dev_warn(&pdev->dev,
210 "Unknown regulator: %s\n", child->name);
Shawn Guo93bcb232011-12-21 23:00:46 +0800211 }
Axel Linc92f5dd2013-01-27 21:16:56 +0800212 of_node_put(parent);
Shawn Guo93bcb232011-12-21 23:00:46 +0800213
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400214 priv->num_regulators = parsed;
215
Shawn Guo93bcb232011-12-21 23:00:46 +0800216 return data;
217}
David Miller53269162012-02-09 16:43:01 -0500218EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt);
Shawn Guo93bcb232011-12-21 23:00:46 +0800219#endif
220
Yong Shen167e3d82010-12-14 14:00:54 +0800221MODULE_LICENSE("GPL v2");
222MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
223MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
224MODULE_ALIAS("mc13xxx-regulator-core");