gryphon-led-module: added driver for RGB led

This commit is contained in:
Oussama Ghorbel 2019-04-03 17:18:39 +02:00
parent 6a483badac
commit b7b48d7815
7 changed files with 551 additions and 0 deletions

View file

@ -0,0 +1,62 @@
# Copyright (c) 2017 Genexis B.V.
#
# 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.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=gryphon-led-kernel-module
PKG_RELEASE:=1
include $(INCLUDE_DIR)/package.mk
define KernelPackage/$(PKG_NAME)
SUBMENU:=Genexis
TITLE:=LED driver for Gryphon
FILES:=$(PKG_BUILD_DIR)/$(PKG_NAME).$(LINUX_KMOD_SUFFIX)
KCONFIG:=CONFIG_PACKAGE_kmod-gryphon-led-kernel-module=y
AUTOLOAD:=$(call AutoLoad,60,$(PKG_NAME))
PKG_LICENSE:=Genexis
PKG_LICENSE_URL:=
endef
define KernelPackage/$(PKG_NAME)/description
This package contains the LED driver for Gryphon devices.
endef
EXTRA_KCONFIG:= CONFIG_RGB_LED=m
MODULE_INCLUDE=-I$(PKG_BUILD_DIR)
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)/kdevlinks/
$(CP) -s `pwd`/src/* $(PKG_BUILD_DIR)/kdevlinks/
$(CP) src/* $(PKG_BUILD_DIR)
endef
define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" \
CROSS_COMPILE="$(TARGET_CROSS)" \
ARCH="$(LINUX_KARCH)" \
SUBDIRS="$(PKG_BUILD_DIR)" \
EXTRA_CFLAGS="-DKERNEL_MODULE $(BUILDFLAGS) -I$(LINUX_DIR)/include -include generated/autoconf.h $(MODULE_INCLUDE)" \
modules
endef
define Build/InstallDev
$(INSTALL_DIR) $(1)/usr/include
endef
$(eval $(call KernelPackage,$(PKG_NAME)))

View file

@ -0,0 +1,26 @@
# Copyright (c) 2017 Genexis B.V.
#
# 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.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
MODULE_NAME = gryphon-led-kernel-module
obj-m := $(MODULE_NAME).o
GENEXIS_FILES= \
main.o \
sk9822.o \
sk9822_bitbang.o
$(MODULE_NAME)-objs := $(GENEXIS_FILES)

View file

@ -0,0 +1,255 @@
/*
* Copyright (c) 2017 Genexis B.V.
*
* 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.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#define DEBUG 1
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/printk.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>
#include <linux/version.h>
#include "sk9822.h"
#define DRIVER_NAME "canyon_led"
#define DRIVER_AUTHOR "Genexis B.V."
#define DRIVER_DESC "Canyon LED driver for SK9822"
#define DRIVER_VERSION "1"
/**
* sysfs interfaces
*/
static ssize_t get_led_color(struct device *dev,
struct device_attribute *attr, char *buf)
{
/* [ln] todo: dummy implementation */
int len;
len = sprintf(buf, "%d\n", 123);
if (len <= 0) {
dev_err(dev, "sk9822: Invalid sprintf len: %d\n", len);
}
return len;
}
/**
* @brief Set complete LED strip to a specific color
* @retval count number of bytes written
* @retval -EMSGSIZE if the message is too big
* @retval -EIO for all other errors (e.g. leds cannot be configured)
*/
static ssize_t set_led_color(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int ret = 0;
size_t buflen = count;
struct sk9822_leds *sk9822 = dev_get_drvdata(dev);
if (IS_ERR(sk9822)) {
printk(KERN_ERR "Platform get drvdata returned NULL\n");
return -EIO;
}
/* strip newline */
if ((count > 0) && (buf[count-1] == '\n')) {
buflen--;
}
if (buflen != 6) { // RRGGBB\0
return -EMSGSIZE;
}
// Update the LED array here
printk(KERN_DEBUG "Set LED color\n");
ret = sk9822_set_color_str(sk9822, buf);
if (ret != 0) {
printk(KERN_ERR "Failed to set led color\n");
return -EIO;
}
// Now push to the HW
ret = sk9822_update(sk9822);
if (ret != 0) {
printk(KERN_ERR "Failed to update led\n");
return -EIO;
}
return count;
}
static DEVICE_ATTR(led_color, S_IRUGO | S_IWUSR, get_led_color, set_led_color);
static struct attribute *sk9822_dev_attrs[] = {
&dev_attr_led_color.attr,
NULL
};
static struct attribute_group sk9822_dev_attr_group = {
.name = "sk9822",
.attrs = sk9822_dev_attrs,
};
/**
* device prope and removal
*/
static int canyon_led_probe(struct platform_device *pdev)
{
int ret;
struct sk9822_leds *leds;
printk(KERN_NOTICE "Begin probing...\n");
leds = devm_kzalloc(&pdev->dev, sizeof(*leds), GFP_KERNEL);
if (!leds) {
return -ENOMEM;
}
leds->dev = &pdev->dev;
leds->led_brightness = SK9822_DEFAULT_BRIGHTNESS;
leds->led_colors = devm_kzalloc(&pdev->dev,
(sizeof(cRGB) * leds->led_count), GFP_KERNEL);
if (!leds->led_colors) {
return -ENOMEM;
}
ret = of_property_read_u16(pdev->dev.of_node, "led-count", &leds->led_count);
if (ret < 0) {
dev_warn(&pdev->dev, "Could not read led-count property\n");
leds->led_count = SK9822_DEFAULT_NUM_LEDS;
}
platform_set_drvdata(pdev, leds);
printk(KERN_INFO "Allocated memory\n");
#if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 16, 0)
leds->clock_gpio = gpiod_get_index(&pdev->dev, "led", 0);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
leds->clock_gpio = gpiod_get_index(&pdev->dev, "led", 0, GPIOD_OUT_HIGH);
#else
dev_warn(&pdev->dev, "Kernel version Not supported\n");
exit(1);
#endif
gpiod_direction_output(leds->clock_gpio, 1);
if (IS_ERR(leds->clock_gpio)) {
dev_err(&pdev->dev, "Failed to acquire clock GPIO %ld\n",
PTR_ERR(leds->clock_gpio));
leds->clock_gpio = NULL;
return PTR_ERR(leds->clock_gpio);
} else {
printk(KERN_INFO "Got clock gpio\n");
gpiod_set_value(leds->clock_gpio, 0);
}
#if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 16, 0)
leds->data_gpio = gpiod_get_index(&pdev->dev, "led", 1);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
leds->data_gpio = gpiod_get_index(&pdev->dev, "led", 1, GPIOD_OUT_HIGH);
#else
dev_warn(&pdev->dev, "Kernel version Not supported\n");
exit(1);
#endif
gpiod_direction_output(leds->data_gpio, 1);
if (IS_ERR(leds->data_gpio)) {
dev_err(&pdev->dev, "Failed to acquire data GPIO %ld\n",
PTR_ERR(leds->data_gpio));
leds->data_gpio = NULL;
return PTR_ERR(leds->data_gpio);
} else {
printk(KERN_INFO "Got data gpio\n");
gpiod_set_value(leds->data_gpio, 0);
}
printk(KERN_INFO "Attempt to set filefs stuff\n");
ret = sysfs_create_group(&pdev->dev.kobj, &sk9822_dev_attr_group);
if (ret) {
dev_err(&pdev->dev, "sysfs creation failed\n");
return ret;
}
printk(KERN_INFO "Flash LEDs to verify they work\n");
sk9822_set_color_str(leds, "00FF00");
sk9822_update(leds);
msleep(200);
sk9822_set_color_str(leds, "000000");
sk9822_update(leds);
printk(KERN_NOTICE "We made it, carry on...\n");
return 0;
}
static int canyon_led_remove(struct platform_device *pdev)
{
struct sk9822_leds *leds;
sysfs_remove_group(&pdev->dev.kobj, &sk9822_dev_attr_group);
leds = platform_get_drvdata(pdev);
if (IS_ERR(leds)) {
printk(KERN_ERR "Platform get drvdata returned NULL\n");
return -1;
}
if (leds->clock_gpio) {
gpiod_put(leds->clock_gpio);
}
if (leds->data_gpio) {
gpiod_put(leds->data_gpio);
}
printk(KERN_NOTICE "Bye, bye\n");
return 0;
}
/**
* platform driver metadata
*/
static const struct of_device_id canyon_led_of_ids[] = {
{ .compatible = "canyon,led" },
{ }
};
static struct platform_driver canyon_led = {
.probe = &canyon_led_probe,
.remove = &canyon_led_remove,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = canyon_led_of_ids,
},
};
MODULE_DEVICE_TABLE(of, canyon_led_of_ids);
module_platform_driver(canyon_led);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");

View file

@ -0,0 +1,86 @@
/*
* Copyright (c) 2017 Genexis B.V.
*
* 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.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <linux/kernel.h>
#include <linux/types.h>
#include "sk9822.h"
#include "sk9822_bitbang.h"
cRGB __hexs_to_rgb(const char *hex)
{
cRGB rgb;
int r, g, b;
sscanf(hex, "%02x%02x%02x", &r, &g, &b);
// This needs sanity checking
rgb.r = r;
rgb.g = g;
rgb.b = b;
return rgb;
}
/**
* @brief update the color over the given device struct to the provided HEX color
*/
int sk9822_set_color_str(struct sk9822_leds *sk9822, const char *hex)
{
int i;
cRGB color = __hexs_to_rgb(hex);
for (i = 0; i < sk9822->led_count; i++) {
sk9822->led_colors[i] = color;
}
return 0;
}
/**
* @brief write device struct to the device
*/
int sk9822_update(struct sk9822_leds *sk9822)
{
uint16_t i;
uint8_t *rawarray = (uint8_t *)sk9822->led_colors;
uint16_t led_count = sk9822->led_count;
sk9822_bb_write(sk9822, 0x00); // Start Frame
sk9822_bb_write(sk9822, 0x00);
sk9822_bb_write(sk9822, 0x00);
sk9822_bb_write(sk9822, 0x00);
for (i = 0; i < (led_count*3); i += 3) {
sk9822_bb_write(sk9822, 0xe0+sk9822->led_brightness); // Maximum global brightness
sk9822_bb_write(sk9822, rawarray[i+0]);
sk9822_bb_write(sk9822, rawarray[i+1]);
sk9822_bb_write(sk9822, rawarray[i+2]);
}
// Reset frame - Only needed for SK9822, has no effect on APA102
sk9822_bb_write(sk9822, 0x00);
sk9822_bb_write(sk9822, 0x00);
sk9822_bb_write(sk9822, 0x00);
sk9822_bb_write(sk9822, 0x00);
// End frame: 8+8*(leds >> 4) clock cycles
for (i = 0; i < led_count; i += 16) {
sk9822_bb_write(sk9822, 0xff); // 8 more clock cycles
}
return 0;
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2017 Genexis B.V.
*
* 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.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifndef SK9822_H_
#define SK9822_H_
#include <linux/types.h>
#define SK9822_DEFAULT_NUM_LEDS 32 // U16, used if DT param fails
#define SK9822_DEFAULT_BRIGHTNESS 31 // 5-bit brightness, 0-31
typedef struct {
uint8_t b;
uint8_t g;
uint8_t r;
} cRGB; // BGR (SK9822 Standard)
struct sk9822_leds {
struct device *dev;
struct gpio_desc *clock_gpio;
struct gpio_desc *data_gpio;
cRGB *led_colors;
uint8_t led_brightness;
uint16_t led_count;
};
int sk9822_set_color_str(struct sk9822_leds *sk9822, const char *hex);
int sk9822_update(struct sk9822_leds *sk9822);
#endif /* SK9822_H_ */

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2017 Genexis B.V.
*
* 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.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <linux/gpio/consumer.h>
#include <linux/delay.h>
#include <linux/types.h>
#include "sk9822.h"
/**
* @brief Bitbang write operation CLOCK+DATA
*
* Assumed state before call: CLOCK- Low, DATA- High
*/
void sk9822_bb_write(struct sk9822_leds *sk9822, uint8_t c)
{
uint8_t i;
for (i = 0; i < 8 ; i++) {
if (!(c&0x80)) {
gpiod_set_value(sk9822->data_gpio, 0); // set data low
} else {
gpiod_set_value(sk9822->data_gpio, 1); // set data high
}
gpiod_set_value(sk9822->clock_gpio, 1); // set clock high, data sampled here
c <<= 1;
udelay(1); // stretch clock
gpiod_set_value(sk9822->clock_gpio, 0); // set clock low
}
// State after call: SCK Low, Data high
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2017 Genexis B.V.
*
* 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.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifndef _SK9822_BB_H_
#define _SK9822_BB_H_
#include <linux/types.h>
#include "sk9822.h"
void sk9822_bb_write(struct sk9822_leds *sk9822, uint8_t c);
#endif /* _SK9822_BB_H_ */