blob: 4ab527f5c53b65ff2872ba07fcef30982a390f3a [file] [log] [blame]
Mark Brown89b40122008-10-10 15:58:10 +01001/*
2 * wm8350-core.c -- Device access for Wolfson WM8350
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 *
6 * Author: Liam Girdwood, Mark Brown
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
15#include <linux/kernel.h>
16#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mark Brownebccec02008-10-10 15:58:14 +010018#include <linux/bug.h>
Mark Brown89b40122008-10-10 15:58:10 +010019#include <linux/device.h>
20#include <linux/delay.h>
21#include <linux/interrupt.h>
Mark Brownb7b142d2012-05-07 10:03:21 +010022#include <linux/regmap.h>
Mark Brownebccec02008-10-10 15:58:14 +010023#include <linux/workqueue.h>
Mark Brown89b40122008-10-10 15:58:10 +010024
25#include <linux/mfd/wm8350/core.h>
26#include <linux/mfd/wm8350/audio.h>
Mark Brownebccec02008-10-10 15:58:14 +010027#include <linux/mfd/wm8350/comparator.h>
Mark Brown89b40122008-10-10 15:58:10 +010028#include <linux/mfd/wm8350/gpio.h>
29#include <linux/mfd/wm8350/pmic.h>
Mark Brownebccec02008-10-10 15:58:14 +010030#include <linux/mfd/wm8350/rtc.h>
Mark Brown89b40122008-10-10 15:58:10 +010031#include <linux/mfd/wm8350/supply.h>
Mark Brownebccec02008-10-10 15:58:14 +010032#include <linux/mfd/wm8350/wdt.h>
Mark Brown89b40122008-10-10 15:58:10 +010033
Mark Brown89b40122008-10-10 15:58:10 +010034#define WM8350_CLOCK_CONTROL_1 0x28
35#define WM8350_AIF_TEST 0x74
36
37/* debug */
38#define WM8350_BUS_DEBUG 0
39#if WM8350_BUS_DEBUG
40#define dump(regs, src) do { \
41 int i_; \
42 u16 *src_ = src; \
43 printk(KERN_DEBUG); \
44 for (i_ = 0; i_ < regs; i_++) \
45 printk(" 0x%4.4x", *src_++); \
46 printk("\n"); \
47} while (0);
48#else
49#define dump(bytes, src)
50#endif
51
52#define WM8350_LOCK_DEBUG 0
53#if WM8350_LOCK_DEBUG
54#define ldbg(format, arg...) printk(format, ## arg)
55#else
56#define ldbg(format, arg...)
57#endif
58
59/*
60 * WM8350 Device IO
61 */
Mark Brown89b40122008-10-10 15:58:10 +010062static DEFINE_MUTEX(reg_lock_mutex);
Mark Brown89b40122008-10-10 15:58:10 +010063
Mark Brown89b40122008-10-10 15:58:10 +010064/*
65 * Safe read, modify, write methods
66 */
67int wm8350_clear_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
68{
Mark Brown19d57ed2012-06-03 13:37:24 +010069 return regmap_update_bits(wm8350->regmap, reg, mask, 0);
Mark Brown89b40122008-10-10 15:58:10 +010070}
71EXPORT_SYMBOL_GPL(wm8350_clear_bits);
72
73int wm8350_set_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
74{
Mark Brown19d57ed2012-06-03 13:37:24 +010075 return regmap_update_bits(wm8350->regmap, reg, mask, mask);
Mark Brown89b40122008-10-10 15:58:10 +010076}
77EXPORT_SYMBOL_GPL(wm8350_set_bits);
78
79u16 wm8350_reg_read(struct wm8350 *wm8350, int reg)
80{
Mark Brown19d57ed2012-06-03 13:37:24 +010081 unsigned int data;
Mark Brown89b40122008-10-10 15:58:10 +010082 int err;
83
Mark Brown19d57ed2012-06-03 13:37:24 +010084 err = regmap_read(wm8350->regmap, reg, &data);
Mark Brown89b40122008-10-10 15:58:10 +010085 if (err)
86 dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
87
Mark Brown89b40122008-10-10 15:58:10 +010088 return data;
89}
90EXPORT_SYMBOL_GPL(wm8350_reg_read);
91
92int wm8350_reg_write(struct wm8350 *wm8350, int reg, u16 val)
93{
94 int ret;
Mark Brown89b40122008-10-10 15:58:10 +010095
Mark Brown19d57ed2012-06-03 13:37:24 +010096 ret = regmap_write(wm8350->regmap, reg, val);
97
Mark Brown89b40122008-10-10 15:58:10 +010098 if (ret)
99 dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
Mark Brown89b40122008-10-10 15:58:10 +0100100 return ret;
101}
102EXPORT_SYMBOL_GPL(wm8350_reg_write);
103
104int wm8350_block_read(struct wm8350 *wm8350, int start_reg, int regs,
105 u16 *dest)
106{
107 int err = 0;
108
Mark Brown19d57ed2012-06-03 13:37:24 +0100109 err = regmap_bulk_read(wm8350->regmap, start_reg, dest, regs);
Mark Brown89b40122008-10-10 15:58:10 +0100110 if (err)
111 dev_err(wm8350->dev, "block read starting from R%d failed\n",
112 start_reg);
Mark Brown19d57ed2012-06-03 13:37:24 +0100113
Mark Brown89b40122008-10-10 15:58:10 +0100114 return err;
115}
116EXPORT_SYMBOL_GPL(wm8350_block_read);
117
118int wm8350_block_write(struct wm8350 *wm8350, int start_reg, int regs,
119 u16 *src)
120{
121 int ret = 0;
122
Mark Brown19d57ed2012-06-03 13:37:24 +0100123 ret = regmap_bulk_write(wm8350->regmap, start_reg, src, regs);
Mark Brown89b40122008-10-10 15:58:10 +0100124 if (ret)
125 dev_err(wm8350->dev, "block write starting at R%d failed\n",
126 start_reg);
Mark Brown19d57ed2012-06-03 13:37:24 +0100127
Mark Brown89b40122008-10-10 15:58:10 +0100128 return ret;
129}
130EXPORT_SYMBOL_GPL(wm8350_block_write);
131
Mark Brown858e6742008-11-08 00:57:33 +0100132/**
133 * wm8350_reg_lock()
134 *
135 * The WM8350 has a hardware lock which can be used to prevent writes to
136 * some registers (generally those which can cause particularly serious
137 * problems if misused). This function enables that lock.
138 */
Mark Brown89b40122008-10-10 15:58:10 +0100139int wm8350_reg_lock(struct wm8350 *wm8350)
140{
Mark Brown89b40122008-10-10 15:58:10 +0100141 int ret;
142
Mark Brown52b461b2012-06-03 13:37:22 +0100143 mutex_lock(&reg_lock_mutex);
144
Mark Brown89b40122008-10-10 15:58:10 +0100145 ldbg(__func__);
Mark Brown52b461b2012-06-03 13:37:22 +0100146
147 ret = wm8350_reg_write(wm8350, WM8350_SECURITY, WM8350_LOCK_KEY);
Mark Brown89b40122008-10-10 15:58:10 +0100148 if (ret)
149 dev_err(wm8350->dev, "lock failed\n");
Mark Brown52b461b2012-06-03 13:37:22 +0100150
151 wm8350->unlocked = false;
152
153 mutex_unlock(&reg_lock_mutex);
154
Mark Brown89b40122008-10-10 15:58:10 +0100155 return ret;
156}
157EXPORT_SYMBOL_GPL(wm8350_reg_lock);
158
Mark Brown858e6742008-11-08 00:57:33 +0100159/**
160 * wm8350_reg_unlock()
161 *
162 * The WM8350 has a hardware lock which can be used to prevent writes to
163 * some registers (generally those which can cause particularly serious
164 * problems if misused). This function disables that lock so updates
165 * can be performed. For maximum safety this should be done only when
166 * required.
167 */
Mark Brown89b40122008-10-10 15:58:10 +0100168int wm8350_reg_unlock(struct wm8350 *wm8350)
169{
Mark Brown89b40122008-10-10 15:58:10 +0100170 int ret;
171
Mark Brown52b461b2012-06-03 13:37:22 +0100172 mutex_lock(&reg_lock_mutex);
173
Mark Brown89b40122008-10-10 15:58:10 +0100174 ldbg(__func__);
Mark Brown52b461b2012-06-03 13:37:22 +0100175
176 ret = wm8350_reg_write(wm8350, WM8350_SECURITY, WM8350_UNLOCK_KEY);
Mark Brown89b40122008-10-10 15:58:10 +0100177 if (ret)
178 dev_err(wm8350->dev, "unlock failed\n");
Mark Brown52b461b2012-06-03 13:37:22 +0100179
180 wm8350->unlocked = true;
181
182 mutex_unlock(&reg_lock_mutex);
183
Mark Brown89b40122008-10-10 15:58:10 +0100184 return ret;
185}
186EXPORT_SYMBOL_GPL(wm8350_reg_unlock);
187
Mark Brown67488522008-11-08 01:10:21 +0100188int wm8350_read_auxadc(struct wm8350 *wm8350, int channel, int scale, int vref)
189{
190 u16 reg, result = 0;
Mark Brown67488522008-11-08 01:10:21 +0100191
192 if (channel < WM8350_AUXADC_AUX1 || channel > WM8350_AUXADC_TEMP)
193 return -EINVAL;
194 if (channel >= WM8350_AUXADC_USB && channel <= WM8350_AUXADC_TEMP
195 && (scale != 0 || vref != 0))
196 return -EINVAL;
197
198 mutex_lock(&wm8350->auxadc_mutex);
199
200 /* Turn on the ADC */
201 reg = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
202 wm8350_reg_write(wm8350, WM8350_POWER_MGMT_5, reg | WM8350_AUXADC_ENA);
203
204 if (scale || vref) {
205 reg = scale << 13;
206 reg |= vref << 12;
207 wm8350_reg_write(wm8350, WM8350_AUX1_READBACK + channel, reg);
208 }
209
210 reg = wm8350_reg_read(wm8350, WM8350_DIGITISER_CONTROL_1);
211 reg |= 1 << channel | WM8350_AUXADC_POLL;
212 wm8350_reg_write(wm8350, WM8350_DIGITISER_CONTROL_1, reg);
213
Mark Brown5051d412010-04-02 13:08:39 +0100214 /* If a late IRQ left the completion signalled then consume
215 * the completion. */
216 try_wait_for_completion(&wm8350->auxadc_done);
217
Mark Brownd19663a2010-02-23 11:08:05 +0000218 /* We ignore the result of the completion and just check for a
219 * conversion result, allowing us to soldier on if the IRQ
220 * infrastructure is not set up for the chip. */
221 wait_for_completion_timeout(&wm8350->auxadc_done, msecs_to_jiffies(5));
Mark Brown67488522008-11-08 01:10:21 +0100222
Mark Brownd19663a2010-02-23 11:08:05 +0000223 reg = wm8350_reg_read(wm8350, WM8350_DIGITISER_CONTROL_1);
224 if (reg & WM8350_AUXADC_POLL)
Mark Brown67488522008-11-08 01:10:21 +0100225 dev_err(wm8350->dev, "adc chn %d read timeout\n", channel);
226 else
227 result = wm8350_reg_read(wm8350,
228 WM8350_AUX1_READBACK + channel);
229
230 /* Turn off the ADC */
231 reg = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
232 wm8350_reg_write(wm8350, WM8350_POWER_MGMT_5,
233 reg & ~WM8350_AUXADC_ENA);
234
235 mutex_unlock(&wm8350->auxadc_mutex);
236
237 return result & WM8350_AUXADC_DATA1_MASK;
238}
239EXPORT_SYMBOL_GPL(wm8350_read_auxadc);
240
Mark Brownd19663a2010-02-23 11:08:05 +0000241static irqreturn_t wm8350_auxadc_irq(int irq, void *irq_data)
242{
243 struct wm8350 *wm8350 = irq_data;
244
245 complete(&wm8350->auxadc_done);
246
247 return IRQ_HANDLED;
248}
249
Mark Brown89b40122008-10-10 15:58:10 +0100250/*
Mark Brown9201d382008-10-10 15:58:16 +0100251 * Register a client device. This is non-fatal since there is no need to
252 * fail the entire device init due to a single platform device failing.
253 */
254static void wm8350_client_dev_register(struct wm8350 *wm8350,
255 const char *name,
256 struct platform_device **pdev)
257{
258 int ret;
259
260 *pdev = platform_device_alloc(name, -1);
Dan Carpentercb9b2242009-11-21 10:13:48 +0200261 if (*pdev == NULL) {
Mark Brown9201d382008-10-10 15:58:16 +0100262 dev_err(wm8350->dev, "Failed to allocate %s\n", name);
263 return;
264 }
265
266 (*pdev)->dev.parent = wm8350->dev;
267 platform_set_drvdata(*pdev, wm8350);
268 ret = platform_device_add(*pdev);
269 if (ret != 0) {
270 dev_err(wm8350->dev, "Failed to register %s: %d\n", name, ret);
271 platform_device_put(*pdev);
272 *pdev = NULL;
273 }
274}
275
Mark Brownebccec02008-10-10 15:58:14 +0100276int wm8350_device_init(struct wm8350 *wm8350, int irq,
Mark Brownbcdd4ef2008-10-10 15:58:13 +0100277 struct wm8350_platform_data *pdata)
Mark Brown89b40122008-10-10 15:58:10 +0100278{
Mark Brown85c93ea2009-02-04 21:09:38 +0100279 int ret;
Mark Brownb7b142d2012-05-07 10:03:21 +0100280 unsigned int id1, id2, mask_rev;
281 unsigned int cust_id, mode, chip_rev;
Mark Brown89b40122008-10-10 15:58:10 +0100282
Mark Brown18bf50a2011-12-08 10:55:21 +0800283 dev_set_drvdata(wm8350->dev, wm8350);
284
Mark Brown89b40122008-10-10 15:58:10 +0100285 /* get WM8350 revision and config mode */
Mark Brownb7b142d2012-05-07 10:03:21 +0100286 ret = regmap_read(wm8350->regmap, WM8350_RESET_ID, &id1);
Mark Brown85c93ea2009-02-04 21:09:38 +0100287 if (ret != 0) {
288 dev_err(wm8350->dev, "Failed to read ID: %d\n", ret);
289 goto err;
290 }
291
Mark Brownb7b142d2012-05-07 10:03:21 +0100292 ret = regmap_read(wm8350->regmap, WM8350_ID, &id2);
Mark Brown85c93ea2009-02-04 21:09:38 +0100293 if (ret != 0) {
294 dev_err(wm8350->dev, "Failed to read ID: %d\n", ret);
295 goto err;
296 }
297
Mark Brownb7b142d2012-05-07 10:03:21 +0100298 ret = regmap_read(wm8350->regmap, WM8350_REVISION, &mask_rev);
Mark Brown85c93ea2009-02-04 21:09:38 +0100299 if (ret != 0) {
300 dev_err(wm8350->dev, "Failed to read revision: %d\n", ret);
301 goto err;
302 }
Mark Brown89b40122008-10-10 15:58:10 +0100303
Mark Brownb797a552008-11-24 20:22:58 +0100304 if (id1 != 0x6143) {
305 dev_err(wm8350->dev,
306 "Device with ID %x is not a WM8350\n", id1);
Mark Brown89b40122008-10-10 15:58:10 +0100307 ret = -ENODEV;
308 goto err;
309 }
310
311 mode = id2 & WM8350_CONF_STS_MASK >> 10;
Mark Brownb797a552008-11-24 20:22:58 +0100312 cust_id = id2 & WM8350_CUST_ID_MASK;
313 chip_rev = (id2 & WM8350_CHIP_REV_MASK) >> 12;
314 dev_info(wm8350->dev,
315 "CONF_STS %d, CUST_ID %d, MASK_REV %d, CHIP_REV %d\n",
316 mode, cust_id, mask_rev, chip_rev);
317
318 if (cust_id != 0) {
319 dev_err(wm8350->dev, "Unsupported CUST_ID\n");
320 ret = -ENODEV;
321 goto err;
322 }
323
324 switch (mask_rev) {
325 case 0:
Mark Brown645524a2008-12-18 23:12:16 +0100326 wm8350->pmic.max_dcdc = WM8350_DCDC_6;
327 wm8350->pmic.max_isink = WM8350_ISINK_B;
328
Mark Brownb797a552008-11-24 20:22:58 +0100329 switch (chip_rev) {
330 case WM8350_REV_E:
331 dev_info(wm8350->dev, "WM8350 Rev E\n");
332 break;
333 case WM8350_REV_F:
334 dev_info(wm8350->dev, "WM8350 Rev F\n");
335 break;
336 case WM8350_REV_G:
337 dev_info(wm8350->dev, "WM8350 Rev G\n");
338 wm8350->power.rev_g_coeff = 1;
339 break;
340 case WM8350_REV_H:
341 dev_info(wm8350->dev, "WM8350 Rev H\n");
342 wm8350->power.rev_g_coeff = 1;
343 break;
344 default:
345 /* For safety we refuse to run on unknown hardware */
346 dev_err(wm8350->dev, "Unknown WM8350 CHIP_REV\n");
347 ret = -ENODEV;
348 goto err;
349 }
350 break;
351
Mark Brownca23f8c2008-12-18 23:12:28 +0100352 case 1:
353 wm8350->pmic.max_dcdc = WM8350_DCDC_4;
354 wm8350->pmic.max_isink = WM8350_ISINK_A;
355
356 switch (chip_rev) {
357 case 0:
358 dev_info(wm8350->dev, "WM8351 Rev A\n");
359 wm8350->power.rev_g_coeff = 1;
360 break;
361
Mark Brown02d46e02009-03-12 14:31:36 -0700362 case 1:
363 dev_info(wm8350->dev, "WM8351 Rev B\n");
364 wm8350->power.rev_g_coeff = 1;
365 break;
366
Mark Brownca23f8c2008-12-18 23:12:28 +0100367 default:
368 dev_err(wm8350->dev, "Unknown WM8351 CHIP_REV\n");
369 ret = -ENODEV;
370 goto err;
371 }
372 break;
373
Mark Brown96920632008-12-18 23:09:50 +0100374 case 2:
Mark Brown645524a2008-12-18 23:12:16 +0100375 wm8350->pmic.max_dcdc = WM8350_DCDC_6;
376 wm8350->pmic.max_isink = WM8350_ISINK_B;
377
Mark Brown96920632008-12-18 23:09:50 +0100378 switch (chip_rev) {
379 case 0:
380 dev_info(wm8350->dev, "WM8352 Rev A\n");
381 wm8350->power.rev_g_coeff = 1;
382 break;
383
384 default:
385 dev_err(wm8350->dev, "Unknown WM8352 CHIP_REV\n");
386 ret = -ENODEV;
387 goto err;
388 }
389 break;
390
Mark Brownb797a552008-11-24 20:22:58 +0100391 default:
392 dev_err(wm8350->dev, "Unknown MASK_REV\n");
393 ret = -ENODEV;
394 goto err;
395 }
Mark Brown89b40122008-10-10 15:58:10 +0100396
Mark Brown67488522008-11-08 01:10:21 +0100397 mutex_init(&wm8350->auxadc_mutex);
Mark Brownd19663a2010-02-23 11:08:05 +0000398 init_completion(&wm8350->auxadc_done);
Mark Brown32064502009-02-06 15:27:13 +0100399
Mark Browne0a33892009-10-12 16:15:09 +0100400 ret = wm8350_irq_init(wm8350, irq, pdata);
401 if (ret < 0)
Mark Brown19d57ed2012-06-03 13:37:24 +0100402 goto err;
Mark Brownebccec02008-10-10 15:58:14 +0100403
Mark Brownd19663a2010-02-23 11:08:05 +0000404 if (wm8350->irq_base) {
405 ret = request_threaded_irq(wm8350->irq_base +
406 WM8350_IRQ_AUXADC_DATARDY,
407 NULL, wm8350_auxadc_irq, 0,
408 "auxadc", wm8350);
409 if (ret < 0)
410 dev_warn(wm8350->dev,
411 "Failed to request AUXADC IRQ: %d\n", ret);
412 }
413
Mark Brown62571c292009-02-04 20:49:52 +0100414 if (pdata && pdata->init) {
415 ret = pdata->init(wm8350);
416 if (ret != 0) {
417 dev_err(wm8350->dev, "Platform init() failed: %d\n",
418 ret);
Mark Browne0a33892009-10-12 16:15:09 +0100419 goto err_irq;
Mark Brown62571c292009-02-04 20:49:52 +0100420 }
421 }
422
Mark Brownebccec02008-10-10 15:58:14 +0100423 wm8350_reg_write(wm8350, WM8350_SYSTEM_INTERRUPTS_MASK, 0x0);
424
Mark Brownadd41cb2008-10-13 15:45:22 +0100425 wm8350_client_dev_register(wm8350, "wm8350-codec",
426 &(wm8350->codec.pdev));
427 wm8350_client_dev_register(wm8350, "wm8350-gpio",
428 &(wm8350->gpio.pdev));
Mark Brownfb6c0232009-07-20 12:43:45 +0100429 wm8350_client_dev_register(wm8350, "wm8350-hwmon",
430 &(wm8350->hwmon.pdev));
Mark Brownadd41cb2008-10-13 15:45:22 +0100431 wm8350_client_dev_register(wm8350, "wm8350-power",
432 &(wm8350->power.pdev));
433 wm8350_client_dev_register(wm8350, "wm8350-rtc", &(wm8350->rtc.pdev));
434 wm8350_client_dev_register(wm8350, "wm8350-wdt", &(wm8350->wdt.pdev));
435
Mark Brown89b40122008-10-10 15:58:10 +0100436 return 0;
437
Mark Browne0a33892009-10-12 16:15:09 +0100438err_irq:
439 wm8350_irq_exit(wm8350);
Axel Lin8c46cf32010-08-04 09:54:21 +0800440err:
Mark Brown89b40122008-10-10 15:58:10 +0100441 return ret;
442}
443EXPORT_SYMBOL_GPL(wm8350_device_init);
444
445void wm8350_device_exit(struct wm8350 *wm8350)
446{
Mark Brownda091552008-10-10 15:58:15 +0100447 int i;
448
Mark Brown0081e802008-12-04 16:52:33 +0000449 for (i = 0; i < ARRAY_SIZE(wm8350->pmic.led); i++)
450 platform_device_unregister(wm8350->pmic.led[i].pdev);
451
Mark Brownda091552008-10-10 15:58:15 +0100452 for (i = 0; i < ARRAY_SIZE(wm8350->pmic.pdev); i++)
Mark Brownadd41cb2008-10-13 15:45:22 +0100453 platform_device_unregister(wm8350->pmic.pdev[i]);
454
455 platform_device_unregister(wm8350->wdt.pdev);
456 platform_device_unregister(wm8350->rtc.pdev);
457 platform_device_unregister(wm8350->power.pdev);
Mark Brownfb6c0232009-07-20 12:43:45 +0100458 platform_device_unregister(wm8350->hwmon.pdev);
Mark Brownadd41cb2008-10-13 15:45:22 +0100459 platform_device_unregister(wm8350->gpio.pdev);
460 platform_device_unregister(wm8350->codec.pdev);
Mark Brownda091552008-10-10 15:58:15 +0100461
Mark Brownd19663a2010-02-23 11:08:05 +0000462 if (wm8350->irq_base)
463 free_irq(wm8350->irq_base + WM8350_IRQ_AUXADC_DATARDY, wm8350);
464
Mark Browne0a33892009-10-12 16:15:09 +0100465 wm8350_irq_exit(wm8350);
Mark Brown89b40122008-10-10 15:58:10 +0100466}
467EXPORT_SYMBOL_GPL(wm8350_device_exit);
468
Mark Brownebccec02008-10-10 15:58:14 +0100469MODULE_DESCRIPTION("WM8350 AudioPlus PMIC core driver");
Mark Brown89b40122008-10-10 15:58:10 +0100470MODULE_LICENSE("GPL");