blob: 0fe32a05421be544548d67a38050e99c8499caeb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Brown1d9f9f02008-09-10 18:58:42 +01002/*
3 * Core driver for WM8400.
4 *
5 * Copyright 2008 Wolfson Microelectronics PLC.
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brown1d9f9f02008-09-10 18:58:42 +01008 */
9
Paul Gortmakerb2b65872019-01-13 13:36:49 -050010#include <linux/init.h>
Mark Brown1d9f9f02008-09-10 18:58:42 +010011#include <linux/bug.h>
Mark Brown50eeef52011-08-09 16:34:29 +090012#include <linux/err.h>
Mark Brown1d9f9f02008-09-10 18:58:42 +010013#include <linux/i2c.h>
14#include <linux/kernel.h>
Mark Brownb8380c12008-12-18 10:54:22 +010015#include <linux/mfd/core.h>
Mark Brown1d9f9f02008-09-10 18:58:42 +010016#include <linux/mfd/wm8400-private.h>
17#include <linux/mfd/wm8400-audio.h>
Mark Brown50eeef52011-08-09 16:34:29 +090018#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Mark Brown1d9f9f02008-09-10 18:58:42 +010020
Mark Brown879eed62012-05-09 22:53:48 +010021static bool wm8400_volatile(struct device *dev, unsigned int reg)
Mark Brown1d9f9f02008-09-10 18:58:42 +010022{
Mark Brown879eed62012-05-09 22:53:48 +010023 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 Brown1d9f9f02008-09-10 18:58:42 +010030 }
Mark Brown1d9f9f02008-09-10 18:58:42 +010031}
32
Mark Brownb8380c12008-12-18 10:54:22 +010033static int wm8400_register_codec(struct wm8400 *wm8400)
34{
Krzysztof Kozlowskid1fb70e2014-05-13 12:58:45 +020035 const struct mfd_cell cell = {
Mark Brownb8380c12008-12-18 10:54:22 +010036 .name = "wm8400-codec",
Samuel Ortize45be4b2011-05-11 10:44:36 +020037 .platform_data = wm8400,
38 .pdata_size = sizeof(*wm8400),
Mark Brownb8380c12008-12-18 10:54:22 +010039 };
40
Laxman Dewangan7825dc02016-04-08 00:13:14 +053041 return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
Mark Brownb8380c12008-12-18 10:54:22 +010042}
43
Mark Brown1d9f9f02008-09-10 18:58:42 +010044/*
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 */
51static int wm8400_init(struct wm8400 *wm8400,
52 struct wm8400_platform_data *pdata)
53{
Mark Brown879eed62012-05-09 22:53:48 +010054 unsigned int reg;
55 int ret;
Mark Brown1d9f9f02008-09-10 18:58:42 +010056
Greg Kroah-Hartman1902a9e2009-04-30 14:43:31 -070057 dev_set_drvdata(wm8400->dev, wm8400);
Mark Brown1d9f9f02008-09-10 18:58:42 +010058
59 /* Check that this is actually a WM8400 */
Mark Brown879eed62012-05-09 22:53:48 +010060 ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, &reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +010061 if (ret != 0) {
62 dev_err(wm8400->dev, "Chip ID register read failed\n");
63 return -EIO;
64 }
Mark Brown879eed62012-05-09 22:53:48 +010065 if (reg != 0x6172) {
66 dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
67 reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +010068 return -ENODEV;
69 }
70
Mark Brown879eed62012-05-09 22:53:48 +010071 ret = regmap_read(wm8400->regmap, WM8400_ID, &reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +010072 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 Brownb8380c12008-12-18 10:54:22 +010079 ret = wm8400_register_codec(wm8400);
80 if (ret != 0) {
81 dev_err(wm8400->dev, "Failed to register codec\n");
Laxman Dewangan7825dc02016-04-08 00:13:14 +053082 return ret;
Mark Brownb8380c12008-12-18 10:54:22 +010083 }
84
Mark Brown1d9f9f02008-09-10 18:58:42 +010085 if (pdata && pdata->platform_init) {
86 ret = pdata->platform_init(wm8400->dev);
Mark Brownb8380c12008-12-18 10:54:22 +010087 if (ret != 0) {
Mark Brown1d9f9f02008-09-10 18:58:42 +010088 dev_err(wm8400->dev, "Platform init failed: %d\n",
89 ret);
Laxman Dewangan7825dc02016-04-08 00:13:14 +053090 return ret;
Mark Brownb8380c12008-12-18 10:54:22 +010091 }
Mark Brown1d9f9f02008-09-10 18:58:42 +010092 } else
93 dev_warn(wm8400->dev, "No platform initialisation supplied\n");
94
Mark Brownb8380c12008-12-18 10:54:22 +010095 return 0;
Mark Brown1d9f9f02008-09-10 18:58:42 +010096}
97
Mark Brown50eeef52011-08-09 16:34:29 +090098static const struct regmap_config wm8400_regmap_config = {
99 .reg_bits = 8,
100 .val_bits = 16,
101 .max_register = WM8400_REGISTER_COUNT - 1,
Mark Brown879eed62012-05-09 22:53:48 +0100102
103 .volatile_reg = wm8400_volatile,
104
105 .cache_type = REGCACHE_RBTREE,
Mark Brown50eeef52011-08-09 16:34:29 +0900106};
107
Mark Brown879eed62012-05-09 22:53:48 +0100108/**
109 * wm8400_reset_codec_reg_cache - Reset cached codec registers to
110 * their default values.
Lee Jonesf139ef72020-06-23 14:06:40 +0100111 *
112 * @wm8400: pointer to local driver data structure
Mark Brown879eed62012-05-09 22:53:48 +0100113 */
114void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
115{
116 regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
117}
118EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
119
Javier Martinez Canillas5eb519f2016-04-20 13:45:46 -0400120#if IS_ENABLED(CONFIG_I2C)
Mark Brown1d9f9f02008-09-10 18:58:42 +0100121static int wm8400_i2c_probe(struct i2c_client *i2c,
122 const struct i2c_device_id *id)
123{
124 struct wm8400 *wm8400;
Mark Brown1d9f9f02008-09-10 18:58:42 +0100125
Mark Brownf5772342011-12-03 21:43:04 +0000126 wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
Sachin Kamat82ae61c2014-02-14 15:21:05 +0530127 if (!wm8400)
128 return -ENOMEM;
Mark Brown1d9f9f02008-09-10 18:58:42 +0100129
Mark Brown2b40e9d2012-01-30 21:18:01 +0000130 wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
Sachin Kamat82ae61c2014-02-14 15:21:05 +0530131 if (IS_ERR(wm8400->regmap))
132 return PTR_ERR(wm8400->regmap);
Mark Brown50eeef52011-08-09 16:34:29 +0900133
Mark Brown1d9f9f02008-09-10 18:58:42 +0100134 wm8400->dev = &i2c->dev;
135 i2c_set_clientdata(i2c, wm8400);
136
Sachin Kamat82ae61c2014-02-14 15:21:05 +0530137 return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
Mark Brown1d9f9f02008-09-10 18:58:42 +0100138}
139
Mark Brown1d9f9f02008-09-10 18:58:42 +0100140static const struct i2c_device_id wm8400_i2c_id[] = {
141 { "wm8400", 0 },
142 { }
143};
Mark Brown1d9f9f02008-09-10 18:58:42 +0100144
145static struct i2c_driver wm8400_i2c_driver = {
146 .driver = {
147 .name = "WM8400",
Mark Brown1d9f9f02008-09-10 18:58:42 +0100148 },
149 .probe = wm8400_i2c_probe,
Mark Brown1d9f9f02008-09-10 18:58:42 +0100150 .id_table = wm8400_i2c_id,
151};
152#endif
153
Paul Gortmakerb2b65872019-01-13 13:36:49 -0500154static int __init wm8400_driver_init(void)
Mark Brown1d9f9f02008-09-10 18:58:42 +0100155{
156 int ret = -ENODEV;
157
Javier Martinez Canillas5eb519f2016-04-20 13:45:46 -0400158#if IS_ENABLED(CONFIG_I2C)
Mark Brown1d9f9f02008-09-10 18:58:42 +0100159 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 Gortmakerb2b65872019-01-13 13:36:49 -0500166subsys_initcall(wm8400_driver_init);