Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * CZ.NIC's Turris Omnia LEDs driver
0004  *
0005  * 2020 by Marek BehĂșn <kabel@kernel.org>
0006  */
0007 
0008 #include <linux/i2c.h>
0009 #include <linux/led-class-multicolor.h>
0010 #include <linux/module.h>
0011 #include <linux/mutex.h>
0012 #include <linux/of.h>
0013 #include "leds.h"
0014 
0015 #define OMNIA_BOARD_LEDS    12
0016 #define OMNIA_LED_NUM_CHANNELS  3
0017 
0018 #define CMD_LED_MODE        3
0019 #define CMD_LED_MODE_LED(l) ((l) & 0x0f)
0020 #define CMD_LED_MODE_USER   0x10
0021 
0022 #define CMD_LED_STATE       4
0023 #define CMD_LED_STATE_LED(l)    ((l) & 0x0f)
0024 #define CMD_LED_STATE_ON    0x10
0025 
0026 #define CMD_LED_COLOR       5
0027 #define CMD_LED_SET_BRIGHTNESS  7
0028 #define CMD_LED_GET_BRIGHTNESS  8
0029 
0030 struct omnia_led {
0031     struct led_classdev_mc mc_cdev;
0032     struct mc_subled subled_info[OMNIA_LED_NUM_CHANNELS];
0033     int reg;
0034 };
0035 
0036 #define to_omnia_led(l)     container_of(l, struct omnia_led, mc_cdev)
0037 
0038 struct omnia_leds {
0039     struct i2c_client *client;
0040     struct mutex lock;
0041     struct omnia_led leds[];
0042 };
0043 
0044 static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
0045                          enum led_brightness brightness)
0046 {
0047     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
0048     struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
0049     struct omnia_led *led = to_omnia_led(mc_cdev);
0050     u8 buf[5], state;
0051     int ret;
0052 
0053     mutex_lock(&leds->lock);
0054 
0055     led_mc_calc_color_components(&led->mc_cdev, brightness);
0056 
0057     buf[0] = CMD_LED_COLOR;
0058     buf[1] = led->reg;
0059     buf[2] = mc_cdev->subled_info[0].brightness;
0060     buf[3] = mc_cdev->subled_info[1].brightness;
0061     buf[4] = mc_cdev->subled_info[2].brightness;
0062 
0063     state = CMD_LED_STATE_LED(led->reg);
0064     if (buf[2] || buf[3] || buf[4])
0065         state |= CMD_LED_STATE_ON;
0066 
0067     ret = i2c_smbus_write_byte_data(leds->client, CMD_LED_STATE, state);
0068     if (ret >= 0 && (state & CMD_LED_STATE_ON))
0069         ret = i2c_master_send(leds->client, buf, 5);
0070 
0071     mutex_unlock(&leds->lock);
0072 
0073     return ret;
0074 }
0075 
0076 static int omnia_led_register(struct i2c_client *client, struct omnia_led *led,
0077                   struct device_node *np)
0078 {
0079     struct led_init_data init_data = {};
0080     struct device *dev = &client->dev;
0081     struct led_classdev *cdev;
0082     int ret, color;
0083 
0084     ret = of_property_read_u32(np, "reg", &led->reg);
0085     if (ret || led->reg >= OMNIA_BOARD_LEDS) {
0086         dev_warn(dev,
0087              "Node %pOF: must contain 'reg' property with values between 0 and %i\n",
0088              np, OMNIA_BOARD_LEDS - 1);
0089         return 0;
0090     }
0091 
0092     ret = of_property_read_u32(np, "color", &color);
0093     if (ret || color != LED_COLOR_ID_RGB) {
0094         dev_warn(dev,
0095              "Node %pOF: must contain 'color' property with value LED_COLOR_ID_RGB\n",
0096              np);
0097         return 0;
0098     }
0099 
0100     led->subled_info[0].color_index = LED_COLOR_ID_RED;
0101     led->subled_info[0].channel = 0;
0102     led->subled_info[1].color_index = LED_COLOR_ID_GREEN;
0103     led->subled_info[1].channel = 1;
0104     led->subled_info[2].color_index = LED_COLOR_ID_BLUE;
0105     led->subled_info[2].channel = 2;
0106 
0107     led->mc_cdev.subled_info = led->subled_info;
0108     led->mc_cdev.num_colors = OMNIA_LED_NUM_CHANNELS;
0109 
0110     init_data.fwnode = &np->fwnode;
0111 
0112     cdev = &led->mc_cdev.led_cdev;
0113     cdev->max_brightness = 255;
0114     cdev->brightness_set_blocking = omnia_led_brightness_set_blocking;
0115 
0116     /* put the LED into software mode */
0117     ret = i2c_smbus_write_byte_data(client, CMD_LED_MODE,
0118                     CMD_LED_MODE_LED(led->reg) |
0119                     CMD_LED_MODE_USER);
0120     if (ret < 0) {
0121         dev_err(dev, "Cannot set LED %pOF to software mode: %i\n", np,
0122             ret);
0123         return ret;
0124     }
0125 
0126     /* disable the LED */
0127     ret = i2c_smbus_write_byte_data(client, CMD_LED_STATE,
0128                     CMD_LED_STATE_LED(led->reg));
0129     if (ret < 0) {
0130         dev_err(dev, "Cannot set LED %pOF brightness: %i\n", np, ret);
0131         return ret;
0132     }
0133 
0134     ret = devm_led_classdev_multicolor_register_ext(dev, &led->mc_cdev,
0135                             &init_data);
0136     if (ret < 0) {
0137         dev_err(dev, "Cannot register LED %pOF: %i\n", np, ret);
0138         return ret;
0139     }
0140 
0141     return 1;
0142 }
0143 
0144 /*
0145  * On the front panel of the Turris Omnia router there is also a button which
0146  * can be used to control the intensity of all the LEDs at once, so that if they
0147  * are too bright, user can dim them.
0148  * The microcontroller cycles between 8 levels of this global brightness (from
0149  * 100% to 0%), but this setting can have any integer value between 0 and 100.
0150  * It is therefore convenient to be able to change this setting from software.
0151  * We expose this setting via a sysfs attribute file called "brightness". This
0152  * file lives in the device directory of the LED controller, not an individual
0153  * LED, so it should not confuse users.
0154  */
0155 static ssize_t brightness_show(struct device *dev, struct device_attribute *a,
0156                    char *buf)
0157 {
0158     struct i2c_client *client = to_i2c_client(dev);
0159     struct omnia_leds *leds = i2c_get_clientdata(client);
0160     int ret;
0161 
0162     mutex_lock(&leds->lock);
0163     ret = i2c_smbus_read_byte_data(client, CMD_LED_GET_BRIGHTNESS);
0164     mutex_unlock(&leds->lock);
0165 
0166     if (ret < 0)
0167         return ret;
0168 
0169     return sprintf(buf, "%d\n", ret);
0170 }
0171 
0172 static ssize_t brightness_store(struct device *dev, struct device_attribute *a,
0173                 const char *buf, size_t count)
0174 {
0175     struct i2c_client *client = to_i2c_client(dev);
0176     struct omnia_leds *leds = i2c_get_clientdata(client);
0177     unsigned long brightness;
0178     int ret;
0179 
0180     if (kstrtoul(buf, 10, &brightness))
0181         return -EINVAL;
0182 
0183     if (brightness > 100)
0184         return -EINVAL;
0185 
0186     mutex_lock(&leds->lock);
0187     ret = i2c_smbus_write_byte_data(client, CMD_LED_SET_BRIGHTNESS,
0188                     (u8)brightness);
0189     mutex_unlock(&leds->lock);
0190 
0191     if (ret < 0)
0192         return ret;
0193 
0194     return count;
0195 }
0196 static DEVICE_ATTR_RW(brightness);
0197 
0198 static struct attribute *omnia_led_controller_attrs[] = {
0199     &dev_attr_brightness.attr,
0200     NULL,
0201 };
0202 ATTRIBUTE_GROUPS(omnia_led_controller);
0203 
0204 static int omnia_leds_probe(struct i2c_client *client,
0205                 const struct i2c_device_id *id)
0206 {
0207     struct device *dev = &client->dev;
0208     struct device_node *np = dev_of_node(dev), *child;
0209     struct omnia_leds *leds;
0210     struct omnia_led *led;
0211     int ret, count;
0212 
0213     count = of_get_available_child_count(np);
0214     if (!count) {
0215         dev_err(dev, "LEDs are not defined in device tree!\n");
0216         return -ENODEV;
0217     } else if (count > OMNIA_BOARD_LEDS) {
0218         dev_err(dev, "Too many LEDs defined in device tree!\n");
0219         return -EINVAL;
0220     }
0221 
0222     leds = devm_kzalloc(dev, struct_size(leds, leds, count), GFP_KERNEL);
0223     if (!leds)
0224         return -ENOMEM;
0225 
0226     leds->client = client;
0227     i2c_set_clientdata(client, leds);
0228 
0229     mutex_init(&leds->lock);
0230 
0231     led = &leds->leds[0];
0232     for_each_available_child_of_node(np, child) {
0233         ret = omnia_led_register(client, led, child);
0234         if (ret < 0) {
0235             of_node_put(child);
0236             return ret;
0237         }
0238 
0239         led += ret;
0240     }
0241 
0242     return 0;
0243 }
0244 
0245 static int omnia_leds_remove(struct i2c_client *client)
0246 {
0247     u8 buf[5];
0248 
0249     /* put all LEDs into default (HW triggered) mode */
0250     i2c_smbus_write_byte_data(client, CMD_LED_MODE,
0251                   CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
0252 
0253     /* set all LEDs color to [255, 255, 255] */
0254     buf[0] = CMD_LED_COLOR;
0255     buf[1] = OMNIA_BOARD_LEDS;
0256     buf[2] = 255;
0257     buf[3] = 255;
0258     buf[4] = 255;
0259 
0260     i2c_master_send(client, buf, 5);
0261 
0262     return 0;
0263 }
0264 
0265 static const struct of_device_id of_omnia_leds_match[] = {
0266     { .compatible = "cznic,turris-omnia-leds", },
0267     {},
0268 };
0269 
0270 static const struct i2c_device_id omnia_id[] = {
0271     { "omnia", 0 },
0272     { }
0273 };
0274 MODULE_DEVICE_TABLE(i2c, omnia_id);
0275 
0276 static struct i2c_driver omnia_leds_driver = {
0277     .probe      = omnia_leds_probe,
0278     .remove     = omnia_leds_remove,
0279     .id_table   = omnia_id,
0280     .driver     = {
0281         .name   = "leds-turris-omnia",
0282         .of_match_table = of_omnia_leds_match,
0283         .dev_groups = omnia_led_controller_groups,
0284     },
0285 };
0286 
0287 module_i2c_driver(omnia_leds_driver);
0288 
0289 MODULE_AUTHOR("Marek Behun <kabel@kernel.org>");
0290 MODULE_DESCRIPTION("CZ.NIC's Turris Omnia LEDs");
0291 MODULE_LICENSE("GPL v2");