Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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> |
Hans Verkuil | 099f88e | 2013-02-03 08:25:32 -0300 | [diff] [blame] | 42 | #include <media/v4l2-device.h> |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 43 | |
Mauro Carvalho Chehab | 29834c1 | 2011-06-25 10:15:42 -0300 | [diff] [blame] | 44 | #define DRIVER_VERSION "0.0.2" |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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 | |
Paul Bolle | 8c4343e | 2011-10-12 17:51:22 -0300 | [diff] [blame] | 132 | #ifdef CONFIG_RADIO_TEA5764_XTAL |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 133 | #define RADIO_TEA5764_XTAL 1 |
Paul Bolle | 8c4343e | 2011-10-12 17:51:22 -0300 | [diff] [blame] | 134 | #else |
| 135 | #define RADIO_TEA5764_XTAL 0 |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | static int radio_nr = -1; |
| 139 | static int use_xtal = RADIO_TEA5764_XTAL; |
| 140 | |
| 141 | struct tea5764_device { |
Hans Verkuil | 099f88e | 2013-02-03 08:25:32 -0300 | [diff] [blame] | 142 | struct v4l2_device v4l2_dev; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 143 | struct i2c_client *i2c_client; |
Hans Verkuil | cf9033f | 2013-02-03 08:27:57 -0300 | [diff] [blame^] | 144 | struct video_device vdev; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 145 | struct tea5764_regs regs; |
| 146 | struct mutex mutex; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /* I2C code related */ |
Mauro Carvalho Chehab | 0dec868 | 2012-10-27 18:35:46 -0200 | [diff] [blame] | 150 | static int tea5764_i2c_read(struct tea5764_device *radio) |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 151 | { |
| 152 | int i; |
| 153 | u16 *p = (u16 *) &radio->regs; |
| 154 | |
| 155 | struct i2c_msg msgs[1] = { |
Shubhrajyoti D | 66ba959 | 2012-09-18 08:22:33 -0300 | [diff] [blame] | 156 | { .addr = radio->i2c_client->addr, |
| 157 | .flags = I2C_M_RD, |
| 158 | .len = sizeof(radio->regs), |
| 159 | .buf = (void *)&radio->regs |
| 160 | }, |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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 | |
Mauro Carvalho Chehab | 0dec868 | 2012-10-27 18:35:46 -0200 | [diff] [blame] | 170 | static int tea5764_i2c_write(struct tea5764_device *radio) |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 171 | { |
| 172 | struct tea5764_write_regs wr; |
| 173 | struct tea5764_regs *r = &radio->regs; |
| 174 | struct i2c_msg msgs[1] = { |
Shubhrajyoti D | 66ba959 | 2012-09-18 08:22:33 -0300 | [diff] [blame] | 175 | { |
| 176 | .addr = radio->i2c_client->addr, |
| 177 | .len = sizeof(wr), |
| 178 | .buf = (void *)&wr |
| 179 | }, |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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); |
Hans Verkuil | cf9033f | 2013-02-03 08:27:57 -0300 | [diff] [blame^] | 306 | struct video_device *dev = &radio->vdev; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 307 | |
| 308 | strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver)); |
| 309 | strlcpy(v->card, dev->name, sizeof(v->card)); |
Kay Sievers | c3ef01c | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 310 | snprintf(v->bus_info, sizeof(v->bus_info), |
| 311 | "I2C:%s", dev_name(&dev->dev)); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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 | |
Julia Lawall | 909d15a | 2009-12-10 17:17:49 -0300 | [diff] [blame] | 325 | memset(v, 0, sizeof(*v)); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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) |
Hans Verkuil | 5543e2b | 2009-06-20 06:29:12 -0300 | [diff] [blame] | 333 | v->rxsubchans = V4L2_TUNER_SUB_STEREO; |
| 334 | else |
| 335 | v->rxsubchans = V4L2_TUNER_SUB_MONO; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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, |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame] | 344 | const struct v4l2_tuner *v) |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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, |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 356 | const struct v4l2_frequency *f) |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 357 | { |
| 358 | struct tea5764_device *radio = video_drvdata(file); |
| 359 | |
Hans Verkuil | a3a9e28 | 2009-11-27 04:33:25 -0300 | [diff] [blame] | 360 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | a3a9e28 | 2009-11-27 04:33:25 -0300 | [diff] [blame] | 381 | if (f->tuner != 0) |
| 382 | return -EINVAL; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 383 | tea5764_i2c_read(radio); |
Julia Lawall | 909d15a | 2009-12-10 17:17:49 -0300 | [diff] [blame] | 384 | memset(f, 0, sizeof(*f)); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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, |
Hans Verkuil | 0e8025b9 | 2012-09-04 11:59:31 -0300 | [diff] [blame] | 460 | const struct v4l2_audio *a) |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 461 | { |
| 462 | if (a->index != 0) |
| 463 | return -EINVAL; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 468 | /* File system interface */ |
| 469 | static const struct v4l2_file_operations tea5764_fops = { |
| 470 | .owner = THIS_MODULE, |
Hans Verkuil | ee71e42 | 2010-11-14 09:46:23 -0300 | [diff] [blame] | 471 | .unlocked_ioctl = video_ioctl2, |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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, |
Hans Verkuil | cf9033f | 2013-02-03 08:27:57 -0300 | [diff] [blame^] | 494 | .release = video_device_release_empty, |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | /* I2C probe: check if the device exists and register with v4l if it is */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 498 | static int tea5764_i2c_probe(struct i2c_client *client, |
| 499 | const struct i2c_device_id *id) |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 500 | { |
| 501 | struct tea5764_device *radio; |
Hans Verkuil | 099f88e | 2013-02-03 08:25:32 -0300 | [diff] [blame] | 502 | struct v4l2_device *v4l2_dev; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 503 | struct tea5764_regs *r; |
| 504 | int ret; |
| 505 | |
| 506 | PDEBUG("probe"); |
Hans Verkuil | ee71e42 | 2010-11-14 09:46:23 -0300 | [diff] [blame] | 507 | radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 508 | if (!radio) |
| 509 | return -ENOMEM; |
| 510 | |
Hans Verkuil | 099f88e | 2013-02-03 08:25:32 -0300 | [diff] [blame] | 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 | } |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 517 | mutex_init(&radio->mutex); |
| 518 | radio->i2c_client = client; |
| 519 | ret = tea5764_i2c_read(radio); |
| 520 | if (ret) |
Hans Verkuil | 099f88e | 2013-02-03 08:25:32 -0300 | [diff] [blame] | 521 | goto errunreg; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 099f88e | 2013-02-03 08:25:32 -0300 | [diff] [blame] | 528 | goto errunreg; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 529 | } |
| 530 | |
Hans Verkuil | cf9033f | 2013-02-03 08:27:57 -0300 | [diff] [blame^] | 531 | radio->vdev = tea5764_radio_template; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 532 | |
| 533 | i2c_set_clientdata(client, radio); |
Hans Verkuil | cf9033f | 2013-02-03 08:27:57 -0300 | [diff] [blame^] | 534 | video_set_drvdata(&radio->vdev, radio); |
| 535 | radio->vdev.lock = &radio->mutex; |
| 536 | radio->vdev.v4l2_dev = v4l2_dev; |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | cf9033f | 2013-02-03 08:27:57 -0300 | [diff] [blame^] | 544 | ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr); |
Hans Verkuil | ee71e42 | 2010-11-14 09:46:23 -0300 | [diff] [blame] | 545 | if (ret < 0) { |
| 546 | PWARN("Could not register video device!"); |
Hans Verkuil | cf9033f | 2013-02-03 08:27:57 -0300 | [diff] [blame^] | 547 | goto errunreg; |
Hans Verkuil | ee71e42 | 2010-11-14 09:46:23 -0300 | [diff] [blame] | 548 | } |
| 549 | |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 550 | PINFO("registered."); |
| 551 | return 0; |
Hans Verkuil | 099f88e | 2013-02-03 08:25:32 -0300 | [diff] [blame] | 552 | errunreg: |
| 553 | v4l2_device_unregister(v4l2_dev); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 554 | errfr: |
| 555 | kfree(radio); |
| 556 | return ret; |
| 557 | } |
| 558 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 559 | static int tea5764_i2c_remove(struct i2c_client *client) |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 560 | { |
| 561 | struct tea5764_device *radio = i2c_get_clientdata(client); |
| 562 | |
| 563 | PDEBUG("remove"); |
| 564 | if (radio) { |
| 565 | tea5764_power_down(radio); |
Hans Verkuil | cf9033f | 2013-02-03 08:27:57 -0300 | [diff] [blame^] | 566 | video_unregister_device(&radio->vdev); |
Hans Verkuil | 099f88e | 2013-02-03 08:25:32 -0300 | [diff] [blame] | 567 | v4l2_device_unregister(&radio->v4l2_dev); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 586 | .remove = tea5764_i2c_remove, |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 587 | .id_table = tea5764_id, |
| 588 | }; |
| 589 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 590 | module_i2c_driver(tea5764_i2c_driver); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 591 | |
| 592 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 593 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 594 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 29834c1 | 2011-06-25 10:15:42 -0300 | [diff] [blame] | 595 | MODULE_VERSION(DRIVER_VERSION); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 596 | |
Jean Delvare | 731884b | 2011-07-08 06:00:37 -0300 | [diff] [blame] | 597 | module_param(use_xtal, int, 0); |
Fabio Belavenuto | 46a60cf | 2008-12-30 19:27:09 -0300 | [diff] [blame] | 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"); |