xref: /linux/drivers/media/i2c/saa6588.c (revision c771600c6af14749609b49565ffb4cac2959710d)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
210b89ee3SMauro Carvalho Chehab /*
310b89ee3SMauro Carvalho Chehab     Driver for SAA6588 RDS decoder
410b89ee3SMauro Carvalho Chehab 
510b89ee3SMauro Carvalho Chehab     (c) 2005 Hans J. Koch
610b89ee3SMauro Carvalho Chehab 
710b89ee3SMauro Carvalho Chehab */
810b89ee3SMauro Carvalho Chehab 
910b89ee3SMauro Carvalho Chehab 
1010b89ee3SMauro Carvalho Chehab #include <linux/module.h>
1110b89ee3SMauro Carvalho Chehab #include <linux/kernel.h>
1210b89ee3SMauro Carvalho Chehab #include <linux/i2c.h>
1310b89ee3SMauro Carvalho Chehab #include <linux/types.h>
147f6adeafSHans Verkuil #include <linux/videodev2.h>
1510b89ee3SMauro Carvalho Chehab #include <linux/init.h>
1610b89ee3SMauro Carvalho Chehab #include <linux/errno.h>
1710b89ee3SMauro Carvalho Chehab #include <linux/slab.h>
1810b89ee3SMauro Carvalho Chehab #include <linux/poll.h>
1910b89ee3SMauro Carvalho Chehab #include <linux/wait.h>
207c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
2110b89ee3SMauro Carvalho Chehab 
22b5dcee22SMauro Carvalho Chehab #include <media/i2c/saa6588.h>
23c3fda7f8SHans Verkuil #include <media/v4l2-device.h>
2410b89ee3SMauro Carvalho Chehab 
2510b89ee3SMauro Carvalho Chehab 
2610b89ee3SMauro Carvalho Chehab /* insmod options */
27ff699e6bSDouglas Schilling Landgraf static unsigned int debug;
28ff699e6bSDouglas Schilling Landgraf static unsigned int xtal;
293c86cf7aSHans Verkuil static unsigned int mmbs;
30ff699e6bSDouglas Schilling Landgraf static unsigned int plvl;
3110b89ee3SMauro Carvalho Chehab static unsigned int bufblocks = 100;
3210b89ee3SMauro Carvalho Chehab 
339b565eb7SEric Sesterhenn / snakebyte module_param(debug, int, 0644);
3410b89ee3SMauro Carvalho Chehab MODULE_PARM_DESC(debug, "enable debug messages");
359b565eb7SEric Sesterhenn / snakebyte module_param(xtal, int, 0);
3610b89ee3SMauro Carvalho Chehab MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
373c86cf7aSHans Verkuil module_param(mmbs, int, 0);
383c86cf7aSHans Verkuil MODULE_PARM_DESC(mmbs, "enable MMBS mode: 0=off (default), 1=on");
399b565eb7SEric Sesterhenn / snakebyte module_param(plvl, int, 0);
4010b89ee3SMauro Carvalho Chehab MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
419b565eb7SEric Sesterhenn / snakebyte module_param(bufblocks, int, 0);
4210b89ee3SMauro Carvalho Chehab MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
4310b89ee3SMauro Carvalho Chehab 
4410b89ee3SMauro Carvalho Chehab MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
4510b89ee3SMauro Carvalho Chehab MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
4610b89ee3SMauro Carvalho Chehab 
4710b89ee3SMauro Carvalho Chehab MODULE_LICENSE("GPL");
4810b89ee3SMauro Carvalho Chehab 
4910b89ee3SMauro Carvalho Chehab /* ---------------------------------------------------------------------- */
5010b89ee3SMauro Carvalho Chehab 
5110b89ee3SMauro Carvalho Chehab #define UNSET       (-1U)
5210b89ee3SMauro Carvalho Chehab #define PREFIX      "saa6588: "
5310b89ee3SMauro Carvalho Chehab #define dprintk     if (debug) printk
5410b89ee3SMauro Carvalho Chehab 
5510b89ee3SMauro Carvalho Chehab struct saa6588 {
56c3fda7f8SHans Verkuil 	struct v4l2_subdev sd;
57fb6991d4SJean Delvare 	struct delayed_work work;
5810b89ee3SMauro Carvalho Chehab 	spinlock_t lock;
5910b89ee3SMauro Carvalho Chehab 	unsigned char *buffer;
6010b89ee3SMauro Carvalho Chehab 	unsigned int buf_size;
6110b89ee3SMauro Carvalho Chehab 	unsigned int rd_index;
6210b89ee3SMauro Carvalho Chehab 	unsigned int wr_index;
6310b89ee3SMauro Carvalho Chehab 	unsigned int block_count;
6410b89ee3SMauro Carvalho Chehab 	unsigned char last_blocknum;
6510b89ee3SMauro Carvalho Chehab 	wait_queue_head_t read_queue;
6610b89ee3SMauro Carvalho Chehab 	int data_available_for_read;
673c86cf7aSHans Verkuil 	u8 sync;
6810b89ee3SMauro Carvalho Chehab };
6910b89ee3SMauro Carvalho Chehab 
to_saa6588(struct v4l2_subdev * sd)70c3fda7f8SHans Verkuil static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd)
71c3fda7f8SHans Verkuil {
72c3fda7f8SHans Verkuil 	return container_of(sd, struct saa6588, sd);
73c3fda7f8SHans Verkuil }
74c3fda7f8SHans Verkuil 
7510b89ee3SMauro Carvalho Chehab /* ---------------------------------------------------------------------- */
7610b89ee3SMauro Carvalho Chehab 
7710b89ee3SMauro Carvalho Chehab /*
7810b89ee3SMauro Carvalho Chehab  * SAA6588 defines
7910b89ee3SMauro Carvalho Chehab  */
8010b89ee3SMauro Carvalho Chehab 
8110b89ee3SMauro Carvalho Chehab /* Initialization and mode control byte (0w) */
8210b89ee3SMauro Carvalho Chehab 
8310b89ee3SMauro Carvalho Chehab /* bit 0+1 (DAC0/DAC1) */
8410b89ee3SMauro Carvalho Chehab #define cModeStandard           0x00
8510b89ee3SMauro Carvalho Chehab #define cModeFastPI             0x01
8610b89ee3SMauro Carvalho Chehab #define cModeReducedRequest     0x02
8710b89ee3SMauro Carvalho Chehab #define cModeInvalid            0x03
8810b89ee3SMauro Carvalho Chehab 
8910b89ee3SMauro Carvalho Chehab /* bit 2 (RBDS) */
9010b89ee3SMauro Carvalho Chehab #define cProcessingModeRDS      0x00
9110b89ee3SMauro Carvalho Chehab #define cProcessingModeRBDS     0x04
9210b89ee3SMauro Carvalho Chehab 
9310b89ee3SMauro Carvalho Chehab /* bit 3+4 (SYM0/SYM1) */
9410b89ee3SMauro Carvalho Chehab #define cErrCorrectionNone      0x00
9510b89ee3SMauro Carvalho Chehab #define cErrCorrection2Bits     0x08
9610b89ee3SMauro Carvalho Chehab #define cErrCorrection5Bits     0x10
9710b89ee3SMauro Carvalho Chehab #define cErrCorrectionNoneRBDS  0x18
9810b89ee3SMauro Carvalho Chehab 
9910b89ee3SMauro Carvalho Chehab /* bit 5 (NWSY) */
10010b89ee3SMauro Carvalho Chehab #define cSyncNormal             0x00
10110b89ee3SMauro Carvalho Chehab #define cSyncRestart            0x20
10210b89ee3SMauro Carvalho Chehab 
10310b89ee3SMauro Carvalho Chehab /* bit 6 (TSQD) */
10410b89ee3SMauro Carvalho Chehab #define cSigQualityDetectOFF    0x00
10510b89ee3SMauro Carvalho Chehab #define cSigQualityDetectON     0x40
10610b89ee3SMauro Carvalho Chehab 
10710b89ee3SMauro Carvalho Chehab /* bit 7 (SQCM) */
10810b89ee3SMauro Carvalho Chehab #define cSigQualityTriggered    0x00
10910b89ee3SMauro Carvalho Chehab #define cSigQualityContinous    0x80
11010b89ee3SMauro Carvalho Chehab 
11110b89ee3SMauro Carvalho Chehab /* Pause level and flywheel control byte (1w) */
11210b89ee3SMauro Carvalho Chehab 
11310b89ee3SMauro Carvalho Chehab /* bits 0..5 (FEB0..FEB5) */
11410b89ee3SMauro Carvalho Chehab #define cFlywheelMaxBlocksMask  0x3F
11510b89ee3SMauro Carvalho Chehab #define cFlywheelDefault        0x20
11610b89ee3SMauro Carvalho Chehab 
11710b89ee3SMauro Carvalho Chehab /* bits 6+7 (PL0/PL1) */
11810b89ee3SMauro Carvalho Chehab #define cPauseLevel_11mV	0x00
11910b89ee3SMauro Carvalho Chehab #define cPauseLevel_17mV        0x40
12010b89ee3SMauro Carvalho Chehab #define cPauseLevel_27mV        0x80
12110b89ee3SMauro Carvalho Chehab #define cPauseLevel_43mV        0xC0
12210b89ee3SMauro Carvalho Chehab 
12310b89ee3SMauro Carvalho Chehab /* Pause time/oscillator frequency/quality detector control byte (1w) */
12410b89ee3SMauro Carvalho Chehab 
12510b89ee3SMauro Carvalho Chehab /* bits 0..4 (SQS0..SQS4) */
12610b89ee3SMauro Carvalho Chehab #define cQualityDetectSensMask  0x1F
12710b89ee3SMauro Carvalho Chehab #define cQualityDetectDefault   0x0F
12810b89ee3SMauro Carvalho Chehab 
12910b89ee3SMauro Carvalho Chehab /* bit 5 (SOSC) */
13010b89ee3SMauro Carvalho Chehab #define cSelectOscFreqOFF	0x00
13110b89ee3SMauro Carvalho Chehab #define cSelectOscFreqON	0x20
13210b89ee3SMauro Carvalho Chehab 
13310b89ee3SMauro Carvalho Chehab /* bit 6+7 (PTF0/PTF1) */
13410b89ee3SMauro Carvalho Chehab #define cOscFreq_4332kHz	0x00
13510b89ee3SMauro Carvalho Chehab #define cOscFreq_8664kHz	0x40
13610b89ee3SMauro Carvalho Chehab #define cOscFreq_12996kHz	0x80
13710b89ee3SMauro Carvalho Chehab #define cOscFreq_17328kHz	0xC0
13810b89ee3SMauro Carvalho Chehab 
13910b89ee3SMauro Carvalho Chehab /* ---------------------------------------------------------------------- */
14010b89ee3SMauro Carvalho Chehab 
block_from_buf(struct saa6588 * s,unsigned char * buf)14109092787SHans Verkuil static bool block_from_buf(struct saa6588 *s, unsigned char *buf)
14210b89ee3SMauro Carvalho Chehab {
14310b89ee3SMauro Carvalho Chehab 	int i;
14410b89ee3SMauro Carvalho Chehab 
14510b89ee3SMauro Carvalho Chehab 	if (s->rd_index == s->wr_index) {
14610b89ee3SMauro Carvalho Chehab 		if (debug > 2)
14710b89ee3SMauro Carvalho Chehab 			dprintk(PREFIX "Read: buffer empty.\n");
14809092787SHans Verkuil 		return false;
14910b89ee3SMauro Carvalho Chehab 	}
15010b89ee3SMauro Carvalho Chehab 
15110b89ee3SMauro Carvalho Chehab 	if (debug > 2) {
15210b89ee3SMauro Carvalho Chehab 		dprintk(PREFIX "Read: ");
15310b89ee3SMauro Carvalho Chehab 		for (i = s->rd_index; i < s->rd_index + 3; i++)
15410b89ee3SMauro Carvalho Chehab 			dprintk("0x%02x ", s->buffer[i]);
15510b89ee3SMauro Carvalho Chehab 	}
15610b89ee3SMauro Carvalho Chehab 
15709092787SHans Verkuil 	memcpy(buf, &s->buffer[s->rd_index], 3);
15810b89ee3SMauro Carvalho Chehab 
15910b89ee3SMauro Carvalho Chehab 	s->rd_index += 3;
16010b89ee3SMauro Carvalho Chehab 	if (s->rd_index >= s->buf_size)
16110b89ee3SMauro Carvalho Chehab 		s->rd_index = 0;
16210b89ee3SMauro Carvalho Chehab 	s->block_count--;
16310b89ee3SMauro Carvalho Chehab 
16410b89ee3SMauro Carvalho Chehab 	if (debug > 2)
16510b89ee3SMauro Carvalho Chehab 		dprintk("%d blocks total.\n", s->block_count);
16610b89ee3SMauro Carvalho Chehab 
16709092787SHans Verkuil 	return true;
16810b89ee3SMauro Carvalho Chehab }
16910b89ee3SMauro Carvalho Chehab 
read_from_buf(struct saa6588 * s,struct saa6588_command * a)170b9218f2fSHans Verkuil static void read_from_buf(struct saa6588 *s, struct saa6588_command *a)
17110b89ee3SMauro Carvalho Chehab {
172ae8aed03SAl Viro 	unsigned char __user *buf_ptr = a->buffer;
17309092787SHans Verkuil 	unsigned char buf[3];
17409092787SHans Verkuil 	unsigned long flags;
17510b89ee3SMauro Carvalho Chehab 	unsigned int rd_blocks;
17609092787SHans Verkuil 	unsigned int i;
17710b89ee3SMauro Carvalho Chehab 
17810b89ee3SMauro Carvalho Chehab 	a->result = 0;
17910b89ee3SMauro Carvalho Chehab 	if (!a->buffer)
18010b89ee3SMauro Carvalho Chehab 		return;
18110b89ee3SMauro Carvalho Chehab 
18209092787SHans Verkuil 	while (!a->nonblocking && !s->data_available_for_read) {
18310b89ee3SMauro Carvalho Chehab 		int ret = wait_event_interruptible(s->read_queue,
18410b89ee3SMauro Carvalho Chehab 					     s->data_available_for_read);
18510b89ee3SMauro Carvalho Chehab 		if (ret == -ERESTARTSYS) {
18610b89ee3SMauro Carvalho Chehab 			a->result = -EINTR;
18710b89ee3SMauro Carvalho Chehab 			return;
18810b89ee3SMauro Carvalho Chehab 		}
18910b89ee3SMauro Carvalho Chehab 	}
19010b89ee3SMauro Carvalho Chehab 
19110b89ee3SMauro Carvalho Chehab 	rd_blocks = a->block_count;
19209092787SHans Verkuil 	spin_lock_irqsave(&s->lock, flags);
19310b89ee3SMauro Carvalho Chehab 	if (rd_blocks > s->block_count)
19410b89ee3SMauro Carvalho Chehab 		rd_blocks = s->block_count;
195a5bbc7d9SIra Snyder 	spin_unlock_irqrestore(&s->lock, flags);
19609092787SHans Verkuil 
19709092787SHans Verkuil 	if (!rd_blocks)
19810b89ee3SMauro Carvalho Chehab 		return;
19910b89ee3SMauro Carvalho Chehab 
20010b89ee3SMauro Carvalho Chehab 	for (i = 0; i < rd_blocks; i++) {
20109092787SHans Verkuil 		bool got_block;
20209092787SHans Verkuil 
20309092787SHans Verkuil 		spin_lock_irqsave(&s->lock, flags);
20409092787SHans Verkuil 		got_block = block_from_buf(s, buf);
20509092787SHans Verkuil 		spin_unlock_irqrestore(&s->lock, flags);
20609092787SHans Verkuil 		if (!got_block)
20710b89ee3SMauro Carvalho Chehab 			break;
20809092787SHans Verkuil 		if (copy_to_user(buf_ptr, buf, 3)) {
20909092787SHans Verkuil 			a->result = -EFAULT;
21009092787SHans Verkuil 			return;
21110b89ee3SMauro Carvalho Chehab 		}
21209092787SHans Verkuil 		buf_ptr += 3;
21309092787SHans Verkuil 		a->result += 3;
21409092787SHans Verkuil 	}
21509092787SHans Verkuil 	spin_lock_irqsave(&s->lock, flags);
21610b89ee3SMauro Carvalho Chehab 	s->data_available_for_read = (s->block_count > 0);
21710b89ee3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&s->lock, flags);
21810b89ee3SMauro Carvalho Chehab }
21910b89ee3SMauro Carvalho Chehab 
block_to_buf(struct saa6588 * s,unsigned char * blockbuf)22010b89ee3SMauro Carvalho Chehab static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
22110b89ee3SMauro Carvalho Chehab {
22210b89ee3SMauro Carvalho Chehab 	unsigned int i;
22310b89ee3SMauro Carvalho Chehab 
22410b89ee3SMauro Carvalho Chehab 	if (debug > 3)
22510b89ee3SMauro Carvalho Chehab 		dprintk(PREFIX "New block: ");
22610b89ee3SMauro Carvalho Chehab 
22710b89ee3SMauro Carvalho Chehab 	for (i = 0; i < 3; ++i) {
22810b89ee3SMauro Carvalho Chehab 		if (debug > 3)
22910b89ee3SMauro Carvalho Chehab 			dprintk("0x%02x ", blockbuf[i]);
23010b89ee3SMauro Carvalho Chehab 		s->buffer[s->wr_index] = blockbuf[i];
23110b89ee3SMauro Carvalho Chehab 		s->wr_index++;
23210b89ee3SMauro Carvalho Chehab 	}
23310b89ee3SMauro Carvalho Chehab 
23410b89ee3SMauro Carvalho Chehab 	if (s->wr_index >= s->buf_size)
23510b89ee3SMauro Carvalho Chehab 		s->wr_index = 0;
23610b89ee3SMauro Carvalho Chehab 
23710b89ee3SMauro Carvalho Chehab 	if (s->wr_index == s->rd_index) {
2386c3d67abSHans J. Koch 		s->rd_index += 3;
23910b89ee3SMauro Carvalho Chehab 		if (s->rd_index >= s->buf_size)
24010b89ee3SMauro Carvalho Chehab 			s->rd_index = 0;
24110b89ee3SMauro Carvalho Chehab 	} else
24210b89ee3SMauro Carvalho Chehab 		s->block_count++;
24310b89ee3SMauro Carvalho Chehab 
24410b89ee3SMauro Carvalho Chehab 	if (debug > 3)
24510b89ee3SMauro Carvalho Chehab 		dprintk("%d blocks total.\n", s->block_count);
24610b89ee3SMauro Carvalho Chehab }
24710b89ee3SMauro Carvalho Chehab 
saa6588_i2c_poll(struct saa6588 * s)24810b89ee3SMauro Carvalho Chehab static void saa6588_i2c_poll(struct saa6588 *s)
24910b89ee3SMauro Carvalho Chehab {
250c3fda7f8SHans Verkuil 	struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
25110b89ee3SMauro Carvalho Chehab 	unsigned long flags;
25210b89ee3SMauro Carvalho Chehab 	unsigned char tmpbuf[6];
25310b89ee3SMauro Carvalho Chehab 	unsigned char blocknum;
25410b89ee3SMauro Carvalho Chehab 	unsigned char tmp;
25510b89ee3SMauro Carvalho Chehab 
25610b89ee3SMauro Carvalho Chehab 	/* Although we only need 3 bytes, we have to read at least 6.
2573c86cf7aSHans Verkuil 	   SAA6588 returns garbage otherwise. */
258c3fda7f8SHans Verkuil 	if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) {
25910b89ee3SMauro Carvalho Chehab 		if (debug > 1)
26010b89ee3SMauro Carvalho Chehab 			dprintk(PREFIX "read error!\n");
26110b89ee3SMauro Carvalho Chehab 		return;
26210b89ee3SMauro Carvalho Chehab 	}
26310b89ee3SMauro Carvalho Chehab 
2643c86cf7aSHans Verkuil 	s->sync = tmpbuf[0] & 0x10;
2653c86cf7aSHans Verkuil 	if (!s->sync)
2663c86cf7aSHans Verkuil 		return;
26710b89ee3SMauro Carvalho Chehab 	blocknum = tmpbuf[0] >> 5;
26810b89ee3SMauro Carvalho Chehab 	if (blocknum == s->last_blocknum) {
26910b89ee3SMauro Carvalho Chehab 		if (debug > 3)
27010b89ee3SMauro Carvalho Chehab 			dprintk("Saw block %d again.\n", blocknum);
27110b89ee3SMauro Carvalho Chehab 		return;
27210b89ee3SMauro Carvalho Chehab 	}
27310b89ee3SMauro Carvalho Chehab 
27410b89ee3SMauro Carvalho Chehab 	s->last_blocknum = blocknum;
27510b89ee3SMauro Carvalho Chehab 
27610b89ee3SMauro Carvalho Chehab 	/*
27710b89ee3SMauro Carvalho Chehab 	   Byte order according to v4l2 specification:
27810b89ee3SMauro Carvalho Chehab 
27910b89ee3SMauro Carvalho Chehab 	   Byte 0: Least Significant Byte of RDS Block
28010b89ee3SMauro Carvalho Chehab 	   Byte 1: Most Significant Byte of RDS Block
28110b89ee3SMauro Carvalho Chehab 	   Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
28210b89ee3SMauro Carvalho Chehab 	   occurred during reception of this block.
28310b89ee3SMauro Carvalho Chehab 	   Bit 6: Corrected bit. Indicates that an error was
28410b89ee3SMauro Carvalho Chehab 	   corrected for this data block.
2853c86cf7aSHans Verkuil 	   Bits 5-3: Same as bits 0-2.
2863c86cf7aSHans Verkuil 	   Bits 2-0: Block number.
28710b89ee3SMauro Carvalho Chehab 
28810b89ee3SMauro Carvalho Chehab 	   SAA6588 byte order is Status-MSB-LSB, so we have to swap the
28910b89ee3SMauro Carvalho Chehab 	   first and the last of the 3 bytes block.
29010b89ee3SMauro Carvalho Chehab 	 */
29110b89ee3SMauro Carvalho Chehab 
292f47c183cSFabian Frederick 	swap(tmpbuf[2], tmpbuf[0]);
29310b89ee3SMauro Carvalho Chehab 
2943c86cf7aSHans Verkuil 	/* Map 'Invalid block E' to 'Invalid Block' */
2953c86cf7aSHans Verkuil 	if (blocknum == 6)
2963c86cf7aSHans Verkuil 		blocknum = V4L2_RDS_BLOCK_INVALID;
2973c86cf7aSHans Verkuil 	/* And if are not in mmbs mode, then 'Block E' is also mapped
2983c86cf7aSHans Verkuil 	   to 'Invalid Block'. As far as I can tell MMBS is discontinued,
2993c86cf7aSHans Verkuil 	   and if there is ever a need to support E blocks, then please
3003c86cf7aSHans Verkuil 	   contact the linux-media mailinglist. */
3013c86cf7aSHans Verkuil 	else if (!mmbs && blocknum == 5)
3023c86cf7aSHans Verkuil 		blocknum = V4L2_RDS_BLOCK_INVALID;
30310b89ee3SMauro Carvalho Chehab 	tmp = blocknum;
30410b89ee3SMauro Carvalho Chehab 	tmp |= blocknum << 3;	/* Received offset == Offset Name (OK ?) */
30510b89ee3SMauro Carvalho Chehab 	if ((tmpbuf[2] & 0x03) == 0x03)
3063c86cf7aSHans Verkuil 		tmp |= V4L2_RDS_BLOCK_ERROR;	 /* uncorrectable error */
30710b89ee3SMauro Carvalho Chehab 	else if ((tmpbuf[2] & 0x03) != 0x00)
3083c86cf7aSHans Verkuil 		tmp |= V4L2_RDS_BLOCK_CORRECTED; /* corrected error */
30910b89ee3SMauro Carvalho Chehab 	tmpbuf[2] = tmp;	/* Is this enough ? Should we also check other bits ? */
31010b89ee3SMauro Carvalho Chehab 
31110b89ee3SMauro Carvalho Chehab 	spin_lock_irqsave(&s->lock, flags);
31210b89ee3SMauro Carvalho Chehab 	block_to_buf(s, tmpbuf);
31310b89ee3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&s->lock, flags);
31410b89ee3SMauro Carvalho Chehab 	s->data_available_for_read = 1;
31510b89ee3SMauro Carvalho Chehab 	wake_up_interruptible(&s->read_queue);
31610b89ee3SMauro Carvalho Chehab }
31710b89ee3SMauro Carvalho Chehab 
saa6588_work(struct work_struct * work)318c4028958SDavid Howells static void saa6588_work(struct work_struct *work)
31910b89ee3SMauro Carvalho Chehab {
320fb6991d4SJean Delvare 	struct saa6588 *s = container_of(work, struct saa6588, work.work);
32110b89ee3SMauro Carvalho Chehab 
32210b89ee3SMauro Carvalho Chehab 	saa6588_i2c_poll(s);
323fb6991d4SJean Delvare 	schedule_delayed_work(&s->work, msecs_to_jiffies(20));
32410b89ee3SMauro Carvalho Chehab }
32510b89ee3SMauro Carvalho Chehab 
saa6588_configure(struct saa6588 * s)3263c86cf7aSHans Verkuil static void saa6588_configure(struct saa6588 *s)
32710b89ee3SMauro Carvalho Chehab {
328c3fda7f8SHans Verkuil 	struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
32910b89ee3SMauro Carvalho Chehab 	unsigned char buf[3];
33010b89ee3SMauro Carvalho Chehab 	int rc;
33110b89ee3SMauro Carvalho Chehab 
33210b89ee3SMauro Carvalho Chehab 	buf[0] = cSyncRestart;
3333c86cf7aSHans Verkuil 	if (mmbs)
33410b89ee3SMauro Carvalho Chehab 		buf[0] |= cProcessingModeRBDS;
33510b89ee3SMauro Carvalho Chehab 
33610b89ee3SMauro Carvalho Chehab 	buf[1] = cFlywheelDefault;
33710b89ee3SMauro Carvalho Chehab 	switch (plvl) {
33810b89ee3SMauro Carvalho Chehab 	case 0:
33910b89ee3SMauro Carvalho Chehab 		buf[1] |= cPauseLevel_11mV;
34010b89ee3SMauro Carvalho Chehab 		break;
34110b89ee3SMauro Carvalho Chehab 	case 1:
34210b89ee3SMauro Carvalho Chehab 		buf[1] |= cPauseLevel_17mV;
34310b89ee3SMauro Carvalho Chehab 		break;
34410b89ee3SMauro Carvalho Chehab 	case 2:
34510b89ee3SMauro Carvalho Chehab 		buf[1] |= cPauseLevel_27mV;
34610b89ee3SMauro Carvalho Chehab 		break;
34710b89ee3SMauro Carvalho Chehab 	case 3:
34810b89ee3SMauro Carvalho Chehab 		buf[1] |= cPauseLevel_43mV;
34910b89ee3SMauro Carvalho Chehab 		break;
35010b89ee3SMauro Carvalho Chehab 	default:		/* nothing */
35110b89ee3SMauro Carvalho Chehab 		break;
35210b89ee3SMauro Carvalho Chehab 	}
35310b89ee3SMauro Carvalho Chehab 
35410b89ee3SMauro Carvalho Chehab 	buf[2] = cQualityDetectDefault | cSelectOscFreqON;
35510b89ee3SMauro Carvalho Chehab 
35610b89ee3SMauro Carvalho Chehab 	switch (xtal) {
35710b89ee3SMauro Carvalho Chehab 	case 0:
35810b89ee3SMauro Carvalho Chehab 		buf[2] |= cOscFreq_4332kHz;
35910b89ee3SMauro Carvalho Chehab 		break;
36010b89ee3SMauro Carvalho Chehab 	case 1:
36110b89ee3SMauro Carvalho Chehab 		buf[2] |= cOscFreq_8664kHz;
36210b89ee3SMauro Carvalho Chehab 		break;
36310b89ee3SMauro Carvalho Chehab 	case 2:
36410b89ee3SMauro Carvalho Chehab 		buf[2] |= cOscFreq_12996kHz;
36510b89ee3SMauro Carvalho Chehab 		break;
36610b89ee3SMauro Carvalho Chehab 	case 3:
36710b89ee3SMauro Carvalho Chehab 		buf[2] |= cOscFreq_17328kHz;
36810b89ee3SMauro Carvalho Chehab 		break;
36910b89ee3SMauro Carvalho Chehab 	default:		/* nothing */
37010b89ee3SMauro Carvalho Chehab 		break;
37110b89ee3SMauro Carvalho Chehab 	}
37210b89ee3SMauro Carvalho Chehab 
37310b89ee3SMauro Carvalho Chehab 	dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
37410b89ee3SMauro Carvalho Chehab 		buf[0], buf[1], buf[2]);
37510b89ee3SMauro Carvalho Chehab 
376c3fda7f8SHans Verkuil 	rc = i2c_master_send(client, buf, 3);
377b5ffc223SHans Verkuil 	if (rc != 3)
37810b89ee3SMauro Carvalho Chehab 		printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
37910b89ee3SMauro Carvalho Chehab }
38010b89ee3SMauro Carvalho Chehab 
38110b89ee3SMauro Carvalho Chehab /* ---------------------------------------------------------------------- */
38210b89ee3SMauro Carvalho Chehab 
saa6588_command(struct v4l2_subdev * sd,unsigned int cmd,void * arg)3830a7790beSArnd Bergmann static long saa6588_command(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
38410b89ee3SMauro Carvalho Chehab {
385c3fda7f8SHans Verkuil 	struct saa6588 *s = to_saa6588(sd);
386b9218f2fSHans Verkuil 	struct saa6588_command *a = arg;
38710b89ee3SMauro Carvalho Chehab 
38810b89ee3SMauro Carvalho Chehab 	switch (cmd) {
38910b89ee3SMauro Carvalho Chehab 		/* --- close() for /dev/radio --- */
390b9218f2fSHans Verkuil 	case SAA6588_CMD_CLOSE:
39110b89ee3SMauro Carvalho Chehab 		s->data_available_for_read = 1;
39210b89ee3SMauro Carvalho Chehab 		wake_up_interruptible(&s->read_queue);
393af2c5debSHans Verkuil 		s->data_available_for_read = 0;
39410b89ee3SMauro Carvalho Chehab 		a->result = 0;
39510b89ee3SMauro Carvalho Chehab 		break;
39610b89ee3SMauro Carvalho Chehab 		/* --- read() for /dev/radio --- */
397b9218f2fSHans Verkuil 	case SAA6588_CMD_READ:
39810b89ee3SMauro Carvalho Chehab 		read_from_buf(s, a);
39910b89ee3SMauro Carvalho Chehab 		break;
40010b89ee3SMauro Carvalho Chehab 		/* --- poll() for /dev/radio --- */
401b9218f2fSHans Verkuil 	case SAA6588_CMD_POLL:
40237b3c6a6SAl Viro 		a->poll_mask = 0;
40309092787SHans Verkuil 		if (s->data_available_for_read)
40437b3c6a6SAl Viro 			a->poll_mask |= EPOLLIN | EPOLLRDNORM;
40510b89ee3SMauro Carvalho Chehab 		poll_wait(a->instance, &s->read_queue, a->event_list);
40610b89ee3SMauro Carvalho Chehab 		break;
40710b89ee3SMauro Carvalho Chehab 
40810b89ee3SMauro Carvalho Chehab 	default:
40910b89ee3SMauro Carvalho Chehab 		/* nothing */
410c3fda7f8SHans Verkuil 		return -ENOIOCTLCMD;
41110b89ee3SMauro Carvalho Chehab 	}
41210b89ee3SMauro Carvalho Chehab 	return 0;
41310b89ee3SMauro Carvalho Chehab }
41410b89ee3SMauro Carvalho Chehab 
saa6588_g_tuner(struct v4l2_subdev * sd,struct v4l2_tuner * vt)4153c86cf7aSHans Verkuil static int saa6588_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
4163c86cf7aSHans Verkuil {
4173c86cf7aSHans Verkuil 	struct saa6588 *s = to_saa6588(sd);
4183c86cf7aSHans Verkuil 
419cb0ed222SMatti Aaltonen 	vt->capability |= V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO;
4203c86cf7aSHans Verkuil 	if (s->sync)
4213c86cf7aSHans Verkuil 		vt->rxsubchans |= V4L2_TUNER_SUB_RDS;
4223c86cf7aSHans Verkuil 	return 0;
4233c86cf7aSHans Verkuil }
4243c86cf7aSHans Verkuil 
saa6588_s_tuner(struct v4l2_subdev * sd,const struct v4l2_tuner * vt)4252f73c7c5SHans Verkuil static int saa6588_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
4263c86cf7aSHans Verkuil {
4273c86cf7aSHans Verkuil 	struct saa6588 *s = to_saa6588(sd);
4283c86cf7aSHans Verkuil 
4293c86cf7aSHans Verkuil 	saa6588_configure(s);
4303c86cf7aSHans Verkuil 	return 0;
4313c86cf7aSHans Verkuil }
4323c86cf7aSHans Verkuil 
433c3fda7f8SHans Verkuil /* ----------------------------------------------------------------------- */
434c3fda7f8SHans Verkuil 
435c3fda7f8SHans Verkuil static const struct v4l2_subdev_core_ops saa6588_core_ops = {
4360a7790beSArnd Bergmann 	.command = saa6588_command,
437c3fda7f8SHans Verkuil };
438c3fda7f8SHans Verkuil 
4393c86cf7aSHans Verkuil static const struct v4l2_subdev_tuner_ops saa6588_tuner_ops = {
4403c86cf7aSHans Verkuil 	.g_tuner = saa6588_g_tuner,
4413c86cf7aSHans Verkuil 	.s_tuner = saa6588_s_tuner,
4423c86cf7aSHans Verkuil };
4433c86cf7aSHans Verkuil 
444c3fda7f8SHans Verkuil static const struct v4l2_subdev_ops saa6588_ops = {
445c3fda7f8SHans Verkuil 	.core = &saa6588_core_ops,
4463c86cf7aSHans Verkuil 	.tuner = &saa6588_tuner_ops,
447c3fda7f8SHans Verkuil };
448c3fda7f8SHans Verkuil 
449c3fda7f8SHans Verkuil /* ---------------------------------------------------------------------- */
450c3fda7f8SHans Verkuil 
saa6588_probe(struct i2c_client * client)4510764554cSUwe Kleine-König static int saa6588_probe(struct i2c_client *client)
452c3fda7f8SHans Verkuil {
453c3fda7f8SHans Verkuil 	struct saa6588 *s;
454c3fda7f8SHans Verkuil 	struct v4l2_subdev *sd;
455c3fda7f8SHans Verkuil 
456c3fda7f8SHans Verkuil 	v4l_info(client, "saa6588 found @ 0x%x (%s)\n",
457c3fda7f8SHans Verkuil 			client->addr << 1, client->adapter->name);
458c3fda7f8SHans Verkuil 
459c02b211dSLaurent Pinchart 	s = devm_kzalloc(&client->dev, sizeof(*s), GFP_KERNEL);
460c3fda7f8SHans Verkuil 	if (s == NULL)
461c3fda7f8SHans Verkuil 		return -ENOMEM;
462c3fda7f8SHans Verkuil 
463c3fda7f8SHans Verkuil 	s->buf_size = bufblocks * 3;
464c3fda7f8SHans Verkuil 
465c02b211dSLaurent Pinchart 	s->buffer = devm_kzalloc(&client->dev, s->buf_size, GFP_KERNEL);
466c02b211dSLaurent Pinchart 	if (s->buffer == NULL)
467c3fda7f8SHans Verkuil 		return -ENOMEM;
468c3fda7f8SHans Verkuil 	sd = &s->sd;
469c3fda7f8SHans Verkuil 	v4l2_i2c_subdev_init(sd, client, &saa6588_ops);
470c3fda7f8SHans Verkuil 	spin_lock_init(&s->lock);
471c3fda7f8SHans Verkuil 	s->block_count = 0;
472c3fda7f8SHans Verkuil 	s->wr_index = 0;
473c3fda7f8SHans Verkuil 	s->rd_index = 0;
474c3fda7f8SHans Verkuil 	s->last_blocknum = 0xff;
475c3fda7f8SHans Verkuil 	init_waitqueue_head(&s->read_queue);
476c3fda7f8SHans Verkuil 	s->data_available_for_read = 0;
477c3fda7f8SHans Verkuil 
478c3fda7f8SHans Verkuil 	saa6588_configure(s);
479c3fda7f8SHans Verkuil 
480c3fda7f8SHans Verkuil 	/* start polling via eventd */
481fb6991d4SJean Delvare 	INIT_DELAYED_WORK(&s->work, saa6588_work);
482fb6991d4SJean Delvare 	schedule_delayed_work(&s->work, 0);
483c3fda7f8SHans Verkuil 	return 0;
484c3fda7f8SHans Verkuil }
485c3fda7f8SHans Verkuil 
saa6588_remove(struct i2c_client * client)486ed5c2f5fSUwe Kleine-König static void saa6588_remove(struct i2c_client *client)
487c3fda7f8SHans Verkuil {
488c3fda7f8SHans Verkuil 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
489c3fda7f8SHans Verkuil 	struct saa6588 *s = to_saa6588(sd);
490c3fda7f8SHans Verkuil 
491c3fda7f8SHans Verkuil 	v4l2_device_unregister_subdev(sd);
492c3fda7f8SHans Verkuil 
493fb6991d4SJean Delvare 	cancel_delayed_work_sync(&s->work);
494c3fda7f8SHans Verkuil }
495c3fda7f8SHans Verkuil 
49610b89ee3SMauro Carvalho Chehab /* ----------------------------------------------------------------------- */
49710b89ee3SMauro Carvalho Chehab 
498b5ffc223SHans Verkuil static const struct i2c_device_id saa6588_id[] = {
499cc4cbd4bSUwe Kleine-König 	{ "saa6588" },
500b5ffc223SHans Verkuil 	{ }
501b5ffc223SHans Verkuil };
502b5ffc223SHans Verkuil MODULE_DEVICE_TABLE(i2c, saa6588_id);
503b5ffc223SHans Verkuil 
504440d0516SHans Verkuil static struct i2c_driver saa6588_driver = {
505440d0516SHans Verkuil 	.driver = {
506cab462f7SMauro Carvalho Chehab 		.name	= "saa6588",
507440d0516SHans Verkuil 	},
508aaeb31c0SUwe Kleine-König 	.probe		= saa6588_probe,
509b5ffc223SHans Verkuil 	.remove		= saa6588_remove,
510b5ffc223SHans Verkuil 	.id_table	= saa6588_id,
51110b89ee3SMauro Carvalho Chehab };
512440d0516SHans Verkuil 
513c6e8d86fSAxel Lin module_i2c_driver(saa6588_driver);
514