linux/drivers/staging/greybus/light.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Greybus Lights protocol driver.
   4 *
   5 * Copyright 2015 Google Inc.
   6 * Copyright 2015 Linaro Ltd.
   7 */
   8
   9#include <linux/kernel.h>
  10#include <linux/leds.h>
  11#include <linux/led-class-flash.h>
  12#include <linux/module.h>
  13#include <linux/slab.h>
  14#include <media/v4l2-flash-led-class.h>
  15
  16#include "greybus.h"
  17#include "greybus_protocols.h"
  18
  19#define NAMES_MAX       32
  20
  21struct gb_channel {
  22        u8                              id;
  23        u32                             flags;
  24        u32                             color;
  25        char                            *color_name;
  26        u8                              fade_in;
  27        u8                              fade_out;
  28        u32                             mode;
  29        char                            *mode_name;
  30        struct attribute                **attrs;
  31        struct attribute_group          *attr_group;
  32        const struct attribute_group    **attr_groups;
  33        struct led_classdev             *led;
  34#if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
  35        struct led_classdev_flash       fled;
  36        struct led_flash_setting        intensity_uA;
  37        struct led_flash_setting        timeout_us;
  38#else
  39        struct led_classdev             cled;
  40#endif
  41        struct gb_light                 *light;
  42        bool                            is_registered;
  43        bool                            releasing;
  44        bool                            strobe_state;
  45        bool                            active;
  46        struct mutex                    lock;
  47};
  48
  49struct gb_light {
  50        u8                      id;
  51        char                    *name;
  52        struct gb_lights        *glights;
  53        u32                     flags;
  54        u8                      channels_count;
  55        struct gb_channel       *channels;
  56        bool                    has_flash;
  57        bool                    ready;
  58#if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
  59        struct v4l2_flash       *v4l2_flash;
  60        struct v4l2_flash       *v4l2_flash_ind;
  61#endif
  62};
  63
  64struct gb_lights {
  65        struct gb_connection    *connection;
  66        u8                      lights_count;
  67        struct gb_light         *lights;
  68        struct mutex            lights_lock;
  69};
  70
  71static void gb_lights_channel_free(struct gb_channel *channel);
  72
  73static struct gb_connection *get_conn_from_channel(struct gb_channel *channel)
  74{
  75        return channel->light->glights->connection;
  76}
  77
  78static struct gb_connection *get_conn_from_light(struct gb_light *light)
  79{
  80        return light->glights->connection;
  81}
  82
  83static bool is_channel_flash(struct gb_channel *channel)
  84{
  85        return !!(channel->mode & (GB_CHANNEL_MODE_FLASH | GB_CHANNEL_MODE_TORCH
  86                                   | GB_CHANNEL_MODE_INDICATOR));
  87}
  88
  89#if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
  90static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
  91{
  92        struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
  93
  94        return container_of(fled_cdev, struct gb_channel, fled);
  95}
  96
  97static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
  98{
  99        return &channel->fled.led_cdev;
 100}
 101
 102static struct gb_channel *get_channel_from_mode(struct gb_light *light,
 103                                                u32 mode)
 104{
 105        struct gb_channel *channel = NULL;
 106        int i;
 107
 108        for (i = 0; i < light->channels_count; i++) {
 109                channel = &light->channels[i];
 110                if (channel && channel->mode == mode)
 111                        break;
 112        }
 113        return channel;
 114}
 115
 116static int __gb_lights_flash_intensity_set(struct gb_channel *channel,
 117                                           u32 intensity)
 118{
 119        struct gb_connection *connection = get_conn_from_channel(channel);
 120        struct gb_bundle *bundle = connection->bundle;
 121        struct gb_lights_set_flash_intensity_request req;
 122        int ret;
 123
 124        if (channel->releasing)
 125                return -ESHUTDOWN;
 126
 127        ret = gb_pm_runtime_get_sync(bundle);
 128        if (ret < 0)
 129                return ret;
 130
 131        req.light_id = channel->light->id;
 132        req.channel_id = channel->id;
 133        req.intensity_uA = cpu_to_le32(intensity);
 134
 135        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_INTENSITY,
 136                                &req, sizeof(req), NULL, 0);
 137
 138        gb_pm_runtime_put_autosuspend(bundle);
 139
 140        return ret;
 141}
 142
 143static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
 144{
 145        u32 intensity;
 146
 147        /* If the channel is flash we need to get the attached torch channel */
 148        if (channel->mode & GB_CHANNEL_MODE_FLASH)
 149                channel = get_channel_from_mode(channel->light,
 150                                                GB_CHANNEL_MODE_TORCH);
 151
 152        /* For not flash we need to convert brightness to intensity */
 153        intensity = channel->intensity_uA.min +
 154                        (channel->intensity_uA.step * channel->led->brightness);
 155
 156        return __gb_lights_flash_intensity_set(channel, intensity);
 157}
 158#else
 159static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
 160{
 161        return container_of(cdev, struct gb_channel, cled);
 162}
 163
 164static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
 165{
 166        return &channel->cled;
 167}
 168
 169static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
 170{
 171        return 0;
 172}
 173#endif
 174
 175static int gb_lights_color_set(struct gb_channel *channel, u32 color);
 176static int gb_lights_fade_set(struct gb_channel *channel);
 177
 178static void led_lock(struct led_classdev *cdev)
 179{
 180        mutex_lock(&cdev->led_access);
 181}
 182
 183static void led_unlock(struct led_classdev *cdev)
 184{
 185        mutex_unlock(&cdev->led_access);
 186}
 187
 188#define gb_lights_fade_attr(__dir)                                      \
 189static ssize_t fade_##__dir##_show(struct device *dev,                  \
 190                                   struct device_attribute *attr,       \
 191                                   char *buf)                           \
 192{                                                                       \
 193        struct led_classdev *cdev = dev_get_drvdata(dev);               \
 194        struct gb_channel *channel = get_channel_from_cdev(cdev);       \
 195                                                                        \
 196        return sprintf(buf, "%u\n", channel->fade_##__dir);             \
 197}                                                                       \
 198                                                                        \
 199static ssize_t fade_##__dir##_store(struct device *dev,                 \
 200                                    struct device_attribute *attr,      \
 201                                    const char *buf, size_t size)       \
 202{                                                                       \
 203        struct led_classdev *cdev = dev_get_drvdata(dev);               \
 204        struct gb_channel *channel = get_channel_from_cdev(cdev);       \
 205        u8 fade;                                                        \
 206        int ret;                                                        \
 207                                                                        \
 208        led_lock(cdev);                                                 \
 209        if (led_sysfs_is_disabled(cdev)) {                              \
 210                ret = -EBUSY;                                           \
 211                goto unlock;                                            \
 212        }                                                               \
 213                                                                        \
 214        ret = kstrtou8(buf, 0, &fade);                                  \
 215        if (ret < 0) {                                                  \
 216                dev_err(dev, "could not parse fade value %d\n", ret);   \
 217                goto unlock;                                            \
 218        }                                                               \
 219        if (channel->fade_##__dir == fade)                              \
 220                goto unlock;                                            \
 221        channel->fade_##__dir = fade;                                   \
 222                                                                        \
 223        ret = gb_lights_fade_set(channel);                              \
 224        if (ret < 0)                                                    \
 225                goto unlock;                                            \
 226                                                                        \
 227        ret = size;                                                     \
 228unlock:                                                                 \
 229        led_unlock(cdev);                                               \
 230        return ret;                                                     \
 231}                                                                       \
 232static DEVICE_ATTR_RW(fade_##__dir)
 233
 234gb_lights_fade_attr(in);
 235gb_lights_fade_attr(out);
 236
 237static ssize_t color_show(struct device *dev, struct device_attribute *attr,
 238                          char *buf)
 239{
 240        struct led_classdev *cdev = dev_get_drvdata(dev);
 241        struct gb_channel *channel = get_channel_from_cdev(cdev);
 242
 243        return sprintf(buf, "0x%08x\n", channel->color);
 244}
 245
 246static ssize_t color_store(struct device *dev, struct device_attribute *attr,
 247                           const char *buf, size_t size)
 248{
 249        struct led_classdev *cdev = dev_get_drvdata(dev);
 250        struct gb_channel *channel = get_channel_from_cdev(cdev);
 251        u32 color;
 252        int ret;
 253
 254        led_lock(cdev);
 255        if (led_sysfs_is_disabled(cdev)) {
 256                ret = -EBUSY;
 257                goto unlock;
 258        }
 259        ret = kstrtou32(buf, 0, &color);
 260        if (ret < 0) {
 261                dev_err(dev, "could not parse color value %d\n", ret);
 262                goto unlock;
 263        }
 264
 265        ret = gb_lights_color_set(channel, color);
 266        if (ret < 0)
 267                goto unlock;
 268
 269        channel->color = color;
 270        ret = size;
 271unlock:
 272        led_unlock(cdev);
 273        return ret;
 274}
 275static DEVICE_ATTR_RW(color);
 276
 277static int channel_attr_groups_set(struct gb_channel *channel,
 278                                   struct led_classdev *cdev)
 279{
 280        int attr = 0;
 281        int size = 0;
 282
 283        if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
 284                size++;
 285        if (channel->flags & GB_LIGHT_CHANNEL_FADER)
 286                size += 2;
 287
 288        if (!size)
 289                return 0;
 290
 291        /* Set attributes based in the channel flags */
 292        channel->attrs = kcalloc(size + 1, sizeof(*channel->attrs), GFP_KERNEL);
 293        if (!channel->attrs)
 294                return -ENOMEM;
 295        channel->attr_group = kcalloc(1, sizeof(*channel->attr_group),
 296                                      GFP_KERNEL);
 297        if (!channel->attr_group)
 298                return -ENOMEM;
 299        channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
 300                                       GFP_KERNEL);
 301        if (!channel->attr_groups)
 302                return -ENOMEM;
 303
 304        if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
 305                channel->attrs[attr++] = &dev_attr_color.attr;
 306        if (channel->flags & GB_LIGHT_CHANNEL_FADER) {
 307                channel->attrs[attr++] = &dev_attr_fade_in.attr;
 308                channel->attrs[attr++] = &dev_attr_fade_out.attr;
 309        }
 310
 311        channel->attr_group->attrs = channel->attrs;
 312
 313        channel->attr_groups[0] = channel->attr_group;
 314
 315        cdev->groups = channel->attr_groups;
 316
 317        return 0;
 318}
 319
 320static int gb_lights_fade_set(struct gb_channel *channel)
 321{
 322        struct gb_connection *connection = get_conn_from_channel(channel);
 323        struct gb_bundle *bundle = connection->bundle;
 324        struct gb_lights_set_fade_request req;
 325        int ret;
 326
 327        if (channel->releasing)
 328                return -ESHUTDOWN;
 329
 330        ret = gb_pm_runtime_get_sync(bundle);
 331        if (ret < 0)
 332                return ret;
 333
 334        req.light_id = channel->light->id;
 335        req.channel_id = channel->id;
 336        req.fade_in = channel->fade_in;
 337        req.fade_out = channel->fade_out;
 338        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FADE,
 339                                &req, sizeof(req), NULL, 0);
 340
 341        gb_pm_runtime_put_autosuspend(bundle);
 342
 343        return ret;
 344}
 345
 346static int gb_lights_color_set(struct gb_channel *channel, u32 color)
 347{
 348        struct gb_connection *connection = get_conn_from_channel(channel);
 349        struct gb_bundle *bundle = connection->bundle;
 350        struct gb_lights_set_color_request req;
 351        int ret;
 352
 353        if (channel->releasing)
 354                return -ESHUTDOWN;
 355
 356        ret = gb_pm_runtime_get_sync(bundle);
 357        if (ret < 0)
 358                return ret;
 359
 360        req.light_id = channel->light->id;
 361        req.channel_id = channel->id;
 362        req.color = cpu_to_le32(color);
 363        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_COLOR,
 364                                &req, sizeof(req), NULL, 0);
 365
 366        gb_pm_runtime_put_autosuspend(bundle);
 367
 368        return ret;
 369}
 370
 371static int __gb_lights_led_brightness_set(struct gb_channel *channel)
 372{
 373        struct gb_lights_set_brightness_request req;
 374        struct gb_connection *connection = get_conn_from_channel(channel);
 375        struct gb_bundle *bundle = connection->bundle;
 376        bool old_active;
 377        int ret;
 378
 379        mutex_lock(&channel->lock);
 380        ret = gb_pm_runtime_get_sync(bundle);
 381        if (ret < 0)
 382                goto out_unlock;
 383
 384        old_active = channel->active;
 385
 386        req.light_id = channel->light->id;
 387        req.channel_id = channel->id;
 388        req.brightness = (u8)channel->led->brightness;
 389
 390        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
 391                                &req, sizeof(req), NULL, 0);
 392        if (ret < 0)
 393                goto out_pm_put;
 394
 395        if (channel->led->brightness)
 396                channel->active = true;
 397        else
 398                channel->active = false;
 399
 400        /* we need to keep module alive when turning to active state */
 401        if (!old_active && channel->active)
 402                goto out_unlock;
 403
 404        /*
 405         * on the other hand if going to inactive we still hold a reference and
 406         * need to put it, so we could go to suspend.
 407         */
 408        if (old_active && !channel->active)
 409                gb_pm_runtime_put_autosuspend(bundle);
 410
 411out_pm_put:
 412        gb_pm_runtime_put_autosuspend(bundle);
 413out_unlock:
 414        mutex_unlock(&channel->lock);
 415
 416        return ret;
 417}
 418
 419static int __gb_lights_brightness_set(struct gb_channel *channel)
 420{
 421        int ret;
 422
 423        if (channel->releasing)
 424                return 0;
 425
 426        if (is_channel_flash(channel))
 427                ret = __gb_lights_flash_brightness_set(channel);
 428        else
 429                ret = __gb_lights_led_brightness_set(channel);
 430
 431        return ret;
 432}
 433
 434static int gb_brightness_set(struct led_classdev *cdev,
 435                             enum led_brightness value)
 436{
 437        struct gb_channel *channel = get_channel_from_cdev(cdev);
 438
 439        channel->led->brightness = value;
 440
 441        return __gb_lights_brightness_set(channel);
 442}
 443
 444static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
 445
 446{
 447        struct gb_channel *channel = get_channel_from_cdev(cdev);
 448
 449        return channel->led->brightness;
 450}
 451
 452static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
 453                        unsigned long *delay_off)
 454{
 455        struct gb_channel *channel = get_channel_from_cdev(cdev);
 456        struct gb_connection *connection = get_conn_from_channel(channel);
 457        struct gb_bundle *bundle = connection->bundle;
 458        struct gb_lights_blink_request req;
 459        bool old_active;
 460        int ret;
 461
 462        if (channel->releasing)
 463                return -ESHUTDOWN;
 464
 465        if (!delay_on || !delay_off)
 466                return -EINVAL;
 467
 468        mutex_lock(&channel->lock);
 469        ret = gb_pm_runtime_get_sync(bundle);
 470        if (ret < 0)
 471                goto out_unlock;
 472
 473        old_active = channel->active;
 474
 475        req.light_id = channel->light->id;
 476        req.channel_id = channel->id;
 477        req.time_on_ms = cpu_to_le16(*delay_on);
 478        req.time_off_ms = cpu_to_le16(*delay_off);
 479
 480        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
 481                                sizeof(req), NULL, 0);
 482        if (ret < 0)
 483                goto out_pm_put;
 484
 485        if (*delay_on)
 486                channel->active = true;
 487        else
 488                channel->active = false;
 489
 490        /* we need to keep module alive when turning to active state */
 491        if (!old_active && channel->active)
 492                goto out_unlock;
 493
 494        /*
 495         * on the other hand if going to inactive we still hold a reference and
 496         * need to put it, so we could go to suspend.
 497         */
 498        if (old_active && !channel->active)
 499                gb_pm_runtime_put_autosuspend(bundle);
 500
 501out_pm_put:
 502        gb_pm_runtime_put_autosuspend(bundle);
 503out_unlock:
 504        mutex_unlock(&channel->lock);
 505
 506        return ret;
 507}
 508
 509static void gb_lights_led_operations_set(struct gb_channel *channel,
 510                                         struct led_classdev *cdev)
 511{
 512        cdev->brightness_get = gb_brightness_get;
 513        cdev->brightness_set_blocking = gb_brightness_set;
 514
 515        if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
 516                cdev->blink_set = gb_blink_set;
 517}
 518
 519#if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
 520/* V4L2 specific helpers */
 521static const struct v4l2_flash_ops v4l2_flash_ops;
 522
 523static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
 524                                            struct led_flash_setting *v4l2_s)
 525{
 526        v4l2_s->min = channel_s->min;
 527        v4l2_s->max = channel_s->max;
 528        v4l2_s->step = channel_s->step;
 529        /* For v4l2 val is the default value */
 530        v4l2_s->val = channel_s->max;
 531}
 532
 533static int gb_lights_light_v4l2_register(struct gb_light *light)
 534{
 535        struct gb_connection *connection = get_conn_from_light(light);
 536        struct device *dev = &connection->bundle->dev;
 537        struct v4l2_flash_config sd_cfg = { {0} }, sd_cfg_ind = { {0} };
 538        struct led_classdev_flash *fled;
 539        struct led_classdev *iled = NULL;
 540        struct gb_channel *channel_torch, *channel_ind, *channel_flash;
 541
 542        channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
 543        if (channel_torch)
 544                __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
 545                                                &sd_cfg.intensity);
 546
 547        channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
 548        if (channel_ind) {
 549                __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
 550                                                &sd_cfg_ind.intensity);
 551                iled = &channel_ind->fled.led_cdev;
 552        }
 553
 554        channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
 555        WARN_ON(!channel_flash);
 556
 557        fled = &channel_flash->fled;
 558
 559        snprintf(sd_cfg.dev_name, sizeof(sd_cfg.dev_name), "%s", light->name);
 560        snprintf(sd_cfg_ind.dev_name, sizeof(sd_cfg_ind.dev_name),
 561                 "%s indicator", light->name);
 562
 563        /* Set the possible values to faults, in our case all faults */
 564        sd_cfg.flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
 565                LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
 566                LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
 567                LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
 568                LED_FAULT_LED_OVER_TEMPERATURE;
 569
 570        light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, &v4l2_flash_ops,
 571                                            &sd_cfg);
 572        if (IS_ERR(light->v4l2_flash))
 573                return PTR_ERR(light->v4l2_flash);
 574
 575        if (channel_ind) {
 576                light->v4l2_flash_ind =
 577                        v4l2_flash_indicator_init(dev, NULL, iled, &sd_cfg_ind);
 578                if (IS_ERR(light->v4l2_flash_ind)) {
 579                        v4l2_flash_release(light->v4l2_flash);
 580                        return PTR_ERR(light->v4l2_flash_ind);
 581                }
 582        }
 583
 584        return 0;
 585}
 586
 587static void gb_lights_light_v4l2_unregister(struct gb_light *light)
 588{
 589        v4l2_flash_release(light->v4l2_flash_ind);
 590        v4l2_flash_release(light->v4l2_flash);
 591}
 592#else
 593static int gb_lights_light_v4l2_register(struct gb_light *light)
 594{
 595        struct gb_connection *connection = get_conn_from_light(light);
 596
 597        dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
 598        return 0;
 599}
 600
 601static void gb_lights_light_v4l2_unregister(struct gb_light *light)
 602{
 603}
 604#endif
 605
 606#if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
 607/* Flash specific operations */
 608static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
 609                                         u32 brightness)
 610{
 611        struct gb_channel *channel = container_of(fcdev, struct gb_channel,
 612                                                  fled);
 613        int ret;
 614
 615        ret = __gb_lights_flash_intensity_set(channel, brightness);
 616        if (ret < 0)
 617                return ret;
 618
 619        fcdev->brightness.val = brightness;
 620
 621        return 0;
 622}
 623
 624static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
 625                                         u32 *brightness)
 626{
 627        *brightness = fcdev->brightness.val;
 628
 629        return 0;
 630}
 631
 632static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
 633                                      bool state)
 634{
 635        struct gb_channel *channel = container_of(fcdev, struct gb_channel,
 636                                                  fled);
 637        struct gb_connection *connection = get_conn_from_channel(channel);
 638        struct gb_bundle *bundle = connection->bundle;
 639        struct gb_lights_set_flash_strobe_request req;
 640        int ret;
 641
 642        if (channel->releasing)
 643                return -ESHUTDOWN;
 644
 645        ret = gb_pm_runtime_get_sync(bundle);
 646        if (ret < 0)
 647                return ret;
 648
 649        req.light_id = channel->light->id;
 650        req.channel_id = channel->id;
 651        req.state = state ? 1 : 0;
 652
 653        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
 654                                &req, sizeof(req), NULL, 0);
 655        if (!ret)
 656                channel->strobe_state = state;
 657
 658        gb_pm_runtime_put_autosuspend(bundle);
 659
 660        return ret;
 661}
 662
 663static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
 664                                      bool *state)
 665{
 666        struct gb_channel *channel = container_of(fcdev, struct gb_channel,
 667                                                  fled);
 668
 669        *state = channel->strobe_state;
 670        return 0;
 671}
 672
 673static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
 674                                       u32 timeout)
 675{
 676        struct gb_channel *channel = container_of(fcdev, struct gb_channel,
 677                                                  fled);
 678        struct gb_connection *connection = get_conn_from_channel(channel);
 679        struct gb_bundle *bundle = connection->bundle;
 680        struct gb_lights_set_flash_timeout_request req;
 681        int ret;
 682
 683        if (channel->releasing)
 684                return -ESHUTDOWN;
 685
 686        ret = gb_pm_runtime_get_sync(bundle);
 687        if (ret < 0)
 688                return ret;
 689
 690        req.light_id = channel->light->id;
 691        req.channel_id = channel->id;
 692        req.timeout_us = cpu_to_le32(timeout);
 693
 694        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
 695                                &req, sizeof(req), NULL, 0);
 696        if (!ret)
 697                fcdev->timeout.val = timeout;
 698
 699        gb_pm_runtime_put_autosuspend(bundle);
 700
 701        return ret;
 702}
 703
 704static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
 705                                     u32 *fault)
 706{
 707        struct gb_channel *channel = container_of(fcdev, struct gb_channel,
 708                                                  fled);
 709        struct gb_connection *connection = get_conn_from_channel(channel);
 710        struct gb_bundle *bundle = connection->bundle;
 711        struct gb_lights_get_flash_fault_request req;
 712        struct gb_lights_get_flash_fault_response resp;
 713        int ret;
 714
 715        if (channel->releasing)
 716                return -ESHUTDOWN;
 717
 718        ret = gb_pm_runtime_get_sync(bundle);
 719        if (ret < 0)
 720                return ret;
 721
 722        req.light_id = channel->light->id;
 723        req.channel_id = channel->id;
 724
 725        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
 726                                &req, sizeof(req), &resp, sizeof(resp));
 727        if (!ret)
 728                *fault = le32_to_cpu(resp.fault);
 729
 730        gb_pm_runtime_put_autosuspend(bundle);
 731
 732        return ret;
 733}
 734
 735static const struct led_flash_ops gb_lights_flash_ops = {
 736        .flash_brightness_set   = gb_lights_flash_intensity_set,
 737        .flash_brightness_get   = gb_lights_flash_intensity_get,
 738        .strobe_set             = gb_lights_flash_strobe_set,
 739        .strobe_get             = gb_lights_flash_strobe_get,
 740        .timeout_set            = gb_lights_flash_timeout_set,
 741        .fault_get              = gb_lights_flash_fault_get,
 742};
 743
 744static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
 745                                            struct gb_channel *channel_torch)
 746{
 747        char *name;
 748
 749        /* we can only attach torch to a flash channel */
 750        if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
 751                return 0;
 752
 753        /* Move torch brightness to the destination */
 754        channel->led->max_brightness = channel_torch->led->max_brightness;
 755
 756        /* append mode name to flash name */
 757        name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
 758                         channel_torch->mode_name);
 759        if (!name)
 760                return -ENOMEM;
 761        kfree(channel->led->name);
 762        channel->led->name = name;
 763
 764        channel_torch->led = channel->led;
 765
 766        return 0;
 767}
 768
 769static int __gb_lights_flash_led_register(struct gb_channel *channel)
 770{
 771        struct gb_connection *connection = get_conn_from_channel(channel);
 772        struct led_classdev_flash *fled = &channel->fled;
 773        struct led_flash_setting *fset;
 774        struct gb_channel *channel_torch;
 775        int ret;
 776
 777        fled->ops = &gb_lights_flash_ops;
 778
 779        fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
 780
 781        fset = &fled->brightness;
 782        fset->min = channel->intensity_uA.min;
 783        fset->max = channel->intensity_uA.max;
 784        fset->step = channel->intensity_uA.step;
 785        fset->val = channel->intensity_uA.max;
 786
 787        /* Only the flash mode have the timeout constraints settings */
 788        if (channel->mode & GB_CHANNEL_MODE_FLASH) {
 789                fset = &fled->timeout;
 790                fset->min = channel->timeout_us.min;
 791                fset->max = channel->timeout_us.max;
 792                fset->step = channel->timeout_us.step;
 793                fset->val = channel->timeout_us.max;
 794        }
 795
 796        /*
 797         * If light have torch mode channel, this channel will be the led
 798         * classdev of the registered above flash classdev
 799         */
 800        channel_torch = get_channel_from_mode(channel->light,
 801                                              GB_CHANNEL_MODE_TORCH);
 802        if (channel_torch) {
 803                ret = __gb_lights_channel_torch_attach(channel, channel_torch);
 804                if (ret < 0)
 805                        goto fail;
 806        }
 807
 808        ret = led_classdev_flash_register(&connection->bundle->dev, fled);
 809        if (ret < 0)
 810                goto fail;
 811
 812        channel->is_registered = true;
 813        return 0;
 814fail:
 815        channel->led = NULL;
 816        return ret;
 817}
 818
 819static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
 820{
 821        if (!channel->is_registered)
 822                return;
 823
 824        led_classdev_flash_unregister(&channel->fled);
 825}
 826
 827static int gb_lights_channel_flash_config(struct gb_channel *channel)
 828{
 829        struct gb_connection *connection = get_conn_from_channel(channel);
 830        struct gb_lights_get_channel_flash_config_request req;
 831        struct gb_lights_get_channel_flash_config_response conf;
 832        struct led_flash_setting *fset;
 833        int ret;
 834
 835        req.light_id = channel->light->id;
 836        req.channel_id = channel->id;
 837
 838        ret = gb_operation_sync(connection,
 839                                GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
 840                                &req, sizeof(req), &conf, sizeof(conf));
 841        if (ret < 0)
 842                return ret;
 843
 844        /*
 845         * Intensity constraints for flash related modes: flash, torch,
 846         * indicator.  They will be needed for v4l2 registration.
 847         */
 848        fset = &channel->intensity_uA;
 849        fset->min = le32_to_cpu(conf.intensity_min_uA);
 850        fset->max = le32_to_cpu(conf.intensity_max_uA);
 851        fset->step = le32_to_cpu(conf.intensity_step_uA);
 852
 853        /*
 854         * On flash type, max brightness is set as the number of intensity steps
 855         * available.
 856         */
 857        channel->led->max_brightness = (fset->max - fset->min) / fset->step;
 858
 859        /* Only the flash mode have the timeout constraints settings */
 860        if (channel->mode & GB_CHANNEL_MODE_FLASH) {
 861                fset = &channel->timeout_us;
 862                fset->min = le32_to_cpu(conf.timeout_min_us);
 863                fset->max = le32_to_cpu(conf.timeout_max_us);
 864                fset->step = le32_to_cpu(conf.timeout_step_us);
 865        }
 866
 867        return 0;
 868}
 869#else
 870static int gb_lights_channel_flash_config(struct gb_channel *channel)
 871{
 872        struct gb_connection *connection = get_conn_from_channel(channel);
 873
 874        dev_err(&connection->bundle->dev, "no support for flash devices\n");
 875        return 0;
 876}
 877
 878static int __gb_lights_flash_led_register(struct gb_channel *channel)
 879{
 880        return 0;
 881}
 882
 883static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
 884{
 885}
 886
 887#endif
 888
 889static int __gb_lights_led_register(struct gb_channel *channel)
 890{
 891        struct gb_connection *connection = get_conn_from_channel(channel);
 892        struct led_classdev *cdev = get_channel_cdev(channel);
 893        int ret;
 894
 895        ret = led_classdev_register(&connection->bundle->dev, cdev);
 896        if (ret < 0)
 897                channel->led = NULL;
 898        else
 899                channel->is_registered = true;
 900        return ret;
 901}
 902
 903static int gb_lights_channel_register(struct gb_channel *channel)
 904{
 905        /* Normal LED channel, just register in led classdev and we are done */
 906        if (!is_channel_flash(channel))
 907                return __gb_lights_led_register(channel);
 908
 909        /*
 910         * Flash Type need more work, register flash classdev, indicator as
 911         * flash classdev, torch will be led classdev of the flash classdev.
 912         */
 913        if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
 914                return __gb_lights_flash_led_register(channel);
 915
 916        return 0;
 917}
 918
 919static void __gb_lights_led_unregister(struct gb_channel *channel)
 920{
 921        struct led_classdev *cdev = get_channel_cdev(channel);
 922
 923        if (!channel->is_registered)
 924                return;
 925
 926        led_classdev_unregister(cdev);
 927        kfree(cdev->name);
 928        cdev->name = NULL;
 929        channel->led = NULL;
 930}
 931
 932static void gb_lights_channel_unregister(struct gb_channel *channel)
 933{
 934        /* The same as register, handle channels differently */
 935        if (!is_channel_flash(channel)) {
 936                __gb_lights_led_unregister(channel);
 937                return;
 938        }
 939
 940        if (channel->mode & GB_CHANNEL_MODE_TORCH)
 941                __gb_lights_led_unregister(channel);
 942        else
 943                __gb_lights_flash_led_unregister(channel);
 944}
 945
 946static int gb_lights_channel_config(struct gb_light *light,
 947                                    struct gb_channel *channel)
 948{
 949        struct gb_lights_get_channel_config_response conf;
 950        struct gb_lights_get_channel_config_request req;
 951        struct gb_connection *connection = get_conn_from_light(light);
 952        struct led_classdev *cdev = get_channel_cdev(channel);
 953        char *name;
 954        int ret;
 955
 956        req.light_id = light->id;
 957        req.channel_id = channel->id;
 958
 959        ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
 960                                &req, sizeof(req), &conf, sizeof(conf));
 961        if (ret < 0)
 962                return ret;
 963
 964        channel->light = light;
 965        channel->mode = le32_to_cpu(conf.mode);
 966        channel->flags = le32_to_cpu(conf.flags);
 967        channel->color = le32_to_cpu(conf.color);
 968        channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
 969        if (!channel->color_name)
 970                return -ENOMEM;
 971        channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
 972        if (!channel->mode_name)
 973                return -ENOMEM;
 974
 975        channel->led = cdev;
 976
 977        name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
 978                         channel->color_name, channel->mode_name);
 979        if (!name)
 980                return -ENOMEM;
 981
 982        cdev->name = name;
 983
 984        cdev->max_brightness = conf.max_brightness;
 985
 986        ret = channel_attr_groups_set(channel, cdev);
 987        if (ret < 0)
 988                return ret;
 989
 990        gb_lights_led_operations_set(channel, cdev);
 991
 992        /*
 993         * If it is not a flash related channel (flash, torch or indicator) we
 994         * are done here. If not, continue and fetch flash related
 995         * configurations.
 996         */
 997        if (!is_channel_flash(channel))
 998                return ret;
 999
1000        light->has_flash = true;
1001
1002        return gb_lights_channel_flash_config(channel);
1003}
1004
1005static int gb_lights_light_config(struct gb_lights *glights, u8 id)
1006{
1007        struct gb_light *light = &glights->lights[id];
1008        struct gb_lights_get_light_config_request req;
1009        struct gb_lights_get_light_config_response conf;
1010        int ret;
1011        int i;
1012
1013        light->glights = glights;
1014        light->id = id;
1015
1016        req.id = id;
1017
1018        ret = gb_operation_sync(glights->connection,
1019                                GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
1020                                &req, sizeof(req), &conf, sizeof(conf));
1021        if (ret < 0)
1022                return ret;
1023
1024        if (!conf.channel_count)
1025                return -EINVAL;
1026        if (!strlen(conf.name))
1027                return -EINVAL;
1028
1029        light->channels_count = conf.channel_count;
1030        light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1031
1032        light->channels = kcalloc(light->channels_count,
1033                                  sizeof(struct gb_channel), GFP_KERNEL);
1034        if (!light->channels)
1035                return -ENOMEM;
1036
1037        /* First we collect all the configurations for all channels */
1038        for (i = 0; i < light->channels_count; i++) {
1039                light->channels[i].id = i;
1040                ret = gb_lights_channel_config(light, &light->channels[i]);
1041                if (ret < 0)
1042                        return ret;
1043        }
1044
1045        return 0;
1046}
1047
1048static int gb_lights_light_register(struct gb_light *light)
1049{
1050        int ret;
1051        int i;
1052
1053        /*
1054         * Then, if everything went ok in getting configurations, we register
1055         * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1056         * found.
1057         */
1058        for (i = 0; i < light->channels_count; i++) {
1059                ret = gb_lights_channel_register(&light->channels[i]);
1060                if (ret < 0)
1061                        return ret;
1062
1063                mutex_init(&light->channels[i].lock);
1064        }
1065
1066        light->ready = true;
1067
1068        if (light->has_flash) {
1069                ret = gb_lights_light_v4l2_register(light);
1070                if (ret < 0) {
1071                        light->has_flash = false;
1072                        return ret;
1073                }
1074        }
1075
1076        return 0;
1077}
1078
1079static void gb_lights_channel_free(struct gb_channel *channel)
1080{
1081        kfree(channel->attrs);
1082        kfree(channel->attr_group);
1083        kfree(channel->attr_groups);
1084        kfree(channel->color_name);
1085        kfree(channel->mode_name);
1086        mutex_destroy(&channel->lock);
1087}
1088
1089static void gb_lights_channel_release(struct gb_channel *channel)
1090{
1091        channel->releasing = true;
1092
1093        gb_lights_channel_unregister(channel);
1094
1095        gb_lights_channel_free(channel);
1096}
1097
1098static void gb_lights_light_release(struct gb_light *light)
1099{
1100        int i;
1101        int count;
1102
1103        light->ready = false;
1104
1105        count = light->channels_count;
1106
1107        if (light->has_flash)
1108                gb_lights_light_v4l2_unregister(light);
1109
1110        for (i = 0; i < count; i++) {
1111                gb_lights_channel_release(&light->channels[i]);
1112                light->channels_count--;
1113        }
1114        kfree(light->channels);
1115        kfree(light->name);
1116}
1117
1118static void gb_lights_release(struct gb_lights *glights)
1119{
1120        int i;
1121
1122        if (!glights)
1123                return;
1124
1125        mutex_lock(&glights->lights_lock);
1126        if (!glights->lights)
1127                goto free_glights;
1128
1129        for (i = 0; i < glights->lights_count; i++)
1130                gb_lights_light_release(&glights->lights[i]);
1131
1132        kfree(glights->lights);
1133
1134free_glights:
1135        mutex_unlock(&glights->lights_lock);
1136        mutex_destroy(&glights->lights_lock);
1137        kfree(glights);
1138}
1139
1140static int gb_lights_get_count(struct gb_lights *glights)
1141{
1142        struct gb_lights_get_lights_response resp;
1143        int ret;
1144
1145        ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1146                                NULL, 0, &resp, sizeof(resp));
1147        if (ret < 0)
1148                return ret;
1149
1150        if (!resp.lights_count)
1151                return -EINVAL;
1152
1153        glights->lights_count = resp.lights_count;
1154
1155        return 0;
1156}
1157
1158static int gb_lights_create_all(struct gb_lights *glights)
1159{
1160        struct gb_connection *connection = glights->connection;
1161        int ret;
1162        int i;
1163
1164        mutex_lock(&glights->lights_lock);
1165        ret = gb_lights_get_count(glights);
1166        if (ret < 0)
1167                goto out;
1168
1169        glights->lights = kcalloc(glights->lights_count,
1170                                  sizeof(struct gb_light), GFP_KERNEL);
1171        if (!glights->lights) {
1172                ret = -ENOMEM;
1173                goto out;
1174        }
1175
1176        for (i = 0; i < glights->lights_count; i++) {
1177                ret = gb_lights_light_config(glights, i);
1178                if (ret < 0) {
1179                        dev_err(&connection->bundle->dev,
1180                                "Fail to configure lights device\n");
1181                        goto out;
1182                }
1183        }
1184
1185out:
1186        mutex_unlock(&glights->lights_lock);
1187        return ret;
1188}
1189
1190static int gb_lights_register_all(struct gb_lights *glights)
1191{
1192        struct gb_connection *connection = glights->connection;
1193        int ret = 0;
1194        int i;
1195
1196        mutex_lock(&glights->lights_lock);
1197        for (i = 0; i < glights->lights_count; i++) {
1198                ret = gb_lights_light_register(&glights->lights[i]);
1199                if (ret < 0) {
1200                        dev_err(&connection->bundle->dev,
1201                                "Fail to enable lights device\n");
1202                        break;
1203                }
1204        }
1205
1206        mutex_unlock(&glights->lights_lock);
1207        return ret;
1208}
1209
1210static int gb_lights_request_handler(struct gb_operation *op)
1211{
1212        struct gb_connection *connection = op->connection;
1213        struct device *dev = &connection->bundle->dev;
1214        struct gb_lights *glights = gb_connection_get_data(connection);
1215        struct gb_light *light;
1216        struct gb_message *request;
1217        struct gb_lights_event_request *payload;
1218        int ret =  0;
1219        u8 light_id;
1220        u8 event;
1221
1222        if (op->type != GB_LIGHTS_TYPE_EVENT) {
1223                dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1224                return -EINVAL;
1225        }
1226
1227        request = op->request;
1228
1229        if (request->payload_size < sizeof(*payload)) {
1230                dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1231                        request->payload_size, sizeof(*payload));
1232                return -EINVAL;
1233        }
1234
1235        payload = request->payload;
1236        light_id = payload->light_id;
1237
1238        if (light_id >= glights->lights_count ||
1239            !glights->lights[light_id].ready) {
1240                dev_err(dev, "Event received for unconfigured light id: %d\n",
1241                        light_id);
1242                return -EINVAL;
1243        }
1244
1245        event = payload->event;
1246
1247        if (event & GB_LIGHTS_LIGHT_CONFIG) {
1248                light = &glights->lights[light_id];
1249
1250                mutex_lock(&glights->lights_lock);
1251                gb_lights_light_release(light);
1252                ret = gb_lights_light_config(glights, light_id);
1253                if (!ret)
1254                        ret = gb_lights_light_register(light);
1255                if (ret < 0)
1256                        gb_lights_light_release(light);
1257                mutex_unlock(&glights->lights_lock);
1258        }
1259
1260        return ret;
1261}
1262
1263static int gb_lights_probe(struct gb_bundle *bundle,
1264                           const struct greybus_bundle_id *id)
1265{
1266        struct greybus_descriptor_cport *cport_desc;
1267        struct gb_connection *connection;
1268        struct gb_lights *glights;
1269        int ret;
1270
1271        if (bundle->num_cports != 1)
1272                return -ENODEV;
1273
1274        cport_desc = &bundle->cport_desc[0];
1275        if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1276                return -ENODEV;
1277
1278        glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1279        if (!glights)
1280                return -ENOMEM;
1281
1282        mutex_init(&glights->lights_lock);
1283
1284        connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1285                                          gb_lights_request_handler);
1286        if (IS_ERR(connection)) {
1287                ret = PTR_ERR(connection);
1288                goto out;
1289        }
1290
1291        glights->connection = connection;
1292        gb_connection_set_data(connection, glights);
1293
1294        greybus_set_drvdata(bundle, glights);
1295
1296        /* We aren't ready to receive an incoming request yet */
1297        ret = gb_connection_enable_tx(connection);
1298        if (ret)
1299                goto error_connection_destroy;
1300
1301        /*
1302         * Setup all the lights devices over this connection, if anything goes
1303         * wrong tear down all lights
1304         */
1305        ret = gb_lights_create_all(glights);
1306        if (ret < 0)
1307                goto error_connection_disable;
1308
1309        /* We are ready to receive an incoming request now, enable RX as well */
1310        ret = gb_connection_enable(connection);
1311        if (ret)
1312                goto error_connection_disable;
1313
1314        /* Enable & register lights */
1315        ret = gb_lights_register_all(glights);
1316        if (ret < 0)
1317                goto error_connection_disable;
1318
1319        gb_pm_runtime_put_autosuspend(bundle);
1320
1321        return 0;
1322
1323error_connection_disable:
1324        gb_connection_disable(connection);
1325error_connection_destroy:
1326        gb_connection_destroy(connection);
1327out:
1328        gb_lights_release(glights);
1329        return ret;
1330}
1331
1332static void gb_lights_disconnect(struct gb_bundle *bundle)
1333{
1334        struct gb_lights *glights = greybus_get_drvdata(bundle);
1335
1336        if (gb_pm_runtime_get_sync(bundle))
1337                gb_pm_runtime_get_noresume(bundle);
1338
1339        gb_connection_disable(glights->connection);
1340        gb_connection_destroy(glights->connection);
1341
1342        gb_lights_release(glights);
1343}
1344
1345static const struct greybus_bundle_id gb_lights_id_table[] = {
1346        { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1347        { }
1348};
1349MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1350
1351static struct greybus_driver gb_lights_driver = {
1352        .name           = "lights",
1353        .probe          = gb_lights_probe,
1354        .disconnect     = gb_lights_disconnect,
1355        .id_table       = gb_lights_id_table,
1356};
1357module_greybus_driver(gb_lights_driver);
1358
1359MODULE_LICENSE("GPL v2");
1360