blob: e6ed3532deac74754e4c172562b85e493c54d047 [file] [log] [blame]
Kamil Debskid82d5772014-07-16 17:46:42 +02001/*
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;
Lukasz Majewski2e5219c2015-02-26 14:59:36 +010033 unsigned int pwm_value;
34 unsigned int pwm_fan_state;
35 unsigned int pwm_fan_max_state;
36 unsigned int *pwm_fan_cooling_levels;
Kamil Debskid82d5772014-07-16 17:46:42 +020037};
38
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010039static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
Kamil Debskid82d5772014-07-16 17:46:42 +020040{
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010041 unsigned long duty;
42 int ret = 0;
Kamil Debskid82d5772014-07-16 17:46:42 +020043
44 mutex_lock(&ctx->lock);
Kamil Debskid82d5772014-07-16 17:46:42 +020045 if (ctx->pwm_value == pwm)
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010046 goto exit_set_pwm_err;
Kamil Debskid82d5772014-07-16 17:46:42 +020047
48 if (pwm == 0) {
49 pwm_disable(ctx->pwm);
50 goto exit_set_pwm;
51 }
52
53 duty = DIV_ROUND_UP(pwm * (ctx->pwm->period - 1), MAX_PWM);
54 ret = pwm_config(ctx->pwm, duty, ctx->pwm->period);
55 if (ret)
56 goto exit_set_pwm_err;
57
58 if (ctx->pwm_value == 0) {
59 ret = pwm_enable(ctx->pwm);
60 if (ret)
61 goto exit_set_pwm_err;
62 }
63
64exit_set_pwm:
65 ctx->pwm_value = pwm;
Kamil Debskid82d5772014-07-16 17:46:42 +020066exit_set_pwm_err:
67 mutex_unlock(&ctx->lock);
68 return ret;
69}
70
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010071static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
72 const char *buf, size_t count)
73{
74 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
75 unsigned long pwm;
76 int ret;
77
78 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
79 return -EINVAL;
80
81 ret = __set_pwm(ctx, pwm);
82 if (ret)
83 return ret;
84
85 return count;
86}
87
Kamil Debskid82d5772014-07-16 17:46:42 +020088static ssize_t show_pwm(struct device *dev,
89 struct device_attribute *attr, char *buf)
90{
91 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
92
93 return sprintf(buf, "%u\n", ctx->pwm_value);
94}
95
96
97static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
98
99static struct attribute *pwm_fan_attrs[] = {
100 &sensor_dev_attr_pwm1.dev_attr.attr,
101 NULL,
102};
103
104ATTRIBUTE_GROUPS(pwm_fan);
105
Lukasz Majewski2e5219c2015-02-26 14:59:36 +0100106int pwm_fan_of_get_cooling_data(struct device *dev, struct pwm_fan_ctx *ctx)
107{
108 struct device_node *np = dev->of_node;
109 int num, i, ret;
110
111 if (!of_find_property(np, "cooling-levels", NULL))
112 return 0;
113
114 ret = of_property_count_u32_elems(np, "cooling-levels");
115 if (ret <= 0) {
116 dev_err(dev, "Wrong data!\n");
117 return ret ? : -EINVAL;
118 }
119
120 num = ret;
121 ctx->pwm_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
122 GFP_KERNEL);
123 if (!ctx->pwm_fan_cooling_levels)
124 return -ENOMEM;
125
126 ret = of_property_read_u32_array(np, "cooling-levels",
127 ctx->pwm_fan_cooling_levels, num);
128 if (ret) {
129 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
130 return ret;
131 }
132
133 for (i = 0; i < num; i++) {
134 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
135 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
136 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
137 return -EINVAL;
138 }
139 }
140
141 ctx->pwm_fan_max_state = num - 1;
142
143 return 0;
144}
145
Kamil Debskid82d5772014-07-16 17:46:42 +0200146static int pwm_fan_probe(struct platform_device *pdev)
147{
148 struct device *hwmon;
149 struct pwm_fan_ctx *ctx;
150 int duty_cycle;
151 int ret;
152
153 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
154 if (!ctx)
155 return -ENOMEM;
156
157 mutex_init(&ctx->lock);
158
159 ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
160 if (IS_ERR(ctx->pwm)) {
161 dev_err(&pdev->dev, "Could not get PWM\n");
162 return PTR_ERR(ctx->pwm);
163 }
164
Kamil Debskid82d5772014-07-16 17:46:42 +0200165 platform_set_drvdata(pdev, ctx);
166
167 /* Set duty cycle to maximum allowed */
168 duty_cycle = ctx->pwm->period - 1;
169 ctx->pwm_value = MAX_PWM;
170
171 ret = pwm_config(ctx->pwm, duty_cycle, ctx->pwm->period);
172 if (ret) {
173 dev_err(&pdev->dev, "Failed to configure PWM\n");
174 return ret;
175 }
176
177 /* Enbale PWM output */
178 ret = pwm_enable(ctx->pwm);
179 if (ret) {
180 dev_err(&pdev->dev, "Failed to enable PWM\n");
181 return ret;
182 }
183
184 hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
185 ctx, pwm_fan_groups);
186 if (IS_ERR(hwmon)) {
187 dev_err(&pdev->dev, "Failed to register hwmon device\n");
188 pwm_disable(ctx->pwm);
189 return PTR_ERR(hwmon);
190 }
Lukasz Majewski2e5219c2015-02-26 14:59:36 +0100191
192 ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
193 if (ret)
194 return ret;
195
Kamil Debskid82d5772014-07-16 17:46:42 +0200196 return 0;
197}
198
199static int pwm_fan_remove(struct platform_device *pdev)
200{
201 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
202
203 if (ctx->pwm_value)
204 pwm_disable(ctx->pwm);
205 return 0;
206}
207
208#ifdef CONFIG_PM_SLEEP
209static int pwm_fan_suspend(struct device *dev)
210{
211 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
212
213 if (ctx->pwm_value)
214 pwm_disable(ctx->pwm);
215 return 0;
216}
217
218static int pwm_fan_resume(struct device *dev)
219{
220 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
Kamil Debski48b9d5b2014-11-03 15:42:55 +0100221 unsigned long duty;
222 int ret;
Kamil Debskid82d5772014-07-16 17:46:42 +0200223
Kamil Debski48b9d5b2014-11-03 15:42:55 +0100224 if (ctx->pwm_value == 0)
225 return 0;
226
227 duty = DIV_ROUND_UP(ctx->pwm_value * (ctx->pwm->period - 1), MAX_PWM);
228 ret = pwm_config(ctx->pwm, duty, ctx->pwm->period);
229 if (ret)
230 return ret;
231 return pwm_enable(ctx->pwm);
Kamil Debskid82d5772014-07-16 17:46:42 +0200232}
233#endif
234
235static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
236
237static struct of_device_id of_pwm_fan_match[] = {
238 { .compatible = "pwm-fan", },
239 {},
240};
241
242static struct platform_driver pwm_fan_driver = {
243 .probe = pwm_fan_probe,
244 .remove = pwm_fan_remove,
245 .driver = {
246 .name = "pwm-fan",
247 .pm = &pwm_fan_pm,
248 .of_match_table = of_pwm_fan_match,
249 },
250};
251
252module_platform_driver(pwm_fan_driver);
253
254MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
255MODULE_ALIAS("platform:pwm-fan");
256MODULE_DESCRIPTION("PWM FAN driver");
257MODULE_LICENSE("GPL");