Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Greybus Lights protocol driver.
0004  *
0005  * Copyright 2015 Google Inc.
0006  * Copyright 2015 Linaro Ltd.
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/leds.h>
0011 #include <linux/led-class-flash.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/greybus.h>
0015 #include <media/v4l2-flash-led-class.h>
0016 
0017 #define NAMES_MAX   32
0018 
0019 struct gb_channel {
0020     u8              id;
0021     u32             flags;
0022     u32             color;
0023     char                *color_name;
0024     u8              fade_in;
0025     u8              fade_out;
0026     u32             mode;
0027     char                *mode_name;
0028     struct attribute        **attrs;
0029     struct attribute_group      *attr_group;
0030     const struct attribute_group    **attr_groups;
0031     struct led_classdev     *led;
0032 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
0033     struct led_classdev_flash   fled;
0034     struct led_flash_setting    intensity_uA;
0035     struct led_flash_setting    timeout_us;
0036 #else
0037     struct led_classdev     cled;
0038 #endif
0039     struct gb_light         *light;
0040     bool                is_registered;
0041     bool                releasing;
0042     bool                strobe_state;
0043     bool                active;
0044     struct mutex            lock;
0045 };
0046 
0047 struct gb_light {
0048     u8          id;
0049     char            *name;
0050     struct gb_lights    *glights;
0051     u32         flags;
0052     u8          channels_count;
0053     struct gb_channel   *channels;
0054     bool            has_flash;
0055     bool            ready;
0056 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
0057     struct v4l2_flash   *v4l2_flash;
0058     struct v4l2_flash   *v4l2_flash_ind;
0059 #endif
0060 };
0061 
0062 struct gb_lights {
0063     struct gb_connection    *connection;
0064     u8          lights_count;
0065     struct gb_light     *lights;
0066     struct mutex        lights_lock;
0067 };
0068 
0069 static void gb_lights_channel_free(struct gb_channel *channel);
0070 
0071 static struct gb_connection *get_conn_from_channel(struct gb_channel *channel)
0072 {
0073     return channel->light->glights->connection;
0074 }
0075 
0076 static struct gb_connection *get_conn_from_light(struct gb_light *light)
0077 {
0078     return light->glights->connection;
0079 }
0080 
0081 static bool is_channel_flash(struct gb_channel *channel)
0082 {
0083     return !!(channel->mode & (GB_CHANNEL_MODE_FLASH | GB_CHANNEL_MODE_TORCH
0084                    | GB_CHANNEL_MODE_INDICATOR));
0085 }
0086 
0087 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
0088 static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
0089 {
0090     struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
0091 
0092     return container_of(fled_cdev, struct gb_channel, fled);
0093 }
0094 
0095 static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
0096 {
0097     return &channel->fled.led_cdev;
0098 }
0099 
0100 static struct gb_channel *get_channel_from_mode(struct gb_light *light,
0101                         u32 mode)
0102 {
0103     struct gb_channel *channel = NULL;
0104     int i;
0105 
0106     for (i = 0; i < light->channels_count; i++) {
0107         channel = &light->channels[i];
0108         if (channel && channel->mode == mode)
0109             break;
0110     }
0111     return channel;
0112 }
0113 
0114 static int __gb_lights_flash_intensity_set(struct gb_channel *channel,
0115                        u32 intensity)
0116 {
0117     struct gb_connection *connection = get_conn_from_channel(channel);
0118     struct gb_bundle *bundle = connection->bundle;
0119     struct gb_lights_set_flash_intensity_request req;
0120     int ret;
0121 
0122     if (channel->releasing)
0123         return -ESHUTDOWN;
0124 
0125     ret = gb_pm_runtime_get_sync(bundle);
0126     if (ret < 0)
0127         return ret;
0128 
0129     req.light_id = channel->light->id;
0130     req.channel_id = channel->id;
0131     req.intensity_uA = cpu_to_le32(intensity);
0132 
0133     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_INTENSITY,
0134                 &req, sizeof(req), NULL, 0);
0135 
0136     gb_pm_runtime_put_autosuspend(bundle);
0137 
0138     return ret;
0139 }
0140 
0141 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
0142 {
0143     u32 intensity;
0144 
0145     /* If the channel is flash we need to get the attached torch channel */
0146     if (channel->mode & GB_CHANNEL_MODE_FLASH)
0147         channel = get_channel_from_mode(channel->light,
0148                         GB_CHANNEL_MODE_TORCH);
0149 
0150     /* For not flash we need to convert brightness to intensity */
0151     intensity = channel->intensity_uA.min +
0152             (channel->intensity_uA.step * channel->led->brightness);
0153 
0154     return __gb_lights_flash_intensity_set(channel, intensity);
0155 }
0156 #else
0157 static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
0158 {
0159     return container_of(cdev, struct gb_channel, cled);
0160 }
0161 
0162 static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
0163 {
0164     return &channel->cled;
0165 }
0166 
0167 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
0168 {
0169     return 0;
0170 }
0171 #endif
0172 
0173 static int gb_lights_color_set(struct gb_channel *channel, u32 color);
0174 static int gb_lights_fade_set(struct gb_channel *channel);
0175 
0176 static void led_lock(struct led_classdev *cdev)
0177 {
0178     mutex_lock(&cdev->led_access);
0179 }
0180 
0181 static void led_unlock(struct led_classdev *cdev)
0182 {
0183     mutex_unlock(&cdev->led_access);
0184 }
0185 
0186 #define gb_lights_fade_attr(__dir)                  \
0187 static ssize_t fade_##__dir##_show(struct device *dev,          \
0188                    struct device_attribute *attr,   \
0189                    char *buf)               \
0190 {                                   \
0191     struct led_classdev *cdev = dev_get_drvdata(dev);       \
0192     struct gb_channel *channel = get_channel_from_cdev(cdev);   \
0193                                     \
0194     return sprintf(buf, "%u\n", channel->fade_##__dir);     \
0195 }                                   \
0196                                     \
0197 static ssize_t fade_##__dir##_store(struct device *dev,         \
0198                     struct device_attribute *attr,  \
0199                     const char *buf, size_t size)   \
0200 {                                   \
0201     struct led_classdev *cdev = dev_get_drvdata(dev);       \
0202     struct gb_channel *channel = get_channel_from_cdev(cdev);   \
0203     u8 fade;                            \
0204     int ret;                            \
0205                                     \
0206     led_lock(cdev);                         \
0207     if (led_sysfs_is_disabled(cdev)) {              \
0208         ret = -EBUSY;                       \
0209         goto unlock;                        \
0210     }                               \
0211                                     \
0212     ret = kstrtou8(buf, 0, &fade);                  \
0213     if (ret < 0) {                          \
0214         dev_err(dev, "could not parse fade value %d\n", ret);   \
0215         goto unlock;                        \
0216     }                               \
0217     if (channel->fade_##__dir == fade)              \
0218         goto unlock;                        \
0219     channel->fade_##__dir = fade;                   \
0220                                     \
0221     ret = gb_lights_fade_set(channel);              \
0222     if (ret < 0)                            \
0223         goto unlock;                        \
0224                                     \
0225     ret = size;                         \
0226 unlock:                                 \
0227     led_unlock(cdev);                       \
0228     return ret;                         \
0229 }                                   \
0230 static DEVICE_ATTR_RW(fade_##__dir)
0231 
0232 gb_lights_fade_attr(in);
0233 gb_lights_fade_attr(out);
0234 
0235 static ssize_t color_show(struct device *dev, struct device_attribute *attr,
0236               char *buf)
0237 {
0238     struct led_classdev *cdev = dev_get_drvdata(dev);
0239     struct gb_channel *channel = get_channel_from_cdev(cdev);
0240 
0241     return sprintf(buf, "0x%08x\n", channel->color);
0242 }
0243 
0244 static ssize_t color_store(struct device *dev, struct device_attribute *attr,
0245                const char *buf, size_t size)
0246 {
0247     struct led_classdev *cdev = dev_get_drvdata(dev);
0248     struct gb_channel *channel = get_channel_from_cdev(cdev);
0249     u32 color;
0250     int ret;
0251 
0252     led_lock(cdev);
0253     if (led_sysfs_is_disabled(cdev)) {
0254         ret = -EBUSY;
0255         goto unlock;
0256     }
0257     ret = kstrtou32(buf, 0, &color);
0258     if (ret < 0) {
0259         dev_err(dev, "could not parse color value %d\n", ret);
0260         goto unlock;
0261     }
0262 
0263     ret = gb_lights_color_set(channel, color);
0264     if (ret < 0)
0265         goto unlock;
0266 
0267     channel->color = color;
0268     ret = size;
0269 unlock:
0270     led_unlock(cdev);
0271     return ret;
0272 }
0273 static DEVICE_ATTR_RW(color);
0274 
0275 static int channel_attr_groups_set(struct gb_channel *channel,
0276                    struct led_classdev *cdev)
0277 {
0278     int attr = 0;
0279     int size = 0;
0280 
0281     if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
0282         size++;
0283     if (channel->flags & GB_LIGHT_CHANNEL_FADER)
0284         size += 2;
0285 
0286     if (!size)
0287         return 0;
0288 
0289     /* Set attributes based in the channel flags */
0290     channel->attrs = kcalloc(size + 1, sizeof(*channel->attrs), GFP_KERNEL);
0291     if (!channel->attrs)
0292         return -ENOMEM;
0293     channel->attr_group = kzalloc(sizeof(*channel->attr_group), GFP_KERNEL);
0294     if (!channel->attr_group)
0295         return -ENOMEM;
0296     channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
0297                        GFP_KERNEL);
0298     if (!channel->attr_groups)
0299         return -ENOMEM;
0300 
0301     if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
0302         channel->attrs[attr++] = &dev_attr_color.attr;
0303     if (channel->flags & GB_LIGHT_CHANNEL_FADER) {
0304         channel->attrs[attr++] = &dev_attr_fade_in.attr;
0305         channel->attrs[attr++] = &dev_attr_fade_out.attr;
0306     }
0307 
0308     channel->attr_group->attrs = channel->attrs;
0309 
0310     channel->attr_groups[0] = channel->attr_group;
0311 
0312     cdev->groups = channel->attr_groups;
0313 
0314     return 0;
0315 }
0316 
0317 static int gb_lights_fade_set(struct gb_channel *channel)
0318 {
0319     struct gb_connection *connection = get_conn_from_channel(channel);
0320     struct gb_bundle *bundle = connection->bundle;
0321     struct gb_lights_set_fade_request req;
0322     int ret;
0323 
0324     if (channel->releasing)
0325         return -ESHUTDOWN;
0326 
0327     ret = gb_pm_runtime_get_sync(bundle);
0328     if (ret < 0)
0329         return ret;
0330 
0331     req.light_id = channel->light->id;
0332     req.channel_id = channel->id;
0333     req.fade_in = channel->fade_in;
0334     req.fade_out = channel->fade_out;
0335     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FADE,
0336                 &req, sizeof(req), NULL, 0);
0337 
0338     gb_pm_runtime_put_autosuspend(bundle);
0339 
0340     return ret;
0341 }
0342 
0343 static int gb_lights_color_set(struct gb_channel *channel, u32 color)
0344 {
0345     struct gb_connection *connection = get_conn_from_channel(channel);
0346     struct gb_bundle *bundle = connection->bundle;
0347     struct gb_lights_set_color_request req;
0348     int ret;
0349 
0350     if (channel->releasing)
0351         return -ESHUTDOWN;
0352 
0353     ret = gb_pm_runtime_get_sync(bundle);
0354     if (ret < 0)
0355         return ret;
0356 
0357     req.light_id = channel->light->id;
0358     req.channel_id = channel->id;
0359     req.color = cpu_to_le32(color);
0360     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_COLOR,
0361                 &req, sizeof(req), NULL, 0);
0362 
0363     gb_pm_runtime_put_autosuspend(bundle);
0364 
0365     return ret;
0366 }
0367 
0368 static int __gb_lights_led_brightness_set(struct gb_channel *channel)
0369 {
0370     struct gb_lights_set_brightness_request req;
0371     struct gb_connection *connection = get_conn_from_channel(channel);
0372     struct gb_bundle *bundle = connection->bundle;
0373     bool old_active;
0374     int ret;
0375 
0376     mutex_lock(&channel->lock);
0377     ret = gb_pm_runtime_get_sync(bundle);
0378     if (ret < 0)
0379         goto out_unlock;
0380 
0381     old_active = channel->active;
0382 
0383     req.light_id = channel->light->id;
0384     req.channel_id = channel->id;
0385     req.brightness = (u8)channel->led->brightness;
0386 
0387     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
0388                 &req, sizeof(req), NULL, 0);
0389     if (ret < 0)
0390         goto out_pm_put;
0391 
0392     if (channel->led->brightness)
0393         channel->active = true;
0394     else
0395         channel->active = false;
0396 
0397     /* we need to keep module alive when turning to active state */
0398     if (!old_active && channel->active)
0399         goto out_unlock;
0400 
0401     /*
0402      * on the other hand if going to inactive we still hold a reference and
0403      * need to put it, so we could go to suspend.
0404      */
0405     if (old_active && !channel->active)
0406         gb_pm_runtime_put_autosuspend(bundle);
0407 
0408 out_pm_put:
0409     gb_pm_runtime_put_autosuspend(bundle);
0410 out_unlock:
0411     mutex_unlock(&channel->lock);
0412 
0413     return ret;
0414 }
0415 
0416 static int __gb_lights_brightness_set(struct gb_channel *channel)
0417 {
0418     int ret;
0419 
0420     if (channel->releasing)
0421         return 0;
0422 
0423     if (is_channel_flash(channel))
0424         ret = __gb_lights_flash_brightness_set(channel);
0425     else
0426         ret = __gb_lights_led_brightness_set(channel);
0427 
0428     return ret;
0429 }
0430 
0431 static int gb_brightness_set(struct led_classdev *cdev,
0432                  enum led_brightness value)
0433 {
0434     struct gb_channel *channel = get_channel_from_cdev(cdev);
0435 
0436     channel->led->brightness = value;
0437 
0438     return __gb_lights_brightness_set(channel);
0439 }
0440 
0441 static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
0442 
0443 {
0444     struct gb_channel *channel = get_channel_from_cdev(cdev);
0445 
0446     return channel->led->brightness;
0447 }
0448 
0449 static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
0450             unsigned long *delay_off)
0451 {
0452     struct gb_channel *channel = get_channel_from_cdev(cdev);
0453     struct gb_connection *connection = get_conn_from_channel(channel);
0454     struct gb_bundle *bundle = connection->bundle;
0455     struct gb_lights_blink_request req;
0456     bool old_active;
0457     int ret;
0458 
0459     if (channel->releasing)
0460         return -ESHUTDOWN;
0461 
0462     if (!delay_on || !delay_off)
0463         return -EINVAL;
0464 
0465     mutex_lock(&channel->lock);
0466     ret = gb_pm_runtime_get_sync(bundle);
0467     if (ret < 0)
0468         goto out_unlock;
0469 
0470     old_active = channel->active;
0471 
0472     req.light_id = channel->light->id;
0473     req.channel_id = channel->id;
0474     req.time_on_ms = cpu_to_le16(*delay_on);
0475     req.time_off_ms = cpu_to_le16(*delay_off);
0476 
0477     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
0478                 sizeof(req), NULL, 0);
0479     if (ret < 0)
0480         goto out_pm_put;
0481 
0482     if (*delay_on)
0483         channel->active = true;
0484     else
0485         channel->active = false;
0486 
0487     /* we need to keep module alive when turning to active state */
0488     if (!old_active && channel->active)
0489         goto out_unlock;
0490 
0491     /*
0492      * on the other hand if going to inactive we still hold a reference and
0493      * need to put it, so we could go to suspend.
0494      */
0495     if (old_active && !channel->active)
0496         gb_pm_runtime_put_autosuspend(bundle);
0497 
0498 out_pm_put:
0499     gb_pm_runtime_put_autosuspend(bundle);
0500 out_unlock:
0501     mutex_unlock(&channel->lock);
0502 
0503     return ret;
0504 }
0505 
0506 static void gb_lights_led_operations_set(struct gb_channel *channel,
0507                      struct led_classdev *cdev)
0508 {
0509     cdev->brightness_get = gb_brightness_get;
0510     cdev->brightness_set_blocking = gb_brightness_set;
0511 
0512     if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
0513         cdev->blink_set = gb_blink_set;
0514 }
0515 
0516 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
0517 /* V4L2 specific helpers */
0518 static const struct v4l2_flash_ops v4l2_flash_ops;
0519 
0520 static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
0521                         struct led_flash_setting *v4l2_s)
0522 {
0523     v4l2_s->min = channel_s->min;
0524     v4l2_s->max = channel_s->max;
0525     v4l2_s->step = channel_s->step;
0526     /* For v4l2 val is the default value */
0527     v4l2_s->val = channel_s->max;
0528 }
0529 
0530 static int gb_lights_light_v4l2_register(struct gb_light *light)
0531 {
0532     struct gb_connection *connection = get_conn_from_light(light);
0533     struct device *dev = &connection->bundle->dev;
0534     struct v4l2_flash_config sd_cfg = { {0} }, sd_cfg_ind = { {0} };
0535     struct led_classdev_flash *fled;
0536     struct led_classdev *iled = NULL;
0537     struct gb_channel *channel_torch, *channel_ind, *channel_flash;
0538 
0539     channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
0540     if (channel_torch)
0541         __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
0542                         &sd_cfg.intensity);
0543 
0544     channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
0545     if (channel_ind) {
0546         __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
0547                         &sd_cfg_ind.intensity);
0548         iled = &channel_ind->fled.led_cdev;
0549     }
0550 
0551     channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
0552     WARN_ON(!channel_flash);
0553 
0554     fled = &channel_flash->fled;
0555 
0556     snprintf(sd_cfg.dev_name, sizeof(sd_cfg.dev_name), "%s", light->name);
0557     snprintf(sd_cfg_ind.dev_name, sizeof(sd_cfg_ind.dev_name),
0558          "%s indicator", light->name);
0559 
0560     /* Set the possible values to faults, in our case all faults */
0561     sd_cfg.flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
0562         LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
0563         LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
0564         LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
0565         LED_FAULT_LED_OVER_TEMPERATURE;
0566 
0567     light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, &v4l2_flash_ops,
0568                         &sd_cfg);
0569     if (IS_ERR(light->v4l2_flash))
0570         return PTR_ERR(light->v4l2_flash);
0571 
0572     if (channel_ind) {
0573         light->v4l2_flash_ind =
0574             v4l2_flash_indicator_init(dev, NULL, iled, &sd_cfg_ind);
0575         if (IS_ERR(light->v4l2_flash_ind)) {
0576             v4l2_flash_release(light->v4l2_flash);
0577             return PTR_ERR(light->v4l2_flash_ind);
0578         }
0579     }
0580 
0581     return 0;
0582 }
0583 
0584 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
0585 {
0586     v4l2_flash_release(light->v4l2_flash_ind);
0587     v4l2_flash_release(light->v4l2_flash);
0588 }
0589 #else
0590 static int gb_lights_light_v4l2_register(struct gb_light *light)
0591 {
0592     struct gb_connection *connection = get_conn_from_light(light);
0593 
0594     dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
0595     return 0;
0596 }
0597 
0598 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
0599 {
0600 }
0601 #endif
0602 
0603 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
0604 /* Flash specific operations */
0605 static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
0606                      u32 brightness)
0607 {
0608     struct gb_channel *channel = container_of(fcdev, struct gb_channel,
0609                           fled);
0610     int ret;
0611 
0612     ret = __gb_lights_flash_intensity_set(channel, brightness);
0613     if (ret < 0)
0614         return ret;
0615 
0616     fcdev->brightness.val = brightness;
0617 
0618     return 0;
0619 }
0620 
0621 static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
0622                      u32 *brightness)
0623 {
0624     *brightness = fcdev->brightness.val;
0625 
0626     return 0;
0627 }
0628 
0629 static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
0630                       bool state)
0631 {
0632     struct gb_channel *channel = container_of(fcdev, struct gb_channel,
0633                           fled);
0634     struct gb_connection *connection = get_conn_from_channel(channel);
0635     struct gb_bundle *bundle = connection->bundle;
0636     struct gb_lights_set_flash_strobe_request req;
0637     int ret;
0638 
0639     if (channel->releasing)
0640         return -ESHUTDOWN;
0641 
0642     ret = gb_pm_runtime_get_sync(bundle);
0643     if (ret < 0)
0644         return ret;
0645 
0646     req.light_id = channel->light->id;
0647     req.channel_id = channel->id;
0648     req.state = state ? 1 : 0;
0649 
0650     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
0651                 &req, sizeof(req), NULL, 0);
0652     if (!ret)
0653         channel->strobe_state = state;
0654 
0655     gb_pm_runtime_put_autosuspend(bundle);
0656 
0657     return ret;
0658 }
0659 
0660 static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
0661                       bool *state)
0662 {
0663     struct gb_channel *channel = container_of(fcdev, struct gb_channel,
0664                           fled);
0665 
0666     *state = channel->strobe_state;
0667     return 0;
0668 }
0669 
0670 static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
0671                        u32 timeout)
0672 {
0673     struct gb_channel *channel = container_of(fcdev, struct gb_channel,
0674                           fled);
0675     struct gb_connection *connection = get_conn_from_channel(channel);
0676     struct gb_bundle *bundle = connection->bundle;
0677     struct gb_lights_set_flash_timeout_request req;
0678     int ret;
0679 
0680     if (channel->releasing)
0681         return -ESHUTDOWN;
0682 
0683     ret = gb_pm_runtime_get_sync(bundle);
0684     if (ret < 0)
0685         return ret;
0686 
0687     req.light_id = channel->light->id;
0688     req.channel_id = channel->id;
0689     req.timeout_us = cpu_to_le32(timeout);
0690 
0691     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
0692                 &req, sizeof(req), NULL, 0);
0693     if (!ret)
0694         fcdev->timeout.val = timeout;
0695 
0696     gb_pm_runtime_put_autosuspend(bundle);
0697 
0698     return ret;
0699 }
0700 
0701 static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
0702                      u32 *fault)
0703 {
0704     struct gb_channel *channel = container_of(fcdev, struct gb_channel,
0705                           fled);
0706     struct gb_connection *connection = get_conn_from_channel(channel);
0707     struct gb_bundle *bundle = connection->bundle;
0708     struct gb_lights_get_flash_fault_request req;
0709     struct gb_lights_get_flash_fault_response resp;
0710     int ret;
0711 
0712     if (channel->releasing)
0713         return -ESHUTDOWN;
0714 
0715     ret = gb_pm_runtime_get_sync(bundle);
0716     if (ret < 0)
0717         return ret;
0718 
0719     req.light_id = channel->light->id;
0720     req.channel_id = channel->id;
0721 
0722     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
0723                 &req, sizeof(req), &resp, sizeof(resp));
0724     if (!ret)
0725         *fault = le32_to_cpu(resp.fault);
0726 
0727     gb_pm_runtime_put_autosuspend(bundle);
0728 
0729     return ret;
0730 }
0731 
0732 static const struct led_flash_ops gb_lights_flash_ops = {
0733     .flash_brightness_set   = gb_lights_flash_intensity_set,
0734     .flash_brightness_get   = gb_lights_flash_intensity_get,
0735     .strobe_set     = gb_lights_flash_strobe_set,
0736     .strobe_get     = gb_lights_flash_strobe_get,
0737     .timeout_set        = gb_lights_flash_timeout_set,
0738     .fault_get      = gb_lights_flash_fault_get,
0739 };
0740 
0741 static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
0742                         struct gb_channel *channel_torch)
0743 {
0744     char *name;
0745 
0746     /* we can only attach torch to a flash channel */
0747     if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
0748         return 0;
0749 
0750     /* Move torch brightness to the destination */
0751     channel->led->max_brightness = channel_torch->led->max_brightness;
0752 
0753     /* append mode name to flash name */
0754     name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
0755              channel_torch->mode_name);
0756     if (!name)
0757         return -ENOMEM;
0758     kfree(channel->led->name);
0759     channel->led->name = name;
0760 
0761     channel_torch->led = channel->led;
0762 
0763     return 0;
0764 }
0765 
0766 static int __gb_lights_flash_led_register(struct gb_channel *channel)
0767 {
0768     struct gb_connection *connection = get_conn_from_channel(channel);
0769     struct led_classdev_flash *fled = &channel->fled;
0770     struct led_flash_setting *fset;
0771     struct gb_channel *channel_torch;
0772     int ret;
0773 
0774     fled->ops = &gb_lights_flash_ops;
0775 
0776     fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
0777 
0778     fset = &fled->brightness;
0779     fset->min = channel->intensity_uA.min;
0780     fset->max = channel->intensity_uA.max;
0781     fset->step = channel->intensity_uA.step;
0782     fset->val = channel->intensity_uA.max;
0783 
0784     /* Only the flash mode have the timeout constraints settings */
0785     if (channel->mode & GB_CHANNEL_MODE_FLASH) {
0786         fset = &fled->timeout;
0787         fset->min = channel->timeout_us.min;
0788         fset->max = channel->timeout_us.max;
0789         fset->step = channel->timeout_us.step;
0790         fset->val = channel->timeout_us.max;
0791     }
0792 
0793     /*
0794      * If light have torch mode channel, this channel will be the led
0795      * classdev of the registered above flash classdev
0796      */
0797     channel_torch = get_channel_from_mode(channel->light,
0798                           GB_CHANNEL_MODE_TORCH);
0799     if (channel_torch) {
0800         ret = __gb_lights_channel_torch_attach(channel, channel_torch);
0801         if (ret < 0)
0802             goto fail;
0803     }
0804 
0805     ret = led_classdev_flash_register(&connection->bundle->dev, fled);
0806     if (ret < 0)
0807         goto fail;
0808 
0809     channel->is_registered = true;
0810     return 0;
0811 fail:
0812     channel->led = NULL;
0813     return ret;
0814 }
0815 
0816 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
0817 {
0818     if (!channel->is_registered)
0819         return;
0820 
0821     led_classdev_flash_unregister(&channel->fled);
0822 }
0823 
0824 static int gb_lights_channel_flash_config(struct gb_channel *channel)
0825 {
0826     struct gb_connection *connection = get_conn_from_channel(channel);
0827     struct gb_lights_get_channel_flash_config_request req;
0828     struct gb_lights_get_channel_flash_config_response conf;
0829     struct led_flash_setting *fset;
0830     int ret;
0831 
0832     req.light_id = channel->light->id;
0833     req.channel_id = channel->id;
0834 
0835     ret = gb_operation_sync(connection,
0836                 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
0837                 &req, sizeof(req), &conf, sizeof(conf));
0838     if (ret < 0)
0839         return ret;
0840 
0841     /*
0842      * Intensity constraints for flash related modes: flash, torch,
0843      * indicator.  They will be needed for v4l2 registration.
0844      */
0845     fset = &channel->intensity_uA;
0846     fset->min = le32_to_cpu(conf.intensity_min_uA);
0847     fset->max = le32_to_cpu(conf.intensity_max_uA);
0848     fset->step = le32_to_cpu(conf.intensity_step_uA);
0849 
0850     /*
0851      * On flash type, max brightness is set as the number of intensity steps
0852      * available.
0853      */
0854     channel->led->max_brightness = (fset->max - fset->min) / fset->step;
0855 
0856     /* Only the flash mode have the timeout constraints settings */
0857     if (channel->mode & GB_CHANNEL_MODE_FLASH) {
0858         fset = &channel->timeout_us;
0859         fset->min = le32_to_cpu(conf.timeout_min_us);
0860         fset->max = le32_to_cpu(conf.timeout_max_us);
0861         fset->step = le32_to_cpu(conf.timeout_step_us);
0862     }
0863 
0864     return 0;
0865 }
0866 #else
0867 static int gb_lights_channel_flash_config(struct gb_channel *channel)
0868 {
0869     struct gb_connection *connection = get_conn_from_channel(channel);
0870 
0871     dev_err(&connection->bundle->dev, "no support for flash devices\n");
0872     return 0;
0873 }
0874 
0875 static int __gb_lights_flash_led_register(struct gb_channel *channel)
0876 {
0877     return 0;
0878 }
0879 
0880 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
0881 {
0882 }
0883 
0884 #endif
0885 
0886 static int __gb_lights_led_register(struct gb_channel *channel)
0887 {
0888     struct gb_connection *connection = get_conn_from_channel(channel);
0889     struct led_classdev *cdev = get_channel_cdev(channel);
0890     int ret;
0891 
0892     ret = led_classdev_register(&connection->bundle->dev, cdev);
0893     if (ret < 0)
0894         channel->led = NULL;
0895     else
0896         channel->is_registered = true;
0897     return ret;
0898 }
0899 
0900 static int gb_lights_channel_register(struct gb_channel *channel)
0901 {
0902     /* Normal LED channel, just register in led classdev and we are done */
0903     if (!is_channel_flash(channel))
0904         return __gb_lights_led_register(channel);
0905 
0906     /*
0907      * Flash Type need more work, register flash classdev, indicator as
0908      * flash classdev, torch will be led classdev of the flash classdev.
0909      */
0910     if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
0911         return __gb_lights_flash_led_register(channel);
0912 
0913     return 0;
0914 }
0915 
0916 static void __gb_lights_led_unregister(struct gb_channel *channel)
0917 {
0918     struct led_classdev *cdev = get_channel_cdev(channel);
0919 
0920     if (!channel->is_registered)
0921         return;
0922 
0923     led_classdev_unregister(cdev);
0924     kfree(cdev->name);
0925     cdev->name = NULL;
0926     channel->led = NULL;
0927 }
0928 
0929 static void gb_lights_channel_unregister(struct gb_channel *channel)
0930 {
0931     /* The same as register, handle channels differently */
0932     if (!is_channel_flash(channel)) {
0933         __gb_lights_led_unregister(channel);
0934         return;
0935     }
0936 
0937     if (channel->mode & GB_CHANNEL_MODE_TORCH)
0938         __gb_lights_led_unregister(channel);
0939     else
0940         __gb_lights_flash_led_unregister(channel);
0941 }
0942 
0943 static int gb_lights_channel_config(struct gb_light *light,
0944                     struct gb_channel *channel)
0945 {
0946     struct gb_lights_get_channel_config_response conf;
0947     struct gb_lights_get_channel_config_request req;
0948     struct gb_connection *connection = get_conn_from_light(light);
0949     struct led_classdev *cdev = get_channel_cdev(channel);
0950     char *name;
0951     int ret;
0952 
0953     req.light_id = light->id;
0954     req.channel_id = channel->id;
0955 
0956     ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
0957                 &req, sizeof(req), &conf, sizeof(conf));
0958     if (ret < 0)
0959         return ret;
0960 
0961     channel->light = light;
0962     channel->mode = le32_to_cpu(conf.mode);
0963     channel->flags = le32_to_cpu(conf.flags);
0964     channel->color = le32_to_cpu(conf.color);
0965     channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
0966     if (!channel->color_name)
0967         return -ENOMEM;
0968     channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
0969     if (!channel->mode_name)
0970         return -ENOMEM;
0971 
0972     channel->led = cdev;
0973 
0974     name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
0975              channel->color_name, channel->mode_name);
0976     if (!name)
0977         return -ENOMEM;
0978 
0979     cdev->name = name;
0980 
0981     cdev->max_brightness = conf.max_brightness;
0982 
0983     ret = channel_attr_groups_set(channel, cdev);
0984     if (ret < 0)
0985         return ret;
0986 
0987     gb_lights_led_operations_set(channel, cdev);
0988 
0989     /*
0990      * If it is not a flash related channel (flash, torch or indicator) we
0991      * are done here. If not, continue and fetch flash related
0992      * configurations.
0993      */
0994     if (!is_channel_flash(channel))
0995         return ret;
0996 
0997     light->has_flash = true;
0998 
0999     return gb_lights_channel_flash_config(channel);
1000 }
1001 
1002 static int gb_lights_light_config(struct gb_lights *glights, u8 id)
1003 {
1004     struct gb_light *light = &glights->lights[id];
1005     struct gb_lights_get_light_config_request req;
1006     struct gb_lights_get_light_config_response conf;
1007     int ret;
1008     int i;
1009 
1010     light->glights = glights;
1011     light->id = id;
1012 
1013     req.id = id;
1014 
1015     ret = gb_operation_sync(glights->connection,
1016                 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
1017                 &req, sizeof(req), &conf, sizeof(conf));
1018     if (ret < 0)
1019         return ret;
1020 
1021     if (!conf.channel_count)
1022         return -EINVAL;
1023     if (!strlen(conf.name))
1024         return -EINVAL;
1025 
1026     light->channels_count = conf.channel_count;
1027     light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1028     if (!light->name)
1029         return -ENOMEM;
1030     light->channels = kcalloc(light->channels_count,
1031                   sizeof(struct gb_channel), GFP_KERNEL);
1032     if (!light->channels)
1033         return -ENOMEM;
1034 
1035     /* First we collect all the configurations for all channels */
1036     for (i = 0; i < light->channels_count; i++) {
1037         light->channels[i].id = i;
1038         ret = gb_lights_channel_config(light, &light->channels[i]);
1039         if (ret < 0)
1040             return ret;
1041     }
1042 
1043     return 0;
1044 }
1045 
1046 static int gb_lights_light_register(struct gb_light *light)
1047 {
1048     int ret;
1049     int i;
1050 
1051     /*
1052      * Then, if everything went ok in getting configurations, we register
1053      * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1054      * found.
1055      */
1056     for (i = 0; i < light->channels_count; i++) {
1057         ret = gb_lights_channel_register(&light->channels[i]);
1058         if (ret < 0)
1059             return ret;
1060 
1061         mutex_init(&light->channels[i].lock);
1062     }
1063 
1064     light->ready = true;
1065 
1066     if (light->has_flash) {
1067         ret = gb_lights_light_v4l2_register(light);
1068         if (ret < 0) {
1069             light->has_flash = false;
1070             return ret;
1071         }
1072     }
1073 
1074     return 0;
1075 }
1076 
1077 static void gb_lights_channel_free(struct gb_channel *channel)
1078 {
1079     kfree(channel->attrs);
1080     kfree(channel->attr_group);
1081     kfree(channel->attr_groups);
1082     kfree(channel->color_name);
1083     kfree(channel->mode_name);
1084     mutex_destroy(&channel->lock);
1085 }
1086 
1087 static void gb_lights_channel_release(struct gb_channel *channel)
1088 {
1089     channel->releasing = true;
1090 
1091     gb_lights_channel_unregister(channel);
1092 
1093     gb_lights_channel_free(channel);
1094 }
1095 
1096 static void gb_lights_light_release(struct gb_light *light)
1097 {
1098     int i;
1099 
1100     light->ready = false;
1101 
1102     if (light->has_flash)
1103         gb_lights_light_v4l2_unregister(light);
1104     light->has_flash = false;
1105 
1106     for (i = 0; i < light->channels_count; i++)
1107         gb_lights_channel_release(&light->channels[i]);
1108     light->channels_count = 0;
1109 
1110     kfree(light->channels);
1111     light->channels = NULL;
1112     kfree(light->name);
1113     light->name = NULL;
1114 }
1115 
1116 static void gb_lights_release(struct gb_lights *glights)
1117 {
1118     int i;
1119 
1120     if (!glights)
1121         return;
1122 
1123     mutex_lock(&glights->lights_lock);
1124     if (!glights->lights)
1125         goto free_glights;
1126 
1127     for (i = 0; i < glights->lights_count; i++)
1128         gb_lights_light_release(&glights->lights[i]);
1129 
1130     kfree(glights->lights);
1131 
1132 free_glights:
1133     mutex_unlock(&glights->lights_lock);
1134     mutex_destroy(&glights->lights_lock);
1135     kfree(glights);
1136 }
1137 
1138 static int gb_lights_get_count(struct gb_lights *glights)
1139 {
1140     struct gb_lights_get_lights_response resp;
1141     int ret;
1142 
1143     ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1144                 NULL, 0, &resp, sizeof(resp));
1145     if (ret < 0)
1146         return ret;
1147 
1148     if (!resp.lights_count)
1149         return -EINVAL;
1150 
1151     glights->lights_count = resp.lights_count;
1152 
1153     return 0;
1154 }
1155 
1156 static int gb_lights_create_all(struct gb_lights *glights)
1157 {
1158     struct gb_connection *connection = glights->connection;
1159     int ret;
1160     int i;
1161 
1162     mutex_lock(&glights->lights_lock);
1163     ret = gb_lights_get_count(glights);
1164     if (ret < 0)
1165         goto out;
1166 
1167     glights->lights = kcalloc(glights->lights_count,
1168                   sizeof(struct gb_light), GFP_KERNEL);
1169     if (!glights->lights) {
1170         ret = -ENOMEM;
1171         goto out;
1172     }
1173 
1174     for (i = 0; i < glights->lights_count; i++) {
1175         ret = gb_lights_light_config(glights, i);
1176         if (ret < 0) {
1177             dev_err(&connection->bundle->dev,
1178                 "Fail to configure lights device\n");
1179             goto out;
1180         }
1181     }
1182 
1183 out:
1184     mutex_unlock(&glights->lights_lock);
1185     return ret;
1186 }
1187 
1188 static int gb_lights_register_all(struct gb_lights *glights)
1189 {
1190     struct gb_connection *connection = glights->connection;
1191     int ret = 0;
1192     int i;
1193 
1194     mutex_lock(&glights->lights_lock);
1195     for (i = 0; i < glights->lights_count; i++) {
1196         ret = gb_lights_light_register(&glights->lights[i]);
1197         if (ret < 0) {
1198             dev_err(&connection->bundle->dev,
1199                 "Fail to enable lights device\n");
1200             break;
1201         }
1202     }
1203 
1204     mutex_unlock(&glights->lights_lock);
1205     return ret;
1206 }
1207 
1208 static int gb_lights_request_handler(struct gb_operation *op)
1209 {
1210     struct gb_connection *connection = op->connection;
1211     struct device *dev = &connection->bundle->dev;
1212     struct gb_lights *glights = gb_connection_get_data(connection);
1213     struct gb_light *light;
1214     struct gb_message *request;
1215     struct gb_lights_event_request *payload;
1216     int ret =  0;
1217     u8 light_id;
1218     u8 event;
1219 
1220     if (op->type != GB_LIGHTS_TYPE_EVENT) {
1221         dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1222         return -EINVAL;
1223     }
1224 
1225     request = op->request;
1226 
1227     if (request->payload_size < sizeof(*payload)) {
1228         dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1229             request->payload_size, sizeof(*payload));
1230         return -EINVAL;
1231     }
1232 
1233     payload = request->payload;
1234     light_id = payload->light_id;
1235 
1236     if (light_id >= glights->lights_count ||
1237         !glights->lights[light_id].ready) {
1238         dev_err(dev, "Event received for unconfigured light id: %d\n",
1239             light_id);
1240         return -EINVAL;
1241     }
1242 
1243     event = payload->event;
1244 
1245     if (event & GB_LIGHTS_LIGHT_CONFIG) {
1246         light = &glights->lights[light_id];
1247 
1248         mutex_lock(&glights->lights_lock);
1249         gb_lights_light_release(light);
1250         ret = gb_lights_light_config(glights, light_id);
1251         if (!ret)
1252             ret = gb_lights_light_register(light);
1253         if (ret < 0)
1254             gb_lights_light_release(light);
1255         mutex_unlock(&glights->lights_lock);
1256     }
1257 
1258     return ret;
1259 }
1260 
1261 static int gb_lights_probe(struct gb_bundle *bundle,
1262                const struct greybus_bundle_id *id)
1263 {
1264     struct greybus_descriptor_cport *cport_desc;
1265     struct gb_connection *connection;
1266     struct gb_lights *glights;
1267     int ret;
1268 
1269     if (bundle->num_cports != 1)
1270         return -ENODEV;
1271 
1272     cport_desc = &bundle->cport_desc[0];
1273     if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1274         return -ENODEV;
1275 
1276     glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1277     if (!glights)
1278         return -ENOMEM;
1279 
1280     mutex_init(&glights->lights_lock);
1281 
1282     connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1283                       gb_lights_request_handler);
1284     if (IS_ERR(connection)) {
1285         ret = PTR_ERR(connection);
1286         goto out;
1287     }
1288 
1289     glights->connection = connection;
1290     gb_connection_set_data(connection, glights);
1291 
1292     greybus_set_drvdata(bundle, glights);
1293 
1294     /* We aren't ready to receive an incoming request yet */
1295     ret = gb_connection_enable_tx(connection);
1296     if (ret)
1297         goto error_connection_destroy;
1298 
1299     /*
1300      * Setup all the lights devices over this connection, if anything goes
1301      * wrong tear down all lights
1302      */
1303     ret = gb_lights_create_all(glights);
1304     if (ret < 0)
1305         goto error_connection_disable;
1306 
1307     /* We are ready to receive an incoming request now, enable RX as well */
1308     ret = gb_connection_enable(connection);
1309     if (ret)
1310         goto error_connection_disable;
1311 
1312     /* Enable & register lights */
1313     ret = gb_lights_register_all(glights);
1314     if (ret < 0)
1315         goto error_connection_disable;
1316 
1317     gb_pm_runtime_put_autosuspend(bundle);
1318 
1319     return 0;
1320 
1321 error_connection_disable:
1322     gb_connection_disable(connection);
1323 error_connection_destroy:
1324     gb_connection_destroy(connection);
1325 out:
1326     gb_lights_release(glights);
1327     return ret;
1328 }
1329 
1330 static void gb_lights_disconnect(struct gb_bundle *bundle)
1331 {
1332     struct gb_lights *glights = greybus_get_drvdata(bundle);
1333 
1334     if (gb_pm_runtime_get_sync(bundle))
1335         gb_pm_runtime_get_noresume(bundle);
1336 
1337     gb_connection_disable(glights->connection);
1338     gb_connection_destroy(glights->connection);
1339 
1340     gb_lights_release(glights);
1341 }
1342 
1343 static const struct greybus_bundle_id gb_lights_id_table[] = {
1344     { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1345     { }
1346 };
1347 MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1348 
1349 static struct greybus_driver gb_lights_driver = {
1350     .name       = "lights",
1351     .probe      = gb_lights_probe,
1352     .disconnect = gb_lights_disconnect,
1353     .id_table   = gb_lights_id_table,
1354 };
1355 module_greybus_driver(gb_lights_driver);
1356 
1357 MODULE_LICENSE("GPL v2");