Re: [PATCH v2 1/8] leds: Add support for AXP20X CHGLED

From: Maxime Ripard
Date: Fri Feb 15 2019 - 10:57:39 EST


Hi Stefan,

Thanks for working on this.

On Fri, Feb 15, 2019 at 01:50:06PM +0200, Stefan Mavrodiev wrote:
> +static ssize_t control_show(struct device *dev, struct device_attribute *attr,
> + char *buf)
> +{
> + struct led_classdev *cdev = dev_get_drvdata(dev);
> + struct axp20x_led *priv = to_axp20x_led(cdev);
> +
> + return sprintf(buf, "%u\n", priv->ctrl);
> +}
> +
> +static ssize_t control_store(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t size)
> +{
> + struct led_classdev *cdev = dev_get_drvdata(dev);
> + struct axp20x_led *priv = to_axp20x_led(cdev);
> + unsigned long val;
> + int ret;
> +
> + ret = kstrtoul(buf, 0, &val);
> + if (ret)
> + return ret;
> +
> + /**
> + * Supported values are:
> + * - 0 : Manual control
> + * - 1 : Charger control
> + */
> + if (val > 1)
> + return -EINVAL;
> +
> + priv->ctrl = val;
> +
> + return axp20x_led_setup(priv) ? : size;
> +}
> +static DEVICE_ATTR_RW(control);
> +
> +static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
> + char *buf)
> +{
> + struct led_classdev *cdev = dev_get_drvdata(dev);
> + struct axp20x_led *priv = to_axp20x_led(cdev);
> +
> + return sprintf(buf, "%u\n", priv->mode);
> +}
> +
> +static ssize_t mode_store(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t size)
> +{
> + struct led_classdev *cdev = dev_get_drvdata(dev);
> + struct axp20x_led *priv = to_axp20x_led(cdev);
> + unsigned long val;
> + int ret;
> +
> + ret = kstrtoul(buf, 0, &val);
> + if (ret)
> + return ret;
> + /**
> + * Supported values are:
> + * - 0 : Mode A
> + * - 1 : Mode B
> + */
> + if (val > 1)
> + return -EINVAL;
> +
> + priv->mode = val;
> +
> + return axp20x_led_setup(priv) ? : size;
> +}
> +static DEVICE_ATTR_RW(mode);
> +
> +static struct attribute *axp20x_led_attrs[] = {
> + &dev_attr_control.attr,
> + &dev_attr_mode.attr,
> + NULL,
> +};
> +ATTRIBUTE_GROUPS(axp20x_led);

I can't really say whether adding sysfs handles for this is the right
thing to do, but if it is you should document the interface.

> + if (!of_property_read_u8(np, "x-powers,charger-mode", &value)) {
> + priv->ctrl = AXP20X_CHGLED_CTRL_CHARGER;
> + priv->mode = (value < 2) ? value : 0;
> + } else {
> + priv->ctrl = AXP20X_CHGLED_CTRL_MANUAL;
> + }

I'm not sure we want to make this a property of the device
tree. Changing the device tree isn't an option for some users, so we
need to make sure we can change it even if we can't change the device
tree.

A kernel module is one option, but the other reviewers might have some
alternatives.

> + str = of_get_property(np, "default-state", NULL);
> + if (str) {
> + if (!strcmp(str, "keep")) {
> + ret = axp20x_led_brightness_get(&priv->cdev);
> + if (ret < 0)
> + return ret;
> + priv->cdev.brightness = ret;
> + } else if (!strcmp(str, "on")) {
> + ret = axp20x_led_brightness_set_blocking(&priv->cdev,
> + LED_FULL);
> + } else {
> + ret = axp20x_led_brightness_set_blocking(&priv->cdev,
> + LED_OFF);
> + }
> + }
> +
> + return ret;
> +}
> +
> +static const struct of_device_id axp20x_led_of_match[] = {
> + { .compatible = "x-powers,axp20x-led" },

Having a wildcard in the compatible isn't great, since it doesn't
really allow you to identify which part it is you're actually using,
and that wildcard might become inconsistent at some point.

You'd better use axp209-led (but the name of the driver is fine).

> + {}
> +};
> +MODULE_DEVICE_TABLE(of, axp20x_led_of_match);
> +
> +static int axp20x_led_probe(struct platform_device *pdev)
> +{
> + struct axp20x_led *priv;
> + int ret;
> +
> + if (!of_device_is_available(pdev->dev.of_node))
> + return -ENODEV;
> +
> + priv = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_led),
> + GFP_KERNEL);
> + if (!priv)
> + return -ENOMEM;
> +
> + priv->axp20x = dev_get_drvdata(pdev->dev.parent);
> + if (!priv->axp20x) {
> + dev_err(&pdev->dev, "Failed to get parent data\n");
> + return -ENXIO;
> + }
> +
> + mutex_init(&priv->lock);
> +
> + priv->cdev.brightness_set_blocking = axp20x_led_brightness_set_blocking;
> + priv->cdev.brightness_get = axp20x_led_brightness_get;
> + priv->cdev.groups = axp20x_led_groups;
> +
> + ret = axp20x_led_parse_dt(priv, pdev->dev.of_node);
> + if (ret < 0) {
> + dev_err(&pdev->dev, "Failed to set parameters\n");
> + return ret;
> + }
> +
> + /**
> + * For some reason in AXP209 the bit that controls CHGLED is with
> + * inverted logic compared to all other PMICs.
> + * If the PMIC is actually AXP209, set inverted flag and later use it
> + * when configuring the LED.
> + */
> + if (priv->axp20x->variant == AXP209_ID)
> + priv->ctrl_inverted = 1;

This should be matched on the compatible.

Thanks!
Maxime

--
Maxime Ripard, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

Attachment: signature.asc
Description: PGP signature