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/ak881x.h>
19 #include <media/v4l2-chip-ident.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-device.h>
23 #define AK881X_INTERFACE_MODE 0
24 #define AK881X_VIDEO_PROCESS1 1
25 #define AK881X_VIDEO_PROCESS2 2
26 #define AK881X_VIDEO_PROCESS3 3
27 #define AK881X_DAC_MODE 5
28 #define AK881X_STATUS 0x24
29 #define AK881X_DEVICE_ID 0x25
30 #define AK881X_DEVICE_REVISION 0x26
33 struct v4l2_subdev subdev
;
34 struct ak881x_pdata
*pdata
;
36 int id
; /* DEVICE_ID code V4L2_IDENT_AK881X code from v4l2-chip-ident.h */
37 char revision
; /* DEVICE_REVISION content */
40 static int reg_read(struct i2c_client
*client
, const u8 reg
)
42 return i2c_smbus_read_byte_data(client
, reg
);
45 static int reg_write(struct i2c_client
*client
, const u8 reg
,
48 return i2c_smbus_write_byte_data(client
, reg
, data
);
51 static int reg_set(struct i2c_client
*client
, const u8 reg
,
52 const u8 data
, u8 mask
)
54 int ret
= reg_read(client
, reg
);
57 return reg_write(client
, reg
, (ret
& ~mask
) | (data
& mask
));
60 static struct ak881x
*to_ak881x(const struct i2c_client
*client
)
62 return container_of(i2c_get_clientdata(client
), struct ak881x
, subdev
);
65 static int ak881x_g_chip_ident(struct v4l2_subdev
*sd
,
66 struct v4l2_dbg_chip_ident
*id
)
68 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
69 struct ak881x
*ak881x
= to_ak881x(client
);
71 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
74 if (id
->match
.addr
!= client
->addr
)
77 id
->ident
= ak881x
->id
;
78 id
->revision
= ak881x
->revision
;
83 #ifdef CONFIG_VIDEO_ADV_DEBUG
84 static int ak881x_g_register(struct v4l2_subdev
*sd
,
85 struct v4l2_dbg_register
*reg
)
87 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
89 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0x26)
92 if (reg
->match
.addr
!= client
->addr
)
95 reg
->val
= reg_read(client
, reg
->reg
);
97 if (reg
->val
> 0xffff)
103 static int ak881x_s_register(struct v4l2_subdev
*sd
,
104 struct v4l2_dbg_register
*reg
)
106 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
108 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0x26)
111 if (reg
->match
.addr
!= client
->addr
)
114 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
121 static int ak881x_try_g_mbus_fmt(struct v4l2_subdev
*sd
,
122 struct v4l2_mbus_framefmt
*mf
)
124 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
125 struct ak881x
*ak881x
= to_ak881x(client
);
127 v4l_bound_align_image(&mf
->width
, 0, 720, 2,
128 &mf
->height
, 0, ak881x
->lines
, 1, 0);
129 mf
->field
= V4L2_FIELD_INTERLACED
;
130 mf
->code
= V4L2_MBUS_FMT_YUYV8_2X8
;
131 mf
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
136 static int ak881x_s_mbus_fmt(struct v4l2_subdev
*sd
,
137 struct v4l2_mbus_framefmt
*mf
)
139 if (mf
->field
!= V4L2_FIELD_INTERLACED
||
140 mf
->code
!= V4L2_MBUS_FMT_YUYV8_2X8
)
143 return ak881x_try_g_mbus_fmt(sd
, mf
);
146 static int ak881x_enum_mbus_fmt(struct v4l2_subdev
*sd
, unsigned int index
,
147 enum v4l2_mbus_pixelcode
*code
)
152 *code
= V4L2_MBUS_FMT_YUYV8_2X8
;
156 static int ak881x_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
158 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
159 struct ak881x
*ak881x
= to_ak881x(client
);
163 a
->bounds
.width
= 720;
164 a
->bounds
.height
= ak881x
->lines
;
165 a
->defrect
= a
->bounds
;
166 a
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
167 a
->pixelaspect
.numerator
= 1;
168 a
->pixelaspect
.denominator
= 1;
173 static int ak881x_s_std_output(struct v4l2_subdev
*sd
, v4l2_std_id std
)
175 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
176 struct ak881x
*ak881x
= to_ak881x(client
);
179 if (std
== V4L2_STD_NTSC_443
) {
182 } else if (std
== V4L2_STD_PAL_M
) {
185 } else if (std
== V4L2_STD_PAL_60
) {
188 } else if (std
&& !(std
& ~V4L2_STD_PAL
)) {
191 } else if (std
&& !(std
& ~V4L2_STD_NTSC
)) {
195 /* No SECAM or PAL_N/Nc supported */
199 reg_set(client
, AK881X_VIDEO_PROCESS1
, vp1
, 0xf);
204 static int ak881x_s_stream(struct v4l2_subdev
*sd
, int enable
)
206 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
207 struct ak881x
*ak881x
= to_ak881x(client
);
211 /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
212 /* Default: composite output */
213 if (ak881x
->pdata
->flags
& AK881X_COMPONENT
)
217 /* Turn on the DAC(s) */
218 reg_write(client
, AK881X_DAC_MODE
, dac
);
219 dev_dbg(&client
->dev
, "chip status 0x%x\n",
220 reg_read(client
, AK881X_STATUS
));
222 /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */
223 reg_write(client
, AK881X_DAC_MODE
, 0);
224 dev_dbg(&client
->dev
, "chip status 0x%x\n",
225 reg_read(client
, AK881X_STATUS
));
231 static struct v4l2_subdev_core_ops ak881x_subdev_core_ops
= {
232 .g_chip_ident
= ak881x_g_chip_ident
,
233 #ifdef CONFIG_VIDEO_ADV_DEBUG
234 .g_register
= ak881x_g_register
,
235 .s_register
= ak881x_s_register
,
239 static struct v4l2_subdev_video_ops ak881x_subdev_video_ops
= {
240 .s_mbus_fmt
= ak881x_s_mbus_fmt
,
241 .g_mbus_fmt
= ak881x_try_g_mbus_fmt
,
242 .try_mbus_fmt
= ak881x_try_g_mbus_fmt
,
243 .cropcap
= ak881x_cropcap
,
244 .enum_mbus_fmt
= ak881x_enum_mbus_fmt
,
245 .s_std_output
= ak881x_s_std_output
,
246 .s_stream
= ak881x_s_stream
,
249 static struct v4l2_subdev_ops ak881x_subdev_ops
= {
250 .core
= &ak881x_subdev_core_ops
,
251 .video
= &ak881x_subdev_video_ops
,
254 static int ak881x_probe(struct i2c_client
*client
,
255 const struct i2c_device_id
*did
)
257 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
258 struct ak881x
*ak881x
;
261 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
262 dev_warn(&adapter
->dev
,
263 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
267 ak881x
= kzalloc(sizeof(struct ak881x
), GFP_KERNEL
);
271 v4l2_i2c_subdev_init(&ak881x
->subdev
, client
, &ak881x_subdev_ops
);
273 data
= reg_read(client
, AK881X_DEVICE_ID
);
277 ak881x
->id
= V4L2_IDENT_AK8813
;
280 ak881x
->id
= V4L2_IDENT_AK8814
;
283 dev_err(&client
->dev
,
284 "No ak881x chip detected, register read %x\n", data
);
289 ak881x
->revision
= reg_read(client
, AK881X_DEVICE_REVISION
);
290 ak881x
->pdata
= client
->dev
.platform_data
;
293 if (ak881x
->pdata
->flags
& AK881X_FIELD
)
298 switch (ak881x
->pdata
->flags
& AK881X_IF_MODE_MASK
) {
299 case AK881X_IF_MODE_BT656
:
302 case AK881X_IF_MODE_MASTER
:
305 case AK881X_IF_MODE_SLAVE
:
310 dev_dbg(&client
->dev
, "IF mode %x\n", ifmode
);
313 * "Line Blanking No." seems to be the same as the number of
314 * "black" lines on, e.g., SuperH VOU, whose default value of 20
315 * "incidentally" matches ak881x' default
317 reg_write(client
, AK881X_INTERFACE_MODE
, ifmode
| (20 << 3));
320 /* Hardware default: NTSC-M */
323 dev_info(&client
->dev
, "Detected an ak881x chip ID %x, revision %x\n",
324 data
, ak881x
->revision
);
329 static int ak881x_remove(struct i2c_client
*client
)
331 struct ak881x
*ak881x
= to_ak881x(client
);
333 v4l2_device_unregister_subdev(&ak881x
->subdev
);
339 static const struct i2c_device_id ak881x_id
[] = {
344 MODULE_DEVICE_TABLE(i2c
, ak881x_id
);
346 static struct i2c_driver ak881x_i2c_driver
= {
350 .probe
= ak881x_probe
,
351 .remove
= ak881x_remove
,
352 .id_table
= ak881x_id
,
355 static int __init
ak881x_module_init(void)
357 return i2c_add_driver(&ak881x_i2c_driver
);
360 static void __exit
ak881x_module_exit(void)
362 i2c_del_driver(&ak881x_i2c_driver
);
365 module_init(ak881x_module_init
);
366 module_exit(ak881x_module_exit
);
368 MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
369 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
370 MODULE_LICENSE("GPL v2");