1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OKI Semiconductor ML86V7667 video decoder driver
5 * Author: Vladimir Barinov <source@cogentembedded.com>
6 * Copyright (C) 2013 Cogent Embedded, Inc.
7 * Copyright (C) 2013 Renesas Solutions Corp.
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/videodev2.h>
15 #include <media/v4l2-subdev.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/v4l2-ctrls.h>
20 #define DRV_NAME "ml86v7667"
23 #define MRA_REG 0x00 /* Mode Register A */
24 #define MRC_REG 0x02 /* Mode Register C */
25 #define LUMC_REG 0x0C /* Luminance Control */
26 #define CLC_REG 0x10 /* Contrast level control */
27 #define SSEPL_REG 0x11 /* Sync separation level */
28 #define CHRCA_REG 0x12 /* Chrominance Control A */
29 #define ACCC_REG 0x14 /* ACC Loop filter & Chrominance control */
30 #define ACCRC_REG 0x15 /* ACC Reference level control */
31 #define HUE_REG 0x16 /* Hue control */
32 #define ADC2_REG 0x1F /* ADC Register 2 */
33 #define PLLR1_REG 0x20 /* PLL Register 1 */
34 #define STATUS_REG 0x2C /* STATUS Register */
36 /* Mode Register A register bits */
37 #define MRA_OUTPUT_MODE_MASK (3 << 6)
38 #define MRA_ITUR_BT601 (1 << 6)
39 #define MRA_ITUR_BT656 (0 << 6)
40 #define MRA_INPUT_MODE_MASK (7 << 3)
41 #define MRA_PAL_BT601 (4 << 3)
42 #define MRA_NTSC_BT601 (0 << 3)
43 #define MRA_REGISTER_MODE (1 << 0)
45 /* Mode Register C register bits */
46 #define MRC_AUTOSELECT (1 << 7)
48 /* Luminance Control register bits */
49 #define LUMC_ONOFF_SHIFT 7
50 #define LUMC_ONOFF_MASK (1 << 7)
52 /* Contrast level control register bits */
53 #define CLC_CONTRAST_ONOFF (1 << 7)
54 #define CLC_CONTRAST_MASK 0x0F
56 /* Sync separation level register bits */
57 #define SSEPL_LUMINANCE_ONOFF (1 << 7)
58 #define SSEPL_LUMINANCE_MASK 0x7F
60 /* Chrominance Control A register bits */
61 #define CHRCA_MODE_SHIFT 6
62 #define CHRCA_MODE_MASK (1 << 6)
64 /* ACC Loop filter & Chrominance control register bits */
65 #define ACCC_CHROMA_CR_SHIFT 3
66 #define ACCC_CHROMA_CR_MASK (7 << 3)
67 #define ACCC_CHROMA_CB_SHIFT 0
68 #define ACCC_CHROMA_CB_MASK (7 << 0)
70 /* ACC Reference level control register bits */
71 #define ACCRC_CHROMA_MASK 0xfc
72 #define ACCRC_CHROMA_SHIFT 2
74 /* ADC Register 2 register bits */
75 #define ADC2_CLAMP_VOLTAGE_MASK (7 << 1)
76 #define ADC2_CLAMP_VOLTAGE(n) ((n & 7) << 1)
78 /* PLL Register 1 register bits */
79 #define PLLR1_FIXED_CLOCK (1 << 7)
81 /* STATUS Register register bits */
82 #define STATUS_HLOCK_DETECT (1 << 3)
83 #define STATUS_NTSCPAL (1 << 2)
85 struct ml86v7667_priv
{
86 struct v4l2_subdev sd
;
87 struct v4l2_ctrl_handler hdl
;
91 static inline struct ml86v7667_priv
*to_ml86v7667(struct v4l2_subdev
*subdev
)
93 return container_of(subdev
, struct ml86v7667_priv
, sd
);
96 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
98 return &container_of(ctrl
->handler
, struct ml86v7667_priv
, hdl
)->sd
;
101 static int ml86v7667_mask_set(struct i2c_client
*client
, const u8 reg
,
102 const u8 mask
, const u8 data
)
104 int val
= i2c_smbus_read_byte_data(client
, reg
);
108 val
= (val
& ~mask
) | (data
& mask
);
109 return i2c_smbus_write_byte_data(client
, reg
, val
);
112 static int ml86v7667_s_ctrl(struct v4l2_ctrl
*ctrl
)
114 struct v4l2_subdev
*sd
= to_sd(ctrl
);
115 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
119 case V4L2_CID_BRIGHTNESS
:
120 ret
= ml86v7667_mask_set(client
, SSEPL_REG
,
121 SSEPL_LUMINANCE_MASK
, ctrl
->val
);
123 case V4L2_CID_CONTRAST
:
124 ret
= ml86v7667_mask_set(client
, CLC_REG
,
125 CLC_CONTRAST_MASK
, ctrl
->val
);
127 case V4L2_CID_CHROMA_GAIN
:
128 ret
= ml86v7667_mask_set(client
, ACCRC_REG
, ACCRC_CHROMA_MASK
,
129 ctrl
->val
<< ACCRC_CHROMA_SHIFT
);
132 ret
= ml86v7667_mask_set(client
, HUE_REG
, ~0, ctrl
->val
);
134 case V4L2_CID_RED_BALANCE
:
135 ret
= ml86v7667_mask_set(client
, ACCC_REG
,
137 ctrl
->val
<< ACCC_CHROMA_CR_SHIFT
);
139 case V4L2_CID_BLUE_BALANCE
:
140 ret
= ml86v7667_mask_set(client
, ACCC_REG
,
142 ctrl
->val
<< ACCC_CHROMA_CB_SHIFT
);
144 case V4L2_CID_SHARPNESS
:
145 ret
= ml86v7667_mask_set(client
, LUMC_REG
,
147 ctrl
->val
<< LUMC_ONOFF_SHIFT
);
149 case V4L2_CID_COLOR_KILLER
:
150 ret
= ml86v7667_mask_set(client
, CHRCA_REG
,
152 ctrl
->val
<< CHRCA_MODE_SHIFT
);
159 static int ml86v7667_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
161 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
164 status
= i2c_smbus_read_byte_data(client
, STATUS_REG
);
168 if (status
& STATUS_HLOCK_DETECT
)
169 *std
&= status
& STATUS_NTSCPAL
? V4L2_STD_625_50
: V4L2_STD_525_60
;
171 *std
= V4L2_STD_UNKNOWN
;
176 static int ml86v7667_g_input_status(struct v4l2_subdev
*sd
, u32
*status
)
178 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
181 status_reg
= i2c_smbus_read_byte_data(client
, STATUS_REG
);
185 *status
= status_reg
& STATUS_HLOCK_DETECT
? 0 : V4L2_IN_ST_NO_SIGNAL
;
190 static int ml86v7667_enum_mbus_code(struct v4l2_subdev
*sd
,
191 struct v4l2_subdev_pad_config
*cfg
,
192 struct v4l2_subdev_mbus_code_enum
*code
)
194 if (code
->pad
|| code
->index
> 0)
197 code
->code
= MEDIA_BUS_FMT_YUYV8_2X8
;
202 static int ml86v7667_fill_fmt(struct v4l2_subdev
*sd
,
203 struct v4l2_subdev_pad_config
*cfg
,
204 struct v4l2_subdev_format
*format
)
206 struct ml86v7667_priv
*priv
= to_ml86v7667(sd
);
207 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
212 fmt
->code
= MEDIA_BUS_FMT_YUYV8_2X8
;
213 fmt
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
214 /* The top field is always transferred first by the chip */
215 fmt
->field
= V4L2_FIELD_INTERLACED_TB
;
217 fmt
->height
= priv
->std
& V4L2_STD_525_60
? 480 : 576;
222 static int ml86v7667_get_mbus_config(struct v4l2_subdev
*sd
,
224 struct v4l2_mbus_config
*cfg
)
226 cfg
->flags
= V4L2_MBUS_MASTER
| V4L2_MBUS_PCLK_SAMPLE_RISING
|
227 V4L2_MBUS_DATA_ACTIVE_HIGH
;
228 cfg
->type
= V4L2_MBUS_BT656
;
233 static int ml86v7667_g_std(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
235 struct ml86v7667_priv
*priv
= to_ml86v7667(sd
);
242 static int ml86v7667_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
244 struct ml86v7667_priv
*priv
= to_ml86v7667(sd
);
245 struct i2c_client
*client
= v4l2_get_subdevdata(&priv
->sd
);
249 /* PAL/NTSC ITU-R BT.601 input mode */
250 mode
= std
& V4L2_STD_525_60
? MRA_NTSC_BT601
: MRA_PAL_BT601
;
251 ret
= ml86v7667_mask_set(client
, MRA_REG
, MRA_INPUT_MODE_MASK
, mode
);
260 #ifdef CONFIG_VIDEO_ADV_DEBUG
261 static int ml86v7667_g_register(struct v4l2_subdev
*sd
,
262 struct v4l2_dbg_register
*reg
)
264 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
267 ret
= i2c_smbus_read_byte_data(client
, (u8
)reg
->reg
);
272 reg
->size
= sizeof(u8
);
277 static int ml86v7667_s_register(struct v4l2_subdev
*sd
,
278 const struct v4l2_dbg_register
*reg
)
280 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
282 return i2c_smbus_write_byte_data(client
, (u8
)reg
->reg
, (u8
)reg
->val
);
286 static const struct v4l2_ctrl_ops ml86v7667_ctrl_ops
= {
287 .s_ctrl
= ml86v7667_s_ctrl
,
290 static const struct v4l2_subdev_video_ops ml86v7667_subdev_video_ops
= {
291 .g_std
= ml86v7667_g_std
,
292 .s_std
= ml86v7667_s_std
,
293 .querystd
= ml86v7667_querystd
,
294 .g_input_status
= ml86v7667_g_input_status
,
297 static const struct v4l2_subdev_pad_ops ml86v7667_subdev_pad_ops
= {
298 .enum_mbus_code
= ml86v7667_enum_mbus_code
,
299 .get_fmt
= ml86v7667_fill_fmt
,
300 .set_fmt
= ml86v7667_fill_fmt
,
301 .get_mbus_config
= ml86v7667_get_mbus_config
,
304 static const struct v4l2_subdev_core_ops ml86v7667_subdev_core_ops
= {
305 #ifdef CONFIG_VIDEO_ADV_DEBUG
306 .g_register
= ml86v7667_g_register
,
307 .s_register
= ml86v7667_s_register
,
311 static const struct v4l2_subdev_ops ml86v7667_subdev_ops
= {
312 .core
= &ml86v7667_subdev_core_ops
,
313 .video
= &ml86v7667_subdev_video_ops
,
314 .pad
= &ml86v7667_subdev_pad_ops
,
317 static int ml86v7667_init(struct ml86v7667_priv
*priv
)
319 struct i2c_client
*client
= v4l2_get_subdevdata(&priv
->sd
);
323 /* BT.656-4 output mode, register mode */
324 ret
= ml86v7667_mask_set(client
, MRA_REG
,
325 MRA_OUTPUT_MODE_MASK
| MRA_REGISTER_MODE
,
326 MRA_ITUR_BT656
| MRA_REGISTER_MODE
);
328 /* PLL circuit fixed clock, 32MHz */
329 ret
|= ml86v7667_mask_set(client
, PLLR1_REG
, PLLR1_FIXED_CLOCK
,
332 /* ADC2 clamping voltage maximum */
333 ret
|= ml86v7667_mask_set(client
, ADC2_REG
, ADC2_CLAMP_VOLTAGE_MASK
,
334 ADC2_CLAMP_VOLTAGE(7));
336 /* enable luminance function */
337 ret
|= ml86v7667_mask_set(client
, SSEPL_REG
, SSEPL_LUMINANCE_ONOFF
,
338 SSEPL_LUMINANCE_ONOFF
);
340 /* enable contrast function */
341 ret
|= ml86v7667_mask_set(client
, CLC_REG
, CLC_CONTRAST_ONOFF
, 0);
344 * PAL/NTSC autodetection is enabled after reset,
345 * set the autodetected std in manual std mode and
346 * disable autodetection
348 val
= i2c_smbus_read_byte_data(client
, STATUS_REG
);
352 priv
->std
= val
& STATUS_NTSCPAL
? V4L2_STD_625_50
: V4L2_STD_525_60
;
353 ret
|= ml86v7667_mask_set(client
, MRC_REG
, MRC_AUTOSELECT
, 0);
355 val
= priv
->std
& V4L2_STD_525_60
? MRA_NTSC_BT601
: MRA_PAL_BT601
;
356 ret
|= ml86v7667_mask_set(client
, MRA_REG
, MRA_INPUT_MODE_MASK
, val
);
361 static int ml86v7667_probe(struct i2c_client
*client
,
362 const struct i2c_device_id
*did
)
364 struct ml86v7667_priv
*priv
;
367 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
370 priv
= devm_kzalloc(&client
->dev
, sizeof(*priv
), GFP_KERNEL
);
374 v4l2_i2c_subdev_init(&priv
->sd
, client
, &ml86v7667_subdev_ops
);
376 v4l2_ctrl_handler_init(&priv
->hdl
, 8);
377 v4l2_ctrl_new_std(&priv
->hdl
, &ml86v7667_ctrl_ops
,
378 V4L2_CID_BRIGHTNESS
, -64, 63, 1, 0);
379 v4l2_ctrl_new_std(&priv
->hdl
, &ml86v7667_ctrl_ops
,
380 V4L2_CID_CONTRAST
, -8, 7, 1, 0);
381 v4l2_ctrl_new_std(&priv
->hdl
, &ml86v7667_ctrl_ops
,
382 V4L2_CID_CHROMA_GAIN
, -32, 31, 1, 0);
383 v4l2_ctrl_new_std(&priv
->hdl
, &ml86v7667_ctrl_ops
,
384 V4L2_CID_HUE
, -128, 127, 1, 0);
385 v4l2_ctrl_new_std(&priv
->hdl
, &ml86v7667_ctrl_ops
,
386 V4L2_CID_RED_BALANCE
, -4, 3, 1, 0);
387 v4l2_ctrl_new_std(&priv
->hdl
, &ml86v7667_ctrl_ops
,
388 V4L2_CID_BLUE_BALANCE
, -4, 3, 1, 0);
389 v4l2_ctrl_new_std(&priv
->hdl
, &ml86v7667_ctrl_ops
,
390 V4L2_CID_SHARPNESS
, 0, 1, 1, 0);
391 v4l2_ctrl_new_std(&priv
->hdl
, &ml86v7667_ctrl_ops
,
392 V4L2_CID_COLOR_KILLER
, 0, 1, 1, 0);
393 priv
->sd
.ctrl_handler
= &priv
->hdl
;
395 ret
= priv
->hdl
.error
;
399 v4l2_ctrl_handler_setup(&priv
->hdl
);
401 ret
= ml86v7667_init(priv
);
405 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
406 client
->addr
, client
->adapter
->name
);
410 v4l2_ctrl_handler_free(&priv
->hdl
);
411 v4l2_device_unregister_subdev(&priv
->sd
);
412 v4l_err(client
, "failed to probe @ 0x%02x (%s)\n",
413 client
->addr
, client
->adapter
->name
);
417 static int ml86v7667_remove(struct i2c_client
*client
)
419 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
420 struct ml86v7667_priv
*priv
= to_ml86v7667(sd
);
422 v4l2_ctrl_handler_free(&priv
->hdl
);
423 v4l2_device_unregister_subdev(&priv
->sd
);
428 static const struct i2c_device_id ml86v7667_id
[] = {
432 MODULE_DEVICE_TABLE(i2c
, ml86v7667_id
);
434 static struct i2c_driver ml86v7667_i2c_driver
= {
438 .probe
= ml86v7667_probe
,
439 .remove
= ml86v7667_remove
,
440 .id_table
= ml86v7667_id
,
443 module_i2c_driver(ml86v7667_i2c_driver
);
445 MODULE_DESCRIPTION("OKI Semiconductor ML86V7667 video decoder driver");
446 MODULE_AUTHOR("Vladimir Barinov");
447 MODULE_LICENSE("GPL");