/*
 * 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 <linux/string.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)
{
	int len;
	struct sk9822_leds *sk9822 = dev_get_drvdata(dev);

	len = scnprintf(buf, PAGE_SIZE, "%02x%02x%02x\n", sk9822->led_colors[0].r, sk9822->led_colors[0].g, sk9822->led_colors[0].b);
	if (len <= 0) {
		dev_err(dev, "sk9822: Invalid sprintf len: %d\n", len);
		return -EIO;
	}

	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);

	/* strip newline */
	if ((count > 0) && (buf[count-1] == '\n')) {
		buflen--;
	}

	if (buflen != 6) { // RRGGBB\0
		return -EMSGSIZE;
	}

	// Update the LED array here
	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 ssize_t get_led_brightness(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	int len;
	struct sk9822_leds *sk9822 = dev_get_drvdata(dev);

	len = scnprintf(buf, PAGE_SIZE, "%x\n", sk9822->led_brightness);
	if (len <= 0) {
		dev_err(dev, "sk9822: Invalid sprintf len: %d\n", len);
		return -EIO;
	}

	return len;
}

/**
 * @brief Set LED brightness
 * @retval count number of bytes written
 * @retval -EINVAL if the message is not a valid hexadecimal number
 * @retval -EIO for all other errors (e.g. leds cannot be configured)
 */
static ssize_t set_led_brightness(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	int ret = 0;
	struct sk9822_leds *sk9822 = dev_get_drvdata(dev);
	unsigned long val = SK9822_DEFAULT_BRIGHTNESS;

	if (kstrtoul(buf, 16, &val)) {
		return -EINVAL;
	}

	if(val > SK9822_DEFAULT_BRIGHTNESS) {
		val = SK9822_DEFAULT_BRIGHTNESS;
	}

	sk9822->led_brightness = (uint8_t)val;

	// 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_brightness, S_IRUGO | S_IWUSR, get_led_brightness, set_led_brightness);


static struct attribute *sk9822_dev_attrs[] = {
	 &dev_attr_led_color.attr,
	 &dev_attr_led_brightness.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;

	leds = devm_kzalloc(&pdev->dev, sizeof(*leds), GFP_KERNEL);
	if (!leds) {
		return -ENOMEM;
	}
	leds->dev = &pdev->dev;
	leds->led_brightness = SK9822_DEFAULT_BRIGHTNESS;

	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;
	} else {
		printk(KERN_INFO "Got led count: %u\n", leds->led_count);
	}

	leds->led_colors = devm_kzalloc(&pdev->dev,
			(sizeof(cRGB) * leds->led_count), GFP_KERNEL);
	if (!leds->led_colors) {
		return -ENOMEM;
	}


	platform_set_drvdata(pdev, leds);

	leds->clock_gpio = devm_gpiod_get_index(&pdev->dev, "led", 0, GPIOD_OUT_HIGH);
	if (IS_ERR(leds->clock_gpio)) {
		dev_err(&pdev->dev, "Failed to acquire clock GPIO %ld\n",
				PTR_ERR(leds->clock_gpio));
		return PTR_ERR(leds->clock_gpio);
	}

	ret = gpiod_direction_output(leds->clock_gpio, 1);
	if (ret) {
		dev_err(&pdev->dev, "Failed to set clock GPIO output %d\n", ret);
		return ret;
	} else {
		printk(KERN_INFO "Got clock gpio\n");
		gpiod_set_value(leds->clock_gpio, 0);
	}

	leds->data_gpio = devm_gpiod_get_index(&pdev->dev, "led", 1, GPIOD_OUT_HIGH);
	if (IS_ERR(leds->data_gpio)) {
		dev_err(&pdev->dev, "Failed to acquire data GPIO %ld\n",
				PTR_ERR(leds->data_gpio));
		return PTR_ERR(leds->data_gpio);
	}

	ret = gpiod_direction_output(leds->data_gpio, 1);
	if (ret) {
		dev_err(&pdev->dev, "Failed to set data GPIO output %d\n", ret);
		return ret;
	} 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 "canyon led successfully probed\n");

	return 0;
}

static void canyon_led_off(struct sk9822_leds *leds)
{
	leds->led_brightness = 0;
	memset(leds->led_colors, 0, sizeof(cRGB) * leds->led_count);
	sk9822_update(leds);
}

static int canyon_led_remove(struct platform_device *pdev)
{
	sysfs_remove_group(&pdev->dev.kobj, &sk9822_dev_attr_group);
	canyon_led_off(platform_get_drvdata(pdev));

	return 0;
}

static void canyon_led_shutdown(struct platform_device *pdev)
{
	canyon_led_off(platform_get_drvdata(pdev));
}

/**
 * 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,
	.shutdown = &canyon_led_shutdown,
	.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");
