1/* $NetBSD: ch7006_drv.c,v 1.3 2021/12/18 23:45:27 riastradh Exp $ */ 2 3/* 4 * Copyright (C) 2009 Francisco Jerez. 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial 17 * portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 */ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: ch7006_drv.c,v 1.3 2021/12/18 23:45:27 riastradh Exp $"); 31 32#include <linux/module.h> 33 34#include "ch7006_priv.h" 35 36/* DRM encoder functions */ 37 38static void ch7006_encoder_set_config(struct drm_encoder *encoder, 39 void *params) 40{ 41 struct ch7006_priv *priv = to_ch7006_priv(encoder); 42 43 priv->params = *(struct ch7006_encoder_params *)params; 44} 45 46static void ch7006_encoder_destroy(struct drm_encoder *encoder) 47{ 48 struct ch7006_priv *priv = to_ch7006_priv(encoder); 49 50 drm_property_destroy(encoder->dev, priv->scale_property); 51 52 kfree(priv); 53 to_encoder_slave(encoder)->slave_priv = NULL; 54 55 drm_i2c_encoder_destroy(encoder); 56} 57 58static void ch7006_encoder_dpms(struct drm_encoder *encoder, int mode) 59{ 60 struct i2c_client *client = drm_i2c_encoder_get_client(encoder); 61 struct ch7006_priv *priv = to_ch7006_priv(encoder); 62 struct ch7006_state *state = &priv->state; 63 64 ch7006_dbg(client, "\n"); 65 66 if (mode == priv->last_dpms) 67 return; 68 priv->last_dpms = mode; 69 70 ch7006_setup_power_state(encoder); 71 72 ch7006_load_reg(client, state, CH7006_POWER); 73} 74 75static void ch7006_encoder_save(struct drm_encoder *encoder) 76{ 77 struct i2c_client *client = drm_i2c_encoder_get_client(encoder); 78 struct ch7006_priv *priv = to_ch7006_priv(encoder); 79 80 ch7006_dbg(client, "\n"); 81 82 ch7006_state_save(client, &priv->saved_state); 83} 84 85static void ch7006_encoder_restore(struct drm_encoder *encoder) 86{ 87 struct i2c_client *client = drm_i2c_encoder_get_client(encoder); 88 struct ch7006_priv *priv = to_ch7006_priv(encoder); 89 90 ch7006_dbg(client, "\n"); 91 92 ch7006_state_load(client, &priv->saved_state); 93} 94 95static bool ch7006_encoder_mode_fixup(struct drm_encoder *encoder, 96 const struct drm_display_mode *mode, 97 struct drm_display_mode *adjusted_mode) 98{ 99 struct ch7006_priv *priv = to_ch7006_priv(encoder); 100 101 /* The ch7006 is painfully picky with the input timings so no 102 * custom modes for now... */ 103 104 priv->mode = ch7006_lookup_mode(encoder, mode); 105 106 return !!priv->mode; 107} 108 109static int ch7006_encoder_mode_valid(struct drm_encoder *encoder, 110 struct drm_display_mode *mode) 111{ 112 if (ch7006_lookup_mode(encoder, mode)) 113 return MODE_OK; 114 else 115 return MODE_BAD; 116} 117 118static void ch7006_encoder_mode_set(struct drm_encoder *encoder, 119 struct drm_display_mode *drm_mode, 120 struct drm_display_mode *adjusted_mode) 121{ 122 struct i2c_client *client = drm_i2c_encoder_get_client(encoder); 123 struct ch7006_priv *priv = to_ch7006_priv(encoder); 124 struct ch7006_encoder_params *params = &priv->params; 125 struct ch7006_state *state = &priv->state; 126 uint8_t *regs = state->regs; 127 const struct ch7006_mode *mode = priv->mode; 128 const struct ch7006_tv_norm_info *norm = &ch7006_tv_norms[priv->norm]; 129 int start_active; 130 131 ch7006_dbg(client, "\n"); 132 133 regs[CH7006_DISPMODE] = norm->dispmode | mode->dispmode; 134 regs[CH7006_BWIDTH] = 0; 135 regs[CH7006_INPUT_FORMAT] = bitf(CH7006_INPUT_FORMAT_FORMAT, 136 params->input_format); 137 138 regs[CH7006_CLKMODE] = CH7006_CLKMODE_SUBC_LOCK 139 | bitf(CH7006_CLKMODE_XCM, params->xcm) 140 | bitf(CH7006_CLKMODE_PCM, params->pcm); 141 if (params->clock_mode) 142 regs[CH7006_CLKMODE] |= CH7006_CLKMODE_MASTER; 143 if (params->clock_edge) 144 regs[CH7006_CLKMODE] |= CH7006_CLKMODE_POS_EDGE; 145 146 start_active = (drm_mode->htotal & ~0x7) - (drm_mode->hsync_start & ~0x7); 147 regs[CH7006_POV] = bitf(CH7006_POV_START_ACTIVE_8, start_active); 148 regs[CH7006_START_ACTIVE] = bitf(CH7006_START_ACTIVE_0, start_active); 149 150 regs[CH7006_INPUT_SYNC] = 0; 151 if (params->sync_direction) 152 regs[CH7006_INPUT_SYNC] |= CH7006_INPUT_SYNC_OUTPUT; 153 if (params->sync_encoding) 154 regs[CH7006_INPUT_SYNC] |= CH7006_INPUT_SYNC_EMBEDDED; 155 if (drm_mode->flags & DRM_MODE_FLAG_PVSYNC) 156 regs[CH7006_INPUT_SYNC] |= CH7006_INPUT_SYNC_PVSYNC; 157 if (drm_mode->flags & DRM_MODE_FLAG_PHSYNC) 158 regs[CH7006_INPUT_SYNC] |= CH7006_INPUT_SYNC_PHSYNC; 159 160 regs[CH7006_DETECT] = 0; 161 regs[CH7006_BCLKOUT] = 0; 162 163 regs[CH7006_SUBC_INC3] = 0; 164 if (params->pout_level) 165 regs[CH7006_SUBC_INC3] |= CH7006_SUBC_INC3_POUT_3_3V; 166 167 regs[CH7006_SUBC_INC4] = 0; 168 if (params->active_detect) 169 regs[CH7006_SUBC_INC4] |= CH7006_SUBC_INC4_DS_INPUT; 170 171 regs[CH7006_PLL_CONTROL] = priv->saved_state.regs[CH7006_PLL_CONTROL]; 172 173 ch7006_setup_levels(encoder); 174 ch7006_setup_subcarrier(encoder); 175 ch7006_setup_pll(encoder); 176 ch7006_setup_power_state(encoder); 177 ch7006_setup_properties(encoder); 178 179 ch7006_state_load(client, state); 180} 181 182static enum drm_connector_status ch7006_encoder_detect(struct drm_encoder *encoder, 183 struct drm_connector *connector) 184{ 185 struct i2c_client *client = drm_i2c_encoder_get_client(encoder); 186 struct ch7006_priv *priv = to_ch7006_priv(encoder); 187 struct ch7006_state *state = &priv->state; 188 int det; 189 190 ch7006_dbg(client, "\n"); 191 192 ch7006_save_reg(client, state, CH7006_DETECT); 193 ch7006_save_reg(client, state, CH7006_POWER); 194 ch7006_save_reg(client, state, CH7006_CLKMODE); 195 196 ch7006_write(client, CH7006_POWER, CH7006_POWER_RESET | 197 bitfs(CH7006_POWER_LEVEL, NORMAL)); 198 ch7006_write(client, CH7006_CLKMODE, CH7006_CLKMODE_MASTER); 199 200 ch7006_write(client, CH7006_DETECT, CH7006_DETECT_SENSE); 201 202 ch7006_write(client, CH7006_DETECT, 0); 203 204 det = ch7006_read(client, CH7006_DETECT); 205 206 ch7006_load_reg(client, state, CH7006_CLKMODE); 207 ch7006_load_reg(client, state, CH7006_POWER); 208 ch7006_load_reg(client, state, CH7006_DETECT); 209 210 if ((det & (CH7006_DETECT_SVIDEO_Y_TEST| 211 CH7006_DETECT_SVIDEO_C_TEST| 212 CH7006_DETECT_CVBS_TEST)) == 0) 213 priv->subconnector = DRM_MODE_SUBCONNECTOR_SCART; 214 else if ((det & (CH7006_DETECT_SVIDEO_Y_TEST| 215 CH7006_DETECT_SVIDEO_C_TEST)) == 0) 216 priv->subconnector = DRM_MODE_SUBCONNECTOR_SVIDEO; 217 else if ((det & CH7006_DETECT_CVBS_TEST) == 0) 218 priv->subconnector = DRM_MODE_SUBCONNECTOR_Composite; 219 else 220 priv->subconnector = DRM_MODE_SUBCONNECTOR_Unknown; 221 222 drm_object_property_set_value(&connector->base, 223 encoder->dev->mode_config.tv_subconnector_property, 224 priv->subconnector); 225 226 return priv->subconnector ? connector_status_connected : 227 connector_status_disconnected; 228} 229 230static int ch7006_encoder_get_modes(struct drm_encoder *encoder, 231 struct drm_connector *connector) 232{ 233 struct ch7006_priv *priv = to_ch7006_priv(encoder); 234 const struct ch7006_mode *mode; 235 int n = 0; 236 237 for (mode = ch7006_modes; mode->mode.clock; mode++) { 238 if (~mode->valid_scales & 1<<priv->scale || 239 ~mode->valid_norms & 1<<priv->norm) 240 continue; 241 242 drm_mode_probed_add(connector, 243 drm_mode_duplicate(encoder->dev, &mode->mode)); 244 245 n++; 246 } 247 248 return n; 249} 250 251static int ch7006_encoder_create_resources(struct drm_encoder *encoder, 252 struct drm_connector *connector) 253{ 254 struct ch7006_priv *priv = to_ch7006_priv(encoder); 255 struct drm_device *dev = encoder->dev; 256 struct drm_mode_config *conf = &dev->mode_config; 257 258 drm_mode_create_tv_properties(dev, NUM_TV_NORMS, ch7006_tv_norm_names); 259 260 priv->scale_property = drm_property_create_range(dev, 0, "scale", 0, 2); 261 if (!priv->scale_property) 262 return -ENOMEM; 263 264 drm_object_attach_property(&connector->base, conf->tv_select_subconnector_property, 265 priv->select_subconnector); 266 drm_object_attach_property(&connector->base, conf->tv_subconnector_property, 267 priv->subconnector); 268 drm_object_attach_property(&connector->base, conf->tv_left_margin_property, 269 priv->hmargin); 270 drm_object_attach_property(&connector->base, conf->tv_bottom_margin_property, 271 priv->vmargin); 272 drm_object_attach_property(&connector->base, conf->tv_mode_property, 273 priv->norm); 274 drm_object_attach_property(&connector->base, conf->tv_brightness_property, 275 priv->brightness); 276 drm_object_attach_property(&connector->base, conf->tv_contrast_property, 277 priv->contrast); 278 drm_object_attach_property(&connector->base, conf->tv_flicker_reduction_property, 279 priv->flicker); 280 drm_object_attach_property(&connector->base, priv->scale_property, 281 priv->scale); 282 283 return 0; 284} 285 286static int ch7006_encoder_set_property(struct drm_encoder *encoder, 287 struct drm_connector *connector, 288 struct drm_property *property, 289 uint64_t val) 290{ 291 struct i2c_client *client = drm_i2c_encoder_get_client(encoder); 292 struct ch7006_priv *priv = to_ch7006_priv(encoder); 293 struct ch7006_state *state = &priv->state; 294 struct drm_mode_config *conf = &encoder->dev->mode_config; 295 struct drm_crtc *crtc = encoder->crtc; 296 bool modes_changed = false; 297 298 ch7006_dbg(client, "\n"); 299 300 if (property == conf->tv_select_subconnector_property) { 301 priv->select_subconnector = val; 302 303 ch7006_setup_power_state(encoder); 304 305 ch7006_load_reg(client, state, CH7006_POWER); 306 307 } else if (property == conf->tv_left_margin_property) { 308 priv->hmargin = val; 309 310 ch7006_setup_properties(encoder); 311 312 ch7006_load_reg(client, state, CH7006_POV); 313 ch7006_load_reg(client, state, CH7006_HPOS); 314 315 } else if (property == conf->tv_bottom_margin_property) { 316 priv->vmargin = val; 317 318 ch7006_setup_properties(encoder); 319 320 ch7006_load_reg(client, state, CH7006_POV); 321 ch7006_load_reg(client, state, CH7006_VPOS); 322 323 } else if (property == conf->tv_mode_property) { 324 if (connector->dpms != DRM_MODE_DPMS_OFF) 325 return -EINVAL; 326 327 priv->norm = val; 328 329 modes_changed = true; 330 331 } else if (property == conf->tv_brightness_property) { 332 priv->brightness = val; 333 334 ch7006_setup_levels(encoder); 335 336 ch7006_load_reg(client, state, CH7006_BLACK_LEVEL); 337 338 } else if (property == conf->tv_contrast_property) { 339 priv->contrast = val; 340 341 ch7006_setup_properties(encoder); 342 343 ch7006_load_reg(client, state, CH7006_CONTRAST); 344 345 } else if (property == conf->tv_flicker_reduction_property) { 346 priv->flicker = val; 347 348 ch7006_setup_properties(encoder); 349 350 ch7006_load_reg(client, state, CH7006_FFILTER); 351 352 } else if (property == priv->scale_property) { 353 if (connector->dpms != DRM_MODE_DPMS_OFF) 354 return -EINVAL; 355 356 priv->scale = val; 357 358 modes_changed = true; 359 360 } else { 361 return -EINVAL; 362 } 363 364 if (modes_changed) { 365 drm_helper_probe_single_connector_modes(connector, 0, 0); 366 367 if (crtc) 368 drm_crtc_helper_set_mode(crtc, &crtc->mode, 369 crtc->x, crtc->y, 370 crtc->primary->fb); 371 } 372 373 return 0; 374} 375 376static const struct drm_encoder_slave_funcs ch7006_encoder_funcs = { 377 .set_config = ch7006_encoder_set_config, 378 .destroy = ch7006_encoder_destroy, 379 .dpms = ch7006_encoder_dpms, 380 .save = ch7006_encoder_save, 381 .restore = ch7006_encoder_restore, 382 .mode_fixup = ch7006_encoder_mode_fixup, 383 .mode_valid = ch7006_encoder_mode_valid, 384 .mode_set = ch7006_encoder_mode_set, 385 .detect = ch7006_encoder_detect, 386 .get_modes = ch7006_encoder_get_modes, 387 .create_resources = ch7006_encoder_create_resources, 388 .set_property = ch7006_encoder_set_property, 389}; 390 391 392/* I2C driver functions */ 393 394static int ch7006_probe(struct i2c_client *client, const struct i2c_device_id *id) 395{ 396 uint8_t addr = CH7006_VERSION_ID; 397 uint8_t val; 398 int ret; 399 400 ch7006_dbg(client, "\n"); 401 402 ret = i2c_master_send(client, &addr, sizeof(addr)); 403 if (ret < 0) 404 goto fail; 405 406 ret = i2c_master_recv(client, &val, sizeof(val)); 407 if (ret < 0) 408 goto fail; 409 410 ch7006_info(client, "Detected version ID: %x\n", val); 411 412 /* I don't know what this is for, but otherwise I get no 413 * signal. 414 */ 415 ch7006_write(client, 0x3d, 0x0); 416 417 return 0; 418 419fail: 420 ch7006_err(client, "Error %d reading version ID\n", ret); 421 422 return -ENODEV; 423} 424 425static int ch7006_remove(struct i2c_client *client) 426{ 427 ch7006_dbg(client, "\n"); 428 429 return 0; 430} 431 432static int ch7006_resume(struct device *dev) 433{ 434 struct i2c_client *client = to_i2c_client(dev); 435 436 ch7006_dbg(client, "\n"); 437 438 ch7006_write(client, 0x3d, 0x0); 439 440 return 0; 441} 442 443static int ch7006_encoder_init(struct i2c_client *client, 444 struct drm_device *dev, 445 struct drm_encoder_slave *encoder) 446{ 447 struct ch7006_priv *priv; 448 int i; 449 450 ch7006_dbg(client, "\n"); 451 452 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 453 if (!priv) 454 return -ENOMEM; 455 456 encoder->slave_priv = priv; 457 encoder->slave_funcs = &ch7006_encoder_funcs; 458 459 priv->norm = TV_NORM_PAL; 460 priv->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic; 461 priv->subconnector = DRM_MODE_SUBCONNECTOR_Unknown; 462 priv->scale = 1; 463 priv->contrast = 50; 464 priv->brightness = 50; 465 priv->flicker = 50; 466 priv->hmargin = 50; 467 priv->vmargin = 50; 468 priv->last_dpms = -1; 469 priv->chip_version = ch7006_read(client, CH7006_VERSION_ID); 470 471 if (ch7006_tv_norm) { 472 for (i = 0; i < NUM_TV_NORMS; i++) { 473 if (!strcmp(ch7006_tv_norm_names[i], ch7006_tv_norm)) { 474 priv->norm = i; 475 break; 476 } 477 } 478 479 if (i == NUM_TV_NORMS) 480 ch7006_err(client, "Invalid TV norm setting \"%s\".\n", 481 ch7006_tv_norm); 482 } 483 484 if (ch7006_scale >= 0 && ch7006_scale <= 2) 485 priv->scale = ch7006_scale; 486 else 487 ch7006_err(client, "Invalid scale setting \"%d\".\n", 488 ch7006_scale); 489 490 return 0; 491} 492 493static const struct i2c_device_id ch7006_ids[] = { 494 { "ch7006", 0 }, 495 { } 496}; 497MODULE_DEVICE_TABLE(i2c, ch7006_ids); 498 499static const struct dev_pm_ops ch7006_pm_ops = { 500 .resume = ch7006_resume, 501}; 502 503static struct drm_i2c_encoder_driver ch7006_driver = { 504 .i2c_driver = { 505 .probe = ch7006_probe, 506 .remove = ch7006_remove, 507 508 .driver = { 509 .name = "ch7006", 510 .pm = &ch7006_pm_ops, 511 }, 512 513 .id_table = ch7006_ids, 514 }, 515 516 .encoder_init = ch7006_encoder_init, 517}; 518 519 520/* Module initialization */ 521 522static int __init ch7006_init(void) 523{ 524 return drm_i2c_encoder_register(THIS_MODULE, &ch7006_driver); 525} 526 527static void __exit ch7006_exit(void) 528{ 529 drm_i2c_encoder_unregister(&ch7006_driver); 530} 531 532int ch7006_debug; 533module_param_named(debug, ch7006_debug, int, 0600); 534MODULE_PARM_DESC(debug, "Enable debug output."); 535 536char *ch7006_tv_norm; 537module_param_named(tv_norm, ch7006_tv_norm, charp, 0600); 538MODULE_PARM_DESC(tv_norm, "Default TV norm.\n" 539 "\t\tSupported: PAL, PAL-M, PAL-N, PAL-Nc, PAL-60, NTSC-M, NTSC-J.\n" 540 "\t\tDefault: PAL"); 541 542int ch7006_scale = 1; 543module_param_named(scale, ch7006_scale, int, 0600); 544MODULE_PARM_DESC(scale, "Default scale.\n" 545 "\t\tSupported: 0 -> Select video modes with a higher blanking ratio.\n" 546 "\t\t\t1 -> Select default video modes.\n" 547 "\t\t\t2 -> Select video modes with a lower blanking ratio."); 548 549MODULE_AUTHOR("Francisco Jerez <currojerez@riseup.net>"); 550MODULE_DESCRIPTION("Chrontel ch7006 TV encoder driver"); 551MODULE_LICENSE("GPL and additional rights"); 552 553module_init(ch7006_init); 554module_exit(ch7006_exit); 555