aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/adv7180.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/adv7180.c')
-rw-r--r--drivers/media/video/adv7180.c323
1 files changed, 292 insertions, 31 deletions
diff --git a/drivers/media/video/adv7180.c b/drivers/media/video/adv7180.c
index 1b3cbd02a7fd..0826f0dabc17 100644
--- a/drivers/media/video/adv7180.c
+++ b/drivers/media/video/adv7180.c
@@ -27,17 +27,40 @@
27#include <linux/videodev2.h> 27#include <linux/videodev2.h>
28#include <media/v4l2-device.h> 28#include <media/v4l2-device.h>
29#include <media/v4l2-chip-ident.h> 29#include <media/v4l2-chip-ident.h>
30#include <linux/mutex.h>
30 31
31#define DRIVER_NAME "adv7180" 32#define DRIVER_NAME "adv7180"
32 33
33#define ADV7180_INPUT_CONTROL_REG 0x00 34#define ADV7180_INPUT_CONTROL_REG 0x00
34#define ADV7180_INPUT_CONTROL_PAL_BG_NTSC_J_SECAM 0x00 35#define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00
35#define ADV7180_AUTODETECT_ENABLE_REG 0x07 36#define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
36#define ADV7180_AUTODETECT_DEFAULT 0x7f 37#define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20
37 38#define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30
38 39#define ADV7180_INPUT_CONTROL_NTSC_J 0x40
39#define ADV7180_STATUS1_REG 0x10 40#define ADV7180_INPUT_CONTROL_NTSC_M 0x50
40#define ADV7180_STATUS1_AUTOD_MASK 0x70 41#define ADV7180_INPUT_CONTROL_PAL60 0x60
42#define ADV7180_INPUT_CONTROL_NTSC_443 0x70
43#define ADV7180_INPUT_CONTROL_PAL_BG 0x80
44#define ADV7180_INPUT_CONTROL_PAL_N 0x90
45#define ADV7180_INPUT_CONTROL_PAL_M 0xa0
46#define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0
47#define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0
48#define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0
49#define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0
50#define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0
51
52#define ADV7180_EXTENDED_OUTPUT_CONTROL_REG 0x04
53#define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
54
55#define ADV7180_AUTODETECT_ENABLE_REG 0x07
56#define ADV7180_AUTODETECT_DEFAULT 0x7f
57
58#define ADV7180_ADI_CTRL_REG 0x0e
59#define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
60
61#define ADV7180_STATUS1_REG 0x10
62#define ADV7180_STATUS1_IN_LOCK 0x01
63#define ADV7180_STATUS1_AUTOD_MASK 0x70
41#define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00 64#define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
42#define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10 65#define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
43#define ADV7180_STATUS1_AUTOD_PAL_M 0x20 66#define ADV7180_STATUS1_AUTOD_PAL_M 0x20
@@ -50,18 +73,37 @@
50#define ADV7180_IDENT_REG 0x11 73#define ADV7180_IDENT_REG 0x11
51#define ADV7180_ID_7180 0x18 74#define ADV7180_ID_7180 0x18
52 75
76#define ADV7180_ICONF1_ADI 0x40
77#define ADV7180_ICONF1_ACTIVE_LOW 0x01
78#define ADV7180_ICONF1_PSYNC_ONLY 0x10
79#define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
80
81#define ADV7180_IRQ1_LOCK 0x01
82#define ADV7180_IRQ1_UNLOCK 0x02
83#define ADV7180_ISR1_ADI 0x42
84#define ADV7180_ICR1_ADI 0x43
85#define ADV7180_IMR1_ADI 0x44
86#define ADV7180_IMR2_ADI 0x48
87#define ADV7180_IRQ3_AD_CHANGE 0x08
88#define ADV7180_ISR3_ADI 0x4A
89#define ADV7180_ICR3_ADI 0x4B
90#define ADV7180_IMR3_ADI 0x4C
91#define ADV7180_IMR4_ADI 0x50
53 92
54struct adv7180_state { 93struct adv7180_state {
55 struct v4l2_subdev sd; 94 struct v4l2_subdev sd;
95 struct work_struct work;
96 struct mutex mutex; /* mutual excl. when accessing chip */
97 int irq;
98 v4l2_std_id curr_norm;
99 bool autodetect;
56}; 100};
57 101
58static v4l2_std_id determine_norm(struct i2c_client *client) 102static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
59{ 103{
60 u8 status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
61
62 switch (status1 & ADV7180_STATUS1_AUTOD_MASK) { 104 switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
63 case ADV7180_STATUS1_AUTOD_NTSM_M_J: 105 case ADV7180_STATUS1_AUTOD_NTSM_M_J:
64 return V4L2_STD_NTSC_M_JP; 106 return V4L2_STD_NTSC;
65 case ADV7180_STATUS1_AUTOD_NTSC_4_43: 107 case ADV7180_STATUS1_AUTOD_NTSC_4_43:
66 return V4L2_STD_NTSC_443; 108 return V4L2_STD_NTSC_443;
67 case ADV7180_STATUS1_AUTOD_PAL_M: 109 case ADV7180_STATUS1_AUTOD_PAL_M:
@@ -81,6 +123,53 @@ static v4l2_std_id determine_norm(struct i2c_client *client)
81 } 123 }
82} 124}
83 125
126static int v4l2_std_to_adv7180(v4l2_std_id std)
127{
128 if (std == V4L2_STD_PAL_60)
129 return ADV7180_INPUT_CONTROL_PAL60;
130 if (std == V4L2_STD_NTSC_443)
131 return ADV7180_INPUT_CONTROL_NTSC_443;
132 if (std == V4L2_STD_PAL_N)
133 return ADV7180_INPUT_CONTROL_PAL_N;
134 if (std == V4L2_STD_PAL_M)
135 return ADV7180_INPUT_CONTROL_PAL_M;
136 if (std == V4L2_STD_PAL_Nc)
137 return ADV7180_INPUT_CONTROL_PAL_COMB_N;
138
139 if (std & V4L2_STD_PAL)
140 return ADV7180_INPUT_CONTROL_PAL_BG;
141 if (std & V4L2_STD_NTSC)
142 return ADV7180_INPUT_CONTROL_NTSC_M;
143 if (std & V4L2_STD_SECAM)
144 return ADV7180_INPUT_CONTROL_PAL_SECAM;
145
146 return -EINVAL;
147}
148
149static u32 adv7180_status_to_v4l2(u8 status1)
150{
151 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
152 return V4L2_IN_ST_NO_SIGNAL;
153
154 return 0;
155}
156
157static int __adv7180_status(struct i2c_client *client, u32 *status,
158 v4l2_std_id *std)
159{
160 int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
161
162 if (status1 < 0)
163 return status1;
164
165 if (status)
166 *status = adv7180_status_to_v4l2(status1);
167 if (std)
168 *std = adv7180_std_to_v4l2(status1);
169
170 return 0;
171}
172
84static inline struct adv7180_state *to_state(struct v4l2_subdev *sd) 173static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
85{ 174{
86 return container_of(sd, struct adv7180_state, sd); 175 return container_of(sd, struct adv7180_state, sd);
@@ -88,10 +177,31 @@ static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
88 177
89static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std) 178static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
90{ 179{
91 struct i2c_client *client = v4l2_get_subdevdata(sd); 180 struct adv7180_state *state = to_state(sd);
181 int err = mutex_lock_interruptible(&state->mutex);
182 if (err)
183 return err;
184
185 /* when we are interrupt driven we know the state */
186 if (!state->autodetect || state->irq > 0)
187 *std = state->curr_norm;
188 else
189 err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
190
191 mutex_unlock(&state->mutex);
192 return err;
193}
92 194
93 *std = determine_norm(client); 195static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
94 return 0; 196{
197 struct adv7180_state *state = to_state(sd);
198 int ret = mutex_lock_interruptible(&state->mutex);
199 if (ret)
200 return ret;
201
202 ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
203 mutex_unlock(&state->mutex);
204 return ret;
95} 205}
96 206
97static int adv7180_g_chip_ident(struct v4l2_subdev *sd, 207static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
@@ -102,12 +212,51 @@ static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
102 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0); 212 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
103} 213}
104 214
215static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
216{
217 struct adv7180_state *state = to_state(sd);
218 struct i2c_client *client = v4l2_get_subdevdata(sd);
219 int ret = mutex_lock_interruptible(&state->mutex);
220 if (ret)
221 return ret;
222
223 /* all standards -> autodetect */
224 if (std == V4L2_STD_ALL) {
225 ret = i2c_smbus_write_byte_data(client,
226 ADV7180_INPUT_CONTROL_REG,
227 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
228 if (ret < 0)
229 goto out;
230
231 __adv7180_status(client, NULL, &state->curr_norm);
232 state->autodetect = true;
233 } else {
234 ret = v4l2_std_to_adv7180(std);
235 if (ret < 0)
236 goto out;
237
238 ret = i2c_smbus_write_byte_data(client,
239 ADV7180_INPUT_CONTROL_REG, ret);
240 if (ret < 0)
241 goto out;
242
243 state->curr_norm = std;
244 state->autodetect = false;
245 }
246 ret = 0;
247out:
248 mutex_unlock(&state->mutex);
249 return ret;
250}
251
105static const struct v4l2_subdev_video_ops adv7180_video_ops = { 252static const struct v4l2_subdev_video_ops adv7180_video_ops = {
106 .querystd = adv7180_querystd, 253 .querystd = adv7180_querystd,
254 .g_input_status = adv7180_g_input_status,
107}; 255};
108 256
109static const struct v4l2_subdev_core_ops adv7180_core_ops = { 257static const struct v4l2_subdev_core_ops adv7180_core_ops = {
110 .g_chip_ident = adv7180_g_chip_ident, 258 .g_chip_ident = adv7180_g_chip_ident,
259 .s_std = adv7180_s_std,
111}; 260};
112 261
113static const struct v4l2_subdev_ops adv7180_ops = { 262static const struct v4l2_subdev_ops adv7180_ops = {
@@ -115,12 +264,45 @@ static const struct v4l2_subdev_ops adv7180_ops = {
115 .video = &adv7180_video_ops, 264 .video = &adv7180_video_ops,
116}; 265};
117 266
267static void adv7180_work(struct work_struct *work)
268{
269 struct adv7180_state *state = container_of(work, struct adv7180_state,
270 work);
271 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
272 u8 isr3;
273
274 mutex_lock(&state->mutex);
275 i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
276 ADV7180_ADI_CTRL_IRQ_SPACE);
277 isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
278 /* clear */
279 i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
280 i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);
281
282 if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
283 __adv7180_status(client, NULL, &state->curr_norm);
284 mutex_unlock(&state->mutex);
285
286 enable_irq(state->irq);
287}
288
289static irqreturn_t adv7180_irq(int irq, void *devid)
290{
291 struct adv7180_state *state = devid;
292
293 schedule_work(&state->work);
294
295 disable_irq_nosync(state->irq);
296
297 return IRQ_HANDLED;
298}
299
118/* 300/*
119 * Generic i2c probe 301 * Generic i2c probe
120 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' 302 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
121 */ 303 */
122 304
123static int adv7180_probe(struct i2c_client *client, 305static __devinit int adv7180_probe(struct i2c_client *client,
124 const struct i2c_device_id *id) 306 const struct i2c_device_id *id)
125{ 307{
126 struct adv7180_state *state; 308 struct adv7180_state *state;
@@ -135,32 +317,111 @@ static int adv7180_probe(struct i2c_client *client,
135 client->addr << 1, client->adapter->name); 317 client->addr << 1, client->adapter->name);
136 318
137 state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL); 319 state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
138 if (state == NULL) 320 if (state == NULL) {
139 return -ENOMEM; 321 ret = -ENOMEM;
322 goto err;
323 }
324
325 state->irq = client->irq;
326 INIT_WORK(&state->work, adv7180_work);
327 mutex_init(&state->mutex);
328 state->autodetect = true;
140 sd = &state->sd; 329 sd = &state->sd;
141 v4l2_i2c_subdev_init(sd, client, &adv7180_ops); 330 v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
142 331
143 /* Initialize adv7180 */ 332 /* Initialize adv7180 */
144 /* enable autodetection */ 333 /* Enable autodetection */
145 ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG, 334 ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
146 ADV7180_INPUT_CONTROL_PAL_BG_NTSC_J_SECAM); 335 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
147 if (ret > 0) 336 if (ret < 0)
148 ret = i2c_smbus_write_byte_data(client, 337 goto err_unreg_subdev;
149 ADV7180_AUTODETECT_ENABLE_REG, 338
150 ADV7180_AUTODETECT_DEFAULT); 339 ret = i2c_smbus_write_byte_data(client, ADV7180_AUTODETECT_ENABLE_REG,
151 if (ret < 0) { 340 ADV7180_AUTODETECT_DEFAULT);
152 printk(KERN_ERR DRIVER_NAME 341 if (ret < 0)
153 ": Failed to communicate to chip: %d\n", ret); 342 goto err_unreg_subdev;
154 return ret; 343
344 /* ITU-R BT.656-4 compatible */
345 ret = i2c_smbus_write_byte_data(client,
346 ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
347 ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
348 if (ret < 0)
349 goto err_unreg_subdev;
350
351 /* read current norm */
352 __adv7180_status(client, NULL, &state->curr_norm);
353
354 /* register for interrupts */
355 if (state->irq > 0) {
356 ret = request_irq(state->irq, adv7180_irq, 0, DRIVER_NAME,
357 state);
358 if (ret)
359 goto err_unreg_subdev;
360
361 ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
362 ADV7180_ADI_CTRL_IRQ_SPACE);
363 if (ret < 0)
364 goto err_unreg_subdev;
365
366 /* config the Interrupt pin to be active low */
367 ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
368 ADV7180_ICONF1_ACTIVE_LOW | ADV7180_ICONF1_PSYNC_ONLY);
369 if (ret < 0)
370 goto err_unreg_subdev;
371
372 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
373 if (ret < 0)
374 goto err_unreg_subdev;
375
376 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
377 if (ret < 0)
378 goto err_unreg_subdev;
379
380 /* enable AD change interrupts interrupts */
381 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
382 ADV7180_IRQ3_AD_CHANGE);
383 if (ret < 0)
384 goto err_unreg_subdev;
385
386 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
387 if (ret < 0)
388 goto err_unreg_subdev;
389
390 ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
391 0);
392 if (ret < 0)
393 goto err_unreg_subdev;
155 } 394 }
156 395
157 return 0; 396 return 0;
397
398err_unreg_subdev:
399 mutex_destroy(&state->mutex);
400 v4l2_device_unregister_subdev(sd);
401 kfree(state);
402err:
403 printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret);
404 return ret;
158} 405}
159 406
160static int adv7180_remove(struct i2c_client *client) 407static __devexit int adv7180_remove(struct i2c_client *client)
161{ 408{
162 struct v4l2_subdev *sd = i2c_get_clientdata(client); 409 struct v4l2_subdev *sd = i2c_get_clientdata(client);
410 struct adv7180_state *state = to_state(sd);
411
412 if (state->irq > 0) {
413 free_irq(client->irq, state);
414 if (cancel_work_sync(&state->work)) {
415 /*
416 * Work was pending, therefore we need to enable
417 * IRQ here to balance the disable_irq() done in the
418 * interrupt handler.
419 */
420 enable_irq(state->irq);
421 }
422 }
163 423
424 mutex_destroy(&state->mutex);
164 v4l2_device_unregister_subdev(sd); 425 v4l2_device_unregister_subdev(sd);
165 kfree(to_state(sd)); 426 kfree(to_state(sd));
166 return 0; 427 return 0;
@@ -179,7 +440,7 @@ static struct i2c_driver adv7180_driver = {
179 .name = DRIVER_NAME, 440 .name = DRIVER_NAME,
180 }, 441 },
181 .probe = adv7180_probe, 442 .probe = adv7180_probe,
182 .remove = adv7180_remove, 443 .remove = __devexit_p(adv7180_remove),
183 .id_table = adv7180_id, 444 .id_table = adv7180_id,
184}; 445};
185 446