gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / regulator / wm831x-isink.c
blobff3d2bf504107b87ad7149ef05be6457fbc1e22b
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // wm831x-isink.c -- Current sink driver for the WM831x series
4 //
5 // Copyright 2009 Wolfson Microelectronics PLC.
6 //
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/bitops.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/slab.h>
19 #include <linux/mfd/wm831x/core.h>
20 #include <linux/mfd/wm831x/regulator.h>
21 #include <linux/mfd/wm831x/pdata.h>
23 #define WM831X_ISINK_MAX_NAME 7
25 struct wm831x_isink {
26 char name[WM831X_ISINK_MAX_NAME];
27 struct regulator_desc desc;
28 int reg;
29 struct wm831x *wm831x;
30 struct regulator_dev *regulator;
33 static int wm831x_isink_enable(struct regulator_dev *rdev)
35 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
36 struct wm831x *wm831x = isink->wm831x;
37 int ret;
39 /* We have a two stage enable: first start the ISINK... */
40 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA,
41 WM831X_CS1_ENA);
42 if (ret != 0)
43 return ret;
45 /* ...then enable drive */
46 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE,
47 WM831X_CS1_DRIVE);
48 if (ret != 0)
49 wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0);
51 return ret;
55 static int wm831x_isink_disable(struct regulator_dev *rdev)
57 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
58 struct wm831x *wm831x = isink->wm831x;
59 int ret;
61 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE, 0);
62 if (ret < 0)
63 return ret;
65 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0);
66 if (ret < 0)
67 return ret;
69 return ret;
73 static int wm831x_isink_is_enabled(struct regulator_dev *rdev)
75 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
76 struct wm831x *wm831x = isink->wm831x;
77 int ret;
79 ret = wm831x_reg_read(wm831x, isink->reg);
80 if (ret < 0)
81 return ret;
83 if ((ret & (WM831X_CS1_ENA | WM831X_CS1_DRIVE)) ==
84 (WM831X_CS1_ENA | WM831X_CS1_DRIVE))
85 return 1;
86 else
87 return 0;
90 static const struct regulator_ops wm831x_isink_ops = {
91 .is_enabled = wm831x_isink_is_enabled,
92 .enable = wm831x_isink_enable,
93 .disable = wm831x_isink_disable,
94 .set_current_limit = regulator_set_current_limit_regmap,
95 .get_current_limit = regulator_get_current_limit_regmap,
98 static irqreturn_t wm831x_isink_irq(int irq, void *data)
100 struct wm831x_isink *isink = data;
102 regulator_lock(isink->regulator);
103 regulator_notifier_call_chain(isink->regulator,
104 REGULATOR_EVENT_OVER_CURRENT,
105 NULL);
106 regulator_unlock(isink->regulator);
108 return IRQ_HANDLED;
112 static int wm831x_isink_probe(struct platform_device *pdev)
114 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
115 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
116 struct wm831x_isink *isink;
117 int id = pdev->id % ARRAY_SIZE(pdata->isink);
118 struct regulator_config config = { };
119 struct resource *res;
120 int ret, irq;
122 dev_dbg(&pdev->dev, "Probing ISINK%d\n", id + 1);
124 if (pdata == NULL || pdata->isink[id] == NULL)
125 return -ENODEV;
127 isink = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_isink),
128 GFP_KERNEL);
129 if (!isink)
130 return -ENOMEM;
132 isink->wm831x = wm831x;
134 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
135 if (res == NULL) {
136 dev_err(&pdev->dev, "No REG resource\n");
137 ret = -EINVAL;
138 goto err;
140 isink->reg = res->start;
142 /* For current parts this is correct; probably need to revisit
143 * in future.
145 snprintf(isink->name, sizeof(isink->name), "ISINK%d", id + 1);
146 isink->desc.name = isink->name;
147 isink->desc.id = id;
148 isink->desc.ops = &wm831x_isink_ops;
149 isink->desc.type = REGULATOR_CURRENT;
150 isink->desc.owner = THIS_MODULE;
151 isink->desc.curr_table = wm831x_isinkv_values,
152 isink->desc.n_current_limits = ARRAY_SIZE(wm831x_isinkv_values),
153 isink->desc.csel_reg = isink->reg,
154 isink->desc.csel_mask = WM831X_CS1_ISEL_MASK,
156 config.dev = pdev->dev.parent;
157 config.init_data = pdata->isink[id];
158 config.driver_data = isink;
159 config.regmap = wm831x->regmap;
161 isink->regulator = devm_regulator_register(&pdev->dev, &isink->desc,
162 &config);
163 if (IS_ERR(isink->regulator)) {
164 ret = PTR_ERR(isink->regulator);
165 dev_err(wm831x->dev, "Failed to register ISINK%d: %d\n",
166 id + 1, ret);
167 goto err;
170 irq = wm831x_irq(wm831x, platform_get_irq(pdev, 0));
171 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
172 wm831x_isink_irq,
173 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
174 isink->name,
175 isink);
176 if (ret != 0) {
177 dev_err(&pdev->dev, "Failed to request ISINK IRQ %d: %d\n",
178 irq, ret);
179 goto err;
182 platform_set_drvdata(pdev, isink);
184 return 0;
186 err:
187 return ret;
190 static struct platform_driver wm831x_isink_driver = {
191 .probe = wm831x_isink_probe,
192 .driver = {
193 .name = "wm831x-isink",
197 static int __init wm831x_isink_init(void)
199 int ret;
200 ret = platform_driver_register(&wm831x_isink_driver);
201 if (ret != 0)
202 pr_err("Failed to register WM831x ISINK driver: %d\n", ret);
204 return ret;
206 subsys_initcall(wm831x_isink_init);
208 static void __exit wm831x_isink_exit(void)
210 platform_driver_unregister(&wm831x_isink_driver);
212 module_exit(wm831x_isink_exit);
214 /* Module information */
215 MODULE_AUTHOR("Mark Brown");
216 MODULE_DESCRIPTION("WM831x current sink driver");
217 MODULE_LICENSE("GPL");
218 MODULE_ALIAS("platform:wm831x-isink");