gpio-fan.c 13.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
S
Simon Guinot 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * gpio-fan.c - Hwmon driver for fans connected to GPIO lines.
 *
 * Copyright (C) 2010 LaCie
 *
 * Author: Simon Guinot <sguinot@lacie.com>
 */

#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>
19
#include <linux/gpio/consumer.h>
20
#include <linux/of.h>
21
#include <linux/of_platform.h>
22
#include <linux/thermal.h>
S
Simon Guinot 已提交
23

24 25 26 27 28
struct gpio_fan_speed {
	int rpm;
	int ctrl_val;
};

S
Simon Guinot 已提交
29
struct gpio_fan_data {
30
	struct device		*dev;
S
Simon Guinot 已提交
31
	struct device		*hwmon_dev;
32 33
	/* Cooling device if any */
	struct thermal_cooling_device *cdev;
S
Simon Guinot 已提交
34
	struct mutex		lock; /* lock GPIOs operations. */
35
	int			num_gpios;
36
	struct gpio_desc	**gpios;
S
Simon Guinot 已提交
37 38 39
	int			num_speed;
	struct gpio_fan_speed	*speed;
	int			speed_index;
40
#ifdef CONFIG_PM_SLEEP
S
Simon Guinot 已提交
41 42 43
	int			resume_speed;
#endif
	bool			pwm_enable;
44
	struct gpio_desc	*alarm_gpio;
S
Simon Guinot 已提交
45 46 47 48 49 50 51 52 53 54 55 56
	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);

57 58
	sysfs_notify(&fan_data->hwmon_dev->kobj, NULL, "fan1_alarm");
	kobject_uevent(&fan_data->hwmon_dev->kobj, KOBJ_CHANGE);
S
Simon Guinot 已提交
59 60 61 62 63 64 65 66 67 68 69
}

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;
}

70 71
static ssize_t fan1_alarm_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
72 73 74
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);

75 76
	return sprintf(buf, "%d\n",
		       gpiod_get_value_cansleep(fan_data->alarm_gpio));
S
Simon Guinot 已提交
77 78
}

79
static DEVICE_ATTR_RO(fan1_alarm);
S
Simon Guinot 已提交
80

81
static int fan_alarm_init(struct gpio_fan_data *fan_data)
S
Simon Guinot 已提交
82 83
{
	int alarm_irq;
84
	struct device *dev = fan_data->dev;
S
Simon Guinot 已提交
85 86 87 88 89

	/*
	 * If the alarm GPIO don't support interrupts, just leave
	 * without initializing the fail notification support.
	 */
90 91
	alarm_irq = gpiod_to_irq(fan_data->alarm_gpio);
	if (alarm_irq <= 0)
S
Simon Guinot 已提交
92 93 94
		return 0;

	INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
95
	irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
96 97
	return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
				IRQF_SHARED, "GPIO fan alarm", fan_data);
S
Simon Guinot 已提交
98 99 100 101 102 103 104 105 106 107 108
}

/*
 * 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;

109
	for (i = 0; i < fan_data->num_gpios; i++)
110 111
		gpiod_set_value_cansleep(fan_data->gpios[i],
					 (ctrl_val >> i) & 1);
S
Simon Guinot 已提交
112 113 114 115 116 117 118
}

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

119
	for (i = 0; i < fan_data->num_gpios; i++) {
S
Simon Guinot 已提交
120 121
		int value;

122
		value = gpiod_get_value_cansleep(fan_data->gpios[i]);
S
Simon Guinot 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
		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;

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

150
	return -ENODEV;
S
Simon Guinot 已提交
151 152
}

153
static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
S
Simon Guinot 已提交
154 155 156 157 158 159 160 161 162 163 164
{
	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;
}

165 166
static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
S
Simon Guinot 已提交
167 168 169 170 171 172 173
{
	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);
}

174 175
static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t count)
S
Simon Guinot 已提交
176 177 178 179 180 181
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
	unsigned long pwm;
	int speed_index;
	int ret = count;

182
	if (kstrtoul(buf, 10, &pwm) || pwm > 255)
S
Simon Guinot 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		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;
}

201 202
static ssize_t pwm1_enable_show(struct device *dev,
				struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
203 204 205 206 207 208
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);

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

209 210 211
static ssize_t pwm1_enable_store(struct device *dev,
				 struct device_attribute *attr,
				 const char *buf, size_t count)
S
Simon Guinot 已提交
212 213 214 215
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
	unsigned long val;

216
	if (kstrtoul(buf, 10, &val) || val > 1)
S
Simon Guinot 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		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;
}

235 236
static ssize_t pwm1_mode_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
237 238 239 240
{
	return sprintf(buf, "0\n");
}

241 242
static ssize_t fan1_min_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
243 244 245 246 247 248
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);

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

249 250
static ssize_t fan1_max_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
251 252 253 254 255 256 257
{
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);

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

258 259
static ssize_t fan1_input_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
S
Simon Guinot 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272
{
	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;

273
	if (kstrtoul(buf, 10, &rpm))
S
Simon Guinot 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
		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;
}

291 292 293 294 295 296
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);
297
static DEVICE_ATTR(fan1_target, 0644, fan1_input_show, set_rpm);
S
Simon Guinot 已提交
298

299 300 301
static umode_t gpio_fan_is_visible(struct kobject *kobj,
				   struct attribute *attr, int index)
{
302
	struct device *dev = kobj_to_dev(kobj);
303 304
	struct gpio_fan_data *data = dev_get_drvdata(dev);

305
	if (index == 0 && !data->alarm_gpio)
306
		return 0;
307
	if (index > 0 && !data->gpios)
308 309 310 311 312 313
		return 0;

	return attr->mode;
}

static struct attribute *gpio_fan_attributes[] = {
314 315
	&dev_attr_fan1_alarm.attr,		/* 0 */
	&dev_attr_pwm1.attr,			/* 1 */
S
Simon Guinot 已提交
316 317 318 319 320 321 322 323 324
	&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
};

325 326 327
static const struct attribute_group gpio_fan_group = {
	.attrs = gpio_fan_attributes,
	.is_visible = gpio_fan_is_visible,
S
Simon Guinot 已提交
328 329
};

330 331 332 333 334
static const struct attribute_group *gpio_fan_groups[] = {
	&gpio_fan_group,
	NULL
};

335
static int fan_ctrl_init(struct gpio_fan_data *fan_data)
S
Simon Guinot 已提交
336
{
337
	int num_gpios = fan_data->num_gpios;
338
	struct gpio_desc **gpios = fan_data->gpios;
S
Simon Guinot 已提交
339 340
	int i, err;

341
	for (i = 0; i < num_gpios; i++) {
342 343 344 345 346 347 348 349
		/*
		 * The GPIO descriptors were retrieved with GPIOD_ASIS so here
		 * we set the GPIO into output mode, carefully preserving the
		 * current value by setting it to whatever it is already set
		 * (no surprise changes in default fan speed).
		 */
		err = gpiod_direction_output(gpios[i],
					gpiod_get_value_cansleep(gpios[i]));
350 351
		if (err)
			return err;
S
Simon Guinot 已提交
352 353 354 355
	}

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

359
	return 0;
S
Simon Guinot 已提交
360 361
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
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;

382
	*state = fan_data->speed_index;
383 384 385 386 387 388 389 390 391 392 393
	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;

394 395 396
	if (state >= fan_data->num_speed)
		return -EINVAL;

397 398 399 400 401 402 403 404 405 406
	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,
};

407 408 409
/*
 * Translate OpenFirmware node properties into platform_data
 */
410
static int gpio_fan_get_of_data(struct gpio_fan_data *fan_data)
411 412
{
	struct gpio_fan_speed *speed;
413 414
	struct device *dev = fan_data->dev;
	struct device_node *np = dev->of_node;
415
	struct gpio_desc **gpios;
416 417 418 419 420
	unsigned i;
	u32 u;
	struct property *prop;
	const __be32 *p;

421
	/* Alarm GPIO if one exists */
422 423 424
	fan_data->alarm_gpio = devm_gpiod_get_optional(dev, "alarm", GPIOD_IN);
	if (IS_ERR(fan_data->alarm_gpio))
		return PTR_ERR(fan_data->alarm_gpio);
425

426
	/* Fill GPIO pin array */
427
	fan_data->num_gpios = gpiod_count(dev, NULL);
428
	if (fan_data->num_gpios <= 0) {
429
		if (fan_data->alarm_gpio)
430 431
			return 0;
		dev_err(dev, "DT properties empty / missing");
432 433
		return -ENODEV;
	}
434 435
	gpios = devm_kcalloc(dev,
			     fan_data->num_gpios, sizeof(struct gpio_desc *),
436
			     GFP_KERNEL);
437
	if (!gpios)
438
		return -ENOMEM;
439
	for (i = 0; i < fan_data->num_gpios; i++) {
440 441 442
		gpios[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
		if (IS_ERR(gpios[i]))
			return PTR_ERR(gpios[i]);
443
	}
444
	fan_data->gpios = gpios;
445 446

	/* Get number of RPM/ctrl_val pairs in speed map */
447
	prop = of_find_property(np, "gpio-fan,speed-map", &i);
448 449 450 451 452 453 454 455 456
	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;
	}
457
	fan_data->num_speed = i / 2;
458 459 460 461 462 463

	/*
	 * 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
	 */
464 465
	speed = devm_kcalloc(dev,
			fan_data->num_speed, sizeof(struct gpio_fan_speed),
466 467 468 469
			GFP_KERNEL);
	if (!speed)
		return -ENOMEM;
	p = NULL;
470
	for (i = 0; i < fan_data->num_speed; i++) {
471 472 473 474 475 476 477 478 479
		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;
	}
480
	fan_data->speed = speed;
481 482 483 484

	return 0;
}

485
static const struct of_device_id of_gpio_fan_match[] = {
486 487 488
	{ .compatible = "gpio-fan", },
	{},
};
489
MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
490

491 492 493 494 495
static void gpio_fan_stop(void *data)
{
	set_fan_speed(data, 0);
}

B
Bill Pemberton 已提交
496
static int gpio_fan_probe(struct platform_device *pdev)
S
Simon Guinot 已提交
497 498 499
{
	int err;
	struct gpio_fan_data *fan_data;
500 501
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
S
Simon Guinot 已提交
502

503
	fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
504 505 506 507
				GFP_KERNEL);
	if (!fan_data)
		return -ENOMEM;

508
	fan_data->dev = dev;
509
	err = gpio_fan_get_of_data(fan_data);
510 511
	if (err)
		return err;
S
Simon Guinot 已提交
512 513 514 515 516

	platform_set_drvdata(pdev, fan_data);
	mutex_init(&fan_data->lock);

	/* Configure control GPIOs if available. */
517
	if (fan_data->gpios && fan_data->num_gpios > 0) {
518
		if (!fan_data->speed || fan_data->num_speed <= 1)
519
			return -EINVAL;
520
		err = fan_ctrl_init(fan_data);
S
Simon Guinot 已提交
521
		if (err)
522
			return err;
523 524 525
		err = devm_add_action_or_reset(dev, gpio_fan_stop, fan_data);
		if (err)
			return err;
S
Simon Guinot 已提交
526 527 528
	}

	/* Make this driver part of hwmon class. */
529
	fan_data->hwmon_dev =
530
		devm_hwmon_device_register_with_groups(dev,
531 532
						       "gpio_fan", fan_data,
						       gpio_fan_groups);
533 534
	if (IS_ERR(fan_data->hwmon_dev))
		return PTR_ERR(fan_data->hwmon_dev);
535

536 537 538 539 540 541 542
	/* Configure alarm GPIO if available. */
	if (fan_data->alarm_gpio) {
		err = fan_alarm_init(fan_data);
		if (err)
			return err;
	}

543
	/* Optional cooling device register for Device tree platforms */
544 545
	fan_data->cdev = devm_thermal_of_cooling_device_register(dev, np,
				"gpio-fan", fan_data, &gpio_fan_cool_ops);
S
Simon Guinot 已提交
546

547
	dev_info(dev, "GPIO fan initialized\n");
S
Simon Guinot 已提交
548 549 550 551

	return 0;
}

552
static void gpio_fan_shutdown(struct platform_device *pdev)
553
{
554 555
	struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);

556
	if (fan_data->gpios)
557 558 559
		set_fan_speed(fan_data, 0);
}

560 561
#ifdef CONFIG_PM_SLEEP
static int gpio_fan_suspend(struct device *dev)
S
Simon Guinot 已提交
562
{
563
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
S
Simon Guinot 已提交
564

565
	if (fan_data->gpios) {
S
Simon Guinot 已提交
566 567 568 569 570 571 572
		fan_data->resume_speed = fan_data->speed_index;
		set_fan_speed(fan_data, 0);
	}

	return 0;
}

573
static int gpio_fan_resume(struct device *dev)
S
Simon Guinot 已提交
574
{
575
	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
S
Simon Guinot 已提交
576

577
	if (fan_data->gpios)
S
Simon Guinot 已提交
578 579 580 581
		set_fan_speed(fan_data, fan_data->resume_speed);

	return 0;
}
582 583

static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
584
#define GPIO_FAN_PM	(&gpio_fan_pm)
S
Simon Guinot 已提交
585
#else
586
#define GPIO_FAN_PM	NULL
S
Simon Guinot 已提交
587 588 589 590
#endif

static struct platform_driver gpio_fan_driver = {
	.probe		= gpio_fan_probe,
591
	.shutdown	= gpio_fan_shutdown,
S
Simon Guinot 已提交
592 593
	.driver	= {
		.name	= "gpio-fan",
594
		.pm	= GPIO_FAN_PM,
595
		.of_match_table = of_match_ptr(of_gpio_fan_match),
S
Simon Guinot 已提交
596 597 598
	},
};

599
module_platform_driver(gpio_fan_driver);
S
Simon Guinot 已提交
600 601 602 603 604

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