Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Core driver for WM8400. |
| 4 | * |
| 5 | * Copyright 2008 Wolfson Microelectronics PLC. |
| 6 | * |
| 7 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Paul Gortmaker | b2b6587 | 2019-01-13 13:36:49 -0500 | [diff] [blame] | 10 | #include <linux/init.h> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 11 | #include <linux/bug.h> |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 12 | #include <linux/err.h> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 13 | #include <linux/i2c.h> |
| 14 | #include <linux/kernel.h> |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 15 | #include <linux/mfd/core.h> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 16 | #include <linux/mfd/wm8400-private.h> |
| 17 | #include <linux/mfd/wm8400-audio.h> |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 18 | #include <linux/regmap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 20 | |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 21 | static bool wm8400_volatile(struct device *dev, unsigned int reg) |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 22 | { |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 23 | switch (reg) { |
| 24 | case WM8400_INTERRUPT_STATUS_1: |
| 25 | case WM8400_INTERRUPT_LEVELS: |
| 26 | case WM8400_SHUTDOWN_REASON: |
| 27 | return true; |
| 28 | default: |
| 29 | return false; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 30 | } |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 33 | static int wm8400_register_codec(struct wm8400 *wm8400) |
| 34 | { |
Krzysztof Kozlowski | d1fb70e | 2014-05-13 12:58:45 +0200 | [diff] [blame] | 35 | const struct mfd_cell cell = { |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 36 | .name = "wm8400-codec", |
Samuel Ortiz | e45be4b | 2011-05-11 10:44:36 +0200 | [diff] [blame] | 37 | .platform_data = wm8400, |
| 38 | .pdata_size = sizeof(*wm8400), |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Laxman Dewangan | 7825dc0 | 2016-04-08 00:13:14 +0530 | [diff] [blame] | 41 | 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] | 42 | } |
| 43 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 44 | /* |
| 45 | * wm8400_init - Generic initialisation |
| 46 | * |
| 47 | * The WM8400 can be configured as either an I2C or SPI device. Probe |
| 48 | * functions for each bus set up the accessors then call into this to |
| 49 | * set up the device itself. |
| 50 | */ |
| 51 | static int wm8400_init(struct wm8400 *wm8400, |
| 52 | struct wm8400_platform_data *pdata) |
| 53 | { |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 54 | unsigned int reg; |
| 55 | int ret; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 56 | |
Greg Kroah-Hartman | 1902a9e | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 57 | dev_set_drvdata(wm8400->dev, wm8400); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 58 | |
| 59 | /* Check that this is actually a WM8400 */ |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 60 | ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, ®); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 61 | if (ret != 0) { |
| 62 | dev_err(wm8400->dev, "Chip ID register read failed\n"); |
| 63 | return -EIO; |
| 64 | } |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 65 | if (reg != 0x6172) { |
| 66 | dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n", |
| 67 | reg); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 68 | return -ENODEV; |
| 69 | } |
| 70 | |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 71 | ret = regmap_read(wm8400->regmap, WM8400_ID, ®); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 72 | if (ret != 0) { |
| 73 | dev_err(wm8400->dev, "ID register read failed: %d\n", ret); |
| 74 | return ret; |
| 75 | } |
| 76 | reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT; |
| 77 | dev_info(wm8400->dev, "WM8400 revision %x\n", reg); |
| 78 | |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 79 | ret = wm8400_register_codec(wm8400); |
| 80 | if (ret != 0) { |
| 81 | dev_err(wm8400->dev, "Failed to register codec\n"); |
Laxman Dewangan | 7825dc0 | 2016-04-08 00:13:14 +0530 | [diff] [blame] | 82 | return ret; |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 85 | if (pdata && pdata->platform_init) { |
| 86 | ret = pdata->platform_init(wm8400->dev); |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 87 | if (ret != 0) { |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 88 | dev_err(wm8400->dev, "Platform init failed: %d\n", |
| 89 | ret); |
Laxman Dewangan | 7825dc0 | 2016-04-08 00:13:14 +0530 | [diff] [blame] | 90 | return ret; |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 91 | } |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 92 | } else |
| 93 | dev_warn(wm8400->dev, "No platform initialisation supplied\n"); |
| 94 | |
Mark Brown | b8380c1 | 2008-12-18 10:54:22 +0100 | [diff] [blame] | 95 | return 0; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 98 | static const struct regmap_config wm8400_regmap_config = { |
| 99 | .reg_bits = 8, |
| 100 | .val_bits = 16, |
| 101 | .max_register = WM8400_REGISTER_COUNT - 1, |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 102 | |
| 103 | .volatile_reg = wm8400_volatile, |
| 104 | |
| 105 | .cache_type = REGCACHE_RBTREE, |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 106 | }; |
| 107 | |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 108 | /** |
| 109 | * wm8400_reset_codec_reg_cache - Reset cached codec registers to |
| 110 | * their default values. |
Lee Jones | f139ef7 | 2020-06-23 14:06:40 +0100 | [diff] [blame] | 111 | * |
| 112 | * @wm8400: pointer to local driver data structure |
Mark Brown | 879eed6 | 2012-05-09 22:53:48 +0100 | [diff] [blame] | 113 | */ |
| 114 | void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400) |
| 115 | { |
| 116 | regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config); |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache); |
| 119 | |
Javier Martinez Canillas | 5eb519f | 2016-04-20 13:45:46 -0400 | [diff] [blame] | 120 | #if IS_ENABLED(CONFIG_I2C) |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 121 | static int wm8400_i2c_probe(struct i2c_client *i2c, |
| 122 | const struct i2c_device_id *id) |
| 123 | { |
| 124 | struct wm8400 *wm8400; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 125 | |
Mark Brown | f577234 | 2011-12-03 21:43:04 +0000 | [diff] [blame] | 126 | wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL); |
Sachin Kamat | 82ae61c | 2014-02-14 15:21:05 +0530 | [diff] [blame] | 127 | if (!wm8400) |
| 128 | return -ENOMEM; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 129 | |
Mark Brown | 2b40e9d | 2012-01-30 21:18:01 +0000 | [diff] [blame] | 130 | wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config); |
Sachin Kamat | 82ae61c | 2014-02-14 15:21:05 +0530 | [diff] [blame] | 131 | if (IS_ERR(wm8400->regmap)) |
| 132 | return PTR_ERR(wm8400->regmap); |
Mark Brown | 50eeef5 | 2011-08-09 16:34:29 +0900 | [diff] [blame] | 133 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 134 | wm8400->dev = &i2c->dev; |
| 135 | i2c_set_clientdata(i2c, wm8400); |
| 136 | |
Sachin Kamat | 82ae61c | 2014-02-14 15:21:05 +0530 | [diff] [blame] | 137 | return wm8400_init(wm8400, dev_get_platdata(&i2c->dev)); |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 140 | static const struct i2c_device_id wm8400_i2c_id[] = { |
| 141 | { "wm8400", 0 }, |
| 142 | { } |
| 143 | }; |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 144 | |
| 145 | static struct i2c_driver wm8400_i2c_driver = { |
| 146 | .driver = { |
| 147 | .name = "WM8400", |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 148 | }, |
| 149 | .probe = wm8400_i2c_probe, |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 150 | .id_table = wm8400_i2c_id, |
| 151 | }; |
| 152 | #endif |
| 153 | |
Paul Gortmaker | b2b6587 | 2019-01-13 13:36:49 -0500 | [diff] [blame] | 154 | static int __init wm8400_driver_init(void) |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 155 | { |
| 156 | int ret = -ENODEV; |
| 157 | |
Javier Martinez Canillas | 5eb519f | 2016-04-20 13:45:46 -0400 | [diff] [blame] | 158 | #if IS_ENABLED(CONFIG_I2C) |
Mark Brown | 1d9f9f0 | 2008-09-10 18:58:42 +0100 | [diff] [blame] | 159 | ret = i2c_add_driver(&wm8400_i2c_driver); |
| 160 | if (ret != 0) |
| 161 | pr_err("Failed to register I2C driver: %d\n", ret); |
| 162 | #endif |
| 163 | |
| 164 | return ret; |
| 165 | } |
Paul Gortmaker | b2b6587 | 2019-01-13 13:36:49 -0500 | [diff] [blame] | 166 | subsys_initcall(wm8400_driver_init); |