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 #include <media/v4l2-ctrls.h> 44 45 #define DRIVER_VERSION "0.0.2" 46 47 #define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>" 48 #define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones." 49 50 #define PINFO(format, ...)\ 51 printk(KERN_INFO KBUILD_MODNAME ": "\ 52 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__) 53 #define PWARN(format, ...)\ 54 printk(KERN_WARNING KBUILD_MODNAME ": "\ 55 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__) 56 # define PDEBUG(format, ...)\ 57 printk(KERN_DEBUG KBUILD_MODNAME ": "\ 58 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__) 59 60 /* Frequency limits in MHz -- these are European values. For Japanese 61 devices, that would be 76000 and 91000. */ 62 #define FREQ_MIN 87500 63 #define FREQ_MAX 108000 64 #define FREQ_MUL 16 65 66 /* TEA5764 registers */ 67 #define TEA5764_MANID 0x002b 68 #define TEA5764_CHIPID 0x5764 69 70 #define TEA5764_INTREG_BLMSK 0x0001 71 #define TEA5764_INTREG_FRRMSK 0x0002 72 #define TEA5764_INTREG_LEVMSK 0x0008 73 #define TEA5764_INTREG_IFMSK 0x0010 74 #define TEA5764_INTREG_BLMFLAG 0x0100 75 #define TEA5764_INTREG_FRRFLAG 0x0200 76 #define TEA5764_INTREG_LEVFLAG 0x0800 77 #define TEA5764_INTREG_IFFLAG 0x1000 78 79 #define TEA5764_FRQSET_SUD 0x8000 80 #define TEA5764_FRQSET_SM 0x4000 81 82 #define TEA5764_TNCTRL_PUPD1 0x8000 83 #define TEA5764_TNCTRL_PUPD0 0x4000 84 #define TEA5764_TNCTRL_BLIM 0x2000 85 #define TEA5764_TNCTRL_SWPM 0x1000 86 #define TEA5764_TNCTRL_IFCTC 0x0800 87 #define TEA5764_TNCTRL_AFM 0x0400 88 #define TEA5764_TNCTRL_SMUTE 0x0200 89 #define TEA5764_TNCTRL_SNC 0x0100 90 #define TEA5764_TNCTRL_MU 0x0080 91 #define TEA5764_TNCTRL_SSL1 0x0040 92 #define TEA5764_TNCTRL_SSL0 0x0020 93 #define TEA5764_TNCTRL_HLSI 0x0010 94 #define TEA5764_TNCTRL_MST 0x0008 95 #define TEA5764_TNCTRL_SWP 0x0004 96 #define TEA5764_TNCTRL_DTC 0x0002 97 #define TEA5764_TNCTRL_AHLSI 0x0001 98 99 #define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4) 100 #define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9) 101 #define TEA5764_TUNCHK_TUNTO 0x0100 102 #define TEA5764_TUNCHK_LD 0x0008 103 #define TEA5764_TUNCHK_STEREO 0x0004 104 105 #define TEA5764_TESTREG_TRIGFR 0x0800 106 107 struct tea5764_regs { 108 u16 intreg; /* INTFLAG & INTMSK */ 109 u16 frqset; /* FRQSETMSB & FRQSETLSB */ 110 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */ 111 u16 frqchk; /* FRQCHKMSB & FRQCHKLSB */ 112 u16 tunchk; /* IFCHK & LEVCHK */ 113 u16 testreg; /* TESTBITS & TESTMODE */ 114 u16 rdsstat; /* RDSSTAT1 & RDSSTAT2 */ 115 u16 rdslb; /* RDSLBMSB & RDSLBLSB */ 116 u16 rdspb; /* RDSPBMSB & RDSPBLSB */ 117 u16 rdsbc; /* RDSBBC & RDSGBC */ 118 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */ 119 u16 rdsbbl; /* PAUSEDET & RDSBBL */ 120 u16 manid; /* MANID1 & MANID2 */ 121 u16 chipid; /* CHIPID1 & CHIPID2 */ 122 } __attribute__ ((packed)); 123 124 struct tea5764_write_regs { 125 u8 intreg; /* INTMSK */ 126 u16 frqset; /* FRQSETMSB & FRQSETLSB */ 127 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */ 128 u16 testreg; /* TESTBITS & TESTMODE */ 129 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */ 130 u16 rdsbbl; /* PAUSEDET & RDSBBL */ 131 } __attribute__ ((packed)); 132 133 #ifdef CONFIG_RADIO_TEA5764_XTAL 134 #define RADIO_TEA5764_XTAL 1 135 #else 136 #define RADIO_TEA5764_XTAL 0 137 #endif 138 139 static int radio_nr = -1; 140 static int use_xtal = RADIO_TEA5764_XTAL; 141 142 struct tea5764_device { 143 struct v4l2_device v4l2_dev; 144 struct v4l2_ctrl_handler ctrl_handler; 145 struct i2c_client *i2c_client; 146 struct video_device vdev; 147 struct tea5764_regs regs; 148 struct mutex mutex; 149 }; 150 151 /* I2C code related */ 152 static int tea5764_i2c_read(struct tea5764_device *radio) 153 { 154 int i; 155 u16 *p = (u16 *) &radio->regs; 156 157 struct i2c_msg msgs[1] = { 158 { .addr = radio->i2c_client->addr, 159 .flags = I2C_M_RD, 160 .len = sizeof(radio->regs), 161 .buf = (void *)&radio->regs 162 }, 163 }; 164 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1) 165 return -EIO; 166 for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++) 167 p[i] = __be16_to_cpu(p[i]); 168 169 return 0; 170 } 171 172 static int tea5764_i2c_write(struct tea5764_device *radio) 173 { 174 struct tea5764_write_regs wr; 175 struct tea5764_regs *r = &radio->regs; 176 struct i2c_msg msgs[1] = { 177 { 178 .addr = radio->i2c_client->addr, 179 .len = sizeof(wr), 180 .buf = (void *)&wr 181 }, 182 }; 183 wr.intreg = r->intreg & 0xff; 184 wr.frqset = __cpu_to_be16(r->frqset); 185 wr.tnctrl = __cpu_to_be16(r->tnctrl); 186 wr.testreg = __cpu_to_be16(r->testreg); 187 wr.rdsctrl = __cpu_to_be16(r->rdsctrl); 188 wr.rdsbbl = __cpu_to_be16(r->rdsbbl); 189 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1) 190 return -EIO; 191 return 0; 192 } 193 194 static void tea5764_power_up(struct tea5764_device *radio) 195 { 196 struct tea5764_regs *r = &radio->regs; 197 198 if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) { 199 r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU | 200 TEA5764_TNCTRL_HLSI); 201 if (!use_xtal) 202 r->testreg |= TEA5764_TESTREG_TRIGFR; 203 else 204 r->testreg &= ~TEA5764_TESTREG_TRIGFR; 205 206 r->tnctrl |= TEA5764_TNCTRL_PUPD0; 207 tea5764_i2c_write(radio); 208 } 209 } 210 211 static void tea5764_power_down(struct tea5764_device *radio) 212 { 213 struct tea5764_regs *r = &radio->regs; 214 215 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) { 216 r->tnctrl &= ~TEA5764_TNCTRL_PUPD0; 217 tea5764_i2c_write(radio); 218 } 219 } 220 221 static void tea5764_set_freq(struct tea5764_device *radio, int freq) 222 { 223 struct tea5764_regs *r = &radio->regs; 224 225 /* formula: (freq [+ or -] 225000) / 8192 */ 226 if (r->tnctrl & TEA5764_TNCTRL_HLSI) 227 r->frqset = (freq + 225000) / 8192; 228 else 229 r->frqset = (freq - 225000) / 8192; 230 } 231 232 static int tea5764_get_freq(struct tea5764_device *radio) 233 { 234 struct tea5764_regs *r = &radio->regs; 235 236 if (r->tnctrl & TEA5764_TNCTRL_HLSI) 237 return (r->frqchk * 8192) - 225000; 238 else 239 return (r->frqchk * 8192) + 225000; 240 } 241 242 /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */ 243 static void tea5764_tune(struct tea5764_device *radio, int freq) 244 { 245 tea5764_set_freq(radio, freq); 246 if (tea5764_i2c_write(radio)) 247 PWARN("Could not set frequency!"); 248 } 249 250 static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode) 251 { 252 struct tea5764_regs *r = &radio->regs; 253 int tnctrl = r->tnctrl; 254 255 if (audmode == V4L2_TUNER_MODE_MONO) 256 r->tnctrl |= TEA5764_TNCTRL_MST; 257 else 258 r->tnctrl &= ~TEA5764_TNCTRL_MST; 259 if (tnctrl != r->tnctrl) 260 tea5764_i2c_write(radio); 261 } 262 263 static int tea5764_get_audout_mode(struct tea5764_device *radio) 264 { 265 struct tea5764_regs *r = &radio->regs; 266 267 if (r->tnctrl & TEA5764_TNCTRL_MST) 268 return V4L2_TUNER_MODE_MONO; 269 else 270 return V4L2_TUNER_MODE_STEREO; 271 } 272 273 static void tea5764_mute(struct tea5764_device *radio, int on) 274 { 275 struct tea5764_regs *r = &radio->regs; 276 int tnctrl = r->tnctrl; 277 278 if (on) 279 r->tnctrl |= TEA5764_TNCTRL_MU; 280 else 281 r->tnctrl &= ~TEA5764_TNCTRL_MU; 282 if (tnctrl != r->tnctrl) 283 tea5764_i2c_write(radio); 284 } 285 286 /* V4L2 vidioc */ 287 static int vidioc_querycap(struct file *file, void *priv, 288 struct v4l2_capability *v) 289 { 290 struct tea5764_device *radio = video_drvdata(file); 291 struct video_device *dev = &radio->vdev; 292 293 strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver)); 294 strlcpy(v->card, dev->name, sizeof(v->card)); 295 snprintf(v->bus_info, sizeof(v->bus_info), 296 "I2C:%s", dev_name(&dev->dev)); 297 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; 298 return 0; 299 } 300 301 static int vidioc_g_tuner(struct file *file, void *priv, 302 struct v4l2_tuner *v) 303 { 304 struct tea5764_device *radio = video_drvdata(file); 305 struct tea5764_regs *r = &radio->regs; 306 307 if (v->index > 0) 308 return -EINVAL; 309 310 memset(v, 0, sizeof(*v)); 311 strcpy(v->name, "FM"); 312 v->type = V4L2_TUNER_RADIO; 313 tea5764_i2c_read(radio); 314 v->rangelow = FREQ_MIN * FREQ_MUL; 315 v->rangehigh = FREQ_MAX * FREQ_MUL; 316 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; 317 if (r->tunchk & TEA5764_TUNCHK_STEREO) 318 v->rxsubchans = V4L2_TUNER_SUB_STEREO; 319 else 320 v->rxsubchans = V4L2_TUNER_SUB_MONO; 321 v->audmode = tea5764_get_audout_mode(radio); 322 v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf; 323 v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk); 324 325 return 0; 326 } 327 328 static int vidioc_s_tuner(struct file *file, void *priv, 329 const struct v4l2_tuner *v) 330 { 331 struct tea5764_device *radio = video_drvdata(file); 332 333 if (v->index > 0) 334 return -EINVAL; 335 336 tea5764_set_audout_mode(radio, v->audmode); 337 return 0; 338 } 339 340 static int vidioc_s_frequency(struct file *file, void *priv, 341 const struct v4l2_frequency *f) 342 { 343 struct tea5764_device *radio = video_drvdata(file); 344 345 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) 346 return -EINVAL; 347 if (f->frequency == 0) { 348 /* We special case this as a power down control. */ 349 tea5764_power_down(radio); 350 } 351 if (f->frequency < (FREQ_MIN * FREQ_MUL)) 352 return -EINVAL; 353 if (f->frequency > (FREQ_MAX * FREQ_MUL)) 354 return -EINVAL; 355 tea5764_power_up(radio); 356 tea5764_tune(radio, (f->frequency * 125) / 2); 357 return 0; 358 } 359 360 static int vidioc_g_frequency(struct file *file, void *priv, 361 struct v4l2_frequency *f) 362 { 363 struct tea5764_device *radio = video_drvdata(file); 364 struct tea5764_regs *r = &radio->regs; 365 366 if (f->tuner != 0) 367 return -EINVAL; 368 tea5764_i2c_read(radio); 369 memset(f, 0, sizeof(*f)); 370 f->type = V4L2_TUNER_RADIO; 371 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) 372 f->frequency = (tea5764_get_freq(radio) * 2) / 125; 373 else 374 f->frequency = 0; 375 376 return 0; 377 } 378 379 static int tea5764_s_ctrl(struct v4l2_ctrl *ctrl) 380 { 381 struct tea5764_device *radio = 382 container_of(ctrl->handler, struct tea5764_device, ctrl_handler); 383 384 switch (ctrl->id) { 385 case V4L2_CID_AUDIO_MUTE: 386 tea5764_mute(radio, ctrl->val); 387 return 0; 388 } 389 return -EINVAL; 390 } 391 392 static const struct v4l2_ctrl_ops tea5764_ctrl_ops = { 393 .s_ctrl = tea5764_s_ctrl, 394 }; 395 396 /* File system interface */ 397 static const struct v4l2_file_operations tea5764_fops = { 398 .owner = THIS_MODULE, 399 .unlocked_ioctl = video_ioctl2, 400 }; 401 402 static const struct v4l2_ioctl_ops tea5764_ioctl_ops = { 403 .vidioc_querycap = vidioc_querycap, 404 .vidioc_g_tuner = vidioc_g_tuner, 405 .vidioc_s_tuner = vidioc_s_tuner, 406 .vidioc_g_frequency = vidioc_g_frequency, 407 .vidioc_s_frequency = vidioc_s_frequency, 408 }; 409 410 /* V4L2 interface */ 411 static struct video_device tea5764_radio_template = { 412 .name = "TEA5764 FM-Radio", 413 .fops = &tea5764_fops, 414 .ioctl_ops = &tea5764_ioctl_ops, 415 .release = video_device_release_empty, 416 }; 417 418 /* I2C probe: check if the device exists and register with v4l if it is */ 419 static int tea5764_i2c_probe(struct i2c_client *client, 420 const struct i2c_device_id *id) 421 { 422 struct tea5764_device *radio; 423 struct v4l2_device *v4l2_dev; 424 struct v4l2_ctrl_handler *hdl; 425 struct tea5764_regs *r; 426 int ret; 427 428 PDEBUG("probe"); 429 radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL); 430 if (!radio) 431 return -ENOMEM; 432 433 v4l2_dev = &radio->v4l2_dev; 434 ret = v4l2_device_register(&client->dev, v4l2_dev); 435 if (ret < 0) { 436 v4l2_err(v4l2_dev, "could not register v4l2_device\n"); 437 goto errfr; 438 } 439 440 hdl = &radio->ctrl_handler; 441 v4l2_ctrl_handler_init(hdl, 1); 442 v4l2_ctrl_new_std(hdl, &tea5764_ctrl_ops, 443 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); 444 v4l2_dev->ctrl_handler = hdl; 445 if (hdl->error) { 446 ret = hdl->error; 447 v4l2_err(v4l2_dev, "Could not register controls\n"); 448 goto errunreg; 449 } 450 451 mutex_init(&radio->mutex); 452 radio->i2c_client = client; 453 ret = tea5764_i2c_read(radio); 454 if (ret) 455 goto errunreg; 456 r = &radio->regs; 457 PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid); 458 if (r->chipid != TEA5764_CHIPID || 459 (r->manid & 0x0fff) != TEA5764_MANID) { 460 PWARN("This chip is not a TEA5764!"); 461 ret = -EINVAL; 462 goto errunreg; 463 } 464 465 radio->vdev = tea5764_radio_template; 466 467 i2c_set_clientdata(client, radio); 468 video_set_drvdata(&radio->vdev, radio); 469 radio->vdev.lock = &radio->mutex; 470 radio->vdev.v4l2_dev = v4l2_dev; 471 472 /* initialize and power off the chip */ 473 tea5764_i2c_read(radio); 474 tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO); 475 tea5764_mute(radio, 1); 476 tea5764_power_down(radio); 477 478 ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr); 479 if (ret < 0) { 480 PWARN("Could not register video device!"); 481 goto errunreg; 482 } 483 484 PINFO("registered."); 485 return 0; 486 errunreg: 487 v4l2_ctrl_handler_free(hdl); 488 v4l2_device_unregister(v4l2_dev); 489 errfr: 490 kfree(radio); 491 return ret; 492 } 493 494 static int tea5764_i2c_remove(struct i2c_client *client) 495 { 496 struct tea5764_device *radio = i2c_get_clientdata(client); 497 498 PDEBUG("remove"); 499 if (radio) { 500 tea5764_power_down(radio); 501 video_unregister_device(&radio->vdev); 502 v4l2_ctrl_handler_free(&radio->ctrl_handler); 503 v4l2_device_unregister(&radio->v4l2_dev); 504 kfree(radio); 505 } 506 return 0; 507 } 508 509 /* I2C subsystem interface */ 510 static const struct i2c_device_id tea5764_id[] = { 511 { "radio-tea5764", 0 }, 512 { } /* Terminating entry */ 513 }; 514 MODULE_DEVICE_TABLE(i2c, tea5764_id); 515 516 static struct i2c_driver tea5764_i2c_driver = { 517 .driver = { 518 .name = "radio-tea5764", 519 .owner = THIS_MODULE, 520 }, 521 .probe = tea5764_i2c_probe, 522 .remove = tea5764_i2c_remove, 523 .id_table = tea5764_id, 524 }; 525 526 module_i2c_driver(tea5764_i2c_driver); 527 528 MODULE_AUTHOR(DRIVER_AUTHOR); 529 MODULE_DESCRIPTION(DRIVER_DESC); 530 MODULE_LICENSE("GPL"); 531 MODULE_VERSION(DRIVER_VERSION); 532 533 module_param(use_xtal, int, 0); 534 MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board"); 535 module_param(radio_nr, int, 0); 536 MODULE_PARM_DESC(radio_nr, "video4linux device number to use"); 537