^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2014 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Kamil Debski <k.debski@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <soc/rockchip/rockchip_system_monitor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define MAX_PWM 255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct thermal_trips {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct pwm_fan_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct pwm_device *pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct regulator *reg_en;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) atomic_t pulses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int rpm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u8 pulses_per_revolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ktime_t sample_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct timer_list rpm_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int pwm_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int pwm_fan_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int pwm_fan_max_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned int *pwm_fan_cooling_levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct thermal_cooling_device *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct notifier_block thermal_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct thermal_trips *thermal_trips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) bool thermal_notifier_is_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* This handler assumes self resetting edge triggered interrupt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static irqreturn_t pulse_handler(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct pwm_fan_ctx *ctx = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) atomic_inc(&ctx->pulses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void sample_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int pulses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) pulses = atomic_read(&ctx->pulses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) atomic_sub(pulses, &ctx->pulses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) (ctx->pulses_per_revolution * delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ctx->sample_start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) mod_timer(&ctx->rpm_timer, jiffies + HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned long period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct pwm_state state = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mutex_lock(&ctx->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (ctx->pwm_value == pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) goto exit_set_pwm_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pwm_init_state(ctx->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) period = ctx->pwm->args.period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) state.enabled = pwm ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ret = pwm_apply_state(ctx->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ctx->pwm_value = pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) exit_set_pwm_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) mutex_unlock(&ctx->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) for (i = 0; i < ctx->pwm_fan_max_state; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ctx->pwm_fan_state = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unsigned long pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ret = __set_pwm(ctx, pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) pwm_fan_update_state(ctx, pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return sprintf(buf, "%u\n", ctx->pwm_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static ssize_t rpm_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return sprintf(buf, "%u\n", ctx->rpm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static struct attribute *pwm_fan_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) &sensor_dev_attr_pwm1.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) &sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct device *dev = container_of(kobj, struct device, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* Hide fan_input in case no interrupt is available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (n == 1 && ctx->irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return a->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static const struct attribute_group pwm_fan_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .attrs = pwm_fan_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .is_visible = pwm_fan_attrs_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static const struct attribute_group *pwm_fan_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) &pwm_fan_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* thermal cooling device callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned long *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct pwm_fan_ctx *ctx = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) *state = ctx->pwm_fan_max_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) unsigned long *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct pwm_fan_ctx *ctx = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) *state = ctx->pwm_fan_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct pwm_fan_ctx *ctx = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!ctx || (state > ctx->pwm_fan_max_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (state == ctx->pwm_fan_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) dev_err(&cdev->device, "Cannot set pwm!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) ctx->pwm_fan_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .get_max_state = pwm_fan_get_max_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .get_cur_state = pwm_fan_get_cur_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) .set_cur_state = pwm_fan_set_cur_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int pwm_fan_of_get_cooling_data(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct pwm_fan_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int num, i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!of_find_property(np, "cooling-levels", NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) ret = of_property_count_u32_elems(np, "cooling-levels");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) dev_err(dev, "Wrong data!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return ret ? : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) num = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (!ctx->pwm_fan_cooling_levels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) ret = of_property_read_u32_array(np, "cooling-levels",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) ctx->pwm_fan_cooling_levels, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ctx->pwm_fan_cooling_levels[i], MAX_PWM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ctx->pwm_fan_max_state = num - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static void pwm_fan_regulator_disable(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) regulator_disable(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static void pwm_fan_pwm_disable(void *__ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct pwm_fan_ctx *ctx = __ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) pwm_disable(ctx->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) del_timer_sync(&ctx->rpm_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int pwm_fan_get_thermal_trips(struct device *dev, char *porp_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct thermal_trips **trips)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct thermal_trips *thermal_trips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) const struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int count, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) prop = of_find_property(np, porp_name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!prop->value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) count = of_property_count_u32_elems(np, porp_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (count < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (count % 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) thermal_trips = devm_kzalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) sizeof(*thermal_trips) * (count / 2 + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (!thermal_trips)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) for (i = 0; i < count / 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) of_property_read_u32_index(np, porp_name, 2 * i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) &thermal_trips[i].temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) of_property_read_u32_index(np, porp_name, 2 * i + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) &thermal_trips[i].state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) thermal_trips[i].temp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) thermal_trips[i].state = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) *trips = thermal_trips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int pwm_fan_temp_to_state(struct pwm_fan_ctx *ctx, int temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct thermal_trips *trips = ctx->thermal_trips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int i, state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) for (i = 0; trips[i].state != INT_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (temp >= trips[i].temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) state = trips[i].state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static int pwm_fan_thermal_notifier_call(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) unsigned long event, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct pwm_fan_ctx *ctx = container_of(nb, struct pwm_fan_ctx, thermal_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct system_monitor_event_data *event_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int state, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (event != SYSTEM_MONITOR_CHANGE_TEMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) state = pwm_fan_temp_to_state(ctx, event_data->temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (state > ctx->pwm_fan_max_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return NOTIFY_BAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (state == ctx->pwm_fan_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return NOTIFY_BAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) ctx->pwm_fan_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int pwm_fan_register_thermal_notifier(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct pwm_fan_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (pwm_fan_get_thermal_trips(dev, "rockchip,temp-trips",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) &ctx->thermal_trips))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) ctx->thermal_nb.notifier_call = pwm_fan_thermal_notifier_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return rockchip_system_monitor_register_notifier(&ctx->thermal_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static int pwm_fan_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct thermal_cooling_device *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct pwm_fan_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct device *hwmon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct pwm_state state = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) u32 ppr = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) mutex_init(&ctx->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (IS_ERR(ctx->pwm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) platform_set_drvdata(pdev, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) ctx->irq = platform_get_irq_optional(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (ctx->irq == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return ctx->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ctx->reg_en = devm_regulator_get_optional(dev, "fan");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (IS_ERR(ctx->reg_en)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (PTR_ERR(ctx->reg_en) != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return PTR_ERR(ctx->reg_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ctx->reg_en = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) ret = regulator_enable(ctx->reg_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) dev_err(dev, "Failed to enable fan supply: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ctx->reg_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) ctx->pwm_value = MAX_PWM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) pwm_init_state(ctx->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * long. Check this here to prevent the fan running at a too low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (state.period > ULONG_MAX / MAX_PWM + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) dev_err(dev, "Configured period too big\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* Set duty cycle to maximum allowed and enable PWM output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) state.duty_cycle = ctx->pwm->args.period - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) state.enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) ret = pwm_apply_state(ctx->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) dev_err(dev, "Failed to configure PWM: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) timer_setup(&ctx->rpm_timer, sample_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) ctx->pulses_per_revolution = ppr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (!ctx->pulses_per_revolution) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) dev_err(dev, "pulses-per-revolution can't be zero.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (ctx->irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) pdev->name, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) dev_err(dev, "Failed to request interrupt: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ctx->sample_start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) mod_timer(&ctx->rpm_timer, jiffies + HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) ctx, pwm_fan_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (IS_ERR(hwmon)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) dev_err(dev, "Failed to register hwmon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return PTR_ERR(hwmon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) ret = pwm_fan_of_get_cooling_data(dev, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) ctx->pwm_fan_state = ctx->pwm_fan_max_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (IS_REACHABLE(CONFIG_ROCKCHIP_SYSTEM_MONITOR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) of_find_property(dev->of_node, "rockchip,temp-trips", NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) ret = pwm_fan_register_thermal_notifier(dev, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) dev_err(dev, "Failed to register thermal notifier: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ctx->thermal_notifier_is_ok = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (IS_ENABLED(CONFIG_THERMAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) cdev = devm_thermal_of_cooling_device_register(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (IS_ERR(cdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) ret = PTR_ERR(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) "Failed to register pwm-fan as cooling device: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) ctx->cdev = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) thermal_cdev_update(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) static int pwm_fan_disable(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct pwm_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) pwm_get_args(ctx->pwm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (ctx->pwm_value || ctx->thermal_notifier_is_ok) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) ret = pwm_config(ctx->pwm, 0, args.period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) pwm_disable(ctx->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (ctx->reg_en) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) ret = regulator_disable(ctx->reg_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) dev_err(dev, "Failed to disable fan supply: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static void pwm_fan_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) pwm_fan_disable(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static int pwm_fan_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return pwm_fan_disable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) static int pwm_fan_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) struct pwm_args pargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) unsigned long duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (ctx->reg_en) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) ret = regulator_enable(ctx->reg_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) dev_err(dev, "Failed to enable fan supply: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (ctx->pwm_value == 0 && !ctx->thermal_notifier_is_ok)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) pwm_get_args(ctx->pwm, &pargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) duty = DIV_ROUND_UP_ULL(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) ret = pwm_config(ctx->pwm, duty, pargs.period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return pwm_enable(ctx->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) static const struct of_device_id of_pwm_fan_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) { .compatible = "pwm-fan", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static struct platform_driver pwm_fan_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) .probe = pwm_fan_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) .shutdown = pwm_fan_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) .name = "pwm-fan",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) .pm = &pwm_fan_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) .of_match_table = of_pwm_fan_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) module_platform_driver(pwm_fan_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) MODULE_ALIAS("platform:pwm-fan");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) MODULE_DESCRIPTION("PWM FAN driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) MODULE_LICENSE("GPL");