Commit | Line | Data |
---|---|---|
6789cb52 RR |
1 | /* |
2 | * adv7180.c Analog Devices ADV7180 video decoder driver | |
3 | * Copyright (c) 2009 Intel Corporation | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/interrupt.h> | |
24 | #include <linux/i2c.h> | |
5a0e3ad6 | 25 | #include <linux/slab.h> |
6789cb52 RR |
26 | #include <media/v4l2-ioctl.h> |
27 | #include <linux/videodev2.h> | |
28 | #include <media/v4l2-device.h> | |
29 | #include <media/v4l2-chip-ident.h> | |
42752f7a | 30 | #include <linux/mutex.h> |
6789cb52 RR |
31 | |
32 | #define DRIVER_NAME "adv7180" | |
33 | ||
d3124294 RR |
34 | #define ADV7180_INPUT_CONTROL_REG 0x00 |
35 | #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00 | |
36 | #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10 | |
37 | #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20 | |
38 | #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30 | |
39 | #define ADV7180_INPUT_CONTROL_NTSC_J 0x40 | |
40 | #define ADV7180_INPUT_CONTROL_NTSC_M 0x50 | |
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 | ||
42752f7a RR |
52 | #define ADV7180_EXTENDED_OUTPUT_CONTROL_REG 0x04 |
53 | #define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5 | |
6789cb52 | 54 | |
42752f7a RR |
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 | |
6789cb52 | 60 | |
d3124294 RR |
61 | #define ADV7180_STATUS1_REG 0x10 |
62 | #define ADV7180_STATUS1_IN_LOCK 0x01 | |
63 | #define ADV7180_STATUS1_AUTOD_MASK 0x70 | |
6789cb52 RR |
64 | #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00 |
65 | #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10 | |
66 | #define ADV7180_STATUS1_AUTOD_PAL_M 0x20 | |
67 | #define ADV7180_STATUS1_AUTOD_PAL_60 0x30 | |
68 | #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40 | |
69 | #define ADV7180_STATUS1_AUTOD_SECAM 0x50 | |
70 | #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60 | |
71 | #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70 | |
72 | ||
73 | #define ADV7180_IDENT_REG 0x11 | |
74 | #define ADV7180_ID_7180 0x18 | |
75 | ||
42752f7a RR |
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 | |
6789cb52 RR |
92 | |
93 | struct adv7180_state { | |
c277b60a | 94 | struct v4l2_subdev sd; |
42752f7a RR |
95 | struct work_struct work; |
96 | struct mutex mutex; /* mutual excl. when accessing chip */ | |
97 | int irq; | |
c277b60a RR |
98 | v4l2_std_id curr_norm; |
99 | bool autodetect; | |
6789cb52 RR |
100 | }; |
101 | ||
d3124294 | 102 | static v4l2_std_id adv7180_std_to_v4l2(u8 status1) |
6789cb52 | 103 | { |
6789cb52 RR |
104 | switch (status1 & ADV7180_STATUS1_AUTOD_MASK) { |
105 | case ADV7180_STATUS1_AUTOD_NTSM_M_J: | |
d3124294 | 106 | return V4L2_STD_NTSC; |
6789cb52 RR |
107 | case ADV7180_STATUS1_AUTOD_NTSC_4_43: |
108 | return V4L2_STD_NTSC_443; | |
109 | case ADV7180_STATUS1_AUTOD_PAL_M: | |
110 | return V4L2_STD_PAL_M; | |
111 | case ADV7180_STATUS1_AUTOD_PAL_60: | |
112 | return V4L2_STD_PAL_60; | |
113 | case ADV7180_STATUS1_AUTOD_PAL_B_G: | |
114 | return V4L2_STD_PAL; | |
115 | case ADV7180_STATUS1_AUTOD_SECAM: | |
116 | return V4L2_STD_SECAM; | |
117 | case ADV7180_STATUS1_AUTOD_PAL_COMB: | |
118 | return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N; | |
119 | case ADV7180_STATUS1_AUTOD_SECAM_525: | |
120 | return V4L2_STD_SECAM; | |
121 | default: | |
122 | return V4L2_STD_UNKNOWN; | |
123 | } | |
124 | } | |
125 | ||
c277b60a RR |
126 | static 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 | ||
d3124294 RR |
149 | static 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 | ||
157 | static 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 | ||
6789cb52 RR |
173 | static inline struct adv7180_state *to_state(struct v4l2_subdev *sd) |
174 | { | |
175 | return container_of(sd, struct adv7180_state, sd); | |
176 | } | |
177 | ||
178 | static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std) | |
179 | { | |
c277b60a | 180 | struct adv7180_state *state = to_state(sd); |
42752f7a RR |
181 | int err = mutex_lock_interruptible(&state->mutex); |
182 | if (err) | |
183 | return err; | |
c277b60a | 184 | |
42752f7a RR |
185 | /* when we are interrupt driven we know the state */ |
186 | if (!state->autodetect || state->irq > 0) | |
c277b60a RR |
187 | *std = state->curr_norm; |
188 | else | |
189 | err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std); | |
190 | ||
42752f7a | 191 | mutex_unlock(&state->mutex); |
c277b60a | 192 | return err; |
d3124294 | 193 | } |
6789cb52 | 194 | |
d3124294 RR |
195 | static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status) |
196 | { | |
42752f7a RR |
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; | |
6789cb52 RR |
205 | } |
206 | ||
207 | static int adv7180_g_chip_ident(struct v4l2_subdev *sd, | |
208 | struct v4l2_dbg_chip_ident *chip) | |
209 | { | |
210 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
211 | ||
212 | return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0); | |
213 | } | |
214 | ||
c277b60a RR |
215 | static 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); | |
42752f7a RR |
219 | int ret = mutex_lock_interruptible(&state->mutex); |
220 | if (ret) | |
221 | return ret; | |
c277b60a RR |
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 | ||
42752f7a | 231 | __adv7180_status(client, NULL, &state->curr_norm); |
c277b60a RR |
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; | |
247 | out: | |
42752f7a | 248 | mutex_unlock(&state->mutex); |
c277b60a RR |
249 | return ret; |
250 | } | |
251 | ||
6789cb52 RR |
252 | static const struct v4l2_subdev_video_ops adv7180_video_ops = { |
253 | .querystd = adv7180_querystd, | |
d3124294 | 254 | .g_input_status = adv7180_g_input_status, |
6789cb52 RR |
255 | }; |
256 | ||
257 | static const struct v4l2_subdev_core_ops adv7180_core_ops = { | |
258 | .g_chip_ident = adv7180_g_chip_ident, | |
c277b60a | 259 | .s_std = adv7180_s_std, |
6789cb52 RR |
260 | }; |
261 | ||
262 | static const struct v4l2_subdev_ops adv7180_ops = { | |
263 | .core = &adv7180_core_ops, | |
264 | .video = &adv7180_video_ops, | |
265 | }; | |
266 | ||
42752f7a RR |
267 | static 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 | ||
289 | static 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 | ||
6789cb52 RR |
300 | /* |
301 | * Generic i2c probe | |
302 | * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' | |
303 | */ | |
304 | ||
527aebf2 | 305 | static __devinit int adv7180_probe(struct i2c_client *client, |
6789cb52 RR |
306 | const struct i2c_device_id *id) |
307 | { | |
308 | struct adv7180_state *state; | |
309 | struct v4l2_subdev *sd; | |
310 | int ret; | |
311 | ||
312 | /* Check if the adapter supports the needed features */ | |
313 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
314 | return -EIO; | |
315 | ||
316 | v4l_info(client, "chip found @ 0x%02x (%s)\n", | |
317 | client->addr << 1, client->adapter->name); | |
318 | ||
319 | state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL); | |
42752f7a RR |
320 | if (state == NULL) { |
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); | |
c277b60a | 328 | state->autodetect = true; |
6789cb52 RR |
329 | sd = &state->sd; |
330 | v4l2_i2c_subdev_init(sd, client, &adv7180_ops); | |
331 | ||
332 | /* Initialize adv7180 */ | |
42752f7a | 333 | /* Enable autodetection */ |
6789cb52 | 334 | ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG, |
d3124294 | 335 | ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM); |
42752f7a RR |
336 | if (ret < 0) |
337 | goto err_unreg_subdev; | |
338 | ||
339 | ret = i2c_smbus_write_byte_data(client, ADV7180_AUTODETECT_ENABLE_REG, | |
340 | ADV7180_AUTODETECT_DEFAULT); | |
341 | if (ret < 0) | |
342 | goto err_unreg_subdev; | |
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; | |
6789cb52 RR |
394 | } |
395 | ||
396 | return 0; | |
42752f7a RR |
397 | |
398 | err_unreg_subdev: | |
399 | mutex_destroy(&state->mutex); | |
400 | v4l2_device_unregister_subdev(sd); | |
401 | kfree(state); | |
402 | err: | |
403 | printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret); | |
404 | return ret; | |
6789cb52 RR |
405 | } |
406 | ||
527aebf2 | 407 | static __devexit int adv7180_remove(struct i2c_client *client) |
6789cb52 RR |
408 | { |
409 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
42752f7a RR |
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 | } | |
6789cb52 | 423 | |
42752f7a | 424 | mutex_destroy(&state->mutex); |
6789cb52 RR |
425 | v4l2_device_unregister_subdev(sd); |
426 | kfree(to_state(sd)); | |
427 | return 0; | |
428 | } | |
429 | ||
430 | static const struct i2c_device_id adv7180_id[] = { | |
431 | {DRIVER_NAME, 0}, | |
432 | {}, | |
433 | }; | |
434 | ||
435 | MODULE_DEVICE_TABLE(i2c, adv7180_id); | |
436 | ||
437 | static struct i2c_driver adv7180_driver = { | |
438 | .driver = { | |
439 | .owner = THIS_MODULE, | |
440 | .name = DRIVER_NAME, | |
441 | }, | |
442 | .probe = adv7180_probe, | |
527aebf2 | 443 | .remove = __devexit_p(adv7180_remove), |
6789cb52 RR |
444 | .id_table = adv7180_id, |
445 | }; | |
446 | ||
447 | static __init int adv7180_init(void) | |
448 | { | |
449 | return i2c_add_driver(&adv7180_driver); | |
450 | } | |
451 | ||
452 | static __exit void adv7180_exit(void) | |
453 | { | |
454 | i2c_del_driver(&adv7180_driver); | |
455 | } | |
456 | ||
457 | module_init(adv7180_init); | |
458 | module_exit(adv7180_exit); | |
459 | ||
460 | MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver"); | |
461 | MODULE_AUTHOR("Mocean Laboratories"); | |
462 | MODULE_LICENSE("GPL v2"); | |
463 |