Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | Driver for SAA6588 RDS decoder |
| 3 | |
| 4 | (c) 2005 Hans J. Koch |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/types.h> |
Hans Verkuil | 7f6adea | 2009-02-19 17:31:17 -0300 | [diff] [blame^] | 26 | #include <linux/videodev2.h> |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/poll.h> |
| 31 | #include <linux/wait.h> |
| 32 | #include <asm/uaccess.h> |
| 33 | |
Mauro Carvalho Chehab | fa3fcce | 2006-03-23 21:45:24 -0300 | [diff] [blame] | 34 | #include <media/rds.h> |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 35 | #include <media/v4l2-device.h> |
Hans Verkuil | f1f8c90 | 2009-02-11 19:28:30 -0300 | [diff] [blame] | 36 | #include <media/v4l2-chip-ident.h> |
Hans Verkuil | b5ffc22 | 2009-02-11 19:14:09 -0300 | [diff] [blame] | 37 | #include <media/v4l2-i2c-drv-legacy.h> |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 38 | |
| 39 | /* Addresses to scan */ |
| 40 | static unsigned short normal_i2c[] = { |
| 41 | 0x20 >> 1, |
| 42 | 0x22 >> 1, |
| 43 | I2C_CLIENT_END, |
| 44 | }; |
| 45 | |
| 46 | I2C_CLIENT_INSMOD; |
| 47 | |
| 48 | /* insmod options */ |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 49 | static unsigned int debug; |
| 50 | static unsigned int xtal; |
| 51 | static unsigned int rbds; |
| 52 | static unsigned int plvl; |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 53 | static unsigned int bufblocks = 100; |
| 54 | |
Eric Sesterhenn / snakebyte | 9b565eb | 2006-01-13 14:10:23 -0200 | [diff] [blame] | 55 | module_param(debug, int, 0644); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 56 | MODULE_PARM_DESC(debug, "enable debug messages"); |
Eric Sesterhenn / snakebyte | 9b565eb | 2006-01-13 14:10:23 -0200 | [diff] [blame] | 57 | module_param(xtal, int, 0); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 58 | MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0"); |
Eric Sesterhenn / snakebyte | 9b565eb | 2006-01-13 14:10:23 -0200 | [diff] [blame] | 59 | module_param(rbds, int, 0); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 60 | MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0"); |
Eric Sesterhenn / snakebyte | 9b565eb | 2006-01-13 14:10:23 -0200 | [diff] [blame] | 61 | module_param(plvl, int, 0); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 62 | MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0"); |
Eric Sesterhenn / snakebyte | 9b565eb | 2006-01-13 14:10:23 -0200 | [diff] [blame] | 63 | module_param(bufblocks, int, 0); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 64 | MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100"); |
| 65 | |
| 66 | MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder"); |
| 67 | MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>"); |
| 68 | |
| 69 | MODULE_LICENSE("GPL"); |
| 70 | |
| 71 | /* ---------------------------------------------------------------------- */ |
| 72 | |
| 73 | #define UNSET (-1U) |
| 74 | #define PREFIX "saa6588: " |
| 75 | #define dprintk if (debug) printk |
| 76 | |
| 77 | struct saa6588 { |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 78 | struct v4l2_subdev sd; |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 79 | struct work_struct work; |
| 80 | struct timer_list timer; |
| 81 | spinlock_t lock; |
| 82 | unsigned char *buffer; |
| 83 | unsigned int buf_size; |
| 84 | unsigned int rd_index; |
| 85 | unsigned int wr_index; |
| 86 | unsigned int block_count; |
| 87 | unsigned char last_blocknum; |
| 88 | wait_queue_head_t read_queue; |
| 89 | int data_available_for_read; |
| 90 | }; |
| 91 | |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 92 | static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd) |
| 93 | { |
| 94 | return container_of(sd, struct saa6588, sd); |
| 95 | } |
| 96 | |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 97 | /* ---------------------------------------------------------------------- */ |
| 98 | |
| 99 | /* |
| 100 | * SAA6588 defines |
| 101 | */ |
| 102 | |
| 103 | /* Initialization and mode control byte (0w) */ |
| 104 | |
| 105 | /* bit 0+1 (DAC0/DAC1) */ |
| 106 | #define cModeStandard 0x00 |
| 107 | #define cModeFastPI 0x01 |
| 108 | #define cModeReducedRequest 0x02 |
| 109 | #define cModeInvalid 0x03 |
| 110 | |
| 111 | /* bit 2 (RBDS) */ |
| 112 | #define cProcessingModeRDS 0x00 |
| 113 | #define cProcessingModeRBDS 0x04 |
| 114 | |
| 115 | /* bit 3+4 (SYM0/SYM1) */ |
| 116 | #define cErrCorrectionNone 0x00 |
| 117 | #define cErrCorrection2Bits 0x08 |
| 118 | #define cErrCorrection5Bits 0x10 |
| 119 | #define cErrCorrectionNoneRBDS 0x18 |
| 120 | |
| 121 | /* bit 5 (NWSY) */ |
| 122 | #define cSyncNormal 0x00 |
| 123 | #define cSyncRestart 0x20 |
| 124 | |
| 125 | /* bit 6 (TSQD) */ |
| 126 | #define cSigQualityDetectOFF 0x00 |
| 127 | #define cSigQualityDetectON 0x40 |
| 128 | |
| 129 | /* bit 7 (SQCM) */ |
| 130 | #define cSigQualityTriggered 0x00 |
| 131 | #define cSigQualityContinous 0x80 |
| 132 | |
| 133 | /* Pause level and flywheel control byte (1w) */ |
| 134 | |
| 135 | /* bits 0..5 (FEB0..FEB5) */ |
| 136 | #define cFlywheelMaxBlocksMask 0x3F |
| 137 | #define cFlywheelDefault 0x20 |
| 138 | |
| 139 | /* bits 6+7 (PL0/PL1) */ |
| 140 | #define cPauseLevel_11mV 0x00 |
| 141 | #define cPauseLevel_17mV 0x40 |
| 142 | #define cPauseLevel_27mV 0x80 |
| 143 | #define cPauseLevel_43mV 0xC0 |
| 144 | |
| 145 | /* Pause time/oscillator frequency/quality detector control byte (1w) */ |
| 146 | |
| 147 | /* bits 0..4 (SQS0..SQS4) */ |
| 148 | #define cQualityDetectSensMask 0x1F |
| 149 | #define cQualityDetectDefault 0x0F |
| 150 | |
| 151 | /* bit 5 (SOSC) */ |
| 152 | #define cSelectOscFreqOFF 0x00 |
| 153 | #define cSelectOscFreqON 0x20 |
| 154 | |
| 155 | /* bit 6+7 (PTF0/PTF1) */ |
| 156 | #define cOscFreq_4332kHz 0x00 |
| 157 | #define cOscFreq_8664kHz 0x40 |
| 158 | #define cOscFreq_12996kHz 0x80 |
| 159 | #define cOscFreq_17328kHz 0xC0 |
| 160 | |
| 161 | /* ---------------------------------------------------------------------- */ |
| 162 | |
Al Viro | ae8aed0 | 2005-09-29 00:37:34 +0100 | [diff] [blame] | 163 | static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf) |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 164 | { |
| 165 | int i; |
| 166 | |
| 167 | if (s->rd_index == s->wr_index) { |
| 168 | if (debug > 2) |
| 169 | dprintk(PREFIX "Read: buffer empty.\n"); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | if (debug > 2) { |
| 174 | dprintk(PREFIX "Read: "); |
| 175 | for (i = s->rd_index; i < s->rd_index + 3; i++) |
| 176 | dprintk("0x%02x ", s->buffer[i]); |
| 177 | } |
| 178 | |
| 179 | if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3)) |
| 180 | return -EFAULT; |
| 181 | |
| 182 | s->rd_index += 3; |
| 183 | if (s->rd_index >= s->buf_size) |
| 184 | s->rd_index = 0; |
| 185 | s->block_count--; |
| 186 | |
| 187 | if (debug > 2) |
| 188 | dprintk("%d blocks total.\n", s->block_count); |
| 189 | |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | static void read_from_buf(struct saa6588 *s, struct rds_command *a) |
| 194 | { |
| 195 | unsigned long flags; |
| 196 | |
Al Viro | ae8aed0 | 2005-09-29 00:37:34 +0100 | [diff] [blame] | 197 | unsigned char __user *buf_ptr = a->buffer; |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 198 | unsigned int i; |
| 199 | unsigned int rd_blocks; |
| 200 | |
| 201 | a->result = 0; |
| 202 | if (!a->buffer) |
| 203 | return; |
| 204 | |
| 205 | while (!s->data_available_for_read) { |
| 206 | int ret = wait_event_interruptible(s->read_queue, |
| 207 | s->data_available_for_read); |
| 208 | if (ret == -ERESTARTSYS) { |
| 209 | a->result = -EINTR; |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | spin_lock_irqsave(&s->lock, flags); |
| 215 | rd_blocks = a->block_count; |
| 216 | if (rd_blocks > s->block_count) |
| 217 | rd_blocks = s->block_count; |
| 218 | |
Ira Snyder | a5bbc7d | 2006-11-20 07:20:48 -0300 | [diff] [blame] | 219 | if (!rd_blocks) { |
| 220 | spin_unlock_irqrestore(&s->lock, flags); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 221 | return; |
Ira Snyder | a5bbc7d | 2006-11-20 07:20:48 -0300 | [diff] [blame] | 222 | } |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 223 | |
| 224 | for (i = 0; i < rd_blocks; i++) { |
| 225 | if (block_to_user_buf(s, buf_ptr)) { |
| 226 | buf_ptr += 3; |
| 227 | a->result++; |
| 228 | } else |
| 229 | break; |
| 230 | } |
| 231 | a->result *= 3; |
| 232 | s->data_available_for_read = (s->block_count > 0); |
| 233 | spin_unlock_irqrestore(&s->lock, flags); |
| 234 | } |
| 235 | |
| 236 | static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf) |
| 237 | { |
| 238 | unsigned int i; |
| 239 | |
| 240 | if (debug > 3) |
| 241 | dprintk(PREFIX "New block: "); |
| 242 | |
| 243 | for (i = 0; i < 3; ++i) { |
| 244 | if (debug > 3) |
| 245 | dprintk("0x%02x ", blockbuf[i]); |
| 246 | s->buffer[s->wr_index] = blockbuf[i]; |
| 247 | s->wr_index++; |
| 248 | } |
| 249 | |
| 250 | if (s->wr_index >= s->buf_size) |
| 251 | s->wr_index = 0; |
| 252 | |
| 253 | if (s->wr_index == s->rd_index) { |
Hans J. Koch | 6c3d67a | 2005-11-08 21:38:39 -0800 | [diff] [blame] | 254 | s->rd_index += 3; |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 255 | if (s->rd_index >= s->buf_size) |
| 256 | s->rd_index = 0; |
| 257 | } else |
| 258 | s->block_count++; |
| 259 | |
| 260 | if (debug > 3) |
| 261 | dprintk("%d blocks total.\n", s->block_count); |
| 262 | } |
| 263 | |
| 264 | static void saa6588_i2c_poll(struct saa6588 *s) |
| 265 | { |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 266 | struct i2c_client *client = v4l2_get_subdevdata(&s->sd); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 267 | unsigned long flags; |
| 268 | unsigned char tmpbuf[6]; |
| 269 | unsigned char blocknum; |
| 270 | unsigned char tmp; |
| 271 | |
| 272 | /* Although we only need 3 bytes, we have to read at least 6. |
| 273 | SAA6588 returns garbage otherwise */ |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 274 | if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) { |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 275 | if (debug > 1) |
| 276 | dprintk(PREFIX "read error!\n"); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | blocknum = tmpbuf[0] >> 5; |
| 281 | if (blocknum == s->last_blocknum) { |
| 282 | if (debug > 3) |
| 283 | dprintk("Saw block %d again.\n", blocknum); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | s->last_blocknum = blocknum; |
| 288 | |
| 289 | /* |
| 290 | Byte order according to v4l2 specification: |
| 291 | |
| 292 | Byte 0: Least Significant Byte of RDS Block |
| 293 | Byte 1: Most Significant Byte of RDS Block |
| 294 | Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error |
| 295 | occurred during reception of this block. |
| 296 | Bit 6: Corrected bit. Indicates that an error was |
| 297 | corrected for this data block. |
| 298 | Bits 5-3: Received Offset. Indicates the offset received |
| 299 | by the sync system. |
| 300 | Bits 2-0: Offset Name. Indicates the offset applied to this data. |
| 301 | |
| 302 | SAA6588 byte order is Status-MSB-LSB, so we have to swap the |
| 303 | first and the last of the 3 bytes block. |
| 304 | */ |
| 305 | |
| 306 | tmp = tmpbuf[2]; |
| 307 | tmpbuf[2] = tmpbuf[0]; |
| 308 | tmpbuf[0] = tmp; |
| 309 | |
| 310 | tmp = blocknum; |
| 311 | tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */ |
| 312 | if ((tmpbuf[2] & 0x03) == 0x03) |
| 313 | tmp |= 0x80; /* uncorrectable error */ |
| 314 | else if ((tmpbuf[2] & 0x03) != 0x00) |
| 315 | tmp |= 0x40; /* corrected error */ |
| 316 | tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */ |
| 317 | |
| 318 | spin_lock_irqsave(&s->lock, flags); |
| 319 | block_to_buf(s, tmpbuf); |
| 320 | spin_unlock_irqrestore(&s->lock, flags); |
| 321 | s->data_available_for_read = 1; |
| 322 | wake_up_interruptible(&s->read_queue); |
| 323 | } |
| 324 | |
| 325 | static void saa6588_timer(unsigned long data) |
| 326 | { |
| 327 | struct saa6588 *s = (struct saa6588 *)data; |
| 328 | |
| 329 | schedule_work(&s->work); |
| 330 | } |
| 331 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 332 | static void saa6588_work(struct work_struct *work) |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 333 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 334 | struct saa6588 *s = container_of(work, struct saa6588, work); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 335 | |
| 336 | saa6588_i2c_poll(s); |
Hans J. Koch | e222f83 | 2005-11-08 21:36:35 -0800 | [diff] [blame] | 337 | mod_timer(&s->timer, jiffies + msecs_to_jiffies(20)); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static int saa6588_configure(struct saa6588 *s) |
| 341 | { |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 342 | struct i2c_client *client = v4l2_get_subdevdata(&s->sd); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 343 | unsigned char buf[3]; |
| 344 | int rc; |
| 345 | |
| 346 | buf[0] = cSyncRestart; |
| 347 | if (rbds) |
| 348 | buf[0] |= cProcessingModeRBDS; |
| 349 | |
| 350 | buf[1] = cFlywheelDefault; |
| 351 | switch (plvl) { |
| 352 | case 0: |
| 353 | buf[1] |= cPauseLevel_11mV; |
| 354 | break; |
| 355 | case 1: |
| 356 | buf[1] |= cPauseLevel_17mV; |
| 357 | break; |
| 358 | case 2: |
| 359 | buf[1] |= cPauseLevel_27mV; |
| 360 | break; |
| 361 | case 3: |
| 362 | buf[1] |= cPauseLevel_43mV; |
| 363 | break; |
| 364 | default: /* nothing */ |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | buf[2] = cQualityDetectDefault | cSelectOscFreqON; |
| 369 | |
| 370 | switch (xtal) { |
| 371 | case 0: |
| 372 | buf[2] |= cOscFreq_4332kHz; |
| 373 | break; |
| 374 | case 1: |
| 375 | buf[2] |= cOscFreq_8664kHz; |
| 376 | break; |
| 377 | case 2: |
| 378 | buf[2] |= cOscFreq_12996kHz; |
| 379 | break; |
| 380 | case 3: |
| 381 | buf[2] |= cOscFreq_17328kHz; |
| 382 | break; |
| 383 | default: /* nothing */ |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n", |
| 388 | buf[0], buf[1], buf[2]); |
| 389 | |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 390 | rc = i2c_master_send(client, buf, 3); |
Hans Verkuil | b5ffc22 | 2009-02-11 19:14:09 -0300 | [diff] [blame] | 391 | if (rc != 3) |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 392 | printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc); |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* ---------------------------------------------------------------------- */ |
| 398 | |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 399 | static long saa6588_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 400 | { |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 401 | struct saa6588 *s = to_saa6588(sd); |
| 402 | struct rds_command *a = arg; |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 403 | |
| 404 | switch (cmd) { |
| 405 | /* --- open() for /dev/radio --- */ |
| 406 | case RDS_CMD_OPEN: |
| 407 | a->result = 0; /* return error if chip doesn't work ??? */ |
| 408 | break; |
| 409 | /* --- close() for /dev/radio --- */ |
| 410 | case RDS_CMD_CLOSE: |
| 411 | s->data_available_for_read = 1; |
| 412 | wake_up_interruptible(&s->read_queue); |
| 413 | a->result = 0; |
| 414 | break; |
| 415 | /* --- read() for /dev/radio --- */ |
| 416 | case RDS_CMD_READ: |
| 417 | read_from_buf(s, a); |
| 418 | break; |
| 419 | /* --- poll() for /dev/radio --- */ |
| 420 | case RDS_CMD_POLL: |
| 421 | a->result = 0; |
| 422 | if (s->data_available_for_read) { |
| 423 | a->result |= POLLIN | POLLRDNORM; |
| 424 | } |
| 425 | poll_wait(a->instance, &s->read_queue, a->event_list); |
| 426 | break; |
| 427 | |
| 428 | default: |
| 429 | /* nothing */ |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 430 | return -ENOIOCTLCMD; |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 431 | } |
| 432 | return 0; |
| 433 | } |
| 434 | |
Hans Verkuil | f1f8c90 | 2009-02-11 19:28:30 -0300 | [diff] [blame] | 435 | static int saa6588_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) |
| 436 | { |
| 437 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 438 | |
| 439 | return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA6588, 0); |
| 440 | } |
| 441 | |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 442 | static int saa6588_command(struct i2c_client *client, unsigned cmd, void *arg) |
| 443 | { |
| 444 | return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg); |
| 445 | } |
| 446 | |
| 447 | /* ----------------------------------------------------------------------- */ |
| 448 | |
| 449 | static const struct v4l2_subdev_core_ops saa6588_core_ops = { |
Hans Verkuil | f1f8c90 | 2009-02-11 19:28:30 -0300 | [diff] [blame] | 450 | .g_chip_ident = saa6588_g_chip_ident, |
Hans Verkuil | c3fda7f | 2009-02-11 19:23:57 -0300 | [diff] [blame] | 451 | .ioctl = saa6588_ioctl, |
| 452 | }; |
| 453 | |
| 454 | static const struct v4l2_subdev_ops saa6588_ops = { |
| 455 | .core = &saa6588_core_ops, |
| 456 | }; |
| 457 | |
| 458 | /* ---------------------------------------------------------------------- */ |
| 459 | |
| 460 | static int saa6588_probe(struct i2c_client *client, |
| 461 | const struct i2c_device_id *id) |
| 462 | { |
| 463 | struct saa6588 *s; |
| 464 | struct v4l2_subdev *sd; |
| 465 | |
| 466 | v4l_info(client, "saa6588 found @ 0x%x (%s)\n", |
| 467 | client->addr << 1, client->adapter->name); |
| 468 | |
| 469 | s = kzalloc(sizeof(*s), GFP_KERNEL); |
| 470 | if (s == NULL) |
| 471 | return -ENOMEM; |
| 472 | |
| 473 | s->buf_size = bufblocks * 3; |
| 474 | |
| 475 | s->buffer = kmalloc(s->buf_size, GFP_KERNEL); |
| 476 | if (s->buffer == NULL) { |
| 477 | kfree(s); |
| 478 | return -ENOMEM; |
| 479 | } |
| 480 | sd = &s->sd; |
| 481 | v4l2_i2c_subdev_init(sd, client, &saa6588_ops); |
| 482 | spin_lock_init(&s->lock); |
| 483 | s->block_count = 0; |
| 484 | s->wr_index = 0; |
| 485 | s->rd_index = 0; |
| 486 | s->last_blocknum = 0xff; |
| 487 | init_waitqueue_head(&s->read_queue); |
| 488 | s->data_available_for_read = 0; |
| 489 | |
| 490 | saa6588_configure(s); |
| 491 | |
| 492 | /* start polling via eventd */ |
| 493 | INIT_WORK(&s->work, saa6588_work); |
| 494 | init_timer(&s->timer); |
| 495 | s->timer.function = saa6588_timer; |
| 496 | s->timer.data = (unsigned long)s; |
| 497 | schedule_work(&s->work); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int saa6588_remove(struct i2c_client *client) |
| 502 | { |
| 503 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 504 | struct saa6588 *s = to_saa6588(sd); |
| 505 | |
| 506 | v4l2_device_unregister_subdev(sd); |
| 507 | |
| 508 | del_timer_sync(&s->timer); |
| 509 | flush_scheduled_work(); |
| 510 | |
| 511 | kfree(s->buffer); |
| 512 | kfree(s); |
| 513 | return 0; |
| 514 | } |
| 515 | |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 516 | /* ----------------------------------------------------------------------- */ |
| 517 | |
Hans Verkuil | b5ffc22 | 2009-02-11 19:14:09 -0300 | [diff] [blame] | 518 | static const struct i2c_device_id saa6588_id[] = { |
| 519 | { "saa6588", 0 }, |
| 520 | { } |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 521 | }; |
Hans Verkuil | b5ffc22 | 2009-02-11 19:14:09 -0300 | [diff] [blame] | 522 | MODULE_DEVICE_TABLE(i2c, saa6588_id); |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 523 | |
Hans Verkuil | b5ffc22 | 2009-02-11 19:14:09 -0300 | [diff] [blame] | 524 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 525 | .name = "saa6588", |
Hans Verkuil | b5ffc22 | 2009-02-11 19:14:09 -0300 | [diff] [blame] | 526 | .command = saa6588_command, |
| 527 | .probe = saa6588_probe, |
| 528 | .remove = saa6588_remove, |
Hans Verkuil | b5ffc22 | 2009-02-11 19:14:09 -0300 | [diff] [blame] | 529 | .id_table = saa6588_id, |
Mauro Carvalho Chehab | 10b89ee | 2005-09-09 13:04:03 -0700 | [diff] [blame] | 530 | }; |