blob: 8a98a2fc74e11fa1204677ffffa9b57ca206b92e [file] [log] [blame]
Mark Brown1d9f9f02008-09-10 18:58:42 +01001/*
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 Gortmaker4e36dd32011-07-03 15:13:27 -040015#include <linux/module.h>
Mark Brown1d9f9f02008-09-10 18:58:42 +010016#include <linux/bug.h>
Mark Brown50eeef52011-08-09 16:34:29 +090017#include <linux/err.h>
Mark Brown1d9f9f02008-09-10 18:58:42 +010018#include <linux/i2c.h>
19#include <linux/kernel.h>
Mark Brownb8380c12008-12-18 10:54:22 +010020#include <linux/mfd/core.h>
Mark Brown1d9f9f02008-09-10 18:58:42 +010021#include <linux/mfd/wm8400-private.h>
22#include <linux/mfd/wm8400-audio.h>
Mark Brown50eeef52011-08-09 16:34:29 +090023#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mark Brown1d9f9f02008-09-10 18:58:42 +010025
Mark Brown879eed62012-05-09 22:53:48 +010026static bool wm8400_volatile(struct device *dev, unsigned int reg)
Mark Brown1d9f9f02008-09-10 18:58:42 +010027{
Mark Brown879eed62012-05-09 22:53:48 +010028 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 Brown1d9f9f02008-09-10 18:58:42 +010035 }
Mark Brown1d9f9f02008-09-10 18:58:42 +010036}
37
Mark Brown1d9f9f02008-09-10 18:58:42 +010038int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data)
39{
Mark Brown879eed62012-05-09 22:53:48 +010040 return regmap_bulk_read(wm8400->regmap, reg, data, count);
Mark Brown1d9f9f02008-09-10 18:58:42 +010041}
Mark Brownebd29c62012-05-14 10:13:16 +010042EXPORT_SYMBOL_GPL(wm8400_block_read);
Mark Brown1d9f9f02008-09-10 18:58:42 +010043
Mark Brownb8380c12008-12-18 10:54:22 +010044static int wm8400_register_codec(struct wm8400 *wm8400)
45{
Krzysztof Kozlowskid1fb70e2014-05-13 12:58:45 +020046 const struct mfd_cell cell = {
Mark Brownb8380c12008-12-18 10:54:22 +010047 .name = "wm8400-codec",
Samuel Ortize45be4b2011-05-11 10:44:36 +020048 .platform_data = wm8400,
49 .pdata_size = sizeof(*wm8400),
Mark Brownb8380c12008-12-18 10:54:22 +010050 };
51
Laxman Dewangan7825dc02016-04-08 00:13:14 +053052 return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
Mark Brownb8380c12008-12-18 10:54:22 +010053}
54
Mark Brown1d9f9f02008-09-10 18:58:42 +010055/*
56 * wm8400_init - Generic initialisation
57 *
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.
61 */
62static int wm8400_init(struct wm8400 *wm8400,
63 struct wm8400_platform_data *pdata)
64{
Mark Brown879eed62012-05-09 22:53:48 +010065 unsigned int reg;
66 int ret;
Mark Brown1d9f9f02008-09-10 18:58:42 +010067
Greg Kroah-Hartman1902a9e2009-04-30 14:43:31 -070068 dev_set_drvdata(wm8400->dev, wm8400);
Mark Brown1d9f9f02008-09-10 18:58:42 +010069
70 /* Check that this is actually a WM8400 */
Mark Brown879eed62012-05-09 22:53:48 +010071 ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, &reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +010072 if (ret != 0) {
73 dev_err(wm8400->dev, "Chip ID register read failed\n");
74 return -EIO;
75 }
Mark Brown879eed62012-05-09 22:53:48 +010076 if (reg != 0x6172) {
77 dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
78 reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +010079 return -ENODEV;
80 }
81
Mark Brown879eed62012-05-09 22:53:48 +010082 ret = regmap_read(wm8400->regmap, WM8400_ID, &reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +010083 if (ret != 0) {
84 dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
85 return ret;
86 }
87 reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
88 dev_info(wm8400->dev, "WM8400 revision %x\n", reg);
89
Mark Brownb8380c12008-12-18 10:54:22 +010090 ret = wm8400_register_codec(wm8400);
91 if (ret != 0) {
92 dev_err(wm8400->dev, "Failed to register codec\n");
Laxman Dewangan7825dc02016-04-08 00:13:14 +053093 return ret;
Mark Brownb8380c12008-12-18 10:54:22 +010094 }
95
Mark Brown1d9f9f02008-09-10 18:58:42 +010096 if (pdata && pdata->platform_init) {
97 ret = pdata->platform_init(wm8400->dev);
Mark Brownb8380c12008-12-18 10:54:22 +010098 if (ret != 0) {
Mark Brown1d9f9f02008-09-10 18:58:42 +010099 dev_err(wm8400->dev, "Platform init failed: %d\n",
100 ret);
Laxman Dewangan7825dc02016-04-08 00:13:14 +0530101 return ret;
Mark Brownb8380c12008-12-18 10:54:22 +0100102 }
Mark Brown1d9f9f02008-09-10 18:58:42 +0100103 } else
104 dev_warn(wm8400->dev, "No platform initialisation supplied\n");
105
Mark Brownb8380c12008-12-18 10:54:22 +0100106 return 0;
Mark Brown1d9f9f02008-09-10 18:58:42 +0100107}
108
Mark Brown50eeef52011-08-09 16:34:29 +0900109static const struct regmap_config wm8400_regmap_config = {
110 .reg_bits = 8,
111 .val_bits = 16,
112 .max_register = WM8400_REGISTER_COUNT - 1,
Mark Brown879eed62012-05-09 22:53:48 +0100113
114 .volatile_reg = wm8400_volatile,
115
116 .cache_type = REGCACHE_RBTREE,
Mark Brown50eeef52011-08-09 16:34:29 +0900117};
118
Mark Brown879eed62012-05-09 22:53:48 +0100119/**
120 * wm8400_reset_codec_reg_cache - Reset cached codec registers to
121 * their default values.
122 */
123void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
124{
125 regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
126}
127EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
128
Javier Martinez Canillas5eb519f2016-04-20 13:45:46 -0400129#if IS_ENABLED(CONFIG_I2C)
Mark Brown1d9f9f02008-09-10 18:58:42 +0100130static int wm8400_i2c_probe(struct i2c_client *i2c,
131 const struct i2c_device_id *id)
132{
133 struct wm8400 *wm8400;
Mark Brown1d9f9f02008-09-10 18:58:42 +0100134
Mark Brownf5772342011-12-03 21:43:04 +0000135 wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
Sachin Kamat82ae61c2014-02-14 15:21:05 +0530136 if (!wm8400)
137 return -ENOMEM;
Mark Brown1d9f9f02008-09-10 18:58:42 +0100138
Mark Brown2b40e9d2012-01-30 21:18:01 +0000139 wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
Sachin Kamat82ae61c2014-02-14 15:21:05 +0530140 if (IS_ERR(wm8400->regmap))
141 return PTR_ERR(wm8400->regmap);
Mark Brown50eeef52011-08-09 16:34:29 +0900142
Mark Brown1d9f9f02008-09-10 18:58:42 +0100143 wm8400->dev = &i2c->dev;
144 i2c_set_clientdata(i2c, wm8400);
145
Sachin Kamat82ae61c2014-02-14 15:21:05 +0530146 return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
Mark Brown1d9f9f02008-09-10 18:58:42 +0100147}
148
Mark Brown1d9f9f02008-09-10 18:58:42 +0100149static const struct i2c_device_id wm8400_i2c_id[] = {
150 { "wm8400", 0 },
151 { }
152};
153MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id);
154
155static struct i2c_driver wm8400_i2c_driver = {
156 .driver = {
157 .name = "WM8400",
Mark Brown1d9f9f02008-09-10 18:58:42 +0100158 },
159 .probe = wm8400_i2c_probe,
Mark Brown1d9f9f02008-09-10 18:58:42 +0100160 .id_table = wm8400_i2c_id,
161};
162#endif
163
164static int __init wm8400_module_init(void)
165{
166 int ret = -ENODEV;
167
Javier Martinez Canillas5eb519f2016-04-20 13:45:46 -0400168#if IS_ENABLED(CONFIG_I2C)
Mark Brown1d9f9f02008-09-10 18:58:42 +0100169 ret = i2c_add_driver(&wm8400_i2c_driver);
170 if (ret != 0)
171 pr_err("Failed to register I2C driver: %d\n", ret);
172#endif
173
174 return ret;
175}
Samuel Ortiz2021de82009-06-15 18:04:54 +0200176subsys_initcall(wm8400_module_init);
Mark Brown1d9f9f02008-09-10 18:58:42 +0100177
178static void __exit wm8400_module_exit(void)
179{
Javier Martinez Canillas5eb519f2016-04-20 13:45:46 -0400180#if IS_ENABLED(CONFIG_I2C)
Mark Brown1d9f9f02008-09-10 18:58:42 +0100181 i2c_del_driver(&wm8400_i2c_driver);
182#endif
183}
184module_exit(wm8400_module_exit);
185
186MODULE_LICENSE("GPL");
187MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");