12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21d9f9f04SMark Brown /*
31d9f9f04SMark Brown * Core driver for WM8400.
41d9f9f04SMark Brown *
51d9f9f04SMark Brown * Copyright 2008 Wolfson Microelectronics PLC.
61d9f9f04SMark Brown *
71d9f9f04SMark Brown * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
81d9f9f04SMark Brown */
91d9f9f04SMark Brown
10b2b65875SPaul Gortmaker #include <linux/init.h>
111d9f9f04SMark Brown #include <linux/bug.h>
1250eeef5dSMark Brown #include <linux/err.h>
131d9f9f04SMark Brown #include <linux/i2c.h>
141d9f9f04SMark Brown #include <linux/kernel.h>
15b8380c1aSMark Brown #include <linux/mfd/core.h>
161d9f9f04SMark Brown #include <linux/mfd/wm8400-private.h>
171d9f9f04SMark Brown #include <linux/mfd/wm8400-audio.h>
1850eeef5dSMark Brown #include <linux/regmap.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201d9f9f04SMark Brown
wm8400_volatile(struct device * dev,unsigned int reg)21879eed68SMark Brown static bool wm8400_volatile(struct device *dev, unsigned int reg)
221d9f9f04SMark Brown {
23879eed68SMark Brown switch (reg) {
24879eed68SMark Brown case WM8400_INTERRUPT_STATUS_1:
25879eed68SMark Brown case WM8400_INTERRUPT_LEVELS:
26879eed68SMark Brown case WM8400_SHUTDOWN_REASON:
27879eed68SMark Brown return true;
28879eed68SMark Brown default:
29879eed68SMark Brown return false;
301d9f9f04SMark Brown }
311d9f9f04SMark Brown }
321d9f9f04SMark Brown
wm8400_register_codec(struct wm8400 * wm8400)33b8380c1aSMark Brown static int wm8400_register_codec(struct wm8400 *wm8400)
34b8380c1aSMark Brown {
35d1fb70e2SKrzysztof Kozlowski const struct mfd_cell cell = {
36b8380c1aSMark Brown .name = "wm8400-codec",
37e45be4b5SSamuel Ortiz .platform_data = wm8400,
38e45be4b5SSamuel Ortiz .pdata_size = sizeof(*wm8400),
39b8380c1aSMark Brown };
40b8380c1aSMark Brown
417825dc05SLaxman Dewangan return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
42b8380c1aSMark Brown }
43b8380c1aSMark Brown
441d9f9f04SMark Brown /*
451d9f9f04SMark Brown * wm8400_init - Generic initialisation
461d9f9f04SMark Brown *
471d9f9f04SMark Brown * The WM8400 can be configured as either an I2C or SPI device. Probe
481d9f9f04SMark Brown * functions for each bus set up the accessors then call into this to
491d9f9f04SMark Brown * set up the device itself.
501d9f9f04SMark Brown */
wm8400_init(struct wm8400 * wm8400,struct wm8400_platform_data * pdata)511d9f9f04SMark Brown static int wm8400_init(struct wm8400 *wm8400,
521d9f9f04SMark Brown struct wm8400_platform_data *pdata)
531d9f9f04SMark Brown {
54879eed68SMark Brown unsigned int reg;
55879eed68SMark Brown int ret;
561d9f9f04SMark Brown
571d9f9f04SMark Brown /* Check that this is actually a WM8400 */
58879eed68SMark Brown ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, ®);
591d9f9f04SMark Brown if (ret != 0) {
601d9f9f04SMark Brown dev_err(wm8400->dev, "Chip ID register read failed\n");
611d9f9f04SMark Brown return -EIO;
621d9f9f04SMark Brown }
63879eed68SMark Brown if (reg != 0x6172) {
64879eed68SMark Brown dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
65879eed68SMark Brown reg);
661d9f9f04SMark Brown return -ENODEV;
671d9f9f04SMark Brown }
681d9f9f04SMark Brown
69879eed68SMark Brown ret = regmap_read(wm8400->regmap, WM8400_ID, ®);
701d9f9f04SMark Brown if (ret != 0) {
711d9f9f04SMark Brown dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
721d9f9f04SMark Brown return ret;
731d9f9f04SMark Brown }
741d9f9f04SMark Brown reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
751d9f9f04SMark Brown dev_info(wm8400->dev, "WM8400 revision %x\n", reg);
761d9f9f04SMark Brown
77b8380c1aSMark Brown ret = wm8400_register_codec(wm8400);
78b8380c1aSMark Brown if (ret != 0) {
79b8380c1aSMark Brown dev_err(wm8400->dev, "Failed to register codec\n");
807825dc05SLaxman Dewangan return ret;
81b8380c1aSMark Brown }
82b8380c1aSMark Brown
831d9f9f04SMark Brown if (pdata && pdata->platform_init) {
841d9f9f04SMark Brown ret = pdata->platform_init(wm8400->dev);
85b8380c1aSMark Brown if (ret != 0) {
861d9f9f04SMark Brown dev_err(wm8400->dev, "Platform init failed: %d\n",
871d9f9f04SMark Brown ret);
887825dc05SLaxman Dewangan return ret;
89b8380c1aSMark Brown }
901d9f9f04SMark Brown } else
911d9f9f04SMark Brown dev_warn(wm8400->dev, "No platform initialisation supplied\n");
921d9f9f04SMark Brown
93b8380c1aSMark Brown return 0;
941d9f9f04SMark Brown }
951d9f9f04SMark Brown
9650eeef5dSMark Brown static const struct regmap_config wm8400_regmap_config = {
9750eeef5dSMark Brown .reg_bits = 8,
9850eeef5dSMark Brown .val_bits = 16,
9950eeef5dSMark Brown .max_register = WM8400_REGISTER_COUNT - 1,
100879eed68SMark Brown
101879eed68SMark Brown .volatile_reg = wm8400_volatile,
102879eed68SMark Brown
1039261cd04SBo Liu .cache_type = REGCACHE_MAPLE,
10450eeef5dSMark Brown };
10550eeef5dSMark Brown
106879eed68SMark Brown /**
107879eed68SMark Brown * wm8400_reset_codec_reg_cache - Reset cached codec registers to
108879eed68SMark Brown * their default values.
109f139ef70SLee Jones *
110f139ef70SLee Jones * @wm8400: pointer to local driver data structure
111879eed68SMark Brown */
wm8400_reset_codec_reg_cache(struct wm8400 * wm8400)112879eed68SMark Brown void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
113879eed68SMark Brown {
114879eed68SMark Brown regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
115879eed68SMark Brown }
116879eed68SMark Brown EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
117879eed68SMark Brown
1185eb519f3SJavier Martinez Canillas #if IS_ENABLED(CONFIG_I2C)
wm8400_i2c_probe(struct i2c_client * i2c)119521fcf40SUwe Kleine-König static int wm8400_i2c_probe(struct i2c_client *i2c)
1201d9f9f04SMark Brown {
1211d9f9f04SMark Brown struct wm8400 *wm8400;
1221d9f9f04SMark Brown
123f5772345SMark Brown wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
12482ae61c4SSachin Kamat if (!wm8400)
12582ae61c4SSachin Kamat return -ENOMEM;
1261d9f9f04SMark Brown
1272b40e9d9SMark Brown wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
12882ae61c4SSachin Kamat if (IS_ERR(wm8400->regmap))
12982ae61c4SSachin Kamat return PTR_ERR(wm8400->regmap);
13050eeef5dSMark Brown
1311d9f9f04SMark Brown wm8400->dev = &i2c->dev;
1321d9f9f04SMark Brown i2c_set_clientdata(i2c, wm8400);
1331d9f9f04SMark Brown
13482ae61c4SSachin Kamat return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
1351d9f9f04SMark Brown }
1361d9f9f04SMark Brown
1371d9f9f04SMark Brown static const struct i2c_device_id wm8400_i2c_id[] = {
138*5e9ea43cSUwe Kleine-König { "wm8400" },
1391d9f9f04SMark Brown { }
1401d9f9f04SMark Brown };
1411d9f9f04SMark Brown
1421d9f9f04SMark Brown static struct i2c_driver wm8400_i2c_driver = {
1431d9f9f04SMark Brown .driver = {
1441d9f9f04SMark Brown .name = "WM8400",
1451d9f9f04SMark Brown },
1469816d859SUwe Kleine-König .probe = wm8400_i2c_probe,
1471d9f9f04SMark Brown .id_table = wm8400_i2c_id,
1481d9f9f04SMark Brown };
1491d9f9f04SMark Brown #endif
1501d9f9f04SMark Brown
wm8400_driver_init(void)151b2b65875SPaul Gortmaker static int __init wm8400_driver_init(void)
1521d9f9f04SMark Brown {
1531d9f9f04SMark Brown int ret = -ENODEV;
1541d9f9f04SMark Brown
1555eb519f3SJavier Martinez Canillas #if IS_ENABLED(CONFIG_I2C)
1561d9f9f04SMark Brown ret = i2c_add_driver(&wm8400_i2c_driver);
1571d9f9f04SMark Brown if (ret != 0)
1581d9f9f04SMark Brown pr_err("Failed to register I2C driver: %d\n", ret);
1591d9f9f04SMark Brown #endif
1601d9f9f04SMark Brown
1611d9f9f04SMark Brown return ret;
1621d9f9f04SMark Brown }
163b2b65875SPaul Gortmaker subsys_initcall(wm8400_driver_init);
164