/* MCU controller I2C address 0x2a, needed for detecting MCU features */ #define OMNIA_MCU_I2C_ADDR 0x2a
/** * struct omnia_led - per-LED part of driver private data structure * @mc_cdev: multi-color LED class device * @subled_info: per-channel information * @cached_channels: cached values of per-channel brightness that was sent to the MCU * @on: whether the LED was set on * @hwtrig: whether the LED blinking was offloaded to the MCU * @reg: LED identifier to the MCU
*/ struct omnia_led { struct led_classdev_mc mc_cdev; struct mc_subled subled_info[OMNIA_LED_NUM_CHANNELS];
u8 cached_channels[OMNIA_LED_NUM_CHANNELS]; bool on, hwtrig; int reg;
};
staticint omnia_led_send_color_cmd(conststruct i2c_client *client, struct omnia_led *led)
{ int ret;
/* Send the color change command */
ret = omnia_cmd_set_color(client, led->reg, led->subled_info[0].brightness,
led->subled_info[1].brightness, led->subled_info[2].brightness); if (ret < 0) return ret;
/* Cache the RGB channel brightnesses */ for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i)
led->cached_channels[i] = led->subled_info[i].brightness;
return 0;
}
/* Determine if the computed RGB channels are different from the cached ones */ staticbool omnia_led_channels_changed(struct omnia_led *led)
{ for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i) if (led->subled_info[i].brightness != led->cached_channels[i]) returntrue;
/* * Only recalculate RGB brightnesses from intensities if brightness is * non-zero (if it is zero and the LED is in HW blinking mode, we use * max_brightness as brightness). Otherwise we won't be using them and * we can save ourselves some software divisions (Omnia's CPU does not * implement the division instruction).
*/ if (brightness || led->hwtrig) {
led_mc_calc_color_components(mc_cdev, brightness ?:
cdev->max_brightness);
/* * Send color command only if brightness is non-zero and the RGB * channel brightnesses changed.
*/ if (omnia_led_channels_changed(led))
err = omnia_led_send_color_cmd(leds->client, led);
}
/* * Send on/off state change only if (bool)brightness changed and the LED * is not being blinked by HW.
*/ if (!err && !led->hwtrig && !brightness != !led->on) {
u8 state = OMNIA_CMD_LED_STATE_LED(led->reg);
if (!led->on) { /* * If the LED is off (brightness was set to 0), the last * configured color was not necessarily sent to the MCU. * Recompute with max_brightness and send if needed.
*/
led_mc_calc_color_components(mc_cdev, cdev->max_brightness);
if (omnia_led_channels_changed(led))
err = omnia_led_send_color_cmd(leds->client, led);
}
if (!err) { /* Put the LED into MCU controlled mode */
err = omnia_cmd_write_u8(leds->client, OMNIA_CMD_LED_MODE,
OMNIA_CMD_LED_MODE_LED(led->reg)); if (!err)
led->hwtrig = true;
}
/* Put the LED into software mode */
err = omnia_cmd_write_u8(leds->client, OMNIA_CMD_LED_MODE,
OMNIA_CMD_LED_MODE_LED(led->reg) | OMNIA_CMD_LED_MODE_USER);
mutex_unlock(&leds->lock);
if (err)
dev_err(cdev->dev, "Cannot put LED to software mode: %i\n",
err);
}
ret = of_property_read_u32(np, "reg", &led->reg); if (ret || led->reg >= OMNIA_BOARD_LEDS) {
dev_warn(dev, "Node %pOF: must contain 'reg' property with values between 0 and %i\n",
np, OMNIA_BOARD_LEDS - 1); return 0;
}
ret = of_property_read_u32(np, "color", &color); if (ret || color != LED_COLOR_ID_RGB) {
dev_warn(dev, "Node %pOF: must contain 'color' property with value LED_COLOR_ID_RGB\n",
np); return 0;
}
/* Initial color is white */ for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i) {
led->subled_info[i].intensity = 255;
led->subled_info[i].brightness = 255;
led->subled_info[i].channel = i;
}
cdev = &led->mc_cdev.led_cdev;
cdev->max_brightness = 255;
cdev->brightness_set_blocking = omnia_led_brightness_set_blocking;
cdev->trigger_type = &omnia_hw_trigger_type; /* * Use the omnia-mcu trigger as the default trigger. It may be rewritten * by LED class from the linux,default-trigger property.
*/
cdev->default_trigger = omnia_hw_trigger.name;
/* Put the LED into software mode */
ret = omnia_cmd_write_u8(client, OMNIA_CMD_LED_MODE, OMNIA_CMD_LED_MODE_LED(led->reg) |
OMNIA_CMD_LED_MODE_USER); if (ret) return dev_err_probe(dev, ret, "Cannot set LED %pOF to software mode\n", np);
/* Disable the LED */
ret = omnia_cmd_write_u8(client, OMNIA_CMD_LED_STATE, OMNIA_CMD_LED_STATE_LED(led->reg)); if (ret) return dev_err_probe(dev, ret, "Cannot set LED %pOF brightness\n", np);
/* Set initial color and cache it */
ret = omnia_led_send_color_cmd(client, led); if (ret < 0) return dev_err_probe(dev, ret, "Cannot set LED %pOF initial color\n", np);
ret = devm_led_classdev_multicolor_register_ext(dev, &led->mc_cdev,
&init_data); if (ret < 0) return dev_err_probe(dev, ret, "Cannot register LED %pOF\n", np);
return 1;
}
/* * On the front panel of the Turris Omnia router there is also a button which * can be used to control the intensity of all the LEDs at once, so that if they * are too bright, user can dim them. * The microcontroller cycles between 8 levels of this global brightness (from * 100% to 0%), but this setting can have any integer value between 0 and 100. * It is therefore convenient to be able to change this setting from software. * We expose this setting via a sysfs attribute file called "brightness". This * file lives in the device directory of the LED controller, not an individual * LED, so it should not confuse users.
*/ static ssize_t brightness_show(struct device *dev, struct device_attribute *a, char *buf)
{ struct i2c_client *client = to_i2c_client(dev);
u8 reply; int err;
if (unlikely(!leds->brightness_knode)) { /* * Note that sysfs_get_dirent() may sleep. This is okay, because we are in threaded * context.
*/
leds->brightness_knode = sysfs_get_dirent(leds->client->dev.kobj.sd, "brightness"); if (!leds->brightness_knode) return IRQ_NONE;
}
if (!leds->client->irq) {
dev_info(dev, "Brightness change interrupt supported by MCU firmware but not described in device-tree\n");
return 0;
}
/* * Registering the brightness_knode destructor before requesting the IRQ ensures that on * removal the brightness_knode sysfs node is put only after the IRQ is freed. * This is needed because the interrupt handler uses the knode.
*/
ret = devm_add_action(dev, omnia_brightness_knode_put, leds); if (ret < 0) return ret;
count = of_get_available_child_count(np); if (count == 0) return dev_err_probe(dev, -ENODEV, "LEDs are not defined in device tree!\n"); if (count > OMNIA_BOARD_LEDS) return dev_err_probe(dev, -EINVAL, "Too many LEDs defined in device tree!\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.