From patchwork Fri Oct 7 15:21:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pantelis Antoniou X-Patchwork-Id: 9366285 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AB5D66075E for ; Fri, 7 Oct 2016 15:24:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C688296CD for ; Fri, 7 Oct 2016 15:24:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8FCB6296D8; Fri, 7 Oct 2016 15:24:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D57C9296CD for ; Fri, 7 Oct 2016 15:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756995AbcJGPYZ (ORCPT ); Fri, 7 Oct 2016 11:24:25 -0400 Received: from mail-wm0-f51.google.com ([74.125.82.51]:37735 "EHLO mail-wm0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S938889AbcJGPXn (ORCPT ); Fri, 7 Oct 2016 11:23:43 -0400 Received: by mail-wm0-f51.google.com with SMTP id b201so46108783wmb.0 for ; Fri, 07 Oct 2016 08:23:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7eO2vQpXdoGuhhLGQn8D/20+qsFWuwP0CsJjnuacC0w=; b=iAXP4Sg+hAOAwaQczrS/wADZWlinmMslUQFGh7R8hBUjriYURqMEhZuy5WWAztSHVx J1E3t7TtTMMkEKhC81LUalCeWsSpPD/duTb0858iuD6rQkQ5xTrVfzyWvUwMgzcdG84H zQ5klxsP4zakeez2SVUbY0enCitYaqPZvTz3w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7eO2vQpXdoGuhhLGQn8D/20+qsFWuwP0CsJjnuacC0w=; b=kiGu47BFygBedKMASED+e3sSxkFI9owmeNy7P6rQGFYabAoZvKB6Zit7l6yxapJl0p aouRjfcssCiP7ytuzxOJigfgs86NapHGXmkj4YLQt7TOFXPFLl1zbbO7IYkcR7vXs+xR FdnSYxlTLs3TfvNUrKUPDtNCtYrIVBlbUzaIjQelZTJMpY2LtbOb05ErGsYllr4XTxyD iVdR9/+kY7nqzWUU9NSQ7VesN4xA9sw4aCl+5oorGnzR8F7XeGzzUK0s08KLNpbKbhjt xfHF4RBdo3hiZXnUd+a/Xe1VTaou4H1f9QJGsU8cA4+X/JDQnKaGkoYnCOn1r4RwbQ7m EZAA== X-Gm-Message-State: AA6/9Rm1PG2suexVSL6gOtCy84Y+qs4du9RoCp+2YYBsMv+ui60aGno5WjVlfHDvlKzQ3g== X-Received: by 10.194.117.10 with SMTP id ka10mr44412wjb.148.1475853821499; Fri, 07 Oct 2016 08:23:41 -0700 (PDT) Received: from localhost.localdomain ([195.97.110.117]) by smtp.gmail.com with ESMTPSA id n5sm20048269wjv.35.2016.10.07.08.23.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 07 Oct 2016 08:23:40 -0700 (PDT) From: Pantelis Antoniou To: Lee Jones Cc: Linus Walleij , Alexandre Courbot , Rob Herring , Mark Rutland , Frank Rowand , Wolfram Sang , Richard Purdie , Jacek Anaszewski , Jean Delvare , Peter Rosin , Avirup Banerjee , Georgi Vlaev , Guenter Roeck , JawaharBalaji Thirumalaisamy , Pantelis Antoniou , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, linux-i2c@vger.kernel.org, linux-leds@vger.kernel.org, linux-hwmon@vger.kernel.org Subject: [PATCH 07/10] leds: i2cs: Add I2CS FPGA leds driver Date: Fri, 7 Oct 2016 18:21:06 +0300 Message-Id: <1475853669-22480-8-git-send-email-pantelis.antoniou@konsulko.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1475853669-22480-1-git-send-email-pantelis.antoniou@konsulko.com> References: <1475853669-22480-1-git-send-email-pantelis.antoniou@konsulko.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Georgi Vlaev Add support for the FRU faceplate status LEDs (OK, FAIL, ACTIVE, STANDBY) controlled by the Juniper I2CS FPGA. This driver is a jnx-i2cs-core client. Signed-off-by: Georgi Vlaev [Ported from Juniper kernel] Signed-off-by: Pantelis Antoniou --- drivers/leds/Kconfig | 9 ++ drivers/leds/Makefile | 1 + drivers/leds/leds-jnx-i2cs.c | 219 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 drivers/leds/leds-jnx-i2cs.c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 7a628c6..45c6612 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -659,6 +659,15 @@ config LEDS_MLXCPLD This option enabled support for the LEDs on the Mellanox boards. Say Y to enabled these. +config LEDS_JNX_I2CS + tristate "LED support for the Juniper Networks I2CS FPGA" + depends on LEDS_CLASS && I2C + select REGMAP_I2C + help + This option enables support for the FRU faceplate status + LEDs (OK, FAIL, ACTIVE, STANDBY) controlled by the Juniper + I2CS FPGA. + comment "LED Triggers" source "drivers/leds/trigger/Kconfig" diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index 3965070..1ce2d0b 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -71,6 +71,7 @@ obj-$(CONFIG_LEDS_IS31FL319X) += leds-is31fl319x.o obj-$(CONFIG_LEDS_IS31FL32XX) += leds-is31fl32xx.o obj-$(CONFIG_LEDS_PM8058) += leds-pm8058.o obj-$(CONFIG_LEDS_MLXCPLD) += leds-mlxcpld.o +obj-$(CONFIG_LEDS_JNX_I2CS) += leds-jnx-i2cs.o # LED SPI Drivers obj-$(CONFIG_LEDS_DAC124S085) += leds-dac124s085.o diff --git a/drivers/leds/leds-jnx-i2cs.c b/drivers/leds/leds-jnx-i2cs.c new file mode 100644 index 0000000..c2d7274 --- /dev/null +++ b/drivers/leds/leds-jnx-i2cs.c @@ -0,0 +1,219 @@ +/* + * Juniper Networks I2CS FPGA LEDs driver + * + * Copyright (C) 2016 Juniper Networks + * Author: Georgi Vlaev + * + * 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 +#include + +#define FRU_LEDS 4 /* Total LEDs (active, fail, ok, standby) */ +#define HW_BLINK_LEDS 3 /* LEDs with hw blink cap (standby not supported) */ + +/* + * I2CS fru_led [0x12] + * + * bit 6 | bit 5 | bit 4 |... | bit 0 + * blink_ok|blink_fail|blink_act|led_standby|led_ok|led_fail|led_act + */ + +/* TODO: Use the regmap from the parent MFD */ +static struct regmap_config i2cs_leds_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = I2CS_SPARE_OE, +}; + +struct i2cs_led { + struct led_classdev lc; + struct regmap *regmap; + struct work_struct work; + int blink; + int on; + int bit; +}; + +struct i2cs_led_data { + int num_leds; + struct i2cs_led *leds; +}; + +static void jnx_i2cs_leds_work(struct work_struct *work) +{ + struct i2cs_led *led = container_of(work, struct i2cs_led, work); + + int mask = (BIT(led->bit) << 4) | BIT(led->bit); + int value = ((led->blink << led->bit) << 4) | (led->on << led->bit); + + regmap_update_bits(led->regmap, I2CS_FRU_LED, mask, value & 0x7f); +} + +static void jnx_i2cs_leds_brightness_set(struct led_classdev *lc, + enum led_brightness brightness) +{ + struct i2cs_led *led = container_of(lc, struct i2cs_led, lc); + + led->on = (brightness != LED_OFF); + led->blink = 0; /* always turn off hw blink on brightness_set() */ + schedule_work(&led->work); +} + +static int jnx_i2cs_leds_blink_set(struct led_classdev *lc, + unsigned long *delay_on, + unsigned long *delay_off) +{ + struct i2cs_led *led = container_of(lc, struct i2cs_led, lc); + + led->blink = (*delay_on > 0); + led->on = led->blink; /* 'on' bit should be set if blinking */ + schedule_work(&led->work); + + return 0; +} + +static int jnx_i2cs_leds_init_one(struct device *dev, struct device_node *np, + struct i2cs_led_data *ild, + struct regmap *regmap, int num) +{ + struct i2cs_led *led; + const char *string; + bool hw_blink; + int ret; + u32 reg; + + ret = of_property_read_u32(np, "reg", ®); + if (ret || reg >= FRU_LEDS) + return -ENODEV; + + led = &ild->leds[num]; + led->bit = reg; + led->regmap = regmap; + + if (!of_property_read_string(np, "label", &string)) + led->lc.name = string; + else + led->lc.name = np->name; + + if (!of_property_read_string(np, "linux,default-trigger", &string)) + led->lc.default_trigger = string; + + led->lc.brightness = LED_OFF; + led->lc.brightness_set = jnx_i2cs_leds_brightness_set; + if (led->bit <= HW_BLINK_LEDS) { + hw_blink = of_property_read_bool(np, "hw-blink"); + if (hw_blink) + led->lc.blink_set = jnx_i2cs_leds_blink_set; + } + + ret = devm_led_classdev_register(dev, &led->lc); + if (ret) + return ret; + + INIT_WORK(&led->work, jnx_i2cs_leds_work); + + return 0; +} + +static int jnx_i2cs_leds_of_init(struct device *dev, struct i2cs_led_data *ild) +{ + struct device_node *child, *np = dev->of_node; + struct regmap *regmap; + struct i2c_client *client; + int ret, num_leds, i = 0; + + if (!dev->parent) + return -ENODEV; + + client = i2c_verify_client(dev->parent); + if (!client) + return -ENODEV; + + regmap = devm_regmap_init_i2c(client, &i2cs_leds_regmap_config); + if (IS_ERR(regmap)) { + dev_err(dev, "Failed to allocate register map\n"); + return PTR_ERR(regmap); + } + + num_leds = of_get_child_count(np); + if (!num_leds || num_leds > FRU_LEDS) + return -ENODEV; + + ild->num_leds = num_leds; + ild->leds = devm_kzalloc(dev, sizeof(struct i2cs_led) * num_leds, + GFP_KERNEL); + if (!ild->leds) + return -ENOMEM; + + for_each_child_of_node(np, child) { + ret = jnx_i2cs_leds_init_one(dev, child, ild, regmap, i++); + if (ret) + return ret; + } + + return 0; +} + +static int jnx_i2cs_leds_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct i2cs_led_data *ild; + int ret; + + ild = devm_kzalloc(dev, sizeof(*ild), GFP_KERNEL); + if (!ild) + return -ENOMEM; + + ret = jnx_i2cs_leds_of_init(dev, ild); + if (ret < 0) + return ret; + + platform_set_drvdata(pdev, ild); + + return 0; +} + +static int jnx_i2cs_leds_remove(struct platform_device *pdev) +{ + struct i2cs_led_data *ild = platform_get_drvdata(pdev); + int i; + + for (i = 0; i < ild->num_leds; i++) { + devm_led_classdev_unregister(&pdev->dev, &ild->leds[i].lc); + cancel_work_sync(&ild->leds[i].work); + } + + return 0; +} + +static const struct of_device_id jnx_i2cs_leds_match[] = { + { .compatible = "jnx,leds-i2cs", }, + { }, +}; +MODULE_DEVICE_TABLE(of, jnx_i2cs_leds_match); + +static struct platform_driver jnx_i2cs_leds_driver = { + .driver = { + .name = "leds-i2cs", + .of_match_table = jnx_i2cs_leds_match, + }, + .probe = jnx_i2cs_leds_probe, + .remove = jnx_i2cs_leds_remove, +}; + +module_platform_driver(jnx_i2cs_leds_driver); + +MODULE_DESCRIPTION("Juniper Networks I2CS leds driver"); +MODULE_AUTHOR("Georgi Vlaev "); +MODULE_LICENSE("GPL");