blob: 6dd891d7eee3bf9488de82f90add099eeda2fcdc [file] [log] [blame]
Mark Brownd4d6b722009-07-28 15:23:46 +01001/*
2 * wm831x-isink.c -- Current sink driver for the WM831x series
3 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/bitops.h>
18#include <linux/err.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brownd4d6b722009-07-28 15:23:46 +010023
24#include <linux/mfd/wm831x/core.h>
25#include <linux/mfd/wm831x/regulator.h>
26#include <linux/mfd/wm831x/pdata.h>
27
28#define WM831X_ISINK_MAX_NAME 7
29
30struct wm831x_isink {
31 char name[WM831X_ISINK_MAX_NAME];
32 struct regulator_desc desc;
33 int reg;
34 struct wm831x *wm831x;
35 struct regulator_dev *regulator;
36};
37
38static int wm831x_isink_enable(struct regulator_dev *rdev)
39{
40 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
41 struct wm831x *wm831x = isink->wm831x;
42 int ret;
43
44 /* We have a two stage enable: first start the ISINK... */
45 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA,
46 WM831X_CS1_ENA);
47 if (ret != 0)
48 return ret;
49
50 /* ...then enable drive */
51 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE,
52 WM831X_CS1_DRIVE);
53 if (ret != 0)
54 wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0);
55
56 return ret;
57
58}
59
60static int wm831x_isink_disable(struct regulator_dev *rdev)
61{
62 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
63 struct wm831x *wm831x = isink->wm831x;
64 int ret;
65
66 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE, 0);
67 if (ret < 0)
68 return ret;
69
70 ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0);
71 if (ret < 0)
72 return ret;
73
74 return ret;
75
76}
77
78static int wm831x_isink_is_enabled(struct regulator_dev *rdev)
79{
80 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
81 struct wm831x *wm831x = isink->wm831x;
82 int ret;
83
84 ret = wm831x_reg_read(wm831x, isink->reg);
85 if (ret < 0)
86 return ret;
87
88 if ((ret & (WM831X_CS1_ENA | WM831X_CS1_DRIVE)) ==
89 (WM831X_CS1_ENA | WM831X_CS1_DRIVE))
90 return 1;
91 else
92 return 0;
93}
94
95static int wm831x_isink_set_current(struct regulator_dev *rdev,
96 int min_uA, int max_uA)
97{
98 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
99 struct wm831x *wm831x = isink->wm831x;
100 int ret, i;
101
102 for (i = 0; i < ARRAY_SIZE(wm831x_isinkv_values); i++) {
103 int val = wm831x_isinkv_values[i];
Axel Lined3be9a2012-03-27 15:18:53 +0800104 if (min_uA <= val && val <= max_uA) {
Mark Brownd4d6b722009-07-28 15:23:46 +0100105 ret = wm831x_set_bits(wm831x, isink->reg,
106 WM831X_CS1_ISEL_MASK, i);
107 return ret;
108 }
109 }
110
111 return -EINVAL;
112}
113
114static int wm831x_isink_get_current(struct regulator_dev *rdev)
115{
116 struct wm831x_isink *isink = rdev_get_drvdata(rdev);
117 struct wm831x *wm831x = isink->wm831x;
118 int ret;
119
120 ret = wm831x_reg_read(wm831x, isink->reg);
121 if (ret < 0)
122 return ret;
123
124 ret &= WM831X_CS1_ISEL_MASK;
125 if (ret > WM831X_ISINK_MAX_ISEL)
126 ret = WM831X_ISINK_MAX_ISEL;
127
128 return wm831x_isinkv_values[ret];
129}
130
Julia Lawallb0d6dd32015-12-19 16:31:24 +0100131static const struct regulator_ops wm831x_isink_ops = {
Mark Brownd4d6b722009-07-28 15:23:46 +0100132 .is_enabled = wm831x_isink_is_enabled,
133 .enable = wm831x_isink_enable,
134 .disable = wm831x_isink_disable,
135 .set_current_limit = wm831x_isink_set_current,
136 .get_current_limit = wm831x_isink_get_current,
137};
138
139static irqreturn_t wm831x_isink_irq(int irq, void *data)
140{
141 struct wm831x_isink *isink = data;
142
143 regulator_notifier_call_chain(isink->regulator,
144 REGULATOR_EVENT_OVER_CURRENT,
145 NULL);
146
147 return IRQ_HANDLED;
148}
149
150
Bill Pembertona5023572012-11-19 13:22:22 -0500151static int wm831x_isink_probe(struct platform_device *pdev)
Mark Brownd4d6b722009-07-28 15:23:46 +0100152{
153 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900154 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownd4d6b722009-07-28 15:23:46 +0100155 struct wm831x_isink *isink;
156 int id = pdev->id % ARRAY_SIZE(pdata->isink);
Mark Brownc1727082012-04-04 00:50:22 +0100157 struct regulator_config config = { };
Mark Brownd4d6b722009-07-28 15:23:46 +0100158 struct resource *res;
159 int ret, irq;
160
161 dev_dbg(&pdev->dev, "Probing ISINK%d\n", id + 1);
162
163 if (pdata == NULL || pdata->isink[id] == NULL)
164 return -ENODEV;
165
Mark Brownfded2f42011-12-15 02:11:14 +0800166 isink = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_isink),
167 GFP_KERNEL);
Sachin Kamatd718deb2014-02-20 14:23:21 +0530168 if (!isink)
Mark Brownd4d6b722009-07-28 15:23:46 +0100169 return -ENOMEM;
Mark Brownd4d6b722009-07-28 15:23:46 +0100170
Mark Browne8092da2009-11-30 14:01:56 +0000171 isink->wm831x = wm831x;
172
Mark Brown56560982012-08-07 19:42:47 +0100173 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd4d6b722009-07-28 15:23:46 +0100174 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100175 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd4d6b722009-07-28 15:23:46 +0100176 ret = -EINVAL;
177 goto err;
178 }
179 isink->reg = res->start;
180
181 /* For current parts this is correct; probably need to revisit
182 * in future.
183 */
184 snprintf(isink->name, sizeof(isink->name), "ISINK%d", id + 1);
185 isink->desc.name = isink->name;
186 isink->desc.id = id;
187 isink->desc.ops = &wm831x_isink_ops;
188 isink->desc.type = REGULATOR_CURRENT;
189 isink->desc.owner = THIS_MODULE;
190
Mark Brownc1727082012-04-04 00:50:22 +0100191 config.dev = pdev->dev.parent;
192 config.init_data = pdata->isink[id];
193 config.driver_data = isink;
194
Mark Brownaf151de2013-08-31 11:59:57 +0100195 isink->regulator = devm_regulator_register(&pdev->dev, &isink->desc,
196 &config);
Mark Brownd4d6b722009-07-28 15:23:46 +0100197 if (IS_ERR(isink->regulator)) {
198 ret = PTR_ERR(isink->regulator);
199 dev_err(wm831x->dev, "Failed to register ISINK%d: %d\n",
200 id + 1, ret);
201 goto err;
202 }
203
Mark Browncd997582012-05-14 23:14:24 +0200204 irq = wm831x_irq(wm831x, platform_get_irq(pdev, 0));
Mark Brown63fb3142013-08-31 11:48:45 +0100205 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
206 wm831x_isink_irq,
Fabio Estevam29454732015-06-01 22:33:53 -0300207 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
208 isink->name,
Mark Brown63fb3142013-08-31 11:48:45 +0100209 isink);
Mark Brownd4d6b722009-07-28 15:23:46 +0100210 if (ret != 0) {
211 dev_err(&pdev->dev, "Failed to request ISINK IRQ %d: %d\n",
212 irq, ret);
Mark Brownaf151de2013-08-31 11:59:57 +0100213 goto err;
Mark Brownd4d6b722009-07-28 15:23:46 +0100214 }
215
216 platform_set_drvdata(pdev, isink);
217
218 return 0;
219
Mark Brownd4d6b722009-07-28 15:23:46 +0100220err:
Mark Brownd4d6b722009-07-28 15:23:46 +0100221 return ret;
222}
223
Mark Brownd4d6b722009-07-28 15:23:46 +0100224static struct platform_driver wm831x_isink_driver = {
225 .probe = wm831x_isink_probe,
Mark Brownd4d6b722009-07-28 15:23:46 +0100226 .driver = {
227 .name = "wm831x-isink",
Mark Brownd4d6b722009-07-28 15:23:46 +0100228 },
229};
230
231static int __init wm831x_isink_init(void)
232{
233 int ret;
234 ret = platform_driver_register(&wm831x_isink_driver);
235 if (ret != 0)
236 pr_err("Failed to register WM831x ISINK driver: %d\n", ret);
237
238 return ret;
239}
240subsys_initcall(wm831x_isink_init);
241
242static void __exit wm831x_isink_exit(void)
243{
244 platform_driver_unregister(&wm831x_isink_driver);
245}
246module_exit(wm831x_isink_exit);
247
248/* Module information */
249MODULE_AUTHOR("Mark Brown");
250MODULE_DESCRIPTION("WM831x current sink driver");
251MODULE_LICENSE("GPL");
252MODULE_ALIAS("platform:wm831x-isink");