Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 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 | |
Paul Gortmaker | 4e36dd3 | 2011-07-03 15:13:27 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 16 | #include <linux/bug.h> |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 17 | #include <linux/err.h> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 18 | #include <linux/i2c.h> |
| 19 | #include <linux/kernel.h> |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 20 | #include <linux/mfd/core.h> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 21 | #include <linux/mfd/wm8400-private.h> |
| 22 | #include <linux/mfd/wm8400-audio.h> |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 23 | #include <linux/regmap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 25 | |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 26 | static bool wm8400_volatile(struct device *dev, unsigned int reg) |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 27 | { |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 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; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 35 | } |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
| 39 | * wm8400_reg_read - Single register read |
| 40 | * |
| 41 | * @wm8400: Pointer to wm8400 control structure |
| 42 | * @reg: Register to read |
| 43 | * |
| 44 | * @return Read value |
| 45 | */ |
| 46 | u16 wm8400_reg_read(struct wm8400 *wm8400, u8 reg) |
| 47 | { |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 48 | unsigned int val; |
| 49 | int ret; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 50 | |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 51 | ret = regmap_read(wm8400->regmap, reg, &val); |
| 52 | if (ret < 0) |
| 53 | return ret; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 54 | |
| 55 | return val; |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(wm8400_reg_read); |
| 58 | |
| 59 | int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data) |
| 60 | { |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 61 | return regmap_bulk_read(wm8400->regmap, reg, data, count); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 62 | } |
Mark Brown | ebd29c6 | 2012-05-14 10:13:16 +0100 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(wm8400_block_read); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 64 | |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 65 | static int wm8400_register_codec(struct wm8400 *wm8400) |
| 66 | { |
Krzysztof Kozlowski | d1fb70e | 2014-05-13 12:58:45 +0200 | [diff] [blame] | 67 | const struct mfd_cell cell = { |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 68 | .name = "wm8400-codec", |
Samuel Ortiz | e45be4b | 2011-05-11 10:44:36 +0200 | [diff] [blame] | 69 | .platform_data = wm8400, |
| 70 | .pdata_size = sizeof(*wm8400), |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Laxman Dewangan | 7825dc0 | 2016-04-08 00:13:14 +0530 | [diff] [blame^] | 73 | return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL); |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 76 | /* |
| 77 | * wm8400_init - Generic initialisation |
| 78 | * |
| 79 | * The WM8400 can be configured as either an I2C or SPI device. Probe |
| 80 | * functions for each bus set up the accessors then call into this to |
| 81 | * set up the device itself. |
| 82 | */ |
| 83 | static int wm8400_init(struct wm8400 *wm8400, |
| 84 | struct wm8400_platform_data *pdata) |
| 85 | { |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 86 | unsigned int reg; |
| 87 | int ret; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 88 | |
Greg Kroah-Hartman | 1902a9e | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 89 | dev_set_drvdata(wm8400->dev, wm8400); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 90 | |
| 91 | /* Check that this is actually a WM8400 */ |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 92 | ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, ®); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 93 | if (ret != 0) { |
| 94 | dev_err(wm8400->dev, "Chip ID register read failed\n"); |
| 95 | return -EIO; |
| 96 | } |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 97 | if (reg != 0x6172) { |
| 98 | dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n", |
| 99 | reg); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 100 | return -ENODEV; |
| 101 | } |
| 102 | |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 103 | ret = regmap_read(wm8400->regmap, WM8400_ID, ®); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 104 | if (ret != 0) { |
| 105 | dev_err(wm8400->dev, "ID register read failed: %d\n", ret); |
| 106 | return ret; |
| 107 | } |
| 108 | reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT; |
| 109 | dev_info(wm8400->dev, "WM8400 revision %x\n", reg); |
| 110 | |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 111 | ret = wm8400_register_codec(wm8400); |
| 112 | if (ret != 0) { |
| 113 | dev_err(wm8400->dev, "Failed to register codec\n"); |
Laxman Dewangan | 7825dc0 | 2016-04-08 00:13:14 +0530 | [diff] [blame^] | 114 | return ret; |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 117 | if (pdata && pdata->platform_init) { |
| 118 | ret = pdata->platform_init(wm8400->dev); |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 119 | if (ret != 0) { |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 120 | dev_err(wm8400->dev, "Platform init failed: %d\n", |
| 121 | ret); |
Laxman Dewangan | 7825dc0 | 2016-04-08 00:13:14 +0530 | [diff] [blame^] | 122 | return ret; |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 123 | } |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 124 | } else |
| 125 | dev_warn(wm8400->dev, "No platform initialisation supplied\n"); |
| 126 | |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 127 | return 0; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 130 | static const struct regmap_config wm8400_regmap_config = { |
| 131 | .reg_bits = 8, |
| 132 | .val_bits = 16, |
| 133 | .max_register = WM8400_REGISTER_COUNT - 1, |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 134 | |
| 135 | .volatile_reg = wm8400_volatile, |
| 136 | |
| 137 | .cache_type = REGCACHE_RBTREE, |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 138 | }; |
| 139 | |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 140 | /** |
| 141 | * wm8400_reset_codec_reg_cache - Reset cached codec registers to |
| 142 | * their default values. |
| 143 | */ |
| 144 | void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400) |
| 145 | { |
| 146 | regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config); |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache); |
| 149 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 150 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 151 | static int wm8400_i2c_probe(struct i2c_client *i2c, |
| 152 | const struct i2c_device_id *id) |
| 153 | { |
| 154 | struct wm8400 *wm8400; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 155 | |
Mark Brown | f577234 | 2011-12-03 21:43:04 +0000 | [diff] [blame] | 156 | wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL); |
Sachin Kamat | 82ae61c | 2014-02-14 15:21:05 +0530 | [diff] [blame] | 157 | if (!wm8400) |
| 158 | return -ENOMEM; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 159 | |
Mark Brown | 2b40e9d | 2012-01-30 21:18:01 +0000 | [diff] [blame] | 160 | wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config); |
Sachin Kamat | 82ae61c | 2014-02-14 15:21:05 +0530 | [diff] [blame] | 161 | if (IS_ERR(wm8400->regmap)) |
| 162 | return PTR_ERR(wm8400->regmap); |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 163 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 164 | wm8400->dev = &i2c->dev; |
| 165 | i2c_set_clientdata(i2c, wm8400); |
| 166 | |
Sachin Kamat | 82ae61c | 2014-02-14 15:21:05 +0530 | [diff] [blame] | 167 | return wm8400_init(wm8400, dev_get_platdata(&i2c->dev)); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 170 | static const struct i2c_device_id wm8400_i2c_id[] = { |
| 171 | { "wm8400", 0 }, |
| 172 | { } |
| 173 | }; |
| 174 | MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id); |
| 175 | |
| 176 | static struct i2c_driver wm8400_i2c_driver = { |
| 177 | .driver = { |
| 178 | .name = "WM8400", |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 179 | }, |
| 180 | .probe = wm8400_i2c_probe, |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 181 | .id_table = wm8400_i2c_id, |
| 182 | }; |
| 183 | #endif |
| 184 | |
| 185 | static int __init wm8400_module_init(void) |
| 186 | { |
| 187 | int ret = -ENODEV; |
| 188 | |
| 189 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 190 | ret = i2c_add_driver(&wm8400_i2c_driver); |
| 191 | if (ret != 0) |
| 192 | pr_err("Failed to register I2C driver: %d\n", ret); |
| 193 | #endif |
| 194 | |
| 195 | return ret; |
| 196 | } |
Samuel Ortiz | 2021de8 | 2009-06-15 18:04:54 +0200 | [diff] [blame] | 197 | subsys_initcall(wm8400_module_init); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 198 | |
| 199 | static void __exit wm8400_module_exit(void) |
| 200 | { |
| 201 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 202 | i2c_del_driver(&wm8400_i2c_driver); |
| 203 | #endif |
| 204 | } |
| 205 | module_exit(wm8400_module_exit); |
| 206 | |
| 207 | MODULE_LICENSE("GPL"); |
| 208 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |