blob: 6a35f2a2702cec37d05bdf166b498295defb644e [file] [log] [blame]
Mark Brownd2bedfe2009-07-27 14:45:52 +01001/*
2 * wm831x-core.c -- Device access for Wolfson WM831x PMICs
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
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/i2c.h>
Mark Brown7e9f9fd2009-07-27 14:45:54 +010018#include <linux/bcd.h>
19#include <linux/delay.h>
Mark Brownd2bedfe2009-07-27 14:45:52 +010020#include <linux/mfd/core.h>
21
22#include <linux/mfd/wm831x/core.h>
23#include <linux/mfd/wm831x/pdata.h>
Mark Brown7d4d0a32009-07-27 14:45:53 +010024#include <linux/mfd/wm831x/irq.h>
Mark Brown7e9f9fd2009-07-27 14:45:54 +010025#include <linux/mfd/wm831x/auxadc.h>
Mark Brown6704e512009-07-27 14:45:56 +010026#include <linux/mfd/wm831x/otp.h>
Mark Brown698659d2009-07-27 14:45:57 +010027#include <linux/mfd/wm831x/regulator.h>
28
29/* Current settings - values are 2*2^(reg_val/4) microamps. These are
30 * exported since they are used by multiple drivers.
31 */
32int wm831x_isinkv_values[WM831X_ISINK_MAX_ISEL] = {
33 2,
34 2,
35 3,
36 3,
37 4,
38 5,
39 6,
40 7,
41 8,
42 10,
43 11,
44 13,
45 16,
46 19,
47 23,
48 27,
49 32,
50 38,
51 45,
52 54,
53 64,
54 76,
55 91,
56 108,
57 128,
58 152,
59 181,
60 215,
61 256,
62 304,
63 362,
64 431,
65 512,
66 609,
67 724,
68 861,
69 1024,
70 1218,
71 1448,
72 1722,
73 2048,
74 2435,
75 2896,
76 3444,
77 4096,
78 4871,
79 5793,
80 6889,
81 8192,
82 9742,
83 11585,
84 13777,
85 16384,
86 19484,
87 23170,
88 27554,
89};
90EXPORT_SYMBOL_GPL(wm831x_isinkv_values);
Mark Brownd2bedfe2009-07-27 14:45:52 +010091
92enum wm831x_parent {
93 WM8310 = 0,
94 WM8311 = 1,
95 WM8312 = 2,
96};
97
98static int wm831x_reg_locked(struct wm831x *wm831x, unsigned short reg)
99{
100 if (!wm831x->locked)
101 return 0;
102
103 switch (reg) {
104 case WM831X_WATCHDOG:
105 case WM831X_DC4_CONTROL:
106 case WM831X_ON_PIN_CONTROL:
107 case WM831X_BACKUP_CHARGER_CONTROL:
108 case WM831X_CHARGER_CONTROL_1:
109 case WM831X_CHARGER_CONTROL_2:
110 return 1;
111
112 default:
113 return 0;
114 }
115}
116
117/**
118 * wm831x_reg_unlock: Unlock user keyed registers
119 *
120 * The WM831x has a user key preventing writes to particularly
121 * critical registers. This function locks those registers,
122 * allowing writes to them.
123 */
124void wm831x_reg_lock(struct wm831x *wm831x)
125{
126 int ret;
127
128 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
129 if (ret == 0) {
130 dev_vdbg(wm831x->dev, "Registers locked\n");
131
132 mutex_lock(&wm831x->io_lock);
133 WARN_ON(wm831x->locked);
134 wm831x->locked = 1;
135 mutex_unlock(&wm831x->io_lock);
136 } else {
137 dev_err(wm831x->dev, "Failed to lock registers: %d\n", ret);
138 }
139
140}
141EXPORT_SYMBOL_GPL(wm831x_reg_lock);
142
143/**
144 * wm831x_reg_unlock: Unlock user keyed registers
145 *
146 * The WM831x has a user key preventing writes to particularly
147 * critical registers. This function locks those registers,
148 * preventing spurious writes.
149 */
150int wm831x_reg_unlock(struct wm831x *wm831x)
151{
152 int ret;
153
154 /* 0x9716 is the value required to unlock the registers */
155 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716);
156 if (ret == 0) {
157 dev_vdbg(wm831x->dev, "Registers unlocked\n");
158
159 mutex_lock(&wm831x->io_lock);
160 WARN_ON(!wm831x->locked);
161 wm831x->locked = 0;
162 mutex_unlock(&wm831x->io_lock);
163 }
164
165 return ret;
166}
167EXPORT_SYMBOL_GPL(wm831x_reg_unlock);
168
169static int wm831x_read(struct wm831x *wm831x, unsigned short reg,
170 int bytes, void *dest)
171{
172 int ret, i;
173 u16 *buf = dest;
174
175 BUG_ON(bytes % 2);
176 BUG_ON(bytes <= 0);
177
178 ret = wm831x->read_dev(wm831x, reg, bytes, dest);
179 if (ret < 0)
180 return ret;
181
182 for (i = 0; i < bytes / 2; i++) {
183 buf[i] = be16_to_cpu(buf[i]);
184
185 dev_vdbg(wm831x->dev, "Read %04x from R%d(0x%x)\n",
186 buf[i], reg + i, reg + i);
187 }
188
189 return 0;
190}
191
192/**
193 * wm831x_reg_read: Read a single WM831x register.
194 *
195 * @wm831x: Device to read from.
196 * @reg: Register to read.
197 */
198int wm831x_reg_read(struct wm831x *wm831x, unsigned short reg)
199{
200 unsigned short val;
201 int ret;
202
203 mutex_lock(&wm831x->io_lock);
204
205 ret = wm831x_read(wm831x, reg, 2, &val);
206
207 mutex_unlock(&wm831x->io_lock);
208
209 if (ret < 0)
210 return ret;
211 else
212 return val;
213}
214EXPORT_SYMBOL_GPL(wm831x_reg_read);
215
216/**
217 * wm831x_bulk_read: Read multiple WM831x registers
218 *
219 * @wm831x: Device to read from
220 * @reg: First register
221 * @count: Number of registers
222 * @buf: Buffer to fill.
223 */
224int wm831x_bulk_read(struct wm831x *wm831x, unsigned short reg,
225 int count, u16 *buf)
226{
227 int ret;
228
229 mutex_lock(&wm831x->io_lock);
230
231 ret = wm831x_read(wm831x, reg, count * 2, buf);
232
233 mutex_unlock(&wm831x->io_lock);
234
235 return ret;
236}
237EXPORT_SYMBOL_GPL(wm831x_bulk_read);
238
239static int wm831x_write(struct wm831x *wm831x, unsigned short reg,
240 int bytes, void *src)
241{
242 u16 *buf = src;
243 int i;
244
245 BUG_ON(bytes % 2);
246 BUG_ON(bytes <= 0);
247
248 for (i = 0; i < bytes / 2; i++) {
249 if (wm831x_reg_locked(wm831x, reg))
250 return -EPERM;
251
252 dev_vdbg(wm831x->dev, "Write %04x to R%d(0x%x)\n",
253 buf[i], reg + i, reg + i);
254
255 buf[i] = cpu_to_be16(buf[i]);
256 }
257
258 return wm831x->write_dev(wm831x, reg, bytes, src);
259}
260
261/**
262 * wm831x_reg_write: Write a single WM831x register.
263 *
264 * @wm831x: Device to write to.
265 * @reg: Register to write to.
266 * @val: Value to write.
267 */
268int wm831x_reg_write(struct wm831x *wm831x, unsigned short reg,
269 unsigned short val)
270{
271 int ret;
272
273 mutex_lock(&wm831x->io_lock);
274
275 ret = wm831x_write(wm831x, reg, 2, &val);
276
277 mutex_unlock(&wm831x->io_lock);
278
279 return ret;
280}
281EXPORT_SYMBOL_GPL(wm831x_reg_write);
282
283/**
284 * wm831x_set_bits: Set the value of a bitfield in a WM831x register
285 *
286 * @wm831x: Device to write to.
287 * @reg: Register to write to.
288 * @mask: Mask of bits to set.
289 * @val: Value to set (unshifted)
290 */
291int wm831x_set_bits(struct wm831x *wm831x, unsigned short reg,
292 unsigned short mask, unsigned short val)
293{
294 int ret;
295 u16 r;
296
297 mutex_lock(&wm831x->io_lock);
298
299 ret = wm831x_read(wm831x, reg, 2, &r);
300 if (ret < 0)
301 goto out;
302
303 r &= ~mask;
304 r |= val;
305
306 ret = wm831x_write(wm831x, reg, 2, &r);
307
308out:
309 mutex_unlock(&wm831x->io_lock);
310
311 return ret;
312}
313EXPORT_SYMBOL_GPL(wm831x_set_bits);
314
Mark Brown7e9f9fd2009-07-27 14:45:54 +0100315/**
316 * wm831x_auxadc_read: Read a value from the WM831x AUXADC
317 *
318 * @wm831x: Device to read from.
319 * @input: AUXADC input to read.
320 */
321int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
322{
323 int tries = 10;
324 int ret, src;
325
326 mutex_lock(&wm831x->auxadc_lock);
327
328 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
329 WM831X_AUX_ENA, WM831X_AUX_ENA);
330 if (ret < 0) {
331 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
332 goto out;
333 }
334
335 /* We force a single source at present */
336 src = input;
337 ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
338 1 << src);
339 if (ret < 0) {
340 dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
341 goto out;
342 }
343
344 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
345 WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
346 if (ret < 0) {
347 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
348 goto disable;
349 }
350
351 do {
352 msleep(1);
353
354 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_CONTROL);
355 if (ret < 0)
356 ret = WM831X_AUX_CVT_ENA;
357 } while ((ret & WM831X_AUX_CVT_ENA) && --tries);
358
359 if (ret & WM831X_AUX_CVT_ENA) {
360 dev_err(wm831x->dev, "Timed out reading AUXADC\n");
361 ret = -EBUSY;
362 goto disable;
363 }
364
365 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
366 if (ret < 0) {
367 dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
368 } else {
369 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
370 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
371
372 if (src == 14)
373 src = WM831X_AUX_CAL;
374
375 if (src != input) {
376 dev_err(wm831x->dev, "Data from source %d not %d\n",
377 src, input);
378 ret = -EINVAL;
379 } else {
380 ret &= WM831X_AUX_DATA_MASK;
381 }
382 }
383
384disable:
385 wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
386out:
387 mutex_unlock(&wm831x->auxadc_lock);
388 return ret;
389}
390EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
391
392/**
393 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
394 *
395 * @wm831x: Device to read from.
396 * @input: AUXADC input to read.
397 */
398int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
399{
400 int ret;
401
402 ret = wm831x_auxadc_read(wm831x, input);
403 if (ret < 0)
404 return ret;
405
406 ret *= 1465;
407
408 return ret;
409}
410EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
411
Mark Brownd2bedfe2009-07-27 14:45:52 +0100412static struct resource wm831x_dcdc1_resources[] = {
413 {
414 .start = WM831X_DC1_CONTROL_1,
415 .end = WM831X_DC1_DVS_CONTROL,
416 .flags = IORESOURCE_IO,
417 },
418 {
419 .name = "UV",
420 .start = WM831X_IRQ_UV_DC1,
421 .end = WM831X_IRQ_UV_DC1,
422 .flags = IORESOURCE_IRQ,
423 },
424 {
425 .name = "HC",
426 .start = WM831X_IRQ_HC_DC1,
427 .end = WM831X_IRQ_HC_DC1,
428 .flags = IORESOURCE_IRQ,
429 },
430};
431
432
433static struct resource wm831x_dcdc2_resources[] = {
434 {
435 .start = WM831X_DC2_CONTROL_1,
436 .end = WM831X_DC2_DVS_CONTROL,
437 .flags = IORESOURCE_IO,
438 },
439 {
440 .name = "UV",
441 .start = WM831X_IRQ_UV_DC2,
442 .end = WM831X_IRQ_UV_DC2,
443 .flags = IORESOURCE_IRQ,
444 },
445 {
446 .name = "HC",
447 .start = WM831X_IRQ_HC_DC2,
448 .end = WM831X_IRQ_HC_DC2,
449 .flags = IORESOURCE_IRQ,
450 },
451};
452
453static struct resource wm831x_dcdc3_resources[] = {
454 {
455 .start = WM831X_DC3_CONTROL_1,
456 .end = WM831X_DC3_SLEEP_CONTROL,
457 .flags = IORESOURCE_IO,
458 },
459 {
460 .name = "UV",
461 .start = WM831X_IRQ_UV_DC3,
462 .end = WM831X_IRQ_UV_DC3,
463 .flags = IORESOURCE_IRQ,
464 },
465};
466
467static struct resource wm831x_dcdc4_resources[] = {
468 {
469 .start = WM831X_DC4_CONTROL,
470 .end = WM831X_DC4_SLEEP_CONTROL,
471 .flags = IORESOURCE_IO,
472 },
473 {
474 .name = "UV",
475 .start = WM831X_IRQ_UV_DC4,
476 .end = WM831X_IRQ_UV_DC4,
477 .flags = IORESOURCE_IRQ,
478 },
479};
480
481static struct resource wm831x_gpio_resources[] = {
482 {
483 .start = WM831X_IRQ_GPIO_1,
484 .end = WM831X_IRQ_GPIO_16,
485 .flags = IORESOURCE_IRQ,
486 },
487};
488
489static struct resource wm831x_isink1_resources[] = {
490 {
491 .start = WM831X_CURRENT_SINK_1,
492 .end = WM831X_CURRENT_SINK_1,
493 .flags = IORESOURCE_IO,
494 },
495 {
496 .start = WM831X_IRQ_CS1,
497 .end = WM831X_IRQ_CS1,
498 .flags = IORESOURCE_IRQ,
499 },
500};
501
502static struct resource wm831x_isink2_resources[] = {
503 {
504 .start = WM831X_CURRENT_SINK_2,
505 .end = WM831X_CURRENT_SINK_2,
506 .flags = IORESOURCE_IO,
507 },
508 {
509 .start = WM831X_IRQ_CS2,
510 .end = WM831X_IRQ_CS2,
511 .flags = IORESOURCE_IRQ,
512 },
513};
514
515static struct resource wm831x_ldo1_resources[] = {
516 {
517 .start = WM831X_LDO1_CONTROL,
518 .end = WM831X_LDO1_SLEEP_CONTROL,
519 .flags = IORESOURCE_IO,
520 },
521 {
522 .name = "UV",
523 .start = WM831X_IRQ_UV_LDO1,
524 .end = WM831X_IRQ_UV_LDO1,
525 .flags = IORESOURCE_IRQ,
526 },
527};
528
529static struct resource wm831x_ldo2_resources[] = {
530 {
531 .start = WM831X_LDO2_CONTROL,
532 .end = WM831X_LDO2_SLEEP_CONTROL,
533 .flags = IORESOURCE_IO,
534 },
535 {
536 .name = "UV",
537 .start = WM831X_IRQ_UV_LDO2,
538 .end = WM831X_IRQ_UV_LDO2,
539 .flags = IORESOURCE_IRQ,
540 },
541};
542
543static struct resource wm831x_ldo3_resources[] = {
544 {
545 .start = WM831X_LDO3_CONTROL,
546 .end = WM831X_LDO3_SLEEP_CONTROL,
547 .flags = IORESOURCE_IO,
548 },
549 {
550 .name = "UV",
551 .start = WM831X_IRQ_UV_LDO3,
552 .end = WM831X_IRQ_UV_LDO3,
553 .flags = IORESOURCE_IRQ,
554 },
555};
556
557static struct resource wm831x_ldo4_resources[] = {
558 {
559 .start = WM831X_LDO4_CONTROL,
560 .end = WM831X_LDO4_SLEEP_CONTROL,
561 .flags = IORESOURCE_IO,
562 },
563 {
564 .name = "UV",
565 .start = WM831X_IRQ_UV_LDO4,
566 .end = WM831X_IRQ_UV_LDO4,
567 .flags = IORESOURCE_IRQ,
568 },
569};
570
571static struct resource wm831x_ldo5_resources[] = {
572 {
573 .start = WM831X_LDO5_CONTROL,
574 .end = WM831X_LDO5_SLEEP_CONTROL,
575 .flags = IORESOURCE_IO,
576 },
577 {
578 .name = "UV",
579 .start = WM831X_IRQ_UV_LDO5,
580 .end = WM831X_IRQ_UV_LDO5,
581 .flags = IORESOURCE_IRQ,
582 },
583};
584
585static struct resource wm831x_ldo6_resources[] = {
586 {
587 .start = WM831X_LDO6_CONTROL,
588 .end = WM831X_LDO6_SLEEP_CONTROL,
589 .flags = IORESOURCE_IO,
590 },
591 {
592 .name = "UV",
593 .start = WM831X_IRQ_UV_LDO6,
594 .end = WM831X_IRQ_UV_LDO6,
595 .flags = IORESOURCE_IRQ,
596 },
597};
598
599static struct resource wm831x_ldo7_resources[] = {
600 {
601 .start = WM831X_LDO7_CONTROL,
602 .end = WM831X_LDO7_SLEEP_CONTROL,
603 .flags = IORESOURCE_IO,
604 },
605 {
606 .name = "UV",
607 .start = WM831X_IRQ_UV_LDO7,
608 .end = WM831X_IRQ_UV_LDO7,
609 .flags = IORESOURCE_IRQ,
610 },
611};
612
613static struct resource wm831x_ldo8_resources[] = {
614 {
615 .start = WM831X_LDO8_CONTROL,
616 .end = WM831X_LDO8_SLEEP_CONTROL,
617 .flags = IORESOURCE_IO,
618 },
619 {
620 .name = "UV",
621 .start = WM831X_IRQ_UV_LDO8,
622 .end = WM831X_IRQ_UV_LDO8,
623 .flags = IORESOURCE_IRQ,
624 },
625};
626
627static struct resource wm831x_ldo9_resources[] = {
628 {
629 .start = WM831X_LDO9_CONTROL,
630 .end = WM831X_LDO9_SLEEP_CONTROL,
631 .flags = IORESOURCE_IO,
632 },
633 {
634 .name = "UV",
635 .start = WM831X_IRQ_UV_LDO9,
636 .end = WM831X_IRQ_UV_LDO9,
637 .flags = IORESOURCE_IRQ,
638 },
639};
640
641static struct resource wm831x_ldo10_resources[] = {
642 {
643 .start = WM831X_LDO10_CONTROL,
644 .end = WM831X_LDO10_SLEEP_CONTROL,
645 .flags = IORESOURCE_IO,
646 },
647 {
648 .name = "UV",
649 .start = WM831X_IRQ_UV_LDO10,
650 .end = WM831X_IRQ_UV_LDO10,
651 .flags = IORESOURCE_IRQ,
652 },
653};
654
655static struct resource wm831x_ldo11_resources[] = {
656 {
657 .start = WM831X_LDO11_ON_CONTROL,
658 .end = WM831X_LDO11_SLEEP_CONTROL,
659 .flags = IORESOURCE_IO,
660 },
661};
662
663static struct resource wm831x_on_resources[] = {
664 {
665 .start = WM831X_IRQ_ON,
666 .end = WM831X_IRQ_ON,
667 .flags = IORESOURCE_IRQ,
668 },
669};
670
671
672static struct resource wm831x_power_resources[] = {
673 {
674 .name = "SYSLO",
675 .start = WM831X_IRQ_PPM_SYSLO,
676 .end = WM831X_IRQ_PPM_SYSLO,
677 .flags = IORESOURCE_IRQ,
678 },
679 {
680 .name = "PWR SRC",
681 .start = WM831X_IRQ_PPM_PWR_SRC,
682 .end = WM831X_IRQ_PPM_PWR_SRC,
683 .flags = IORESOURCE_IRQ,
684 },
685 {
686 .name = "USB CURR",
687 .start = WM831X_IRQ_PPM_USB_CURR,
688 .end = WM831X_IRQ_PPM_USB_CURR,
689 .flags = IORESOURCE_IRQ,
690 },
691 {
692 .name = "BATT HOT",
693 .start = WM831X_IRQ_CHG_BATT_HOT,
694 .end = WM831X_IRQ_CHG_BATT_HOT,
695 .flags = IORESOURCE_IRQ,
696 },
697 {
698 .name = "BATT COLD",
699 .start = WM831X_IRQ_CHG_BATT_COLD,
700 .end = WM831X_IRQ_CHG_BATT_COLD,
701 .flags = IORESOURCE_IRQ,
702 },
703 {
704 .name = "BATT FAIL",
705 .start = WM831X_IRQ_CHG_BATT_FAIL,
706 .end = WM831X_IRQ_CHG_BATT_FAIL,
707 .flags = IORESOURCE_IRQ,
708 },
709 {
710 .name = "OV",
711 .start = WM831X_IRQ_CHG_OV,
712 .end = WM831X_IRQ_CHG_OV,
713 .flags = IORESOURCE_IRQ,
714 },
715 {
716 .name = "END",
717 .start = WM831X_IRQ_CHG_END,
718 .end = WM831X_IRQ_CHG_END,
719 .flags = IORESOURCE_IRQ,
720 },
721 {
722 .name = "TO",
723 .start = WM831X_IRQ_CHG_TO,
724 .end = WM831X_IRQ_CHG_TO,
725 .flags = IORESOURCE_IRQ,
726 },
727 {
728 .name = "MODE",
729 .start = WM831X_IRQ_CHG_MODE,
730 .end = WM831X_IRQ_CHG_MODE,
731 .flags = IORESOURCE_IRQ,
732 },
733 {
734 .name = "START",
735 .start = WM831X_IRQ_CHG_START,
736 .end = WM831X_IRQ_CHG_START,
737 .flags = IORESOURCE_IRQ,
738 },
739};
740
741static struct resource wm831x_rtc_resources[] = {
742 {
743 .name = "PER",
744 .start = WM831X_IRQ_RTC_PER,
745 .end = WM831X_IRQ_RTC_PER,
746 .flags = IORESOURCE_IRQ,
747 },
748 {
749 .name = "ALM",
750 .start = WM831X_IRQ_RTC_ALM,
751 .end = WM831X_IRQ_RTC_ALM,
752 .flags = IORESOURCE_IRQ,
753 },
754};
755
756static struct resource wm831x_status1_resources[] = {
757 {
758 .start = WM831X_STATUS_LED_1,
759 .end = WM831X_STATUS_LED_1,
760 .flags = IORESOURCE_IO,
761 },
762};
763
764static struct resource wm831x_status2_resources[] = {
765 {
766 .start = WM831X_STATUS_LED_2,
767 .end = WM831X_STATUS_LED_2,
768 .flags = IORESOURCE_IO,
769 },
770};
771
772static struct resource wm831x_touch_resources[] = {
773 {
774 .name = "TCHPD",
775 .start = WM831X_IRQ_TCHPD,
776 .end = WM831X_IRQ_TCHPD,
777 .flags = IORESOURCE_IRQ,
778 },
779 {
780 .name = "TCHDATA",
781 .start = WM831X_IRQ_TCHDATA,
782 .end = WM831X_IRQ_TCHDATA,
783 .flags = IORESOURCE_IRQ,
784 },
785};
786
787static struct resource wm831x_wdt_resources[] = {
788 {
789 .start = WM831X_IRQ_WDOG_TO,
790 .end = WM831X_IRQ_WDOG_TO,
791 .flags = IORESOURCE_IRQ,
792 },
793};
794
795static struct mfd_cell wm8310_devs[] = {
796 {
Mark Brownc26964e2009-10-01 15:41:06 +0100797 .name = "wm831x-backup",
798 },
799 {
Mark Brownd2bedfe2009-07-27 14:45:52 +0100800 .name = "wm831x-buckv",
801 .id = 1,
802 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
803 .resources = wm831x_dcdc1_resources,
804 },
805 {
806 .name = "wm831x-buckv",
807 .id = 2,
808 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
809 .resources = wm831x_dcdc2_resources,
810 },
811 {
812 .name = "wm831x-buckp",
813 .id = 3,
814 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
815 .resources = wm831x_dcdc3_resources,
816 },
817 {
818 .name = "wm831x-boostp",
819 .id = 4,
820 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
821 .resources = wm831x_dcdc4_resources,
822 },
823 {
824 .name = "wm831x-epe",
825 .id = 1,
826 },
827 {
828 .name = "wm831x-epe",
829 .id = 2,
830 },
831 {
832 .name = "wm831x-gpio",
833 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
834 .resources = wm831x_gpio_resources,
835 },
836 {
837 .name = "wm831x-hwmon",
838 },
839 {
840 .name = "wm831x-isink",
841 .id = 1,
842 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
843 .resources = wm831x_isink1_resources,
844 },
845 {
846 .name = "wm831x-isink",
847 .id = 2,
848 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
849 .resources = wm831x_isink2_resources,
850 },
851 {
852 .name = "wm831x-ldo",
853 .id = 1,
854 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
855 .resources = wm831x_ldo1_resources,
856 },
857 {
858 .name = "wm831x-ldo",
859 .id = 2,
860 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
861 .resources = wm831x_ldo2_resources,
862 },
863 {
864 .name = "wm831x-ldo",
865 .id = 3,
866 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
867 .resources = wm831x_ldo3_resources,
868 },
869 {
870 .name = "wm831x-ldo",
871 .id = 4,
872 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
873 .resources = wm831x_ldo4_resources,
874 },
875 {
876 .name = "wm831x-ldo",
877 .id = 5,
878 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
879 .resources = wm831x_ldo5_resources,
880 },
881 {
882 .name = "wm831x-ldo",
883 .id = 6,
884 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
885 .resources = wm831x_ldo6_resources,
886 },
887 {
888 .name = "wm831x-aldo",
889 .id = 7,
890 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
891 .resources = wm831x_ldo7_resources,
892 },
893 {
894 .name = "wm831x-aldo",
895 .id = 8,
896 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
897 .resources = wm831x_ldo8_resources,
898 },
899 {
900 .name = "wm831x-aldo",
901 .id = 9,
902 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
903 .resources = wm831x_ldo9_resources,
904 },
905 {
906 .name = "wm831x-aldo",
907 .id = 10,
908 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
909 .resources = wm831x_ldo10_resources,
910 },
911 {
912 .name = "wm831x-alive-ldo",
913 .id = 11,
914 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
915 .resources = wm831x_ldo11_resources,
916 },
917 {
918 .name = "wm831x-on",
919 .num_resources = ARRAY_SIZE(wm831x_on_resources),
920 .resources = wm831x_on_resources,
921 },
922 {
923 .name = "wm831x-power",
924 .num_resources = ARRAY_SIZE(wm831x_power_resources),
925 .resources = wm831x_power_resources,
926 },
927 {
928 .name = "wm831x-rtc",
929 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
930 .resources = wm831x_rtc_resources,
931 },
932 {
933 .name = "wm831x-status",
934 .id = 1,
935 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
936 .resources = wm831x_status1_resources,
937 },
938 {
939 .name = "wm831x-status",
940 .id = 2,
941 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
942 .resources = wm831x_status2_resources,
943 },
944 {
945 .name = "wm831x-watchdog",
946 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
947 .resources = wm831x_wdt_resources,
948 },
949};
950
951static struct mfd_cell wm8311_devs[] = {
952 {
Mark Brownc26964e2009-10-01 15:41:06 +0100953 .name = "wm831x-backup",
954 },
955 {
Mark Brownd2bedfe2009-07-27 14:45:52 +0100956 .name = "wm831x-buckv",
957 .id = 1,
958 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
959 .resources = wm831x_dcdc1_resources,
960 },
961 {
962 .name = "wm831x-buckv",
963 .id = 2,
964 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
965 .resources = wm831x_dcdc2_resources,
966 },
967 {
968 .name = "wm831x-buckp",
969 .id = 3,
970 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
971 .resources = wm831x_dcdc3_resources,
972 },
973 {
974 .name = "wm831x-boostp",
975 .id = 4,
976 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
977 .resources = wm831x_dcdc4_resources,
978 },
979 {
980 .name = "wm831x-epe",
981 .id = 1,
982 },
983 {
984 .name = "wm831x-epe",
985 .id = 2,
986 },
987 {
988 .name = "wm831x-gpio",
989 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
990 .resources = wm831x_gpio_resources,
991 },
992 {
993 .name = "wm831x-hwmon",
994 },
995 {
996 .name = "wm831x-isink",
997 .id = 1,
998 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
999 .resources = wm831x_isink1_resources,
1000 },
1001 {
1002 .name = "wm831x-isink",
1003 .id = 2,
1004 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1005 .resources = wm831x_isink2_resources,
1006 },
1007 {
1008 .name = "wm831x-ldo",
1009 .id = 1,
1010 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1011 .resources = wm831x_ldo1_resources,
1012 },
1013 {
1014 .name = "wm831x-ldo",
1015 .id = 2,
1016 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1017 .resources = wm831x_ldo2_resources,
1018 },
1019 {
1020 .name = "wm831x-ldo",
1021 .id = 3,
1022 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1023 .resources = wm831x_ldo3_resources,
1024 },
1025 {
1026 .name = "wm831x-ldo",
1027 .id = 4,
1028 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1029 .resources = wm831x_ldo4_resources,
1030 },
1031 {
1032 .name = "wm831x-ldo",
1033 .id = 5,
1034 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1035 .resources = wm831x_ldo5_resources,
1036 },
1037 {
1038 .name = "wm831x-aldo",
1039 .id = 7,
1040 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1041 .resources = wm831x_ldo7_resources,
1042 },
1043 {
1044 .name = "wm831x-alive-ldo",
1045 .id = 11,
1046 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1047 .resources = wm831x_ldo11_resources,
1048 },
1049 {
1050 .name = "wm831x-on",
1051 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1052 .resources = wm831x_on_resources,
1053 },
1054 {
1055 .name = "wm831x-power",
1056 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1057 .resources = wm831x_power_resources,
1058 },
1059 {
1060 .name = "wm831x-rtc",
1061 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1062 .resources = wm831x_rtc_resources,
1063 },
1064 {
1065 .name = "wm831x-status",
1066 .id = 1,
1067 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1068 .resources = wm831x_status1_resources,
1069 },
1070 {
1071 .name = "wm831x-status",
1072 .id = 2,
1073 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1074 .resources = wm831x_status2_resources,
1075 },
1076 {
1077 .name = "wm831x-touch",
1078 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1079 .resources = wm831x_touch_resources,
1080 },
1081 {
1082 .name = "wm831x-watchdog",
1083 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1084 .resources = wm831x_wdt_resources,
1085 },
1086};
1087
1088static struct mfd_cell wm8312_devs[] = {
1089 {
Mark Brownc26964e2009-10-01 15:41:06 +01001090 .name = "wm831x-backup",
1091 },
1092 {
Mark Brownd2bedfe2009-07-27 14:45:52 +01001093 .name = "wm831x-buckv",
1094 .id = 1,
1095 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1096 .resources = wm831x_dcdc1_resources,
1097 },
1098 {
1099 .name = "wm831x-buckv",
1100 .id = 2,
1101 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1102 .resources = wm831x_dcdc2_resources,
1103 },
1104 {
1105 .name = "wm831x-buckp",
1106 .id = 3,
1107 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1108 .resources = wm831x_dcdc3_resources,
1109 },
1110 {
1111 .name = "wm831x-boostp",
1112 .id = 4,
1113 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1114 .resources = wm831x_dcdc4_resources,
1115 },
1116 {
1117 .name = "wm831x-epe",
1118 .id = 1,
1119 },
1120 {
1121 .name = "wm831x-epe",
1122 .id = 2,
1123 },
1124 {
1125 .name = "wm831x-gpio",
1126 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1127 .resources = wm831x_gpio_resources,
1128 },
1129 {
1130 .name = "wm831x-hwmon",
1131 },
1132 {
1133 .name = "wm831x-isink",
1134 .id = 1,
1135 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1136 .resources = wm831x_isink1_resources,
1137 },
1138 {
1139 .name = "wm831x-isink",
1140 .id = 2,
1141 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1142 .resources = wm831x_isink2_resources,
1143 },
1144 {
1145 .name = "wm831x-ldo",
1146 .id = 1,
1147 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1148 .resources = wm831x_ldo1_resources,
1149 },
1150 {
1151 .name = "wm831x-ldo",
1152 .id = 2,
1153 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1154 .resources = wm831x_ldo2_resources,
1155 },
1156 {
1157 .name = "wm831x-ldo",
1158 .id = 3,
1159 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1160 .resources = wm831x_ldo3_resources,
1161 },
1162 {
1163 .name = "wm831x-ldo",
1164 .id = 4,
1165 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1166 .resources = wm831x_ldo4_resources,
1167 },
1168 {
1169 .name = "wm831x-ldo",
1170 .id = 5,
1171 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1172 .resources = wm831x_ldo5_resources,
1173 },
1174 {
1175 .name = "wm831x-ldo",
1176 .id = 6,
1177 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1178 .resources = wm831x_ldo6_resources,
1179 },
1180 {
1181 .name = "wm831x-aldo",
1182 .id = 7,
1183 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1184 .resources = wm831x_ldo7_resources,
1185 },
1186 {
1187 .name = "wm831x-aldo",
1188 .id = 8,
1189 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1190 .resources = wm831x_ldo8_resources,
1191 },
1192 {
1193 .name = "wm831x-aldo",
1194 .id = 9,
1195 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1196 .resources = wm831x_ldo9_resources,
1197 },
1198 {
1199 .name = "wm831x-aldo",
1200 .id = 10,
1201 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1202 .resources = wm831x_ldo10_resources,
1203 },
1204 {
1205 .name = "wm831x-alive-ldo",
1206 .id = 11,
1207 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1208 .resources = wm831x_ldo11_resources,
1209 },
1210 {
1211 .name = "wm831x-on",
1212 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1213 .resources = wm831x_on_resources,
1214 },
1215 {
1216 .name = "wm831x-power",
1217 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1218 .resources = wm831x_power_resources,
1219 },
1220 {
1221 .name = "wm831x-rtc",
1222 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1223 .resources = wm831x_rtc_resources,
1224 },
1225 {
1226 .name = "wm831x-status",
1227 .id = 1,
1228 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1229 .resources = wm831x_status1_resources,
1230 },
1231 {
1232 .name = "wm831x-status",
1233 .id = 2,
1234 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1235 .resources = wm831x_status2_resources,
1236 },
1237 {
1238 .name = "wm831x-touch",
1239 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1240 .resources = wm831x_touch_resources,
1241 },
1242 {
1243 .name = "wm831x-watchdog",
1244 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1245 .resources = wm831x_wdt_resources,
1246 },
1247};
1248
Mark Brown63aed852009-07-27 14:45:55 +01001249static struct mfd_cell backlight_devs[] = {
1250 {
1251 .name = "wm831x-backlight",
1252 },
1253};
1254
Mark Brownd2bedfe2009-07-27 14:45:52 +01001255/*
1256 * Instantiate the generic non-control parts of the device.
1257 */
1258static int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
1259{
1260 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1261 int rev;
1262 enum wm831x_parent parent;
1263 int ret;
1264
1265 mutex_init(&wm831x->io_lock);
1266 mutex_init(&wm831x->key_lock);
Mark Brown7e9f9fd2009-07-27 14:45:54 +01001267 mutex_init(&wm831x->auxadc_lock);
Mark Brownd2bedfe2009-07-27 14:45:52 +01001268 dev_set_drvdata(wm831x->dev, wm831x);
1269
1270 ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
1271 if (ret < 0) {
1272 dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
1273 goto err;
1274 }
1275 if (ret != 0x6204) {
1276 dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
1277 ret = -EINVAL;
1278 goto err;
1279 }
1280
1281 ret = wm831x_reg_read(wm831x, WM831X_REVISION);
1282 if (ret < 0) {
1283 dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
1284 goto err;
1285 }
1286 rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
1287
1288 ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
1289 if (ret < 0) {
1290 dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
1291 goto err;
1292 }
1293
1294 switch (ret) {
1295 case 0x8310:
1296 parent = WM8310;
1297 switch (rev) {
1298 case 0:
1299 dev_info(wm831x->dev, "WM8310 revision %c\n",
1300 'A' + rev);
1301 break;
1302 }
1303 break;
1304
1305 case 0x8311:
1306 parent = WM8311;
1307 switch (rev) {
1308 case 0:
1309 dev_info(wm831x->dev, "WM8311 revision %c\n",
1310 'A' + rev);
1311 break;
1312 }
1313 break;
1314
1315 case 0x8312:
1316 parent = WM8312;
1317 switch (rev) {
1318 case 0:
1319 dev_info(wm831x->dev, "WM8312 revision %c\n",
1320 'A' + rev);
1321 break;
1322 }
1323 break;
1324
1325 case 0:
1326 /* Some engineering samples do not have the ID set,
1327 * rely on the device being registered correctly.
1328 * This will need revisiting for future devices with
1329 * multiple dies.
1330 */
1331 parent = id;
1332 switch (rev) {
1333 case 0:
1334 dev_info(wm831x->dev, "WM831%d ES revision %c\n",
1335 parent, 'A' + rev);
1336 break;
1337 }
1338 break;
1339
1340 default:
1341 dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
1342 ret = -EINVAL;
1343 goto err;
1344 }
1345
1346 /* This will need revisiting in future but is OK for all
1347 * current parts.
1348 */
1349 if (parent != id)
1350 dev_warn(wm831x->dev, "Device was registered as a WM831%lu\n",
1351 id);
1352
1353 /* Bootstrap the user key */
1354 ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
1355 if (ret < 0) {
1356 dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
1357 goto err;
1358 }
1359 if (ret != 0) {
1360 dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
1361 ret);
1362 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
1363 }
1364 wm831x->locked = 1;
1365
1366 if (pdata && pdata->pre_init) {
1367 ret = pdata->pre_init(wm831x);
1368 if (ret != 0) {
1369 dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
1370 goto err;
1371 }
1372 }
1373
Mark Brown7d4d0a32009-07-27 14:45:53 +01001374 ret = wm831x_irq_init(wm831x, irq);
1375 if (ret != 0)
1376 goto err;
1377
Mark Brownd2bedfe2009-07-27 14:45:52 +01001378 /* The core device is up, instantiate the subdevices. */
1379 switch (parent) {
1380 case WM8310:
1381 ret = mfd_add_devices(wm831x->dev, -1,
1382 wm8310_devs, ARRAY_SIZE(wm8310_devs),
1383 NULL, 0);
1384 break;
1385
1386 case WM8311:
1387 ret = mfd_add_devices(wm831x->dev, -1,
1388 wm8311_devs, ARRAY_SIZE(wm8311_devs),
1389 NULL, 0);
1390 break;
1391
1392 case WM8312:
1393 ret = mfd_add_devices(wm831x->dev, -1,
1394 wm8312_devs, ARRAY_SIZE(wm8312_devs),
1395 NULL, 0);
1396 break;
1397
1398 default:
1399 /* If this happens the bus probe function is buggy */
1400 BUG();
1401 }
1402
1403 if (ret != 0) {
1404 dev_err(wm831x->dev, "Failed to add children\n");
Mark Brown7d4d0a32009-07-27 14:45:53 +01001405 goto err_irq;
Mark Brownd2bedfe2009-07-27 14:45:52 +01001406 }
1407
Mark Brown63aed852009-07-27 14:45:55 +01001408 if (pdata && pdata->backlight) {
1409 /* Treat errors as non-critical */
1410 ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
1411 ARRAY_SIZE(backlight_devs), NULL, 0);
1412 if (ret < 0)
1413 dev_err(wm831x->dev, "Failed to add backlight: %d\n",
1414 ret);
1415 }
1416
Mark Brown6704e512009-07-27 14:45:56 +01001417 wm831x_otp_init(wm831x);
1418
Mark Brownd2bedfe2009-07-27 14:45:52 +01001419 if (pdata && pdata->post_init) {
1420 ret = pdata->post_init(wm831x);
1421 if (ret != 0) {
1422 dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
Mark Brown7d4d0a32009-07-27 14:45:53 +01001423 goto err_irq;
Mark Brownd2bedfe2009-07-27 14:45:52 +01001424 }
1425 }
1426
1427 return 0;
1428
Mark Brown7d4d0a32009-07-27 14:45:53 +01001429err_irq:
1430 wm831x_irq_exit(wm831x);
Mark Brownd2bedfe2009-07-27 14:45:52 +01001431err:
1432 mfd_remove_devices(wm831x->dev);
1433 kfree(wm831x);
1434 return ret;
1435}
1436
1437static void wm831x_device_exit(struct wm831x *wm831x)
1438{
Mark Brown6704e512009-07-27 14:45:56 +01001439 wm831x_otp_exit(wm831x);
Mark Brownd2bedfe2009-07-27 14:45:52 +01001440 mfd_remove_devices(wm831x->dev);
Mark Brown7d4d0a32009-07-27 14:45:53 +01001441 wm831x_irq_exit(wm831x);
Mark Brownd2bedfe2009-07-27 14:45:52 +01001442 kfree(wm831x);
1443}
1444
1445static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
1446 int bytes, void *dest)
1447{
1448 struct i2c_client *i2c = wm831x->control_data;
1449 int ret;
1450 u16 r = cpu_to_be16(reg);
1451
1452 ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
1453 if (ret < 0)
1454 return ret;
1455 if (ret != 2)
1456 return -EIO;
1457
1458 ret = i2c_master_recv(i2c, dest, bytes);
1459 if (ret < 0)
1460 return ret;
1461 if (ret != bytes)
1462 return -EIO;
1463 return 0;
1464}
1465
1466/* Currently we allocate the write buffer on the stack; this is OK for
1467 * small writes - if we need to do large writes this will need to be
1468 * revised.
1469 */
1470static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
1471 int bytes, void *src)
1472{
1473 struct i2c_client *i2c = wm831x->control_data;
1474 unsigned char msg[bytes + 2];
1475 int ret;
1476
1477 reg = cpu_to_be16(reg);
1478 memcpy(&msg[0], &reg, 2);
1479 memcpy(&msg[2], src, bytes);
1480
1481 ret = i2c_master_send(i2c, msg, bytes + 2);
1482 if (ret < 0)
1483 return ret;
1484 if (ret < bytes + 2)
1485 return -EIO;
1486
1487 return 0;
1488}
1489
1490static int wm831x_i2c_probe(struct i2c_client *i2c,
1491 const struct i2c_device_id *id)
1492{
1493 struct wm831x *wm831x;
1494
1495 wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
1496 if (wm831x == NULL) {
1497 kfree(i2c);
1498 return -ENOMEM;
1499 }
1500
1501 i2c_set_clientdata(i2c, wm831x);
1502 wm831x->dev = &i2c->dev;
1503 wm831x->control_data = i2c;
1504 wm831x->read_dev = wm831x_i2c_read_device;
1505 wm831x->write_dev = wm831x_i2c_write_device;
1506
1507 return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
1508}
1509
1510static int wm831x_i2c_remove(struct i2c_client *i2c)
1511{
1512 struct wm831x *wm831x = i2c_get_clientdata(i2c);
1513
1514 wm831x_device_exit(wm831x);
1515
1516 return 0;
1517}
1518
1519static const struct i2c_device_id wm831x_i2c_id[] = {
1520 { "wm8310", WM8310 },
1521 { "wm8311", WM8311 },
1522 { "wm8312", WM8312 },
1523 { }
1524};
1525MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
1526
1527
1528static struct i2c_driver wm831x_i2c_driver = {
1529 .driver = {
1530 .name = "wm831x",
1531 .owner = THIS_MODULE,
1532 },
1533 .probe = wm831x_i2c_probe,
1534 .remove = wm831x_i2c_remove,
1535 .id_table = wm831x_i2c_id,
1536};
1537
1538static int __init wm831x_i2c_init(void)
1539{
1540 int ret;
1541
1542 ret = i2c_add_driver(&wm831x_i2c_driver);
1543 if (ret != 0)
1544 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
1545
1546 return ret;
1547}
1548subsys_initcall(wm831x_i2c_init);
1549
1550static void __exit wm831x_i2c_exit(void)
1551{
1552 i2c_del_driver(&wm831x_i2c_driver);
1553}
1554module_exit(wm831x_i2c_exit);
1555
1556MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
1557MODULE_LICENSE("GPL");
1558MODULE_AUTHOR("Mark Brown");