1 /* 2 * driver/media/radio/radio-tea5764.c 3 * 4 * Driver for TEA5764 radio chip for linux 2.6. 5 * This driver is for TEA5764 chip from NXP, used in EZX phones from Motorola. 6 * The I2C protocol is used for communicate with chip. 7 * 8 * Based in radio-tea5761.c Copyright (C) 2005 Nokia Corporation 9 * 10 * Copyright (c) 2008 Fabio Belavenuto <belavenuto@gmail.com> 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 * 26 * History: 27 * 2008-12-06 Fabio Belavenuto <belavenuto@gmail.com> 28 * initial code 29 * 30 * TODO: 31 * add platform_data support for IRQs platform dependencies 32 * add RDS support 33 */ 34 #include <linux/kernel.h> 35 #include <linux/slab.h> 36 #include <linux/module.h> 37 #include <linux/init.h> /* Initdata */ 38 #include <linux/videodev2.h> /* kernel radio structs */ 39 #include <linux/i2c.h> /* I2C */ 40 #include <media/v4l2-common.h> 41 #include <media/v4l2-ioctl.h> 42 #include <media/v4l2-device.h> 43 44 #define DRIVER_VERSION "0.0.2" 45 46 #define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>" 47 #define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones." 48 49 #define PINFO(format, ...)\ 50 printk(KERN_INFO KBUILD_MODNAME ": "\ 51 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__) 52 #define PWARN(format, ...)\ 53 printk(KERN_WARNING KBUILD_MODNAME ": "\ 54 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__) 55 # define PDEBUG(format, ...)\ 56 printk(KERN_DEBUG KBUILD_MODNAME ": "\ 57 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__) 58 59 /* Frequency limits in MHz -- these are European values. For Japanese 60 devices, that would be 76000 and 91000. */ 61 #define FREQ_MIN 87500 62 #define FREQ_MAX 108000 63 #define FREQ_MUL 16 64 65 /* TEA5764 registers */ 66 #define TEA5764_MANID 0x002b 67 #define TEA5764_CHIPID 0x5764 68 69 #define TEA5764_INTREG_BLMSK 0x0001 70 #define TEA5764_INTREG_FRRMSK 0x0002 71 #define TEA5764_INTREG_LEVMSK 0x0008 72 #define TEA5764_INTREG_IFMSK 0x0010 73 #define TEA5764_INTREG_BLMFLAG 0x0100 74 #define TEA5764_INTREG_FRRFLAG 0x0200 75 #define TEA5764_INTREG_LEVFLAG 0x0800 76 #define TEA5764_INTREG_IFFLAG 0x1000 77 78 #define TEA5764_FRQSET_SUD 0x8000 79 #define TEA5764_FRQSET_SM 0x4000 80 81 #define TEA5764_TNCTRL_PUPD1 0x8000 82 #define TEA5764_TNCTRL_PUPD0 0x4000 83 #define TEA5764_TNCTRL_BLIM 0x2000 84 #define TEA5764_TNCTRL_SWPM 0x1000 85 #define TEA5764_TNCTRL_IFCTC 0x0800 86 #define TEA5764_TNCTRL_AFM 0x0400 87 #define TEA5764_TNCTRL_SMUTE 0x0200 88 #define TEA5764_TNCTRL_SNC 0x0100 89 #define TEA5764_TNCTRL_MU 0x0080 90 #define TEA5764_TNCTRL_SSL1 0x0040 91 #define TEA5764_TNCTRL_SSL0 0x0020 92 #define TEA5764_TNCTRL_HLSI 0x0010 93 #define TEA5764_TNCTRL_MST 0x0008 94 #define TEA5764_TNCTRL_SWP 0x0004 95 #define TEA5764_TNCTRL_DTC 0x0002 96 #define TEA5764_TNCTRL_AHLSI 0x0001 97 98 #define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4) 99 #define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9) 100 #define TEA5764_TUNCHK_TUNTO 0x0100 101 #define TEA5764_TUNCHK_LD 0x0008 102 #define TEA5764_TUNCHK_STEREO 0x0004 103 104 #define TEA5764_TESTREG_TRIGFR 0x0800 105 106 struct tea5764_regs { 107 u16 intreg; /* INTFLAG & INTMSK */ 108 u16 frqset; /* FRQSETMSB & FRQSETLSB */ 109 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */ 110 u16 frqchk; /* FRQCHKMSB & FRQCHKLSB */ 111 u16 tunchk; /* IFCHK & LEVCHK */ 112 u16 testreg; /* TESTBITS & TESTMODE */ 113 u16 rdsstat; /* RDSSTAT1 & RDSSTAT2 */ 114 u16 rdslb; /* RDSLBMSB & RDSLBLSB */ 115 u16 rdspb; /* RDSPBMSB & RDSPBLSB */ 116 u16 rdsbc; /* RDSBBC & RDSGBC */ 117 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */ 118 u16 rdsbbl; /* PAUSEDET & RDSBBL */ 119 u16 manid; /* MANID1 & MANID2 */ 120 u16 chipid; /* CHIPID1 & CHIPID2 */ 121 } __attribute__ ((packed)); 122 123 struct tea5764_write_regs { 124 u8 intreg; /* INTMSK */ 125 u16 frqset; /* FRQSETMSB & FRQSETLSB */ 126 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */ 127 u16 testreg; /* TESTBITS & TESTMODE */ 128 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */ 129 u16 rdsbbl; /* PAUSEDET & RDSBBL */ 130 } __attribute__ ((packed)); 131 132 #ifdef CONFIG_RADIO_TEA5764_XTAL 133 #define RADIO_TEA5764_XTAL 1 134 #else 135 #define RADIO_TEA5764_XTAL 0 136 #endif 137 138 static int radio_nr = -1; 139 static int use_xtal = RADIO_TEA5764_XTAL; 140 141 struct tea5764_device { 142 struct v4l2_device v4l2_dev; 143 struct i2c_client *i2c_client; 144 struct video_device vdev; 145 struct tea5764_regs regs; 146 struct mutex mutex; 147 }; 148 149 /* I2C code related */ 150 static int tea5764_i2c_read(struct tea5764_device *radio) 151 { 152 int i; 153 u16 *p = (u16 *) &radio->regs; 154 155 struct i2c_msg msgs[1] = { 156 { .addr = radio->i2c_client->addr, 157 .flags = I2C_M_RD, 158 .len = sizeof(radio->regs), 159 .buf = (void *)&radio->regs 160 }, 161 }; 162 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1) 163 return -EIO; 164 for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++) 165 p[i] = __be16_to_cpu(p[i]); 166 167 return 0; 168 } 169 170 static int tea5764_i2c_write(struct tea5764_device *radio) 171 { 172 struct tea5764_write_regs wr; 173 struct tea5764_regs *r = &radio->regs; 174 struct i2c_msg msgs[1] = { 175 { 176 .addr = radio->i2c_client->addr, 177 .len = sizeof(wr), 178 .buf = (void *)&wr 179 }, 180 }; 181 wr.intreg = r->intreg & 0xff; 182 wr.frqset = __cpu_to_be16(r->frqset); 183 wr.tnctrl = __cpu_to_be16(r->tnctrl); 184 wr.testreg = __cpu_to_be16(r->testreg); 185 wr.rdsctrl = __cpu_to_be16(r->rdsctrl); 186 wr.rdsbbl = __cpu_to_be16(r->rdsbbl); 187 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1) 188 return -EIO; 189 return 0; 190 } 191 192 /* V4L2 code related */ 193 static struct v4l2_queryctrl radio_qctrl[] = { 194 { 195 .id = V4L2_CID_AUDIO_MUTE, 196 .name = "Mute", 197 .minimum = 0, 198 .maximum = 1, 199 .default_value = 1, 200 .type = V4L2_CTRL_TYPE_BOOLEAN, 201 } 202 }; 203 204 static void tea5764_power_up(struct tea5764_device *radio) 205 { 206 struct tea5764_regs *r = &radio->regs; 207 208 if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) { 209 r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU | 210 TEA5764_TNCTRL_HLSI); 211 if (!use_xtal) 212 r->testreg |= TEA5764_TESTREG_TRIGFR; 213 else 214 r->testreg &= ~TEA5764_TESTREG_TRIGFR; 215 216 r->tnctrl |= TEA5764_TNCTRL_PUPD0; 217 tea5764_i2c_write(radio); 218 } 219 } 220 221 static void tea5764_power_down(struct tea5764_device *radio) 222 { 223 struct tea5764_regs *r = &radio->regs; 224 225 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) { 226 r->tnctrl &= ~TEA5764_TNCTRL_PUPD0; 227 tea5764_i2c_write(radio); 228 } 229 } 230 231 static void tea5764_set_freq(struct tea5764_device *radio, int freq) 232 { 233 struct tea5764_regs *r = &radio->regs; 234 235 /* formula: (freq [+ or -] 225000) / 8192 */ 236 if (r->tnctrl & TEA5764_TNCTRL_HLSI) 237 r->frqset = (freq + 225000) / 8192; 238 else 239 r->frqset = (freq - 225000) / 8192; 240 } 241 242 static int tea5764_get_freq(struct tea5764_device *radio) 243 { 244 struct tea5764_regs *r = &radio->regs; 245 246 if (r->tnctrl & TEA5764_TNCTRL_HLSI) 247 return (r->frqchk * 8192) - 225000; 248 else 249 return (r->frqchk * 8192) + 225000; 250 } 251 252 /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */ 253 static void tea5764_tune(struct tea5764_device *radio, int freq) 254 { 255 tea5764_set_freq(radio, freq); 256 if (tea5764_i2c_write(radio)) 257 PWARN("Could not set frequency!"); 258 } 259 260 static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode) 261 { 262 struct tea5764_regs *r = &radio->regs; 263 int tnctrl = r->tnctrl; 264 265 if (audmode == V4L2_TUNER_MODE_MONO) 266 r->tnctrl |= TEA5764_TNCTRL_MST; 267 else 268 r->tnctrl &= ~TEA5764_TNCTRL_MST; 269 if (tnctrl != r->tnctrl) 270 tea5764_i2c_write(radio); 271 } 272 273 static int tea5764_get_audout_mode(struct tea5764_device *radio) 274 { 275 struct tea5764_regs *r = &radio->regs; 276 277 if (r->tnctrl & TEA5764_TNCTRL_MST) 278 return V4L2_TUNER_MODE_MONO; 279 else 280 return V4L2_TUNER_MODE_STEREO; 281 } 282 283 static void tea5764_mute(struct tea5764_device *radio, int on) 284 { 285 struct tea5764_regs *r = &radio->regs; 286 int tnctrl = r->tnctrl; 287 288 if (on) 289 r->tnctrl |= TEA5764_TNCTRL_MU; 290 else 291 r->tnctrl &= ~TEA5764_TNCTRL_MU; 292 if (tnctrl != r->tnctrl) 293 tea5764_i2c_write(radio); 294 } 295 296 static int tea5764_is_muted(struct tea5764_device *radio) 297 { 298 return radio->regs.tnctrl & TEA5764_TNCTRL_MU; 299 } 300 301 /* V4L2 vidioc */ 302 static int vidioc_querycap(struct file *file, void *priv, 303 struct v4l2_capability *v) 304 { 305 struct tea5764_device *radio = video_drvdata(file); 306 struct video_device *dev = &radio->vdev; 307 308 strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver)); 309 strlcpy(v->card, dev->name, sizeof(v->card)); 310 snprintf(v->bus_info, sizeof(v->bus_info), 311 "I2C:%s", dev_name(&dev->dev)); 312 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; 313 return 0; 314 } 315 316 static int vidioc_g_tuner(struct file *file, void *priv, 317 struct v4l2_tuner *v) 318 { 319 struct tea5764_device *radio = video_drvdata(file); 320 struct tea5764_regs *r = &radio->regs; 321 322 if (v->index > 0) 323 return -EINVAL; 324 325 memset(v, 0, sizeof(*v)); 326 strcpy(v->name, "FM"); 327 v->type = V4L2_TUNER_RADIO; 328 tea5764_i2c_read(radio); 329 v->rangelow = FREQ_MIN * FREQ_MUL; 330 v->rangehigh = FREQ_MAX * FREQ_MUL; 331 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; 332 if (r->tunchk & TEA5764_TUNCHK_STEREO) 333 v->rxsubchans = V4L2_TUNER_SUB_STEREO; 334 else 335 v->rxsubchans = V4L2_TUNER_SUB_MONO; 336 v->audmode = tea5764_get_audout_mode(radio); 337 v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf; 338 v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk); 339 340 return 0; 341 } 342 343 static int vidioc_s_tuner(struct file *file, void *priv, 344 const struct v4l2_tuner *v) 345 { 346 struct tea5764_device *radio = video_drvdata(file); 347 348 if (v->index > 0) 349 return -EINVAL; 350 351 tea5764_set_audout_mode(radio, v->audmode); 352 return 0; 353 } 354 355 static int vidioc_s_frequency(struct file *file, void *priv, 356 const struct v4l2_frequency *f) 357 { 358 struct tea5764_device *radio = video_drvdata(file); 359 360 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) 361 return -EINVAL; 362 if (f->frequency == 0) { 363 /* We special case this as a power down control. */ 364 tea5764_power_down(radio); 365 } 366 if (f->frequency < (FREQ_MIN * FREQ_MUL)) 367 return -EINVAL; 368 if (f->frequency > (FREQ_MAX * FREQ_MUL)) 369 return -EINVAL; 370 tea5764_power_up(radio); 371 tea5764_tune(radio, (f->frequency * 125) / 2); 372 return 0; 373 } 374 375 static int vidioc_g_frequency(struct file *file, void *priv, 376 struct v4l2_frequency *f) 377 { 378 struct tea5764_device *radio = video_drvdata(file); 379 struct tea5764_regs *r = &radio->regs; 380 381 if (f->tuner != 0) 382 return -EINVAL; 383 tea5764_i2c_read(radio); 384 memset(f, 0, sizeof(*f)); 385 f->type = V4L2_TUNER_RADIO; 386 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) 387 f->frequency = (tea5764_get_freq(radio) * 2) / 125; 388 else 389 f->frequency = 0; 390 391 return 0; 392 } 393 394 static int vidioc_queryctrl(struct file *file, void *priv, 395 struct v4l2_queryctrl *qc) 396 { 397 int i; 398 399 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) { 400 if (qc->id && qc->id == radio_qctrl[i].id) { 401 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc)); 402 return 0; 403 } 404 } 405 return -EINVAL; 406 } 407 408 static int vidioc_g_ctrl(struct file *file, void *priv, 409 struct v4l2_control *ctrl) 410 { 411 struct tea5764_device *radio = video_drvdata(file); 412 413 switch (ctrl->id) { 414 case V4L2_CID_AUDIO_MUTE: 415 tea5764_i2c_read(radio); 416 ctrl->value = tea5764_is_muted(radio) ? 1 : 0; 417 return 0; 418 } 419 return -EINVAL; 420 } 421 422 static int vidioc_s_ctrl(struct file *file, void *priv, 423 struct v4l2_control *ctrl) 424 { 425 struct tea5764_device *radio = video_drvdata(file); 426 427 switch (ctrl->id) { 428 case V4L2_CID_AUDIO_MUTE: 429 tea5764_mute(radio, ctrl->value); 430 return 0; 431 } 432 return -EINVAL; 433 } 434 435 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) 436 { 437 *i = 0; 438 return 0; 439 } 440 441 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) 442 { 443 if (i != 0) 444 return -EINVAL; 445 return 0; 446 } 447 448 static int vidioc_g_audio(struct file *file, void *priv, 449 struct v4l2_audio *a) 450 { 451 if (a->index > 1) 452 return -EINVAL; 453 454 strcpy(a->name, "Radio"); 455 a->capability = V4L2_AUDCAP_STEREO; 456 return 0; 457 } 458 459 static int vidioc_s_audio(struct file *file, void *priv, 460 const struct v4l2_audio *a) 461 { 462 if (a->index != 0) 463 return -EINVAL; 464 465 return 0; 466 } 467 468 /* File system interface */ 469 static const struct v4l2_file_operations tea5764_fops = { 470 .owner = THIS_MODULE, 471 .unlocked_ioctl = video_ioctl2, 472 }; 473 474 static const struct v4l2_ioctl_ops tea5764_ioctl_ops = { 475 .vidioc_querycap = vidioc_querycap, 476 .vidioc_g_tuner = vidioc_g_tuner, 477 .vidioc_s_tuner = vidioc_s_tuner, 478 .vidioc_g_audio = vidioc_g_audio, 479 .vidioc_s_audio = vidioc_s_audio, 480 .vidioc_g_input = vidioc_g_input, 481 .vidioc_s_input = vidioc_s_input, 482 .vidioc_g_frequency = vidioc_g_frequency, 483 .vidioc_s_frequency = vidioc_s_frequency, 484 .vidioc_queryctrl = vidioc_queryctrl, 485 .vidioc_g_ctrl = vidioc_g_ctrl, 486 .vidioc_s_ctrl = vidioc_s_ctrl, 487 }; 488 489 /* V4L2 interface */ 490 static struct video_device tea5764_radio_template = { 491 .name = "TEA5764 FM-Radio", 492 .fops = &tea5764_fops, 493 .ioctl_ops = &tea5764_ioctl_ops, 494 .release = video_device_release_empty, 495 }; 496 497 /* I2C probe: check if the device exists and register with v4l if it is */ 498 static int tea5764_i2c_probe(struct i2c_client *client, 499 const struct i2c_device_id *id) 500 { 501 struct tea5764_device *radio; 502 struct v4l2_device *v4l2_dev; 503 struct tea5764_regs *r; 504 int ret; 505 506 PDEBUG("probe"); 507 radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL); 508 if (!radio) 509 return -ENOMEM; 510 511 v4l2_dev = &radio->v4l2_dev; 512 ret = v4l2_device_register(&client->dev, v4l2_dev); 513 if (ret < 0) { 514 v4l2_err(v4l2_dev, "could not register v4l2_device\n"); 515 goto errfr; 516 } 517 mutex_init(&radio->mutex); 518 radio->i2c_client = client; 519 ret = tea5764_i2c_read(radio); 520 if (ret) 521 goto errunreg; 522 r = &radio->regs; 523 PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid); 524 if (r->chipid != TEA5764_CHIPID || 525 (r->manid & 0x0fff) != TEA5764_MANID) { 526 PWARN("This chip is not a TEA5764!"); 527 ret = -EINVAL; 528 goto errunreg; 529 } 530 531 radio->vdev = tea5764_radio_template; 532 533 i2c_set_clientdata(client, radio); 534 video_set_drvdata(&radio->vdev, radio); 535 radio->vdev.lock = &radio->mutex; 536 radio->vdev.v4l2_dev = v4l2_dev; 537 538 /* initialize and power off the chip */ 539 tea5764_i2c_read(radio); 540 tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO); 541 tea5764_mute(radio, 1); 542 tea5764_power_down(radio); 543 544 ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr); 545 if (ret < 0) { 546 PWARN("Could not register video device!"); 547 goto errunreg; 548 } 549 550 PINFO("registered."); 551 return 0; 552 errunreg: 553 v4l2_device_unregister(v4l2_dev); 554 errfr: 555 kfree(radio); 556 return ret; 557 } 558 559 static int tea5764_i2c_remove(struct i2c_client *client) 560 { 561 struct tea5764_device *radio = i2c_get_clientdata(client); 562 563 PDEBUG("remove"); 564 if (radio) { 565 tea5764_power_down(radio); 566 video_unregister_device(&radio->vdev); 567 v4l2_device_unregister(&radio->v4l2_dev); 568 kfree(radio); 569 } 570 return 0; 571 } 572 573 /* I2C subsystem interface */ 574 static const struct i2c_device_id tea5764_id[] = { 575 { "radio-tea5764", 0 }, 576 { } /* Terminating entry */ 577 }; 578 MODULE_DEVICE_TABLE(i2c, tea5764_id); 579 580 static struct i2c_driver tea5764_i2c_driver = { 581 .driver = { 582 .name = "radio-tea5764", 583 .owner = THIS_MODULE, 584 }, 585 .probe = tea5764_i2c_probe, 586 .remove = tea5764_i2c_remove, 587 .id_table = tea5764_id, 588 }; 589 590 module_i2c_driver(tea5764_i2c_driver); 591 592 MODULE_AUTHOR(DRIVER_AUTHOR); 593 MODULE_DESCRIPTION(DRIVER_DESC); 594 MODULE_LICENSE("GPL"); 595 MODULE_VERSION(DRIVER_VERSION); 596 597 module_param(use_xtal, int, 0); 598 MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board"); 599 module_param(radio_nr, int, 0); 600 MODULE_PARM_DESC(radio_nr, "video4linux device number to use"); 601