Linux Audio

Check our new training course

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