gpio-fan.c 14.9 KB
Newer Older
S
Simon Guinot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * gpio-fan.c - Hwmon driver for fans connected to GPIO lines.
 *
 * Copyright (C) 2010 LaCie
 *
 * Author: Simon Guinot <sguinot@lacie.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/hwmon.h>
#include <linux/gpio.h>
33
#include <linux/of.h>
34 35
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
36
#include <linux/thermal.h>
S
Simon Guinot 已提交
37

38 39 40 41 42
struct gpio_fan_speed {
	int rpm;
	int ctrl_val;
};

S
Simon Guinot 已提交
43
struct gpio_fan_data {
44
	struct device		*dev;
S
Simon Guinot 已提交
45
	struct device		*hwmon_dev;
46 47
	/* Cooling device if any */
	struct thermal_cooling_device *cdev;
S
Simon Guinot 已提交
48 49 50 51 52 53
	struct mutex		lock; /* lock GPIOs operations. */
	int			num_ctrl;
	unsigned		*ctrl;
	int			num_speed;
	struct gpio_fan_speed	*speed;
	int			speed_index;
54
#ifdef CONFIG_PM_SLEEP
S
Simon Guinot 已提交
55 56 57
	int			resume_speed;
#endif
	bool			pwm_enable;
58 59
	unsigned int		alarm_gpio;
	unsigned int		alarm_gpio_active_low;
S
Simon Guinot 已提交
60 61 62 63 64 65 66 67 68 69 70 71
	struct work_struct	alarm_work;
};

/*
 * Alarm GPIO.
 */

static void fan_alarm_notify(struct work_struct *ws)
{
	struct gpio_fan_data *fan_data =
		container_of(ws, struct gpio_fan_data, alarm_work);

72 73
	sysfs_notify(&fan_data->dev->kobj, NULL, "fan1_alarm");
	kobject_uevent(&fan_data->dev->kobj, KOBJ_CHANGE);
S
Simon Guinot 已提交
74 75 76 77 78 79 80 81 82 83 84
}

static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
{
	struct gpio_fan_data *fan_data = dev_id;

	schedule_work(&fan_data->alarm_work);

	return IRQ_NONE;
}

85 86
static ssize_t fan1_alarm_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
87 88
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
89
	int value = gpio_get_value_cansleep(fan_data->alarm_gpio);
S
Simon Guinot 已提交
90

91
	if (fan_data->alarm_gpio_active_low)
S
Simon Guinot 已提交
92 93 94 95 96
		value = !value;

	return sprintf(buf, "%d\n", value);
}

97
static DEVICE_ATTR_RO(fan1_alarm);
S
Simon Guinot 已提交
98

99
static int fan_alarm_init(struct gpio_fan_data *fan_data)
S
Simon Guinot 已提交
100 101 102
{
	int err;
	int alarm_irq;
103
	struct device *dev = fan_data->dev;
S
Simon Guinot 已提交
104

105
	err = devm_gpio_request(dev, fan_data->alarm_gpio, "GPIO fan alarm");
S
Simon Guinot 已提交
106 107 108
	if (err)
		return err;

109
	err = gpio_direction_input(fan_data->alarm_gpio);
S
Simon Guinot 已提交
110
	if (err)
111
		return err;
S
Simon Guinot 已提交
112 113 114 115 116

	/*
	 * If the alarm GPIO don't support interrupts, just leave
	 * without initializing the fail notification support.
	 */
117
	alarm_irq = gpio_to_irq(fan_data->alarm_gpio);
S
Simon Guinot 已提交
118 119 120 121
	if (alarm_irq < 0)
		return 0;

	INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
122
	irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
123
	err = devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
124
			       IRQF_SHARED, "GPIO fan alarm", fan_data);
S
Simon Guinot 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
	return err;
}

/*
 * Control GPIOs.
 */

/* Must be called with fan_data->lock held, except during initialization. */
static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
{
	int i;

	for (i = 0; i < fan_data->num_ctrl; i++)
138
		gpio_set_value_cansleep(fan_data->ctrl[i], (ctrl_val >> i) & 1);
S
Simon Guinot 已提交
139 140 141 142 143 144 145 146 147 148
}

static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
{
	int i;
	int ctrl_val = 0;

	for (i = 0; i < fan_data->num_ctrl; i++) {
		int value;

149
		value = gpio_get_value_cansleep(fan_data->ctrl[i]);
S
Simon Guinot 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		ctrl_val |= (value << i);
	}
	return ctrl_val;
}

/* Must be called with fan_data->lock held, except during initialization. */
static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
{
	if (fan_data->speed_index == speed_index)
		return;

	__set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
	fan_data->speed_index = speed_index;
}

static int get_fan_speed_index(struct gpio_fan_data *fan_data)
{
	int ctrl_val = __get_fan_ctrl(fan_data);
	int i;

	for (i = 0; i < fan_data->num_speed; i++)
		if (fan_data->speed[i].ctrl_val == ctrl_val)
			return i;

174
	dev_warn(fan_data->dev,
S
Simon Guinot 已提交
175 176
		 "missing speed array entry for GPIO value 0x%x\n", ctrl_val);

177
	return -ENODEV;
S
Simon Guinot 已提交
178 179
}

180
static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
S
Simon Guinot 已提交
181 182 183 184 185 186 187 188 189 190 191
{
	struct gpio_fan_speed *speed = fan_data->speed;
	int i;

	for (i = 0; i < fan_data->num_speed; i++)
		if (speed[i].rpm >= rpm)
			return i;

	return fan_data->num_speed - 1;
}

192 193
static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
S
Simon Guinot 已提交
194 195 196 197 198 199 200
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
	u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);

	return sprintf(buf, "%d\n", pwm);
}

201 202
static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t count)
S
Simon Guinot 已提交
203 204 205 206 207 208
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
	unsigned long pwm;
	int speed_index;
	int ret = count;

209
	if (kstrtoul(buf, 10, &pwm) || pwm > 255)
S
Simon Guinot 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		return -EINVAL;

	mutex_lock(&fan_data->lock);

	if (!fan_data->pwm_enable) {
		ret = -EPERM;
		goto exit_unlock;
	}

	speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
	set_fan_speed(fan_data, speed_index);

exit_unlock:
	mutex_unlock(&fan_data->lock);

	return ret;
}

228 229
static ssize_t pwm1_enable_show(struct device *dev,
				struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
230 231 232 233 234 235
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n", fan_data->pwm_enable);
}

236 237 238
static ssize_t pwm1_enable_store(struct device *dev,
				 struct device_attribute *attr,
				 const char *buf, size_t count)
S
Simon Guinot 已提交
239 240 241 242
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
	unsigned long val;

243
	if (kstrtoul(buf, 10, &val) || val > 1)
S
Simon Guinot 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
		return -EINVAL;

	if (fan_data->pwm_enable == val)
		return count;

	mutex_lock(&fan_data->lock);

	fan_data->pwm_enable = val;

	/* Disable manual control mode: set fan at full speed. */
	if (val == 0)
		set_fan_speed(fan_data, fan_data->num_speed - 1);

	mutex_unlock(&fan_data->lock);

	return count;
}

262 263
static ssize_t pwm1_mode_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
264 265 266 267
{
	return sprintf(buf, "0\n");
}

268 269
static ssize_t fan1_min_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
270 271 272 273 274 275
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
}

276 277
static ssize_t fan1_max_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
278 279 280 281 282 283 284
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n",
		       fan_data->speed[fan_data->num_speed - 1].rpm);
}

285 286
static ssize_t fan1_input_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
}

static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
		       const char *buf, size_t count)
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
	unsigned long rpm;
	int ret = count;

300
	if (kstrtoul(buf, 10, &rpm))
S
Simon Guinot 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
		return -EINVAL;

	mutex_lock(&fan_data->lock);

	if (!fan_data->pwm_enable) {
		ret = -EPERM;
		goto exit_unlock;
	}

	set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));

exit_unlock:
	mutex_unlock(&fan_data->lock);

	return ret;
}

318 319 320 321 322 323 324
static DEVICE_ATTR_RW(pwm1);
static DEVICE_ATTR_RW(pwm1_enable);
static DEVICE_ATTR_RO(pwm1_mode);
static DEVICE_ATTR_RO(fan1_min);
static DEVICE_ATTR_RO(fan1_max);
static DEVICE_ATTR_RO(fan1_input);
static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, fan1_input_show, set_rpm);
S
Simon Guinot 已提交
325

326 327 328 329 330 331
static umode_t gpio_fan_is_visible(struct kobject *kobj,
				   struct attribute *attr, int index)
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct gpio_fan_data *data = dev_get_drvdata(dev);

332
	if (index == 0 && !data->alarm_gpio)
333
		return 0;
334
	if (index > 0 && !data->ctrl)
335 336 337 338 339 340
		return 0;

	return attr->mode;
}

static struct attribute *gpio_fan_attributes[] = {
341 342
	&dev_attr_fan1_alarm.attr,		/* 0 */
	&dev_attr_pwm1.attr,			/* 1 */
S
Simon Guinot 已提交
343 344 345 346 347 348 349 350 351
	&dev_attr_pwm1_enable.attr,
	&dev_attr_pwm1_mode.attr,
	&dev_attr_fan1_input.attr,
	&dev_attr_fan1_target.attr,
	&dev_attr_fan1_min.attr,
	&dev_attr_fan1_max.attr,
	NULL
};

352 353 354
static const struct attribute_group gpio_fan_group = {
	.attrs = gpio_fan_attributes,
	.is_visible = gpio_fan_is_visible,
S
Simon Guinot 已提交
355 356
};

357 358 359 360 361
static const struct attribute_group *gpio_fan_groups[] = {
	&gpio_fan_group,
	NULL
};

362
static int fan_ctrl_init(struct gpio_fan_data *fan_data)
S
Simon Guinot 已提交
363
{
364
	struct device *dev = fan_data->dev;
365 366
	int num_ctrl = fan_data->num_ctrl;
	unsigned int *ctrl = fan_data->ctrl;
S
Simon Guinot 已提交
367 368 369
	int i, err;

	for (i = 0; i < num_ctrl; i++) {
370
		err = devm_gpio_request(dev, ctrl[i],
371
					"GPIO fan control");
S
Simon Guinot 已提交
372
		if (err)
373
			return err;
S
Simon Guinot 已提交
374

375 376
		err = gpio_direction_output(ctrl[i],
					    gpio_get_value_cansleep(ctrl[i]));
377 378
		if (err)
			return err;
S
Simon Guinot 已提交
379 380 381 382
	}

	fan_data->pwm_enable = true; /* Enable manual fan speed control. */
	fan_data->speed_index = get_fan_speed_index(fan_data);
383
	if (fan_data->speed_index < 0)
384
		return fan_data->speed_index;
S
Simon Guinot 已提交
385

386
	return 0;
S
Simon Guinot 已提交
387 388
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
				  unsigned long *state)
{
	struct gpio_fan_data *fan_data = cdev->devdata;

	if (!fan_data)
		return -EINVAL;

	*state = fan_data->num_speed - 1;
	return 0;
}

static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
				  unsigned long *state)
{
	struct gpio_fan_data *fan_data = cdev->devdata;

	if (!fan_data)
		return -EINVAL;

409
	*state = fan_data->speed_index;
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	return 0;
}

static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
				  unsigned long state)
{
	struct gpio_fan_data *fan_data = cdev->devdata;

	if (!fan_data)
		return -EINVAL;

	set_fan_speed(fan_data, state);
	return 0;
}

static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
	.get_max_state = gpio_fan_get_max_state,
	.get_cur_state = gpio_fan_get_cur_state,
	.set_cur_state = gpio_fan_set_cur_state,
};

431 432 433
/*
 * Translate OpenFirmware node properties into platform_data
 */
434
static int gpio_fan_get_of_data(struct gpio_fan_data *fan_data)
435 436
{
	struct gpio_fan_speed *speed;
437 438
	struct device *dev = fan_data->dev;
	struct device_node *np = dev->of_node;
439 440 441 442 443 444
	unsigned *ctrl;
	unsigned i;
	u32 u;
	struct property *prop;
	const __be32 *p;

445
	/* Alarm GPIO if one exists */
446
	if (of_gpio_named_count(np, "alarm-gpios") > 0) {
447 448 449
		int val;
		enum of_gpio_flags flags;

450
		val = of_get_named_gpio_flags(np, "alarm-gpios", 0, &flags);
451 452
		if (val < 0)
			return val;
453 454
		fan_data->alarm_gpio = val;
		fan_data->alarm_gpio_active_low = flags & OF_GPIO_ACTIVE_LOW;
455 456
	}

457
	/* Fill GPIO pin array */
458 459
	fan_data->num_ctrl = of_gpio_count(np);
	if (fan_data->num_ctrl <= 0) {
460
		if (fan_data->alarm_gpio)
461 462
			return 0;
		dev_err(dev, "DT properties empty / missing");
463 464
		return -ENODEV;
	}
465 466
	ctrl = devm_kzalloc(dev, fan_data->num_ctrl * sizeof(unsigned int),
			    GFP_KERNEL);
467 468
	if (!ctrl)
		return -ENOMEM;
469
	for (i = 0; i < fan_data->num_ctrl; i++) {
470 471
		int val;

472
		val = of_get_gpio(np, i);
473 474 475 476
		if (val < 0)
			return val;
		ctrl[i] = val;
	}
477
	fan_data->ctrl = ctrl;
478 479

	/* Get number of RPM/ctrl_val pairs in speed map */
480
	prop = of_find_property(np, "gpio-fan,speed-map", &i);
481 482 483 484 485 486 487 488 489
	if (!prop) {
		dev_err(dev, "gpio-fan,speed-map DT property missing");
		return -ENODEV;
	}
	i = i / sizeof(u32);
	if (i == 0 || i & 1) {
		dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
		return -ENODEV;
	}
490
	fan_data->num_speed = i / 2;
491 492 493 494 495 496 497

	/*
	 * Populate speed map
	 * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
	 * this needs splitting into pairs to create gpio_fan_speed structs
	 */
	speed = devm_kzalloc(dev,
498
			fan_data->num_speed * sizeof(struct gpio_fan_speed),
499 500 501 502
			GFP_KERNEL);
	if (!speed)
		return -ENOMEM;
	p = NULL;
503
	for (i = 0; i < fan_data->num_speed; i++) {
504 505 506 507 508 509 510 511 512
		p = of_prop_next_u32(prop, p, &u);
		if (!p)
			return -ENODEV;
		speed[i].rpm = u;
		p = of_prop_next_u32(prop, p, &u);
		if (!p)
			return -ENODEV;
		speed[i].ctrl_val = u;
	}
513
	fan_data->speed = speed;
514 515 516 517

	return 0;
}

518
static const struct of_device_id of_gpio_fan_match[] = {
519 520 521
	{ .compatible = "gpio-fan", },
	{},
};
522
MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
523

B
Bill Pemberton 已提交
524
static int gpio_fan_probe(struct platform_device *pdev)
S
Simon Guinot 已提交
525 526 527
{
	int err;
	struct gpio_fan_data *fan_data;
528 529
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
S
Simon Guinot 已提交
530

531
	fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
532 533 534 535
				GFP_KERNEL);
	if (!fan_data)
		return -ENOMEM;

536
	err = gpio_fan_get_of_data(fan_data);
537 538
	if (err)
		return err;
S
Simon Guinot 已提交
539

540
	fan_data->dev = dev;
S
Simon Guinot 已提交
541 542 543 544
	platform_set_drvdata(pdev, fan_data);
	mutex_init(&fan_data->lock);

	/* Configure alarm GPIO if available. */
545
	if (fan_data->alarm_gpio) {
546
		err = fan_alarm_init(fan_data);
S
Simon Guinot 已提交
547
		if (err)
548
			return err;
S
Simon Guinot 已提交
549 550 551
	}

	/* Configure control GPIOs if available. */
552 553
	if (fan_data->ctrl && fan_data->num_ctrl > 0) {
		if (!fan_data->speed || fan_data->num_speed <= 1)
554
			return -EINVAL;
555
		err = fan_ctrl_init(fan_data);
S
Simon Guinot 已提交
556
		if (err)
557
			return err;
S
Simon Guinot 已提交
558 559 560
	}

	/* Make this driver part of hwmon class. */
561
	fan_data->hwmon_dev =
562
		devm_hwmon_device_register_with_groups(dev,
563 564
						       "gpio_fan", fan_data,
						       gpio_fan_groups);
565 566
	if (IS_ERR(fan_data->hwmon_dev))
		return PTR_ERR(fan_data->hwmon_dev);
567

568
	/* Optional cooling device register for Device tree platforms */
569
	fan_data->cdev = thermal_of_cooling_device_register(np,
570 571 572
							    "gpio-fan",
							    fan_data,
							    &gpio_fan_cool_ops);
S
Simon Guinot 已提交
573

574
	dev_info(dev, "GPIO fan initialized\n");
S
Simon Guinot 已提交
575 576 577 578

	return 0;
}

579
static int gpio_fan_remove(struct platform_device *pdev)
580
{
581 582 583 584
	struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);

	if (!IS_ERR(fan_data->cdev))
		thermal_cooling_device_unregister(fan_data->cdev);
585 586 587

	if (fan_data->ctrl)
		set_fan_speed(fan_data, 0);
588 589 590 591 592 593 594

	return 0;
}

static void gpio_fan_shutdown(struct platform_device *pdev)
{
	gpio_fan_remove(pdev);
595 596
}

597 598
#ifdef CONFIG_PM_SLEEP
static int gpio_fan_suspend(struct device *dev)
S
Simon Guinot 已提交
599
{
600
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
S
Simon Guinot 已提交
601 602 603 604 605 606 607 608 609

	if (fan_data->ctrl) {
		fan_data->resume_speed = fan_data->speed_index;
		set_fan_speed(fan_data, 0);
	}

	return 0;
}

610
static int gpio_fan_resume(struct device *dev)
S
Simon Guinot 已提交
611
{
612
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
S
Simon Guinot 已提交
613 614 615 616 617 618

	if (fan_data->ctrl)
		set_fan_speed(fan_data, fan_data->resume_speed);

	return 0;
}
619 620

static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
621
#define GPIO_FAN_PM	(&gpio_fan_pm)
S
Simon Guinot 已提交
622
#else
623
#define GPIO_FAN_PM	NULL
S
Simon Guinot 已提交
624 625 626 627
#endif

static struct platform_driver gpio_fan_driver = {
	.probe		= gpio_fan_probe,
628
	.remove		= gpio_fan_remove,
629
	.shutdown	= gpio_fan_shutdown,
S
Simon Guinot 已提交
630 631
	.driver	= {
		.name	= "gpio-fan",
632
		.pm	= GPIO_FAN_PM,
633
		.of_match_table = of_match_ptr(of_gpio_fan_match),
S
Simon Guinot 已提交
634 635 636
	},
};

637
module_platform_driver(gpio_fan_driver);
S
Simon Guinot 已提交
638 639 640 641 642

MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
MODULE_DESCRIPTION("GPIO FAN driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:gpio-fan");