1/* 2 * pwm-fan.c - Hwmon driver for fans connected to PWM lines. 3 * 4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. 5 * 6 * Author: Kamil Debski <k.debski@samsung.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 */ 18 19#include <linux/hwmon.h> 20#include <linux/hwmon-sysfs.h> 21#include <linux/module.h> 22#include <linux/mutex.h> 23#include <linux/of.h> 24#include <linux/platform_device.h> 25#include <linux/pwm.h> 26#include <linux/sysfs.h> 27 28#define MAX_PWM 255 29 30struct pwm_fan_ctx { 31 struct mutex lock; 32 struct pwm_device *pwm; 33 unsigned char pwm_value; 34}; 35 36static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, 37 const char *buf, size_t count) 38{ 39 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); 40 unsigned long pwm, duty; 41 ssize_t ret; 42 43 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM) 44 return -EINVAL; 45 46 mutex_lock(&ctx->lock); 47 48 if (ctx->pwm_value == pwm) 49 goto exit_set_pwm_no_change; 50 51 if (pwm == 0) { 52 pwm_disable(ctx->pwm); 53 goto exit_set_pwm; 54 } 55 56 duty = DIV_ROUND_UP(pwm * (ctx->pwm->period - 1), MAX_PWM); 57 ret = pwm_config(ctx->pwm, duty, ctx->pwm->period); 58 if (ret) 59 goto exit_set_pwm_err; 60 61 if (ctx->pwm_value == 0) { 62 ret = pwm_enable(ctx->pwm); 63 if (ret) 64 goto exit_set_pwm_err; 65 } 66 67exit_set_pwm: 68 ctx->pwm_value = pwm; 69exit_set_pwm_no_change: 70 ret = count; 71exit_set_pwm_err: 72 mutex_unlock(&ctx->lock); 73 return ret; 74} 75 76static ssize_t show_pwm(struct device *dev, 77 struct device_attribute *attr, char *buf) 78{ 79 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); 80 81 return sprintf(buf, "%u\n", ctx->pwm_value); 82} 83 84 85static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0); 86 87static struct attribute *pwm_fan_attrs[] = { 88 &sensor_dev_attr_pwm1.dev_attr.attr, 89 NULL, 90}; 91 92ATTRIBUTE_GROUPS(pwm_fan); 93 94static int pwm_fan_probe(struct platform_device *pdev) 95{ 96 struct device *hwmon; 97 struct pwm_fan_ctx *ctx; 98 int duty_cycle; 99 int ret; 100 101 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 102 if (!ctx) 103 return -ENOMEM; 104 105 mutex_init(&ctx->lock); 106 107 ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL); 108 if (IS_ERR(ctx->pwm)) { 109 dev_err(&pdev->dev, "Could not get PWM\n"); 110 return PTR_ERR(ctx->pwm); 111 } 112 113 platform_set_drvdata(pdev, ctx); 114 115 /* Set duty cycle to maximum allowed */ 116 duty_cycle = ctx->pwm->period - 1; 117 ctx->pwm_value = MAX_PWM; 118 119 ret = pwm_config(ctx->pwm, duty_cycle, ctx->pwm->period); 120 if (ret) { 121 dev_err(&pdev->dev, "Failed to configure PWM\n"); 122 return ret; 123 } 124 125 /* Enbale PWM output */ 126 ret = pwm_enable(ctx->pwm); 127 if (ret) { 128 dev_err(&pdev->dev, "Failed to enable PWM\n"); 129 return ret; 130 } 131 132 hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan", 133 ctx, pwm_fan_groups); 134 if (IS_ERR(hwmon)) { 135 dev_err(&pdev->dev, "Failed to register hwmon device\n"); 136 pwm_disable(ctx->pwm); 137 return PTR_ERR(hwmon); 138 } 139 return 0; 140} 141 142static int pwm_fan_remove(struct platform_device *pdev) 143{ 144 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); 145 146 if (ctx->pwm_value) 147 pwm_disable(ctx->pwm); 148 return 0; 149} 150 151#ifdef CONFIG_PM_SLEEP 152static int pwm_fan_suspend(struct device *dev) 153{ 154 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); 155 156 if (ctx->pwm_value) 157 pwm_disable(ctx->pwm); 158 return 0; 159} 160 161static int pwm_fan_resume(struct device *dev) 162{ 163 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); 164 unsigned long duty; 165 int ret; 166 167 if (ctx->pwm_value == 0) 168 return 0; 169 170 duty = DIV_ROUND_UP(ctx->pwm_value * (ctx->pwm->period - 1), MAX_PWM); 171 ret = pwm_config(ctx->pwm, duty, ctx->pwm->period); 172 if (ret) 173 return ret; 174 return pwm_enable(ctx->pwm); 175} 176#endif 177 178static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume); 179 180static struct of_device_id of_pwm_fan_match[] = { 181 { .compatible = "pwm-fan", }, 182 {}, 183}; 184 185static struct platform_driver pwm_fan_driver = { 186 .probe = pwm_fan_probe, 187 .remove = pwm_fan_remove, 188 .driver = { 189 .name = "pwm-fan", 190 .pm = &pwm_fan_pm, 191 .of_match_table = of_pwm_fan_match, 192 }, 193}; 194 195module_platform_driver(pwm_fan_driver); 196 197MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>"); 198MODULE_ALIAS("platform:pwm-fan"); 199MODULE_DESCRIPTION("PWM FAN driver"); 200MODULE_LICENSE("GPL"); 201