blob: 30f9db1351b91a26fb2369695eebddf4a4d014c3 [file] [log] [blame]
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -03001/*
2 * Driver for AK8813 / AK8814 TV-ecoders from Asahi Kasei Microsystems Co., Ltd. (AKM)
3 *
4 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 *
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 */
10
11#include <linux/i2c.h>
12#include <linux/init.h>
13#include <linux/platform_device.h>
Randy Dunlap1db2c222010-05-20 18:08:23 -030014#include <linux/slab.h>
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030015#include <linux/videodev2.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040016#include <linux/module.h>
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030017
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020018#include <media/i2c/ak881x.h>
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030019#include <media/v4l2-common.h>
20#include <media/v4l2-device.h>
21
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
30
31struct ak881x {
32 struct v4l2_subdev subdev;
33 struct ak881x_pdata *pdata;
34 unsigned int lines;
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030035 char revision; /* DEVICE_REVISION content */
36};
37
38static int reg_read(struct i2c_client *client, const u8 reg)
39{
40 return i2c_smbus_read_byte_data(client, reg);
41}
42
43static int reg_write(struct i2c_client *client, const u8 reg,
44 const u8 data)
45{
46 return i2c_smbus_write_byte_data(client, reg, data);
47}
48
49static int reg_set(struct i2c_client *client, const u8 reg,
50 const u8 data, u8 mask)
51{
52 int ret = reg_read(client, reg);
53 if (ret < 0)
54 return ret;
55 return reg_write(client, reg, (ret & ~mask) | (data & mask));
56}
57
58static struct ak881x *to_ak881x(const struct i2c_client *client)
59{
60 return container_of(i2c_get_clientdata(client), struct ak881x, subdev);
61}
62
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030063#ifdef CONFIG_VIDEO_ADV_DEBUG
64static int ak881x_g_register(struct v4l2_subdev *sd,
65 struct v4l2_dbg_register *reg)
66{
67 struct i2c_client *client = v4l2_get_subdevdata(sd);
68
Hans Verkuile1277112013-05-29 06:59:51 -030069 if (reg->reg > 0x26)
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030070 return -EINVAL;
71
Hans Verkuil15c4fee2013-05-29 07:00:07 -030072 reg->size = 1;
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030073 reg->val = reg_read(client, reg->reg);
74
75 if (reg->val > 0xffff)
76 return -EIO;
77
78 return 0;
79}
80
81static int ak881x_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b12013-03-24 08:28:46 -030082 const struct v4l2_dbg_register *reg)
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030083{
84 struct i2c_client *client = v4l2_get_subdevdata(sd);
85
Hans Verkuile1277112013-05-29 06:59:51 -030086 if (reg->reg > 0x26)
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030087 return -EINVAL;
88
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030089 if (reg_write(client, reg->reg, reg->val) < 0)
90 return -EIO;
91
92 return 0;
93}
94#endif
95
Hans Verkuilda298c62015-04-09 04:02:34 -030096static int ak881x_fill_fmt(struct v4l2_subdev *sd,
97 struct v4l2_subdev_pad_config *cfg,
98 struct v4l2_subdev_format *format)
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -030099{
Hans Verkuilda298c62015-04-09 04:02:34 -0300100 struct v4l2_mbus_framefmt *mf = &format->format;
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300101 struct i2c_client *client = v4l2_get_subdevdata(sd);
102 struct ak881x *ak881x = to_ak881x(client);
103
Hans Verkuilda298c62015-04-09 04:02:34 -0300104 if (format->pad)
105 return -EINVAL;
106
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300107 v4l_bound_align_image(&mf->width, 0, 720, 2,
108 &mf->height, 0, ak881x->lines, 1, 0);
109 mf->field = V4L2_FIELD_INTERLACED;
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300110 mf->code = MEDIA_BUS_FMT_YUYV8_2X8;
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300111 mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
112
113 return 0;
114}
115
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300116static int ak881x_enum_mbus_code(struct v4l2_subdev *sd,
117 struct v4l2_subdev_pad_config *cfg,
118 struct v4l2_subdev_mbus_code_enum *code)
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300119{
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300120 if (code->pad || code->index)
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300121 return -EINVAL;
122
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300123 code->code = MEDIA_BUS_FMT_YUYV8_2X8;
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300124 return 0;
125}
126
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200127static int ak881x_get_selection(struct v4l2_subdev *sd,
128 struct v4l2_subdev_pad_config *cfg,
129 struct v4l2_subdev_selection *sel)
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300130{
131 struct i2c_client *client = v4l2_get_subdevdata(sd);
132 struct ak881x *ak881x = to_ak881x(client);
133
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200134 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
135 return -EINVAL;
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300136
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200137 switch (sel->target) {
138 case V4L2_SEL_TGT_CROP_BOUNDS:
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200139 sel->r.left = 0;
140 sel->r.top = 0;
141 sel->r.width = 720;
142 sel->r.height = ak881x->lines;
143 return 0;
144 default:
145 return -EINVAL;
146 }
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300147}
148
149static int ak881x_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
150{
151 struct i2c_client *client = v4l2_get_subdevdata(sd);
152 struct ak881x *ak881x = to_ak881x(client);
153 u8 vp1;
154
155 if (std == V4L2_STD_NTSC_443) {
156 vp1 = 3;
157 ak881x->lines = 480;
158 } else if (std == V4L2_STD_PAL_M) {
159 vp1 = 5;
160 ak881x->lines = 480;
161 } else if (std == V4L2_STD_PAL_60) {
162 vp1 = 7;
163 ak881x->lines = 480;
Hans Verkuil517ef252015-06-15 08:33:32 -0300164 } else if (std & V4L2_STD_NTSC) {
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300165 vp1 = 0;
166 ak881x->lines = 480;
Hans Verkuil517ef252015-06-15 08:33:32 -0300167 } else if (std & V4L2_STD_PAL) {
168 vp1 = 0xf;
169 ak881x->lines = 576;
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300170 } else {
171 /* No SECAM or PAL_N/Nc supported */
172 return -EINVAL;
173 }
174
175 reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf);
176
177 return 0;
178}
179
180static int ak881x_s_stream(struct v4l2_subdev *sd, int enable)
181{
182 struct i2c_client *client = v4l2_get_subdevdata(sd);
183 struct ak881x *ak881x = to_ak881x(client);
184
185 if (enable) {
186 u8 dac;
187 /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
188 /* Default: composite output */
189 if (ak881x->pdata->flags & AK881X_COMPONENT)
190 dac = 3;
191 else
192 dac = 4;
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));
197 } else {
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));
202 }
203
204 return 0;
205}
206
Bhumika Goyal4d7aa5e2016-12-13 15:26:13 -0200207static const struct v4l2_subdev_core_ops ak881x_subdev_core_ops = {
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300208#ifdef CONFIG_VIDEO_ADV_DEBUG
209 .g_register = ak881x_g_register,
210 .s_register = ak881x_s_register,
211#endif
212};
213
Bhumika Goyal4d7aa5e2016-12-13 15:26:13 -0200214static const struct v4l2_subdev_video_ops ak881x_subdev_video_ops = {
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300215 .s_std_output = ak881x_s_std_output,
216 .s_stream = ak881x_s_stream,
217};
218
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300219static const struct v4l2_subdev_pad_ops ak881x_subdev_pad_ops = {
220 .enum_mbus_code = ak881x_enum_mbus_code,
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200221 .get_selection = ak881x_get_selection,
Hans Verkuilda298c62015-04-09 04:02:34 -0300222 .set_fmt = ak881x_fill_fmt,
223 .get_fmt = ak881x_fill_fmt,
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300224};
225
Bhumika Goyal4d7aa5e2016-12-13 15:26:13 -0200226static const struct v4l2_subdev_ops ak881x_subdev_ops = {
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300227 .core = &ak881x_subdev_core_ops,
228 .video = &ak881x_subdev_video_ops,
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300229 .pad = &ak881x_subdev_pad_ops,
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300230};
231
232static int ak881x_probe(struct i2c_client *client,
233 const struct i2c_device_id *did)
234{
235 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
236 struct ak881x *ak881x;
237 u8 ifmode, data;
238
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");
242 return -EIO;
243 }
244
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300245 ak881x = devm_kzalloc(&client->dev, sizeof(*ak881x), GFP_KERNEL);
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300246 if (!ak881x)
247 return -ENOMEM;
248
249 v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops);
250
251 data = reg_read(client, AK881X_DEVICE_ID);
252
253 switch (data) {
254 case 0x13:
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300255 case 0x14:
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300256 break;
257 default:
258 dev_err(&client->dev,
259 "No ak881x chip detected, register read %x\n", data);
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300260 return -ENODEV;
261 }
262
263 ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION);
264 ak881x->pdata = client->dev.platform_data;
265
266 if (ak881x->pdata) {
267 if (ak881x->pdata->flags & AK881X_FIELD)
268 ifmode = 4;
269 else
270 ifmode = 0;
271
272 switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) {
273 case AK881X_IF_MODE_BT656:
274 ifmode |= 1;
275 break;
276 case AK881X_IF_MODE_MASTER:
277 ifmode |= 2;
278 break;
279 case AK881X_IF_MODE_SLAVE:
280 default:
281 break;
282 }
283
284 dev_dbg(&client->dev, "IF mode %x\n", ifmode);
285
286 /*
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
290 */
291 reg_write(client, AK881X_INTERFACE_MODE, ifmode | (20 << 3));
292 }
293
294 /* Hardware default: NTSC-M */
295 ak881x->lines = 480;
296
297 dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n",
298 data, ak881x->revision);
299
300 return 0;
301}
302
303static int ak881x_remove(struct i2c_client *client)
304{
305 struct ak881x *ak881x = to_ak881x(client);
306
307 v4l2_device_unregister_subdev(&ak881x->subdev);
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300308
309 return 0;
310}
311
312static const struct i2c_device_id ak881x_id[] = {
313 { "ak8813", 0 },
314 { "ak8814", 0 },
315 { }
316};
317MODULE_DEVICE_TABLE(i2c, ak881x_id);
318
319static struct i2c_driver ak881x_i2c_driver = {
320 .driver = {
321 .name = "ak881x",
322 },
323 .probe = ak881x_probe,
324 .remove = ak881x_remove,
325 .id_table = ak881x_id,
326};
327
Axel Linc6e8d862012-02-12 06:56:32 -0300328module_i2c_driver(ak881x_i2c_driver);
Guennadi Liakhovetskiaec11e52010-03-29 04:45:22 -0300329
330MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
331MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
332MODULE_LICENSE("GPL v2");