Merge branch linux-linaro-lsk-v3.10-android
[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 = NULL;
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 static void rockchip_hdmiv2_hdcp_start(struct hdmi *hdmi)
115 {
116         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
117
118         if (!hdcp->enable)
119                 return;
120         if (hdmi_dev->soctype == HDMI_SOC_RK3368) {
121                 hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
122                              m_HDCP2_OVR_EN | m_HDCP2_FORCE,
123                              v_HDCP2_OVR_EN(1) | v_HDCP2_FORCE(0));
124                 hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0x00);
125                 hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0x00);
126         }
127
128         hdmi_msk_reg(hdmi_dev, FC_INVIDCONF,
129                      m_FC_HDCP_KEEPOUT, v_FC_HDCP_KEEPOUT(1));
130         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0,
131                      m_HDMI_DVI, v_HDMI_DVI(hdmi->edid.sink_hdmi));
132         hdmi_writel(hdmi_dev, A_OESSWCFG, 0x40);
133         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0,
134                      m_ENCRYPT_BYPASS | m_FEATURE11_EN | m_SYNC_RI_CHECK,
135                      v_ENCRYPT_BYPASS(0) | v_FEATURE11_EN(0) |
136                      v_SYNC_RI_CHECK(1));
137         hdmi_msk_reg(hdmi_dev, A_HDCPCFG1,
138                      m_ENCRYPT_DISBALE | m_PH2UPSHFTENC,
139                      v_ENCRYPT_DISBALE(0) | v_PH2UPSHFTENC(1));
140         /* Reset HDCP Engine */
141         hdmi_msk_reg(hdmi_dev, A_HDCPCFG1,
142                      m_HDCP_SW_RST, v_HDCP_SW_RST(0));
143
144         hdmi_writel(hdmi_dev, A_APIINTMSK, 0x00);
145         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(1));
146
147         hdmi_msk_reg(hdmi_dev, MC_CLKDIS,
148                      m_HDCPCLK_DISABLE, v_HDCPCLK_DISABLE(0));
149         pr_info("%s success\n", __func__);
150 }
151
152 static void rockchip_hdmiv2_hdcp_stop(struct hdmi *hdmi)
153 {
154         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
155
156         if (!hdcp->enable)
157                 return;
158
159         hdmi_msk_reg(hdmi_dev, MC_CLKDIS,
160                      m_HDCPCLK_DISABLE, v_HDCPCLK_DISABLE(1));
161         hdmi_writel(hdmi_dev, A_APIINTMSK, 0xff);
162         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(0));
163 }
164
165 static ssize_t hdcp_enable_read(struct device *device,
166                                 struct device_attribute *attr, char *buf)
167 {
168         int enable = 0;
169
170         if (hdcp)
171                 enable = hdcp->enable;
172
173         return snprintf(buf, PAGE_SIZE, "%d\n", enable);
174 }
175
176 static ssize_t hdcp_enable_write(struct device *device,
177                                  struct device_attribute *attr,
178                                  const char *buf, size_t count)
179 {
180         int enable;
181
182         if (hdcp == NULL)
183                 return -EINVAL;
184
185         if (kstrtoint(buf, 0, &enable))
186                 return -EINVAL;
187
188         if (hdcp->enable != enable) {
189                 if (!hdcp->enable)
190                         hdmi_submit_work(hdcp->hdmi, HDMI_ENABLE_HDCP, 0, NULL);
191                 else
192                         rockchip_hdmiv2_hdcp_stop(hdcp->hdmi);
193                 hdcp->enable =  enable;
194         }
195
196         return count;
197 }
198 static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR,
199                    hdcp_enable_read, hdcp_enable_write);
200
201 static ssize_t hdcp_trytimes_read(struct device *device,
202                                   struct device_attribute *attr, char *buf)
203 {
204         int trytimes = 0;
205
206         if (hdcp)
207                 trytimes = hdcp->retry_times;
208
209         return snprintf(buf, PAGE_SIZE, "%d\n", trytimes);
210 }
211
212 static ssize_t hdcp_trytimes_wrtie(struct device *device,
213                                    struct device_attribute *attr,
214                                    const char *buf, size_t count)
215 {
216         int trytimes;
217
218         if (hdcp == NULL)
219                 return -EINVAL;
220
221         if (kstrtoint(buf, 0, &trytimes))
222                 return -EINVAL;
223
224         if (hdcp->retry_times != trytimes)
225                 hdcp->retry_times = trytimes;
226
227         return count;
228 }
229 static DEVICE_ATTR(trytimes, S_IRUGO|S_IWUSR,
230                    hdcp_trytimes_read, hdcp_trytimes_wrtie);
231
232 static int hdcp_init(struct hdmi *hdmi)
233 {
234         int ret;
235
236         mdev.minor = MISC_DYNAMIC_MINOR;
237         mdev.name = "hdcp";
238         mdev.mode = 0666;
239         hdcp = kmalloc(sizeof(*hdcp), GFP_KERNEL);
240         if (!hdcp) {
241                 pr_err("HDCP: kmalloc fail!\n");
242                 ret = -ENOMEM;
243                 goto error0;
244         }
245         memset(hdcp, 0, sizeof(struct hdcp));
246         hdcp->hdmi = hdmi;
247         if (misc_register(&mdev)) {
248                 pr_err("HDCP: Could not add character driver\n");
249                 ret = HDMI_ERROR_FALSE;
250                 goto error1;
251         }
252         ret = device_create_file(mdev.this_device, &dev_attr_enable);
253         if (ret) {
254                 pr_err("HDCP: Could not add sys file enable\n");
255                 ret = -EINVAL;
256                 goto error2;
257         }
258         ret = device_create_file(mdev.this_device, &dev_attr_trytimes);
259         if (ret) {
260                 pr_err("HDCP: Could not add sys file enable\n");
261                 ret = -EINVAL;
262                 goto error3;
263         }
264
265         ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
266                                       "hdcp", mdev.this_device, GFP_KERNEL,
267                                       hdmi, hdcp_load_keys_cb);
268
269         if (ret < 0) {
270                 pr_err("HDCP: request_firmware_nowait failed: %d\n", ret);
271                 goto error4;
272         }
273
274         hdmi->ops->hdcp_cb = rockchip_hdmiv2_hdcp_start;
275         return 0;
276
277 error4:
278         device_remove_file(mdev.this_device, &dev_attr_trytimes);
279 error3:
280         device_remove_file(mdev.this_device, &dev_attr_enable);
281 error2:
282         misc_deregister(&mdev);
283 error1:
284         kfree(hdcp->keys);
285         kfree(hdcp->invalidkeys);
286         kfree(hdcp);
287 error0:
288         return ret;
289 }
290
291 void rockchip_hdmiv2_hdcp_init(struct hdmi *hdmi)
292 {
293         pr_info("%s", __func__);
294         if (hdcp == NULL)
295                 hdcp_init(hdmi);
296         else
297                 hdcp_load_key(hdmi, hdcp->keys);
298 }
299