mxs: forward-port regulator patches into 4.4
[openwrt/openwrt.git] / target / linux / mxs / patches-4.4 / 101-mxs-add-mxs_power.patch
1 --- a/drivers/power/Kconfig
2 +++ b/drivers/power/Kconfig
3 @@ -43,6 +43,14 @@ config MAX8925_POWER
4 Say Y here to enable support for the battery charger in the Maxim
5 MAX8925 PMIC.
6
7 +config MXS_POWER
8 + tristate "Freescale MXS power subsystem support"
9 + depends on ARCH_MXS || COMPILE_TEST
10 + help
11 + Say Y here to enable support for the Freescale i.MX23/i.MX28
12 + power subsystem. This is a requirement to get access to on-chip
13 + regulators, battery charger and many more.
14 +
15 config WM831X_BACKUP
16 tristate "WM831X backup battery charger support"
17 depends on MFD_WM831X
18 --- a/drivers/power/Makefile
19 +++ b/drivers/power/Makefile
20 @@ -10,6 +10,7 @@ obj-$(CONFIG_GENERIC_ADC_BATTERY) += gen
21 obj-$(CONFIG_PDA_POWER) += pda_power.o
22 obj-$(CONFIG_APM_POWER) += apm_power.o
23 obj-$(CONFIG_MAX8925_POWER) += max8925_power.o
24 +obj-$(CONFIG_MXS_POWER) += mxs_power.o
25 obj-$(CONFIG_WM831X_BACKUP) += wm831x_backup.o
26 obj-$(CONFIG_WM831X_POWER) += wm831x_power.o
27 obj-$(CONFIG_WM8350_POWER) += wm8350_power.o
28 --- /dev/null
29 +++ b/drivers/power/mxs_power.c
30 @@ -0,0 +1,136 @@
31 +/*
32 + * Freescale MXS power subsystem
33 + *
34 + * Copyright (C) 2014 Stefan Wahren
35 + *
36 + * Inspired by imx-bootlets
37 + */
38 +
39 +/*
40 + * The code contained herein is licensed under the GNU General Public
41 + * License. You may obtain a copy of the GNU General Public License
42 + * Version 2 or later at the following locations:
43 + *
44 + * http://www.opensource.org/licenses/gpl-license.html
45 + * http://www.gnu.org/copyleft/gpl.html
46 + */
47 +
48 +#include <linux/device.h>
49 +#include <linux/err.h>
50 +#include <linux/io.h>
51 +#include <linux/kernel.h>
52 +#include <linux/module.h>
53 +#include <linux/of.h>
54 +#include <linux/of_platform.h>
55 +#include <linux/platform_device.h>
56 +#include <linux/power_supply.h>
57 +#include <linux/stmp_device.h>
58 +#include <linux/types.h>
59 +
60 +#define BM_POWER_CTRL_POLARITY_VBUSVALID BIT(5)
61 +#define BM_POWER_CTRL_VBUSVALID_IRQ BIT(4)
62 +#define BM_POWER_CTRL_ENIRQ_VBUS_VALID BIT(3)
63 +
64 +#define HW_POWER_5VCTRL_OFFSET 0x10
65 +
66 +#define BM_POWER_5VCTRL_VBUSVALID_THRESH (7 << 8)
67 +#define BM_POWER_5VCTRL_PWDN_5VBRNOUT BIT(7)
68 +#define BM_POWER_5VCTRL_ENABLE_LINREG_ILIMIT BIT(6)
69 +#define BM_POWER_5VCTRL_VBUSVALID_5VDETECT BIT(4)
70 +
71 +#define HW_POWER_5VCTRL_VBUSVALID_THRESH_4_40V (5 << 8)
72 +
73 +struct mxs_power_data {
74 + void __iomem *base_addr;
75 + struct power_supply *ac;
76 +};
77 +
78 +static enum power_supply_property mxs_power_ac_props[] = {
79 + POWER_SUPPLY_PROP_ONLINE,
80 +};
81 +
82 +static int mxs_power_ac_get_property(struct power_supply *psy,
83 + enum power_supply_property psp,
84 + union power_supply_propval *val)
85 +{
86 + int ret = 0;
87 +
88 + switch (psp) {
89 + case POWER_SUPPLY_PROP_ONLINE:
90 + val->intval = 1;
91 + break;
92 + default:
93 + ret = -EINVAL;
94 + break;
95 + }
96 + return ret;
97 +}
98 +
99 +static const struct of_device_id of_mxs_power_match[] = {
100 + { .compatible = "fsl,imx23-power" },
101 + { .compatible = "fsl,imx28-power" },
102 + { /* end */ }
103 +};
104 +MODULE_DEVICE_TABLE(of, of_mxs_power_match);
105 +
106 +static const struct power_supply_desc ac_desc = {
107 + .properties = mxs_power_ac_props,
108 + .num_properties = ARRAY_SIZE(mxs_power_ac_props),
109 + .get_property = mxs_power_ac_get_property,
110 + .name = "ac",
111 + .type = POWER_SUPPLY_TYPE_MAINS,
112 +};
113 +
114 +static int mxs_power_probe(struct platform_device *pdev)
115 +{
116 + struct device *dev = &pdev->dev;
117 + struct device_node *np = dev->of_node;
118 + struct resource *res;
119 + struct mxs_power_data *data;
120 + struct power_supply_config psy_cfg = {};
121 + void __iomem *v5ctrl_addr;
122 +
123 + if (!np) {
124 + dev_err(dev, "missing device tree\n");
125 + return -EINVAL;
126 + }
127 +
128 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
129 + if (!data)
130 + return -ENOMEM;
131 +
132 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 + data->base_addr = devm_ioremap_resource(dev, res);
134 + if (IS_ERR(data->base_addr))
135 + return PTR_ERR(data->base_addr);
136 +
137 + psy_cfg.drv_data = data;
138 +
139 + data->ac = devm_power_supply_register(dev, &ac_desc, &psy_cfg);
140 + if (IS_ERR(data->ac))
141 + return PTR_ERR(data->ac);
142 +
143 + platform_set_drvdata(pdev, data);
144 +
145 + v5ctrl_addr = data->base_addr + HW_POWER_5VCTRL_OFFSET;
146 +
147 + /* Make sure the current limit of the linregs are disabled. */
148 + writel(BM_POWER_5VCTRL_ENABLE_LINREG_ILIMIT,
149 + v5ctrl_addr + STMP_OFFSET_REG_CLR);
150 +
151 + return of_platform_populate(np, NULL, NULL, dev);
152 +}
153 +
154 +static struct platform_driver mxs_power_driver = {
155 + .driver = {
156 + .name = "mxs_power",
157 + .of_match_table = of_mxs_power_match,
158 + },
159 + .probe = mxs_power_probe,
160 +};
161 +
162 +module_platform_driver(mxs_power_driver);
163 +
164 +MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
165 +MODULE_DESCRIPTION("Freescale MXS power subsystem");
166 +MODULE_LICENSE("GPL v2");