Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 1 | /* |
| 2 | * MFD driver for wl1273 FM radio and audio codec submodules. |
| 3 | * |
Matti Aaltonen | 94fd5b7 | 2011-03-01 10:10:35 -0300 | [diff] [blame] | 4 | * Copyright (C) 2011 Nokia Corporation |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 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> |
Paul Gortmaker | 4e36dd3 | 2011-07-03 15:13:27 -0400 | [diff] [blame] | 25 | #include <linux/module.h> |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 26 | |
| 27 | #define DRIVER_DESC "WL1273 FM Radio Core" |
| 28 | |
Axel Lin | 1206552 | 2011-03-23 20:54:17 +0800 | [diff] [blame] | 29 | static const struct i2c_device_id wl1273_driver_id_table[] = { |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 30 | { WL1273_FM_DRIVER_NAME, 0 }, |
| 31 | { } |
| 32 | }; |
| 33 | MODULE_DEVICE_TABLE(i2c, wl1273_driver_id_table); |
| 34 | |
Matti Aaltonen | 94fd5b7 | 2011-03-01 10:10:35 -0300 | [diff] [blame] | 35 | static 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 | |
| 52 | static 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 | |
| 67 | static 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 | */ |
| 94 | static 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; |
| 146 | out: |
| 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 | */ |
| 155 | static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume) |
| 156 | { |
Matti Aaltonen | 94fd5b7 | 2011-03-01 10:10:35 -0300 | [diff] [blame] | 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 | |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 173 | static int wl1273_core_remove(struct i2c_client *client) |
| 174 | { |
| 175 | struct wl1273_core *core = i2c_get_clientdata(client); |
| 176 | |
| 177 | dev_dbg(&client->dev, "%s\n", __func__); |
| 178 | |
| 179 | mfd_remove_devices(&client->dev); |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 180 | kfree(core); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int __devinit wl1273_core_probe(struct i2c_client *client, |
| 186 | const struct i2c_device_id *id) |
| 187 | { |
| 188 | struct wl1273_fm_platform_data *pdata = client->dev.platform_data; |
| 189 | struct wl1273_core *core; |
| 190 | struct mfd_cell *cell; |
| 191 | int children = 0; |
| 192 | int r = 0; |
| 193 | |
| 194 | dev_dbg(&client->dev, "%s\n", __func__); |
| 195 | |
| 196 | if (!pdata) { |
| 197 | dev_err(&client->dev, "No platform data.\n"); |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | if (!(pdata->children & WL1273_RADIO_CHILD)) { |
| 202 | dev_err(&client->dev, "Cannot function without radio child.\n"); |
| 203 | return -EINVAL; |
| 204 | } |
| 205 | |
| 206 | core = kzalloc(sizeof(*core), GFP_KERNEL); |
| 207 | if (!core) |
| 208 | return -ENOMEM; |
| 209 | |
| 210 | core->pdata = pdata; |
| 211 | core->client = client; |
| 212 | mutex_init(&core->lock); |
| 213 | |
| 214 | i2c_set_clientdata(client, core); |
| 215 | |
| 216 | dev_dbg(&client->dev, "%s: Have V4L2.\n", __func__); |
| 217 | |
| 218 | cell = &core->cells[children]; |
| 219 | cell->name = "wl1273_fm_radio"; |
Samuel Ortiz | 9e55469 | 2011-04-06 11:56:04 +0200 | [diff] [blame] | 220 | cell->platform_data = &core; |
| 221 | cell->pdata_size = sizeof(core); |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 222 | children++; |
| 223 | |
Matti Aaltonen | 94fd5b7 | 2011-03-01 10:10:35 -0300 | [diff] [blame] | 224 | core->read = wl1273_fm_read_reg; |
| 225 | core->write = wl1273_fm_write_cmd; |
| 226 | core->write_data = wl1273_fm_write_data; |
| 227 | core->set_audio = wl1273_fm_set_audio; |
| 228 | core->set_volume = wl1273_fm_set_volume; |
| 229 | |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 230 | if (pdata->children & WL1273_CODEC_CHILD) { |
| 231 | cell = &core->cells[children]; |
| 232 | |
| 233 | dev_dbg(&client->dev, "%s: Have codec.\n", __func__); |
| 234 | cell->name = "wl1273-codec"; |
Samuel Ortiz | 9e55469 | 2011-04-06 11:56:04 +0200 | [diff] [blame] | 235 | cell->platform_data = &core; |
| 236 | cell->pdata_size = sizeof(core); |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 237 | children++; |
| 238 | } |
| 239 | |
| 240 | dev_dbg(&client->dev, "%s: number of children: %d.\n", |
| 241 | __func__, children); |
| 242 | |
| 243 | r = mfd_add_devices(&client->dev, -1, core->cells, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 244 | children, NULL, 0, NULL); |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 245 | if (r) |
| 246 | goto err; |
| 247 | |
| 248 | return 0; |
| 249 | |
| 250 | err: |
Matti Aaltonen | 383268a | 2010-12-10 11:41:33 -0300 | [diff] [blame] | 251 | pdata->free_resources(); |
| 252 | kfree(core); |
| 253 | |
| 254 | dev_dbg(&client->dev, "%s\n", __func__); |
| 255 | |
| 256 | return r; |
| 257 | } |
| 258 | |
| 259 | static struct i2c_driver wl1273_core_driver = { |
| 260 | .driver = { |
| 261 | .name = WL1273_FM_DRIVER_NAME, |
| 262 | }, |
| 263 | .probe = wl1273_core_probe, |
| 264 | .id_table = wl1273_driver_id_table, |
| 265 | .remove = __devexit_p(wl1273_core_remove), |
| 266 | }; |
| 267 | |
| 268 | static int __init wl1273_core_init(void) |
| 269 | { |
| 270 | int r; |
| 271 | |
| 272 | r = i2c_add_driver(&wl1273_core_driver); |
| 273 | if (r) { |
| 274 | pr_err(WL1273_FM_DRIVER_NAME |
| 275 | ": driver registration failed\n"); |
| 276 | return r; |
| 277 | } |
| 278 | |
| 279 | return r; |
| 280 | } |
| 281 | |
| 282 | static void __exit wl1273_core_exit(void) |
| 283 | { |
| 284 | i2c_del_driver(&wl1273_core_driver); |
| 285 | } |
| 286 | late_initcall(wl1273_core_init); |
| 287 | module_exit(wl1273_core_exit); |
| 288 | |
| 289 | MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>"); |
| 290 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 291 | MODULE_LICENSE("GPL"); |