Linux 5.1.15
[linux/fpc-iii.git] / drivers / mfd / wm8400-core.c
blob79756c83f5f028fae366e63db2f4e0d1e90de9fe
1 /*
2 * Core driver for WM8400.
4 * Copyright 2008 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
26 static bool wm8400_volatile(struct device *dev, unsigned int reg)
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;
38 int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data)
40 return regmap_bulk_read(wm8400->regmap, reg, data, count);
42 EXPORT_SYMBOL_GPL(wm8400_block_read);
44 static int wm8400_register_codec(struct wm8400 *wm8400)
46 const struct mfd_cell cell = {
47 .name = "wm8400-codec",
48 .platform_data = wm8400,
49 .pdata_size = sizeof(*wm8400),
52 return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
56 * wm8400_init - Generic initialisation
58 * The WM8400 can be configured as either an I2C or SPI device. Probe
59 * functions for each bus set up the accessors then call into this to
60 * set up the device itself.
62 static int wm8400_init(struct wm8400 *wm8400,
63 struct wm8400_platform_data *pdata)
65 unsigned int reg;
66 int ret;
68 dev_set_drvdata(wm8400->dev, wm8400);
70 /* Check that this is actually a WM8400 */
71 ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, &reg);
72 if (ret != 0) {
73 dev_err(wm8400->dev, "Chip ID register read failed\n");
74 return -EIO;
76 if (reg != 0x6172) {
77 dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
78 reg);
79 return -ENODEV;
82 ret = regmap_read(wm8400->regmap, WM8400_ID, &reg);
83 if (ret != 0) {
84 dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
85 return ret;
87 reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
88 dev_info(wm8400->dev, "WM8400 revision %x\n", reg);
90 ret = wm8400_register_codec(wm8400);
91 if (ret != 0) {
92 dev_err(wm8400->dev, "Failed to register codec\n");
93 return ret;
96 if (pdata && pdata->platform_init) {
97 ret = pdata->platform_init(wm8400->dev);
98 if (ret != 0) {
99 dev_err(wm8400->dev, "Platform init failed: %d\n",
100 ret);
101 return ret;
103 } else
104 dev_warn(wm8400->dev, "No platform initialisation supplied\n");
106 return 0;
109 static const struct regmap_config wm8400_regmap_config = {
110 .reg_bits = 8,
111 .val_bits = 16,
112 .max_register = WM8400_REGISTER_COUNT - 1,
114 .volatile_reg = wm8400_volatile,
116 .cache_type = REGCACHE_RBTREE,
120 * wm8400_reset_codec_reg_cache - Reset cached codec registers to
121 * their default values.
123 void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
125 regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
127 EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
129 #if IS_ENABLED(CONFIG_I2C)
130 static int wm8400_i2c_probe(struct i2c_client *i2c,
131 const struct i2c_device_id *id)
133 struct wm8400 *wm8400;
135 wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
136 if (!wm8400)
137 return -ENOMEM;
139 wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
140 if (IS_ERR(wm8400->regmap))
141 return PTR_ERR(wm8400->regmap);
143 wm8400->dev = &i2c->dev;
144 i2c_set_clientdata(i2c, wm8400);
146 return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
149 static const struct i2c_device_id wm8400_i2c_id[] = {
150 { "wm8400", 0 },
154 static struct i2c_driver wm8400_i2c_driver = {
155 .driver = {
156 .name = "WM8400",
158 .probe = wm8400_i2c_probe,
159 .id_table = wm8400_i2c_id,
161 #endif
163 static int __init wm8400_driver_init(void)
165 int ret = -ENODEV;
167 #if IS_ENABLED(CONFIG_I2C)
168 ret = i2c_add_driver(&wm8400_i2c_driver);
169 if (ret != 0)
170 pr_err("Failed to register I2C driver: %d\n", ret);
171 #endif
173 return ret;
175 subsys_initcall(wm8400_driver_init);