Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * GPD Pocket fan controller driver
0004  *
0005  * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
0006  */
0007 
0008 #include <linux/acpi.h>
0009 #include <linux/devm-helpers.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/module.h>
0012 #include <linux/moduleparam.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/power_supply.h>
0015 #include <linux/thermal.h>
0016 #include <linux/workqueue.h>
0017 
0018 #define MAX_SPEED 3
0019 
0020 #define TEMP_LIMIT0_DEFAULT 55000
0021 #define TEMP_LIMIT1_DEFAULT 60000
0022 #define TEMP_LIMIT2_DEFAULT 65000
0023 
0024 #define HYSTERESIS_DEFAULT  3000
0025 
0026 #define SPEED_ON_AC_DEFAULT 2
0027 
0028 static int temp_limits[3] = {
0029     TEMP_LIMIT0_DEFAULT, TEMP_LIMIT1_DEFAULT, TEMP_LIMIT2_DEFAULT,
0030 };
0031 module_param_array(temp_limits, int, NULL, 0444);
0032 MODULE_PARM_DESC(temp_limits,
0033          "Millicelsius values above which the fan speed increases");
0034 
0035 static int hysteresis = HYSTERESIS_DEFAULT;
0036 module_param(hysteresis, int, 0444);
0037 MODULE_PARM_DESC(hysteresis,
0038          "Hysteresis in millicelsius before lowering the fan speed");
0039 
0040 static int speed_on_ac = SPEED_ON_AC_DEFAULT;
0041 module_param(speed_on_ac, int, 0444);
0042 MODULE_PARM_DESC(speed_on_ac,
0043          "minimum fan speed to allow when system is powered by AC");
0044 
0045 struct gpd_pocket_fan_data {
0046     struct device *dev;
0047     struct thermal_zone_device *dts0;
0048     struct thermal_zone_device *dts1;
0049     struct gpio_desc *gpio0;
0050     struct gpio_desc *gpio1;
0051     struct delayed_work work;
0052     int last_speed;
0053 };
0054 
0055 static void gpd_pocket_fan_set_speed(struct gpd_pocket_fan_data *fan, int speed)
0056 {
0057     if (speed == fan->last_speed)
0058         return;
0059 
0060     gpiod_direction_output(fan->gpio0, !!(speed & 1));
0061     gpiod_direction_output(fan->gpio1, !!(speed & 2));
0062 
0063     fan->last_speed = speed;
0064 }
0065 
0066 static int gpd_pocket_fan_min_speed(void)
0067 {
0068     if (power_supply_is_system_supplied())
0069         return speed_on_ac;
0070     else
0071         return 0;
0072 }
0073 
0074 static void gpd_pocket_fan_worker(struct work_struct *work)
0075 {
0076     struct gpd_pocket_fan_data *fan =
0077         container_of(work, struct gpd_pocket_fan_data, work.work);
0078     int t0, t1, temp, speed, min_speed, i;
0079 
0080     if (thermal_zone_get_temp(fan->dts0, &t0) ||
0081         thermal_zone_get_temp(fan->dts1, &t1)) {
0082         dev_warn(fan->dev, "Error getting temperature\n");
0083         speed = MAX_SPEED;
0084         goto set_speed;
0085     }
0086 
0087     temp = max(t0, t1);
0088 
0089     speed = fan->last_speed;
0090     min_speed = gpd_pocket_fan_min_speed();
0091 
0092     /* Determine minimum speed */
0093     for (i = min_speed; i < ARRAY_SIZE(temp_limits); i++) {
0094         if (temp < temp_limits[i])
0095             break;
0096     }
0097     if (speed < i)
0098         speed = i;
0099 
0100     /* Use hysteresis before lowering speed again */
0101     for (i = min_speed; i < ARRAY_SIZE(temp_limits); i++) {
0102         if (temp <= (temp_limits[i] - hysteresis))
0103             break;
0104     }
0105     if (speed > i)
0106         speed = i;
0107 
0108     if (fan->last_speed <= 0 && speed)
0109         speed = MAX_SPEED; /* kick start motor */
0110 
0111 set_speed:
0112     gpd_pocket_fan_set_speed(fan, speed);
0113 
0114     /* When mostly idle (low temp/speed), slow down the poll interval. */
0115     queue_delayed_work(system_wq, &fan->work,
0116                msecs_to_jiffies(4000 / (speed + 1)));
0117 }
0118 
0119 static void gpd_pocket_fan_force_update(struct gpd_pocket_fan_data *fan)
0120 {
0121     fan->last_speed = -1;
0122     mod_delayed_work(system_wq, &fan->work, 0);
0123 }
0124 
0125 static int gpd_pocket_fan_probe(struct platform_device *pdev)
0126 {
0127     struct gpd_pocket_fan_data *fan;
0128     int i, ret;
0129 
0130     for (i = 0; i < ARRAY_SIZE(temp_limits); i++) {
0131         if (temp_limits[i] < 20000 || temp_limits[i] > 90000) {
0132             dev_err(&pdev->dev, "Invalid temp-limit %d (must be between 20000 and 90000)\n",
0133                 temp_limits[i]);
0134             temp_limits[0] = TEMP_LIMIT0_DEFAULT;
0135             temp_limits[1] = TEMP_LIMIT1_DEFAULT;
0136             temp_limits[2] = TEMP_LIMIT2_DEFAULT;
0137             break;
0138         }
0139     }
0140     if (hysteresis < 1000 || hysteresis > 10000) {
0141         dev_err(&pdev->dev, "Invalid hysteresis %d (must be between 1000 and 10000)\n",
0142             hysteresis);
0143         hysteresis = HYSTERESIS_DEFAULT;
0144     }
0145     if (speed_on_ac < 0 || speed_on_ac > MAX_SPEED) {
0146         dev_err(&pdev->dev, "Invalid speed_on_ac %d (must be between 0 and 3)\n",
0147             speed_on_ac);
0148         speed_on_ac = SPEED_ON_AC_DEFAULT;
0149     }
0150 
0151     fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
0152     if (!fan)
0153         return -ENOMEM;
0154 
0155     fan->dev = &pdev->dev;
0156     ret = devm_delayed_work_autocancel(&pdev->dev, &fan->work,
0157                        gpd_pocket_fan_worker);
0158     if (ret)
0159         return ret;
0160 
0161     /* Note this returns a "weak" reference which we don't need to free */
0162     fan->dts0 = thermal_zone_get_zone_by_name("soc_dts0");
0163     if (IS_ERR(fan->dts0))
0164         return -EPROBE_DEFER;
0165 
0166     fan->dts1 = thermal_zone_get_zone_by_name("soc_dts1");
0167     if (IS_ERR(fan->dts1))
0168         return -EPROBE_DEFER;
0169 
0170     fan->gpio0 = devm_gpiod_get_index(fan->dev, NULL, 0, GPIOD_ASIS);
0171     if (IS_ERR(fan->gpio0))
0172         return PTR_ERR(fan->gpio0);
0173 
0174     fan->gpio1 = devm_gpiod_get_index(fan->dev, NULL, 1, GPIOD_ASIS);
0175     if (IS_ERR(fan->gpio1))
0176         return PTR_ERR(fan->gpio1);
0177 
0178     gpd_pocket_fan_force_update(fan);
0179 
0180     platform_set_drvdata(pdev, fan);
0181     return 0;
0182 }
0183 
0184 #ifdef CONFIG_PM_SLEEP
0185 static int gpd_pocket_fan_suspend(struct device *dev)
0186 {
0187     struct gpd_pocket_fan_data *fan = dev_get_drvdata(dev);
0188 
0189     cancel_delayed_work_sync(&fan->work);
0190     gpd_pocket_fan_set_speed(fan, gpd_pocket_fan_min_speed());
0191     return 0;
0192 }
0193 
0194 static int gpd_pocket_fan_resume(struct device *dev)
0195 {
0196     struct gpd_pocket_fan_data *fan = dev_get_drvdata(dev);
0197 
0198     gpd_pocket_fan_force_update(fan);
0199     return 0;
0200 }
0201 #endif
0202 static SIMPLE_DEV_PM_OPS(gpd_pocket_fan_pm_ops,
0203              gpd_pocket_fan_suspend,
0204              gpd_pocket_fan_resume);
0205 
0206 static struct acpi_device_id gpd_pocket_fan_acpi_match[] = {
0207     { "FAN02501" },
0208     {},
0209 };
0210 MODULE_DEVICE_TABLE(acpi, gpd_pocket_fan_acpi_match);
0211 
0212 static struct platform_driver gpd_pocket_fan_driver = {
0213     .probe  = gpd_pocket_fan_probe,
0214     .driver = {
0215         .name           = "gpd_pocket_fan",
0216         .acpi_match_table   = gpd_pocket_fan_acpi_match,
0217         .pm         = &gpd_pocket_fan_pm_ops,
0218      },
0219 };
0220 
0221 module_platform_driver(gpd_pocket_fan_driver);
0222 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com");
0223 MODULE_DESCRIPTION("GPD pocket fan driver");
0224 MODULE_LICENSE("GPL");