From patchwork Wed Feb 18 16:20:40 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacek Anaszewski X-Patchwork-Id: 28306 Received: from mail.tu-berlin.de ([130.149.7.33]) by www.linuxtv.org with esmtp (Exim 4.72) (envelope-from ) id 1YO7SX-0007tO-QT; Wed, 18 Feb 2015 17:26:45 +0100 X-tubIT-Incoming-IP: 209.132.180.67 Received: from vger.kernel.org ([209.132.180.67]) by mail.tu-berlin.de (exim-4.72/mailfrontend-5) with esmtp id 1YO7SV-00040d-6q; Wed, 18 Feb 2015 17:26:45 +0100 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752588AbbBRQ0k (ORCPT + 1 other); Wed, 18 Feb 2015 11:26:40 -0500 Received: from mailout2.samsung.com ([203.254.224.25]:11224 "EHLO mailout2.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752295AbbBRQ0j (ORCPT ); Wed, 18 Feb 2015 11:26:39 -0500 Received: from epcpsbgm2.samsung.com (epcpsbgm2 [203.254.230.27]) by mailout2.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0NJZ00HWI70DXO70@mailout2.samsung.com>; Thu, 19 Feb 2015 01:26:37 +0900 (KST) X-AuditID: cbfee61b-f79d76d0000024d6-33-54e4bd3d544e Received: from epmmp1.local.host ( [203.254.227.16]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id 1F.24.09430.D3DB4E45; Thu, 19 Feb 2015 01:26:37 +0900 (KST) Received: from AMDC2362.DIGITAL.local ([106.120.53.23]) by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0NJZ003WS6QJDJ20@mmp1.samsung.com>; Thu, 19 Feb 2015 01:26:37 +0900 (KST) From: Jacek Anaszewski To: linux-leds@vger.kernel.org, linux-media@vger.kernel.org, devicetree@vger.kernel.org Cc: kyungmin.park@samsung.com, pavel@ucw.cz, cooloney@gmail.com, rpurdie@rpsys.net, sakari.ailus@iki.fi, s.nawrocki@samsung.com, Jacek Anaszewski , Sakari Ailus Subject: [PATCH/RFC v11 19/20] leds: max77693: add support for V4L2 Flash sub-device Date: Wed, 18 Feb 2015 17:20:40 +0100 Message-id: <1424276441-3969-20-git-send-email-j.anaszewski@samsung.com> X-Mailer: git-send-email 1.7.9.5 In-reply-to: <1424276441-3969-1-git-send-email-j.anaszewski@samsung.com> References: <1424276441-3969-1-git-send-email-j.anaszewski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrPLMWRmVeSWpSXmKPExsVy+t9jAV3bvU9CDJ63G1oc3TmRyWL+kXOs Fr1XnzNanG16w26x9c06RoueDVtZLe6eOspmsXvXU1aLw2/aWS3O7F/JZvFpyzcmB26PnbPu snsc/rqQxWPeyUCPPfN/sHr0bVnF6LFi9Xd2j8+b5ALYo7hsUlJzMstSi/TtErgyrlzZzVzw zrfi3NyfzA2Mdx26GDk5JARMJCZum8MGYYtJXLi3Hsjm4hASWMQosaznNAtIQkignUliUqM/ iM0mYCjx88VrJhBbRCBS4t3RS8wgDcwC9xklVj1awgqSEBYIk3h37jIziM0ioCoxccNasAZe AQ+Jnw3P2LsYOYC2KUjMmWQDYnIChS8894BY5S4x/eY71gmMvAsYGVYxiqYWJBcUJ6XnGukV J+YWl+al6yXn525iBAfjM+kdjKsaLA4xCnAwKvHwNvx+HCLEmlhWXJl7iFGCg1lJhDd35ZMQ Id6UxMqq1KL8+KLSnNTiQ4zSHCxK4rxK9m0hQgLpiSWp2ampBalFMFkmDk6pBkZTq7+xkg/e srM5qhy/e9NH036ZV21D6Yx32SdKV3o754v+SK4IUn5eOss7vausd5m2w4es1Ef/QmWPzl1y 0/b5jgnODv0sHlLdf5L2Se62Ldqxs/oX/+fHM7I+/PPuLxSUes8lzP1NM2utr+KbDNbLQQlz 1VdL1rtc2zFbxXzHks3PbDxtLyqxFGckGmoxFxUnAgDIjy8UQgIAAA== Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-PMX-Version: 6.0.0.2142326, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2015.2.18.161817 X-PMX-Spam: Gauge=IIIIIIII, Probability=8%, Report=' MULTIPLE_RCPTS 0.1, HTML_00_01 0.05, HTML_00_10 0.05, REFERENCES 0, URI_ENDS_IN_HTML 0, __ANY_URI 0, __CP_URI_IN_BODY 0, __FRAUD_BADTHINGS 0, __FRAUD_BODY_WEBMAIL 0, __FRAUD_WEBMAIL 0, __HAS_FROM 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __HAS_X_MAILING_LIST 0, __IN_REP_TO 0, __MIME_TEXT_ONLY 0, __MULTIPLE_RCPTS_CC_X2 0, __REFERENCES 0, __SANE_MSGID 0, __SUBJ_ALPHA_END 0, __TO_MALFORMED_2 0, __TO_NO_NAME 0, __URI_NO_WWW 0, __URI_NS ' Add support for V4L2 Flash sub-device to the max77693 LED Flash class driver. The support allows for V4L2 Flash sub-device to take the control of the LED Flash class device. Signed-off-by: Jacek Anaszewski Acked-by: Kyungmin Park Cc: Bryan Wu Cc: Richard Purdie Cc: Sakari Ailus --- drivers/leds/leds-max77693.c | 149 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 141 insertions(+), 8 deletions(-) diff --git a/drivers/leds/leds-max77693.c b/drivers/leds/leds-max77693.c index 45d50d9..af06ca7 100644 --- a/drivers/leds/leds-max77693.c +++ b/drivers/leds/leds-max77693.c @@ -21,6 +21,7 @@ #include #include #include +#include #define MODE_OFF 0 #define MODE_FLASH(a) (1 << (a)) @@ -64,6 +65,8 @@ struct max77693_sub_led { struct led_classdev_flash fled_cdev; /* assures led-triggers compatibility */ struct work_struct work_brightness_set; + /* V4L2 Flash device */ + struct v4l2_flash *v4l2_flash; /* brightness cache */ unsigned int torch_brightness; @@ -640,6 +643,32 @@ unlock: return ret; } +#if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) +static int max77693_led_external_strobe_set( + struct v4l2_flash *v4l2_flash, + bool enable) +{ + struct max77693_sub_led *sub_led = + flcdev_to_sub_led(v4l2_flash->fled_cdev); + struct max77693_led_device *led = sub_led_to_led(sub_led); + int fled_id = sub_led->fled_id; + int ret; + + mutex_lock(&led->lock); + + if (enable) + ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id)); + else + ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id)); + + mutex_unlock(&led->lock); + + return ret; +} +#else +#define max77693_led_external_strobe_set(v4l2_flash, enable) (NULL) +#endif + static int max77693_led_flash_fault_get( struct led_classdev_flash *fled_cdev, u32 *fault) @@ -688,7 +717,8 @@ static int max77693_led_flash_timeout_set( } static int max77693_led_parse_dt(struct max77693_led_device *led, - struct max77693_led_config_data *cfg) + struct max77693_led_config_data *cfg, + struct device_node **sub_nodes) { struct device *dev = &led->pdev->dev; struct max77693_sub_led *sub_leds = led->sub_leds; @@ -734,6 +764,13 @@ static int max77693_led_parse_dt(struct max77693_led_device *led, return -EINVAL; } + if (sub_nodes[fled_id]) { + dev_err(dev, + "Conflicting \"led-sources\" DT properties\n"); + return -EINVAL; + } + + sub_nodes[fled_id] = child_node; sub_leds[fled_id].fled_id = fled_id; ret = of_property_read_string(child_node, "label", @@ -825,11 +862,12 @@ static void max77693_led_validate_configuration(struct max77693_led_device *led, } static int max77693_led_get_configuration(struct max77693_led_device *led, - struct max77693_led_config_data *cfg) + struct max77693_led_config_data *cfg, + struct device_node **sub_nodes) { int ret; - ret = max77693_led_parse_dt(led, cfg); + ret = max77693_led_parse_dt(led, cfg, sub_nodes); if (ret < 0) return ret; @@ -853,6 +891,12 @@ static const struct led_flash_ops flash_ops = { .fault_get = max77693_led_flash_fault_get, }; +#if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) +static const struct v4l2_flash_ops v4l2_flash_ops = { + .external_strobe_set = max77693_led_external_strobe_set, +}; +#endif + static void max77693_init_flash_settings(struct max77693_led_device *led, int fled_id, struct max77693_led_config_data *cfg, @@ -887,6 +931,46 @@ static void max77693_init_flash_settings(struct max77693_led_device *led, setting->val = setting->max; } +#if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) +static void max77693_init_v4l2_ctrl_config(int fled_id, + struct max77693_led_settings *s, + struct v4l2_flash_ctrl_config *config) +{ + struct led_flash_setting *setting; + struct v4l2_ctrl_config *c; + + c = &config->intensity; + setting = &s->torch_brightness; + c->min = setting->min; + c->max = setting->max; + c->step = setting->step; + c->def = setting->val; + + c = &config->flash_intensity; + setting = &s->flash_brightness; + c->min = setting->min; + c->max = setting->max; + c->step = setting->step; + c->def = setting->val; + + c = &config->flash_timeout; + setting = &s->flash_timeout; + c->min = setting->min; + c->max = setting->max; + c->step = setting->step; + c->def = setting->val; + + /* Init flash faults config */ + config->flash_faults = V4L2_FLASH_FAULT_OVER_VOLTAGE | + V4L2_FLASH_FAULT_SHORT_CIRCUIT | + V4L2_FLASH_FAULT_OVER_CURRENT; + + config->has_external_strobe = true; +} +#else +#define max77693_init_v4l2_ctrl_config(fled_id, s, config) +#endif + static int max77693_set_available_sync_led(struct max77693_led_device *led, int fled_id) { @@ -906,7 +990,8 @@ static int max77693_set_available_sync_led(struct max77693_led_device *led, static void max77693_init_fled_cdev(struct max77693_led_device *led, int fled_id, - struct max77693_led_config_data *cfg) + struct max77693_led_config_data *cfg, + struct v4l2_flash_ctrl_config *v4l2_flash_cfg) { struct led_classdev_flash *fled_cdev; struct led_classdev *led_cdev; @@ -915,6 +1000,8 @@ static void max77693_init_fled_cdev(struct max77693_led_device *led, /* Initialize flash settings */ max77693_init_flash_settings(led, fled_id, cfg, &settings); + /* Initialize V4L2 Flash config basing on initialized settings */ + max77693_init_v4l2_ctrl_config(fled_id, &settings, v4l2_flash_cfg); /* Initialize LED Flash class device */ fled_cdev = &sub_led->fled_cdev; @@ -937,15 +1024,52 @@ static void max77693_init_fled_cdev(struct max77693_led_device *led, sub_led->flash_timeout = fled_cdev->timeout.val; } +static int max77693_register_led(struct max77693_sub_led *sub_led, + struct v4l2_flash_ctrl_config *v4l2_flash_cfg, + struct device_node *sub_node) +{ + struct max77693_led_device *led = sub_led_to_led(sub_led); + struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev; + struct device *dev = &led->pdev->dev; + int ret; + + /* Register in the LED subsystem */ + ret = led_classdev_flash_register(dev, fled_cdev); + if (ret < 0) + return ret; + + of_node_get(sub_node); + fled_cdev->led_cdev.dev->of_node = sub_node; + + /* Register in the V4L2 subsystem. */ + sub_led->v4l2_flash = v4l2_flash_init(fled_cdev, &v4l2_flash_ops, + v4l2_flash_cfg); + if (IS_ERR(sub_led->v4l2_flash)) { + ret = PTR_ERR(sub_led->v4l2_flash); + goto err_v4l2_flash_init; + } + + return 0; + +err_v4l2_flash_init: + of_node_put(sub_node); + led_classdev_flash_unregister(fled_cdev); + return ret; +} + static int max77693_led_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; struct max77693_dev *iodev = dev_get_drvdata(dev->parent); struct max77693_led_device *led; struct max77693_sub_led *sub_leds; + struct device_node *sub_nodes[2] = { NULL, NULL }; struct max77693_led_config_data cfg = {}; + struct v4l2_flash_ctrl_config v4l2_flash_config[2]; int init_fled_cdev[2], i, ret; + memset(v4l2_flash_config, 0, sizeof(v4l2_flash_config)); + led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); if (!led) return -ENOMEM; @@ -955,7 +1079,7 @@ static int max77693_led_probe(struct platform_device *pdev) sub_leds = led->sub_leds; platform_set_drvdata(pdev, led); - ret = max77693_led_get_configuration(led, &cfg); + ret = max77693_led_get_configuration(led, &cfg, sub_nodes); if (ret < 0) return ret; @@ -971,7 +1095,8 @@ static int max77693_led_probe(struct platform_device *pdev) /* Initialize LED Flash class device(s) */ for (i = FLED1; i <= FLED2; ++i) if (init_fled_cdev[i]) - max77693_init_fled_cdev(led, i, &cfg); + max77693_init_fled_cdev(led, i, &cfg, + &v4l2_flash_config[i]); /* Setup sub-leds available for flash strobe synchronization */ if (cfg.num_leds == 2) @@ -983,12 +1108,14 @@ static int max77693_led_probe(struct platform_device *pdev) mutex_init(&led->lock); - /* Register LED Flash class device(s) */ + /* Register LED Flash class and related V4L2 Flash device(s) */ for (i = FLED1; i <= FLED2; ++i) { if (!init_fled_cdev[i]) continue; - ret = led_classdev_flash_register(dev, &sub_leds[i].fled_cdev); + ret = max77693_register_led(&sub_leds[i], + &v4l2_flash_config[i], + sub_nodes[i]); if (ret < 0) { /* * At this moment FLED1 might have been already @@ -1008,6 +1135,8 @@ err_register_led2: /* It is possible than only FLED2 was to be registered */ if (!init_fled_cdev[FLED1]) goto err_register_led1; + v4l2_flash_release(sub_leds[FLED1].v4l2_flash); + of_node_put(sub_leds[FLED1].fled_cdev.led_cdev.dev->of_node); led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev); err_register_led1: mutex_destroy(&led->lock); @@ -1021,11 +1150,15 @@ static int max77693_led_remove(struct platform_device *pdev) struct max77693_sub_led *sub_leds = led->sub_leds; if (led->iout_joint || max77693_fled_used(led, FLED1)) { + v4l2_flash_release(sub_leds[FLED1].v4l2_flash); + of_node_put(sub_leds[FLED1].fled_cdev.led_cdev.dev->of_node); led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev); cancel_work_sync(&sub_leds[FLED1].work_brightness_set); } if (!led->iout_joint && max77693_fled_used(led, FLED2)) { + v4l2_flash_release(sub_leds[FLED2].v4l2_flash); + of_node_put(sub_leds[FLED2].fled_cdev.led_cdev.dev->of_node); led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev); cancel_work_sync(&sub_leds[FLED2].work_brightness_set); }