aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media')
-rw-r--r--drivers/media/i2c/adv7604.c1464
-rw-r--r--drivers/media/platform/Kconfig4
-rw-r--r--drivers/media/platform/omap3isp/Makefile2
-rw-r--r--drivers/media/platform/omap3isp/isp.c108
-rw-r--r--drivers/media/platform/omap3isp/isp.h8
-rw-r--r--drivers/media/platform/omap3isp/ispccdc.c107
-rw-r--r--drivers/media/platform/omap3isp/ispccdc.h16
-rw-r--r--drivers/media/platform/omap3isp/ispccp2.c4
-rw-r--r--drivers/media/platform/omap3isp/ispcsi2.c4
-rw-r--r--drivers/media/platform/omap3isp/isph3a_aewb.c2
-rw-r--r--drivers/media/platform/omap3isp/isph3a_af.c2
-rw-r--r--drivers/media/platform/omap3isp/isppreview.c8
-rw-r--r--drivers/media/platform/omap3isp/ispqueue.c1161
-rw-r--r--drivers/media/platform/omap3isp/ispqueue.h188
-rw-r--r--drivers/media/platform/omap3isp/ispresizer.c8
-rw-r--r--drivers/media/platform/omap3isp/ispstat.c197
-rw-r--r--drivers/media/platform/omap3isp/ispstat.h3
-rw-r--r--drivers/media/platform/omap3isp/ispvideo.c325
-rw-r--r--drivers/media/platform/omap3isp/ispvideo.h29
-rw-r--r--drivers/media/platform/vsp1/Makefile2
-rw-r--r--drivers/media/platform/vsp1/vsp1.h3
-rw-r--r--drivers/media/platform/vsp1/vsp1_bru.c395
-rw-r--r--drivers/media/platform/vsp1/vsp1_bru.h39
-rw-r--r--drivers/media/platform/vsp1/vsp1_drv.c101
-rw-r--r--drivers/media/platform/vsp1/vsp1_entity.c57
-rw-r--r--drivers/media/platform/vsp1/vsp1_entity.h24
-rw-r--r--drivers/media/platform/vsp1/vsp1_hsit.c7
-rw-r--r--drivers/media/platform/vsp1/vsp1_lif.c1
-rw-r--r--drivers/media/platform/vsp1/vsp1_lut.c1
-rw-r--r--drivers/media/platform/vsp1/vsp1_regs.h98
-rw-r--r--drivers/media/platform/vsp1/vsp1_rpf.c7
-rw-r--r--drivers/media/platform/vsp1/vsp1_rwpf.h4
-rw-r--r--drivers/media/platform/vsp1/vsp1_sru.c1
-rw-r--r--drivers/media/platform/vsp1/vsp1_uds.c4
-rw-r--r--drivers/media/platform/vsp1/vsp1_video.c26
-rw-r--r--drivers/media/platform/vsp1/vsp1_video.h1
-rw-r--r--drivers/media/platform/vsp1/vsp1_wpf.c13
-rw-r--r--drivers/media/rc/redrat3.c102
-rw-r--r--drivers/media/rc/streamzap.c9
-rw-r--r--drivers/media/usb/dvb-usb-v2/dvb_usb_core.c6
-rw-r--r--drivers/media/v4l2-core/videobuf2-core.c24
41 files changed, 2200 insertions, 2365 deletions
diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c
index 338baa4c23ef..1778d320272e 100644
--- a/drivers/media/i2c/adv7604.c
+++ b/drivers/media/i2c/adv7604.c
@@ -27,19 +27,21 @@
27 * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010 27 * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010
28 */ 28 */
29 29
30 30#include <linux/delay.h>
31#include <linux/gpio/consumer.h>
32#include <linux/i2c.h>
31#include <linux/kernel.h> 33#include <linux/kernel.h>
32#include <linux/module.h> 34#include <linux/module.h>
33#include <linux/slab.h> 35#include <linux/slab.h>
34#include <linux/i2c.h> 36#include <linux/v4l2-dv-timings.h>
35#include <linux/delay.h>
36#include <linux/videodev2.h> 37#include <linux/videodev2.h>
37#include <linux/workqueue.h> 38#include <linux/workqueue.h>
38#include <linux/v4l2-dv-timings.h> 39
39#include <media/v4l2-device.h> 40#include <media/adv7604.h>
40#include <media/v4l2-ctrls.h> 41#include <media/v4l2-ctrls.h>
42#include <media/v4l2-device.h>
41#include <media/v4l2-dv-timings.h> 43#include <media/v4l2-dv-timings.h>
42#include <media/adv7604.h> 44#include <media/v4l2-of.h>
43 45
44static int debug; 46static int debug;
45module_param(debug, int, 0644); 47module_param(debug, int, 0644);
@@ -53,6 +55,76 @@ MODULE_LICENSE("GPL");
53/* ADV7604 system clock frequency */ 55/* ADV7604 system clock frequency */
54#define ADV7604_fsc (28636360) 56#define ADV7604_fsc (28636360)
55 57
58#define ADV7604_RGB_OUT (1 << 1)
59
60#define ADV7604_OP_FORMAT_SEL_8BIT (0 << 0)
61#define ADV7604_OP_FORMAT_SEL_10BIT (1 << 0)
62#define ADV7604_OP_FORMAT_SEL_12BIT (2 << 0)
63
64#define ADV7604_OP_MODE_SEL_SDR_422 (0 << 5)
65#define ADV7604_OP_MODE_SEL_DDR_422 (1 << 5)
66#define ADV7604_OP_MODE_SEL_SDR_444 (2 << 5)
67#define ADV7604_OP_MODE_SEL_DDR_444 (3 << 5)
68#define ADV7604_OP_MODE_SEL_SDR_422_2X (4 << 5)
69#define ADV7604_OP_MODE_SEL_ADI_CM (5 << 5)
70
71#define ADV7604_OP_CH_SEL_GBR (0 << 5)
72#define ADV7604_OP_CH_SEL_GRB (1 << 5)
73#define ADV7604_OP_CH_SEL_BGR (2 << 5)
74#define ADV7604_OP_CH_SEL_RGB (3 << 5)
75#define ADV7604_OP_CH_SEL_BRG (4 << 5)
76#define ADV7604_OP_CH_SEL_RBG (5 << 5)
77
78#define ADV7604_OP_SWAP_CB_CR (1 << 0)
79
80enum adv7604_type {
81 ADV7604,
82 ADV7611,
83};
84
85struct adv7604_reg_seq {
86 unsigned int reg;
87 u8 val;
88};
89
90struct adv7604_format_info {
91 enum v4l2_mbus_pixelcode code;
92 u8 op_ch_sel;
93 bool rgb_out;
94 bool swap_cb_cr;
95 u8 op_format_sel;
96};
97
98struct adv7604_chip_info {
99 enum adv7604_type type;
100
101 bool has_afe;
102 unsigned int max_port;
103 unsigned int num_dv_ports;
104
105 unsigned int edid_enable_reg;
106 unsigned int edid_status_reg;
107 unsigned int lcf_reg;
108
109 unsigned int cable_det_mask;
110 unsigned int tdms_lock_mask;
111 unsigned int fmt_change_digital_mask;
112
113 const struct adv7604_format_info *formats;
114 unsigned int nformats;
115
116 void (*set_termination)(struct v4l2_subdev *sd, bool enable);
117 void (*setup_irqs)(struct v4l2_subdev *sd);
118 unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
119 unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
120
121 /* 0 = AFE, 1 = HDMI */
122 const struct adv7604_reg_seq *recommended_settings[2];
123 unsigned int num_recommended_settings[2];
124
125 unsigned long page_mask;
126};
127
56/* 128/*
57 ********************************************************************** 129 **********************************************************************
58 * 130 *
@@ -60,13 +132,24 @@ MODULE_LICENSE("GPL");
60 * 132 *
61 ********************************************************************** 133 **********************************************************************
62 */ 134 */
135
63struct adv7604_state { 136struct adv7604_state {
137 const struct adv7604_chip_info *info;
64 struct adv7604_platform_data pdata; 138 struct adv7604_platform_data pdata;
139
140 struct gpio_desc *hpd_gpio[4];
141
65 struct v4l2_subdev sd; 142 struct v4l2_subdev sd;
66 struct media_pad pad; 143 struct media_pad pads[ADV7604_PAD_MAX];
144 unsigned int source_pad;
145
67 struct v4l2_ctrl_handler hdl; 146 struct v4l2_ctrl_handler hdl;
68 enum adv7604_input_port selected_input; 147
148 enum adv7604_pad selected_input;
149
69 struct v4l2_dv_timings timings; 150 struct v4l2_dv_timings timings;
151 const struct adv7604_format_info *format;
152
70 struct { 153 struct {
71 u8 edid[256]; 154 u8 edid[256];
72 u32 present; 155 u32 present;
@@ -80,18 +163,7 @@ struct adv7604_state {
80 bool restart_stdi_once; 163 bool restart_stdi_once;
81 164
82 /* i2c clients */ 165 /* i2c clients */
83 struct i2c_client *i2c_avlink; 166 struct i2c_client *i2c_clients[ADV7604_PAGE_MAX];
84 struct i2c_client *i2c_cec;
85 struct i2c_client *i2c_infoframe;
86 struct i2c_client *i2c_esdp;
87 struct i2c_client *i2c_dpp;
88 struct i2c_client *i2c_afe;
89 struct i2c_client *i2c_repeater;
90 struct i2c_client *i2c_edid;
91 struct i2c_client *i2c_hdmi;
92 struct i2c_client *i2c_test;
93 struct i2c_client *i2c_cp;
94 struct i2c_client *i2c_vdp;
95 167
96 /* controls */ 168 /* controls */
97 struct v4l2_ctrl *detect_tx_5v_ctrl; 169 struct v4l2_ctrl *detect_tx_5v_ctrl;
@@ -101,6 +173,11 @@ struct adv7604_state {
101 struct v4l2_ctrl *rgb_quantization_range_ctrl; 173 struct v4l2_ctrl *rgb_quantization_range_ctrl;
102}; 174};
103 175
176static bool adv7604_has_afe(struct adv7604_state *state)
177{
178 return state->info->has_afe;
179}
180
104/* Supported CEA and DMT timings */ 181/* Supported CEA and DMT timings */
105static const struct v4l2_dv_timings adv7604_timings[] = { 182static const struct v4l2_dv_timings adv7604_timings[] = {
106 V4L2_DV_BT_CEA_720X480P59_94, 183 V4L2_DV_BT_CEA_720X480P59_94,
@@ -256,11 +333,6 @@ static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
256 return container_of(sd, struct adv7604_state, sd); 333 return container_of(sd, struct adv7604_state, sd);
257} 334}
258 335
259static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
260{
261 return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
262}
263
264static inline unsigned hblanking(const struct v4l2_bt_timings *t) 336static inline unsigned hblanking(const struct v4l2_bt_timings *t)
265{ 337{
266 return V4L2_DV_BT_BLANKING_WIDTH(t); 338 return V4L2_DV_BT_BLANKING_WIDTH(t);
@@ -298,14 +370,18 @@ static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
298 return -EIO; 370 return -EIO;
299} 371}
300 372
301static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command) 373static s32 adv_smbus_read_byte_data(struct adv7604_state *state,
374 enum adv7604_page page, u8 command)
302{ 375{
303 return adv_smbus_read_byte_data_check(client, command, true); 376 return adv_smbus_read_byte_data_check(state->i2c_clients[page],
377 command, true);
304} 378}
305 379
306static s32 adv_smbus_write_byte_data(struct i2c_client *client, 380static s32 adv_smbus_write_byte_data(struct adv7604_state *state,
307 u8 command, u8 value) 381 enum adv7604_page page, u8 command,
382 u8 value)
308{ 383{
384 struct i2c_client *client = state->i2c_clients[page];
309 union i2c_smbus_data data; 385 union i2c_smbus_data data;
310 int err; 386 int err;
311 int i; 387 int i;
@@ -325,9 +401,11 @@ static s32 adv_smbus_write_byte_data(struct i2c_client *client,
325 return err; 401 return err;
326} 402}
327 403
328static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client, 404static s32 adv_smbus_write_i2c_block_data(struct adv7604_state *state,
329 u8 command, unsigned length, const u8 *values) 405 enum adv7604_page page, u8 command,
406 unsigned length, const u8 *values)
330{ 407{
408 struct i2c_client *client = state->i2c_clients[page];
331 union i2c_smbus_data data; 409 union i2c_smbus_data data;
332 410
333 if (length > I2C_SMBUS_BLOCK_MAX) 411 if (length > I2C_SMBUS_BLOCK_MAX)
@@ -343,149 +421,150 @@ static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
343 421
344static inline int io_read(struct v4l2_subdev *sd, u8 reg) 422static inline int io_read(struct v4l2_subdev *sd, u8 reg)
345{ 423{
346 struct i2c_client *client = v4l2_get_subdevdata(sd); 424 struct adv7604_state *state = to_state(sd);
347 425
348 return adv_smbus_read_byte_data(client, reg); 426 return adv_smbus_read_byte_data(state, ADV7604_PAGE_IO, reg);
349} 427}
350 428
351static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val) 429static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
352{ 430{
353 struct i2c_client *client = v4l2_get_subdevdata(sd); 431 struct adv7604_state *state = to_state(sd);
354 432
355 return adv_smbus_write_byte_data(client, reg, val); 433 return adv_smbus_write_byte_data(state, ADV7604_PAGE_IO, reg, val);
356} 434}
357 435
358static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val) 436static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
359{ 437{
360 return io_write(sd, reg, (io_read(sd, reg) & mask) | val); 438 return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
361} 439}
362 440
363static inline int avlink_read(struct v4l2_subdev *sd, u8 reg) 441static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
364{ 442{
365 struct adv7604_state *state = to_state(sd); 443 struct adv7604_state *state = to_state(sd);
366 444
367 return adv_smbus_read_byte_data(state->i2c_avlink, reg); 445 return adv_smbus_read_byte_data(state, ADV7604_PAGE_AVLINK, reg);
368} 446}
369 447
370static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val) 448static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
371{ 449{
372 struct adv7604_state *state = to_state(sd); 450 struct adv7604_state *state = to_state(sd);
373 451
374 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val); 452 return adv_smbus_write_byte_data(state, ADV7604_PAGE_AVLINK, reg, val);
375} 453}
376 454
377static inline int cec_read(struct v4l2_subdev *sd, u8 reg) 455static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
378{ 456{
379 struct adv7604_state *state = to_state(sd); 457 struct adv7604_state *state = to_state(sd);
380 458
381 return adv_smbus_read_byte_data(state->i2c_cec, reg); 459 return adv_smbus_read_byte_data(state, ADV7604_PAGE_CEC, reg);
382} 460}
383 461
384static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val) 462static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
385{ 463{
386 struct adv7604_state *state = to_state(sd); 464 struct adv7604_state *state = to_state(sd);
387 465
388 return adv_smbus_write_byte_data(state->i2c_cec, reg, val); 466 return adv_smbus_write_byte_data(state, ADV7604_PAGE_CEC, reg, val);
389} 467}
390 468
391static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val) 469static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
392{ 470{
393 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val); 471 return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
394} 472}
395 473
396static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg) 474static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
397{ 475{
398 struct adv7604_state *state = to_state(sd); 476 struct adv7604_state *state = to_state(sd);
399 477
400 return adv_smbus_read_byte_data(state->i2c_infoframe, reg); 478 return adv_smbus_read_byte_data(state, ADV7604_PAGE_INFOFRAME, reg);
401} 479}
402 480
403static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val) 481static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
404{ 482{
405 struct adv7604_state *state = to_state(sd); 483 struct adv7604_state *state = to_state(sd);
406 484
407 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val); 485 return adv_smbus_write_byte_data(state, ADV7604_PAGE_INFOFRAME,
486 reg, val);
408} 487}
409 488
410static inline int esdp_read(struct v4l2_subdev *sd, u8 reg) 489static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
411{ 490{
412 struct adv7604_state *state = to_state(sd); 491 struct adv7604_state *state = to_state(sd);
413 492
414 return adv_smbus_read_byte_data(state->i2c_esdp, reg); 493 return adv_smbus_read_byte_data(state, ADV7604_PAGE_ESDP, reg);
415} 494}
416 495
417static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val) 496static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
418{ 497{
419 struct adv7604_state *state = to_state(sd); 498 struct adv7604_state *state = to_state(sd);
420 499
421 return adv_smbus_write_byte_data(state->i2c_esdp, reg, val); 500 return adv_smbus_write_byte_data(state, ADV7604_PAGE_ESDP, reg, val);
422} 501}
423 502
424static inline int dpp_read(struct v4l2_subdev *sd, u8 reg) 503static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
425{ 504{
426 struct adv7604_state *state = to_state(sd); 505 struct adv7604_state *state = to_state(sd);
427 506
428 return adv_smbus_read_byte_data(state->i2c_dpp, reg); 507 return adv_smbus_read_byte_data(state, ADV7604_PAGE_DPP, reg);
429} 508}
430 509
431static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val) 510static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
432{ 511{
433 struct adv7604_state *state = to_state(sd); 512 struct adv7604_state *state = to_state(sd);
434 513
435 return adv_smbus_write_byte_data(state->i2c_dpp, reg, val); 514 return adv_smbus_write_byte_data(state, ADV7604_PAGE_DPP, reg, val);
436} 515}
437 516
438static inline int afe_read(struct v4l2_subdev *sd, u8 reg) 517static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
439{ 518{
440 struct adv7604_state *state = to_state(sd); 519 struct adv7604_state *state = to_state(sd);
441 520
442 return adv_smbus_read_byte_data(state->i2c_afe, reg); 521 return adv_smbus_read_byte_data(state, ADV7604_PAGE_AFE, reg);
443} 522}
444 523
445static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val) 524static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
446{ 525{
447 struct adv7604_state *state = to_state(sd); 526 struct adv7604_state *state = to_state(sd);
448 527
449 return adv_smbus_write_byte_data(state->i2c_afe, reg, val); 528 return adv_smbus_write_byte_data(state, ADV7604_PAGE_AFE, reg, val);
450} 529}
451 530
452static inline int rep_read(struct v4l2_subdev *sd, u8 reg) 531static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
453{ 532{
454 struct adv7604_state *state = to_state(sd); 533 struct adv7604_state *state = to_state(sd);
455 534
456 return adv_smbus_read_byte_data(state->i2c_repeater, reg); 535 return adv_smbus_read_byte_data(state, ADV7604_PAGE_REP, reg);
457} 536}
458 537
459static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val) 538static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
460{ 539{
461 struct adv7604_state *state = to_state(sd); 540 struct adv7604_state *state = to_state(sd);
462 541
463 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val); 542 return adv_smbus_write_byte_data(state, ADV7604_PAGE_REP, reg, val);
464} 543}
465 544
466static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val) 545static inline int rep_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
467{ 546{
468 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val); 547 return rep_write(sd, reg, (rep_read(sd, reg) & ~mask) | val);
469} 548}
470 549
471static inline int edid_read(struct v4l2_subdev *sd, u8 reg) 550static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
472{ 551{
473 struct adv7604_state *state = to_state(sd); 552 struct adv7604_state *state = to_state(sd);
474 553
475 return adv_smbus_read_byte_data(state->i2c_edid, reg); 554 return adv_smbus_read_byte_data(state, ADV7604_PAGE_EDID, reg);
476} 555}
477 556
478static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val) 557static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
479{ 558{
480 struct adv7604_state *state = to_state(sd); 559 struct adv7604_state *state = to_state(sd);
481 560
482 return adv_smbus_write_byte_data(state->i2c_edid, reg, val); 561 return adv_smbus_write_byte_data(state, ADV7604_PAGE_EDID, reg, val);
483} 562}
484 563
485static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val) 564static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
486{ 565{
487 struct adv7604_state *state = to_state(sd); 566 struct adv7604_state *state = to_state(sd);
488 struct i2c_client *client = state->i2c_edid; 567 struct i2c_client *client = state->i2c_clients[ADV7604_PAGE_EDID];
489 u8 msgbuf0[1] = { 0 }; 568 u8 msgbuf0[1] = { 0 };
490 u8 msgbuf1[256]; 569 u8 msgbuf1[256];
491 struct i2c_msg msg[2] = { 570 struct i2c_msg msg[2] = {
@@ -518,11 +597,25 @@ static inline int edid_write_block(struct v4l2_subdev *sd,
518 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len); 597 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
519 598
520 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX) 599 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
521 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i, 600 err = adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_EDID,
522 I2C_SMBUS_BLOCK_MAX, val + i); 601 i, I2C_SMBUS_BLOCK_MAX, val + i);
523 return err; 602 return err;
524} 603}
525 604
605static void adv7604_set_hpd(struct adv7604_state *state, unsigned int hpd)
606{
607 unsigned int i;
608
609 for (i = 0; i < state->info->num_dv_ports; ++i) {
610 if (IS_ERR(state->hpd_gpio[i]))
611 continue;
612
613 gpiod_set_value_cansleep(state->hpd_gpio[i], hpd & BIT(i));
614 }
615
616 v4l2_subdev_notify(&state->sd, ADV7604_HOTPLUG, &hpd);
617}
618
526static void adv7604_delayed_work_enable_hotplug(struct work_struct *work) 619static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
527{ 620{
528 struct delayed_work *dwork = to_delayed_work(work); 621 struct delayed_work *dwork = to_delayed_work(work);
@@ -532,73 +625,210 @@ static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
532 625
533 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__); 626 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
534 627
535 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present); 628 adv7604_set_hpd(state, state->edid.present);
536} 629}
537 630
538static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg) 631static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
539{ 632{
540 struct adv7604_state *state = to_state(sd); 633 struct adv7604_state *state = to_state(sd);
541 634
542 return adv_smbus_read_byte_data(state->i2c_hdmi, reg); 635 return adv_smbus_read_byte_data(state, ADV7604_PAGE_HDMI, reg);
636}
637
638static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
639{
640 return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
543} 641}
544 642
545static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val) 643static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
546{ 644{
547 struct adv7604_state *state = to_state(sd); 645 struct adv7604_state *state = to_state(sd);
548 646
549 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val); 647 return adv_smbus_write_byte_data(state, ADV7604_PAGE_HDMI, reg, val);
550} 648}
551 649
552static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val) 650static inline int hdmi_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
553{ 651{
554 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val); 652 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & ~mask) | val);
555} 653}
556 654
557static inline int test_read(struct v4l2_subdev *sd, u8 reg) 655static inline int test_read(struct v4l2_subdev *sd, u8 reg)
558{ 656{
559 struct adv7604_state *state = to_state(sd); 657 struct adv7604_state *state = to_state(sd);
560 658
561 return adv_smbus_read_byte_data(state->i2c_test, reg); 659 return adv_smbus_read_byte_data(state, ADV7604_PAGE_TEST, reg);
562} 660}
563 661
564static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val) 662static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
565{ 663{
566 struct adv7604_state *state = to_state(sd); 664 struct adv7604_state *state = to_state(sd);
567 665
568 return adv_smbus_write_byte_data(state->i2c_test, reg, val); 666 return adv_smbus_write_byte_data(state, ADV7604_PAGE_TEST, reg, val);
569} 667}
570 668
571static inline int cp_read(struct v4l2_subdev *sd, u8 reg) 669static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
572{ 670{
573 struct adv7604_state *state = to_state(sd); 671 struct adv7604_state *state = to_state(sd);
574 672
575 return adv_smbus_read_byte_data(state->i2c_cp, reg); 673 return adv_smbus_read_byte_data(state, ADV7604_PAGE_CP, reg);
674}
675
676static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
677{
678 return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
576} 679}
577 680
578static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val) 681static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
579{ 682{
580 struct adv7604_state *state = to_state(sd); 683 struct adv7604_state *state = to_state(sd);
581 684
582 return adv_smbus_write_byte_data(state->i2c_cp, reg, val); 685 return adv_smbus_write_byte_data(state, ADV7604_PAGE_CP, reg, val);
583} 686}
584 687
585static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val) 688static inline int cp_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
586{ 689{
587 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val); 690 return cp_write(sd, reg, (cp_read(sd, reg) & ~mask) | val);
588} 691}
589 692
590static inline int vdp_read(struct v4l2_subdev *sd, u8 reg) 693static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
591{ 694{
592 struct adv7604_state *state = to_state(sd); 695 struct adv7604_state *state = to_state(sd);
593 696
594 return adv_smbus_read_byte_data(state->i2c_vdp, reg); 697 return adv_smbus_read_byte_data(state, ADV7604_PAGE_VDP, reg);
595} 698}
596 699
597static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val) 700static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
598{ 701{
599 struct adv7604_state *state = to_state(sd); 702 struct adv7604_state *state = to_state(sd);
600 703
601 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val); 704 return adv_smbus_write_byte_data(state, ADV7604_PAGE_VDP, reg, val);
705}
706
707#define ADV7604_REG(page, offset) (((page) << 8) | (offset))
708#define ADV7604_REG_SEQ_TERM 0xffff
709
710#ifdef CONFIG_VIDEO_ADV_DEBUG
711static int adv7604_read_reg(struct v4l2_subdev *sd, unsigned int reg)
712{
713 struct adv7604_state *state = to_state(sd);
714 unsigned int page = reg >> 8;
715
716 if (!(BIT(page) & state->info->page_mask))
717 return -EINVAL;
718
719 reg &= 0xff;
720
721 return adv_smbus_read_byte_data(state, page, reg);
722}
723#endif
724
725static int adv7604_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
726{
727 struct adv7604_state *state = to_state(sd);
728 unsigned int page = reg >> 8;
729
730 if (!(BIT(page) & state->info->page_mask))
731 return -EINVAL;
732
733 reg &= 0xff;
734
735 return adv_smbus_write_byte_data(state, page, reg, val);
736}
737
738static void adv7604_write_reg_seq(struct v4l2_subdev *sd,
739 const struct adv7604_reg_seq *reg_seq)
740{
741 unsigned int i;
742
743 for (i = 0; reg_seq[i].reg != ADV7604_REG_SEQ_TERM; i++)
744 adv7604_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
745}
746
747/* -----------------------------------------------------------------------------
748 * Format helpers
749 */
750
751static const struct adv7604_format_info adv7604_formats[] = {
752 { V4L2_MBUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
753 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
754 { V4L2_MBUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
755 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
756 { V4L2_MBUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
757 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
758 { V4L2_MBUS_FMT_YUYV10_2X10, ADV7604_OP_CH_SEL_RGB, false, false,
759 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
760 { V4L2_MBUS_FMT_YVYU10_2X10, ADV7604_OP_CH_SEL_RGB, false, true,
761 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
762 { V4L2_MBUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
763 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
764 { V4L2_MBUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
765 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
766 { V4L2_MBUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
767 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
768 { V4L2_MBUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
769 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
770 { V4L2_MBUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
771 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
772 { V4L2_MBUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
773 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
774 { V4L2_MBUS_FMT_UYVY10_1X20, ADV7604_OP_CH_SEL_RBG, false, false,
775 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
776 { V4L2_MBUS_FMT_VYUY10_1X20, ADV7604_OP_CH_SEL_RBG, false, true,
777 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
778 { V4L2_MBUS_FMT_YUYV10_1X20, ADV7604_OP_CH_SEL_RGB, false, false,
779 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
780 { V4L2_MBUS_FMT_YVYU10_1X20, ADV7604_OP_CH_SEL_RGB, false, true,
781 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
782 { V4L2_MBUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
783 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
784 { V4L2_MBUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
785 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
786 { V4L2_MBUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
787 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
788 { V4L2_MBUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
789 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
790};
791
792static const struct adv7604_format_info adv7611_formats[] = {
793 { V4L2_MBUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
794 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
795 { V4L2_MBUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
796 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
797 { V4L2_MBUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
798 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
799 { V4L2_MBUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
800 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
801 { V4L2_MBUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
802 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
803 { V4L2_MBUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
804 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
805 { V4L2_MBUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
806 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
807 { V4L2_MBUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
808 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
809 { V4L2_MBUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
810 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
811 { V4L2_MBUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
812 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
813 { V4L2_MBUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
814 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
815 { V4L2_MBUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
816 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
817 { V4L2_MBUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
818 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
819};
820
821static const struct adv7604_format_info *
822adv7604_format_info(struct adv7604_state *state, enum v4l2_mbus_pixelcode code)
823{
824 unsigned int i;
825
826 for (i = 0; i < state->info->nformats; ++i) {
827 if (state->info->formats[i].code == code)
828 return &state->info->formats[i];
829 }
830
831 return NULL;
602} 832}
603 833
604/* ----------------------------------------------------------------------- */ 834/* ----------------------------------------------------------------------- */
@@ -607,18 +837,18 @@ static inline bool is_analog_input(struct v4l2_subdev *sd)
607{ 837{
608 struct adv7604_state *state = to_state(sd); 838 struct adv7604_state *state = to_state(sd);
609 839
610 return state->selected_input == ADV7604_INPUT_VGA_RGB || 840 return state->selected_input == ADV7604_PAD_VGA_RGB ||
611 state->selected_input == ADV7604_INPUT_VGA_COMP; 841 state->selected_input == ADV7604_PAD_VGA_COMP;
612} 842}
613 843
614static inline bool is_digital_input(struct v4l2_subdev *sd) 844static inline bool is_digital_input(struct v4l2_subdev *sd)
615{ 845{
616 struct adv7604_state *state = to_state(sd); 846 struct adv7604_state *state = to_state(sd);
617 847
618 return state->selected_input == ADV7604_INPUT_HDMI_PORT_A || 848 return state->selected_input == ADV7604_PAD_HDMI_PORT_A ||
619 state->selected_input == ADV7604_INPUT_HDMI_PORT_B || 849 state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
620 state->selected_input == ADV7604_INPUT_HDMI_PORT_C || 850 state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
621 state->selected_input == ADV7604_INPUT_HDMI_PORT_D; 851 state->selected_input == ADV7604_PAD_HDMI_PORT_D;
622} 852}
623 853
624/* ----------------------------------------------------------------------- */ 854/* ----------------------------------------------------------------------- */
@@ -644,119 +874,61 @@ static void adv7604_inv_register(struct v4l2_subdev *sd)
644static int adv7604_g_register(struct v4l2_subdev *sd, 874static int adv7604_g_register(struct v4l2_subdev *sd,
645 struct v4l2_dbg_register *reg) 875 struct v4l2_dbg_register *reg)
646{ 876{
647 reg->size = 1; 877 int ret;
648 switch (reg->reg >> 8) { 878
649 case 0: 879 ret = adv7604_read_reg(sd, reg->reg);
650 reg->val = io_read(sd, reg->reg & 0xff); 880 if (ret < 0) {
651 break;
652 case 1:
653 reg->val = avlink_read(sd, reg->reg & 0xff);
654 break;
655 case 2:
656 reg->val = cec_read(sd, reg->reg & 0xff);
657 break;
658 case 3:
659 reg->val = infoframe_read(sd, reg->reg & 0xff);
660 break;
661 case 4:
662 reg->val = esdp_read(sd, reg->reg & 0xff);
663 break;
664 case 5:
665 reg->val = dpp_read(sd, reg->reg & 0xff);
666 break;
667 case 6:
668 reg->val = afe_read(sd, reg->reg & 0xff);
669 break;
670 case 7:
671 reg->val = rep_read(sd, reg->reg & 0xff);
672 break;
673 case 8:
674 reg->val = edid_read(sd, reg->reg & 0xff);
675 break;
676 case 9:
677 reg->val = hdmi_read(sd, reg->reg & 0xff);
678 break;
679 case 0xa:
680 reg->val = test_read(sd, reg->reg & 0xff);
681 break;
682 case 0xb:
683 reg->val = cp_read(sd, reg->reg & 0xff);
684 break;
685 case 0xc:
686 reg->val = vdp_read(sd, reg->reg & 0xff);
687 break;
688 default:
689 v4l2_info(sd, "Register %03llx not supported\n", reg->reg); 881 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
690 adv7604_inv_register(sd); 882 adv7604_inv_register(sd);
691 break; 883 return ret;
692 } 884 }
885
886 reg->size = 1;
887 reg->val = ret;
888
693 return 0; 889 return 0;
694} 890}
695 891
696static int adv7604_s_register(struct v4l2_subdev *sd, 892static int adv7604_s_register(struct v4l2_subdev *sd,
697 const struct v4l2_dbg_register *reg) 893 const struct v4l2_dbg_register *reg)
698{ 894{
699 u8 val = reg->val & 0xff; 895 int ret;
700 896
701 switch (reg->reg >> 8) { 897 ret = adv7604_write_reg(sd, reg->reg, reg->val);
702 case 0: 898 if (ret < 0) {
703 io_write(sd, reg->reg & 0xff, val);
704 break;
705 case 1:
706 avlink_write(sd, reg->reg & 0xff, val);
707 break;
708 case 2:
709 cec_write(sd, reg->reg & 0xff, val);
710 break;
711 case 3:
712 infoframe_write(sd, reg->reg & 0xff, val);
713 break;
714 case 4:
715 esdp_write(sd, reg->reg & 0xff, val);
716 break;
717 case 5:
718 dpp_write(sd, reg->reg & 0xff, val);
719 break;
720 case 6:
721 afe_write(sd, reg->reg & 0xff, val);
722 break;
723 case 7:
724 rep_write(sd, reg->reg & 0xff, val);
725 break;
726 case 8:
727 edid_write(sd, reg->reg & 0xff, val);
728 break;
729 case 9:
730 hdmi_write(sd, reg->reg & 0xff, val);
731 break;
732 case 0xa:
733 test_write(sd, reg->reg & 0xff, val);
734 break;
735 case 0xb:
736 cp_write(sd, reg->reg & 0xff, val);
737 break;
738 case 0xc:
739 vdp_write(sd, reg->reg & 0xff, val);
740 break;
741 default:
742 v4l2_info(sd, "Register %03llx not supported\n", reg->reg); 899 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
743 adv7604_inv_register(sd); 900 adv7604_inv_register(sd);
744 break; 901 return ret;
745 } 902 }
903
746 return 0; 904 return 0;
747} 905}
748#endif 906#endif
749 907
908static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
909{
910 u8 value = io_read(sd, 0x6f);
911
912 return ((value & 0x10) >> 4)
913 | ((value & 0x08) >> 2)
914 | ((value & 0x04) << 0)
915 | ((value & 0x02) << 2);
916}
917
918static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
919{
920 u8 value = io_read(sd, 0x6f);
921
922 return value & 1;
923}
924
750static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd) 925static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
751{ 926{
752 struct adv7604_state *state = to_state(sd); 927 struct adv7604_state *state = to_state(sd);
753 u8 reg_io_6f = io_read(sd, 0x6f); 928 const struct adv7604_chip_info *info = state->info;
754 929
755 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, 930 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
756 ((reg_io_6f & 0x10) >> 4) | 931 info->read_cable_det(sd));
757 ((reg_io_6f & 0x08) >> 2) |
758 (reg_io_6f & 0x04) |
759 ((reg_io_6f & 0x02) << 2));
760} 932}
761 933
762static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd, 934static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
@@ -787,11 +959,13 @@ static int configure_predefined_video_timings(struct v4l2_subdev *sd,
787 959
788 v4l2_dbg(1, debug, sd, "%s", __func__); 960 v4l2_dbg(1, debug, sd, "%s", __func__);
789 961
790 /* reset to default values */ 962 if (adv7604_has_afe(state)) {
791 io_write(sd, 0x16, 0x43); 963 /* reset to default values */
792 io_write(sd, 0x17, 0x5a); 964 io_write(sd, 0x16, 0x43);
965 io_write(sd, 0x17, 0x5a);
966 }
793 /* disable embedded syncs for auto graphics mode */ 967 /* disable embedded syncs for auto graphics mode */
794 cp_write_and_or(sd, 0x81, 0xef, 0x00); 968 cp_write_clr_set(sd, 0x81, 0x10, 0x00);
795 cp_write(sd, 0x8f, 0x00); 969 cp_write(sd, 0x8f, 0x00);
796 cp_write(sd, 0x90, 0x00); 970 cp_write(sd, 0x90, 0x00);
797 cp_write(sd, 0xa2, 0x00); 971 cp_write(sd, 0xa2, 0x00);
@@ -829,7 +1003,6 @@ static void configure_custom_video_timings(struct v4l2_subdev *sd,
829 const struct v4l2_bt_timings *bt) 1003 const struct v4l2_bt_timings *bt)
830{ 1004{
831 struct adv7604_state *state = to_state(sd); 1005 struct adv7604_state *state = to_state(sd);
832 struct i2c_client *client = v4l2_get_subdevdata(sd);
833 u32 width = htotal(bt); 1006 u32 width = htotal(bt);
834 u32 height = vtotal(bt); 1007 u32 height = vtotal(bt);
835 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4; 1008 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
@@ -850,12 +1023,13 @@ static void configure_custom_video_timings(struct v4l2_subdev *sd,
850 io_write(sd, 0x00, 0x07); /* video std */ 1023 io_write(sd, 0x00, 0x07); /* video std */
851 io_write(sd, 0x01, 0x02); /* prim mode */ 1024 io_write(sd, 0x01, 0x02); /* prim mode */
852 /* enable embedded syncs for auto graphics mode */ 1025 /* enable embedded syncs for auto graphics mode */
853 cp_write_and_or(sd, 0x81, 0xef, 0x10); 1026 cp_write_clr_set(sd, 0x81, 0x10, 0x10);
854 1027
855 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */ 1028 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
856 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */ 1029 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
857 /* IO-map reg. 0x16 and 0x17 should be written in sequence */ 1030 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
858 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) 1031 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_IO,
1032 0x16, 2, pll))
859 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n"); 1033 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
860 1034
861 /* active video - horizontal timing */ 1035 /* active video - horizontal timing */
@@ -906,7 +1080,8 @@ static void adv7604_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 off
906 offset_buf[3] = offset_c & 0x0ff; 1080 offset_buf[3] = offset_c & 0x0ff;
907 1081
908 /* Registers must be written in this order with no i2c access in between */ 1082 /* Registers must be written in this order with no i2c access in between */
909 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf)) 1083 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_CP,
1084 0x77, 4, offset_buf))
910 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__); 1085 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
911} 1086}
912 1087
@@ -935,7 +1110,8 @@ static void adv7604_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a,
935 gain_buf[3] = ((gain_c & 0x0ff)); 1110 gain_buf[3] = ((gain_c & 0x0ff));
936 1111
937 /* Registers must be written in this order with no i2c access in between */ 1112 /* Registers must be written in this order with no i2c access in between */
938 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf)) 1113 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_CP,
1114 0x73, 4, gain_buf))
939 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__); 1115 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
940} 1116}
941 1117
@@ -954,24 +1130,24 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
954 1130
955 switch (state->rgb_quantization_range) { 1131 switch (state->rgb_quantization_range) {
956 case V4L2_DV_RGB_RANGE_AUTO: 1132 case V4L2_DV_RGB_RANGE_AUTO:
957 if (state->selected_input == ADV7604_INPUT_VGA_RGB) { 1133 if (state->selected_input == ADV7604_PAD_VGA_RGB) {
958 /* Receiving analog RGB signal 1134 /* Receiving analog RGB signal
959 * Set RGB full range (0-255) */ 1135 * Set RGB full range (0-255) */
960 io_write_and_or(sd, 0x02, 0x0f, 0x10); 1136 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
961 break; 1137 break;
962 } 1138 }
963 1139
964 if (state->selected_input == ADV7604_INPUT_VGA_COMP) { 1140 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
965 /* Receiving analog YPbPr signal 1141 /* Receiving analog YPbPr signal
966 * Set automode */ 1142 * Set automode */
967 io_write_and_or(sd, 0x02, 0x0f, 0xf0); 1143 io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
968 break; 1144 break;
969 } 1145 }
970 1146
971 if (hdmi_signal) { 1147 if (hdmi_signal) {
972 /* Receiving HDMI signal 1148 /* Receiving HDMI signal
973 * Set automode */ 1149 * Set automode */
974 io_write_and_or(sd, 0x02, 0x0f, 0xf0); 1150 io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
975 break; 1151 break;
976 } 1152 }
977 1153
@@ -980,10 +1156,10 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
980 * input format (CE/IT) in automatic mode */ 1156 * input format (CE/IT) in automatic mode */
981 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) { 1157 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
982 /* RGB limited range (16-235) */ 1158 /* RGB limited range (16-235) */
983 io_write_and_or(sd, 0x02, 0x0f, 0x00); 1159 io_write_clr_set(sd, 0x02, 0xf0, 0x00);
984 } else { 1160 } else {
985 /* RGB full range (0-255) */ 1161 /* RGB full range (0-255) */
986 io_write_and_or(sd, 0x02, 0x0f, 0x10); 1162 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
987 1163
988 if (is_digital_input(sd) && rgb_output) { 1164 if (is_digital_input(sd) && rgb_output) {
989 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40); 1165 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
@@ -994,25 +1170,25 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
994 } 1170 }
995 break; 1171 break;
996 case V4L2_DV_RGB_RANGE_LIMITED: 1172 case V4L2_DV_RGB_RANGE_LIMITED:
997 if (state->selected_input == ADV7604_INPUT_VGA_COMP) { 1173 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
998 /* YCrCb limited range (16-235) */ 1174 /* YCrCb limited range (16-235) */
999 io_write_and_or(sd, 0x02, 0x0f, 0x20); 1175 io_write_clr_set(sd, 0x02, 0xf0, 0x20);
1000 break; 1176 break;
1001 } 1177 }
1002 1178
1003 /* RGB limited range (16-235) */ 1179 /* RGB limited range (16-235) */
1004 io_write_and_or(sd, 0x02, 0x0f, 0x00); 1180 io_write_clr_set(sd, 0x02, 0xf0, 0x00);
1005 1181
1006 break; 1182 break;
1007 case V4L2_DV_RGB_RANGE_FULL: 1183 case V4L2_DV_RGB_RANGE_FULL:
1008 if (state->selected_input == ADV7604_INPUT_VGA_COMP) { 1184 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
1009 /* YCrCb full range (0-255) */ 1185 /* YCrCb full range (0-255) */
1010 io_write_and_or(sd, 0x02, 0x0f, 0x60); 1186 io_write_clr_set(sd, 0x02, 0xf0, 0x60);
1011 break; 1187 break;
1012 } 1188 }
1013 1189
1014 /* RGB full range (0-255) */ 1190 /* RGB full range (0-255) */
1015 io_write_and_or(sd, 0x02, 0x0f, 0x10); 1191 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
1016 1192
1017 if (is_analog_input(sd) || hdmi_signal) 1193 if (is_analog_input(sd) || hdmi_signal)
1018 break; 1194 break;
@@ -1030,7 +1206,9 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1030 1206
1031static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl) 1207static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1032{ 1208{
1033 struct v4l2_subdev *sd = to_sd(ctrl); 1209 struct v4l2_subdev *sd =
1210 &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
1211
1034 struct adv7604_state *state = to_state(sd); 1212 struct adv7604_state *state = to_state(sd);
1035 1213
1036 switch (ctrl->id) { 1214 switch (ctrl->id) {
@@ -1051,6 +1229,8 @@ static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1051 set_rgb_quantization_range(sd); 1229 set_rgb_quantization_range(sd);
1052 return 0; 1230 return 0;
1053 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE: 1231 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
1232 if (!adv7604_has_afe(state))
1233 return -EINVAL;
1054 /* Set the analog sampling phase. This is needed to find the 1234 /* Set the analog sampling phase. This is needed to find the
1055 best sampling phase for analog video: an application or 1235 best sampling phase for analog video: an application or
1056 driver has to try a number of phases and analyze the picture 1236 driver has to try a number of phases and analyze the picture
@@ -1060,7 +1240,7 @@ static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1060 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL: 1240 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1061 /* Use the default blue color for free running mode, 1241 /* Use the default blue color for free running mode,
1062 or supply your own. */ 1242 or supply your own. */
1063 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2)); 1243 cp_write_clr_set(sd, 0xbf, 0x04, ctrl->val << 2);
1064 return 0; 1244 return 0;
1065 case V4L2_CID_ADV_RX_FREE_RUN_COLOR: 1245 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
1066 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16); 1246 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
@@ -1088,7 +1268,10 @@ static inline bool no_signal_tmds(struct v4l2_subdev *sd)
1088 1268
1089static inline bool no_lock_tmds(struct v4l2_subdev *sd) 1269static inline bool no_lock_tmds(struct v4l2_subdev *sd)
1090{ 1270{
1091 return (io_read(sd, 0x6a) & 0xe0) != 0xe0; 1271 struct adv7604_state *state = to_state(sd);
1272 const struct adv7604_chip_info *info = state->info;
1273
1274 return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
1092} 1275}
1093 1276
1094static inline bool is_hdmi(struct v4l2_subdev *sd) 1277static inline bool is_hdmi(struct v4l2_subdev *sd)
@@ -1098,6 +1281,15 @@ static inline bool is_hdmi(struct v4l2_subdev *sd)
1098 1281
1099static inline bool no_lock_sspd(struct v4l2_subdev *sd) 1282static inline bool no_lock_sspd(struct v4l2_subdev *sd)
1100{ 1283{
1284 struct adv7604_state *state = to_state(sd);
1285
1286 /*
1287 * Chips without a AFE don't expose registers for the SSPD, so just assume
1288 * that we have a lock.
1289 */
1290 if (adv7604_has_afe(state))
1291 return false;
1292
1101 /* TODO channel 2 */ 1293 /* TODO channel 2 */
1102 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0); 1294 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
1103} 1295}
@@ -1127,6 +1319,11 @@ static inline bool no_signal(struct v4l2_subdev *sd)
1127 1319
1128static inline bool no_lock_cp(struct v4l2_subdev *sd) 1320static inline bool no_lock_cp(struct v4l2_subdev *sd)
1129{ 1321{
1322 struct adv7604_state *state = to_state(sd);
1323
1324 if (!adv7604_has_afe(state))
1325 return false;
1326
1130 /* CP has detected a non standard number of lines on the incoming 1327 /* CP has detected a non standard number of lines on the incoming
1131 video compared to what it is configured to receive by s_dv_timings */ 1328 video compared to what it is configured to receive by s_dv_timings */
1132 return io_read(sd, 0x12) & 0x01; 1329 return io_read(sd, 0x12) & 0x01;
@@ -1195,28 +1392,40 @@ static int stdi2dv_timings(struct v4l2_subdev *sd,
1195 return -1; 1392 return -1;
1196} 1393}
1197 1394
1395
1198static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi) 1396static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1199{ 1397{
1398 struct adv7604_state *state = to_state(sd);
1399 const struct adv7604_chip_info *info = state->info;
1400 u8 polarity;
1401
1200 if (no_lock_stdi(sd) || no_lock_sspd(sd)) { 1402 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1201 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__); 1403 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
1202 return -1; 1404 return -1;
1203 } 1405 }
1204 1406
1205 /* read STDI */ 1407 /* read STDI */
1206 stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2); 1408 stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
1207 stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4); 1409 stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
1208 stdi->lcvs = cp_read(sd, 0xb3) >> 3; 1410 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1209 stdi->interlaced = io_read(sd, 0x12) & 0x10; 1411 stdi->interlaced = io_read(sd, 0x12) & 0x10;
1210 1412
1211 /* read SSPD */ 1413 if (adv7604_has_afe(state)) {
1212 if ((cp_read(sd, 0xb5) & 0x03) == 0x01) { 1414 /* read SSPD */
1213 stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ? 1415 polarity = cp_read(sd, 0xb5);
1214 ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x'); 1416 if ((polarity & 0x03) == 0x01) {
1215 stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ? 1417 stdi->hs_pol = polarity & 0x10
1216 ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x'); 1418 ? (polarity & 0x08 ? '+' : '-') : 'x';
1419 stdi->vs_pol = polarity & 0x40
1420 ? (polarity & 0x20 ? '+' : '-') : 'x';
1421 } else {
1422 stdi->hs_pol = 'x';
1423 stdi->vs_pol = 'x';
1424 }
1217 } else { 1425 } else {
1218 stdi->hs_pol = 'x'; 1426 polarity = hdmi_read(sd, 0x05);
1219 stdi->vs_pol = 'x'; 1427 stdi->hs_pol = polarity & 0x20 ? '+' : '-';
1428 stdi->vs_pol = polarity & 0x10 ? '+' : '-';
1220 } 1429 }
1221 1430
1222 if (no_lock_stdi(sd) || no_lock_sspd(sd)) { 1431 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
@@ -1243,8 +1452,14 @@ static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1243static int adv7604_enum_dv_timings(struct v4l2_subdev *sd, 1452static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1244 struct v4l2_enum_dv_timings *timings) 1453 struct v4l2_enum_dv_timings *timings)
1245{ 1454{
1455 struct adv7604_state *state = to_state(sd);
1456
1246 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1) 1457 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1247 return -EINVAL; 1458 return -EINVAL;
1459
1460 if (timings->pad >= state->source_pad)
1461 return -EINVAL;
1462
1248 memset(timings->reserved, 0, sizeof(timings->reserved)); 1463 memset(timings->reserved, 0, sizeof(timings->reserved));
1249 timings->timings = adv7604_timings[timings->index]; 1464 timings->timings = adv7604_timings[timings->index];
1250 return 0; 1465 return 0;
@@ -1253,14 +1468,30 @@ static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1253static int adv7604_dv_timings_cap(struct v4l2_subdev *sd, 1468static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1254 struct v4l2_dv_timings_cap *cap) 1469 struct v4l2_dv_timings_cap *cap)
1255{ 1470{
1471 struct adv7604_state *state = to_state(sd);
1472
1473 if (cap->pad >= state->source_pad)
1474 return -EINVAL;
1475
1256 cap->type = V4L2_DV_BT_656_1120; 1476 cap->type = V4L2_DV_BT_656_1120;
1257 cap->bt.max_width = 1920; 1477 cap->bt.max_width = 1920;
1258 cap->bt.max_height = 1200; 1478 cap->bt.max_height = 1200;
1259 cap->bt.min_pixelclock = 25000000; 1479 cap->bt.min_pixelclock = 25000000;
1260 if (is_digital_input(sd)) 1480
1481 switch (cap->pad) {
1482 case ADV7604_PAD_HDMI_PORT_A:
1483 case ADV7604_PAD_HDMI_PORT_B:
1484 case ADV7604_PAD_HDMI_PORT_C:
1485 case ADV7604_PAD_HDMI_PORT_D:
1261 cap->bt.max_pixelclock = 225000000; 1486 cap->bt.max_pixelclock = 225000000;
1262 else 1487 break;
1488 case ADV7604_PAD_VGA_RGB:
1489 case ADV7604_PAD_VGA_COMP:
1490 default:
1263 cap->bt.max_pixelclock = 170000000; 1491 cap->bt.max_pixelclock = 170000000;
1492 break;
1493 }
1494
1264 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT | 1495 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1265 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT; 1496 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1266 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE | 1497 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
@@ -1284,10 +1515,43 @@ static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1284 } 1515 }
1285} 1516}
1286 1517
1518static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1519{
1520 unsigned int freq;
1521 int a, b;
1522
1523 a = hdmi_read(sd, 0x06);
1524 b = hdmi_read(sd, 0x3b);
1525 if (a < 0 || b < 0)
1526 return 0;
1527 freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
1528
1529 if (is_hdmi(sd)) {
1530 /* adjust for deep color mode */
1531 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
1532
1533 freq = freq * 8 / bits_per_channel;
1534 }
1535
1536 return freq;
1537}
1538
1539static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1540{
1541 int a, b;
1542
1543 a = hdmi_read(sd, 0x51);
1544 b = hdmi_read(sd, 0x52);
1545 if (a < 0 || b < 0)
1546 return 0;
1547 return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
1548}
1549
1287static int adv7604_query_dv_timings(struct v4l2_subdev *sd, 1550static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1288 struct v4l2_dv_timings *timings) 1551 struct v4l2_dv_timings *timings)
1289{ 1552{
1290 struct adv7604_state *state = to_state(sd); 1553 struct adv7604_state *state = to_state(sd);
1554 const struct adv7604_chip_info *info = state->info;
1291 struct v4l2_bt_timings *bt = &timings->bt; 1555 struct v4l2_bt_timings *bt = &timings->bt;
1292 struct stdi_readback stdi; 1556 struct stdi_readback stdi;
1293 1557
@@ -1311,44 +1575,25 @@ static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1311 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE; 1575 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1312 1576
1313 if (is_digital_input(sd)) { 1577 if (is_digital_input(sd)) {
1314 uint32_t freq;
1315
1316 timings->type = V4L2_DV_BT_656_1120; 1578 timings->type = V4L2_DV_BT_656_1120;
1317 1579
1318 bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08); 1580 /* FIXME: All masks are incorrect for ADV7611 */
1319 bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a); 1581 bt->width = hdmi_read16(sd, 0x07, 0xfff);
1320 freq = (hdmi_read(sd, 0x06) * 1000000) + 1582 bt->height = hdmi_read16(sd, 0x09, 0xfff);
1321 ((hdmi_read(sd, 0x3b) & 0x30) >> 4) * 250000; 1583 bt->pixelclock = info->read_hdmi_pixelclock(sd);
1322 if (is_hdmi(sd)) { 1584 bt->hfrontporch = hdmi_read16(sd, 0x20, 0x3ff);
1323 /* adjust for deep color mode */ 1585 bt->hsync = hdmi_read16(sd, 0x22, 0x3ff);
1324 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8; 1586 bt->hbackporch = hdmi_read16(sd, 0x24, 0x3ff);
1325 1587 bt->vfrontporch = hdmi_read16(sd, 0x2a, 0x1fff) / 2;
1326 freq = freq * 8 / bits_per_channel; 1588 bt->vsync = hdmi_read16(sd, 0x2e, 0x1fff) / 2;
1327 } 1589 bt->vbackporch = hdmi_read16(sd, 0x32, 0x1fff) / 2;
1328 bt->pixelclock = freq;
1329 bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
1330 hdmi_read(sd, 0x21);
1331 bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
1332 hdmi_read(sd, 0x23);
1333 bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
1334 hdmi_read(sd, 0x25);
1335 bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
1336 hdmi_read(sd, 0x2b)) / 2;
1337 bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
1338 hdmi_read(sd, 0x2f)) / 2;
1339 bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
1340 hdmi_read(sd, 0x33)) / 2;
1341 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) | 1590 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1342 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0); 1591 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1343 if (bt->interlaced == V4L2_DV_INTERLACED) { 1592 if (bt->interlaced == V4L2_DV_INTERLACED) {
1344 bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 + 1593 bt->height += hdmi_read16(sd, 0x0b, 0xfff);
1345 hdmi_read(sd, 0x0c); 1594 bt->il_vfrontporch = hdmi_read16(sd, 0x2c, 0x1fff) / 2;
1346 bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 + 1595 bt->il_vsync = hdmi_read16(sd, 0x30, 0x1fff) / 2;
1347 hdmi_read(sd, 0x2d)) / 2; 1596 bt->vbackporch = hdmi_read16(sd, 0x34, 0x1fff) / 2;
1348 bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
1349 hdmi_read(sd, 0x31)) / 2;
1350 bt->vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
1351 hdmi_read(sd, 0x35)) / 2;
1352 } 1597 }
1353 adv7604_fill_optional_dv_timings_fields(sd, timings); 1598 adv7604_fill_optional_dv_timings_fields(sd, timings);
1354 } else { 1599 } else {
@@ -1378,11 +1623,11 @@ static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1378 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__); 1623 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1379 /* TODO restart STDI for Sync Channel 2 */ 1624 /* TODO restart STDI for Sync Channel 2 */
1380 /* enter one-shot mode */ 1625 /* enter one-shot mode */
1381 cp_write_and_or(sd, 0x86, 0xf9, 0x00); 1626 cp_write_clr_set(sd, 0x86, 0x06, 0x00);
1382 /* trigger STDI restart */ 1627 /* trigger STDI restart */
1383 cp_write_and_or(sd, 0x86, 0xf9, 0x04); 1628 cp_write_clr_set(sd, 0x86, 0x06, 0x04);
1384 /* reset to continuous mode */ 1629 /* reset to continuous mode */
1385 cp_write_and_or(sd, 0x86, 0xf9, 0x02); 1630 cp_write_clr_set(sd, 0x86, 0x06, 0x02);
1386 state->restart_stdi_once = false; 1631 state->restart_stdi_once = false;
1387 return -ENOLINK; 1632 return -ENOLINK;
1388 } 1633 }
@@ -1441,7 +1686,7 @@ static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1441 1686
1442 state->timings = *timings; 1687 state->timings = *timings;
1443 1688
1444 cp_write(sd, 0x91, bt->interlaced ? 0x50 : 0x10); 1689 cp_write_clr_set(sd, 0x91, 0x40, bt->interlaced ? 0x40 : 0x00);
1445 1690
1446 /* Use prim_mode and vid_std when available */ 1691 /* Use prim_mode and vid_std when available */
1447 err = configure_predefined_video_timings(sd, timings); 1692 err = configure_predefined_video_timings(sd, timings);
@@ -1468,6 +1713,16 @@ static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1468 return 0; 1713 return 0;
1469} 1714}
1470 1715
1716static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
1717{
1718 hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
1719}
1720
1721static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
1722{
1723 hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
1724}
1725
1471static void enable_input(struct v4l2_subdev *sd) 1726static void enable_input(struct v4l2_subdev *sd)
1472{ 1727{
1473 struct adv7604_state *state = to_state(sd); 1728 struct adv7604_state *state = to_state(sd);
@@ -1475,10 +1730,10 @@ static void enable_input(struct v4l2_subdev *sd)
1475 if (is_analog_input(sd)) { 1730 if (is_analog_input(sd)) {
1476 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */ 1731 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
1477 } else if (is_digital_input(sd)) { 1732 } else if (is_digital_input(sd)) {
1478 hdmi_write_and_or(sd, 0x00, 0xfc, state->selected_input); 1733 hdmi_write_clr_set(sd, 0x00, 0x03, state->selected_input);
1479 hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */ 1734 state->info->set_termination(sd, true);
1480 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */ 1735 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
1481 hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */ 1736 hdmi_write_clr_set(sd, 0x1a, 0x10, 0x00); /* Unmute audio */
1482 } else { 1737 } else {
1483 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n", 1738 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1484 __func__, state->selected_input); 1739 __func__, state->selected_input);
@@ -1487,67 +1742,36 @@ static void enable_input(struct v4l2_subdev *sd)
1487 1742
1488static void disable_input(struct v4l2_subdev *sd) 1743static void disable_input(struct v4l2_subdev *sd)
1489{ 1744{
1490 hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio */ 1745 struct adv7604_state *state = to_state(sd);
1746
1747 hdmi_write_clr_set(sd, 0x1a, 0x10, 0x10); /* Mute audio */
1491 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */ 1748 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
1492 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */ 1749 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
1493 hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */ 1750 state->info->set_termination(sd, false);
1494} 1751}
1495 1752
1496static void select_input(struct v4l2_subdev *sd) 1753static void select_input(struct v4l2_subdev *sd)
1497{ 1754{
1498 struct adv7604_state *state = to_state(sd); 1755 struct adv7604_state *state = to_state(sd);
1756 const struct adv7604_chip_info *info = state->info;
1499 1757
1500 if (is_analog_input(sd)) { 1758 if (is_analog_input(sd)) {
1501 /* reset ADI recommended settings for HDMI: */ 1759 adv7604_write_reg_seq(sd, info->recommended_settings[0]);
1502 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
1503 hdmi_write(sd, 0x0d, 0x04); /* HDMI filter optimization */
1504 hdmi_write(sd, 0x3d, 0x00); /* DDC bus active pull-up control */
1505 hdmi_write(sd, 0x3e, 0x74); /* TMDS PLL optimization */
1506 hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
1507 hdmi_write(sd, 0x57, 0x74); /* TMDS PLL optimization */
1508 hdmi_write(sd, 0x58, 0x63); /* TMDS PLL optimization */
1509 hdmi_write(sd, 0x8d, 0x18); /* equaliser */
1510 hdmi_write(sd, 0x8e, 0x34); /* equaliser */
1511 hdmi_write(sd, 0x93, 0x88); /* equaliser */
1512 hdmi_write(sd, 0x94, 0x2e); /* equaliser */
1513 hdmi_write(sd, 0x96, 0x00); /* enable automatic EQ changing */
1514 1760
1515 afe_write(sd, 0x00, 0x08); /* power up ADC */ 1761 afe_write(sd, 0x00, 0x08); /* power up ADC */
1516 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */ 1762 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1517 afe_write(sd, 0xc8, 0x00); /* phase control */ 1763 afe_write(sd, 0xc8, 0x00); /* phase control */
1518
1519 /* set ADI recommended settings for digitizer */
1520 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
1521 afe_write(sd, 0x12, 0x7b); /* ADC noise shaping filter controls */
1522 afe_write(sd, 0x0c, 0x1f); /* CP core gain controls */
1523 cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
1524 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1525 cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
1526 } else if (is_digital_input(sd)) { 1764 } else if (is_digital_input(sd)) {
1527 hdmi_write(sd, 0x00, state->selected_input & 0x03); 1765 hdmi_write(sd, 0x00, state->selected_input & 0x03);
1528 1766
1529 /* set ADI recommended settings for HDMI: */ 1767 adv7604_write_reg_seq(sd, info->recommended_settings[1]);
1530 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */ 1768
1531 hdmi_write(sd, 0x0d, 0x84); /* HDMI filter optimization */ 1769 if (adv7604_has_afe(state)) {
1532 hdmi_write(sd, 0x3d, 0x10); /* DDC bus active pull-up control */ 1770 afe_write(sd, 0x00, 0xff); /* power down ADC */
1533 hdmi_write(sd, 0x3e, 0x39); /* TMDS PLL optimization */ 1771 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1534 hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */ 1772 afe_write(sd, 0xc8, 0x40); /* phase control */
1535 hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */ 1773 }
1536 hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */ 1774
1537 hdmi_write(sd, 0x8d, 0x18); /* equaliser */
1538 hdmi_write(sd, 0x8e, 0x34); /* equaliser */
1539 hdmi_write(sd, 0x93, 0x8b); /* equaliser */
1540 hdmi_write(sd, 0x94, 0x2d); /* equaliser */
1541 hdmi_write(sd, 0x96, 0x01); /* enable automatic EQ changing */
1542
1543 afe_write(sd, 0x00, 0xff); /* power down ADC */
1544 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1545 afe_write(sd, 0xc8, 0x40); /* phase control */
1546
1547 /* reset ADI recommended settings for digitizer */
1548 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
1549 afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
1550 afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
1551 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */ 1775 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1552 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */ 1776 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1553 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */ 1777 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
@@ -1568,6 +1792,9 @@ static int adv7604_s_routing(struct v4l2_subdev *sd,
1568 if (input == state->selected_input) 1792 if (input == state->selected_input)
1569 return 0; 1793 return 0;
1570 1794
1795 if (input > state->info->max_port)
1796 return -EINVAL;
1797
1571 state->selected_input = input; 1798 state->selected_input = input;
1572 1799
1573 disable_input(sd); 1800 disable_input(sd);
@@ -1579,34 +1806,139 @@ static int adv7604_s_routing(struct v4l2_subdev *sd,
1579 return 0; 1806 return 0;
1580} 1807}
1581 1808
1582static int adv7604_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index, 1809static int adv7604_enum_mbus_code(struct v4l2_subdev *sd,
1583 enum v4l2_mbus_pixelcode *code) 1810 struct v4l2_subdev_fh *fh,
1811 struct v4l2_subdev_mbus_code_enum *code)
1812{
1813 struct adv7604_state *state = to_state(sd);
1814
1815 if (code->index >= state->info->nformats)
1816 return -EINVAL;
1817
1818 code->code = state->info->formats[code->index].code;
1819
1820 return 0;
1821}
1822
1823static void adv7604_fill_format(struct adv7604_state *state,
1824 struct v4l2_mbus_framefmt *format)
1825{
1826 memset(format, 0, sizeof(*format));
1827
1828 format->width = state->timings.bt.width;
1829 format->height = state->timings.bt.height;
1830 format->field = V4L2_FIELD_NONE;
1831
1832 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861)
1833 format->colorspace = (state->timings.bt.height <= 576) ?
1834 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
1835}
1836
1837/*
1838 * Compute the op_ch_sel value required to obtain on the bus the component order
1839 * corresponding to the selected format taking into account bus reordering
1840 * applied by the board at the output of the device.
1841 *
1842 * The following table gives the op_ch_value from the format component order
1843 * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
1844 * adv7604_bus_order value in row).
1845 *
1846 * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
1847 * ----------+-------------------------------------------------
1848 * RGB (NOP) | GBR GRB BGR RGB BRG RBG
1849 * GRB (1-2) | BGR RGB GBR GRB RBG BRG
1850 * RBG (2-3) | GRB GBR BRG RBG BGR RGB
1851 * BGR (1-3) | RBG BRG RGB BGR GRB GBR
1852 * BRG (ROR) | BRG RBG GRB GBR RGB BGR
1853 * GBR (ROL) | RGB BGR RBG BRG GBR GRB
1854 */
1855static unsigned int adv7604_op_ch_sel(struct adv7604_state *state)
1856{
1857#define _SEL(a,b,c,d,e,f) { \
1858 ADV7604_OP_CH_SEL_##a, ADV7604_OP_CH_SEL_##b, ADV7604_OP_CH_SEL_##c, \
1859 ADV7604_OP_CH_SEL_##d, ADV7604_OP_CH_SEL_##e, ADV7604_OP_CH_SEL_##f }
1860#define _BUS(x) [ADV7604_BUS_ORDER_##x]
1861
1862 static const unsigned int op_ch_sel[6][6] = {
1863 _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
1864 _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
1865 _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
1866 _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
1867 _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
1868 _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
1869 };
1870
1871 return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
1872}
1873
1874static void adv7604_setup_format(struct adv7604_state *state)
1875{
1876 struct v4l2_subdev *sd = &state->sd;
1877
1878 io_write_clr_set(sd, 0x02, 0x02,
1879 state->format->rgb_out ? ADV7604_RGB_OUT : 0);
1880 io_write(sd, 0x03, state->format->op_format_sel |
1881 state->pdata.op_format_mode_sel);
1882 io_write_clr_set(sd, 0x04, 0xe0, adv7604_op_ch_sel(state));
1883 io_write_clr_set(sd, 0x05, 0x01,
1884 state->format->swap_cb_cr ? ADV7604_OP_SWAP_CB_CR : 0);
1885}
1886
1887static int adv7604_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1888 struct v4l2_subdev_format *format)
1584{ 1889{
1585 if (index) 1890 struct adv7604_state *state = to_state(sd);
1891
1892 if (format->pad != state->source_pad)
1586 return -EINVAL; 1893 return -EINVAL;
1587 /* Good enough for now */ 1894
1588 *code = V4L2_MBUS_FMT_FIXED; 1895 adv7604_fill_format(state, &format->format);
1896
1897 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1898 struct v4l2_mbus_framefmt *fmt;
1899
1900 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1901 format->format.code = fmt->code;
1902 } else {
1903 format->format.code = state->format->code;
1904 }
1905
1589 return 0; 1906 return 0;
1590} 1907}
1591 1908
1592static int adv7604_g_mbus_fmt(struct v4l2_subdev *sd, 1909static int adv7604_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1593 struct v4l2_mbus_framefmt *fmt) 1910 struct v4l2_subdev_format *format)
1594{ 1911{
1595 struct adv7604_state *state = to_state(sd); 1912 struct adv7604_state *state = to_state(sd);
1913 const struct adv7604_format_info *info;
1596 1914
1597 fmt->width = state->timings.bt.width; 1915 if (format->pad != state->source_pad)
1598 fmt->height = state->timings.bt.height; 1916 return -EINVAL;
1599 fmt->code = V4L2_MBUS_FMT_FIXED; 1917
1600 fmt->field = V4L2_FIELD_NONE; 1918 info = adv7604_format_info(state, format->format.code);
1601 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) { 1919 if (info == NULL)
1602 fmt->colorspace = (state->timings.bt.height <= 576) ? 1920 info = adv7604_format_info(state, V4L2_MBUS_FMT_YUYV8_2X8);
1603 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709; 1921
1922 adv7604_fill_format(state, &format->format);
1923 format->format.code = info->code;
1924
1925 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1926 struct v4l2_mbus_framefmt *fmt;
1927
1928 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1929 fmt->code = format->format.code;
1930 } else {
1931 state->format = info;
1932 adv7604_setup_format(state);
1604 } 1933 }
1934
1605 return 0; 1935 return 0;
1606} 1936}
1607 1937
1608static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled) 1938static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1609{ 1939{
1940 struct adv7604_state *state = to_state(sd);
1941 const struct adv7604_chip_info *info = state->info;
1610 const u8 irq_reg_0x43 = io_read(sd, 0x43); 1942 const u8 irq_reg_0x43 = io_read(sd, 0x43);
1611 const u8 irq_reg_0x6b = io_read(sd, 0x6b); 1943 const u8 irq_reg_0x6b = io_read(sd, 0x6b);
1612 const u8 irq_reg_0x70 = io_read(sd, 0x70); 1944 const u8 irq_reg_0x70 = io_read(sd, 0x70);
@@ -1625,7 +1957,9 @@ static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1625 1957
1626 /* format change */ 1958 /* format change */
1627 fmt_change = irq_reg_0x43 & 0x98; 1959 fmt_change = irq_reg_0x43 & 0x98;
1628 fmt_change_digital = is_digital_input(sd) ? (irq_reg_0x6b & 0xc0) : 0; 1960 fmt_change_digital = is_digital_input(sd)
1961 ? irq_reg_0x6b & info->fmt_change_digital_mask
1962 : 0;
1629 1963
1630 if (fmt_change || fmt_change_digital) { 1964 if (fmt_change || fmt_change_digital) {
1631 v4l2_dbg(1, debug, sd, 1965 v4l2_dbg(1, debug, sd,
@@ -1647,7 +1981,7 @@ static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1647 } 1981 }
1648 1982
1649 /* tx 5v detect */ 1983 /* tx 5v detect */
1650 tx_5v = io_read(sd, 0x70) & 0x1e; 1984 tx_5v = io_read(sd, 0x70) & info->cable_det_mask;
1651 if (tx_5v) { 1985 if (tx_5v) {
1652 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v); 1986 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
1653 io_write(sd, 0x71, tx_5v); 1987 io_write(sd, 0x71, tx_5v);
@@ -1663,7 +1997,7 @@ static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1663 struct adv7604_state *state = to_state(sd); 1997 struct adv7604_state *state = to_state(sd);
1664 u8 *data = NULL; 1998 u8 *data = NULL;
1665 1999
1666 if (edid->pad > ADV7604_EDID_PORT_D) 2000 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
1667 return -EINVAL; 2001 return -EINVAL;
1668 if (edid->blocks == 0) 2002 if (edid->blocks == 0)
1669 return -EINVAL; 2003 return -EINVAL;
@@ -1678,10 +2012,10 @@ static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1678 edid->blocks = state->edid.blocks; 2012 edid->blocks = state->edid.blocks;
1679 2013
1680 switch (edid->pad) { 2014 switch (edid->pad) {
1681 case ADV7604_EDID_PORT_A: 2015 case ADV7604_PAD_HDMI_PORT_A:
1682 case ADV7604_EDID_PORT_B: 2016 case ADV7604_PAD_HDMI_PORT_B:
1683 case ADV7604_EDID_PORT_C: 2017 case ADV7604_PAD_HDMI_PORT_C:
1684 case ADV7604_EDID_PORT_D: 2018 case ADV7604_PAD_HDMI_PORT_D:
1685 if (state->edid.present & (1 << edid->pad)) 2019 if (state->edid.present & (1 << edid->pad))
1686 data = state->edid.edid; 2020 data = state->edid.edid;
1687 break; 2021 break;
@@ -1729,20 +2063,20 @@ static int get_edid_spa_location(const u8 *edid)
1729static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid) 2063static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1730{ 2064{
1731 struct adv7604_state *state = to_state(sd); 2065 struct adv7604_state *state = to_state(sd);
2066 const struct adv7604_chip_info *info = state->info;
1732 int spa_loc; 2067 int spa_loc;
1733 int tmp = 0;
1734 int err; 2068 int err;
1735 int i; 2069 int i;
1736 2070
1737 if (edid->pad > ADV7604_EDID_PORT_D) 2071 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
1738 return -EINVAL; 2072 return -EINVAL;
1739 if (edid->start_block != 0) 2073 if (edid->start_block != 0)
1740 return -EINVAL; 2074 return -EINVAL;
1741 if (edid->blocks == 0) { 2075 if (edid->blocks == 0) {
1742 /* Disable hotplug and I2C access to EDID RAM from DDC port */ 2076 /* Disable hotplug and I2C access to EDID RAM from DDC port */
1743 state->edid.present &= ~(1 << edid->pad); 2077 state->edid.present &= ~(1 << edid->pad);
1744 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present); 2078 adv7604_set_hpd(state, state->edid.present);
1745 rep_write_and_or(sd, 0x77, 0xf0, state->edid.present); 2079 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
1746 2080
1747 /* Fall back to a 16:9 aspect ratio */ 2081 /* Fall back to a 16:9 aspect ratio */
1748 state->aspect_ratio.numerator = 16; 2082 state->aspect_ratio.numerator = 16;
@@ -1765,35 +2099,41 @@ static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1765 2099
1766 /* Disable hotplug and I2C access to EDID RAM from DDC port */ 2100 /* Disable hotplug and I2C access to EDID RAM from DDC port */
1767 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug); 2101 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
1768 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&tmp); 2102 adv7604_set_hpd(state, 0);
1769 rep_write_and_or(sd, 0x77, 0xf0, 0x00); 2103 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, 0x00);
1770 2104
1771 spa_loc = get_edid_spa_location(edid->edid); 2105 spa_loc = get_edid_spa_location(edid->edid);
1772 if (spa_loc < 0) 2106 if (spa_loc < 0)
1773 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */ 2107 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
1774 2108
1775 switch (edid->pad) { 2109 switch (edid->pad) {
1776 case ADV7604_EDID_PORT_A: 2110 case ADV7604_PAD_HDMI_PORT_A:
1777 state->spa_port_a[0] = edid->edid[spa_loc]; 2111 state->spa_port_a[0] = edid->edid[spa_loc];
1778 state->spa_port_a[1] = edid->edid[spa_loc + 1]; 2112 state->spa_port_a[1] = edid->edid[spa_loc + 1];
1779 break; 2113 break;
1780 case ADV7604_EDID_PORT_B: 2114 case ADV7604_PAD_HDMI_PORT_B:
1781 rep_write(sd, 0x70, edid->edid[spa_loc]); 2115 rep_write(sd, 0x70, edid->edid[spa_loc]);
1782 rep_write(sd, 0x71, edid->edid[spa_loc + 1]); 2116 rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
1783 break; 2117 break;
1784 case ADV7604_EDID_PORT_C: 2118 case ADV7604_PAD_HDMI_PORT_C:
1785 rep_write(sd, 0x72, edid->edid[spa_loc]); 2119 rep_write(sd, 0x72, edid->edid[spa_loc]);
1786 rep_write(sd, 0x73, edid->edid[spa_loc + 1]); 2120 rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
1787 break; 2121 break;
1788 case ADV7604_EDID_PORT_D: 2122 case ADV7604_PAD_HDMI_PORT_D:
1789 rep_write(sd, 0x74, edid->edid[spa_loc]); 2123 rep_write(sd, 0x74, edid->edid[spa_loc]);
1790 rep_write(sd, 0x75, edid->edid[spa_loc + 1]); 2124 rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
1791 break; 2125 break;
1792 default: 2126 default:
1793 return -EINVAL; 2127 return -EINVAL;
1794 } 2128 }
1795 rep_write(sd, 0x76, spa_loc & 0xff); 2129
1796 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40); 2130 if (info->type == ADV7604) {
2131 rep_write(sd, 0x76, spa_loc & 0xff);
2132 rep_write_clr_set(sd, 0x77, 0x40, (spa_loc & 0x100) >> 2);
2133 } else {
2134 /* FIXME: Where is the SPA location LSB register ? */
2135 rep_write_clr_set(sd, 0x71, 0x01, (spa_loc & 0x100) >> 8);
2136 }
1797 2137
1798 edid->edid[spa_loc] = state->spa_port_a[0]; 2138 edid->edid[spa_loc] = state->spa_port_a[0];
1799 edid->edid[spa_loc + 1] = state->spa_port_a[1]; 2139 edid->edid[spa_loc + 1] = state->spa_port_a[1];
@@ -1812,10 +2152,10 @@ static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1812 2152
1813 /* adv7604 calculates the checksums and enables I2C access to internal 2153 /* adv7604 calculates the checksums and enables I2C access to internal
1814 EDID RAM from DDC port. */ 2154 EDID RAM from DDC port. */
1815 rep_write_and_or(sd, 0x77, 0xf0, state->edid.present); 2155 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
1816 2156
1817 for (i = 0; i < 1000; i++) { 2157 for (i = 0; i < 1000; i++) {
1818 if (rep_read(sd, 0x7d) & state->edid.present) 2158 if (rep_read(sd, info->edid_status_reg) & state->edid.present)
1819 break; 2159 break;
1820 mdelay(1); 2160 mdelay(1);
1821 } 2161 }
@@ -1878,17 +2218,20 @@ static void print_avi_infoframe(struct v4l2_subdev *sd)
1878static int adv7604_log_status(struct v4l2_subdev *sd) 2218static int adv7604_log_status(struct v4l2_subdev *sd)
1879{ 2219{
1880 struct adv7604_state *state = to_state(sd); 2220 struct adv7604_state *state = to_state(sd);
2221 const struct adv7604_chip_info *info = state->info;
1881 struct v4l2_dv_timings timings; 2222 struct v4l2_dv_timings timings;
1882 struct stdi_readback stdi; 2223 struct stdi_readback stdi;
1883 u8 reg_io_0x02 = io_read(sd, 0x02); 2224 u8 reg_io_0x02 = io_read(sd, 0x02);
2225 u8 edid_enabled;
2226 u8 cable_det;
1884 2227
1885 char *csc_coeff_sel_rb[16] = { 2228 static const char * const csc_coeff_sel_rb[16] = {
1886 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB", 2229 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
1887 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709", 2230 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
1888 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709", 2231 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
1889 "reserved", "reserved", "reserved", "reserved", "manual" 2232 "reserved", "reserved", "reserved", "reserved", "manual"
1890 }; 2233 };
1891 char *input_color_space_txt[16] = { 2234 static const char * const input_color_space_txt[16] = {
1892 "RGB limited range (16-235)", "RGB full range (0-255)", 2235 "RGB limited range (16-235)", "RGB full range (0-255)",
1893 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)", 2236 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
1894 "xvYCC Bt.601", "xvYCC Bt.709", 2237 "xvYCC Bt.601", "xvYCC Bt.709",
@@ -1896,12 +2239,12 @@ static int adv7604_log_status(struct v4l2_subdev *sd)
1896 "invalid", "invalid", "invalid", "invalid", "invalid", 2239 "invalid", "invalid", "invalid", "invalid", "invalid",
1897 "invalid", "invalid", "automatic" 2240 "invalid", "invalid", "automatic"
1898 }; 2241 };
1899 char *rgb_quantization_range_txt[] = { 2242 static const char * const rgb_quantization_range_txt[] = {
1900 "Automatic", 2243 "Automatic",
1901 "RGB limited range (16-235)", 2244 "RGB limited range (16-235)",
1902 "RGB full range (0-255)", 2245 "RGB full range (0-255)",
1903 }; 2246 };
1904 char *deep_color_mode_txt[4] = { 2247 static const char * const deep_color_mode_txt[4] = {
1905 "8-bits per channel", 2248 "8-bits per channel",
1906 "10-bits per channel", 2249 "10-bits per channel",
1907 "12-bits per channel", 2250 "12-bits per channel",
@@ -1910,20 +2253,22 @@ static int adv7604_log_status(struct v4l2_subdev *sd)
1910 2253
1911 v4l2_info(sd, "-----Chip status-----\n"); 2254 v4l2_info(sd, "-----Chip status-----\n");
1912 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on"); 2255 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
2256 edid_enabled = rep_read(sd, info->edid_status_reg);
1913 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n", 2257 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
1914 ((rep_read(sd, 0x7d) & 0x01) ? "Yes" : "No"), 2258 ((edid_enabled & 0x01) ? "Yes" : "No"),
1915 ((rep_read(sd, 0x7d) & 0x02) ? "Yes" : "No"), 2259 ((edid_enabled & 0x02) ? "Yes" : "No"),
1916 ((rep_read(sd, 0x7d) & 0x04) ? "Yes" : "No"), 2260 ((edid_enabled & 0x04) ? "Yes" : "No"),
1917 ((rep_read(sd, 0x7d) & 0x08) ? "Yes" : "No")); 2261 ((edid_enabled & 0x08) ? "Yes" : "No"));
1918 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ? 2262 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
1919 "enabled" : "disabled"); 2263 "enabled" : "disabled");
1920 2264
1921 v4l2_info(sd, "-----Signal status-----\n"); 2265 v4l2_info(sd, "-----Signal status-----\n");
2266 cable_det = info->read_cable_det(sd);
1922 v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n", 2267 v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
1923 ((io_read(sd, 0x6f) & 0x10) ? "Yes" : "No"), 2268 ((cable_det & 0x01) ? "Yes" : "No"),
1924 ((io_read(sd, 0x6f) & 0x08) ? "Yes" : "No"), 2269 ((cable_det & 0x02) ? "Yes" : "No"),
1925 ((io_read(sd, 0x6f) & 0x04) ? "Yes" : "No"), 2270 ((cable_det & 0x04) ? "Yes" : "No"),
1926 ((io_read(sd, 0x6f) & 0x02) ? "Yes" : "No")); 2271 ((cable_det & 0x08) ? "Yes" : "No"));
1927 v4l2_info(sd, "TMDS signal detected: %s\n", 2272 v4l2_info(sd, "TMDS signal detected: %s\n",
1928 no_signal_tmds(sd) ? "false" : "true"); 2273 no_signal_tmds(sd) ? "false" : "true");
1929 v4l2_info(sd, "TMDS signal locked: %s\n", 2274 v4l2_info(sd, "TMDS signal locked: %s\n",
@@ -2017,13 +2362,6 @@ static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
2017 2362
2018static const struct v4l2_subdev_core_ops adv7604_core_ops = { 2363static const struct v4l2_subdev_core_ops adv7604_core_ops = {
2019 .log_status = adv7604_log_status, 2364 .log_status = adv7604_log_status,
2020 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
2021 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
2022 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
2023 .g_ctrl = v4l2_subdev_g_ctrl,
2024 .s_ctrl = v4l2_subdev_s_ctrl,
2025 .queryctrl = v4l2_subdev_queryctrl,
2026 .querymenu = v4l2_subdev_querymenu,
2027 .interrupt_service_routine = adv7604_isr, 2365 .interrupt_service_routine = adv7604_isr,
2028#ifdef CONFIG_VIDEO_ADV_DEBUG 2366#ifdef CONFIG_VIDEO_ADV_DEBUG
2029 .g_register = adv7604_g_register, 2367 .g_register = adv7604_g_register,
@@ -2037,17 +2375,16 @@ static const struct v4l2_subdev_video_ops adv7604_video_ops = {
2037 .s_dv_timings = adv7604_s_dv_timings, 2375 .s_dv_timings = adv7604_s_dv_timings,
2038 .g_dv_timings = adv7604_g_dv_timings, 2376 .g_dv_timings = adv7604_g_dv_timings,
2039 .query_dv_timings = adv7604_query_dv_timings, 2377 .query_dv_timings = adv7604_query_dv_timings,
2040 .enum_dv_timings = adv7604_enum_dv_timings,
2041 .dv_timings_cap = adv7604_dv_timings_cap,
2042 .enum_mbus_fmt = adv7604_enum_mbus_fmt,
2043 .g_mbus_fmt = adv7604_g_mbus_fmt,
2044 .try_mbus_fmt = adv7604_g_mbus_fmt,
2045 .s_mbus_fmt = adv7604_g_mbus_fmt,
2046}; 2378};
2047 2379
2048static const struct v4l2_subdev_pad_ops adv7604_pad_ops = { 2380static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
2381 .enum_mbus_code = adv7604_enum_mbus_code,
2382 .get_fmt = adv7604_get_format,
2383 .set_fmt = adv7604_set_format,
2049 .get_edid = adv7604_get_edid, 2384 .get_edid = adv7604_get_edid,
2050 .set_edid = adv7604_set_edid, 2385 .set_edid = adv7604_set_edid,
2386 .dv_timings_cap = adv7604_dv_timings_cap,
2387 .enum_dv_timings = adv7604_enum_dv_timings,
2051}; 2388};
2052 2389
2053static const struct v4l2_subdev_ops adv7604_ops = { 2390static const struct v4l2_subdev_ops adv7604_ops = {
@@ -2096,6 +2433,7 @@ static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
2096static int adv7604_core_init(struct v4l2_subdev *sd) 2433static int adv7604_core_init(struct v4l2_subdev *sd)
2097{ 2434{
2098 struct adv7604_state *state = to_state(sd); 2435 struct adv7604_state *state = to_state(sd);
2436 const struct adv7604_chip_info *info = state->info;
2099 struct adv7604_platform_data *pdata = &state->pdata; 2437 struct adv7604_platform_data *pdata = &state->pdata;
2100 2438
2101 hdmi_write(sd, 0x48, 2439 hdmi_write(sd, 0x48,
@@ -2104,28 +2442,33 @@ static int adv7604_core_init(struct v4l2_subdev *sd)
2104 2442
2105 disable_input(sd); 2443 disable_input(sd);
2106 2444
2445 if (pdata->default_input >= 0 &&
2446 pdata->default_input < state->source_pad) {
2447 state->selected_input = pdata->default_input;
2448 select_input(sd);
2449 enable_input(sd);
2450 }
2451
2107 /* power */ 2452 /* power */
2108 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */ 2453 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2109 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */ 2454 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
2110 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */ 2455 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
2111 2456
2112 /* video format */ 2457 /* video format */
2113 io_write_and_or(sd, 0x02, 0xf0, 2458 io_write_clr_set(sd, 0x02, 0x0f,
2114 pdata->alt_gamma << 3 | 2459 pdata->alt_gamma << 3 |
2115 pdata->op_656_range << 2 | 2460 pdata->op_656_range << 2 |
2116 pdata->rgb_out << 1 |
2117 pdata->alt_data_sat << 0); 2461 pdata->alt_data_sat << 0);
2118 io_write(sd, 0x03, pdata->op_format_sel); 2462 io_write_clr_set(sd, 0x05, 0x0e, pdata->blank_data << 3 |
2119 io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5); 2463 pdata->insert_av_codes << 2 |
2120 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 | 2464 pdata->replicate_av_codes << 1);
2121 pdata->insert_av_codes << 2 | 2465 adv7604_setup_format(state);
2122 pdata->replicate_av_codes << 1 |
2123 pdata->invert_cbcr << 0);
2124 2466
2125 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */ 2467 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
2126 2468
2127 /* VS, HS polarities */ 2469 /* VS, HS polarities */
2128 io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 | pdata->inv_hs_pol << 1); 2470 io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 |
2471 pdata->inv_hs_pol << 1 | pdata->inv_llc_pol);
2129 2472
2130 /* Adjust drive strength */ 2473 /* Adjust drive strength */
2131 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 | 2474 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
@@ -2142,52 +2485,46 @@ static int adv7604_core_init(struct v4l2_subdev *sd)
2142 for digital formats */ 2485 for digital formats */
2143 2486
2144 /* HDMI audio */ 2487 /* HDMI audio */
2145 hdmi_write_and_or(sd, 0x15, 0xfc, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */ 2488 hdmi_write_clr_set(sd, 0x15, 0x03, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
2146 hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */ 2489 hdmi_write_clr_set(sd, 0x1a, 0x0e, 0x08); /* Wait 1 s before unmute */
2147 hdmi_write_and_or(sd, 0x68, 0xf9, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */ 2490 hdmi_write_clr_set(sd, 0x68, 0x06, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
2148 2491
2149 /* TODO from platform data */ 2492 /* TODO from platform data */
2150 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */ 2493 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
2151 2494
2152 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */ 2495 if (adv7604_has_afe(state)) {
2153 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4); 2496 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
2497 io_write_clr_set(sd, 0x30, 1 << 4, pdata->output_bus_lsb_to_msb << 4);
2498 }
2154 2499
2155 /* interrupts */ 2500 /* interrupts */
2156 io_write(sd, 0x40, 0xc2); /* Configure INT1 */ 2501 io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
2157 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
2158 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */ 2502 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
2159 io_write(sd, 0x6e, 0xc1); /* Enable V_LOCKED, DE_REGEN_LCK, HDMI_MODE interrupts */ 2503 io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
2160 io_write(sd, 0x73, 0x1e); /* Enable CABLE_DET_A_ST (+5v) interrupts */ 2504 io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
2505 info->setup_irqs(sd);
2161 2506
2162 return v4l2_ctrl_handler_setup(sd->ctrl_handler); 2507 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
2163} 2508}
2164 2509
2510static void adv7604_setup_irqs(struct v4l2_subdev *sd)
2511{
2512 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
2513}
2514
2515static void adv7611_setup_irqs(struct v4l2_subdev *sd)
2516{
2517 io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
2518}
2519
2165static void adv7604_unregister_clients(struct adv7604_state *state) 2520static void adv7604_unregister_clients(struct adv7604_state *state)
2166{ 2521{
2167 if (state->i2c_avlink) 2522 unsigned int i;
2168 i2c_unregister_device(state->i2c_avlink); 2523
2169 if (state->i2c_cec) 2524 for (i = 1; i < ARRAY_SIZE(state->i2c_clients); ++i) {
2170 i2c_unregister_device(state->i2c_cec); 2525 if (state->i2c_clients[i])
2171 if (state->i2c_infoframe) 2526 i2c_unregister_device(state->i2c_clients[i]);
2172 i2c_unregister_device(state->i2c_infoframe); 2527 }
2173 if (state->i2c_esdp)
2174 i2c_unregister_device(state->i2c_esdp);
2175 if (state->i2c_dpp)
2176 i2c_unregister_device(state->i2c_dpp);
2177 if (state->i2c_afe)
2178 i2c_unregister_device(state->i2c_afe);
2179 if (state->i2c_repeater)
2180 i2c_unregister_device(state->i2c_repeater);
2181 if (state->i2c_edid)
2182 i2c_unregister_device(state->i2c_edid);
2183 if (state->i2c_hdmi)
2184 i2c_unregister_device(state->i2c_hdmi);
2185 if (state->i2c_test)
2186 i2c_unregister_device(state->i2c_test);
2187 if (state->i2c_cp)
2188 i2c_unregister_device(state->i2c_cp);
2189 if (state->i2c_vdp)
2190 i2c_unregister_device(state->i2c_vdp);
2191} 2528}
2192 2529
2193static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd, 2530static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
@@ -2200,15 +2537,219 @@ static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
2200 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1); 2537 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2201} 2538}
2202 2539
2540static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
2541 /* reset ADI recommended settings for HDMI: */
2542 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2543 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2544 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2545 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
2546 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
2547 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2548 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
2549 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
2550 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2551 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2552 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
2553 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
2554 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
2555
2556 /* set ADI recommended settings for digitizer */
2557 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2558 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
2559 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
2560 { ADV7604_REG(ADV7604_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
2561 { ADV7604_REG(ADV7604_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
2562 { ADV7604_REG(ADV7604_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
2563
2564 { ADV7604_REG_SEQ_TERM, 0 },
2565};
2566
2567static const struct adv7604_reg_seq adv7604_recommended_settings_hdmi[] = {
2568 /* set ADI recommended settings for HDMI: */
2569 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2570 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
2571 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
2572 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
2573 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2574 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
2575 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
2576 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2577 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2578 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
2579 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
2580 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
2581
2582 /* reset ADI recommended settings for digitizer */
2583 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2584 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
2585 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
2586
2587 { ADV7604_REG_SEQ_TERM, 0 },
2588};
2589
2590static const struct adv7604_reg_seq adv7611_recommended_settings_hdmi[] = {
2591 { ADV7604_REG(ADV7604_PAGE_CP, 0x6c), 0x00 },
2592 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x6f), 0x0c },
2593 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x87), 0x70 },
2594 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xda },
2595 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x01 },
2596 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x03), 0x98 },
2597 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4c), 0x44 },
2598 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x04 },
2599 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x1e },
2600
2601 { ADV7604_REG_SEQ_TERM, 0 },
2602};
2603
2604static const struct adv7604_chip_info adv7604_chip_info[] = {
2605 [ADV7604] = {
2606 .type = ADV7604,
2607 .has_afe = true,
2608 .max_port = ADV7604_PAD_VGA_COMP,
2609 .num_dv_ports = 4,
2610 .edid_enable_reg = 0x77,
2611 .edid_status_reg = 0x7d,
2612 .lcf_reg = 0xb3,
2613 .tdms_lock_mask = 0xe0,
2614 .cable_det_mask = 0x1e,
2615 .fmt_change_digital_mask = 0xc1,
2616 .formats = adv7604_formats,
2617 .nformats = ARRAY_SIZE(adv7604_formats),
2618 .set_termination = adv7604_set_termination,
2619 .setup_irqs = adv7604_setup_irqs,
2620 .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
2621 .read_cable_det = adv7604_read_cable_det,
2622 .recommended_settings = {
2623 [0] = adv7604_recommended_settings_afe,
2624 [1] = adv7604_recommended_settings_hdmi,
2625 },
2626 .num_recommended_settings = {
2627 [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
2628 [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
2629 },
2630 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
2631 BIT(ADV7604_PAGE_CEC) | BIT(ADV7604_PAGE_INFOFRAME) |
2632 BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
2633 BIT(ADV7604_PAGE_AFE) | BIT(ADV7604_PAGE_REP) |
2634 BIT(ADV7604_PAGE_EDID) | BIT(ADV7604_PAGE_HDMI) |
2635 BIT(ADV7604_PAGE_TEST) | BIT(ADV7604_PAGE_CP) |
2636 BIT(ADV7604_PAGE_VDP),
2637 },
2638 [ADV7611] = {
2639 .type = ADV7611,
2640 .has_afe = false,
2641 .max_port = ADV7604_PAD_HDMI_PORT_A,
2642 .num_dv_ports = 1,
2643 .edid_enable_reg = 0x74,
2644 .edid_status_reg = 0x76,
2645 .lcf_reg = 0xa3,
2646 .tdms_lock_mask = 0x43,
2647 .cable_det_mask = 0x01,
2648 .fmt_change_digital_mask = 0x03,
2649 .formats = adv7611_formats,
2650 .nformats = ARRAY_SIZE(adv7611_formats),
2651 .set_termination = adv7611_set_termination,
2652 .setup_irqs = adv7611_setup_irqs,
2653 .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
2654 .read_cable_det = adv7611_read_cable_det,
2655 .recommended_settings = {
2656 [1] = adv7611_recommended_settings_hdmi,
2657 },
2658 .num_recommended_settings = {
2659 [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
2660 },
2661 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_CEC) |
2662 BIT(ADV7604_PAGE_INFOFRAME) | BIT(ADV7604_PAGE_AFE) |
2663 BIT(ADV7604_PAGE_REP) | BIT(ADV7604_PAGE_EDID) |
2664 BIT(ADV7604_PAGE_HDMI) | BIT(ADV7604_PAGE_CP),
2665 },
2666};
2667
2668static struct i2c_device_id adv7604_i2c_id[] = {
2669 { "adv7604", (kernel_ulong_t)&adv7604_chip_info[ADV7604] },
2670 { "adv7611", (kernel_ulong_t)&adv7604_chip_info[ADV7611] },
2671 { }
2672};
2673MODULE_DEVICE_TABLE(i2c, adv7604_i2c_id);
2674
2675static struct of_device_id adv7604_of_id[] __maybe_unused = {
2676 { .compatible = "adi,adv7611", .data = &adv7604_chip_info[ADV7611] },
2677 { }
2678};
2679MODULE_DEVICE_TABLE(of, adv7604_of_id);
2680
2681static int adv7604_parse_dt(struct adv7604_state *state)
2682{
2683 struct v4l2_of_endpoint bus_cfg;
2684 struct device_node *endpoint;
2685 struct device_node *np;
2686 unsigned int flags;
2687
2688 np = state->i2c_clients[ADV7604_PAGE_IO]->dev.of_node;
2689
2690 /* Parse the endpoint. */
2691 endpoint = of_graph_get_next_endpoint(np, NULL);
2692 if (!endpoint)
2693 return -EINVAL;
2694
2695 v4l2_of_parse_endpoint(endpoint, &bus_cfg);
2696 of_node_put(endpoint);
2697
2698 flags = bus_cfg.bus.parallel.flags;
2699
2700 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2701 state->pdata.inv_hs_pol = 1;
2702
2703 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2704 state->pdata.inv_vs_pol = 1;
2705
2706 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2707 state->pdata.inv_llc_pol = 1;
2708
2709 if (bus_cfg.bus_type == V4L2_MBUS_BT656) {
2710 state->pdata.insert_av_codes = 1;
2711 state->pdata.op_656_range = 1;
2712 }
2713
2714 /* Disable the interrupt for now as no DT-based board uses it. */
2715 state->pdata.int1_config = ADV7604_INT1_CONFIG_DISABLED;
2716
2717 /* Use the default I2C addresses. */
2718 state->pdata.i2c_addresses[ADV7604_PAGE_AVLINK] = 0x42;
2719 state->pdata.i2c_addresses[ADV7604_PAGE_CEC] = 0x40;
2720 state->pdata.i2c_addresses[ADV7604_PAGE_INFOFRAME] = 0x3e;
2721 state->pdata.i2c_addresses[ADV7604_PAGE_ESDP] = 0x38;
2722 state->pdata.i2c_addresses[ADV7604_PAGE_DPP] = 0x3c;
2723 state->pdata.i2c_addresses[ADV7604_PAGE_AFE] = 0x26;
2724 state->pdata.i2c_addresses[ADV7604_PAGE_REP] = 0x32;
2725 state->pdata.i2c_addresses[ADV7604_PAGE_EDID] = 0x36;
2726 state->pdata.i2c_addresses[ADV7604_PAGE_HDMI] = 0x34;
2727 state->pdata.i2c_addresses[ADV7604_PAGE_TEST] = 0x30;
2728 state->pdata.i2c_addresses[ADV7604_PAGE_CP] = 0x22;
2729 state->pdata.i2c_addresses[ADV7604_PAGE_VDP] = 0x24;
2730
2731 /* Hardcode the remaining platform data fields. */
2732 state->pdata.disable_pwrdnb = 0;
2733 state->pdata.disable_cable_det_rst = 0;
2734 state->pdata.default_input = -1;
2735 state->pdata.blank_data = 1;
2736 state->pdata.alt_data_sat = 1;
2737 state->pdata.op_format_mode_sel = ADV7604_OP_FORMAT_MODE0;
2738 state->pdata.bus_order = ADV7604_BUS_ORDER_RGB;
2739
2740 return 0;
2741}
2742
2203static int adv7604_probe(struct i2c_client *client, 2743static int adv7604_probe(struct i2c_client *client,
2204 const struct i2c_device_id *id) 2744 const struct i2c_device_id *id)
2205{ 2745{
2206 static const struct v4l2_dv_timings cea640x480 = 2746 static const struct v4l2_dv_timings cea640x480 =
2207 V4L2_DV_BT_CEA_640X480P59_94; 2747 V4L2_DV_BT_CEA_640X480P59_94;
2208 struct adv7604_state *state; 2748 struct adv7604_state *state;
2209 struct adv7604_platform_data *pdata = client->dev.platform_data;
2210 struct v4l2_ctrl_handler *hdl; 2749 struct v4l2_ctrl_handler *hdl;
2211 struct v4l2_subdev *sd; 2750 struct v4l2_subdev *sd;
2751 unsigned int i;
2752 u16 val;
2212 int err; 2753 int err;
2213 2754
2214 /* Check if the adapter supports the needed features */ 2755 /* Check if the adapter supports the needed features */
@@ -2223,32 +2764,80 @@ static int adv7604_probe(struct i2c_client *client,
2223 return -ENOMEM; 2764 return -ENOMEM;
2224 } 2765 }
2225 2766
2767 state->i2c_clients[ADV7604_PAGE_IO] = client;
2768
2226 /* initialize variables */ 2769 /* initialize variables */
2227 state->restart_stdi_once = true; 2770 state->restart_stdi_once = true;
2228 state->selected_input = ~0; 2771 state->selected_input = ~0;
2229 2772
2230 /* platform data */ 2773 if (IS_ENABLED(CONFIG_OF) && client->dev.of_node) {
2231 if (!pdata) { 2774 const struct of_device_id *oid;
2775
2776 oid = of_match_node(adv7604_of_id, client->dev.of_node);
2777 state->info = oid->data;
2778
2779 err = adv7604_parse_dt(state);
2780 if (err < 0) {
2781 v4l_err(client, "DT parsing error\n");
2782 return err;
2783 }
2784 } else if (client->dev.platform_data) {
2785 struct adv7604_platform_data *pdata = client->dev.platform_data;
2786
2787 state->info = (const struct adv7604_chip_info *)id->driver_data;
2788 state->pdata = *pdata;
2789 } else {
2232 v4l_err(client, "No platform data!\n"); 2790 v4l_err(client, "No platform data!\n");
2233 return -ENODEV; 2791 return -ENODEV;
2234 } 2792 }
2235 state->pdata = *pdata; 2793
2794 /* Request GPIOs. */
2795 for (i = 0; i < state->info->num_dv_ports; ++i) {
2796 state->hpd_gpio[i] =
2797 devm_gpiod_get_index(&client->dev, "hpd", i);
2798 if (IS_ERR(state->hpd_gpio[i]))
2799 continue;
2800
2801 gpiod_direction_output(state->hpd_gpio[i], 0);
2802
2803 v4l_info(client, "Handling HPD %u GPIO\n", i);
2804 }
2805
2236 state->timings = cea640x480; 2806 state->timings = cea640x480;
2807 state->format = adv7604_format_info(state, V4L2_MBUS_FMT_YUYV8_2X8);
2237 2808
2238 sd = &state->sd; 2809 sd = &state->sd;
2239 v4l2_i2c_subdev_init(sd, client, &adv7604_ops); 2810 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
2811 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
2812 id->name, i2c_adapter_id(client->adapter),
2813 client->addr);
2240 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 2814 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2241 2815
2242 /* i2c access to adv7604? */ 2816 /*
2243 if (adv_smbus_read_byte_data_check(client, 0xfb, false) != 0x68) { 2817 * Verify that the chip is present. On ADV7604 the RD_INFO register only
2244 v4l2_info(sd, "not an adv7604 on address 0x%x\n", 2818 * identifies the revision, while on ADV7611 it identifies the model as
2245 client->addr << 1); 2819 * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
2246 return -ENODEV; 2820 */
2821 if (state->info->type == ADV7604) {
2822 val = adv_smbus_read_byte_data_check(client, 0xfb, false);
2823 if (val != 0x68) {
2824 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
2825 client->addr << 1);
2826 return -ENODEV;
2827 }
2828 } else {
2829 val = (adv_smbus_read_byte_data_check(client, 0xea, false) << 8)
2830 | (adv_smbus_read_byte_data_check(client, 0xeb, false) << 0);
2831 if (val != 0x2051) {
2832 v4l2_info(sd, "not an adv7611 on address 0x%x\n",
2833 client->addr << 1);
2834 return -ENODEV;
2835 }
2247 } 2836 }
2248 2837
2249 /* control handlers */ 2838 /* control handlers */
2250 hdl = &state->hdl; 2839 hdl = &state->hdl;
2251 v4l2_ctrl_handler_init(hdl, 9); 2840 v4l2_ctrl_handler_init(hdl, adv7604_has_afe(state) ? 9 : 8);
2252 2841
2253 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops, 2842 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2254 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0); 2843 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
@@ -2261,15 +2850,17 @@ static int adv7604_probe(struct i2c_client *client,
2261 2850
2262 /* private controls */ 2851 /* private controls */
2263 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL, 2852 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
2264 V4L2_CID_DV_RX_POWER_PRESENT, 0, 0x0f, 0, 0); 2853 V4L2_CID_DV_RX_POWER_PRESENT, 0,
2854 (1 << state->info->num_dv_ports) - 1, 0, 0);
2265 state->rgb_quantization_range_ctrl = 2855 state->rgb_quantization_range_ctrl =
2266 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops, 2856 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
2267 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL, 2857 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2268 0, V4L2_DV_RGB_RANGE_AUTO); 2858 0, V4L2_DV_RGB_RANGE_AUTO);
2269 2859
2270 /* custom controls */ 2860 /* custom controls */
2271 state->analog_sampling_phase_ctrl = 2861 if (adv7604_has_afe(state))
2272 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL); 2862 state->analog_sampling_phase_ctrl =
2863 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
2273 state->free_run_color_manual_ctrl = 2864 state->free_run_color_manual_ctrl =
2274 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL); 2865 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
2275 state->free_run_color_ctrl = 2866 state->free_run_color_ctrl =
@@ -2282,7 +2873,8 @@ static int adv7604_probe(struct i2c_client *client,
2282 } 2873 }
2283 state->detect_tx_5v_ctrl->is_private = true; 2874 state->detect_tx_5v_ctrl->is_private = true;
2284 state->rgb_quantization_range_ctrl->is_private = true; 2875 state->rgb_quantization_range_ctrl->is_private = true;
2285 state->analog_sampling_phase_ctrl->is_private = true; 2876 if (adv7604_has_afe(state))
2877 state->analog_sampling_phase_ctrl->is_private = true;
2286 state->free_run_color_manual_ctrl->is_private = true; 2878 state->free_run_color_manual_ctrl->is_private = true;
2287 state->free_run_color_ctrl->is_private = true; 2879 state->free_run_color_ctrl->is_private = true;
2288 2880
@@ -2291,25 +2883,18 @@ static int adv7604_probe(struct i2c_client *client,
2291 goto err_hdl; 2883 goto err_hdl;
2292 } 2884 }
2293 2885
2294 state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3); 2886 for (i = 1; i < ADV7604_PAGE_MAX; ++i) {
2295 state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4); 2887 if (!(BIT(i) & state->info->page_mask))
2296 state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5); 2888 continue;
2297 state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6); 2889
2298 state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7); 2890 state->i2c_clients[i] =
2299 state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8); 2891 adv7604_dummy_client(sd, state->pdata.i2c_addresses[i],
2300 state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9); 2892 0xf2 + i);
2301 state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa); 2893 if (state->i2c_clients[i] == NULL) {
2302 state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb); 2894 err = -ENOMEM;
2303 state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc); 2895 v4l2_err(sd, "failed to create i2c client %u\n", i);
2304 state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd); 2896 goto err_i2c;
2305 state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe); 2897 }
2306 if (!state->i2c_avlink || !state->i2c_cec || !state->i2c_infoframe ||
2307 !state->i2c_esdp || !state->i2c_dpp || !state->i2c_afe ||
2308 !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
2309 !state->i2c_test || !state->i2c_cp || !state->i2c_vdp) {
2310 err = -ENOMEM;
2311 v4l2_err(sd, "failed to create all i2c clients\n");
2312 goto err_i2c;
2313 } 2898 }
2314 2899
2315 /* work queues */ 2900 /* work queues */
@@ -2323,8 +2908,14 @@ static int adv7604_probe(struct i2c_client *client,
2323 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug, 2908 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2324 adv7604_delayed_work_enable_hotplug); 2909 adv7604_delayed_work_enable_hotplug);
2325 2910
2326 state->pad.flags = MEDIA_PAD_FL_SOURCE; 2911 state->source_pad = state->info->num_dv_ports
2327 err = media_entity_init(&sd->entity, 1, &state->pad, 0); 2912 + (state->info->has_afe ? 2 : 0);
2913 for (i = 0; i < state->source_pad; ++i)
2914 state->pads[i].flags = MEDIA_PAD_FL_SINK;
2915 state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2916
2917 err = media_entity_init(&sd->entity, state->source_pad + 1,
2918 state->pads, 0);
2328 if (err) 2919 if (err)
2329 goto err_work_queues; 2920 goto err_work_queues;
2330 2921
@@ -2333,6 +2924,11 @@ static int adv7604_probe(struct i2c_client *client,
2333 goto err_entity; 2924 goto err_entity;
2334 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name, 2925 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2335 client->addr << 1, client->adapter->name); 2926 client->addr << 1, client->adapter->name);
2927
2928 err = v4l2_async_register_subdev(sd);
2929 if (err)
2930 goto err_entity;
2931
2336 return 0; 2932 return 0;
2337 2933
2338err_entity: 2934err_entity:
@@ -2356,6 +2952,7 @@ static int adv7604_remove(struct i2c_client *client)
2356 2952
2357 cancel_delayed_work(&state->delayed_work_enable_hotplug); 2953 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2358 destroy_workqueue(state->work_queues); 2954 destroy_workqueue(state->work_queues);
2955 v4l2_async_unregister_subdev(sd);
2359 v4l2_device_unregister_subdev(sd); 2956 v4l2_device_unregister_subdev(sd);
2360 media_entity_cleanup(&sd->entity); 2957 media_entity_cleanup(&sd->entity);
2361 adv7604_unregister_clients(to_state(sd)); 2958 adv7604_unregister_clients(to_state(sd));
@@ -2365,20 +2962,15 @@ static int adv7604_remove(struct i2c_client *client)
2365 2962
2366/* ----------------------------------------------------------------------- */ 2963/* ----------------------------------------------------------------------- */
2367 2964
2368static struct i2c_device_id adv7604_id[] = {
2369 { "adv7604", 0 },
2370 { }
2371};
2372MODULE_DEVICE_TABLE(i2c, adv7604_id);
2373
2374static struct i2c_driver adv7604_driver = { 2965static struct i2c_driver adv7604_driver = {
2375 .driver = { 2966 .driver = {
2376 .owner = THIS_MODULE, 2967 .owner = THIS_MODULE,
2377 .name = "adv7604", 2968 .name = "adv7604",
2969 .of_match_table = of_match_ptr(adv7604_of_id),
2378 }, 2970 },
2379 .probe = adv7604_probe, 2971 .probe = adv7604_probe,
2380 .remove = adv7604_remove, 2972 .remove = adv7604_remove,
2381 .id_table = adv7604_id, 2973 .id_table = adv7604_i2c_id,
2382}; 2974};
2383 2975
2384module_i2c_driver(adv7604_driver); 2976module_i2c_driver(adv7604_driver);
diff --git a/drivers/media/platform/Kconfig b/drivers/media/platform/Kconfig
index 20f1655e6d75..8108c698b548 100644
--- a/drivers/media/platform/Kconfig
+++ b/drivers/media/platform/Kconfig
@@ -93,7 +93,9 @@ config VIDEO_M32R_AR_M64278
93 93
94config VIDEO_OMAP3 94config VIDEO_OMAP3
95 tristate "OMAP 3 Camera support" 95 tristate "OMAP 3 Camera support"
96 depends on OMAP_IOVMM && VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API && ARCH_OMAP3 96 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API && ARCH_OMAP3
97 select ARM_DMA_USE_IOMMU
98 select OMAP_IOMMU
97 ---help--- 99 ---help---
98 Driver for an OMAP 3 camera controller. 100 Driver for an OMAP 3 camera controller.
99 101
diff --git a/drivers/media/platform/omap3isp/Makefile b/drivers/media/platform/omap3isp/Makefile
index e8847e79e31a..254975a9174e 100644
--- a/drivers/media/platform/omap3isp/Makefile
+++ b/drivers/media/platform/omap3isp/Makefile
@@ -3,7 +3,7 @@
3ccflags-$(CONFIG_VIDEO_OMAP3_DEBUG) += -DDEBUG 3ccflags-$(CONFIG_VIDEO_OMAP3_DEBUG) += -DDEBUG
4 4
5omap3-isp-objs += \ 5omap3-isp-objs += \
6 isp.o ispqueue.o ispvideo.o \ 6 isp.o ispvideo.o \
7 ispcsiphy.o ispccp2.o ispcsi2.o \ 7 ispcsiphy.o ispccp2.o ispcsi2.o \
8 ispccdc.o isppreview.o ispresizer.o \ 8 ispccdc.o isppreview.o ispresizer.o \
9 ispstat.o isph3a_aewb.o isph3a_af.o isphist.o 9 ispstat.o isph3a_aewb.o isph3a_af.o isphist.o
diff --git a/drivers/media/platform/omap3isp/isp.c b/drivers/media/platform/omap3isp/isp.c
index 06a0df434249..2c7aa6720569 100644
--- a/drivers/media/platform/omap3isp/isp.c
+++ b/drivers/media/platform/omap3isp/isp.c
@@ -69,6 +69,8 @@
69#include <linux/sched.h> 69#include <linux/sched.h>
70#include <linux/vmalloc.h> 70#include <linux/vmalloc.h>
71 71
72#include <asm/dma-iommu.h>
73
72#include <media/v4l2-common.h> 74#include <media/v4l2-common.h>
73#include <media/v4l2-device.h> 75#include <media/v4l2-device.h>
74 76
@@ -1397,14 +1399,14 @@ int omap3isp_module_sync_idle(struct media_entity *me, wait_queue_head_t *wait,
1397 if (isp_pipeline_is_last(me)) { 1399 if (isp_pipeline_is_last(me)) {
1398 struct isp_video *video = pipe->output; 1400 struct isp_video *video = pipe->output;
1399 unsigned long flags; 1401 unsigned long flags;
1400 spin_lock_irqsave(&video->queue->irqlock, flags); 1402 spin_lock_irqsave(&video->irqlock, flags);
1401 if (video->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_UNDERRUN) { 1403 if (video->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_UNDERRUN) {
1402 spin_unlock_irqrestore(&video->queue->irqlock, flags); 1404 spin_unlock_irqrestore(&video->irqlock, flags);
1403 atomic_set(stopping, 0); 1405 atomic_set(stopping, 0);
1404 smp_mb(); 1406 smp_mb();
1405 return 0; 1407 return 0;
1406 } 1408 }
1407 spin_unlock_irqrestore(&video->queue->irqlock, flags); 1409 spin_unlock_irqrestore(&video->irqlock, flags);
1408 if (!wait_event_timeout(*wait, !atomic_read(stopping), 1410 if (!wait_event_timeout(*wait, !atomic_read(stopping),
1409 msecs_to_jiffies(1000))) { 1411 msecs_to_jiffies(1000))) {
1410 atomic_set(stopping, 0); 1412 atomic_set(stopping, 0);
@@ -1625,7 +1627,7 @@ struct isp_device *omap3isp_get(struct isp_device *isp)
1625 * Decrement the reference count on the ISP. If the last reference is released, 1627 * Decrement the reference count on the ISP. If the last reference is released,
1626 * power-down all submodules, disable clocks and free temporary buffers. 1628 * power-down all submodules, disable clocks and free temporary buffers.
1627 */ 1629 */
1628void omap3isp_put(struct isp_device *isp) 1630static void __omap3isp_put(struct isp_device *isp, bool save_ctx)
1629{ 1631{
1630 if (isp == NULL) 1632 if (isp == NULL)
1631 return; 1633 return;
@@ -1634,7 +1636,7 @@ void omap3isp_put(struct isp_device *isp)
1634 BUG_ON(isp->ref_count == 0); 1636 BUG_ON(isp->ref_count == 0);
1635 if (--isp->ref_count == 0) { 1637 if (--isp->ref_count == 0) {
1636 isp_disable_interrupts(isp); 1638 isp_disable_interrupts(isp);
1637 if (isp->domain) { 1639 if (save_ctx) {
1638 isp_save_ctx(isp); 1640 isp_save_ctx(isp);
1639 isp->has_context = 1; 1641 isp->has_context = 1;
1640 } 1642 }
@@ -1648,6 +1650,11 @@ void omap3isp_put(struct isp_device *isp)
1648 mutex_unlock(&isp->isp_mutex); 1650 mutex_unlock(&isp->isp_mutex);
1649} 1651}
1650 1652
1653void omap3isp_put(struct isp_device *isp)
1654{
1655 __omap3isp_put(isp, true);
1656}
1657
1651/* -------------------------------------------------------------------------- 1658/* --------------------------------------------------------------------------
1652 * Platform device driver 1659 * Platform device driver
1653 */ 1660 */
@@ -2120,6 +2127,61 @@ error_csiphy:
2120 return ret; 2127 return ret;
2121} 2128}
2122 2129
2130static void isp_detach_iommu(struct isp_device *isp)
2131{
2132 arm_iommu_release_mapping(isp->mapping);
2133 isp->mapping = NULL;
2134 iommu_group_remove_device(isp->dev);
2135}
2136
2137static int isp_attach_iommu(struct isp_device *isp)
2138{
2139 struct dma_iommu_mapping *mapping;
2140 struct iommu_group *group;
2141 int ret;
2142
2143 /* Create a device group and add the device to it. */
2144 group = iommu_group_alloc();
2145 if (IS_ERR(group)) {
2146 dev_err(isp->dev, "failed to allocate IOMMU group\n");
2147 return PTR_ERR(group);
2148 }
2149
2150 ret = iommu_group_add_device(group, isp->dev);
2151 iommu_group_put(group);
2152
2153 if (ret < 0) {
2154 dev_err(isp->dev, "failed to add device to IPMMU group\n");
2155 return ret;
2156 }
2157
2158 /*
2159 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
2160 * VAs. This will allocate a corresponding IOMMU domain.
2161 */
2162 mapping = arm_iommu_create_mapping(&platform_bus_type, SZ_1G, SZ_2G);
2163 if (IS_ERR(mapping)) {
2164 dev_err(isp->dev, "failed to create ARM IOMMU mapping\n");
2165 ret = PTR_ERR(mapping);
2166 goto error;
2167 }
2168
2169 isp->mapping = mapping;
2170
2171 /* Attach the ARM VA mapping to the device. */
2172 ret = arm_iommu_attach_device(isp->dev, mapping);
2173 if (ret < 0) {
2174 dev_err(isp->dev, "failed to attach device to VA mapping\n");
2175 goto error;
2176 }
2177
2178 return 0;
2179
2180error:
2181 isp_detach_iommu(isp);
2182 return ret;
2183}
2184
2123/* 2185/*
2124 * isp_remove - Remove ISP platform device 2186 * isp_remove - Remove ISP platform device
2125 * @pdev: Pointer to ISP platform device 2187 * @pdev: Pointer to ISP platform device
@@ -2135,10 +2197,8 @@ static int isp_remove(struct platform_device *pdev)
2135 isp_xclk_cleanup(isp); 2197 isp_xclk_cleanup(isp);
2136 2198
2137 __omap3isp_get(isp, false); 2199 __omap3isp_get(isp, false);
2138 iommu_detach_device(isp->domain, &pdev->dev); 2200 isp_detach_iommu(isp);
2139 iommu_domain_free(isp->domain); 2201 __omap3isp_put(isp, false);
2140 isp->domain = NULL;
2141 omap3isp_put(isp);
2142 2202
2143 return 0; 2203 return 0;
2144} 2204}
@@ -2265,39 +2325,32 @@ static int isp_probe(struct platform_device *pdev)
2265 } 2325 }
2266 } 2326 }
2267 2327
2268 isp->domain = iommu_domain_alloc(pdev->dev.bus); 2328 /* IOMMU */
2269 if (!isp->domain) { 2329 ret = isp_attach_iommu(isp);
2270 dev_err(isp->dev, "can't alloc iommu domain\n"); 2330 if (ret < 0) {
2271 ret = -ENOMEM; 2331 dev_err(&pdev->dev, "unable to attach to IOMMU\n");
2272 goto error_isp; 2332 goto error_isp;
2273 } 2333 }
2274 2334
2275 ret = iommu_attach_device(isp->domain, &pdev->dev);
2276 if (ret) {
2277 dev_err(&pdev->dev, "can't attach iommu device: %d\n", ret);
2278 ret = -EPROBE_DEFER;
2279 goto free_domain;
2280 }
2281
2282 /* Interrupt */ 2335 /* Interrupt */
2283 isp->irq_num = platform_get_irq(pdev, 0); 2336 isp->irq_num = platform_get_irq(pdev, 0);
2284 if (isp->irq_num <= 0) { 2337 if (isp->irq_num <= 0) {
2285 dev_err(isp->dev, "No IRQ resource\n"); 2338 dev_err(isp->dev, "No IRQ resource\n");
2286 ret = -ENODEV; 2339 ret = -ENODEV;
2287 goto detach_dev; 2340 goto error_iommu;
2288 } 2341 }
2289 2342
2290 if (devm_request_irq(isp->dev, isp->irq_num, isp_isr, IRQF_SHARED, 2343 if (devm_request_irq(isp->dev, isp->irq_num, isp_isr, IRQF_SHARED,
2291 "OMAP3 ISP", isp)) { 2344 "OMAP3 ISP", isp)) {
2292 dev_err(isp->dev, "Unable to request IRQ\n"); 2345 dev_err(isp->dev, "Unable to request IRQ\n");
2293 ret = -EINVAL; 2346 ret = -EINVAL;
2294 goto detach_dev; 2347 goto error_iommu;
2295 } 2348 }
2296 2349
2297 /* Entities */ 2350 /* Entities */
2298 ret = isp_initialize_modules(isp); 2351 ret = isp_initialize_modules(isp);
2299 if (ret < 0) 2352 if (ret < 0)
2300 goto detach_dev; 2353 goto error_iommu;
2301 2354
2302 ret = isp_register_entities(isp); 2355 ret = isp_register_entities(isp);
2303 if (ret < 0) 2356 if (ret < 0)
@@ -2310,14 +2363,11 @@ static int isp_probe(struct platform_device *pdev)
2310 2363
2311error_modules: 2364error_modules:
2312 isp_cleanup_modules(isp); 2365 isp_cleanup_modules(isp);
2313detach_dev: 2366error_iommu:
2314 iommu_detach_device(isp->domain, &pdev->dev); 2367 isp_detach_iommu(isp);
2315free_domain:
2316 iommu_domain_free(isp->domain);
2317 isp->domain = NULL;
2318error_isp: 2368error_isp:
2319 isp_xclk_cleanup(isp); 2369 isp_xclk_cleanup(isp);
2320 omap3isp_put(isp); 2370 __omap3isp_put(isp, false);
2321error: 2371error:
2322 mutex_destroy(&isp->isp_mutex); 2372 mutex_destroy(&isp->isp_mutex);
2323 2373
diff --git a/drivers/media/platform/omap3isp/isp.h b/drivers/media/platform/omap3isp/isp.h
index 6d5e69711907..2c314eea1252 100644
--- a/drivers/media/platform/omap3isp/isp.h
+++ b/drivers/media/platform/omap3isp/isp.h
@@ -45,8 +45,6 @@
45#include "ispcsi2.h" 45#include "ispcsi2.h"
46#include "ispccp2.h" 46#include "ispccp2.h"
47 47
48#define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
49
50#define ISP_TOK_TERM 0xFFFFFFFF /* 48#define ISP_TOK_TERM 0xFFFFFFFF /*
51 * terminating token for ISP 49 * terminating token for ISP
52 * modules reg list 50 * modules reg list
@@ -152,6 +150,7 @@ struct isp_xclk {
152 * regions. 150 * regions.
153 * @mmio_base_phys: Array with physical L4 bus addresses for ISP register 151 * @mmio_base_phys: Array with physical L4 bus addresses for ISP register
154 * regions. 152 * regions.
153 * @mapping: IOMMU mapping
155 * @stat_lock: Spinlock for handling statistics 154 * @stat_lock: Spinlock for handling statistics
156 * @isp_mutex: Mutex for serializing requests to ISP. 155 * @isp_mutex: Mutex for serializing requests to ISP.
157 * @stop_failure: Indicates that an entity failed to stop. 156 * @stop_failure: Indicates that an entity failed to stop.
@@ -171,7 +170,6 @@ struct isp_xclk {
171 * @isp_res: Pointer to current settings for ISP Resizer. 170 * @isp_res: Pointer to current settings for ISP Resizer.
172 * @isp_prev: Pointer to current settings for ISP Preview. 171 * @isp_prev: Pointer to current settings for ISP Preview.
173 * @isp_ccdc: Pointer to current settings for ISP CCDC. 172 * @isp_ccdc: Pointer to current settings for ISP CCDC.
174 * @iommu: Pointer to requested IOMMU instance for ISP.
175 * @platform_cb: ISP driver callback function pointers for platform code 173 * @platform_cb: ISP driver callback function pointers for platform code
176 * 174 *
177 * This structure is used to store the OMAP ISP Information. 175 * This structure is used to store the OMAP ISP Information.
@@ -189,6 +187,8 @@ struct isp_device {
189 void __iomem *mmio_base[OMAP3_ISP_IOMEM_LAST]; 187 void __iomem *mmio_base[OMAP3_ISP_IOMEM_LAST];
190 unsigned long mmio_base_phys[OMAP3_ISP_IOMEM_LAST]; 188 unsigned long mmio_base_phys[OMAP3_ISP_IOMEM_LAST];
191 189
190 struct dma_iommu_mapping *mapping;
191
192 /* ISP Obj */ 192 /* ISP Obj */
193 spinlock_t stat_lock; /* common lock for statistic drivers */ 193 spinlock_t stat_lock; /* common lock for statistic drivers */
194 struct mutex isp_mutex; /* For handling ref_count field */ 194 struct mutex isp_mutex; /* For handling ref_count field */
@@ -219,8 +219,6 @@ struct isp_device {
219 219
220 unsigned int sbl_resources; 220 unsigned int sbl_resources;
221 unsigned int subclk_resources; 221 unsigned int subclk_resources;
222
223 struct iommu_domain *domain;
224}; 222};
225 223
226#define v4l2_dev_to_isp_device(dev) \ 224#define v4l2_dev_to_isp_device(dev) \
diff --git a/drivers/media/platform/omap3isp/ispccdc.c b/drivers/media/platform/omap3isp/ispccdc.c
index 4d920c800ff5..9f727d20f06d 100644
--- a/drivers/media/platform/omap3isp/ispccdc.c
+++ b/drivers/media/platform/omap3isp/ispccdc.c
@@ -30,7 +30,6 @@
30#include <linux/device.h> 30#include <linux/device.h>
31#include <linux/dma-mapping.h> 31#include <linux/dma-mapping.h>
32#include <linux/mm.h> 32#include <linux/mm.h>
33#include <linux/omap-iommu.h>
34#include <linux/sched.h> 33#include <linux/sched.h>
35#include <linux/slab.h> 34#include <linux/slab.h>
36#include <media/v4l2-event.h> 35#include <media/v4l2-event.h>
@@ -206,7 +205,8 @@ static int ccdc_lsc_validate_config(struct isp_ccdc_device *ccdc,
206 * ccdc_lsc_program_table - Program Lens Shading Compensation table address. 205 * ccdc_lsc_program_table - Program Lens Shading Compensation table address.
207 * @ccdc: Pointer to ISP CCDC device. 206 * @ccdc: Pointer to ISP CCDC device.
208 */ 207 */
209static void ccdc_lsc_program_table(struct isp_ccdc_device *ccdc, u32 addr) 208static void ccdc_lsc_program_table(struct isp_ccdc_device *ccdc,
209 dma_addr_t addr)
210{ 210{
211 isp_reg_writel(to_isp_device(ccdc), addr, 211 isp_reg_writel(to_isp_device(ccdc), addr,
212 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_TABLE_BASE); 212 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_TABLE_BASE);
@@ -333,7 +333,7 @@ static int __ccdc_lsc_configure(struct isp_ccdc_device *ccdc,
333 return -EBUSY; 333 return -EBUSY;
334 334
335 ccdc_lsc_setup_regs(ccdc, &req->config); 335 ccdc_lsc_setup_regs(ccdc, &req->config);
336 ccdc_lsc_program_table(ccdc, req->table); 336 ccdc_lsc_program_table(ccdc, req->table.dma);
337 return 0; 337 return 0;
338} 338}
339 339
@@ -368,11 +368,12 @@ static void ccdc_lsc_free_request(struct isp_ccdc_device *ccdc,
368 if (req == NULL) 368 if (req == NULL)
369 return; 369 return;
370 370
371 if (req->iovm) 371 if (req->table.addr) {
372 dma_unmap_sg(isp->dev, req->iovm->sgt->sgl, 372 sg_free_table(&req->table.sgt);
373 req->iovm->sgt->nents, DMA_TO_DEVICE); 373 dma_free_coherent(isp->dev, req->config.size, req->table.addr,
374 if (req->table) 374 req->table.dma);
375 omap_iommu_vfree(isp->domain, isp->dev, req->table); 375 }
376
376 kfree(req); 377 kfree(req);
377} 378}
378 379
@@ -416,7 +417,6 @@ static int ccdc_lsc_config(struct isp_ccdc_device *ccdc,
416 struct isp_device *isp = to_isp_device(ccdc); 417 struct isp_device *isp = to_isp_device(ccdc);
417 struct ispccdc_lsc_config_req *req; 418 struct ispccdc_lsc_config_req *req;
418 unsigned long flags; 419 unsigned long flags;
419 void *table;
420 u16 update; 420 u16 update;
421 int ret; 421 int ret;
422 422
@@ -444,38 +444,31 @@ static int ccdc_lsc_config(struct isp_ccdc_device *ccdc,
444 444
445 req->enable = 1; 445 req->enable = 1;
446 446
447 req->table = omap_iommu_vmalloc(isp->domain, isp->dev, 0, 447 req->table.addr = dma_alloc_coherent(isp->dev, req->config.size,
448 req->config.size, IOMMU_FLAG); 448 &req->table.dma,
449 if (IS_ERR_VALUE(req->table)) { 449 GFP_KERNEL);
450 req->table = 0; 450 if (req->table.addr == NULL) {
451 ret = -ENOMEM;
452 goto done;
453 }
454
455 req->iovm = omap_find_iovm_area(isp->dev, req->table);
456 if (req->iovm == NULL) {
457 ret = -ENOMEM; 451 ret = -ENOMEM;
458 goto done; 452 goto done;
459 } 453 }
460 454
461 if (!dma_map_sg(isp->dev, req->iovm->sgt->sgl, 455 ret = dma_get_sgtable(isp->dev, &req->table.sgt,
462 req->iovm->sgt->nents, DMA_TO_DEVICE)) { 456 req->table.addr, req->table.dma,
463 ret = -ENOMEM; 457 req->config.size);
464 req->iovm = NULL; 458 if (ret < 0)
465 goto done; 459 goto done;
466 }
467 460
468 dma_sync_sg_for_cpu(isp->dev, req->iovm->sgt->sgl, 461 dma_sync_sg_for_cpu(isp->dev, req->table.sgt.sgl,
469 req->iovm->sgt->nents, DMA_TO_DEVICE); 462 req->table.sgt.nents, DMA_TO_DEVICE);
470 463
471 table = omap_da_to_va(isp->dev, req->table); 464 if (copy_from_user(req->table.addr, config->lsc,
472 if (copy_from_user(table, config->lsc, req->config.size)) { 465 req->config.size)) {
473 ret = -EFAULT; 466 ret = -EFAULT;
474 goto done; 467 goto done;
475 } 468 }
476 469
477 dma_sync_sg_for_device(isp->dev, req->iovm->sgt->sgl, 470 dma_sync_sg_for_device(isp->dev, req->table.sgt.sgl,
478 req->iovm->sgt->nents, DMA_TO_DEVICE); 471 req->table.sgt.nents, DMA_TO_DEVICE);
479 } 472 }
480 473
481 spin_lock_irqsave(&ccdc->lsc.req_lock, flags); 474 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
@@ -584,7 +577,7 @@ static void ccdc_configure_fpc(struct isp_ccdc_device *ccdc)
584 if (!ccdc->fpc_en) 577 if (!ccdc->fpc_en)
585 return; 578 return;
586 579
587 isp_reg_writel(isp, ccdc->fpc.fpcaddr, OMAP3_ISP_IOMEM_CCDC, 580 isp_reg_writel(isp, ccdc->fpc.dma, OMAP3_ISP_IOMEM_CCDC,
588 ISPCCDC_FPC_ADDR); 581 ISPCCDC_FPC_ADDR);
589 /* The FPNUM field must be set before enabling FPC. */ 582 /* The FPNUM field must be set before enabling FPC. */
590 isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT), 583 isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT),
@@ -724,8 +717,9 @@ static int ccdc_config(struct isp_ccdc_device *ccdc,
724 ccdc->shadow_update = 0; 717 ccdc->shadow_update = 0;
725 718
726 if (OMAP3ISP_CCDC_FPC & ccdc_struct->update) { 719 if (OMAP3ISP_CCDC_FPC & ccdc_struct->update) {
727 u32 table_old = 0; 720 struct omap3isp_ccdc_fpc fpc;
728 u32 table_new; 721 struct ispccdc_fpc fpc_old = { .addr = NULL, };
722 struct ispccdc_fpc fpc_new;
729 u32 size; 723 u32 size;
730 724
731 if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED) 725 if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
@@ -734,35 +728,39 @@ static int ccdc_config(struct isp_ccdc_device *ccdc,
734 ccdc->fpc_en = !!(OMAP3ISP_CCDC_FPC & ccdc_struct->flag); 728 ccdc->fpc_en = !!(OMAP3ISP_CCDC_FPC & ccdc_struct->flag);
735 729
736 if (ccdc->fpc_en) { 730 if (ccdc->fpc_en) {
737 if (copy_from_user(&ccdc->fpc, ccdc_struct->fpc, 731 if (copy_from_user(&fpc, ccdc_struct->fpc, sizeof(fpc)))
738 sizeof(ccdc->fpc)))
739 return -EFAULT; 732 return -EFAULT;
740 733
734 size = fpc.fpnum * 4;
735
741 /* 736 /*
742 * table_new must be 64-bytes aligned, but it's 737 * The table address must be 64-bytes aligned, which is
743 * already done by omap_iommu_vmalloc(). 738 * guaranteed by dma_alloc_coherent().
744 */ 739 */
745 size = ccdc->fpc.fpnum * 4; 740 fpc_new.fpnum = fpc.fpnum;
746 table_new = omap_iommu_vmalloc(isp->domain, isp->dev, 741 fpc_new.addr = dma_alloc_coherent(isp->dev, size,
747 0, size, IOMMU_FLAG); 742 &fpc_new.dma,
748 if (IS_ERR_VALUE(table_new)) 743 GFP_KERNEL);
744 if (fpc_new.addr == NULL)
749 return -ENOMEM; 745 return -ENOMEM;
750 746
751 if (copy_from_user(omap_da_to_va(isp->dev, table_new), 747 if (copy_from_user(fpc_new.addr,
752 (__force void __user *) 748 (__force void __user *)fpc.fpcaddr,
753 ccdc->fpc.fpcaddr, size)) { 749 size)) {
754 omap_iommu_vfree(isp->domain, isp->dev, 750 dma_free_coherent(isp->dev, size, fpc_new.addr,
755 table_new); 751 fpc_new.dma);
756 return -EFAULT; 752 return -EFAULT;
757 } 753 }
758 754
759 table_old = ccdc->fpc.fpcaddr; 755 fpc_old = ccdc->fpc;
760 ccdc->fpc.fpcaddr = table_new; 756 ccdc->fpc = fpc_new;
761 } 757 }
762 758
763 ccdc_configure_fpc(ccdc); 759 ccdc_configure_fpc(ccdc);
764 if (table_old != 0) 760
765 omap_iommu_vfree(isp->domain, isp->dev, table_old); 761 if (fpc_old.addr != NULL)
762 dma_free_coherent(isp->dev, fpc_old.fpnum * 4,
763 fpc_old.addr, fpc_old.dma);
766 } 764 }
767 765
768 return ccdc_lsc_config(ccdc, ccdc_struct); 766 return ccdc_lsc_config(ccdc, ccdc_struct);
@@ -1523,7 +1521,7 @@ static int ccdc_isr_buffer(struct isp_ccdc_device *ccdc)
1523 1521
1524 buffer = omap3isp_video_buffer_next(&ccdc->video_out); 1522 buffer = omap3isp_video_buffer_next(&ccdc->video_out);
1525 if (buffer != NULL) { 1523 if (buffer != NULL) {
1526 ccdc_set_outaddr(ccdc, buffer->isp_addr); 1524 ccdc_set_outaddr(ccdc, buffer->dma);
1527 restart = 1; 1525 restart = 1;
1528 } 1526 }
1529 1527
@@ -1662,7 +1660,7 @@ static int ccdc_video_queue(struct isp_video *video, struct isp_buffer *buffer)
1662 if (!(ccdc->output & CCDC_OUTPUT_MEMORY)) 1660 if (!(ccdc->output & CCDC_OUTPUT_MEMORY))
1663 return -ENODEV; 1661 return -ENODEV;
1664 1662
1665 ccdc_set_outaddr(ccdc, buffer->isp_addr); 1663 ccdc_set_outaddr(ccdc, buffer->dma);
1666 1664
1667 /* We now have a buffer queued on the output, restart the pipeline 1665 /* We now have a buffer queued on the output, restart the pipeline
1668 * on the next CCDC interrupt if running in continuous mode (or when 1666 * on the next CCDC interrupt if running in continuous mode (or when
@@ -2580,8 +2578,9 @@ void omap3isp_ccdc_cleanup(struct isp_device *isp)
2580 cancel_work_sync(&ccdc->lsc.table_work); 2578 cancel_work_sync(&ccdc->lsc.table_work);
2581 ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue); 2579 ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
2582 2580
2583 if (ccdc->fpc.fpcaddr != 0) 2581 if (ccdc->fpc.addr != NULL)
2584 omap_iommu_vfree(isp->domain, isp->dev, ccdc->fpc.fpcaddr); 2582 dma_free_coherent(isp->dev, ccdc->fpc.fpnum * 4, ccdc->fpc.addr,
2583 ccdc->fpc.dma);
2585 2584
2586 mutex_destroy(&ccdc->ioctl_lock); 2585 mutex_destroy(&ccdc->ioctl_lock);
2587} 2586}
diff --git a/drivers/media/platform/omap3isp/ispccdc.h b/drivers/media/platform/omap3isp/ispccdc.h
index 9d24e4107864..f65061602c71 100644
--- a/drivers/media/platform/omap3isp/ispccdc.h
+++ b/drivers/media/platform/omap3isp/ispccdc.h
@@ -46,6 +46,12 @@ enum ccdc_input_entity {
46 46
47#define OMAP3ISP_CCDC_NEVENTS 16 47#define OMAP3ISP_CCDC_NEVENTS 16
48 48
49struct ispccdc_fpc {
50 void *addr;
51 dma_addr_t dma;
52 unsigned int fpnum;
53};
54
49enum ispccdc_lsc_state { 55enum ispccdc_lsc_state {
50 LSC_STATE_STOPPED = 0, 56 LSC_STATE_STOPPED = 0,
51 LSC_STATE_STOPPING = 1, 57 LSC_STATE_STOPPING = 1,
@@ -57,8 +63,12 @@ struct ispccdc_lsc_config_req {
57 struct list_head list; 63 struct list_head list;
58 struct omap3isp_ccdc_lsc_config config; 64 struct omap3isp_ccdc_lsc_config config;
59 unsigned char enable; 65 unsigned char enable;
60 u32 table; 66
61 struct iovm_struct *iovm; 67 struct {
68 void *addr;
69 dma_addr_t dma;
70 struct sg_table sgt;
71 } table;
62}; 72};
63 73
64/* 74/*
@@ -136,7 +146,7 @@ struct isp_ccdc_device {
136 fpc_en:1; 146 fpc_en:1;
137 struct omap3isp_ccdc_blcomp blcomp; 147 struct omap3isp_ccdc_blcomp blcomp;
138 struct omap3isp_ccdc_bclamp clamp; 148 struct omap3isp_ccdc_bclamp clamp;
139 struct omap3isp_ccdc_fpc fpc; 149 struct ispccdc_fpc fpc;
140 struct ispccdc_lsc lsc; 150 struct ispccdc_lsc lsc;
141 unsigned int update; 151 unsigned int update;
142 unsigned int shadow_update; 152 unsigned int shadow_update;
diff --git a/drivers/media/platform/omap3isp/ispccp2.c b/drivers/media/platform/omap3isp/ispccp2.c
index b30b67d22a58..f3801db9095c 100644
--- a/drivers/media/platform/omap3isp/ispccp2.c
+++ b/drivers/media/platform/omap3isp/ispccp2.c
@@ -549,7 +549,7 @@ static void ccp2_isr_buffer(struct isp_ccp2_device *ccp2)
549 549
550 buffer = omap3isp_video_buffer_next(&ccp2->video_in); 550 buffer = omap3isp_video_buffer_next(&ccp2->video_in);
551 if (buffer != NULL) 551 if (buffer != NULL)
552 ccp2_set_inaddr(ccp2, buffer->isp_addr); 552 ccp2_set_inaddr(ccp2, buffer->dma);
553 553
554 pipe->state |= ISP_PIPELINE_IDLE_INPUT; 554 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
555 555
@@ -940,7 +940,7 @@ static int ccp2_video_queue(struct isp_video *video, struct isp_buffer *buffer)
940{ 940{
941 struct isp_ccp2_device *ccp2 = &video->isp->isp_ccp2; 941 struct isp_ccp2_device *ccp2 = &video->isp->isp_ccp2;
942 942
943 ccp2_set_inaddr(ccp2, buffer->isp_addr); 943 ccp2_set_inaddr(ccp2, buffer->dma);
944 return 0; 944 return 0;
945} 945}
946 946
diff --git a/drivers/media/platform/omap3isp/ispcsi2.c b/drivers/media/platform/omap3isp/ispcsi2.c
index 620560828a48..5a2e47e58b84 100644
--- a/drivers/media/platform/omap3isp/ispcsi2.c
+++ b/drivers/media/platform/omap3isp/ispcsi2.c
@@ -695,7 +695,7 @@ static void csi2_isr_buffer(struct isp_csi2_device *csi2)
695 if (buffer == NULL) 695 if (buffer == NULL)
696 return; 696 return;
697 697
698 csi2_set_outaddr(csi2, buffer->isp_addr); 698 csi2_set_outaddr(csi2, buffer->dma);
699 csi2_ctx_enable(isp, csi2, 0, 1); 699 csi2_ctx_enable(isp, csi2, 0, 1);
700} 700}
701 701
@@ -812,7 +812,7 @@ static int csi2_queue(struct isp_video *video, struct isp_buffer *buffer)
812 struct isp_device *isp = video->isp; 812 struct isp_device *isp = video->isp;
813 struct isp_csi2_device *csi2 = &isp->isp_csi2a; 813 struct isp_csi2_device *csi2 = &isp->isp_csi2a;
814 814
815 csi2_set_outaddr(csi2, buffer->isp_addr); 815 csi2_set_outaddr(csi2, buffer->dma);
816 816
817 /* 817 /*
818 * If streaming was enabled before there was a buffer queued 818 * If streaming was enabled before there was a buffer queued
diff --git a/drivers/media/platform/omap3isp/isph3a_aewb.c b/drivers/media/platform/omap3isp/isph3a_aewb.c
index 75fd82b152ba..d6811ce263eb 100644
--- a/drivers/media/platform/omap3isp/isph3a_aewb.c
+++ b/drivers/media/platform/omap3isp/isph3a_aewb.c
@@ -47,7 +47,7 @@ static void h3a_aewb_setup_regs(struct ispstat *aewb, void *priv)
47 if (aewb->state == ISPSTAT_DISABLED) 47 if (aewb->state == ISPSTAT_DISABLED)
48 return; 48 return;
49 49
50 isp_reg_writel(aewb->isp, aewb->active_buf->iommu_addr, 50 isp_reg_writel(aewb->isp, aewb->active_buf->dma_addr,
51 OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWBUFST); 51 OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWBUFST);
52 52
53 if (!aewb->update) 53 if (!aewb->update)
diff --git a/drivers/media/platform/omap3isp/isph3a_af.c b/drivers/media/platform/omap3isp/isph3a_af.c
index a0bf5af32438..6fc960cd30f5 100644
--- a/drivers/media/platform/omap3isp/isph3a_af.c
+++ b/drivers/media/platform/omap3isp/isph3a_af.c
@@ -51,7 +51,7 @@ static void h3a_af_setup_regs(struct ispstat *af, void *priv)
51 if (af->state == ISPSTAT_DISABLED) 51 if (af->state == ISPSTAT_DISABLED)
52 return; 52 return;
53 53
54 isp_reg_writel(af->isp, af->active_buf->iommu_addr, OMAP3_ISP_IOMEM_H3A, 54 isp_reg_writel(af->isp, af->active_buf->dma_addr, OMAP3_ISP_IOMEM_H3A,
55 ISPH3A_AFBUFST); 55 ISPH3A_AFBUFST);
56 56
57 if (!af->update) 57 if (!af->update)
diff --git a/drivers/media/platform/omap3isp/isppreview.c b/drivers/media/platform/omap3isp/isppreview.c
index 395b2b068c75..720809b07e75 100644
--- a/drivers/media/platform/omap3isp/isppreview.c
+++ b/drivers/media/platform/omap3isp/isppreview.c
@@ -1499,14 +1499,14 @@ static void preview_isr_buffer(struct isp_prev_device *prev)
1499 if (prev->input == PREVIEW_INPUT_MEMORY) { 1499 if (prev->input == PREVIEW_INPUT_MEMORY) {
1500 buffer = omap3isp_video_buffer_next(&prev->video_in); 1500 buffer = omap3isp_video_buffer_next(&prev->video_in);
1501 if (buffer != NULL) 1501 if (buffer != NULL)
1502 preview_set_inaddr(prev, buffer->isp_addr); 1502 preview_set_inaddr(prev, buffer->dma);
1503 pipe->state |= ISP_PIPELINE_IDLE_INPUT; 1503 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1504 } 1504 }
1505 1505
1506 if (prev->output & PREVIEW_OUTPUT_MEMORY) { 1506 if (prev->output & PREVIEW_OUTPUT_MEMORY) {
1507 buffer = omap3isp_video_buffer_next(&prev->video_out); 1507 buffer = omap3isp_video_buffer_next(&prev->video_out);
1508 if (buffer != NULL) { 1508 if (buffer != NULL) {
1509 preview_set_outaddr(prev, buffer->isp_addr); 1509 preview_set_outaddr(prev, buffer->dma);
1510 restart = 1; 1510 restart = 1;
1511 } 1511 }
1512 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT; 1512 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
@@ -1577,10 +1577,10 @@ static int preview_video_queue(struct isp_video *video,
1577 struct isp_prev_device *prev = &video->isp->isp_prev; 1577 struct isp_prev_device *prev = &video->isp->isp_prev;
1578 1578
1579 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) 1579 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1580 preview_set_inaddr(prev, buffer->isp_addr); 1580 preview_set_inaddr(prev, buffer->dma);
1581 1581
1582 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1582 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1583 preview_set_outaddr(prev, buffer->isp_addr); 1583 preview_set_outaddr(prev, buffer->dma);
1584 1584
1585 return 0; 1585 return 0;
1586} 1586}
diff --git a/drivers/media/platform/omap3isp/ispqueue.c b/drivers/media/platform/omap3isp/ispqueue.c
deleted file mode 100644
index a5e65858e799..000000000000
--- a/drivers/media/platform/omap3isp/ispqueue.c
+++ /dev/null
@@ -1,1161 +0,0 @@
1/*
2 * ispqueue.c
3 *
4 * TI OMAP3 ISP - Video buffers queue handling
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26#include <asm/cacheflush.h>
27#include <linux/dma-mapping.h>
28#include <linux/mm.h>
29#include <linux/pagemap.h>
30#include <linux/poll.h>
31#include <linux/scatterlist.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/vmalloc.h>
35
36#include "ispqueue.h"
37
38/* -----------------------------------------------------------------------------
39 * Video buffers management
40 */
41
42/*
43 * isp_video_buffer_cache_sync - Keep the buffers coherent between CPU and ISP
44 *
45 * The typical operation required here is Cache Invalidation across
46 * the (user space) buffer address range. And this _must_ be done
47 * at QBUF stage (and *only* at QBUF).
48 *
49 * We try to use optimal cache invalidation function:
50 * - dmac_map_area:
51 * - used when the number of pages are _low_.
52 * - it becomes quite slow as the number of pages increase.
53 * - for 648x492 viewfinder (150 pages) it takes 1.3 ms.
54 * - for 5 Mpix buffer (2491 pages) it takes between 25-50 ms.
55 *
56 * - flush_cache_all:
57 * - used when the number of pages are _high_.
58 * - time taken in the range of 500-900 us.
59 * - has a higher penalty but, as whole dcache + icache is invalidated
60 */
61/*
62 * FIXME: dmac_inv_range crashes randomly on the user space buffer
63 * address. Fall back to flush_cache_all for now.
64 */
65#define ISP_CACHE_FLUSH_PAGES_MAX 0
66
67static void isp_video_buffer_cache_sync(struct isp_video_buffer *buf)
68{
69 if (buf->skip_cache)
70 return;
71
72 if (buf->vbuf.m.userptr == 0 || buf->npages == 0 ||
73 buf->npages > ISP_CACHE_FLUSH_PAGES_MAX)
74 flush_cache_all();
75 else {
76 dmac_map_area((void *)buf->vbuf.m.userptr, buf->vbuf.length,
77 DMA_FROM_DEVICE);
78 outer_inv_range(buf->vbuf.m.userptr,
79 buf->vbuf.m.userptr + buf->vbuf.length);
80 }
81}
82
83/*
84 * isp_video_buffer_lock_vma - Prevent VMAs from being unmapped
85 *
86 * Lock the VMAs underlying the given buffer into memory. This avoids the
87 * userspace buffer mapping from being swapped out, making VIPT cache handling
88 * easier.
89 *
90 * Note that the pages will not be freed as the buffers have been locked to
91 * memory using by a call to get_user_pages(), but the userspace mapping could
92 * still disappear if the VMAs are not locked. This is caused by the memory
93 * management code trying to be as lock-less as possible, which results in the
94 * userspace mapping manager not finding out that the pages are locked under
95 * some conditions.
96 */
97static int isp_video_buffer_lock_vma(struct isp_video_buffer *buf, int lock)
98{
99 struct vm_area_struct *vma;
100 unsigned long start;
101 unsigned long end;
102 int ret = 0;
103
104 if (buf->vbuf.memory == V4L2_MEMORY_MMAP)
105 return 0;
106
107 /* We can be called from workqueue context if the current task dies to
108 * unlock the VMAs. In that case there's no current memory management
109 * context so unlocking can't be performed, but the VMAs have been or
110 * are getting destroyed anyway so it doesn't really matter.
111 */
112 if (!current || !current->mm)
113 return lock ? -EINVAL : 0;
114
115 start = buf->vbuf.m.userptr;
116 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
117
118 down_write(&current->mm->mmap_sem);
119 spin_lock(&current->mm->page_table_lock);
120
121 do {
122 vma = find_vma(current->mm, start);
123 if (vma == NULL) {
124 ret = -EFAULT;
125 goto out;
126 }
127
128 if (lock)
129 vma->vm_flags |= VM_LOCKED;
130 else
131 vma->vm_flags &= ~VM_LOCKED;
132
133 start = vma->vm_end + 1;
134 } while (vma->vm_end < end);
135
136 if (lock)
137 buf->vm_flags |= VM_LOCKED;
138 else
139 buf->vm_flags &= ~VM_LOCKED;
140
141out:
142 spin_unlock(&current->mm->page_table_lock);
143 up_write(&current->mm->mmap_sem);
144 return ret;
145}
146
147/*
148 * isp_video_buffer_sglist_kernel - Build a scatter list for a vmalloc'ed buffer
149 *
150 * Iterate over the vmalloc'ed area and create a scatter list entry for every
151 * page.
152 */
153static int isp_video_buffer_sglist_kernel(struct isp_video_buffer *buf)
154{
155 struct scatterlist *sglist;
156 unsigned int npages;
157 unsigned int i;
158 void *addr;
159
160 addr = buf->vaddr;
161 npages = PAGE_ALIGN(buf->vbuf.length) >> PAGE_SHIFT;
162
163 sglist = vmalloc(npages * sizeof(*sglist));
164 if (sglist == NULL)
165 return -ENOMEM;
166
167 sg_init_table(sglist, npages);
168
169 for (i = 0; i < npages; ++i, addr += PAGE_SIZE) {
170 struct page *page = vmalloc_to_page(addr);
171
172 if (page == NULL || PageHighMem(page)) {
173 vfree(sglist);
174 return -EINVAL;
175 }
176
177 sg_set_page(&sglist[i], page, PAGE_SIZE, 0);
178 }
179
180 buf->sglen = npages;
181 buf->sglist = sglist;
182
183 return 0;
184}
185
186/*
187 * isp_video_buffer_sglist_user - Build a scatter list for a userspace buffer
188 *
189 * Walk the buffer pages list and create a 1:1 mapping to a scatter list.
190 */
191static int isp_video_buffer_sglist_user(struct isp_video_buffer *buf)
192{
193 struct scatterlist *sglist;
194 unsigned int offset = buf->offset;
195 unsigned int i;
196
197 sglist = vmalloc(buf->npages * sizeof(*sglist));
198 if (sglist == NULL)
199 return -ENOMEM;
200
201 sg_init_table(sglist, buf->npages);
202
203 for (i = 0; i < buf->npages; ++i) {
204 if (PageHighMem(buf->pages[i])) {
205 vfree(sglist);
206 return -EINVAL;
207 }
208
209 sg_set_page(&sglist[i], buf->pages[i], PAGE_SIZE - offset,
210 offset);
211 offset = 0;
212 }
213
214 buf->sglen = buf->npages;
215 buf->sglist = sglist;
216
217 return 0;
218}
219
220/*
221 * isp_video_buffer_sglist_pfnmap - Build a scatter list for a VM_PFNMAP buffer
222 *
223 * Create a scatter list of physically contiguous pages starting at the buffer
224 * memory physical address.
225 */
226static int isp_video_buffer_sglist_pfnmap(struct isp_video_buffer *buf)
227{
228 struct scatterlist *sglist;
229 unsigned int offset = buf->offset;
230 unsigned long pfn = buf->paddr >> PAGE_SHIFT;
231 unsigned int i;
232
233 sglist = vmalloc(buf->npages * sizeof(*sglist));
234 if (sglist == NULL)
235 return -ENOMEM;
236
237 sg_init_table(sglist, buf->npages);
238
239 for (i = 0; i < buf->npages; ++i, ++pfn) {
240 sg_set_page(&sglist[i], pfn_to_page(pfn), PAGE_SIZE - offset,
241 offset);
242 /* PFNMAP buffers will not get DMA-mapped, set the DMA address
243 * manually.
244 */
245 sg_dma_address(&sglist[i]) = (pfn << PAGE_SHIFT) + offset;
246 offset = 0;
247 }
248
249 buf->sglen = buf->npages;
250 buf->sglist = sglist;
251
252 return 0;
253}
254
255/*
256 * isp_video_buffer_cleanup - Release pages for a userspace VMA.
257 *
258 * Release pages locked by a call isp_video_buffer_prepare_user and free the
259 * pages table.
260 */
261static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
262{
263 enum dma_data_direction direction;
264 unsigned int i;
265
266 if (buf->queue->ops->buffer_cleanup)
267 buf->queue->ops->buffer_cleanup(buf);
268
269 if (!(buf->vm_flags & VM_PFNMAP)) {
270 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
271 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
272 dma_unmap_sg(buf->queue->dev, buf->sglist, buf->sglen,
273 direction);
274 }
275
276 vfree(buf->sglist);
277 buf->sglist = NULL;
278 buf->sglen = 0;
279
280 if (buf->pages != NULL) {
281 isp_video_buffer_lock_vma(buf, 0);
282
283 for (i = 0; i < buf->npages; ++i)
284 page_cache_release(buf->pages[i]);
285
286 vfree(buf->pages);
287 buf->pages = NULL;
288 }
289
290 buf->npages = 0;
291 buf->skip_cache = false;
292}
293
294/*
295 * isp_video_buffer_prepare_user - Pin userspace VMA pages to memory.
296 *
297 * This function creates a list of pages for a userspace VMA. The number of
298 * pages is first computed based on the buffer size, and pages are then
299 * retrieved by a call to get_user_pages.
300 *
301 * Pages are pinned to memory by get_user_pages, making them available for DMA
302 * transfers. However, due to memory management optimization, it seems the
303 * get_user_pages doesn't guarantee that the pinned pages will not be written
304 * to swap and removed from the userspace mapping(s). When this happens, a page
305 * fault can be generated when accessing those unmapped pages.
306 *
307 * If the fault is triggered by a page table walk caused by VIPT cache
308 * management operations, the page fault handler might oops if the MM semaphore
309 * is held, as it can't handle kernel page faults in that case. To fix that, a
310 * fixup entry needs to be added to the cache management code, or the userspace
311 * VMA must be locked to avoid removing pages from the userspace mapping in the
312 * first place.
313 *
314 * If the number of pages retrieved is smaller than the number required by the
315 * buffer size, the function returns -EFAULT.
316 */
317static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf)
318{
319 unsigned long data;
320 unsigned int first;
321 unsigned int last;
322 int ret;
323
324 data = buf->vbuf.m.userptr;
325 first = (data & PAGE_MASK) >> PAGE_SHIFT;
326 last = ((data + buf->vbuf.length - 1) & PAGE_MASK) >> PAGE_SHIFT;
327
328 buf->offset = data & ~PAGE_MASK;
329 buf->npages = last - first + 1;
330 buf->pages = vmalloc(buf->npages * sizeof(buf->pages[0]));
331 if (buf->pages == NULL)
332 return -ENOMEM;
333
334 down_read(&current->mm->mmap_sem);
335 ret = get_user_pages(current, current->mm, data & PAGE_MASK,
336 buf->npages,
337 buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
338 buf->pages, NULL);
339 up_read(&current->mm->mmap_sem);
340
341 if (ret != buf->npages) {
342 buf->npages = ret < 0 ? 0 : ret;
343 isp_video_buffer_cleanup(buf);
344 return -EFAULT;
345 }
346
347 ret = isp_video_buffer_lock_vma(buf, 1);
348 if (ret < 0)
349 isp_video_buffer_cleanup(buf);
350
351 return ret;
352}
353
354/*
355 * isp_video_buffer_prepare_pfnmap - Validate a VM_PFNMAP userspace buffer
356 *
357 * Userspace VM_PFNMAP buffers are supported only if they are contiguous in
358 * memory and if they span a single VMA.
359 *
360 * Return 0 if the buffer is valid, or -EFAULT otherwise.
361 */
362static int isp_video_buffer_prepare_pfnmap(struct isp_video_buffer *buf)
363{
364 struct vm_area_struct *vma;
365 unsigned long prev_pfn;
366 unsigned long this_pfn;
367 unsigned long start;
368 unsigned long end;
369 dma_addr_t pa = 0;
370 int ret = -EFAULT;
371
372 start = buf->vbuf.m.userptr;
373 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
374
375 buf->offset = start & ~PAGE_MASK;
376 buf->npages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
377 buf->pages = NULL;
378
379 down_read(&current->mm->mmap_sem);
380 vma = find_vma(current->mm, start);
381 if (vma == NULL || vma->vm_end < end)
382 goto done;
383
384 for (prev_pfn = 0; start <= end; start += PAGE_SIZE) {
385 ret = follow_pfn(vma, start, &this_pfn);
386 if (ret)
387 goto done;
388
389 if (prev_pfn == 0)
390 pa = this_pfn << PAGE_SHIFT;
391 else if (this_pfn != prev_pfn + 1) {
392 ret = -EFAULT;
393 goto done;
394 }
395
396 prev_pfn = this_pfn;
397 }
398
399 buf->paddr = pa + buf->offset;
400 ret = 0;
401
402done:
403 up_read(&current->mm->mmap_sem);
404 return ret;
405}
406
407/*
408 * isp_video_buffer_prepare_vm_flags - Get VMA flags for a userspace address
409 *
410 * This function locates the VMAs for the buffer's userspace address and checks
411 * that their flags match. The only flag that we need to care for at the moment
412 * is VM_PFNMAP.
413 *
414 * The buffer vm_flags field is set to the first VMA flags.
415 *
416 * Return -EFAULT if no VMA can be found for part of the buffer, or if the VMAs
417 * have incompatible flags.
418 */
419static int isp_video_buffer_prepare_vm_flags(struct isp_video_buffer *buf)
420{
421 struct vm_area_struct *vma;
422 pgprot_t uninitialized_var(vm_page_prot);
423 unsigned long start;
424 unsigned long end;
425 int ret = -EFAULT;
426
427 start = buf->vbuf.m.userptr;
428 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
429
430 down_read(&current->mm->mmap_sem);
431
432 do {
433 vma = find_vma(current->mm, start);
434 if (vma == NULL)
435 goto done;
436
437 if (start == buf->vbuf.m.userptr) {
438 buf->vm_flags = vma->vm_flags;
439 vm_page_prot = vma->vm_page_prot;
440 }
441
442 if ((buf->vm_flags ^ vma->vm_flags) & VM_PFNMAP)
443 goto done;
444
445 if (vm_page_prot != vma->vm_page_prot)
446 goto done;
447
448 start = vma->vm_end + 1;
449 } while (vma->vm_end < end);
450
451 /* Skip cache management to enhance performances for non-cached or
452 * write-combining buffers.
453 */
454 if (vm_page_prot == pgprot_noncached(vm_page_prot) ||
455 vm_page_prot == pgprot_writecombine(vm_page_prot))
456 buf->skip_cache = true;
457
458 ret = 0;
459
460done:
461 up_read(&current->mm->mmap_sem);
462 return ret;
463}
464
465/*
466 * isp_video_buffer_prepare - Make a buffer ready for operation
467 *
468 * Preparing a buffer involves:
469 *
470 * - validating VMAs (userspace buffers only)
471 * - locking pages and VMAs into memory (userspace buffers only)
472 * - building page and scatter-gather lists
473 * - mapping buffers for DMA operation
474 * - performing driver-specific preparation
475 *
476 * The function must be called in userspace context with a valid mm context
477 * (this excludes cleanup paths such as sys_close when the userspace process
478 * segfaults).
479 */
480static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
481{
482 enum dma_data_direction direction;
483 int ret;
484
485 switch (buf->vbuf.memory) {
486 case V4L2_MEMORY_MMAP:
487 ret = isp_video_buffer_sglist_kernel(buf);
488 break;
489
490 case V4L2_MEMORY_USERPTR:
491 ret = isp_video_buffer_prepare_vm_flags(buf);
492 if (ret < 0)
493 return ret;
494
495 if (buf->vm_flags & VM_PFNMAP) {
496 ret = isp_video_buffer_prepare_pfnmap(buf);
497 if (ret < 0)
498 return ret;
499
500 ret = isp_video_buffer_sglist_pfnmap(buf);
501 } else {
502 ret = isp_video_buffer_prepare_user(buf);
503 if (ret < 0)
504 return ret;
505
506 ret = isp_video_buffer_sglist_user(buf);
507 }
508 break;
509
510 default:
511 return -EINVAL;
512 }
513
514 if (ret < 0)
515 goto done;
516
517 if (!(buf->vm_flags & VM_PFNMAP)) {
518 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
519 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
520 ret = dma_map_sg(buf->queue->dev, buf->sglist, buf->sglen,
521 direction);
522 if (ret != buf->sglen) {
523 ret = -EFAULT;
524 goto done;
525 }
526 }
527
528 if (buf->queue->ops->buffer_prepare)
529 ret = buf->queue->ops->buffer_prepare(buf);
530
531done:
532 if (ret < 0) {
533 isp_video_buffer_cleanup(buf);
534 return ret;
535 }
536
537 return ret;
538}
539
540/*
541 * isp_video_queue_query - Query the status of a given buffer
542 *
543 * Locking: must be called with the queue lock held.
544 */
545static void isp_video_buffer_query(struct isp_video_buffer *buf,
546 struct v4l2_buffer *vbuf)
547{
548 memcpy(vbuf, &buf->vbuf, sizeof(*vbuf));
549
550 if (buf->vma_use_count)
551 vbuf->flags |= V4L2_BUF_FLAG_MAPPED;
552
553 switch (buf->state) {
554 case ISP_BUF_STATE_ERROR:
555 vbuf->flags |= V4L2_BUF_FLAG_ERROR;
556 /* Fallthrough */
557 case ISP_BUF_STATE_DONE:
558 vbuf->flags |= V4L2_BUF_FLAG_DONE;
559 break;
560 case ISP_BUF_STATE_QUEUED:
561 case ISP_BUF_STATE_ACTIVE:
562 vbuf->flags |= V4L2_BUF_FLAG_QUEUED;
563 break;
564 case ISP_BUF_STATE_IDLE:
565 default:
566 break;
567 }
568}
569
570/*
571 * isp_video_buffer_wait - Wait for a buffer to be ready
572 *
573 * In non-blocking mode, return immediately with 0 if the buffer is ready or
574 * -EAGAIN if the buffer is in the QUEUED or ACTIVE state.
575 *
576 * In blocking mode, wait (interruptibly but with no timeout) on the buffer wait
577 * queue using the same condition.
578 */
579static int isp_video_buffer_wait(struct isp_video_buffer *buf, int nonblocking)
580{
581 if (nonblocking) {
582 return (buf->state != ISP_BUF_STATE_QUEUED &&
583 buf->state != ISP_BUF_STATE_ACTIVE)
584 ? 0 : -EAGAIN;
585 }
586
587 return wait_event_interruptible(buf->wait,
588 buf->state != ISP_BUF_STATE_QUEUED &&
589 buf->state != ISP_BUF_STATE_ACTIVE);
590}
591
592/* -----------------------------------------------------------------------------
593 * Queue management
594 */
595
596/*
597 * isp_video_queue_free - Free video buffers memory
598 *
599 * Buffers can only be freed if the queue isn't streaming and if no buffer is
600 * mapped to userspace. Return -EBUSY if those conditions aren't satisfied.
601 *
602 * This function must be called with the queue lock held.
603 */
604static int isp_video_queue_free(struct isp_video_queue *queue)
605{
606 unsigned int i;
607
608 if (queue->streaming)
609 return -EBUSY;
610
611 for (i = 0; i < queue->count; ++i) {
612 if (queue->buffers[i]->vma_use_count != 0)
613 return -EBUSY;
614 }
615
616 for (i = 0; i < queue->count; ++i) {
617 struct isp_video_buffer *buf = queue->buffers[i];
618
619 isp_video_buffer_cleanup(buf);
620
621 vfree(buf->vaddr);
622 buf->vaddr = NULL;
623
624 kfree(buf);
625 queue->buffers[i] = NULL;
626 }
627
628 INIT_LIST_HEAD(&queue->queue);
629 queue->count = 0;
630 return 0;
631}
632
633/*
634 * isp_video_queue_alloc - Allocate video buffers memory
635 *
636 * This function must be called with the queue lock held.
637 */
638static int isp_video_queue_alloc(struct isp_video_queue *queue,
639 unsigned int nbuffers,
640 unsigned int size, enum v4l2_memory memory)
641{
642 struct isp_video_buffer *buf;
643 unsigned int i;
644 void *mem;
645 int ret;
646
647 /* Start by freeing the buffers. */
648 ret = isp_video_queue_free(queue);
649 if (ret < 0)
650 return ret;
651
652 /* Bail out if no buffers should be allocated. */
653 if (nbuffers == 0)
654 return 0;
655
656 /* Initialize the allocated buffers. */
657 for (i = 0; i < nbuffers; ++i) {
658 buf = kzalloc(queue->bufsize, GFP_KERNEL);
659 if (buf == NULL)
660 break;
661
662 if (memory == V4L2_MEMORY_MMAP) {
663 /* Allocate video buffers memory for mmap mode. Align
664 * the size to the page size.
665 */
666 mem = vmalloc_32_user(PAGE_ALIGN(size));
667 if (mem == NULL) {
668 kfree(buf);
669 break;
670 }
671
672 buf->vbuf.m.offset = i * PAGE_ALIGN(size);
673 buf->vaddr = mem;
674 }
675
676 buf->vbuf.index = i;
677 buf->vbuf.length = size;
678 buf->vbuf.type = queue->type;
679 buf->vbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
680 buf->vbuf.field = V4L2_FIELD_NONE;
681 buf->vbuf.memory = memory;
682
683 buf->queue = queue;
684 init_waitqueue_head(&buf->wait);
685
686 queue->buffers[i] = buf;
687 }
688
689 if (i == 0)
690 return -ENOMEM;
691
692 queue->count = i;
693 return nbuffers;
694}
695
696/**
697 * omap3isp_video_queue_cleanup - Clean up the video buffers queue
698 * @queue: Video buffers queue
699 *
700 * Free all allocated resources and clean up the video buffers queue. The queue
701 * must not be busy (no ongoing video stream) and buffers must have been
702 * unmapped.
703 *
704 * Return 0 on success or -EBUSY if the queue is busy or buffers haven't been
705 * unmapped.
706 */
707int omap3isp_video_queue_cleanup(struct isp_video_queue *queue)
708{
709 return isp_video_queue_free(queue);
710}
711
712/**
713 * omap3isp_video_queue_init - Initialize the video buffers queue
714 * @queue: Video buffers queue
715 * @type: V4L2 buffer type (capture or output)
716 * @ops: Driver-specific queue operations
717 * @dev: Device used for DMA operations
718 * @bufsize: Size of the driver-specific buffer structure
719 *
720 * Initialize the video buffers queue with the supplied parameters.
721 *
722 * The queue type must be one of V4L2_BUF_TYPE_VIDEO_CAPTURE or
723 * V4L2_BUF_TYPE_VIDEO_OUTPUT. Other buffer types are not supported yet.
724 *
725 * Buffer objects will be allocated using the given buffer size to allow room
726 * for driver-specific fields. Driver-specific buffer structures must start
727 * with a struct isp_video_buffer field. Drivers with no driver-specific buffer
728 * structure must pass the size of the isp_video_buffer structure in the bufsize
729 * parameter.
730 *
731 * Return 0 on success.
732 */
733int omap3isp_video_queue_init(struct isp_video_queue *queue,
734 enum v4l2_buf_type type,
735 const struct isp_video_queue_operations *ops,
736 struct device *dev, unsigned int bufsize)
737{
738 INIT_LIST_HEAD(&queue->queue);
739 mutex_init(&queue->lock);
740 spin_lock_init(&queue->irqlock);
741
742 queue->type = type;
743 queue->ops = ops;
744 queue->dev = dev;
745 queue->bufsize = bufsize;
746
747 return 0;
748}
749
750/* -----------------------------------------------------------------------------
751 * V4L2 operations
752 */
753
754/**
755 * omap3isp_video_queue_reqbufs - Allocate video buffers memory
756 *
757 * This function is intended to be used as a VIDIOC_REQBUFS ioctl handler. It
758 * allocated video buffer objects and, for MMAP buffers, buffer memory.
759 *
760 * If the number of buffers is 0, all buffers are freed and the function returns
761 * without performing any allocation.
762 *
763 * If the number of buffers is not 0, currently allocated buffers (if any) are
764 * freed and the requested number of buffers are allocated. Depending on
765 * driver-specific requirements and on memory availability, a number of buffer
766 * smaller or bigger than requested can be allocated. This isn't considered as
767 * an error.
768 *
769 * Return 0 on success or one of the following error codes:
770 *
771 * -EINVAL if the buffer type or index are invalid
772 * -EBUSY if the queue is busy (streaming or buffers mapped)
773 * -ENOMEM if the buffers can't be allocated due to an out-of-memory condition
774 */
775int omap3isp_video_queue_reqbufs(struct isp_video_queue *queue,
776 struct v4l2_requestbuffers *rb)
777{
778 unsigned int nbuffers = rb->count;
779 unsigned int size;
780 int ret;
781
782 if (rb->type != queue->type)
783 return -EINVAL;
784
785 queue->ops->queue_prepare(queue, &nbuffers, &size);
786 if (size == 0)
787 return -EINVAL;
788
789 nbuffers = min_t(unsigned int, nbuffers, ISP_VIDEO_MAX_BUFFERS);
790
791 mutex_lock(&queue->lock);
792
793 ret = isp_video_queue_alloc(queue, nbuffers, size, rb->memory);
794 if (ret < 0)
795 goto done;
796
797 rb->count = ret;
798 ret = 0;
799
800done:
801 mutex_unlock(&queue->lock);
802 return ret;
803}
804
805/**
806 * omap3isp_video_queue_querybuf - Query the status of a buffer in a queue
807 *
808 * This function is intended to be used as a VIDIOC_QUERYBUF ioctl handler. It
809 * returns the status of a given video buffer.
810 *
811 * Return 0 on success or -EINVAL if the buffer type or index are invalid.
812 */
813int omap3isp_video_queue_querybuf(struct isp_video_queue *queue,
814 struct v4l2_buffer *vbuf)
815{
816 struct isp_video_buffer *buf;
817 int ret = 0;
818
819 if (vbuf->type != queue->type)
820 return -EINVAL;
821
822 mutex_lock(&queue->lock);
823
824 if (vbuf->index >= queue->count) {
825 ret = -EINVAL;
826 goto done;
827 }
828
829 buf = queue->buffers[vbuf->index];
830 isp_video_buffer_query(buf, vbuf);
831
832done:
833 mutex_unlock(&queue->lock);
834 return ret;
835}
836
837/**
838 * omap3isp_video_queue_qbuf - Queue a buffer
839 *
840 * This function is intended to be used as a VIDIOC_QBUF ioctl handler.
841 *
842 * The v4l2_buffer structure passed from userspace is first sanity tested. If
843 * sane, the buffer is then processed and added to the main queue and, if the
844 * queue is streaming, to the IRQ queue.
845 *
846 * Before being enqueued, USERPTR buffers are checked for address changes. If
847 * the buffer has a different userspace address, the old memory area is unlocked
848 * and the new memory area is locked.
849 */
850int omap3isp_video_queue_qbuf(struct isp_video_queue *queue,
851 struct v4l2_buffer *vbuf)
852{
853 struct isp_video_buffer *buf;
854 unsigned long flags;
855 int ret = -EINVAL;
856
857 if (vbuf->type != queue->type)
858 goto done;
859
860 mutex_lock(&queue->lock);
861
862 if (vbuf->index >= queue->count)
863 goto done;
864
865 buf = queue->buffers[vbuf->index];
866
867 if (vbuf->memory != buf->vbuf.memory)
868 goto done;
869
870 if (buf->state != ISP_BUF_STATE_IDLE)
871 goto done;
872
873 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
874 vbuf->length < buf->vbuf.length)
875 goto done;
876
877 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
878 vbuf->m.userptr != buf->vbuf.m.userptr) {
879 isp_video_buffer_cleanup(buf);
880 buf->vbuf.m.userptr = vbuf->m.userptr;
881 buf->prepared = 0;
882 }
883
884 if (!buf->prepared) {
885 ret = isp_video_buffer_prepare(buf);
886 if (ret < 0)
887 goto done;
888 buf->prepared = 1;
889 }
890
891 isp_video_buffer_cache_sync(buf);
892
893 buf->state = ISP_BUF_STATE_QUEUED;
894 list_add_tail(&buf->stream, &queue->queue);
895
896 if (queue->streaming) {
897 spin_lock_irqsave(&queue->irqlock, flags);
898 queue->ops->buffer_queue(buf);
899 spin_unlock_irqrestore(&queue->irqlock, flags);
900 }
901
902 ret = 0;
903
904done:
905 mutex_unlock(&queue->lock);
906 return ret;
907}
908
909/**
910 * omap3isp_video_queue_dqbuf - Dequeue a buffer
911 *
912 * This function is intended to be used as a VIDIOC_DQBUF ioctl handler.
913 *
914 * Wait until a buffer is ready to be dequeued, remove it from the queue and
915 * copy its information to the v4l2_buffer structure.
916 *
917 * If the nonblocking argument is not zero and no buffer is ready, return
918 * -EAGAIN immediately instead of waiting.
919 *
920 * If no buffer has been enqueued, or if the requested buffer type doesn't match
921 * the queue type, return -EINVAL.
922 */
923int omap3isp_video_queue_dqbuf(struct isp_video_queue *queue,
924 struct v4l2_buffer *vbuf, int nonblocking)
925{
926 struct isp_video_buffer *buf;
927 int ret;
928
929 if (vbuf->type != queue->type)
930 return -EINVAL;
931
932 mutex_lock(&queue->lock);
933
934 if (list_empty(&queue->queue)) {
935 ret = -EINVAL;
936 goto done;
937 }
938
939 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
940 ret = isp_video_buffer_wait(buf, nonblocking);
941 if (ret < 0)
942 goto done;
943
944 list_del(&buf->stream);
945
946 isp_video_buffer_query(buf, vbuf);
947 buf->state = ISP_BUF_STATE_IDLE;
948 vbuf->flags &= ~V4L2_BUF_FLAG_QUEUED;
949
950done:
951 mutex_unlock(&queue->lock);
952 return ret;
953}
954
955/**
956 * omap3isp_video_queue_streamon - Start streaming
957 *
958 * This function is intended to be used as a VIDIOC_STREAMON ioctl handler. It
959 * starts streaming on the queue and calls the buffer_queue operation for all
960 * queued buffers.
961 *
962 * Return 0 on success.
963 */
964int omap3isp_video_queue_streamon(struct isp_video_queue *queue)
965{
966 struct isp_video_buffer *buf;
967 unsigned long flags;
968
969 mutex_lock(&queue->lock);
970
971 if (queue->streaming)
972 goto done;
973
974 queue->streaming = 1;
975
976 spin_lock_irqsave(&queue->irqlock, flags);
977 list_for_each_entry(buf, &queue->queue, stream)
978 queue->ops->buffer_queue(buf);
979 spin_unlock_irqrestore(&queue->irqlock, flags);
980
981done:
982 mutex_unlock(&queue->lock);
983 return 0;
984}
985
986/**
987 * omap3isp_video_queue_streamoff - Stop streaming
988 *
989 * This function is intended to be used as a VIDIOC_STREAMOFF ioctl handler. It
990 * stops streaming on the queue and wakes up all the buffers.
991 *
992 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
993 * delayed works before calling this function to make sure no buffer will be
994 * touched by the driver and/or hardware.
995 */
996void omap3isp_video_queue_streamoff(struct isp_video_queue *queue)
997{
998 struct isp_video_buffer *buf;
999 unsigned long flags;
1000 unsigned int i;
1001
1002 mutex_lock(&queue->lock);
1003
1004 if (!queue->streaming)
1005 goto done;
1006
1007 queue->streaming = 0;
1008
1009 spin_lock_irqsave(&queue->irqlock, flags);
1010 for (i = 0; i < queue->count; ++i) {
1011 buf = queue->buffers[i];
1012
1013 if (buf->state == ISP_BUF_STATE_ACTIVE)
1014 wake_up(&buf->wait);
1015
1016 buf->state = ISP_BUF_STATE_IDLE;
1017 }
1018 spin_unlock_irqrestore(&queue->irqlock, flags);
1019
1020 INIT_LIST_HEAD(&queue->queue);
1021
1022done:
1023 mutex_unlock(&queue->lock);
1024}
1025
1026/**
1027 * omap3isp_video_queue_discard_done - Discard all buffers marked as DONE
1028 *
1029 * This function is intended to be used with suspend/resume operations. It
1030 * discards all 'done' buffers as they would be too old to be requested after
1031 * resume.
1032 *
1033 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1034 * delayed works before calling this function to make sure no buffer will be
1035 * touched by the driver and/or hardware.
1036 */
1037void omap3isp_video_queue_discard_done(struct isp_video_queue *queue)
1038{
1039 struct isp_video_buffer *buf;
1040 unsigned int i;
1041
1042 mutex_lock(&queue->lock);
1043
1044 if (!queue->streaming)
1045 goto done;
1046
1047 for (i = 0; i < queue->count; ++i) {
1048 buf = queue->buffers[i];
1049
1050 if (buf->state == ISP_BUF_STATE_DONE)
1051 buf->state = ISP_BUF_STATE_ERROR;
1052 }
1053
1054done:
1055 mutex_unlock(&queue->lock);
1056}
1057
1058static void isp_video_queue_vm_open(struct vm_area_struct *vma)
1059{
1060 struct isp_video_buffer *buf = vma->vm_private_data;
1061
1062 buf->vma_use_count++;
1063}
1064
1065static void isp_video_queue_vm_close(struct vm_area_struct *vma)
1066{
1067 struct isp_video_buffer *buf = vma->vm_private_data;
1068
1069 buf->vma_use_count--;
1070}
1071
1072static const struct vm_operations_struct isp_video_queue_vm_ops = {
1073 .open = isp_video_queue_vm_open,
1074 .close = isp_video_queue_vm_close,
1075};
1076
1077/**
1078 * omap3isp_video_queue_mmap - Map buffers to userspace
1079 *
1080 * This function is intended to be used as an mmap() file operation handler. It
1081 * maps a buffer to userspace based on the VMA offset.
1082 *
1083 * Only buffers of memory type MMAP are supported.
1084 */
1085int omap3isp_video_queue_mmap(struct isp_video_queue *queue,
1086 struct vm_area_struct *vma)
1087{
1088 struct isp_video_buffer *uninitialized_var(buf);
1089 unsigned long size;
1090 unsigned int i;
1091 int ret = 0;
1092
1093 mutex_lock(&queue->lock);
1094
1095 for (i = 0; i < queue->count; ++i) {
1096 buf = queue->buffers[i];
1097 if ((buf->vbuf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1098 break;
1099 }
1100
1101 if (i == queue->count) {
1102 ret = -EINVAL;
1103 goto done;
1104 }
1105
1106 size = vma->vm_end - vma->vm_start;
1107
1108 if (buf->vbuf.memory != V4L2_MEMORY_MMAP ||
1109 size != PAGE_ALIGN(buf->vbuf.length)) {
1110 ret = -EINVAL;
1111 goto done;
1112 }
1113
1114 ret = remap_vmalloc_range(vma, buf->vaddr, 0);
1115 if (ret < 0)
1116 goto done;
1117
1118 vma->vm_ops = &isp_video_queue_vm_ops;
1119 vma->vm_private_data = buf;
1120 isp_video_queue_vm_open(vma);
1121
1122done:
1123 mutex_unlock(&queue->lock);
1124 return ret;
1125}
1126
1127/**
1128 * omap3isp_video_queue_poll - Poll video queue state
1129 *
1130 * This function is intended to be used as a poll() file operation handler. It
1131 * polls the state of the video buffer at the front of the queue and returns an
1132 * events mask.
1133 *
1134 * If no buffer is present at the front of the queue, POLLERR is returned.
1135 */
1136unsigned int omap3isp_video_queue_poll(struct isp_video_queue *queue,
1137 struct file *file, poll_table *wait)
1138{
1139 struct isp_video_buffer *buf;
1140 unsigned int mask = 0;
1141
1142 mutex_lock(&queue->lock);
1143 if (list_empty(&queue->queue)) {
1144 mask |= POLLERR;
1145 goto done;
1146 }
1147 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
1148
1149 poll_wait(file, &buf->wait, wait);
1150 if (buf->state == ISP_BUF_STATE_DONE ||
1151 buf->state == ISP_BUF_STATE_ERROR) {
1152 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1153 mask |= POLLIN | POLLRDNORM;
1154 else
1155 mask |= POLLOUT | POLLWRNORM;
1156 }
1157
1158done:
1159 mutex_unlock(&queue->lock);
1160 return mask;
1161}
diff --git a/drivers/media/platform/omap3isp/ispqueue.h b/drivers/media/platform/omap3isp/ispqueue.h
deleted file mode 100644
index 3e048ad65647..000000000000
--- a/drivers/media/platform/omap3isp/ispqueue.h
+++ /dev/null
@@ -1,188 +0,0 @@
1/*
2 * ispqueue.h
3 *
4 * TI OMAP3 ISP - Video buffers queue handling
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26#ifndef OMAP3_ISP_QUEUE_H
27#define OMAP3_ISP_QUEUE_H
28
29#include <linux/kernel.h>
30#include <linux/list.h>
31#include <linux/mm_types.h>
32#include <linux/mutex.h>
33#include <linux/videodev2.h>
34#include <linux/wait.h>
35
36struct isp_video_queue;
37struct page;
38struct scatterlist;
39
40#define ISP_VIDEO_MAX_BUFFERS 16
41
42/**
43 * enum isp_video_buffer_state - ISP video buffer state
44 * @ISP_BUF_STATE_IDLE: The buffer is under userspace control (dequeued
45 * or not queued yet).
46 * @ISP_BUF_STATE_QUEUED: The buffer has been queued but isn't used by the
47 * device yet.
48 * @ISP_BUF_STATE_ACTIVE: The buffer is in use for an active video transfer.
49 * @ISP_BUF_STATE_ERROR: The device is done with the buffer and an error
50 * occurred. For capture device the buffer likely contains corrupted data or
51 * no data at all.
52 * @ISP_BUF_STATE_DONE: The device is done with the buffer and no error occurred.
53 * For capture devices the buffer contains valid data.
54 */
55enum isp_video_buffer_state {
56 ISP_BUF_STATE_IDLE,
57 ISP_BUF_STATE_QUEUED,
58 ISP_BUF_STATE_ACTIVE,
59 ISP_BUF_STATE_ERROR,
60 ISP_BUF_STATE_DONE,
61};
62
63/**
64 * struct isp_video_buffer - ISP video buffer
65 * @vma_use_count: Number of times the buffer is mmap'ed to userspace
66 * @stream: List head for insertion into main queue
67 * @queue: ISP buffers queue this buffer belongs to
68 * @prepared: Whether the buffer has been prepared
69 * @skip_cache: Whether to skip cache management operations for this buffer
70 * @vaddr: Memory virtual address (for kernel buffers)
71 * @vm_flags: Buffer VMA flags (for userspace buffers)
72 * @offset: Offset inside the first page (for userspace buffers)
73 * @npages: Number of pages (for userspace buffers)
74 * @pages: Pages table (for userspace non-VM_PFNMAP buffers)
75 * @paddr: Memory physical address (for userspace VM_PFNMAP buffers)
76 * @sglen: Number of elements in the scatter list (for non-VM_PFNMAP buffers)
77 * @sglist: Scatter list (for non-VM_PFNMAP buffers)
78 * @vbuf: V4L2 buffer
79 * @irqlist: List head for insertion into IRQ queue
80 * @state: Current buffer state
81 * @wait: Wait queue to signal buffer completion
82 */
83struct isp_video_buffer {
84 unsigned long vma_use_count;
85 struct list_head stream;
86 struct isp_video_queue *queue;
87 unsigned int prepared:1;
88 bool skip_cache;
89
90 /* For kernel buffers. */
91 void *vaddr;
92
93 /* For userspace buffers. */
94 vm_flags_t vm_flags;
95 unsigned long offset;
96 unsigned int npages;
97 struct page **pages;
98 dma_addr_t paddr;
99
100 /* For all buffers except VM_PFNMAP. */
101 unsigned int sglen;
102 struct scatterlist *sglist;
103
104 /* Touched by the interrupt handler. */
105 struct v4l2_buffer vbuf;
106 struct list_head irqlist;
107 enum isp_video_buffer_state state;
108 wait_queue_head_t wait;
109};
110
111#define to_isp_video_buffer(vb) container_of(vb, struct isp_video_buffer, vb)
112
113/**
114 * struct isp_video_queue_operations - Driver-specific operations
115 * @queue_prepare: Called before allocating buffers. Drivers should clamp the
116 * number of buffers according to their requirements, and must return the
117 * buffer size in bytes.
118 * @buffer_prepare: Called the first time a buffer is queued, or after changing
119 * the userspace memory address for a USERPTR buffer, with the queue lock
120 * held. Drivers should perform device-specific buffer preparation (such as
121 * mapping the buffer memory in an IOMMU). This operation is optional.
122 * @buffer_queue: Called when a buffer is being added to the queue with the
123 * queue irqlock spinlock held.
124 * @buffer_cleanup: Called before freeing buffers, or before changing the
125 * userspace memory address for a USERPTR buffer, with the queue lock held.
126 * Drivers must perform cleanup operations required to undo the
127 * buffer_prepare call. This operation is optional.
128 */
129struct isp_video_queue_operations {
130 void (*queue_prepare)(struct isp_video_queue *queue,
131 unsigned int *nbuffers, unsigned int *size);
132 int (*buffer_prepare)(struct isp_video_buffer *buf);
133 void (*buffer_queue)(struct isp_video_buffer *buf);
134 void (*buffer_cleanup)(struct isp_video_buffer *buf);
135};
136
137/**
138 * struct isp_video_queue - ISP video buffers queue
139 * @type: Type of video buffers handled by this queue
140 * @ops: Queue operations
141 * @dev: Device used for DMA operations
142 * @bufsize: Size of a driver-specific buffer object
143 * @count: Number of currently allocated buffers
144 * @buffers: ISP video buffers
145 * @lock: Mutex to protect access to the buffers, main queue and state
146 * @irqlock: Spinlock to protect access to the IRQ queue
147 * @streaming: Queue state, indicates whether the queue is streaming
148 * @queue: List of all queued buffers
149 */
150struct isp_video_queue {
151 enum v4l2_buf_type type;
152 const struct isp_video_queue_operations *ops;
153 struct device *dev;
154 unsigned int bufsize;
155
156 unsigned int count;
157 struct isp_video_buffer *buffers[ISP_VIDEO_MAX_BUFFERS];
158 struct mutex lock;
159 spinlock_t irqlock;
160
161 unsigned int streaming:1;
162
163 struct list_head queue;
164};
165
166int omap3isp_video_queue_cleanup(struct isp_video_queue *queue);
167int omap3isp_video_queue_init(struct isp_video_queue *queue,
168 enum v4l2_buf_type type,
169 const struct isp_video_queue_operations *ops,
170 struct device *dev, unsigned int bufsize);
171
172int omap3isp_video_queue_reqbufs(struct isp_video_queue *queue,
173 struct v4l2_requestbuffers *rb);
174int omap3isp_video_queue_querybuf(struct isp_video_queue *queue,
175 struct v4l2_buffer *vbuf);
176int omap3isp_video_queue_qbuf(struct isp_video_queue *queue,
177 struct v4l2_buffer *vbuf);
178int omap3isp_video_queue_dqbuf(struct isp_video_queue *queue,
179 struct v4l2_buffer *vbuf, int nonblocking);
180int omap3isp_video_queue_streamon(struct isp_video_queue *queue);
181void omap3isp_video_queue_streamoff(struct isp_video_queue *queue);
182void omap3isp_video_queue_discard_done(struct isp_video_queue *queue);
183int omap3isp_video_queue_mmap(struct isp_video_queue *queue,
184 struct vm_area_struct *vma);
185unsigned int omap3isp_video_queue_poll(struct isp_video_queue *queue,
186 struct file *file, poll_table *wait);
187
188#endif /* OMAP3_ISP_QUEUE_H */
diff --git a/drivers/media/platform/omap3isp/ispresizer.c b/drivers/media/platform/omap3isp/ispresizer.c
index 86369df81d74..6f077c2377db 100644
--- a/drivers/media/platform/omap3isp/ispresizer.c
+++ b/drivers/media/platform/omap3isp/ispresizer.c
@@ -1040,7 +1040,7 @@ static void resizer_isr_buffer(struct isp_res_device *res)
1040 */ 1040 */
1041 buffer = omap3isp_video_buffer_next(&res->video_out); 1041 buffer = omap3isp_video_buffer_next(&res->video_out);
1042 if (buffer != NULL) { 1042 if (buffer != NULL) {
1043 resizer_set_outaddr(res, buffer->isp_addr); 1043 resizer_set_outaddr(res, buffer->dma);
1044 restart = 1; 1044 restart = 1;
1045 } 1045 }
1046 1046
@@ -1049,7 +1049,7 @@ static void resizer_isr_buffer(struct isp_res_device *res)
1049 if (res->input == RESIZER_INPUT_MEMORY) { 1049 if (res->input == RESIZER_INPUT_MEMORY) {
1050 buffer = omap3isp_video_buffer_next(&res->video_in); 1050 buffer = omap3isp_video_buffer_next(&res->video_in);
1051 if (buffer != NULL) 1051 if (buffer != NULL)
1052 resizer_set_inaddr(res, buffer->isp_addr); 1052 resizer_set_inaddr(res, buffer->dma);
1053 pipe->state |= ISP_PIPELINE_IDLE_INPUT; 1053 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1054 } 1054 }
1055 1055
@@ -1101,7 +1101,7 @@ static int resizer_video_queue(struct isp_video *video,
1101 struct isp_res_device *res = &video->isp->isp_res; 1101 struct isp_res_device *res = &video->isp->isp_res;
1102 1102
1103 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) 1103 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1104 resizer_set_inaddr(res, buffer->isp_addr); 1104 resizer_set_inaddr(res, buffer->dma);
1105 1105
1106 /* 1106 /*
1107 * We now have a buffer queued on the output. Despite what the 1107 * We now have a buffer queued on the output. Despite what the
@@ -1116,7 +1116,7 @@ static int resizer_video_queue(struct isp_video *video,
1116 * continuous mode or when starting the stream. 1116 * continuous mode or when starting the stream.
1117 */ 1117 */
1118 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1118 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1119 resizer_set_outaddr(res, buffer->isp_addr); 1119 resizer_set_outaddr(res, buffer->dma);
1120 1120
1121 return 0; 1121 return 0;
1122} 1122}
diff --git a/drivers/media/platform/omap3isp/ispstat.c b/drivers/media/platform/omap3isp/ispstat.c
index 5707f85c4cc4..e6cbc1eaf4ca 100644
--- a/drivers/media/platform/omap3isp/ispstat.c
+++ b/drivers/media/platform/omap3isp/ispstat.c
@@ -26,13 +26,12 @@
26 */ 26 */
27 27
28#include <linux/dma-mapping.h> 28#include <linux/dma-mapping.h>
29#include <linux/omap-iommu.h>
30#include <linux/slab.h> 29#include <linux/slab.h>
31#include <linux/uaccess.h> 30#include <linux/uaccess.h>
32 31
33#include "isp.h" 32#include "isp.h"
34 33
35#define IS_COHERENT_BUF(stat) ((stat)->dma_ch >= 0) 34#define ISP_STAT_USES_DMAENGINE(stat) ((stat)->dma_ch >= 0)
36 35
37/* 36/*
38 * MAGIC_SIZE must always be the greatest common divisor of 37 * MAGIC_SIZE must always be the greatest common divisor of
@@ -77,21 +76,10 @@ static void __isp_stat_buf_sync_magic(struct ispstat *stat,
77 dma_addr_t, unsigned long, size_t, 76 dma_addr_t, unsigned long, size_t,
78 enum dma_data_direction)) 77 enum dma_data_direction))
79{ 78{
80 struct device *dev = stat->isp->dev; 79 /* Sync the initial and final magic words. */
81 struct page *pg; 80 dma_sync(stat->isp->dev, buf->dma_addr, 0, MAGIC_SIZE, dir);
82 dma_addr_t dma_addr; 81 dma_sync(stat->isp->dev, buf->dma_addr + (buf_size & PAGE_MASK),
83 u32 offset; 82 buf_size & ~PAGE_MASK, MAGIC_SIZE, dir);
84
85 /* Initial magic words */
86 pg = vmalloc_to_page(buf->virt_addr);
87 dma_addr = pfn_to_dma(dev, page_to_pfn(pg));
88 dma_sync(dev, dma_addr, 0, MAGIC_SIZE, dir);
89
90 /* Final magic words */
91 pg = vmalloc_to_page(buf->virt_addr + buf_size);
92 dma_addr = pfn_to_dma(dev, page_to_pfn(pg));
93 offset = ((u32)buf->virt_addr + buf_size) & ~PAGE_MASK;
94 dma_sync(dev, dma_addr, offset, MAGIC_SIZE, dir);
95} 83}
96 84
97static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat, 85static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
@@ -99,7 +87,7 @@ static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
99 u32 buf_size, 87 u32 buf_size,
100 enum dma_data_direction dir) 88 enum dma_data_direction dir)
101{ 89{
102 if (IS_COHERENT_BUF(stat)) 90 if (ISP_STAT_USES_DMAENGINE(stat))
103 return; 91 return;
104 92
105 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir, 93 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
@@ -111,7 +99,7 @@ static void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
111 u32 buf_size, 99 u32 buf_size,
112 enum dma_data_direction dir) 100 enum dma_data_direction dir)
113{ 101{
114 if (IS_COHERENT_BUF(stat)) 102 if (ISP_STAT_USES_DMAENGINE(stat))
115 return; 103 return;
116 104
117 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir, 105 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
@@ -180,21 +168,21 @@ static void isp_stat_buf_insert_magic(struct ispstat *stat,
180static void isp_stat_buf_sync_for_device(struct ispstat *stat, 168static void isp_stat_buf_sync_for_device(struct ispstat *stat,
181 struct ispstat_buffer *buf) 169 struct ispstat_buffer *buf)
182{ 170{
183 if (IS_COHERENT_BUF(stat)) 171 if (ISP_STAT_USES_DMAENGINE(stat))
184 return; 172 return;
185 173
186 dma_sync_sg_for_device(stat->isp->dev, buf->iovm->sgt->sgl, 174 dma_sync_sg_for_device(stat->isp->dev, buf->sgt.sgl,
187 buf->iovm->sgt->nents, DMA_FROM_DEVICE); 175 buf->sgt.nents, DMA_FROM_DEVICE);
188} 176}
189 177
190static void isp_stat_buf_sync_for_cpu(struct ispstat *stat, 178static void isp_stat_buf_sync_for_cpu(struct ispstat *stat,
191 struct ispstat_buffer *buf) 179 struct ispstat_buffer *buf)
192{ 180{
193 if (IS_COHERENT_BUF(stat)) 181 if (ISP_STAT_USES_DMAENGINE(stat))
194 return; 182 return;
195 183
196 dma_sync_sg_for_cpu(stat->isp->dev, buf->iovm->sgt->sgl, 184 dma_sync_sg_for_cpu(stat->isp->dev, buf->sgt.sgl,
197 buf->iovm->sgt->nents, DMA_FROM_DEVICE); 185 buf->sgt.nents, DMA_FROM_DEVICE);
198} 186}
199 187
200static void isp_stat_buf_clear(struct ispstat *stat) 188static void isp_stat_buf_clear(struct ispstat *stat)
@@ -354,29 +342,21 @@ static struct ispstat_buffer *isp_stat_buf_get(struct ispstat *stat,
354 342
355static void isp_stat_bufs_free(struct ispstat *stat) 343static void isp_stat_bufs_free(struct ispstat *stat)
356{ 344{
357 struct isp_device *isp = stat->isp; 345 struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
358 int i; 346 ? NULL : stat->isp->dev;
347 unsigned int i;
359 348
360 for (i = 0; i < STAT_MAX_BUFS; i++) { 349 for (i = 0; i < STAT_MAX_BUFS; i++) {
361 struct ispstat_buffer *buf = &stat->buf[i]; 350 struct ispstat_buffer *buf = &stat->buf[i];
362 351
363 if (!IS_COHERENT_BUF(stat)) { 352 if (!buf->virt_addr)
364 if (IS_ERR_OR_NULL((void *)buf->iommu_addr)) 353 continue;
365 continue; 354
366 if (buf->iovm) 355 sg_free_table(&buf->sgt);
367 dma_unmap_sg(isp->dev, buf->iovm->sgt->sgl, 356
368 buf->iovm->sgt->nents, 357 dma_free_coherent(dev, stat->buf_alloc_size, buf->virt_addr,
369 DMA_FROM_DEVICE); 358 buf->dma_addr);
370 omap_iommu_vfree(isp->domain, isp->dev, 359
371 buf->iommu_addr);
372 } else {
373 if (!buf->virt_addr)
374 continue;
375 dma_free_coherent(stat->isp->dev, stat->buf_alloc_size,
376 buf->virt_addr, buf->dma_addr);
377 }
378 buf->iommu_addr = 0;
379 buf->iovm = NULL;
380 buf->dma_addr = 0; 360 buf->dma_addr = 0;
381 buf->virt_addr = NULL; 361 buf->virt_addr = NULL;
382 buf->empty = 1; 362 buf->empty = 1;
@@ -389,83 +369,51 @@ static void isp_stat_bufs_free(struct ispstat *stat)
389 stat->active_buf = NULL; 369 stat->active_buf = NULL;
390} 370}
391 371
392static int isp_stat_bufs_alloc_iommu(struct ispstat *stat, unsigned int size) 372static int isp_stat_bufs_alloc_one(struct device *dev,
393{ 373 struct ispstat_buffer *buf,
394 struct isp_device *isp = stat->isp; 374 unsigned int size)
395 int i;
396
397 stat->buf_alloc_size = size;
398
399 for (i = 0; i < STAT_MAX_BUFS; i++) {
400 struct ispstat_buffer *buf = &stat->buf[i];
401 struct iovm_struct *iovm;
402
403 WARN_ON(buf->dma_addr);
404 buf->iommu_addr = omap_iommu_vmalloc(isp->domain, isp->dev, 0,
405 size, IOMMU_FLAG);
406 if (IS_ERR((void *)buf->iommu_addr)) {
407 dev_err(stat->isp->dev,
408 "%s: Can't acquire memory for "
409 "buffer %d\n", stat->subdev.name, i);
410 isp_stat_bufs_free(stat);
411 return -ENOMEM;
412 }
413
414 iovm = omap_find_iovm_area(isp->dev, buf->iommu_addr);
415 if (!iovm ||
416 !dma_map_sg(isp->dev, iovm->sgt->sgl, iovm->sgt->nents,
417 DMA_FROM_DEVICE)) {
418 isp_stat_bufs_free(stat);
419 return -ENOMEM;
420 }
421 buf->iovm = iovm;
422
423 buf->virt_addr = omap_da_to_va(stat->isp->dev,
424 (u32)buf->iommu_addr);
425 buf->empty = 1;
426 dev_dbg(stat->isp->dev, "%s: buffer[%d] allocated."
427 "iommu_addr=0x%08lx virt_addr=0x%08lx",
428 stat->subdev.name, i, buf->iommu_addr,
429 (unsigned long)buf->virt_addr);
430 }
431
432 return 0;
433}
434
435static int isp_stat_bufs_alloc_dma(struct ispstat *stat, unsigned int size)
436{ 375{
437 int i; 376 int ret;
438
439 stat->buf_alloc_size = size;
440
441 for (i = 0; i < STAT_MAX_BUFS; i++) {
442 struct ispstat_buffer *buf = &stat->buf[i];
443
444 WARN_ON(buf->iommu_addr);
445 buf->virt_addr = dma_alloc_coherent(stat->isp->dev, size,
446 &buf->dma_addr, GFP_KERNEL | GFP_DMA);
447 377
448 if (!buf->virt_addr || !buf->dma_addr) { 378 buf->virt_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
449 dev_info(stat->isp->dev, 379 GFP_KERNEL | GFP_DMA);
450 "%s: Can't acquire memory for " 380 if (!buf->virt_addr)
451 "DMA buffer %d\n", stat->subdev.name, i); 381 return -ENOMEM;
452 isp_stat_bufs_free(stat);
453 return -ENOMEM;
454 }
455 buf->empty = 1;
456 382
457 dev_dbg(stat->isp->dev, "%s: buffer[%d] allocated." 383 ret = dma_get_sgtable(dev, &buf->sgt, buf->virt_addr, buf->dma_addr,
458 "dma_addr=0x%08lx virt_addr=0x%08lx\n", 384 size);
459 stat->subdev.name, i, (unsigned long)buf->dma_addr, 385 if (ret < 0) {
460 (unsigned long)buf->virt_addr); 386 dma_free_coherent(dev, size, buf->virt_addr, buf->dma_addr);
387 buf->virt_addr = NULL;
388 buf->dma_addr = 0;
389 return ret;
461 } 390 }
462 391
463 return 0; 392 return 0;
464} 393}
465 394
395/*
396 * The device passed to the DMA API depends on whether the statistics block uses
397 * ISP DMA, external DMA or PIO to transfer data.
398 *
399 * The first case (for the AEWB and AF engines) passes the ISP device, resulting
400 * in the DMA buffers being mapped through the ISP IOMMU.
401 *
402 * The second case (for the histogram engine) should pass the DMA engine device.
403 * As that device isn't accessible through the OMAP DMA engine API the driver
404 * passes NULL instead, resulting in the buffers being mapped directly as
405 * physical pages.
406 *
407 * The third case (for the histogram engine) doesn't require any mapping. The
408 * buffers could be allocated with kmalloc/vmalloc, but we still use
409 * dma_alloc_coherent() for consistency purpose.
410 */
466static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size) 411static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
467{ 412{
413 struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
414 ? NULL : stat->isp->dev;
468 unsigned long flags; 415 unsigned long flags;
416 unsigned int i;
469 417
470 spin_lock_irqsave(&stat->isp->stat_lock, flags); 418 spin_lock_irqsave(&stat->isp->stat_lock, flags);
471 419
@@ -489,10 +437,31 @@ static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
489 437
490 isp_stat_bufs_free(stat); 438 isp_stat_bufs_free(stat);
491 439
492 if (IS_COHERENT_BUF(stat)) 440 stat->buf_alloc_size = size;
493 return isp_stat_bufs_alloc_dma(stat, size); 441
494 else 442 for (i = 0; i < STAT_MAX_BUFS; i++) {
495 return isp_stat_bufs_alloc_iommu(stat, size); 443 struct ispstat_buffer *buf = &stat->buf[i];
444 int ret;
445
446 ret = isp_stat_bufs_alloc_one(dev, buf, size);
447 if (ret < 0) {
448 dev_err(stat->isp->dev,
449 "%s: Failed to allocate DMA buffer %u\n",
450 stat->subdev.name, i);
451 isp_stat_bufs_free(stat);
452 return ret;
453 }
454
455 buf->empty = 1;
456
457 dev_dbg(stat->isp->dev,
458 "%s: buffer[%u] allocated. dma=0x%08lx virt=0x%08lx",
459 stat->subdev.name, i,
460 (unsigned long)buf->dma_addr,
461 (unsigned long)buf->virt_addr);
462 }
463
464 return 0;
496} 465}
497 466
498static void isp_stat_queue_event(struct ispstat *stat, int err) 467static void isp_stat_queue_event(struct ispstat *stat, int err)
diff --git a/drivers/media/platform/omap3isp/ispstat.h b/drivers/media/platform/omap3isp/ispstat.h
index 9a047c929b9f..58d6ac7cb664 100644
--- a/drivers/media/platform/omap3isp/ispstat.h
+++ b/drivers/media/platform/omap3isp/ispstat.h
@@ -46,8 +46,7 @@
46struct ispstat; 46struct ispstat;
47 47
48struct ispstat_buffer { 48struct ispstat_buffer {
49 unsigned long iommu_addr; 49 struct sg_table sgt;
50 struct iovm_struct *iovm;
51 void *virt_addr; 50 void *virt_addr;
52 dma_addr_t dma_addr; 51 dma_addr_t dma_addr;
53 struct timespec ts; 52 struct timespec ts;
diff --git a/drivers/media/platform/omap3isp/ispvideo.c b/drivers/media/platform/omap3isp/ispvideo.c
index 85b4036ba5e4..e36bac26476c 100644
--- a/drivers/media/platform/omap3isp/ispvideo.c
+++ b/drivers/media/platform/omap3isp/ispvideo.c
@@ -27,7 +27,6 @@
27#include <linux/clk.h> 27#include <linux/clk.h>
28#include <linux/mm.h> 28#include <linux/mm.h>
29#include <linux/module.h> 29#include <linux/module.h>
30#include <linux/omap-iommu.h>
31#include <linux/pagemap.h> 30#include <linux/pagemap.h>
32#include <linux/scatterlist.h> 31#include <linux/scatterlist.h>
33#include <linux/sched.h> 32#include <linux/sched.h>
@@ -35,6 +34,7 @@
35#include <linux/vmalloc.h> 34#include <linux/vmalloc.h>
36#include <media/v4l2-dev.h> 35#include <media/v4l2-dev.h>
37#include <media/v4l2-ioctl.h> 36#include <media/v4l2-ioctl.h>
37#include <media/videobuf2-dma-contig.h>
38 38
39#include "ispvideo.h" 39#include "ispvideo.h"
40#include "isp.h" 40#include "isp.h"
@@ -326,90 +326,36 @@ isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
326} 326}
327 327
328/* ----------------------------------------------------------------------------- 328/* -----------------------------------------------------------------------------
329 * IOMMU management
330 */
331
332#define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
333
334/*
335 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
336 * @isp: Device pointer specific to the OMAP3 ISP.
337 * @sglist: Pointer to source Scatter gather list to allocate.
338 * @sglen: Number of elements of the scatter-gatter list.
339 *
340 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
341 * we ran out of memory.
342 */
343static dma_addr_t
344ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
345{
346 struct sg_table *sgt;
347 u32 da;
348
349 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
350 if (sgt == NULL)
351 return -ENOMEM;
352
353 sgt->sgl = (struct scatterlist *)sglist;
354 sgt->nents = sglen;
355 sgt->orig_nents = sglen;
356
357 da = omap_iommu_vmap(isp->domain, isp->dev, 0, sgt, IOMMU_FLAG);
358 if (IS_ERR_VALUE(da))
359 kfree(sgt);
360
361 return da;
362}
363
364/*
365 * ispmmu_vunmap - Unmap a device address from the ISP MMU
366 * @isp: Device pointer specific to the OMAP3 ISP.
367 * @da: Device address generated from a ispmmu_vmap call.
368 */
369static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
370{
371 struct sg_table *sgt;
372
373 sgt = omap_iommu_vunmap(isp->domain, isp->dev, (u32)da);
374 kfree(sgt);
375}
376
377/* -----------------------------------------------------------------------------
378 * Video queue operations 329 * Video queue operations
379 */ 330 */
380 331
381static void isp_video_queue_prepare(struct isp_video_queue *queue, 332static int isp_video_queue_setup(struct vb2_queue *queue,
382 unsigned int *nbuffers, unsigned int *size) 333 const struct v4l2_format *fmt,
334 unsigned int *count, unsigned int *num_planes,
335 unsigned int sizes[], void *alloc_ctxs[])
383{ 336{
384 struct isp_video_fh *vfh = 337 struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
385 container_of(queue, struct isp_video_fh, queue);
386 struct isp_video *video = vfh->video; 338 struct isp_video *video = vfh->video;
387 339
388 *size = vfh->format.fmt.pix.sizeimage; 340 *num_planes = 1;
389 if (*size == 0)
390 return;
391 341
392 *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size)); 342 sizes[0] = vfh->format.fmt.pix.sizeimage;
393} 343 if (sizes[0] == 0)
344 return -EINVAL;
394 345
395static void isp_video_buffer_cleanup(struct isp_video_buffer *buf) 346 alloc_ctxs[0] = video->alloc_ctx;
396{
397 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
398 struct isp_buffer *buffer = to_isp_buffer(buf);
399 struct isp_video *video = vfh->video;
400 347
401 if (buffer->isp_addr) { 348 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
402 ispmmu_vunmap(video->isp, buffer->isp_addr); 349
403 buffer->isp_addr = 0; 350 return 0;
404 }
405} 351}
406 352
407static int isp_video_buffer_prepare(struct isp_video_buffer *buf) 353static int isp_video_buffer_prepare(struct vb2_buffer *buf)
408{ 354{
409 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue); 355 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
410 struct isp_buffer *buffer = to_isp_buffer(buf); 356 struct isp_buffer *buffer = to_isp_buffer(buf);
411 struct isp_video *video = vfh->video; 357 struct isp_video *video = vfh->video;
412 unsigned long addr; 358 dma_addr_t addr;
413 359
414 /* Refuse to prepare the buffer is the video node has registered an 360 /* Refuse to prepare the buffer is the video node has registered an
415 * error. We don't need to take any lock here as the operation is 361 * error. We don't need to take any lock here as the operation is
@@ -420,19 +366,16 @@ static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
420 if (unlikely(video->error)) 366 if (unlikely(video->error))
421 return -EIO; 367 return -EIO;
422 368
423 addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen); 369 addr = vb2_dma_contig_plane_dma_addr(buf, 0);
424 if (IS_ERR_VALUE(addr))
425 return -EIO;
426
427 if (!IS_ALIGNED(addr, 32)) { 370 if (!IS_ALIGNED(addr, 32)) {
428 dev_dbg(video->isp->dev, "Buffer address must be " 371 dev_dbg(video->isp->dev,
429 "aligned to 32 bytes boundary.\n"); 372 "Buffer address must be aligned to 32 bytes boundary.\n");
430 ispmmu_vunmap(video->isp, buffer->isp_addr);
431 return -EINVAL; 373 return -EINVAL;
432 } 374 }
433 375
434 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage; 376 vb2_set_plane_payload(&buffer->vb, 0, vfh->format.fmt.pix.sizeimage);
435 buffer->isp_addr = addr; 377 buffer->dma = addr;
378
436 return 0; 379 return 0;
437} 380}
438 381
@@ -445,9 +388,9 @@ static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
445 * If the pipeline is busy, it will be restarted in the output module interrupt 388 * If the pipeline is busy, it will be restarted in the output module interrupt
446 * handler. 389 * handler.
447 */ 390 */
448static void isp_video_buffer_queue(struct isp_video_buffer *buf) 391static void isp_video_buffer_queue(struct vb2_buffer *buf)
449{ 392{
450 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue); 393 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
451 struct isp_buffer *buffer = to_isp_buffer(buf); 394 struct isp_buffer *buffer = to_isp_buffer(buf);
452 struct isp_video *video = vfh->video; 395 struct isp_video *video = vfh->video;
453 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity); 396 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
@@ -456,14 +399,18 @@ static void isp_video_buffer_queue(struct isp_video_buffer *buf)
456 unsigned int empty; 399 unsigned int empty;
457 unsigned int start; 400 unsigned int start;
458 401
402 spin_lock_irqsave(&video->irqlock, flags);
403
459 if (unlikely(video->error)) { 404 if (unlikely(video->error)) {
460 buf->state = ISP_BUF_STATE_ERROR; 405 vb2_buffer_done(&buffer->vb, VB2_BUF_STATE_ERROR);
461 wake_up(&buf->wait); 406 spin_unlock_irqrestore(&video->irqlock, flags);
462 return; 407 return;
463 } 408 }
464 409
465 empty = list_empty(&video->dmaqueue); 410 empty = list_empty(&video->dmaqueue);
466 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue); 411 list_add_tail(&buffer->irqlist, &video->dmaqueue);
412
413 spin_unlock_irqrestore(&video->irqlock, flags);
467 414
468 if (empty) { 415 if (empty) {
469 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 416 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
@@ -487,23 +434,22 @@ static void isp_video_buffer_queue(struct isp_video_buffer *buf)
487 } 434 }
488} 435}
489 436
490static const struct isp_video_queue_operations isp_video_queue_ops = { 437static const struct vb2_ops isp_video_queue_ops = {
491 .queue_prepare = &isp_video_queue_prepare, 438 .queue_setup = isp_video_queue_setup,
492 .buffer_prepare = &isp_video_buffer_prepare, 439 .buf_prepare = isp_video_buffer_prepare,
493 .buffer_queue = &isp_video_buffer_queue, 440 .buf_queue = isp_video_buffer_queue,
494 .buffer_cleanup = &isp_video_buffer_cleanup,
495}; 441};
496 442
497/* 443/*
498 * omap3isp_video_buffer_next - Complete the current buffer and return the next 444 * omap3isp_video_buffer_next - Complete the current buffer and return the next
499 * @video: ISP video object 445 * @video: ISP video object
500 * 446 *
501 * Remove the current video buffer from the DMA queue and fill its timestamp, 447 * Remove the current video buffer from the DMA queue and fill its timestamp and
502 * field count and state fields before waking up its completion handler. 448 * field count before handing it back to videobuf2.
503 * 449 *
504 * For capture video nodes the buffer state is set to ISP_BUF_STATE_DONE if no 450 * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
505 * error has been flagged in the pipeline, or to ISP_BUF_STATE_ERROR otherwise. 451 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
506 * For video output nodes the buffer state is always set to ISP_BUF_STATE_DONE. 452 * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
507 * 453 *
508 * The DMA queue is expected to contain at least one buffer. 454 * The DMA queue is expected to contain at least one buffer.
509 * 455 *
@@ -513,26 +459,25 @@ static const struct isp_video_queue_operations isp_video_queue_ops = {
513struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video) 459struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
514{ 460{
515 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity); 461 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
516 struct isp_video_queue *queue = video->queue;
517 enum isp_pipeline_state state; 462 enum isp_pipeline_state state;
518 struct isp_video_buffer *buf; 463 struct isp_buffer *buf;
519 unsigned long flags; 464 unsigned long flags;
520 struct timespec ts; 465 struct timespec ts;
521 466
522 spin_lock_irqsave(&queue->irqlock, flags); 467 spin_lock_irqsave(&video->irqlock, flags);
523 if (WARN_ON(list_empty(&video->dmaqueue))) { 468 if (WARN_ON(list_empty(&video->dmaqueue))) {
524 spin_unlock_irqrestore(&queue->irqlock, flags); 469 spin_unlock_irqrestore(&video->irqlock, flags);
525 return NULL; 470 return NULL;
526 } 471 }
527 472
528 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer, 473 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
529 irqlist); 474 irqlist);
530 list_del(&buf->irqlist); 475 list_del(&buf->irqlist);
531 spin_unlock_irqrestore(&queue->irqlock, flags); 476 spin_unlock_irqrestore(&video->irqlock, flags);
532 477
533 ktime_get_ts(&ts); 478 ktime_get_ts(&ts);
534 buf->vbuf.timestamp.tv_sec = ts.tv_sec; 479 buf->vb.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
535 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC; 480 buf->vb.v4l2_buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
536 481
537 /* Do frame number propagation only if this is the output video node. 482 /* Do frame number propagation only if this is the output video node.
538 * Frame number either comes from the CSI receivers or it gets 483 * Frame number either comes from the CSI receivers or it gets
@@ -541,22 +486,27 @@ struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
541 * first, so the input number might lag behind by 1 in some cases. 486 * first, so the input number might lag behind by 1 in some cases.
542 */ 487 */
543 if (video == pipe->output && !pipe->do_propagation) 488 if (video == pipe->output && !pipe->do_propagation)
544 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number); 489 buf->vb.v4l2_buf.sequence =
490 atomic_inc_return(&pipe->frame_number);
545 else 491 else
546 buf->vbuf.sequence = atomic_read(&pipe->frame_number); 492 buf->vb.v4l2_buf.sequence = atomic_read(&pipe->frame_number);
547 493
548 /* Report pipeline errors to userspace on the capture device side. */ 494 /* Report pipeline errors to userspace on the capture device side. */
549 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) { 495 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
550 buf->state = ISP_BUF_STATE_ERROR; 496 state = VB2_BUF_STATE_ERROR;
551 pipe->error = false; 497 pipe->error = false;
552 } else { 498 } else {
553 buf->state = ISP_BUF_STATE_DONE; 499 state = VB2_BUF_STATE_DONE;
554 } 500 }
555 501
556 wake_up(&buf->wait); 502 vb2_buffer_done(&buf->vb, state);
503
504 spin_lock_irqsave(&video->irqlock, flags);
557 505
558 if (list_empty(&video->dmaqueue)) { 506 if (list_empty(&video->dmaqueue)) {
559 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 507 spin_unlock_irqrestore(&video->irqlock, flags);
508
509 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
560 state = ISP_PIPELINE_QUEUE_OUTPUT 510 state = ISP_PIPELINE_QUEUE_OUTPUT
561 | ISP_PIPELINE_STREAM; 511 | ISP_PIPELINE_STREAM;
562 else 512 else
@@ -571,16 +521,19 @@ struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
571 return NULL; 521 return NULL;
572 } 522 }
573 523
574 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) { 524 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
575 spin_lock_irqsave(&pipe->lock, flags); 525 spin_lock(&pipe->lock);
576 pipe->state &= ~ISP_PIPELINE_STREAM; 526 pipe->state &= ~ISP_PIPELINE_STREAM;
577 spin_unlock_irqrestore(&pipe->lock, flags); 527 spin_unlock(&pipe->lock);
578 } 528 }
579 529
580 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer, 530 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
581 irqlist); 531 irqlist);
582 buf->state = ISP_BUF_STATE_ACTIVE; 532 buf->vb.state = VB2_BUF_STATE_ACTIVE;
583 return to_isp_buffer(buf); 533
534 spin_unlock_irqrestore(&video->irqlock, flags);
535
536 return buf;
584} 537}
585 538
586/* 539/*
@@ -592,25 +545,22 @@ struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
592 */ 545 */
593void omap3isp_video_cancel_stream(struct isp_video *video) 546void omap3isp_video_cancel_stream(struct isp_video *video)
594{ 547{
595 struct isp_video_queue *queue = video->queue;
596 unsigned long flags; 548 unsigned long flags;
597 549
598 spin_lock_irqsave(&queue->irqlock, flags); 550 spin_lock_irqsave(&video->irqlock, flags);
599 551
600 while (!list_empty(&video->dmaqueue)) { 552 while (!list_empty(&video->dmaqueue)) {
601 struct isp_video_buffer *buf; 553 struct isp_buffer *buf;
602 554
603 buf = list_first_entry(&video->dmaqueue, 555 buf = list_first_entry(&video->dmaqueue,
604 struct isp_video_buffer, irqlist); 556 struct isp_buffer, irqlist);
605 list_del(&buf->irqlist); 557 list_del(&buf->irqlist);
606 558 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
607 buf->state = ISP_BUF_STATE_ERROR;
608 wake_up(&buf->wait);
609 } 559 }
610 560
611 video->error = true; 561 video->error = true;
612 562
613 spin_unlock_irqrestore(&queue->irqlock, flags); 563 spin_unlock_irqrestore(&video->irqlock, flags);
614} 564}
615 565
616/* 566/*
@@ -627,12 +577,15 @@ void omap3isp_video_resume(struct isp_video *video, int continuous)
627{ 577{
628 struct isp_buffer *buf = NULL; 578 struct isp_buffer *buf = NULL;
629 579
630 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 580 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
631 omap3isp_video_queue_discard_done(video->queue); 581 mutex_lock(&video->queue_lock);
582 vb2_discard_done(video->queue);
583 mutex_unlock(&video->queue_lock);
584 }
632 585
633 if (!list_empty(&video->dmaqueue)) { 586 if (!list_empty(&video->dmaqueue)) {
634 buf = list_first_entry(&video->dmaqueue, 587 buf = list_first_entry(&video->dmaqueue,
635 struct isp_buffer, buffer.irqlist); 588 struct isp_buffer, irqlist);
636 video->ops->queue(video, buf); 589 video->ops->queue(video, buf);
637 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED; 590 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
638 } else { 591 } else {
@@ -840,33 +793,56 @@ static int
840isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb) 793isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
841{ 794{
842 struct isp_video_fh *vfh = to_isp_video_fh(fh); 795 struct isp_video_fh *vfh = to_isp_video_fh(fh);
796 struct isp_video *video = video_drvdata(file);
797 int ret;
843 798
844 return omap3isp_video_queue_reqbufs(&vfh->queue, rb); 799 mutex_lock(&video->queue_lock);
800 ret = vb2_reqbufs(&vfh->queue, rb);
801 mutex_unlock(&video->queue_lock);
802
803 return ret;
845} 804}
846 805
847static int 806static int
848isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b) 807isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
849{ 808{
850 struct isp_video_fh *vfh = to_isp_video_fh(fh); 809 struct isp_video_fh *vfh = to_isp_video_fh(fh);
810 struct isp_video *video = video_drvdata(file);
811 int ret;
812
813 mutex_lock(&video->queue_lock);
814 ret = vb2_querybuf(&vfh->queue, b);
815 mutex_unlock(&video->queue_lock);
851 816
852 return omap3isp_video_queue_querybuf(&vfh->queue, b); 817 return ret;
853} 818}
854 819
855static int 820static int
856isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b) 821isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
857{ 822{
858 struct isp_video_fh *vfh = to_isp_video_fh(fh); 823 struct isp_video_fh *vfh = to_isp_video_fh(fh);
824 struct isp_video *video = video_drvdata(file);
825 int ret;
859 826
860 return omap3isp_video_queue_qbuf(&vfh->queue, b); 827 mutex_lock(&video->queue_lock);
828 ret = vb2_qbuf(&vfh->queue, b);
829 mutex_unlock(&video->queue_lock);
830
831 return ret;
861} 832}
862 833
863static int 834static int
864isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b) 835isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
865{ 836{
866 struct isp_video_fh *vfh = to_isp_video_fh(fh); 837 struct isp_video_fh *vfh = to_isp_video_fh(fh);
838 struct isp_video *video = video_drvdata(file);
839 int ret;
840
841 mutex_lock(&video->queue_lock);
842 ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
843 mutex_unlock(&video->queue_lock);
867 844
868 return omap3isp_video_queue_dqbuf(&vfh->queue, b, 845 return ret;
869 file->f_flags & O_NONBLOCK);
870} 846}
871 847
872static int isp_video_check_external_subdevs(struct isp_video *video, 848static int isp_video_check_external_subdevs(struct isp_video *video,
@@ -1006,11 +982,6 @@ isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1006 982
1007 mutex_lock(&video->stream_lock); 983 mutex_lock(&video->stream_lock);
1008 984
1009 if (video->streaming) {
1010 mutex_unlock(&video->stream_lock);
1011 return -EBUSY;
1012 }
1013
1014 /* Start streaming on the pipeline. No link touching an entity in the 985 /* Start streaming on the pipeline. No link touching an entity in the
1015 * pipeline can be activated or deactivated once streaming is started. 986 * pipeline can be activated or deactivated once streaming is started.
1016 */ 987 */
@@ -1069,7 +1040,9 @@ isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1069 INIT_LIST_HEAD(&video->dmaqueue); 1040 INIT_LIST_HEAD(&video->dmaqueue);
1070 atomic_set(&pipe->frame_number, -1); 1041 atomic_set(&pipe->frame_number, -1);
1071 1042
1072 ret = omap3isp_video_queue_streamon(&vfh->queue); 1043 mutex_lock(&video->queue_lock);
1044 ret = vb2_streamon(&vfh->queue, type);
1045 mutex_unlock(&video->queue_lock);
1073 if (ret < 0) 1046 if (ret < 0)
1074 goto err_check_format; 1047 goto err_check_format;
1075 1048
@@ -1082,19 +1055,19 @@ isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1082 ISP_PIPELINE_STREAM_CONTINUOUS); 1055 ISP_PIPELINE_STREAM_CONTINUOUS);
1083 if (ret < 0) 1056 if (ret < 0)
1084 goto err_set_stream; 1057 goto err_set_stream;
1085 spin_lock_irqsave(&video->queue->irqlock, flags); 1058 spin_lock_irqsave(&video->irqlock, flags);
1086 if (list_empty(&video->dmaqueue)) 1059 if (list_empty(&video->dmaqueue))
1087 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN; 1060 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1088 spin_unlock_irqrestore(&video->queue->irqlock, flags); 1061 spin_unlock_irqrestore(&video->irqlock, flags);
1089 } 1062 }
1090 1063
1091 video->streaming = 1;
1092
1093 mutex_unlock(&video->stream_lock); 1064 mutex_unlock(&video->stream_lock);
1094 return 0; 1065 return 0;
1095 1066
1096err_set_stream: 1067err_set_stream:
1097 omap3isp_video_queue_streamoff(&vfh->queue); 1068 mutex_lock(&video->queue_lock);
1069 vb2_streamoff(&vfh->queue, type);
1070 mutex_unlock(&video->queue_lock);
1098err_check_format: 1071err_check_format:
1099 media_entity_pipeline_stop(&video->video.entity); 1072 media_entity_pipeline_stop(&video->video.entity);
1100err_pipeline_start: 1073err_pipeline_start:
@@ -1130,9 +1103,9 @@ isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1130 mutex_lock(&video->stream_lock); 1103 mutex_lock(&video->stream_lock);
1131 1104
1132 /* Make sure we're not streaming yet. */ 1105 /* Make sure we're not streaming yet. */
1133 mutex_lock(&vfh->queue.lock); 1106 mutex_lock(&video->queue_lock);
1134 streaming = vfh->queue.streaming; 1107 streaming = vb2_is_streaming(&vfh->queue);
1135 mutex_unlock(&vfh->queue.lock); 1108 mutex_unlock(&video->queue_lock);
1136 1109
1137 if (!streaming) 1110 if (!streaming)
1138 goto done; 1111 goto done;
@@ -1151,9 +1124,12 @@ isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1151 1124
1152 /* Stop the stream. */ 1125 /* Stop the stream. */
1153 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED); 1126 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1154 omap3isp_video_queue_streamoff(&vfh->queue); 1127 omap3isp_video_cancel_stream(video);
1128
1129 mutex_lock(&video->queue_lock);
1130 vb2_streamoff(&vfh->queue, type);
1131 mutex_unlock(&video->queue_lock);
1155 video->queue = NULL; 1132 video->queue = NULL;
1156 video->streaming = 0;
1157 video->error = false; 1133 video->error = false;
1158 1134
1159 if (video->isp->pdata->set_constraints) 1135 if (video->isp->pdata->set_constraints)
@@ -1223,6 +1199,7 @@ static int isp_video_open(struct file *file)
1223{ 1199{
1224 struct isp_video *video = video_drvdata(file); 1200 struct isp_video *video = video_drvdata(file);
1225 struct isp_video_fh *handle; 1201 struct isp_video_fh *handle;
1202 struct vb2_queue *queue;
1226 int ret = 0; 1203 int ret = 0;
1227 1204
1228 handle = kzalloc(sizeof(*handle), GFP_KERNEL); 1205 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
@@ -1244,9 +1221,20 @@ static int isp_video_open(struct file *file)
1244 goto done; 1221 goto done;
1245 } 1222 }
1246 1223
1247 omap3isp_video_queue_init(&handle->queue, video->type, 1224 queue = &handle->queue;
1248 &isp_video_queue_ops, video->isp->dev, 1225 queue->type = video->type;
1249 sizeof(struct isp_buffer)); 1226 queue->io_modes = VB2_MMAP | VB2_USERPTR;
1227 queue->drv_priv = handle;
1228 queue->ops = &isp_video_queue_ops;
1229 queue->mem_ops = &vb2_dma_contig_memops;
1230 queue->buf_struct_size = sizeof(struct isp_buffer);
1231 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1232
1233 ret = vb2_queue_init(&handle->queue);
1234 if (ret < 0) {
1235 omap3isp_put(video->isp);
1236 goto done;
1237 }
1250 1238
1251 memset(&handle->format, 0, sizeof(handle->format)); 1239 memset(&handle->format, 0, sizeof(handle->format));
1252 handle->format.type = video->type; 1240 handle->format.type = video->type;
@@ -1273,9 +1261,9 @@ static int isp_video_release(struct file *file)
1273 /* Disable streaming and free the buffers queue resources. */ 1261 /* Disable streaming and free the buffers queue resources. */
1274 isp_video_streamoff(file, vfh, video->type); 1262 isp_video_streamoff(file, vfh, video->type);
1275 1263
1276 mutex_lock(&handle->queue.lock); 1264 mutex_lock(&video->queue_lock);
1277 omap3isp_video_queue_cleanup(&handle->queue); 1265 vb2_queue_release(&handle->queue);
1278 mutex_unlock(&handle->queue.lock); 1266 mutex_unlock(&video->queue_lock);
1279 1267
1280 omap3isp_pipeline_pm_use(&video->video.entity, 0); 1268 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1281 1269
@@ -1292,16 +1280,27 @@ static int isp_video_release(struct file *file)
1292static unsigned int isp_video_poll(struct file *file, poll_table *wait) 1280static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1293{ 1281{
1294 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data); 1282 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1295 struct isp_video_queue *queue = &vfh->queue; 1283 struct isp_video *video = video_drvdata(file);
1284 int ret;
1296 1285
1297 return omap3isp_video_queue_poll(queue, file, wait); 1286 mutex_lock(&video->queue_lock);
1287 ret = vb2_poll(&vfh->queue, file, wait);
1288 mutex_unlock(&video->queue_lock);
1289
1290 return ret;
1298} 1291}
1299 1292
1300static int isp_video_mmap(struct file *file, struct vm_area_struct *vma) 1293static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1301{ 1294{
1302 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data); 1295 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1296 struct isp_video *video = video_drvdata(file);
1297 int ret;
1298
1299 mutex_lock(&video->queue_lock);
1300 ret = vb2_mmap(&vfh->queue, vma);
1301 mutex_unlock(&video->queue_lock);
1303 1302
1304 return omap3isp_video_queue_mmap(&vfh->queue, vma); 1303 return ret;
1305} 1304}
1306 1305
1307static struct v4l2_file_operations isp_video_fops = { 1306static struct v4l2_file_operations isp_video_fops = {
@@ -1342,15 +1341,23 @@ int omap3isp_video_init(struct isp_video *video, const char *name)
1342 return -EINVAL; 1341 return -EINVAL;
1343 } 1342 }
1344 1343
1344 video->alloc_ctx = vb2_dma_contig_init_ctx(video->isp->dev);
1345 if (IS_ERR(video->alloc_ctx))
1346 return PTR_ERR(video->alloc_ctx);
1347
1345 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0); 1348 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1346 if (ret < 0) 1349 if (ret < 0) {
1350 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1347 return ret; 1351 return ret;
1352 }
1348 1353
1349 mutex_init(&video->mutex); 1354 mutex_init(&video->mutex);
1350 atomic_set(&video->active, 0); 1355 atomic_set(&video->active, 0);
1351 1356
1352 spin_lock_init(&video->pipe.lock); 1357 spin_lock_init(&video->pipe.lock);
1353 mutex_init(&video->stream_lock); 1358 mutex_init(&video->stream_lock);
1359 mutex_init(&video->queue_lock);
1360 spin_lock_init(&video->irqlock);
1354 1361
1355 /* Initialize the video device. */ 1362 /* Initialize the video device. */
1356 if (video->ops == NULL) 1363 if (video->ops == NULL)
@@ -1371,7 +1378,9 @@ int omap3isp_video_init(struct isp_video *video, const char *name)
1371 1378
1372void omap3isp_video_cleanup(struct isp_video *video) 1379void omap3isp_video_cleanup(struct isp_video *video)
1373{ 1380{
1381 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1374 media_entity_cleanup(&video->video.entity); 1382 media_entity_cleanup(&video->video.entity);
1383 mutex_destroy(&video->queue_lock);
1375 mutex_destroy(&video->stream_lock); 1384 mutex_destroy(&video->stream_lock);
1376 mutex_destroy(&video->mutex); 1385 mutex_destroy(&video->mutex);
1377} 1386}
diff --git a/drivers/media/platform/omap3isp/ispvideo.h b/drivers/media/platform/omap3isp/ispvideo.h
index 4e194076cc60..7d2e82122ecd 100644
--- a/drivers/media/platform/omap3isp/ispvideo.h
+++ b/drivers/media/platform/omap3isp/ispvideo.h
@@ -30,8 +30,7 @@
30#include <media/media-entity.h> 30#include <media/media-entity.h>
31#include <media/v4l2-dev.h> 31#include <media/v4l2-dev.h>
32#include <media/v4l2-fh.h> 32#include <media/v4l2-fh.h>
33 33#include <media/videobuf2-core.h>
34#include "ispqueue.h"
35 34
36#define ISP_VIDEO_DRIVER_NAME "ispvideo" 35#define ISP_VIDEO_DRIVER_NAME "ispvideo"
37#define ISP_VIDEO_DRIVER_VERSION "0.0.2" 36#define ISP_VIDEO_DRIVER_VERSION "0.0.2"
@@ -124,17 +123,19 @@ static inline int isp_pipeline_ready(struct isp_pipeline *pipe)
124 ISP_PIPELINE_IDLE_OUTPUT); 123 ISP_PIPELINE_IDLE_OUTPUT);
125} 124}
126 125
127/* 126/**
128 * struct isp_buffer - ISP buffer 127 * struct isp_buffer - ISP video buffer
129 * @buffer: ISP video buffer 128 * @vb: videobuf2 buffer
130 * @isp_addr: MMU mapped address (a.k.a. device address) of the buffer. 129 * @irqlist: List head for insertion into IRQ queue
130 * @dma: DMA address
131 */ 131 */
132struct isp_buffer { 132struct isp_buffer {
133 struct isp_video_buffer buffer; 133 struct vb2_buffer vb;
134 dma_addr_t isp_addr; 134 struct list_head irqlist;
135 dma_addr_t dma;
135}; 136};
136 137
137#define to_isp_buffer(buf) container_of(buf, struct isp_buffer, buffer) 138#define to_isp_buffer(buf) container_of(buf, struct isp_buffer, vb)
138 139
139enum isp_video_dmaqueue_flags { 140enum isp_video_dmaqueue_flags {
140 /* Set if DMA queue becomes empty when ISP_PIPELINE_STREAM_CONTINUOUS */ 141 /* Set if DMA queue becomes empty when ISP_PIPELINE_STREAM_CONTINUOUS */
@@ -172,16 +173,16 @@ struct isp_video {
172 unsigned int bpl_value; /* bytes per line value */ 173 unsigned int bpl_value; /* bytes per line value */
173 unsigned int bpl_padding; /* padding at end of line */ 174 unsigned int bpl_padding; /* padding at end of line */
174 175
175 /* Entity video node streaming */
176 unsigned int streaming:1;
177
178 /* Pipeline state */ 176 /* Pipeline state */
179 struct isp_pipeline pipe; 177 struct isp_pipeline pipe;
180 struct mutex stream_lock; /* pipeline and stream states */ 178 struct mutex stream_lock; /* pipeline and stream states */
181 bool error; 179 bool error;
182 180
183 /* Video buffers queue */ 181 /* Video buffers queue */
184 struct isp_video_queue *queue; 182 void *alloc_ctx;
183 struct vb2_queue *queue;
184 struct mutex queue_lock; /* protects the queue */
185 spinlock_t irqlock; /* protects dmaqueue */
185 struct list_head dmaqueue; 186 struct list_head dmaqueue;
186 enum isp_video_dmaqueue_flags dmaqueue_flags; 187 enum isp_video_dmaqueue_flags dmaqueue_flags;
187 188
@@ -193,7 +194,7 @@ struct isp_video {
193struct isp_video_fh { 194struct isp_video_fh {
194 struct v4l2_fh vfh; 195 struct v4l2_fh vfh;
195 struct isp_video *video; 196 struct isp_video *video;
196 struct isp_video_queue queue; 197 struct vb2_queue queue;
197 struct v4l2_format format; 198 struct v4l2_format format;
198 struct v4l2_fract timeperframe; 199 struct v4l2_fract timeperframe;
199}; 200};
diff --git a/drivers/media/platform/vsp1/Makefile b/drivers/media/platform/vsp1/Makefile
index 151cecd0ea25..6a93f928dfde 100644
--- a/drivers/media/platform/vsp1/Makefile
+++ b/drivers/media/platform/vsp1/Makefile
@@ -1,6 +1,6 @@
1vsp1-y := vsp1_drv.o vsp1_entity.o vsp1_video.o 1vsp1-y := vsp1_drv.o vsp1_entity.o vsp1_video.o
2vsp1-y += vsp1_rpf.o vsp1_rwpf.o vsp1_wpf.o 2vsp1-y += vsp1_rpf.o vsp1_rwpf.o vsp1_wpf.o
3vsp1-y += vsp1_hsit.o vsp1_lif.o vsp1_lut.o 3vsp1-y += vsp1_hsit.o vsp1_lif.o vsp1_lut.o
4vsp1-y += vsp1_sru.o vsp1_uds.o 4vsp1-y += vsp1_bru.o vsp1_sru.o vsp1_uds.o
5 5
6obj-$(CONFIG_VIDEO_RENESAS_VSP1) += vsp1.o 6obj-$(CONFIG_VIDEO_RENESAS_VSP1) += vsp1.o
diff --git a/drivers/media/platform/vsp1/vsp1.h b/drivers/media/platform/vsp1/vsp1.h
index 0313210c6e9e..6ca2cf20d545 100644
--- a/drivers/media/platform/vsp1/vsp1.h
+++ b/drivers/media/platform/vsp1/vsp1.h
@@ -28,6 +28,7 @@ struct clk;
28struct device; 28struct device;
29 29
30struct vsp1_platform_data; 30struct vsp1_platform_data;
31struct vsp1_bru;
31struct vsp1_hsit; 32struct vsp1_hsit;
32struct vsp1_lif; 33struct vsp1_lif;
33struct vsp1_lut; 34struct vsp1_lut;
@@ -45,11 +46,11 @@ struct vsp1_device {
45 46
46 void __iomem *mmio; 47 void __iomem *mmio;
47 struct clk *clock; 48 struct clk *clock;
48 struct clk *rt_clock;
49 49
50 struct mutex lock; 50 struct mutex lock;
51 int ref_count; 51 int ref_count;
52 52
53 struct vsp1_bru *bru;
53 struct vsp1_hsit *hsi; 54 struct vsp1_hsit *hsi;
54 struct vsp1_hsit *hst; 55 struct vsp1_hsit *hst;
55 struct vsp1_lif *lif; 56 struct vsp1_lif *lif;
diff --git a/drivers/media/platform/vsp1/vsp1_bru.c b/drivers/media/platform/vsp1/vsp1_bru.c
new file mode 100644
index 000000000000..f80695480060
--- /dev/null
+++ b/drivers/media/platform/vsp1/vsp1_bru.c
@@ -0,0 +1,395 @@
1/*
2 * vsp1_bru.c -- R-Car VSP1 Blend ROP Unit
3 *
4 * Copyright (C) 2013 Renesas Corporation
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/device.h>
15#include <linux/gfp.h>
16
17#include <media/v4l2-subdev.h>
18
19#include "vsp1.h"
20#include "vsp1_bru.h"
21
22#define BRU_MIN_SIZE 4U
23#define BRU_MAX_SIZE 8190U
24
25/* -----------------------------------------------------------------------------
26 * Device Access
27 */
28
29static inline u32 vsp1_bru_read(struct vsp1_bru *bru, u32 reg)
30{
31 return vsp1_read(bru->entity.vsp1, reg);
32}
33
34static inline void vsp1_bru_write(struct vsp1_bru *bru, u32 reg, u32 data)
35{
36 vsp1_write(bru->entity.vsp1, reg, data);
37}
38
39/* -----------------------------------------------------------------------------
40 * V4L2 Subdevice Core Operations
41 */
42
43static bool bru_is_input_enabled(struct vsp1_bru *bru, unsigned int input)
44{
45 return media_entity_remote_pad(&bru->entity.pads[input]) != NULL;
46}
47
48static int bru_s_stream(struct v4l2_subdev *subdev, int enable)
49{
50 struct vsp1_bru *bru = to_bru(subdev);
51 struct v4l2_mbus_framefmt *format;
52 unsigned int i;
53
54 if (!enable)
55 return 0;
56
57 format = &bru->entity.formats[BRU_PAD_SOURCE];
58
59 /* The hardware is extremely flexible but we have no userspace API to
60 * expose all the parameters, nor is it clear whether we would have use
61 * cases for all the supported modes. Let's just harcode the parameters
62 * to sane default values for now.
63 */
64
65 /* Disable both color data normalization and dithering. */
66 vsp1_bru_write(bru, VI6_BRU_INCTRL, 0);
67
68 /* Set the background position to cover the whole output image and
69 * set its color to opaque black.
70 */
71 vsp1_bru_write(bru, VI6_BRU_VIRRPF_SIZE,
72 (format->width << VI6_BRU_VIRRPF_SIZE_HSIZE_SHIFT) |
73 (format->height << VI6_BRU_VIRRPF_SIZE_VSIZE_SHIFT));
74 vsp1_bru_write(bru, VI6_BRU_VIRRPF_LOC, 0);
75 vsp1_bru_write(bru, VI6_BRU_VIRRPF_COL,
76 0xff << VI6_BRU_VIRRPF_COL_A_SHIFT);
77
78 /* Route BRU input 1 as SRC input to the ROP unit and configure the ROP
79 * unit with a NOP operation to make BRU input 1 available as the
80 * Blend/ROP unit B SRC input.
81 */
82 vsp1_bru_write(bru, VI6_BRU_ROP, VI6_BRU_ROP_DSTSEL_BRUIN(1) |
83 VI6_BRU_ROP_CROP(VI6_ROP_NOP) |
84 VI6_BRU_ROP_AROP(VI6_ROP_NOP));
85
86 for (i = 0; i < 4; ++i) {
87 u32 ctrl = 0;
88
89 /* Configure all Blend/ROP units corresponding to an enabled BRU
90 * input for alpha blending. Blend/ROP units corresponding to
91 * disabled BRU inputs are used in ROP NOP mode to ignore the
92 * SRC input.
93 */
94 if (bru_is_input_enabled(bru, i))
95 ctrl |= VI6_BRU_CTRL_RBC;
96 else
97 ctrl |= VI6_BRU_CTRL_CROP(VI6_ROP_NOP)
98 | VI6_BRU_CTRL_AROP(VI6_ROP_NOP);
99
100 /* Select the virtual RPF as the Blend/ROP unit A DST input to
101 * serve as a background color.
102 */
103 if (i == 0)
104 ctrl |= VI6_BRU_CTRL_DSTSEL_VRPF;
105
106 /* Route BRU inputs 0 to 3 as SRC inputs to Blend/ROP units A to
107 * D in that order. The Blend/ROP unit B SRC is hardwired to the
108 * ROP unit output, the corresponding register bits must be set
109 * to 0.
110 */
111 if (i != 1)
112 ctrl |= VI6_BRU_CTRL_SRCSEL_BRUIN(i);
113
114 vsp1_bru_write(bru, VI6_BRU_CTRL(i), ctrl);
115
116 /* Harcode the blending formula to
117 *
118 * DSTc = DSTc * (1 - SRCa) + SRCc * SRCa
119 * DSTa = DSTa * (1 - SRCa) + SRCa
120 */
121 vsp1_bru_write(bru, VI6_BRU_BLD(i),
122 VI6_BRU_BLD_CCMDX_255_SRC_A |
123 VI6_BRU_BLD_CCMDY_SRC_A |
124 VI6_BRU_BLD_ACMDX_255_SRC_A |
125 VI6_BRU_BLD_ACMDY_COEFY |
126 (0xff << VI6_BRU_BLD_COEFY_SHIFT));
127 }
128
129 return 0;
130}
131
132/* -----------------------------------------------------------------------------
133 * V4L2 Subdevice Pad Operations
134 */
135
136/*
137 * The BRU can't perform format conversion, all sink and source formats must be
138 * identical. We pick the format on the first sink pad (pad 0) and propagate it
139 * to all other pads.
140 */
141
142static int bru_enum_mbus_code(struct v4l2_subdev *subdev,
143 struct v4l2_subdev_fh *fh,
144 struct v4l2_subdev_mbus_code_enum *code)
145{
146 static const unsigned int codes[] = {
147 V4L2_MBUS_FMT_ARGB8888_1X32,
148 V4L2_MBUS_FMT_AYUV8_1X32,
149 };
150 struct v4l2_mbus_framefmt *format;
151
152 if (code->pad == BRU_PAD_SINK(0)) {
153 if (code->index >= ARRAY_SIZE(codes))
154 return -EINVAL;
155
156 code->code = codes[code->index];
157 } else {
158 if (code->index)
159 return -EINVAL;
160
161 format = v4l2_subdev_get_try_format(fh, BRU_PAD_SINK(0));
162 code->code = format->code;
163 }
164
165 return 0;
166}
167
168static int bru_enum_frame_size(struct v4l2_subdev *subdev,
169 struct v4l2_subdev_fh *fh,
170 struct v4l2_subdev_frame_size_enum *fse)
171{
172 if (fse->index)
173 return -EINVAL;
174
175 if (fse->code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
176 fse->code != V4L2_MBUS_FMT_AYUV8_1X32)
177 return -EINVAL;
178
179 fse->min_width = BRU_MIN_SIZE;
180 fse->max_width = BRU_MAX_SIZE;
181 fse->min_height = BRU_MIN_SIZE;
182 fse->max_height = BRU_MAX_SIZE;
183
184 return 0;
185}
186
187static struct v4l2_rect *bru_get_compose(struct vsp1_bru *bru,
188 struct v4l2_subdev_fh *fh,
189 unsigned int pad, u32 which)
190{
191 switch (which) {
192 case V4L2_SUBDEV_FORMAT_TRY:
193 return v4l2_subdev_get_try_crop(fh, pad);
194 case V4L2_SUBDEV_FORMAT_ACTIVE:
195 return &bru->compose[pad];
196 default:
197 return NULL;
198 }
199}
200
201static int bru_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
202 struct v4l2_subdev_format *fmt)
203{
204 struct vsp1_bru *bru = to_bru(subdev);
205
206 fmt->format = *vsp1_entity_get_pad_format(&bru->entity, fh, fmt->pad,
207 fmt->which);
208
209 return 0;
210}
211
212static void bru_try_format(struct vsp1_bru *bru, struct v4l2_subdev_fh *fh,
213 unsigned int pad, struct v4l2_mbus_framefmt *fmt,
214 enum v4l2_subdev_format_whence which)
215{
216 struct v4l2_mbus_framefmt *format;
217
218 switch (pad) {
219 case BRU_PAD_SINK(0):
220 /* Default to YUV if the requested format is not supported. */
221 if (fmt->code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
222 fmt->code != V4L2_MBUS_FMT_AYUV8_1X32)
223 fmt->code = V4L2_MBUS_FMT_AYUV8_1X32;
224 break;
225
226 default:
227 /* The BRU can't perform format conversion. */
228 format = vsp1_entity_get_pad_format(&bru->entity, fh,
229 BRU_PAD_SINK(0), which);
230 fmt->code = format->code;
231 break;
232 }
233
234 fmt->width = clamp(fmt->width, BRU_MIN_SIZE, BRU_MAX_SIZE);
235 fmt->height = clamp(fmt->height, BRU_MIN_SIZE, BRU_MAX_SIZE);
236 fmt->field = V4L2_FIELD_NONE;
237 fmt->colorspace = V4L2_COLORSPACE_SRGB;
238}
239
240static int bru_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
241 struct v4l2_subdev_format *fmt)
242{
243 struct vsp1_bru *bru = to_bru(subdev);
244 struct v4l2_mbus_framefmt *format;
245
246 bru_try_format(bru, fh, fmt->pad, &fmt->format, fmt->which);
247
248 format = vsp1_entity_get_pad_format(&bru->entity, fh, fmt->pad,
249 fmt->which);
250 *format = fmt->format;
251
252 /* Reset the compose rectangle */
253 if (fmt->pad != BRU_PAD_SOURCE) {
254 struct v4l2_rect *compose;
255
256 compose = bru_get_compose(bru, fh, fmt->pad, fmt->which);
257 compose->left = 0;
258 compose->top = 0;
259 compose->width = format->width;
260 compose->height = format->height;
261 }
262
263 /* Propagate the format code to all pads */
264 if (fmt->pad == BRU_PAD_SINK(0)) {
265 unsigned int i;
266
267 for (i = 0; i <= BRU_PAD_SOURCE; ++i) {
268 format = vsp1_entity_get_pad_format(&bru->entity, fh,
269 i, fmt->which);
270 format->code = fmt->format.code;
271 }
272 }
273
274 return 0;
275}
276
277static int bru_get_selection(struct v4l2_subdev *subdev,
278 struct v4l2_subdev_fh *fh,
279 struct v4l2_subdev_selection *sel)
280{
281 struct vsp1_bru *bru = to_bru(subdev);
282
283 if (sel->pad == BRU_PAD_SOURCE)
284 return -EINVAL;
285
286 switch (sel->target) {
287 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
288 sel->r.left = 0;
289 sel->r.top = 0;
290 sel->r.width = BRU_MAX_SIZE;
291 sel->r.height = BRU_MAX_SIZE;
292 return 0;
293
294 case V4L2_SEL_TGT_COMPOSE:
295 sel->r = *bru_get_compose(bru, fh, sel->pad, sel->which);
296 return 0;
297
298 default:
299 return -EINVAL;
300 }
301}
302
303static int bru_set_selection(struct v4l2_subdev *subdev,
304 struct v4l2_subdev_fh *fh,
305 struct v4l2_subdev_selection *sel)
306{
307 struct vsp1_bru *bru = to_bru(subdev);
308 struct v4l2_mbus_framefmt *format;
309 struct v4l2_rect *compose;
310
311 if (sel->pad == BRU_PAD_SOURCE)
312 return -EINVAL;
313
314 if (sel->target != V4L2_SEL_TGT_COMPOSE)
315 return -EINVAL;
316
317 /* The compose rectangle top left corner must be inside the output
318 * frame.
319 */
320 format = vsp1_entity_get_pad_format(&bru->entity, fh, BRU_PAD_SOURCE,
321 sel->which);
322 sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
323 sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
324
325 /* Scaling isn't supported, the compose rectangle size must be identical
326 * to the sink format size.
327 */
328 format = vsp1_entity_get_pad_format(&bru->entity, fh, sel->pad,
329 sel->which);
330 sel->r.width = format->width;
331 sel->r.height = format->height;
332
333 compose = bru_get_compose(bru, fh, sel->pad, sel->which);
334 *compose = sel->r;
335
336 return 0;
337}
338
339/* -----------------------------------------------------------------------------
340 * V4L2 Subdevice Operations
341 */
342
343static struct v4l2_subdev_video_ops bru_video_ops = {
344 .s_stream = bru_s_stream,
345};
346
347static struct v4l2_subdev_pad_ops bru_pad_ops = {
348 .enum_mbus_code = bru_enum_mbus_code,
349 .enum_frame_size = bru_enum_frame_size,
350 .get_fmt = bru_get_format,
351 .set_fmt = bru_set_format,
352 .get_selection = bru_get_selection,
353 .set_selection = bru_set_selection,
354};
355
356static struct v4l2_subdev_ops bru_ops = {
357 .video = &bru_video_ops,
358 .pad = &bru_pad_ops,
359};
360
361/* -----------------------------------------------------------------------------
362 * Initialization and Cleanup
363 */
364
365struct vsp1_bru *vsp1_bru_create(struct vsp1_device *vsp1)
366{
367 struct v4l2_subdev *subdev;
368 struct vsp1_bru *bru;
369 int ret;
370
371 bru = devm_kzalloc(vsp1->dev, sizeof(*bru), GFP_KERNEL);
372 if (bru == NULL)
373 return ERR_PTR(-ENOMEM);
374
375 bru->entity.type = VSP1_ENTITY_BRU;
376
377 ret = vsp1_entity_init(vsp1, &bru->entity, 5);
378 if (ret < 0)
379 return ERR_PTR(ret);
380
381 /* Initialize the V4L2 subdev. */
382 subdev = &bru->entity.subdev;
383 v4l2_subdev_init(subdev, &bru_ops);
384
385 subdev->entity.ops = &vsp1_media_ops;
386 subdev->internal_ops = &vsp1_subdev_internal_ops;
387 snprintf(subdev->name, sizeof(subdev->name), "%s bru",
388 dev_name(vsp1->dev));
389 v4l2_set_subdevdata(subdev, bru);
390 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
391
392 vsp1_entity_init_formats(subdev, NULL);
393
394 return bru;
395}
diff --git a/drivers/media/platform/vsp1/vsp1_bru.h b/drivers/media/platform/vsp1/vsp1_bru.h
new file mode 100644
index 000000000000..37062704dbf6
--- /dev/null
+++ b/drivers/media/platform/vsp1/vsp1_bru.h
@@ -0,0 +1,39 @@
1/*
2 * vsp1_bru.h -- R-Car VSP1 Blend ROP Unit
3 *
4 * Copyright (C) 2013 Renesas Corporation
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13#ifndef __VSP1_BRU_H__
14#define __VSP1_BRU_H__
15
16#include <media/media-entity.h>
17#include <media/v4l2-subdev.h>
18
19#include "vsp1_entity.h"
20
21struct vsp1_device;
22
23#define BRU_PAD_SINK(n) (n)
24#define BRU_PAD_SOURCE 4
25
26struct vsp1_bru {
27 struct vsp1_entity entity;
28
29 struct v4l2_rect compose[4];
30};
31
32static inline struct vsp1_bru *to_bru(struct v4l2_subdev *subdev)
33{
34 return container_of(subdev, struct vsp1_bru, entity.subdev);
35}
36
37struct vsp1_bru *vsp1_bru_create(struct vsp1_device *vsp1);
38
39#endif /* __VSP1_BRU_H__ */
diff --git a/drivers/media/platform/vsp1/vsp1_drv.c b/drivers/media/platform/vsp1/vsp1_drv.c
index 2f74f0e0ddf5..c69ee0657f75 100644
--- a/drivers/media/platform/vsp1/vsp1_drv.c
+++ b/drivers/media/platform/vsp1/vsp1_drv.c
@@ -16,10 +16,12 @@
16#include <linux/device.h> 16#include <linux/device.h>
17#include <linux/interrupt.h> 17#include <linux/interrupt.h>
18#include <linux/module.h> 18#include <linux/module.h>
19#include <linux/of.h>
19#include <linux/platform_device.h> 20#include <linux/platform_device.h>
20#include <linux/videodev2.h> 21#include <linux/videodev2.h>
21 22
22#include "vsp1.h" 23#include "vsp1.h"
24#include "vsp1_bru.h"
23#include "vsp1_hsit.h" 25#include "vsp1_hsit.h"
24#include "vsp1_lif.h" 26#include "vsp1_lif.h"
25#include "vsp1_lut.h" 27#include "vsp1_lut.h"
@@ -155,6 +157,14 @@ static int vsp1_create_entities(struct vsp1_device *vsp1)
155 } 157 }
156 158
157 /* Instantiate all the entities. */ 159 /* Instantiate all the entities. */
160 vsp1->bru = vsp1_bru_create(vsp1);
161 if (IS_ERR(vsp1->bru)) {
162 ret = PTR_ERR(vsp1->bru);
163 goto done;
164 }
165
166 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
167
158 vsp1->hsi = vsp1_hsit_create(vsp1, true); 168 vsp1->hsi = vsp1_hsit_create(vsp1, true);
159 if (IS_ERR(vsp1->hsi)) { 169 if (IS_ERR(vsp1->hsi)) {
160 ret = PTR_ERR(vsp1->hsi); 170 ret = PTR_ERR(vsp1->hsi);
@@ -329,33 +339,6 @@ static int vsp1_device_init(struct vsp1_device *vsp1)
329 return 0; 339 return 0;
330} 340}
331 341
332static int vsp1_clocks_enable(struct vsp1_device *vsp1)
333{
334 int ret;
335
336 ret = clk_prepare_enable(vsp1->clock);
337 if (ret < 0)
338 return ret;
339
340 if (IS_ERR(vsp1->rt_clock))
341 return 0;
342
343 ret = clk_prepare_enable(vsp1->rt_clock);
344 if (ret < 0) {
345 clk_disable_unprepare(vsp1->clock);
346 return ret;
347 }
348
349 return 0;
350}
351
352static void vsp1_clocks_disable(struct vsp1_device *vsp1)
353{
354 if (!IS_ERR(vsp1->rt_clock))
355 clk_disable_unprepare(vsp1->rt_clock);
356 clk_disable_unprepare(vsp1->clock);
357}
358
359/* 342/*
360 * vsp1_device_get - Acquire the VSP1 device 343 * vsp1_device_get - Acquire the VSP1 device
361 * 344 *
@@ -373,7 +356,7 @@ struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
373 if (vsp1->ref_count > 0) 356 if (vsp1->ref_count > 0)
374 goto done; 357 goto done;
375 358
376 ret = vsp1_clocks_enable(vsp1); 359 ret = clk_prepare_enable(vsp1->clock);
377 if (ret < 0) { 360 if (ret < 0) {
378 __vsp1 = NULL; 361 __vsp1 = NULL;
379 goto done; 362 goto done;
@@ -381,7 +364,7 @@ struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
381 364
382 ret = vsp1_device_init(vsp1); 365 ret = vsp1_device_init(vsp1);
383 if (ret < 0) { 366 if (ret < 0) {
384 vsp1_clocks_disable(vsp1); 367 clk_disable_unprepare(vsp1->clock);
385 __vsp1 = NULL; 368 __vsp1 = NULL;
386 goto done; 369 goto done;
387 } 370 }
@@ -405,7 +388,7 @@ void vsp1_device_put(struct vsp1_device *vsp1)
405 mutex_lock(&vsp1->lock); 388 mutex_lock(&vsp1->lock);
406 389
407 if (--vsp1->ref_count == 0) 390 if (--vsp1->ref_count == 0)
408 vsp1_clocks_disable(vsp1); 391 clk_disable_unprepare(vsp1->clock);
409 392
410 mutex_unlock(&vsp1->lock); 393 mutex_unlock(&vsp1->lock);
411} 394}
@@ -424,7 +407,7 @@ static int vsp1_pm_suspend(struct device *dev)
424 if (vsp1->ref_count == 0) 407 if (vsp1->ref_count == 0)
425 return 0; 408 return 0;
426 409
427 vsp1_clocks_disable(vsp1); 410 clk_disable_unprepare(vsp1->clock);
428 return 0; 411 return 0;
429} 412}
430 413
@@ -437,7 +420,7 @@ static int vsp1_pm_resume(struct device *dev)
437 if (vsp1->ref_count) 420 if (vsp1->ref_count)
438 return 0; 421 return 0;
439 422
440 return vsp1_clocks_enable(vsp1); 423 return clk_prepare_enable(vsp1->clock);
441} 424}
442#endif 425#endif
443 426
@@ -449,34 +432,59 @@ static const struct dev_pm_ops vsp1_pm_ops = {
449 * Platform Driver 432 * Platform Driver
450 */ 433 */
451 434
452static struct vsp1_platform_data * 435static int vsp1_validate_platform_data(struct platform_device *pdev,
453vsp1_get_platform_data(struct platform_device *pdev) 436 struct vsp1_platform_data *pdata)
454{ 437{
455 struct vsp1_platform_data *pdata = pdev->dev.platform_data;
456
457 if (pdata == NULL) { 438 if (pdata == NULL) {
458 dev_err(&pdev->dev, "missing platform data\n"); 439 dev_err(&pdev->dev, "missing platform data\n");
459 return NULL; 440 return -EINVAL;
460 } 441 }
461 442
462 if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) { 443 if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
463 dev_err(&pdev->dev, "invalid number of RPF (%u)\n", 444 dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
464 pdata->rpf_count); 445 pdata->rpf_count);
465 return NULL; 446 return -EINVAL;
466 } 447 }
467 448
468 if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) { 449 if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
469 dev_err(&pdev->dev, "invalid number of UDS (%u)\n", 450 dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
470 pdata->uds_count); 451 pdata->uds_count);
471 return NULL; 452 return -EINVAL;
472 } 453 }
473 454
474 if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) { 455 if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
475 dev_err(&pdev->dev, "invalid number of WPF (%u)\n", 456 dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
476 pdata->wpf_count); 457 pdata->wpf_count);
477 return NULL; 458 return -EINVAL;
478 } 459 }
479 460
461 return 0;
462}
463
464static struct vsp1_platform_data *
465vsp1_get_platform_data(struct platform_device *pdev)
466{
467 struct device_node *np = pdev->dev.of_node;
468 struct vsp1_platform_data *pdata;
469
470 if (!IS_ENABLED(CONFIG_OF) || np == NULL)
471 return pdev->dev.platform_data;
472
473 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
474 if (pdata == NULL)
475 return NULL;
476
477 if (of_property_read_bool(np, "renesas,has-lif"))
478 pdata->features |= VSP1_HAS_LIF;
479 if (of_property_read_bool(np, "renesas,has-lut"))
480 pdata->features |= VSP1_HAS_LUT;
481 if (of_property_read_bool(np, "renesas,has-sru"))
482 pdata->features |= VSP1_HAS_SRU;
483
484 of_property_read_u32(np, "renesas,#rpf", &pdata->rpf_count);
485 of_property_read_u32(np, "renesas,#uds", &pdata->uds_count);
486 of_property_read_u32(np, "renesas,#wpf", &pdata->wpf_count);
487
480 return pdata; 488 return pdata;
481} 489}
482 490
@@ -499,6 +507,10 @@ static int vsp1_probe(struct platform_device *pdev)
499 if (vsp1->pdata == NULL) 507 if (vsp1->pdata == NULL)
500 return -ENODEV; 508 return -ENODEV;
501 509
510 ret = vsp1_validate_platform_data(pdev, vsp1->pdata);
511 if (ret < 0)
512 return ret;
513
502 /* I/O, IRQ and clock resources */ 514 /* I/O, IRQ and clock resources */
503 io = platform_get_resource(pdev, IORESOURCE_MEM, 0); 515 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
504 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io); 516 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
@@ -511,9 +523,6 @@ static int vsp1_probe(struct platform_device *pdev)
511 return PTR_ERR(vsp1->clock); 523 return PTR_ERR(vsp1->clock);
512 } 524 }
513 525
514 /* The RT clock is optional */
515 vsp1->rt_clock = devm_clk_get(&pdev->dev, "rt");
516
517 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 526 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
518 if (!irq) { 527 if (!irq) {
519 dev_err(&pdev->dev, "missing IRQ\n"); 528 dev_err(&pdev->dev, "missing IRQ\n");
@@ -548,6 +557,11 @@ static int vsp1_remove(struct platform_device *pdev)
548 return 0; 557 return 0;
549} 558}
550 559
560static const struct of_device_id vsp1_of_match[] = {
561 { .compatible = "renesas,vsp1" },
562 { },
563};
564
551static struct platform_driver vsp1_platform_driver = { 565static struct platform_driver vsp1_platform_driver = {
552 .probe = vsp1_probe, 566 .probe = vsp1_probe,
553 .remove = vsp1_remove, 567 .remove = vsp1_remove,
@@ -555,6 +569,7 @@ static struct platform_driver vsp1_platform_driver = {
555 .owner = THIS_MODULE, 569 .owner = THIS_MODULE,
556 .name = "vsp1", 570 .name = "vsp1",
557 .pm = &vsp1_pm_ops, 571 .pm = &vsp1_pm_ops,
572 .of_match_table = vsp1_of_match,
558 }, 573 },
559}; 574};
560 575
diff --git a/drivers/media/platform/vsp1/vsp1_entity.c b/drivers/media/platform/vsp1/vsp1_entity.c
index 3fc9e4266caf..44167834285d 100644
--- a/drivers/media/platform/vsp1/vsp1_entity.c
+++ b/drivers/media/platform/vsp1/vsp1_entity.c
@@ -100,8 +100,10 @@ static int vsp1_entity_link_setup(struct media_entity *entity,
100 if (source->sink) 100 if (source->sink)
101 return -EBUSY; 101 return -EBUSY;
102 source->sink = remote->entity; 102 source->sink = remote->entity;
103 source->sink_pad = remote->index;
103 } else { 104 } else {
104 source->sink = NULL; 105 source->sink = NULL;
106 source->sink_pad = 0;
105 } 107 }
106 108
107 return 0; 109 return 0;
@@ -116,42 +118,43 @@ const struct media_entity_operations vsp1_media_ops = {
116 * Initialization 118 * Initialization
117 */ 119 */
118 120
121static const struct vsp1_route vsp1_routes[] = {
122 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
123 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
124 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3), } },
125 { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } },
126 { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } },
127 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } },
128 { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } },
129 { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { VI6_DPR_NODE_RPF(0), } },
130 { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { VI6_DPR_NODE_RPF(1), } },
131 { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { VI6_DPR_NODE_RPF(2), } },
132 { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { VI6_DPR_NODE_RPF(3), } },
133 { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { VI6_DPR_NODE_RPF(4), } },
134 { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } },
135 { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } },
136 { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } },
137 { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } },
138 { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } },
139 { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } },
140 { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } },
141 { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } },
142};
143
119int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity, 144int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
120 unsigned int num_pads) 145 unsigned int num_pads)
121{ 146{
122 static const struct {
123 unsigned int id;
124 unsigned int reg;
125 } routes[] = {
126 { VI6_DPR_NODE_HSI, VI6_DPR_HSI_ROUTE },
127 { VI6_DPR_NODE_HST, VI6_DPR_HST_ROUTE },
128 { VI6_DPR_NODE_LIF, 0 },
129 { VI6_DPR_NODE_LUT, VI6_DPR_LUT_ROUTE },
130 { VI6_DPR_NODE_RPF(0), VI6_DPR_RPF_ROUTE(0) },
131 { VI6_DPR_NODE_RPF(1), VI6_DPR_RPF_ROUTE(1) },
132 { VI6_DPR_NODE_RPF(2), VI6_DPR_RPF_ROUTE(2) },
133 { VI6_DPR_NODE_RPF(3), VI6_DPR_RPF_ROUTE(3) },
134 { VI6_DPR_NODE_RPF(4), VI6_DPR_RPF_ROUTE(4) },
135 { VI6_DPR_NODE_SRU, VI6_DPR_SRU_ROUTE },
136 { VI6_DPR_NODE_UDS(0), VI6_DPR_UDS_ROUTE(0) },
137 { VI6_DPR_NODE_UDS(1), VI6_DPR_UDS_ROUTE(1) },
138 { VI6_DPR_NODE_UDS(2), VI6_DPR_UDS_ROUTE(2) },
139 { VI6_DPR_NODE_WPF(0), 0 },
140 { VI6_DPR_NODE_WPF(1), 0 },
141 { VI6_DPR_NODE_WPF(2), 0 },
142 { VI6_DPR_NODE_WPF(3), 0 },
143 };
144
145 unsigned int i; 147 unsigned int i;
146 148
147 for (i = 0; i < ARRAY_SIZE(routes); ++i) { 149 for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
148 if (routes[i].id == entity->id) { 150 if (vsp1_routes[i].type == entity->type &&
149 entity->route = routes[i].reg; 151 vsp1_routes[i].index == entity->index) {
152 entity->route = &vsp1_routes[i];
150 break; 153 break;
151 } 154 }
152 } 155 }
153 156
154 if (i == ARRAY_SIZE(routes)) 157 if (i == ARRAY_SIZE(vsp1_routes))
155 return -EINVAL; 158 return -EINVAL;
156 159
157 entity->vsp1 = vsp1; 160 entity->vsp1 = vsp1;
diff --git a/drivers/media/platform/vsp1/vsp1_entity.h b/drivers/media/platform/vsp1/vsp1_entity.h
index f6fd6988aeb0..7afbd8a7ba66 100644
--- a/drivers/media/platform/vsp1/vsp1_entity.h
+++ b/drivers/media/platform/vsp1/vsp1_entity.h
@@ -20,6 +20,7 @@
20struct vsp1_device; 20struct vsp1_device;
21 21
22enum vsp1_entity_type { 22enum vsp1_entity_type {
23 VSP1_ENTITY_BRU,
23 VSP1_ENTITY_HSI, 24 VSP1_ENTITY_HSI,
24 VSP1_ENTITY_HST, 25 VSP1_ENTITY_HST,
25 VSP1_ENTITY_LIF, 26 VSP1_ENTITY_LIF,
@@ -30,13 +31,31 @@ enum vsp1_entity_type {
30 VSP1_ENTITY_WPF, 31 VSP1_ENTITY_WPF,
31}; 32};
32 33
34/*
35 * struct vsp1_route - Entity routing configuration
36 * @type: Entity type this routing entry is associated with
37 * @index: Entity index this routing entry is associated with
38 * @reg: Output routing configuration register
39 * @inputs: Target node value for each input
40 *
41 * Each $vsp1_route entry describes routing configuration for the entity
42 * specified by the entry's @type and @index. @reg indicates the register that
43 * holds output routing configuration for the entity, and the @inputs array
44 * store the target node value for each input of the entity.
45 */
46struct vsp1_route {
47 enum vsp1_entity_type type;
48 unsigned int index;
49 unsigned int reg;
50 unsigned int inputs[4];
51};
52
33struct vsp1_entity { 53struct vsp1_entity {
34 struct vsp1_device *vsp1; 54 struct vsp1_device *vsp1;
35 55
36 enum vsp1_entity_type type; 56 enum vsp1_entity_type type;
37 unsigned int index; 57 unsigned int index;
38 unsigned int id; 58 const struct vsp1_route *route;
39 unsigned int route;
40 59
41 struct list_head list_dev; 60 struct list_head list_dev;
42 struct list_head list_pipe; 61 struct list_head list_pipe;
@@ -45,6 +64,7 @@ struct vsp1_entity {
45 unsigned int source_pad; 64 unsigned int source_pad;
46 65
47 struct media_entity *sink; 66 struct media_entity *sink;
67 unsigned int sink_pad;
48 68
49 struct v4l2_subdev subdev; 69 struct v4l2_subdev subdev;
50 struct v4l2_mbus_framefmt *formats; 70 struct v4l2_mbus_framefmt *formats;
diff --git a/drivers/media/platform/vsp1/vsp1_hsit.c b/drivers/media/platform/vsp1/vsp1_hsit.c
index 285485350d82..db2950a73c60 100644
--- a/drivers/media/platform/vsp1/vsp1_hsit.c
+++ b/drivers/media/platform/vsp1/vsp1_hsit.c
@@ -193,13 +193,10 @@ struct vsp1_hsit *vsp1_hsit_create(struct vsp1_device *vsp1, bool inverse)
193 193
194 hsit->inverse = inverse; 194 hsit->inverse = inverse;
195 195
196 if (inverse) { 196 if (inverse)
197 hsit->entity.type = VSP1_ENTITY_HSI; 197 hsit->entity.type = VSP1_ENTITY_HSI;
198 hsit->entity.id = VI6_DPR_NODE_HSI; 198 else
199 } else {
200 hsit->entity.type = VSP1_ENTITY_HST; 199 hsit->entity.type = VSP1_ENTITY_HST;
201 hsit->entity.id = VI6_DPR_NODE_HST;
202 }
203 200
204 ret = vsp1_entity_init(vsp1, &hsit->entity, 2); 201 ret = vsp1_entity_init(vsp1, &hsit->entity, 2);
205 if (ret < 0) 202 if (ret < 0)
diff --git a/drivers/media/platform/vsp1/vsp1_lif.c b/drivers/media/platform/vsp1/vsp1_lif.c
index 135a78957014..d4fb23e9c4a8 100644
--- a/drivers/media/platform/vsp1/vsp1_lif.c
+++ b/drivers/media/platform/vsp1/vsp1_lif.c
@@ -215,7 +215,6 @@ struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
215 return ERR_PTR(-ENOMEM); 215 return ERR_PTR(-ENOMEM);
216 216
217 lif->entity.type = VSP1_ENTITY_LIF; 217 lif->entity.type = VSP1_ENTITY_LIF;
218 lif->entity.id = VI6_DPR_NODE_LIF;
219 218
220 ret = vsp1_entity_init(vsp1, &lif->entity, 2); 219 ret = vsp1_entity_init(vsp1, &lif->entity, 2);
221 if (ret < 0) 220 if (ret < 0)
diff --git a/drivers/media/platform/vsp1/vsp1_lut.c b/drivers/media/platform/vsp1/vsp1_lut.c
index 4e9dc7c86ef8..fea36ebe2565 100644
--- a/drivers/media/platform/vsp1/vsp1_lut.c
+++ b/drivers/media/platform/vsp1/vsp1_lut.c
@@ -229,7 +229,6 @@ struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
229 return ERR_PTR(-ENOMEM); 229 return ERR_PTR(-ENOMEM);
230 230
231 lut->entity.type = VSP1_ENTITY_LUT; 231 lut->entity.type = VSP1_ENTITY_LUT;
232 lut->entity.id = VI6_DPR_NODE_LUT;
233 232
234 ret = vsp1_entity_init(vsp1, &lut->entity, 2); 233 ret = vsp1_entity_init(vsp1, &lut->entity, 2);
235 if (ret < 0) 234 if (ret < 0)
diff --git a/drivers/media/platform/vsp1/vsp1_regs.h b/drivers/media/platform/vsp1/vsp1_regs.h
index 28650806c20f..3e74b44286f6 100644
--- a/drivers/media/platform/vsp1/vsp1_regs.h
+++ b/drivers/media/platform/vsp1/vsp1_regs.h
@@ -451,13 +451,111 @@
451 * BRU Control Registers 451 * BRU Control Registers
452 */ 452 */
453 453
454#define VI6_ROP_NOP 0
455#define VI6_ROP_AND 1
456#define VI6_ROP_AND_REV 2
457#define VI6_ROP_COPY 3
458#define VI6_ROP_AND_INV 4
459#define VI6_ROP_CLEAR 5
460#define VI6_ROP_XOR 6
461#define VI6_ROP_OR 7
462#define VI6_ROP_NOR 8
463#define VI6_ROP_EQUIV 9
464#define VI6_ROP_INVERT 10
465#define VI6_ROP_OR_REV 11
466#define VI6_ROP_COPY_INV 12
467#define VI6_ROP_OR_INV 13
468#define VI6_ROP_NAND 14
469#define VI6_ROP_SET 15
470
454#define VI6_BRU_INCTRL 0x2c00 471#define VI6_BRU_INCTRL 0x2c00
472#define VI6_BRU_INCTRL_NRM (1 << 28)
473#define VI6_BRU_INCTRL_DnON (1 << (16 + (n)))
474#define VI6_BRU_INCTRL_DITHn_OFF (0 << ((n) * 4))
475#define VI6_BRU_INCTRL_DITHn_18BPP (1 << ((n) * 4))
476#define VI6_BRU_INCTRL_DITHn_16BPP (2 << ((n) * 4))
477#define VI6_BRU_INCTRL_DITHn_15BPP (3 << ((n) * 4))
478#define VI6_BRU_INCTRL_DITHn_12BPP (4 << ((n) * 4))
479#define VI6_BRU_INCTRL_DITHn_8BPP (5 << ((n) * 4))
480#define VI6_BRU_INCTRL_DITHn_MASK (7 << ((n) * 4))
481#define VI6_BRU_INCTRL_DITHn_SHIFT ((n) * 4)
482
455#define VI6_BRU_VIRRPF_SIZE 0x2c04 483#define VI6_BRU_VIRRPF_SIZE 0x2c04
484#define VI6_BRU_VIRRPF_SIZE_HSIZE_MASK (0x1fff << 16)
485#define VI6_BRU_VIRRPF_SIZE_HSIZE_SHIFT 16
486#define VI6_BRU_VIRRPF_SIZE_VSIZE_MASK (0x1fff << 0)
487#define VI6_BRU_VIRRPF_SIZE_VSIZE_SHIFT 0
488
456#define VI6_BRU_VIRRPF_LOC 0x2c08 489#define VI6_BRU_VIRRPF_LOC 0x2c08
490#define VI6_BRU_VIRRPF_LOC_HCOORD_MASK (0x1fff << 16)
491#define VI6_BRU_VIRRPF_LOC_HCOORD_SHIFT 16
492#define VI6_BRU_VIRRPF_LOC_VCOORD_MASK (0x1fff << 0)
493#define VI6_BRU_VIRRPF_LOC_VCOORD_SHIFT 0
494
457#define VI6_BRU_VIRRPF_COL 0x2c0c 495#define VI6_BRU_VIRRPF_COL 0x2c0c
496#define VI6_BRU_VIRRPF_COL_A_MASK (0xff << 24)
497#define VI6_BRU_VIRRPF_COL_A_SHIFT 24
498#define VI6_BRU_VIRRPF_COL_RCR_MASK (0xff << 16)
499#define VI6_BRU_VIRRPF_COL_RCR_SHIFT 16
500#define VI6_BRU_VIRRPF_COL_GY_MASK (0xff << 8)
501#define VI6_BRU_VIRRPF_COL_GY_SHIFT 8
502#define VI6_BRU_VIRRPF_COL_BCB_MASK (0xff << 0)
503#define VI6_BRU_VIRRPF_COL_BCB_SHIFT 0
504
458#define VI6_BRU_CTRL(n) (0x2c10 + (n) * 8) 505#define VI6_BRU_CTRL(n) (0x2c10 + (n) * 8)
506#define VI6_BRU_CTRL_RBC (1 << 31)
507#define VI6_BRU_CTRL_DSTSEL_BRUIN(n) ((n) << 20)
508#define VI6_BRU_CTRL_DSTSEL_VRPF (4 << 20)
509#define VI6_BRU_CTRL_DSTSEL_MASK (7 << 20)
510#define VI6_BRU_CTRL_SRCSEL_BRUIN(n) ((n) << 16)
511#define VI6_BRU_CTRL_SRCSEL_VRPF (4 << 16)
512#define VI6_BRU_CTRL_SRCSEL_MASK (7 << 16)
513#define VI6_BRU_CTRL_CROP(rop) ((rop) << 4)
514#define VI6_BRU_CTRL_CROP_MASK (0xf << 4)
515#define VI6_BRU_CTRL_AROP(rop) ((rop) << 0)
516#define VI6_BRU_CTRL_AROP_MASK (0xf << 0)
517
459#define VI6_BRU_BLD(n) (0x2c14 + (n) * 8) 518#define VI6_BRU_BLD(n) (0x2c14 + (n) * 8)
519#define VI6_BRU_BLD_CBES (1 << 31)
520#define VI6_BRU_BLD_CCMDX_DST_A (0 << 28)
521#define VI6_BRU_BLD_CCMDX_255_DST_A (1 << 28)
522#define VI6_BRU_BLD_CCMDX_SRC_A (2 << 28)
523#define VI6_BRU_BLD_CCMDX_255_SRC_A (3 << 28)
524#define VI6_BRU_BLD_CCMDX_COEFX (4 << 28)
525#define VI6_BRU_BLD_CCMDX_MASK (7 << 28)
526#define VI6_BRU_BLD_CCMDY_DST_A (0 << 24)
527#define VI6_BRU_BLD_CCMDY_255_DST_A (1 << 24)
528#define VI6_BRU_BLD_CCMDY_SRC_A (2 << 24)
529#define VI6_BRU_BLD_CCMDY_255_SRC_A (3 << 24)
530#define VI6_BRU_BLD_CCMDY_COEFY (4 << 24)
531#define VI6_BRU_BLD_CCMDY_MASK (7 << 24)
532#define VI6_BRU_BLD_CCMDY_SHIFT 24
533#define VI6_BRU_BLD_ABES (1 << 23)
534#define VI6_BRU_BLD_ACMDX_DST_A (0 << 20)
535#define VI6_BRU_BLD_ACMDX_255_DST_A (1 << 20)
536#define VI6_BRU_BLD_ACMDX_SRC_A (2 << 20)
537#define VI6_BRU_BLD_ACMDX_255_SRC_A (3 << 20)
538#define VI6_BRU_BLD_ACMDX_COEFX (4 << 20)
539#define VI6_BRU_BLD_ACMDX_MASK (7 << 20)
540#define VI6_BRU_BLD_ACMDY_DST_A (0 << 16)
541#define VI6_BRU_BLD_ACMDY_255_DST_A (1 << 16)
542#define VI6_BRU_BLD_ACMDY_SRC_A (2 << 16)
543#define VI6_BRU_BLD_ACMDY_255_SRC_A (3 << 16)
544#define VI6_BRU_BLD_ACMDY_COEFY (4 << 16)
545#define VI6_BRU_BLD_ACMDY_MASK (7 << 16)
546#define VI6_BRU_BLD_COEFX_MASK (0xff << 8)
547#define VI6_BRU_BLD_COEFX_SHIFT 8
548#define VI6_BRU_BLD_COEFY_MASK (0xff << 0)
549#define VI6_BRU_BLD_COEFY_SHIFT 0
550
460#define VI6_BRU_ROP 0x2c30 551#define VI6_BRU_ROP 0x2c30
552#define VI6_BRU_ROP_DSTSEL_BRUIN(n) ((n) << 20)
553#define VI6_BRU_ROP_DSTSEL_VRPF (4 << 20)
554#define VI6_BRU_ROP_DSTSEL_MASK (7 << 20)
555#define VI6_BRU_ROP_CROP(rop) ((rop) << 4)
556#define VI6_BRU_ROP_CROP_MASK (0xf << 4)
557#define VI6_BRU_ROP_AROP(rop) ((rop) << 0)
558#define VI6_BRU_ROP_AROP_MASK (0xf << 0)
461 559
462/* ----------------------------------------------------------------------------- 560/* -----------------------------------------------------------------------------
463 * HGO Control Registers 561 * HGO Control Registers
diff --git a/drivers/media/platform/vsp1/vsp1_rpf.c b/drivers/media/platform/vsp1/vsp1_rpf.c
index 2b04d0f95c62..c3d98642a4aa 100644
--- a/drivers/media/platform/vsp1/vsp1_rpf.c
+++ b/drivers/media/platform/vsp1/vsp1_rpf.c
@@ -96,8 +96,10 @@ static int rpf_s_stream(struct v4l2_subdev *subdev, int enable)
96 vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt); 96 vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
97 vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap); 97 vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
98 98
99 /* Output location. Composing isn't supported yet. */ 99 /* Output location */
100 vsp1_rpf_write(rpf, VI6_RPF_LOC, 0); 100 vsp1_rpf_write(rpf, VI6_RPF_LOC,
101 (rpf->location.left << VI6_RPF_LOC_HCOORD_SHIFT) |
102 (rpf->location.top << VI6_RPF_LOC_VCOORD_SHIFT));
101 103
102 /* Disable alpha, mask and color key. Set the alpha channel to a fixed 104 /* Disable alpha, mask and color key. Set the alpha channel to a fixed
103 * value of 255. 105 * value of 255.
@@ -176,7 +178,6 @@ struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
176 178
177 rpf->entity.type = VSP1_ENTITY_RPF; 179 rpf->entity.type = VSP1_ENTITY_RPF;
178 rpf->entity.index = index; 180 rpf->entity.index = index;
179 rpf->entity.id = VI6_DPR_NODE_RPF(index);
180 181
181 ret = vsp1_entity_init(vsp1, &rpf->entity, 2); 182 ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
182 if (ret < 0) 183 if (ret < 0)
diff --git a/drivers/media/platform/vsp1/vsp1_rwpf.h b/drivers/media/platform/vsp1/vsp1_rwpf.h
index 5c5ee81bbeae..b4fb65e58770 100644
--- a/drivers/media/platform/vsp1/vsp1_rwpf.h
+++ b/drivers/media/platform/vsp1/vsp1_rwpf.h
@@ -30,6 +30,10 @@ struct vsp1_rwpf {
30 unsigned int max_width; 30 unsigned int max_width;
31 unsigned int max_height; 31 unsigned int max_height;
32 32
33 struct {
34 unsigned int left;
35 unsigned int top;
36 } location;
33 struct v4l2_rect crop; 37 struct v4l2_rect crop;
34 38
35 unsigned int offsets[2]; 39 unsigned int offsets[2];
diff --git a/drivers/media/platform/vsp1/vsp1_sru.c b/drivers/media/platform/vsp1/vsp1_sru.c
index 7ab1a0b2d656..aa0e04c56f3f 100644
--- a/drivers/media/platform/vsp1/vsp1_sru.c
+++ b/drivers/media/platform/vsp1/vsp1_sru.c
@@ -327,7 +327,6 @@ struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1)
327 return ERR_PTR(-ENOMEM); 327 return ERR_PTR(-ENOMEM);
328 328
329 sru->entity.type = VSP1_ENTITY_SRU; 329 sru->entity.type = VSP1_ENTITY_SRU;
330 sru->entity.id = VI6_DPR_NODE_SRU;
331 330
332 ret = vsp1_entity_init(vsp1, &sru->entity, 2); 331 ret = vsp1_entity_init(vsp1, &sru->entity, 2);
333 if (ret < 0) 332 if (ret < 0)
diff --git a/drivers/media/platform/vsp1/vsp1_uds.c b/drivers/media/platform/vsp1/vsp1_uds.c
index 622342ac7770..0293bdbb4401 100644
--- a/drivers/media/platform/vsp1/vsp1_uds.c
+++ b/drivers/media/platform/vsp1/vsp1_uds.c
@@ -131,7 +131,7 @@ static int uds_s_stream(struct v4l2_subdev *subdev, int enable)
131 return 0; 131 return 0;
132 132
133 /* Enable multi-tap scaling. */ 133 /* Enable multi-tap scaling. */
134 vsp1_uds_write(uds, VI6_UDS_CTRL, VI6_UDS_CTRL_BC); 134 vsp1_uds_write(uds, VI6_UDS_CTRL, VI6_UDS_CTRL_AON | VI6_UDS_CTRL_BC);
135 135
136 vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH, 136 vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH,
137 (uds_passband_width(uds->hscale) 137 (uds_passband_width(uds->hscale)
@@ -139,7 +139,6 @@ static int uds_s_stream(struct v4l2_subdev *subdev, int enable)
139 (uds_passband_width(uds->vscale) 139 (uds_passband_width(uds->vscale)
140 << VI6_UDS_PASS_BWIDTH_V_SHIFT)); 140 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
141 141
142
143 /* Set the scaling ratios and the output size. */ 142 /* Set the scaling ratios and the output size. */
144 format = &uds->entity.formats[UDS_PAD_SOURCE]; 143 format = &uds->entity.formats[UDS_PAD_SOURCE];
145 144
@@ -323,7 +322,6 @@ struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
323 322
324 uds->entity.type = VSP1_ENTITY_UDS; 323 uds->entity.type = VSP1_ENTITY_UDS;
325 uds->entity.index = index; 324 uds->entity.index = index;
326 uds->entity.id = VI6_DPR_NODE_UDS(index);
327 325
328 ret = vsp1_entity_init(vsp1, &uds->entity, 2); 326 ret = vsp1_entity_init(vsp1, &uds->entity, 2);
329 if (ret < 0) 327 if (ret < 0)
diff --git a/drivers/media/platform/vsp1/vsp1_video.c b/drivers/media/platform/vsp1/vsp1_video.c
index a0595c17700f..8a1253e51f04 100644
--- a/drivers/media/platform/vsp1/vsp1_video.c
+++ b/drivers/media/platform/vsp1/vsp1_video.c
@@ -28,6 +28,7 @@
28#include <media/videobuf2-dma-contig.h> 28#include <media/videobuf2-dma-contig.h>
29 29
30#include "vsp1.h" 30#include "vsp1.h"
31#include "vsp1_bru.h"
31#include "vsp1_entity.h" 32#include "vsp1_entity.h"
32#include "vsp1_rwpf.h" 33#include "vsp1_rwpf.h"
33#include "vsp1_video.h" 34#include "vsp1_video.h"
@@ -280,6 +281,9 @@ static int vsp1_pipeline_validate_branch(struct vsp1_rwpf *input,
280 struct media_pad *pad; 281 struct media_pad *pad;
281 bool uds_found = false; 282 bool uds_found = false;
282 283
284 input->location.left = 0;
285 input->location.top = 0;
286
283 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]); 287 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
284 288
285 while (1) { 289 while (1) {
@@ -292,6 +296,17 @@ static int vsp1_pipeline_validate_branch(struct vsp1_rwpf *input,
292 296
293 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity)); 297 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
294 298
299 /* A BRU is present in the pipeline, store the compose rectangle
300 * location in the input RPF for use when configuring the RPF.
301 */
302 if (entity->type == VSP1_ENTITY_BRU) {
303 struct vsp1_bru *bru = to_bru(&entity->subdev);
304 struct v4l2_rect *rect = &bru->compose[pad->index];
305
306 input->location.left = rect->left;
307 input->location.top = rect->top;
308 }
309
295 /* We've reached the WPF, we're done. */ 310 /* We've reached the WPF, we're done. */
296 if (entity->type == VSP1_ENTITY_WPF) 311 if (entity->type == VSP1_ENTITY_WPF)
297 break; 312 break;
@@ -363,6 +378,8 @@ static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe,
363 rwpf->video.pipe_index = 0; 378 rwpf->video.pipe_index = 0;
364 } else if (e->type == VSP1_ENTITY_LIF) { 379 } else if (e->type == VSP1_ENTITY_LIF) {
365 pipe->lif = e; 380 pipe->lif = e;
381 } else if (e->type == VSP1_ENTITY_BRU) {
382 pipe->bru = e;
366 } 383 }
367 } 384 }
368 385
@@ -392,6 +409,7 @@ error:
392 pipe->num_video = 0; 409 pipe->num_video = 0;
393 pipe->num_inputs = 0; 410 pipe->num_inputs = 0;
394 pipe->output = NULL; 411 pipe->output = NULL;
412 pipe->bru = NULL;
395 pipe->lif = NULL; 413 pipe->lif = NULL;
396 return ret; 414 return ret;
397} 415}
@@ -430,6 +448,7 @@ static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
430 pipe->num_video = 0; 448 pipe->num_video = 0;
431 pipe->num_inputs = 0; 449 pipe->num_inputs = 0;
432 pipe->output = NULL; 450 pipe->output = NULL;
451 pipe->bru = NULL;
433 pipe->lif = NULL; 452 pipe->lif = NULL;
434 } 453 }
435 454
@@ -461,7 +480,7 @@ static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
461 480
462 list_for_each_entry(entity, &pipe->entities, list_pipe) { 481 list_for_each_entry(entity, &pipe->entities, list_pipe) {
463 if (entity->route) 482 if (entity->route)
464 vsp1_write(entity->vsp1, entity->route, 483 vsp1_write(entity->vsp1, entity->route->reg,
465 VI6_DPR_NODE_UNUSED); 484 VI6_DPR_NODE_UNUSED);
466 485
467 v4l2_subdev_call(&entity->subdev, video, s_stream, 0); 486 v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
@@ -680,11 +699,12 @@ static void vsp1_entity_route_setup(struct vsp1_entity *source)
680{ 699{
681 struct vsp1_entity *sink; 700 struct vsp1_entity *sink;
682 701
683 if (source->route == 0) 702 if (source->route->reg == 0)
684 return; 703 return;
685 704
686 sink = container_of(source->sink, struct vsp1_entity, subdev.entity); 705 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
687 vsp1_write(source->vsp1, source->route, sink->id); 706 vsp1_write(source->vsp1, source->route->reg,
707 sink->route->inputs[source->sink_pad]);
688} 708}
689 709
690static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count) 710static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
diff --git a/drivers/media/platform/vsp1/vsp1_video.h b/drivers/media/platform/vsp1/vsp1_video.h
index 53e4b3745940..c04d48fa2999 100644
--- a/drivers/media/platform/vsp1/vsp1_video.h
+++ b/drivers/media/platform/vsp1/vsp1_video.h
@@ -75,6 +75,7 @@ struct vsp1_pipeline {
75 unsigned int num_inputs; 75 unsigned int num_inputs;
76 struct vsp1_rwpf *inputs[VPS1_MAX_RPF]; 76 struct vsp1_rwpf *inputs[VPS1_MAX_RPF];
77 struct vsp1_rwpf *output; 77 struct vsp1_rwpf *output;
78 struct vsp1_entity *bru;
78 struct vsp1_entity *lif; 79 struct vsp1_entity *lif;
79 80
80 struct list_head entities; 81 struct list_head entities;
diff --git a/drivers/media/platform/vsp1/vsp1_wpf.c b/drivers/media/platform/vsp1/vsp1_wpf.c
index 11a61c601da0..1294340dcb36 100644
--- a/drivers/media/platform/vsp1/vsp1_wpf.c
+++ b/drivers/media/platform/vsp1/vsp1_wpf.c
@@ -58,13 +58,21 @@ static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
58 return 0; 58 return 0;
59 } 59 }
60 60
61 /* Sources */ 61 /* Sources. If the pipeline has a single input configure it as the
62 * master layer. Otherwise configure all inputs as sub-layers and
63 * select the virtual RPF as the master layer.
64 */
62 for (i = 0; i < pipe->num_inputs; ++i) { 65 for (i = 0; i < pipe->num_inputs; ++i) {
63 struct vsp1_rwpf *input = pipe->inputs[i]; 66 struct vsp1_rwpf *input = pipe->inputs[i];
64 67
65 srcrpf |= VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index); 68 srcrpf |= pipe->num_inputs == 1
69 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
70 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
66 } 71 }
67 72
73 if (pipe->num_inputs > 1)
74 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
75
68 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf); 76 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
69 77
70 /* Destination stride. */ 78 /* Destination stride. */
@@ -181,7 +189,6 @@ struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
181 189
182 wpf->entity.type = VSP1_ENTITY_WPF; 190 wpf->entity.type = VSP1_ENTITY_WPF;
183 wpf->entity.index = index; 191 wpf->entity.index = index;
184 wpf->entity.id = VI6_DPR_NODE_WPF(index);
185 192
186 ret = vsp1_entity_init(vsp1, &wpf->entity, 2); 193 ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
187 if (ret < 0) 194 if (ret < 0)
diff --git a/drivers/media/rc/redrat3.c b/drivers/media/rc/redrat3.c
index 47cd373e2295..79abbc8d9600 100644
--- a/drivers/media/rc/redrat3.c
+++ b/drivers/media/rc/redrat3.c
@@ -60,28 +60,6 @@
60#define DRIVER_DESC "RedRat3 USB IR Transceiver Driver" 60#define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
61#define DRIVER_NAME "redrat3" 61#define DRIVER_NAME "redrat3"
62 62
63/* module parameters */
64#ifdef CONFIG_USB_DEBUG
65static int debug = 1;
66#else
67static int debug;
68#endif
69
70#define RR3_DEBUG_STANDARD 0x1
71#define RR3_DEBUG_FUNCTION_TRACE 0x2
72
73#define rr3_dbg(dev, fmt, ...) \
74 do { \
75 if (debug & RR3_DEBUG_STANDARD) \
76 dev_info(dev, fmt, ## __VA_ARGS__); \
77 } while (0)
78
79#define rr3_ftr(dev, fmt, ...) \
80 do { \
81 if (debug & RR3_DEBUG_FUNCTION_TRACE) \
82 dev_info(dev, fmt, ## __VA_ARGS__); \
83 } while (0)
84
85/* bulk data transfer types */ 63/* bulk data transfer types */
86#define RR3_ERROR 0x01 64#define RR3_ERROR 0x01
87#define RR3_MOD_SIGNAL_IN 0x20 65#define RR3_MOD_SIGNAL_IN 0x20
@@ -237,13 +215,11 @@ static void redrat3_issue_async(struct redrat3_dev *rr3)
237{ 215{
238 int res; 216 int res;
239 217
240 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
241
242 res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC); 218 res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
243 if (res) 219 if (res)
244 rr3_dbg(rr3->dev, "%s: receive request FAILED! " 220 dev_dbg(rr3->dev,
245 "(res %d, len %d)\n", __func__, res, 221 "%s: receive request FAILED! (res %d, len %d)\n",
246 rr3->read_urb->transfer_buffer_length); 222 __func__, res, rr3->read_urb->transfer_buffer_length);
247} 223}
248 224
249static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code) 225static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
@@ -359,7 +335,7 @@ static void redrat3_rx_timeout(unsigned long data)
359{ 335{
360 struct redrat3_dev *rr3 = (struct redrat3_dev *)data; 336 struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
361 337
362 rr3_dbg(rr3->dev, "calling ir_raw_event_reset\n"); 338 dev_dbg(rr3->dev, "calling ir_raw_event_reset\n");
363 ir_raw_event_reset(rr3->rc); 339 ir_raw_event_reset(rr3->rc);
364} 340}
365 341
@@ -377,8 +353,6 @@ static void redrat3_process_ir_data(struct redrat3_dev *rr3)
377 return; 353 return;
378 } 354 }
379 355
380 rr3_ftr(rr3->dev, "Entered %s\n", __func__);
381
382 dev = rr3->dev; 356 dev = rr3->dev;
383 357
384 /* Make sure we reset the IR kfifo after a bit of inactivity */ 358 /* Make sure we reset the IR kfifo after a bit of inactivity */
@@ -386,7 +360,7 @@ static void redrat3_process_ir_data(struct redrat3_dev *rr3)
386 mod_timer(&rr3->rx_timeout, jiffies + delay); 360 mod_timer(&rr3->rx_timeout, jiffies + delay);
387 361
388 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata); 362 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
389 rr3_dbg(dev, "Got mod_freq of %u\n", mod_freq); 363 dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
390 364
391 /* process each rr3 encoded byte into an int */ 365 /* process each rr3 encoded byte into an int */
392 sig_size = be16_to_cpu(rr3->irdata.sig_size); 366 sig_size = be16_to_cpu(rr3->irdata.sig_size);
@@ -408,7 +382,7 @@ static void redrat3_process_ir_data(struct redrat3_dev *rr3)
408 /* cap the value to IR_MAX_DURATION */ 382 /* cap the value to IR_MAX_DURATION */
409 rawir.duration &= IR_MAX_DURATION; 383 rawir.duration &= IR_MAX_DURATION;
410 384
411 rr3_dbg(dev, "storing %s with duration %d (i: %d)\n", 385 dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
412 rawir.pulse ? "pulse" : "space", rawir.duration, i); 386 rawir.pulse ? "pulse" : "space", rawir.duration, i);
413 ir_raw_event_store_with_filter(rr3->rc, &rawir); 387 ir_raw_event_store_with_filter(rr3->rc, &rawir);
414 } 388 }
@@ -421,12 +395,12 @@ static void redrat3_process_ir_data(struct redrat3_dev *rr3)
421 rawir.duration = US_TO_NS(2800); 395 rawir.duration = US_TO_NS(2800);
422 else 396 else
423 rawir.duration = trailer; 397 rawir.duration = trailer;
424 rr3_dbg(dev, "storing trailing space with duration %d\n", 398 dev_dbg(dev, "storing trailing space with duration %d\n",
425 rawir.duration); 399 rawir.duration);
426 ir_raw_event_store_with_filter(rr3->rc, &rawir); 400 ir_raw_event_store_with_filter(rr3->rc, &rawir);
427 } 401 }
428 402
429 rr3_dbg(dev, "calling ir_raw_event_handle\n"); 403 dev_dbg(dev, "calling ir_raw_event_handle\n");
430 ir_raw_event_handle(rr3->rc); 404 ir_raw_event_handle(rr3->rc);
431} 405}
432 406
@@ -464,8 +438,6 @@ static int redrat3_enable_detector(struct redrat3_dev *rr3)
464 struct device *dev = rr3->dev; 438 struct device *dev = rr3->dev;
465 u8 ret; 439 u8 ret;
466 440
467 rr3_ftr(dev, "Entering %s\n", __func__);
468
469 ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3); 441 ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
470 if (ret != 0) 442 if (ret != 0)
471 dev_dbg(dev, "%s: unexpected ret of %d\n", 443 dev_dbg(dev, "%s: unexpected ret of %d\n",
@@ -486,7 +458,6 @@ static int redrat3_enable_detector(struct redrat3_dev *rr3)
486static inline void redrat3_delete(struct redrat3_dev *rr3, 458static inline void redrat3_delete(struct redrat3_dev *rr3,
487 struct usb_device *udev) 459 struct usb_device *udev)
488{ 460{
489 rr3_ftr(rr3->dev, "%s cleaning up\n", __func__);
490 usb_kill_urb(rr3->read_urb); 461 usb_kill_urb(rr3->read_urb);
491 usb_kill_urb(rr3->flash_urb); 462 usb_kill_urb(rr3->flash_urb);
492 usb_free_urb(rr3->read_urb); 463 usb_free_urb(rr3->read_urb);
@@ -519,7 +490,7 @@ static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
519 else { 490 else {
520 timeout = redrat3_len_to_us(be32_to_cpup(tmp)); 491 timeout = redrat3_len_to_us(be32_to_cpup(tmp));
521 492
522 rr3_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000); 493 dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
523 } 494 }
524 495
525 kfree(tmp); 496 kfree(tmp);
@@ -535,8 +506,6 @@ static void redrat3_reset(struct redrat3_dev *rr3)
535 u8 *val; 506 u8 *val;
536 int len = sizeof(u8); 507 int len = sizeof(u8);
537 508
538 rr3_ftr(dev, "Entering %s\n", __func__);
539
540 rxpipe = usb_rcvctrlpipe(udev, 0); 509 rxpipe = usb_rcvctrlpipe(udev, 0);
541 txpipe = usb_sndctrlpipe(udev, 0); 510 txpipe = usb_sndctrlpipe(udev, 0);
542 511
@@ -550,19 +519,19 @@ static void redrat3_reset(struct redrat3_dev *rr3)
550 rc = usb_control_msg(udev, rxpipe, RR3_RESET, 519 rc = usb_control_msg(udev, rxpipe, RR3_RESET,
551 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 520 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
552 RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25); 521 RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
553 rr3_dbg(dev, "reset returned 0x%02x\n", rc); 522 dev_dbg(dev, "reset returned 0x%02x\n", rc);
554 523
555 *val = 5; 524 *val = 5;
556 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, 525 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
557 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 526 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
558 RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25); 527 RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
559 rr3_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc); 528 dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
560 529
561 *val = RR3_DRIVER_MAXLENS; 530 *val = RR3_DRIVER_MAXLENS;
562 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, 531 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
563 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 532 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
564 RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25); 533 RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
565 rr3_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc); 534 dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
566 535
567 kfree(val); 536 kfree(val);
568} 537}
@@ -572,8 +541,6 @@ static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
572 int rc = 0; 541 int rc = 0;
573 char *buffer; 542 char *buffer;
574 543
575 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
576
577 buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL); 544 buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
578 if (!buffer) { 545 if (!buffer) {
579 dev_err(rr3->dev, "Memory allocation failure\n"); 546 dev_err(rr3->dev, "Memory allocation failure\n");
@@ -591,7 +558,6 @@ static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
591 dev_err(rr3->dev, "Problem fetching firmware ID\n"); 558 dev_err(rr3->dev, "Problem fetching firmware ID\n");
592 559
593 kfree(buffer); 560 kfree(buffer);
594 rr3_ftr(rr3->dev, "Exiting %s\n", __func__);
595} 561}
596 562
597static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len) 563static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
@@ -599,8 +565,6 @@ static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
599 struct redrat3_header *header = rr3->bulk_in_buf; 565 struct redrat3_header *header = rr3->bulk_in_buf;
600 unsigned pktlen, pkttype; 566 unsigned pktlen, pkttype;
601 567
602 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
603
604 /* grab the Length and type of transfer */ 568 /* grab the Length and type of transfer */
605 pktlen = be16_to_cpu(header->length); 569 pktlen = be16_to_cpu(header->length);
606 pkttype = be16_to_cpu(header->transfer_type); 570 pkttype = be16_to_cpu(header->transfer_type);
@@ -622,12 +586,12 @@ static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
622 case RR3_MOD_SIGNAL_IN: 586 case RR3_MOD_SIGNAL_IN:
623 memcpy(&rr3->irdata, rr3->bulk_in_buf, len); 587 memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
624 rr3->bytes_read = len; 588 rr3->bytes_read = len;
625 rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", 589 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
626 rr3->bytes_read, pktlen); 590 rr3->bytes_read, pktlen);
627 break; 591 break;
628 592
629 default: 593 default:
630 rr3_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n", 594 dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
631 pkttype, len, pktlen); 595 pkttype, len, pktlen);
632 break; 596 break;
633 } 597 }
@@ -637,8 +601,6 @@ static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
637{ 601{
638 void *irdata = &rr3->irdata; 602 void *irdata = &rr3->irdata;
639 603
640 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
641
642 if (len + rr3->bytes_read > sizeof(rr3->irdata)) { 604 if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
643 dev_warn(rr3->dev, "too much data for packet\n"); 605 dev_warn(rr3->dev, "too much data for packet\n");
644 rr3->bytes_read = 0; 606 rr3->bytes_read = 0;
@@ -648,7 +610,7 @@ static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
648 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len); 610 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
649 611
650 rr3->bytes_read += len; 612 rr3->bytes_read += len;
651 rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read, 613 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
652 be16_to_cpu(rr3->irdata.header.length)); 614 be16_to_cpu(rr3->irdata.header.length));
653} 615}
654 616
@@ -659,8 +621,6 @@ static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
659 unsigned pkttype; 621 unsigned pkttype;
660 int ret = 0; 622 int ret = 0;
661 623
662 rr3_ftr(dev, "Entering %s\n", __func__);
663
664 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) { 624 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
665 redrat3_read_packet_start(rr3, len); 625 redrat3_read_packet_start(rr3, len);
666 } else if (rr3->bytes_read != 0) { 626 } else if (rr3->bytes_read != 0) {
@@ -681,7 +641,7 @@ static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
681 if (pkttype == RR3_MOD_SIGNAL_IN) 641 if (pkttype == RR3_MOD_SIGNAL_IN)
682 redrat3_process_ir_data(rr3); 642 redrat3_process_ir_data(rr3);
683 else 643 else
684 rr3_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n", 644 dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
685 pkttype); 645 pkttype);
686 646
687out: 647out:
@@ -705,8 +665,6 @@ static void redrat3_handle_async(struct urb *urb)
705 return; 665 return;
706 } 666 }
707 667
708 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
709
710 switch (urb->status) { 668 switch (urb->status) {
711 case 0: 669 case 0:
712 ret = redrat3_get_ir_data(rr3, urb->actual_length); 670 ret = redrat3_get_ir_data(rr3, urb->actual_length);
@@ -743,7 +701,7 @@ static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
743 struct redrat3_dev *rr3 = rcdev->priv; 701 struct redrat3_dev *rr3 = rcdev->priv;
744 struct device *dev = rr3->dev; 702 struct device *dev = rr3->dev;
745 703
746 rr3_dbg(dev, "Setting modulation frequency to %u", carrier); 704 dev_dbg(dev, "Setting modulation frequency to %u", carrier);
747 if (carrier == 0) 705 if (carrier == 0)
748 return -EINVAL; 706 return -EINVAL;
749 707
@@ -764,8 +722,6 @@ static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
764 u8 curlencheck = 0; 722 u8 curlencheck = 0;
765 unsigned i, sendbuf_len; 723 unsigned i, sendbuf_len;
766 724
767 rr3_ftr(dev, "Entering %s\n", __func__);
768
769 if (rr3->transmitting) { 725 if (rr3->transmitting) {
770 dev_warn(dev, "%s: transmitter already in use\n", __func__); 726 dev_warn(dev, "%s: transmitter already in use\n", __func__);
771 return -EAGAIN; 727 return -EAGAIN;
@@ -801,7 +757,7 @@ static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
801 break; 757 break;
802 } 758 }
803 if (lencheck == curlencheck) { 759 if (lencheck == curlencheck) {
804 rr3_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n", 760 dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
805 i, txbuf[i], curlencheck, cur_sample_len); 761 i, txbuf[i], curlencheck, cur_sample_len);
806 if (curlencheck < RR3_DRIVER_MAXLENS) { 762 if (curlencheck < RR3_DRIVER_MAXLENS) {
807 /* now convert the value to a proper 763 /* now convert the value to a proper
@@ -835,7 +791,7 @@ static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
835 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress); 791 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
836 ret = usb_bulk_msg(rr3->udev, pipe, irdata, 792 ret = usb_bulk_msg(rr3->udev, pipe, irdata,
837 sendbuf_len, &ret_len, 10 * HZ); 793 sendbuf_len, &ret_len, 10 * HZ);
838 rr3_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret); 794 dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
839 795
840 /* now tell the hardware to transmit what we sent it */ 796 /* now tell the hardware to transmit what we sent it */
841 pipe = usb_rcvctrlpipe(rr3->udev, 0); 797 pipe = usb_rcvctrlpipe(rr3->udev, 0);
@@ -957,8 +913,6 @@ static int redrat3_dev_probe(struct usb_interface *intf,
957 int pipe, i; 913 int pipe, i;
958 int retval = -ENOMEM; 914 int retval = -ENOMEM;
959 915
960 rr3_ftr(dev, "%s called\n", __func__);
961
962 uhi = intf->cur_altsetting; 916 uhi = intf->cur_altsetting;
963 917
964 /* find our bulk-in and bulk-out endpoints */ 918 /* find our bulk-in and bulk-out endpoints */
@@ -971,7 +925,7 @@ static int redrat3_dev_probe(struct usb_interface *intf,
971 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) && 925 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
972 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == 926 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
973 USB_ENDPOINT_XFER_BULK)) { 927 USB_ENDPOINT_XFER_BULK)) {
974 rr3_dbg(dev, "found bulk-in endpoint at 0x%02x\n", 928 dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
975 ep->bEndpointAddress); 929 ep->bEndpointAddress);
976 /* data comes in on 0x82, 0x81 is for other data... */ 930 /* data comes in on 0x82, 0x81 is for other data... */
977 if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR) 931 if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
@@ -982,7 +936,7 @@ static int redrat3_dev_probe(struct usb_interface *intf,
982 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) && 936 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
983 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == 937 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
984 USB_ENDPOINT_XFER_BULK)) { 938 USB_ENDPOINT_XFER_BULK)) {
985 rr3_dbg(dev, "found bulk-out endpoint at 0x%02x\n", 939 dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
986 ep->bEndpointAddress); 940 ep->bEndpointAddress);
987 ep_out = ep; 941 ep_out = ep;
988 } 942 }
@@ -1074,7 +1028,6 @@ static int redrat3_dev_probe(struct usb_interface *intf,
1074 /* we can register the device now, as it is ready */ 1028 /* we can register the device now, as it is ready */
1075 usb_set_intfdata(intf, rr3); 1029 usb_set_intfdata(intf, rr3);
1076 1030
1077 rr3_ftr(dev, "Exiting %s\n", __func__);
1078 return 0; 1031 return 0;
1079 1032
1080led_free_error: 1033led_free_error:
@@ -1093,8 +1046,6 @@ static void redrat3_dev_disconnect(struct usb_interface *intf)
1093 struct usb_device *udev = interface_to_usbdev(intf); 1046 struct usb_device *udev = interface_to_usbdev(intf);
1094 struct redrat3_dev *rr3 = usb_get_intfdata(intf); 1047 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1095 1048
1096 rr3_ftr(&intf->dev, "Entering %s\n", __func__);
1097
1098 if (!rr3) 1049 if (!rr3)
1099 return; 1050 return;
1100 1051
@@ -1103,14 +1054,12 @@ static void redrat3_dev_disconnect(struct usb_interface *intf)
1103 led_classdev_unregister(&rr3->led); 1054 led_classdev_unregister(&rr3->led);
1104 del_timer_sync(&rr3->rx_timeout); 1055 del_timer_sync(&rr3->rx_timeout);
1105 redrat3_delete(rr3, udev); 1056 redrat3_delete(rr3, udev);
1106
1107 rr3_ftr(&intf->dev, "RedRat3 IR Transceiver now disconnected\n");
1108} 1057}
1109 1058
1110static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message) 1059static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1111{ 1060{
1112 struct redrat3_dev *rr3 = usb_get_intfdata(intf); 1061 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1113 rr3_ftr(rr3->dev, "suspend\n"); 1062
1114 led_classdev_suspend(&rr3->led); 1063 led_classdev_suspend(&rr3->led);
1115 usb_kill_urb(rr3->read_urb); 1064 usb_kill_urb(rr3->read_urb);
1116 usb_kill_urb(rr3->flash_urb); 1065 usb_kill_urb(rr3->flash_urb);
@@ -1120,7 +1069,7 @@ static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1120static int redrat3_dev_resume(struct usb_interface *intf) 1069static int redrat3_dev_resume(struct usb_interface *intf)
1121{ 1070{
1122 struct redrat3_dev *rr3 = usb_get_intfdata(intf); 1071 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1123 rr3_ftr(rr3->dev, "resume\n"); 1072
1124 if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC)) 1073 if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
1125 return -EIO; 1074 return -EIO;
1126 led_classdev_resume(&rr3->led); 1075 led_classdev_resume(&rr3->led);
@@ -1144,8 +1093,3 @@ MODULE_AUTHOR(DRIVER_AUTHOR);
1144MODULE_AUTHOR(DRIVER_AUTHOR2); 1093MODULE_AUTHOR(DRIVER_AUTHOR2);
1145MODULE_LICENSE("GPL"); 1094MODULE_LICENSE("GPL");
1146MODULE_DEVICE_TABLE(usb, redrat3_dev_table); 1095MODULE_DEVICE_TABLE(usb, redrat3_dev_table);
1147
1148module_param(debug, int, S_IRUGO | S_IWUSR);
1149MODULE_PARM_DESC(debug, "Enable module debug spew. 0 = no debugging (default) "
1150 "0x1 = standard debug messages, 0x2 = function tracing debug. "
1151 "Flag bits are addative (i.e., 0x3 for both debug types).");
diff --git a/drivers/media/rc/streamzap.c b/drivers/media/rc/streamzap.c
index f4e0bc3d382c..bd5e4ff9e0ba 100644
--- a/drivers/media/rc/streamzap.c
+++ b/drivers/media/rc/streamzap.c
@@ -42,12 +42,6 @@
42#define DRIVER_NAME "streamzap" 42#define DRIVER_NAME "streamzap"
43#define DRIVER_DESC "Streamzap Remote Control driver" 43#define DRIVER_DESC "Streamzap Remote Control driver"
44 44
45#ifdef CONFIG_USB_DEBUG
46static bool debug = 1;
47#else
48static bool debug;
49#endif
50
51#define USB_STREAMZAP_VENDOR_ID 0x0e9c 45#define USB_STREAMZAP_VENDOR_ID 0x0e9c
52#define USB_STREAMZAP_PRODUCT_ID 0x0000 46#define USB_STREAMZAP_PRODUCT_ID 0x0000
53 47
@@ -528,6 +522,3 @@ module_usb_driver(streamzap_driver);
528MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>"); 522MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
529MODULE_DESCRIPTION(DRIVER_DESC); 523MODULE_DESCRIPTION(DRIVER_DESC);
530MODULE_LICENSE("GPL"); 524MODULE_LICENSE("GPL");
531
532module_param(debug, bool, S_IRUGO | S_IWUSR);
533MODULE_PARM_DESC(debug, "Enable debugging messages");
diff --git a/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c b/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c
index de02db802ace..e35580618936 100644
--- a/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c
+++ b/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c
@@ -399,7 +399,7 @@ static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
399 399
400 /* clear 'streaming' status bit */ 400 /* clear 'streaming' status bit */
401 clear_bit(ADAP_STREAMING, &adap->state_bits); 401 clear_bit(ADAP_STREAMING, &adap->state_bits);
402 smp_mb__after_clear_bit(); 402 smp_mb__after_atomic();
403 wake_up_bit(&adap->state_bits, ADAP_STREAMING); 403 wake_up_bit(&adap->state_bits, ADAP_STREAMING);
404skip_feed_stop: 404skip_feed_stop:
405 405
@@ -550,7 +550,7 @@ static int dvb_usb_fe_init(struct dvb_frontend *fe)
550err: 550err:
551 if (!adap->suspend_resume_active) { 551 if (!adap->suspend_resume_active) {
552 clear_bit(ADAP_INIT, &adap->state_bits); 552 clear_bit(ADAP_INIT, &adap->state_bits);
553 smp_mb__after_clear_bit(); 553 smp_mb__after_atomic();
554 wake_up_bit(&adap->state_bits, ADAP_INIT); 554 wake_up_bit(&adap->state_bits, ADAP_INIT);
555 } 555 }
556 556
@@ -591,7 +591,7 @@ err:
591 if (!adap->suspend_resume_active) { 591 if (!adap->suspend_resume_active) {
592 adap->active_fe = -1; 592 adap->active_fe = -1;
593 clear_bit(ADAP_SLEEP, &adap->state_bits); 593 clear_bit(ADAP_SLEEP, &adap->state_bits);
594 smp_mb__after_clear_bit(); 594 smp_mb__after_atomic();
595 wake_up_bit(&adap->state_bits, ADAP_SLEEP); 595 wake_up_bit(&adap->state_bits, ADAP_SLEEP);
596 } 596 }
597 597
diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c
index 349e659d75fb..7c4489c42365 100644
--- a/drivers/media/v4l2-core/videobuf2-core.c
+++ b/drivers/media/v4l2-core/videobuf2-core.c
@@ -1200,6 +1200,30 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
1200EXPORT_SYMBOL_GPL(vb2_buffer_done); 1200EXPORT_SYMBOL_GPL(vb2_buffer_done);
1201 1201
1202/** 1202/**
1203 * vb2_discard_done() - discard all buffers marked as DONE
1204 * @q: videobuf2 queue
1205 *
1206 * This function is intended to be used with suspend/resume operations. It
1207 * discards all 'done' buffers as they would be too old to be requested after
1208 * resume.
1209 *
1210 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1211 * delayed works before calling this function to make sure no buffer will be
1212 * touched by the driver and/or hardware.
1213 */
1214void vb2_discard_done(struct vb2_queue *q)
1215{
1216 struct vb2_buffer *vb;
1217 unsigned long flags;
1218
1219 spin_lock_irqsave(&q->done_lock, flags);
1220 list_for_each_entry(vb, &q->done_list, done_entry)
1221 vb->state = VB2_BUF_STATE_ERROR;
1222 spin_unlock_irqrestore(&q->done_lock, flags);
1223}
1224EXPORT_SYMBOL_GPL(vb2_discard_done);
1225
1226/**
1203 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a 1227 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
1204 * v4l2_buffer by the userspace. The caller has already verified that struct 1228 * v4l2_buffer by the userspace. The caller has already verified that struct
1205 * v4l2_buffer has a valid number of planes. 1229 * v4l2_buffer has a valid number of planes.