Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / media / video / ak881x.c
blob53c496c00fb6b76fd03f151ad04cd9106b79c1c3
1 /*
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.
9 */
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
32 struct ak881x {
33 struct v4l2_subdev subdev;
34 struct ak881x_pdata *pdata;
35 unsigned int lines;
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,
46 const u8 data)
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);
55 if (ret < 0)
56 return ret;
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)
72 return -EINVAL;
74 if (id->match.addr != client->addr)
75 return -ENODEV;
77 id->ident = ak881x->id;
78 id->revision = ak881x->revision;
80 return 0;
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)
90 return -EINVAL;
92 if (reg->match.addr != client->addr)
93 return -ENODEV;
95 reg->val = reg_read(client, reg->reg);
97 if (reg->val > 0xffff)
98 return -EIO;
100 return 0;
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)
109 return -EINVAL;
111 if (reg->match.addr != client->addr)
112 return -ENODEV;
114 if (reg_write(client, reg->reg, reg->val) < 0)
115 return -EIO;
117 return 0;
119 #endif
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;
133 return 0;
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)
141 return -EINVAL;
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)
149 if (index)
150 return -EINVAL;
152 *code = V4L2_MBUS_FMT_YUYV8_2X8;
153 return 0;
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);
161 a->bounds.left = 0;
162 a->bounds.top = 0;
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;
170 return 0;
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);
177 u8 vp1;
179 if (std == V4L2_STD_NTSC_443) {
180 vp1 = 3;
181 ak881x->lines = 480;
182 } else if (std == V4L2_STD_PAL_M) {
183 vp1 = 5;
184 ak881x->lines = 480;
185 } else if (std == V4L2_STD_PAL_60) {
186 vp1 = 7;
187 ak881x->lines = 480;
188 } else if (std && !(std & ~V4L2_STD_PAL)) {
189 vp1 = 0xf;
190 ak881x->lines = 576;
191 } else if (std && !(std & ~V4L2_STD_NTSC)) {
192 vp1 = 0;
193 ak881x->lines = 480;
194 } else {
195 /* No SECAM or PAL_N/Nc supported */
196 return -EINVAL;
199 reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf);
201 return 0;
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);
209 if (enable) {
210 u8 dac;
211 /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
212 /* Default: composite output */
213 if (ak881x->pdata->flags & AK881X_COMPONENT)
214 dac = 3;
215 else
216 dac = 4;
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));
221 } else {
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));
228 return 0;
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,
236 #endif
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;
259 u8 ifmode, data;
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");
264 return -EIO;
267 ak881x = kzalloc(sizeof(struct ak881x), GFP_KERNEL);
268 if (!ak881x)
269 return -ENOMEM;
271 v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops);
273 data = reg_read(client, AK881X_DEVICE_ID);
275 switch (data) {
276 case 0x13:
277 ak881x->id = V4L2_IDENT_AK8813;
278 break;
279 case 0x14:
280 ak881x->id = V4L2_IDENT_AK8814;
281 break;
282 default:
283 dev_err(&client->dev,
284 "No ak881x chip detected, register read %x\n", data);
285 kfree(ak881x);
286 return -ENODEV;
289 ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION);
290 ak881x->pdata = client->dev.platform_data;
292 if (ak881x->pdata) {
293 if (ak881x->pdata->flags & AK881X_FIELD)
294 ifmode = 4;
295 else
296 ifmode = 0;
298 switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) {
299 case AK881X_IF_MODE_BT656:
300 ifmode |= 1;
301 break;
302 case AK881X_IF_MODE_MASTER:
303 ifmode |= 2;
304 break;
305 case AK881X_IF_MODE_SLAVE:
306 default:
307 break;
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 */
321 ak881x->lines = 480;
323 dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n",
324 data, ak881x->revision);
326 return 0;
329 static int ak881x_remove(struct i2c_client *client)
331 struct ak881x *ak881x = to_ak881x(client);
333 v4l2_device_unregister_subdev(&ak881x->subdev);
334 kfree(ak881x);
336 return 0;
339 static const struct i2c_device_id ak881x_id[] = {
340 { "ak8813", 0 },
341 { "ak8814", 0 },
344 MODULE_DEVICE_TABLE(i2c, ak881x_id);
346 static struct i2c_driver ak881x_i2c_driver = {
347 .driver = {
348 .name = "ak881x",
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");