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