1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
4  */
5 
6 #define pr_fmt(fmt)	"[drm-dp] %s: " fmt, __func__
7 
8 #include <linux/slab.h>
9 #include <linux/device.h>
10 
11 #include "dp_hpd.h"
12 
13 /* DP specific VDM commands */
14 #define DP_USBPD_VDM_STATUS	0x10
15 #define DP_USBPD_VDM_CONFIGURE	0x11
16 
17 /* USBPD-TypeC specific Macros */
18 #define VDM_VERSION		0x0
19 #define USB_C_DP_SID		0xFF01
20 
21 struct dp_hpd_private {
22 	struct device *dev;
23 	struct dp_usbpd_cb *dp_cb;
24 	struct dp_usbpd dp_usbpd;
25 };
26 
dp_hpd_connect(struct dp_usbpd * dp_usbpd,bool hpd)27 int dp_hpd_connect(struct dp_usbpd *dp_usbpd, bool hpd)
28 {
29 	int rc = 0;
30 	struct dp_hpd_private *hpd_priv;
31 
32 	hpd_priv = container_of(dp_usbpd, struct dp_hpd_private,
33 					dp_usbpd);
34 
35 	dp_usbpd->hpd_high = hpd;
36 
37 	if (!hpd_priv->dp_cb || !hpd_priv->dp_cb->configure
38 				|| !hpd_priv->dp_cb->disconnect) {
39 		pr_err("hpd dp_cb not initialized\n");
40 		return -EINVAL;
41 	}
42 	if (hpd)
43 		hpd_priv->dp_cb->configure(hpd_priv->dev);
44 	else
45 		hpd_priv->dp_cb->disconnect(hpd_priv->dev);
46 
47 	return rc;
48 }
49 
dp_hpd_get(struct device * dev,struct dp_usbpd_cb * cb)50 struct dp_usbpd *dp_hpd_get(struct device *dev, struct dp_usbpd_cb *cb)
51 {
52 	struct dp_hpd_private *dp_hpd;
53 
54 	if (!cb) {
55 		pr_err("invalid cb data\n");
56 		return ERR_PTR(-EINVAL);
57 	}
58 
59 	dp_hpd = devm_kzalloc(dev, sizeof(*dp_hpd), GFP_KERNEL);
60 	if (!dp_hpd)
61 		return ERR_PTR(-ENOMEM);
62 
63 	dp_hpd->dev = dev;
64 	dp_hpd->dp_cb = cb;
65 
66 	dp_hpd->dp_usbpd.connect = dp_hpd_connect;
67 
68 	return &dp_hpd->dp_usbpd;
69 }
70