1b09cd163SJoonyoung Shim /*
2b09cd163SJoonyoung Shim  *  drivers/media/radio/si470x/radio-si470x.h
3b09cd163SJoonyoung Shim  *
4b09cd163SJoonyoung Shim  *  Driver for radios with Silicon Labs Si470x FM Radio Receivers
5b09cd163SJoonyoung Shim  *
6b09cd163SJoonyoung Shim  *  Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
7b09cd163SJoonyoung Shim  *
8b09cd163SJoonyoung Shim  * This program is free software; you can redistribute it and/or modify
9b09cd163SJoonyoung Shim  * it under the terms of the GNU General Public License as published by
10b09cd163SJoonyoung Shim  * the Free Software Foundation; either version 2 of the License, or
11b09cd163SJoonyoung Shim  * (at your option) any later version.
12b09cd163SJoonyoung Shim  *
13b09cd163SJoonyoung Shim  * This program is distributed in the hope that it will be useful,
14b09cd163SJoonyoung Shim  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15b09cd163SJoonyoung Shim  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16b09cd163SJoonyoung Shim  * GNU General Public License for more details.
17b09cd163SJoonyoung Shim  *
18b09cd163SJoonyoung Shim  * You should have received a copy of the GNU General Public License
19b09cd163SJoonyoung Shim  * along with this program; if not, write to the Free Software
20b09cd163SJoonyoung Shim  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21b09cd163SJoonyoung Shim  */
22b09cd163SJoonyoung Shim 
23b09cd163SJoonyoung Shim 
24b09cd163SJoonyoung Shim /* driver definitions */
25b09cd163SJoonyoung Shim #define DRIVER_NAME "radio-si470x"
26b09cd163SJoonyoung Shim 
27b09cd163SJoonyoung Shim 
28b09cd163SJoonyoung Shim /* kernel includes */
29b09cd163SJoonyoung Shim #include <linux/kernel.h>
30b09cd163SJoonyoung Shim #include <linux/module.h>
31b09cd163SJoonyoung Shim #include <linux/init.h>
32788f6febSRandy Dunlap #include <linux/sched.h>
33b09cd163SJoonyoung Shim #include <linux/slab.h>
34b09cd163SJoonyoung Shim #include <linux/input.h>
35b09cd163SJoonyoung Shim #include <linux/videodev2.h>
36b09cd163SJoonyoung Shim #include <linux/mutex.h>
37b09cd163SJoonyoung Shim #include <media/v4l2-common.h>
38b09cd163SJoonyoung Shim #include <media/v4l2-ioctl.h>
394967d53dSHans Verkuil #include <media/v4l2-ctrls.h>
404967d53dSHans Verkuil #include <media/v4l2-event.h>
414967d53dSHans Verkuil #include <media/v4l2-device.h>
42b09cd163SJoonyoung Shim #include <asm/unaligned.h>
43b09cd163SJoonyoung Shim 
44b09cd163SJoonyoung Shim 
459dcb79c2STobias Lorenz 
46b09cd163SJoonyoung Shim /**************************************************************************
47b09cd163SJoonyoung Shim  * Register Definitions
48b09cd163SJoonyoung Shim  **************************************************************************/
49b09cd163SJoonyoung Shim #define RADIO_REGISTER_SIZE	2	/* 16 register bit width */
50b09cd163SJoonyoung Shim #define RADIO_REGISTER_NUM	16	/* DEVICEID   ... RDSD */
51b09cd163SJoonyoung Shim #define RDS_REGISTER_NUM	6	/* STATUSRSSI ... RDSD */
52b09cd163SJoonyoung Shim 
53b09cd163SJoonyoung Shim #define DEVICEID		0	/* Device ID */
54b09cd163SJoonyoung Shim #define DEVICEID_PN		0xf000	/* bits 15..12: Part Number */
55b09cd163SJoonyoung Shim #define DEVICEID_MFGID		0x0fff	/* bits 11..00: Manufacturer ID */
56b09cd163SJoonyoung Shim 
57dd7a2acfSMauro Carvalho Chehab #define SI_CHIPID		1	/* Chip ID */
58dd7a2acfSMauro Carvalho Chehab #define SI_CHIPID_REV		0xfc00	/* bits 15..10: Chip Version */
59dd7a2acfSMauro Carvalho Chehab #define SI_CHIPID_DEV		0x0200	/* bits 09..09: Device */
60dd7a2acfSMauro Carvalho Chehab #define SI_CHIPID_FIRMWARE	0x01ff	/* bits 08..00: Firmware Version */
61b09cd163SJoonyoung Shim 
62b09cd163SJoonyoung Shim #define POWERCFG		2	/* Power Configuration */
63b09cd163SJoonyoung Shim #define POWERCFG_DSMUTE		0x8000	/* bits 15..15: Softmute Disable */
64b09cd163SJoonyoung Shim #define POWERCFG_DMUTE		0x4000	/* bits 14..14: Mute Disable */
65b09cd163SJoonyoung Shim #define POWERCFG_MONO		0x2000	/* bits 13..13: Mono Select */
66b09cd163SJoonyoung Shim #define POWERCFG_RDSM		0x0800	/* bits 11..11: RDS Mode (Si4701 only) */
67b09cd163SJoonyoung Shim #define POWERCFG_SKMODE		0x0400	/* bits 10..10: Seek Mode */
68b09cd163SJoonyoung Shim #define POWERCFG_SEEKUP		0x0200	/* bits 09..09: Seek Direction */
69b09cd163SJoonyoung Shim #define POWERCFG_SEEK		0x0100	/* bits 08..08: Seek */
70b09cd163SJoonyoung Shim #define POWERCFG_DISABLE	0x0040	/* bits 06..06: Powerup Disable */
71b09cd163SJoonyoung Shim #define POWERCFG_ENABLE		0x0001	/* bits 00..00: Powerup Enable */
72b09cd163SJoonyoung Shim 
73b09cd163SJoonyoung Shim #define CHANNEL			3	/* Channel */
74b09cd163SJoonyoung Shim #define CHANNEL_TUNE		0x8000	/* bits 15..15: Tune */
75b09cd163SJoonyoung Shim #define CHANNEL_CHAN		0x03ff	/* bits 09..00: Channel Select */
76b09cd163SJoonyoung Shim 
77b09cd163SJoonyoung Shim #define SYSCONFIG1		4	/* System Configuration 1 */
78b09cd163SJoonyoung Shim #define SYSCONFIG1_RDSIEN	0x8000	/* bits 15..15: RDS Interrupt Enable (Si4701 only) */
79b09cd163SJoonyoung Shim #define SYSCONFIG1_STCIEN	0x4000	/* bits 14..14: Seek/Tune Complete Interrupt Enable */
80b09cd163SJoonyoung Shim #define SYSCONFIG1_RDS		0x1000	/* bits 12..12: RDS Enable (Si4701 only) */
81b09cd163SJoonyoung Shim #define SYSCONFIG1_DE		0x0800	/* bits 11..11: De-emphasis (0=75us 1=50us) */
82b09cd163SJoonyoung Shim #define SYSCONFIG1_AGCD		0x0400	/* bits 10..10: AGC Disable */
83b09cd163SJoonyoung Shim #define SYSCONFIG1_BLNDADJ	0x00c0	/* bits 07..06: Stereo/Mono Blend Level Adjustment */
84b09cd163SJoonyoung Shim #define SYSCONFIG1_GPIO3	0x0030	/* bits 05..04: General Purpose I/O 3 */
85b09cd163SJoonyoung Shim #define SYSCONFIG1_GPIO2	0x000c	/* bits 03..02: General Purpose I/O 2 */
86b09cd163SJoonyoung Shim #define SYSCONFIG1_GPIO1	0x0003	/* bits 01..00: General Purpose I/O 1 */
87b09cd163SJoonyoung Shim 
88b09cd163SJoonyoung Shim #define SYSCONFIG2		5	/* System Configuration 2 */
89b09cd163SJoonyoung Shim #define SYSCONFIG2_SEEKTH	0xff00	/* bits 15..08: RSSI Seek Threshold */
908d8c1b37SHans de Goede #define SYSCONFIG2_BAND		0x00c0	/* bits 07..06: Band Select */
91b09cd163SJoonyoung Shim #define SYSCONFIG2_SPACE	0x0030	/* bits 05..04: Channel Spacing */
92b09cd163SJoonyoung Shim #define SYSCONFIG2_VOLUME	0x000f	/* bits 03..00: Volume */
93b09cd163SJoonyoung Shim 
94b09cd163SJoonyoung Shim #define SYSCONFIG3		6	/* System Configuration 3 */
95b09cd163SJoonyoung Shim #define SYSCONFIG3_SMUTER	0xc000	/* bits 15..14: Softmute Attack/Recover Rate */
96b09cd163SJoonyoung Shim #define SYSCONFIG3_SMUTEA	0x3000	/* bits 13..12: Softmute Attenuation */
97b09cd163SJoonyoung Shim #define SYSCONFIG3_SKSNR	0x00f0	/* bits 07..04: Seek SNR Threshold */
98b09cd163SJoonyoung Shim #define SYSCONFIG3_SKCNT	0x000f	/* bits 03..00: Seek FM Impulse Detection Threshold */
99b09cd163SJoonyoung Shim 
100b09cd163SJoonyoung Shim #define TEST1			7	/* Test 1 */
101b09cd163SJoonyoung Shim #define TEST1_AHIZEN		0x4000	/* bits 14..14: Audio High-Z Enable */
102b09cd163SJoonyoung Shim 
103b09cd163SJoonyoung Shim #define TEST2			8	/* Test 2 */
104b09cd163SJoonyoung Shim /* TEST2 only contains reserved bits */
105b09cd163SJoonyoung Shim 
106b09cd163SJoonyoung Shim #define BOOTCONFIG		9	/* Boot Configuration */
107b09cd163SJoonyoung Shim /* BOOTCONFIG only contains reserved bits */
108b09cd163SJoonyoung Shim 
109b09cd163SJoonyoung Shim #define STATUSRSSI		10	/* Status RSSI */
110b09cd163SJoonyoung Shim #define STATUSRSSI_RDSR		0x8000	/* bits 15..15: RDS Ready (Si4701 only) */
111b09cd163SJoonyoung Shim #define STATUSRSSI_STC		0x4000	/* bits 14..14: Seek/Tune Complete */
112b09cd163SJoonyoung Shim #define STATUSRSSI_SF		0x2000	/* bits 13..13: Seek Fail/Band Limit */
113b09cd163SJoonyoung Shim #define STATUSRSSI_AFCRL	0x1000	/* bits 12..12: AFC Rail */
114b09cd163SJoonyoung Shim #define STATUSRSSI_RDSS		0x0800	/* bits 11..11: RDS Synchronized (Si4701 only) */
115b09cd163SJoonyoung Shim #define STATUSRSSI_BLERA	0x0600	/* bits 10..09: RDS Block A Errors (Si4701 only) */
116b09cd163SJoonyoung Shim #define STATUSRSSI_ST		0x0100	/* bits 08..08: Stereo Indicator */
117b09cd163SJoonyoung Shim #define STATUSRSSI_RSSI		0x00ff	/* bits 07..00: RSSI (Received Signal Strength Indicator) */
118b09cd163SJoonyoung Shim 
119b09cd163SJoonyoung Shim #define READCHAN		11	/* Read Channel */
120b09cd163SJoonyoung Shim #define READCHAN_BLERB		0xc000	/* bits 15..14: RDS Block D Errors (Si4701 only) */
121b09cd163SJoonyoung Shim #define READCHAN_BLERC		0x3000	/* bits 13..12: RDS Block C Errors (Si4701 only) */
122b09cd163SJoonyoung Shim #define READCHAN_BLERD		0x0c00	/* bits 11..10: RDS Block B Errors (Si4701 only) */
123b09cd163SJoonyoung Shim #define READCHAN_READCHAN	0x03ff	/* bits 09..00: Read Channel */
124b09cd163SJoonyoung Shim 
125b09cd163SJoonyoung Shim #define RDSA			12	/* RDSA */
126b09cd163SJoonyoung Shim #define RDSA_RDSA		0xffff	/* bits 15..00: RDS Block A Data (Si4701 only) */
127b09cd163SJoonyoung Shim 
128b09cd163SJoonyoung Shim #define RDSB			13	/* RDSB */
129b09cd163SJoonyoung Shim #define RDSB_RDSB		0xffff	/* bits 15..00: RDS Block B Data (Si4701 only) */
130b09cd163SJoonyoung Shim 
131b09cd163SJoonyoung Shim #define RDSC			14	/* RDSC */
132b09cd163SJoonyoung Shim #define RDSC_RDSC		0xffff	/* bits 15..00: RDS Block C Data (Si4701 only) */
133b09cd163SJoonyoung Shim 
134b09cd163SJoonyoung Shim #define RDSD			15	/* RDSD */
135b09cd163SJoonyoung Shim #define RDSD_RDSD		0xffff	/* bits 15..00: RDS Block D Data (Si4701 only) */
136b09cd163SJoonyoung Shim 
137b09cd163SJoonyoung Shim 
1389dcb79c2STobias Lorenz 
139b09cd163SJoonyoung Shim /**************************************************************************
140b09cd163SJoonyoung Shim  * General Driver Definitions
141b09cd163SJoonyoung Shim  **************************************************************************/
142b09cd163SJoonyoung Shim 
143b09cd163SJoonyoung Shim /*
144b09cd163SJoonyoung Shim  * si470x_device - private data
145b09cd163SJoonyoung Shim  */
146b09cd163SJoonyoung Shim struct si470x_device {
1474967d53dSHans Verkuil 	struct v4l2_device v4l2_dev;
1484967d53dSHans Verkuil 	struct video_device videodev;
1494967d53dSHans Verkuil 	struct v4l2_ctrl_handler hdl;
150f140612dSHans de Goede 	int band;
1519dcb79c2STobias Lorenz 
1529dcb79c2STobias Lorenz 	/* Silabs internal registers (0..15) */
1539dcb79c2STobias Lorenz 	unsigned short registers[RADIO_REGISTER_NUM];
1549dcb79c2STobias Lorenz 
1559dcb79c2STobias Lorenz 	/* RDS receive buffer */
1569dcb79c2STobias Lorenz 	wait_queue_head_t read_queue;
1579dcb79c2STobias Lorenz 	struct mutex lock;		/* buffer locking */
1589dcb79c2STobias Lorenz 	unsigned char *buffer;		/* size is always multiple of three */
1599dcb79c2STobias Lorenz 	unsigned int buf_size;
1609dcb79c2STobias Lorenz 	unsigned int rd_index;
1619dcb79c2STobias Lorenz 	unsigned int wr_index;
162cc35bbddSJoonyoung Shim 
1630830be3fSJoonyoung Shim 	struct completion completion;
16486ef3f78SHans de Goede 	bool status_rssi_auto_update;	/* Does RSSI get updated automatic? */
1650830be3fSJoonyoung Shim 
166480d3ca1SPeter Senna Tschudin #if IS_ENABLED(CONFIG_USB_SI470X)
167b09cd163SJoonyoung Shim 	/* reference to USB and video device */
168b09cd163SJoonyoung Shim 	struct usb_device *usbdev;
169b09cd163SJoonyoung Shim 	struct usb_interface *intf;
170567e2e96SHans Verkuil 	char *usb_buf;
171b09cd163SJoonyoung Shim 
172b09cd163SJoonyoung Shim 	/* Interrupt endpoint handling */
173b09cd163SJoonyoung Shim 	char *int_in_buffer;
174b09cd163SJoonyoung Shim 	struct usb_endpoint_descriptor *int_in_endpoint;
175b09cd163SJoonyoung Shim 	struct urb *int_in_urb;
176b09cd163SJoonyoung Shim 	int int_in_running;
177b09cd163SJoonyoung Shim 
178b09cd163SJoonyoung Shim 	/* scratch page */
179b09cd163SJoonyoung Shim 	unsigned char software_version;
180b09cd163SJoonyoung Shim 	unsigned char hardware_version;
181cc35bbddSJoonyoung Shim #endif
182b09cd163SJoonyoung Shim 
183480d3ca1SPeter Senna Tschudin #if IS_ENABLED(CONFIG_I2C_SI470X)
1849dcb79c2STobias Lorenz 	struct i2c_client *client;
1859dcb79c2STobias Lorenz #endif
186b09cd163SJoonyoung Shim };
187b09cd163SJoonyoung Shim 
188b09cd163SJoonyoung Shim 
1899dcb79c2STobias Lorenz 
1909dcb79c2STobias Lorenz /**************************************************************************
1919dcb79c2STobias Lorenz  * Firmware Versions
1929dcb79c2STobias Lorenz  **************************************************************************/
1939dcb79c2STobias Lorenz 
194db3912c0SHans de Goede #define RADIO_FW_VERSION	12
1959dcb79c2STobias Lorenz 
1969dcb79c2STobias Lorenz 
1979dcb79c2STobias Lorenz 
1989dcb79c2STobias Lorenz /**************************************************************************
1999dcb79c2STobias Lorenz  * Frequency Multiplicator
2009dcb79c2STobias Lorenz  **************************************************************************/
2019dcb79c2STobias Lorenz 
202b09cd163SJoonyoung Shim /*
203b09cd163SJoonyoung Shim  * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
204b09cd163SJoonyoung Shim  * 62.5 kHz otherwise.
205b09cd163SJoonyoung Shim  * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
206b09cd163SJoonyoung Shim  * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
207b09cd163SJoonyoung Shim  * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
208b09cd163SJoonyoung Shim  */
209b09cd163SJoonyoung Shim #define FREQ_MUL (1000000 / 62.5)
210b09cd163SJoonyoung Shim 
211b09cd163SJoonyoung Shim 
212b09cd163SJoonyoung Shim 
213b09cd163SJoonyoung Shim /**************************************************************************
214b09cd163SJoonyoung Shim  * Common Functions
215b09cd163SJoonyoung Shim  **************************************************************************/
216b09cd163SJoonyoung Shim extern struct video_device si470x_viddev_template;
2174967d53dSHans Verkuil extern const struct v4l2_ctrl_ops si470x_ctrl_ops;
218b09cd163SJoonyoung Shim int si470x_get_register(struct si470x_device *radio, int regnr);
219b09cd163SJoonyoung Shim int si470x_set_register(struct si470x_device *radio, int regnr);
2203c7cc8d5SJoonyoung Shim int si470x_disconnect_check(struct si470x_device *radio);
221b09cd163SJoonyoung Shim int si470x_set_freq(struct si470x_device *radio, unsigned int freq);
222b09cd163SJoonyoung Shim int si470x_start(struct si470x_device *radio);
223b09cd163SJoonyoung Shim int si470x_stop(struct si470x_device *radio);
2241aa925c9SJoonyoung Shim int si470x_fops_open(struct file *file);
2251aa925c9SJoonyoung Shim int si470x_fops_release(struct file *file);
226b09cd163SJoonyoung Shim int si470x_vidioc_querycap(struct file *file, void *priv,
227b09cd163SJoonyoung Shim 		struct v4l2_capability *capability);
228