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 154e36dd33SPaul Gortmaker #include <linux/module.h> 161d9f9f04SMark Brown #include <linux/bug.h> 1750eeef5dSMark Brown #include <linux/err.h> 181d9f9f04SMark Brown #include <linux/i2c.h> 191d9f9f04SMark Brown #include <linux/kernel.h> 20b8380c1aSMark Brown #include <linux/mfd/core.h> 211d9f9f04SMark Brown #include <linux/mfd/wm8400-private.h> 221d9f9f04SMark Brown #include <linux/mfd/wm8400-audio.h> 2350eeef5dSMark Brown #include <linux/regmap.h> 245a0e3ad6STejun Heo #include <linux/slab.h> 251d9f9f04SMark Brown 26*879eed68SMark Brown static bool wm8400_volatile(struct device *dev, unsigned int reg) 271d9f9f04SMark Brown { 28*879eed68SMark Brown switch (reg) { 29*879eed68SMark Brown case WM8400_INTERRUPT_STATUS_1: 30*879eed68SMark Brown case WM8400_INTERRUPT_LEVELS: 31*879eed68SMark Brown case WM8400_SHUTDOWN_REASON: 32*879eed68SMark Brown return true; 33*879eed68SMark Brown default: 34*879eed68SMark Brown return false; 351d9f9f04SMark Brown } 361d9f9f04SMark Brown } 371d9f9f04SMark Brown 381d9f9f04SMark Brown /** 391d9f9f04SMark Brown * wm8400_reg_read - Single register read 401d9f9f04SMark Brown * 411d9f9f04SMark Brown * @wm8400: Pointer to wm8400 control structure 421d9f9f04SMark Brown * @reg: Register to read 431d9f9f04SMark Brown * 441d9f9f04SMark Brown * @return Read value 451d9f9f04SMark Brown */ 461d9f9f04SMark Brown u16 wm8400_reg_read(struct wm8400 *wm8400, u8 reg) 471d9f9f04SMark Brown { 48*879eed68SMark Brown unsigned int val; 49*879eed68SMark Brown int ret; 501d9f9f04SMark Brown 51*879eed68SMark Brown ret = regmap_read(wm8400->regmap, reg, &val); 52*879eed68SMark Brown if (ret < 0) 53*879eed68SMark Brown return ret; 541d9f9f04SMark Brown 551d9f9f04SMark Brown return val; 561d9f9f04SMark Brown } 571d9f9f04SMark Brown EXPORT_SYMBOL_GPL(wm8400_reg_read); 581d9f9f04SMark Brown 591d9f9f04SMark Brown int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data) 601d9f9f04SMark Brown { 61*879eed68SMark Brown return regmap_bulk_read(wm8400->regmap, reg, data, count); 621d9f9f04SMark Brown } 631d9f9f04SMark Brown 64b8380c1aSMark Brown static int wm8400_register_codec(struct wm8400 *wm8400) 65b8380c1aSMark Brown { 66b8380c1aSMark Brown struct mfd_cell cell = { 67b8380c1aSMark Brown .name = "wm8400-codec", 68e45be4b5SSamuel Ortiz .platform_data = wm8400, 69e45be4b5SSamuel Ortiz .pdata_size = sizeof(*wm8400), 70b8380c1aSMark Brown }; 71b8380c1aSMark Brown 72b8380c1aSMark Brown return mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0); 73b8380c1aSMark Brown } 74b8380c1aSMark Brown 751d9f9f04SMark Brown /* 761d9f9f04SMark Brown * wm8400_init - Generic initialisation 771d9f9f04SMark Brown * 781d9f9f04SMark Brown * The WM8400 can be configured as either an I2C or SPI device. Probe 791d9f9f04SMark Brown * functions for each bus set up the accessors then call into this to 801d9f9f04SMark Brown * set up the device itself. 811d9f9f04SMark Brown */ 821d9f9f04SMark Brown static int wm8400_init(struct wm8400 *wm8400, 831d9f9f04SMark Brown struct wm8400_platform_data *pdata) 841d9f9f04SMark Brown { 85*879eed68SMark Brown unsigned int reg; 86*879eed68SMark Brown int ret; 871d9f9f04SMark Brown 881902a9e6SGreg Kroah-Hartman dev_set_drvdata(wm8400->dev, wm8400); 891d9f9f04SMark Brown 901d9f9f04SMark Brown /* Check that this is actually a WM8400 */ 91*879eed68SMark Brown ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, ®); 921d9f9f04SMark Brown if (ret != 0) { 931d9f9f04SMark Brown dev_err(wm8400->dev, "Chip ID register read failed\n"); 941d9f9f04SMark Brown return -EIO; 951d9f9f04SMark Brown } 96*879eed68SMark Brown if (reg != 0x6172) { 97*879eed68SMark Brown dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n", 98*879eed68SMark Brown reg); 991d9f9f04SMark Brown return -ENODEV; 1001d9f9f04SMark Brown } 1011d9f9f04SMark Brown 102*879eed68SMark Brown ret = regmap_read(wm8400->regmap, WM8400_ID, ®); 1031d9f9f04SMark Brown if (ret != 0) { 1041d9f9f04SMark Brown dev_err(wm8400->dev, "ID register read failed: %d\n", ret); 1051d9f9f04SMark Brown return ret; 1061d9f9f04SMark Brown } 1071d9f9f04SMark Brown reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT; 1081d9f9f04SMark Brown dev_info(wm8400->dev, "WM8400 revision %x\n", reg); 1091d9f9f04SMark Brown 110b8380c1aSMark Brown ret = wm8400_register_codec(wm8400); 111b8380c1aSMark Brown if (ret != 0) { 112b8380c1aSMark Brown dev_err(wm8400->dev, "Failed to register codec\n"); 113b8380c1aSMark Brown goto err_children; 114b8380c1aSMark Brown } 115b8380c1aSMark Brown 1161d9f9f04SMark Brown if (pdata && pdata->platform_init) { 1171d9f9f04SMark Brown ret = pdata->platform_init(wm8400->dev); 118b8380c1aSMark Brown if (ret != 0) { 1191d9f9f04SMark Brown dev_err(wm8400->dev, "Platform init failed: %d\n", 1201d9f9f04SMark Brown ret); 121b8380c1aSMark Brown goto err_children; 122b8380c1aSMark Brown } 1231d9f9f04SMark Brown } else 1241d9f9f04SMark Brown dev_warn(wm8400->dev, "No platform initialisation supplied\n"); 1251d9f9f04SMark Brown 126b8380c1aSMark Brown return 0; 127b8380c1aSMark Brown 128b8380c1aSMark Brown err_children: 129b8380c1aSMark Brown mfd_remove_devices(wm8400->dev); 1301d9f9f04SMark Brown return ret; 1311d9f9f04SMark Brown } 1321d9f9f04SMark Brown 1331d9f9f04SMark Brown static void wm8400_release(struct wm8400 *wm8400) 1341d9f9f04SMark Brown { 135b8380c1aSMark Brown mfd_remove_devices(wm8400->dev); 1361d9f9f04SMark Brown } 1371d9f9f04SMark Brown 13850eeef5dSMark Brown static const struct regmap_config wm8400_regmap_config = { 13950eeef5dSMark Brown .reg_bits = 8, 14050eeef5dSMark Brown .val_bits = 16, 14150eeef5dSMark Brown .max_register = WM8400_REGISTER_COUNT - 1, 142*879eed68SMark Brown 143*879eed68SMark Brown .volatile_reg = wm8400_volatile, 144*879eed68SMark Brown 145*879eed68SMark Brown .cache_type = REGCACHE_RBTREE, 14650eeef5dSMark Brown }; 14750eeef5dSMark Brown 148*879eed68SMark Brown /** 149*879eed68SMark Brown * wm8400_reset_codec_reg_cache - Reset cached codec registers to 150*879eed68SMark Brown * their default values. 151*879eed68SMark Brown */ 152*879eed68SMark Brown void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400) 153*879eed68SMark Brown { 154*879eed68SMark Brown regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config); 155*879eed68SMark Brown } 156*879eed68SMark Brown EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache); 157*879eed68SMark Brown 1581d9f9f04SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 1591d9f9f04SMark Brown static int wm8400_i2c_probe(struct i2c_client *i2c, 1601d9f9f04SMark Brown const struct i2c_device_id *id) 1611d9f9f04SMark Brown { 1621d9f9f04SMark Brown struct wm8400 *wm8400; 1631d9f9f04SMark Brown int ret; 1641d9f9f04SMark Brown 165f5772345SMark Brown wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL); 1661d9f9f04SMark Brown if (wm8400 == NULL) { 1671d9f9f04SMark Brown ret = -ENOMEM; 1681d9f9f04SMark Brown goto err; 1691d9f9f04SMark Brown } 1701d9f9f04SMark Brown 1712b40e9d9SMark Brown wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config); 17250eeef5dSMark Brown if (IS_ERR(wm8400->regmap)) { 17350eeef5dSMark Brown ret = PTR_ERR(wm8400->regmap); 174f5772345SMark Brown goto err; 17550eeef5dSMark Brown } 17650eeef5dSMark Brown 1771d9f9f04SMark Brown wm8400->dev = &i2c->dev; 1781d9f9f04SMark Brown i2c_set_clientdata(i2c, wm8400); 1791d9f9f04SMark Brown 1801d9f9f04SMark Brown ret = wm8400_init(wm8400, i2c->dev.platform_data); 1811d9f9f04SMark Brown if (ret != 0) 1822b40e9d9SMark Brown goto err; 1831d9f9f04SMark Brown 1841d9f9f04SMark Brown return 0; 1851d9f9f04SMark Brown 1861d9f9f04SMark Brown err: 1871d9f9f04SMark Brown return ret; 1881d9f9f04SMark Brown } 1891d9f9f04SMark Brown 1901d9f9f04SMark Brown static int wm8400_i2c_remove(struct i2c_client *i2c) 1911d9f9f04SMark Brown { 1921d9f9f04SMark Brown struct wm8400 *wm8400 = i2c_get_clientdata(i2c); 1931d9f9f04SMark Brown 1941d9f9f04SMark Brown wm8400_release(wm8400); 1951d9f9f04SMark Brown 1961d9f9f04SMark Brown return 0; 1971d9f9f04SMark Brown } 1981d9f9f04SMark Brown 1991d9f9f04SMark Brown static const struct i2c_device_id wm8400_i2c_id[] = { 2001d9f9f04SMark Brown { "wm8400", 0 }, 2011d9f9f04SMark Brown { } 2021d9f9f04SMark Brown }; 2031d9f9f04SMark Brown MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id); 2041d9f9f04SMark Brown 2051d9f9f04SMark Brown static struct i2c_driver wm8400_i2c_driver = { 2061d9f9f04SMark Brown .driver = { 2071d9f9f04SMark Brown .name = "WM8400", 2081d9f9f04SMark Brown .owner = THIS_MODULE, 2091d9f9f04SMark Brown }, 2101d9f9f04SMark Brown .probe = wm8400_i2c_probe, 2111d9f9f04SMark Brown .remove = wm8400_i2c_remove, 2121d9f9f04SMark Brown .id_table = wm8400_i2c_id, 2131d9f9f04SMark Brown }; 2141d9f9f04SMark Brown #endif 2151d9f9f04SMark Brown 2161d9f9f04SMark Brown static int __init wm8400_module_init(void) 2171d9f9f04SMark Brown { 2181d9f9f04SMark Brown int ret = -ENODEV; 2191d9f9f04SMark Brown 2201d9f9f04SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 2211d9f9f04SMark Brown ret = i2c_add_driver(&wm8400_i2c_driver); 2221d9f9f04SMark Brown if (ret != 0) 2231d9f9f04SMark Brown pr_err("Failed to register I2C driver: %d\n", ret); 2241d9f9f04SMark Brown #endif 2251d9f9f04SMark Brown 2261d9f9f04SMark Brown return ret; 2271d9f9f04SMark Brown } 2282021de87SSamuel Ortiz subsys_initcall(wm8400_module_init); 2291d9f9f04SMark Brown 2301d9f9f04SMark Brown static void __exit wm8400_module_exit(void) 2311d9f9f04SMark Brown { 2321d9f9f04SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 2331d9f9f04SMark Brown i2c_del_driver(&wm8400_i2c_driver); 2341d9f9f04SMark Brown #endif 2351d9f9f04SMark Brown } 2361d9f9f04SMark Brown module_exit(wm8400_module_exit); 2371d9f9f04SMark Brown 2381d9f9f04SMark Brown MODULE_LICENSE("GPL"); 2391d9f9f04SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 240