Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * driver/media/radio/radio-tea5764.c
0004  *
0005  * Driver for TEA5764 radio chip for linux 2.6.
0006  * This driver is for TEA5764 chip from NXP, used in EZX phones from Motorola.
0007  * The I2C protocol is used for communicate with chip.
0008  *
0009  * Based in radio-tea5761.c Copyright (C) 2005 Nokia Corporation
0010  *
0011  *  Copyright (c) 2008 Fabio Belavenuto <belavenuto@gmail.com>
0012  *
0013  * History:
0014  * 2008-12-06   Fabio Belavenuto <belavenuto@gmail.com>
0015  *              initial code
0016  *
0017  * TODO:
0018  *  add platform_data support for IRQs platform dependencies
0019  *  add RDS support
0020  */
0021 #include <linux/kernel.h>
0022 #include <linux/slab.h>
0023 #include <linux/module.h>
0024 #include <linux/init.h>         /* Initdata         */
0025 #include <linux/videodev2.h>        /* kernel radio structs     */
0026 #include <linux/i2c.h>          /* I2C              */
0027 #include <media/v4l2-common.h>
0028 #include <media/v4l2-ioctl.h>
0029 #include <media/v4l2-device.h>
0030 #include <media/v4l2-ctrls.h>
0031 #include <media/v4l2-event.h>
0032 
0033 #define DRIVER_VERSION  "0.0.2"
0034 
0035 #define DRIVER_AUTHOR   "Fabio Belavenuto <belavenuto@gmail.com>"
0036 #define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
0037 
0038 #define PINFO(format, ...)\
0039     printk(KERN_INFO KBUILD_MODNAME ": "\
0040         DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
0041 #define PWARN(format, ...)\
0042     printk(KERN_WARNING KBUILD_MODNAME ": "\
0043         DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
0044 # define PDEBUG(format, ...)\
0045     printk(KERN_DEBUG KBUILD_MODNAME ": "\
0046         DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
0047 
0048 /* Frequency limits in MHz -- these are European values.  For Japanese
0049 devices, that would be 76000 and 91000.  */
0050 #define FREQ_MIN  87500U
0051 #define FREQ_MAX 108000U
0052 #define FREQ_MUL 16
0053 
0054 /* TEA5764 registers */
0055 #define TEA5764_MANID       0x002b
0056 #define TEA5764_CHIPID      0x5764
0057 
0058 #define TEA5764_INTREG_BLMSK    0x0001
0059 #define TEA5764_INTREG_FRRMSK   0x0002
0060 #define TEA5764_INTREG_LEVMSK   0x0008
0061 #define TEA5764_INTREG_IFMSK    0x0010
0062 #define TEA5764_INTREG_BLMFLAG  0x0100
0063 #define TEA5764_INTREG_FRRFLAG  0x0200
0064 #define TEA5764_INTREG_LEVFLAG  0x0800
0065 #define TEA5764_INTREG_IFFLAG   0x1000
0066 
0067 #define TEA5764_FRQSET_SUD  0x8000
0068 #define TEA5764_FRQSET_SM   0x4000
0069 
0070 #define TEA5764_TNCTRL_PUPD1    0x8000
0071 #define TEA5764_TNCTRL_PUPD0    0x4000
0072 #define TEA5764_TNCTRL_BLIM 0x2000
0073 #define TEA5764_TNCTRL_SWPM 0x1000
0074 #define TEA5764_TNCTRL_IFCTC    0x0800
0075 #define TEA5764_TNCTRL_AFM  0x0400
0076 #define TEA5764_TNCTRL_SMUTE    0x0200
0077 #define TEA5764_TNCTRL_SNC  0x0100
0078 #define TEA5764_TNCTRL_MU   0x0080
0079 #define TEA5764_TNCTRL_SSL1 0x0040
0080 #define TEA5764_TNCTRL_SSL0 0x0020
0081 #define TEA5764_TNCTRL_HLSI 0x0010
0082 #define TEA5764_TNCTRL_MST  0x0008
0083 #define TEA5764_TNCTRL_SWP  0x0004
0084 #define TEA5764_TNCTRL_DTC  0x0002
0085 #define TEA5764_TNCTRL_AHLSI    0x0001
0086 
0087 #define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
0088 #define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
0089 #define TEA5764_TUNCHK_TUNTO    0x0100
0090 #define TEA5764_TUNCHK_LD   0x0008
0091 #define TEA5764_TUNCHK_STEREO   0x0004
0092 
0093 #define TEA5764_TESTREG_TRIGFR  0x0800
0094 
0095 struct tea5764_regs {
0096     u16 intreg;             /* INTFLAG & INTMSK */
0097     u16 frqset;             /* FRQSETMSB & FRQSETLSB */
0098     u16 tnctrl;             /* TNCTRL1 & TNCTRL2 */
0099     u16 frqchk;             /* FRQCHKMSB & FRQCHKLSB */
0100     u16 tunchk;             /* IFCHK & LEVCHK */
0101     u16 testreg;                /* TESTBITS & TESTMODE */
0102     u16 rdsstat;                /* RDSSTAT1 & RDSSTAT2 */
0103     u16 rdslb;              /* RDSLBMSB & RDSLBLSB */
0104     u16 rdspb;              /* RDSPBMSB & RDSPBLSB */
0105     u16 rdsbc;              /* RDSBBC & RDSGBC */
0106     u16 rdsctrl;                /* RDSCTRL1 & RDSCTRL2 */
0107     u16 rdsbbl;             /* PAUSEDET & RDSBBL */
0108     u16 manid;              /* MANID1 & MANID2 */
0109     u16 chipid;             /* CHIPID1 & CHIPID2 */
0110 } __attribute__ ((packed));
0111 
0112 struct tea5764_write_regs {
0113     u8 intreg;              /* INTMSK */
0114     __be16 frqset;              /* FRQSETMSB & FRQSETLSB */
0115     __be16 tnctrl;              /* TNCTRL1 & TNCTRL2 */
0116     __be16 testreg;             /* TESTBITS & TESTMODE */
0117     __be16 rdsctrl;             /* RDSCTRL1 & RDSCTRL2 */
0118     __be16 rdsbbl;              /* PAUSEDET & RDSBBL */
0119 } __attribute__ ((packed));
0120 
0121 #ifdef CONFIG_RADIO_TEA5764_XTAL
0122 #define RADIO_TEA5764_XTAL 1
0123 #else
0124 #define RADIO_TEA5764_XTAL 0
0125 #endif
0126 
0127 static int radio_nr = -1;
0128 static int use_xtal = RADIO_TEA5764_XTAL;
0129 
0130 struct tea5764_device {
0131     struct v4l2_device      v4l2_dev;
0132     struct v4l2_ctrl_handler    ctrl_handler;
0133     struct i2c_client       *i2c_client;
0134     struct video_device     vdev;
0135     struct tea5764_regs     regs;
0136     struct mutex            mutex;
0137 };
0138 
0139 /* I2C code related */
0140 static int tea5764_i2c_read(struct tea5764_device *radio)
0141 {
0142     int i;
0143     u16 *p = (u16 *) &radio->regs;
0144 
0145     struct i2c_msg msgs[1] = {
0146         {   .addr = radio->i2c_client->addr,
0147             .flags = I2C_M_RD,
0148             .len = sizeof(radio->regs),
0149             .buf = (void *)&radio->regs
0150         },
0151     };
0152     if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
0153         return -EIO;
0154     for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
0155         p[i] = __be16_to_cpu((__force __be16)p[i]);
0156 
0157     return 0;
0158 }
0159 
0160 static int tea5764_i2c_write(struct tea5764_device *radio)
0161 {
0162     struct tea5764_write_regs wr;
0163     struct tea5764_regs *r = &radio->regs;
0164     struct i2c_msg msgs[1] = {
0165         {
0166             .addr = radio->i2c_client->addr,
0167             .len = sizeof(wr),
0168             .buf = (void *)&wr
0169         },
0170     };
0171     wr.intreg  = r->intreg & 0xff;
0172     wr.frqset  = __cpu_to_be16(r->frqset);
0173     wr.tnctrl  = __cpu_to_be16(r->tnctrl);
0174     wr.testreg = __cpu_to_be16(r->testreg);
0175     wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
0176     wr.rdsbbl  = __cpu_to_be16(r->rdsbbl);
0177     if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
0178         return -EIO;
0179     return 0;
0180 }
0181 
0182 static void tea5764_power_up(struct tea5764_device *radio)
0183 {
0184     struct tea5764_regs *r = &radio->regs;
0185 
0186     if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
0187         r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
0188                    TEA5764_TNCTRL_HLSI);
0189         if (!use_xtal)
0190             r->testreg |= TEA5764_TESTREG_TRIGFR;
0191         else
0192             r->testreg &= ~TEA5764_TESTREG_TRIGFR;
0193 
0194         r->tnctrl |= TEA5764_TNCTRL_PUPD0;
0195         tea5764_i2c_write(radio);
0196     }
0197 }
0198 
0199 static void tea5764_power_down(struct tea5764_device *radio)
0200 {
0201     struct tea5764_regs *r = &radio->regs;
0202 
0203     if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
0204         r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
0205         tea5764_i2c_write(radio);
0206     }
0207 }
0208 
0209 static void tea5764_set_freq(struct tea5764_device *radio, int freq)
0210 {
0211     struct tea5764_regs *r = &radio->regs;
0212 
0213     /* formula: (freq [+ or -] 225000) / 8192 */
0214     if (r->tnctrl & TEA5764_TNCTRL_HLSI)
0215         r->frqset = (freq + 225000) / 8192;
0216     else
0217         r->frqset = (freq - 225000) / 8192;
0218 }
0219 
0220 static int tea5764_get_freq(struct tea5764_device *radio)
0221 {
0222     struct tea5764_regs *r = &radio->regs;
0223 
0224     if (r->tnctrl & TEA5764_TNCTRL_HLSI)
0225         return (r->frqchk * 8192) - 225000;
0226     else
0227         return (r->frqchk * 8192) + 225000;
0228 }
0229 
0230 /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
0231 static void tea5764_tune(struct tea5764_device *radio, int freq)
0232 {
0233     tea5764_set_freq(radio, freq);
0234     if (tea5764_i2c_write(radio))
0235         PWARN("Could not set frequency!");
0236 }
0237 
0238 static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
0239 {
0240     struct tea5764_regs *r = &radio->regs;
0241     int tnctrl = r->tnctrl;
0242 
0243     if (audmode == V4L2_TUNER_MODE_MONO)
0244         r->tnctrl |= TEA5764_TNCTRL_MST;
0245     else
0246         r->tnctrl &= ~TEA5764_TNCTRL_MST;
0247     if (tnctrl != r->tnctrl)
0248         tea5764_i2c_write(radio);
0249 }
0250 
0251 static int tea5764_get_audout_mode(struct tea5764_device *radio)
0252 {
0253     struct tea5764_regs *r = &radio->regs;
0254 
0255     if (r->tnctrl & TEA5764_TNCTRL_MST)
0256         return V4L2_TUNER_MODE_MONO;
0257     else
0258         return V4L2_TUNER_MODE_STEREO;
0259 }
0260 
0261 static void tea5764_mute(struct tea5764_device *radio, int on)
0262 {
0263     struct tea5764_regs *r = &radio->regs;
0264     int tnctrl = r->tnctrl;
0265 
0266     if (on)
0267         r->tnctrl |= TEA5764_TNCTRL_MU;
0268     else
0269         r->tnctrl &= ~TEA5764_TNCTRL_MU;
0270     if (tnctrl != r->tnctrl)
0271         tea5764_i2c_write(radio);
0272 }
0273 
0274 /* V4L2 vidioc */
0275 static int vidioc_querycap(struct file *file, void  *priv,
0276                     struct v4l2_capability *v)
0277 {
0278     struct tea5764_device *radio = video_drvdata(file);
0279     struct video_device *dev = &radio->vdev;
0280 
0281     strscpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
0282     strscpy(v->card, dev->name, sizeof(v->card));
0283     snprintf(v->bus_info, sizeof(v->bus_info),
0284          "I2C:%s", dev_name(&dev->dev));
0285     return 0;
0286 }
0287 
0288 static int vidioc_g_tuner(struct file *file, void *priv,
0289                 struct v4l2_tuner *v)
0290 {
0291     struct tea5764_device *radio = video_drvdata(file);
0292     struct tea5764_regs *r = &radio->regs;
0293 
0294     if (v->index > 0)
0295         return -EINVAL;
0296 
0297     strscpy(v->name, "FM", sizeof(v->name));
0298     v->type = V4L2_TUNER_RADIO;
0299     tea5764_i2c_read(radio);
0300     v->rangelow   = FREQ_MIN * FREQ_MUL;
0301     v->rangehigh  = FREQ_MAX * FREQ_MUL;
0302     v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
0303     if (r->tunchk & TEA5764_TUNCHK_STEREO)
0304         v->rxsubchans = V4L2_TUNER_SUB_STEREO;
0305     else
0306         v->rxsubchans = V4L2_TUNER_SUB_MONO;
0307     v->audmode = tea5764_get_audout_mode(radio);
0308     v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
0309     v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
0310 
0311     return 0;
0312 }
0313 
0314 static int vidioc_s_tuner(struct file *file, void *priv,
0315                 const struct v4l2_tuner *v)
0316 {
0317     struct tea5764_device *radio = video_drvdata(file);
0318 
0319     if (v->index > 0)
0320         return -EINVAL;
0321 
0322     tea5764_set_audout_mode(radio, v->audmode);
0323     return 0;
0324 }
0325 
0326 static int vidioc_s_frequency(struct file *file, void *priv,
0327                 const struct v4l2_frequency *f)
0328 {
0329     struct tea5764_device *radio = video_drvdata(file);
0330     unsigned freq = f->frequency;
0331 
0332     if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
0333         return -EINVAL;
0334     if (freq == 0) {
0335         /* We special case this as a power down control. */
0336         tea5764_power_down(radio);
0337         /* Yes, that's what is returned in this case. This
0338            whole special case is non-compliant and should really
0339            be replaced with something better, but changing this
0340            might well break code that depends on this behavior.
0341            So we keep it as-is. */
0342         return -EINVAL;
0343     }
0344     freq = clamp(freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
0345     tea5764_power_up(radio);
0346     tea5764_tune(radio, (freq * 125) / 2);
0347     return 0;
0348 }
0349 
0350 static int vidioc_g_frequency(struct file *file, void *priv,
0351                 struct v4l2_frequency *f)
0352 {
0353     struct tea5764_device *radio = video_drvdata(file);
0354     struct tea5764_regs *r = &radio->regs;
0355 
0356     if (f->tuner != 0)
0357         return -EINVAL;
0358     tea5764_i2c_read(radio);
0359     f->type = V4L2_TUNER_RADIO;
0360     if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
0361         f->frequency = (tea5764_get_freq(radio) * 2) / 125;
0362     else
0363         f->frequency = 0;
0364 
0365     return 0;
0366 }
0367 
0368 static int tea5764_s_ctrl(struct v4l2_ctrl *ctrl)
0369 {
0370     struct tea5764_device *radio =
0371         container_of(ctrl->handler, struct tea5764_device, ctrl_handler);
0372 
0373     switch (ctrl->id) {
0374     case V4L2_CID_AUDIO_MUTE:
0375         tea5764_mute(radio, ctrl->val);
0376         return 0;
0377     }
0378     return -EINVAL;
0379 }
0380 
0381 static const struct v4l2_ctrl_ops tea5764_ctrl_ops = {
0382     .s_ctrl = tea5764_s_ctrl,
0383 };
0384 
0385 /* File system interface */
0386 static const struct v4l2_file_operations tea5764_fops = {
0387     .owner      = THIS_MODULE,
0388     .open       = v4l2_fh_open,
0389     .release    = v4l2_fh_release,
0390     .poll       = v4l2_ctrl_poll,
0391     .unlocked_ioctl = video_ioctl2,
0392 };
0393 
0394 static const struct v4l2_ioctl_ops tea5764_ioctl_ops = {
0395     .vidioc_querycap    = vidioc_querycap,
0396     .vidioc_g_tuner     = vidioc_g_tuner,
0397     .vidioc_s_tuner     = vidioc_s_tuner,
0398     .vidioc_g_frequency = vidioc_g_frequency,
0399     .vidioc_s_frequency = vidioc_s_frequency,
0400     .vidioc_log_status  = v4l2_ctrl_log_status,
0401     .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
0402     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
0403 };
0404 
0405 /* V4L2 interface */
0406 static const struct video_device tea5764_radio_template = {
0407     .name       = "TEA5764 FM-Radio",
0408     .fops           = &tea5764_fops,
0409     .ioctl_ops  = &tea5764_ioctl_ops,
0410     .release    = video_device_release_empty,
0411 };
0412 
0413 /* I2C probe: check if the device exists and register with v4l if it is */
0414 static int tea5764_i2c_probe(struct i2c_client *client,
0415                  const struct i2c_device_id *id)
0416 {
0417     struct tea5764_device *radio;
0418     struct v4l2_device *v4l2_dev;
0419     struct v4l2_ctrl_handler *hdl;
0420     struct tea5764_regs *r;
0421     int ret;
0422 
0423     PDEBUG("probe");
0424     radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
0425     if (!radio)
0426         return -ENOMEM;
0427 
0428     v4l2_dev = &radio->v4l2_dev;
0429     ret = v4l2_device_register(&client->dev, v4l2_dev);
0430     if (ret < 0) {
0431         v4l2_err(v4l2_dev, "could not register v4l2_device\n");
0432         goto errfr;
0433     }
0434 
0435     hdl = &radio->ctrl_handler;
0436     v4l2_ctrl_handler_init(hdl, 1);
0437     v4l2_ctrl_new_std(hdl, &tea5764_ctrl_ops,
0438             V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
0439     v4l2_dev->ctrl_handler = hdl;
0440     if (hdl->error) {
0441         ret = hdl->error;
0442         v4l2_err(v4l2_dev, "Could not register controls\n");
0443         goto errunreg;
0444     }
0445 
0446     mutex_init(&radio->mutex);
0447     radio->i2c_client = client;
0448     ret = tea5764_i2c_read(radio);
0449     if (ret)
0450         goto errunreg;
0451     r = &radio->regs;
0452     PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
0453     if (r->chipid != TEA5764_CHIPID ||
0454         (r->manid & 0x0fff) != TEA5764_MANID) {
0455         PWARN("This chip is not a TEA5764!");
0456         ret = -EINVAL;
0457         goto errunreg;
0458     }
0459 
0460     radio->vdev = tea5764_radio_template;
0461 
0462     i2c_set_clientdata(client, radio);
0463     video_set_drvdata(&radio->vdev, radio);
0464     radio->vdev.lock = &radio->mutex;
0465     radio->vdev.v4l2_dev = v4l2_dev;
0466     radio->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
0467 
0468     /* initialize and power off the chip */
0469     tea5764_i2c_read(radio);
0470     tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
0471     tea5764_mute(radio, 1);
0472     tea5764_power_down(radio);
0473 
0474     ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr);
0475     if (ret < 0) {
0476         PWARN("Could not register video device!");
0477         goto errunreg;
0478     }
0479 
0480     PINFO("registered.");
0481     return 0;
0482 errunreg:
0483     v4l2_ctrl_handler_free(hdl);
0484     v4l2_device_unregister(v4l2_dev);
0485 errfr:
0486     kfree(radio);
0487     return ret;
0488 }
0489 
0490 static int tea5764_i2c_remove(struct i2c_client *client)
0491 {
0492     struct tea5764_device *radio = i2c_get_clientdata(client);
0493 
0494     PDEBUG("remove");
0495     if (radio) {
0496         tea5764_power_down(radio);
0497         video_unregister_device(&radio->vdev);
0498         v4l2_ctrl_handler_free(&radio->ctrl_handler);
0499         v4l2_device_unregister(&radio->v4l2_dev);
0500         kfree(radio);
0501     }
0502     return 0;
0503 }
0504 
0505 /* I2C subsystem interface */
0506 static const struct i2c_device_id tea5764_id[] = {
0507     { "radio-tea5764", 0 },
0508     { }                 /* Terminating entry */
0509 };
0510 MODULE_DEVICE_TABLE(i2c, tea5764_id);
0511 
0512 static struct i2c_driver tea5764_i2c_driver = {
0513     .driver = {
0514         .name = "radio-tea5764",
0515     },
0516     .probe = tea5764_i2c_probe,
0517     .remove = tea5764_i2c_remove,
0518     .id_table = tea5764_id,
0519 };
0520 
0521 module_i2c_driver(tea5764_i2c_driver);
0522 
0523 MODULE_AUTHOR(DRIVER_AUTHOR);
0524 MODULE_DESCRIPTION(DRIVER_DESC);
0525 MODULE_LICENSE("GPL");
0526 MODULE_VERSION(DRIVER_VERSION);
0527 
0528 module_param(use_xtal, int, 0);
0529 MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
0530 module_param(radio_nr, int, 0);
0531 MODULE_PARM_DESC(radio_nr, "video4linux device number to use");