[media] soc_camera sensors: remove g_chip_ident op
[deliverable/linux.git] / drivers / media / i2c / ak881x.c
CommitLineData
aec11e5d
GL
1/*
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>
1db2c22b 14#include <linux/slab.h>
aec11e5d 15#include <linux/videodev2.h>
7a707b89 16#include <linux/module.h>
aec11e5d
GL
17
18#include <media/ak881x.h>
19#include <media/v4l2-chip-ident.h>
20#include <media/v4l2-common.h>
21#include <media/v4l2-device.h>
22
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
31
32struct 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 */
38};
39
40static int reg_read(struct i2c_client *client, const u8 reg)
41{
42 return i2c_smbus_read_byte_data(client, reg);
43}
44
45static int reg_write(struct i2c_client *client, const u8 reg,
46 const u8 data)
47{
48 return i2c_smbus_write_byte_data(client, reg, data);
49}
50
51static int reg_set(struct i2c_client *client, const u8 reg,
52 const u8 data, u8 mask)
53{
54 int ret = reg_read(client, reg);
55 if (ret < 0)
56 return ret;
57 return reg_write(client, reg, (ret & ~mask) | (data & mask));
58}
59
60static struct ak881x *to_ak881x(const struct i2c_client *client)
61{
62 return container_of(i2c_get_clientdata(client), struct ak881x, subdev);
63}
64
65static int ak881x_g_chip_ident(struct v4l2_subdev *sd,
66 struct v4l2_dbg_chip_ident *id)
67{
68 struct i2c_client *client = v4l2_get_subdevdata(sd);
69 struct ak881x *ak881x = to_ak881x(client);
70
71 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
72 return -EINVAL;
73
74 if (id->match.addr != client->addr)
75 return -ENODEV;
76
77 id->ident = ak881x->id;
78 id->revision = ak881x->revision;
79
80 return 0;
81}
82
83#ifdef CONFIG_VIDEO_ADV_DEBUG
84static int ak881x_g_register(struct v4l2_subdev *sd,
85 struct v4l2_dbg_register *reg)
86{
87 struct i2c_client *client = v4l2_get_subdevdata(sd);
88
89 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x26)
90 return -EINVAL;
91
92 if (reg->match.addr != client->addr)
93 return -ENODEV;
94
95 reg->val = reg_read(client, reg->reg);
96
97 if (reg->val > 0xffff)
98 return -EIO;
99
100 return 0;
101}
102
103static int ak881x_s_register(struct v4l2_subdev *sd,
977ba3b1 104 const struct v4l2_dbg_register *reg)
aec11e5d
GL
105{
106 struct i2c_client *client = v4l2_get_subdevdata(sd);
107
108 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x26)
109 return -EINVAL;
110
111 if (reg->match.addr != client->addr)
112 return -ENODEV;
113
114 if (reg_write(client, reg->reg, reg->val) < 0)
115 return -EIO;
116
117 return 0;
118}
119#endif
120
121static int ak881x_try_g_mbus_fmt(struct v4l2_subdev *sd,
122 struct v4l2_mbus_framefmt *mf)
123{
124 struct i2c_client *client = v4l2_get_subdevdata(sd);
125 struct ak881x *ak881x = to_ak881x(client);
126
127 v4l_bound_align_image(&mf->width, 0, 720, 2,
128 &mf->height, 0, ak881x->lines, 1, 0);
129 mf->field = V4L2_FIELD_INTERLACED;
ace6e979 130 mf->code = V4L2_MBUS_FMT_YUYV8_2X8;
aec11e5d
GL
131 mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
132
133 return 0;
134}
135
136static int ak881x_s_mbus_fmt(struct v4l2_subdev *sd,
137 struct v4l2_mbus_framefmt *mf)
138{
139 if (mf->field != V4L2_FIELD_INTERLACED ||
ace6e979 140 mf->code != V4L2_MBUS_FMT_YUYV8_2X8)
aec11e5d
GL
141 return -EINVAL;
142
143 return ak881x_try_g_mbus_fmt(sd, mf);
144}
145
3805f201 146static int ak881x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
aec11e5d
GL
147 enum v4l2_mbus_pixelcode *code)
148{
149 if (index)
150 return -EINVAL;
151
ace6e979 152 *code = V4L2_MBUS_FMT_YUYV8_2X8;
aec11e5d
GL
153 return 0;
154}
155
156static int ak881x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
157{
158 struct i2c_client *client = v4l2_get_subdevdata(sd);
159 struct ak881x *ak881x = to_ak881x(client);
160
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;
169
170 return 0;
171}
172
173static int ak881x_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
174{
175 struct i2c_client *client = v4l2_get_subdevdata(sd);
176 struct ak881x *ak881x = to_ak881x(client);
177 u8 vp1;
178
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;
197 }
198
199 reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf);
200
201 return 0;
202}
203
204static int ak881x_s_stream(struct v4l2_subdev *sd, int enable)
205{
206 struct i2c_client *client = v4l2_get_subdevdata(sd);
207 struct ak881x *ak881x = to_ak881x(client);
208
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));
226 }
227
228 return 0;
229}
230
231static 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
237};
238
239static 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,
247};
248
249static struct v4l2_subdev_ops ak881x_subdev_ops = {
250 .core = &ak881x_subdev_core_ops,
251 .video = &ak881x_subdev_video_ops,
252};
253
254static int ak881x_probe(struct i2c_client *client,
255 const struct i2c_device_id *did)
256{
257 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
258 struct ak881x *ak881x;
259 u8 ifmode, data;
260
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;
265 }
266
c02b211d 267 ak881x = devm_kzalloc(&client->dev, sizeof(*ak881x), GFP_KERNEL);
aec11e5d
GL
268 if (!ak881x)
269 return -ENOMEM;
270
271 v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops);
272
273 data = reg_read(client, AK881X_DEVICE_ID);
274
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);
aec11e5d
GL
285 return -ENODEV;
286 }
287
288 ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION);
289 ak881x->pdata = client->dev.platform_data;
290
291 if (ak881x->pdata) {
292 if (ak881x->pdata->flags & AK881X_FIELD)
293 ifmode = 4;
294 else
295 ifmode = 0;
296
297 switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) {
298 case AK881X_IF_MODE_BT656:
299 ifmode |= 1;
300 break;
301 case AK881X_IF_MODE_MASTER:
302 ifmode |= 2;
303 break;
304 case AK881X_IF_MODE_SLAVE:
305 default:
306 break;
307 }
308
309 dev_dbg(&client->dev, "IF mode %x\n", ifmode);
310
311 /*
312 * "Line Blanking No." seems to be the same as the number of
313 * "black" lines on, e.g., SuperH VOU, whose default value of 20
314 * "incidentally" matches ak881x' default
315 */
316 reg_write(client, AK881X_INTERFACE_MODE, ifmode | (20 << 3));
317 }
318
319 /* Hardware default: NTSC-M */
320 ak881x->lines = 480;
321
322 dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n",
323 data, ak881x->revision);
324
325 return 0;
326}
327
328static int ak881x_remove(struct i2c_client *client)
329{
330 struct ak881x *ak881x = to_ak881x(client);
331
332 v4l2_device_unregister_subdev(&ak881x->subdev);
aec11e5d
GL
333
334 return 0;
335}
336
337static const struct i2c_device_id ak881x_id[] = {
338 { "ak8813", 0 },
339 { "ak8814", 0 },
340 { }
341};
342MODULE_DEVICE_TABLE(i2c, ak881x_id);
343
344static struct i2c_driver ak881x_i2c_driver = {
345 .driver = {
346 .name = "ak881x",
347 },
348 .probe = ak881x_probe,
349 .remove = ak881x_remove,
350 .id_table = ak881x_id,
351};
352
c6e8d86f 353module_i2c_driver(ak881x_i2c_driver);
aec11e5d
GL
354
355MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
356MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
357MODULE_LICENSE("GPL v2");
This page took 0.244999 seconds and 5 git commands to generate.