1 // SPDX-License-Identifier: GPL-2.0-only
3 * MFD driver for wl1273 FM radio and audio codec submodules.
5 * Copyright (C) 2011 Nokia Corporation
6 * Author: Matti Aaltonen <matti.j.aaltonen@nokia.com>
9 #include <linux/mfd/wl1273-core.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
13 #define DRIVER_DESC "WL1273 FM Radio Core"
15 static const struct i2c_device_id wl1273_driver_id_table[] = {
16 { WL1273_FM_DRIVER_NAME, 0 },
19 MODULE_DEVICE_TABLE(i2c, wl1273_driver_id_table);
21 static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
23 struct i2c_client *client = core->client;
27 r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
29 dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
33 *value = (u16)b[0] << 8 | b[1];
38 static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
40 struct i2c_client *client = core->client;
41 u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
44 r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
46 dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
53 static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
55 struct i2c_client *client = core->client;
59 msg.addr = client->addr;
64 r = i2c_transfer(client->adapter, &msg, 1);
66 dev_err(&client->dev, "%s: write error.\n", __func__);
74 * wl1273_fm_set_audio() - Set audio mode.
75 * @core: A pointer to the device struct.
76 * @new_mode: The new audio mode.
78 * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
80 static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
84 if (core->mode == WL1273_MODE_OFF ||
85 core->mode == WL1273_MODE_SUSPENDED)
88 if (core->mode == WL1273_MODE_RX && new_mode == WL1273_AUDIO_DIGITAL) {
89 r = wl1273_fm_write_cmd(core, WL1273_PCM_MODE_SET,
94 r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
99 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
100 WL1273_AUDIO_ENABLE_I2S);
104 } else if (core->mode == WL1273_MODE_RX &&
105 new_mode == WL1273_AUDIO_ANALOG) {
106 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
107 WL1273_AUDIO_ENABLE_ANALOG);
111 } else if (core->mode == WL1273_MODE_TX &&
112 new_mode == WL1273_AUDIO_DIGITAL) {
113 r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
118 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
119 WL1273_AUDIO_IO_SET_I2S);
123 } else if (core->mode == WL1273_MODE_TX &&
124 new_mode == WL1273_AUDIO_ANALOG) {
125 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
126 WL1273_AUDIO_IO_SET_ANALOG);
131 core->audio_mode = new_mode;
137 * wl1273_fm_set_volume() - Set volume.
138 * @core: A pointer to the device struct.
139 * @volume: The new volume value.
141 static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
145 if (volume > WL1273_MAX_VOLUME)
148 if (core->volume == volume)
151 r = wl1273_fm_write_cmd(core, WL1273_VOLUME_SET, volume);
155 core->volume = volume;
159 static int wl1273_core_probe(struct i2c_client *client,
160 const struct i2c_device_id *id)
162 struct wl1273_fm_platform_data *pdata = dev_get_platdata(&client->dev);
163 struct wl1273_core *core;
164 struct mfd_cell *cell;
168 dev_dbg(&client->dev, "%s\n", __func__);
171 dev_err(&client->dev, "No platform data.\n");
175 if (!(pdata->children & WL1273_RADIO_CHILD)) {
176 dev_err(&client->dev, "Cannot function without radio child.\n");
180 core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
185 core->client = client;
186 mutex_init(&core->lock);
188 i2c_set_clientdata(client, core);
190 dev_dbg(&client->dev, "%s: Have V4L2.\n", __func__);
192 cell = &core->cells[children];
193 cell->name = "wl1273_fm_radio";
194 cell->platform_data = &core;
195 cell->pdata_size = sizeof(core);
198 core->read = wl1273_fm_read_reg;
199 core->write = wl1273_fm_write_cmd;
200 core->write_data = wl1273_fm_write_data;
201 core->set_audio = wl1273_fm_set_audio;
202 core->set_volume = wl1273_fm_set_volume;
204 if (pdata->children & WL1273_CODEC_CHILD) {
205 cell = &core->cells[children];
207 dev_dbg(&client->dev, "%s: Have codec.\n", __func__);
208 cell->name = "wl1273-codec";
209 cell->platform_data = &core;
210 cell->pdata_size = sizeof(core);
214 dev_dbg(&client->dev, "%s: number of children: %d.\n",
217 r = devm_mfd_add_devices(&client->dev, -1, core->cells,
218 children, NULL, 0, NULL);
225 pdata->free_resources();
227 dev_dbg(&client->dev, "%s\n", __func__);
232 static struct i2c_driver wl1273_core_driver = {
234 .name = WL1273_FM_DRIVER_NAME,
236 .probe = wl1273_core_probe,
237 .id_table = wl1273_driver_id_table,
240 static int __init wl1273_core_init(void)
244 r = i2c_add_driver(&wl1273_core_driver);
246 pr_err(WL1273_FM_DRIVER_NAME
247 ": driver registration failed\n");
254 static void __exit wl1273_core_exit(void)
256 i2c_del_driver(&wl1273_core_driver);
258 late_initcall(wl1273_core_init);
259 module_exit(wl1273_core_exit);
261 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
262 MODULE_DESCRIPTION(DRIVER_DESC);
263 MODULE_LICENSE("GPL");