Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[media] e4000: implement V4L2 subdevice tuner and core ops

Implement V4L2 subdevice tuner and core ops. After that this driver
is hybrid driver implementing both V4L2 and DVB ops.

Signed-off-by: Antti Palosaari <crope@iki.fi>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>

authored by

Antti Palosaari and committed by
Mauro Carvalho Chehab
c7861bb0 f8b9b871

+183 -37
+181 -37
drivers/media/tuners/e4000.c
··· 20 20 21 21 #include "e4000_priv.h" 22 22 23 - static int e4000_init(struct dvb_frontend *fe) 23 + static int e4000_init(struct e4000_dev *dev) 24 24 { 25 - struct e4000_dev *dev = fe->tuner_priv; 26 25 struct i2c_client *client = dev->client; 27 26 int ret; 28 27 ··· 88 89 return ret; 89 90 } 90 91 91 - static int e4000_sleep(struct dvb_frontend *fe) 92 + static int e4000_sleep(struct e4000_dev *dev) 92 93 { 93 - struct e4000_dev *dev = fe->tuner_priv; 94 94 struct i2c_client *client = dev->client; 95 95 int ret; 96 96 ··· 107 109 return ret; 108 110 } 109 111 110 - static int e4000_set_params(struct dvb_frontend *fe) 112 + static int e4000_set_params(struct e4000_dev *dev) 111 113 { 112 - struct e4000_dev *dev = fe->tuner_priv; 113 114 struct i2c_client *client = dev->client; 114 - struct dtv_frontend_properties *c = &fe->dtv_property_cache; 115 115 int ret, i; 116 116 unsigned int div_n, k, k_cw, div_out; 117 117 u64 f_vco; 118 118 u8 buf[5], i_data[4], q_data[4]; 119 119 120 - dev_dbg(&client->dev, 121 - "delivery_system=%d frequency=%u bandwidth_hz=%u\n", 122 - c->delivery_system, c->frequency, c->bandwidth_hz); 120 + if (!dev->active) { 121 + dev_dbg(&client->dev, "tuner is sleeping\n"); 122 + return 0; 123 + } 123 124 124 125 /* gain control manual */ 125 126 ret = regmap_write(dev->regmap, 0x1a, 0x00); ··· 141 144 * +-------+ 142 145 */ 143 146 for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) { 144 - if (c->frequency <= e4000_pll_lut[i].freq) 147 + if (dev->f_frequency <= e4000_pll_lut[i].freq) 145 148 break; 146 149 } 147 150 if (i == ARRAY_SIZE(e4000_pll_lut)) { ··· 151 154 152 155 #define F_REF dev->clk 153 156 div_out = e4000_pll_lut[i].div_out; 154 - f_vco = (u64) c->frequency * div_out; 157 + f_vco = (u64) dev->f_frequency * div_out; 155 158 /* calculate PLL integer and fractional control word */ 156 159 div_n = div_u64_rem(f_vco, F_REF, &k); 157 160 k_cw = div_u64((u64) k * 0x10000, F_REF); 158 161 159 162 dev_dbg(&client->dev, 160 - "frequency=%u f_vco=%llu F_REF=%u div_n=%u k=%u k_cw=%04x div_out=%u\n", 161 - c->frequency, f_vco, F_REF, div_n, k, k_cw, div_out); 163 + "frequency=%u bandwidth=%u f_vco=%llu F_REF=%u div_n=%u k=%u k_cw=%04x div_out=%u\n", 164 + dev->f_frequency, dev->f_bandwidth, f_vco, F_REF, div_n, k, 165 + k_cw, div_out); 162 166 163 167 buf[0] = div_n; 164 168 buf[1] = (k_cw >> 0) & 0xff; ··· 172 174 173 175 /* LNA filter (RF filter) */ 174 176 for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) { 175 - if (c->frequency <= e400_lna_filter_lut[i].freq) 177 + if (dev->f_frequency <= e400_lna_filter_lut[i].freq) 176 178 break; 177 179 } 178 180 if (i == ARRAY_SIZE(e400_lna_filter_lut)) { ··· 186 188 187 189 /* IF filters */ 188 190 for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) { 189 - if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq) 191 + if (dev->f_bandwidth <= e4000_if_filter_lut[i].freq) 190 192 break; 191 193 } 192 194 if (i == ARRAY_SIZE(e4000_if_filter_lut)) { ··· 203 205 204 206 /* frequency band */ 205 207 for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) { 206 - if (c->frequency <= e4000_band_lut[i].freq) 208 + if (dev->f_frequency <= e4000_band_lut[i].freq) 207 209 break; 208 210 } 209 211 if (i == ARRAY_SIZE(e4000_band_lut)) { ··· 267 269 return ret; 268 270 } 269 271 270 - static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) 272 + /* 273 + * V4L2 API 274 + */ 275 + #if IS_ENABLED(CONFIG_VIDEO_V4L2) 276 + static const struct v4l2_frequency_band bands[] = { 277 + { 278 + .type = V4L2_TUNER_RF, 279 + .index = 0, 280 + .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 281 + .rangelow = 59000000, 282 + .rangehigh = 1105000000, 283 + }, 284 + { 285 + .type = V4L2_TUNER_RF, 286 + .index = 1, 287 + .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 288 + .rangelow = 1249000000, 289 + .rangehigh = 2208000000, 290 + }, 291 + }; 292 + 293 + static inline struct e4000_dev *e4000_subdev_to_dev(struct v4l2_subdev *sd) 271 294 { 272 - struct e4000_dev *dev = fe->tuner_priv; 295 + return container_of(sd, struct e4000_dev, sd); 296 + } 297 + 298 + static int e4000_s_power(struct v4l2_subdev *sd, int on) 299 + { 300 + struct e4000_dev *dev = e4000_subdev_to_dev(sd); 301 + struct i2c_client *client = dev->client; 302 + int ret; 303 + 304 + dev_dbg(&client->dev, "on=%d\n", on); 305 + 306 + if (on) 307 + ret = e4000_init(dev); 308 + else 309 + ret = e4000_sleep(dev); 310 + if (ret) 311 + return ret; 312 + 313 + return e4000_set_params(dev); 314 + } 315 + 316 + static const struct v4l2_subdev_core_ops e4000_subdev_core_ops = { 317 + .s_power = e4000_s_power, 318 + }; 319 + 320 + static int e4000_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v) 321 + { 322 + struct e4000_dev *dev = e4000_subdev_to_dev(sd); 273 323 struct i2c_client *client = dev->client; 274 324 275 - dev_dbg(&client->dev, "\n"); 325 + dev_dbg(&client->dev, "index=%d\n", v->index); 276 326 277 - *frequency = 0; /* Zero-IF */ 278 - 327 + strlcpy(v->name, "Elonics E4000", sizeof(v->name)); 328 + v->type = V4L2_TUNER_RF; 329 + v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 330 + v->rangelow = bands[0].rangelow; 331 + v->rangehigh = bands[1].rangehigh; 279 332 return 0; 280 333 } 281 334 282 - #if IS_ENABLED(CONFIG_VIDEO_V4L2) 335 + static int e4000_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v) 336 + { 337 + struct e4000_dev *dev = e4000_subdev_to_dev(sd); 338 + struct i2c_client *client = dev->client; 339 + 340 + dev_dbg(&client->dev, "index=%d\n", v->index); 341 + return 0; 342 + } 343 + 344 + static int e4000_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f) 345 + { 346 + struct e4000_dev *dev = e4000_subdev_to_dev(sd); 347 + struct i2c_client *client = dev->client; 348 + 349 + dev_dbg(&client->dev, "tuner=%d\n", f->tuner); 350 + f->frequency = dev->f_frequency; 351 + return 0; 352 + } 353 + 354 + static int e4000_s_frequency(struct v4l2_subdev *sd, 355 + const struct v4l2_frequency *f) 356 + { 357 + struct e4000_dev *dev = e4000_subdev_to_dev(sd); 358 + struct i2c_client *client = dev->client; 359 + 360 + dev_dbg(&client->dev, "tuner=%d type=%d frequency=%u\n", 361 + f->tuner, f->type, f->frequency); 362 + 363 + dev->f_frequency = clamp_t(unsigned int, f->frequency, 364 + bands[0].rangelow, bands[1].rangehigh); 365 + return e4000_set_params(dev); 366 + } 367 + 368 + static int e4000_enum_freq_bands(struct v4l2_subdev *sd, 369 + struct v4l2_frequency_band *band) 370 + { 371 + struct e4000_dev *dev = e4000_subdev_to_dev(sd); 372 + struct i2c_client *client = dev->client; 373 + 374 + dev_dbg(&client->dev, "tuner=%d type=%d index=%d\n", 375 + band->tuner, band->type, band->index); 376 + 377 + if (band->index >= ARRAY_SIZE(bands)) 378 + return -EINVAL; 379 + 380 + band->capability = bands[band->index].capability; 381 + band->rangelow = bands[band->index].rangelow; 382 + band->rangehigh = bands[band->index].rangehigh; 383 + return 0; 384 + } 385 + 386 + static const struct v4l2_subdev_tuner_ops e4000_subdev_tuner_ops = { 387 + .g_tuner = e4000_g_tuner, 388 + .s_tuner = e4000_s_tuner, 389 + .g_frequency = e4000_g_frequency, 390 + .s_frequency = e4000_s_frequency, 391 + .enum_freq_bands = e4000_enum_freq_bands, 392 + }; 393 + 394 + static const struct v4l2_subdev_ops e4000_subdev_ops = { 395 + .core = &e4000_subdev_core_ops, 396 + .tuner = &e4000_subdev_tuner_ops, 397 + }; 398 + 283 399 static int e4000_set_lna_gain(struct dvb_frontend *fe) 284 400 { 285 401 struct e4000_dev *dev = fe->tuner_priv; ··· 546 434 { 547 435 struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl); 548 436 struct i2c_client *client = dev->client; 549 - struct dtv_frontend_properties *c = &dev->fe->dtv_property_cache; 550 437 int ret; 551 438 552 439 if (!dev->active) ··· 554 443 switch (ctrl->id) { 555 444 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: 556 445 case V4L2_CID_RF_TUNER_BANDWIDTH: 557 - c->bandwidth_hz = dev->bandwidth->val; 558 - ret = e4000_set_params(dev->fe); 446 + /* 447 + * TODO: Auto logic does not work 100% correctly as tuner driver 448 + * do not have information to calculate maximum suitable 449 + * bandwidth. Calculating it is responsible of master driver. 450 + */ 451 + dev->f_bandwidth = dev->bandwidth->val; 452 + ret = e4000_set_params(dev); 559 453 break; 560 454 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO: 561 455 case V4L2_CID_RF_TUNER_LNA_GAIN: ··· 589 473 }; 590 474 #endif 591 475 592 - static const struct dvb_tuner_ops e4000_tuner_ops = { 476 + /* 477 + * DVB API 478 + */ 479 + static int e4000_dvb_set_params(struct dvb_frontend *fe) 480 + { 481 + struct e4000_dev *dev = fe->tuner_priv; 482 + struct dtv_frontend_properties *c = &fe->dtv_property_cache; 483 + 484 + dev->f_frequency = c->frequency; 485 + dev->f_bandwidth = c->bandwidth_hz; 486 + return e4000_set_params(dev); 487 + } 488 + 489 + static int e4000_dvb_init(struct dvb_frontend *fe) 490 + { 491 + return e4000_init(fe->tuner_priv); 492 + } 493 + 494 + static int e4000_dvb_sleep(struct dvb_frontend *fe) 495 + { 496 + return e4000_sleep(fe->tuner_priv); 497 + } 498 + 499 + static int e4000_dvb_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) 500 + { 501 + *frequency = 0; /* Zero-IF */ 502 + return 0; 503 + } 504 + 505 + static const struct dvb_tuner_ops e4000_dvb_tuner_ops = { 593 506 .info = { 594 507 .name = "Elonics E4000", 595 508 .frequency_min = 174000000, 596 509 .frequency_max = 862000000, 597 510 }, 598 511 599 - .init = e4000_init, 600 - .sleep = e4000_sleep, 601 - .set_params = e4000_set_params, 512 + .init = e4000_dvb_init, 513 + .sleep = e4000_dvb_sleep, 514 + .set_params = e4000_dvb_set_params, 602 515 603 - .get_if_frequency = e4000_get_if_frequency, 516 + .get_if_frequency = e4000_dvb_get_if_frequency, 604 517 }; 605 518 606 - /* 607 - * Use V4L2 subdev to carry V4L2 control handler, even we don't implement 608 - * subdev itself, just to avoid reinventing the wheel. 609 - */ 610 519 static int e4000_probe(struct i2c_client *client, 611 520 const struct i2c_device_id *id) 612 521 { ··· 710 569 } 711 570 712 571 dev->sd.ctrl_handler = &dev->hdl; 572 + dev->f_frequency = bands[0].rangelow; 573 + dev->f_bandwidth = dev->bandwidth->val; 574 + v4l2_i2c_subdev_init(&dev->sd, client, &e4000_subdev_ops); 713 575 #endif 714 576 fe->tuner_priv = dev; 715 - memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops, 716 - sizeof(struct dvb_tuner_ops)); 577 + memcpy(&fe->ops.tuner_ops, &e4000_dvb_tuner_ops, 578 + sizeof(fe->ops.tuner_ops)); 717 579 v4l2_set_subdevdata(&dev->sd, client); 718 580 i2c_set_clientdata(client, &dev->sd); 719 581
+2
drivers/media/tuners/e4000_priv.h
··· 34 34 struct dvb_frontend *fe; 35 35 struct v4l2_subdev sd; 36 36 bool active; 37 + unsigned int f_frequency; 38 + unsigned int f_bandwidth; 37 39 38 40 /* Controls */ 39 41 struct v4l2_ctrl_handler hdl;