Path: blob/master/drivers/media/radio/radio-trust.c
15112 views
/* radio-trust.c - Trust FM Radio card driver for Linux 2.21* by Eric Lammerts <[email protected]>2*3* Based on radio-aztech.c. Original notes:4*5* Adapted to support the Video for Linux API by6* Russell Kroll <[email protected]>. Based on original tuner code by:7*8* Quay Ly9* Donald Song10* Jason Lewis ([email protected])11* Scott McGrath ([email protected])12* William McGrath ([email protected])13*14* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>15*/1617#include <stdarg.h>18#include <linux/module.h>19#include <linux/init.h>20#include <linux/ioport.h>21#include <linux/version.h> /* for KERNEL_VERSION MACRO */22#include <linux/videodev2.h>23#include <linux/io.h>24#include <media/v4l2-device.h>25#include <media/v4l2-ioctl.h>2627MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");28MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");29MODULE_LICENSE("GPL");3031/* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */3233#ifndef CONFIG_RADIO_TRUST_PORT34#define CONFIG_RADIO_TRUST_PORT -135#endif3637static int io = CONFIG_RADIO_TRUST_PORT;38static int radio_nr = -1;3940module_param(io, int, 0);41MODULE_PARM_DESC(io, "I/O address of the Trust FM Radio card (0x350 or 0x358)");42module_param(radio_nr, int, 0);4344#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)4546struct trust {47struct v4l2_device v4l2_dev;48struct video_device vdev;49int io;50int ioval;51__u16 curvol;52__u16 curbass;53__u16 curtreble;54int muted;55unsigned long curfreq;56int curstereo;57int curmute;58struct mutex lock;59};6061static struct trust trust_card;6263/* i2c addresses */64#define TDA7318_ADDR 0x8865#define TSA6060T_ADDR 0xc46667#define TR_DELAY do { inb(tr->io); inb(tr->io); inb(tr->io); } while (0)68#define TR_SET_SCL outb(tr->ioval |= 2, tr->io)69#define TR_CLR_SCL outb(tr->ioval &= 0xfd, tr->io)70#define TR_SET_SDA outb(tr->ioval |= 1, tr->io)71#define TR_CLR_SDA outb(tr->ioval &= 0xfe, tr->io)7273static void write_i2c(struct trust *tr, int n, ...)74{75unsigned char val, mask;76va_list args;7778va_start(args, n);7980/* start condition */81TR_SET_SDA;82TR_SET_SCL;83TR_DELAY;84TR_CLR_SDA;85TR_CLR_SCL;86TR_DELAY;8788for(; n; n--) {89val = va_arg(args, unsigned);90for(mask = 0x80; mask; mask >>= 1) {91if(val & mask)92TR_SET_SDA;93else94TR_CLR_SDA;95TR_SET_SCL;96TR_DELAY;97TR_CLR_SCL;98TR_DELAY;99}100/* acknowledge bit */101TR_SET_SDA;102TR_SET_SCL;103TR_DELAY;104TR_CLR_SCL;105TR_DELAY;106}107108/* stop condition */109TR_CLR_SDA;110TR_DELAY;111TR_SET_SCL;112TR_DELAY;113TR_SET_SDA;114TR_DELAY;115116va_end(args);117}118119static void tr_setvol(struct trust *tr, __u16 vol)120{121mutex_lock(&tr->lock);122tr->curvol = vol / 2048;123write_i2c(tr, 2, TDA7318_ADDR, tr->curvol ^ 0x1f);124mutex_unlock(&tr->lock);125}126127static int basstreble2chip[15] = {1280, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8129};130131static void tr_setbass(struct trust *tr, __u16 bass)132{133mutex_lock(&tr->lock);134tr->curbass = bass / 4370;135write_i2c(tr, 2, TDA7318_ADDR, 0x60 | basstreble2chip[tr->curbass]);136mutex_unlock(&tr->lock);137}138139static void tr_settreble(struct trust *tr, __u16 treble)140{141mutex_lock(&tr->lock);142tr->curtreble = treble / 4370;143write_i2c(tr, 2, TDA7318_ADDR, 0x70 | basstreble2chip[tr->curtreble]);144mutex_unlock(&tr->lock);145}146147static void tr_setstereo(struct trust *tr, int stereo)148{149mutex_lock(&tr->lock);150tr->curstereo = !!stereo;151tr->ioval = (tr->ioval & 0xfb) | (!tr->curstereo << 2);152outb(tr->ioval, tr->io);153mutex_unlock(&tr->lock);154}155156static void tr_setmute(struct trust *tr, int mute)157{158mutex_lock(&tr->lock);159tr->curmute = !!mute;160tr->ioval = (tr->ioval & 0xf7) | (tr->curmute << 3);161outb(tr->ioval, tr->io);162mutex_unlock(&tr->lock);163}164165static int tr_getsigstr(struct trust *tr)166{167int i, v;168169mutex_lock(&tr->lock);170for (i = 0, v = 0; i < 100; i++)171v |= inb(tr->io);172mutex_unlock(&tr->lock);173return (v & 1) ? 0 : 0xffff;174}175176static int tr_getstereo(struct trust *tr)177{178/* don't know how to determine it, just return the setting */179return tr->curstereo;180}181182static void tr_setfreq(struct trust *tr, unsigned long f)183{184mutex_lock(&tr->lock);185tr->curfreq = f;186f /= 160; /* Convert to 10 kHz units */187f += 1070; /* Add 10.7 MHz IF */188write_i2c(tr, 5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0);189mutex_unlock(&tr->lock);190}191192static int vidioc_querycap(struct file *file, void *priv,193struct v4l2_capability *v)194{195strlcpy(v->driver, "radio-trust", sizeof(v->driver));196strlcpy(v->card, "Trust FM Radio", sizeof(v->card));197strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));198v->version = RADIO_VERSION;199v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;200return 0;201}202203static int vidioc_g_tuner(struct file *file, void *priv,204struct v4l2_tuner *v)205{206struct trust *tr = video_drvdata(file);207208if (v->index > 0)209return -EINVAL;210211strlcpy(v->name, "FM", sizeof(v->name));212v->type = V4L2_TUNER_RADIO;213v->rangelow = 87.5 * 16000;214v->rangehigh = 108 * 16000;215v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;216v->capability = V4L2_TUNER_CAP_LOW;217if (tr_getstereo(tr))218v->audmode = V4L2_TUNER_MODE_STEREO;219else220v->audmode = V4L2_TUNER_MODE_MONO;221v->signal = tr_getsigstr(tr);222return 0;223}224225static int vidioc_s_tuner(struct file *file, void *priv,226struct v4l2_tuner *v)227{228struct trust *tr = video_drvdata(file);229230if (v->index)231return -EINVAL;232tr_setstereo(tr, v->audmode == V4L2_TUNER_MODE_STEREO);233return 0;234}235236static int vidioc_s_frequency(struct file *file, void *priv,237struct v4l2_frequency *f)238{239struct trust *tr = video_drvdata(file);240241if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)242return -EINVAL;243tr_setfreq(tr, f->frequency);244return 0;245}246247static int vidioc_g_frequency(struct file *file, void *priv,248struct v4l2_frequency *f)249{250struct trust *tr = video_drvdata(file);251252if (f->tuner != 0)253return -EINVAL;254f->type = V4L2_TUNER_RADIO;255f->frequency = tr->curfreq;256return 0;257}258259static int vidioc_queryctrl(struct file *file, void *priv,260struct v4l2_queryctrl *qc)261{262switch (qc->id) {263case V4L2_CID_AUDIO_MUTE:264return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);265case V4L2_CID_AUDIO_VOLUME:266return v4l2_ctrl_query_fill(qc, 0, 65535, 2048, 65535);267case V4L2_CID_AUDIO_BASS:268case V4L2_CID_AUDIO_TREBLE:269return v4l2_ctrl_query_fill(qc, 0, 65535, 4370, 32768);270}271return -EINVAL;272}273274static int vidioc_g_ctrl(struct file *file, void *priv,275struct v4l2_control *ctrl)276{277struct trust *tr = video_drvdata(file);278279switch (ctrl->id) {280case V4L2_CID_AUDIO_MUTE:281ctrl->value = tr->curmute;282return 0;283case V4L2_CID_AUDIO_VOLUME:284ctrl->value = tr->curvol * 2048;285return 0;286case V4L2_CID_AUDIO_BASS:287ctrl->value = tr->curbass * 4370;288return 0;289case V4L2_CID_AUDIO_TREBLE:290ctrl->value = tr->curtreble * 4370;291return 0;292}293return -EINVAL;294}295296static int vidioc_s_ctrl(struct file *file, void *priv,297struct v4l2_control *ctrl)298{299struct trust *tr = video_drvdata(file);300301switch (ctrl->id) {302case V4L2_CID_AUDIO_MUTE:303tr_setmute(tr, ctrl->value);304return 0;305case V4L2_CID_AUDIO_VOLUME:306tr_setvol(tr, ctrl->value);307return 0;308case V4L2_CID_AUDIO_BASS:309tr_setbass(tr, ctrl->value);310return 0;311case V4L2_CID_AUDIO_TREBLE:312tr_settreble(tr, ctrl->value);313return 0;314}315return -EINVAL;316}317318static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)319{320*i = 0;321return 0;322}323324static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)325{326return i ? -EINVAL : 0;327}328329static int vidioc_g_audio(struct file *file, void *priv,330struct v4l2_audio *a)331{332a->index = 0;333strlcpy(a->name, "Radio", sizeof(a->name));334a->capability = V4L2_AUDCAP_STEREO;335return 0;336}337338static int vidioc_s_audio(struct file *file, void *priv,339struct v4l2_audio *a)340{341return a->index ? -EINVAL : 0;342}343344static const struct v4l2_file_operations trust_fops = {345.owner = THIS_MODULE,346.unlocked_ioctl = video_ioctl2,347};348349static const struct v4l2_ioctl_ops trust_ioctl_ops = {350.vidioc_querycap = vidioc_querycap,351.vidioc_g_tuner = vidioc_g_tuner,352.vidioc_s_tuner = vidioc_s_tuner,353.vidioc_g_frequency = vidioc_g_frequency,354.vidioc_s_frequency = vidioc_s_frequency,355.vidioc_queryctrl = vidioc_queryctrl,356.vidioc_g_ctrl = vidioc_g_ctrl,357.vidioc_s_ctrl = vidioc_s_ctrl,358.vidioc_g_audio = vidioc_g_audio,359.vidioc_s_audio = vidioc_s_audio,360.vidioc_g_input = vidioc_g_input,361.vidioc_s_input = vidioc_s_input,362};363364static int __init trust_init(void)365{366struct trust *tr = &trust_card;367struct v4l2_device *v4l2_dev = &tr->v4l2_dev;368int res;369370strlcpy(v4l2_dev->name, "trust", sizeof(v4l2_dev->name));371tr->io = io;372tr->ioval = 0xf;373mutex_init(&tr->lock);374375if (tr->io == -1) {376v4l2_err(v4l2_dev, "You must set an I/O address with io=0x0x350 or 0x358\n");377return -EINVAL;378}379if (!request_region(tr->io, 2, "Trust FM Radio")) {380v4l2_err(v4l2_dev, "port 0x%x already in use\n", tr->io);381return -EBUSY;382}383384res = v4l2_device_register(NULL, v4l2_dev);385if (res < 0) {386release_region(tr->io, 2);387v4l2_err(v4l2_dev, "Could not register v4l2_device\n");388return res;389}390391strlcpy(tr->vdev.name, v4l2_dev->name, sizeof(tr->vdev.name));392tr->vdev.v4l2_dev = v4l2_dev;393tr->vdev.fops = &trust_fops;394tr->vdev.ioctl_ops = &trust_ioctl_ops;395tr->vdev.release = video_device_release_empty;396video_set_drvdata(&tr->vdev, tr);397398write_i2c(tr, 2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */399write_i2c(tr, 2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */400write_i2c(tr, 2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */401write_i2c(tr, 2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */402write_i2c(tr, 2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */403404tr_setvol(tr, 0xffff);405tr_setbass(tr, 0x8000);406tr_settreble(tr, 0x8000);407tr_setstereo(tr, 1);408409/* mute card - prevents noisy bootups */410tr_setmute(tr, 1);411412if (video_register_device(&tr->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {413v4l2_device_unregister(v4l2_dev);414release_region(tr->io, 2);415return -EINVAL;416}417418v4l2_info(v4l2_dev, "Trust FM Radio card driver v1.0.\n");419420return 0;421}422423static void __exit cleanup_trust_module(void)424{425struct trust *tr = &trust_card;426427video_unregister_device(&tr->vdev);428v4l2_device_unregister(&tr->v4l2_dev);429release_region(tr->io, 2);430}431432module_init(trust_init);433module_exit(cleanup_trust_module);434435436