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>
26 #define DRIVER_DESC "WL1273 FM Radio Core"
28 static const struct i2c_device_id wl1273_driver_id_table
[] = {
29 { WL1273_FM_DRIVER_NAME
, 0 },
32 MODULE_DEVICE_TABLE(i2c
, wl1273_driver_id_table
);
34 static int wl1273_fm_read_reg(struct wl1273_core
*core
, u8 reg
, u16
*value
)
36 struct i2c_client
*client
= core
->client
;
40 r
= i2c_smbus_read_i2c_block_data(client
, reg
, sizeof(b
), b
);
42 dev_err(&client
->dev
, "%s: Read: %d fails.\n", __func__
, reg
);
46 *value
= (u16
)b
[0] << 8 | b
[1];
51 static int wl1273_fm_write_cmd(struct wl1273_core
*core
, u8 cmd
, u16 param
)
53 struct i2c_client
*client
= core
->client
;
54 u8 buf
[] = { (param
>> 8) & 0xff, param
& 0xff };
57 r
= i2c_smbus_write_i2c_block_data(client
, cmd
, sizeof(buf
), buf
);
59 dev_err(&client
->dev
, "%s: Cmd: %d fails.\n", __func__
, cmd
);
66 static int wl1273_fm_write_data(struct wl1273_core
*core
, u8
*data
, u16 len
)
68 struct i2c_client
*client
= core
->client
;
72 msg
.addr
= client
->addr
;
77 r
= i2c_transfer(client
->adapter
, &msg
, 1);
79 dev_err(&client
->dev
, "%s: write error.\n", __func__
);
87 * wl1273_fm_set_audio() - Set audio mode.
88 * @core: A pointer to the device struct.
89 * @new_mode: The new audio mode.
91 * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
93 static int wl1273_fm_set_audio(struct wl1273_core
*core
, unsigned int new_mode
)
97 if (core
->mode
== WL1273_MODE_OFF
||
98 core
->mode
== WL1273_MODE_SUSPENDED
)
101 if (core
->mode
== WL1273_MODE_RX
&& new_mode
== WL1273_AUDIO_DIGITAL
) {
102 r
= wl1273_fm_write_cmd(core
, WL1273_PCM_MODE_SET
,
103 WL1273_PCM_DEF_MODE
);
107 r
= wl1273_fm_write_cmd(core
, WL1273_I2S_MODE_CONFIG_SET
,
112 r
= wl1273_fm_write_cmd(core
, WL1273_AUDIO_ENABLE
,
113 WL1273_AUDIO_ENABLE_I2S
);
117 } else if (core
->mode
== WL1273_MODE_RX
&&
118 new_mode
== WL1273_AUDIO_ANALOG
) {
119 r
= wl1273_fm_write_cmd(core
, WL1273_AUDIO_ENABLE
,
120 WL1273_AUDIO_ENABLE_ANALOG
);
124 } else if (core
->mode
== WL1273_MODE_TX
&&
125 new_mode
== WL1273_AUDIO_DIGITAL
) {
126 r
= wl1273_fm_write_cmd(core
, WL1273_I2S_MODE_CONFIG_SET
,
131 r
= wl1273_fm_write_cmd(core
, WL1273_AUDIO_IO_SET
,
132 WL1273_AUDIO_IO_SET_I2S
);
136 } else if (core
->mode
== WL1273_MODE_TX
&&
137 new_mode
== WL1273_AUDIO_ANALOG
) {
138 r
= wl1273_fm_write_cmd(core
, WL1273_AUDIO_IO_SET
,
139 WL1273_AUDIO_IO_SET_ANALOG
);
144 core
->audio_mode
= new_mode
;
150 * wl1273_fm_set_volume() - Set volume.
151 * @core: A pointer to the device struct.
152 * @volume: The new volume value.
154 static int wl1273_fm_set_volume(struct wl1273_core
*core
, unsigned int volume
)
158 if (volume
> WL1273_MAX_VOLUME
)
161 if (core
->volume
== volume
)
164 r
= wl1273_fm_write_cmd(core
, WL1273_VOLUME_SET
, volume
);
168 core
->volume
= volume
;
172 static int wl1273_core_remove(struct i2c_client
*client
)
174 struct wl1273_core
*core
= i2c_get_clientdata(client
);
176 dev_dbg(&client
->dev
, "%s\n", __func__
);
178 mfd_remove_devices(&client
->dev
);
184 static int __devinit
wl1273_core_probe(struct i2c_client
*client
,
185 const struct i2c_device_id
*id
)
187 struct wl1273_fm_platform_data
*pdata
= client
->dev
.platform_data
;
188 struct wl1273_core
*core
;
189 struct mfd_cell
*cell
;
193 dev_dbg(&client
->dev
, "%s\n", __func__
);
196 dev_err(&client
->dev
, "No platform data.\n");
200 if (!(pdata
->children
& WL1273_RADIO_CHILD
)) {
201 dev_err(&client
->dev
, "Cannot function without radio child.\n");
205 core
= kzalloc(sizeof(*core
), GFP_KERNEL
);
210 core
->client
= client
;
211 mutex_init(&core
->lock
);
213 i2c_set_clientdata(client
, core
);
215 dev_dbg(&client
->dev
, "%s: Have V4L2.\n", __func__
);
217 cell
= &core
->cells
[children
];
218 cell
->name
= "wl1273_fm_radio";
219 cell
->platform_data
= &core
;
220 cell
->pdata_size
= sizeof(core
);
223 core
->read
= wl1273_fm_read_reg
;
224 core
->write
= wl1273_fm_write_cmd
;
225 core
->write_data
= wl1273_fm_write_data
;
226 core
->set_audio
= wl1273_fm_set_audio
;
227 core
->set_volume
= wl1273_fm_set_volume
;
229 if (pdata
->children
& WL1273_CODEC_CHILD
) {
230 cell
= &core
->cells
[children
];
232 dev_dbg(&client
->dev
, "%s: Have codec.\n", __func__
);
233 cell
->name
= "wl1273-codec";
234 cell
->platform_data
= &core
;
235 cell
->pdata_size
= sizeof(core
);
239 dev_dbg(&client
->dev
, "%s: number of children: %d.\n",
242 r
= mfd_add_devices(&client
->dev
, -1, core
->cells
,
250 pdata
->free_resources();
253 dev_dbg(&client
->dev
, "%s\n", __func__
);
258 static struct i2c_driver wl1273_core_driver
= {
260 .name
= WL1273_FM_DRIVER_NAME
,
262 .probe
= wl1273_core_probe
,
263 .id_table
= wl1273_driver_id_table
,
264 .remove
= __devexit_p(wl1273_core_remove
),
267 static int __init
wl1273_core_init(void)
271 r
= i2c_add_driver(&wl1273_core_driver
);
273 pr_err(WL1273_FM_DRIVER_NAME
274 ": driver registration failed\n");
281 static void __exit
wl1273_core_exit(void)
283 i2c_del_driver(&wl1273_core_driver
);
285 late_initcall(wl1273_core_init
);
286 module_exit(wl1273_core_exit
);
288 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
289 MODULE_DESCRIPTION(DRIVER_DESC
);
290 MODULE_LICENSE("GPL");