1 /* 2 * dptf_power: DPTF platform power driver 3 * Copyright (c) 2016, Intel Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 */ 15 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/acpi.h> 19 #include <linux/platform_device.h> 20 21 /* 22 * Presentation of attributes which are defined for INT3407. They are: 23 * PMAX : Maximum platform powe 24 * PSRC : Platform power source 25 * ARTG : Adapter rating 26 * CTYP : Charger type 27 * PBSS : Battery steady power 28 */ 29 #define DPTF_POWER_SHOW(name, object) \ 30 static ssize_t name##_show(struct device *dev,\ 31 struct device_attribute *attr,\ 32 char *buf)\ 33 {\ 34 struct platform_device *pdev = to_platform_device(dev);\ 35 struct acpi_device *acpi_dev = platform_get_drvdata(pdev);\ 36 unsigned long long val;\ 37 acpi_status status;\ 38 \ 39 status = acpi_evaluate_integer(acpi_dev->handle, #object,\ 40 NULL, &val);\ 41 if (ACPI_SUCCESS(status))\ 42 return sprintf(buf, "%d\n", (int)val);\ 43 else \ 44 return -EINVAL;\ 45 } 46 47 DPTF_POWER_SHOW(max_platform_power_mw, PMAX) 48 DPTF_POWER_SHOW(platform_power_source, PSRC) 49 DPTF_POWER_SHOW(adapter_rating_mw, ARTG) 50 DPTF_POWER_SHOW(battery_steady_power_mw, PBSS) 51 DPTF_POWER_SHOW(charger_type, CTYP) 52 53 static DEVICE_ATTR_RO(max_platform_power_mw); 54 static DEVICE_ATTR_RO(platform_power_source); 55 static DEVICE_ATTR_RO(adapter_rating_mw); 56 static DEVICE_ATTR_RO(battery_steady_power_mw); 57 static DEVICE_ATTR_RO(charger_type); 58 59 static struct attribute *dptf_power_attrs[] = { 60 &dev_attr_max_platform_power_mw.attr, 61 &dev_attr_platform_power_source.attr, 62 &dev_attr_adapter_rating_mw.attr, 63 &dev_attr_battery_steady_power_mw.attr, 64 &dev_attr_charger_type.attr, 65 NULL 66 }; 67 68 static struct attribute_group dptf_power_attribute_group = { 69 .attrs = dptf_power_attrs, 70 .name = "dptf_power" 71 }; 72 73 static int dptf_power_add(struct platform_device *pdev) 74 { 75 struct acpi_device *acpi_dev; 76 acpi_status status; 77 unsigned long long ptype; 78 int result; 79 80 acpi_dev = ACPI_COMPANION(&(pdev->dev)); 81 if (!acpi_dev) 82 return -ENODEV; 83 84 status = acpi_evaluate_integer(acpi_dev->handle, "PTYP", NULL, &ptype); 85 if (ACPI_FAILURE(status)) 86 return -ENODEV; 87 88 if (ptype != 0x11) 89 return -ENODEV; 90 91 result = sysfs_create_group(&pdev->dev.kobj, 92 &dptf_power_attribute_group); 93 if (result) 94 return result; 95 96 platform_set_drvdata(pdev, acpi_dev); 97 98 return 0; 99 } 100 101 static int dptf_power_remove(struct platform_device *pdev) 102 { 103 104 sysfs_remove_group(&pdev->dev.kobj, &dptf_power_attribute_group); 105 106 return 0; 107 } 108 109 static const struct acpi_device_id int3407_device_ids[] = { 110 {"INT3407", 0}, 111 {"", 0}, 112 }; 113 MODULE_DEVICE_TABLE(acpi, int3407_device_ids); 114 115 static struct platform_driver dptf_power_driver = { 116 .probe = dptf_power_add, 117 .remove = dptf_power_remove, 118 .driver = { 119 .name = "DPTF Platform Power", 120 .acpi_match_table = int3407_device_ids, 121 }, 122 }; 123 124 module_platform_driver(dptf_power_driver); 125 126 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 127 MODULE_LICENSE("GPL v2"); 128 MODULE_DESCRIPTION("ACPI DPTF platform power driver"); 129