From patchwork Mon Aug 17 22:06:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Cvek X-Patchwork-Id: 7027681 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 441B6C05AC for ; Mon, 17 Aug 2015 22:05:34 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 24B67205EC for ; Mon, 17 Aug 2015 22:05:33 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E03A4205E5 for ; Mon, 17 Aug 2015 22:05:31 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZRSVW-00064q-MZ; Mon, 17 Aug 2015 22:03:54 +0000 Received: from bubo.tul.cz ([2001:718:1c01:16::aa]) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZRSVS-0005ds-Cb for linux-arm-kernel@lists.infradead.org; Mon, 17 Aug 2015 22:03:51 +0000 Received: from [78.80.185.17] (78-80-185-17.tmcz.cz [78.80.185.17]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by bubo.tul.cz (Postfix) with ESMTPSA id B418A1F0002; Tue, 18 Aug 2015 00:03:05 +0200 (CEST) From: Petr Cvek Subject: [PATCH v2 19/21] leds: leds-pasic3: Add support for PASIC3 LED controller To: robert.jarzmik@free.fr, philipp.zabel@gmail.com, daniel@zonque.org, haojian.zhuang@gmail.com, sameo@linux.intel.com, lee.jones@linaro.org, cooloney@gmail.com, rpurdie@rpsys.net, j.anaszewski@samsung.com, linux@arm.linux.org.uk, sre@kernel.org, dbaryshkov@gmail.com, dwmw2@infradead.org References: Message-ID: <55D25ADC.10703@tul.cz> Date: Tue, 18 Aug 2015 00:06:20 +0200 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150817_150350_991559_EDFEFDAE X-CRM114-Status: GOOD ( 21.95 ) X-Spam-Score: -2.5 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org, linux-leds@vger.kernel.org, linux-pm@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 Add support for LEDs inside the PASIC3 chip. Signed-off-by: Petr Cvek --- drivers/leds/Kconfig | 9 ++ drivers/leds/Makefile | 1 + drivers/leds/leds-pasic3.c | 232 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 drivers/leds/leds-pasic3.c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 9ad35f7..516ba65 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -586,6 +586,15 @@ config LEDS_PM8941_WLED This option enables support for the 'White' LED block on Qualcomm PM8941 PMICs. +config LEDS_PASIC3 + tristate "LED support for the HTC Magician/Alpine PASIC3" + depends on LEDS_CLASS + depends on HTC_PASIC3 + select REGMAP + help + This option enables support for the PASIC3 chip (different chip + than Compaq ASIC3). + comment "LED Triggers" source "drivers/leds/trigger/Kconfig" diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index 8d6a24a..b1c659c 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -65,6 +65,7 @@ obj-$(CONFIG_LEDS_VERSATILE) += leds-versatile.o obj-$(CONFIG_LEDS_MENF21BMC) += leds-menf21bmc.o obj-$(CONFIG_LEDS_PM8941_WLED) += leds-pm8941-wled.o obj-$(CONFIG_LEDS_KTD2692) += leds-ktd2692.o +obj-$(CONFIG_LEDS_PASIC3) += leds-pasic3.o # LED SPI Drivers obj-$(CONFIG_LEDS_DAC124S085) += leds-dac124s085.o diff --git a/drivers/leds/leds-pasic3.c b/drivers/leds/leds-pasic3.c new file mode 100644 index 0000000..0f95230 --- /dev/null +++ b/drivers/leds/leds-pasic3.c @@ -0,0 +1,232 @@ +/* + * AIC3 (or PASIC3) LED driver for HTC Magician/Alpine/.. (not Compaq ASIC3) + * + * Copyright (c) 2015 Petr Cvek + * + * CLK to MS calculation based on leds-asic3.c + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + */ + +#include +#include +#include +#include +#include +#include +#include + +/* + * 1 tick is around 62-63 ms, blinking settings (on+off): + * Min: 1+1 ticks ~125ms + * Max: 127+127 ticks ~15?875ms + * HW sometimes takes time to change (counter overflow after write?) + */ + +#define MS_TO_CLK(ms) DIV_ROUND_CLOSEST(((ms)*1024), 64000) +#define CLK_TO_MS(clk) (((clk)*64000)/1024) +#define MAX_CLK 254 /* 127 + 127 (2x 7 bit reg) */ +#define MAX_MS CLK_TO_MS(MAX_CLK) + +spinlock_t lock; + +static void brightness_set(struct led_classdev *cdev, + enum led_brightness value) +{ + struct platform_device *pdev = to_platform_device(cdev->dev->parent); + struct pasic3_led *led = dev_get_platdata(cdev->dev); + struct pasic3_leds_pdata *pdata = dev_get_platdata(&pdev->dev); + unsigned long flags; + u8 val; + + spin_lock_irqsave(&lock, flags); + + if (value == LED_OFF) { + val = pasic3_read_register(pdata->mfd_dev, PASIC3_CH_CONTROL); + pasic3_write_register(pdata->mfd_dev, PASIC3_CH_CONTROL, + val & ~(led->bit_blink_en | led->bit_force_on)); + + val = pasic3_read_register(pdata->mfd_dev, PASIC3_MASK_A); + pasic3_write_register(pdata->mfd_dev, PASIC3_MASK_A, + val & ~led->bit_mask); + + if (pdata->power_gpio) { + pdata->power_gpio_users--; + + if (pdata->power_gpio_users == 0) + gpio_set_value(pdata->power_gpio, 0); + } + } else { + if (pdata->power_gpio) { + pdata->power_gpio_users++; + + if (pdata->power_gpio_users != 0) + gpio_set_value(pdata->power_gpio, 1); + } + + val = pasic3_read_register(pdata->mfd_dev, PASIC3_CH_CONTROL); + pasic3_write_register(pdata->mfd_dev, PASIC3_CH_CONTROL, + val | led->bit_force_on); + } + + spin_unlock_irqrestore(&lock, flags); +} + +static int blink_set(struct led_classdev *cdev, + unsigned long *delay_on, unsigned long *delay_off) +{ + struct platform_device *pdev = to_platform_device(cdev->dev->parent); + struct pasic3_led *led = dev_get_platdata(cdev->dev); + struct pasic3_leds_pdata *pdata = dev_get_platdata(&pdev->dev); + unsigned long flags; + u32 on, off; + u8 val; + + if (*delay_on > MAX_MS || *delay_off > MAX_MS) + return -EINVAL; + + if (*delay_on == 0 && *delay_off == 0) { + /* Choose sensible default delays */ + on = MS_TO_CLK(500); + off = MS_TO_CLK(500); + } else { + on = MS_TO_CLK(*delay_on); + off = MS_TO_CLK(*delay_off); + if ((on + off) > MAX_CLK) + return -EINVAL; + /* Minimal on/off value must be 1 */ + on = on ? on : 1; + off = off ? off : 1; + } + + spin_lock_irqsave(&lock, flags); + + pasic3_write_register(pdata->mfd_dev, led->reg_delay_on, on); + pasic3_write_register(pdata->mfd_dev, led->reg_delay_off, off); + + val = pasic3_read_register(pdata->mfd_dev, PASIC3_CH_CONTROL); + pasic3_write_register(pdata->mfd_dev, PASIC3_CH_CONTROL, + val | led->bit_blink_en); + + spin_unlock_irqrestore(&lock, flags); + + *delay_on = CLK_TO_MS(on); + *delay_off = CLK_TO_MS(off); + + return 0; +} + +static int pasic3_led_probe(struct platform_device *pdev) +{ + struct pasic3_leds_pdata *pdata = dev_get_platdata(&pdev->dev); + struct led_classdev *cdev; + int ret; + int i; + + if (!pdata) { + dev_err(&pdev->dev, "no platform data\n"); + return -EINVAL; + } + + if (!pdata->leds) { + dev_err(&pdev->dev, "no leds data\n"); + return -EINVAL; + } + + if (pdata->power_gpio) { + pdata->power_gpio_users = 0; + + ret = gpio_request(pdata->power_gpio, + "Red-Blue LED Power"); + if (ret < 0) { + dev_warn(&pdev->dev, "failed to request power GPIO\n"); + goto err; + } + } + + spin_lock_init(&lock); + + cdev = devm_kzalloc(&pdev->dev, + sizeof(struct led_classdev) * pdata->num_leds, GFP_KERNEL); + if (!cdev) { + ret = -ENOMEM; + goto err; + } + + for (i = 0; i < pdata->num_leds; i++) { + cdev[i].name = pdata->leds[i].name; + cdev[i].default_trigger = pdata->leds[i].default_trigger; + cdev[i].flags = LED_CORE_SUSPENDRESUME; + cdev[i].brightness_set = brightness_set; + cdev[i].blink_set = blink_set; + + ret = devm_led_classdev_register(&pdev->dev, &cdev[i]); + if (ret < 0) + goto err; + + cdev[i].dev->platform_data = &pdata->leds[i]; + } + + return 0; + +err: + if (pdata->power_gpio) + gpio_free(pdata->power_gpio); + + return ret; +} + +static int pasic3_led_remove(struct platform_device *pdev) +{ + struct pasic3_leds_pdata *pdata = dev_get_platdata(&pdev->dev); + + if (pdata->power_gpio) + gpio_free(pdata->power_gpio); + + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int pasic3_led_suspend(struct device *dev) +{ + struct pasic3_leds_pdata *pdata = dev_get_platdata(dev); + + if (pdata->power_gpio) + gpio_set_value(pdata->power_gpio, 0); + + return 0; +} + +static int pasic3_led_resume(struct device *dev) +{ + struct pasic3_leds_pdata *pdata = dev_get_platdata(dev); + + if (pdata->power_gpio) + gpio_set_value(pdata->power_gpio, 1); + + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(pasic3_led_pm_ops, pasic3_led_suspend, + pasic3_led_resume); + +static struct platform_driver pasic3_led_driver = { + .probe = pasic3_led_probe, + .remove = pasic3_led_remove, + .driver = { + .name = "leds-pasic3", + .pm = &pasic3_led_pm_ops, + }, +}; + +module_platform_driver(pasic3_led_driver); + +MODULE_AUTHOR("Petr Cvek "); +MODULE_DESCRIPTION("HTC Magician PASIC3 LED driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:leds-pasic3");