ths8200.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  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. #include <linux/i2c.h>
  20. #include <linux/module.h>
  21. #include <linux/v4l2-dv-timings.h>
  22. #include <media/v4l2-dv-timings.h>
  23. #include <media/v4l2-async.h>
  24. #include <media/v4l2-device.h>
  25. #include "ths8200_regs.h"
  26. static int debug;
  27. module_param(debug, int, 0644);
  28. MODULE_PARM_DESC(debug, "debug level (0-2)");
  29. MODULE_DESCRIPTION("Texas Instruments THS8200 video encoder driver");
  30. MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
  31. MODULE_AUTHOR("Martin Bugge <martin.bugge@cisco.com>");
  32. MODULE_LICENSE("GPL v2");
  33. struct ths8200_state {
  34. struct v4l2_subdev sd;
  35. uint8_t chip_version;
  36. /* Is the ths8200 powered on? */
  37. bool power_on;
  38. struct v4l2_dv_timings dv_timings;
  39. };
  40. static const struct v4l2_dv_timings_cap ths8200_timings_cap = {
  41. .type = V4L2_DV_BT_656_1120,
  42. /* Allow gcc 4.5.4 to build this */
  43. .reserved = { },
  44. {
  45. .bt = {
  46. .max_width = 1920,
  47. .max_height = 1080,
  48. .min_pixelclock = 25000000,
  49. .max_pixelclock = 148500000,
  50. .standards = V4L2_DV_BT_STD_CEA861,
  51. .capabilities = V4L2_DV_BT_CAP_PROGRESSIVE,
  52. },
  53. },
  54. };
  55. static inline struct ths8200_state *to_state(struct v4l2_subdev *sd)
  56. {
  57. return container_of(sd, struct ths8200_state, sd);
  58. }
  59. static inline unsigned hblanking(const struct v4l2_bt_timings *t)
  60. {
  61. return V4L2_DV_BT_BLANKING_WIDTH(t);
  62. }
  63. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  64. {
  65. return V4L2_DV_BT_FRAME_WIDTH(t);
  66. }
  67. static inline unsigned vblanking(const struct v4l2_bt_timings *t)
  68. {
  69. return V4L2_DV_BT_BLANKING_HEIGHT(t);
  70. }
  71. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  72. {
  73. return V4L2_DV_BT_FRAME_HEIGHT(t);
  74. }
  75. static int ths8200_read(struct v4l2_subdev *sd, u8 reg)
  76. {
  77. struct i2c_client *client = v4l2_get_subdevdata(sd);
  78. return i2c_smbus_read_byte_data(client, reg);
  79. }
  80. static int ths8200_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  81. {
  82. struct i2c_client *client = v4l2_get_subdevdata(sd);
  83. int ret;
  84. int i;
  85. for (i = 0; i < 3; i++) {
  86. ret = i2c_smbus_write_byte_data(client, reg, val);
  87. if (ret == 0)
  88. return 0;
  89. }
  90. v4l2_err(sd, "I2C Write Problem\n");
  91. return ret;
  92. }
  93. /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
  94. * and then the value-mask (to be OR-ed).
  95. */
  96. static inline void
  97. ths8200_write_and_or(struct v4l2_subdev *sd, u8 reg,
  98. uint8_t clr_mask, uint8_t val_mask)
  99. {
  100. ths8200_write(sd, reg, (ths8200_read(sd, reg) & clr_mask) | val_mask);
  101. }
  102. #ifdef CONFIG_VIDEO_ADV_DEBUG
  103. static int ths8200_g_register(struct v4l2_subdev *sd,
  104. struct v4l2_dbg_register *reg)
  105. {
  106. reg->val = ths8200_read(sd, reg->reg & 0xff);
  107. reg->size = 1;
  108. return 0;
  109. }
  110. static int ths8200_s_register(struct v4l2_subdev *sd,
  111. const struct v4l2_dbg_register *reg)
  112. {
  113. ths8200_write(sd, reg->reg & 0xff, reg->val & 0xff);
  114. return 0;
  115. }
  116. #endif
  117. static int ths8200_log_status(struct v4l2_subdev *sd)
  118. {
  119. struct ths8200_state *state = to_state(sd);
  120. uint8_t reg_03 = ths8200_read(sd, THS8200_CHIP_CTL);
  121. v4l2_info(sd, "----- Chip status -----\n");
  122. v4l2_info(sd, "version: %u\n", state->chip_version);
  123. v4l2_info(sd, "power: %s\n", (reg_03 & 0x0c) ? "off" : "on");
  124. v4l2_info(sd, "reset: %s\n", (reg_03 & 0x01) ? "off" : "on");
  125. v4l2_info(sd, "test pattern: %s\n",
  126. (reg_03 & 0x20) ? "enabled" : "disabled");
  127. v4l2_info(sd, "format: %ux%u\n",
  128. ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_MSB) * 256 +
  129. ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_LSB),
  130. (ths8200_read(sd, THS8200_DTG2_LINE_CNT_MSB) & 0x07) * 256 +
  131. ths8200_read(sd, THS8200_DTG2_LINE_CNT_LSB));
  132. v4l2_print_dv_timings(sd->name, "Configured format:",
  133. &state->dv_timings, true);
  134. return 0;
  135. }
  136. /* Power up/down ths8200 */
  137. static int ths8200_s_power(struct v4l2_subdev *sd, int on)
  138. {
  139. struct ths8200_state *state = to_state(sd);
  140. v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
  141. state->power_on = on;
  142. /* Power up/down - leave in reset state until input video is present */
  143. ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xf2, (on ? 0x00 : 0x0c));
  144. return 0;
  145. }
  146. static const struct v4l2_subdev_core_ops ths8200_core_ops = {
  147. .log_status = ths8200_log_status,
  148. .s_power = ths8200_s_power,
  149. #ifdef CONFIG_VIDEO_ADV_DEBUG
  150. .g_register = ths8200_g_register,
  151. .s_register = ths8200_s_register,
  152. #endif
  153. };
  154. /* -----------------------------------------------------------------------------
  155. * V4L2 subdev video operations
  156. */
  157. static int ths8200_s_stream(struct v4l2_subdev *sd, int enable)
  158. {
  159. struct ths8200_state *state = to_state(sd);
  160. if (enable && !state->power_on)
  161. ths8200_s_power(sd, true);
  162. ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xfe,
  163. (enable ? 0x01 : 0x00));
  164. v4l2_dbg(1, debug, sd, "%s: %sable\n",
  165. __func__, (enable ? "en" : "dis"));
  166. return 0;
  167. }
  168. static void ths8200_core_init(struct v4l2_subdev *sd)
  169. {
  170. /* setup clocks */
  171. ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0x3f, 0xc0);
  172. /**** Data path control (DATA) ****/
  173. /* Set FSADJ 700 mV,
  174. * bypass 422-444 interpolation,
  175. * input format 30 bit RGB444
  176. */
  177. ths8200_write(sd, THS8200_DATA_CNTL, 0x70);
  178. /* DTG Mode (Video blocked during blanking
  179. * VESA slave
  180. */
  181. ths8200_write(sd, THS8200_DTG1_MODE, 0x87);
  182. /**** Display Timing Generator Control, Part 1 (DTG1). ****/
  183. /* Disable embedded syncs on the output by setting
  184. * the amplitude to zero for all channels.
  185. */
  186. ths8200_write(sd, THS8200_DTG1_Y_SYNC_MSB, 0x2a);
  187. ths8200_write(sd, THS8200_DTG1_CBCR_SYNC_MSB, 0x2a);
  188. }
  189. static void ths8200_setup(struct v4l2_subdev *sd, struct v4l2_bt_timings *bt)
  190. {
  191. uint8_t polarity = 0;
  192. uint16_t line_start_active_video = (bt->vsync + bt->vbackporch);
  193. uint16_t line_start_front_porch = (vtotal(bt) - bt->vfrontporch);
  194. /*** System ****/
  195. /* Set chip in reset while it is configured */
  196. ths8200_s_stream(sd, false);
  197. /* configure video output timings */
  198. ths8200_write(sd, THS8200_DTG1_SPEC_A, bt->hsync);
  199. ths8200_write(sd, THS8200_DTG1_SPEC_B, bt->hfrontporch);
  200. /* Zero for progressive scan formats.*/
  201. if (!bt->interlaced)
  202. ths8200_write(sd, THS8200_DTG1_SPEC_C, 0x00);
  203. /* Distance from leading edge of h sync to start of active video.
  204. * MSB in 0x2b
  205. */
  206. ths8200_write(sd, THS8200_DTG1_SPEC_D_LSB,
  207. (bt->hbackporch + bt->hsync) & 0xff);
  208. /* Zero for SDTV-mode. MSB in 0x2b */
  209. ths8200_write(sd, THS8200_DTG1_SPEC_E_LSB, 0x00);
  210. /*
  211. * MSB for dtg1_spec(d/e/h). See comment for
  212. * corresponding LSB registers.
  213. */
  214. ths8200_write(sd, THS8200_DTG1_SPEC_DEH_MSB,
  215. ((bt->hbackporch + bt->hsync) & 0x100) >> 1);
  216. /* h front porch */
  217. ths8200_write(sd, THS8200_DTG1_SPEC_K_LSB, (bt->hfrontporch) & 0xff);
  218. ths8200_write(sd, THS8200_DTG1_SPEC_K_MSB,
  219. ((bt->hfrontporch) & 0x700) >> 8);
  220. /* Half the line length. Used to calculate SDTV line types. */
  221. ths8200_write(sd, THS8200_DTG1_SPEC_G_LSB, (htotal(bt)/2) & 0xff);
  222. ths8200_write(sd, THS8200_DTG1_SPEC_G_MSB,
  223. ((htotal(bt)/2) >> 8) & 0x0f);
  224. /* Total pixels per line (ex. 720p: 1650) */
  225. ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_MSB, htotal(bt) >> 8);
  226. ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_LSB, htotal(bt) & 0xff);
  227. /* Frame height and field height */
  228. /* Field height should be programmed higher than frame_size for
  229. * progressive scan formats
  230. */
  231. ths8200_write(sd, THS8200_DTG1_FRAME_FIELD_SZ_MSB,
  232. ((vtotal(bt) >> 4) & 0xf0) + 0x7);
  233. ths8200_write(sd, THS8200_DTG1_FRAME_SZ_LSB, vtotal(bt) & 0xff);
  234. /* Should be programmed higher than frame_size
  235. * for progressive formats
  236. */
  237. if (!bt->interlaced)
  238. ths8200_write(sd, THS8200_DTG1_FIELD_SZ_LSB, 0xff);
  239. /**** Display Timing Generator Control, Part 2 (DTG2). ****/
  240. /* Set breakpoint line numbers and types
  241. * THS8200 generates line types with different properties. A line type
  242. * that sets all the RGB-outputs to zero is used in the blanking areas,
  243. * while a line type that enable the RGB-outputs is used in active video
  244. * area. The line numbers for start of active video, start of front
  245. * porch and after the last line in the frame must be set with the
  246. * corresponding line types.
  247. *
  248. * Line types:
  249. * 0x9 - Full normal sync pulse: Blocks data when dtg1_pass is off.
  250. * Used in blanking area.
  251. * 0x0 - Active video: Video data is always passed. Used in active
  252. * video area.
  253. */
  254. ths8200_write_and_or(sd, THS8200_DTG2_BP1_2_MSB, 0x88,
  255. ((line_start_active_video >> 4) & 0x70) +
  256. ((line_start_front_porch >> 8) & 0x07));
  257. ths8200_write(sd, THS8200_DTG2_BP3_4_MSB, ((vtotal(bt)) >> 4) & 0x70);
  258. ths8200_write(sd, THS8200_DTG2_BP1_LSB, line_start_active_video & 0xff);
  259. ths8200_write(sd, THS8200_DTG2_BP2_LSB, line_start_front_porch & 0xff);
  260. ths8200_write(sd, THS8200_DTG2_BP3_LSB, (vtotal(bt)) & 0xff);
  261. /* line types */
  262. ths8200_write(sd, THS8200_DTG2_LINETYPE1, 0x90);
  263. ths8200_write(sd, THS8200_DTG2_LINETYPE2, 0x90);
  264. /* h sync width transmitted */
  265. ths8200_write(sd, THS8200_DTG2_HLENGTH_LSB, bt->hsync & 0xff);
  266. ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0x3f,
  267. (bt->hsync >> 2) & 0xc0);
  268. /* The pixel value h sync is asserted on */
  269. ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0xe0,
  270. (htotal(bt) >> 8) & 0x1f);
  271. ths8200_write(sd, THS8200_DTG2_HLENGTH_HDLY_LSB, htotal(bt));
  272. /* v sync width transmitted */
  273. ths8200_write(sd, THS8200_DTG2_VLENGTH1_LSB, (bt->vsync) & 0xff);
  274. ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0x3f,
  275. ((bt->vsync) >> 2) & 0xc0);
  276. /* The pixel value v sync is asserted on */
  277. ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0xf8,
  278. (vtotal(bt)>>8) & 0x7);
  279. ths8200_write(sd, THS8200_DTG2_VDLY1_LSB, vtotal(bt));
  280. /* For progressive video vlength2 must be set to all 0 and vdly2 must
  281. * be set to all 1.
  282. */
  283. ths8200_write(sd, THS8200_DTG2_VLENGTH2_LSB, 0x00);
  284. ths8200_write(sd, THS8200_DTG2_VLENGTH2_MSB_VDLY2_MSB, 0x07);
  285. ths8200_write(sd, THS8200_DTG2_VDLY2_LSB, 0xff);
  286. /* Internal delay factors to synchronize the sync pulses and the data */
  287. /* Experimental values delays (hor 4, ver 1) */
  288. ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_MSB, (htotal(bt)>>8) & 0x1f);
  289. ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_LSB, (htotal(bt) - 4) & 0xff);
  290. ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_MSB, 0);
  291. ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_LSB, 1);
  292. /* Polarity of received and transmitted sync signals */
  293. if (bt->polarities & V4L2_DV_HSYNC_POS_POL) {
  294. polarity |= 0x01; /* HS_IN */
  295. polarity |= 0x08; /* HS_OUT */
  296. }
  297. if (bt->polarities & V4L2_DV_VSYNC_POS_POL) {
  298. polarity |= 0x02; /* VS_IN */
  299. polarity |= 0x10; /* VS_OUT */
  300. }
  301. /* RGB mode, no embedded timings */
  302. /* Timing of video input bus is derived from HS, VS, and FID dedicated
  303. * inputs
  304. */
  305. ths8200_write(sd, THS8200_DTG2_CNTL, 0x47 | polarity);
  306. /* leave reset */
  307. ths8200_s_stream(sd, true);
  308. v4l2_dbg(1, debug, sd, "%s: frame %dx%d, polarity %d\n"
  309. "horizontal: front porch %d, back porch %d, sync %d\n"
  310. "vertical: sync %d\n", __func__, htotal(bt), vtotal(bt),
  311. polarity, bt->hfrontporch, bt->hbackporch,
  312. bt->hsync, bt->vsync);
  313. }
  314. static int ths8200_s_dv_timings(struct v4l2_subdev *sd,
  315. struct v4l2_dv_timings *timings)
  316. {
  317. struct ths8200_state *state = to_state(sd);
  318. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  319. if (!v4l2_valid_dv_timings(timings, &ths8200_timings_cap,
  320. NULL, NULL))
  321. return -EINVAL;
  322. if (!v4l2_find_dv_timings_cap(timings, &ths8200_timings_cap, 10,
  323. NULL, NULL)) {
  324. v4l2_dbg(1, debug, sd, "Unsupported format\n");
  325. return -EINVAL;
  326. }
  327. timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
  328. /* save timings */
  329. state->dv_timings = *timings;
  330. ths8200_setup(sd, &timings->bt);
  331. return 0;
  332. }
  333. static int ths8200_g_dv_timings(struct v4l2_subdev *sd,
  334. struct v4l2_dv_timings *timings)
  335. {
  336. struct ths8200_state *state = to_state(sd);
  337. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  338. *timings = state->dv_timings;
  339. return 0;
  340. }
  341. static int ths8200_enum_dv_timings(struct v4l2_subdev *sd,
  342. struct v4l2_enum_dv_timings *timings)
  343. {
  344. return v4l2_enum_dv_timings_cap(timings, &ths8200_timings_cap,
  345. NULL, NULL);
  346. }
  347. static int ths8200_dv_timings_cap(struct v4l2_subdev *sd,
  348. struct v4l2_dv_timings_cap *cap)
  349. {
  350. *cap = ths8200_timings_cap;
  351. return 0;
  352. }
  353. /* Specific video subsystem operation handlers */
  354. static const struct v4l2_subdev_video_ops ths8200_video_ops = {
  355. .s_stream = ths8200_s_stream,
  356. .s_dv_timings = ths8200_s_dv_timings,
  357. .g_dv_timings = ths8200_g_dv_timings,
  358. .enum_dv_timings = ths8200_enum_dv_timings,
  359. .dv_timings_cap = ths8200_dv_timings_cap,
  360. };
  361. /* V4L2 top level operation handlers */
  362. static const struct v4l2_subdev_ops ths8200_ops = {
  363. .core = &ths8200_core_ops,
  364. .video = &ths8200_video_ops,
  365. };
  366. static int ths8200_probe(struct i2c_client *client,
  367. const struct i2c_device_id *id)
  368. {
  369. struct ths8200_state *state;
  370. struct v4l2_subdev *sd;
  371. int error;
  372. /* Check if the adapter supports the needed features */
  373. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  374. return -EIO;
  375. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  376. if (!state)
  377. return -ENOMEM;
  378. sd = &state->sd;
  379. v4l2_i2c_subdev_init(sd, client, &ths8200_ops);
  380. state->chip_version = ths8200_read(sd, THS8200_VERSION);
  381. v4l2_dbg(1, debug, sd, "chip version 0x%x\n", state->chip_version);
  382. ths8200_core_init(sd);
  383. error = v4l2_async_register_subdev(&state->sd);
  384. if (error)
  385. return error;
  386. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  387. client->addr << 1, client->adapter->name);
  388. return 0;
  389. }
  390. static int ths8200_remove(struct i2c_client *client)
  391. {
  392. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  393. struct ths8200_state *decoder = to_state(sd);
  394. v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
  395. client->addr << 1, client->adapter->name);
  396. ths8200_s_power(sd, false);
  397. v4l2_async_unregister_subdev(&decoder->sd);
  398. v4l2_device_unregister_subdev(sd);
  399. return 0;
  400. }
  401. static struct i2c_device_id ths8200_id[] = {
  402. { "ths8200", 0 },
  403. {},
  404. };
  405. MODULE_DEVICE_TABLE(i2c, ths8200_id);
  406. #if IS_ENABLED(CONFIG_OF)
  407. static const struct of_device_id ths8200_of_match[] = {
  408. { .compatible = "ti,ths8200", },
  409. { /* sentinel */ },
  410. };
  411. MODULE_DEVICE_TABLE(of, ths8200_of_match);
  412. #endif
  413. static struct i2c_driver ths8200_driver = {
  414. .driver = {
  415. .owner = THIS_MODULE,
  416. .name = "ths8200",
  417. .of_match_table = of_match_ptr(ths8200_of_match),
  418. },
  419. .probe = ths8200_probe,
  420. .remove = ths8200_remove,
  421. .id_table = ths8200_id,
  422. };
  423. module_i2c_driver(ths8200_driver);