11d9f9f04SMark Brown /* 21d9f9f04SMark Brown * Core driver for WM8400. 31d9f9f04SMark Brown * 41d9f9f04SMark Brown * Copyright 2008 Wolfson Microelectronics PLC. 51d9f9f04SMark Brown * 61d9f9f04SMark Brown * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 71d9f9f04SMark Brown * 81d9f9f04SMark Brown * This program is free software; you can redistribute it and/or 91d9f9f04SMark Brown * modify it under the terms of the GNU General Public License as 101d9f9f04SMark Brown * published by the Free Software Foundation; either version 2 of the 111d9f9f04SMark Brown * License, or (at your option) any later version. 121d9f9f04SMark Brown * 131d9f9f04SMark Brown */ 141d9f9f04SMark Brown 151d9f9f04SMark Brown #include <linux/bug.h> 161d9f9f04SMark Brown #include <linux/i2c.h> 171d9f9f04SMark Brown #include <linux/kernel.h> 18b8380c1aSMark Brown #include <linux/mfd/core.h> 191d9f9f04SMark Brown #include <linux/mfd/wm8400-private.h> 201d9f9f04SMark Brown #include <linux/mfd/wm8400-audio.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 221d9f9f04SMark Brown 231d9f9f04SMark Brown static struct { 241d9f9f04SMark Brown u16 readable; /* Mask of readable bits */ 251d9f9f04SMark Brown u16 writable; /* Mask of writable bits */ 261d9f9f04SMark Brown u16 vol; /* Mask of volatile bits */ 271d9f9f04SMark Brown int is_codec; /* Register controlled by codec reset */ 281d9f9f04SMark Brown u16 default_val; /* Value on reset */ 291d9f9f04SMark Brown } reg_data[] = { 301d9f9f04SMark Brown { 0xFFFF, 0xFFFF, 0x0000, 0, 0x6172 }, /* R0 */ 311d9f9f04SMark Brown { 0x7000, 0x0000, 0x8000, 0, 0x0000 }, /* R1 */ 321d9f9f04SMark Brown { 0xFF17, 0xFF17, 0x0000, 0, 0x0000 }, /* R2 */ 331d9f9f04SMark Brown { 0xEBF3, 0xEBF3, 0x0000, 1, 0x6000 }, /* R3 */ 341d9f9f04SMark Brown { 0x3CF3, 0x3CF3, 0x0000, 1, 0x0000 }, /* R4 */ 351d9f9f04SMark Brown { 0xF1F8, 0xF1F8, 0x0000, 1, 0x4050 }, /* R5 */ 361d9f9f04SMark Brown { 0xFC1F, 0xFC1F, 0x0000, 1, 0x4000 }, /* R6 */ 371d9f9f04SMark Brown { 0xDFDE, 0xDFDE, 0x0000, 1, 0x01C8 }, /* R7 */ 381d9f9f04SMark Brown { 0xFCFC, 0xFCFC, 0x0000, 1, 0x0000 }, /* R8 */ 391d9f9f04SMark Brown { 0xEFFF, 0xEFFF, 0x0000, 1, 0x0040 }, /* R9 */ 401d9f9f04SMark Brown { 0xEFFF, 0xEFFF, 0x0000, 1, 0x0040 }, /* R10 */ 411d9f9f04SMark Brown { 0x27F7, 0x27F7, 0x0000, 1, 0x0004 }, /* R11 */ 421d9f9f04SMark Brown { 0x01FF, 0x01FF, 0x0000, 1, 0x00C0 }, /* R12 */ 431d9f9f04SMark Brown { 0x01FF, 0x01FF, 0x0000, 1, 0x00C0 }, /* R13 */ 441d9f9f04SMark Brown { 0x1FEF, 0x1FEF, 0x0000, 1, 0x0000 }, /* R14 */ 451d9f9f04SMark Brown { 0x0163, 0x0163, 0x0000, 1, 0x0100 }, /* R15 */ 461d9f9f04SMark Brown { 0x01FF, 0x01FF, 0x0000, 1, 0x00C0 }, /* R16 */ 471d9f9f04SMark Brown { 0x01FF, 0x01FF, 0x0000, 1, 0x00C0 }, /* R17 */ 481d9f9f04SMark Brown { 0x1FFF, 0x0FFF, 0x0000, 1, 0x0000 }, /* R18 */ 491d9f9f04SMark Brown { 0xFFFF, 0xFFFF, 0x0000, 1, 0x1000 }, /* R19 */ 501d9f9f04SMark Brown { 0xFFFF, 0xFFFF, 0x0000, 1, 0x1010 }, /* R20 */ 511d9f9f04SMark Brown { 0xFFFF, 0xFFFF, 0x0000, 1, 0x1010 }, /* R21 */ 521d9f9f04SMark Brown { 0x0FDD, 0x0FDD, 0x0000, 1, 0x8000 }, /* R22 */ 531d9f9f04SMark Brown { 0x1FFF, 0x1FFF, 0x0000, 1, 0x0800 }, /* R23 */ 541d9f9f04SMark Brown { 0x0000, 0x01DF, 0x0000, 1, 0x008B }, /* R24 */ 551d9f9f04SMark Brown { 0x0000, 0x01DF, 0x0000, 1, 0x008B }, /* R25 */ 561d9f9f04SMark Brown { 0x0000, 0x01DF, 0x0000, 1, 0x008B }, /* R26 */ 571d9f9f04SMark Brown { 0x0000, 0x01DF, 0x0000, 1, 0x008B }, /* R27 */ 581d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R28 */ 591d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R29 */ 601d9f9f04SMark Brown { 0x0000, 0x0077, 0x0000, 1, 0x0066 }, /* R30 */ 611d9f9f04SMark Brown { 0x0000, 0x0033, 0x0000, 1, 0x0022 }, /* R31 */ 621d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0079 }, /* R32 */ 631d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0079 }, /* R33 */ 641d9f9f04SMark Brown { 0x0000, 0x0003, 0x0000, 1, 0x0003 }, /* R34 */ 651d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0003 }, /* R35 */ 661d9f9f04SMark Brown { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R36 */ 671d9f9f04SMark Brown { 0x0000, 0x003F, 0x0000, 1, 0x0100 }, /* R37 */ 681d9f9f04SMark Brown { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R38 */ 691d9f9f04SMark Brown { 0x0000, 0x000F, 0x0000, 0, 0x0000 }, /* R39 */ 701d9f9f04SMark Brown { 0x0000, 0x00FF, 0x0000, 1, 0x0000 }, /* R40 */ 711d9f9f04SMark Brown { 0x0000, 0x01B7, 0x0000, 1, 0x0000 }, /* R41 */ 721d9f9f04SMark Brown { 0x0000, 0x01B7, 0x0000, 1, 0x0000 }, /* R42 */ 731d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R43 */ 741d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R44 */ 751d9f9f04SMark Brown { 0x0000, 0x00FD, 0x0000, 1, 0x0000 }, /* R45 */ 761d9f9f04SMark Brown { 0x0000, 0x00FD, 0x0000, 1, 0x0000 }, /* R46 */ 771d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R47 */ 781d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R48 */ 791d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R49 */ 801d9f9f04SMark Brown { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R50 */ 811d9f9f04SMark Brown { 0x0000, 0x01B3, 0x0000, 1, 0x0180 }, /* R51 */ 821d9f9f04SMark Brown { 0x0000, 0x0077, 0x0000, 1, 0x0000 }, /* R52 */ 831d9f9f04SMark Brown { 0x0000, 0x0077, 0x0000, 1, 0x0000 }, /* R53 */ 841d9f9f04SMark Brown { 0x0000, 0x00FF, 0x0000, 1, 0x0000 }, /* R54 */ 851d9f9f04SMark Brown { 0x0000, 0x0001, 0x0000, 1, 0x0000 }, /* R55 */ 861d9f9f04SMark Brown { 0x0000, 0x003F, 0x0000, 1, 0x0000 }, /* R56 */ 871d9f9f04SMark Brown { 0x0000, 0x004F, 0x0000, 1, 0x0000 }, /* R57 */ 881d9f9f04SMark Brown { 0x0000, 0x00FD, 0x0000, 1, 0x0000 }, /* R58 */ 891d9f9f04SMark Brown { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R59 */ 901d9f9f04SMark Brown { 0x1FFF, 0x1FFF, 0x0000, 1, 0x0000 }, /* R60 */ 911d9f9f04SMark Brown { 0xFFFF, 0xFFFF, 0x0000, 1, 0x0000 }, /* R61 */ 921d9f9f04SMark Brown { 0x03FF, 0x03FF, 0x0000, 1, 0x0000 }, /* R62 */ 931d9f9f04SMark Brown { 0x007F, 0x007F, 0x0000, 1, 0x0000 }, /* R63 */ 941d9f9f04SMark Brown { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R64 */ 951d9f9f04SMark Brown { 0xDFFF, 0xDFFF, 0x0000, 0, 0x0000 }, /* R65 */ 961d9f9f04SMark Brown { 0xDFFF, 0xDFFF, 0x0000, 0, 0x0000 }, /* R66 */ 971d9f9f04SMark Brown { 0xDFFF, 0xDFFF, 0x0000, 0, 0x0000 }, /* R67 */ 981d9f9f04SMark Brown { 0xDFFF, 0xDFFF, 0x0000, 0, 0x0000 }, /* R68 */ 991d9f9f04SMark Brown { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R69 */ 1001d9f9f04SMark Brown { 0xFFFF, 0xFFFF, 0x0000, 0, 0x4400 }, /* R70 */ 1011d9f9f04SMark Brown { 0x23FF, 0x23FF, 0x0000, 0, 0x0000 }, /* R71 */ 1021d9f9f04SMark Brown { 0xFFFF, 0xFFFF, 0x0000, 0, 0x4400 }, /* R72 */ 1031d9f9f04SMark Brown { 0x23FF, 0x23FF, 0x0000, 0, 0x0000 }, /* R73 */ 1041d9f9f04SMark Brown { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R74 */ 1051d9f9f04SMark Brown { 0x000E, 0x000E, 0x0000, 0, 0x0008 }, /* R75 */ 1061d9f9f04SMark Brown { 0xE00F, 0xE00F, 0x0000, 0, 0x0000 }, /* R76 */ 1071d9f9f04SMark Brown { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R77 */ 1081d9f9f04SMark Brown { 0x03C0, 0x03C0, 0x0000, 0, 0x02C0 }, /* R78 */ 1091d9f9f04SMark Brown { 0xFFFF, 0x0000, 0xffff, 0, 0x0000 }, /* R79 */ 1101d9f9f04SMark Brown { 0xFFFF, 0xFFFF, 0x0000, 0, 0x0000 }, /* R80 */ 1111d9f9f04SMark Brown { 0xFFFF, 0x0000, 0xffff, 0, 0x0000 }, /* R81 */ 1121d9f9f04SMark Brown { 0x2BFF, 0x0000, 0xffff, 0, 0x0000 }, /* R82 */ 1131d9f9f04SMark Brown { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R83 */ 1141d9f9f04SMark Brown { 0x80FF, 0x80FF, 0x0000, 0, 0x00ff }, /* R84 */ 1151d9f9f04SMark Brown }; 1161d9f9f04SMark Brown 1171d9f9f04SMark Brown static int wm8400_read(struct wm8400 *wm8400, u8 reg, int num_regs, u16 *dest) 1181d9f9f04SMark Brown { 1191d9f9f04SMark Brown int i, ret = 0; 1201d9f9f04SMark Brown 121fffba64cSPhil Carmody BUG_ON(reg + num_regs > ARRAY_SIZE(wm8400->reg_cache)); 1221d9f9f04SMark Brown 1231d9f9f04SMark Brown /* If there are any volatile reads then read back the entire block */ 1241d9f9f04SMark Brown for (i = reg; i < reg + num_regs; i++) 1251d9f9f04SMark Brown if (reg_data[i].vol) { 1261d9f9f04SMark Brown ret = wm8400->read_dev(wm8400->io_data, reg, 1271d9f9f04SMark Brown num_regs, dest); 1281d9f9f04SMark Brown if (ret != 0) 1291d9f9f04SMark Brown return ret; 1301d9f9f04SMark Brown for (i = 0; i < num_regs; i++) 1311d9f9f04SMark Brown dest[i] = be16_to_cpu(dest[i]); 1321d9f9f04SMark Brown 1331d9f9f04SMark Brown return 0; 1341d9f9f04SMark Brown } 1351d9f9f04SMark Brown 1361d9f9f04SMark Brown /* Otherwise use the cache */ 1371d9f9f04SMark Brown memcpy(dest, &wm8400->reg_cache[reg], num_regs * sizeof(u16)); 1381d9f9f04SMark Brown 1391d9f9f04SMark Brown return 0; 1401d9f9f04SMark Brown } 1411d9f9f04SMark Brown 1421d9f9f04SMark Brown static int wm8400_write(struct wm8400 *wm8400, u8 reg, int num_regs, 1431d9f9f04SMark Brown u16 *src) 1441d9f9f04SMark Brown { 1451d9f9f04SMark Brown int ret, i; 1461d9f9f04SMark Brown 147fffba64cSPhil Carmody BUG_ON(reg + num_regs > ARRAY_SIZE(wm8400->reg_cache)); 1481d9f9f04SMark Brown 1491d9f9f04SMark Brown for (i = 0; i < num_regs; i++) { 1501d9f9f04SMark Brown BUG_ON(!reg_data[reg + i].writable); 1511d9f9f04SMark Brown wm8400->reg_cache[reg + i] = src[i]; 1521d9f9f04SMark Brown src[i] = cpu_to_be16(src[i]); 1531d9f9f04SMark Brown } 1541d9f9f04SMark Brown 1551d9f9f04SMark Brown /* Do the actual I/O */ 1561d9f9f04SMark Brown ret = wm8400->write_dev(wm8400->io_data, reg, num_regs, src); 1571d9f9f04SMark Brown if (ret != 0) 1581d9f9f04SMark Brown return -EIO; 1591d9f9f04SMark Brown 1601d9f9f04SMark Brown return 0; 1611d9f9f04SMark Brown } 1621d9f9f04SMark Brown 1631d9f9f04SMark Brown /** 1641d9f9f04SMark Brown * wm8400_reg_read - Single register read 1651d9f9f04SMark Brown * 1661d9f9f04SMark Brown * @wm8400: Pointer to wm8400 control structure 1671d9f9f04SMark Brown * @reg: Register to read 1681d9f9f04SMark Brown * 1691d9f9f04SMark Brown * @return Read value 1701d9f9f04SMark Brown */ 1711d9f9f04SMark Brown u16 wm8400_reg_read(struct wm8400 *wm8400, u8 reg) 1721d9f9f04SMark Brown { 1731d9f9f04SMark Brown u16 val; 1741d9f9f04SMark Brown 1751d9f9f04SMark Brown mutex_lock(&wm8400->io_lock); 1761d9f9f04SMark Brown 1771d9f9f04SMark Brown wm8400_read(wm8400, reg, 1, &val); 1781d9f9f04SMark Brown 1791d9f9f04SMark Brown mutex_unlock(&wm8400->io_lock); 1801d9f9f04SMark Brown 1811d9f9f04SMark Brown return val; 1821d9f9f04SMark Brown } 1831d9f9f04SMark Brown EXPORT_SYMBOL_GPL(wm8400_reg_read); 1841d9f9f04SMark Brown 1851d9f9f04SMark Brown int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data) 1861d9f9f04SMark Brown { 1871d9f9f04SMark Brown int ret; 1881d9f9f04SMark Brown 1891d9f9f04SMark Brown mutex_lock(&wm8400->io_lock); 1901d9f9f04SMark Brown 1911d9f9f04SMark Brown ret = wm8400_read(wm8400, reg, count, data); 1921d9f9f04SMark Brown 1931d9f9f04SMark Brown mutex_unlock(&wm8400->io_lock); 1941d9f9f04SMark Brown 1951d9f9f04SMark Brown return ret; 1961d9f9f04SMark Brown } 1971d9f9f04SMark Brown EXPORT_SYMBOL_GPL(wm8400_block_read); 1981d9f9f04SMark Brown 1991d9f9f04SMark Brown /** 2001d9f9f04SMark Brown * wm8400_set_bits - Bitmask write 2011d9f9f04SMark Brown * 2021d9f9f04SMark Brown * @wm8400: Pointer to wm8400 control structure 2031d9f9f04SMark Brown * @reg: Register to access 2041d9f9f04SMark Brown * @mask: Mask of bits to change 2051d9f9f04SMark Brown * @val: Value to set for masked bits 2061d9f9f04SMark Brown */ 2071d9f9f04SMark Brown int wm8400_set_bits(struct wm8400 *wm8400, u8 reg, u16 mask, u16 val) 2081d9f9f04SMark Brown { 2091d9f9f04SMark Brown u16 tmp; 2101d9f9f04SMark Brown int ret; 2111d9f9f04SMark Brown 2121d9f9f04SMark Brown mutex_lock(&wm8400->io_lock); 2131d9f9f04SMark Brown 2141d9f9f04SMark Brown ret = wm8400_read(wm8400, reg, 1, &tmp); 2151d9f9f04SMark Brown tmp = (tmp & ~mask) | val; 2161d9f9f04SMark Brown if (ret == 0) 2171d9f9f04SMark Brown ret = wm8400_write(wm8400, reg, 1, &tmp); 2181d9f9f04SMark Brown 2191d9f9f04SMark Brown mutex_unlock(&wm8400->io_lock); 2201d9f9f04SMark Brown 2211d9f9f04SMark Brown return ret; 2221d9f9f04SMark Brown } 2231d9f9f04SMark Brown EXPORT_SYMBOL_GPL(wm8400_set_bits); 2241d9f9f04SMark Brown 2251d9f9f04SMark Brown /** 2261d9f9f04SMark Brown * wm8400_reset_codec_reg_cache - Reset cached codec registers to 2271d9f9f04SMark Brown * their default values. 2281d9f9f04SMark Brown */ 2291d9f9f04SMark Brown void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400) 2301d9f9f04SMark Brown { 2311d9f9f04SMark Brown int i; 2321d9f9f04SMark Brown 2331d9f9f04SMark Brown mutex_lock(&wm8400->io_lock); 2341d9f9f04SMark Brown 2351d9f9f04SMark Brown /* Reset all codec registers to their initial value */ 2361d9f9f04SMark Brown for (i = 0; i < ARRAY_SIZE(wm8400->reg_cache); i++) 2371d9f9f04SMark Brown if (reg_data[i].is_codec) 2381d9f9f04SMark Brown wm8400->reg_cache[i] = reg_data[i].default_val; 2391d9f9f04SMark Brown 2401d9f9f04SMark Brown mutex_unlock(&wm8400->io_lock); 2411d9f9f04SMark Brown } 2421d9f9f04SMark Brown EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache); 2431d9f9f04SMark Brown 244b8380c1aSMark Brown static int wm8400_register_codec(struct wm8400 *wm8400) 245b8380c1aSMark Brown { 246b8380c1aSMark Brown struct mfd_cell cell = { 247b8380c1aSMark Brown .name = "wm8400-codec", 248*e45be4b5SSamuel Ortiz .platform_data = wm8400, 249*e45be4b5SSamuel Ortiz .pdata_size = sizeof(*wm8400), 250b8380c1aSMark Brown }; 251b8380c1aSMark Brown 252b8380c1aSMark Brown return mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0); 253b8380c1aSMark Brown } 254b8380c1aSMark Brown 2551d9f9f04SMark Brown /* 2561d9f9f04SMark Brown * wm8400_init - Generic initialisation 2571d9f9f04SMark Brown * 2581d9f9f04SMark Brown * The WM8400 can be configured as either an I2C or SPI device. Probe 2591d9f9f04SMark Brown * functions for each bus set up the accessors then call into this to 2601d9f9f04SMark Brown * set up the device itself. 2611d9f9f04SMark Brown */ 2621d9f9f04SMark Brown static int wm8400_init(struct wm8400 *wm8400, 2631d9f9f04SMark Brown struct wm8400_platform_data *pdata) 2641d9f9f04SMark Brown { 2651d9f9f04SMark Brown u16 reg; 2661d9f9f04SMark Brown int ret, i; 2671d9f9f04SMark Brown 2681d9f9f04SMark Brown mutex_init(&wm8400->io_lock); 2691d9f9f04SMark Brown 2701902a9e6SGreg Kroah-Hartman dev_set_drvdata(wm8400->dev, wm8400); 2711d9f9f04SMark Brown 2721d9f9f04SMark Brown /* Check that this is actually a WM8400 */ 2731d9f9f04SMark Brown ret = wm8400->read_dev(wm8400->io_data, WM8400_RESET_ID, 1, ®); 2741d9f9f04SMark Brown if (ret != 0) { 2751d9f9f04SMark Brown dev_err(wm8400->dev, "Chip ID register read failed\n"); 2761d9f9f04SMark Brown return -EIO; 2771d9f9f04SMark Brown } 2781d9f9f04SMark Brown if (be16_to_cpu(reg) != reg_data[WM8400_RESET_ID].default_val) { 2791d9f9f04SMark Brown dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n", 2801d9f9f04SMark Brown be16_to_cpu(reg)); 2811d9f9f04SMark Brown return -ENODEV; 2821d9f9f04SMark Brown } 2831d9f9f04SMark Brown 2841d9f9f04SMark Brown /* We don't know what state the hardware is in and since this 2851d9f9f04SMark Brown * is a PMIC we can't reset it safely so initialise the register 2861d9f9f04SMark Brown * cache from the hardware. 2871d9f9f04SMark Brown */ 2881d9f9f04SMark Brown ret = wm8400->read_dev(wm8400->io_data, 0, 2891d9f9f04SMark Brown ARRAY_SIZE(wm8400->reg_cache), 2901d9f9f04SMark Brown wm8400->reg_cache); 2911d9f9f04SMark Brown if (ret != 0) { 2921d9f9f04SMark Brown dev_err(wm8400->dev, "Register cache read failed\n"); 2931d9f9f04SMark Brown return -EIO; 2941d9f9f04SMark Brown } 2951d9f9f04SMark Brown for (i = 0; i < ARRAY_SIZE(wm8400->reg_cache); i++) 2961d9f9f04SMark Brown wm8400->reg_cache[i] = be16_to_cpu(wm8400->reg_cache[i]); 2971d9f9f04SMark Brown 2981d9f9f04SMark Brown /* If the codec is in reset use hard coded values */ 2991d9f9f04SMark Brown if (!(wm8400->reg_cache[WM8400_POWER_MANAGEMENT_1] & WM8400_CODEC_ENA)) 3001d9f9f04SMark Brown for (i = 0; i < ARRAY_SIZE(wm8400->reg_cache); i++) 3011d9f9f04SMark Brown if (reg_data[i].is_codec) 3021d9f9f04SMark Brown wm8400->reg_cache[i] = reg_data[i].default_val; 3031d9f9f04SMark Brown 3041d9f9f04SMark Brown ret = wm8400_read(wm8400, WM8400_ID, 1, ®); 3051d9f9f04SMark Brown if (ret != 0) { 3061d9f9f04SMark Brown dev_err(wm8400->dev, "ID register read failed: %d\n", ret); 3071d9f9f04SMark Brown return ret; 3081d9f9f04SMark Brown } 3091d9f9f04SMark Brown reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT; 3101d9f9f04SMark Brown dev_info(wm8400->dev, "WM8400 revision %x\n", reg); 3111d9f9f04SMark Brown 312b8380c1aSMark Brown ret = wm8400_register_codec(wm8400); 313b8380c1aSMark Brown if (ret != 0) { 314b8380c1aSMark Brown dev_err(wm8400->dev, "Failed to register codec\n"); 315b8380c1aSMark Brown goto err_children; 316b8380c1aSMark Brown } 317b8380c1aSMark Brown 3181d9f9f04SMark Brown if (pdata && pdata->platform_init) { 3191d9f9f04SMark Brown ret = pdata->platform_init(wm8400->dev); 320b8380c1aSMark Brown if (ret != 0) { 3211d9f9f04SMark Brown dev_err(wm8400->dev, "Platform init failed: %d\n", 3221d9f9f04SMark Brown ret); 323b8380c1aSMark Brown goto err_children; 324b8380c1aSMark Brown } 3251d9f9f04SMark Brown } else 3261d9f9f04SMark Brown dev_warn(wm8400->dev, "No platform initialisation supplied\n"); 3271d9f9f04SMark Brown 328b8380c1aSMark Brown return 0; 329b8380c1aSMark Brown 330b8380c1aSMark Brown err_children: 331b8380c1aSMark Brown mfd_remove_devices(wm8400->dev); 3321d9f9f04SMark Brown return ret; 3331d9f9f04SMark Brown } 3341d9f9f04SMark Brown 3351d9f9f04SMark Brown static void wm8400_release(struct wm8400 *wm8400) 3361d9f9f04SMark Brown { 337b8380c1aSMark Brown mfd_remove_devices(wm8400->dev); 3381d9f9f04SMark Brown } 3391d9f9f04SMark Brown 3401d9f9f04SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 3411d9f9f04SMark Brown static int wm8400_i2c_read(void *io_data, char reg, int count, u16 *dest) 3421d9f9f04SMark Brown { 3431d9f9f04SMark Brown struct i2c_client *i2c = io_data; 3441d9f9f04SMark Brown struct i2c_msg xfer[2]; 3451d9f9f04SMark Brown int ret; 3461d9f9f04SMark Brown 3471d9f9f04SMark Brown /* Write register */ 3481d9f9f04SMark Brown xfer[0].addr = i2c->addr; 3491d9f9f04SMark Brown xfer[0].flags = 0; 3501d9f9f04SMark Brown xfer[0].len = 1; 3511d9f9f04SMark Brown xfer[0].buf = ® 3521d9f9f04SMark Brown 3531d9f9f04SMark Brown /* Read data */ 3541d9f9f04SMark Brown xfer[1].addr = i2c->addr; 3551d9f9f04SMark Brown xfer[1].flags = I2C_M_RD; 3561d9f9f04SMark Brown xfer[1].len = count * sizeof(u16); 3571d9f9f04SMark Brown xfer[1].buf = (u8 *)dest; 3581d9f9f04SMark Brown 3591d9f9f04SMark Brown ret = i2c_transfer(i2c->adapter, xfer, 2); 3601d9f9f04SMark Brown if (ret == 2) 3611d9f9f04SMark Brown ret = 0; 3621d9f9f04SMark Brown else if (ret >= 0) 3631d9f9f04SMark Brown ret = -EIO; 3641d9f9f04SMark Brown 3651d9f9f04SMark Brown return ret; 3661d9f9f04SMark Brown } 3671d9f9f04SMark Brown 3681d9f9f04SMark Brown static int wm8400_i2c_write(void *io_data, char reg, int count, const u16 *src) 3691d9f9f04SMark Brown { 3701d9f9f04SMark Brown struct i2c_client *i2c = io_data; 3711d9f9f04SMark Brown u8 *msg; 3721d9f9f04SMark Brown int ret; 3731d9f9f04SMark Brown 3741d9f9f04SMark Brown /* We add 1 byte for device register - ideally I2C would gather. */ 3751d9f9f04SMark Brown msg = kmalloc((count * sizeof(u16)) + 1, GFP_KERNEL); 3761d9f9f04SMark Brown if (msg == NULL) 3771d9f9f04SMark Brown return -ENOMEM; 3781d9f9f04SMark Brown 3791d9f9f04SMark Brown msg[0] = reg; 3801d9f9f04SMark Brown memcpy(&msg[1], src, count * sizeof(u16)); 3811d9f9f04SMark Brown 3821d9f9f04SMark Brown ret = i2c_master_send(i2c, msg, (count * sizeof(u16)) + 1); 3831d9f9f04SMark Brown 3841d9f9f04SMark Brown if (ret == (count * 2) + 1) 3851d9f9f04SMark Brown ret = 0; 3861d9f9f04SMark Brown else if (ret >= 0) 3871d9f9f04SMark Brown ret = -EIO; 3881d9f9f04SMark Brown 3891d9f9f04SMark Brown kfree(msg); 3901d9f9f04SMark Brown 3911d9f9f04SMark Brown return ret; 3921d9f9f04SMark Brown } 3931d9f9f04SMark Brown 3941d9f9f04SMark Brown static int wm8400_i2c_probe(struct i2c_client *i2c, 3951d9f9f04SMark Brown const struct i2c_device_id *id) 3961d9f9f04SMark Brown { 3971d9f9f04SMark Brown struct wm8400 *wm8400; 3981d9f9f04SMark Brown int ret; 3991d9f9f04SMark Brown 4001d9f9f04SMark Brown wm8400 = kzalloc(sizeof(struct wm8400), GFP_KERNEL); 4011d9f9f04SMark Brown if (wm8400 == NULL) { 4021d9f9f04SMark Brown ret = -ENOMEM; 4031d9f9f04SMark Brown goto err; 4041d9f9f04SMark Brown } 4051d9f9f04SMark Brown 4061d9f9f04SMark Brown wm8400->io_data = i2c; 4071d9f9f04SMark Brown wm8400->read_dev = wm8400_i2c_read; 4081d9f9f04SMark Brown wm8400->write_dev = wm8400_i2c_write; 4091d9f9f04SMark Brown wm8400->dev = &i2c->dev; 4101d9f9f04SMark Brown i2c_set_clientdata(i2c, wm8400); 4111d9f9f04SMark Brown 4121d9f9f04SMark Brown ret = wm8400_init(wm8400, i2c->dev.platform_data); 4131d9f9f04SMark Brown if (ret != 0) 4141d9f9f04SMark Brown goto struct_err; 4151d9f9f04SMark Brown 4161d9f9f04SMark Brown return 0; 4171d9f9f04SMark Brown 4181d9f9f04SMark Brown struct_err: 4191d9f9f04SMark Brown kfree(wm8400); 4201d9f9f04SMark Brown err: 4211d9f9f04SMark Brown return ret; 4221d9f9f04SMark Brown } 4231d9f9f04SMark Brown 4241d9f9f04SMark Brown static int wm8400_i2c_remove(struct i2c_client *i2c) 4251d9f9f04SMark Brown { 4261d9f9f04SMark Brown struct wm8400 *wm8400 = i2c_get_clientdata(i2c); 4271d9f9f04SMark Brown 4281d9f9f04SMark Brown wm8400_release(wm8400); 4291d9f9f04SMark Brown kfree(wm8400); 4301d9f9f04SMark Brown 4311d9f9f04SMark Brown return 0; 4321d9f9f04SMark Brown } 4331d9f9f04SMark Brown 4341d9f9f04SMark Brown static const struct i2c_device_id wm8400_i2c_id[] = { 4351d9f9f04SMark Brown { "wm8400", 0 }, 4361d9f9f04SMark Brown { } 4371d9f9f04SMark Brown }; 4381d9f9f04SMark Brown MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id); 4391d9f9f04SMark Brown 4401d9f9f04SMark Brown static struct i2c_driver wm8400_i2c_driver = { 4411d9f9f04SMark Brown .driver = { 4421d9f9f04SMark Brown .name = "WM8400", 4431d9f9f04SMark Brown .owner = THIS_MODULE, 4441d9f9f04SMark Brown }, 4451d9f9f04SMark Brown .probe = wm8400_i2c_probe, 4461d9f9f04SMark Brown .remove = wm8400_i2c_remove, 4471d9f9f04SMark Brown .id_table = wm8400_i2c_id, 4481d9f9f04SMark Brown }; 4491d9f9f04SMark Brown #endif 4501d9f9f04SMark Brown 4511d9f9f04SMark Brown static int __init wm8400_module_init(void) 4521d9f9f04SMark Brown { 4531d9f9f04SMark Brown int ret = -ENODEV; 4541d9f9f04SMark Brown 4551d9f9f04SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 4561d9f9f04SMark Brown ret = i2c_add_driver(&wm8400_i2c_driver); 4571d9f9f04SMark Brown if (ret != 0) 4581d9f9f04SMark Brown pr_err("Failed to register I2C driver: %d\n", ret); 4591d9f9f04SMark Brown #endif 4601d9f9f04SMark Brown 4611d9f9f04SMark Brown return ret; 4621d9f9f04SMark Brown } 4632021de87SSamuel Ortiz subsys_initcall(wm8400_module_init); 4641d9f9f04SMark Brown 4651d9f9f04SMark Brown static void __exit wm8400_module_exit(void) 4661d9f9f04SMark Brown { 4671d9f9f04SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 4681d9f9f04SMark Brown i2c_del_driver(&wm8400_i2c_driver); 4691d9f9f04SMark Brown #endif 4701d9f9f04SMark Brown } 4711d9f9f04SMark Brown module_exit(wm8400_module_exit); 4721d9f9f04SMark Brown 4731d9f9f04SMark Brown MODULE_LICENSE("GPL"); 4741d9f9f04SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 475