1089381b2SMarek Behún // SPDX-License-Identifier: GPL-2.0
2089381b2SMarek Behún /*
3089381b2SMarek Behún * CZ.NIC's Turris Omnia LEDs driver
4089381b2SMarek Behún *
56de283b9SMarek Behún * 2020, 2023 by Marek Behún <kabel@kernel.org>
6089381b2SMarek Behún */
7089381b2SMarek Behún
8089381b2SMarek Behún #include <linux/i2c.h>
9089381b2SMarek Behún #include <linux/led-class-multicolor.h>
10089381b2SMarek Behún #include <linux/module.h>
11089381b2SMarek Behún #include <linux/mutex.h>
12089381b2SMarek Behún #include <linux/of.h>
13089381b2SMarek Behún #include "leds.h"
14089381b2SMarek Behún
15089381b2SMarek Behún #define OMNIA_BOARD_LEDS 12
16089381b2SMarek Behún #define OMNIA_LED_NUM_CHANNELS 3
17089381b2SMarek Behún
1843e9082fSMarek Behún /* MCU controller commands at I2C address 0x2a */
1943e9082fSMarek Behún #define OMNIA_MCU_I2C_ADDR 0x2a
2043e9082fSMarek Behún
2143e9082fSMarek Behún #define CMD_GET_STATUS_WORD 0x01
2243e9082fSMarek Behún #define STS_FEATURES_SUPPORTED BIT(2)
2343e9082fSMarek Behún
2443e9082fSMarek Behún #define CMD_GET_FEATURES 0x10
2543e9082fSMarek Behún #define FEAT_LED_GAMMA_CORRECTION BIT(5)
2643e9082fSMarek Behún
2743e9082fSMarek Behún /* LED controller commands at I2C address 0x2b */
2843e9082fSMarek Behún #define CMD_LED_MODE 0x03
29089381b2SMarek Behún #define CMD_LED_MODE_LED(l) ((l) & 0x0f)
30089381b2SMarek Behún #define CMD_LED_MODE_USER 0x10
31089381b2SMarek Behún
3243e9082fSMarek Behún #define CMD_LED_STATE 0x04
33089381b2SMarek Behún #define CMD_LED_STATE_LED(l) ((l) & 0x0f)
34089381b2SMarek Behún #define CMD_LED_STATE_ON 0x10
35089381b2SMarek Behún
3643e9082fSMarek Behún #define CMD_LED_COLOR 0x05
3743e9082fSMarek Behún #define CMD_LED_SET_BRIGHTNESS 0x07
3843e9082fSMarek Behún #define CMD_LED_GET_BRIGHTNESS 0x08
3943e9082fSMarek Behún
4043e9082fSMarek Behún #define CMD_SET_GAMMA_CORRECTION 0x30
4143e9082fSMarek Behún #define CMD_GET_GAMMA_CORRECTION 0x31
42089381b2SMarek Behún
43089381b2SMarek Behún struct omnia_led {
44089381b2SMarek Behún struct led_classdev_mc mc_cdev;
45089381b2SMarek Behún struct mc_subled subled_info[OMNIA_LED_NUM_CHANNELS];
469f028c9eSMarek Behún u8 cached_channels[OMNIA_LED_NUM_CHANNELS];
47cbd6954fSMarek Behún bool on, hwtrig;
48089381b2SMarek Behún int reg;
49089381b2SMarek Behún };
50089381b2SMarek Behún
51089381b2SMarek Behún #define to_omnia_led(l) container_of(l, struct omnia_led, mc_cdev)
52089381b2SMarek Behún
53089381b2SMarek Behún struct omnia_leds {
54089381b2SMarek Behún struct i2c_client *client;
55089381b2SMarek Behún struct mutex lock;
5643e9082fSMarek Behún bool has_gamma_correction;
57089381b2SMarek Behún struct omnia_led leds[];
58089381b2SMarek Behún };
59089381b2SMarek Behún
omnia_cmd_write_u8(const struct i2c_client * client,u8 cmd,u8 val)606de283b9SMarek Behún static int omnia_cmd_write_u8(const struct i2c_client *client, u8 cmd, u8 val)
616de283b9SMarek Behún {
626de283b9SMarek Behún u8 buf[2] = { cmd, val };
6378cbcfd8SMarek Behún int ret;
646de283b9SMarek Behún
6578cbcfd8SMarek Behún ret = i2c_master_send(client, buf, sizeof(buf));
6678cbcfd8SMarek Behún
6778cbcfd8SMarek Behún return ret < 0 ? ret : 0;
686de283b9SMarek Behún }
696de283b9SMarek Behún
omnia_cmd_read_raw(struct i2c_adapter * adapter,u8 addr,u8 cmd,void * reply,size_t len)7043e9082fSMarek Behún static int omnia_cmd_read_raw(struct i2c_adapter *adapter, u8 addr, u8 cmd,
7143e9082fSMarek Behún void *reply, size_t len)
726de283b9SMarek Behún {
736de283b9SMarek Behún struct i2c_msg msgs[2];
746de283b9SMarek Behún int ret;
756de283b9SMarek Behún
7643e9082fSMarek Behún msgs[0].addr = addr;
776de283b9SMarek Behún msgs[0].flags = 0;
786de283b9SMarek Behún msgs[0].len = 1;
796de283b9SMarek Behún msgs[0].buf = &cmd;
8043e9082fSMarek Behún msgs[1].addr = addr;
816de283b9SMarek Behún msgs[1].flags = I2C_M_RD;
8243e9082fSMarek Behún msgs[1].len = len;
8343e9082fSMarek Behún msgs[1].buf = reply;
846de283b9SMarek Behún
8543e9082fSMarek Behún ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
866de283b9SMarek Behún if (likely(ret == ARRAY_SIZE(msgs)))
8778cbcfd8SMarek Behún return 0;
886de283b9SMarek Behún else if (ret < 0)
896de283b9SMarek Behún return ret;
906de283b9SMarek Behún else
916de283b9SMarek Behún return -EIO;
926de283b9SMarek Behún }
936de283b9SMarek Behún
omnia_cmd_read_u8(const struct i2c_client * client,u8 cmd)9443e9082fSMarek Behún static int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd)
9543e9082fSMarek Behún {
9643e9082fSMarek Behún u8 reply;
9778cbcfd8SMarek Behún int err;
9843e9082fSMarek Behún
9978cbcfd8SMarek Behún err = omnia_cmd_read_raw(client->adapter, client->addr, cmd, &reply, 1);
10078cbcfd8SMarek Behún if (err)
10178cbcfd8SMarek Behún return err;
10243e9082fSMarek Behún
10343e9082fSMarek Behún return reply;
10443e9082fSMarek Behún }
10543e9082fSMarek Behún
omnia_led_send_color_cmd(const struct i2c_client * client,struct omnia_led * led)1069f028c9eSMarek Behún static int omnia_led_send_color_cmd(const struct i2c_client *client,
1079f028c9eSMarek Behún struct omnia_led *led)
1089f028c9eSMarek Behún {
1099f028c9eSMarek Behún char cmd[5];
1109f028c9eSMarek Behún int ret;
1119f028c9eSMarek Behún
1129f028c9eSMarek Behún cmd[0] = CMD_LED_COLOR;
1139f028c9eSMarek Behún cmd[1] = led->reg;
1149f028c9eSMarek Behún cmd[2] = led->subled_info[0].brightness;
1159f028c9eSMarek Behún cmd[3] = led->subled_info[1].brightness;
1169f028c9eSMarek Behún cmd[4] = led->subled_info[2].brightness;
1179f028c9eSMarek Behún
1189f028c9eSMarek Behún /* Send the color change command */
1199f028c9eSMarek Behún ret = i2c_master_send(client, cmd, 5);
1209f028c9eSMarek Behún if (ret < 0)
1219f028c9eSMarek Behún return ret;
1229f028c9eSMarek Behún
1239f028c9eSMarek Behún /* Cache the RGB channel brightnesses */
1249f028c9eSMarek Behún for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i)
1259f028c9eSMarek Behún led->cached_channels[i] = led->subled_info[i].brightness;
1269f028c9eSMarek Behún
1279f028c9eSMarek Behún return 0;
1289f028c9eSMarek Behún }
1299f028c9eSMarek Behún
1309f028c9eSMarek Behún /* Determine if the computed RGB channels are different from the cached ones */
omnia_led_channels_changed(struct omnia_led * led)1319f028c9eSMarek Behún static bool omnia_led_channels_changed(struct omnia_led *led)
1329f028c9eSMarek Behún {
1339f028c9eSMarek Behún for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i)
1349f028c9eSMarek Behún if (led->subled_info[i].brightness != led->cached_channels[i])
1359f028c9eSMarek Behún return true;
1369f028c9eSMarek Behún
1379f028c9eSMarek Behún return false;
1389f028c9eSMarek Behún }
1399f028c9eSMarek Behún
omnia_led_brightness_set_blocking(struct led_classdev * cdev,enum led_brightness brightness)140089381b2SMarek Behún static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
141089381b2SMarek Behún enum led_brightness brightness)
142089381b2SMarek Behún {
143089381b2SMarek Behún struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
144089381b2SMarek Behún struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
145089381b2SMarek Behún struct omnia_led *led = to_omnia_led(mc_cdev);
1469f028c9eSMarek Behún int err = 0;
147089381b2SMarek Behún
148089381b2SMarek Behún mutex_lock(&leds->lock);
149089381b2SMarek Behún
1509f028c9eSMarek Behún /*
1519f028c9eSMarek Behún * Only recalculate RGB brightnesses from intensities if brightness is
152cbd6954fSMarek Behún * non-zero (if it is zero and the LED is in HW blinking mode, we use
153cbd6954fSMarek Behún * max_brightness as brightness). Otherwise we won't be using them and
154cbd6954fSMarek Behún * we can save ourselves some software divisions (Omnia's CPU does not
155cbd6954fSMarek Behún * implement the division instruction).
1569f028c9eSMarek Behún */
157cbd6954fSMarek Behún if (brightness || led->hwtrig) {
158cbd6954fSMarek Behún led_mc_calc_color_components(mc_cdev, brightness ?:
159cbd6954fSMarek Behún cdev->max_brightness);
160089381b2SMarek Behún
1619f028c9eSMarek Behún /*
1629f028c9eSMarek Behún * Send color command only if brightness is non-zero and the RGB
1639f028c9eSMarek Behún * channel brightnesses changed.
1649f028c9eSMarek Behún */
1659f028c9eSMarek Behún if (omnia_led_channels_changed(led))
1669f028c9eSMarek Behún err = omnia_led_send_color_cmd(leds->client, led);
1679f028c9eSMarek Behún }
168089381b2SMarek Behún
169cbd6954fSMarek Behún /*
170cbd6954fSMarek Behún * Send on/off state change only if (bool)brightness changed and the LED
171cbd6954fSMarek Behún * is not being blinked by HW.
172cbd6954fSMarek Behún */
173cbd6954fSMarek Behún if (!err && !led->hwtrig && !brightness != !led->on) {
1749f028c9eSMarek Behún u8 state = CMD_LED_STATE_LED(led->reg);
1759f028c9eSMarek Behún
1769f028c9eSMarek Behún if (brightness)
177089381b2SMarek Behún state |= CMD_LED_STATE_ON;
178089381b2SMarek Behún
1799f028c9eSMarek Behún err = omnia_cmd_write_u8(leds->client, CMD_LED_STATE, state);
1809f028c9eSMarek Behún if (!err)
1819f028c9eSMarek Behún led->on = !!brightness;
1829f028c9eSMarek Behún }
183089381b2SMarek Behún
184089381b2SMarek Behún mutex_unlock(&leds->lock);
185089381b2SMarek Behún
1869f028c9eSMarek Behún return err;
187089381b2SMarek Behún }
188089381b2SMarek Behún
189cbd6954fSMarek Behún static struct led_hw_trigger_type omnia_hw_trigger_type;
190cbd6954fSMarek Behún
omnia_hwtrig_activate(struct led_classdev * cdev)191cbd6954fSMarek Behún static int omnia_hwtrig_activate(struct led_classdev *cdev)
192cbd6954fSMarek Behún {
193cbd6954fSMarek Behún struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
194cbd6954fSMarek Behún struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
195cbd6954fSMarek Behún struct omnia_led *led = to_omnia_led(mc_cdev);
196cbd6954fSMarek Behún int err = 0;
197cbd6954fSMarek Behún
198cbd6954fSMarek Behún mutex_lock(&leds->lock);
199cbd6954fSMarek Behún
200cbd6954fSMarek Behún if (!led->on) {
201cbd6954fSMarek Behún /*
202cbd6954fSMarek Behún * If the LED is off (brightness was set to 0), the last
203cbd6954fSMarek Behún * configured color was not necessarily sent to the MCU.
204cbd6954fSMarek Behún * Recompute with max_brightness and send if needed.
205cbd6954fSMarek Behún */
206cbd6954fSMarek Behún led_mc_calc_color_components(mc_cdev, cdev->max_brightness);
207cbd6954fSMarek Behún
208cbd6954fSMarek Behún if (omnia_led_channels_changed(led))
209cbd6954fSMarek Behún err = omnia_led_send_color_cmd(leds->client, led);
210cbd6954fSMarek Behún }
211cbd6954fSMarek Behún
212cbd6954fSMarek Behún if (!err) {
213cbd6954fSMarek Behún /* Put the LED into MCU controlled mode */
214cbd6954fSMarek Behún err = omnia_cmd_write_u8(leds->client, CMD_LED_MODE,
215cbd6954fSMarek Behún CMD_LED_MODE_LED(led->reg));
216cbd6954fSMarek Behún if (!err)
217cbd6954fSMarek Behún led->hwtrig = true;
218cbd6954fSMarek Behún }
219cbd6954fSMarek Behún
220cbd6954fSMarek Behún mutex_unlock(&leds->lock);
221cbd6954fSMarek Behún
222cbd6954fSMarek Behún return err;
223cbd6954fSMarek Behún }
224cbd6954fSMarek Behún
omnia_hwtrig_deactivate(struct led_classdev * cdev)225cbd6954fSMarek Behún static void omnia_hwtrig_deactivate(struct led_classdev *cdev)
226cbd6954fSMarek Behún {
227cbd6954fSMarek Behún struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
228cbd6954fSMarek Behún struct omnia_led *led = to_omnia_led(lcdev_to_mccdev(cdev));
229cbd6954fSMarek Behún int err;
230cbd6954fSMarek Behún
231cbd6954fSMarek Behún mutex_lock(&leds->lock);
232cbd6954fSMarek Behún
233cbd6954fSMarek Behún led->hwtrig = false;
234cbd6954fSMarek Behún
235cbd6954fSMarek Behún /* Put the LED into software mode */
236cbd6954fSMarek Behún err = omnia_cmd_write_u8(leds->client, CMD_LED_MODE,
237cbd6954fSMarek Behún CMD_LED_MODE_LED(led->reg) |
238cbd6954fSMarek Behún CMD_LED_MODE_USER);
239cbd6954fSMarek Behún
240cbd6954fSMarek Behún mutex_unlock(&leds->lock);
241cbd6954fSMarek Behún
24278cbcfd8SMarek Behún if (err)
243cbd6954fSMarek Behún dev_err(cdev->dev, "Cannot put LED to software mode: %i\n",
244cbd6954fSMarek Behún err);
245cbd6954fSMarek Behún }
246cbd6954fSMarek Behún
247cbd6954fSMarek Behún static struct led_trigger omnia_hw_trigger = {
248cbd6954fSMarek Behún .name = "omnia-mcu",
249cbd6954fSMarek Behún .activate = omnia_hwtrig_activate,
250cbd6954fSMarek Behún .deactivate = omnia_hwtrig_deactivate,
251cbd6954fSMarek Behún .trigger_type = &omnia_hw_trigger_type,
252cbd6954fSMarek Behún };
253cbd6954fSMarek Behún
omnia_led_register(struct i2c_client * client,struct omnia_led * led,struct device_node * np)254089381b2SMarek Behún static int omnia_led_register(struct i2c_client *client, struct omnia_led *led,
255089381b2SMarek Behún struct device_node *np)
256089381b2SMarek Behún {
257089381b2SMarek Behún struct led_init_data init_data = {};
258089381b2SMarek Behún struct device *dev = &client->dev;
259089381b2SMarek Behún struct led_classdev *cdev;
260089381b2SMarek Behún int ret, color;
261089381b2SMarek Behún
262089381b2SMarek Behún ret = of_property_read_u32(np, "reg", &led->reg);
263089381b2SMarek Behún if (ret || led->reg >= OMNIA_BOARD_LEDS) {
264089381b2SMarek Behún dev_warn(dev,
265089381b2SMarek Behún "Node %pOF: must contain 'reg' property with values between 0 and %i\n",
266089381b2SMarek Behún np, OMNIA_BOARD_LEDS - 1);
267089381b2SMarek Behún return 0;
268089381b2SMarek Behún }
269089381b2SMarek Behún
270089381b2SMarek Behún ret = of_property_read_u32(np, "color", &color);
27198650b08SMarek Behún if (ret || color != LED_COLOR_ID_RGB) {
272089381b2SMarek Behún dev_warn(dev,
27398650b08SMarek Behún "Node %pOF: must contain 'color' property with value LED_COLOR_ID_RGB\n",
274089381b2SMarek Behún np);
275089381b2SMarek Behún return 0;
276089381b2SMarek Behún }
277089381b2SMarek Behún
278089381b2SMarek Behún led->subled_info[0].color_index = LED_COLOR_ID_RED;
279089381b2SMarek Behún led->subled_info[1].color_index = LED_COLOR_ID_GREEN;
280089381b2SMarek Behún led->subled_info[2].color_index = LED_COLOR_ID_BLUE;
2819f028c9eSMarek Behún
2829f028c9eSMarek Behún /* Initial color is white */
2839f028c9eSMarek Behún for (int i = 0; i < OMNIA_LED_NUM_CHANNELS; ++i) {
2849f028c9eSMarek Behún led->subled_info[i].intensity = 255;
2859f028c9eSMarek Behún led->subled_info[i].brightness = 255;
2869f028c9eSMarek Behún led->subled_info[i].channel = i;
2879f028c9eSMarek Behún }
288089381b2SMarek Behún
289089381b2SMarek Behún led->mc_cdev.subled_info = led->subled_info;
290089381b2SMarek Behún led->mc_cdev.num_colors = OMNIA_LED_NUM_CHANNELS;
291089381b2SMarek Behún
292089381b2SMarek Behún init_data.fwnode = &np->fwnode;
293089381b2SMarek Behún
294089381b2SMarek Behún cdev = &led->mc_cdev.led_cdev;
295089381b2SMarek Behún cdev->max_brightness = 255;
296089381b2SMarek Behún cdev->brightness_set_blocking = omnia_led_brightness_set_blocking;
297cbd6954fSMarek Behún cdev->trigger_type = &omnia_hw_trigger_type;
298cbd6954fSMarek Behún /*
299cbd6954fSMarek Behún * Use the omnia-mcu trigger as the default trigger. It may be rewritten
300cbd6954fSMarek Behún * by LED class from the linux,default-trigger property.
301cbd6954fSMarek Behún */
302cbd6954fSMarek Behún cdev->default_trigger = omnia_hw_trigger.name;
303089381b2SMarek Behún
304089381b2SMarek Behún /* put the LED into software mode */
3056de283b9SMarek Behún ret = omnia_cmd_write_u8(client, CMD_LED_MODE,
306089381b2SMarek Behún CMD_LED_MODE_LED(led->reg) |
307089381b2SMarek Behún CMD_LED_MODE_USER);
30878cbcfd8SMarek Behún if (ret) {
3095d47ce1dSMarek Behún dev_err(dev, "Cannot set LED %pOF to software mode: %i\n", np,
3105d47ce1dSMarek Behún ret);
311089381b2SMarek Behún return ret;
312089381b2SMarek Behún }
313089381b2SMarek Behún
314089381b2SMarek Behún /* disable the LED */
3156de283b9SMarek Behún ret = omnia_cmd_write_u8(client, CMD_LED_STATE,
3165d47ce1dSMarek Behún CMD_LED_STATE_LED(led->reg));
31778cbcfd8SMarek Behún if (ret) {
318089381b2SMarek Behún dev_err(dev, "Cannot set LED %pOF brightness: %i\n", np, ret);
319089381b2SMarek Behún return ret;
320089381b2SMarek Behún }
321089381b2SMarek Behún
3229f028c9eSMarek Behún /* Set initial color and cache it */
3239f028c9eSMarek Behún ret = omnia_led_send_color_cmd(client, led);
3249f028c9eSMarek Behún if (ret < 0) {
3259f028c9eSMarek Behún dev_err(dev, "Cannot set LED %pOF initial color: %i\n", np,
3269f028c9eSMarek Behún ret);
3279f028c9eSMarek Behún return ret;
3289f028c9eSMarek Behún }
3299f028c9eSMarek Behún
3305d47ce1dSMarek Behún ret = devm_led_classdev_multicolor_register_ext(dev, &led->mc_cdev,
3315d47ce1dSMarek Behún &init_data);
332089381b2SMarek Behún if (ret < 0) {
333089381b2SMarek Behún dev_err(dev, "Cannot register LED %pOF: %i\n", np, ret);
334089381b2SMarek Behún return ret;
335089381b2SMarek Behún }
336089381b2SMarek Behún
337089381b2SMarek Behún return 1;
338089381b2SMarek Behún }
339089381b2SMarek Behún
340089381b2SMarek Behún /*
341089381b2SMarek Behún * On the front panel of the Turris Omnia router there is also a button which
342089381b2SMarek Behún * can be used to control the intensity of all the LEDs at once, so that if they
343089381b2SMarek Behún * are too bright, user can dim them.
344089381b2SMarek Behún * The microcontroller cycles between 8 levels of this global brightness (from
345089381b2SMarek Behún * 100% to 0%), but this setting can have any integer value between 0 and 100.
346089381b2SMarek Behún * It is therefore convenient to be able to change this setting from software.
347089381b2SMarek Behún * We expose this setting via a sysfs attribute file called "brightness". This
348089381b2SMarek Behún * file lives in the device directory of the LED controller, not an individual
349089381b2SMarek Behún * LED, so it should not confuse users.
350089381b2SMarek Behún */
brightness_show(struct device * dev,struct device_attribute * a,char * buf)3515d47ce1dSMarek Behún static ssize_t brightness_show(struct device *dev, struct device_attribute *a,
3525d47ce1dSMarek Behún char *buf)
353089381b2SMarek Behún {
354089381b2SMarek Behún struct i2c_client *client = to_i2c_client(dev);
355089381b2SMarek Behún int ret;
356089381b2SMarek Behún
3576de283b9SMarek Behún ret = omnia_cmd_read_u8(client, CMD_LED_GET_BRIGHTNESS);
358089381b2SMarek Behún
359089381b2SMarek Behún if (ret < 0)
360089381b2SMarek Behún return ret;
361089381b2SMarek Behún
36272a29725SMarek Behún return sysfs_emit(buf, "%d\n", ret);
363089381b2SMarek Behún }
364089381b2SMarek Behún
brightness_store(struct device * dev,struct device_attribute * a,const char * buf,size_t count)3655d47ce1dSMarek Behún static ssize_t brightness_store(struct device *dev, struct device_attribute *a,
3665d47ce1dSMarek Behún const char *buf, size_t count)
367089381b2SMarek Behún {
368089381b2SMarek Behún struct i2c_client *client = to_i2c_client(dev);
369fca050bbSMarek Behún unsigned long brightness;
37078cbcfd8SMarek Behún int err;
371089381b2SMarek Behún
372fca050bbSMarek Behún if (kstrtoul(buf, 10, &brightness))
373089381b2SMarek Behún return -EINVAL;
374089381b2SMarek Behún
375089381b2SMarek Behún if (brightness > 100)
376089381b2SMarek Behún return -EINVAL;
377089381b2SMarek Behún
37878cbcfd8SMarek Behún err = omnia_cmd_write_u8(client, CMD_LED_SET_BRIGHTNESS, brightness);
379089381b2SMarek Behún
38078cbcfd8SMarek Behún return err ?: count;
381089381b2SMarek Behún }
382089381b2SMarek Behún static DEVICE_ATTR_RW(brightness);
383089381b2SMarek Behún
gamma_correction_show(struct device * dev,struct device_attribute * a,char * buf)38443e9082fSMarek Behún static ssize_t gamma_correction_show(struct device *dev,
38543e9082fSMarek Behún struct device_attribute *a, char *buf)
38643e9082fSMarek Behún {
38743e9082fSMarek Behún struct i2c_client *client = to_i2c_client(dev);
38843e9082fSMarek Behún struct omnia_leds *leds = i2c_get_clientdata(client);
38943e9082fSMarek Behún int ret;
39043e9082fSMarek Behún
39143e9082fSMarek Behún if (leds->has_gamma_correction) {
39243e9082fSMarek Behún ret = omnia_cmd_read_u8(client, CMD_GET_GAMMA_CORRECTION);
39343e9082fSMarek Behún if (ret < 0)
39443e9082fSMarek Behún return ret;
39543e9082fSMarek Behún } else {
39643e9082fSMarek Behún ret = 0;
39743e9082fSMarek Behún }
39843e9082fSMarek Behún
39943e9082fSMarek Behún return sysfs_emit(buf, "%d\n", !!ret);
40043e9082fSMarek Behún }
40143e9082fSMarek Behún
gamma_correction_store(struct device * dev,struct device_attribute * a,const char * buf,size_t count)40243e9082fSMarek Behún static ssize_t gamma_correction_store(struct device *dev,
40343e9082fSMarek Behún struct device_attribute *a,
40443e9082fSMarek Behún const char *buf, size_t count)
40543e9082fSMarek Behún {
40643e9082fSMarek Behún struct i2c_client *client = to_i2c_client(dev);
40743e9082fSMarek Behún struct omnia_leds *leds = i2c_get_clientdata(client);
40843e9082fSMarek Behún bool val;
40978cbcfd8SMarek Behún int err;
41043e9082fSMarek Behún
41143e9082fSMarek Behún if (!leds->has_gamma_correction)
41243e9082fSMarek Behún return -EOPNOTSUPP;
41343e9082fSMarek Behún
41443e9082fSMarek Behún if (kstrtobool(buf, &val) < 0)
41543e9082fSMarek Behún return -EINVAL;
41643e9082fSMarek Behún
41778cbcfd8SMarek Behún err = omnia_cmd_write_u8(client, CMD_SET_GAMMA_CORRECTION, val);
41843e9082fSMarek Behún
41978cbcfd8SMarek Behún return err ?: count;
42043e9082fSMarek Behún }
42143e9082fSMarek Behún static DEVICE_ATTR_RW(gamma_correction);
42243e9082fSMarek Behún
423089381b2SMarek Behún static struct attribute *omnia_led_controller_attrs[] = {
424089381b2SMarek Behún &dev_attr_brightness.attr,
42543e9082fSMarek Behún &dev_attr_gamma_correction.attr,
426089381b2SMarek Behún NULL,
427089381b2SMarek Behún };
428089381b2SMarek Behún ATTRIBUTE_GROUPS(omnia_led_controller);
429089381b2SMarek Behún
omnia_mcu_get_features(const struct i2c_client * client)43043e9082fSMarek Behún static int omnia_mcu_get_features(const struct i2c_client *client)
43143e9082fSMarek Behún {
43243e9082fSMarek Behún u16 reply;
43343e9082fSMarek Behún int err;
43443e9082fSMarek Behún
43543e9082fSMarek Behún err = omnia_cmd_read_raw(client->adapter, OMNIA_MCU_I2C_ADDR,
43643e9082fSMarek Behún CMD_GET_STATUS_WORD, &reply, sizeof(reply));
43778cbcfd8SMarek Behún if (err)
43843e9082fSMarek Behún return err;
43943e9082fSMarek Behún
44043e9082fSMarek Behún /* Check whether MCU firmware supports the CMD_GET_FEAUTRES command */
44143e9082fSMarek Behún if (!(le16_to_cpu(reply) & STS_FEATURES_SUPPORTED))
44243e9082fSMarek Behún return 0;
44343e9082fSMarek Behún
44443e9082fSMarek Behún err = omnia_cmd_read_raw(client->adapter, OMNIA_MCU_I2C_ADDR,
44543e9082fSMarek Behún CMD_GET_FEATURES, &reply, sizeof(reply));
44678cbcfd8SMarek Behún if (err)
44743e9082fSMarek Behún return err;
44843e9082fSMarek Behún
44943e9082fSMarek Behún return le16_to_cpu(reply);
45043e9082fSMarek Behún }
45143e9082fSMarek Behún
omnia_leds_probe(struct i2c_client * client)45249346304SUwe Kleine-König static int omnia_leds_probe(struct i2c_client *client)
453089381b2SMarek Behún {
454089381b2SMarek Behún struct device *dev = &client->dev;
455122d57e2SKrzysztof Kozlowski struct device_node *np = dev_of_node(dev);
456089381b2SMarek Behún struct omnia_leds *leds;
457089381b2SMarek Behún struct omnia_led *led;
458089381b2SMarek Behún int ret, count;
459089381b2SMarek Behún
460089381b2SMarek Behún count = of_get_available_child_count(np);
461089381b2SMarek Behún if (!count) {
462089381b2SMarek Behún dev_err(dev, "LEDs are not defined in device tree!\n");
463089381b2SMarek Behún return -ENODEV;
464089381b2SMarek Behún } else if (count > OMNIA_BOARD_LEDS) {
465089381b2SMarek Behún dev_err(dev, "Too many LEDs defined in device tree!\n");
466089381b2SMarek Behún return -EINVAL;
467089381b2SMarek Behún }
468089381b2SMarek Behún
469089381b2SMarek Behún leds = devm_kzalloc(dev, struct_size(leds, leds, count), GFP_KERNEL);
470089381b2SMarek Behún if (!leds)
471089381b2SMarek Behún return -ENOMEM;
472089381b2SMarek Behún
473089381b2SMarek Behún leds->client = client;
474089381b2SMarek Behún i2c_set_clientdata(client, leds);
475089381b2SMarek Behún
47643e9082fSMarek Behún ret = omnia_mcu_get_features(client);
47743e9082fSMarek Behún if (ret < 0) {
47843e9082fSMarek Behún dev_err(dev, "Cannot determine MCU supported features: %d\n",
47943e9082fSMarek Behún ret);
48043e9082fSMarek Behún return ret;
48143e9082fSMarek Behún }
48243e9082fSMarek Behún
48343e9082fSMarek Behún leds->has_gamma_correction = ret & FEAT_LED_GAMMA_CORRECTION;
48443e9082fSMarek Behún if (!leds->has_gamma_correction) {
48543e9082fSMarek Behún dev_info(dev,
48643e9082fSMarek Behún "Your board's MCU firmware does not support the LED gamma correction feature.\n");
48743e9082fSMarek Behún dev_info(dev,
48843e9082fSMarek Behún "Consider upgrading MCU firmware with the omnia-mcutool utility.\n");
48943e9082fSMarek Behún }
49043e9082fSMarek Behún
491089381b2SMarek Behún mutex_init(&leds->lock);
492089381b2SMarek Behún
493cbd6954fSMarek Behún ret = devm_led_trigger_register(dev, &omnia_hw_trigger);
494cbd6954fSMarek Behún if (ret < 0) {
495cbd6954fSMarek Behún dev_err(dev, "Cannot register private LED trigger: %d\n", ret);
496cbd6954fSMarek Behún return ret;
497cbd6954fSMarek Behún }
498cbd6954fSMarek Behún
499089381b2SMarek Behún led = &leds->leds[0];
500122d57e2SKrzysztof Kozlowski for_each_available_child_of_node_scoped(np, child) {
501089381b2SMarek Behún ret = omnia_led_register(client, led, child);
502122d57e2SKrzysztof Kozlowski if (ret < 0)
503089381b2SMarek Behún return ret;
504089381b2SMarek Behún
505089381b2SMarek Behún led += ret;
506089381b2SMarek Behún }
507089381b2SMarek Behún
508089381b2SMarek Behún return 0;
509089381b2SMarek Behún }
510089381b2SMarek Behún
omnia_leds_remove(struct i2c_client * client)511ed5c2f5fSUwe Kleine-König static void omnia_leds_remove(struct i2c_client *client)
512089381b2SMarek Behún {
513493d2e43SMarek Behún u8 buf[5];
514089381b2SMarek Behún
515089381b2SMarek Behún /* put all LEDs into default (HW triggered) mode */
5166de283b9SMarek Behún omnia_cmd_write_u8(client, CMD_LED_MODE,
517089381b2SMarek Behún CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
518089381b2SMarek Behún
519089381b2SMarek Behún /* set all LEDs color to [255, 255, 255] */
520493d2e43SMarek Behún buf[0] = CMD_LED_COLOR;
521493d2e43SMarek Behún buf[1] = OMNIA_BOARD_LEDS;
522493d2e43SMarek Behún buf[2] = 255;
523493d2e43SMarek Behún buf[3] = 255;
524493d2e43SMarek Behún buf[4] = 255;
525089381b2SMarek Behún
526089381b2SMarek Behún i2c_master_send(client, buf, 5);
527089381b2SMarek Behún }
528089381b2SMarek Behún
529089381b2SMarek Behún static const struct of_device_id of_omnia_leds_match[] = {
530089381b2SMarek Behún { .compatible = "cznic,turris-omnia-leds", },
531089381b2SMarek Behún {},
532089381b2SMarek Behún };
533*64c38866SLiao Chen MODULE_DEVICE_TABLE(of, of_omnia_leds_match);
534089381b2SMarek Behún
535089381b2SMarek Behún static const struct i2c_device_id omnia_id[] = {
536c0e3d2beSUwe Kleine-König { "omnia" },
537089381b2SMarek Behún { }
538089381b2SMarek Behún };
5399d0150dbSZou Wei MODULE_DEVICE_TABLE(i2c, omnia_id);
540089381b2SMarek Behún
541089381b2SMarek Behún static struct i2c_driver omnia_leds_driver = {
542d9ff8a8eSUwe Kleine-König .probe = omnia_leds_probe,
543089381b2SMarek Behún .remove = omnia_leds_remove,
544089381b2SMarek Behún .id_table = omnia_id,
545089381b2SMarek Behún .driver = {
546089381b2SMarek Behún .name = "leds-turris-omnia",
547089381b2SMarek Behún .of_match_table = of_omnia_leds_match,
548a01633cdSGreg Kroah-Hartman .dev_groups = omnia_led_controller_groups,
549089381b2SMarek Behún },
550089381b2SMarek Behún };
551089381b2SMarek Behún
552089381b2SMarek Behún module_i2c_driver(omnia_leds_driver);
553089381b2SMarek Behún
554b37c3848SMarek Behún MODULE_AUTHOR("Marek Behun <kabel@kernel.org>");
555089381b2SMarek Behún MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
556089381b2SMarek Behún MODULE_LICENSE("GPL v2");
557