1c942fddfSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b09cd163SJoonyoung Shim /*
3b09cd163SJoonyoung Shim  *  drivers/media/radio/si470x/radio-si470x.h
4b09cd163SJoonyoung Shim  *
5b09cd163SJoonyoung Shim  *  Driver for radios with Silicon Labs Si470x FM Radio Receivers
6b09cd163SJoonyoung Shim  *
7b09cd163SJoonyoung Shim  *  Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
8b09cd163SJoonyoung Shim  */
9b09cd163SJoonyoung Shim 
10b09cd163SJoonyoung Shim 
11b09cd163SJoonyoung Shim /* driver definitions */
12b09cd163SJoonyoung Shim #define DRIVER_NAME "radio-si470x"
13b09cd163SJoonyoung Shim 
14b09cd163SJoonyoung Shim 
15b09cd163SJoonyoung Shim /* kernel includes */
16b09cd163SJoonyoung Shim #include <linux/kernel.h>
17b09cd163SJoonyoung Shim #include <linux/module.h>
18b09cd163SJoonyoung Shim #include <linux/init.h>
19788f6febSRandy Dunlap #include <linux/sched.h>
20b09cd163SJoonyoung Shim #include <linux/slab.h>
21b09cd163SJoonyoung Shim #include <linux/input.h>
22b09cd163SJoonyoung Shim #include <linux/videodev2.h>
23b09cd163SJoonyoung Shim #include <linux/mutex.h>
24b09cd163SJoonyoung Shim #include <media/v4l2-common.h>
25b09cd163SJoonyoung Shim #include <media/v4l2-ioctl.h>
264967d53dSHans Verkuil #include <media/v4l2-ctrls.h>
274967d53dSHans Verkuil #include <media/v4l2-event.h>
284967d53dSHans Verkuil #include <media/v4l2-device.h>
29b09cd163SJoonyoung Shim #include <asm/unaligned.h>
30b09cd163SJoonyoung Shim 
31b09cd163SJoonyoung Shim 
329dcb79c2STobias Lorenz 
33b09cd163SJoonyoung Shim /**************************************************************************
34b09cd163SJoonyoung Shim  * Register Definitions
35b09cd163SJoonyoung Shim  **************************************************************************/
36b09cd163SJoonyoung Shim #define RADIO_REGISTER_SIZE	2	/* 16 register bit width */
37b09cd163SJoonyoung Shim #define RADIO_REGISTER_NUM	16	/* DEVICEID   ... RDSD */
38b09cd163SJoonyoung Shim #define RDS_REGISTER_NUM	6	/* STATUSRSSI ... RDSD */
39b09cd163SJoonyoung Shim 
40b09cd163SJoonyoung Shim #define DEVICEID		0	/* Device ID */
41b09cd163SJoonyoung Shim #define DEVICEID_PN		0xf000	/* bits 15..12: Part Number */
42b09cd163SJoonyoung Shim #define DEVICEID_MFGID		0x0fff	/* bits 11..00: Manufacturer ID */
43b09cd163SJoonyoung Shim 
44dd7a2acfSMauro Carvalho Chehab #define SI_CHIPID		1	/* Chip ID */
45dd7a2acfSMauro Carvalho Chehab #define SI_CHIPID_REV		0xfc00	/* bits 15..10: Chip Version */
46dd7a2acfSMauro Carvalho Chehab #define SI_CHIPID_DEV		0x0200	/* bits 09..09: Device */
47dd7a2acfSMauro Carvalho Chehab #define SI_CHIPID_FIRMWARE	0x01ff	/* bits 08..00: Firmware Version */
48b09cd163SJoonyoung Shim 
49b09cd163SJoonyoung Shim #define POWERCFG		2	/* Power Configuration */
50b09cd163SJoonyoung Shim #define POWERCFG_DSMUTE		0x8000	/* bits 15..15: Softmute Disable */
51b09cd163SJoonyoung Shim #define POWERCFG_DMUTE		0x4000	/* bits 14..14: Mute Disable */
52b09cd163SJoonyoung Shim #define POWERCFG_MONO		0x2000	/* bits 13..13: Mono Select */
53b09cd163SJoonyoung Shim #define POWERCFG_RDSM		0x0800	/* bits 11..11: RDS Mode (Si4701 only) */
54b09cd163SJoonyoung Shim #define POWERCFG_SKMODE		0x0400	/* bits 10..10: Seek Mode */
55b09cd163SJoonyoung Shim #define POWERCFG_SEEKUP		0x0200	/* bits 09..09: Seek Direction */
56b09cd163SJoonyoung Shim #define POWERCFG_SEEK		0x0100	/* bits 08..08: Seek */
57b09cd163SJoonyoung Shim #define POWERCFG_DISABLE	0x0040	/* bits 06..06: Powerup Disable */
58b09cd163SJoonyoung Shim #define POWERCFG_ENABLE		0x0001	/* bits 00..00: Powerup Enable */
59b09cd163SJoonyoung Shim 
60b09cd163SJoonyoung Shim #define CHANNEL			3	/* Channel */
61b09cd163SJoonyoung Shim #define CHANNEL_TUNE		0x8000	/* bits 15..15: Tune */
62b09cd163SJoonyoung Shim #define CHANNEL_CHAN		0x03ff	/* bits 09..00: Channel Select */
63b09cd163SJoonyoung Shim 
64b09cd163SJoonyoung Shim #define SYSCONFIG1		4	/* System Configuration 1 */
65b09cd163SJoonyoung Shim #define SYSCONFIG1_RDSIEN	0x8000	/* bits 15..15: RDS Interrupt Enable (Si4701 only) */
66b09cd163SJoonyoung Shim #define SYSCONFIG1_STCIEN	0x4000	/* bits 14..14: Seek/Tune Complete Interrupt Enable */
67b09cd163SJoonyoung Shim #define SYSCONFIG1_RDS		0x1000	/* bits 12..12: RDS Enable (Si4701 only) */
68b09cd163SJoonyoung Shim #define SYSCONFIG1_DE		0x0800	/* bits 11..11: De-emphasis (0=75us 1=50us) */
69b09cd163SJoonyoung Shim #define SYSCONFIG1_AGCD		0x0400	/* bits 10..10: AGC Disable */
70b09cd163SJoonyoung Shim #define SYSCONFIG1_BLNDADJ	0x00c0	/* bits 07..06: Stereo/Mono Blend Level Adjustment */
71b09cd163SJoonyoung Shim #define SYSCONFIG1_GPIO3	0x0030	/* bits 05..04: General Purpose I/O 3 */
72b09cd163SJoonyoung Shim #define SYSCONFIG1_GPIO2	0x000c	/* bits 03..02: General Purpose I/O 2 */
73dd328275SDouglas Fischer #define SYSCONFIG1_GPIO2_DIS	0x0000	/* Disable GPIO 2 interrupt */
74dd328275SDouglas Fischer #define SYSCONFIG1_GPIO2_INT	0x0004	/* Enable STC/RDS interrupt */
75b09cd163SJoonyoung Shim #define SYSCONFIG1_GPIO1	0x0003	/* bits 01..00: General Purpose I/O 1 */
76b09cd163SJoonyoung Shim 
77b09cd163SJoonyoung Shim #define SYSCONFIG2		5	/* System Configuration 2 */
78b09cd163SJoonyoung Shim #define SYSCONFIG2_SEEKTH	0xff00	/* bits 15..08: RSSI Seek Threshold */
798d8c1b37SHans de Goede #define SYSCONFIG2_BAND		0x00c0	/* bits 07..06: Band Select */
80b09cd163SJoonyoung Shim #define SYSCONFIG2_SPACE	0x0030	/* bits 05..04: Channel Spacing */
81b09cd163SJoonyoung Shim #define SYSCONFIG2_VOLUME	0x000f	/* bits 03..00: Volume */
82b09cd163SJoonyoung Shim 
83b09cd163SJoonyoung Shim #define SYSCONFIG3		6	/* System Configuration 3 */
84b09cd163SJoonyoung Shim #define SYSCONFIG3_SMUTER	0xc000	/* bits 15..14: Softmute Attack/Recover Rate */
85b09cd163SJoonyoung Shim #define SYSCONFIG3_SMUTEA	0x3000	/* bits 13..12: Softmute Attenuation */
86b09cd163SJoonyoung Shim #define SYSCONFIG3_SKSNR	0x00f0	/* bits 07..04: Seek SNR Threshold */
87b09cd163SJoonyoung Shim #define SYSCONFIG3_SKCNT	0x000f	/* bits 03..00: Seek FM Impulse Detection Threshold */
88b09cd163SJoonyoung Shim 
89b09cd163SJoonyoung Shim #define TEST1			7	/* Test 1 */
90b09cd163SJoonyoung Shim #define TEST1_AHIZEN		0x4000	/* bits 14..14: Audio High-Z Enable */
91b09cd163SJoonyoung Shim 
92b09cd163SJoonyoung Shim #define TEST2			8	/* Test 2 */
93b09cd163SJoonyoung Shim /* TEST2 only contains reserved bits */
94b09cd163SJoonyoung Shim 
95b09cd163SJoonyoung Shim #define BOOTCONFIG		9	/* Boot Configuration */
96b09cd163SJoonyoung Shim /* BOOTCONFIG only contains reserved bits */
97b09cd163SJoonyoung Shim 
98b09cd163SJoonyoung Shim #define STATUSRSSI		10	/* Status RSSI */
99b09cd163SJoonyoung Shim #define STATUSRSSI_RDSR		0x8000	/* bits 15..15: RDS Ready (Si4701 only) */
100b09cd163SJoonyoung Shim #define STATUSRSSI_STC		0x4000	/* bits 14..14: Seek/Tune Complete */
101b09cd163SJoonyoung Shim #define STATUSRSSI_SF		0x2000	/* bits 13..13: Seek Fail/Band Limit */
102b09cd163SJoonyoung Shim #define STATUSRSSI_AFCRL	0x1000	/* bits 12..12: AFC Rail */
103b09cd163SJoonyoung Shim #define STATUSRSSI_RDSS		0x0800	/* bits 11..11: RDS Synchronized (Si4701 only) */
104b09cd163SJoonyoung Shim #define STATUSRSSI_BLERA	0x0600	/* bits 10..09: RDS Block A Errors (Si4701 only) */
105b09cd163SJoonyoung Shim #define STATUSRSSI_ST		0x0100	/* bits 08..08: Stereo Indicator */
106b09cd163SJoonyoung Shim #define STATUSRSSI_RSSI		0x00ff	/* bits 07..00: RSSI (Received Signal Strength Indicator) */
107b09cd163SJoonyoung Shim 
108b09cd163SJoonyoung Shim #define READCHAN		11	/* Read Channel */
109b09cd163SJoonyoung Shim #define READCHAN_BLERB		0xc000	/* bits 15..14: RDS Block D Errors (Si4701 only) */
110b09cd163SJoonyoung Shim #define READCHAN_BLERC		0x3000	/* bits 13..12: RDS Block C Errors (Si4701 only) */
111b09cd163SJoonyoung Shim #define READCHAN_BLERD		0x0c00	/* bits 11..10: RDS Block B Errors (Si4701 only) */
112b09cd163SJoonyoung Shim #define READCHAN_READCHAN	0x03ff	/* bits 09..00: Read Channel */
113b09cd163SJoonyoung Shim 
114b09cd163SJoonyoung Shim #define RDSA			12	/* RDSA */
115b09cd163SJoonyoung Shim #define RDSA_RDSA		0xffff	/* bits 15..00: RDS Block A Data (Si4701 only) */
116b09cd163SJoonyoung Shim 
117b09cd163SJoonyoung Shim #define RDSB			13	/* RDSB */
118b09cd163SJoonyoung Shim #define RDSB_RDSB		0xffff	/* bits 15..00: RDS Block B Data (Si4701 only) */
119b09cd163SJoonyoung Shim 
120b09cd163SJoonyoung Shim #define RDSC			14	/* RDSC */
121b09cd163SJoonyoung Shim #define RDSC_RDSC		0xffff	/* bits 15..00: RDS Block C Data (Si4701 only) */
122b09cd163SJoonyoung Shim 
123b09cd163SJoonyoung Shim #define RDSD			15	/* RDSD */
124b09cd163SJoonyoung Shim #define RDSD_RDSD		0xffff	/* bits 15..00: RDS Block D Data (Si4701 only) */
125b09cd163SJoonyoung Shim 
126b09cd163SJoonyoung Shim 
1279dcb79c2STobias Lorenz 
128b09cd163SJoonyoung Shim /**************************************************************************
129b09cd163SJoonyoung Shim  * General Driver Definitions
130b09cd163SJoonyoung Shim  **************************************************************************/
131b09cd163SJoonyoung Shim 
132b09cd163SJoonyoung Shim /*
133b09cd163SJoonyoung Shim  * si470x_device - private data
134b09cd163SJoonyoung Shim  */
135b09cd163SJoonyoung Shim struct si470x_device {
1364967d53dSHans Verkuil 	struct v4l2_device v4l2_dev;
1374967d53dSHans Verkuil 	struct video_device videodev;
1384967d53dSHans Verkuil 	struct v4l2_ctrl_handler hdl;
139f140612dSHans de Goede 	int band;
1409dcb79c2STobias Lorenz 
1419dcb79c2STobias Lorenz 	/* Silabs internal registers (0..15) */
1429dcb79c2STobias Lorenz 	unsigned short registers[RADIO_REGISTER_NUM];
1439dcb79c2STobias Lorenz 
1449dcb79c2STobias Lorenz 	/* RDS receive buffer */
1459dcb79c2STobias Lorenz 	wait_queue_head_t read_queue;
1469dcb79c2STobias Lorenz 	struct mutex lock;		/* buffer locking */
1479dcb79c2STobias Lorenz 	unsigned char *buffer;		/* size is always multiple of three */
1489dcb79c2STobias Lorenz 	unsigned int buf_size;
1499dcb79c2STobias Lorenz 	unsigned int rd_index;
1509dcb79c2STobias Lorenz 	unsigned int wr_index;
151cc35bbddSJoonyoung Shim 
1520830be3fSJoonyoung Shim 	struct completion completion;
15386ef3f78SHans de Goede 	bool status_rssi_auto_update;	/* Does RSSI get updated automatic? */
1540830be3fSJoonyoung Shim 
15558757984SMauro Carvalho Chehab 	/* si470x ops */
15658757984SMauro Carvalho Chehab 
15758757984SMauro Carvalho Chehab 	int (*get_register)(struct si470x_device *radio, int regnr);
15858757984SMauro Carvalho Chehab 	int (*set_register)(struct si470x_device *radio, int regnr);
15958757984SMauro Carvalho Chehab 	int (*fops_open)(struct file *file);
16058757984SMauro Carvalho Chehab 	int (*fops_release)(struct file *file);
16158757984SMauro Carvalho Chehab 	int (*vidioc_querycap)(struct file *file, void *priv,
16258757984SMauro Carvalho Chehab 			       struct v4l2_capability *capability);
16358757984SMauro Carvalho Chehab 
164480d3ca1SPeter Senna Tschudin #if IS_ENABLED(CONFIG_USB_SI470X)
165b09cd163SJoonyoung Shim 	/* reference to USB and video device */
166b09cd163SJoonyoung Shim 	struct usb_device *usbdev;
167b09cd163SJoonyoung Shim 	struct usb_interface *intf;
168567e2e96SHans Verkuil 	char *usb_buf;
169b09cd163SJoonyoung Shim 
170b09cd163SJoonyoung Shim 	/* Interrupt endpoint handling */
171b09cd163SJoonyoung Shim 	char *int_in_buffer;
172b09cd163SJoonyoung Shim 	struct usb_endpoint_descriptor *int_in_endpoint;
173b09cd163SJoonyoung Shim 	struct urb *int_in_urb;
174b09cd163SJoonyoung Shim 	int int_in_running;
175b09cd163SJoonyoung Shim 
176b09cd163SJoonyoung Shim 	/* scratch page */
177b09cd163SJoonyoung Shim 	unsigned char software_version;
178b09cd163SJoonyoung Shim 	unsigned char hardware_version;
179cc35bbddSJoonyoung Shim #endif
180b09cd163SJoonyoung Shim 
181480d3ca1SPeter Senna Tschudin #if IS_ENABLED(CONFIG_I2C_SI470X)
1829dcb79c2STobias Lorenz 	struct i2c_client *client;
1831c64222bSPawe? Chmiel 	struct gpio_desc *gpio_reset;
1849dcb79c2STobias Lorenz #endif
185b09cd163SJoonyoung Shim };
186b09cd163SJoonyoung Shim 
187b09cd163SJoonyoung Shim 
1889dcb79c2STobias Lorenz 
1899dcb79c2STobias Lorenz /**************************************************************************
1909dcb79c2STobias Lorenz  * Firmware Versions
1919dcb79c2STobias Lorenz  **************************************************************************/
1929dcb79c2STobias Lorenz 
193db3912c0SHans de Goede #define RADIO_FW_VERSION	12
1949dcb79c2STobias Lorenz 
1959dcb79c2STobias Lorenz 
1969dcb79c2STobias Lorenz 
1979dcb79c2STobias Lorenz /**************************************************************************
1989dcb79c2STobias Lorenz  * Frequency Multiplicator
1999dcb79c2STobias Lorenz  **************************************************************************/
2009dcb79c2STobias Lorenz 
201b09cd163SJoonyoung Shim /*
202b09cd163SJoonyoung Shim  * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
203b09cd163SJoonyoung Shim  * 62.5 kHz otherwise.
204b09cd163SJoonyoung Shim  * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
205b09cd163SJoonyoung Shim  * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
206b09cd163SJoonyoung Shim  * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
207b09cd163SJoonyoung Shim  */
208b09cd163SJoonyoung Shim #define FREQ_MUL (1000000 / 62.5)
209b09cd163SJoonyoung Shim 
210b09cd163SJoonyoung Shim 
211b09cd163SJoonyoung Shim 
212b09cd163SJoonyoung Shim /**************************************************************************
213b09cd163SJoonyoung Shim  * Common Functions
214b09cd163SJoonyoung Shim  **************************************************************************/
21582fad476SBhumika Goyal extern const struct video_device si470x_viddev_template;
2164967d53dSHans Verkuil extern const struct v4l2_ctrl_ops si470x_ctrl_ops;
2173c7cc8d5SJoonyoung Shim int si470x_disconnect_check(struct si470x_device *radio);
218b09cd163SJoonyoung Shim int si470x_set_freq(struct si470x_device *radio, unsigned int freq);
219b09cd163SJoonyoung Shim int si470x_start(struct si470x_device *radio);
220b09cd163SJoonyoung Shim int si470x_stop(struct si470x_device *radio);
221