2 * ths8200 - Texas Instruments THS8200 video encoder driver
4 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
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.
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.
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.
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/v4l2-dv-timings.h>
24 #include <media/v4l2-dv-timings.h>
25 #include <media/v4l2-async.h>
26 #include <media/v4l2-device.h>
28 #include "ths8200_regs.h"
31 module_param(debug, int, 0644);
32 MODULE_PARM_DESC(debug, "debug level (0-2)");
34 MODULE_DESCRIPTION("Texas Instruments THS8200 video encoder driver");
35 MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
36 MODULE_AUTHOR("Martin Bugge <martin.bugge@cisco.com>");
37 MODULE_LICENSE("GPL v2");
39 struct ths8200_state {
40 struct v4l2_subdev sd;
42 /* Is the ths8200 powered on? */
44 struct v4l2_dv_timings dv_timings;
47 static const struct v4l2_dv_timings ths8200_timings[] = {
48 V4L2_DV_BT_CEA_720X480P59_94,
49 V4L2_DV_BT_CEA_1280X720P24,
50 V4L2_DV_BT_CEA_1280X720P25,
51 V4L2_DV_BT_CEA_1280X720P30,
52 V4L2_DV_BT_CEA_1280X720P50,
53 V4L2_DV_BT_CEA_1280X720P60,
54 V4L2_DV_BT_CEA_1920X1080P24,
55 V4L2_DV_BT_CEA_1920X1080P25,
56 V4L2_DV_BT_CEA_1920X1080P30,
57 V4L2_DV_BT_CEA_1920X1080P50,
58 V4L2_DV_BT_CEA_1920X1080P60,
61 static inline struct ths8200_state *to_state(struct v4l2_subdev *sd)
63 return container_of(sd, struct ths8200_state, sd);
66 static inline unsigned hblanking(const struct v4l2_bt_timings *t)
68 return V4L2_DV_BT_BLANKING_WIDTH(t);
71 static inline unsigned htotal(const struct v4l2_bt_timings *t)
73 return V4L2_DV_BT_FRAME_WIDTH(t);
76 static inline unsigned vblanking(const struct v4l2_bt_timings *t)
78 return V4L2_DV_BT_BLANKING_HEIGHT(t);
81 static inline unsigned vtotal(const struct v4l2_bt_timings *t)
83 return V4L2_DV_BT_FRAME_HEIGHT(t);
86 static int ths8200_read(struct v4l2_subdev *sd, u8 reg)
88 struct i2c_client *client = v4l2_get_subdevdata(sd);
90 return i2c_smbus_read_byte_data(client, reg);
93 static int ths8200_write(struct v4l2_subdev *sd, u8 reg, u8 val)
95 struct i2c_client *client = v4l2_get_subdevdata(sd);
99 for (i = 0; i < 3; i++) {
100 ret = i2c_smbus_write_byte_data(client, reg, val);
104 v4l2_err(sd, "I2C Write Problem\n");
108 /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
109 * and then the value-mask (to be OR-ed).
112 ths8200_write_and_or(struct v4l2_subdev *sd, u8 reg,
113 uint8_t clr_mask, uint8_t val_mask)
115 ths8200_write(sd, reg, (ths8200_read(sd, reg) & clr_mask) | val_mask);
118 #ifdef CONFIG_VIDEO_ADV_DEBUG
120 static int ths8200_g_register(struct v4l2_subdev *sd,
121 struct v4l2_dbg_register *reg)
123 reg->val = ths8200_read(sd, reg->reg & 0xff);
129 static int ths8200_s_register(struct v4l2_subdev *sd,
130 const struct v4l2_dbg_register *reg)
132 ths8200_write(sd, reg->reg & 0xff, reg->val & 0xff);
138 static void ths8200_print_timings(struct v4l2_subdev *sd,
139 struct v4l2_dv_timings *timings,
140 const char *txt, bool detailed)
142 struct v4l2_bt_timings *bt = &timings->bt;
145 if (timings->type != V4L2_DV_BT_656_1120)
151 v4l2_info(sd, "%s %dx%d%s%d (%dx%d)",
152 txt, bt->width, bt->height, bt->interlaced ? "i" : "p",
153 (htot * vtot) > 0 ? ((u32)bt->pixelclock / (htot * vtot)) : 0,
157 v4l2_info(sd, " horizontal: fp = %d, %ssync = %d, bp = %d\n",
159 (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
160 bt->hsync, bt->hbackporch);
161 v4l2_info(sd, " vertical: fp = %d, %ssync = %d, bp = %d\n",
163 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
164 bt->vsync, bt->vbackporch);
166 " pixelclock: %lld, flags: 0x%x, standards: 0x%x\n",
167 bt->pixelclock, bt->flags, bt->standards);
171 static int ths8200_log_status(struct v4l2_subdev *sd)
173 struct ths8200_state *state = to_state(sd);
174 uint8_t reg_03 = ths8200_read(sd, THS8200_CHIP_CTL);
176 v4l2_info(sd, "----- Chip status -----\n");
177 v4l2_info(sd, "version: %u\n", state->chip_version);
178 v4l2_info(sd, "power: %s\n", (reg_03 & 0x0c) ? "off" : "on");
179 v4l2_info(sd, "reset: %s\n", (reg_03 & 0x01) ? "off" : "on");
180 v4l2_info(sd, "test pattern: %s\n",
181 (reg_03 & 0x20) ? "enabled" : "disabled");
182 v4l2_info(sd, "format: %ux%u\n",
183 ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_MSB) * 256 +
184 ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_LSB),
185 (ths8200_read(sd, THS8200_DTG2_LINE_CNT_MSB) & 0x07) * 256 +
186 ths8200_read(sd, THS8200_DTG2_LINE_CNT_LSB));
187 ths8200_print_timings(sd, &state->dv_timings,
188 "Configured format:", true);
193 /* Power up/down ths8200 */
194 static int ths8200_s_power(struct v4l2_subdev *sd, int on)
196 struct ths8200_state *state = to_state(sd);
198 v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
200 state->power_on = on;
202 /* Power up/down - leave in reset state until input video is present */
203 ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xf2, (on ? 0x00 : 0x0c));
208 static const struct v4l2_subdev_core_ops ths8200_core_ops = {
209 .log_status = ths8200_log_status,
210 .s_power = ths8200_s_power,
211 #ifdef CONFIG_VIDEO_ADV_DEBUG
212 .g_register = ths8200_g_register,
213 .s_register = ths8200_s_register,
217 /* -----------------------------------------------------------------------------
218 * V4L2 subdev video operations
221 static int ths8200_s_stream(struct v4l2_subdev *sd, int enable)
223 struct ths8200_state *state = to_state(sd);
225 if (enable && !state->power_on)
226 ths8200_s_power(sd, true);
228 ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xfe,
229 (enable ? 0x01 : 0x00));
231 v4l2_dbg(1, debug, sd, "%s: %sable\n",
232 __func__, (enable ? "en" : "dis"));
237 static void ths8200_core_init(struct v4l2_subdev *sd)
240 ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0x3f, 0xc0);
242 /**** Data path control (DATA) ****/
244 * bypass 422-444 interpolation,
245 * input format 30 bit RGB444
247 ths8200_write(sd, THS8200_DATA_CNTL, 0x70);
249 /* DTG Mode (Video blocked during blanking
252 ths8200_write(sd, THS8200_DTG1_MODE, 0x87);
254 /**** Display Timing Generator Control, Part 1 (DTG1). ****/
256 /* Disable embedded syncs on the output by setting
257 * the amplitude to zero for all channels.
259 ths8200_write(sd, THS8200_DTG1_Y_SYNC_MSB, 0x2a);
260 ths8200_write(sd, THS8200_DTG1_CBCR_SYNC_MSB, 0x2a);
263 static void ths8200_setup(struct v4l2_subdev *sd, struct v4l2_bt_timings *bt)
265 uint8_t polarity = 0;
266 uint16_t line_start_active_video = (bt->vsync + bt->vbackporch);
267 uint16_t line_start_front_porch = (vtotal(bt) - bt->vfrontporch);
270 /* Set chip in reset while it is configured */
271 ths8200_s_stream(sd, false);
273 /* configure video output timings */
274 ths8200_write(sd, THS8200_DTG1_SPEC_A, bt->hsync);
275 ths8200_write(sd, THS8200_DTG1_SPEC_B, bt->hfrontporch);
277 /* Zero for progressive scan formats.*/
279 ths8200_write(sd, THS8200_DTG1_SPEC_C, 0x00);
281 /* Distance from leading edge of h sync to start of active video.
284 ths8200_write(sd, THS8200_DTG1_SPEC_D_LSB,
285 (bt->hbackporch + bt->hsync) & 0xff);
286 /* Zero for SDTV-mode. MSB in 0x2b */
287 ths8200_write(sd, THS8200_DTG1_SPEC_E_LSB, 0x00);
289 * MSB for dtg1_spec(d/e/h). See comment for
290 * corresponding LSB registers.
292 ths8200_write(sd, THS8200_DTG1_SPEC_DEH_MSB,
293 ((bt->hbackporch + bt->hsync) & 0x100) >> 1);
296 ths8200_write(sd, THS8200_DTG1_SPEC_K_LSB, (bt->hfrontporch) & 0xff);
297 ths8200_write(sd, THS8200_DTG1_SPEC_K_MSB,
298 ((bt->hfrontporch) & 0x700) >> 8);
300 /* Half the line length. Used to calculate SDTV line types. */
301 ths8200_write(sd, THS8200_DTG1_SPEC_G_LSB, (htotal(bt)/2) & 0xff);
302 ths8200_write(sd, THS8200_DTG1_SPEC_G_MSB,
303 ((htotal(bt)/2) >> 8) & 0x0f);
305 /* Total pixels per line (ex. 720p: 1650) */
306 ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_MSB, htotal(bt) >> 8);
307 ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_LSB, htotal(bt) & 0xff);
309 /* Frame height and field height */
310 /* Field height should be programmed higher than frame_size for
311 * progressive scan formats
313 ths8200_write(sd, THS8200_DTG1_FRAME_FIELD_SZ_MSB,
314 ((vtotal(bt) >> 4) & 0xf0) + 0x7);
315 ths8200_write(sd, THS8200_DTG1_FRAME_SZ_LSB, vtotal(bt) & 0xff);
317 /* Should be programmed higher than frame_size
318 * for progressive formats
321 ths8200_write(sd, THS8200_DTG1_FIELD_SZ_LSB, 0xff);
323 /**** Display Timing Generator Control, Part 2 (DTG2). ****/
324 /* Set breakpoint line numbers and types
325 * THS8200 generates line types with different properties. A line type
326 * that sets all the RGB-outputs to zero is used in the blanking areas,
327 * while a line type that enable the RGB-outputs is used in active video
328 * area. The line numbers for start of active video, start of front
329 * porch and after the last line in the frame must be set with the
330 * corresponding line types.
333 * 0x9 - Full normal sync pulse: Blocks data when dtg1_pass is off.
334 * Used in blanking area.
335 * 0x0 - Active video: Video data is always passed. Used in active
338 ths8200_write_and_or(sd, THS8200_DTG2_BP1_2_MSB, 0x88,
339 ((line_start_active_video >> 4) & 0x70) +
340 ((line_start_front_porch >> 8) & 0x07));
341 ths8200_write(sd, THS8200_DTG2_BP3_4_MSB, ((vtotal(bt)) >> 4) & 0x70);
342 ths8200_write(sd, THS8200_DTG2_BP1_LSB, line_start_active_video & 0xff);
343 ths8200_write(sd, THS8200_DTG2_BP2_LSB, line_start_front_porch & 0xff);
344 ths8200_write(sd, THS8200_DTG2_BP3_LSB, (vtotal(bt)) & 0xff);
347 ths8200_write(sd, THS8200_DTG2_LINETYPE1, 0x90);
348 ths8200_write(sd, THS8200_DTG2_LINETYPE2, 0x90);
350 /* h sync width transmitted */
351 ths8200_write(sd, THS8200_DTG2_HLENGTH_LSB, bt->hsync & 0xff);
352 ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0x3f,
353 (bt->hsync >> 2) & 0xc0);
355 /* The pixel value h sync is asserted on */
356 ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0xe0,
357 (htotal(bt) >> 8) & 0x1f);
358 ths8200_write(sd, THS8200_DTG2_HLENGTH_HDLY_LSB, htotal(bt));
360 /* v sync width transmitted */
361 ths8200_write(sd, THS8200_DTG2_VLENGTH1_LSB, (bt->vsync) & 0xff);
362 ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0x3f,
363 ((bt->vsync) >> 2) & 0xc0);
365 /* The pixel value v sync is asserted on */
366 ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0xf8,
367 (vtotal(bt)>>8) & 0x7);
368 ths8200_write(sd, THS8200_DTG2_VDLY1_LSB, vtotal(bt));
370 /* For progressive video vlength2 must be set to all 0 and vdly2 must
373 ths8200_write(sd, THS8200_DTG2_VLENGTH2_LSB, 0x00);
374 ths8200_write(sd, THS8200_DTG2_VLENGTH2_MSB_VDLY2_MSB, 0x07);
375 ths8200_write(sd, THS8200_DTG2_VDLY2_LSB, 0xff);
377 /* Internal delay factors to synchronize the sync pulses and the data */
378 /* Experimental values delays (hor 4, ver 1) */
379 ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_MSB, (htotal(bt)>>8) & 0x1f);
380 ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_LSB, (htotal(bt) - 4) & 0xff);
381 ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_MSB, 0);
382 ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_LSB, 1);
384 /* Polarity of received and transmitted sync signals */
385 if (bt->polarities & V4L2_DV_HSYNC_POS_POL) {
386 polarity |= 0x01; /* HS_IN */
387 polarity |= 0x08; /* HS_OUT */
389 if (bt->polarities & V4L2_DV_VSYNC_POS_POL) {
390 polarity |= 0x02; /* VS_IN */
391 polarity |= 0x10; /* VS_OUT */
394 /* RGB mode, no embedded timings */
395 /* Timing of video input bus is derived from HS, VS, and FID dedicated
398 ths8200_write(sd, THS8200_DTG2_CNTL, 0x47 | polarity);
401 ths8200_s_stream(sd, true);
403 v4l2_dbg(1, debug, sd, "%s: frame %dx%d, polarity %d\n"
404 "horizontal: front porch %d, back porch %d, sync %d\n"
405 "vertical: sync %d\n", __func__, htotal(bt), vtotal(bt),
406 polarity, bt->hfrontporch, bt->hbackporch,
407 bt->hsync, bt->vsync);
410 static int ths8200_s_dv_timings(struct v4l2_subdev *sd,
411 struct v4l2_dv_timings *timings)
413 struct ths8200_state *state = to_state(sd);
416 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
418 if (timings->type != V4L2_DV_BT_656_1120)
421 /* TODO Support interlaced formats */
422 if (timings->bt.interlaced) {
423 v4l2_dbg(1, debug, sd, "TODO Support interlaced formats\n");
427 for (i = 0; i < ARRAY_SIZE(ths8200_timings); i++) {
428 if (v4l_match_dv_timings(&ths8200_timings[i], timings, 10))
432 if (i == ARRAY_SIZE(ths8200_timings)) {
433 v4l2_dbg(1, debug, sd, "Unsupported format\n");
437 timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
440 state->dv_timings = *timings;
442 ths8200_setup(sd, &timings->bt);
447 static int ths8200_g_dv_timings(struct v4l2_subdev *sd,
448 struct v4l2_dv_timings *timings)
450 struct ths8200_state *state = to_state(sd);
452 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
454 *timings = state->dv_timings;
459 static int ths8200_enum_dv_timings(struct v4l2_subdev *sd,
460 struct v4l2_enum_dv_timings *timings)
462 /* Check requested format index is within range */
463 if (timings->index >= ARRAY_SIZE(ths8200_timings))
466 timings->timings = ths8200_timings[timings->index];
471 static int ths8200_dv_timings_cap(struct v4l2_subdev *sd,
472 struct v4l2_dv_timings_cap *cap)
474 cap->type = V4L2_DV_BT_656_1120;
475 cap->bt.max_width = 1920;
476 cap->bt.max_height = 1080;
477 cap->bt.min_pixelclock = 27000000;
478 cap->bt.max_pixelclock = 148500000;
479 cap->bt.standards = V4L2_DV_BT_STD_CEA861;
480 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE;
485 /* Specific video subsystem operation handlers */
486 static const struct v4l2_subdev_video_ops ths8200_video_ops = {
487 .s_stream = ths8200_s_stream,
488 .s_dv_timings = ths8200_s_dv_timings,
489 .g_dv_timings = ths8200_g_dv_timings,
490 .enum_dv_timings = ths8200_enum_dv_timings,
491 .dv_timings_cap = ths8200_dv_timings_cap,
494 /* V4L2 top level operation handlers */
495 static const struct v4l2_subdev_ops ths8200_ops = {
496 .core = &ths8200_core_ops,
497 .video = &ths8200_video_ops,
500 static int ths8200_probe(struct i2c_client *client,
501 const struct i2c_device_id *id)
503 struct ths8200_state *state;
504 struct v4l2_subdev *sd;
507 /* Check if the adapter supports the needed features */
508 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
511 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
516 v4l2_i2c_subdev_init(sd, client, &ths8200_ops);
518 state->chip_version = ths8200_read(sd, THS8200_VERSION);
519 v4l2_dbg(1, debug, sd, "chip version 0x%x\n", state->chip_version);
521 ths8200_core_init(sd);
523 error = v4l2_async_register_subdev(&state->sd);
527 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
528 client->addr << 1, client->adapter->name);
533 static int ths8200_remove(struct i2c_client *client)
535 struct v4l2_subdev *sd = i2c_get_clientdata(client);
536 struct ths8200_state *decoder = to_state(sd);
538 v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
539 client->addr << 1, client->adapter->name);
541 ths8200_s_power(sd, false);
542 v4l2_async_unregister_subdev(&decoder->sd);
543 v4l2_device_unregister_subdev(sd);
548 static struct i2c_device_id ths8200_id[] = {
552 MODULE_DEVICE_TABLE(i2c, ths8200_id);
554 #if IS_ENABLED(CONFIG_OF)
555 static const struct of_device_id ths8200_of_match[] = {
556 { .compatible = "ti,ths8200", },
559 MODULE_DEVICE_TABLE(of, ths8200_of_match);
562 static struct i2c_driver ths8200_driver = {
564 .owner = THIS_MODULE,
566 .of_match_table = of_match_ptr(ths8200_of_match),
568 .probe = ths8200_probe,
569 .remove = ths8200_remove,
570 .id_table = ths8200_id,
573 module_i2c_driver(ths8200_driver);