2 * wm831x-auxadc.c -- AUXADC for Wolfson WM831x PMICs
4 * Copyright 2009-2011 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/mfd/core.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
22 #include <linux/mfd/wm831x/core.h>
23 #include <linux/mfd/wm831x/pdata.h>
24 #include <linux/mfd/wm831x/irq.h>
25 #include <linux/mfd/wm831x/auxadc.h>
26 #include <linux/mfd/wm831x/otp.h>
27 #include <linux/mfd/wm831x/regulator.h>
29 struct wm831x_auxadc_req {
30 struct list_head list;
31 enum wm831x_auxadc input;
33 struct completion done;
36 static int wm831x_auxadc_read_irq(struct wm831x *wm831x,
37 enum wm831x_auxadc input)
39 struct wm831x_auxadc_req *req;
43 req = kzalloc(sizeof(*req), GFP_KERNEL);
47 init_completion(&req->done);
49 req->val = -ETIMEDOUT;
51 mutex_lock(&wm831x->auxadc_lock);
53 /* Enqueue the request */
54 list_add(&req->list, &wm831x->auxadc_pending);
56 ena = !wm831x->auxadc_active;
59 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
60 WM831X_AUX_ENA, WM831X_AUX_ENA);
62 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n",
68 /* Enable the conversion if not already running */
69 if (!(wm831x->auxadc_active & (1 << input))) {
70 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_SOURCE,
71 1 << input, 1 << input);
74 "Failed to set AUXADC source: %d\n", ret);
78 wm831x->auxadc_active |= 1 << input;
81 /* We convert at the fastest rate possible */
83 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
87 WM831X_AUX_RATE_MASK);
89 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n",
95 mutex_unlock(&wm831x->auxadc_lock);
97 /* Wait for an interrupt */
98 wait_for_completion_timeout(&req->done, msecs_to_jiffies(500));
100 mutex_lock(&wm831x->auxadc_lock);
104 list_del(&req->list);
105 mutex_unlock(&wm831x->auxadc_lock);
112 static irqreturn_t wm831x_auxadc_irq(int irq, void *irq_data)
114 struct wm831x *wm831x = irq_data;
115 struct wm831x_auxadc_req *req;
118 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
121 "Failed to read AUXADC data: %d\n", ret);
125 input = ((ret & WM831X_AUX_DATA_SRC_MASK)
126 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
129 input = WM831X_AUX_CAL;
131 val = ret & WM831X_AUX_DATA_MASK;
133 mutex_lock(&wm831x->auxadc_lock);
135 /* Disable this conversion, we're about to complete all users */
136 wm831x_set_bits(wm831x, WM831X_AUXADC_SOURCE,
138 wm831x->auxadc_active &= ~(1 << input);
140 /* Turn off the entire convertor if idle */
141 if (!wm831x->auxadc_active)
142 wm831x_reg_write(wm831x, WM831X_AUXADC_CONTROL, 0);
144 /* Wake up any threads waiting for this request */
145 list_for_each_entry(req, &wm831x->auxadc_pending, list) {
146 if (req->input == input) {
148 complete(&req->done);
152 mutex_unlock(&wm831x->auxadc_lock);
157 static int wm831x_auxadc_read_polled(struct wm831x *wm831x,
158 enum wm831x_auxadc input)
160 int ret, src, timeout;
162 mutex_lock(&wm831x->auxadc_lock);
164 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
165 WM831X_AUX_ENA, WM831X_AUX_ENA);
167 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
171 /* We force a single source at present */
173 ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
176 dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
180 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
181 WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
183 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
187 /* If we're not using interrupts then poll the
188 * interrupt status register */
193 ret = wm831x_reg_read(wm831x,
194 WM831X_INTERRUPT_STATUS_1);
197 "ISR 1 read failed: %d\n", ret);
201 /* Did it complete? */
202 if (ret & WM831X_AUXADC_DATA_EINT) {
203 wm831x_reg_write(wm831x,
204 WM831X_INTERRUPT_STATUS_1,
205 WM831X_AUXADC_DATA_EINT);
209 "AUXADC conversion timeout\n");
215 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
218 "Failed to read AUXADC data: %d\n", ret);
222 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
223 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
226 src = WM831X_AUX_CAL;
229 dev_err(wm831x->dev, "Data from source %d not %d\n",
233 ret &= WM831X_AUX_DATA_MASK;
237 wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
239 mutex_unlock(&wm831x->auxadc_lock);
244 * wm831x_auxadc_read: Read a value from the WM831x AUXADC
246 * @wm831x: Device to read from.
247 * @input: AUXADC input to read.
249 int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
251 return wm831x->auxadc_read(wm831x, input);
253 EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
256 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
258 * @wm831x: Device to read from.
259 * @input: AUXADC input to read.
261 int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
265 ret = wm831x_auxadc_read(wm831x, input);
273 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
275 void wm831x_auxadc_init(struct wm831x *wm831x)
279 mutex_init(&wm831x->auxadc_lock);
280 INIT_LIST_HEAD(&wm831x->auxadc_pending);
283 wm831x->auxadc_read = wm831x_auxadc_read_irq;
285 ret = request_threaded_irq(wm831x_irq(wm831x,
286 WM831X_IRQ_AUXADC_DATA),
287 NULL, wm831x_auxadc_irq,
291 dev_err(wm831x->dev, "AUXADC IRQ request failed: %d\n",
293 wm831x->auxadc_read = NULL;
297 if (!wm831x->auxadc_read)
298 wm831x->auxadc_read = wm831x_auxadc_read_polled;