Commit | Line | Data |
---|---|---|
1d9f9f04 MB |
1 | /* |
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 | ||
4e36dd33 | 15 | #include <linux/module.h> |
1d9f9f04 | 16 | #include <linux/bug.h> |
50eeef5d | 17 | #include <linux/err.h> |
1d9f9f04 MB |
18 | #include <linux/i2c.h> |
19 | #include <linux/kernel.h> | |
b8380c1a | 20 | #include <linux/mfd/core.h> |
1d9f9f04 MB |
21 | #include <linux/mfd/wm8400-private.h> |
22 | #include <linux/mfd/wm8400-audio.h> | |
50eeef5d | 23 | #include <linux/regmap.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
1d9f9f04 | 25 | |
879eed68 | 26 | static bool wm8400_volatile(struct device *dev, unsigned int reg) |
1d9f9f04 | 27 | { |
879eed68 MB |
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; | |
1d9f9f04 | 35 | } |
1d9f9f04 MB |
36 | } |
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 | */ | |
46 | u16 wm8400_reg_read(struct wm8400 *wm8400, u8 reg) | |
47 | { | |
879eed68 MB |
48 | unsigned int val; |
49 | int ret; | |
1d9f9f04 | 50 | |
879eed68 MB |
51 | ret = regmap_read(wm8400->regmap, reg, &val); |
52 | if (ret < 0) | |
53 | return ret; | |
1d9f9f04 MB |
54 | |
55 | return val; | |
56 | } | |
57 | EXPORT_SYMBOL_GPL(wm8400_reg_read); | |
58 | ||
59 | int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data) | |
60 | { | |
879eed68 | 61 | return regmap_bulk_read(wm8400->regmap, reg, data, count); |
1d9f9f04 | 62 | } |
ebd29c6c | 63 | EXPORT_SYMBOL_GPL(wm8400_block_read); |
1d9f9f04 | 64 | |
b8380c1a MB |
65 | static int wm8400_register_codec(struct wm8400 *wm8400) |
66 | { | |
d1fb70e2 | 67 | const struct mfd_cell cell = { |
b8380c1a | 68 | .name = "wm8400-codec", |
e45be4b5 SO |
69 | .platform_data = wm8400, |
70 | .pdata_size = sizeof(*wm8400), | |
b8380c1a MB |
71 | }; |
72 | ||
0848c94f | 73 | return mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL); |
b8380c1a MB |
74 | } |
75 | ||
1d9f9f04 MB |
76 | /* |
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 | */ | |
83 | static int wm8400_init(struct wm8400 *wm8400, | |
84 | struct wm8400_platform_data *pdata) | |
85 | { | |
879eed68 MB |
86 | unsigned int reg; |
87 | int ret; | |
1d9f9f04 | 88 | |
1902a9e6 | 89 | dev_set_drvdata(wm8400->dev, wm8400); |
1d9f9f04 MB |
90 | |
91 | /* Check that this is actually a WM8400 */ | |
879eed68 | 92 | ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, ®); |
1d9f9f04 MB |
93 | if (ret != 0) { |
94 | dev_err(wm8400->dev, "Chip ID register read failed\n"); | |
95 | return -EIO; | |
96 | } | |
879eed68 MB |
97 | if (reg != 0x6172) { |
98 | dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n", | |
99 | reg); | |
1d9f9f04 MB |
100 | return -ENODEV; |
101 | } | |
102 | ||
879eed68 | 103 | ret = regmap_read(wm8400->regmap, WM8400_ID, ®); |
1d9f9f04 MB |
104 | 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 | ||
b8380c1a MB |
111 | 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 | ||
1d9f9f04 MB |
117 | if (pdata && pdata->platform_init) { |
118 | ret = pdata->platform_init(wm8400->dev); | |
b8380c1a | 119 | if (ret != 0) { |
1d9f9f04 MB |
120 | dev_err(wm8400->dev, "Platform init failed: %d\n", |
121 | ret); | |
b8380c1a MB |
122 | goto err_children; |
123 | } | |
1d9f9f04 MB |
124 | } else |
125 | dev_warn(wm8400->dev, "No platform initialisation supplied\n"); | |
126 | ||
b8380c1a MB |
127 | return 0; |
128 | ||
129 | err_children: | |
130 | mfd_remove_devices(wm8400->dev); | |
1d9f9f04 MB |
131 | return ret; |
132 | } | |
133 | ||
134 | static void wm8400_release(struct wm8400 *wm8400) | |
135 | { | |
b8380c1a | 136 | mfd_remove_devices(wm8400->dev); |
1d9f9f04 MB |
137 | } |
138 | ||
50eeef5d MB |
139 | static const struct regmap_config wm8400_regmap_config = { |
140 | .reg_bits = 8, | |
141 | .val_bits = 16, | |
142 | .max_register = WM8400_REGISTER_COUNT - 1, | |
879eed68 MB |
143 | |
144 | .volatile_reg = wm8400_volatile, | |
145 | ||
146 | .cache_type = REGCACHE_RBTREE, | |
50eeef5d | 147 | }; |
1d9f9f04 | 148 | |
879eed68 MB |
149 | /** |
150 | * wm8400_reset_codec_reg_cache - Reset cached codec registers to | |
151 | * their default values. | |
152 | */ | |
153 | void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400) | |
154 | { | |
155 | regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config); | |
156 | } | |
157 | EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache); | |
158 | ||
50eeef5d | 159 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
1d9f9f04 MB |
160 | static int wm8400_i2c_probe(struct i2c_client *i2c, |
161 | const struct i2c_device_id *id) | |
162 | { | |
163 | struct wm8400 *wm8400; | |
1d9f9f04 | 164 | |
f5772345 | 165 | wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL); |
82ae61c4 SK |
166 | if (!wm8400) |
167 | return -ENOMEM; | |
1d9f9f04 | 168 | |
2b40e9d9 | 169 | wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config); |
82ae61c4 SK |
170 | if (IS_ERR(wm8400->regmap)) |
171 | return PTR_ERR(wm8400->regmap); | |
50eeef5d | 172 | |
1d9f9f04 MB |
173 | wm8400->dev = &i2c->dev; |
174 | i2c_set_clientdata(i2c, wm8400); | |
175 | ||
82ae61c4 | 176 | return wm8400_init(wm8400, dev_get_platdata(&i2c->dev)); |
1d9f9f04 MB |
177 | } |
178 | ||
179 | static int wm8400_i2c_remove(struct i2c_client *i2c) | |
180 | { | |
181 | struct wm8400 *wm8400 = i2c_get_clientdata(i2c); | |
182 | ||
183 | wm8400_release(wm8400); | |
1d9f9f04 MB |
184 | |
185 | return 0; | |
186 | } | |
187 | ||
188 | static const struct i2c_device_id wm8400_i2c_id[] = { | |
189 | { "wm8400", 0 }, | |
190 | { } | |
191 | }; | |
192 | MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id); | |
193 | ||
194 | static struct i2c_driver wm8400_i2c_driver = { | |
195 | .driver = { | |
196 | .name = "WM8400", | |
1d9f9f04 MB |
197 | }, |
198 | .probe = wm8400_i2c_probe, | |
199 | .remove = wm8400_i2c_remove, | |
200 | .id_table = wm8400_i2c_id, | |
201 | }; | |
202 | #endif | |
203 | ||
204 | static int __init wm8400_module_init(void) | |
205 | { | |
206 | int ret = -ENODEV; | |
207 | ||
208 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | |
209 | ret = i2c_add_driver(&wm8400_i2c_driver); | |
210 | if (ret != 0) | |
211 | pr_err("Failed to register I2C driver: %d\n", ret); | |
212 | #endif | |
213 | ||
214 | return ret; | |
215 | } | |
2021de87 | 216 | subsys_initcall(wm8400_module_init); |
1d9f9f04 MB |
217 | |
218 | static void __exit wm8400_module_exit(void) | |
219 | { | |
220 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | |
221 | i2c_del_driver(&wm8400_i2c_driver); | |
222 | #endif | |
223 | } | |
224 | module_exit(wm8400_module_exit); | |
225 | ||
226 | MODULE_LICENSE("GPL"); | |
227 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |