gs1662.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * GS1662 device registration.
  3. *
  4. * Copyright (C) 2015-2016 Nexvision
  5. * Author: Charles-Antoine Couret <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/ctype.h>
  17. #include <linux/err.h>
  18. #include <linux/device.h>
  19. #include <linux/module.h>
  20. #include <linux/videodev2.h>
  21. #include <media/v4l2-common.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-subdev.h>
  25. #include <media/v4l2-dv-timings.h>
  26. #include <linux/v4l2-dv-timings.h>
  27. #define REG_STATUS 0x04
  28. #define REG_FORCE_FMT 0x06
  29. #define REG_LINES_PER_FRAME 0x12
  30. #define REG_WORDS_PER_LINE 0x13
  31. #define REG_WORDS_PER_ACT_LINE 0x14
  32. #define REG_ACT_LINES_PER_FRAME 0x15
  33. #define MASK_H_LOCK 0x001
  34. #define MASK_V_LOCK 0x002
  35. #define MASK_STD_LOCK 0x004
  36. #define MASK_FORCE_STD 0x020
  37. #define MASK_STD_STATUS 0x3E0
  38. #define GS_WIDTH_MIN 720
  39. #define GS_WIDTH_MAX 2048
  40. #define GS_HEIGHT_MIN 487
  41. #define GS_HEIGHT_MAX 1080
  42. #define GS_PIXELCLOCK_MIN 10519200
  43. #define GS_PIXELCLOCK_MAX 74250000
  44. struct gs {
  45. struct spi_device *pdev;
  46. struct v4l2_subdev sd;
  47. struct v4l2_dv_timings current_timings;
  48. int enabled;
  49. };
  50. struct gs_reg_fmt {
  51. u16 reg_value;
  52. struct v4l2_dv_timings format;
  53. };
  54. struct gs_reg_fmt_custom {
  55. u16 reg_value;
  56. __u32 width;
  57. __u32 height;
  58. __u64 pixelclock;
  59. __u32 interlaced;
  60. };
  61. static const struct spi_device_id gs_id[] = {
  62. { "gs1662", 0 },
  63. { }
  64. };
  65. MODULE_DEVICE_TABLE(spi, gs_id);
  66. static const struct v4l2_dv_timings fmt_cap[] = {
  67. V4L2_DV_BT_SDI_720X487I60,
  68. V4L2_DV_BT_CEA_720X576P50,
  69. V4L2_DV_BT_CEA_1280X720P24,
  70. V4L2_DV_BT_CEA_1280X720P25,
  71. V4L2_DV_BT_CEA_1280X720P30,
  72. V4L2_DV_BT_CEA_1280X720P50,
  73. V4L2_DV_BT_CEA_1280X720P60,
  74. V4L2_DV_BT_CEA_1920X1080P24,
  75. V4L2_DV_BT_CEA_1920X1080P25,
  76. V4L2_DV_BT_CEA_1920X1080P30,
  77. V4L2_DV_BT_CEA_1920X1080I50,
  78. V4L2_DV_BT_CEA_1920X1080I60,
  79. };
  80. static const struct gs_reg_fmt reg_fmt[] = {
  81. { 0x00, V4L2_DV_BT_CEA_1280X720P60 },
  82. { 0x01, V4L2_DV_BT_CEA_1280X720P60 },
  83. { 0x02, V4L2_DV_BT_CEA_1280X720P30 },
  84. { 0x03, V4L2_DV_BT_CEA_1280X720P30 },
  85. { 0x04, V4L2_DV_BT_CEA_1280X720P50 },
  86. { 0x05, V4L2_DV_BT_CEA_1280X720P50 },
  87. { 0x06, V4L2_DV_BT_CEA_1280X720P25 },
  88. { 0x07, V4L2_DV_BT_CEA_1280X720P25 },
  89. { 0x08, V4L2_DV_BT_CEA_1280X720P24 },
  90. { 0x09, V4L2_DV_BT_CEA_1280X720P24 },
  91. { 0x0A, V4L2_DV_BT_CEA_1920X1080I60 },
  92. { 0x0B, V4L2_DV_BT_CEA_1920X1080P30 },
  93. /* Default value: keep this field before 0xC */
  94. { 0x14, V4L2_DV_BT_CEA_1920X1080I50 },
  95. { 0x0C, V4L2_DV_BT_CEA_1920X1080I50 },
  96. { 0x0D, V4L2_DV_BT_CEA_1920X1080P25 },
  97. { 0x0E, V4L2_DV_BT_CEA_1920X1080P25 },
  98. { 0x10, V4L2_DV_BT_CEA_1920X1080P24 },
  99. { 0x12, V4L2_DV_BT_CEA_1920X1080P24 },
  100. { 0x16, V4L2_DV_BT_SDI_720X487I60 },
  101. { 0x19, V4L2_DV_BT_SDI_720X487I60 },
  102. { 0x18, V4L2_DV_BT_CEA_720X576P50 },
  103. { 0x1A, V4L2_DV_BT_CEA_720X576P50 },
  104. /* Implement following timings before enable it.
  105. * Because of we don't have access to these theoretical timings yet.
  106. * Workaround: use functions to get and set registers for these formats.
  107. */
  108. #if 0
  109. { 0x0F, V4L2_DV_BT_XXX_1920X1080I25 }, /* SMPTE 274M */
  110. { 0x11, V4L2_DV_BT_XXX_1920X1080I24 }, /* SMPTE 274M */
  111. { 0x13, V4L2_DV_BT_XXX_1920X1080I25 }, /* SMPTE 274M */
  112. { 0x15, V4L2_DV_BT_XXX_1920X1035I60 }, /* SMPTE 260M */
  113. { 0x17, V4L2_DV_BT_SDI_720X507I60 }, /* SMPTE 125M */
  114. { 0x1B, V4L2_DV_BT_SDI_720X507I60 }, /* SMPTE 125M */
  115. { 0x1C, V4L2_DV_BT_XXX_2048X1080P25 }, /* SMPTE 428.1M */
  116. #endif
  117. };
  118. static const struct v4l2_dv_timings_cap gs_timings_cap = {
  119. .type = V4L2_DV_BT_656_1120,
  120. /* keep this initialization for compatibility with GCC < 4.4.6 */
  121. .reserved = { 0 },
  122. V4L2_INIT_BT_TIMINGS(GS_WIDTH_MIN, GS_WIDTH_MAX, GS_HEIGHT_MIN,
  123. GS_HEIGHT_MAX, GS_PIXELCLOCK_MIN,
  124. GS_PIXELCLOCK_MAX,
  125. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_SDI,
  126. V4L2_DV_BT_CAP_PROGRESSIVE
  127. | V4L2_DV_BT_CAP_INTERLACED)
  128. };
  129. static int gs_read_register(struct spi_device *spi, u16 addr, u16 *value)
  130. {
  131. int ret;
  132. u16 buf_addr = (0x8000 | (0x0FFF & addr));
  133. u16 buf_value = 0;
  134. struct spi_message msg;
  135. struct spi_transfer tx[] = {
  136. {
  137. .tx_buf = &buf_addr,
  138. .len = 2,
  139. .delay_usecs = 1,
  140. }, {
  141. .rx_buf = &buf_value,
  142. .len = 2,
  143. .delay_usecs = 1,
  144. },
  145. };
  146. spi_message_init(&msg);
  147. spi_message_add_tail(&tx[0], &msg);
  148. spi_message_add_tail(&tx[1], &msg);
  149. ret = spi_sync(spi, &msg);
  150. *value = buf_value;
  151. return ret;
  152. }
  153. static int gs_write_register(struct spi_device *spi, u16 addr, u16 value)
  154. {
  155. int ret;
  156. u16 buf_addr = addr;
  157. u16 buf_value = value;
  158. struct spi_message msg;
  159. struct spi_transfer tx[] = {
  160. {
  161. .tx_buf = &buf_addr,
  162. .len = 2,
  163. .delay_usecs = 1,
  164. }, {
  165. .tx_buf = &buf_value,
  166. .len = 2,
  167. .delay_usecs = 1,
  168. },
  169. };
  170. spi_message_init(&msg);
  171. spi_message_add_tail(&tx[0], &msg);
  172. spi_message_add_tail(&tx[1], &msg);
  173. ret = spi_sync(spi, &msg);
  174. return ret;
  175. }
  176. #ifdef CONFIG_VIDEO_ADV_DEBUG
  177. static int gs_g_register(struct v4l2_subdev *sd,
  178. struct v4l2_dbg_register *reg)
  179. {
  180. struct spi_device *spi = v4l2_get_subdevdata(sd);
  181. u16 val;
  182. int ret;
  183. ret = gs_read_register(spi, reg->reg & 0xFFFF, &val);
  184. reg->val = val;
  185. reg->size = 2;
  186. return ret;
  187. }
  188. static int gs_s_register(struct v4l2_subdev *sd,
  189. const struct v4l2_dbg_register *reg)
  190. {
  191. struct spi_device *spi = v4l2_get_subdevdata(sd);
  192. return gs_write_register(spi, reg->reg & 0xFFFF, reg->val & 0xFFFF);
  193. }
  194. #endif
  195. static int gs_status_format(u16 status, struct v4l2_dv_timings *timings)
  196. {
  197. int std = (status & MASK_STD_STATUS) >> 5;
  198. int i;
  199. for (i = 0; i < ARRAY_SIZE(reg_fmt); i++) {
  200. if (reg_fmt[i].reg_value == std) {
  201. *timings = reg_fmt[i].format;
  202. return 0;
  203. }
  204. }
  205. return -ERANGE;
  206. }
  207. static u16 get_register_timings(struct v4l2_dv_timings *timings)
  208. {
  209. int i;
  210. for (i = 0; i < ARRAY_SIZE(reg_fmt); i++) {
  211. if (v4l2_match_dv_timings(timings, &reg_fmt[i].format, 0,
  212. false))
  213. return reg_fmt[i].reg_value | MASK_FORCE_STD;
  214. }
  215. return 0x0;
  216. }
  217. static inline struct gs *to_gs(struct v4l2_subdev *sd)
  218. {
  219. return container_of(sd, struct gs, sd);
  220. }
  221. static int gs_s_dv_timings(struct v4l2_subdev *sd,
  222. struct v4l2_dv_timings *timings)
  223. {
  224. struct gs *gs = to_gs(sd);
  225. int reg_value;
  226. reg_value = get_register_timings(timings);
  227. if (reg_value == 0x0)
  228. return -EINVAL;
  229. gs->current_timings = *timings;
  230. return 0;
  231. }
  232. static int gs_g_dv_timings(struct v4l2_subdev *sd,
  233. struct v4l2_dv_timings *timings)
  234. {
  235. struct gs *gs = to_gs(sd);
  236. *timings = gs->current_timings;
  237. return 0;
  238. }
  239. static int gs_query_dv_timings(struct v4l2_subdev *sd,
  240. struct v4l2_dv_timings *timings)
  241. {
  242. struct gs *gs = to_gs(sd);
  243. struct v4l2_dv_timings fmt;
  244. u16 reg_value, i;
  245. int ret;
  246. if (gs->enabled)
  247. return -EBUSY;
  248. /*
  249. * Check if the component detect a line, a frame or something else
  250. * which looks like a video signal activity.
  251. */
  252. for (i = 0; i < 4; i++) {
  253. gs_read_register(gs->pdev, REG_LINES_PER_FRAME + i, &reg_value);
  254. if (reg_value)
  255. break;
  256. }
  257. /* If no register reports a video signal */
  258. if (i >= 4)
  259. return -ENOLINK;
  260. gs_read_register(gs->pdev, REG_STATUS, &reg_value);
  261. if (!(reg_value & MASK_H_LOCK) || !(reg_value & MASK_V_LOCK))
  262. return -ENOLCK;
  263. if (!(reg_value & MASK_STD_LOCK))
  264. return -ERANGE;
  265. ret = gs_status_format(reg_value, &fmt);
  266. if (ret < 0)
  267. return ret;
  268. *timings = fmt;
  269. return 0;
  270. }
  271. static int gs_enum_dv_timings(struct v4l2_subdev *sd,
  272. struct v4l2_enum_dv_timings *timings)
  273. {
  274. if (timings->index >= ARRAY_SIZE(fmt_cap))
  275. return -EINVAL;
  276. if (timings->pad != 0)
  277. return -EINVAL;
  278. timings->timings = fmt_cap[timings->index];
  279. return 0;
  280. }
  281. static int gs_s_stream(struct v4l2_subdev *sd, int enable)
  282. {
  283. struct gs *gs = to_gs(sd);
  284. int reg_value;
  285. if (gs->enabled == enable)
  286. return 0;
  287. gs->enabled = enable;
  288. if (enable) {
  289. /* To force the specific format */
  290. reg_value = get_register_timings(&gs->current_timings);
  291. return gs_write_register(gs->pdev, REG_FORCE_FMT, reg_value);
  292. }
  293. /* To renable auto-detection mode */
  294. return gs_write_register(gs->pdev, REG_FORCE_FMT, 0x0);
  295. }
  296. static int gs_g_input_status(struct v4l2_subdev *sd, u32 *status)
  297. {
  298. struct gs *gs = to_gs(sd);
  299. u16 reg_value, i;
  300. int ret;
  301. /*
  302. * Check if the component detect a line, a frame or something else
  303. * which looks like a video signal activity.
  304. */
  305. for (i = 0; i < 4; i++) {
  306. ret = gs_read_register(gs->pdev,
  307. REG_LINES_PER_FRAME + i, &reg_value);
  308. if (reg_value)
  309. break;
  310. if (ret) {
  311. *status = V4L2_IN_ST_NO_POWER;
  312. return ret;
  313. }
  314. }
  315. /* If no register reports a video signal */
  316. if (i >= 4)
  317. *status |= V4L2_IN_ST_NO_SIGNAL;
  318. ret = gs_read_register(gs->pdev, REG_STATUS, &reg_value);
  319. if (!(reg_value & MASK_H_LOCK))
  320. *status |= V4L2_IN_ST_NO_H_LOCK;
  321. if (!(reg_value & MASK_V_LOCK))
  322. *status |= V4L2_IN_ST_NO_V_LOCK;
  323. if (!(reg_value & MASK_STD_LOCK))
  324. *status |= V4L2_IN_ST_NO_STD_LOCK;
  325. return ret;
  326. }
  327. static int gs_dv_timings_cap(struct v4l2_subdev *sd,
  328. struct v4l2_dv_timings_cap *cap)
  329. {
  330. if (cap->pad != 0)
  331. return -EINVAL;
  332. *cap = gs_timings_cap;
  333. return 0;
  334. }
  335. /* V4L2 core operation handlers */
  336. static const struct v4l2_subdev_core_ops gs_core_ops = {
  337. #ifdef CONFIG_VIDEO_ADV_DEBUG
  338. .g_register = gs_g_register,
  339. .s_register = gs_s_register,
  340. #endif
  341. };
  342. static const struct v4l2_subdev_video_ops gs_video_ops = {
  343. .s_dv_timings = gs_s_dv_timings,
  344. .g_dv_timings = gs_g_dv_timings,
  345. .s_stream = gs_s_stream,
  346. .g_input_status = gs_g_input_status,
  347. .query_dv_timings = gs_query_dv_timings,
  348. };
  349. static const struct v4l2_subdev_pad_ops gs_pad_ops = {
  350. .enum_dv_timings = gs_enum_dv_timings,
  351. .dv_timings_cap = gs_dv_timings_cap,
  352. };
  353. /* V4L2 top level operation handlers */
  354. static const struct v4l2_subdev_ops gs_ops = {
  355. .core = &gs_core_ops,
  356. .video = &gs_video_ops,
  357. .pad = &gs_pad_ops,
  358. };
  359. static int gs_probe(struct spi_device *spi)
  360. {
  361. int ret;
  362. struct gs *gs;
  363. struct v4l2_subdev *sd;
  364. gs = devm_kzalloc(&spi->dev, sizeof(struct gs), GFP_KERNEL);
  365. if (!gs)
  366. return -ENOMEM;
  367. gs->pdev = spi;
  368. sd = &gs->sd;
  369. spi->mode = SPI_MODE_0;
  370. spi->irq = -1;
  371. spi->max_speed_hz = 10000000;
  372. spi->bits_per_word = 16;
  373. ret = spi_setup(spi);
  374. v4l2_spi_subdev_init(sd, spi, &gs_ops);
  375. gs->current_timings = reg_fmt[0].format;
  376. gs->enabled = 0;
  377. /* Set H_CONFIG to SMPTE timings */
  378. gs_write_register(spi, 0x0, 0x300);
  379. return ret;
  380. }
  381. static int gs_remove(struct spi_device *spi)
  382. {
  383. struct v4l2_subdev *sd = spi_get_drvdata(spi);
  384. v4l2_device_unregister_subdev(sd);
  385. return 0;
  386. }
  387. static struct spi_driver gs_driver = {
  388. .driver = {
  389. .name = "gs1662",
  390. .owner = THIS_MODULE,
  391. },
  392. .probe = gs_probe,
  393. .remove = gs_remove,
  394. .id_table = gs_id,
  395. };
  396. module_spi_driver(gs_driver);
  397. MODULE_LICENSE("GPL");
  398. MODULE_AUTHOR("Charles-Antoine Couret <[email protected]>");
  399. MODULE_DESCRIPTION("Gennum GS1662 HD/SD-SDI Serializer driver");