Hans de Goede | 1b46f17 | 2017-12-12 17:40:54 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * GPD Pocket fan controller driver |
| 4 | * |
| 5 | * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/acpi.h> |
| 9 | #include <linux/gpio/consumer.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/moduleparam.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/thermal.h> |
| 14 | #include <linux/workqueue.h> |
| 15 | |
| 16 | static int temp_limits[3] = { 55000, 60000, 65000 }; |
| 17 | module_param_array(temp_limits, int, NULL, 0444); |
| 18 | MODULE_PARM_DESC(temp_limits, |
| 19 | "Milli-celcius values above which the fan speed increases"); |
| 20 | |
| 21 | static int hysteresis = 3000; |
| 22 | module_param(hysteresis, int, 0444); |
| 23 | MODULE_PARM_DESC(hysteresis, |
| 24 | "Hysteresis in milli-celcius before lowering the fan speed"); |
| 25 | |
| 26 | struct gpd_pocket_fan_data { |
| 27 | struct device *dev; |
| 28 | struct thermal_zone_device *dts0; |
| 29 | struct thermal_zone_device *dts1; |
| 30 | struct gpio_desc *gpio0; |
| 31 | struct gpio_desc *gpio1; |
| 32 | struct delayed_work work; |
| 33 | int last_speed; |
| 34 | }; |
| 35 | |
| 36 | static void gpd_pocket_fan_set_speed(struct gpd_pocket_fan_data *fan, int speed) |
| 37 | { |
| 38 | if (speed == fan->last_speed) |
| 39 | return; |
| 40 | |
| 41 | gpiod_direction_output(fan->gpio0, !!(speed & 1)); |
| 42 | gpiod_direction_output(fan->gpio1, !!(speed & 2)); |
| 43 | |
| 44 | fan->last_speed = speed; |
| 45 | } |
| 46 | |
| 47 | static void gpd_pocket_fan_worker(struct work_struct *work) |
| 48 | { |
| 49 | struct gpd_pocket_fan_data *fan = |
| 50 | container_of(work, struct gpd_pocket_fan_data, work.work); |
| 51 | int t0, t1, temp, speed, i; |
| 52 | |
| 53 | if (thermal_zone_get_temp(fan->dts0, &t0) || |
| 54 | thermal_zone_get_temp(fan->dts1, &t1)) { |
| 55 | dev_warn(fan->dev, "Error getting temperature\n"); |
| 56 | queue_delayed_work(system_wq, &fan->work, |
| 57 | msecs_to_jiffies(1000)); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | temp = max(t0, t1); |
| 62 | |
| 63 | speed = fan->last_speed; |
| 64 | |
| 65 | /* Determine minimum speed */ |
| 66 | for (i = 0; i < ARRAY_SIZE(temp_limits); i++) { |
| 67 | if (temp < temp_limits[i]) |
| 68 | break; |
| 69 | } |
| 70 | if (speed < i) |
| 71 | speed = i; |
| 72 | |
| 73 | /* Use hysteresis before lowering speed again */ |
| 74 | for (i = 0; i < ARRAY_SIZE(temp_limits); i++) { |
| 75 | if (temp <= (temp_limits[i] - hysteresis)) |
| 76 | break; |
| 77 | } |
| 78 | if (speed > i) |
| 79 | speed = i; |
| 80 | |
| 81 | if (fan->last_speed <= 0 && speed) |
| 82 | speed = 3; /* kick start motor */ |
| 83 | |
| 84 | gpd_pocket_fan_set_speed(fan, speed); |
| 85 | |
| 86 | /* When mostly idle (low temp/speed), slow down the poll interval. */ |
| 87 | queue_delayed_work(system_wq, &fan->work, |
| 88 | msecs_to_jiffies(4000 / (speed + 1))); |
| 89 | } |
| 90 | |
| 91 | static void gpd_pocket_fan_force_update(struct gpd_pocket_fan_data *fan) |
| 92 | { |
| 93 | fan->last_speed = -1; |
| 94 | mod_delayed_work(system_wq, &fan->work, 0); |
| 95 | } |
| 96 | |
| 97 | static int gpd_pocket_fan_probe(struct platform_device *pdev) |
| 98 | { |
| 99 | struct gpd_pocket_fan_data *fan; |
| 100 | int i; |
| 101 | |
| 102 | for (i = 0; i < ARRAY_SIZE(temp_limits); i++) { |
| 103 | if (temp_limits[i] < 40000 || temp_limits[i] > 70000) { |
| 104 | dev_err(&pdev->dev, "Invalid temp-limit %d (must be between 40000 and 70000)\n", |
| 105 | temp_limits[i]); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | } |
| 109 | if (hysteresis < 1000 || hysteresis > 10000) { |
| 110 | dev_err(&pdev->dev, "Invalid hysteresis %d (must be between 1000 and 10000)\n", |
| 111 | hysteresis); |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
| 115 | fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL); |
| 116 | if (!fan) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | fan->dev = &pdev->dev; |
| 120 | INIT_DELAYED_WORK(&fan->work, gpd_pocket_fan_worker); |
| 121 | |
| 122 | /* Note this returns a "weak" reference which we don't need to free */ |
| 123 | fan->dts0 = thermal_zone_get_zone_by_name("soc_dts0"); |
| 124 | if (IS_ERR(fan->dts0)) |
| 125 | return -EPROBE_DEFER; |
| 126 | |
| 127 | fan->dts1 = thermal_zone_get_zone_by_name("soc_dts1"); |
| 128 | if (IS_ERR(fan->dts1)) |
| 129 | return -EPROBE_DEFER; |
| 130 | |
| 131 | fan->gpio0 = devm_gpiod_get_index(fan->dev, NULL, 0, GPIOD_ASIS); |
| 132 | if (IS_ERR(fan->gpio0)) |
| 133 | return PTR_ERR(fan->gpio0); |
| 134 | |
| 135 | fan->gpio1 = devm_gpiod_get_index(fan->dev, NULL, 1, GPIOD_ASIS); |
| 136 | if (IS_ERR(fan->gpio1)) |
| 137 | return PTR_ERR(fan->gpio1); |
| 138 | |
| 139 | gpd_pocket_fan_force_update(fan); |
| 140 | |
| 141 | platform_set_drvdata(pdev, fan); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int gpd_pocket_fan_remove(struct platform_device *pdev) |
| 146 | { |
| 147 | struct gpd_pocket_fan_data *fan = platform_get_drvdata(pdev); |
| 148 | |
| 149 | cancel_delayed_work_sync(&fan->work); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | #ifdef CONFIG_PM_SLEEP |
| 154 | static int gpd_pocket_fan_suspend(struct device *dev) |
| 155 | { |
| 156 | struct gpd_pocket_fan_data *fan = dev_get_drvdata(dev); |
| 157 | |
| 158 | gpd_pocket_fan_set_speed(fan, 0); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int gpd_pocket_fan_resume(struct device *dev) |
| 163 | { |
| 164 | struct gpd_pocket_fan_data *fan = dev_get_drvdata(dev); |
| 165 | |
| 166 | gpd_pocket_fan_force_update(fan); |
| 167 | return 0; |
| 168 | } |
| 169 | #endif |
| 170 | static SIMPLE_DEV_PM_OPS(gpd_pocket_fan_pm_ops, |
| 171 | gpd_pocket_fan_suspend, |
| 172 | gpd_pocket_fan_resume); |
| 173 | |
| 174 | static struct acpi_device_id gpd_pocket_fan_acpi_match[] = { |
| 175 | { "FAN02501" }, |
| 176 | {}, |
| 177 | }; |
| 178 | MODULE_DEVICE_TABLE(acpi, gpd_pocket_fan_acpi_match); |
| 179 | |
| 180 | static struct platform_driver gpd_pocket_fan_driver = { |
| 181 | .probe = gpd_pocket_fan_probe, |
| 182 | .remove = gpd_pocket_fan_remove, |
| 183 | .driver = { |
| 184 | .name = "gpd_pocket_fan", |
| 185 | .acpi_match_table = gpd_pocket_fan_acpi_match, |
| 186 | .pm = &gpd_pocket_fan_pm_ops, |
| 187 | }, |
| 188 | }; |
| 189 | |
| 190 | module_platform_driver(gpd_pocket_fan_driver); |
| 191 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com"); |
| 192 | MODULE_DESCRIPTION("GPD pocket fan driver"); |
| 193 | MODULE_LICENSE("GPL"); |