2 * MFD driver for wl1273 FM radio and audio codec submodules.
4 * Copyright (C) 2011 Nokia Corporation
5 * Author: Matti Aaltonen <matti.j.aaltonen@nokia.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 #include <linux/mfd/wl1273-core.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
27 #define DRIVER_DESC "WL1273 FM Radio Core"
29 static const struct i2c_device_id wl1273_driver_id_table
[] = {
30 { WL1273_FM_DRIVER_NAME
, 0 },
33 MODULE_DEVICE_TABLE(i2c
, wl1273_driver_id_table
);
35 static int wl1273_fm_read_reg(struct wl1273_core
*core
, u8 reg
, u16
*value
)
37 struct i2c_client
*client
= core
->client
;
41 r
= i2c_smbus_read_i2c_block_data(client
, reg
, sizeof(b
), b
);
43 dev_err(&client
->dev
, "%s: Read: %d fails.\n", __func__
, reg
);
47 *value
= (u16
)b
[0] << 8 | b
[1];
52 static int wl1273_fm_write_cmd(struct wl1273_core
*core
, u8 cmd
, u16 param
)
54 struct i2c_client
*client
= core
->client
;
55 u8 buf
[] = { (param
>> 8) & 0xff, param
& 0xff };
58 r
= i2c_smbus_write_i2c_block_data(client
, cmd
, sizeof(buf
), buf
);
60 dev_err(&client
->dev
, "%s: Cmd: %d fails.\n", __func__
, cmd
);
67 static int wl1273_fm_write_data(struct wl1273_core
*core
, u8
*data
, u16 len
)
69 struct i2c_client
*client
= core
->client
;
73 msg
.addr
= client
->addr
;
78 r
= i2c_transfer(client
->adapter
, &msg
, 1);
80 dev_err(&client
->dev
, "%s: write error.\n", __func__
);
88 * wl1273_fm_set_audio() - Set audio mode.
89 * @core: A pointer to the device struct.
90 * @new_mode: The new audio mode.
92 * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
94 static int wl1273_fm_set_audio(struct wl1273_core
*core
, unsigned int new_mode
)
98 if (core
->mode
== WL1273_MODE_OFF
||
99 core
->mode
== WL1273_MODE_SUSPENDED
)
102 if (core
->mode
== WL1273_MODE_RX
&& new_mode
== WL1273_AUDIO_DIGITAL
) {
103 r
= wl1273_fm_write_cmd(core
, WL1273_PCM_MODE_SET
,
104 WL1273_PCM_DEF_MODE
);
108 r
= wl1273_fm_write_cmd(core
, WL1273_I2S_MODE_CONFIG_SET
,
113 r
= wl1273_fm_write_cmd(core
, WL1273_AUDIO_ENABLE
,
114 WL1273_AUDIO_ENABLE_I2S
);
118 } else if (core
->mode
== WL1273_MODE_RX
&&
119 new_mode
== WL1273_AUDIO_ANALOG
) {
120 r
= wl1273_fm_write_cmd(core
, WL1273_AUDIO_ENABLE
,
121 WL1273_AUDIO_ENABLE_ANALOG
);
125 } else if (core
->mode
== WL1273_MODE_TX
&&
126 new_mode
== WL1273_AUDIO_DIGITAL
) {
127 r
= wl1273_fm_write_cmd(core
, WL1273_I2S_MODE_CONFIG_SET
,
132 r
= wl1273_fm_write_cmd(core
, WL1273_AUDIO_IO_SET
,
133 WL1273_AUDIO_IO_SET_I2S
);
137 } else if (core
->mode
== WL1273_MODE_TX
&&
138 new_mode
== WL1273_AUDIO_ANALOG
) {
139 r
= wl1273_fm_write_cmd(core
, WL1273_AUDIO_IO_SET
,
140 WL1273_AUDIO_IO_SET_ANALOG
);
145 core
->audio_mode
= new_mode
;
151 * wl1273_fm_set_volume() - Set volume.
152 * @core: A pointer to the device struct.
153 * @volume: The new volume value.
155 static int wl1273_fm_set_volume(struct wl1273_core
*core
, unsigned int volume
)
159 if (volume
> WL1273_MAX_VOLUME
)
162 if (core
->volume
== volume
)
165 r
= wl1273_fm_write_cmd(core
, WL1273_VOLUME_SET
, volume
);
169 core
->volume
= volume
;
173 static int wl1273_core_remove(struct i2c_client
*client
)
175 dev_dbg(&client
->dev
, "%s\n", __func__
);
177 mfd_remove_devices(&client
->dev
);
182 static int wl1273_core_probe(struct i2c_client
*client
,
183 const struct i2c_device_id
*id
)
185 struct wl1273_fm_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
186 struct wl1273_core
*core
;
187 struct mfd_cell
*cell
;
191 dev_dbg(&client
->dev
, "%s\n", __func__
);
194 dev_err(&client
->dev
, "No platform data.\n");
198 if (!(pdata
->children
& WL1273_RADIO_CHILD
)) {
199 dev_err(&client
->dev
, "Cannot function without radio child.\n");
203 core
= devm_kzalloc(&client
->dev
, sizeof(*core
), GFP_KERNEL
);
208 core
->client
= client
;
209 mutex_init(&core
->lock
);
211 i2c_set_clientdata(client
, core
);
213 dev_dbg(&client
->dev
, "%s: Have V4L2.\n", __func__
);
215 cell
= &core
->cells
[children
];
216 cell
->name
= "wl1273_fm_radio";
217 cell
->platform_data
= &core
;
218 cell
->pdata_size
= sizeof(core
);
221 core
->read
= wl1273_fm_read_reg
;
222 core
->write
= wl1273_fm_write_cmd
;
223 core
->write_data
= wl1273_fm_write_data
;
224 core
->set_audio
= wl1273_fm_set_audio
;
225 core
->set_volume
= wl1273_fm_set_volume
;
227 if (pdata
->children
& WL1273_CODEC_CHILD
) {
228 cell
= &core
->cells
[children
];
230 dev_dbg(&client
->dev
, "%s: Have codec.\n", __func__
);
231 cell
->name
= "wl1273-codec";
232 cell
->platform_data
= &core
;
233 cell
->pdata_size
= sizeof(core
);
237 dev_dbg(&client
->dev
, "%s: number of children: %d.\n",
240 r
= mfd_add_devices(&client
->dev
, -1, core
->cells
,
241 children
, NULL
, 0, NULL
);
248 pdata
->free_resources();
250 dev_dbg(&client
->dev
, "%s\n", __func__
);
255 static struct i2c_driver wl1273_core_driver
= {
257 .name
= WL1273_FM_DRIVER_NAME
,
259 .probe
= wl1273_core_probe
,
260 .id_table
= wl1273_driver_id_table
,
261 .remove
= wl1273_core_remove
,
264 static int __init
wl1273_core_init(void)
268 r
= i2c_add_driver(&wl1273_core_driver
);
270 pr_err(WL1273_FM_DRIVER_NAME
271 ": driver registration failed\n");
278 static void __exit
wl1273_core_exit(void)
280 i2c_del_driver(&wl1273_core_driver
);
282 late_initcall(wl1273_core_init
);
283 module_exit(wl1273_core_exit
);
285 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
286 MODULE_DESCRIPTION(DRIVER_DESC
);
287 MODULE_LICENSE("GPL");