gs1662.c 11 KB

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