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