blob: 4b7d378551d58daf515532dbcaea2c6f35c17f1e [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
38/**
39 * wm8400_reg_read - Single register read
40 *
41 * @wm8400: Pointer to wm8400 control structure
42 * @reg: Register to read
43 *
44 * @return Read value
45 */
46u16 wm8400_reg_read(struct wm8400 *wm8400, u8 reg)
47{
Mark Brown879eed62012-05-09 22:53:48 +010048 unsigned int val;
49 int ret;
Mark Brown1d9f9f02008-09-10 18:58:42 +010050
Mark Brown879eed62012-05-09 22:53:48 +010051 ret = regmap_read(wm8400->regmap, reg, &val);
52 if (ret < 0)
53 return ret;
Mark Brown1d9f9f02008-09-10 18:58:42 +010054
55 return val;
56}
57EXPORT_SYMBOL_GPL(wm8400_reg_read);
58
59int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data)
60{
Mark Brown879eed62012-05-09 22:53:48 +010061 return regmap_bulk_read(wm8400->regmap, reg, data, count);
Mark Brown1d9f9f02008-09-10 18:58:42 +010062}
Mark Brownebd29c62012-05-14 10:13:16 +010063EXPORT_SYMBOL_GPL(wm8400_block_read);
Mark Brown1d9f9f02008-09-10 18:58:42 +010064
Mark Brownb8380c12008-12-18 10:54:22 +010065static int wm8400_register_codec(struct wm8400 *wm8400)
66{
67 struct mfd_cell cell = {
68 .name = "wm8400-codec",
Samuel Ortize45be4b2011-05-11 10:44:36 +020069 .platform_data = wm8400,
70 .pdata_size = sizeof(*wm8400),
Mark Brownb8380c12008-12-18 10:54:22 +010071 };
72
73 return mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0);
74}
75
Mark Brown1d9f9f02008-09-10 18:58:42 +010076/*
77 * wm8400_init - Generic initialisation
78 *
79 * The WM8400 can be configured as either an I2C or SPI device. Probe
80 * functions for each bus set up the accessors then call into this to
81 * set up the device itself.
82 */
83static int wm8400_init(struct wm8400 *wm8400,
84 struct wm8400_platform_data *pdata)
85{
Mark Brown879eed62012-05-09 22:53:48 +010086 unsigned int reg;
87 int ret;
Mark Brown1d9f9f02008-09-10 18:58:42 +010088
Greg Kroah-Hartman1902a9e2009-04-30 14:43:31 -070089 dev_set_drvdata(wm8400->dev, wm8400);
Mark Brown1d9f9f02008-09-10 18:58:42 +010090
91 /* Check that this is actually a WM8400 */
Mark Brown879eed62012-05-09 22:53:48 +010092 ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, &reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +010093 if (ret != 0) {
94 dev_err(wm8400->dev, "Chip ID register read failed\n");
95 return -EIO;
96 }
Mark Brown879eed62012-05-09 22:53:48 +010097 if (reg != 0x6172) {
98 dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
99 reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +0100100 return -ENODEV;
101 }
102
Mark Brown879eed62012-05-09 22:53:48 +0100103 ret = regmap_read(wm8400->regmap, WM8400_ID, &reg);
Mark Brown1d9f9f02008-09-10 18:58:42 +0100104 if (ret != 0) {
105 dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
106 return ret;
107 }
108 reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
109 dev_info(wm8400->dev, "WM8400 revision %x\n", reg);
110
Mark Brownb8380c12008-12-18 10:54:22 +0100111 ret = wm8400_register_codec(wm8400);
112 if (ret != 0) {
113 dev_err(wm8400->dev, "Failed to register codec\n");
114 goto err_children;
115 }
116
Mark Brown1d9f9f02008-09-10 18:58:42 +0100117 if (pdata && pdata->platform_init) {
118 ret = pdata->platform_init(wm8400->dev);
Mark Brownb8380c12008-12-18 10:54:22 +0100119 if (ret != 0) {
Mark Brown1d9f9f02008-09-10 18:58:42 +0100120 dev_err(wm8400->dev, "Platform init failed: %d\n",
121 ret);
Mark Brownb8380c12008-12-18 10:54:22 +0100122 goto err_children;
123 }
Mark Brown1d9f9f02008-09-10 18:58:42 +0100124 } else
125 dev_warn(wm8400->dev, "No platform initialisation supplied\n");
126
Mark Brownb8380c12008-12-18 10:54:22 +0100127 return 0;
128
129err_children:
130 mfd_remove_devices(wm8400->dev);
Mark Brown1d9f9f02008-09-10 18:58:42 +0100131 return ret;
132}
133
134static void wm8400_release(struct wm8400 *wm8400)
135{
Mark Brownb8380c12008-12-18 10:54:22 +0100136 mfd_remove_devices(wm8400->dev);
Mark Brown1d9f9f02008-09-10 18:58:42 +0100137}
138
Mark Brown50eeef52011-08-09 16:34:29 +0900139static const struct regmap_config wm8400_regmap_config = {
140 .reg_bits = 8,
141 .val_bits = 16,
142 .max_register = WM8400_REGISTER_COUNT - 1,
Mark Brown879eed62012-05-09 22:53:48 +0100143
144 .volatile_reg = wm8400_volatile,
145
146 .cache_type = REGCACHE_RBTREE,
Mark Brown50eeef52011-08-09 16:34:29 +0900147};
148
Mark Brown879eed62012-05-09 22:53:48 +0100149/**
150 * wm8400_reset_codec_reg_cache - Reset cached codec registers to
151 * their default values.
152 */
153void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
154{
155 regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
156}
157EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
158
Mark Brown1d9f9f02008-09-10 18:58:42 +0100159#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brown1d9f9f02008-09-10 18:58:42 +0100160static int wm8400_i2c_probe(struct i2c_client *i2c,
161 const struct i2c_device_id *id)
162{
163 struct wm8400 *wm8400;
164 int ret;
165
Mark Brownf5772342011-12-03 21:43:04 +0000166 wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
Mark Brown1d9f9f02008-09-10 18:58:42 +0100167 if (wm8400 == NULL) {
168 ret = -ENOMEM;
169 goto err;
170 }
171
Mark Brown2b40e9d2012-01-30 21:18:01 +0000172 wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
Mark Brown50eeef52011-08-09 16:34:29 +0900173 if (IS_ERR(wm8400->regmap)) {
174 ret = PTR_ERR(wm8400->regmap);
Mark Brownf5772342011-12-03 21:43:04 +0000175 goto err;
Mark Brown50eeef52011-08-09 16:34:29 +0900176 }
177
Mark Brown1d9f9f02008-09-10 18:58:42 +0100178 wm8400->dev = &i2c->dev;
179 i2c_set_clientdata(i2c, wm8400);
180
181 ret = wm8400_init(wm8400, i2c->dev.platform_data);
182 if (ret != 0)
Mark Brown2b40e9d2012-01-30 21:18:01 +0000183 goto err;
Mark Brown1d9f9f02008-09-10 18:58:42 +0100184
185 return 0;
186
Mark Brown1d9f9f02008-09-10 18:58:42 +0100187err:
188 return ret;
189}
190
191static int wm8400_i2c_remove(struct i2c_client *i2c)
192{
193 struct wm8400 *wm8400 = i2c_get_clientdata(i2c);
194
195 wm8400_release(wm8400);
Mark Brown1d9f9f02008-09-10 18:58:42 +0100196
197 return 0;
198}
199
200static const struct i2c_device_id wm8400_i2c_id[] = {
201 { "wm8400", 0 },
202 { }
203};
204MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id);
205
206static struct i2c_driver wm8400_i2c_driver = {
207 .driver = {
208 .name = "WM8400",
209 .owner = THIS_MODULE,
210 },
211 .probe = wm8400_i2c_probe,
212 .remove = wm8400_i2c_remove,
213 .id_table = wm8400_i2c_id,
214};
215#endif
216
217static int __init wm8400_module_init(void)
218{
219 int ret = -ENODEV;
220
221#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
222 ret = i2c_add_driver(&wm8400_i2c_driver);
223 if (ret != 0)
224 pr_err("Failed to register I2C driver: %d\n", ret);
225#endif
226
227 return ret;
228}
Samuel Ortiz2021de82009-06-15 18:04:54 +0200229subsys_initcall(wm8400_module_init);
Mark Brown1d9f9f02008-09-10 18:58:42 +0100230
231static void __exit wm8400_module_exit(void)
232{
233#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
234 i2c_del_driver(&wm8400_i2c_driver);
235#endif
236}
237module_exit(wm8400_module_exit);
238
239MODULE_LICENSE("GPL");
240MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");