0e5a76c55d2463869cad492721357026a2e96454
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / hdmi / rockchip-hdmiv2 / rockchip_hdmiv2_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 "rockchip_hdmiv2.h"
9 #include "rockchip_hdmiv2_hw.h"
10
11 #define HDCP_KEY_SIZE           308
12 #define HDCP_PRIVATE_KEY_SIZE   280
13 #define HDCP_KEY_SHA_SIZE       20
14 #define HDCP_KEY_SEED_SIZE      2
15
16 struct hdcp_keys {
17         u8 KSV[8];
18         u8 devicekey[HDCP_PRIVATE_KEY_SIZE];
19         u8 sha1[HDCP_KEY_SHA_SIZE];
20 };
21
22 struct hdcp {
23         struct hdmi             *hdmi;
24         int                     enable;
25         int                     retry_times;
26         struct hdcp_keys        *keys;
27         char                    *seeds;
28         int                     invalidkey;
29         char                    *invalidkeys;
30 };
31
32 static struct miscdevice mdev;
33 static struct hdcp *hdcp;
34
35 static void hdcp_load_key(struct hdmi *hdmi, struct hdcp_keys *key)
36 {
37         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
38         int i, value;
39
40         /* Disable decryption logic */
41         hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 0);
42         /* Poll untile DPK write is allowed */
43         do {
44                 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
45         } while ((value & m_DPK_WR_OK_STS) == 0);
46
47         /* write unencryped AKSV */
48         hdmi_writel(hdmi_dev, HDCPREG_DPK6, 0);
49         hdmi_writel(hdmi_dev, HDCPREG_DPK5, 0);
50         hdmi_writel(hdmi_dev, HDCPREG_DPK4, key->KSV[4]);
51         hdmi_writel(hdmi_dev, HDCPREG_DPK3, key->KSV[3]);
52         hdmi_writel(hdmi_dev, HDCPREG_DPK2, key->KSV[2]);
53         hdmi_writel(hdmi_dev, HDCPREG_DPK1, key->KSV[1]);
54         hdmi_writel(hdmi_dev, HDCPREG_DPK0, key->KSV[0]);
55         /* Poll untile DPK write is allowed */
56         do {
57                 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
58         } while ((value & m_DPK_WR_OK_STS) == 0);
59
60         if (hdcp->seeds != NULL) {
61                 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 1);
62                 hdmi_writel(hdmi_dev, HDCPREG_SEED1, hdcp->seeds[0]);
63                 hdmi_writel(hdmi_dev, HDCPREG_SEED0, hdcp->seeds[1]);
64         } else {
65                 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 0);
66         }
67
68         /* write private key */
69         for (i = 0; i < HDCP_PRIVATE_KEY_SIZE; i += 7) {
70                 hdmi_writel(hdmi_dev, HDCPREG_DPK6, key->devicekey[i + 6]);
71                 hdmi_writel(hdmi_dev, HDCPREG_DPK5, key->devicekey[i + 5]);
72                 hdmi_writel(hdmi_dev, HDCPREG_DPK4, key->devicekey[i + 4]);
73                 hdmi_writel(hdmi_dev, HDCPREG_DPK3, key->devicekey[i + 3]);
74                 hdmi_writel(hdmi_dev, HDCPREG_DPK2, key->devicekey[i + 2]);
75                 hdmi_writel(hdmi_dev, HDCPREG_DPK1, key->devicekey[i + 1]);
76                 hdmi_writel(hdmi_dev, HDCPREG_DPK0, key->devicekey[i]);
77
78                 do {
79                         value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
80                 } while ((value & m_DPK_WR_OK_STS) == 0);
81         }
82
83         pr_info("%s success\n", __func__);
84 }
85
86 static void hdcp_load_keys_cb(const struct firmware *fw,
87                               void *context)
88 {
89         struct hdmi *hdmi = (struct hdmi *)context;
90
91         if (fw->size < HDCP_KEY_SIZE) {
92                 pr_err("HDCP: firmware wrong size %d\n", (int)fw->size);
93                 return;
94         }
95         hdcp->keys = kmalloc(HDCP_KEY_SIZE, GFP_KERNEL);
96         memcpy(hdcp->keys, fw->data, HDCP_KEY_SIZE);
97
98         if (fw->size > HDCP_KEY_SIZE) {
99                 if ((fw->size - HDCP_KEY_SIZE) < HDCP_KEY_SEED_SIZE) {
100                         pr_err("HDCP: invalid seed key size\n");
101                         return;
102                 }
103                 hdcp->seeds = kmalloc(HDCP_KEY_SEED_SIZE, GFP_KERNEL);
104                 if (hdcp->seeds == NULL) {
105                         pr_err("HDCP: can't allocated space for seed keys\n");
106                         return;
107                 }
108                 memcpy(hdcp->seeds, fw->data + HDCP_KEY_SIZE,
109                        HDCP_KEY_SEED_SIZE);
110         }
111         hdcp_load_key(hdmi, hdcp->keys);
112 }
113
114 void rockchip_hdmiv2_hdcp2_enable(int enable)
115 {
116         struct hdmi_dev *hdmi_dev;
117
118         if (!hdcp) {
119                 pr_err("rockchip hdmiv2 hdcp is not exist\n");
120                 return;
121         }
122         hdmi_dev = hdcp->hdmi->property->priv;
123         if (hdmi_dev->soctype == HDMI_SOC_RK3368 &&
124             hdmi_dev->hdcp2_enable != enable) {
125                 hdmi_dev->hdcp2_enable = enable;
126                 if (hdmi_dev->hdcp2_enable == 0) {
127                         hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
128                                      m_HDCP2_OVR_EN | m_HDCP2_FORCE,
129                                      v_HDCP2_OVR_EN(1) | v_HDCP2_FORCE(0));
130                         hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0xff);
131                         hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0xff);
132                 } else {
133                         hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
134                                      m_HDCP2_OVR_EN | m_HDCP2_FORCE,
135                                      v_HDCP2_OVR_EN(0) | v_HDCP2_FORCE(0));
136                         hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0x00);
137                         hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0x00);
138                 }
139         }
140 }
141 EXPORT_SYMBOL(rockchip_hdmiv2_hdcp2_enable);
142
143 void rockchip_hdmiv2_hdcp2_init(void (*hdcp2_enble)(int),
144                                 void (*hdcp2_reset)(void),
145                                 void (*hdcp2_start)(void))
146 {
147         struct hdmi_dev *hdmi_dev;
148
149         if (!hdcp) {
150                 pr_err("rockchip hdmiv2 hdcp is not exist\n");
151                 return;
152         }
153         hdmi_dev = hdcp->hdmi->property->priv;
154         hdmi_dev->hdcp2_en = hdcp2_enble;
155         hdmi_dev->hdcp2_reset = hdcp2_reset;
156         hdmi_dev->hdcp2_start = hdcp2_start;
157 }
158 EXPORT_SYMBOL(rockchip_hdmiv2_hdcp2_init);
159
160 static void rockchip_hdmiv2_hdcp_start(struct hdmi *hdmi)
161 {
162         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
163
164         if (!hdcp->enable)
165                 return;
166         if (hdmi_dev->soctype == HDMI_SOC_RK3368) {
167                 if (hdmi_dev->hdcp2_enable == 0) {
168                         hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
169                                      m_HDCP2_OVR_EN | m_HDCP2_FORCE,
170                                      v_HDCP2_OVR_EN(1) | v_HDCP2_FORCE(0));
171                         hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0xff);
172                         hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0xff);
173                 } else {
174                         hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
175                                      m_HDCP2_OVR_EN | m_HDCP2_FORCE,
176                                      v_HDCP2_OVR_EN(0) | v_HDCP2_FORCE(0));
177                         hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0x00);
178                         hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0x00);
179                 }
180         }
181
182         hdmi_msk_reg(hdmi_dev, FC_INVIDCONF,
183                      m_FC_HDCP_KEEPOUT, v_FC_HDCP_KEEPOUT(1));
184         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0,
185                      m_HDMI_DVI, v_HDMI_DVI(hdmi->edid.sink_hdmi));
186         hdmi_writel(hdmi_dev, A_OESSWCFG, 0x40);
187         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0,
188                      m_ENCRYPT_BYPASS | m_FEATURE11_EN | m_SYNC_RI_CHECK,
189                      v_ENCRYPT_BYPASS(0) | v_FEATURE11_EN(0) |
190                      v_SYNC_RI_CHECK(1));
191         hdmi_msk_reg(hdmi_dev, A_HDCPCFG1,
192                      m_ENCRYPT_DISBALE | m_PH2UPSHFTENC,
193                      v_ENCRYPT_DISBALE(0) | v_PH2UPSHFTENC(1));
194         /* Reset HDCP Engine */
195         hdmi_msk_reg(hdmi_dev, A_HDCPCFG1,
196                      m_HDCP_SW_RST, v_HDCP_SW_RST(0));
197
198         hdmi_writel(hdmi_dev, A_APIINTMSK, 0x00);
199         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(1));
200
201         hdmi_msk_reg(hdmi_dev, MC_CLKDIS,
202                      m_HDCPCLK_DISABLE, v_HDCPCLK_DISABLE(0));
203         if (hdmi_dev->hdcp2_start)
204                 hdmi_dev->hdcp2_start();
205         pr_info("%s success\n", __func__);
206 }
207
208 static void rockchip_hdmiv2_hdcp_stop(struct hdmi *hdmi)
209 {
210         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
211
212         if (!hdcp->enable)
213                 return;
214
215         hdmi_msk_reg(hdmi_dev, MC_CLKDIS,
216                      m_HDCPCLK_DISABLE, v_HDCPCLK_DISABLE(1));
217         hdmi_writel(hdmi_dev, A_APIINTMSK, 0xff);
218         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(0));
219         rockchip_hdmiv2_hdcp2_enable(0);
220 }
221
222 static ssize_t hdcp_enable_read(struct device *device,
223                                 struct device_attribute *attr, char *buf)
224 {
225         int enable = 0;
226
227         if (hdcp)
228                 enable = hdcp->enable;
229
230         return snprintf(buf, PAGE_SIZE, "%d\n", enable);
231 }
232
233 static ssize_t hdcp_enable_write(struct device *device,
234                                  struct device_attribute *attr,
235                                  const char *buf, size_t count)
236 {
237         int enable;
238
239         if (hdcp == NULL)
240                 return -EINVAL;
241
242         if (kstrtoint(buf, 0, &enable))
243                 return -EINVAL;
244
245         if (hdcp->enable != enable) {
246                 if (!hdcp->enable)
247                         hdmi_submit_work(hdcp->hdmi, HDMI_ENABLE_HDCP, 0, NULL);
248                 else
249                         rockchip_hdmiv2_hdcp_stop(hdcp->hdmi);
250                 hdcp->enable =  enable;
251         }
252
253         return count;
254 }
255 static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR,
256                    hdcp_enable_read, hdcp_enable_write);
257
258 static ssize_t hdcp_trytimes_read(struct device *device,
259                                   struct device_attribute *attr, char *buf)
260 {
261         int trytimes = 0;
262
263         if (hdcp)
264                 trytimes = hdcp->retry_times;
265
266         return snprintf(buf, PAGE_SIZE, "%d\n", trytimes);
267 }
268
269 static ssize_t hdcp_trytimes_wrtie(struct device *device,
270                                    struct device_attribute *attr,
271                                    const char *buf, size_t count)
272 {
273         int trytimes;
274
275         if (hdcp == NULL)
276                 return -EINVAL;
277
278         if (kstrtoint(buf, 0, &trytimes))
279                 return -EINVAL;
280
281         if (hdcp->retry_times != trytimes)
282                 hdcp->retry_times = trytimes;
283
284         return count;
285 }
286 static DEVICE_ATTR(trytimes, S_IRUGO|S_IWUSR,
287                    hdcp_trytimes_read, hdcp_trytimes_wrtie);
288
289 static int hdcp_init(struct hdmi *hdmi)
290 {
291         int ret;
292
293         mdev.minor = MISC_DYNAMIC_MINOR;
294         mdev.name = "hdcp";
295         mdev.mode = 0666;
296         hdcp = kmalloc(sizeof(*hdcp), GFP_KERNEL);
297         if (!hdcp) {
298                 pr_err("HDCP: kmalloc fail!\n");
299                 ret = -ENOMEM;
300                 goto error0;
301         }
302         memset(hdcp, 0, sizeof(struct hdcp));
303         hdcp->hdmi = hdmi;
304         if (misc_register(&mdev)) {
305                 pr_err("HDCP: Could not add character driver\n");
306                 ret = HDMI_ERROR_FALSE;
307                 goto error1;
308         }
309         ret = device_create_file(mdev.this_device, &dev_attr_enable);
310         if (ret) {
311                 pr_err("HDCP: Could not add sys file enable\n");
312                 ret = -EINVAL;
313                 goto error2;
314         }
315         ret = device_create_file(mdev.this_device, &dev_attr_trytimes);
316         if (ret) {
317                 pr_err("HDCP: Could not add sys file enable\n");
318                 ret = -EINVAL;
319                 goto error3;
320         }
321
322         ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
323                                       "hdcp", mdev.this_device, GFP_KERNEL,
324                                       hdmi, hdcp_load_keys_cb);
325
326         if (ret < 0) {
327                 pr_err("HDCP: request_firmware_nowait failed: %d\n", ret);
328                 goto error4;
329         }
330
331         hdmi->ops->hdcp_cb = rockchip_hdmiv2_hdcp_start;
332         return 0;
333
334 error4:
335         device_remove_file(mdev.this_device, &dev_attr_trytimes);
336 error3:
337         device_remove_file(mdev.this_device, &dev_attr_enable);
338 error2:
339         misc_deregister(&mdev);
340 error1:
341         kfree(hdcp->keys);
342         kfree(hdcp->invalidkeys);
343         kfree(hdcp);
344 error0:
345         return ret;
346 }
347
348 void rockchip_hdmiv2_hdcp_init(struct hdmi *hdmi)
349 {
350         pr_info("%s", __func__);
351         if (hdcp == NULL)
352                 hdcp_init(hdmi);
353         else
354                 hdcp_load_key(hdmi, hdcp->keys);
355 }