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>
8
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>
18
19 #include <linux/mfd/wm831x/core.h>
20 #include <linux/mfd/wm831x/regulator.h>
21 #include <linux/mfd/wm831x/pdata.h>
22
23 #define WM831X_ISINK_MAX_NAME 7
24
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;
31 };
32
wm831x_isink_enable(struct regulator_dev * rdev)33 static int wm831x_isink_enable(struct regulator_dev *rdev)
34 {
35 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
36 struct wm831x *wm831x = isink->wm831x;
37 int ret;
38
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;
44
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);
50
51 return ret;
52
53 }
54
wm831x_isink_disable(struct regulator_dev * rdev)55 static int wm831x_isink_disable(struct regulator_dev *rdev)
56 {
57 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
58 struct wm831x *wm831x = isink->wm831x;
59 int ret;
60
61 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE, 0);
62 if (ret < 0)
63 return ret;
64
65 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0);
66 if (ret < 0)
67 return ret;
68
69 return ret;
70
71 }
72
wm831x_isink_is_enabled(struct regulator_dev * rdev)73 static int wm831x_isink_is_enabled(struct regulator_dev *rdev)
74 {
75 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
76 struct wm831x *wm831x = isink->wm831x;
77 int ret;
78
79 ret = wm831x_reg_read(wm831x, isink->reg);
80 if (ret < 0)
81 return ret;
82
83 if ((ret & (WM831X_CS1_ENA | WM831X_CS1_DRIVE)) ==
84 (WM831X_CS1_ENA | WM831X_CS1_DRIVE))
85 return 1;
86 else
87 return 0;
88 }
89
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,
96 };
97
wm831x_isink_irq(int irq,void * data)98 static irqreturn_t wm831x_isink_irq(int irq, void *data)
99 {
100 struct wm831x_isink *isink = data;
101
102 regulator_notifier_call_chain(isink->regulator,
103 REGULATOR_EVENT_OVER_CURRENT,
104 NULL);
105
106 return IRQ_HANDLED;
107 }
108
109
wm831x_isink_probe(struct platform_device * pdev)110 static int wm831x_isink_probe(struct platform_device *pdev)
111 {
112 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
113 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
114 struct wm831x_isink *isink;
115 int id = pdev->id % ARRAY_SIZE(pdata->isink);
116 struct regulator_config config = { };
117 struct resource *res;
118 int ret, irq;
119
120 dev_dbg(&pdev->dev, "Probing ISINK%d\n", id + 1);
121
122 if (pdata == NULL || pdata->isink[id] == NULL)
123 return -ENODEV;
124
125 isink = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_isink),
126 GFP_KERNEL);
127 if (!isink)
128 return -ENOMEM;
129
130 isink->wm831x = wm831x;
131
132 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
133 if (res == NULL) {
134 dev_err(&pdev->dev, "No REG resource\n");
135 ret = -EINVAL;
136 goto err;
137 }
138 isink->reg = res->start;
139
140 /* For current parts this is correct; probably need to revisit
141 * in future.
142 */
143 snprintf(isink->name, sizeof(isink->name), "ISINK%d", id + 1);
144 isink->desc.name = isink->name;
145 isink->desc.id = id;
146 isink->desc.ops = &wm831x_isink_ops;
147 isink->desc.type = REGULATOR_CURRENT;
148 isink->desc.owner = THIS_MODULE;
149 isink->desc.curr_table = wm831x_isinkv_values;
150 isink->desc.n_current_limits = ARRAY_SIZE(wm831x_isinkv_values);
151 isink->desc.csel_reg = isink->reg;
152 isink->desc.csel_mask = WM831X_CS1_ISEL_MASK;
153
154 config.dev = pdev->dev.parent;
155 config.init_data = pdata->isink[id];
156 config.driver_data = isink;
157 config.regmap = wm831x->regmap;
158
159 isink->regulator = devm_regulator_register(&pdev->dev, &isink->desc,
160 &config);
161 if (IS_ERR(isink->regulator)) {
162 ret = PTR_ERR(isink->regulator);
163 dev_err(wm831x->dev, "Failed to register ISINK%d: %d\n",
164 id + 1, ret);
165 goto err;
166 }
167
168 irq = wm831x_irq(wm831x, platform_get_irq(pdev, 0));
169 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
170 wm831x_isink_irq,
171 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
172 isink->name,
173 isink);
174 if (ret != 0) {
175 dev_err(&pdev->dev, "Failed to request ISINK IRQ %d: %d\n",
176 irq, ret);
177 goto err;
178 }
179
180 platform_set_drvdata(pdev, isink);
181
182 return 0;
183
184 err:
185 return ret;
186 }
187
188 static struct platform_driver wm831x_isink_driver = {
189 .probe = wm831x_isink_probe,
190 .driver = {
191 .name = "wm831x-isink",
192 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
193 },
194 };
195
wm831x_isink_init(void)196 static int __init wm831x_isink_init(void)
197 {
198 int ret;
199 ret = platform_driver_register(&wm831x_isink_driver);
200 if (ret != 0)
201 pr_err("Failed to register WM831x ISINK driver: %d\n", ret);
202
203 return ret;
204 }
205 subsys_initcall(wm831x_isink_init);
206
wm831x_isink_exit(void)207 static void __exit wm831x_isink_exit(void)
208 {
209 platform_driver_unregister(&wm831x_isink_driver);
210 }
211 module_exit(wm831x_isink_exit);
212
213 /* Module information */
214 MODULE_AUTHOR("Mark Brown");
215 MODULE_DESCRIPTION("WM831x current sink driver");
216 MODULE_LICENSE("GPL");
217 MODULE_ALIAS("platform:wm831x-isink");
218