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/videodev2.h>
16 #include <media/ak881x.h>
17 #include <media/v4l2-chip-ident.h>
18 #include <media/v4l2-common.h>
19 #include <media/v4l2-device.h>
21 #define AK881X_INTERFACE_MODE 0
22 #define AK881X_VIDEO_PROCESS1 1
23 #define AK881X_VIDEO_PROCESS2 2
24 #define AK881X_VIDEO_PROCESS3 3
25 #define AK881X_DAC_MODE 5
26 #define AK881X_STATUS 0x24
27 #define AK881X_DEVICE_ID 0x25
28 #define AK881X_DEVICE_REVISION 0x26
31 struct v4l2_subdev subdev
;
32 struct ak881x_pdata
*pdata
;
34 int id
; /* DEVICE_ID code V4L2_IDENT_AK881X code from v4l2-chip-ident.h */
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 static int ak881x_g_chip_ident(struct v4l2_subdev
*sd
,
64 struct v4l2_dbg_chip_ident
*id
)
66 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
67 struct ak881x
*ak881x
= to_ak881x(client
);
69 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
72 if (id
->match
.addr
!= client
->addr
)
75 id
->ident
= ak881x
->id
;
76 id
->revision
= ak881x
->revision
;
81 #ifdef CONFIG_VIDEO_ADV_DEBUG
82 static int ak881x_g_register(struct v4l2_subdev
*sd
,
83 struct v4l2_dbg_register
*reg
)
85 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
87 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0x26)
90 if (reg
->match
.addr
!= client
->addr
)
93 reg
->val
= reg_read(client
, reg
->reg
);
95 if (reg
->val
> 0xffff)
101 static int ak881x_s_register(struct v4l2_subdev
*sd
,
102 struct v4l2_dbg_register
*reg
)
104 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
106 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0x26)
109 if (reg
->match
.addr
!= client
->addr
)
112 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
119 static int ak881x_try_g_mbus_fmt(struct v4l2_subdev
*sd
,
120 struct v4l2_mbus_framefmt
*mf
)
122 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
123 struct ak881x
*ak881x
= to_ak881x(client
);
125 v4l_bound_align_image(&mf
->width
, 0, 720, 2,
126 &mf
->height
, 0, ak881x
->lines
, 1, 0);
127 mf
->field
= V4L2_FIELD_INTERLACED
;
128 mf
->code
= V4L2_MBUS_FMT_YUYV8_2X8_LE
;
129 mf
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
134 static int ak881x_s_mbus_fmt(struct v4l2_subdev
*sd
,
135 struct v4l2_mbus_framefmt
*mf
)
137 if (mf
->field
!= V4L2_FIELD_INTERLACED
||
138 mf
->code
!= V4L2_MBUS_FMT_YUYV8_2X8_LE
)
141 return ak881x_try_g_mbus_fmt(sd
, mf
);
144 static int ak881x_enum_mbus_fmt(struct v4l2_subdev
*sd
, int index
,
145 enum v4l2_mbus_pixelcode
*code
)
150 *code
= V4L2_MBUS_FMT_YUYV8_2X8_LE
;
154 static int ak881x_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
156 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
157 struct ak881x
*ak881x
= to_ak881x(client
);
161 a
->bounds
.width
= 720;
162 a
->bounds
.height
= ak881x
->lines
;
163 a
->defrect
= a
->bounds
;
164 a
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
165 a
->pixelaspect
.numerator
= 1;
166 a
->pixelaspect
.denominator
= 1;
171 static int ak881x_s_std_output(struct v4l2_subdev
*sd
, v4l2_std_id std
)
173 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
174 struct ak881x
*ak881x
= to_ak881x(client
);
177 if (std
== V4L2_STD_NTSC_443
) {
180 } else if (std
== V4L2_STD_PAL_M
) {
183 } else if (std
== V4L2_STD_PAL_60
) {
186 } else if (std
&& !(std
& ~V4L2_STD_PAL
)) {
189 } else if (std
&& !(std
& ~V4L2_STD_NTSC
)) {
193 /* No SECAM or PAL_N/Nc supported */
197 reg_set(client
, AK881X_VIDEO_PROCESS1
, vp1
, 0xf);
202 static int ak881x_s_stream(struct v4l2_subdev
*sd
, int enable
)
204 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
205 struct ak881x
*ak881x
= to_ak881x(client
);
209 /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
210 /* Default: composite output */
211 if (ak881x
->pdata
->flags
& AK881X_COMPONENT
)
215 /* Turn on the DAC(s) */
216 reg_write(client
, AK881X_DAC_MODE
, dac
);
217 dev_dbg(&client
->dev
, "chip status 0x%x\n",
218 reg_read(client
, AK881X_STATUS
));
220 /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */
221 reg_write(client
, AK881X_DAC_MODE
, 0);
222 dev_dbg(&client
->dev
, "chip status 0x%x\n",
223 reg_read(client
, AK881X_STATUS
));
229 static struct v4l2_subdev_core_ops ak881x_subdev_core_ops
= {
230 .g_chip_ident
= ak881x_g_chip_ident
,
231 #ifdef CONFIG_VIDEO_ADV_DEBUG
232 .g_register
= ak881x_g_register
,
233 .s_register
= ak881x_s_register
,
237 static struct v4l2_subdev_video_ops ak881x_subdev_video_ops
= {
238 .s_mbus_fmt
= ak881x_s_mbus_fmt
,
239 .g_mbus_fmt
= ak881x_try_g_mbus_fmt
,
240 .try_mbus_fmt
= ak881x_try_g_mbus_fmt
,
241 .cropcap
= ak881x_cropcap
,
242 .enum_mbus_fmt
= ak881x_enum_mbus_fmt
,
243 .s_std_output
= ak881x_s_std_output
,
244 .s_stream
= ak881x_s_stream
,
247 static struct v4l2_subdev_ops ak881x_subdev_ops
= {
248 .core
= &ak881x_subdev_core_ops
,
249 .video
= &ak881x_subdev_video_ops
,
252 static int ak881x_probe(struct i2c_client
*client
,
253 const struct i2c_device_id
*did
)
255 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
256 struct ak881x
*ak881x
;
259 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
260 dev_warn(&adapter
->dev
,
261 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
265 ak881x
= kzalloc(sizeof(struct ak881x
), GFP_KERNEL
);
269 v4l2_i2c_subdev_init(&ak881x
->subdev
, client
, &ak881x_subdev_ops
);
271 data
= reg_read(client
, AK881X_DEVICE_ID
);
275 ak881x
->id
= V4L2_IDENT_AK8813
;
278 ak881x
->id
= V4L2_IDENT_AK8814
;
281 dev_err(&client
->dev
,
282 "No ak881x chip detected, register read %x\n", data
);
287 ak881x
->revision
= reg_read(client
, AK881X_DEVICE_REVISION
);
288 ak881x
->pdata
= client
->dev
.platform_data
;
291 if (ak881x
->pdata
->flags
& AK881X_FIELD
)
296 switch (ak881x
->pdata
->flags
& AK881X_IF_MODE_MASK
) {
297 case AK881X_IF_MODE_BT656
:
300 case AK881X_IF_MODE_MASTER
:
303 case AK881X_IF_MODE_SLAVE
:
308 dev_dbg(&client
->dev
, "IF mode %x\n", ifmode
);
311 * "Line Blanking No." seems to be the same as the number of
312 * "black" lines on, e.g., SuperH VOU, whose default value of 20
313 * "incidentally" matches ak881x' default
315 reg_write(client
, AK881X_INTERFACE_MODE
, ifmode
| (20 << 3));
318 /* Hardware default: NTSC-M */
321 dev_info(&client
->dev
, "Detected an ak881x chip ID %x, revision %x\n",
322 data
, ak881x
->revision
);
327 static int ak881x_remove(struct i2c_client
*client
)
329 struct ak881x
*ak881x
= to_ak881x(client
);
331 v4l2_device_unregister_subdev(&ak881x
->subdev
);
337 static const struct i2c_device_id ak881x_id
[] = {
342 MODULE_DEVICE_TABLE(i2c
, ak881x_id
);
344 static struct i2c_driver ak881x_i2c_driver
= {
348 .probe
= ak881x_probe
,
349 .remove
= ak881x_remove
,
350 .id_table
= ak881x_id
,
353 static int __init
ak881x_module_init(void)
355 return i2c_add_driver(&ak881x_i2c_driver
);
358 static void __exit
ak881x_module_exit(void)
360 i2c_del_driver(&ak881x_i2c_driver
);
363 module_init(ak881x_module_init
);
364 module_exit(ak881x_module_exit
);
366 MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
367 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
368 MODULE_LICENSE("GPL v2");