ths8200.c 15 KB

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