2 * Driver for AK8813 / AK8814 TV-ecoders from Asahi Kasei Microsystems Co., Ltd. (AKM)
4 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/videodev2.h>
16 #include <linux/module.h>
18 #include <media/i2c/ak881x.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-device.h>
22 #define AK881X_INTERFACE_MODE 0
23 #define AK881X_VIDEO_PROCESS1 1
24 #define AK881X_VIDEO_PROCESS2 2
25 #define AK881X_VIDEO_PROCESS3 3
26 #define AK881X_DAC_MODE 5
27 #define AK881X_STATUS 0x24
28 #define AK881X_DEVICE_ID 0x25
29 #define AK881X_DEVICE_REVISION 0x26
32 struct v4l2_subdev subdev
;
33 struct ak881x_pdata
*pdata
;
35 char revision
; /* DEVICE_REVISION content */
38 static int reg_read(struct i2c_client
*client
, const u8 reg
)
40 return i2c_smbus_read_byte_data(client
, reg
);
43 static int reg_write(struct i2c_client
*client
, const u8 reg
,
46 return i2c_smbus_write_byte_data(client
, reg
, data
);
49 static int reg_set(struct i2c_client
*client
, const u8 reg
,
50 const u8 data
, u8 mask
)
52 int ret
= reg_read(client
, reg
);
55 return reg_write(client
, reg
, (ret
& ~mask
) | (data
& mask
));
58 static struct ak881x
*to_ak881x(const struct i2c_client
*client
)
60 return container_of(i2c_get_clientdata(client
), struct ak881x
, subdev
);
63 #ifdef CONFIG_VIDEO_ADV_DEBUG
64 static int ak881x_g_register(struct v4l2_subdev
*sd
,
65 struct v4l2_dbg_register
*reg
)
67 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
73 reg
->val
= reg_read(client
, reg
->reg
);
75 if (reg
->val
> 0xffff)
81 static int ak881x_s_register(struct v4l2_subdev
*sd
,
82 const struct v4l2_dbg_register
*reg
)
84 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
89 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
96 static int ak881x_fill_fmt(struct v4l2_subdev
*sd
,
97 struct v4l2_subdev_pad_config
*cfg
,
98 struct v4l2_subdev_format
*format
)
100 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
101 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
102 struct ak881x
*ak881x
= to_ak881x(client
);
107 v4l_bound_align_image(&mf
->width
, 0, 720, 2,
108 &mf
->height
, 0, ak881x
->lines
, 1, 0);
109 mf
->field
= V4L2_FIELD_INTERLACED
;
110 mf
->code
= MEDIA_BUS_FMT_YUYV8_2X8
;
111 mf
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
116 static int ak881x_enum_mbus_code(struct v4l2_subdev
*sd
,
117 struct v4l2_subdev_pad_config
*cfg
,
118 struct v4l2_subdev_mbus_code_enum
*code
)
120 if (code
->pad
|| code
->index
)
123 code
->code
= MEDIA_BUS_FMT_YUYV8_2X8
;
127 static int ak881x_get_selection(struct v4l2_subdev
*sd
,
128 struct v4l2_subdev_pad_config
*cfg
,
129 struct v4l2_subdev_selection
*sel
)
131 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
132 struct ak881x
*ak881x
= to_ak881x(client
);
134 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
137 switch (sel
->target
) {
138 case V4L2_SEL_TGT_CROP_BOUNDS
:
142 sel
->r
.height
= ak881x
->lines
;
149 static int ak881x_s_std_output(struct v4l2_subdev
*sd
, v4l2_std_id std
)
151 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
152 struct ak881x
*ak881x
= to_ak881x(client
);
155 if (std
== V4L2_STD_NTSC_443
) {
158 } else if (std
== V4L2_STD_PAL_M
) {
161 } else if (std
== V4L2_STD_PAL_60
) {
164 } else if (std
& V4L2_STD_NTSC
) {
167 } else if (std
& V4L2_STD_PAL
) {
171 /* No SECAM or PAL_N/Nc supported */
175 reg_set(client
, AK881X_VIDEO_PROCESS1
, vp1
, 0xf);
180 static int ak881x_s_stream(struct v4l2_subdev
*sd
, int enable
)
182 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
183 struct ak881x
*ak881x
= to_ak881x(client
);
187 /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
188 /* Default: composite output */
189 if (ak881x
->pdata
->flags
& AK881X_COMPONENT
)
193 /* Turn on the DAC(s) */
194 reg_write(client
, AK881X_DAC_MODE
, dac
);
195 dev_dbg(&client
->dev
, "chip status 0x%x\n",
196 reg_read(client
, AK881X_STATUS
));
198 /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */
199 reg_write(client
, AK881X_DAC_MODE
, 0);
200 dev_dbg(&client
->dev
, "chip status 0x%x\n",
201 reg_read(client
, AK881X_STATUS
));
207 static const struct v4l2_subdev_core_ops ak881x_subdev_core_ops
= {
208 #ifdef CONFIG_VIDEO_ADV_DEBUG
209 .g_register
= ak881x_g_register
,
210 .s_register
= ak881x_s_register
,
214 static const struct v4l2_subdev_video_ops ak881x_subdev_video_ops
= {
215 .s_std_output
= ak881x_s_std_output
,
216 .s_stream
= ak881x_s_stream
,
219 static const struct v4l2_subdev_pad_ops ak881x_subdev_pad_ops
= {
220 .enum_mbus_code
= ak881x_enum_mbus_code
,
221 .get_selection
= ak881x_get_selection
,
222 .set_fmt
= ak881x_fill_fmt
,
223 .get_fmt
= ak881x_fill_fmt
,
226 static const struct v4l2_subdev_ops ak881x_subdev_ops
= {
227 .core
= &ak881x_subdev_core_ops
,
228 .video
= &ak881x_subdev_video_ops
,
229 .pad
= &ak881x_subdev_pad_ops
,
232 static int ak881x_probe(struct i2c_client
*client
,
233 const struct i2c_device_id
*did
)
235 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
236 struct ak881x
*ak881x
;
239 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
240 dev_warn(&adapter
->dev
,
241 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
245 ak881x
= devm_kzalloc(&client
->dev
, sizeof(*ak881x
), GFP_KERNEL
);
249 v4l2_i2c_subdev_init(&ak881x
->subdev
, client
, &ak881x_subdev_ops
);
251 data
= reg_read(client
, AK881X_DEVICE_ID
);
258 dev_err(&client
->dev
,
259 "No ak881x chip detected, register read %x\n", data
);
263 ak881x
->revision
= reg_read(client
, AK881X_DEVICE_REVISION
);
264 ak881x
->pdata
= client
->dev
.platform_data
;
267 if (ak881x
->pdata
->flags
& AK881X_FIELD
)
272 switch (ak881x
->pdata
->flags
& AK881X_IF_MODE_MASK
) {
273 case AK881X_IF_MODE_BT656
:
276 case AK881X_IF_MODE_MASTER
:
279 case AK881X_IF_MODE_SLAVE
:
284 dev_dbg(&client
->dev
, "IF mode %x\n", ifmode
);
287 * "Line Blanking No." seems to be the same as the number of
288 * "black" lines on, e.g., SuperH VOU, whose default value of 20
289 * "incidentally" matches ak881x' default
291 reg_write(client
, AK881X_INTERFACE_MODE
, ifmode
| (20 << 3));
294 /* Hardware default: NTSC-M */
297 dev_info(&client
->dev
, "Detected an ak881x chip ID %x, revision %x\n",
298 data
, ak881x
->revision
);
303 static int ak881x_remove(struct i2c_client
*client
)
305 struct ak881x
*ak881x
= to_ak881x(client
);
307 v4l2_device_unregister_subdev(&ak881x
->subdev
);
312 static const struct i2c_device_id ak881x_id
[] = {
317 MODULE_DEVICE_TABLE(i2c
, ak881x_id
);
319 static struct i2c_driver ak881x_i2c_driver
= {
323 .probe
= ak881x_probe
,
324 .remove
= ak881x_remove
,
325 .id_table
= ak881x_id
,
328 module_i2c_driver(ak881x_i2c_driver
);
330 MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
331 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
332 MODULE_LICENSE("GPL v2");