1 /* 2 * Core driver for WM8400. 3 * 4 * Copyright 2008 Wolfson Microelectronics PLC. 5 * 6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of the 11 * License, or (at your option) any later version. 12 * 13 */ 14 15 #include <linux/init.h> 16 #include <linux/bug.h> 17 #include <linux/err.h> 18 #include <linux/i2c.h> 19 #include <linux/kernel.h> 20 #include <linux/mfd/core.h> 21 #include <linux/mfd/wm8400-private.h> 22 #include <linux/mfd/wm8400-audio.h> 23 #include <linux/regmap.h> 24 #include <linux/slab.h> 25 26 static bool wm8400_volatile(struct device *dev, unsigned int reg) 27 { 28 switch (reg) { 29 case WM8400_INTERRUPT_STATUS_1: 30 case WM8400_INTERRUPT_LEVELS: 31 case WM8400_SHUTDOWN_REASON: 32 return true; 33 default: 34 return false; 35 } 36 } 37 38 static int wm8400_register_codec(struct wm8400 *wm8400) 39 { 40 const struct mfd_cell cell = { 41 .name = "wm8400-codec", 42 .platform_data = wm8400, 43 .pdata_size = sizeof(*wm8400), 44 }; 45 46 return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL); 47 } 48 49 /* 50 * wm8400_init - Generic initialisation 51 * 52 * The WM8400 can be configured as either an I2C or SPI device. Probe 53 * functions for each bus set up the accessors then call into this to 54 * set up the device itself. 55 */ 56 static int wm8400_init(struct wm8400 *wm8400, 57 struct wm8400_platform_data *pdata) 58 { 59 unsigned int reg; 60 int ret; 61 62 dev_set_drvdata(wm8400->dev, wm8400); 63 64 /* Check that this is actually a WM8400 */ 65 ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, ®); 66 if (ret != 0) { 67 dev_err(wm8400->dev, "Chip ID register read failed\n"); 68 return -EIO; 69 } 70 if (reg != 0x6172) { 71 dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n", 72 reg); 73 return -ENODEV; 74 } 75 76 ret = regmap_read(wm8400->regmap, WM8400_ID, ®); 77 if (ret != 0) { 78 dev_err(wm8400->dev, "ID register read failed: %d\n", ret); 79 return ret; 80 } 81 reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT; 82 dev_info(wm8400->dev, "WM8400 revision %x\n", reg); 83 84 ret = wm8400_register_codec(wm8400); 85 if (ret != 0) { 86 dev_err(wm8400->dev, "Failed to register codec\n"); 87 return ret; 88 } 89 90 if (pdata && pdata->platform_init) { 91 ret = pdata->platform_init(wm8400->dev); 92 if (ret != 0) { 93 dev_err(wm8400->dev, "Platform init failed: %d\n", 94 ret); 95 return ret; 96 } 97 } else 98 dev_warn(wm8400->dev, "No platform initialisation supplied\n"); 99 100 return 0; 101 } 102 103 static const struct regmap_config wm8400_regmap_config = { 104 .reg_bits = 8, 105 .val_bits = 16, 106 .max_register = WM8400_REGISTER_COUNT - 1, 107 108 .volatile_reg = wm8400_volatile, 109 110 .cache_type = REGCACHE_RBTREE, 111 }; 112 113 /** 114 * wm8400_reset_codec_reg_cache - Reset cached codec registers to 115 * their default values. 116 */ 117 void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400) 118 { 119 regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config); 120 } 121 EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache); 122 123 #if IS_ENABLED(CONFIG_I2C) 124 static int wm8400_i2c_probe(struct i2c_client *i2c, 125 const struct i2c_device_id *id) 126 { 127 struct wm8400 *wm8400; 128 129 wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL); 130 if (!wm8400) 131 return -ENOMEM; 132 133 wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config); 134 if (IS_ERR(wm8400->regmap)) 135 return PTR_ERR(wm8400->regmap); 136 137 wm8400->dev = &i2c->dev; 138 i2c_set_clientdata(i2c, wm8400); 139 140 return wm8400_init(wm8400, dev_get_platdata(&i2c->dev)); 141 } 142 143 static const struct i2c_device_id wm8400_i2c_id[] = { 144 { "wm8400", 0 }, 145 { } 146 }; 147 148 static struct i2c_driver wm8400_i2c_driver = { 149 .driver = { 150 .name = "WM8400", 151 }, 152 .probe = wm8400_i2c_probe, 153 .id_table = wm8400_i2c_id, 154 }; 155 #endif 156 157 static int __init wm8400_driver_init(void) 158 { 159 int ret = -ENODEV; 160 161 #if IS_ENABLED(CONFIG_I2C) 162 ret = i2c_add_driver(&wm8400_i2c_driver); 163 if (ret != 0) 164 pr_err("Failed to register I2C driver: %d\n", ret); 165 #endif 166 167 return ret; 168 } 169 subsys_initcall(wm8400_driver_init); 170