Blame view
drivers/mfd/wm831x-auxadc.c
6.65 KB
e69b6de18 mfd: Refactor wm8... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* * wm831x-auxadc.c -- AUXADC for Wolfson WM831x PMICs * * Copyright 2009-2011 Wolfson Microelectronics PLC. * * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/delay.h> #include <linux/mfd/core.h> #include <linux/slab.h> |
78bb3688e mfd: Support mult... |
20 |
#include <linux/list.h> |
e69b6de18 mfd: Refactor wm8... |
21 22 23 24 25 26 27 |
#include <linux/mfd/wm831x/core.h> #include <linux/mfd/wm831x/pdata.h> #include <linux/mfd/wm831x/irq.h> #include <linux/mfd/wm831x/auxadc.h> #include <linux/mfd/wm831x/otp.h> #include <linux/mfd/wm831x/regulator.h> |
78bb3688e mfd: Support mult... |
28 29 30 31 32 33 34 35 36 |
struct wm831x_auxadc_req { struct list_head list; enum wm831x_auxadc input; int val; struct completion done; }; static int wm831x_auxadc_read_irq(struct wm831x *wm831x, enum wm831x_auxadc input) |
e69b6de18 mfd: Refactor wm8... |
37 |
{ |
78bb3688e mfd: Support mult... |
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
struct wm831x_auxadc_req *req; int ret; bool ena = false; req = kzalloc(sizeof(*req), GFP_KERNEL); if (!req) return -ENOMEM; init_completion(&req->done); req->input = input; req->val = -ETIMEDOUT; mutex_lock(&wm831x->auxadc_lock); /* Enqueue the request */ list_add(&req->list, &wm831x->auxadc_pending); ena = !wm831x->auxadc_active; if (ena) { ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, WM831X_AUX_ENA); if (ret != 0) { dev_err(wm831x->dev, "Failed to enable AUXADC: %d ", ret); goto out; } } /* Enable the conversion if not already running */ if (!(wm831x->auxadc_active & (1 << input))) { ret = wm831x_set_bits(wm831x, WM831X_AUXADC_SOURCE, 1 << input, 1 << input); if (ret != 0) { dev_err(wm831x->dev, "Failed to set AUXADC source: %d ", ret); goto out; } wm831x->auxadc_active |= 1 << input; } /* We convert at the fastest rate possible */ if (ena) { ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_CVT_ENA | WM831X_AUX_RATE_MASK, WM831X_AUX_CVT_ENA | WM831X_AUX_RATE_MASK); if (ret != 0) { dev_err(wm831x->dev, "Failed to start AUXADC: %d ", ret); goto out; } } mutex_unlock(&wm831x->auxadc_lock); /* Wait for an interrupt */ wait_for_completion_timeout(&req->done, msecs_to_jiffies(500)); mutex_lock(&wm831x->auxadc_lock); list_del(&req->list); ret = req->val; out: mutex_unlock(&wm831x->auxadc_lock); kfree(req); return ret; } static irqreturn_t wm831x_auxadc_irq(int irq, void *irq_data) { struct wm831x *wm831x = irq_data; struct wm831x_auxadc_req *req; int ret, input, val; ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA); if (ret < 0) { dev_err(wm831x->dev, "Failed to read AUXADC data: %d ", ret); return IRQ_NONE; } input = ((ret & WM831X_AUX_DATA_SRC_MASK) >> WM831X_AUX_DATA_SRC_SHIFT) - 1; if (input == 14) input = WM831X_AUX_CAL; |
e69b6de18 mfd: Refactor wm8... |
134 |
|
78bb3688e mfd: Support mult... |
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
val = ret & WM831X_AUX_DATA_MASK; mutex_lock(&wm831x->auxadc_lock); /* Disable this conversion, we're about to complete all users */ wm831x_set_bits(wm831x, WM831X_AUXADC_SOURCE, 1 << input, 0); wm831x->auxadc_active &= ~(1 << input); /* Turn off the entire convertor if idle */ if (!wm831x->auxadc_active) wm831x_reg_write(wm831x, WM831X_AUXADC_CONTROL, 0); /* Wake up any threads waiting for this request */ list_for_each_entry(req, &wm831x->auxadc_pending, list) { if (req->input == input) { req->val = val; complete(&req->done); } } mutex_unlock(&wm831x->auxadc_lock); return IRQ_HANDLED; } static int wm831x_auxadc_read_polled(struct wm831x *wm831x, enum wm831x_auxadc input) { int ret, src, timeout; |
e69b6de18 mfd: Refactor wm8... |
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
mutex_lock(&wm831x->auxadc_lock); ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, WM831X_AUX_ENA); if (ret < 0) { dev_err(wm831x->dev, "Failed to enable AUXADC: %d ", ret); goto out; } /* We force a single source at present */ src = input; ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE, 1 << src); if (ret < 0) { dev_err(wm831x->dev, "Failed to set AUXADC source: %d ", ret); goto out; } |
e69b6de18 mfd: Refactor wm8... |
185 186 187 188 189 190 191 |
ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA); if (ret < 0) { dev_err(wm831x->dev, "Failed to start AUXADC: %d ", ret); goto disable; } |
78bb3688e mfd: Support mult... |
192 193 194 195 196 |
/* If we're not using interrupts then poll the * interrupt status register */ timeout = 5; while (timeout) { msleep(1); |
e69b6de18 mfd: Refactor wm8... |
197 |
|
78bb3688e mfd: Support mult... |
198 199 |
ret = wm831x_reg_read(wm831x, WM831X_INTERRUPT_STATUS_1); |
e69b6de18 mfd: Refactor wm8... |
200 201 |
if (ret < 0) { dev_err(wm831x->dev, |
78bb3688e mfd: Support mult... |
202 203 |
"ISR 1 read failed: %d ", ret); |
e69b6de18 mfd: Refactor wm8... |
204 205 |
goto disable; } |
78bb3688e mfd: Support mult... |
206 207 208 209 210 211 212 213 214 215 |
/* Did it complete? */ if (ret & WM831X_AUXADC_DATA_EINT) { wm831x_reg_write(wm831x, WM831X_INTERRUPT_STATUS_1, WM831X_AUXADC_DATA_EINT); break; } else { dev_err(wm831x->dev, "AUXADC conversion timeout "); |
e69b6de18 mfd: Refactor wm8... |
216 217 218 219 |
ret = -EBUSY; goto disable; } } |
78bb3688e mfd: Support mult... |
220 221 222 223 224 225 226 227 228 |
ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA); if (ret < 0) { dev_err(wm831x->dev, "Failed to read AUXADC data: %d ", ret); goto disable; } src = ((ret & WM831X_AUX_DATA_SRC_MASK) |
e69b6de18 mfd: Refactor wm8... |
229 230 231 232 233 234 235 236 237 238 239 |
>> WM831X_AUX_DATA_SRC_SHIFT) - 1; if (src == 14) src = WM831X_AUX_CAL; if (src != input) { dev_err(wm831x->dev, "Data from source %d not %d ", src, input); ret = -EINVAL; } else { |
78bb3688e mfd: Support mult... |
240 |
ret &= WM831X_AUX_DATA_MASK; |
e69b6de18 mfd: Refactor wm8... |
241 242 243 244 245 246 247 248 |
} disable: wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0); out: mutex_unlock(&wm831x->auxadc_lock); return ret; } |
e69b6de18 mfd: Refactor wm8... |
249 |
|
78bb3688e mfd: Support mult... |
250 251 252 253 254 255 256 |
/** * wm831x_auxadc_read: Read a value from the WM831x AUXADC * * @wm831x: Device to read from. * @input: AUXADC input to read. */ int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input) |
e69b6de18 mfd: Refactor wm8... |
257 |
{ |
78bb3688e mfd: Support mult... |
258 |
return wm831x->auxadc_read(wm831x, input); |
e69b6de18 mfd: Refactor wm8... |
259 |
} |
78bb3688e mfd: Support mult... |
260 |
EXPORT_SYMBOL_GPL(wm831x_auxadc_read); |
e69b6de18 mfd: Refactor wm8... |
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
/** * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC * * @wm831x: Device to read from. * @input: AUXADC input to read. */ int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input) { int ret; ret = wm831x_auxadc_read(wm831x, input); if (ret < 0) return ret; ret *= 1465; return ret; } EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv); void wm831x_auxadc_init(struct wm831x *wm831x) { int ret; mutex_init(&wm831x->auxadc_lock); |
78bb3688e mfd: Support mult... |
287 288 289 290 |
INIT_LIST_HEAD(&wm831x->auxadc_pending); if (wm831x->irq && wm831x->irq_base) { wm831x->auxadc_read = wm831x_auxadc_read_irq; |
e69b6de18 mfd: Refactor wm8... |
291 |
|
e69b6de18 mfd: Refactor wm8... |
292 293 294 295 |
ret = request_threaded_irq(wm831x->irq_base + WM831X_IRQ_AUXADC_DATA, NULL, wm831x_auxadc_irq, 0, "auxadc", wm831x); |
78bb3688e mfd: Support mult... |
296 |
if (ret < 0) { |
e69b6de18 mfd: Refactor wm8... |
297 298 299 |
dev_err(wm831x->dev, "AUXADC IRQ request failed: %d ", ret); |
78bb3688e mfd: Support mult... |
300 301 |
wm831x->auxadc_read = NULL; } |
e69b6de18 mfd: Refactor wm8... |
302 |
} |
78bb3688e mfd: Support mult... |
303 304 305 |
if (!wm831x->auxadc_read) wm831x->auxadc_read = wm831x_auxadc_read_polled; |
e69b6de18 mfd: Refactor wm8... |
306 |
} |