Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * MFD driver for wl1273 FM radio and audio codec submodules.
  3 *
  4 * Copyright (C) 2011 Nokia Corporation
  5 * Author: Matti Aaltonen <matti.j.aaltonen@nokia.com>
  6 *
  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.
 10 *
 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.
 15 *
 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
 19 * 02110-1301 USA
 20 *
 21 */
 22
 23#include <linux/mfd/wl1273-core.h>
 24#include <linux/slab.h>
 
 25
 26#define DRIVER_DESC "WL1273 FM Radio Core"
 27
 28static const struct i2c_device_id wl1273_driver_id_table[] = {
 29	{ WL1273_FM_DRIVER_NAME, 0 },
 30	{ }
 31};
 32MODULE_DEVICE_TABLE(i2c, wl1273_driver_id_table);
 33
 34static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
 35{
 36	struct i2c_client *client = core->client;
 37	u8 b[2];
 38	int r;
 39
 40	r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
 41	if (r != 2) {
 42		dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
 43		return -EREMOTEIO;
 44	}
 45
 46	*value = (u16)b[0] << 8 | b[1];
 47
 48	return 0;
 49}
 50
 51static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
 52{
 53	struct i2c_client *client = core->client;
 54	u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
 55	int r;
 56
 57	r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
 58	if (r) {
 59		dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
 60		return r;
 61	}
 62
 63	return 0;
 64}
 65
 66static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
 67{
 68	struct i2c_client *client = core->client;
 69	struct i2c_msg msg;
 70	int r;
 71
 72	msg.addr = client->addr;
 73	msg.flags = 0;
 74	msg.buf = data;
 75	msg.len = len;
 76
 77	r = i2c_transfer(client->adapter, &msg, 1);
 78	if (r != 1) {
 79		dev_err(&client->dev, "%s: write error.\n", __func__);
 80		return -EREMOTEIO;
 81	}
 82
 83	return 0;
 84}
 85
 86/**
 87 * wl1273_fm_set_audio() -	Set audio mode.
 88 * @core:			A pointer to the device struct.
 89 * @new_mode:			The new audio mode.
 90 *
 91 * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
 92 */
 93static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
 94{
 95	int r = 0;
 96
 97	if (core->mode == WL1273_MODE_OFF ||
 98	    core->mode == WL1273_MODE_SUSPENDED)
 99		return -EPERM;
100
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);
104		if (r)
105			goto out;
106
107		r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
108					core->i2s_mode);
109		if (r)
110			goto out;
111
112		r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
113					WL1273_AUDIO_ENABLE_I2S);
114		if (r)
115			goto out;
116
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);
121		if (r)
122			goto out;
123
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,
127					core->i2s_mode);
128		if (r)
129			goto out;
130
131		r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
132					WL1273_AUDIO_IO_SET_I2S);
133		if (r)
134			goto out;
135
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);
140		if (r)
141			goto out;
142	}
143
144	core->audio_mode = new_mode;
145out:
146	return r;
147}
148
149/**
150 * wl1273_fm_set_volume() -	Set volume.
151 * @core:			A pointer to the device struct.
152 * @volume:			The new volume value.
153 */
154static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
155{
156	int r;
157
158	if (volume > WL1273_MAX_VOLUME)
159		return -EINVAL;
160
161	if (core->volume == volume)
162		return 0;
163
164	r = wl1273_fm_write_cmd(core, WL1273_VOLUME_SET, volume);
165	if (r)
166		return r;
167
168	core->volume = volume;
169	return 0;
170}
171
172static int wl1273_core_remove(struct i2c_client *client)
173{
174	struct wl1273_core *core = i2c_get_clientdata(client);
175
176	dev_dbg(&client->dev, "%s\n", __func__);
177
178	mfd_remove_devices(&client->dev);
179	kfree(core);
180
181	return 0;
182}
183
184static int __devinit wl1273_core_probe(struct i2c_client *client,
185				       const struct i2c_device_id *id)
186{
187	struct wl1273_fm_platform_data *pdata = client->dev.platform_data;
188	struct wl1273_core *core;
189	struct mfd_cell *cell;
190	int children = 0;
191	int r = 0;
192
193	dev_dbg(&client->dev, "%s\n", __func__);
194
195	if (!pdata) {
196		dev_err(&client->dev, "No platform data.\n");
197		return -EINVAL;
198	}
199
200	if (!(pdata->children & WL1273_RADIO_CHILD)) {
201		dev_err(&client->dev, "Cannot function without radio child.\n");
202		return -EINVAL;
203	}
204
205	core = kzalloc(sizeof(*core), GFP_KERNEL);
206	if (!core)
207		return -ENOMEM;
208
209	core->pdata = pdata;
210	core->client = client;
211	mutex_init(&core->lock);
212
213	i2c_set_clientdata(client, core);
214
215	dev_dbg(&client->dev, "%s: Have V4L2.\n", __func__);
216
217	cell = &core->cells[children];
218	cell->name = "wl1273_fm_radio";
219	cell->platform_data = &core;
220	cell->pdata_size = sizeof(core);
221	children++;
222
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;
228
229	if (pdata->children & WL1273_CODEC_CHILD) {
230		cell = &core->cells[children];
231
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);
236		children++;
237	}
238
239	dev_dbg(&client->dev, "%s: number of children: %d.\n",
240		__func__, children);
241
242	r = mfd_add_devices(&client->dev, -1, core->cells,
243			    children, NULL, 0);
244	if (r)
245		goto err;
246
247	return 0;
248
249err:
250	pdata->free_resources();
251	kfree(core);
252
253	dev_dbg(&client->dev, "%s\n", __func__);
254
255	return r;
256}
257
258static struct i2c_driver wl1273_core_driver = {
259	.driver = {
260		.name = WL1273_FM_DRIVER_NAME,
261	},
262	.probe = wl1273_core_probe,
263	.id_table = wl1273_driver_id_table,
264	.remove = __devexit_p(wl1273_core_remove),
265};
266
267static int __init wl1273_core_init(void)
268{
269	int r;
270
271	r = i2c_add_driver(&wl1273_core_driver);
272	if (r) {
273		pr_err(WL1273_FM_DRIVER_NAME
274		       ": driver registration failed\n");
275		return r;
276	}
277
278	return r;
279}
280
281static void __exit wl1273_core_exit(void)
282{
283	i2c_del_driver(&wl1273_core_driver);
284}
285late_initcall(wl1273_core_init);
286module_exit(wl1273_core_exit);
287
288MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
289MODULE_DESCRIPTION(DRIVER_DESC);
290MODULE_LICENSE("GPL");
v4.6
  1/*
  2 * MFD driver for wl1273 FM radio and audio codec submodules.
  3 *
  4 * Copyright (C) 2011 Nokia Corporation
  5 * Author: Matti Aaltonen <matti.j.aaltonen@nokia.com>
  6 *
  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.
 10 *
 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.
 15 *
 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
 19 * 02110-1301 USA
 20 *
 21 */
 22
 23#include <linux/mfd/wl1273-core.h>
 24#include <linux/slab.h>
 25#include <linux/module.h>
 26
 27#define DRIVER_DESC "WL1273 FM Radio Core"
 28
 29static const struct i2c_device_id wl1273_driver_id_table[] = {
 30	{ WL1273_FM_DRIVER_NAME, 0 },
 31	{ }
 32};
 33MODULE_DEVICE_TABLE(i2c, wl1273_driver_id_table);
 34
 35static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
 36{
 37	struct i2c_client *client = core->client;
 38	u8 b[2];
 39	int r;
 40
 41	r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
 42	if (r != 2) {
 43		dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
 44		return -EREMOTEIO;
 45	}
 46
 47	*value = (u16)b[0] << 8 | b[1];
 48
 49	return 0;
 50}
 51
 52static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
 53{
 54	struct i2c_client *client = core->client;
 55	u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
 56	int r;
 57
 58	r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
 59	if (r) {
 60		dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
 61		return r;
 62	}
 63
 64	return 0;
 65}
 66
 67static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
 68{
 69	struct i2c_client *client = core->client;
 70	struct i2c_msg msg;
 71	int r;
 72
 73	msg.addr = client->addr;
 74	msg.flags = 0;
 75	msg.buf = data;
 76	msg.len = len;
 77
 78	r = i2c_transfer(client->adapter, &msg, 1);
 79	if (r != 1) {
 80		dev_err(&client->dev, "%s: write error.\n", __func__);
 81		return -EREMOTEIO;
 82	}
 83
 84	return 0;
 85}
 86
 87/**
 88 * wl1273_fm_set_audio() -	Set audio mode.
 89 * @core:			A pointer to the device struct.
 90 * @new_mode:			The new audio mode.
 91 *
 92 * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
 93 */
 94static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
 95{
 96	int r = 0;
 97
 98	if (core->mode == WL1273_MODE_OFF ||
 99	    core->mode == WL1273_MODE_SUSPENDED)
100		return -EPERM;
101
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);
105		if (r)
106			goto out;
107
108		r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
109					core->i2s_mode);
110		if (r)
111			goto out;
112
113		r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
114					WL1273_AUDIO_ENABLE_I2S);
115		if (r)
116			goto out;
117
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);
122		if (r)
123			goto out;
124
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,
128					core->i2s_mode);
129		if (r)
130			goto out;
131
132		r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
133					WL1273_AUDIO_IO_SET_I2S);
134		if (r)
135			goto out;
136
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);
141		if (r)
142			goto out;
143	}
144
145	core->audio_mode = new_mode;
146out:
147	return r;
148}
149
150/**
151 * wl1273_fm_set_volume() -	Set volume.
152 * @core:			A pointer to the device struct.
153 * @volume:			The new volume value.
154 */
155static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
156{
157	int r;
158
159	if (volume > WL1273_MAX_VOLUME)
160		return -EINVAL;
161
162	if (core->volume == volume)
163		return 0;
164
165	r = wl1273_fm_write_cmd(core, WL1273_VOLUME_SET, volume);
166	if (r)
167		return r;
168
169	core->volume = volume;
170	return 0;
171}
172
173static int wl1273_core_remove(struct i2c_client *client)
174{
 
 
175	dev_dbg(&client->dev, "%s\n", __func__);
176
177	mfd_remove_devices(&client->dev);
 
178
179	return 0;
180}
181
182static int wl1273_core_probe(struct i2c_client *client,
183				       const struct i2c_device_id *id)
184{
185	struct wl1273_fm_platform_data *pdata = dev_get_platdata(&client->dev);
186	struct wl1273_core *core;
187	struct mfd_cell *cell;
188	int children = 0;
189	int r = 0;
190
191	dev_dbg(&client->dev, "%s\n", __func__);
192
193	if (!pdata) {
194		dev_err(&client->dev, "No platform data.\n");
195		return -EINVAL;
196	}
197
198	if (!(pdata->children & WL1273_RADIO_CHILD)) {
199		dev_err(&client->dev, "Cannot function without radio child.\n");
200		return -EINVAL;
201	}
202
203	core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
204	if (!core)
205		return -ENOMEM;
206
207	core->pdata = pdata;
208	core->client = client;
209	mutex_init(&core->lock);
210
211	i2c_set_clientdata(client, core);
212
213	dev_dbg(&client->dev, "%s: Have V4L2.\n", __func__);
214
215	cell = &core->cells[children];
216	cell->name = "wl1273_fm_radio";
217	cell->platform_data = &core;
218	cell->pdata_size = sizeof(core);
219	children++;
220
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;
226
227	if (pdata->children & WL1273_CODEC_CHILD) {
228		cell = &core->cells[children];
229
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);
234		children++;
235	}
236
237	dev_dbg(&client->dev, "%s: number of children: %d.\n",
238		__func__, children);
239
240	r = mfd_add_devices(&client->dev, -1, core->cells,
241			    children, NULL, 0, NULL);
242	if (r)
243		goto err;
244
245	return 0;
246
247err:
248	pdata->free_resources();
 
249
250	dev_dbg(&client->dev, "%s\n", __func__);
251
252	return r;
253}
254
255static struct i2c_driver wl1273_core_driver = {
256	.driver = {
257		.name = WL1273_FM_DRIVER_NAME,
258	},
259	.probe = wl1273_core_probe,
260	.id_table = wl1273_driver_id_table,
261	.remove = wl1273_core_remove,
262};
263
264static int __init wl1273_core_init(void)
265{
266	int r;
267
268	r = i2c_add_driver(&wl1273_core_driver);
269	if (r) {
270		pr_err(WL1273_FM_DRIVER_NAME
271		       ": driver registration failed\n");
272		return r;
273	}
274
275	return r;
276}
277
278static void __exit wl1273_core_exit(void)
279{
280	i2c_del_driver(&wl1273_core_driver);
281}
282late_initcall(wl1273_core_init);
283module_exit(wl1273_core_exit);
284
285MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
286MODULE_DESCRIPTION(DRIVER_DESC);
287MODULE_LICENSE("GPL");