nds32: fix build error "relocation truncated to fit: R_NDS32_25_PCREL_RELA" when
[linux-2.6-microblaze.git] / drivers / usb / typec / ucsi / ucsi_acpi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * UCSI ACPI driver
4  *
5  * Copyright (C) 2017, Intel Corporation
6  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7  */
8
9 #include <linux/platform_device.h>
10 #include <linux/module.h>
11 #include <linux/acpi.h>
12
13 #include "ucsi.h"
14
15 #define UCSI_DSM_UUID           "6f8398c2-7ca4-11e4-ad36-631042b5008f"
16 #define UCSI_DSM_FUNC_WRITE     1
17 #define UCSI_DSM_FUNC_READ      2
18
19 struct ucsi_acpi {
20         struct device *dev;
21         struct ucsi *ucsi;
22         struct ucsi_ppm ppm;
23         guid_t guid;
24 };
25
26 static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func)
27 {
28         union acpi_object *obj;
29
30         obj = acpi_evaluate_dsm(ACPI_HANDLE(ua->dev), &ua->guid, 1, func,
31                                 NULL);
32         if (!obj) {
33                 dev_err(ua->dev, "%s: failed to evaluate _DSM %d\n",
34                         __func__, func);
35                 return -EIO;
36         }
37
38         ACPI_FREE(obj);
39         return 0;
40 }
41
42 static int ucsi_acpi_cmd(struct ucsi_ppm *ppm, struct ucsi_control *ctrl)
43 {
44         struct ucsi_acpi *ua = container_of(ppm, struct ucsi_acpi, ppm);
45
46         ppm->data->ctrl.raw_cmd = ctrl->raw_cmd;
47
48         return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
49 }
50
51 static int ucsi_acpi_sync(struct ucsi_ppm *ppm)
52 {
53         struct ucsi_acpi *ua = container_of(ppm, struct ucsi_acpi, ppm);
54
55         return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
56 }
57
58 static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
59 {
60         struct ucsi_acpi *ua = data;
61
62         ucsi_notify(ua->ucsi);
63 }
64
65 static int ucsi_acpi_probe(struct platform_device *pdev)
66 {
67         struct ucsi_acpi *ua;
68         struct resource *res;
69         acpi_status status;
70         int ret;
71
72         ua = devm_kzalloc(&pdev->dev, sizeof(*ua), GFP_KERNEL);
73         if (!ua)
74                 return -ENOMEM;
75
76         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
77         if (!res) {
78                 dev_err(&pdev->dev, "missing memory resource\n");
79                 return -ENODEV;
80         }
81
82         /*
83          * NOTE: The memory region for the data structures is used also in an
84          * operation region, which means ACPI has already reserved it. Therefore
85          * it can not be requested here, and we can not use
86          * devm_ioremap_resource().
87          */
88         ua->ppm.data = devm_ioremap(&pdev->dev, res->start, resource_size(res));
89         if (!ua->ppm.data)
90                 return -ENOMEM;
91
92         if (!ua->ppm.data->version)
93                 return -ENODEV;
94
95         ret = guid_parse(UCSI_DSM_UUID, &ua->guid);
96         if (ret)
97                 return ret;
98
99         ua->ppm.cmd = ucsi_acpi_cmd;
100         ua->ppm.sync = ucsi_acpi_sync;
101         ua->dev = &pdev->dev;
102
103         status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
104                                              ACPI_DEVICE_NOTIFY,
105                                              ucsi_acpi_notify, ua);
106         if (ACPI_FAILURE(status)) {
107                 dev_err(&pdev->dev, "failed to install notify handler\n");
108                 return -ENODEV;
109         }
110
111         ua->ucsi = ucsi_register_ppm(&pdev->dev, &ua->ppm);
112         if (IS_ERR(ua->ucsi)) {
113                 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
114                                            ACPI_DEVICE_NOTIFY,
115                                            ucsi_acpi_notify);
116                 return PTR_ERR(ua->ucsi);
117         }
118
119         platform_set_drvdata(pdev, ua);
120
121         return 0;
122 }
123
124 static int ucsi_acpi_remove(struct platform_device *pdev)
125 {
126         struct ucsi_acpi *ua = platform_get_drvdata(pdev);
127
128         ucsi_unregister_ppm(ua->ucsi);
129
130         acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY,
131                                    ucsi_acpi_notify);
132
133         return 0;
134 }
135
136 static const struct acpi_device_id ucsi_acpi_match[] = {
137         { "PNP0CA0", 0 },
138         { },
139 };
140 MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
141
142 static struct platform_driver ucsi_acpi_platform_driver = {
143         .driver = {
144                 .name = "ucsi_acpi",
145                 .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
146         },
147         .probe = ucsi_acpi_probe,
148         .remove = ucsi_acpi_remove,
149 };
150
151 module_platform_driver(ucsi_acpi_platform_driver);
152
153 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
154 MODULE_LICENSE("GPL v2");
155 MODULE_DESCRIPTION("UCSI ACPI driver");