815-v6.7-5-leds-turris-omnia-Fix-brightness-setting-and-trigger.patch 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. From ffec49d391c5f0195360912b216aa24dbc9b53c8 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Marek=20Beh=C3=BAn?= <[email protected]>
  3. Date: Mon, 16 Oct 2023 16:15:38 +0200
  4. Subject: [PATCH] leds: turris-omnia: Fix brightness setting and trigger
  5. activating
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. I have improperly refactored commits
  10. 4d5ed2621c24 ("leds: turris-omnia: Make set_brightness() more efficient")
  11. and
  12. aaf38273cf76 ("leds: turris-omnia: Support HW controlled mode via private trigger")
  13. after Lee requested a change in API semantics of the new functions I
  14. introduced in commit
  15. 28350bc0ac77 ("leds: turris-omnia: Do not use SMBUS calls").
  16. Before the change, the function omnia_cmd_write_u8() returned 0 on
  17. success, and afterwards it returned a positive value (number of bytes
  18. written). The latter version was applied, but the following commits did
  19. not properly account for this change.
  20. This results in non-functional LED's .brightness_set_blocking() and
  21. trigger's .activate() methods.
  22. The main reasoning behind the semantics change was that read/write
  23. methods should return the number of read/written bytes on success.
  24. It was pointed to me [1] that this is not always true (for example the
  25. regmap API does not do so), and since the driver never uses this number
  26. of read/written bytes information, I decided to fix this issue by
  27. changing the functions to the original semantics (return 0 on success).
  28. [1] https://lore.kernel.org/linux-gpio/[email protected]/
  29. Fixes: 28350bc0ac77 ("leds: turris-omnia: Do not use SMBUS calls")
  30. Signed-off-by: Marek Behún <[email protected]>
  31. ---
  32. drivers/leds/leds-turris-omnia.c | 37 +++++++++++++++++---------------
  33. 1 file changed, 20 insertions(+), 17 deletions(-)
  34. --- a/drivers/leds/leds-turris-omnia.c
  35. +++ b/drivers/leds/leds-turris-omnia.c
  36. @@ -60,8 +60,11 @@ struct omnia_leds {
  37. static int omnia_cmd_write_u8(const struct i2c_client *client, u8 cmd, u8 val)
  38. {
  39. u8 buf[2] = { cmd, val };
  40. + int ret;
  41. +
  42. + ret = i2c_master_send(client, buf, sizeof(buf));
  43. - return i2c_master_send(client, buf, sizeof(buf));
  44. + return ret < 0 ? ret : 0;
  45. }
  46. static int omnia_cmd_read_raw(struct i2c_adapter *adapter, u8 addr, u8 cmd,
  47. @@ -81,7 +84,7 @@ static int omnia_cmd_read_raw(struct i2c
  48. ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
  49. if (likely(ret == ARRAY_SIZE(msgs)))
  50. - return len;
  51. + return 0;
  52. else if (ret < 0)
  53. return ret;
  54. else
  55. @@ -91,11 +94,11 @@ static int omnia_cmd_read_raw(struct i2c
  56. static int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd)
  57. {
  58. u8 reply;
  59. - int ret;
  60. + int err;
  61. - ret = omnia_cmd_read_raw(client->adapter, client->addr, cmd, &reply, 1);
  62. - if (ret < 0)
  63. - return ret;
  64. + err = omnia_cmd_read_raw(client->adapter, client->addr, cmd, &reply, 1);
  65. + if (err)
  66. + return err;
  67. return reply;
  68. }
  69. @@ -236,7 +239,7 @@ static void omnia_hwtrig_deactivate(stru
  70. mutex_unlock(&leds->lock);
  71. - if (err < 0)
  72. + if (err)
  73. dev_err(cdev->dev, "Cannot put LED to software mode: %i\n",
  74. err);
  75. }
  76. @@ -302,7 +305,7 @@ static int omnia_led_register(struct i2c
  77. ret = omnia_cmd_write_u8(client, CMD_LED_MODE,
  78. CMD_LED_MODE_LED(led->reg) |
  79. CMD_LED_MODE_USER);
  80. - if (ret < 0) {
  81. + if (ret) {
  82. dev_err(dev, "Cannot set LED %pOF to software mode: %i\n", np,
  83. ret);
  84. return ret;
  85. @@ -311,7 +314,7 @@ static int omnia_led_register(struct i2c
  86. /* disable the LED */
  87. ret = omnia_cmd_write_u8(client, CMD_LED_STATE,
  88. CMD_LED_STATE_LED(led->reg));
  89. - if (ret < 0) {
  90. + if (ret) {
  91. dev_err(dev, "Cannot set LED %pOF brightness: %i\n", np, ret);
  92. return ret;
  93. }
  94. @@ -364,7 +367,7 @@ static ssize_t brightness_store(struct d
  95. {
  96. struct i2c_client *client = to_i2c_client(dev);
  97. unsigned long brightness;
  98. - int ret;
  99. + int err;
  100. if (kstrtoul(buf, 10, &brightness))
  101. return -EINVAL;
  102. @@ -372,9 +375,9 @@ static ssize_t brightness_store(struct d
  103. if (brightness > 100)
  104. return -EINVAL;
  105. - ret = omnia_cmd_write_u8(client, CMD_LED_SET_BRIGHTNESS, brightness);
  106. + err = omnia_cmd_write_u8(client, CMD_LED_SET_BRIGHTNESS, brightness);
  107. - return ret < 0 ? ret : count;
  108. + return err ?: count;
  109. }
  110. static DEVICE_ATTR_RW(brightness);
  111. @@ -403,7 +406,7 @@ static ssize_t gamma_correction_store(st
  112. struct i2c_client *client = to_i2c_client(dev);
  113. struct omnia_leds *leds = i2c_get_clientdata(client);
  114. bool val;
  115. - int ret;
  116. + int err;
  117. if (!leds->has_gamma_correction)
  118. return -EOPNOTSUPP;
  119. @@ -411,9 +414,9 @@ static ssize_t gamma_correction_store(st
  120. if (kstrtobool(buf, &val) < 0)
  121. return -EINVAL;
  122. - ret = omnia_cmd_write_u8(client, CMD_SET_GAMMA_CORRECTION, val);
  123. + err = omnia_cmd_write_u8(client, CMD_SET_GAMMA_CORRECTION, val);
  124. - return ret < 0 ? ret : count;
  125. + return err ?: count;
  126. }
  127. static DEVICE_ATTR_RW(gamma_correction);
  128. @@ -431,7 +434,7 @@ static int omnia_mcu_get_features(const
  129. err = omnia_cmd_read_raw(client->adapter, OMNIA_MCU_I2C_ADDR,
  130. CMD_GET_STATUS_WORD, &reply, sizeof(reply));
  131. - if (err < 0)
  132. + if (err)
  133. return err;
  134. /* Check whether MCU firmware supports the CMD_GET_FEAUTRES command */
  135. @@ -440,7 +443,7 @@ static int omnia_mcu_get_features(const
  136. err = omnia_cmd_read_raw(client->adapter, OMNIA_MCU_I2C_ADDR,
  137. CMD_GET_FEATURES, &reply, sizeof(reply));
  138. - if (err < 0)
  139. + if (err)
  140. return err;
  141. return le16_to_cpu(reply);