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