| 1 | /* |
| 2 | * sound/ubicom32/ubi32-cs4384.c |
| 3 | * Interface to ubicom32 virtual audio peripheral - using CS4384 DAC |
| 4 | * |
| 5 | * (C) Copyright 2009, Ubicom, Inc. |
| 6 | * |
| 7 | * This file is part of the Ubicom32 Linux Kernel Port. |
| 8 | * |
| 9 | * The Ubicom32 Linux Kernel Port is free software: you can redistribute |
| 10 | * it and/or modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation, either version 2 of the |
| 12 | * License, or (at your option) any later version. |
| 13 | * |
| 14 | * The Ubicom32 Linux Kernel Port is distributed in the hope that it |
| 15 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 16 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with the Ubicom32 Linux Kernel Port. If not, |
| 21 | * see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/moduleparam.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/tlv.h> |
| 30 | #include <sound/control.h> |
| 31 | #include <sound/pcm.h> |
| 32 | #include <sound/initval.h> |
| 33 | #include <asm/ip5000.h> |
| 34 | #include <asm/gpio.h> |
| 35 | #include <asm/audio.h> |
| 36 | #include <asm/ubi32-cs4384.h> |
| 37 | #include "ubi32.h" |
| 38 | |
| 39 | #define DRIVER_NAME "snd-ubi32-cs4384" |
| 40 | |
| 41 | /* |
| 42 | * Module properties |
| 43 | */ |
| 44 | static const struct i2c_device_id snd_ubi32_cs4384_id[] = { |
| 45 | {"cs4384", 0 }, |
| 46 | { } |
| 47 | }; |
| 48 | MODULE_DEVICE_TABLE(i2c, ubicom32audio_id); |
| 49 | |
| 50 | static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ |
| 51 | |
| 52 | /* |
| 53 | * Mixer properties |
| 54 | */ |
| 55 | enum { |
| 56 | /* |
| 57 | * Be careful of changing the order of these IDs, they |
| 58 | * are used to index the volume array. |
| 59 | */ |
| 60 | SND_UBI32_CS4384_FRONT_ID, |
| 61 | SND_UBI32_CS4384_SURROUND_ID, |
| 62 | SND_UBI32_CS4384_CENTER_ID, |
| 63 | SND_UBI32_CS4384_LFE_ID, |
| 64 | SND_UBI32_CS4384_REAR_ID, |
| 65 | |
| 66 | /* |
| 67 | * This should be the last ID |
| 68 | */ |
| 69 | SND_UBI32_CS4384_LAST_ID, |
| 70 | }; |
| 71 | static const u8_t snd_ubi32_cs4384_ch_ofs[] = {0, 2, 4, 5, 6}; |
| 72 | |
| 73 | static const DECLARE_TLV_DB_SCALE(snd_ubi32_cs4384_db, -12750, 50, 0); |
| 74 | |
| 75 | #define snd_ubi32_cs4384_info_mute snd_ctl_boolean_stereo_info |
| 76 | #define snd_ubi32_cs4384_info_mute_mono snd_ctl_boolean_mono_info |
| 77 | |
| 78 | /* |
| 79 | * Mixer controls |
| 80 | */ |
| 81 | static int snd_ubi32_cs4384_info_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo); |
| 82 | static int snd_ubi32_cs4384_get_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol); |
| 83 | static int snd_ubi32_cs4384_put_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol); |
| 84 | static int snd_ubi32_cs4384_get_mute(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol); |
| 85 | static int snd_ubi32_cs4384_put_mute(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol); |
| 86 | |
| 87 | /* |
| 88 | * Make sure to update these if the structure below is changed |
| 89 | */ |
| 90 | #define SND_UBI32_MUTE_CTL_START 5 |
| 91 | #define SND_UBI32_MUTE_CTL_END 9 |
| 92 | static struct snd_kcontrol_new snd_ubi32_cs4384_controls[] __devinitdata = { |
| 93 | { |
| 94 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 95 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 96 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 97 | .name = "Front Playback Volume", |
| 98 | .info = snd_ubi32_cs4384_info_volume, |
| 99 | .get = snd_ubi32_cs4384_get_volume, |
| 100 | .put = snd_ubi32_cs4384_put_volume, |
| 101 | .private_value = SND_UBI32_CS4384_FRONT_ID, |
| 102 | .tlv = { |
| 103 | .p = snd_ubi32_cs4384_db, |
| 104 | }, |
| 105 | }, |
| 106 | { |
| 107 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 108 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 109 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 110 | .name = "Surround Playback Volume", |
| 111 | .info = snd_ubi32_cs4384_info_volume, |
| 112 | .get = snd_ubi32_cs4384_get_volume, |
| 113 | .put = snd_ubi32_cs4384_put_volume, |
| 114 | .private_value = SND_UBI32_CS4384_SURROUND_ID, |
| 115 | .tlv = { |
| 116 | .p = snd_ubi32_cs4384_db, |
| 117 | }, |
| 118 | }, |
| 119 | { |
| 120 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 121 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 122 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 123 | .name = "Center Playback Volume", |
| 124 | .info = snd_ubi32_cs4384_info_volume, |
| 125 | .get = snd_ubi32_cs4384_get_volume, |
| 126 | .put = snd_ubi32_cs4384_put_volume, |
| 127 | .private_value = SND_UBI32_CS4384_CENTER_ID, |
| 128 | .tlv = { |
| 129 | .p = snd_ubi32_cs4384_db, |
| 130 | }, |
| 131 | }, |
| 132 | { |
| 133 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 134 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 135 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 136 | .name = "LFE Playback Volume", |
| 137 | .info = snd_ubi32_cs4384_info_volume, |
| 138 | .get = snd_ubi32_cs4384_get_volume, |
| 139 | .put = snd_ubi32_cs4384_put_volume, |
| 140 | .private_value = SND_UBI32_CS4384_LFE_ID, |
| 141 | .tlv = { |
| 142 | .p = snd_ubi32_cs4384_db, |
| 143 | }, |
| 144 | }, |
| 145 | { |
| 146 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 147 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 148 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 149 | .name = "Rear Playback Volume", |
| 150 | .info = snd_ubi32_cs4384_info_volume, |
| 151 | .get = snd_ubi32_cs4384_get_volume, |
| 152 | .put = snd_ubi32_cs4384_put_volume, |
| 153 | .private_value = SND_UBI32_CS4384_REAR_ID, |
| 154 | .tlv = { |
| 155 | .p = snd_ubi32_cs4384_db, |
| 156 | }, |
| 157 | }, |
| 158 | { |
| 159 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 160 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 161 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 162 | .name = "Front Playback Switch", |
| 163 | .info = snd_ubi32_cs4384_info_mute, |
| 164 | .get = snd_ubi32_cs4384_get_mute, |
| 165 | .put = snd_ubi32_cs4384_put_mute, |
| 166 | .private_value = SND_UBI32_CS4384_FRONT_ID, |
| 167 | }, |
| 168 | { |
| 169 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 170 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 171 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 172 | .name = "Surround Playback Switch", |
| 173 | .info = snd_ubi32_cs4384_info_mute, |
| 174 | .get = snd_ubi32_cs4384_get_mute, |
| 175 | .put = snd_ubi32_cs4384_put_mute, |
| 176 | .private_value = SND_UBI32_CS4384_SURROUND_ID, |
| 177 | }, |
| 178 | { |
| 179 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 180 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 181 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 182 | .name = "Center Playback Switch", |
| 183 | .info = snd_ubi32_cs4384_info_mute_mono, |
| 184 | .get = snd_ubi32_cs4384_get_mute, |
| 185 | .put = snd_ubi32_cs4384_put_mute, |
| 186 | .private_value = SND_UBI32_CS4384_CENTER_ID, |
| 187 | }, |
| 188 | { |
| 189 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 190 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 191 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 192 | .name = "LFE Playback Switch", |
| 193 | .info = snd_ubi32_cs4384_info_mute_mono, |
| 194 | .get = snd_ubi32_cs4384_get_mute, |
| 195 | .put = snd_ubi32_cs4384_put_mute, |
| 196 | .private_value = SND_UBI32_CS4384_LFE_ID, |
| 197 | }, |
| 198 | { |
| 199 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 200 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 201 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 202 | .name = "Rear Playback Switch", |
| 203 | .info = snd_ubi32_cs4384_info_mute, |
| 204 | .get = snd_ubi32_cs4384_get_mute, |
| 205 | .put = snd_ubi32_cs4384_put_mute, |
| 206 | .private_value = SND_UBI32_CS4384_REAR_ID, |
| 207 | }, |
| 208 | }; |
| 209 | |
| 210 | /* |
| 211 | * Our private data |
| 212 | */ |
| 213 | struct snd_ubi32_cs4384_priv { |
| 214 | /* |
| 215 | * Array of current volumes |
| 216 | * (L, R, SL, SR, C, LFE, RL, RR) |
| 217 | */ |
| 218 | uint8_t volume[8]; |
| 219 | |
| 220 | /* |
| 221 | * Bitmask of mutes |
| 222 | * MSB (RR, RL, LFE, C, SR, SL, R, L) LSB |
| 223 | */ |
| 224 | uint8_t mute; |
| 225 | |
| 226 | /* |
| 227 | * Array of controls |
| 228 | */ |
| 229 | struct snd_kcontrol *kctls[ARRAY_SIZE(snd_ubi32_cs4384_controls)]; |
| 230 | |
| 231 | /* |
| 232 | * Lock to protect our card |
| 233 | */ |
| 234 | spinlock_t lock; |
| 235 | }; |
| 236 | |
| 237 | /* |
| 238 | * snd_ubi32_cs4384_info_volume |
| 239 | */ |
| 240 | static int snd_ubi32_cs4384_info_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
| 241 | { |
| 242 | unsigned int id = (unsigned int)kcontrol->private_value; |
| 243 | |
| 244 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 245 | uinfo->count = 1; |
| 246 | if ((id != SND_UBI32_CS4384_LFE_ID) && |
| 247 | (id != SND_UBI32_CS4384_CENTER_ID)) { |
| 248 | uinfo->count = 2; |
| 249 | } |
| 250 | uinfo->value.integer.min = 0; |
| 251 | uinfo->value.integer.max = 255; |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * snd_ubi32_cs4384_get_volume |
| 257 | */ |
| 258 | static int snd_ubi32_cs4384_get_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
| 259 | { |
| 260 | struct ubi32_snd_priv *priv = snd_kcontrol_chip(kcontrol); |
| 261 | struct snd_ubi32_cs4384_priv *cs4384_priv; |
| 262 | unsigned int id = (unsigned int)kcontrol->private_value; |
| 263 | int ch = snd_ubi32_cs4384_ch_ofs[id]; |
| 264 | unsigned long flags; |
| 265 | |
| 266 | if (id >= SND_UBI32_CS4384_LAST_ID) { |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | cs4384_priv = snd_ubi32_priv_get_drv(priv); |
| 271 | |
| 272 | spin_lock_irqsave(&cs4384_priv->lock, flags); |
| 273 | |
| 274 | ucontrol->value.integer.value[0] = cs4384_priv->volume[ch]; |
| 275 | if ((id != SND_UBI32_CS4384_LFE_ID) && |
| 276 | (id != SND_UBI32_CS4384_CENTER_ID)) { |
| 277 | ch++; |
| 278 | ucontrol->value.integer.value[1] = cs4384_priv->volume[ch]; |
| 279 | } |
| 280 | |
| 281 | spin_unlock_irqrestore(&cs4384_priv->lock, flags); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * snd_ubi32_cs4384_put_volume |
| 288 | */ |
| 289 | static int snd_ubi32_cs4384_put_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
| 290 | { |
| 291 | struct ubi32_snd_priv *priv = snd_kcontrol_chip(kcontrol); |
| 292 | struct i2c_client *client = (struct i2c_client *)priv->client; |
| 293 | struct snd_ubi32_cs4384_priv *cs4384_priv; |
| 294 | unsigned int id = (unsigned int)kcontrol->private_value; |
| 295 | int ch = snd_ubi32_cs4384_ch_ofs[id]; |
| 296 | unsigned long flags; |
| 297 | unsigned char send[3]; |
| 298 | int nch; |
| 299 | int ret = -EINVAL; |
| 300 | |
| 301 | if (id >= SND_UBI32_CS4384_LAST_ID) { |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | cs4384_priv = snd_ubi32_priv_get_drv(priv); |
| 306 | |
| 307 | spin_lock_irqsave(&cs4384_priv->lock, flags); |
| 308 | |
| 309 | send[0] = 0; |
| 310 | switch (id) { |
| 311 | case SND_UBI32_CS4384_REAR_ID: |
| 312 | send[0] = 0x06; |
| 313 | |
| 314 | /* |
| 315 | * Fall through |
| 316 | */ |
| 317 | |
| 318 | case SND_UBI32_CS4384_SURROUND_ID: |
| 319 | send[0] += 0x03; |
| 320 | |
| 321 | /* |
| 322 | * Fall through |
| 323 | */ |
| 324 | |
| 325 | case SND_UBI32_CS4384_FRONT_ID: |
| 326 | send[0] += 0x8B; |
| 327 | nch = 2; |
| 328 | send[1] = 255 - (ucontrol->value.integer.value[0] & 0xFF); |
| 329 | send[2] = 255 - (ucontrol->value.integer.value[1] & 0xFF); |
| 330 | cs4384_priv->volume[ch++] = send[1]; |
| 331 | cs4384_priv->volume[ch] = send[2]; |
| 332 | break; |
| 333 | |
| 334 | case SND_UBI32_CS4384_LFE_ID: |
| 335 | send[0] = 0x81; |
| 336 | |
| 337 | /* |
| 338 | * Fall through |
| 339 | */ |
| 340 | |
| 341 | case SND_UBI32_CS4384_CENTER_ID: |
| 342 | send[0] += 0x11; |
| 343 | nch = 1; |
| 344 | send[1] = 255 - (ucontrol->value.integer.value[0] & 0xFF); |
| 345 | cs4384_priv->volume[ch] = send[1]; |
| 346 | break; |
| 347 | |
| 348 | default: |
| 349 | spin_unlock_irqrestore(&cs4384_priv->lock, flags); |
| 350 | goto done; |
| 351 | |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Send the volume to the chip |
| 356 | */ |
| 357 | nch++; |
| 358 | ret = i2c_master_send(client, send, nch); |
| 359 | if (ret != nch) { |
| 360 | snd_printk(KERN_ERR "Failed to set volume on CS4384\n"); |
| 361 | } |
| 362 | |
| 363 | done: |
| 364 | spin_unlock_irqrestore(&cs4384_priv->lock, flags); |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * snd_ubi32_cs4384_get_mute |
| 371 | */ |
| 372 | static int snd_ubi32_cs4384_get_mute(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
| 373 | { |
| 374 | struct ubi32_snd_priv *priv = snd_kcontrol_chip(kcontrol); |
| 375 | struct snd_ubi32_cs4384_priv *cs4384_priv; |
| 376 | unsigned int id = (unsigned int)kcontrol->private_value; |
| 377 | int ch = snd_ubi32_cs4384_ch_ofs[id]; |
| 378 | unsigned long flags; |
| 379 | |
| 380 | if (id >= SND_UBI32_CS4384_LAST_ID) { |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | |
| 384 | cs4384_priv = snd_ubi32_priv_get_drv(priv); |
| 385 | |
| 386 | spin_lock_irqsave(&cs4384_priv->lock, flags); |
| 387 | |
| 388 | ucontrol->value.integer.value[0] = !(cs4384_priv->mute & (1 << ch)); |
| 389 | |
| 390 | if ((id != SND_UBI32_CS4384_LFE_ID) && |
| 391 | (id != SND_UBI32_CS4384_CENTER_ID)) { |
| 392 | ch++; |
| 393 | ucontrol->value.integer.value[1] = !(cs4384_priv->mute & (1 << ch)); |
| 394 | } |
| 395 | |
| 396 | spin_unlock_irqrestore(&cs4384_priv->lock, flags); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * snd_ubi32_cs4384_put_mute |
| 403 | */ |
| 404 | static int snd_ubi32_cs4384_put_mute(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
| 405 | { |
| 406 | struct ubi32_snd_priv *priv = snd_kcontrol_chip(kcontrol); |
| 407 | struct i2c_client *client = (struct i2c_client *)priv->client; |
| 408 | struct snd_ubi32_cs4384_priv *cs4384_priv; |
| 409 | unsigned int id = (unsigned int)kcontrol->private_value; |
| 410 | int ch = snd_ubi32_cs4384_ch_ofs[id]; |
| 411 | unsigned long flags; |
| 412 | unsigned char send[2]; |
| 413 | int ret = -EINVAL; |
| 414 | |
| 415 | if (id >= SND_UBI32_CS4384_LAST_ID) { |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
| 419 | cs4384_priv = snd_ubi32_priv_get_drv(priv); |
| 420 | |
| 421 | spin_lock_irqsave(&cs4384_priv->lock, flags); |
| 422 | |
| 423 | if (ucontrol->value.integer.value[0]) { |
| 424 | cs4384_priv->mute &= ~(1 << ch); |
| 425 | } else { |
| 426 | cs4384_priv->mute |= (1 << ch); |
| 427 | } |
| 428 | |
| 429 | if ((id != SND_UBI32_CS4384_LFE_ID) && (id != SND_UBI32_CS4384_CENTER_ID)) { |
| 430 | ch++; |
| 431 | if (ucontrol->value.integer.value[1]) { |
| 432 | cs4384_priv->mute &= ~(1 << ch); |
| 433 | } else { |
| 434 | cs4384_priv->mute |= (1 << ch); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Update the chip's mute reigster |
| 440 | */ |
| 441 | send[0] = 0x09; |
| 442 | send[1] = cs4384_priv->mute; |
| 443 | ret = i2c_master_send(client, send, 2); |
| 444 | if (ret != 2) { |
| 445 | snd_printk(KERN_ERR "Failed to set mute on CS4384\n"); |
| 446 | } |
| 447 | |
| 448 | spin_unlock_irqrestore(&cs4384_priv->lock, flags); |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * snd_ubi32_cs4384_mixer |
| 455 | * Setup the mixer controls |
| 456 | */ |
| 457 | static int __devinit snd_ubi32_cs4384_mixer(struct ubi32_snd_priv *priv) |
| 458 | { |
| 459 | struct snd_card *card = priv->card; |
| 460 | struct snd_ubi32_cs4384_priv *cs4384_priv; |
| 461 | int i; |
| 462 | |
| 463 | cs4384_priv = snd_ubi32_priv_get_drv(priv); |
| 464 | for (i = 0; i < ARRAY_SIZE(snd_ubi32_cs4384_controls); i++) { |
| 465 | int err; |
| 466 | |
| 467 | cs4384_priv->kctls[i] = snd_ctl_new1(&snd_ubi32_cs4384_controls[i], priv); |
| 468 | err = snd_ctl_add(card, cs4384_priv->kctls[i]); |
| 469 | if (err) { |
| 470 | snd_printk(KERN_WARNING "Failed to add control %d\n", i); |
| 471 | return err; |
| 472 | } |
| 473 | } |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * snd_ubi32_cs4384_free |
| 479 | * Card private data free function |
| 480 | */ |
| 481 | void snd_ubi32_cs4384_free(struct snd_card *card) |
| 482 | { |
| 483 | struct snd_ubi32_cs4384_priv *cs4384_priv; |
| 484 | struct ubi32_snd_priv *ubi32_priv; |
| 485 | |
| 486 | ubi32_priv = card->private_data; |
| 487 | cs4384_priv = snd_ubi32_priv_get_drv(ubi32_priv); |
| 488 | if (cs4384_priv) { |
| 489 | kfree(cs4384_priv); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * snd_ubi32_cs4384_setup_mclk |
| 495 | */ |
| 496 | static int snd_ubi32_cs4384_setup_mclk(struct ubi32_cs4384_platform_data *pdata) |
| 497 | { |
| 498 | struct ubicom32_io_port *ioa = (struct ubicom32_io_port *)RA; |
| 499 | struct ubicom32_io_port *ioc = (struct ubicom32_io_port *)RC; |
| 500 | struct ubicom32_io_port *iod = (struct ubicom32_io_port *)RD; |
| 501 | struct ubicom32_io_port *ioe = (struct ubicom32_io_port *)RE; |
| 502 | struct ubicom32_io_port *ioh = (struct ubicom32_io_port *)RH; |
| 503 | unsigned int ctl0; |
| 504 | unsigned int ctlx; |
| 505 | unsigned int div; |
| 506 | |
| 507 | div = pdata->mclk_entries[0].div; |
| 508 | |
| 509 | ctl0 = (1 << 13); |
| 510 | ctlx = ((div - 1) << 16) | (div / 2); |
| 511 | |
| 512 | switch (pdata->mclk_src) { |
| 513 | case UBI32_CS4384_MCLK_PWM_0: |
| 514 | ioc->function |= 2; |
| 515 | ioc->ctl0 |= ctl0; |
| 516 | ioc->ctl1 = ctlx; |
| 517 | if (!ioa->function) { |
| 518 | ioa->function = 3; |
| 519 | } |
| 520 | return 0; |
| 521 | |
| 522 | case UBI32_CS4384_MCLK_PWM_1: |
| 523 | ioc->function |= 2; |
| 524 | ioc->ctl0 |= ctl0 << 16; |
| 525 | ioc->ctl2 = ctlx; |
| 526 | if (!ioe->function) { |
| 527 | ioe->function = 3; |
| 528 | } |
| 529 | return 0; |
| 530 | |
| 531 | case UBI32_CS4384_MCLK_PWM_2: |
| 532 | ioh->ctl0 |= ctl0; |
| 533 | ioh->ctl1 = ctlx; |
| 534 | if (!iod->function) { |
| 535 | iod->function = 3; |
| 536 | } |
| 537 | return 0; |
| 538 | |
| 539 | case UBI32_CS4384_MCLK_CLKDIV_1: |
| 540 | ioa->gpio_mask &= (1 << 7); |
| 541 | ioa->ctl1 &= ~(0x7F << 14); |
| 542 | ioa->ctl1 |= ((div - 1) << 14); |
| 543 | return 0; |
| 544 | |
| 545 | case UBI32_CS4384_MCLK_OTHER: |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * snd_ubi32_cs4384_set_rate |
| 554 | */ |
| 555 | static int snd_ubi32_cs4384_set_rate(struct ubi32_snd_priv *priv, int rate) |
| 556 | { |
| 557 | struct ubi32_cs4384_platform_data *cpd = priv->pdata->priv_data; |
| 558 | struct ubicom32_io_port *ioa = (struct ubicom32_io_port *)RA; |
| 559 | struct ubicom32_io_port *ioc = (struct ubicom32_io_port *)RC; |
| 560 | struct ubicom32_io_port *ioh = (struct ubicom32_io_port *)RH; |
| 561 | unsigned int ctl; |
| 562 | unsigned int div = 0; |
| 563 | const u16_t mult[] = {64, 96, 128, 192, 256, 384, 512, 768, 1024}; |
| 564 | int i; |
| 565 | int j; |
| 566 | |
| 567 | |
| 568 | for (i = 0; i < sizeof(mult) / sizeof(u16_t); i++) { |
| 569 | for (j = 0; j < cpd->n_mclk; j++) { |
| 570 | if (((unsigned int)rate * (unsigned int)mult[i]) == |
| 571 | cpd->mclk_entries[j].rate) { |
| 572 | div = cpd->mclk_entries[j].div; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | ctl = ((div - 1) << 16) | (div / 2); |
| 579 | |
| 580 | switch (cpd->mclk_src) { |
| 581 | case UBI32_CS4384_MCLK_PWM_0: |
| 582 | ioc->ctl1 = ctl; |
| 583 | return 0; |
| 584 | |
| 585 | case UBI32_CS4384_MCLK_PWM_1: |
| 586 | ioc->ctl2 = ctl; |
| 587 | return 0; |
| 588 | |
| 589 | case UBI32_CS4384_MCLK_PWM_2: |
| 590 | ioh->ctl1 = ctl; |
| 591 | return 0; |
| 592 | |
| 593 | case UBI32_CS4384_MCLK_CLKDIV_1: |
| 594 | ioa->ctl1 &= ~(0x7F << 14); |
| 595 | ioa->ctl1 |= ((div - 1) << 14); |
| 596 | return 0; |
| 597 | |
| 598 | case UBI32_CS4384_MCLK_OTHER: |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | return 1; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * snd_ubi32_cs4384_set_channels |
| 607 | * Mute unused channels |
| 608 | */ |
| 609 | static int snd_ubi32_cs4384_set_channels(struct ubi32_snd_priv *priv, int channels) |
| 610 | { |
| 611 | struct i2c_client *client = (struct i2c_client *)priv->client; |
| 612 | struct snd_ubi32_cs4384_priv *cs4384_priv; |
| 613 | unsigned char send[2]; |
| 614 | int ret; |
| 615 | int i; |
| 616 | unsigned long flags; |
| 617 | |
| 618 | /* |
| 619 | * Only support 0, 2, 4, 6, 8 channels |
| 620 | */ |
| 621 | if ((channels > 8) || (channels & 1)) { |
| 622 | return -EINVAL; |
| 623 | } |
| 624 | |
| 625 | cs4384_priv = snd_ubi32_priv_get_drv(priv); |
| 626 | spin_lock_irqsave(&cs4384_priv->lock, flags); |
| 627 | |
| 628 | /* |
| 629 | * Address 09h, Mute control |
| 630 | */ |
| 631 | send[0] = 0x09; |
| 632 | send[1] = (unsigned char)(0xFF << channels); |
| 633 | |
| 634 | ret = i2c_master_send(client, send, 2); |
| 635 | |
| 636 | spin_unlock_irqrestore(&cs4384_priv->lock, flags); |
| 637 | |
| 638 | /* |
| 639 | * Notify the system that we changed the mutes |
| 640 | */ |
| 641 | cs4384_priv->mute = (unsigned char)(0xFF << channels); |
| 642 | |
| 643 | for (i = SND_UBI32_MUTE_CTL_START; i < SND_UBI32_MUTE_CTL_END; i++) { |
| 644 | snd_ctl_notify(priv->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 645 | &cs4384_priv->kctls[i]->id); |
| 646 | } |
| 647 | |
| 648 | if (ret != 2) { |
| 649 | return -ENXIO; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * snd_ubi32_cs4384_dac_init |
| 657 | */ |
| 658 | static int snd_ubi32_cs4384_dac_init(struct i2c_client *client, const struct i2c_device_id *id) |
| 659 | { |
| 660 | int ret; |
| 661 | unsigned char send[2]; |
| 662 | unsigned char recv[2]; |
| 663 | |
| 664 | /* |
| 665 | * Initialize the CS4384 DAC over the I2C interface |
| 666 | */ |
| 667 | snd_printk(KERN_INFO "Initializing CS4384 DAC\n"); |
| 668 | |
| 669 | /* |
| 670 | * Register 0x01: device/revid |
| 671 | */ |
| 672 | send[0] = 0x01; |
| 673 | ret = i2c_master_send(client, send, 1); |
| 674 | if (ret != 1) { |
| 675 | snd_printk(KERN_ERR "Failed 1st attempt to write to CS4384 register 0x01\n"); |
| 676 | goto fail; |
| 677 | } |
| 678 | ret = i2c_master_recv(client, recv, 1); |
| 679 | if (ret != 1) { |
| 680 | snd_printk(KERN_ERR "Failed initial read of CS4384 registers\n"); |
| 681 | goto fail; |
| 682 | } |
| 683 | snd_printk(KERN_INFO "CS4384 DAC Device/Rev: %08x\n", recv[0]); |
| 684 | |
| 685 | /* |
| 686 | * Register 0x02: Mode Control 1 |
| 687 | * Control Port Enable, PCM, All DACs enabled, Power Down |
| 688 | */ |
| 689 | send[0] = 0x02; |
| 690 | send[1] = 0x81; |
| 691 | ret = i2c_master_send(client, send, 2); |
| 692 | if (ret != 2) { |
| 693 | snd_printk(KERN_ERR "Failed to set CPEN CS4384\n"); |
| 694 | goto fail; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Register 0x08: Ramp and Mute |
| 699 | * RMP_UP, RMP_DN, PAMUTE, DAMUTE |
| 700 | */ |
| 701 | send[0] = 0x08; |
| 702 | send[1] = 0xBC; |
| 703 | ret = i2c_master_send(client, send, 2); |
| 704 | if (ret != 2) { |
| 705 | snd_printk(KERN_ERR "Failed to set CPEN CS4384\n"); |
| 706 | goto fail; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * Register 0x03: PCM Control |
| 711 | * I2S DIF[3:0] = 0001, no De-Emphasis, Auto speed mode |
| 712 | */ |
| 713 | send[0] = 0x03; |
| 714 | send[1] = 0x13; |
| 715 | ret = i2c_master_send(client, send, 2); |
| 716 | if (ret != 2) { |
| 717 | snd_printk(KERN_ERR "Failed to set CS4384 to I2S mode\n"); |
| 718 | goto fail; |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Register 0x0B/0x0C: Volume control A1/B1 |
| 723 | * Register 0x0E/0x0F: Volume control A2/B2 |
| 724 | * Register 0x11/0x12: Volume control A3/B3 |
| 725 | * Register 0x14/0x15: Volume control A4/B4 |
| 726 | */ |
| 727 | send[0] = 0x80 | 0x0B; |
| 728 | send[1] = 0x00; |
| 729 | send[2] = 0x00; |
| 730 | ret = i2c_master_send(client, send, 3); |
| 731 | if (ret != 3) { |
| 732 | snd_printk(KERN_ERR "Failed to set ch1 volume on CS4384\n"); |
| 733 | goto fail; |
| 734 | } |
| 735 | |
| 736 | send[0] = 0x80 | 0x0E; |
| 737 | send[1] = 0x00; |
| 738 | send[2] = 0x00; |
| 739 | ret = i2c_master_send(client, send, 3); |
| 740 | if (ret != 3) { |
| 741 | snd_printk(KERN_ERR "Failed to set ch2 volume on CS4384\n"); |
| 742 | goto fail; |
| 743 | } |
| 744 | |
| 745 | send[0] = 0x80 | 0x11; |
| 746 | send[1] = 0x00; |
| 747 | send[2] = 0x00; |
| 748 | ret = i2c_master_send(client, send, 3); |
| 749 | if (ret != 3) { |
| 750 | snd_printk(KERN_ERR "Failed to set ch3 volume on CS4384\n"); |
| 751 | goto fail; |
| 752 | } |
| 753 | |
| 754 | send[0] = 0x80 | 0x14; |
| 755 | send[1] = 0x00; |
| 756 | send[2] = 0x00; |
| 757 | ret = i2c_master_send(client, send, 3); |
| 758 | if (ret != 3) { |
| 759 | snd_printk(KERN_ERR "Failed to set ch4 volume on CS4384\n"); |
| 760 | goto fail; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Register 09h: Mute control |
| 765 | * Mute all (we will unmute channels as needed) |
| 766 | */ |
| 767 | send[0] = 0x09; |
| 768 | send[1] = 0xFF; |
| 769 | ret = i2c_master_send(client, send, 2); |
| 770 | if (ret != 2) { |
| 771 | snd_printk(KERN_ERR "Failed to power up CS4384\n"); |
| 772 | goto fail; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Register 0x02: Mode Control 1 |
| 777 | * Control Port Enable, PCM, All DACs enabled, Power Up |
| 778 | */ |
| 779 | send[0] = 0x02; |
| 780 | send[1] = 0x80; |
| 781 | ret = i2c_master_send(client, send, 2); |
| 782 | if (ret != 2) { |
| 783 | snd_printk(KERN_ERR "Failed to power up CS4384\n"); |
| 784 | goto fail; |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Make sure the changes took place, this helps verify we are talking to |
| 789 | * the correct chip. |
| 790 | */ |
| 791 | send[0] = 0x80 | 0x03; |
| 792 | ret = i2c_master_send(client, send, 1); |
| 793 | if (ret != 1) { |
| 794 | snd_printk(KERN_ERR "Failed to initiate readback\n"); |
| 795 | goto fail; |
| 796 | } |
| 797 | |
| 798 | ret = i2c_master_recv(client, recv, 1); |
| 799 | if (ret != 1) { |
| 800 | snd_printk(KERN_ERR "Failed second read of CS4384 registers\n"); |
| 801 | goto fail; |
| 802 | } |
| 803 | |
| 804 | if (recv[0] != 0x13) { |
| 805 | snd_printk(KERN_ERR "Failed to initialize CS4384 DAC\n"); |
| 806 | goto fail; |
| 807 | } |
| 808 | |
| 809 | snd_printk(KERN_INFO "CS4384 DAC Initialized\n"); |
| 810 | return 0; |
| 811 | |
| 812 | fail: |
| 813 | return -ENODEV; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * snd_ubi32_cs4384_i2c_probe |
| 818 | */ |
| 819 | static int snd_ubi32_cs4384_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 820 | { |
| 821 | struct snd_card *card; |
| 822 | struct ubi32_snd_priv *ubi32_priv; |
| 823 | int err, ret; |
| 824 | struct platform_device *pdev; |
| 825 | struct ubi32_cs4384_platform_data *pdata; |
| 826 | struct snd_ubi32_cs4384_priv *cs4384_priv; |
| 827 | |
| 828 | /* |
| 829 | * pdev is audio device |
| 830 | */ |
| 831 | pdev = client->dev.platform_data; |
| 832 | if (!pdev) { |
| 833 | return -ENODEV; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * pdev->dev.platform_data is ubi32-pcm platform_data |
| 838 | */ |
| 839 | pdata = audio_device_priv(pdev); |
| 840 | if (!pdata) { |
| 841 | return -ENODEV; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Initialize the CS4384 DAC |
| 846 | */ |
| 847 | ret = snd_ubi32_cs4384_dac_init(client, id); |
| 848 | if (ret < 0) { |
| 849 | /* |
| 850 | * Initialization failed. Propagate the error. |
| 851 | */ |
| 852 | return ret; |
| 853 | } |
| 854 | |
| 855 | if (snd_ubi32_cs4384_setup_mclk(pdata)) { |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Create a snd_card structure |
| 861 | */ |
| 862 | card = snd_card_new(index, "Ubi32-CS4384", THIS_MODULE, sizeof(struct ubi32_snd_priv)); |
| 863 | if (card == NULL) { |
| 864 | return -ENOMEM; |
| 865 | } |
| 866 | |
| 867 | card->private_free = snd_ubi32_cs4384_free; |
| 868 | ubi32_priv = card->private_data; |
| 869 | |
| 870 | /* |
| 871 | * Initialize the snd_card's private data structure |
| 872 | */ |
| 873 | ubi32_priv->card = card; |
| 874 | ubi32_priv->client = client; |
| 875 | ubi32_priv->set_channels = snd_ubi32_cs4384_set_channels; |
| 876 | ubi32_priv->set_rate = snd_ubi32_cs4384_set_rate; |
| 877 | |
| 878 | /* |
| 879 | * CS4384 DAC has a minimum sample rate of 4khz and an |
| 880 | * upper limit of 216khz for it's auto-detect. |
| 881 | */ |
| 882 | ubi32_priv->min_sample_rate = 4000; |
| 883 | ubi32_priv->max_sample_rate = 216000; |
| 884 | |
| 885 | /* |
| 886 | * Create our private data (to manage volume, etc) |
| 887 | */ |
| 888 | cs4384_priv = kzalloc(sizeof(struct snd_ubi32_cs4384_priv), GFP_KERNEL); |
| 889 | if (!cs4384_priv) { |
| 890 | snd_card_free(card); |
| 891 | return -ENOMEM; |
| 892 | } |
| 893 | snd_ubi32_priv_set_drv(ubi32_priv, cs4384_priv); |
| 894 | spin_lock_init(&cs4384_priv->lock); |
| 895 | |
| 896 | /* |
| 897 | * We start off all muted and max volume |
| 898 | */ |
| 899 | cs4384_priv->mute = 0xFF; |
| 900 | memset(cs4384_priv->volume, 0xFF, 8); |
| 901 | |
| 902 | /* |
| 903 | * Create the new PCM instance |
| 904 | */ |
| 905 | err = snd_ubi32_pcm_probe(ubi32_priv, pdev); |
| 906 | if (err < 0) { |
| 907 | snd_card_free(card); |
| 908 | return err; /* What is err? Need to include correct file */ |
| 909 | } |
| 910 | |
| 911 | strcpy(card->driver, "Ubi32-CS4384"); |
| 912 | strcpy(card->shortname, "Ubi32-CS4384"); |
| 913 | snprintf(card->longname, sizeof(card->longname), |
| 914 | "%s at sendirq=%d.%d recvirq=%d.%d regs=%p", |
| 915 | card->shortname, ubi32_priv->tx_irq, ubi32_priv->irq_idx, |
| 916 | ubi32_priv->rx_irq, ubi32_priv->irq_idx, ubi32_priv->adr); |
| 917 | |
| 918 | snd_card_set_dev(card, &client->dev); |
| 919 | |
| 920 | /* |
| 921 | * Set up the mixer |
| 922 | */ |
| 923 | snd_ubi32_cs4384_mixer(ubi32_priv); |
| 924 | |
| 925 | /* |
| 926 | * Register the sound card |
| 927 | */ |
| 928 | if ((err = snd_card_register(card)) != 0) { |
| 929 | snd_printk(KERN_INFO "snd_card_register error\n"); |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Store card for access from other methods |
| 934 | */ |
| 935 | i2c_set_clientdata(client, card); |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * snd_ubi32_cs4384_i2c_remove |
| 942 | */ |
| 943 | static int __devexit snd_ubi32_cs4384_i2c_remove(struct i2c_client *client) |
| 944 | { |
| 945 | struct snd_card *card; |
| 946 | struct ubi32_snd_priv *ubi32_priv; |
| 947 | |
| 948 | card = i2c_get_clientdata(client); |
| 949 | |
| 950 | ubi32_priv = card->private_data; |
| 951 | snd_ubi32_pcm_remove(ubi32_priv); |
| 952 | |
| 953 | snd_card_free(i2c_get_clientdata(client)); |
| 954 | i2c_set_clientdata(client, NULL); |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | /* |
| 960 | * I2C driver description |
| 961 | */ |
| 962 | static struct i2c_driver snd_ubi32_cs4384_driver = { |
| 963 | .driver = { |
| 964 | .name = DRIVER_NAME, |
| 965 | .owner = THIS_MODULE, |
| 966 | }, |
| 967 | .id_table = snd_ubi32_cs4384_id, |
| 968 | .probe = snd_ubi32_cs4384_i2c_probe, |
| 969 | .remove = __devexit_p(snd_ubi32_cs4384_i2c_remove), |
| 970 | }; |
| 971 | |
| 972 | /* |
| 973 | * Driver init |
| 974 | */ |
| 975 | static int __init snd_ubi32_cs4384_init(void) |
| 976 | { |
| 977 | return i2c_add_driver(&snd_ubi32_cs4384_driver); |
| 978 | } |
| 979 | module_init(snd_ubi32_cs4384_init); |
| 980 | |
| 981 | /* |
| 982 | * snd_ubi32_cs4384_exit |
| 983 | */ |
| 984 | static void __exit snd_ubi32_cs4384_exit(void) |
| 985 | { |
| 986 | i2c_del_driver(&snd_ubi32_cs4384_driver); |
| 987 | } |
| 988 | module_exit(snd_ubi32_cs4384_exit); |
| 989 | |
| 990 | /* |
| 991 | * Module properties |
| 992 | */ |
| 993 | MODULE_ALIAS("i2c:" DRIVER_NAME); |
| 994 | MODULE_AUTHOR("Patrick Tjin"); |
| 995 | MODULE_DESCRIPTION("Driver for Ubicom32 audio devices CS4384"); |
| 996 | MODULE_LICENSE("GPL"); |
| 997 | |