From patchwork Fri Nov 28 09:18:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacek Anaszewski X-Patchwork-Id: 5402131 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D26449F319 for ; Fri, 28 Nov 2014 09:23:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5F04D2013D for ; Fri, 28 Nov 2014 09:23:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4506B20148 for ; Fri, 28 Nov 2014 09:23:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751909AbaK1JVE (ORCPT ); Fri, 28 Nov 2014 04:21:04 -0500 Received: from mailout3.samsung.com ([203.254.224.33]:44789 "EHLO mailout3.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751262AbaK1JVA (ORCPT ); Fri, 28 Nov 2014 04:21:00 -0500 Received: from epcpsbgm1.samsung.com (epcpsbgm1 [203.254.230.26]) by mailout3.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0NFQ0032DSMRL490@mailout3.samsung.com>; Fri, 28 Nov 2014 18:20:51 +0900 (KST) X-AuditID: cbfee61a-f79c06d000004e71-29-54783e72a4ad Received: from epmmp2 ( [203.254.227.17]) by epcpsbgm1.samsung.com (EPCPMTA) with SMTP id F2.01.20081.27E38745; Fri, 28 Nov 2014 18:20:51 +0900 (KST) Received: from AMDC2362.DIGITAL.local ([106.120.53.23]) by mmp2.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0NFQ00JKNSI8LB60@mmp2.samsung.com>; Fri, 28 Nov 2014 18:20:50 +0900 (KST) From: Jacek Anaszewski To: linux-leds@vger.kernel.org, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Cc: kyungmin.park@samsung.com, b.zolnierkie@samsung.com, pavel@ucw.cz, cooloney@gmail.com, rpurdie@rpsys.net, sakari.ailus@iki.fi, s.nawrocki@samsung.com, Jacek Anaszewski Subject: [PATCH/RFC v8 12/14] leds: Add driver for AAT1290 current regulator Date: Fri, 28 Nov 2014 10:18:04 +0100 Message-id: <1417166286-27685-13-git-send-email-j.anaszewski@samsung.com> X-Mailer: git-send-email 1.7.9.5 In-reply-to: <1417166286-27685-1-git-send-email-j.anaszewski@samsung.com> References: <1417166286-27685-1-git-send-email-j.anaszewski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrFLMWRmVeSWpSXmKPExsVy+t9jQd1iu4oQgzUpFhtnrGe1OLpzIpNF 79XnjBZnm96wW1zeNYfNYuubdYwWPRu2slrcPXWUzWL3rqesFofftLNanNm/ks2B22PnrLvs Hoe/LmTx2DP/B6tH35ZVjB4rVn9n9/i8SS6ALYrLJiU1J7MstUjfLoEr4/mS32wFzSUVzT3d rA2MFxO6GDk5JARMJG7NO8cIYYtJXLi3nq2LkYtDSGA6o8S3yQ+hnHYmif5DJ8Gq2AQMJX6+ eM3UxcjBISIQLXHpWD1IDbPAKUaJdTNXg9UIC/hI3N+3nRnEZhFQlei4+4IdxOYV8JQ4t3EO M0ivhICCxJxJNiBhTqDw3M9rWUDCQgIeEotepUxg5F3AyLCKUTS1ILmgOCk911CvODG3uDQv XS85P3cTIzgEn0ntYFzZYHGIUYCDUYmH98e/8hAh1sSy4srcQ4wSHMxKIrynvgKFeFMSK6tS i/Lji0pzUosPMUpzsCiJ8964mRsiJJCeWJKanZpakFoEk2Xi4JRqYJzTaLooInD/do8pX55U f/qtfy2hlZX9HK9RZWTfqQX2d3OrvoXaczgxG9cUnwr+9/bf5tNR3I/Vm8wucrSGzj8g6PTD UlOQ5VLFkZeiv2UPzrE79LGTmeOXFzvrpldthu1bTi9vunPm2Jtb+zUvuR3gcDwtur1hmcvJ xdeWOP/K1ss9s0y46YoSS3FGoqEWc1FxIgACJl1FPQIAAA== Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds a driver for the 1.5A Step-Up Current Regulator for Flash LEDs. The device is programmed through a Skyworks proprietary AS2Cwire serial digital interface. Signed-off-by: Jacek Anaszewski Acked-by: Kyungmin Park Cc: Bryan Wu Cc: Richard Purdie Signed-off-by: Pavel Machek --- drivers/leds/Kconfig | 7 + drivers/leds/Makefile | 1 + drivers/leds/leds-aat1290.c | 472 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 480 insertions(+) create mode 100644 drivers/leds/leds-aat1290.c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 2e66d55..ec4b78c 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -39,6 +39,13 @@ config LEDS_88PM860X This option enables support for on-chip LED drivers found on Marvell Semiconductor 88PM8606 PMIC. +config LEDS_AAT1290 + tristate "LED support for the AAT1290" + depends on LEDS_CLASS_FLASH + depends on OF + help + This option enables support for the LEDs on the AAT1290. + config LEDS_LM3530 tristate "LCD Backlight driver for LM3530" depends on LEDS_CLASS diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index 57ca62b..b802251 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_LEDS_TRIGGERS) += led-triggers.o # LED Platform Drivers obj-$(CONFIG_LEDS_88PM860X) += leds-88pm860x.o +obj-$(CONFIG_LEDS_AAT1290) += leds-aat1290.o obj-$(CONFIG_LEDS_BD2802) += leds-bd2802.o obj-$(CONFIG_LEDS_LOCOMO) += leds-locomo.o obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o diff --git a/drivers/leds/leds-aat1290.c b/drivers/leds/leds-aat1290.c new file mode 100644 index 0000000..2f8bd99 --- /dev/null +++ b/drivers/leds/leds-aat1290.c @@ -0,0 +1,472 @@ +/* + * LED Flash class driver for the AAT1290 + * 1.5A Step-Up Current Regulator for Flash LEDs + * + * Copyright (C) 2014, Samsung Electronics Co., Ltd. + * Author: Jacek Anaszewski + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define AAT1290_MOVIE_MODE_CURRENT_ADDR 17 +#define AAT1290_FLASH_SAFETY_TIMER_ADDR 18 +#define AAT1290_MOVIE_MODE_CONFIG_ADDR 19 +#define AAT1290_MM_CURRENT_RATIO_ADDR 20 +#define AAT1290_LATCH_TIME_US 500 +#define AAT1290_EN_SET_TICK_TIME_US 1 +#define AAT1290_MOVIE_MODE_OFF 1 +#define AAT1290_MOVIE_MODE_ON 3 +#define AAT1290_MAX_MM_CURR_PERCENT_0 16 +#define AAT1290_MAX_MM_CURR_PERCENT_100 1 +#define AAT1290_FLASH_TM_NUM_LEVELS 16 + +#define AAT1290_MM_TO_FL_1_92 1 +#define AAT1290_MM_TO_FL_3_7 2 +#define AAT1290_MM_TO_FL_5_5 3 +#define AAT1290_MM_TO_FL_7_3 4 +#define AAT1290_MM_TO_FL_9 5 +#define AAT1290_MM_TO_FL_10_7 6 +#define AAT1290_MM_TO_FL_12_4 7 +#define AAT1290_MM_TO_FL_14 8 +#define AAT1290_MM_TO_FL_15_9 9 +#define AAT1290_MM_TO_FL_17_5 10 +#define AAT1290_MM_TO_FL_19_1 11 +#define AAT1290_MM_TO_FL_20_8 12 +#define AAT1290_MM_TO_FL_22_4 13 +#define AAT1290_MM_TO_FL_24 14 +#define AAT1290_MM_TO_FL_25_6 15 +#define AAT1290_MM_TO_FL_OFF 16 + +struct aat1290_led_settings { + struct led_flash_setting torch_brightness; + struct led_flash_setting flash_brightness; + struct led_flash_setting flash_timeout; +}; + +struct aat1290_led { + struct platform_device *pdev; + struct mutex lock; + + struct led_classdev_flash ldev; + struct v4l2_flash *v4l2_flash; + + int flen_gpio; + int en_set_gpio; + + u32 max_flash_tm; + bool movie_mode; + + char *label; + unsigned int torch_brightness; + unsigned int flash_timeout; + struct work_struct work_brightness_set; +}; + +static struct aat1290_led *ldev_to_led(struct led_classdev_flash *ldev) +{ + return container_of(ldev, struct aat1290_led, ldev); +} + +static void aat1290_as2cwire_write(struct aat1290_led *led, int addr, int value) +{ + int i; + + gpio_set_value(led->flen_gpio, 0); + gpio_set_value(led->en_set_gpio, 0); + + udelay(10); + + /* write address */ + for (i = 0; i < addr; ++i) { + udelay(AAT1290_EN_SET_TICK_TIME_US); + gpio_set_value(led->en_set_gpio, 0); + udelay(AAT1290_EN_SET_TICK_TIME_US); + gpio_set_value(led->en_set_gpio, 1); + } + + udelay(AAT1290_LATCH_TIME_US); + + /* write data */ + for (i = 0; i < value; ++i) { + udelay(AAT1290_EN_SET_TICK_TIME_US); + gpio_set_value(led->en_set_gpio, 0); + udelay(AAT1290_EN_SET_TICK_TIME_US); + gpio_set_value(led->en_set_gpio, 1); + } + + udelay(AAT1290_LATCH_TIME_US); +} + +static void aat1290_set_flash_safety_timer(struct aat1290_led *led, + unsigned int micro_sec) +{ + struct led_classdev_flash *flash = &led->ldev; + struct led_flash_setting *flash_tm = &flash->timeout; + int flash_tm_reg = AAT1290_FLASH_TM_NUM_LEVELS - + (micro_sec / flash_tm->step) + 1; + + aat1290_as2cwire_write(led, AAT1290_FLASH_SAFETY_TIMER_ADDR, + flash_tm_reg); +} + +static void aat1290_brightness_set(struct aat1290_led *led, + enum led_brightness brightness) +{ + mutex_lock(&led->lock); + + if (brightness == 0) { + gpio_set_value(led->flen_gpio, 0); + gpio_set_value(led->en_set_gpio, 0); + goto unlock; + } + + if (!led->movie_mode) { + aat1290_as2cwire_write(led, AAT1290_MM_CURRENT_RATIO_ADDR, + AAT1290_MM_TO_FL_1_92); + led->movie_mode = true; + } + + aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CURRENT_ADDR, + AAT1290_MAX_MM_CURR_PERCENT_0 - brightness); + aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CONFIG_ADDR, + AAT1290_MOVIE_MODE_ON); +unlock: + mutex_unlock(&led->lock); +} + +/* LED subsystem callbacks */ + +static void aat1290_brightness_set_work(struct work_struct *work) +{ + struct aat1290_led *led = + container_of(work, struct aat1290_led, work_brightness_set); + + aat1290_brightness_set(led, led->torch_brightness); +} + +static void aat1290_led_brightness_set(struct led_classdev *led_cdev, + enum led_brightness brightness) +{ + struct led_classdev_flash *flash = lcdev_to_flash(led_cdev); + struct aat1290_led *led = ldev_to_led(flash); + + led->torch_brightness = brightness; + schedule_work(&led->work_brightness_set); +} + +static int aat1290_torch_brightness_set(struct led_classdev *led_cdev, + enum led_brightness brightness) +{ + struct led_classdev_flash *flash = lcdev_to_flash(led_cdev); + struct aat1290_led *led = ldev_to_led(flash); + + aat1290_brightness_set(led, brightness); + + return 0; +} + +static int aat1290_led_flash_strobe_set(struct led_classdev_flash *flash, + bool state) + +{ + struct aat1290_led *led = ldev_to_led(flash); + struct led_classdev *led_cdev = &flash->led_cdev; + struct led_flash_setting *timeout = &flash->timeout; + + mutex_lock(&led->lock); + + if (state == 0) { + gpio_set_value(led->flen_gpio, 0); + gpio_set_value(led->en_set_gpio, 0); + goto unlock; + } + + aat1290_set_flash_safety_timer(led, timeout->val); + + /* + * To reenter movie mode after a flash event the part + * must be cycled off and back on to reset the movie + * mode and reprogrammed via the AS2Cwire. Therefore + * the brightness value needs to be updated here to + * reflect the actual state. + */ + led_cdev->brightness = 0; + led->movie_mode = false; + + gpio_set_value(led->flen_gpio, 1); + +unlock: + mutex_unlock(&led->lock); + + return 0; +} + +static int aat1290_led_flash_timeout_set(struct led_classdev_flash *flash, + u32 timeout) +{ + /* + * Don't do anything - flash timeout is cached in the led-class-flash + * core and will be applied in the strobe_set op, as writing the + * safety timer register spuriously turns the torch mode on. + */ + + return 0; +} + +static int aat1290_led_parse_dt(struct aat1290_led *led, + struct device *dev) +{ + int ret; + char *pname = "label"; + + ret = of_property_read_string(dev->of_node, pname, + (const char **) &led->label); + if (ret < 0) { + dev_err(dev, "Error reading %s Device Tree property (%d)\n", + pname, ret); + return ret; + } + + pname = "flash-timeout-microsec"; + + ret = of_property_read_u32(dev->of_node, pname, &led->max_flash_tm); + if (ret) { + dev_err(dev, "Error reading %s Device Tree property (%d)\n", + pname, ret); + return ret; + } + + return ret; +} + +static void aat1290_init_flash_settings(struct aat1290_led *led, + struct aat1290_led_settings *s) +{ + struct led_flash_setting *setting; + + /* Init flash intensity setting */ + setting = &s->torch_brightness; + /* + * Torch current is adjustable in logarithmic fashion and thus + * it is not possible to define fixed step in microamperes. + * Instead led brightness levels are used to make possible + * setting all the supported levels from V4L2 Flash sub-device. + */ + setting->min = 1; + setting->max = AAT1290_MAX_MM_CURR_PERCENT_0 - + AAT1290_MAX_MM_CURR_PERCENT_100; + setting->step = 1; + setting->val = setting->max; + + /* Init flash timeout setting */ + setting = &s->flash_timeout; + setting->min = led->max_flash_tm / AAT1290_FLASH_TM_NUM_LEVELS; + setting->max = setting->min * AAT1290_FLASH_TM_NUM_LEVELS; + setting->step = setting->min; + setting->val = setting->max; +} + +#if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) +static void aat1290_init_v4l2_ctrl_config(struct aat1290_led_settings *s, + struct v4l2_flash_ctrl_config *config) +{ + struct led_flash_setting *setting; + struct v4l2_ctrl_config *c; + + c = &config->torch_intensity; + setting = &s->torch_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; + + config->has_external_strobe = false; +} +#else +#define aat1290_init_v4l2_ctrl_config(s, config) +#endif + +static const struct led_flash_ops flash_ops = { + .strobe_set = aat1290_led_flash_strobe_set, + .timeout_set = aat1290_led_flash_timeout_set, +}; + +#if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) +static const struct v4l2_flash_ops v4l2_flash_ops = { + .external_strobe_set = NULL, +}; + +static const struct v4l2_flash_ops *get_v4l2_flash_ops(void) +{ + return &v4l2_flash_ops; +} +#else +#define get_v4l2_flash_ops() (NULL) +#endif + +static int aat1290_led_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *dev_node = pdev->dev.of_node; + struct aat1290_led *led; + struct led_classdev *led_cdev; + struct led_classdev_flash *flash; +#if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) + struct v4l2_flash_ctrl_config v4l2_flash_config; +#endif + struct aat1290_led_settings settings; + int flen_gpio, enset_gpio, ret; + + led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); + if (!led) + return -ENOMEM; + + led->pdev = pdev; + platform_set_drvdata(pdev, led); + + if (!dev_node) + return -ENXIO; + + flen_gpio = of_get_gpio(dev_node, 0); + if (gpio_is_valid(flen_gpio)) { + ret = gpio_request_one(flen_gpio, GPIOF_DIR_OUT, + "aat1290_flen"); + if (ret < 0) { + dev_err(dev, + "failed to request GPIO %d, error %d\n", + flen_gpio, ret); + goto error_gpio_flen; + } + } + led->flen_gpio = flen_gpio; + + enset_gpio = of_get_gpio(dev_node, 1); + if (gpio_is_valid(enset_gpio)) { + ret = gpio_request_one(enset_gpio, GPIOF_DIR_OUT, + "aat1290_en_set"); + if (ret < 0) { + dev_err(dev, + "failed to request GPIO %d, error %d\n", + enset_gpio, ret); + goto error_gpio_en_set; + } + } + led->en_set_gpio = enset_gpio; + + ret = aat1290_led_parse_dt(led, &pdev->dev); + if (ret < 0) + goto error_gpio_en_set; + + mutex_init(&led->lock); + + flash = &led->ldev; + + /* Init flash settings */ + aat1290_init_flash_settings(led, &settings); + + flash->timeout = settings.flash_timeout; + + /* Init V4L2 Flash controls basing on initialized settings */ + aat1290_init_v4l2_ctrl_config(&settings, &v4l2_flash_config); + + /* Init led class */ + led_cdev = &flash->led_cdev; + led_cdev->name = led->label; + led_cdev->brightness_set = aat1290_led_brightness_set; + led_cdev->brightness_set_sync = aat1290_torch_brightness_set; + led_cdev->max_brightness = settings.torch_brightness.max; + led_cdev->flags |= LED_DEV_CAP_FLASH; + + INIT_WORK(&led->work_brightness_set, aat1290_brightness_set_work); + + flash->ops = &flash_ops; + + /* Register in the LED subsystem. */ + ret = led_classdev_flash_register(&pdev->dev, flash); + if (ret < 0) + goto error_gpio_en_set; + + /* Create V4L2 Flash subdev. */ + led->v4l2_flash = v4l2_flash_init(flash, get_v4l2_flash_ops(), + &v4l2_flash_config); + if (IS_ERR(led->v4l2_flash)) { + ret = PTR_ERR(led->v4l2_flash); + goto error_v4l2_flash_init; + } + + return ret; + +error_v4l2_flash_init: + led_classdev_flash_unregister(flash); +error_gpio_en_set: + if (gpio_is_valid(enset_gpio)) + gpio_free(enset_gpio); +error_gpio_flen: + if (gpio_is_valid(flen_gpio)) + gpio_free(flen_gpio); + mutex_destroy(&led->lock); + + return ret; +} + +static int aat1290_led_remove(struct platform_device *pdev) +{ + struct aat1290_led *led = platform_get_drvdata(pdev); + + v4l2_flash_release(led->v4l2_flash); + led_classdev_flash_unregister(&led->ldev); + cancel_work_sync(&led->work_brightness_set); + + if (gpio_is_valid(led->en_set_gpio)) + gpio_free(led->en_set_gpio); + if (gpio_is_valid(led->flen_gpio)) + gpio_free(led->flen_gpio); + + mutex_destroy(&led->lock); + + return 0; +} + +static struct of_device_id aat1290_led_dt_match[] = { + {.compatible = "skyworks,aat1290"}, + {}, +}; + +static struct platform_driver aat1290_led_driver = { + .probe = aat1290_led_probe, + .remove = aat1290_led_remove, + .driver = { + .name = "aat1290-led", + .owner = THIS_MODULE, + .of_match_table = aat1290_led_dt_match, + }, +}; + +module_platform_driver(aat1290_led_driver); + +MODULE_AUTHOR("Jacek Anaszewski "); +MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs"); +MODULE_LICENSE("GPL");