18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  drivers/media/radio/si470x/radio-si470x.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Driver for radios with Silicon Labs Si470x FM Radio Receivers
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/* driver definitions */
128c2ecf20Sopenharmony_ci#define DRIVER_NAME "radio-si470x"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* kernel includes */
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/sched.h>
208c2ecf20Sopenharmony_ci#include <linux/slab.h>
218c2ecf20Sopenharmony_ci#include <linux/input.h>
228c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
238c2ecf20Sopenharmony_ci#include <linux/mutex.h>
248c2ecf20Sopenharmony_ci#include <media/v4l2-common.h>
258c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h>
268c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
278c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
288c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
298c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**************************************************************************
348c2ecf20Sopenharmony_ci * Register Definitions
358c2ecf20Sopenharmony_ci **************************************************************************/
368c2ecf20Sopenharmony_ci#define RADIO_REGISTER_SIZE	2	/* 16 register bit width */
378c2ecf20Sopenharmony_ci#define RADIO_REGISTER_NUM	16	/* DEVICEID   ... RDSD */
388c2ecf20Sopenharmony_ci#define RDS_REGISTER_NUM	6	/* STATUSRSSI ... RDSD */
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define DEVICEID		0	/* Device ID */
418c2ecf20Sopenharmony_ci#define DEVICEID_PN		0xf000	/* bits 15..12: Part Number */
428c2ecf20Sopenharmony_ci#define DEVICEID_MFGID		0x0fff	/* bits 11..00: Manufacturer ID */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define SI_CHIPID		1	/* Chip ID */
458c2ecf20Sopenharmony_ci#define SI_CHIPID_REV		0xfc00	/* bits 15..10: Chip Version */
468c2ecf20Sopenharmony_ci#define SI_CHIPID_DEV		0x0200	/* bits 09..09: Device */
478c2ecf20Sopenharmony_ci#define SI_CHIPID_FIRMWARE	0x01ff	/* bits 08..00: Firmware Version */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define POWERCFG		2	/* Power Configuration */
508c2ecf20Sopenharmony_ci#define POWERCFG_DSMUTE		0x8000	/* bits 15..15: Softmute Disable */
518c2ecf20Sopenharmony_ci#define POWERCFG_DMUTE		0x4000	/* bits 14..14: Mute Disable */
528c2ecf20Sopenharmony_ci#define POWERCFG_MONO		0x2000	/* bits 13..13: Mono Select */
538c2ecf20Sopenharmony_ci#define POWERCFG_RDSM		0x0800	/* bits 11..11: RDS Mode (Si4701 only) */
548c2ecf20Sopenharmony_ci#define POWERCFG_SKMODE		0x0400	/* bits 10..10: Seek Mode */
558c2ecf20Sopenharmony_ci#define POWERCFG_SEEKUP		0x0200	/* bits 09..09: Seek Direction */
568c2ecf20Sopenharmony_ci#define POWERCFG_SEEK		0x0100	/* bits 08..08: Seek */
578c2ecf20Sopenharmony_ci#define POWERCFG_DISABLE	0x0040	/* bits 06..06: Powerup Disable */
588c2ecf20Sopenharmony_ci#define POWERCFG_ENABLE		0x0001	/* bits 00..00: Powerup Enable */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define CHANNEL			3	/* Channel */
618c2ecf20Sopenharmony_ci#define CHANNEL_TUNE		0x8000	/* bits 15..15: Tune */
628c2ecf20Sopenharmony_ci#define CHANNEL_CHAN		0x03ff	/* bits 09..00: Channel Select */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define SYSCONFIG1		4	/* System Configuration 1 */
658c2ecf20Sopenharmony_ci#define SYSCONFIG1_RDSIEN	0x8000	/* bits 15..15: RDS Interrupt Enable (Si4701 only) */
668c2ecf20Sopenharmony_ci#define SYSCONFIG1_STCIEN	0x4000	/* bits 14..14: Seek/Tune Complete Interrupt Enable */
678c2ecf20Sopenharmony_ci#define SYSCONFIG1_RDS		0x1000	/* bits 12..12: RDS Enable (Si4701 only) */
688c2ecf20Sopenharmony_ci#define SYSCONFIG1_DE		0x0800	/* bits 11..11: De-emphasis (0=75us 1=50us) */
698c2ecf20Sopenharmony_ci#define SYSCONFIG1_AGCD		0x0400	/* bits 10..10: AGC Disable */
708c2ecf20Sopenharmony_ci#define SYSCONFIG1_BLNDADJ	0x00c0	/* bits 07..06: Stereo/Mono Blend Level Adjustment */
718c2ecf20Sopenharmony_ci#define SYSCONFIG1_GPIO3	0x0030	/* bits 05..04: General Purpose I/O 3 */
728c2ecf20Sopenharmony_ci#define SYSCONFIG1_GPIO2	0x000c	/* bits 03..02: General Purpose I/O 2 */
738c2ecf20Sopenharmony_ci#define SYSCONFIG1_GPIO2_DIS	0x0000	/* Disable GPIO 2 interrupt */
748c2ecf20Sopenharmony_ci#define SYSCONFIG1_GPIO2_INT	0x0004	/* Enable STC/RDS interrupt */
758c2ecf20Sopenharmony_ci#define SYSCONFIG1_GPIO1	0x0003	/* bits 01..00: General Purpose I/O 1 */
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define SYSCONFIG2		5	/* System Configuration 2 */
788c2ecf20Sopenharmony_ci#define SYSCONFIG2_SEEKTH	0xff00	/* bits 15..08: RSSI Seek Threshold */
798c2ecf20Sopenharmony_ci#define SYSCONFIG2_BAND		0x00c0	/* bits 07..06: Band Select */
808c2ecf20Sopenharmony_ci#define SYSCONFIG2_SPACE	0x0030	/* bits 05..04: Channel Spacing */
818c2ecf20Sopenharmony_ci#define SYSCONFIG2_VOLUME	0x000f	/* bits 03..00: Volume */
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define SYSCONFIG3		6	/* System Configuration 3 */
848c2ecf20Sopenharmony_ci#define SYSCONFIG3_SMUTER	0xc000	/* bits 15..14: Softmute Attack/Recover Rate */
858c2ecf20Sopenharmony_ci#define SYSCONFIG3_SMUTEA	0x3000	/* bits 13..12: Softmute Attenuation */
868c2ecf20Sopenharmony_ci#define SYSCONFIG3_SKSNR	0x00f0	/* bits 07..04: Seek SNR Threshold */
878c2ecf20Sopenharmony_ci#define SYSCONFIG3_SKCNT	0x000f	/* bits 03..00: Seek FM Impulse Detection Threshold */
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define TEST1			7	/* Test 1 */
908c2ecf20Sopenharmony_ci#define TEST1_AHIZEN		0x4000	/* bits 14..14: Audio High-Z Enable */
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define TEST2			8	/* Test 2 */
938c2ecf20Sopenharmony_ci/* TEST2 only contains reserved bits */
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define BOOTCONFIG		9	/* Boot Configuration */
968c2ecf20Sopenharmony_ci/* BOOTCONFIG only contains reserved bits */
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define STATUSRSSI		10	/* Status RSSI */
998c2ecf20Sopenharmony_ci#define STATUSRSSI_RDSR		0x8000	/* bits 15..15: RDS Ready (Si4701 only) */
1008c2ecf20Sopenharmony_ci#define STATUSRSSI_STC		0x4000	/* bits 14..14: Seek/Tune Complete */
1018c2ecf20Sopenharmony_ci#define STATUSRSSI_SF		0x2000	/* bits 13..13: Seek Fail/Band Limit */
1028c2ecf20Sopenharmony_ci#define STATUSRSSI_AFCRL	0x1000	/* bits 12..12: AFC Rail */
1038c2ecf20Sopenharmony_ci#define STATUSRSSI_RDSS		0x0800	/* bits 11..11: RDS Synchronized (Si4701 only) */
1048c2ecf20Sopenharmony_ci#define STATUSRSSI_BLERA	0x0600	/* bits 10..09: RDS Block A Errors (Si4701 only) */
1058c2ecf20Sopenharmony_ci#define STATUSRSSI_ST		0x0100	/* bits 08..08: Stereo Indicator */
1068c2ecf20Sopenharmony_ci#define STATUSRSSI_RSSI		0x00ff	/* bits 07..00: RSSI (Received Signal Strength Indicator) */
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define READCHAN		11	/* Read Channel */
1098c2ecf20Sopenharmony_ci#define READCHAN_BLERB		0xc000	/* bits 15..14: RDS Block D Errors (Si4701 only) */
1108c2ecf20Sopenharmony_ci#define READCHAN_BLERC		0x3000	/* bits 13..12: RDS Block C Errors (Si4701 only) */
1118c2ecf20Sopenharmony_ci#define READCHAN_BLERD		0x0c00	/* bits 11..10: RDS Block B Errors (Si4701 only) */
1128c2ecf20Sopenharmony_ci#define READCHAN_READCHAN	0x03ff	/* bits 09..00: Read Channel */
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define RDSA			12	/* RDSA */
1158c2ecf20Sopenharmony_ci#define RDSA_RDSA		0xffff	/* bits 15..00: RDS Block A Data (Si4701 only) */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define RDSB			13	/* RDSB */
1188c2ecf20Sopenharmony_ci#define RDSB_RDSB		0xffff	/* bits 15..00: RDS Block B Data (Si4701 only) */
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define RDSC			14	/* RDSC */
1218c2ecf20Sopenharmony_ci#define RDSC_RDSC		0xffff	/* bits 15..00: RDS Block C Data (Si4701 only) */
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define RDSD			15	/* RDSD */
1248c2ecf20Sopenharmony_ci#define RDSD_RDSD		0xffff	/* bits 15..00: RDS Block D Data (Si4701 only) */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/**************************************************************************
1298c2ecf20Sopenharmony_ci * General Driver Definitions
1308c2ecf20Sopenharmony_ci **************************************************************************/
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/*
1338c2ecf20Sopenharmony_ci * si470x_device - private data
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_cistruct si470x_device {
1368c2ecf20Sopenharmony_ci	struct v4l2_device v4l2_dev;
1378c2ecf20Sopenharmony_ci	struct video_device videodev;
1388c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
1398c2ecf20Sopenharmony_ci	int band;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/* Silabs internal registers (0..15) */
1428c2ecf20Sopenharmony_ci	unsigned short registers[RADIO_REGISTER_NUM];
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* RDS receive buffer */
1458c2ecf20Sopenharmony_ci	wait_queue_head_t read_queue;
1468c2ecf20Sopenharmony_ci	struct mutex lock;		/* buffer locking */
1478c2ecf20Sopenharmony_ci	unsigned char *buffer;		/* size is always multiple of three */
1488c2ecf20Sopenharmony_ci	unsigned int buf_size;
1498c2ecf20Sopenharmony_ci	unsigned int rd_index;
1508c2ecf20Sopenharmony_ci	unsigned int wr_index;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	struct completion completion;
1538c2ecf20Sopenharmony_ci	bool status_rssi_auto_update;	/* Does RSSI get updated automatic? */
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* si470x ops */
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	int (*get_register)(struct si470x_device *radio, int regnr);
1588c2ecf20Sopenharmony_ci	int (*set_register)(struct si470x_device *radio, int regnr);
1598c2ecf20Sopenharmony_ci	int (*fops_open)(struct file *file);
1608c2ecf20Sopenharmony_ci	int (*fops_release)(struct file *file);
1618c2ecf20Sopenharmony_ci	int (*vidioc_querycap)(struct file *file, void *priv,
1628c2ecf20Sopenharmony_ci			       struct v4l2_capability *capability);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_SI470X)
1658c2ecf20Sopenharmony_ci	/* reference to USB and video device */
1668c2ecf20Sopenharmony_ci	struct usb_device *usbdev;
1678c2ecf20Sopenharmony_ci	struct usb_interface *intf;
1688c2ecf20Sopenharmony_ci	char *usb_buf;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	/* Interrupt endpoint handling */
1718c2ecf20Sopenharmony_ci	char *int_in_buffer;
1728c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *int_in_endpoint;
1738c2ecf20Sopenharmony_ci	struct urb *int_in_urb;
1748c2ecf20Sopenharmony_ci	int int_in_running;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	/* scratch page */
1778c2ecf20Sopenharmony_ci	unsigned char software_version;
1788c2ecf20Sopenharmony_ci	unsigned char hardware_version;
1798c2ecf20Sopenharmony_ci#endif
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SI470X)
1828c2ecf20Sopenharmony_ci	struct i2c_client *client;
1838c2ecf20Sopenharmony_ci	struct gpio_desc *gpio_reset;
1848c2ecf20Sopenharmony_ci#endif
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/**************************************************************************
1908c2ecf20Sopenharmony_ci * Firmware Versions
1918c2ecf20Sopenharmony_ci **************************************************************************/
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#define RADIO_FW_VERSION	12
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/**************************************************************************
1988c2ecf20Sopenharmony_ci * Frequency Multiplicator
1998c2ecf20Sopenharmony_ci **************************************************************************/
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/*
2028c2ecf20Sopenharmony_ci * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
2038c2ecf20Sopenharmony_ci * 62.5 kHz otherwise.
2048c2ecf20Sopenharmony_ci * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
2058c2ecf20Sopenharmony_ci * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
2068c2ecf20Sopenharmony_ci * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
2078c2ecf20Sopenharmony_ci */
2088c2ecf20Sopenharmony_ci#define FREQ_MUL (1000000 / 62.5)
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/**************************************************************************
2138c2ecf20Sopenharmony_ci * Common Functions
2148c2ecf20Sopenharmony_ci **************************************************************************/
2158c2ecf20Sopenharmony_ciextern const struct video_device si470x_viddev_template;
2168c2ecf20Sopenharmony_ciextern const struct v4l2_ctrl_ops si470x_ctrl_ops;
2178c2ecf20Sopenharmony_ciint si470x_disconnect_check(struct si470x_device *radio);
2188c2ecf20Sopenharmony_ciint si470x_set_freq(struct si470x_device *radio, unsigned int freq);
2198c2ecf20Sopenharmony_ciint si470x_start(struct si470x_device *radio);
2208c2ecf20Sopenharmony_ciint si470x_stop(struct si470x_device *radio);
221