xref: /openbmc/linux/drivers/media/i2c/bt856.c (revision c819e2cf)
1 /*
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>
32 #include <linux/types.h>
33 #include <linux/slab.h>
34 #include <linux/ioctl.h>
35 #include <asm/uaccess.h>
36 #include <linux/i2c.h>
37 #include <linux/videodev2.h>
38 #include <media/v4l2-device.h>
39 
40 MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
41 MODULE_AUTHOR("Mike Bernson & Dave Perks");
42 MODULE_LICENSE("GPL");
43 
44 static int debug;
45 module_param(debug, int, 0);
46 MODULE_PARM_DESC(debug, "Debug level (0-1)");
47 
48 
49 /* ----------------------------------------------------------------------- */
50 
51 #define BT856_REG_OFFSET	0xDA
52 #define BT856_NR_REG		6
53 
54 struct bt856 {
55 	struct v4l2_subdev sd;
56 	unsigned char reg[BT856_NR_REG];
57 
58 	v4l2_std_id norm;
59 };
60 
61 static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
62 {
63 	return container_of(sd, struct bt856, sd);
64 }
65 
66 /* ----------------------------------------------------------------------- */
67 
68 static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
69 {
70 	struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
71 
72 	encoder->reg[reg - BT856_REG_OFFSET] = value;
73 	return i2c_smbus_write_byte_data(client, reg, value);
74 }
75 
76 static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
77 {
78 	return bt856_write(encoder, reg,
79 		(encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
80 				(value ? (1 << bit) : 0));
81 }
82 
83 static void bt856_dump(struct bt856 *encoder)
84 {
85 	int i;
86 
87 	v4l2_info(&encoder->sd, "register dump:\n");
88 	for (i = 0; i < BT856_NR_REG; i += 2)
89 		printk(KERN_CONT " %02x", encoder->reg[i]);
90 	printk(KERN_CONT "\n");
91 }
92 
93 /* ----------------------------------------------------------------------- */
94 
95 static int bt856_init(struct v4l2_subdev *sd, u32 arg)
96 {
97 	struct bt856 *encoder = to_bt856(sd);
98 
99 	/* This is just for testing!!! */
100 	v4l2_dbg(1, debug, sd, "init\n");
101 	bt856_write(encoder, 0xdc, 0x18);
102 	bt856_write(encoder, 0xda, 0);
103 	bt856_write(encoder, 0xde, 0);
104 
105 	bt856_setbit(encoder, 0xdc, 3, 1);
106 	/*bt856_setbit(encoder, 0xdc, 6, 0);*/
107 	bt856_setbit(encoder, 0xdc, 4, 1);
108 
109 	if (encoder->norm & V4L2_STD_NTSC)
110 		bt856_setbit(encoder, 0xdc, 2, 0);
111 	else
112 		bt856_setbit(encoder, 0xdc, 2, 1);
113 
114 	bt856_setbit(encoder, 0xdc, 1, 1);
115 	bt856_setbit(encoder, 0xde, 4, 0);
116 	bt856_setbit(encoder, 0xde, 3, 1);
117 	if (debug != 0)
118 		bt856_dump(encoder);
119 	return 0;
120 }
121 
122 static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
123 {
124 	struct bt856 *encoder = to_bt856(sd);
125 
126 	v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
127 
128 	if (std & V4L2_STD_NTSC) {
129 		bt856_setbit(encoder, 0xdc, 2, 0);
130 	} else if (std & V4L2_STD_PAL) {
131 		bt856_setbit(encoder, 0xdc, 2, 1);
132 		bt856_setbit(encoder, 0xda, 0, 0);
133 		/*bt856_setbit(encoder, 0xda, 0, 1);*/
134 	} else {
135 		return -EINVAL;
136 	}
137 	encoder->norm = std;
138 	if (debug != 0)
139 		bt856_dump(encoder);
140 	return 0;
141 }
142 
143 static int bt856_s_routing(struct v4l2_subdev *sd,
144 			   u32 input, u32 output, u32 config)
145 {
146 	struct bt856 *encoder = to_bt856(sd);
147 
148 	v4l2_dbg(1, debug, sd, "set input %d\n", input);
149 
150 	/* We only have video bus.
151 	 * input= 0: input is from bt819
152 	 * input= 1: input is from ZR36060 */
153 	switch (input) {
154 	case 0:
155 		bt856_setbit(encoder, 0xde, 4, 0);
156 		bt856_setbit(encoder, 0xde, 3, 1);
157 		bt856_setbit(encoder, 0xdc, 3, 1);
158 		bt856_setbit(encoder, 0xdc, 6, 0);
159 		break;
160 	case 1:
161 		bt856_setbit(encoder, 0xde, 4, 0);
162 		bt856_setbit(encoder, 0xde, 3, 1);
163 		bt856_setbit(encoder, 0xdc, 3, 1);
164 		bt856_setbit(encoder, 0xdc, 6, 1);
165 		break;
166 	case 2:	/* Color bar */
167 		bt856_setbit(encoder, 0xdc, 3, 0);
168 		bt856_setbit(encoder, 0xde, 4, 1);
169 		break;
170 	default:
171 		return -EINVAL;
172 	}
173 
174 	if (debug != 0)
175 		bt856_dump(encoder);
176 	return 0;
177 }
178 
179 /* ----------------------------------------------------------------------- */
180 
181 static const struct v4l2_subdev_core_ops bt856_core_ops = {
182 	.init = bt856_init,
183 };
184 
185 static const struct v4l2_subdev_video_ops bt856_video_ops = {
186 	.s_std_output = bt856_s_std_output,
187 	.s_routing = bt856_s_routing,
188 };
189 
190 static const struct v4l2_subdev_ops bt856_ops = {
191 	.core = &bt856_core_ops,
192 	.video = &bt856_video_ops,
193 };
194 
195 /* ----------------------------------------------------------------------- */
196 
197 static int bt856_probe(struct i2c_client *client,
198 			const struct i2c_device_id *id)
199 {
200 	struct bt856 *encoder;
201 	struct v4l2_subdev *sd;
202 
203 	/* Check if the adapter supports the needed features */
204 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
205 		return -ENODEV;
206 
207 	v4l_info(client, "chip found @ 0x%x (%s)\n",
208 			client->addr << 1, client->adapter->name);
209 
210 	encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
211 	if (encoder == NULL)
212 		return -ENOMEM;
213 	sd = &encoder->sd;
214 	v4l2_i2c_subdev_init(sd, client, &bt856_ops);
215 	encoder->norm = V4L2_STD_NTSC;
216 
217 	bt856_write(encoder, 0xdc, 0x18);
218 	bt856_write(encoder, 0xda, 0);
219 	bt856_write(encoder, 0xde, 0);
220 
221 	bt856_setbit(encoder, 0xdc, 3, 1);
222 	/*bt856_setbit(encoder, 0xdc, 6, 0);*/
223 	bt856_setbit(encoder, 0xdc, 4, 1);
224 
225 	if (encoder->norm & V4L2_STD_NTSC)
226 		bt856_setbit(encoder, 0xdc, 2, 0);
227 	else
228 		bt856_setbit(encoder, 0xdc, 2, 1);
229 
230 	bt856_setbit(encoder, 0xdc, 1, 1);
231 	bt856_setbit(encoder, 0xde, 4, 0);
232 	bt856_setbit(encoder, 0xde, 3, 1);
233 
234 	if (debug != 0)
235 		bt856_dump(encoder);
236 	return 0;
237 }
238 
239 static int bt856_remove(struct i2c_client *client)
240 {
241 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
242 
243 	v4l2_device_unregister_subdev(sd);
244 	return 0;
245 }
246 
247 static const struct i2c_device_id bt856_id[] = {
248 	{ "bt856", 0 },
249 	{ }
250 };
251 MODULE_DEVICE_TABLE(i2c, bt856_id);
252 
253 static struct i2c_driver bt856_driver = {
254 	.driver = {
255 		.owner	= THIS_MODULE,
256 		.name	= "bt856",
257 	},
258 	.probe		= bt856_probe,
259 	.remove		= bt856_remove,
260 	.id_table	= bt856_id,
261 };
262 
263 module_i2c_driver(bt856_driver);
264