Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $) |
| 3 | * |
| 4 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
| 6 | * |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or (at |
| 12 | * your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/uaccess.h> |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 31 | #include <linux/thermal.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 32 | #include <linux/acpi.h> |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 33 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 35 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 36 | MODULE_DESCRIPTION("ACPI Fan Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | MODULE_LICENSE("GPL"); |
| 38 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 39 | static int acpi_fan_probe(struct platform_device *pdev); |
| 40 | static int acpi_fan_remove(struct platform_device *pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 42 | static const struct acpi_device_id fan_device_ids[] = { |
| 43 | {"PNP0C0B", 0}, |
| 44 | {"", 0}, |
| 45 | }; |
| 46 | MODULE_DEVICE_TABLE(acpi, fan_device_ids); |
| 47 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 62fcbdd | 2012-06-27 23:26:07 +0200 | [diff] [blame] | 49 | static int acpi_fan_suspend(struct device *dev); |
| 50 | static int acpi_fan_resume(struct device *dev); |
Aaron Lu | b9b8515 | 2014-02-19 12:16:48 +0800 | [diff] [blame] | 51 | static struct dev_pm_ops acpi_fan_pm = { |
| 52 | .resume = acpi_fan_resume, |
| 53 | .freeze = acpi_fan_suspend, |
| 54 | .thaw = acpi_fan_resume, |
| 55 | .restore = acpi_fan_resume, |
| 56 | }; |
| 57 | #define FAN_PM_OPS_PTR (&acpi_fan_pm) |
Shuah Khan | b108e0e | 2014-02-12 20:19:08 -0700 | [diff] [blame] | 58 | #else |
Aaron Lu | b9b8515 | 2014-02-19 12:16:48 +0800 | [diff] [blame] | 59 | #define FAN_PM_OPS_PTR NULL |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 60 | #endif |
Rafael J. Wysocki | 62fcbdd | 2012-06-27 23:26:07 +0200 | [diff] [blame] | 61 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 62 | static struct platform_driver acpi_fan_driver = { |
| 63 | .probe = acpi_fan_probe, |
| 64 | .remove = acpi_fan_remove, |
| 65 | .driver = { |
| 66 | .name = "acpi-fan", |
| 67 | .acpi_match_table = fan_device_ids, |
| 68 | .pm = FAN_PM_OPS_PTR, |
| 69 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 72 | /* thermal cooling device callbacks */ |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 73 | static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long |
| 74 | *state) |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 75 | { |
| 76 | /* ACPI fan device only support two states: ON/OFF */ |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 77 | *state = 1; |
| 78 | return 0; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 79 | } |
| 80 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 81 | static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long |
| 82 | *state) |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 83 | { |
| 84 | struct acpi_device *device = cdev->devdata; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 85 | int result; |
Naresh Bhat | 85eb982 | 2013-07-01 19:51:00 +0530 | [diff] [blame] | 86 | int acpi_state = ACPI_STATE_D0; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 87 | |
| 88 | if (!device) |
| 89 | return -EINVAL; |
| 90 | |
Aaron Lu | 2bb3a2b | 2013-11-19 15:43:52 +0800 | [diff] [blame] | 91 | result = acpi_device_update_power(device, &acpi_state); |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 92 | if (result) |
| 93 | return result; |
| 94 | |
Rafael J. Wysocki | 8ad928d | 2013-07-30 14:36:20 +0200 | [diff] [blame] | 95 | *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 : |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 96 | (acpi_state == ACPI_STATE_D0 ? 1 : -1)); |
| 97 | return 0; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static int |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 101 | fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 102 | { |
| 103 | struct acpi_device *device = cdev->devdata; |
| 104 | int result; |
| 105 | |
| 106 | if (!device || (state != 0 && state != 1)) |
| 107 | return -EINVAL; |
| 108 | |
Aaron Lu | 2bb3a2b | 2013-11-19 15:43:52 +0800 | [diff] [blame] | 109 | result = acpi_device_set_power(device, |
Rafael J. Wysocki | 8ad928d | 2013-07-30 14:36:20 +0200 | [diff] [blame] | 110 | state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD); |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 111 | |
| 112 | return result; |
| 113 | } |
| 114 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 115 | static const struct thermal_cooling_device_ops fan_cooling_ops = { |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 116 | .get_max_state = fan_get_max_state, |
| 117 | .get_cur_state = fan_get_cur_state, |
| 118 | .set_cur_state = fan_set_cur_state, |
| 119 | }; |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* -------------------------------------------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | Driver Interface |
| 123 | -------------------------------------------------------------------------- */ |
| 124 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 125 | static int acpi_fan_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 127 | int result = 0; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 128 | struct thermal_cooling_device *cdev; |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 129 | struct acpi_device *device = ACPI_COMPANION(&pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Aaron Lu | 2bb3a2b | 2013-11-19 15:43:52 +0800 | [diff] [blame] | 131 | result = acpi_device_update_power(device, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (result) { |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 133 | dev_err(&pdev->dev, "Setting initial power state\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | goto end; |
| 135 | } |
| 136 | |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 137 | cdev = thermal_cooling_device_register("Fan", device, |
| 138 | &fan_cooling_ops); |
Thomas Sujith | 19b3678 | 2008-02-15 01:01:52 -0500 | [diff] [blame] | 139 | if (IS_ERR(cdev)) { |
| 140 | result = PTR_ERR(cdev); |
| 141 | goto end; |
| 142 | } |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 143 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 144 | dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id); |
Thomas Sujith | 19b3678 | 2008-02-15 01:01:52 -0500 | [diff] [blame] | 145 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 146 | platform_set_drvdata(pdev, cdev); |
| 147 | result = sysfs_create_link(&pdev->dev.kobj, |
Julia Lawall | 9030062 | 2008-04-11 10:09:24 +0800 | [diff] [blame] | 148 | &cdev->device.kobj, |
| 149 | "thermal_cooling"); |
| 150 | if (result) |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 151 | dev_err(&pdev->dev, "Failed to create sysfs link " |
Greg Kroah-Hartman | fc3a882 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 152 | "'thermal_cooling'\n"); |
Julia Lawall | 9030062 | 2008-04-11 10:09:24 +0800 | [diff] [blame] | 153 | |
| 154 | result = sysfs_create_link(&cdev->device.kobj, |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 155 | &pdev->dev.kobj, |
Julia Lawall | 9030062 | 2008-04-11 10:09:24 +0800 | [diff] [blame] | 156 | "device"); |
| 157 | if (result) |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 158 | dev_err(&pdev->dev, "Failed to create sysfs link " |
Greg Kroah-Hartman | fc3a882 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 159 | "'device'\n"); |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 160 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 161 | dev_info(&pdev->dev, "%s [%s] (%s)\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 162 | acpi_device_name(device), acpi_device_bid(device), |
| 163 | !device->power.state ? "on" : "off"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Felipe Contreras | 568b6ad | 2013-08-30 16:16:05 -0500 | [diff] [blame] | 165 | end: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 166 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 169 | static int acpi_fan_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 171 | struct thermal_cooling_device *cdev = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 173 | sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling"); |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 174 | sysfs_remove_link(&cdev->device.kobj, "device"); |
| 175 | thermal_cooling_device_unregister(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 177 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 180 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 62fcbdd | 2012-06-27 23:26:07 +0200 | [diff] [blame] | 181 | static int acpi_fan_suspend(struct device *dev) |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 182 | { |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 183 | acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0); |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 184 | |
| 185 | return AE_OK; |
| 186 | } |
| 187 | |
Rafael J. Wysocki | 62fcbdd | 2012-06-27 23:26:07 +0200 | [diff] [blame] | 188 | static int acpi_fan_resume(struct device *dev) |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 189 | { |
Rafael J. Wysocki | 488a76c | 2010-11-25 00:11:24 +0100 | [diff] [blame] | 190 | int result; |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 191 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 192 | result = acpi_device_update_power(ACPI_COMPANION(dev), NULL); |
Rafael J. Wysocki | 488a76c | 2010-11-25 00:11:24 +0100 | [diff] [blame] | 193 | if (result) |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 194 | dev_err(dev, "Error updating fan power state\n"); |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 195 | |
| 196 | return result; |
| 197 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 198 | #endif |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 199 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame^] | 200 | module_platform_driver(acpi_fan_driver); |