blob: 4b7660470e2f8a6cfbd7f868eaf1312029e08b4c [file] [log] [blame]
Joonyoung Shimb09cd162009-08-09 14:22:20 -03001/*
2 * drivers/media/radio/si470x/radio-si470x.h
3 *
4 * Driver for radios with Silicon Labs Si470x FM Radio Receivers
5 *
6 * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23
24/* driver definitions */
25#define DRIVER_NAME "radio-si470x"
26
27
28/* kernel includes */
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
Randy Dunlap788f6fe2009-12-14 14:08:13 -030032#include <linux/sched.h>
Joonyoung Shimb09cd162009-08-09 14:22:20 -030033#include <linux/slab.h>
Joonyoung Shimb09cd162009-08-09 14:22:20 -030034#include <linux/input.h>
Joonyoung Shimb09cd162009-08-09 14:22:20 -030035#include <linux/videodev2.h>
36#include <linux/mutex.h>
37#include <media/v4l2-common.h>
38#include <media/v4l2-ioctl.h>
Hans Verkuil4967d532012-05-04 09:16:57 -030039#include <media/v4l2-ctrls.h>
40#include <media/v4l2-event.h>
41#include <media/v4l2-device.h>
Joonyoung Shimb09cd162009-08-09 14:22:20 -030042#include <asm/unaligned.h>
43
44
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -030045
Joonyoung Shimb09cd162009-08-09 14:22:20 -030046/**************************************************************************
47 * Register Definitions
48 **************************************************************************/
49#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
50#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
51#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
52
53#define DEVICEID 0 /* Device ID */
54#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
55#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
56
57#define CHIPID 1 /* Chip ID */
58#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
59#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
60#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
61
62#define POWERCFG 2 /* Power Configuration */
63#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
64#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
65#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
66#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
67#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
68#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
69#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
70#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
71#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
72
73#define CHANNEL 3 /* Channel */
74#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
75#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
76
77#define SYSCONFIG1 4 /* System Configuration 1 */
78#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
79#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
80#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
81#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
82#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
83#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
84#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
85#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
86#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
87
88#define SYSCONFIG2 5 /* System Configuration 2 */
89#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
Hans de Goede8d8c1b32012-07-12 16:55:47 -030090#define SYSCONFIG2_BAND 0x00c0 /* bits 07..06: Band Select */
Joonyoung Shimb09cd162009-08-09 14:22:20 -030091#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
92#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
93
94#define SYSCONFIG3 6 /* System Configuration 3 */
95#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
96#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
97#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
98#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
99
100#define TEST1 7 /* Test 1 */
101#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
102
103#define TEST2 8 /* Test 2 */
104/* TEST2 only contains reserved bits */
105
106#define BOOTCONFIG 9 /* Boot Configuration */
107/* BOOTCONFIG only contains reserved bits */
108
109#define STATUSRSSI 10 /* Status RSSI */
110#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
111#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
112#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
113#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
114#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
115#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
116#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
117#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
118
119#define READCHAN 11 /* Read Channel */
120#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
121#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
122#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
123#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
124
125#define RDSA 12 /* RDSA */
126#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
127
128#define RDSB 13 /* RDSB */
129#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
130
131#define RDSC 14 /* RDSC */
132#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
133
134#define RDSD 15 /* RDSD */
135#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
136
137
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300138
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300139/**************************************************************************
140 * General Driver Definitions
141 **************************************************************************/
142
143/*
144 * si470x_device - private data
145 */
146struct si470x_device {
Hans Verkuil4967d532012-05-04 09:16:57 -0300147 struct v4l2_device v4l2_dev;
148 struct video_device videodev;
149 struct v4l2_ctrl_handler hdl;
Hans de Goedef1406122012-07-12 16:55:48 -0300150 int band;
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300151
152 /* Silabs internal registers (0..15) */
153 unsigned short registers[RADIO_REGISTER_NUM];
154
155 /* RDS receive buffer */
156 wait_queue_head_t read_queue;
157 struct mutex lock; /* buffer locking */
158 unsigned char *buffer; /* size is always multiple of three */
159 unsigned int buf_size;
160 unsigned int rd_index;
161 unsigned int wr_index;
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300162
Joonyoung Shim0830be32011-03-11 03:54:46 -0300163 struct completion completion;
Hans de Goede86ef3f72012-06-14 09:43:11 -0300164 bool status_rssi_auto_update; /* Does RSSI get updated automatic? */
Joonyoung Shim0830be32011-03-11 03:54:46 -0300165
Peter Senna Tschudin480d3ca12013-01-24 19:28:57 -0300166#if IS_ENABLED(CONFIG_USB_SI470X)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300167 /* reference to USB and video device */
168 struct usb_device *usbdev;
169 struct usb_interface *intf;
Hans Verkuil567e2e92013-12-13 08:06:07 -0300170 char *usb_buf;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300171
172 /* Interrupt endpoint handling */
173 char *int_in_buffer;
174 struct usb_endpoint_descriptor *int_in_endpoint;
175 struct urb *int_in_urb;
176 int int_in_running;
177
178 /* scratch page */
179 unsigned char software_version;
180 unsigned char hardware_version;
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300181#endif
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300182
Peter Senna Tschudin480d3ca12013-01-24 19:28:57 -0300183#if IS_ENABLED(CONFIG_I2C_SI470X)
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300184 struct i2c_client *client;
185#endif
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300186};
187
188
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300189
190/**************************************************************************
191 * Firmware Versions
192 **************************************************************************/
193
Hans de Goededb3912c2012-07-11 11:47:38 -0300194#define RADIO_FW_VERSION 12
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300195
196
197
198/**************************************************************************
199 * Frequency Multiplicator
200 **************************************************************************/
201
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300202/*
203 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
204 * 62.5 kHz otherwise.
205 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
206 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
207 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
208 */
209#define FREQ_MUL (1000000 / 62.5)
210
211
212
213/**************************************************************************
214 * Common Functions
215 **************************************************************************/
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300216extern struct video_device si470x_viddev_template;
Hans Verkuil4967d532012-05-04 09:16:57 -0300217extern const struct v4l2_ctrl_ops si470x_ctrl_ops;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300218int si470x_get_register(struct si470x_device *radio, int regnr);
219int si470x_set_register(struct si470x_device *radio, int regnr);
Joonyoung Shim3c7cc8d2009-08-09 14:23:02 -0300220int si470x_disconnect_check(struct si470x_device *radio);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300221int si470x_set_freq(struct si470x_device *radio, unsigned int freq);
222int si470x_start(struct si470x_device *radio);
223int si470x_stop(struct si470x_device *radio);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300224int si470x_fops_open(struct file *file);
225int si470x_fops_release(struct file *file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300226int si470x_vidioc_querycap(struct file *file, void *priv,
227 struct v4l2_capability *capability);