Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Core driver for WM8400.
4 *
5 * Copyright 2008 Wolfson Microelectronics PLC.
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 */
9
10#include <linux/init.h>
11#include <linux/bug.h>
12#include <linux/err.h>
13#include <linux/i2c.h>
14#include <linux/kernel.h>
15#include <linux/mfd/core.h>
16#include <linux/mfd/wm8400-private.h>
17#include <linux/mfd/wm8400-audio.h>
18#include <linux/regmap.h>
19#include <linux/slab.h>
20
21static bool wm8400_volatile(struct device *dev, unsigned int reg)
22{
23 switch (reg) {
24 case WM8400_INTERRUPT_STATUS_1:
25 case WM8400_INTERRUPT_LEVELS:
26 case WM8400_SHUTDOWN_REASON:
27 return true;
28 default:
29 return false;
30 }
31}
32
33static int wm8400_register_codec(struct wm8400 *wm8400)
34{
35 const struct mfd_cell cell = {
36 .name = "wm8400-codec",
37 .platform_data = wm8400,
38 .pdata_size = sizeof(*wm8400),
39 };
40
41 return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
42}
43
44/*
45 * wm8400_init - Generic initialisation
46 *
47 * The WM8400 can be configured as either an I2C or SPI device. Probe
48 * functions for each bus set up the accessors then call into this to
49 * set up the device itself.
50 */
51static int wm8400_init(struct wm8400 *wm8400,
52 struct wm8400_platform_data *pdata)
53{
54 unsigned int reg;
55 int ret;
56
57 /* Check that this is actually a WM8400 */
58 ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, ®);
59 if (ret != 0) {
60 dev_err(wm8400->dev, "Chip ID register read failed\n");
61 return -EIO;
62 }
63 if (reg != 0x6172) {
64 dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
65 reg);
66 return -ENODEV;
67 }
68
69 ret = regmap_read(wm8400->regmap, WM8400_ID, ®);
70 if (ret != 0) {
71 dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
72 return ret;
73 }
74 reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
75 dev_info(wm8400->dev, "WM8400 revision %x\n", reg);
76
77 ret = wm8400_register_codec(wm8400);
78 if (ret != 0) {
79 dev_err(wm8400->dev, "Failed to register codec\n");
80 return ret;
81 }
82
83 if (pdata && pdata->platform_init) {
84 ret = pdata->platform_init(wm8400->dev);
85 if (ret != 0) {
86 dev_err(wm8400->dev, "Platform init failed: %d\n",
87 ret);
88 return ret;
89 }
90 } else
91 dev_warn(wm8400->dev, "No platform initialisation supplied\n");
92
93 return 0;
94}
95
96static const struct regmap_config wm8400_regmap_config = {
97 .reg_bits = 8,
98 .val_bits = 16,
99 .max_register = WM8400_REGISTER_COUNT - 1,
100
101 .volatile_reg = wm8400_volatile,
102
103 .cache_type = REGCACHE_RBTREE,
104};
105
106/**
107 * wm8400_reset_codec_reg_cache - Reset cached codec registers to
108 * their default values.
109 *
110 * @wm8400: pointer to local driver data structure
111 */
112void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
113{
114 regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
115}
116EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
117
118#if IS_ENABLED(CONFIG_I2C)
119static int wm8400_i2c_probe(struct i2c_client *i2c)
120{
121 struct wm8400 *wm8400;
122
123 wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
124 if (!wm8400)
125 return -ENOMEM;
126
127 wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
128 if (IS_ERR(wm8400->regmap))
129 return PTR_ERR(wm8400->regmap);
130
131 wm8400->dev = &i2c->dev;
132 i2c_set_clientdata(i2c, wm8400);
133
134 return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
135}
136
137static const struct i2c_device_id wm8400_i2c_id[] = {
138 { "wm8400", 0 },
139 { }
140};
141
142static struct i2c_driver wm8400_i2c_driver = {
143 .driver = {
144 .name = "WM8400",
145 },
146 .probe = wm8400_i2c_probe,
147 .id_table = wm8400_i2c_id,
148};
149#endif
150
151static int __init wm8400_driver_init(void)
152{
153 int ret = -ENODEV;
154
155#if IS_ENABLED(CONFIG_I2C)
156 ret = i2c_add_driver(&wm8400_i2c_driver);
157 if (ret != 0)
158 pr_err("Failed to register I2C driver: %d\n", ret);
159#endif
160
161 return ret;
162}
163subsys_initcall(wm8400_driver_init);
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
15#include <linux/module.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>
25
26static bool wm8400_volatile(struct device *dev, unsigned int reg)
27{
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;
35 }
36}
37
38int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data)
39{
40 return regmap_bulk_read(wm8400->regmap, reg, data, count);
41}
42EXPORT_SYMBOL_GPL(wm8400_block_read);
43
44static int wm8400_register_codec(struct wm8400 *wm8400)
45{
46 const struct mfd_cell cell = {
47 .name = "wm8400-codec",
48 .platform_data = wm8400,
49 .pdata_size = sizeof(*wm8400),
50 };
51
52 return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
53}
54
55/*
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{
65 unsigned int reg;
66 int ret;
67
68 dev_set_drvdata(wm8400->dev, wm8400);
69
70 /* Check that this is actually a WM8400 */
71 ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, ®);
72 if (ret != 0) {
73 dev_err(wm8400->dev, "Chip ID register read failed\n");
74 return -EIO;
75 }
76 if (reg != 0x6172) {
77 dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
78 reg);
79 return -ENODEV;
80 }
81
82 ret = regmap_read(wm8400->regmap, WM8400_ID, ®);
83 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
90 ret = wm8400_register_codec(wm8400);
91 if (ret != 0) {
92 dev_err(wm8400->dev, "Failed to register codec\n");
93 return ret;
94 }
95
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;
102 }
103 } else
104 dev_warn(wm8400->dev, "No platform initialisation supplied\n");
105
106 return 0;
107}
108
109static const struct regmap_config wm8400_regmap_config = {
110 .reg_bits = 8,
111 .val_bits = 16,
112 .max_register = WM8400_REGISTER_COUNT - 1,
113
114 .volatile_reg = wm8400_volatile,
115
116 .cache_type = REGCACHE_RBTREE,
117};
118
119/**
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
129#if IS_ENABLED(CONFIG_I2C)
130static int wm8400_i2c_probe(struct i2c_client *i2c,
131 const struct i2c_device_id *id)
132{
133 struct wm8400 *wm8400;
134
135 wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
136 if (!wm8400)
137 return -ENOMEM;
138
139 wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
140 if (IS_ERR(wm8400->regmap))
141 return PTR_ERR(wm8400->regmap);
142
143 wm8400->dev = &i2c->dev;
144 i2c_set_clientdata(i2c, wm8400);
145
146 return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
147}
148
149static 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",
158 },
159 .probe = wm8400_i2c_probe,
160 .id_table = wm8400_i2c_id,
161};
162#endif
163
164static int __init wm8400_module_init(void)
165{
166 int ret = -ENODEV;
167
168#if IS_ENABLED(CONFIG_I2C)
169 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}
176subsys_initcall(wm8400_module_init);
177
178static void __exit wm8400_module_exit(void)
179{
180#if IS_ENABLED(CONFIG_I2C)
181 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>");