Ejemplo n.º 1
0
static int pwm_fan_probe(struct platform_device *pdev)
{
	struct device *hwmon;
	struct pwm_fan_ctx *ctx;
	int duty_cycle;
	int ret;

	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

	mutex_init(&ctx->lock);

	ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
	if (IS_ERR(ctx->pwm)) {
		dev_err(&pdev->dev, "Could not get PWM\n");
		return PTR_ERR(ctx->pwm);
	}

	platform_set_drvdata(pdev, ctx);

	/* Set duty cycle to maximum allowed */
	duty_cycle = ctx->pwm->period - 1;
	ctx->pwm_value = MAX_PWM;

	ret = pwm_config(ctx->pwm, duty_cycle, ctx->pwm->period);
	if (ret) {
		dev_err(&pdev->dev, "Failed to configure PWM\n");
		return ret;
	}

	/* Enbale PWM output */
	ret = pwm_enable(ctx->pwm);
	if (ret) {
		dev_err(&pdev->dev, "Failed to enable PWM\n");
		return ret;
	}

	hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
						       ctx, pwm_fan_groups);
	if (IS_ERR(hwmon)) {
		dev_err(&pdev->dev, "Failed to register hwmon device\n");
		pwm_disable(ctx->pwm);
		return PTR_ERR(hwmon);
	}
	return 0;
}
Ejemplo n.º 2
0
static struct led_pwm_priv *led_pwm_create_of(struct platform_device *pdev)
{
    struct device_node *node = pdev->dev.of_node;
    struct device_node *child;
    struct led_pwm_priv *priv;
    int count, ret;

    /* count LEDs in this device, so we know how much to allocate */
    count = of_get_child_count(node);
    if (!count)
        return NULL;

    priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
                        GFP_KERNEL);
    if (!priv)
        return NULL;

    for_each_child_of_node(node, child) {
        struct led_pwm_data *led_dat = &priv->leds[priv->num_leds];

        led_dat->cdev.name = of_get_property(child, "label",
                                             NULL) ? : child->name;

        led_dat->pwm = devm_of_pwm_get(&pdev->dev, child, NULL);
        if (IS_ERR(led_dat->pwm)) {
            dev_err(&pdev->dev, "unable to request PWM for %s\n",
                    led_dat->cdev.name);
            goto err;
        }
        /* Get the period from PWM core when n*/
        led_dat->period = pwm_get_period(led_dat->pwm);

        led_dat->cdev.default_trigger = of_get_property(child,
                                        "linux,default-trigger", NULL);
        of_property_read_u32(child, "max-brightness",
                             &led_dat->cdev.max_brightness);

        led_dat->cdev.brightness_set = led_pwm_set;
        led_dat->cdev.brightness = LED_OFF;
        led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;

        led_dat->can_sleep = pwm_can_sleep(led_dat->pwm);
        if (led_dat->can_sleep)
            INIT_WORK(&led_dat->work, led_pwm_work);

        ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
        if (ret < 0) {
            dev_err(&pdev->dev, "failed to register for %s\n",
                    led_dat->cdev.name);
            of_node_put(child);
            goto err;
        }
        priv->num_leds++;
    }

    return priv;
err:
    while (priv->num_leds--)
        led_classdev_unregister(&priv->leds[priv->num_leds].cdev);

    return NULL;
}