wm8350: i2c - removed battery init from I2C driver.
[linux-2.6-audioplus.git] / drivers / regulator / wm8350 / reg-wm8350-bus.c
1 /*
2  * wm8350_bus.c  --  Power Management Driver for Wolfson WM8350 PMIC
3  *
4  * Copyright 2007 Wolfson Microelectronics PLC.
5  *
6  * Author: Liam Girdwood
7  *         liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  *
14  *  Revision history
15  *    23rd Jan 2007   Initial version.
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/device.h>
23 #include <linux/i2c.h>
24 #include <linux/spi/spi.h>
25 #include <linux/regulator/wm8350/cache.h>
26 #include <linux/regulator/wm8350/bus.h>
27 #include <linux/regulator/wm8350/pmic.h>
28 #include <linux/regulator/wm8350/gpio.h>
29 #include <linux/regulator/wm8350/comparator.h>
30 #include <linux/regulator/wm8350/supply.h>
31 #include <linux/regulator/wm8350/audio.h>
32 #include <linux/delay.h>
33
34 #define WM8350_UNLOCK_KEY               0x0013
35 #define WM8350_LOCK_KEY                 0x0000
36
37 #define WM8350_CLOCK_CONTROL_1          0x28
38 #define WM8350_AIF_TEST                 0x74
39
40 /* debug */
41 #define WM8350_BUS_DEBUG 0
42 #if WM8350_BUS_DEBUG
43 #define dbg(format, arg...) printk(KERN_DEBUG format, ## arg)
44 #define dump(regs, src) do { \
45         int i; \
46         u16 *src_ = src; \
47         for (i = 0; i < regs; i++) \
48                 dbg(" 0x%4.4x", *src_++); \
49         dbg("\n"); \
50 } while (0);
51 #else
52 #define dbg(format, arg...)
53 #define dump(bytes, src)
54 #endif
55
56 #define WM8350_LOCK_DEBUG 0
57 #if WM8350_LOCK_DEBUG
58 #define ldbg(format, arg...) printk(format, ## arg)
59 #else
60 #define ldbg(format, arg...)
61 #endif
62
63 #define BYTE_SWAP16(x) (x >> 8 | x << 8)        /*lg replace with generic */
64
65 /*
66  * WM8350 can run in 1 of 4 configuration modes.
67  * Each mode has different default register values.
68  */
69 #if defined(CONFIG_PMIC_WM8350_MODE_0)
70 static const u16 wm8350_reg_map[] = WM8350_REGISTER_DEFAULTS_0;
71 #elif defined(CONFIG_PMIC_WM8350_MODE_1)
72 static const u16 wm8350_reg_map[] = WM8350_REGISTER_DEFAULTS_1;
73 #elif defined(CONFIG_PMIC_WM8350_MODE_2)
74 static const u16 wm8350_reg_map[] = WM8350_REGISTER_DEFAULTS_2;
75 #elif defined(CONFIG_PMIC_WM8350_MODE_3)
76 static const u16 wm8350_reg_map[] = WM8350_REGISTER_DEFAULTS_3;
77 #else
78 #error Invalid WM8350 configuration
79 #endif
80
81 /*
82  * WM8350 Register IO access map
83  */
84 static const struct wm8350_reg_access wm8350_reg_io_map[] = WM8350_ACCESS;
85
86 /*
87  * WM8350 Device IO
88  */
89 static DEFINE_MUTEX(io_mutex);
90 static DEFINE_MUTEX(reg_lock_mutex);
91 static DEFINE_MUTEX(auxadc_mutex);
92
93 #ifdef CONFIG_I2C
94 static int wm8350_read_i2c_device(struct wm8350 *wm8350, char reg,
95                                   int bytes, char *dest)
96 {
97         int ret;
98
99         ret = i2c_master_send(wm8350->i2c_client, &reg, 1);
100         if (ret < 0)
101                 return ret;
102         return i2c_master_recv(wm8350->i2c_client, dest, bytes);
103 }
104
105 static int wm8350_write_i2c_device(struct wm8350 *wm8350, char reg,
106                                    int bytes, char *src)
107 {
108         /* we add 1 byte for device register */
109         u8 msg[(WM8350_MAX_REGISTER << 1) + 1];
110
111         if (bytes > ((WM8350_MAX_REGISTER << 1) + 1))
112                 return -EINVAL;
113
114         msg[0] = reg;
115         memcpy(&msg[1], src, bytes);
116         return i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
117 }
118 #endif
119
120 #ifdef CONFIG_SPI
121 static int wm8350_read_spi_device(struct wm8350 *wm8350, char reg,
122                                   int bytes, char *dest)
123 {
124         int ret, done;
125         u8 tx_msg[4], rx_msg[4];
126
127         /* don't support incremental write with SPI */
128         if (bytes > 2) {
129                 done = 0;
130                 while (bytes) {
131                         ret = wm8350_read_spi_device(wm8350, reg, 2, dest);
132                         if (ret != 2)
133                                 return ret;
134                         
135                         done += 2;
136                         bytes -= 2;
137                         dest += 2;
138                         reg++;
139                 }
140
141                 return done;
142         }
143
144         tx_msg[0] = 0x80;
145         tx_msg[1] = reg;
146         tx_msg[2] = 0;
147         tx_msg[3] = 0;
148
149         ret = spi_write_then_read(wm8350->spi_device, tx_msg, 4, rx_msg, 4);
150         if (ret < 0) {
151                 printk(KERN_ERR "%s: io failure %d\n", __func__, ret);
152                 return 0;
153         }
154
155         *dest++ = rx_msg[2];
156         *dest = rx_msg[3];
157         return 0;
158 }
159
160 static int wm8350_write_spi_device(struct wm8350 *wm8350, char reg,
161                                    int bytes, char *src)
162 {
163         u8 msg[4];
164         int done = 0, ret;
165
166         /* don't support incremental write with SPI */
167         if (bytes > 2) {
168                 while (bytes) {
169                         ret = wm8350_write_spi_device(wm8350, reg, 2, src);
170                         if (ret != 2)
171                                 return ret;
172                         
173                         bytes -= 2;
174                         done += 2;
175                         src += 2;
176                         reg++;
177                 }
178
179                 return done;
180         }
181
182         msg[0] = 0;
183         msg[1] = reg;
184         msg[2] = *src++;
185         msg[3] = *src;
186         return spi_write(wm8350->spi_device, msg, 4);
187 }
188 #endif
189
190 /* mask in WM8350 read bits */
191 static inline void wm8350_mask_read(u8 reg, int bytes, u16 * buf)
192 {
193         int i;
194
195         for (i = reg; i < reg + (bytes >> 1); i++)
196                 *buf++ &= wm8350_reg_io_map[i].readable;
197 }
198
199 /* mask in WM8350 write bits */
200 static inline void wm8350_mask_write(u8 reg, int bytes, u16 * buf)
201 {
202         int i;
203
204         for (i = reg; i < reg + (bytes >> 1); i++)
205                 *buf++ &= wm8350_reg_io_map[i].writable;
206 }
207
208 /* WM8350 is big endian, swap if necessary */
209 static inline void wm8350_endian_swap(u8 reg, int bytes, u16 * buf)
210 {
211 #ifdef  __LITTLE_ENDIAN
212         int i;
213         u16 tmp;
214
215         for (i = reg; i < reg + (bytes >> 1); i++) {
216                 tmp = BYTE_SWAP16(*buf);
217                 *buf++ = tmp;
218         }
219 #endif
220 }
221
222 static inline void wm8350_cache_mask(struct wm8350 *wm8350, u8 reg,
223                                      int bytes, u16 * dest)
224 {
225         int i;
226         u16 mask;
227
228         for (i = reg; i < reg + (bytes >> 1); i++) {
229                 *dest &= wm8350_reg_io_map[i].vol;
230                 mask = wm8350->reg_cache[reg] & ~wm8350_reg_io_map[i].vol;
231                 *dest |= mask;
232                 dest++;
233         }
234 }
235
236 static int wm8350_read(struct wm8350 *wm8350, u8 reg, int num_regs, u16 * dest)
237 {
238         int i, end = reg + num_regs, ret = 0, bytes = num_regs << 1;
239
240         if (wm8350->read_dev == NULL)
241                 return -ENODEV;
242
243         if ((reg + num_regs - 1) > WM8350_MAX_REGISTER) {
244                 printk(KERN_ERR "wm8350: invalid reg %x\n", reg + num_regs - 1);
245                 return -EINVAL;
246         }
247
248         dbg("%s R%d(0x%2.2x) %d regs ", __FUNCTION__, reg, reg, num_regs);
249
250 #if WM8350_BUS_DEBUG
251         /* we can _safely_ read any register, but warn if read not supported */
252         for (i = reg; i < end; i++) {
253                 if (!wm8350_reg_io_map[i].readable)
254                         printk(KERN_WARNING "wm8350: reg R%d is not readable\n",
255                                i);
256         }
257 #endif
258         /* if any volatile registers are required, then read back all */
259         for (i = reg; i < end; i++) {
260                 if (wm8350_reg_io_map[i].vol) {
261                         dbg("volatile read ");
262                         ret = wm8350->read_dev(wm8350, reg,
263                                                bytes, (char *)dest);
264                         wm8350_endian_swap(reg, bytes, dest);
265                         wm8350_cache_mask(wm8350, reg, bytes, dest);
266                         wm8350_mask_read(reg, bytes, dest);
267                         dump(num_regs, dest);
268                         return ret;
269                 }
270         }
271
272         /* no volatiles, then cache is good */
273         dbg("cache read ");
274         memcpy(dest, &wm8350->reg_cache[reg], bytes);
275         dump(num_regs, dest);
276         return ret;
277 }
278
279 static inline int is_reg_locked(struct wm8350 *wm8350, u8 reg)
280 {
281         if (reg == WM8350_SECURITY ||
282             wm8350->reg_cache[WM8350_SECURITY] == WM8350_UNLOCK_KEY)
283                 return 0;
284
285         if ((reg == WM8350_GPIO_CONFIGURATION_I_O) ||
286             (reg >= WM8350_GPIO_FUNCTION_SELECT_1 &&
287              reg <= WM8350_GPIO_FUNCTION_SELECT_4) ||
288             (reg >= WM8350_BATTERY_CHARGER_CONTROL_1 &&
289              reg <= WM8350_BATTERY_CHARGER_CONTROL_3))
290                 return 1;
291         return 0;
292 }
293
294 static int wm8350_write(struct wm8350 *wm8350, u8 reg, int num_regs, u16 * src)
295 {
296         int ret, i, end = reg + num_regs, bytes = num_regs << 1;
297
298         if (wm8350->write_dev == NULL)
299                 return -ENODEV;
300
301         if ((reg + num_regs - 1) > WM8350_MAX_REGISTER) {
302                 printk(KERN_ERR "wm8350: invalid reg %x\n", reg + num_regs - 1);
303                 return -EINVAL;
304         }
305
306         wm8350_mask_write(reg, bytes, src);
307         memcpy(&wm8350->reg_cache[reg], src, bytes);
308         dbg("%s R%d(0x%2.2x) %d regs ", __FUNCTION__, reg, reg, num_regs);
309         dump(num_regs, src);
310
311         wm8350_endian_swap(reg, bytes, src);
312
313         /* it's generally not a good idea to write to RO or locked registers */
314         for (i = reg; i < end; i++) {
315                 if (!wm8350_reg_io_map[i].writable) {
316                         printk(KERN_ERR
317                                "wm8350: attempted write to read only reg R%d\n",
318                                i);
319                         return -EINVAL;
320                 }
321
322                 if (is_reg_locked(wm8350, i)) {
323                         printk(KERN_ERR
324                                "wm8350: attempted write to locked reg R%d\n",
325                                i);
326                         return -EINVAL;
327                 }
328         }
329
330         /* write registers and update cache if successful */
331         ret = wm8350->write_dev(wm8350, reg, bytes, (char *)src);
332         return ret;
333 }
334
335 /*
336  * Safe read, modify, write methods
337  */
338 int wm8350_clear_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
339 {
340         u16 data;
341         int err;
342
343         mutex_lock(&io_mutex);
344         err = wm8350_read(wm8350, reg, 1, &data);
345         if (err) {
346                 printk(KERN_ERR "wm8350: read from reg R%d failed\n", reg);
347                 goto out;
348         }
349
350         data &= ~mask;
351         err = wm8350_write(wm8350, reg, 1, &data);
352         if (err)
353                 printk(KERN_ERR "wm8350: write to reg R%d failed\n", reg);
354 out:
355         mutex_unlock(&io_mutex);
356         return err;
357 }
358 EXPORT_SYMBOL_GPL(wm8350_clear_bits);
359
360 int wm8350_set_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
361 {
362         u16 data;
363         int err;
364
365         mutex_lock(&io_mutex);
366         err = wm8350_read(wm8350, reg, 1, &data);
367         if (err) {
368                 printk(KERN_ERR "wm8350: read from reg R%d failed\n", reg);
369                 goto out;
370         }
371
372         data |= mask;
373         err = wm8350_write(wm8350, reg, 1, &data);
374         if (err)
375                 printk(KERN_ERR "wm8350: write to reg R%d failed\n", reg);
376 out:
377         mutex_unlock(&io_mutex);
378         return err;
379 }
380 EXPORT_SYMBOL_GPL(wm8350_set_bits);
381
382 u16 wm8350_reg_read(struct wm8350 *wm8350, int reg)
383 {
384         u16 data;
385         int err;
386
387         mutex_lock(&io_mutex);
388         err = wm8350_read(wm8350, reg, 1, &data);
389         if (err)
390                 printk(KERN_ERR "wm8350: read from reg R%d failed\n", reg);
391
392         mutex_unlock(&io_mutex);
393         return data;
394 }
395 EXPORT_SYMBOL_GPL(wm8350_reg_read);
396
397 int wm8350_reg_write(struct wm8350 *wm8350, int reg, u16 val)
398 {
399         int ret;
400         u16 data = val;
401
402         mutex_lock(&io_mutex);
403         ret = wm8350_write(wm8350, reg, 1, &data);
404         if (ret)
405                 printk(KERN_ERR "wm8350: write to reg R%d failed\n", reg);
406         mutex_unlock(&io_mutex);
407         return ret;
408 }
409 EXPORT_SYMBOL_GPL(wm8350_reg_write);
410
411 int wm8350_block_read(struct wm8350 *wm8350, int start_reg, int regs,
412                       u16 * dest)
413 {
414         int err = 0;
415
416         mutex_lock(&io_mutex);
417         err = wm8350_read(wm8350, start_reg, regs, dest);
418         if (err)
419                 printk(KERN_ERR "wm8350: block read starting from R%d failed\n",
420                        start_reg);
421         mutex_unlock(&io_mutex);
422         return err;
423 }
424 EXPORT_SYMBOL_GPL(wm8350_block_read);
425
426 int wm8350_block_write(struct wm8350 *wm8350, int start_reg, int regs,
427                        u16 * src)
428 {
429         int ret = 0;
430
431         mutex_lock(&io_mutex);
432         ret = wm8350_write(wm8350, start_reg, regs, src);
433         if (ret)
434                 printk(KERN_ERR "wm8350: block write starting at R%d failed\n",
435                        start_reg);
436         mutex_unlock(&io_mutex);
437         return ret;
438 }
439 EXPORT_SYMBOL_GPL(wm8350_block_write);
440
441 int wm8350_reg_lock(struct wm8350 *wm8350)
442 {
443         u16 key = WM8350_LOCK_KEY;
444         int ret;
445
446         ldbg("%s\n", __FUNCTION__);
447         mutex_lock(&io_mutex);
448         ret = wm8350_write(wm8350, WM8350_SECURITY, 1, &key);
449         if (ret)
450                 printk(KERN_ERR "wm8350: lock failed\n");
451         mutex_unlock(&io_mutex);
452         return ret;
453 }
454 EXPORT_SYMBOL_GPL(wm8350_reg_lock);
455
456 int wm8350_reg_unlock(struct wm8350 *wm8350)
457 {
458         u16 key = WM8350_UNLOCK_KEY;
459         int ret;
460
461         ldbg("%s\n", __FUNCTION__);
462         mutex_lock(&io_mutex);
463         ret = wm8350_write(wm8350, WM8350_SECURITY, 1, &key);
464         if (ret)
465                 printk(KERN_ERR "wm8350: unlock failed\n");
466         mutex_unlock(&io_mutex);
467         return ret;
468 }
469 EXPORT_SYMBOL_GPL(wm8350_reg_unlock);
470
471 /*
472  * For Switching between SPI and I2C IO
473  */
474 int wm8350_set_io(struct wm8350 *wm8350, int io,
475                   int (*read_dev) (struct wm8350 *wm8350, char reg, int size,
476                                    char *dest),
477                   int (*write_dev) (struct wm8350 *wm8350, char reg, int size,
478                                     char *src))
479 {
480         mutex_lock(&io_mutex);
481         switch (io) {
482         case WM8350_IO_I2C:
483 #ifdef CONFIG_I2C
484                 wm8350->read_dev = wm8350_read_i2c_device;
485                 wm8350->write_dev = wm8350_write_i2c_device;
486                 break;
487 #else
488                 printk(KERN_ERR "wm8350: I2C not selected.\n");
489                 wm8350->read_dev = NULL;
490                 wm8350->write_dev = NULL;
491                 mutex_unlock(&io_mutex);
492                 return -EINVAL;
493 #endif
494         case WM8350_IO_SPI:
495 #ifdef CONFIG_SPI
496                 wm8350->read_dev = wm8350_read_spi_device;
497                 wm8350->write_dev = wm8350_write_spi_device;
498                 break;
499 #else
500                 printk(KERN_ERR "wm8350: SPI not selected.\n");
501                 wm8350->read_dev = NULL;
502                 wm8350->write_dev = NULL;
503                 mutex_unlock(&io_mutex);
504                 return -EINVAL;
505 #endif
506         default:
507                 printk(KERN_ERR "wm8350: invalid IO mechanism\n");
508                 wm8350->read_dev = NULL;
509                 wm8350->write_dev = NULL;
510                 mutex_unlock(&io_mutex);
511                 return -EINVAL;
512         }
513         mutex_unlock(&io_mutex);
514         return 0;
515 }
516 EXPORT_SYMBOL_GPL(wm8350_set_io);
517
518 /*
519  * Cache is always host endian.
520  */
521 int wm8350_create_cache(struct wm8350 *wm8350)
522 {
523         int i, ret = 0;
524         u16 value;
525
526         if (wm8350->read_dev == NULL)
527                 return -ENODEV;
528
529         wm8350->reg_cache =
530             kzalloc(sizeof(u16) * (WM8350_MAX_REGISTER + 1), GFP_KERNEL);
531         if (wm8350->reg_cache == NULL)
532                 return -ENOMEM;
533
534         /* TODO: check if we are virgin state so we don't have to do this */
535         /* refresh cache with chip regs as some registers can survive reboot */
536         for (i = 0; i < WM8350_MAX_REGISTER; i++) {
537                 /* audio register range */
538                 if (wm8350_reg_io_map[i].readable &&
539                     (i < WM8350_CLOCK_CONTROL_1 || i > WM8350_AIF_TEST)) {
540                         ret = wm8350->read_dev(wm8350, i, 2, (char *)&value);
541                         if (ret < 0) {
542                                 printk(KERN_ERR
543                                        "wm8350: failed to create cache\n");
544                                 goto out;
545                         }
546                         wm8350_endian_swap(i, 2, &value);
547                         wm8350_mask_read(i, 2, &value);
548                         wm8350->reg_cache[i] = value;
549                 } else
550                         wm8350->reg_cache[i] = wm8350_reg_map[i];
551         }
552
553 out:
554         return ret;
555 }
556 EXPORT_SYMBOL_GPL(wm8350_create_cache);
557
558 static void wm8350_irq_call_worker(struct wm8350 *wm8350, int irq)
559 {
560         mutex_lock(&wm8350->work_mutex);
561
562         if (wm8350->irq[irq].handler)
563                 wm8350->irq[irq].handler(wm8350, irq, wm8350->irq[irq].data);
564         else {
565                 mutex_unlock(&wm8350->work_mutex);
566                 printk(KERN_ERR "wm8350: irq %d nobody cared. now masked.\n",
567                        irq);
568                 wm8350_mask_irq(wm8350, irq);
569                 return;
570         }
571         mutex_unlock(&wm8350->work_mutex);
572 }
573
574 void wm8350_irq_worker(struct work_struct *work)
575 {
576         u16 level_one, status1, status2, comp, oc, gpio, uv;
577         struct wm8350 *wm8350 = container_of(work, struct wm8350, work);
578
579         /* read this in 1 block read */
580         /* read 1st level irq sources and then read required 2nd sources */
581         level_one = wm8350_reg_read(wm8350, WM8350_SYSTEM_INTERRUPTS)
582             & ~wm8350_reg_read(wm8350, WM8350_SYSTEM_INTERRUPTS_MASK);
583         uv = wm8350_reg_read(wm8350, WM8350_UNDER_VOLTAGE_INT_STATUS)
584             & ~wm8350_reg_read(wm8350, WM8350_UNDER_VOLTAGE_INT_STATUS_MASK);
585         oc = wm8350_reg_read(wm8350, WM8350_OVER_CURRENT_INT_STATUS)
586             & ~wm8350_reg_read(wm8350, WM8350_OVER_CURRENT_INT_STATUS_MASK);
587         status1 = wm8350_reg_read(wm8350, WM8350_INT_STATUS_1)
588             & ~wm8350_reg_read(wm8350, WM8350_INT_STATUS_1_MASK);
589         status2 = wm8350_reg_read(wm8350, WM8350_INT_STATUS_2)
590             & ~wm8350_reg_read(wm8350, WM8350_INT_STATUS_2_MASK);
591         comp = wm8350_reg_read(wm8350, WM8350_COMPARATOR_INT_STATUS)
592             & ~wm8350_reg_read(wm8350, WM8350_COMPARATOR_INT_STATUS_MASK);
593         gpio = wm8350_reg_read(wm8350, WM8350_GPIO_INT_STATUS)
594             & ~wm8350_reg_read(wm8350, WM8350_GPIO_INT_STATUS_MASK);
595
596         /* over current */
597         if (level_one & WM8350_OC_INT) {
598                 if (oc & WM8350_OC_LS_EINT)     /* limit switch */
599                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_OC_LS);
600         }
601
602         /* under voltage */
603         if (level_one & WM8350_UV_INT) {
604                 if (uv & WM8350_UV_DC1_EINT)
605                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_DC1);
606                 if (uv & WM8350_UV_DC2_EINT)
607                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_DC2);
608                 if (uv & WM8350_UV_DC3_EINT)
609                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_DC3);
610                 if (uv & WM8350_UV_DC4_EINT)
611                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_DC4);
612                 if (uv & WM8350_UV_DC5_EINT)
613                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_DC5);
614                 if (uv & WM8350_UV_DC6_EINT)
615                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_DC6);
616                 if (uv & WM8350_UV_LDO1_EINT)
617                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_LDO1);
618                 if (uv & WM8350_UV_LDO2_EINT)
619                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_LDO2);
620                 if (uv & WM8350_UV_LDO3_EINT)
621                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_LDO3);
622                 if (uv & WM8350_UV_LDO4_EINT)
623                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_UV_LDO4);
624         }
625
626         /* charger, RTC */
627         if (status1) {
628                 if (status1 & WM8350_CHG_BAT_HOT_EINT)
629                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CHG_BAT_HOT);
630                 if (status1 & WM8350_CHG_BAT_COLD_EINT)
631                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CHG_BAT_COLD);
632                 if (status1 & WM8350_CHG_BAT_FAIL_EINT)
633                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CHG_BAT_FAIL);
634                 if (status1 & WM8350_CHG_TO_EINT)
635                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CHG_TO);
636                 if (status1 & WM8350_CHG_END_EINT)
637                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CHG_END);
638                 if (status1 & WM8350_CHG_START_EINT)
639                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CHG_START);
640                 if (status1 & WM8350_CHG_FAST_RDY_EINT)
641                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CHG_FAST_RDY);
642                 if (status1 & WM8350_CHG_VBATT_LT_3P9_EINT)
643                         wm8350_irq_call_worker(wm8350,
644                                                WM8350_IRQ_CHG_VBATT_LT_3P9);
645                 if (status1 & WM8350_CHG_VBATT_LT_3P1_EINT)
646                         wm8350_irq_call_worker(wm8350,
647                                                WM8350_IRQ_CHG_VBATT_LT_3P1);
648                 if (status1 & WM8350_CHG_VBATT_LT_2P85_EINT)
649                         wm8350_irq_call_worker(wm8350,
650                                                WM8350_IRQ_CHG_VBATT_LT_2P85);
651                 if (status1 & WM8350_RTC_ALM_EINT)
652                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_RTC_ALM);
653                 if (status1 & WM8350_RTC_SEC_EINT)
654                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_RTC_SEC);
655                 if (status1 & WM8350_RTC_PER_EINT)
656                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_RTC_PER);
657         }
658
659         /* current sink, system, aux adc */
660         if (status2) {
661                 if (status2 & WM8350_CS1_EINT)
662                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CS1);
663                 if (status2 & WM8350_CS2_EINT)
664                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CS2);
665
666                 if (status2 & WM8350_SYS_HYST_COMP_FAIL_EINT)
667                         wm8350_irq_call_worker(wm8350,
668                                                WM8350_IRQ_SYS_HYST_COMP_FAIL);
669                 if (status2 & WM8350_SYS_CHIP_GT115_EINT)
670                         wm8350_irq_call_worker(wm8350,
671                                                WM8350_IRQ_SYS_CHIP_GT115);
672                 if (status2 & WM8350_SYS_CHIP_GT140_EINT)
673                         wm8350_irq_call_worker(wm8350,
674                                                WM8350_IRQ_SYS_CHIP_GT140);
675                 if (status2 & WM8350_SYS_WDOG_TO_EINT)
676                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_SYS_WDOG_TO);
677
678                 if (status2 & WM8350_AUXADC_DATARDY_EINT)
679                         wm8350_irq_call_worker(wm8350,
680                                                WM8350_IRQ_AUXADC_DATARDY);
681                 if (status2 & WM8350_AUXADC_DCOMP4_EINT)
682                         wm8350_irq_call_worker(wm8350,
683                                                WM8350_IRQ_AUXADC_DCOMP4);
684                 if (status2 & WM8350_AUXADC_DCOMP3_EINT)
685                         wm8350_irq_call_worker(wm8350,
686                                                WM8350_IRQ_AUXADC_DCOMP3);
687                 if (status2 & WM8350_AUXADC_DCOMP2_EINT)
688                         wm8350_irq_call_worker(wm8350,
689                                                WM8350_IRQ_AUXADC_DCOMP2);
690                 if (status2 & WM8350_AUXADC_DCOMP1_EINT)
691                         wm8350_irq_call_worker(wm8350,
692                                                WM8350_IRQ_AUXADC_DCOMP1);
693
694                 if (status2 & WM8350_USB_LIMIT_EINT)
695                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_USB_LIMIT);
696         }
697
698         /* wake, codec, ext */
699         if (comp) {
700                 if (comp & WM8350_WKUP_OFF_STATE_EINT)
701                         wm8350_irq_call_worker(wm8350,
702                                                WM8350_IRQ_WKUP_OFF_STATE);
703                 if (comp & WM8350_WKUP_HIB_STATE_EINT)
704                         wm8350_irq_call_worker(wm8350,
705                                                WM8350_IRQ_WKUP_HIB_STATE);
706                 if (comp & WM8350_WKUP_CONV_FAULT_EINT)
707                         wm8350_irq_call_worker(wm8350,
708                                                WM8350_IRQ_WKUP_CONV_FAULT);
709                 if (comp & WM8350_WKUP_WDOG_RST_EINT)
710                         wm8350_irq_call_worker(wm8350,
711                                                WM8350_IRQ_WKUP_WDOG_RST);
712                 if (comp & WM8350_WKUP_GP_PWR_ON_EINT)
713                         wm8350_irq_call_worker(wm8350,
714                                                WM8350_IRQ_WKUP_GP_PWR_ON);
715                 if (comp & WM8350_WKUP_ONKEY_EINT)
716                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_WKUP_ONKEY);
717                 if (comp & WM8350_WKUP_GP_WAKEUP_EINT)
718                         wm8350_irq_call_worker(wm8350,
719                                                WM8350_IRQ_WKUP_GP_WAKEUP);
720
721                 if (comp & WM8350_CODEC_JCK_DET_L_EINT)
722                         wm8350_irq_call_worker(wm8350,
723                                                WM8350_IRQ_CODEC_JCK_DET_L);
724                 if (comp & WM8350_CODEC_JCK_DET_R_EINT)
725                         wm8350_irq_call_worker(wm8350,
726                                                WM8350_IRQ_CODEC_JCK_DET_R);
727                 if (comp & WM8350_CODEC_MICSCD_EINT)
728                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CODEC_MICSCD);
729                 if (comp & WM8350_CODEC_MICD_EINT)
730                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_CODEC_MICD);
731
732                 if (comp & WM8350_EXT_USB_FB_EINT)
733                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_EXT_USB_FB);
734                 if (comp & WM8350_EXT_WALL_FB_EINT)
735                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_EXT_WALL_FB);
736                 if (comp & WM8350_EXT_BAT_FB_EINT)
737                         wm8350_irq_call_worker(wm8350, WM8350_IRQ_EXT_BAT_FB);
738         }
739
740         if (level_one & WM8350_GP_INT) {
741                 int i;
742
743                 for (i = 0; i < 12; i++) {
744                         if (gpio & (1 << i))
745                                 wm8350_irq_call_worker(wm8350,
746                                                        WM8350_IRQ_GPIO(i));
747                 }
748         }
749 }
750 EXPORT_SYMBOL_GPL(wm8350_irq_worker);
751
752 int wm8350_register_irq(struct wm8350 *wm8350, int irq,
753                         void (*handler) (struct wm8350 *, int, void *),
754                         void *data)
755 {
756         if (irq < 0 || irq > WM8350_NUM_IRQ || !handler)
757                 return -EINVAL;
758
759         if (wm8350->irq[irq].handler)
760                 return -EBUSY;
761
762         mutex_lock(&wm8350->work_mutex);
763         wm8350->irq[irq].handler = handler;
764         wm8350->irq[irq].data = data;
765         mutex_unlock(&wm8350->work_mutex);
766         return 0;
767 }
768 EXPORT_SYMBOL_GPL(wm8350_register_irq);
769
770 int wm8350_free_irq(struct wm8350 *wm8350, int irq)
771 {
772         if (irq < 0 || irq > WM8350_NUM_IRQ)
773                 return -EINVAL;
774
775         mutex_lock(&wm8350->work_mutex);
776         wm8350->irq[irq].handler = NULL;
777         mutex_unlock(&wm8350->work_mutex);
778         return 0;
779 }
780 EXPORT_SYMBOL_GPL(wm8350_free_irq);
781
782 int wm8350_mask_irq(struct wm8350 *wm8350, int irq)
783 {
784         switch (irq) {
785         case WM8350_IRQ_CHG_BAT_HOT:
786                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
787                                        WM8350_IM_CHG_BAT_HOT_EINT);
788         case WM8350_IRQ_CHG_BAT_COLD:
789                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
790                                        WM8350_IM_CHG_BAT_COLD_EINT);
791         case WM8350_IRQ_CHG_BAT_FAIL:
792                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
793                                        WM8350_IM_CHG_BAT_FAIL_EINT);
794         case WM8350_IRQ_CHG_TO:
795                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
796                                        WM8350_IM_CHG_TO_EINT);
797         case WM8350_IRQ_CHG_END:
798                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
799                                        WM8350_IM_CHG_END_EINT);
800         case WM8350_IRQ_CHG_START:
801                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
802                                        WM8350_IM_CHG_START_EINT);
803         case WM8350_IRQ_CHG_FAST_RDY:
804                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
805                                        WM8350_IM_CHG_FAST_RDY_EINT);
806         case WM8350_IRQ_RTC_PER:
807                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
808                                        WM8350_IM_RTC_PER_EINT);
809         case WM8350_IRQ_RTC_SEC:
810                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
811                                        WM8350_IM_RTC_SEC_EINT);
812         case WM8350_IRQ_RTC_ALM:
813                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
814                                        WM8350_IM_RTC_ALM_EINT);
815         case WM8350_IRQ_CHG_VBATT_LT_3P9:
816                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
817                                        WM8350_IM_CHG_VBATT_LT_3P9_EINT);
818         case WM8350_IRQ_CHG_VBATT_LT_3P1:
819                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
820                                        WM8350_IM_CHG_VBATT_LT_3P1_EINT);
821         case WM8350_IRQ_CHG_VBATT_LT_2P85:
822                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_1_MASK,
823                                        WM8350_IM_CHG_VBATT_LT_2P85_EINT);
824         case WM8350_IRQ_CS1:
825                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
826                                        WM8350_IM_CS1_EINT);
827         case WM8350_IRQ_CS2:
828                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
829                                        WM8350_IM_CS2_EINT);
830         case WM8350_IRQ_USB_LIMIT:
831                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
832                                        WM8350_IM_USB_LIMIT_EINT);
833         case WM8350_IRQ_AUXADC_DATARDY:
834                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
835                                        WM8350_IM_AUXADC_DATARDY_EINT);
836         case WM8350_IRQ_AUXADC_DCOMP4:
837                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
838                                        WM8350_IM_AUXADC_DCOMP4_EINT);
839         case WM8350_IRQ_AUXADC_DCOMP3:
840                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
841                                        WM8350_IM_AUXADC_DCOMP3_EINT);
842         case WM8350_IRQ_AUXADC_DCOMP2:
843                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
844                                        WM8350_IM_AUXADC_DCOMP2_EINT);
845         case WM8350_IRQ_AUXADC_DCOMP1:
846                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
847                                        WM8350_IM_AUXADC_DCOMP1_EINT);
848         case WM8350_IRQ_SYS_HYST_COMP_FAIL:
849                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
850                                        WM8350_IM_SYS_HYST_COMP_FAIL_EINT);
851         case WM8350_IRQ_SYS_CHIP_GT115:
852                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
853                                        WM8350_IM_SYS_CHIP_GT115_EINT);
854         case WM8350_IRQ_SYS_CHIP_GT140:
855                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
856                                        WM8350_IM_SYS_CHIP_GT140_EINT);
857         case WM8350_IRQ_SYS_WDOG_TO:
858                 return wm8350_set_bits(wm8350, WM8350_INT_STATUS_2_MASK,
859                                        WM8350_IM_SYS_WDOG_TO_EINT);
860         case WM8350_IRQ_UV_LDO4:
861                 return wm8350_set_bits(wm8350,
862                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
863                                        WM8350_IM_UV_LDO4_EINT);
864         case WM8350_IRQ_UV_LDO3:
865                 return wm8350_set_bits(wm8350,
866                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
867                                        WM8350_IM_UV_LDO3_EINT);
868         case WM8350_IRQ_UV_LDO2:
869                 return wm8350_set_bits(wm8350,
870                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
871                                        WM8350_IM_UV_LDO2_EINT);
872         case WM8350_IRQ_UV_LDO1:
873                 return wm8350_set_bits(wm8350,
874                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
875                                        WM8350_IM_UV_LDO1_EINT);
876         case WM8350_IRQ_UV_DC6:
877                 return wm8350_set_bits(wm8350,
878                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
879                                        WM8350_IM_UV_DC6_EINT);
880         case WM8350_IRQ_UV_DC5:
881                 return wm8350_set_bits(wm8350,
882                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
883                                        WM8350_IM_UV_DC5_EINT);
884         case WM8350_IRQ_UV_DC4:
885                 return wm8350_set_bits(wm8350,
886                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
887                                        WM8350_IM_UV_DC4_EINT);
888         case WM8350_IRQ_UV_DC3:
889                 return wm8350_set_bits(wm8350,
890                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
891                                        WM8350_IM_UV_DC3_EINT);
892         case WM8350_IRQ_UV_DC2:
893                 return wm8350_set_bits(wm8350,
894                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
895                                        WM8350_IM_UV_DC2_EINT);
896         case WM8350_IRQ_UV_DC1:
897                 return wm8350_set_bits(wm8350,
898                                        WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
899                                        WM8350_IM_UV_DC1_EINT);
900         case WM8350_IRQ_OC_LS:
901                 return wm8350_set_bits(wm8350,
902                                        WM8350_OVER_CURRENT_INT_STATUS_MASK,
903                                        WM8350_IM_OC_LS_EINT);
904         case WM8350_IRQ_EXT_USB_FB:
905                 return wm8350_set_bits(wm8350,
906                                        WM8350_COMPARATOR_INT_STATUS_MASK,
907                                        WM8350_IM_EXT_USB_FB_EINT);
908         case WM8350_IRQ_EXT_WALL_FB:
909                 return wm8350_set_bits(wm8350,
910                                        WM8350_COMPARATOR_INT_STATUS_MASK,
911                                        WM8350_IM_EXT_WALL_FB_EINT);
912         case WM8350_IRQ_EXT_BAT_FB:
913                 return wm8350_set_bits(wm8350,
914                                        WM8350_COMPARATOR_INT_STATUS_MASK,
915                                        WM8350_IM_EXT_BAT_FB_EINT);
916         case WM8350_IRQ_CODEC_JCK_DET_L:
917                 return wm8350_set_bits(wm8350,
918                                        WM8350_COMPARATOR_INT_STATUS_MASK,
919                                        WM8350_IM_CODEC_JCK_DET_L_EINT);
920         case WM8350_IRQ_CODEC_JCK_DET_R:
921                 return wm8350_set_bits(wm8350,
922                                        WM8350_COMPARATOR_INT_STATUS_MASK,
923                                        WM8350_IM_CODEC_JCK_DET_R_EINT);
924         case WM8350_IRQ_CODEC_MICSCD:
925                 return wm8350_set_bits(wm8350,
926                                        WM8350_COMPARATOR_INT_STATUS_MASK,
927                                        WM8350_IM_CODEC_MICSCD_EINT);
928         case WM8350_IRQ_CODEC_MICD:
929                 return wm8350_set_bits(wm8350,
930                                        WM8350_COMPARATOR_INT_STATUS_MASK,
931                                        WM8350_IM_CODEC_MICD_EINT);
932         case WM8350_IRQ_WKUP_OFF_STATE:
933                 return wm8350_set_bits(wm8350,
934                                        WM8350_COMPARATOR_INT_STATUS_MASK,
935                                        WM8350_IM_WKUP_OFF_STATE_EINT);
936         case WM8350_IRQ_WKUP_HIB_STATE:
937                 return wm8350_set_bits(wm8350,
938                                        WM8350_COMPARATOR_INT_STATUS_MASK,
939                                        WM8350_IM_WKUP_HIB_STATE_EINT);
940         case WM8350_IRQ_WKUP_CONV_FAULT:
941                 return wm8350_set_bits(wm8350,
942                                        WM8350_COMPARATOR_INT_STATUS_MASK,
943                                        WM8350_IM_WKUP_CONV_FAULT_EINT);
944         case WM8350_IRQ_WKUP_WDOG_RST:
945                 return wm8350_set_bits(wm8350,
946                                        WM8350_COMPARATOR_INT_STATUS_MASK,
947                                        WM8350_IM_WKUP_OFF_STATE_EINT);
948         case WM8350_IRQ_WKUP_GP_PWR_ON:
949                 return wm8350_set_bits(wm8350,
950                                        WM8350_COMPARATOR_INT_STATUS_MASK,
951                                        WM8350_IM_WKUP_GP_PWR_ON_EINT);
952         case WM8350_IRQ_WKUP_ONKEY:
953                 return wm8350_set_bits(wm8350,
954                                        WM8350_COMPARATOR_INT_STATUS_MASK,
955                                        WM8350_IM_WKUP_ONKEY_EINT);
956         case WM8350_IRQ_WKUP_GP_WAKEUP:
957                 return wm8350_set_bits(wm8350,
958                                        WM8350_COMPARATOR_INT_STATUS_MASK,
959                                        WM8350_IM_WKUP_GP_WAKEUP_EINT);
960         case WM8350_IRQ_GPIO(0):
961                 return wm8350_set_bits(wm8350,
962                                        WM8350_GPIO_INT_STATUS_MASK,
963                                        WM8350_IM_GP0_EINT);
964         case WM8350_IRQ_GPIO(1):
965                 return wm8350_set_bits(wm8350,
966                                        WM8350_GPIO_INT_STATUS_MASK,
967                                        WM8350_IM_GP1_EINT);
968         case WM8350_IRQ_GPIO(2):
969                 return wm8350_set_bits(wm8350,
970                                        WM8350_GPIO_INT_STATUS_MASK,
971                                        WM8350_IM_GP2_EINT);
972         case WM8350_IRQ_GPIO(3):
973                 return wm8350_set_bits(wm8350,
974                                        WM8350_GPIO_INT_STATUS_MASK,
975                                        WM8350_IM_GP3_EINT);
976         case WM8350_IRQ_GPIO(4):
977                 return wm8350_set_bits(wm8350,
978                                        WM8350_GPIO_INT_STATUS_MASK,
979                                        WM8350_IM_GP4_EINT);
980         case WM8350_IRQ_GPIO(5):
981                 return wm8350_set_bits(wm8350,
982                                        WM8350_GPIO_INT_STATUS_MASK,
983                                        WM8350_IM_GP5_EINT);
984         case WM8350_IRQ_GPIO(6):
985                 return wm8350_set_bits(wm8350,
986                                        WM8350_GPIO_INT_STATUS_MASK,
987                                        WM8350_IM_GP6_EINT);
988         case WM8350_IRQ_GPIO(7):
989                 return wm8350_set_bits(wm8350,
990                                        WM8350_GPIO_INT_STATUS_MASK,
991                                        WM8350_IM_GP7_EINT);
992         case WM8350_IRQ_GPIO(8):
993                 return wm8350_set_bits(wm8350,
994                                        WM8350_GPIO_INT_STATUS_MASK,
995                                        WM8350_IM_GP8_EINT);
996         case WM8350_IRQ_GPIO(9):
997                 return wm8350_set_bits(wm8350,
998                                        WM8350_GPIO_INT_STATUS_MASK,
999                                        WM8350_IM_GP9_EINT);
1000         case WM8350_IRQ_GPIO(10):
1001                 return wm8350_set_bits(wm8350,
1002                                        WM8350_GPIO_INT_STATUS_MASK,
1003                                        WM8350_IM_GP10_EINT);
1004         case WM8350_IRQ_GPIO(11):
1005                 return wm8350_set_bits(wm8350,
1006                                        WM8350_GPIO_INT_STATUS_MASK,
1007                                        WM8350_IM_GP11_EINT);
1008         case WM8350_IRQ_GPIO(12):
1009                 return wm8350_set_bits(wm8350,
1010                                        WM8350_GPIO_INT_STATUS_MASK,
1011                                        WM8350_IM_GP12_EINT);
1012         default:
1013                 return -EINVAL;
1014         }
1015         return 0;
1016 }
1017 EXPORT_SYMBOL_GPL(wm8350_mask_irq);
1018
1019 int wm8350_unmask_irq(struct wm8350 *wm8350, int irq)
1020 {
1021         switch (irq) {
1022         case WM8350_IRQ_CHG_BAT_HOT:
1023                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1024                                          WM8350_IM_CHG_BAT_HOT_EINT);
1025         case WM8350_IRQ_CHG_BAT_COLD:
1026                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1027                                          WM8350_IM_CHG_BAT_COLD_EINT);
1028         case WM8350_IRQ_CHG_BAT_FAIL:
1029                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1030                                          WM8350_IM_CHG_BAT_FAIL_EINT);
1031         case WM8350_IRQ_CHG_TO:
1032                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1033                                          WM8350_IM_CHG_TO_EINT);
1034         case WM8350_IRQ_CHG_END:
1035                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1036                                          WM8350_IM_CHG_END_EINT);
1037         case WM8350_IRQ_CHG_START:
1038                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1039                                          WM8350_IM_CHG_START_EINT);
1040         case WM8350_IRQ_CHG_FAST_RDY:
1041                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1042                                          WM8350_IM_CHG_FAST_RDY_EINT);
1043         case WM8350_IRQ_RTC_PER:
1044                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1045                                          WM8350_IM_RTC_PER_EINT);
1046         case WM8350_IRQ_RTC_SEC:
1047                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1048                                          WM8350_IM_RTC_SEC_EINT);
1049         case WM8350_IRQ_RTC_ALM:
1050                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1051                                          WM8350_IM_RTC_ALM_EINT);
1052         case WM8350_IRQ_CHG_VBATT_LT_3P9:
1053                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1054                                          WM8350_IM_CHG_VBATT_LT_3P9_EINT);
1055         case WM8350_IRQ_CHG_VBATT_LT_3P1:
1056                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1057                                          WM8350_IM_CHG_VBATT_LT_3P1_EINT);
1058         case WM8350_IRQ_CHG_VBATT_LT_2P85:
1059                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_1_MASK,
1060                                          WM8350_IM_CHG_VBATT_LT_2P85_EINT);
1061         case WM8350_IRQ_CS1:
1062                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1063                                          WM8350_IM_CS1_EINT);
1064         case WM8350_IRQ_CS2:
1065                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1066                                          WM8350_IM_CS2_EINT);
1067         case WM8350_IRQ_USB_LIMIT:
1068                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1069                                          WM8350_IM_USB_LIMIT_EINT);
1070         case WM8350_IRQ_AUXADC_DATARDY:
1071                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1072                                          WM8350_IM_AUXADC_DATARDY_EINT);
1073         case WM8350_IRQ_AUXADC_DCOMP4:
1074                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1075                                          WM8350_IM_AUXADC_DCOMP4_EINT);
1076         case WM8350_IRQ_AUXADC_DCOMP3:
1077                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1078                                          WM8350_IM_AUXADC_DCOMP3_EINT);
1079         case WM8350_IRQ_AUXADC_DCOMP2:
1080                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1081                                          WM8350_IM_AUXADC_DCOMP2_EINT);
1082         case WM8350_IRQ_AUXADC_DCOMP1:
1083                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1084                                          WM8350_IM_AUXADC_DCOMP1_EINT);
1085         case WM8350_IRQ_SYS_HYST_COMP_FAIL:
1086                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1087                                          WM8350_IM_SYS_HYST_COMP_FAIL_EINT);
1088         case WM8350_IRQ_SYS_CHIP_GT115:
1089                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1090                                          WM8350_IM_SYS_CHIP_GT115_EINT);
1091         case WM8350_IRQ_SYS_CHIP_GT140:
1092                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1093                                          WM8350_IM_SYS_CHIP_GT140_EINT);
1094         case WM8350_IRQ_SYS_WDOG_TO:
1095                 return wm8350_clear_bits(wm8350, WM8350_INT_STATUS_2_MASK,
1096                                          WM8350_IM_SYS_WDOG_TO_EINT);
1097         case WM8350_IRQ_UV_LDO4:
1098                 return wm8350_clear_bits(wm8350,
1099                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1100                                          WM8350_IM_UV_LDO4_EINT);
1101         case WM8350_IRQ_UV_LDO3:
1102                 return wm8350_clear_bits(wm8350,
1103                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1104                                          WM8350_IM_UV_LDO3_EINT);
1105         case WM8350_IRQ_UV_LDO2:
1106                 return wm8350_clear_bits(wm8350,
1107                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1108                                          WM8350_IM_UV_LDO2_EINT);
1109         case WM8350_IRQ_UV_LDO1:
1110                 return wm8350_clear_bits(wm8350,
1111                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1112                                          WM8350_IM_UV_LDO1_EINT);
1113         case WM8350_IRQ_UV_DC6:
1114                 return wm8350_clear_bits(wm8350,
1115                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1116                                          WM8350_IM_UV_DC6_EINT);
1117         case WM8350_IRQ_UV_DC5:
1118                 return wm8350_clear_bits(wm8350,
1119                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1120                                          WM8350_IM_UV_DC5_EINT);
1121         case WM8350_IRQ_UV_DC4:
1122                 return wm8350_clear_bits(wm8350,
1123                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1124                                          WM8350_IM_UV_DC4_EINT);
1125         case WM8350_IRQ_UV_DC3:
1126                 return wm8350_clear_bits(wm8350,
1127                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1128                                          WM8350_IM_UV_DC3_EINT);
1129         case WM8350_IRQ_UV_DC2:
1130                 return wm8350_clear_bits(wm8350,
1131                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1132                                          WM8350_IM_UV_DC2_EINT);
1133         case WM8350_IRQ_UV_DC1:
1134                 return wm8350_clear_bits(wm8350,
1135                                          WM8350_UNDER_VOLTAGE_INT_STATUS_MASK,
1136                                          WM8350_IM_UV_DC1_EINT);
1137         case WM8350_IRQ_OC_LS:
1138                 return wm8350_clear_bits(wm8350,
1139                                          WM8350_OVER_CURRENT_INT_STATUS_MASK,
1140                                          WM8350_IM_OC_LS_EINT);
1141         case WM8350_IRQ_EXT_USB_FB:
1142                 return wm8350_clear_bits(wm8350,
1143                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1144                                          WM8350_IM_EXT_USB_FB_EINT);
1145         case WM8350_IRQ_EXT_WALL_FB:
1146                 return wm8350_clear_bits(wm8350,
1147                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1148                                          WM8350_IM_EXT_WALL_FB_EINT);
1149         case WM8350_IRQ_EXT_BAT_FB:
1150                 return wm8350_clear_bits(wm8350,
1151                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1152                                          WM8350_IM_EXT_BAT_FB_EINT);
1153         case WM8350_IRQ_CODEC_JCK_DET_L:
1154                 return wm8350_clear_bits(wm8350,
1155                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1156                                          WM8350_IM_CODEC_JCK_DET_L_EINT);
1157         case WM8350_IRQ_CODEC_JCK_DET_R:
1158                 return wm8350_clear_bits(wm8350,
1159                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1160                                          WM8350_IM_CODEC_JCK_DET_R_EINT);
1161         case WM8350_IRQ_CODEC_MICSCD:
1162                 return wm8350_clear_bits(wm8350,
1163                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1164                                          WM8350_IM_CODEC_MICSCD_EINT);
1165         case WM8350_IRQ_CODEC_MICD:
1166                 return wm8350_clear_bits(wm8350,
1167                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1168                                          WM8350_IM_CODEC_MICD_EINT);
1169         case WM8350_IRQ_WKUP_OFF_STATE:
1170                 return wm8350_clear_bits(wm8350,
1171                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1172                                          WM8350_IM_WKUP_OFF_STATE_EINT);
1173         case WM8350_IRQ_WKUP_HIB_STATE:
1174                 return wm8350_clear_bits(wm8350,
1175                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1176                                          WM8350_IM_WKUP_HIB_STATE_EINT);
1177         case WM8350_IRQ_WKUP_CONV_FAULT:
1178                 return wm8350_clear_bits(wm8350,
1179                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1180                                          WM8350_IM_WKUP_CONV_FAULT_EINT);
1181         case WM8350_IRQ_WKUP_WDOG_RST:
1182                 return wm8350_clear_bits(wm8350,
1183                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1184                                          WM8350_IM_WKUP_OFF_STATE_EINT);
1185         case WM8350_IRQ_WKUP_GP_PWR_ON:
1186                 return wm8350_clear_bits(wm8350,
1187                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1188                                          WM8350_IM_WKUP_GP_PWR_ON_EINT);
1189         case WM8350_IRQ_WKUP_ONKEY:
1190                 return wm8350_clear_bits(wm8350,
1191                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1192                                          WM8350_IM_WKUP_ONKEY_EINT);
1193         case WM8350_IRQ_WKUP_GP_WAKEUP:
1194                 return wm8350_clear_bits(wm8350,
1195                                          WM8350_COMPARATOR_INT_STATUS_MASK,
1196                                          WM8350_IM_WKUP_GP_WAKEUP_EINT);
1197         case WM8350_IRQ_GPIO(0):
1198                 return wm8350_clear_bits(wm8350,
1199                                          WM8350_GPIO_INT_STATUS_MASK,
1200                                          WM8350_IM_GP0_EINT);
1201         case WM8350_IRQ_GPIO(1):
1202                 return wm8350_clear_bits(wm8350,
1203                                          WM8350_GPIO_INT_STATUS_MASK,
1204                                          WM8350_IM_GP1_EINT);
1205         case WM8350_IRQ_GPIO(2):
1206                 return wm8350_clear_bits(wm8350,
1207                                          WM8350_GPIO_INT_STATUS_MASK,
1208                                          WM8350_IM_GP2_EINT);
1209         case WM8350_IRQ_GPIO(3):
1210                 return wm8350_clear_bits(wm8350,
1211                                          WM8350_GPIO_INT_STATUS_MASK,
1212                                          WM8350_IM_GP3_EINT);
1213         case WM8350_IRQ_GPIO(4):
1214                 return wm8350_clear_bits(wm8350,
1215                                          WM8350_GPIO_INT_STATUS_MASK,
1216                                          WM8350_IM_GP4_EINT);
1217         case WM8350_IRQ_GPIO(5):
1218                 return wm8350_clear_bits(wm8350,
1219                                          WM8350_GPIO_INT_STATUS_MASK,
1220                                          WM8350_IM_GP5_EINT);
1221         case WM8350_IRQ_GPIO(6):
1222                 return wm8350_clear_bits(wm8350,
1223                                          WM8350_GPIO_INT_STATUS_MASK,
1224                                          WM8350_IM_GP6_EINT);
1225         case WM8350_IRQ_GPIO(7):
1226                 return wm8350_clear_bits(wm8350,
1227                                          WM8350_GPIO_INT_STATUS_MASK,
1228                                          WM8350_IM_GP7_EINT);
1229         case WM8350_IRQ_GPIO(8):
1230                 return wm8350_clear_bits(wm8350,
1231                                          WM8350_GPIO_INT_STATUS_MASK,
1232                                          WM8350_IM_GP8_EINT);
1233         case WM8350_IRQ_GPIO(9):
1234                 return wm8350_clear_bits(wm8350,
1235                                          WM8350_GPIO_INT_STATUS_MASK,
1236                                          WM8350_IM_GP9_EINT);
1237         case WM8350_IRQ_GPIO(10):
1238                 return wm8350_clear_bits(wm8350,
1239                                          WM8350_GPIO_INT_STATUS_MASK,
1240                                          WM8350_IM_GP10_EINT);
1241         case WM8350_IRQ_GPIO(11):
1242                 return wm8350_clear_bits(wm8350,
1243                                          WM8350_GPIO_INT_STATUS_MASK,
1244                                          WM8350_IM_GP11_EINT);
1245         case WM8350_IRQ_GPIO(12):
1246                 return wm8350_clear_bits(wm8350,
1247                                          WM8350_GPIO_INT_STATUS_MASK,
1248                                          WM8350_IM_GP12_EINT);
1249         default:
1250                 return -EINVAL;
1251         }
1252         return 0;
1253 }
1254 EXPORT_SYMBOL_GPL(wm8350_unmask_irq);
1255
1256 static int gpio_set_dir(struct wm8350 *wm8350, int gpio, int dir)
1257 {
1258         int ret;
1259
1260         wm8350_reg_unlock(wm8350);
1261         if (dir == WM8350_GPIO_DIR_OUT)
1262                 ret = wm8350_clear_bits(wm8350,
1263                                         WM8350_GPIO_CONFIGURATION_I_O,
1264                                         1 << gpio);
1265         else
1266                 ret = wm8350_set_bits(wm8350,
1267                                       WM8350_GPIO_CONFIGURATION_I_O, 1 << gpio);
1268         wm8350_reg_lock(wm8350);
1269         return ret;
1270 }
1271
1272 static int gpio_set_debounce(struct wm8350 *wm8350, int gpio, int db)
1273 {
1274         if (db == WM8350_GPIO_DEBOUNCE_ON)
1275                 return wm8350_set_bits(wm8350, WM8350_GPIO_DEBOUNCE, 1 << gpio);
1276         else
1277                 return wm8350_clear_bits(wm8350,
1278                                          WM8350_GPIO_DEBOUNCE, 1 << gpio);
1279 }
1280
1281 static int gpio_set_func(struct wm8350 *wm8350, int gpio, int func)
1282 {
1283         u16 reg;
1284
1285         wm8350_reg_unlock(wm8350);
1286         switch (gpio) {
1287         case 0:
1288                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_1)
1289                     & ~WM8350_GP0_FN_MASK;
1290                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_1,
1291                                  reg | ((func & 0xf) << 0));
1292                 break;
1293         case 1:
1294                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_1)
1295                     & ~WM8350_GP1_FN_MASK;
1296                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_1,
1297                                  reg | ((func & 0xf) << 4));
1298                 break;
1299         case 2:
1300                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_1)
1301                     & ~WM8350_GP2_FN_MASK;
1302                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_1,
1303                                  reg | ((func & 0xf) << 8));
1304                 break;
1305         case 3:
1306                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_1)
1307                     & ~WM8350_GP3_FN_MASK;
1308                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_1,
1309                                  reg | ((func & 0xf) << 12));
1310                 break;
1311         case 4:
1312                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_2)
1313                     & ~WM8350_GP4_FN_MASK;
1314                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_2,
1315                                  reg | ((func & 0xf) << 0));
1316                 break;
1317         case 5:
1318                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_2)
1319                     & ~WM8350_GP5_FN_MASK;
1320                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_2,
1321                                  reg | ((func & 0xf) << 4));
1322                 break;
1323         case 6:
1324                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_2)
1325                     & ~WM8350_GP6_FN_MASK;
1326                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_2,
1327                                  reg | ((func & 0xf) << 8));
1328                 break;
1329         case 7:
1330                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_2)
1331                     & ~WM8350_GP7_FN_MASK;
1332                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_2,
1333                                  reg | ((func & 0xf) << 12));
1334                 break;
1335         case 8:
1336                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_3)
1337                     & ~WM8350_GP8_FN_MASK;
1338                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_3,
1339                                  reg | ((func & 0xf) << 0));
1340                 break;
1341         case 9:
1342                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_3)
1343                     & ~WM8350_GP9_FN_MASK;
1344                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_3,
1345                                  reg | ((func & 0xf) << 4));
1346                 break;
1347         case 10:
1348                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_3)
1349                     & ~WM8350_GP10_FN_MASK;
1350                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_3,
1351                                  reg | ((func & 0xf) << 8));
1352                 break;
1353         case 11:
1354                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_3)
1355                     & ~WM8350_GP11_FN_MASK;
1356                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_3,
1357                                  reg | ((func & 0xf) << 12));
1358                 break;
1359         case 12:
1360                 reg = wm8350_reg_read(wm8350, WM8350_GPIO_FUNCTION_SELECT_4)
1361                     & ~WM8350_GP12_FN_MASK;
1362                 wm8350_reg_write(wm8350, WM8350_GPIO_FUNCTION_SELECT_4,
1363                                  reg | ((func & 0xf) << 0));
1364                 break;
1365         default:
1366                 wm8350_reg_lock(wm8350);
1367                 return -EINVAL;
1368         }
1369
1370         wm8350_reg_lock(wm8350);
1371         return 0;
1372 }
1373
1374 int wm8350_gpio_set_status(struct wm8350 *wm8350, int gpio, int status)
1375 {
1376         if (status)
1377                 return wm8350_set_bits(wm8350,
1378                                        WM8350_GPIO_PIN_STATUS, 1 << gpio);
1379         else
1380                 return wm8350_clear_bits(wm8350,
1381                                          WM8350_GPIO_PIN_STATUS, 1 << gpio);
1382 }
1383 EXPORT_SYMBOL_GPL(wm8350_gpio_set_status);
1384
1385 int wm8350_gpio_get_status(struct wm8350 *wm8350, int gpio)
1386 {
1387         return (wm8350_reg_read(wm8350, WM8350_GPIO_PIN_STATUS) &
1388                 (1 << gpio)) ? 1 : 0;
1389 }
1390 EXPORT_SYMBOL_GPL(wm8350_gpio_get_status);
1391
1392 static int gpio_set_pull_up(struct wm8350 *wm8350, int gpio, int up)
1393 {
1394         if (up)
1395                 return wm8350_set_bits(wm8350,
1396                                        WM8350_GPIO_PIN_PULL_UP_CONTROL,
1397                                        1 << gpio);
1398         else
1399                 return wm8350_clear_bits(wm8350,
1400                                          WM8350_GPIO_PIN_PULL_UP_CONTROL,
1401                                          1 << gpio);
1402 }
1403
1404 static int gpio_set_pull_down(struct wm8350 *wm8350, int gpio, int down)
1405 {
1406         if (down)
1407                 return wm8350_set_bits(wm8350,
1408                                        WM8350_GPIO_PULL_DOWN_CONTROL,
1409                                        1 << gpio);
1410         else
1411                 return wm8350_clear_bits(wm8350,
1412                                          WM8350_GPIO_PULL_DOWN_CONTROL,
1413                                          1 << gpio);
1414 }
1415
1416 static int gpio_set_polarity(struct wm8350 *wm8350, int gpio, int pol)
1417 {
1418         if (pol == WM8350_GPIO_ACTIVE_HIGH)
1419                 return wm8350_set_bits(wm8350,
1420                                        WM8350_GPIO_PIN_POLARITY_TYPE,
1421                                        1 << gpio);
1422         else
1423                 return wm8350_clear_bits(wm8350,
1424                                          WM8350_GPIO_PIN_POLARITY_TYPE,
1425                                          1 << gpio);
1426 }
1427
1428 static int gpio_set_invert(struct wm8350 *wm8350, int gpio, int invert)
1429 {
1430         if (invert == WM8350_GPIO_INVERT_ON)
1431                 return wm8350_set_bits(wm8350, WM8350_GPIO_INT_MODE, 1 << gpio);
1432         else
1433                 return wm8350_clear_bits(wm8350,
1434                                          WM8350_GPIO_INT_MODE, 1 << gpio);
1435 }
1436
1437 int wm8350_gpio_config(struct wm8350 *wm8350, int gpio, int dir, int func,
1438                        int pol, int pull, int invert, int debounce)
1439 {
1440         /* make sure we never pull up and down at the same time */
1441         if (pull == WM8350_GPIO_PULL_NONE) {
1442                 if (gpio_set_pull_up(wm8350, gpio, 0))
1443                         goto err;
1444                 if (gpio_set_pull_down(wm8350, gpio, 0))
1445                         goto err;
1446         } else if (pull == WM8350_GPIO_PULL_UP) {
1447                 if (gpio_set_pull_down(wm8350, gpio, 0))
1448                         goto err;
1449                 if (gpio_set_pull_up(wm8350, gpio, 1))
1450                         goto err;
1451         } else if (pull == WM8350_GPIO_PULL_DOWN) {
1452                 if (gpio_set_pull_up(wm8350, gpio, 0))
1453                         goto err;
1454                 if (gpio_set_pull_down(wm8350, gpio, 1))
1455                         goto err;
1456         }
1457
1458         if (gpio_set_invert(wm8350, gpio, invert))
1459                 goto err;
1460         if (gpio_set_polarity(wm8350, gpio, pol))
1461                 goto err;
1462         if (gpio_set_debounce(wm8350, gpio, debounce))
1463                 goto err;
1464         if (gpio_set_dir(wm8350, gpio, dir))
1465                 goto err;
1466         return gpio_set_func(wm8350, gpio, func);
1467
1468 err:
1469         return -EIO;
1470 }
1471 EXPORT_SYMBOL_GPL(wm8350_gpio_config);
1472
1473 int wm8350_read_auxadc(struct wm8350 *wm8350, int channel, int scale, int vref)
1474 {
1475         u16 reg, result = 0;
1476         int tries = 5;
1477
1478         if (channel < WM8350_AUXADC_AUX1 || channel > WM8350_AUXADC_TEMP)
1479                 return -EINVAL;
1480         if (channel >= WM8350_AUXADC_USB && channel <= WM8350_AUXADC_TEMP
1481             && (scale != 0 || vref != 0))
1482                 return -EINVAL;
1483
1484         mutex_lock(&auxadc_mutex);
1485
1486         /* Turn on the ADC */
1487         reg = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
1488         wm8350_reg_write(wm8350, WM8350_POWER_MGMT_5, reg | WM8350_AUXADC_ENA);
1489
1490         if (scale || vref) {
1491                 reg = scale << 13;
1492                 reg |= vref << 12;
1493                 wm8350_reg_write(wm8350, WM8350_AUX1_READBACK + channel, reg);
1494         }
1495
1496         reg = wm8350_reg_read(wm8350, WM8350_DIGITISER_CONTROL_1);
1497         reg |= 1 << channel | WM8350_AUXADC_POLL;
1498         wm8350_reg_write(wm8350, WM8350_DIGITISER_CONTROL_1, reg);
1499
1500         do {
1501                 schedule_timeout_interruptible(1);
1502                 reg = wm8350_reg_read(wm8350, WM8350_DIGITISER_CONTROL_1);
1503         } while (tries-- && (reg & WM8350_AUXADC_POLL));
1504
1505         if (!tries)
1506                 printk(KERN_ERR "wm8350: adc chn %d read timeout\n", channel);
1507         else
1508                 result = wm8350_reg_read(wm8350,
1509                                          WM8350_AUX1_READBACK + channel);
1510
1511         /* Turn off the ADC */
1512         reg = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
1513         wm8350_reg_write(wm8350, WM8350_POWER_MGMT_5, reg & ~WM8350_AUXADC_ENA);
1514
1515         mutex_unlock(&auxadc_mutex);
1516         return result & WM8350_AUXADC_DATA1_MASK;
1517 }
1518 EXPORT_SYMBOL_GPL(wm8350_read_auxadc);
1519
1520 static void wm8350_rtc_dev_release(struct device *dev)
1521 {
1522 }
1523
1524 int wm8350_device_register_rtc(struct wm8350 *wm8350)
1525 {
1526         int ret;
1527
1528         strcpy(wm8350->rtc.dev.bus_id, "wm8350-rtc");
1529         wm8350->rtc.dev.bus = &wm8350_bus_type;
1530         wm8350->rtc.dev.parent = &wm8350->i2c_client->dev;
1531         wm8350->rtc.dev.release = wm8350_rtc_dev_release;
1532
1533         ret = device_register(&wm8350->rtc.dev);
1534         if (ret < 0)
1535                 printk(KERN_ERR "failed to register WM8350 RTC device\n");
1536
1537         return ret;
1538 }
1539 EXPORT_SYMBOL_GPL(wm8350_device_register_rtc);
1540
1541 static void wm8350_wdg_dev_release(struct device *dev)
1542 {
1543 }
1544
1545 int wm8350_device_register_wdg(struct wm8350 *wm8350)
1546 {
1547         int ret;
1548
1549         strcpy(wm8350->wdg.dev.bus_id, "wm8350-wdt");
1550         wm8350->wdg.dev.bus = &wm8350_bus_type;
1551         wm8350->wdg.dev.parent = &wm8350->i2c_client->dev;
1552         wm8350->wdg.dev.release = wm8350_wdg_dev_release;
1553
1554         ret = device_register(&wm8350->wdg.dev);
1555         if (ret < 0)
1556                 printk(KERN_ERR "failed to register WM8350 Watchdog device\n");
1557
1558         return ret;
1559 }
1560 EXPORT_SYMBOL_GPL(wm8350_device_register_wdg);
1561
1562 static void wm8350_power_dev_release(struct device *dev)
1563 {
1564 }
1565
1566 int wm8350_device_register_power(struct wm8350 *wm8350)
1567 {
1568         int ret;
1569
1570         strcpy(wm8350->power.dev.bus_id, "wm8350-power");
1571         wm8350->power.dev.bus = &wm8350_bus_type;
1572         wm8350->power.dev.parent = &wm8350->i2c_client->dev;
1573         wm8350->power.dev.release = wm8350_power_dev_release;
1574
1575         ret = device_register(&wm8350->power.dev);
1576         if (ret < 0)
1577                 printk(KERN_ERR "failed to register WM8350 Power device\n");
1578
1579         return ret;
1580 }
1581 EXPORT_SYMBOL_GPL(wm8350_device_register_power);