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