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 acpi_device *acpi_dev = dev_get_drvdata(dev);\ 35 unsigned long long val;\ 36 acpi_status status;\ 37 \ 38 status = acpi_evaluate_integer(acpi_dev->handle, #object,\ 39 NULL, &val);\ 40 if (ACPI_SUCCESS(status))\ 41 return sprintf(buf, "%d\n", (int)val);\ 42 else \ 43 return -EINVAL;\ 44 } 45 46 DPTF_POWER_SHOW(max_platform_power_mw, PMAX) 47 DPTF_POWER_SHOW(platform_power_source, PSRC) 48 DPTF_POWER_SHOW(adapter_rating_mw, ARTG) 49 DPTF_POWER_SHOW(battery_steady_power_mw, PBSS) 50 DPTF_POWER_SHOW(charger_type, CTYP) 51 52 static DEVICE_ATTR_RO(max_platform_power_mw); 53 static DEVICE_ATTR_RO(platform_power_source); 54 static DEVICE_ATTR_RO(adapter_rating_mw); 55 static DEVICE_ATTR_RO(battery_steady_power_mw); 56 static DEVICE_ATTR_RO(charger_type); 57 58 static struct attribute *dptf_power_attrs[] = { 59 &dev_attr_max_platform_power_mw.attr, 60 &dev_attr_platform_power_source.attr, 61 &dev_attr_adapter_rating_mw.attr, 62 &dev_attr_battery_steady_power_mw.attr, 63 &dev_attr_charger_type.attr, 64 NULL 65 }; 66 67 static const struct attribute_group dptf_power_attribute_group = { 68 .attrs = dptf_power_attrs, 69 .name = "dptf_power" 70 }; 71 72 static int dptf_power_add(struct platform_device *pdev) 73 { 74 struct acpi_device *acpi_dev; 75 acpi_status status; 76 unsigned long long ptype; 77 int result; 78 79 acpi_dev = ACPI_COMPANION(&(pdev->dev)); 80 if (!acpi_dev) 81 return -ENODEV; 82 83 status = acpi_evaluate_integer(acpi_dev->handle, "PTYP", NULL, &ptype); 84 if (ACPI_FAILURE(status)) 85 return -ENODEV; 86 87 if (ptype != 0x11) 88 return -ENODEV; 89 90 result = sysfs_create_group(&pdev->dev.kobj, 91 &dptf_power_attribute_group); 92 if (result) 93 return result; 94 95 platform_set_drvdata(pdev, acpi_dev); 96 97 return 0; 98 } 99 100 static int dptf_power_remove(struct platform_device *pdev) 101 { 102 103 sysfs_remove_group(&pdev->dev.kobj, &dptf_power_attribute_group); 104 105 return 0; 106 } 107 108 static const struct acpi_device_id int3407_device_ids[] = { 109 {"INT3407", 0}, 110 {"", 0}, 111 }; 112 MODULE_DEVICE_TABLE(acpi, int3407_device_ids); 113 114 static struct platform_driver dptf_power_driver = { 115 .probe = dptf_power_add, 116 .remove = dptf_power_remove, 117 .driver = { 118 .name = "DPTF Platform Power", 119 .acpi_match_table = int3407_device_ids, 120 }, 121 }; 122 123 module_platform_driver(dptf_power_driver); 124 125 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 126 MODULE_LICENSE("GPL v2"); 127 MODULE_DESCRIPTION("ACPI DPTF platform power driver"); 128