V4L/DVB: saa7191: remove obsolete v4l2-i2c-drv.h header
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / bt856.c
CommitLineData
d56410e0 1/*
1da177e4
LT
2 * bt856 - BT856A Digital Video Encoder (Rockwell Part)
3 *
4 * Copyright (C) 1999 Mike Bernson <mike@mlb.org>
5 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6 *
7 * Modifications for LML33/DC10plus unified driver
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * This code was modify/ported from the saa7111 driver written
11 * by Dave Perks.
12 *
13 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31#include <linux/module.h>
18f3fa1e 32#include <linux/types.h>
5a0e3ad6 33#include <linux/slab.h>
a8c26dfe 34#include <linux/ioctl.h>
18f3fa1e 35#include <asm/uaccess.h>
a8c26dfe
HV
36#include <linux/i2c.h>
37#include <linux/i2c-id.h>
a91f56e7
HV
38#include <linux/videodev2.h>
39#include <media/v4l2-device.h>
40#include <media/v4l2-chip-ident.h>
2d26698e 41#include <media/v4l2-i2c-drv.h>
1da177e4
LT
42
43MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
44MODULE_AUTHOR("Mike Bernson & Dave Perks");
45MODULE_LICENSE("GPL");
46
ff699e6b 47static int debug;
1da177e4
LT
48module_param(debug, int, 0);
49MODULE_PARM_DESC(debug, "Debug level (0-1)");
50
a91f56e7 51
1da177e4
LT
52/* ----------------------------------------------------------------------- */
53
187565c4
HV
54#define BT856_REG_OFFSET 0xDA
55#define BT856_NR_REG 6
1da177e4
LT
56
57struct bt856 {
a91f56e7 58 struct v4l2_subdev sd;
f49a5eae 59 unsigned char reg[BT856_NR_REG];
1da177e4 60
107063c6 61 v4l2_std_id norm;
1da177e4
LT
62};
63
a91f56e7
HV
64static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
65{
66 return container_of(sd, struct bt856, sd);
67}
68
1da177e4
LT
69/* ----------------------------------------------------------------------- */
70
a91f56e7 71static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
1da177e4 72{
a91f56e7 73 struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
1da177e4 74
187565c4 75 encoder->reg[reg - BT856_REG_OFFSET] = value;
1da177e4
LT
76 return i2c_smbus_write_byte_data(client, reg, value);
77}
78
a91f56e7 79static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
1da177e4 80{
a91f56e7 81 return bt856_write(encoder, reg,
a8c26dfe
HV
82 (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
83 (value ? (1 << bit) : 0));
1da177e4
LT
84}
85
a91f56e7 86static void bt856_dump(struct bt856 *encoder)
1da177e4
LT
87{
88 int i;
1da177e4 89
a91f56e7 90 v4l2_info(&encoder->sd, "register dump:\n");
f49a5eae 91 for (i = 0; i < BT856_NR_REG; i += 2)
a8c26dfe
HV
92 printk(KERN_CONT " %02x", encoder->reg[i]);
93 printk(KERN_CONT "\n");
1da177e4
LT
94}
95
96/* ----------------------------------------------------------------------- */
97
a91f56e7 98static int bt856_init(struct v4l2_subdev *sd, u32 arg)
1da177e4 99{
a91f56e7 100 struct bt856 *encoder = to_bt856(sd);
1da177e4 101
a91f56e7
HV
102 /* This is just for testing!!! */
103 v4l2_dbg(1, debug, sd, "init\n");
104 bt856_write(encoder, 0xdc, 0x18);
105 bt856_write(encoder, 0xda, 0);
106 bt856_write(encoder, 0xde, 0);
1da177e4 107
a91f56e7
HV
108 bt856_setbit(encoder, 0xdc, 3, 1);
109 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
110 bt856_setbit(encoder, 0xdc, 4, 1);
111
112 if (encoder->norm & V4L2_STD_NTSC)
113 bt856_setbit(encoder, 0xdc, 2, 0);
114 else
115 bt856_setbit(encoder, 0xdc, 2, 1);
116
117 bt856_setbit(encoder, 0xdc, 1, 1);
118 bt856_setbit(encoder, 0xde, 4, 0);
119 bt856_setbit(encoder, 0xde, 3, 1);
120 if (debug != 0)
121 bt856_dump(encoder);
122 return 0;
123}
124
125static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
126{
127 struct bt856 *encoder = to_bt856(sd);
128
cc5cef8e 129 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
a91f56e7
HV
130
131 if (std & V4L2_STD_NTSC) {
132 bt856_setbit(encoder, 0xdc, 2, 0);
133 } else if (std & V4L2_STD_PAL) {
134 bt856_setbit(encoder, 0xdc, 2, 1);
135 bt856_setbit(encoder, 0xda, 0, 0);
136 /*bt856_setbit(encoder, 0xda, 0, 1);*/
137 } else {
138 return -EINVAL;
a8c26dfe 139 }
a91f56e7
HV
140 encoder->norm = std;
141 if (debug != 0)
142 bt856_dump(encoder);
143 return 0;
144}
1da177e4 145
5325b427
HV
146static int bt856_s_routing(struct v4l2_subdev *sd,
147 u32 input, u32 output, u32 config)
a91f56e7
HV
148{
149 struct bt856 *encoder = to_bt856(sd);
150
5325b427 151 v4l2_dbg(1, debug, sd, "set input %d\n", input);
a91f56e7
HV
152
153 /* We only have video bus.
5325b427
HV
154 * input= 0: input is from bt819
155 * input= 1: input is from ZR36060 */
156 switch (input) {
a91f56e7
HV
157 case 0:
158 bt856_setbit(encoder, 0xde, 4, 0);
159 bt856_setbit(encoder, 0xde, 3, 1);
160 bt856_setbit(encoder, 0xdc, 3, 1);
161 bt856_setbit(encoder, 0xdc, 6, 0);
162 break;
163 case 1:
164 bt856_setbit(encoder, 0xde, 4, 0);
165 bt856_setbit(encoder, 0xde, 3, 1);
166 bt856_setbit(encoder, 0xdc, 3, 1);
167 bt856_setbit(encoder, 0xdc, 6, 1);
168 break;
169 case 2: /* Color bar */
170 bt856_setbit(encoder, 0xdc, 3, 0);
171 bt856_setbit(encoder, 0xde, 4, 1);
172 break;
1da177e4
LT
173 default:
174 return -EINVAL;
175 }
176
a91f56e7
HV
177 if (debug != 0)
178 bt856_dump(encoder);
1da177e4
LT
179 return 0;
180}
181
a91f56e7
HV
182static int bt856_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
183{
184 struct i2c_client *client = v4l2_get_subdevdata(sd);
185
186 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_BT856, 0);
187}
188
1da177e4
LT
189/* ----------------------------------------------------------------------- */
190
a91f56e7
HV
191static const struct v4l2_subdev_core_ops bt856_core_ops = {
192 .g_chip_ident = bt856_g_chip_ident,
193 .init = bt856_init,
194};
1da177e4 195
a91f56e7
HV
196static const struct v4l2_subdev_video_ops bt856_video_ops = {
197 .s_std_output = bt856_s_std_output,
198 .s_routing = bt856_s_routing,
199};
200
201static const struct v4l2_subdev_ops bt856_ops = {
202 .core = &bt856_core_ops,
203 .video = &bt856_video_ops,
204};
205
206/* ----------------------------------------------------------------------- */
1da177e4 207
a8c26dfe
HV
208static int bt856_probe(struct i2c_client *client,
209 const struct i2c_device_id *id)
1da177e4 210{
1da177e4 211 struct bt856 *encoder;
a91f56e7 212 struct v4l2_subdev *sd;
1da177e4 213
1da177e4 214 /* Check if the adapter supports the needed features */
a8c26dfe
HV
215 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
216 return -ENODEV;
1da177e4 217
a8c26dfe
HV
218 v4l_info(client, "chip found @ 0x%x (%s)\n",
219 client->addr << 1, client->adapter->name);
1da177e4 220
7408187d 221 encoder = kzalloc(sizeof(struct bt856), GFP_KERNEL);
a8c26dfe 222 if (encoder == NULL)
1da177e4 223 return -ENOMEM;
a91f56e7
HV
224 sd = &encoder->sd;
225 v4l2_i2c_subdev_init(sd, client, &bt856_ops);
107063c6 226 encoder->norm = V4L2_STD_NTSC;
1da177e4 227
a91f56e7
HV
228 bt856_write(encoder, 0xdc, 0x18);
229 bt856_write(encoder, 0xda, 0);
230 bt856_write(encoder, 0xde, 0);
1da177e4 231
a91f56e7
HV
232 bt856_setbit(encoder, 0xdc, 3, 1);
233 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
234 bt856_setbit(encoder, 0xdc, 4, 1);
1da177e4 235
107063c6 236 if (encoder->norm & V4L2_STD_NTSC)
a91f56e7 237 bt856_setbit(encoder, 0xdc, 2, 0);
107063c6 238 else
a91f56e7 239 bt856_setbit(encoder, 0xdc, 2, 1);
1da177e4 240
a91f56e7
HV
241 bt856_setbit(encoder, 0xdc, 1, 1);
242 bt856_setbit(encoder, 0xde, 4, 0);
243 bt856_setbit(encoder, 0xde, 3, 1);
1da177e4
LT
244
245 if (debug != 0)
a91f56e7 246 bt856_dump(encoder);
1da177e4
LT
247 return 0;
248}
249
a8c26dfe 250static int bt856_remove(struct i2c_client *client)
1da177e4 251{
a91f56e7
HV
252 struct v4l2_subdev *sd = i2c_get_clientdata(client);
253
254 v4l2_device_unregister_subdev(sd);
255 kfree(to_bt856(sd));
1da177e4
LT
256 return 0;
257}
258
a8c26dfe
HV
259static const struct i2c_device_id bt856_id[] = {
260 { "bt856", 0 },
261 { }
262};
263MODULE_DEVICE_TABLE(i2c, bt856_id);
1da177e4 264
a8c26dfe
HV
265static struct v4l2_i2c_driver_data v4l2_i2c_data = {
266 .name = "bt856",
a8c26dfe
HV
267 .probe = bt856_probe,
268 .remove = bt856_remove,
269 .id_table = bt856_id,
1da177e4 270};