aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/staging/media/msi3101/Kconfig3
-rw-r--r--drivers/staging/media/msi3101/sdr-msi3101.c483
2 files changed, 136 insertions, 350 deletions
diff --git a/drivers/staging/media/msi3101/Kconfig b/drivers/staging/media/msi3101/Kconfig
index 97d5210d19c1..de0b3bba3873 100644
--- a/drivers/staging/media/msi3101/Kconfig
+++ b/drivers/staging/media/msi3101/Kconfig
@@ -1,8 +1,9 @@
1config USB_MSI3101 1config USB_MSI3101
2 tristate "Mirics MSi3101 SDR Dongle" 2 tristate "Mirics MSi3101 SDR Dongle"
3 depends on USB && VIDEO_DEV && VIDEO_V4L2 3 depends on USB && VIDEO_DEV && VIDEO_V4L2 && SPI
4 select VIDEOBUF2_CORE 4 select VIDEOBUF2_CORE
5 select VIDEOBUF2_VMALLOC 5 select VIDEOBUF2_VMALLOC
6 select MEDIA_TUNER_MSI001
6 7
7config MEDIA_TUNER_MSI001 8config MEDIA_TUNER_MSI001
8 tristate "Mirics MSi001" 9 tristate "Mirics MSi001"
diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c
index cf71e7e6c317..2135940f2d58 100644
--- a/drivers/staging/media/msi3101/sdr-msi3101.c
+++ b/drivers/staging/media/msi3101/sdr-msi3101.c
@@ -25,7 +25,6 @@
25 25
26#include <linux/module.h> 26#include <linux/module.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/gcd.h>
29#include <asm/div64.h> 28#include <asm/div64.h>
30#include <media/v4l2-device.h> 29#include <media/v4l2-device.h>
31#include <media/v4l2-ioctl.h> 30#include <media/v4l2-ioctl.h>
@@ -33,6 +32,7 @@
33#include <media/v4l2-event.h> 32#include <media/v4l2-event.h>
34#include <linux/usb.h> 33#include <linux/usb.h>
35#include <media/videobuf2-vmalloc.h> 34#include <media/videobuf2-vmalloc.h>
35#include <linux/spi/spi.h>
36 36
37/* 37/*
38 * iConfiguration 0 38 * iConfiguration 0
@@ -57,7 +57,7 @@
57#define V4L2_PIX_FMT_SDR_S14 v4l2_fourcc('D', 'S', '1', '4') /* signed 14-bit */ 57#define V4L2_PIX_FMT_SDR_S14 v4l2_fourcc('D', 'S', '1', '4') /* signed 14-bit */
58#define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */ 58#define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */
59 59
60static const struct v4l2_frequency_band bands_adc[] = { 60static const struct v4l2_frequency_band bands[] = {
61 { 61 {
62 .tuner = 0, 62 .tuner = 0,
63 .type = V4L2_TUNER_ADC, 63 .type = V4L2_TUNER_ADC,
@@ -68,24 +68,6 @@ static const struct v4l2_frequency_band bands_adc[] = {
68 }, 68 },
69}; 69};
70 70
71static const struct v4l2_frequency_band bands_rf[] = {
72 {
73 .tuner = 1,
74 .type = V4L2_TUNER_RF,
75 .index = 0,
76 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
77 .rangelow = 49000000,
78 .rangehigh = 263000000,
79 }, {
80 .tuner = 1,
81 .type = V4L2_TUNER_RF,
82 .index = 1,
83 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
84 .rangelow = 390000000,
85 .rangehigh = 960000000,
86 },
87};
88
89/* stream formats */ 71/* stream formats */
90struct msi3101_format { 72struct msi3101_format {
91 char *name; 73 char *name;
@@ -128,6 +110,8 @@ struct msi3101_frame_buf {
128struct msi3101_state { 110struct msi3101_state {
129 struct video_device vdev; 111 struct video_device vdev;
130 struct v4l2_device v4l2_dev; 112 struct v4l2_device v4l2_dev;
113 struct v4l2_subdev *v4l2_subdev;
114 struct spi_master *master;
131 115
132 /* videobuf2 queue and queued buffers list */ 116 /* videobuf2 queue and queued buffers list */
133 struct vb2_queue vb_queue; 117 struct vb2_queue vb_queue;
@@ -141,7 +125,7 @@ struct msi3101_state {
141 /* Pointer to our usb_device, will be NULL after unplug */ 125 /* Pointer to our usb_device, will be NULL after unplug */
142 struct usb_device *udev; /* Both mutexes most be hold when setting! */ 126 struct usb_device *udev; /* Both mutexes most be hold when setting! */
143 127
144 unsigned int f_adc, f_tuner; 128 unsigned int f_adc;
145 u32 pixelformat; 129 u32 pixelformat;
146 130
147 unsigned int isoc_errors; /* number of contiguous ISOC errors */ 131 unsigned int isoc_errors; /* number of contiguous ISOC errors */
@@ -153,14 +137,6 @@ struct msi3101_state {
153 137
154 /* Controls */ 138 /* Controls */
155 struct v4l2_ctrl_handler hdl; 139 struct v4l2_ctrl_handler hdl;
156 struct v4l2_ctrl *bandwidth_auto;
157 struct v4l2_ctrl *bandwidth;
158 struct v4l2_ctrl *lna_gain_auto;
159 struct v4l2_ctrl *lna_gain;
160 struct v4l2_ctrl *mixer_gain_auto;
161 struct v4l2_ctrl *mixer_gain;
162 struct v4l2_ctrl *if_gain_auto;
163 struct v4l2_ctrl *if_gain;
164 140
165 u32 next_sample; /* for track lost packets */ 141 u32 next_sample; /* for track lost packets */
166 u32 sample; /* for sample rate calc */ 142 u32 sample; /* for sample rate calc */
@@ -822,9 +798,9 @@ static void msi3101_disconnect(struct usb_interface *intf)
822 mutex_lock(&s->v4l2_lock); 798 mutex_lock(&s->v4l2_lock);
823 /* No need to keep the urbs around after disconnection */ 799 /* No need to keep the urbs around after disconnection */
824 s->udev = NULL; 800 s->udev = NULL;
825
826 v4l2_device_disconnect(&s->v4l2_dev); 801 v4l2_device_disconnect(&s->v4l2_dev);
827 video_unregister_device(&s->vdev); 802 video_unregister_device(&s->vdev);
803 spi_unregister_master(s->master);
828 mutex_unlock(&s->v4l2_lock); 804 mutex_unlock(&s->v4l2_lock);
829 mutex_unlock(&s->vb_queue_lock); 805 mutex_unlock(&s->vb_queue_lock);
830 806
@@ -924,20 +900,25 @@ static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data)
924 return ret; 900 return ret;
925}; 901};
926 902
927static int msi3101_tuner_write(struct msi3101_state *s, u32 data)
928{
929 return msi3101_ctrl_msg(s, CMD_WREG, data << 8 | 0x09);
930};
931
932#define F_REF 24000000 903#define F_REF 24000000
933#define DIV_R_IN 2 904#define DIV_R_IN 2
934static int msi3101_set_usb_adc(struct msi3101_state *s) 905static int msi3101_set_usb_adc(struct msi3101_state *s)
935{ 906{
936 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract; 907 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
937 u32 reg3, reg4, reg7; 908 u32 reg3, reg4, reg7;
909 struct v4l2_ctrl *bandwidth_auto;
910 struct v4l2_ctrl *bandwidth;
938 911
939 f_sr = s->f_adc; 912 f_sr = s->f_adc;
940 913
914 /* set tuner, subdev, filters according to sampling rate */
915 bandwidth_auto = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
916 bandwidth = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
917 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
918 bandwidth = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
919 v4l2_ctrl_s_ctrl(bandwidth, s->f_adc);
920 }
921
941 /* select stream format */ 922 /* select stream format */
942 switch (s->pixelformat) { 923 switch (s->pixelformat) {
943 case V4L2_SDR_FMT_CU8: 924 case V4L2_SDR_FMT_CU8:
@@ -1066,222 +1047,6 @@ err:
1066 return ret; 1047 return ret;
1067}; 1048};
1068 1049
1069static int msi3101_set_gain(struct msi3101_state *s)
1070{
1071 int ret;
1072 u32 reg;
1073 dev_dbg(&s->udev->dev, "%s: lna=%d mixer=%d if=%d\n", __func__,
1074 s->lna_gain->val, s->mixer_gain->val, s->if_gain->val);
1075
1076 reg = 1 << 0;
1077 reg |= (59 - s->if_gain->val) << 4;
1078 reg |= 0 << 10;
1079 reg |= (1 - s->mixer_gain->val) << 12;
1080 reg |= (1 - s->lna_gain->val) << 13;
1081 reg |= 4 << 14;
1082 reg |= 0 << 17;
1083 ret = msi3101_tuner_write(s, reg);
1084 if (ret)
1085 goto err;
1086
1087 return 0;
1088err:
1089 dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
1090 return ret;
1091};
1092
1093static int msi3101_set_tuner(struct msi3101_state *s)
1094{
1095 int ret, i;
1096 unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;
1097 u32 reg;
1098 u64 f_vco, tmp64;
1099 u8 mode, filter_mode, lo_div;
1100 static const struct {
1101 u32 rf;
1102 u8 mode;
1103 u8 lo_div;
1104 } band_lut[] = {
1105 { 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
1106 {108000000, 0x42, 32}, /* VHF_MODE */
1107 {330000000, 0x44, 16}, /* B3_MODE */
1108 {960000000, 0x48, 4}, /* B45_MODE */
1109 { ~0U, 0x50, 2}, /* BL_MODE */
1110 };
1111 static const struct {
1112 u32 freq;
1113 u8 filter_mode;
1114 } if_freq_lut[] = {
1115 { 0, 0x03}, /* Zero IF */
1116 { 450000, 0x02}, /* 450 kHz IF */
1117 {1620000, 0x01}, /* 1.62 MHz IF */
1118 {2048000, 0x00}, /* 2.048 MHz IF */
1119 };
1120 static const struct {
1121 u32 freq;
1122 u8 val;
1123 } bandwidth_lut[] = {
1124 { 200000, 0x00}, /* 200 kHz */
1125 { 300000, 0x01}, /* 300 kHz */
1126 { 600000, 0x02}, /* 600 kHz */
1127 {1536000, 0x03}, /* 1.536 MHz */
1128 {5000000, 0x04}, /* 5 MHz */
1129 {6000000, 0x05}, /* 6 MHz */
1130 {7000000, 0x06}, /* 7 MHz */
1131 {8000000, 0x07}, /* 8 MHz */
1132 };
1133
1134 unsigned int f_rf = s->f_tuner;
1135
1136 /*
1137 * bandwidth (Hz)
1138 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
1139 */
1140 unsigned int bandwidth;
1141
1142 /*
1143 * intermediate frequency (Hz)
1144 * 0, 450000, 1620000, 2048000
1145 */
1146 unsigned int f_if = 0;
1147
1148 dev_dbg(&s->udev->dev,
1149 "%s: f_rf=%d f_if=%d\n",
1150 __func__, f_rf, f_if);
1151
1152 ret = -EINVAL;
1153
1154 for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
1155 if (f_rf <= band_lut[i].rf) {
1156 mode = band_lut[i].mode;
1157 lo_div = band_lut[i].lo_div;
1158 break;
1159 }
1160 }
1161
1162 if (i == ARRAY_SIZE(band_lut))
1163 goto err;
1164
1165 /* AM_MODE is upconverted */
1166 if ((mode >> 0) & 0x1)
1167 f_if1 = 5 * F_REF;
1168 else
1169 f_if1 = 0;
1170
1171 for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
1172 if (f_if == if_freq_lut[i].freq) {
1173 filter_mode = if_freq_lut[i].filter_mode;
1174 break;
1175 }
1176 }
1177
1178 if (i == ARRAY_SIZE(if_freq_lut))
1179 goto err;
1180
1181 /* filters */
1182 if (s->bandwidth_auto->val)
1183 bandwidth = s->f_adc;
1184 else
1185 bandwidth = s->bandwidth->val;
1186
1187 bandwidth = clamp(bandwidth, 200000U, 8000000U);
1188
1189 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
1190 if (bandwidth <= bandwidth_lut[i].freq) {
1191 bandwidth = bandwidth_lut[i].val;
1192 break;
1193 }
1194 }
1195
1196 if (i == ARRAY_SIZE(bandwidth_lut))
1197 goto err;
1198
1199 s->bandwidth->val = bandwidth_lut[i].freq;
1200
1201 dev_dbg(&s->udev->dev, "%s: bandwidth selected=%d\n",
1202 __func__, bandwidth_lut[i].freq);
1203
1204#define F_OUT_STEP 1
1205#define R_REF 4
1206 f_vco = (f_rf + f_if + f_if1) * lo_div;
1207
1208 tmp64 = f_vco;
1209 m = do_div(tmp64, F_REF * R_REF);
1210 n = (unsigned int) tmp64;
1211
1212 vco_step = F_OUT_STEP * lo_div;
1213 thresh = (F_REF * R_REF) / vco_step;
1214 frac = 1ul * thresh * m / (F_REF * R_REF);
1215
1216 /* Find out greatest common divisor and divide to smaller. */
1217 tmp = gcd(thresh, frac);
1218 thresh /= tmp;
1219 frac /= tmp;
1220
1221 /* Force divide to reg max. Resolution will be reduced. */
1222 tmp = DIV_ROUND_UP(thresh, 4095);
1223 thresh = DIV_ROUND_CLOSEST(thresh, tmp);
1224 frac = DIV_ROUND_CLOSEST(frac, tmp);
1225
1226 /* calc real RF set */
1227 tmp = 1ul * F_REF * R_REF * n;
1228 tmp += 1ul * F_REF * R_REF * frac / thresh;
1229 tmp /= lo_div;
1230
1231 dev_dbg(&s->udev->dev,
1232 "%s: rf=%u:%u n=%d thresh=%d frac=%d\n",
1233 __func__, f_rf, tmp, n, thresh, frac);
1234
1235 ret = msi3101_tuner_write(s, 0x00000e);
1236 if (ret)
1237 goto err;
1238
1239 ret = msi3101_tuner_write(s, 0x000003);
1240 if (ret)
1241 goto err;
1242
1243 reg = 0 << 0;
1244 reg |= mode << 4;
1245 reg |= filter_mode << 12;
1246 reg |= bandwidth << 14;
1247 reg |= 0x02 << 17;
1248 reg |= 0x00 << 20;
1249 ret = msi3101_tuner_write(s, reg);
1250 if (ret)
1251 goto err;
1252
1253 reg = 5 << 0;
1254 reg |= thresh << 4;
1255 reg |= 1 << 19;
1256 reg |= 1 << 21;
1257 ret = msi3101_tuner_write(s, reg);
1258 if (ret)
1259 goto err;
1260
1261 reg = 2 << 0;
1262 reg |= frac << 4;
1263 reg |= n << 16;
1264 ret = msi3101_tuner_write(s, reg);
1265 if (ret)
1266 goto err;
1267
1268 ret = msi3101_set_gain(s);
1269 if (ret)
1270 goto err;
1271
1272 reg = 6 << 0;
1273 reg |= 63 << 4;
1274 reg |= 4095 << 10;
1275 ret = msi3101_tuner_write(s, reg);
1276 if (ret)
1277 goto err;
1278
1279 return 0;
1280err:
1281 dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
1282 return ret;
1283};
1284
1285static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count) 1050static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
1286{ 1051{
1287 struct msi3101_state *s = vb2_get_drv_priv(vq); 1052 struct msi3101_state *s = vb2_get_drv_priv(vq);
@@ -1294,6 +1059,9 @@ static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
1294 if (mutex_lock_interruptible(&s->v4l2_lock)) 1059 if (mutex_lock_interruptible(&s->v4l2_lock))
1295 return -ERESTARTSYS; 1060 return -ERESTARTSYS;
1296 1061
1062 /* wake-up tuner */
1063 v4l2_subdev_call(s->v4l2_subdev, core, s_power, 1);
1064
1297 ret = msi3101_set_usb_adc(s); 1065 ret = msi3101_set_usb_adc(s);
1298 1066
1299 ret = msi3101_isoc_init(s); 1067 ret = msi3101_isoc_init(s);
@@ -1328,7 +1096,7 @@ static int msi3101_stop_streaming(struct vb2_queue *vq)
1328 msi3101_ctrl_msg(s, CMD_WREG, 0x01000003); 1096 msi3101_ctrl_msg(s, CMD_WREG, 0x01000003);
1329 1097
1330 /* sleep tuner */ 1098 /* sleep tuner */
1331 msi3101_tuner_write(s, 0x000000); 1099 v4l2_subdev_call(s->v4l2_subdev, core, s_power, 0);
1332 1100
1333 mutex_unlock(&s->v4l2_lock); 1101 mutex_unlock(&s->v4l2_lock);
1334 1102
@@ -1418,33 +1186,39 @@ static int msi3101_s_tuner(struct file *file, void *priv,
1418 const struct v4l2_tuner *v) 1186 const struct v4l2_tuner *v)
1419{ 1187{
1420 struct msi3101_state *s = video_drvdata(file); 1188 struct msi3101_state *s = video_drvdata(file);
1421 dev_dbg(&s->udev->dev, "%s:\n", __func__); 1189 int ret;
1190 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
1422 1191
1423 return 0; 1192 if (v->index == 0)
1193 ret = 0;
1194 else if (v->index == 1)
1195 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_tuner, v);
1196 else
1197 ret = -EINVAL;
1198
1199 return ret;
1424} 1200}
1425 1201
1426static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v) 1202static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
1427{ 1203{
1428 struct msi3101_state *s = video_drvdata(file); 1204 struct msi3101_state *s = video_drvdata(file);
1429 dev_dbg(&s->udev->dev, "%s:\n", __func__); 1205 int ret;
1206 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
1430 1207
1431 if (v->index == 0) { 1208 if (v->index == 0) {
1432 strlcpy(v->name, "ADC: Mirics MSi2500", sizeof(v->name)); 1209 strlcpy(v->name, "Mirics MSi2500", sizeof(v->name));
1433 v->type = V4L2_TUNER_ADC; 1210 v->type = V4L2_TUNER_ADC;
1434 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 1211 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1435 v->rangelow = 1200000; 1212 v->rangelow = 1200000;
1436 v->rangehigh = 15000000; 1213 v->rangehigh = 15000000;
1214 ret = 0;
1437 } else if (v->index == 1) { 1215 } else if (v->index == 1) {
1438 strlcpy(v->name, "RF: Mirics MSi001", sizeof(v->name)); 1216 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_tuner, v);
1439 v->type = V4L2_TUNER_RF;
1440 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1441 v->rangelow = 49000000;
1442 v->rangehigh = 960000000;
1443 } else { 1217 } else {
1444 return -EINVAL; 1218 ret = -EINVAL;
1445 } 1219 }
1446 1220
1447 return 0; 1221 return ret;
1448} 1222}
1449 1223
1450static int msi3101_g_frequency(struct file *file, void *priv, 1224static int msi3101_g_frequency(struct file *file, void *priv,
@@ -1455,12 +1229,14 @@ static int msi3101_g_frequency(struct file *file, void *priv,
1455 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n", 1229 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n",
1456 __func__, f->tuner, f->type); 1230 __func__, f->tuner, f->type);
1457 1231
1458 if (f->tuner == 0) 1232 if (f->tuner == 0) {
1459 f->frequency = s->f_adc; 1233 f->frequency = s->f_adc;
1460 else if (f->tuner == 1) 1234 ret = 0;
1461 f->frequency = s->f_tuner; 1235 } else if (f->tuner == 1) {
1462 else 1236 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_frequency, f);
1463 return -EINVAL; 1237 } else {
1238 ret = -EINVAL;
1239 }
1464 1240
1465 return ret; 1241 return ret;
1466} 1242}
@@ -1469,31 +1245,21 @@ static int msi3101_s_frequency(struct file *file, void *priv,
1469 const struct v4l2_frequency *f) 1245 const struct v4l2_frequency *f)
1470{ 1246{
1471 struct msi3101_state *s = video_drvdata(file); 1247 struct msi3101_state *s = video_drvdata(file);
1472 int ret, band; 1248 int ret;
1473 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n", 1249 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n",
1474 __func__, f->tuner, f->type, f->frequency); 1250 __func__, f->tuner, f->type, f->frequency);
1475 1251
1476 if (f->tuner == 0) { 1252 if (f->tuner == 0) {
1477 s->f_adc = clamp_t(unsigned int, f->frequency, 1253 s->f_adc = clamp_t(unsigned int, f->frequency,
1478 bands_adc[0].rangelow, 1254 bands[0].rangelow,
1479 bands_adc[0].rangehigh); 1255 bands[0].rangehigh);
1480 dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n", 1256 dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n",
1481 __func__, s->f_adc); 1257 __func__, s->f_adc);
1482 ret = msi3101_set_usb_adc(s); 1258 ret = msi3101_set_usb_adc(s);
1483 } else if (f->tuner == 1) { 1259 } else if (f->tuner == 1) {
1484 #define BAND_RF_0 ((bands_rf[0].rangehigh + bands_rf[1].rangelow) / 2) 1260 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_frequency, f);
1485 if (f->frequency < BAND_RF_0)
1486 band = 0;
1487 else
1488 band = 1;
1489 s->f_tuner = clamp_t(unsigned int, f->frequency,
1490 bands_rf[band].rangelow,
1491 bands_rf[band].rangehigh);
1492 dev_dbg(&s->udev->dev, "%s: RF frequency=%u Hz\n",
1493 __func__, f->frequency);
1494 ret = msi3101_set_tuner(s);
1495 } else { 1261 } else {
1496 return -EINVAL; 1262 ret = -EINVAL;
1497 } 1263 }
1498 1264
1499 return ret; 1265 return ret;
@@ -1503,24 +1269,25 @@ static int msi3101_enum_freq_bands(struct file *file, void *priv,
1503 struct v4l2_frequency_band *band) 1269 struct v4l2_frequency_band *band)
1504{ 1270{
1505 struct msi3101_state *s = video_drvdata(file); 1271 struct msi3101_state *s = video_drvdata(file);
1272 int ret;
1506 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n", 1273 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n",
1507 __func__, band->tuner, band->type, band->index); 1274 __func__, band->tuner, band->type, band->index);
1508 1275
1509 if (band->tuner == 0) { 1276 if (band->tuner == 0) {
1510 if (band->index >= ARRAY_SIZE(bands_adc)) 1277 if (band->index >= ARRAY_SIZE(bands)) {
1511 return -EINVAL; 1278 ret = -EINVAL;
1512 1279 } else {
1513 *band = bands_adc[band->index]; 1280 *band = bands[band->index];
1281 ret = 0;
1282 }
1514 } else if (band->tuner == 1) { 1283 } else if (band->tuner == 1) {
1515 if (band->index >= ARRAY_SIZE(bands_rf)) 1284 ret = v4l2_subdev_call(s->v4l2_subdev, tuner,
1516 return -EINVAL; 1285 enum_freq_bands, band);
1517
1518 *band = bands_rf[band->index];
1519 } else { 1286 } else {
1520 return -EINVAL; 1287 ret = -EINVAL;
1521 } 1288 }
1522 1289
1523 return 0; 1290 return ret;
1524} 1291}
1525 1292
1526static const struct v4l2_ioctl_ops msi3101_ioctl_ops = { 1293static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
@@ -1570,39 +1337,6 @@ static struct video_device msi3101_template = {
1570 .ioctl_ops = &msi3101_ioctl_ops, 1337 .ioctl_ops = &msi3101_ioctl_ops,
1571}; 1338};
1572 1339
1573static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
1574{
1575 struct msi3101_state *s =
1576 container_of(ctrl->handler, struct msi3101_state,
1577 hdl);
1578 int ret;
1579 dev_dbg(&s->udev->dev,
1580 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
1581 __func__, ctrl->id, ctrl->name, ctrl->val,
1582 ctrl->minimum, ctrl->maximum, ctrl->step);
1583
1584 switch (ctrl->id) {
1585 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1586 case V4L2_CID_RF_TUNER_BANDWIDTH:
1587 ret = msi3101_set_tuner(s);
1588 break;
1589 case V4L2_CID_RF_TUNER_LNA_GAIN:
1590 case V4L2_CID_RF_TUNER_MIXER_GAIN:
1591 case V4L2_CID_RF_TUNER_IF_GAIN:
1592 ret = msi3101_set_gain(s);
1593 break;
1594 default:
1595 dev_dbg(&s->udev->dev, "%s: EINVAL\n", __func__);
1596 ret = -EINVAL;
1597 }
1598
1599 return ret;
1600}
1601
1602static const struct v4l2_ctrl_ops msi3101_ctrl_ops = {
1603 .s_ctrl = msi3101_s_ctrl,
1604};
1605
1606static void msi3101_video_release(struct v4l2_device *v) 1340static void msi3101_video_release(struct v4l2_device *v)
1607{ 1341{
1608 struct msi3101_state *s = 1342 struct msi3101_state *s =
@@ -1613,13 +1347,43 @@ static void msi3101_video_release(struct v4l2_device *v)
1613 kfree(s); 1347 kfree(s);
1614} 1348}
1615 1349
1350static int msi3101_transfer_one_message(struct spi_master *master,
1351 struct spi_message *m)
1352{
1353 struct msi3101_state *s = spi_master_get_devdata(master);
1354 struct spi_transfer *t;
1355 int ret = 0;
1356 u32 data;
1357
1358 list_for_each_entry(t, &m->transfers, transfer_list) {
1359 dev_dbg(&s->udev->dev, "%s: msg=%*ph\n",
1360 __func__, t->len, t->tx_buf);
1361 data = 0x09; /* reg 9 is SPI adapter */
1362 data |= ((u8 *)t->tx_buf)[0] << 8;
1363 data |= ((u8 *)t->tx_buf)[1] << 16;
1364 data |= ((u8 *)t->tx_buf)[2] << 24;
1365 ret = msi3101_ctrl_msg(s, CMD_WREG, data);
1366 }
1367
1368 m->status = ret;
1369 spi_finalize_current_message(master);
1370 return ret;
1371}
1372
1616static int msi3101_probe(struct usb_interface *intf, 1373static int msi3101_probe(struct usb_interface *intf,
1617 const struct usb_device_id *id) 1374 const struct usb_device_id *id)
1618{ 1375{
1619 struct usb_device *udev = interface_to_usbdev(intf); 1376 struct usb_device *udev = interface_to_usbdev(intf);
1620 struct msi3101_state *s = NULL; 1377 struct msi3101_state *s = NULL;
1621 const struct v4l2_ctrl_ops *ops = &msi3101_ctrl_ops; 1378 struct v4l2_subdev *sd;
1379 struct spi_master *master;
1622 int ret; 1380 int ret;
1381 static struct spi_board_info board_info = {
1382 .modalias = "msi001",
1383 .bus_num = 0,
1384 .chip_select = 0,
1385 .max_speed_hz = 12000000,
1386 };
1623 1387
1624 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL); 1388 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1625 if (s == NULL) { 1389 if (s == NULL) {
@@ -1632,7 +1396,7 @@ static int msi3101_probe(struct usb_interface *intf,
1632 spin_lock_init(&s->queued_bufs_lock); 1396 spin_lock_init(&s->queued_bufs_lock);
1633 INIT_LIST_HEAD(&s->queued_bufs); 1397 INIT_LIST_HEAD(&s->queued_bufs);
1634 s->udev = udev; 1398 s->udev = udev;
1635 s->f_adc = bands_adc[0].rangelow; 1399 s->f_adc = bands[0].rangelow;
1636 s->pixelformat = V4L2_SDR_FMT_CU8; 1400 s->pixelformat = V4L2_SDR_FMT_CU8;
1637 1401
1638 /* Init videobuf2 queue structure */ 1402 /* Init videobuf2 queue structure */
@@ -1656,34 +1420,53 @@ static int msi3101_probe(struct usb_interface *intf,
1656 set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags); 1420 set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags);
1657 video_set_drvdata(&s->vdev, s); 1421 video_set_drvdata(&s->vdev, s);
1658 1422
1659 /* Register controls */
1660 v4l2_ctrl_handler_init(&s->hdl, 5);
1661 s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, ops,
1662 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
1663 s->bandwidth = v4l2_ctrl_new_std(&s->hdl, ops,
1664 V4L2_CID_RF_TUNER_BANDWIDTH, 0, 8000000, 1, 0);
1665 v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
1666 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, ops,
1667 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
1668 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, ops,
1669 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
1670 s->if_gain = v4l2_ctrl_new_std(&s->hdl, ops,
1671 V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
1672 if (s->hdl.error) {
1673 ret = s->hdl.error;
1674 dev_err(&s->udev->dev, "Could not initialize controls\n");
1675 goto err_free_controls;
1676 }
1677
1678 /* Register the v4l2_device structure */ 1423 /* Register the v4l2_device structure */
1679 s->v4l2_dev.release = msi3101_video_release; 1424 s->v4l2_dev.release = msi3101_video_release;
1680 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev); 1425 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1681 if (ret) { 1426 if (ret) {
1682 dev_err(&s->udev->dev, 1427 dev_err(&s->udev->dev,
1683 "Failed to register v4l2-device (%d)\n", ret); 1428 "Failed to register v4l2-device (%d)\n", ret);
1429 goto err_free_mem;
1430 }
1431
1432 /* SPI master adapter */
1433 master = spi_alloc_master(&s->udev->dev, 0);
1434 if (master == NULL) {
1435 ret = -ENOMEM;
1436 goto err_unregister_v4l2_dev;
1437 }
1438
1439 s->master = master;
1440 master->bus_num = 0;
1441 master->num_chipselect = 1;
1442 master->transfer_one_message = msi3101_transfer_one_message;
1443 spi_master_set_devdata(master, s);
1444 ret = spi_register_master(master);
1445 if (ret) {
1446 spi_master_put(master);
1447 goto err_unregister_v4l2_dev;
1448 }
1449
1450 /* load v4l2 subdevice */
1451 sd = v4l2_spi_new_subdev(&s->v4l2_dev, master, &board_info);
1452 s->v4l2_subdev = sd;
1453 if (sd == NULL) {
1454 dev_err(&s->udev->dev, "cannot get v4l2 subdevice\n");
1455 ret = -ENODEV;
1456 goto err_unregister_master;
1457 }
1458
1459 /* Register controls */
1460 v4l2_ctrl_handler_init(&s->hdl, 0);
1461 if (s->hdl.error) {
1462 ret = s->hdl.error;
1463 dev_err(&s->udev->dev, "Could not initialize controls\n");
1684 goto err_free_controls; 1464 goto err_free_controls;
1685 } 1465 }
1686 1466
1467 /* currently all controls are from subdev */
1468 v4l2_ctrl_add_handler(&s->hdl, sd->ctrl_handler, NULL);
1469
1687 s->v4l2_dev.ctrl_handler = &s->hdl; 1470 s->v4l2_dev.ctrl_handler = &s->hdl;
1688 s->vdev.v4l2_dev = &s->v4l2_dev; 1471 s->vdev.v4l2_dev = &s->v4l2_dev;
1689 s->vdev.lock = &s->v4l2_lock; 1472 s->vdev.lock = &s->v4l2_lock;
@@ -1700,10 +1483,12 @@ static int msi3101_probe(struct usb_interface *intf,
1700 1483
1701 return 0; 1484 return 0;
1702 1485
1703err_unregister_v4l2_dev:
1704 v4l2_device_unregister(&s->v4l2_dev);
1705err_free_controls: 1486err_free_controls:
1706 v4l2_ctrl_handler_free(&s->hdl); 1487 v4l2_ctrl_handler_free(&s->hdl);
1488err_unregister_master:
1489 spi_unregister_master(s->master);
1490err_unregister_v4l2_dev:
1491 v4l2_device_unregister(&s->v4l2_dev);
1707err_free_mem: 1492err_free_mem:
1708 kfree(s); 1493 kfree(s);
1709 return ret; 1494 return ret;