pwm: cros-ec: Accept more error codes from cros_ec_cmd_xfer_status
authorGuenter Roeck <linux@roeck-us.net>
Sat, 22 Aug 2020 15:08:54 +0000 (08:08 -0700)
committerEnric Balletbo i Serra <enric.balletbo@collabora.com>
Mon, 24 Aug 2020 09:37:34 +0000 (11:37 +0200)
Since commit c5cd2b47b203 ("platform/chrome: cros_ec_proto: Report command
not supported") we can no longer assume that cros_ec_cmd_xfer_status()
reports -EPROTO for all errors returned by the EC itself. A follow-up
patch will change cros_ec_cmd_xfer_status() to report additional errors
reported by the EC as distinguished Linux error codes.

Handle this change by no longer assuming that only -EPROTO is used
to report all errors returned by the EC itself. Instead, support both
the old and the new error codes.

Add a comment describing cros_ec_num_pwms() to explain its functionality.

Cc: Gwendal Grignou <gwendal@chromium.org>
Cc: Yu-Hsuan Hsu <yuhsuan@chromium.org>
Cc: Prashant Malani <pmalani@chromium.org>
Cc: Brian Norris <briannorris@chromium.org>
Acked-by: Thierry Reding <thierry.reding@gmail.com>
Acked-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
drivers/pwm/pwm-cros-ec.c

index 09c08de..94d3dff 100644 (file)
@@ -204,6 +204,11 @@ static const struct pwm_ops cros_ec_pwm_ops = {
        .owner          = THIS_MODULE,
 };
 
+/*
+ * Determine the number of supported PWMs. The EC does not return the number
+ * of PWMs it supports directly, so we have to read the pwm duty cycle for
+ * subsequent channels until we get an error.
+ */
 static int cros_ec_num_pwms(struct cros_ec_device *ec)
 {
        int i, ret;
@@ -213,20 +218,30 @@ static int cros_ec_num_pwms(struct cros_ec_device *ec)
                u32 result = 0;
 
                ret = __cros_ec_pwm_get_duty(ec, i, &result);
-               /* We want to parse EC protocol errors */
-               if (ret < 0 && !(ret == -EPROTO && result))
-                       return ret;
-
                /*
                 * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
                 * responses; everything else is treated as an error.
+                * The EC error codes either map to -EOPNOTSUPP / -EINVAL,
+                * or -EPROTO is returned and the EC error is in the result
+                * field. Check for both.
                 */
-               if (result == EC_RES_INVALID_COMMAND)
+               switch (ret) {
+               case -EOPNOTSUPP:       /* invalid command */
                        return -ENODEV;
-               else if (result == EC_RES_INVALID_PARAM)
+               case -EINVAL:           /* invalid parameter */
                        return i;
-               else if (result)
+               case -EPROTO:
+                       /* Old or new error return code: Handle both */
+                       if (result == EC_RES_INVALID_COMMAND)
+                               return -ENODEV;
+                       else if (result == EC_RES_INVALID_PARAM)
+                               return i;
                        return -EPROTO;
+               default:
+                       if (ret < 0)
+                               return ret;
+                       break;
+               }
        }
 
        return U8_MAX;