ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / hdmi / rockchip-hdmiv1 / rockchip_hdmiv1_hdcp.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/string.h>
5 #include <linux/miscdevice.h>
6 #include <linux/workqueue.h>
7 #include <linux/firmware.h>
8 #include <linux/timer.h>
9 #include <linux/jiffies.h>
10 #include "rockchip_hdmiv1.h"
11 #include "rockchip_hdmiv1_hdcp.h"
12 #include "rockchip_hdmiv1_hw.h"
13
14 static struct hdcp *hdcp;
15
16 static void hdcp_work_queue(struct work_struct *work);
17
18 #define AUTH_TIMEOUT (2 * HZ)
19 static struct timer_list auth_timer;
20 static int timer_state;
21
22 static int is_1b_03_test(struct hdmi_dev *hdmi_dev)
23 {
24         int reg_value;
25         int reg_val_1;
26
27         hdmi_readl(hdmi_dev, 0x58, &reg_value);
28         hdmi_readl(hdmi_dev, 0xc3, &reg_val_1);
29
30         if (reg_value != 0) {
31                 if ((reg_val_1 & 0x40) == 0)
32                         return 1;
33         }
34         return 0;
35 }
36
37 static void rockchip_hdmiv1_set_colorbar(struct hdmi_dev *hdmi_dev,
38                                          int enable)
39 {
40         static int display_mask;
41         int reg_value;
42         int tmds_clk;
43
44         tmds_clk = hdmi_dev->tmdsclk;
45         if (enable) {
46                 if (!display_mask) {
47                         if (tmds_clk <= (HDMI_SYS_FREG_CLK << 2)) {
48                                 hdmi_readl(hdmi_dev, SYS_CTRL, &reg_value);
49                                 hdmi_msk_reg(hdmi_dev, SYS_CTRL,
50                                              m_REG_CLK_SOURCE,
51                                              v_REG_CLK_SOURCE_SYS);
52                                 hdmi_writel(hdmi_dev, HDMI_COLORBAR, 0x00);
53                                 hdmi_writel(hdmi_dev, SYS_CTRL, reg_value);
54                         } else {
55                                 hdmi_writel(hdmi_dev, HDMI_COLORBAR, 0x00);
56                         }
57                         display_mask = 1;
58                 }
59         } else {
60                 if (display_mask) {
61                         if (tmds_clk <= (HDMI_SYS_FREG_CLK << 2)) {
62                                 hdmi_readl(hdmi_dev, SYS_CTRL, &reg_value);
63                                 hdmi_msk_reg(hdmi_dev, SYS_CTRL,
64                                              m_REG_CLK_SOURCE,
65                                              v_REG_CLK_SOURCE_SYS);
66                                 hdmi_writel(hdmi_dev, HDMI_COLORBAR, 0x10);
67                                 hdmi_writel(hdmi_dev, SYS_CTRL, reg_value);
68                         } else {
69                                 hdmi_writel(hdmi_dev, HDMI_COLORBAR, 0x10);
70                         }
71                         display_mask = 0;
72                 }
73         }
74 }
75
76 static void rockchip_hdmiv1_hdcp_disable(struct hdmi_dev *hdmi_dev)
77 {
78         int reg_value;
79         int tmds_clk;
80
81         tmds_clk = hdmi_dev->tmdsclk;
82         if (tmds_clk <= (HDMI_SYS_FREG_CLK << 2)) {
83                 hdmi_readl(hdmi_dev, SYS_CTRL, &reg_value);
84                 hdmi_msk_reg(hdmi_dev, SYS_CTRL,
85                              m_REG_CLK_SOURCE, v_REG_CLK_SOURCE_SYS);
86         }
87
88         /* Disable HDCP Interrupt */
89         hdmi_writel(hdmi_dev, HDCP_INT_MASK1, 0x00);
90         /* Stop and Reset HDCP*/
91         hdmi_msk_reg(hdmi_dev, HDCP_CTRL1,
92                      m_AUTH_START | m_AUTH_STOP | m_HDCP_RESET,
93                      v_AUTH_START(0) | v_AUTH_STOP(1) | v_HDCP_RESET(1));
94
95         if (tmds_clk <= (HDMI_SYS_FREG_CLK << 2))
96                 hdmi_writel(hdmi_dev, SYS_CTRL, reg_value);
97 }
98
99 static int rockchip_hdmiv1_hdcp_key_check(struct hdcp_keys *key)
100 {
101         int i = 0;
102
103         HDMIDBG(3, "HDCP: check hdcp key\n");
104         /*check 40 private key */
105         for (i = 0; i < HDCP_PRIVATE_KEY_SIZE; i++) {
106                 if (key->devicekey[i] != 0x00)
107                         return HDCP_KEY_VALID;
108         }
109         /*check aksv*/
110         for (i = 0; i < 5; i++) {
111                 if (key->ksv[i] != 0x00)
112                         return HDCP_KEY_VALID;
113         }
114
115         return HDCP_KEY_INVALID;
116 }
117
118 static int rockchip_hdmiv1_hdcp_load_key2mem(void)
119 {
120         int i;
121         struct hdmi_dev *hdmi_dev;
122         struct hdcp_keys *key;
123
124         if (!hdcp)
125                 return -1;
126         hdmi_dev = hdcp->hdmi_dev;
127         key = hdcp->keys;
128         HDMIDBG(3, "HDCP: rockchip_hdmiv1_hdcp_load_key2mem start\n");
129         /* Write 40 private key*/
130         for (i = 0; i < HDCP_PRIVATE_KEY_SIZE; i++)
131                 hdmi_writel(hdmi_dev, HDCP_KEY_FIFO, key->devicekey[i]);
132         /* Write 1st aksv*/
133         for (i = 0; i < 5; i++)
134                 hdmi_writel(hdmi_dev, HDCP_KEY_FIFO, key->ksv[i]);
135         /* Write 2nd aksv*/
136         for (i = 0; i < 5; i++)
137                 hdmi_writel(hdmi_dev, HDCP_KEY_FIFO, key->ksv[i]);
138         HDMIDBG(3, "HDCP: rockchip_hdmiv1_hdcp_load_key2mem end\n");
139         return HDCP_OK;
140 }
141
142 static int rockchip_hdmiv1_hdcp_start_authentication(struct hdmi_dev *hdmi_dev)
143 {
144         int temp;
145         int retry = 0;
146         int tmds_clk;
147
148         tmds_clk = hdmi_dev->tmdsclk;
149         if (!hdcp->keys) {
150                 HDCP_WARN("HDCP: key is not loaded\n");
151                 return HDCP_KEY_ERR;
152         }
153         if (rockchip_hdmiv1_hdcp_key_check(hdcp->keys) == HDCP_KEY_INVALID) {
154                 HDCP_WARN("loaded HDCP key is incorrect\n");
155                 return HDCP_KEY_ERR;
156         }
157         if (tmds_clk > (HDMI_SYS_FREG_CLK << 2)) {
158                 /*Select TMDS CLK to configure regs*/
159                 hdmi_msk_reg(hdmi_dev, SYS_CTRL,
160                              m_REG_CLK_SOURCE, v_REG_CLK_SOURCE_TMDS);
161         } else {
162                 hdmi_msk_reg(hdmi_dev, SYS_CTRL,
163                              m_REG_CLK_SOURCE, v_REG_CLK_SOURCE_SYS);
164         }
165         hdmi_writel(hdmi_dev, HDCP_TIMER_100MS, 0x28);
166         hdmi_readl(hdmi_dev, HDCP_KEY_STATUS, &temp);
167         while ((temp & m_KEY_READY) == 0) {
168                 if (retry > 1000) {
169                         HDCP_WARN("HDCP: loaded key error\n");
170                         return HDCP_KEY_ERR;
171                 }
172                 rockchip_hdmiv1_hdcp_load_key2mem();
173                 usleep_range(900, 1000);
174                 hdmi_readl(hdmi_dev, HDCP_KEY_STATUS, &temp);
175                 retry++;
176         }
177         /*Config DDC bus clock: ddc_clk = reg_clk/4*(reg 0x4c 0x4b)*/
178         retry = hdmi_dev->hclk_rate / (HDCP_DDC_CLK << 2);
179         hdmi_writel(hdmi_dev, DDC_CLK_L, retry & 0xFF);
180         hdmi_writel(hdmi_dev, DDC_CLK_H, (retry >> 8) & 0xFF);
181         hdmi_writel(hdmi_dev, HDCP_CTRL2, 0x67);
182         /*Enable interrupt*/
183         hdmi_writel(hdmi_dev, HDCP_INT_MASK1,
184                     m_INT_HDCP_ERR | m_INT_BKSV_READY | m_INT_BKSV_UPDATE |
185                     m_INT_AUTH_SUCCESS | m_INT_AUTH_READY);
186         hdmi_writel(hdmi_dev, HDCP_INT_MASK2, 0x00);
187         /*Start authentication*/
188         hdmi_msk_reg(hdmi_dev, HDCP_CTRL1,
189                      m_AUTH_START | m_ENCRYPT_ENABLE | m_ADVANED_ENABLE |
190                      m_AUTH_STOP | m_HDCP_RESET,
191                      v_AUTH_START(1) | v_ENCRYPT_ENABLE(1) |
192                      v_ADVANED_ENABLE(0) | v_AUTH_STOP(0) | v_HDCP_RESET(0));
193
194         if (tmds_clk <= (HDMI_SYS_FREG_CLK << 2)) {
195                 hdmi_msk_reg(hdmi_dev, SYS_CTRL, m_REG_CLK_SOURCE,
196                              v_REG_CLK_SOURCE_TMDS);
197         }
198         return HDCP_OK;
199 }
200
201 static int rockchip_hdmiv1_hdcp_stop_authentication(struct hdmi_dev *hdmi_dev)
202 {
203         hdmi_msk_reg(hdmi_dev, SYS_CTRL,
204                      m_REG_CLK_SOURCE, v_REG_CLK_SOURCE_SYS);
205         hdmi_writel(hdmi_dev, DDC_CLK_L, 0x1c);
206         hdmi_writel(hdmi_dev, DDC_CLK_H, 0x00);
207         hdmi_writel(hdmi_dev, HDCP_CTRL2, 0x08);
208         hdmi_writel(hdmi_dev, HDCP_INT_MASK2, 0x06);
209         hdmi_writel(hdmi_dev, HDCP_CTRL1, 0x02);
210         return 0;
211         /*hdmi_writel(HDCP_CTRL1, 0x0a);*/
212 }
213
214 static int rockchip_hdmiv1_hdcp_error(int value)
215 {
216         if (value & 0x80)
217                 HDCP_WARN("Timed out waiting for downstream repeater\n");
218         else if (value & 0x40)
219                 HDCP_WARN("Too many devices connected to repeater tree\n");
220         else if (value & 0x20)
221                 HDCP_WARN("SHA-1 hash check of BKSV list failed\n");
222         else if (value & 0x10)
223                 HDCP_WARN("SHA-1 hash check of BKSV list failed\n");
224         else if (value & 0x08)
225                 HDCP_WARN("DDC channels no acknowledge\n");
226         else if (value & 0x04)
227                 HDCP_WARN("Pj mismatch\n");
228         else if (value & 0x02)
229                 HDCP_WARN("Ri mismatch\n");
230         else if (value & 0x01)
231                 HDCP_WARN("Bksv is wrong\n");
232         else
233                 return 0;
234         return 1;
235 }
236
237 static void rockchip_hdmiv1_hdcp_interrupt(struct hdmi_dev *hdmi_dev,
238                                            char *status1, char *status2)
239 {
240         int interrupt1 = 0;
241         int interrupt2 = 0;
242         int temp = 0;
243         int tmds_clk;
244
245         tmds_clk = hdmi_dev->tmdsclk;
246         hdmi_readl(hdmi_dev, HDCP_INT_STATUS1, &interrupt1);
247         hdmi_readl(hdmi_dev, HDCP_INT_STATUS2, &interrupt2);
248
249         if (tmds_clk <= (HDMI_SYS_FREG_CLK << 2))
250                 hdmi_msk_reg(hdmi_dev, SYS_CTRL,
251                              m_REG_CLK_SOURCE, v_REG_CLK_SOURCE_SYS);
252
253         if (interrupt1) {
254                 hdmi_writel(hdmi_dev, HDCP_INT_STATUS1, interrupt1);
255                 if (interrupt1 & m_INT_HDCP_ERR) {
256                         hdmi_readl(hdmi_dev, HDCP_ERROR, &temp);
257                         HDCP_WARN("HDCP: Error reg 0x65 = 0x%02x\n", temp);
258                         rockchip_hdmiv1_hdcp_error(temp);
259                         hdmi_writel(hdmi_dev, HDCP_ERROR, temp);
260                 }
261         }
262         if (interrupt2)
263                 hdmi_writel(hdmi_dev, HDCP_INT_STATUS2, interrupt2);
264
265         *status1 = interrupt1;
266         *status2 = interrupt2;
267
268         if (tmds_clk <= (HDMI_SYS_FREG_CLK << 2))
269                 hdmi_msk_reg(hdmi_dev, SYS_CTRL, m_REG_CLK_SOURCE,
270                              v_REG_CLK_SOURCE_TMDS);
271 }
272
273 /*-----------------------------------------------------------------------------
274  * Function: hdcp_submit_work
275  *-----------------------------------------------------------------------------
276  */
277 static struct delayed_work *hdcp_submit_work(int event, int delay)
278 {
279         struct hdcp_delayed_work *work;
280
281         HDMIDBG(3, "%s event %04x delay %d\n", __func__, event, delay);
282         work = kmalloc(sizeof(*work), GFP_ATOMIC);
283
284         if (work) {
285                 INIT_DELAYED_WORK(&work->work, hdcp_work_queue);
286                 work->event = event;
287                 queue_delayed_work(hdcp->workqueue,
288                                    &work->work,
289                                    msecs_to_jiffies(delay));
290         } else {
291                 HDCP_WARN("HDCP:Cannot allocate memory to create work\n");
292                 return 0;
293         }
294
295         return &work->work;
296 }
297
298 /*-----------------------------------------------------------------------------
299  * Function: hdcp_cancel_work
300  *-----------------------------------------------------------------------------
301  */
302 static void hdcp_cancel_work(struct delayed_work **work)
303 {
304         int ret = 0;
305
306         if (*work) {
307                 ret = cancel_delayed_work(*work);
308                 if (ret != 1) {
309                         ret = cancel_work_sync(&((*work)->work));
310                         HDCP_WARN("Canceling sync work failed %d\n", ret);
311                 }
312                 kfree(*work);
313                 *work = 0;
314         }
315 }
316
317 /*-----------------------------------------------------------------------------
318  * Function: auth_timer_func
319  *-----------------------------------------------------------------------------
320  */
321 static void auth_timer_func(unsigned long data)
322 {
323         HDCP_WARN("hdcp auth 2 second timeout\n");
324         if (hdcp->auth_state == 0) {
325                 mod_timer(&auth_timer, jiffies + AUTH_TIMEOUT);
326                 if ((hdcp->hdcp_state != HDCP_DISABLED) &&
327                     (hdcp->hdcp_state != HDCP_ENABLE_PENDING)) {
328                         if (is_1b_03_test(hdcp->hdmi_dev))
329                                 return;
330                         hdcp_submit_work(HDCP_FAIL_EVENT, 0);
331                 }
332         }
333 }
334
335 /*-----------------------------------------------------------------------------
336  * Function: hdcp_wq_authentication_failure
337  *-----------------------------------------------------------------------------
338  */
339 static void hdcp_wq_authentication_failure(void)
340 {
341         if (hdcp->hdmi_state == HDMI_STOPPED)
342                 return;
343
344         rockchip_hdmiv1_hdcp_disable(hdcp->hdmi_dev);
345
346         /* rockchip_hdmiv1_hdmi_control_output(false); */
347
348         rockchip_hdmiv1_set_colorbar(hdcp->hdmi_dev, 1);
349         hdcp_cancel_work(&hdcp->pending_wq_event);
350         if (hdcp->retry_cnt && (hdcp->hdmi_state != HDMI_STOPPED)) {
351                 if (hdcp->retry_cnt <= HDCP_INFINITE_REAUTH) {
352                         hdcp->retry_cnt--;
353                         HDCP_WARN("authentication failed attempts=%d\n",
354                                   hdcp->retry_cnt);
355                 } else {
356                         HDCP_WARN("authentication failed retrying\n");
357                 }
358                 hdcp->hdcp_state = HDCP_AUTHENTICATION_START;
359
360                 if (hdcp->auth_state == 1 && timer_state == 0) {
361                         HDMIDBG(3, "add auth timer\n");
362                         hdcp->auth_state = 0;
363                         hdcp->retry_cnt = HDCP_INFINITE_REAUTH;
364                         auth_timer.expires = jiffies + AUTH_TIMEOUT;
365                         add_timer(&auth_timer);
366                         timer_state = 1;
367                 }
368
369                 hdcp->pending_wq_event = hdcp_submit_work(HDCP_AUTH_REATT_EVENT,
370                                                          HDCP_REAUTH_DELAY);
371         } else {
372                 HDCP_WARN("authentication failed HDCP disabled\n");
373                 hdcp->hdcp_state = HDCP_ENABLE_PENDING;
374
375                 if (timer_state == 1) {
376                         HDMIDBG(3, "delete auth timer\n");
377                         del_timer_sync(&auth_timer);
378                         timer_state = 0;
379                 }
380         }
381 }
382
383 /*-----------------------------------------------------------------------------
384  * Function: hdcp_wq_start_authentication
385  *-----------------------------------------------------------------------------
386  */
387 static void hdcp_wq_start_authentication(void)
388 {
389         int status = HDCP_OK;
390
391         hdcp->hdcp_state = HDCP_AUTHENTICATION_START;
392         HDMIDBG(3, "HDCP: authentication start\n");
393         status = rockchip_hdmiv1_hdcp_start_authentication(hdcp->hdmi_dev);
394         if (status != HDCP_OK) {
395                 HDMIDBG(3, "HDCP: authentication failed\n");
396                 hdcp_wq_authentication_failure();
397         } else {
398                 /*hdcp->hdcp_state = HDCP_WAIT_KSV_LIST;*/
399                 hdcp->hdcp_state = HDCP_LINK_INTEGRITY_CHECK;
400         }
401 }
402
403 #if 0
404 /*-----------------------------------------------------------------------------
405  * Function: hdcp_wq_check_bksv
406  *-----------------------------------------------------------------------------
407  */
408 static void hdcp_wq_check_bksv(void)
409 {
410         int status = HDCP_OK;
411
412         DBG("Check BKSV start");
413         status = rockchip_hdmiv1_hdcp_check_bksv();
414         if (status != HDCP_OK) {
415                 HDCP_WARN("HDCP: Check BKSV failed");
416                 hdcp->retry_cnt = 0;
417                 hdcp_wq_authentication_failure();
418         } else {
419                 DBG("HDCP: Check BKSV successful");
420                 hdcp->hdcp_state = HDCP_LINK_INTEGRITY_CHECK;
421                 /* Restore retry counter */
422                 if (hdcp->retry_times == 0)
423                         hdcp->retry_cnt = HDCP_INFINITE_REAUTH;
424                 else
425                         hdcp->retry_cnt = hdcp->retry_times;
426         }
427 }
428 #endif
429 /*-----------------------------------------------------------------------------
430  * Function: hdcp_wq_authentication_success
431  *-----------------------------------------------------------------------------
432  */
433 static void hdcp_wq_authentication_success(void)
434 {
435         hdcp->auth_state = 1;
436         if (timer_state == 1) {
437                 HDMIDBG(3, "delete auth timer\n");
438                 timer_state = 0;
439                 del_timer_sync(&auth_timer);
440         }
441
442         rockchip_hdmiv1_set_colorbar(hdcp->hdmi_dev, 0);
443         HDCP_WARN("HDCP: authentication pass\n");
444 }
445
446 /*-----------------------------------------------------------------------------
447  * Function: hdcp_wq_disable
448  *-----------------------------------------------------------------------------
449  */
450 static void hdcp_wq_disable(int event)
451 {
452         HDCP_WARN("HDCP: disabled\n");
453
454         hdcp_cancel_work(&hdcp->pending_wq_event);
455         rockchip_hdmiv1_hdcp_disable(hdcp->hdmi_dev);
456         if (event == HDCP_DISABLE_CTL) {
457                 hdcp->hdcp_state = HDCP_DISABLED;
458                 if (hdcp->hdmi_state == HDMI_STARTED)
459                         rockchip_hdmiv1_set_colorbar(hdcp->hdmi_dev, 0);
460         } else if (event == HDCP_STOP_FRAME_EVENT) {
461                 hdcp->hdcp_state = HDCP_ENABLE_PENDING;
462         }
463 }
464
465 /*-----------------------------------------------------------------------------
466  * Function: hdcp_work_queue
467  *-----------------------------------------------------------------------------
468  */
469 static void hdcp_work_queue(struct work_struct *work)
470 {
471         struct hdcp_delayed_work *hdcp_w =
472                 container_of(work, struct hdcp_delayed_work, work.work);
473         int event = hdcp_w->event;
474
475         mutex_lock(&hdcp->lock);
476         HDMIDBG(3, "%s - START - %u hdmi=%d hdcp=%d evt= %x %d\n",
477                 __func__,
478                 jiffies_to_msecs(jiffies),
479                 hdcp->hdmi_state,
480                 hdcp->hdcp_state,
481                 (event & 0xFF00) >> 8,
482                 event & 0xFF);
483
484         if (event == HDCP_STOP_FRAME_EVENT)
485                 hdcp->hdmi_state = HDMI_STOPPED;
486         if (event == HDCP_DISABLE_CTL || event == HDCP_STOP_FRAME_EVENT)
487                 hdcp_wq_disable(event);
488         if (event & HDCP_WORKQUEUE_SRC)
489                 hdcp->pending_wq_event = 0;
490         /* First handle HDMI state */
491         if (event == HDCP_START_FRAME_EVENT) {
492                 hdcp->pending_start = 0;
493                 hdcp->hdmi_state = HDMI_STARTED;
494         }
495
496         /**********************/
497         /* HDCP state machine */
498         /**********************/
499         switch (hdcp->hdcp_state) {
500         case HDCP_DISABLED:
501                 /* HDCP enable control or re-authentication event */
502                 if (event == HDCP_ENABLE_CTL) {
503                         #if 0
504                         if (hdcp->retry_times == 0)
505                                 hdcp->retry_cnt = HDCP_INFINITE_REAUTH;
506                         else
507                                 hdcp->retry_cnt = hdcp->retry_times;
508                         #endif
509                         hdcp->retry_cnt = HDCP_INFINITE_REAUTH;
510                         if (hdcp->hdmi_state == HDMI_STARTED)
511                                 hdcp_wq_start_authentication();
512                         else
513                                 hdcp->hdcp_state = HDCP_ENABLE_PENDING;
514                 }
515                 break;
516         case HDCP_ENABLE_PENDING:
517                 /* HDMI start frame event */
518                 if (event == HDCP_START_FRAME_EVENT)
519                         hdcp_wq_start_authentication();
520                 break;
521         case HDCP_AUTHENTICATION_START:
522                 /* Re-authentication */
523                 if (event == HDCP_AUTH_REATT_EVENT)
524                         hdcp_wq_start_authentication();
525                 break;
526 #if 0
527         case HDCP_WAIT_KSV_LIST:
528                 /* KSV failure */
529                 if (event == HDCP_FAIL_EVENT) {
530                         HDCP_WARN("HDCP: KSV switch failure\n");
531                         hdcp_wq_authentication_failure();
532                 }
533                 /* KSV list ready event */
534                 else if (event == HDCP_KSV_LIST_RDY_EVENT)
535                         hdcp_wq_check_bksv();
536                 break;
537 #endif
538         case HDCP_LINK_INTEGRITY_CHECK:
539                 /* authentication failure */
540                 if (event == HDCP_FAIL_EVENT) {
541                         HDCP_WARN("HDCP: Ri check failure\n");
542                         hdcp_wq_authentication_failure();
543                 } else if (event == HDCP_AUTH_PASS_EVENT) {
544                         hdcp_wq_authentication_success();
545                 }
546                 break;
547         default:
548                 HDCP_WARN("HDCP: error - unknown HDCP state\n");
549                 break;
550         }
551         kfree(hdcp_w);
552         if (event == HDCP_STOP_FRAME_EVENT)
553                 complete(&hdcp->complete);
554         mutex_unlock(&hdcp->lock);
555 }
556
557 /*-----------------------------------------------------------------------------
558  * Function: hdcp_start_frame_cb
559  *-----------------------------------------------------------------------------
560  */
561 static void hdcp_start_frame_cb(struct hdmi *hdmi)
562 {
563         HDMIDBG(3, "hdcp_start_frame_cb()\n");
564
565         /* Cancel any pending work */
566         if (hdcp->pending_start)
567                 hdcp_cancel_work(&hdcp->pending_start);
568         if (hdcp->pending_wq_event)
569                 hdcp_cancel_work(&hdcp->pending_wq_event);
570
571         if (timer_state == 0) {
572                 HDMIDBG(3, "add auth timer\n");
573                 auth_timer.expires = jiffies + AUTH_TIMEOUT;
574                 add_timer(&auth_timer);
575                 timer_state = 1;
576         }
577
578         hdcp->retry_cnt = HDCP_INFINITE_REAUTH;
579         hdcp->pending_start = hdcp_submit_work(HDCP_START_FRAME_EVENT,
580                                                HDCP_ENABLE_DELAY);
581 }
582
583 /*-----------------------------------------------------------------------------
584  * Function: hdcp_irq_cb
585  *-----------------------------------------------------------------------------
586  */
587 static void hdcp_irq_cb(int status)
588 {
589         char interrupt1;
590         char interrupt2;
591
592         rockchip_hdmiv1_hdcp_interrupt(hdcp->hdmi_dev,
593                                        &interrupt1,
594                                        &interrupt2);
595         HDMIDBG(3, "%s 0x%02x 0x%02x\n", __func__, interrupt1, interrupt2);
596         if (interrupt1 & m_INT_HDCP_ERR) {
597                 if ((hdcp->hdcp_state != HDCP_DISABLED) &&
598                     (hdcp->hdcp_state != HDCP_ENABLE_PENDING))
599                         hdcp_submit_work(HDCP_FAIL_EVENT, 0);
600         }
601         #if 0
602         else if (interrupt1 & (m_INT_BKSV_READY | m_INT_BKSV_UPDATE))
603                 hdcp_submit_work(HDCP_KSV_LIST_RDY_EVENT, 0);
604         #endif
605         else if (interrupt1 & m_INT_AUTH_SUCCESS)
606                 hdcp_submit_work(HDCP_AUTH_PASS_EVENT, 0);
607 }
608
609 /*-----------------------------------------------------------------------------
610  * Function: hdcp_power_on_cb
611  *-----------------------------------------------------------------------------
612  */
613 static int hdcp_power_on_cb(void)
614 {
615         HDMIDBG(3, "%s", __func__);
616         return rockchip_hdmiv1_hdcp_load_key2mem();
617 }
618
619 /*-----------------------------------------------------------------------------
620  * Function: hdcp_power_off_cb
621  *-----------------------------------------------------------------------------
622  */
623 static void hdcp_power_off_cb(struct hdmi *hdmi)
624 {
625         unsigned int time;
626
627         HDMIDBG(3, "%s\n", __func__);
628         if (timer_state == 1) {
629                 HDMIDBG(3, "delete auth timer\n");
630                 timer_state = 0;
631                 del_timer_sync(&auth_timer);
632         }
633         hdcp->auth_state = 0;
634
635         if (!hdcp->enable)
636                 return;
637         rockchip_hdmiv1_hdcp_stop_authentication(hdcp->hdmi_dev);
638         hdcp_cancel_work(&hdcp->pending_start);
639         hdcp_cancel_work(&hdcp->pending_wq_event);
640         init_completion(&hdcp->complete);
641         /* Post event to workqueue */
642         time = msecs_to_jiffies(5000);
643         if (hdcp_submit_work(HDCP_STOP_FRAME_EVENT, 0))
644                 wait_for_completion_interruptible_timeout(&hdcp->complete,
645                                                           time);
646 }
647
648 /*
649  * Load HDCP key to external HDCP memory
650  */
651 static void hdcp_load_keys_cb(const struct firmware *fw, void *context)
652 {
653         if (!fw) {
654                 pr_err("HDCP: failed to load keys\n");
655                 return;
656         }
657         if (fw->size < HDCP_KEY_SIZE) {
658                 pr_err("HDCP: firmware wrong size %d\n", (int)fw->size);
659                 return;
660         }
661         hdcp->keys =  kmalloc(HDCP_KEY_SIZE, GFP_KERNEL);
662         if (!hdcp->keys)
663                 return;
664         memcpy(hdcp->keys, fw->data, HDCP_KEY_SIZE);
665         HDCP_WARN("HDCP: load hdcp key success\n");
666
667         if (fw->size > HDCP_KEY_SIZE) {
668                 HDMIDBG(3, "%s invalid key size %d\n", __func__,
669                         (int)fw->size - HDCP_KEY_SIZE);
670                 if ((fw->size - HDCP_KEY_SIZE) % 5) {
671                         pr_err("HDCP: failed to load invalid keys\n");
672                         return;
673                 }
674                 hdcp->invalidkeys =
675                         kmalloc(fw->size - HDCP_KEY_SIZE, GFP_KERNEL);
676                 if (!hdcp->invalidkeys) {
677                         pr_err("HDCP: can't allocated space for invalid keys\n");
678                         return;
679                 }
680                 memcpy(hdcp->invalidkeys, fw->data +
681                        HDCP_KEY_SIZE, fw->size - HDCP_KEY_SIZE);
682                 hdcp->invalidkey = (fw->size - HDCP_KEY_SIZE) / 5;
683                 HDCP_WARN("HDCP: loaded hdcp invalid key success\n");
684         }
685 }
686
687 static ssize_t hdcp_enable_read(struct device *device,
688                                 struct device_attribute *attr,
689                                 char *buf)
690 {
691         int enable = 0;
692
693         if (hdcp)
694                 enable = hdcp->enable;
695         return snprintf(buf, PAGE_SIZE, "%d\n", enable);
696 }
697
698 static ssize_t hdcp_enable_write(struct device *device,
699                                  struct device_attribute *attr,
700                                  const char *buf, size_t count)
701 {
702         int enable;
703
704         if (!hdcp)
705                 return -EINVAL;
706         if (kstrtoint(buf, 0, &enable))
707                 return -EINVAL;
708         if (hdcp->enable != enable) {
709                 /* Post event to workqueue */
710                 if (enable) {
711                         if (hdcp_submit_work(HDCP_ENABLE_CTL, 0) == 0)
712                                 return -EFAULT;
713                 } else {
714                         hdcp_cancel_work(&hdcp->pending_start);
715                         hdcp_cancel_work(&hdcp->pending_wq_event);
716
717                         /* Post event to workqueue */
718                         if (hdcp_submit_work(HDCP_DISABLE_CTL, 0) == 0)
719                                 return -EFAULT;
720                 }
721                 hdcp->enable = enable;
722         }
723         return count;
724 }
725
726 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
727                          hdcp_enable_read, hdcp_enable_write);
728
729 static ssize_t hdcp_trytimes_read(struct device *device,
730                                   struct device_attribute *attr,
731                                   char *buf)
732 {
733         int trytimes = 0;
734
735         if (hdcp)
736                 trytimes = hdcp->retry_times;
737         return snprintf(buf, PAGE_SIZE, "%d\n", trytimes);
738 }
739
740 static ssize_t hdcp_trytimes_wrtie(struct device *device,
741                                    struct device_attribute *attr,
742                                    const char *buf, size_t count)
743 {
744         int trytimes;
745
746         if (!hdcp)
747                 return -EINVAL;
748         if (kstrtoint(buf, 0, &trytimes))
749                 return -EINVAL;
750         if (hdcp->retry_times != trytimes)
751                 hdcp->retry_times = trytimes;
752         return count;
753 }
754
755 static DEVICE_ATTR(trytimes, S_IRUGO | S_IWUSR,
756                          hdcp_trytimes_read, hdcp_trytimes_wrtie);
757 static struct miscdevice mdev;
758
759 int rockchip_hdmiv1_hdcp_init(struct hdmi *hdmi)
760 {
761         int ret;
762
763         HDMIDBG(3, "[%s]\n", __func__);
764         if (hdcp)
765                 return 0;
766
767         hdcp = kmalloc(sizeof(*hdcp), GFP_KERNEL);
768         if (!hdcp) {
769                 HDCP_WARN(">>HDCP: kmalloc fail!\n");
770                 ret = -ENOMEM;
771                 goto error0;
772         }
773         memset(hdcp, 0, sizeof(struct hdcp));
774         mutex_init(&hdcp->lock);
775         mdev.minor = MISC_DYNAMIC_MINOR;
776         mdev.name = "hdcp";
777         mdev.mode = 0666;
778         if (misc_register(&mdev)) {
779                 HDCP_WARN("HDCP: Could not add character driver\n");
780                 ret = HDMI_ERROR_FALSE;
781                 goto error1;
782         }
783         ret = device_create_file(mdev.this_device, &dev_attr_enable);
784         if (ret) {
785                 HDCP_WARN("HDCP: Could not add sys file enable\n");
786                 ret = -EINVAL;
787                 goto error2;
788         }
789         ret = device_create_file(mdev.this_device, &dev_attr_trytimes);
790         if (ret) {
791                 HDCP_WARN("HDCP: Could not add sys file trytimes\n");
792                 ret = -EINVAL;
793                         goto error3;
794         }
795         hdcp->workqueue = create_singlethread_workqueue("hdcp");
796         if (!hdcp->workqueue) {
797                 HDCP_WARN("HDCP,: create workqueue failed.\n");
798                 goto error4;
799         }
800         ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
801                                       "hdcp", mdev.this_device,
802                                       GFP_KERNEL, hdcp,
803                                       hdcp_load_keys_cb);
804         if (ret < 0) {
805                 HDCP_WARN("HDCP: request_firmware_nowait failed: %d\n", ret);
806                 goto error5;
807         }
808         hdcp->hdmi_dev = hdmi->property->priv;
809         hdmi->ops->hdcp_cb = hdcp_start_frame_cb;
810         hdmi->ops->hdcp_irq_cb = hdcp_irq_cb;
811         hdmi->ops->hdcp_power_on_cb = hdcp_power_on_cb;
812         hdmi->ops->hdcp_power_off_cb = hdcp_power_off_cb;
813
814         init_timer(&auth_timer);
815         auth_timer.data = 0;
816         auth_timer.function = auth_timer_func;
817         HDMIDBG(3, "%s success\n", __func__);
818         return 0;
819 error5:
820         destroy_workqueue(hdcp->workqueue);
821 error4:
822         device_remove_file(mdev.this_device, &dev_attr_trytimes);
823 error3:
824         device_remove_file(mdev.this_device, &dev_attr_enable);
825 error2:
826         misc_deregister(&mdev);
827 error1:
828         kfree(hdcp->keys);
829         kfree(hdcp->invalidkeys);
830         kfree(hdcp);
831 error0:
832         return ret;
833 }
834