blob: 9396829675790a5b7e513417588d75c4eec0a997 [file] [log] [blame]
Hans Verkuil14735192013-06-03 13:26:17 -03001/*
2 * ths8200 - Texas Instruments THS8200 video encoder driver
3 *
4 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation version 2.
13 *
14 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
15 * kind, whether express or implied; without even the implied warranty
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/i2c.h>
21#include <linux/module.h>
22#include <linux/v4l2-dv-timings.h>
23
24#include <media/v4l2-device.h>
25
26#include "ths8200_regs.h"
27
28static int debug;
29module_param(debug, int, 0644);
30MODULE_PARM_DESC(debug, "debug level (0-2)");
31
32MODULE_DESCRIPTION("Texas Instruments THS8200 video encoder driver");
33MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
34MODULE_AUTHOR("Martin Bugge <martin.bugge@cisco.com>");
35MODULE_LICENSE("GPL v2");
36
37struct ths8200_state {
38 struct v4l2_subdev sd;
39 uint8_t chip_version;
40 /* Is the ths8200 powered on? */
41 bool power_on;
42 struct v4l2_dv_timings dv_timings;
43};
44
45static const struct v4l2_dv_timings ths8200_timings[] = {
46 V4L2_DV_BT_CEA_720X480P59_94,
47 V4L2_DV_BT_CEA_1280X720P24,
48 V4L2_DV_BT_CEA_1280X720P25,
49 V4L2_DV_BT_CEA_1280X720P30,
50 V4L2_DV_BT_CEA_1280X720P50,
51 V4L2_DV_BT_CEA_1280X720P60,
52 V4L2_DV_BT_CEA_1920X1080P24,
53 V4L2_DV_BT_CEA_1920X1080P25,
54 V4L2_DV_BT_CEA_1920X1080P30,
55 V4L2_DV_BT_CEA_1920X1080P50,
56 V4L2_DV_BT_CEA_1920X1080P60,
57};
58
59static inline struct ths8200_state *to_state(struct v4l2_subdev *sd)
60{
61 return container_of(sd, struct ths8200_state, sd);
62}
63
64static inline unsigned hblanking(const struct v4l2_bt_timings *t)
65{
66 return t->hfrontporch + t->hsync + t->hbackporch;
67}
68
69static inline unsigned htotal(const struct v4l2_bt_timings *t)
70{
71 return t->width + t->hfrontporch + t->hsync + t->hbackporch;
72}
73
74static inline unsigned vblanking(const struct v4l2_bt_timings *t)
75{
76 return t->vfrontporch + t->vsync + t->vbackporch;
77}
78
79static inline unsigned vtotal(const struct v4l2_bt_timings *t)
80{
81 return t->height + t->vfrontporch + t->vsync + t->vbackporch;
82}
83
84static int ths8200_read(struct v4l2_subdev *sd, u8 reg)
85{
86 struct i2c_client *client = v4l2_get_subdevdata(sd);
87
88 return i2c_smbus_read_byte_data(client, reg);
89}
90
91static int ths8200_write(struct v4l2_subdev *sd, u8 reg, u8 val)
92{
93 struct i2c_client *client = v4l2_get_subdevdata(sd);
94 int ret;
95 int i;
96
97 for (i = 0; i < 3; i++) {
98 ret = i2c_smbus_write_byte_data(client, reg, val);
99 if (ret == 0)
100 return 0;
101 }
102 v4l2_err(sd, "I2C Write Problem\n");
103 return ret;
104}
105
106/* To set specific bits in the register, a clear-mask is given (to be AND-ed),
107 * and then the value-mask (to be OR-ed).
108 */
109static inline void
110ths8200_write_and_or(struct v4l2_subdev *sd, u8 reg,
111 uint8_t clr_mask, uint8_t val_mask)
112{
113 ths8200_write(sd, reg, (ths8200_read(sd, reg) & clr_mask) | val_mask);
114}
115
116#ifdef CONFIG_VIDEO_ADV_DEBUG
117
118static int ths8200_g_register(struct v4l2_subdev *sd,
119 struct v4l2_dbg_register *reg)
120{
121 struct i2c_client *client = v4l2_get_subdevdata(sd);
122
123 reg->val = ths8200_read(sd, reg->reg & 0xff);
124 reg->size = 1;
125
126 return 0;
127}
128
129static int ths8200_s_register(struct v4l2_subdev *sd,
130 const struct v4l2_dbg_register *reg)
131{
132 struct i2c_client *client = v4l2_get_subdevdata(sd);
133
134 ths8200_write(sd, reg->reg & 0xff, reg->val & 0xff);
135
136 return 0;
137}
138#endif
139
140static void ths8200_print_timings(struct v4l2_subdev *sd,
141 struct v4l2_dv_timings *timings,
142 const char *txt, bool detailed)
143{
144 struct v4l2_bt_timings *bt = &timings->bt;
145 u32 htot, vtot;
146
147 if (timings->type != V4L2_DV_BT_656_1120)
148 return;
149
150 htot = htotal(bt);
151 vtot = vtotal(bt);
152
153 v4l2_info(sd, "%s %dx%d%s%d (%dx%d)",
154 txt, bt->width, bt->height, bt->interlaced ? "i" : "p",
155 (htot * vtot) > 0 ? ((u32)bt->pixelclock / (htot * vtot)) : 0,
156 htot, vtot);
157
158 if (detailed) {
159 v4l2_info(sd, " horizontal: fp = %d, %ssync = %d, bp = %d\n",
160 bt->hfrontporch,
161 (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
162 bt->hsync, bt->hbackporch);
163 v4l2_info(sd, " vertical: fp = %d, %ssync = %d, bp = %d\n",
164 bt->vfrontporch,
165 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
166 bt->vsync, bt->vbackporch);
167 v4l2_info(sd,
168 " pixelclock: %lld, flags: 0x%x, standards: 0x%x\n",
169 bt->pixelclock, bt->flags, bt->standards);
170 }
171}
172
173static int ths8200_log_status(struct v4l2_subdev *sd)
174{
175 struct ths8200_state *state = to_state(sd);
176 uint8_t reg_03 = ths8200_read(sd, THS8200_CHIP_CTL);
177
178 v4l2_info(sd, "----- Chip status -----\n");
179 v4l2_info(sd, "version: %u\n", state->chip_version);
180 v4l2_info(sd, "power: %s\n", (reg_03 & 0x0c) ? "off" : "on");
181 v4l2_info(sd, "reset: %s\n", (reg_03 & 0x01) ? "off" : "on");
182 v4l2_info(sd, "test pattern: %s\n",
183 (reg_03 & 0x20) ? "enabled" : "disabled");
184 v4l2_info(sd, "format: %ux%u\n",
185 ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_MSB) * 256 +
186 ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_LSB),
187 (ths8200_read(sd, THS8200_DTG2_LINE_CNT_MSB) & 0x07) * 256 +
188 ths8200_read(sd, THS8200_DTG2_LINE_CNT_LSB));
189 ths8200_print_timings(sd, &state->dv_timings,
190 "Configured format:", true);
191
192 return 0;
193}
194
195/* Power up/down ths8200 */
196static int ths8200_s_power(struct v4l2_subdev *sd, int on)
197{
198 struct ths8200_state *state = to_state(sd);
199
200 v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
201
202 state->power_on = on;
203
204 /* Power up/down - leave in reset state until input video is present */
205 ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xf2, (on ? 0x00 : 0x0c));
206
207 return 0;
208}
209
210static const struct v4l2_subdev_core_ops ths8200_core_ops = {
211 .log_status = ths8200_log_status,
212 .s_power = ths8200_s_power,
213#ifdef CONFIG_VIDEO_ADV_DEBUG
214 .g_register = ths8200_g_register,
215 .s_register = ths8200_s_register,
216#endif
217};
218
219/* -----------------------------------------------------------------------------
220 * V4L2 subdev video operations
221 */
222
223static int ths8200_s_stream(struct v4l2_subdev *sd, int enable)
224{
225 struct ths8200_state *state = to_state(sd);
226
227 if (enable && !state->power_on)
228 ths8200_s_power(sd, true);
229
230 ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xfe,
231 (enable ? 0x01 : 0x00));
232
233 v4l2_dbg(1, debug, sd, "%s: %sable\n",
234 __func__, (enable ? "en" : "dis"));
235
236 return 0;
237}
238
239static void ths8200_core_init(struct v4l2_subdev *sd)
240{
241 /* setup clocks */
242 ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0x3f, 0xc0);
243
244 /**** Data path control (DATA) ****/
245 /* Set FSADJ 700 mV,
246 * bypass 422-444 interpolation,
247 * input format 30 bit RGB444
248 */
249 ths8200_write(sd, THS8200_DATA_CNTL, 0x70);
250
251 /* DTG Mode (Video blocked during blanking
252 * VESA slave
253 */
254 ths8200_write(sd, THS8200_DTG1_MODE, 0x87);
255
256 /**** Display Timing Generator Control, Part 1 (DTG1). ****/
257
258 /* Disable embedded syncs on the output by setting
259 * the amplitude to zero for all channels.
260 */
261 ths8200_write(sd, THS8200_DTG1_Y_SYNC_MSB, 0x2a);
262 ths8200_write(sd, THS8200_DTG1_CBCR_SYNC_MSB, 0x2a);
263}
264
265static void ths8200_setup(struct v4l2_subdev *sd, struct v4l2_bt_timings *bt)
266{
267 uint8_t polarity = 0;
268 uint16_t line_start_active_video = (bt->vsync + bt->vbackporch);
269 uint16_t line_start_front_porch = (vtotal(bt) - bt->vfrontporch);
270
271 /*** System ****/
272 /* Set chip in reset while it is configured */
273 ths8200_s_stream(sd, false);
274
275 /* configure video output timings */
276 ths8200_write(sd, THS8200_DTG1_SPEC_A, bt->hsync);
277 ths8200_write(sd, THS8200_DTG1_SPEC_B, bt->hfrontporch);
278
279 /* Zero for progressive scan formats.*/
280 if (!bt->interlaced)
281 ths8200_write(sd, THS8200_DTG1_SPEC_C, 0x00);
282
283 /* Distance from leading edge of h sync to start of active video.
284 * MSB in 0x2b
285 */
286 ths8200_write(sd, THS8200_DTG1_SPEC_D_LSB,
287 (bt->hbackporch + bt->hsync) & 0xff);
288 /* Zero for SDTV-mode. MSB in 0x2b */
289 ths8200_write(sd, THS8200_DTG1_SPEC_E_LSB, 0x00);
290 /*
291 * MSB for dtg1_spec(d/e/h). See comment for
292 * corresponding LSB registers.
293 */
294 ths8200_write(sd, THS8200_DTG1_SPEC_DEH_MSB,
295 ((bt->hbackporch + bt->hsync) & 0x100) >> 1);
296
297 /* h front porch */
298 ths8200_write(sd, THS8200_DTG1_SPEC_K_LSB, (bt->hfrontporch) & 0xff);
299 ths8200_write(sd, THS8200_DTG1_SPEC_K_MSB,
300 ((bt->hfrontporch) & 0x700) >> 8);
301
302 /* Half the line length. Used to calculate SDTV line types. */
303 ths8200_write(sd, THS8200_DTG1_SPEC_G_LSB, (htotal(bt)/2) & 0xff);
304 ths8200_write(sd, THS8200_DTG1_SPEC_G_MSB,
305 ((htotal(bt)/2) >> 8) & 0x0f);
306
307 /* Total pixels per line (ex. 720p: 1650) */
308 ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_MSB, htotal(bt) >> 8);
309 ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_LSB, htotal(bt) & 0xff);
310
311 /* Frame height and field height */
312 /* Field height should be programmed higher than frame_size for
313 * progressive scan formats
314 */
315 ths8200_write(sd, THS8200_DTG1_FRAME_FIELD_SZ_MSB,
316 ((vtotal(bt) >> 4) & 0xf0) + 0x7);
317 ths8200_write(sd, THS8200_DTG1_FRAME_SZ_LSB, vtotal(bt) & 0xff);
318
319 /* Should be programmed higher than frame_size
320 * for progressive formats
321 */
322 if (!bt->interlaced)
323 ths8200_write(sd, THS8200_DTG1_FIELD_SZ_LSB, 0xff);
324
325 /**** Display Timing Generator Control, Part 2 (DTG2). ****/
326 /* Set breakpoint line numbers and types
327 * THS8200 generates line types with different properties. A line type
328 * that sets all the RGB-outputs to zero is used in the blanking areas,
329 * while a line type that enable the RGB-outputs is used in active video
330 * area. The line numbers for start of active video, start of front
331 * porch and after the last line in the frame must be set with the
332 * corresponding line types.
333 *
334 * Line types:
335 * 0x9 - Full normal sync pulse: Blocks data when dtg1_pass is off.
336 * Used in blanking area.
337 * 0x0 - Active video: Video data is always passed. Used in active
338 * video area.
339 */
340 ths8200_write_and_or(sd, THS8200_DTG2_BP1_2_MSB, 0x88,
341 ((line_start_active_video >> 4) & 0x70) +
342 ((line_start_front_porch >> 8) & 0x07));
343 ths8200_write(sd, THS8200_DTG2_BP3_4_MSB, ((vtotal(bt)) >> 4) & 0x70);
344 ths8200_write(sd, THS8200_DTG2_BP1_LSB, line_start_active_video & 0xff);
345 ths8200_write(sd, THS8200_DTG2_BP2_LSB, line_start_front_porch & 0xff);
346 ths8200_write(sd, THS8200_DTG2_BP3_LSB, (vtotal(bt)) & 0xff);
347
348 /* line types */
349 ths8200_write(sd, THS8200_DTG2_LINETYPE1, 0x90);
350 ths8200_write(sd, THS8200_DTG2_LINETYPE2, 0x90);
351
352 /* h sync width transmitted */
353 ths8200_write(sd, THS8200_DTG2_HLENGTH_LSB, bt->hsync & 0xff);
354 ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0x3f,
355 (bt->hsync >> 2) & 0xc0);
356
357 /* The pixel value h sync is asserted on */
358 ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0xe0,
359 (htotal(bt) >> 8) & 0x1f);
360 ths8200_write(sd, THS8200_DTG2_HLENGTH_HDLY_LSB, htotal(bt));
361
362 /* v sync width transmitted */
363 ths8200_write(sd, THS8200_DTG2_VLENGTH1_LSB, (bt->vsync) & 0xff);
364 ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0x3f,
365 ((bt->vsync) >> 2) & 0xc0);
366
367 /* The pixel value v sync is asserted on */
368 ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0xf8,
369 (vtotal(bt)>>8) & 0x7);
370 ths8200_write(sd, THS8200_DTG2_VDLY1_LSB, vtotal(bt));
371
372 /* For progressive video vlength2 must be set to all 0 and vdly2 must
373 * be set to all 1.
374 */
375 ths8200_write(sd, THS8200_DTG2_VLENGTH2_LSB, 0x00);
376 ths8200_write(sd, THS8200_DTG2_VLENGTH2_MSB_VDLY2_MSB, 0x07);
377 ths8200_write(sd, THS8200_DTG2_VDLY2_LSB, 0xff);
378
379 /* Internal delay factors to synchronize the sync pulses and the data */
380 /* Experimental values delays (hor 4, ver 1) */
381 ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_MSB, (htotal(bt)>>8) & 0x1f);
382 ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_LSB, (htotal(bt) - 4) & 0xff);
383 ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_MSB, 0);
384 ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_LSB, 1);
385
386 /* Polarity of received and transmitted sync signals */
387 if (bt->polarities & V4L2_DV_HSYNC_POS_POL) {
388 polarity |= 0x01; /* HS_IN */
389 polarity |= 0x08; /* HS_OUT */
390 }
391 if (bt->polarities & V4L2_DV_VSYNC_POS_POL) {
392 polarity |= 0x02; /* VS_IN */
393 polarity |= 0x10; /* VS_OUT */
394 }
395
396 /* RGB mode, no embedded timings */
397 /* Timing of video input bus is derived from HS, VS, and FID dedicated
398 * inputs
399 */
400 ths8200_write(sd, THS8200_DTG2_CNTL, 0x47 | polarity);
401
402 /* leave reset */
403 ths8200_s_stream(sd, true);
404
405 v4l2_dbg(1, debug, sd, "%s: frame %dx%d, polarity %d\n"
406 "horizontal: front porch %d, back porch %d, sync %d\n"
407 "vertical: sync %d\n", __func__, htotal(bt), vtotal(bt),
408 polarity, bt->hfrontporch, bt->hbackporch,
409 bt->hsync, bt->vsync);
410}
411
412static int ths8200_s_dv_timings(struct v4l2_subdev *sd,
413 struct v4l2_dv_timings *timings)
414{
415 struct ths8200_state *state = to_state(sd);
416 int i;
417
418 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
419
420 if (timings->type != V4L2_DV_BT_656_1120)
421 return -EINVAL;
422
423 /* TODO Support interlaced formats */
424 if (timings->bt.interlaced) {
425 v4l2_dbg(1, debug, sd, "TODO Support interlaced formats\n");
426 return -EINVAL;
427 }
428
429 for (i = 0; i < ARRAY_SIZE(ths8200_timings); i++) {
430 if (v4l_match_dv_timings(&ths8200_timings[i], timings, 10))
431 break;
432 }
433
434 if (i == ARRAY_SIZE(ths8200_timings)) {
435 v4l2_dbg(1, debug, sd, "Unsupported format\n");
436 return -EINVAL;
437 }
438
439 timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
440
441 /* save timings */
442 state->dv_timings = *timings;
443
444 ths8200_setup(sd, &timings->bt);
445
446 return 0;
447}
448
449static int ths8200_g_dv_timings(struct v4l2_subdev *sd,
450 struct v4l2_dv_timings *timings)
451{
452 struct ths8200_state *state = to_state(sd);
453
454 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
455
456 *timings = state->dv_timings;
457
458 return 0;
459}
460
461static int ths8200_enum_dv_timings(struct v4l2_subdev *sd,
462 struct v4l2_enum_dv_timings *timings)
463{
464 /* Check requested format index is within range */
465 if (timings->index >= ARRAY_SIZE(ths8200_timings))
466 return -EINVAL;
467
468 timings->timings = ths8200_timings[timings->index];
469
470 return 0;
471}
472
473static int ths8200_dv_timings_cap(struct v4l2_subdev *sd,
474 struct v4l2_dv_timings_cap *cap)
475{
476 cap->type = V4L2_DV_BT_656_1120;
477 cap->bt.max_width = 1920;
478 cap->bt.max_height = 1080;
479 cap->bt.min_pixelclock = 27000000;
480 cap->bt.max_pixelclock = 148500000;
481 cap->bt.standards = V4L2_DV_BT_STD_CEA861;
482 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE;
483
484 return 0;
485}
486
487/* Specific video subsystem operation handlers */
488static const struct v4l2_subdev_video_ops ths8200_video_ops = {
489 .s_stream = ths8200_s_stream,
490 .s_dv_timings = ths8200_s_dv_timings,
491 .g_dv_timings = ths8200_g_dv_timings,
492 .enum_dv_timings = ths8200_enum_dv_timings,
493 .dv_timings_cap = ths8200_dv_timings_cap,
494};
495
496/* V4L2 top level operation handlers */
497static const struct v4l2_subdev_ops ths8200_ops = {
498 .core = &ths8200_core_ops,
499 .video = &ths8200_video_ops,
500};
501
502static int ths8200_probe(struct i2c_client *client,
503 const struct i2c_device_id *id)
504{
505 struct ths8200_state *state;
506 struct v4l2_subdev *sd;
507
508 /* Check if the adapter supports the needed features */
509 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
510 return -EIO;
511
512 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
513 if (!state)
514 return -ENOMEM;
515
516 sd = &state->sd;
517 v4l2_i2c_subdev_init(sd, client, &ths8200_ops);
518
519 state->chip_version = ths8200_read(sd, THS8200_VERSION);
520 v4l2_dbg(1, debug, sd, "chip version 0x%x\n", state->chip_version);
521
522 ths8200_core_init(sd);
523
524 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
525 client->addr << 1, client->adapter->name);
526
527 return 0;
528}
529
530static int ths8200_remove(struct i2c_client *client)
531{
532 struct v4l2_subdev *sd = i2c_get_clientdata(client);
533
534 v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
535 client->addr << 1, client->adapter->name);
536
537 ths8200_s_power(sd, false);
538
539 v4l2_device_unregister_subdev(sd);
540
541 return 0;
542}
543
544static struct i2c_device_id ths8200_id[] = {
545 { "ths8200", 0 },
546 {},
547};
548MODULE_DEVICE_TABLE(i2c, ths8200_id);
549
550static struct i2c_driver ths8200_driver = {
551 .driver = {
552 .owner = THIS_MODULE,
553 .name = "ths8200",
554 },
555 .probe = ths8200_probe,
556 .remove = ths8200_remove,
557 .id_table = ths8200_id,
558};
559
560module_i2c_driver(ths8200_driver);