Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

@@ -0,0 +1,8 @@
config RT_FLASHLIGHT
bool "Richtek Flashlight Device Class"
help
Say Y to enable Richtek Flashlight Device Class
This Framework is good for
controling flashlight Leds.
Say N to disable Richtek Flahslight Device Class
@@ -0,0 +1,2 @@
obj-$(CONFIG_RT_FLASHLIGHT) += rt-flashlight.o rtfled.o
@@ -0,0 +1,584 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/ctype.h>
#include <linux/err.h>
#include <linux/slab.h>
#include "rt-flashlight.h"
static const char * const flashlight_type_string[] = {
[FLASHLIGHT_TYPE_XENON] = "Xenon",
[FLASHLIGHT_TYPE_LED] = "LED",
[FLASHLIFHT_TYPE_BULB] = "Bulb",
};
static const char * const flashlight_mode_string[] = {
[FLASHLIGHT_MODE_OFF] = "Off",
[FLASHLIGHT_MODE_TORCH] = "Torch",
[FLASHLIGHT_MODE_FLASH] = "Flash",
[FLASHLIGHT_MODE_MIXED] = "Mixed",
[FLASHLIGHT_MODE_DUAL_FLASH] = "Flash",
[FLASHLIGHT_MODE_DUAL_TORCH] = "Torch",
[FLASHLIGHT_MODE_DUAL_OFF] = "Off",
};
static ssize_t name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
return scnprintf(buf, PAGE_SIZE, "%s\n",
flashlight_dev->props.alias_name ?
flashlight_dev->props.alias_name : "anonymous");
}
static ssize_t type_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
int size = ARRAY_SIZE(flashlight_type_string);
unsigned int type = flashlight_dev->props.type;
if (type >= size)
return -EINVAL;
return scnprintf(buf, PAGE_SIZE, "%s\n",
flashlight_type_string[type]);
}
static ssize_t mode_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
int size = ARRAY_SIZE(flashlight_mode_string);
if ((flashlight_dev->props.mode < 0) ||
(flashlight_dev->props.mode >= size))
return -EINVAL;
return scnprintf(buf, PAGE_SIZE, "%s\n",
flashlight_mode_string[flashlight_dev->props.mode]);
}
static ssize_t torch_max_brightness_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
return scnprintf(buf, PAGE_SIZE, "%d\n",
flashlight_dev->props.torch_max_brightness);
}
static ssize_t strobe_max_brightness_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
return scnprintf(buf, PAGE_SIZE, "%d\n",
flashlight_dev->props.strobe_max_brightness);
}
static ssize_t color_temperature_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
return scnprintf(buf, PAGE_SIZE, "%d\n",
flashlight_dev->props.color_temperature);
}
static ssize_t strobe_delay_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
return scnprintf(buf, PAGE_SIZE, "%d\n",
flashlight_dev->props.strobe_delay);
}
static ssize_t strobe_timeout_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int rc;
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
long timeout;
rc = kstrtol(buf, 0, &timeout);
if (rc)
return rc;
rc = flashlight_dev->ops->set_strobe_timeout(flashlight_dev, timeout);
if (rc == 0)
rc = count;
return rc;
}
static ssize_t strobe_timeout_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
return scnprintf(buf, PAGE_SIZE, "%d\n",
flashlight_dev->props.strobe_timeout);
}
static ssize_t torch_brightness_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int rc;
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
long brightness;
rc = kstrtol(buf, 0, &brightness);
if (rc)
return rc;
rc = -ENXIO;
mutex_lock(&flashlight_dev->ops_lock);
if (flashlight_dev->ops &&
flashlight_dev->ops->set_torch_brightness) {
if (brightness > flashlight_dev->props.torch_max_brightness)
rc = -EINVAL;
else {
pr_debug("flashlight: set torch brightness to %ld\n",
brightness);
flashlight_dev->props.torch_brightness = brightness;
flashlight_dev->ops->set_torch_brightness(
flashlight_dev, brightness);
rc = count;
}
}
mutex_unlock(&flashlight_dev->ops_lock);
return rc;
}
static ssize_t torch_brightness_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
return scnprintf(buf, PAGE_SIZE, "%d\n",
flashlight_dev->props.torch_brightness);
}
static ssize_t strobe_brightness_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
int rc;
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
long brightness;
rc = kstrtol(buf, 0, &brightness);
if (rc)
return rc;
rc = -ENXIO;
mutex_lock(&flashlight_dev->ops_lock);
if (flashlight_dev->ops &&
flashlight_dev->ops->set_strobe_brightness) {
if (brightness > flashlight_dev->props.strobe_max_brightness)
rc = -EINVAL;
else {
pr_debug("flashlight: set strobe brightness to %ld\n",
brightness);
flashlight_dev->props.strobe_brightness = brightness;
flashlight_dev->ops->set_strobe_brightness(
flashlight_dev, brightness);
rc = count;
}
}
mutex_unlock(&flashlight_dev->ops_lock);
return rc;
}
static ssize_t strobe_brightness_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
return scnprintf(buf, PAGE_SIZE, "%d\n",
flashlight_dev->props.strobe_brightness);
}
static struct class *flashlight_class;
static int flashlight_suspend(struct device *dev)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
pm_message_t state = {};
if (flashlight_dev->ops)
flashlight_dev->ops->suspend(flashlight_dev, state);
return 0;
}
static int flashlight_resume(struct device *dev)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
if (flashlight_dev->ops)
flashlight_dev->ops->resume(flashlight_dev);
return 0;
}
static void flashlight_device_release(struct device *dev)
{
struct flashlight_device *flashlight_dev = to_flashlight_device(dev);
kfree(flashlight_dev);
}
static DEVICE_ATTR_RO(name);
static DEVICE_ATTR_RO(type);
static DEVICE_ATTR_RO(mode);
static DEVICE_ATTR_RO(torch_max_brightness);
static DEVICE_ATTR_RO(strobe_max_brightness);
static DEVICE_ATTR_RO(color_temperature);
static DEVICE_ATTR_RO(strobe_delay);
static DEVICE_ATTR_RW(strobe_timeout);
static DEVICE_ATTR_RW(torch_brightness);
static DEVICE_ATTR_RW(strobe_brightness);
static struct attribute *flashlight_class_attrs[] = {
&dev_attr_name.attr,
&dev_attr_type.attr,
&dev_attr_mode.attr,
&dev_attr_torch_max_brightness.attr,
&dev_attr_strobe_max_brightness.attr,
&dev_attr_color_temperature.attr,
&dev_attr_strobe_delay.attr,
&dev_attr_strobe_timeout.attr,
&dev_attr_torch_brightness.attr,
&dev_attr_strobe_brightness.attr,
NULL,
};
static const struct attribute_group flashlight_group = {
.attrs = flashlight_class_attrs,
};
static const struct attribute_group *flashlight_groups[] = {
&flashlight_group,
NULL,
};
/**
* flashlight_device_register - create and register a new object of
* flashlight_device class.
* @name: the name of the new object(must be the same as the name of the
* respective framebuffer device).
* @parent: a pointer to the parent device
* @devdata: an optional pointer to be stored for private driver use. The
* methods may retrieve it by using flashlight_get_data(flashlight_dev).
* @ops: the flashlight operations structure.
*
* Creates and registers new flashlight device. Returns either an
* ERR_PTR() or a pointer to the newly allocated device.
*/
struct flashlight_device *flashlight_device_register(const char *name,
struct device *parent, void *devdata,
const struct flashlight_ops *ops,
const struct flashlight_properties *props)
{
struct flashlight_device *flashlight_dev = NULL;
int rc;
pr_debug("%s: name=%s\n", __func__, name);
flashlight_dev = devm_kzalloc(parent, sizeof(*flashlight_dev), GFP_KERNEL);
if (!flashlight_dev)
return ERR_PTR(-ENOMEM);
mutex_init(&flashlight_dev->ops_lock);
device_initialize(&flashlight_dev->dev);
flashlight_dev->dev.class = flashlight_class;
flashlight_dev->dev.parent = parent;
flashlight_dev->dev.release = flashlight_device_release;
dev_set_name(&flashlight_dev->dev, name);
dev_set_drvdata(&flashlight_dev->dev, devdata);
/* Copy properties */
if (props) {
memcpy(&flashlight_dev->props, props,
sizeof(struct flashlight_properties));
}
rc = device_register(&flashlight_dev->dev);
if (rc) {
kfree(flashlight_dev);
return ERR_PTR(rc);
}
flashlight_dev->ops = ops;
return flashlight_dev;
}
EXPORT_SYMBOL(flashlight_device_register);
/**
* flashlight_device_unregister - unregisters a flashlight device object.
* @flashlight_dev: the flashlight device object to be unregistered and freed.
*
* Unregisters a previously registered via flashlight_device_register object.
*/
void flashlight_device_unregister(struct flashlight_device *flashlight_dev)
{
if (!flashlight_dev)
return;
mutex_lock(&flashlight_dev->ops_lock);
flashlight_dev->ops = NULL;
mutex_unlock(&flashlight_dev->ops_lock);
device_unregister(&flashlight_dev->dev);
}
EXPORT_SYMBOL(flashlight_device_unregister);
int flashlight_list_color_temperature(
struct flashlight_device *flashlight_dev,
int selector)
{
if (flashlight_dev->ops &&
flashlight_dev->ops->list_color_temperature) {
return flashlight_dev->ops->list_color_temperature(
flashlight_dev,
selector);
}
return -EINVAL;
}
EXPORT_SYMBOL(flashlight_list_color_temperature);
int flashlight_set_color_temperature(
struct flashlight_device *flashlight_dev,
int minK, int maxK)
{
int selector = 0;
int rc;
if ((flashlight_dev->ops == NULL) ||
(flashlight_dev->ops->set_color_temperature == NULL))
return -EINVAL;
for (selector = 0; ; selector++) {
rc = flashlight_list_color_temperature(flashlight_dev,
selector);
if (rc < 0)
return rc;
if (rc >= minK && rc <= maxK) {
mutex_lock(&flashlight_dev->ops_lock);
rc = flashlight_dev->ops->set_color_temperature(
flashlight_dev, rc);
mutex_unlock(&flashlight_dev->ops_lock);
if (rc == 0)
flashlight_dev->props.color_temperature = rc;
return rc;
}
}
return -EINVAL;
}
EXPORT_SYMBOL(flashlight_set_color_temperature);
int flashlight_set_torch_brightness(
struct flashlight_device *flashlight_dev,
int brightness_level)
{
int rc;
if ((flashlight_dev->ops == NULL) ||
(flashlight_dev->ops->set_torch_brightness == NULL))
return -EINVAL;
if (brightness_level > flashlight_dev->props.torch_max_brightness)
return -EINVAL;
mutex_lock(&flashlight_dev->ops_lock);
rc = flashlight_dev->ops->set_torch_brightness(flashlight_dev,
brightness_level);
mutex_unlock(&flashlight_dev->ops_lock);
if (rc < 0)
return rc;
flashlight_dev->props.torch_brightness = brightness_level;
return rc;
}
EXPORT_SYMBOL(flashlight_set_torch_brightness);
int flashlight_set_strobe_brightness(
struct flashlight_device *flashlight_dev,
int brightness_level)
{
int rc;
if ((flashlight_dev->ops == NULL) ||
(flashlight_dev->ops->set_strobe_brightness == NULL))
return -EINVAL;
if (brightness_level > flashlight_dev->props.strobe_max_brightness)
return -EINVAL;
mutex_lock(&flashlight_dev->ops_lock);
rc = flashlight_dev->ops->set_strobe_brightness(flashlight_dev,
brightness_level);
mutex_unlock(&flashlight_dev->ops_lock);
if (rc < 0)
return rc;
flashlight_dev->props.strobe_brightness = brightness_level;
return rc;
}
EXPORT_SYMBOL(flashlight_set_strobe_brightness);
int flashlight_list_strobe_timeout(
struct flashlight_device *flashlight_dev,
int selector)
{
if (flashlight_dev->ops &&
flashlight_dev->ops->list_strobe_timeout) {
return flashlight_dev->ops->list_strobe_timeout(flashlight_dev,
selector);
}
return -EINVAL;
}
EXPORT_SYMBOL(flashlight_list_strobe_timeout);
int flashlight_set_strobe_timeout(
struct flashlight_device *flashlight_dev,
int min_ms, int max_ms)
{
int selector = 0;
int rc = -EINVAL;
int timeout;
if ((flashlight_dev->ops == NULL) ||
(flashlight_dev->ops->set_strobe_timeout == NULL))
return -EINVAL;
for (selector = 0; ; selector++) {
timeout = flashlight_list_strobe_timeout(flashlight_dev,
selector);
if (timeout < 0)
return timeout;
if (timeout >= min_ms && timeout <= max_ms) {
mutex_lock(&flashlight_dev->ops_lock);
rc = flashlight_dev->ops->set_strobe_timeout(
flashlight_dev, timeout);
mutex_unlock(&flashlight_dev->ops_lock);
if (rc == 0)
flashlight_dev->props.strobe_timeout = timeout;
return rc;
}
}
return -EINVAL;
}
EXPORT_SYMBOL(flashlight_set_strobe_timeout);
int flashlight_set_mode(struct flashlight_device *flashlight_dev,
int mode)
{
int rc;
if (mode >= FLASHLIGHT_MODE_MAX || mode < 0)
return -EINVAL;
if ((flashlight_dev->ops == NULL) ||
(flashlight_dev->ops->set_mode == NULL)) {
flashlight_dev->props.mode = mode;
return 0;
}
mutex_lock(&flashlight_dev->ops_lock);
rc = flashlight_dev->ops->set_mode(flashlight_dev,
mode);
mutex_unlock(&flashlight_dev->ops_lock);
if (rc < 0)
return rc;
flashlight_dev->props.mode = mode;
return rc;
}
EXPORT_SYMBOL(flashlight_set_mode);
int flashlight_strobe(struct flashlight_device *flashlight_dev)
{
if (flashlight_dev->props.mode == FLASHLIGHT_MODE_FLASH
|| flashlight_dev->props.mode == FLASHLIGHT_MODE_MIXED) {
if (flashlight_dev->ops == NULL ||
flashlight_dev->ops->strobe == NULL)
return -EINVAL;
return flashlight_dev->ops->strobe(flashlight_dev);
}
return -EINVAL;
}
EXPORT_SYMBOL(flashlight_strobe);
int flashlight_is_ready(struct flashlight_device *flashlight_dev)
{
if (flashlight_dev->ops == NULL ||
flashlight_dev->ops->is_ready == NULL)
return -EINVAL;
return flashlight_dev->ops->is_ready(flashlight_dev);
}
EXPORT_SYMBOL(flashlight_is_ready);
static int flashlight_match_device_by_name(struct device *dev, const void *data)
{
const char *name = data;
return strcmp(dev_name(dev), name) == 0;
}
struct flashlight_device *find_flashlight_by_name(const char *name)
{
struct device *dev;
if (!name)
return (struct flashlight_device *)NULL;
dev = class_find_device(flashlight_class, NULL, name,
flashlight_match_device_by_name);
return dev ? to_flashlight_device(dev) : NULL;
}
EXPORT_SYMBOL(find_flashlight_by_name);
int flashlight_strobe_charge(struct flashlight_device *flashlight_dev,
flashlight_charge_event_cb cb, void *data, int start)
{
if (flashlight_dev->ops->strobe_charge)
return flashlight_dev->ops->strobe_charge(flashlight_dev,
cb, data, start);
if (flashlight_dev->props.type == FLASHLIGHT_TYPE_LED) {
if (cb)
cb(data, 0);
return 0;
}
return -EINVAL;
}
EXPORT_SYMBOL(flashlight_strobe_charge);
static SIMPLE_DEV_PM_OPS(flashlight_pm_ops, flashlight_suspend,
flashlight_resume);
static void __exit flashlight_class_exit(void)
{
class_destroy(flashlight_class);
}
static int __init flashlight_class_init(void)
{
flashlight_class = class_create(THIS_MODULE, "flashlight");
if (IS_ERR(flashlight_class)) {
pr_info("Unable to create flashlight class; errno = %ld\n",
PTR_ERR(flashlight_class));
return PTR_ERR(flashlight_class);
}
flashlight_class->dev_groups = flashlight_groups;
flashlight_class->pm = &flashlight_pm_ops;
return 0;
}
subsys_initcall(flashlight_class_init);
module_exit(flashlight_class_exit);
MODULE_DESCRIPTION("Flashlight Class Device");
MODULE_AUTHOR("Patrick Chang <patrick_chang@richtek.com>");
MODULE_VERSION("1.0.2_G");
MODULE_LICENSE("GPL");
@@ -0,0 +1,146 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef LINUX_LEDS_FLASHLIGHT_H
#define LINUX_LEDS_FLASHLIGHT_H
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/mutex.h>
enum flashlight_type {
FLASHLIGHT_TYPE_XENON = 0,
FLASHLIGHT_TYPE_LED,
FLASHLIFHT_TYPE_BULB,
FLASHLIGHT_TYPE_MAX,
};
enum flashlight_mode {
FLASHLIGHT_MODE_OFF = 0,
FLASHLIGHT_MODE_TORCH,
FLASHLIGHT_MODE_FLASH,
/* MIXED mode means TORCH + FLASH */
FLASHLIGHT_MODE_MIXED,
/* DUAL_FLASH mode means turn on FCS_ENx & strobe simultaneously */
FLASHLIGHT_MODE_DUAL_FLASH,
/* DUAL_TORCH mode means turn on FCS_ENx & torch simultaneously */
FLASHLIGHT_MODE_DUAL_TORCH,
/* DUAL_OFF mode means turn off FCS_ENx simultaneously */
FLASHLIGHT_MODE_DUAL_OFF,
FLASHLIGHT_MODE_MAX,
};
struct flashlight_device;
typedef int (*flashlight_charge_event_cb)(void *data, int remains);
struct flashlight_ops {
int (*set_torch_brightness)(struct flashlight_device *dev, int level);
int (*set_strobe_brightness)(struct flashlight_device *dev, int level);
int (*set_strobe_timeout)(struct flashlight_device *dev, int timeout);
int (*list_strobe_timeout)(struct flashlight_device *dev, int selector);
int (*set_mode)(struct flashlight_device *dev, int mode);
int (*set_color_temperature)(struct flashlight_device *dev, int temp);
int (*list_color_temperature)(struct flashlight_device *dev, int temp);
int (*strobe_charge)(struct flashlight_device *dev,
flashlight_charge_event_cb cb, void *data, int start);
int (*strobe)(struct flashlight_device *dev);
int (*is_ready)(struct flashlight_device *dev);
int (*suspend)(struct flashlight_device *dev, pm_message_t state);
int (*resume)(struct flashlight_device *dev);
};
struct flashlight_properties {
/* Flashlight type */
enum flashlight_type type;
/* Xenon type flashlight doesn't support torch mode */
enum flashlight_mode mode;
/* Color temperature, unit: K, 0 means unknown */
int color_temperature;
int torch_brightness;
int torch_max_brightness;
int strobe_brightness;
int strobe_max_brightness;
int strobe_delay;
int strobe_timeout;
const char *alias_name;
};
struct flashlight_device {
/* Flashlight properties */
struct flashlight_properties props;
const struct flashlight_ops *ops;
struct mutex ops_lock;
struct device dev;
};
extern struct flashlight_device *flashlight_device_register(const char *name,
struct device *parent, void *devdata, const struct flashlight_ops *ops,
const struct flashlight_properties *props);
extern void flashlight_device_unregister(
struct flashlight_device *flashlight_dev);
extern struct flashlight_device *find_flashlight_by_name(const char *name);
extern int flashlight_list_color_temperature(
struct flashlight_device *flashlight_dev, int selector);
extern int flashlight_set_color_temperature(
struct flashlight_device *flashlight_dev,
int minK, int maxK);
extern int flashlight_set_torch_brightness(
struct flashlight_device *flashlight_dev,
int brightness_level);
extern int flashlight_set_strobe_brightness(
struct flashlight_device *flashlight_dev,
int brightness_level);
extern int flashlight_list_strobe_timeout(
struct flashlight_device *flashlight_dev,
int selector);
extern int flashlight_set_strobe_timeout(
struct flashlight_device *flashlight_dev,
int min_ms, int max_ms);
extern int flashlight_set_mode(
struct flashlight_device *flashlight_dev, int mode);
extern int flashlight_strobe(struct flashlight_device *flashlight_dev);
/* flashlight_is_ready(struct flashlight_device *flashlight_dev)
*
* description : use this to make sure the flashlight is really ready or not
* return : 0 means not ready, 1 means ready, otherwise, reutrn negative value,
* for the negative value, see definitions in errno.h
*/
extern int flashlight_is_ready(struct flashlight_device *flashlight_dev);
/* flashlight_charge_event_cb(void *data, int remains)
* description :
* callback function of flashlight charging progress
* arguments :
* @data : data pass by flashlight_strobe_charge()
* @remains : remained time to full chargerd, unit : ms ; 0 means ready
* return : 0 means succeess, otherwise see definitions in errno.h
*/
/* flashlight_strobe_charge(
* struct flashlight_device *flashlight_dev,
* flashlight_charge_event_cb cb, void *data, int start)
* description :
* flashlight start / stop charging
* @flashlight_dev : flashlight devices
* @flashlight_charge_event_cb : callback function to report progress
* @data : bypass to callback function
* @start : 1 means start; 0 means stop
*/
extern int flashlight_strobe_charge(struct flashlight_device *flashlight_dev,
flashlight_charge_event_cb cb, void *data, int start);
#define to_flashlight_device(obj) \
container_of(obj, struct flashlight_device, dev)
static inline void *flashlight_get_data(
struct flashlight_device *flashlight_dev)
{
return dev_get_drvdata(&flashlight_dev->dev);
}
#endif /*LINUX_LEDS_FLASHLIGHT_H*/
@@ -0,0 +1,445 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include "rtfled.h"
#include <linux/init.h>
#include <linux/version.h>
#define RTFLED_INFO(format, args...) \
pr_info("%s:%s() line-%d: " format, \
ALIAS_NAME, __func__, __LINE__, ## args)
#define RTFLED_WARN(format, args...) \
pr_warn("%s:%s() line-%d: " format, \
ALIAS_NAME, __func__, __LINE__, ## args)
#define RTFLED_ERR(format, args...) \
pr_err("%s:%s() line-%d: " format, \
ALIAS_NAME, __func__, __LINE__, ## args)
#define RT_FLED_DEVICE "rt-flash-led"
#define ALIAS_NAME RT_FLED_DEVICE
struct rt_fled_dev *rt_fled_get_dev(const char *name)
{
struct flashlight_device *flashlight_dev;
flashlight_dev = find_flashlight_by_name(name ? name : RT_FLED_DEVICE);
if (flashlight_dev == NULL)
return (struct rt_fled_dev *)NULL;
return flashlight_get_data(flashlight_dev);
}
EXPORT_SYMBOL(rt_fled_get_dev);
static int rtfled_set_torch_brightness(struct flashlight_device *flashlight_dev,
int brightness_sel)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
return fled_dev->hal->rt_hal_fled_set_torch_current_sel(fled_dev,
brightness_sel);
}
static int rtfled_set_strobe_brightness(struct flashlight_device
*flashlight_dev, int brightness_sel)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
return fled_dev->hal->rt_hal_fled_set_strobe_current_sel(fled_dev,
brightness_sel);
}
static int rtfled_set_strobe_timeout(struct flashlight_device *flashlight_dev,
int timeout)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
int sel;
return fled_dev->hal->rt_hal_fled_set_strobe_timeout(fled_dev, timeout,
timeout, &sel);
}
static int rtfled_list_strobe_timeout(struct flashlight_device *flashlight_dev,
int selector)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
return fled_dev->hal->rt_hal_fled_strobe_timeout_list(fled_dev,
selector);
}
static int rtfled_set_mode(struct flashlight_device *flashlight_dev, int mode)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
return fled_dev->hal->rt_hal_fled_set_mode(fled_dev, mode);
}
static int rtfled_strobe(struct flashlight_device *flashlight_dev)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
return fled_dev->hal->rt_hal_fled_strobe(fled_dev);
}
static int rtfled_is_ready(struct flashlight_device *flashlight_dev)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
return fled_dev->hal->rt_hal_fled_get_is_ready(fled_dev);
}
static int rtfled_set_color_temperature(struct flashlight_device
*flashlight_dev, int color_temp)
{
/* Doesn't support color temperature */
return -EINVAL;
}
static int rtfled_list_color_temperature(struct flashlight_device
*flashlight_dev, int selector)
{
/* Doesn't support color temperature */
return -EINVAL;
}
static int rtfled_suspend(struct flashlight_device *flashlight_dev,
pm_message_t state)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
if (fled_dev->hal->rt_hal_fled_suspend)
return fled_dev->hal->rt_hal_fled_suspend(fled_dev, state);
return 0;
}
static int rtfled_resume(struct flashlight_device *flashlight_dev)
{
struct rt_fled_dev *fled_dev = flashlight_get_data(flashlight_dev);
if (fled_dev->hal->rt_hal_fled_resume)
return fled_dev->hal->rt_hal_fled_resume(fled_dev);
return 0;
}
static struct flashlight_ops rtfled_impl_ops = {
.set_torch_brightness = rtfled_set_torch_brightness,
.set_strobe_brightness = rtfled_set_strobe_brightness,
.set_strobe_timeout = rtfled_set_strobe_timeout,
.list_strobe_timeout = rtfled_list_strobe_timeout,
.set_mode = rtfled_set_mode,
.strobe = rtfled_strobe,
.is_ready = rtfled_is_ready,
.set_color_temperature = rtfled_set_color_temperature,
.list_color_temperature = rtfled_list_color_temperature,
.suspend = rtfled_suspend,
.resume = rtfled_resume,
};
static void rfled_shutdown(struct platform_device *pdev)
{
struct rt_fled_dev *fled_dev = platform_get_drvdata(pdev);
if (fled_dev->hal->rt_hal_fled_shutdown)
fled_dev->hal->rt_hal_fled_shutdown(fled_dev);
}
static int rtled_impl_set_torch_current(struct rt_fled_dev *fled_dev,
int min_uA, int max_uA, int *selector)
{
int sel = 0;
int rc;
for (sel = 0;; sel++) {
rc = fled_dev->hal->rt_hal_fled_torch_current_list(fled_dev,
sel);
if (rc < 0)
return rc;
if (rc >= min_uA && rc <= max_uA) {
*selector = sel;
return fled_dev->hal->rt_hal_fled_set_torch_current_sel
(fled_dev, sel);
}
}
return -EINVAL;
}
static int rtled_impl_set_strobe_current(struct rt_fled_dev *fled_dev,
int min_uA, int max_uA, int *selector)
{
int sel = 0;
int rc;
for (sel = 0;; sel++) {
rc = fled_dev->hal->rt_hal_fled_strobe_current_list(fled_dev,
sel);
if (rc < 0)
return rc;
if (rc >= min_uA && rc <= max_uA) {
*selector = sel;
return fled_dev->hal->rt_hal_fled_set_strobe_current_sel
(fled_dev, sel);
}
}
return -EINVAL;
}
static int rtled_impl_set_timeout_level(struct rt_fled_dev *fled_dev,
int min_uA, int max_uA, int *selector)
{
int sel = 0;
int rc;
for (sel = 0;; sel++) {
rc = fled_dev->hal->rt_hal_fled_timeout_level_list(fled_dev,
sel);
if (rc < 0)
return rc;
if (rc >= min_uA && rc <= max_uA) {
*selector = sel;
return fled_dev->hal->rt_hal_fled_set_timeout_level_sel
(fled_dev, sel);
}
}
return -EINVAL;
}
static int rtled_impl_set_lv_protection(struct rt_fled_dev *fled_dev,
int min_mV, int max_mV, int *selector)
{
int sel = 0;
int rc;
for (sel = 0;; sel++) {
rc = fled_dev->hal->rt_hal_fled_lv_protection_list(fled_dev,
sel);
if (rc < 0)
return rc;
if (rc >= min_mV && rc <= max_mV) {
*selector = sel;
return fled_dev->hal->rt_hal_fled_set_lv_protection_sel
(fled_dev, sel);
}
}
return -EINVAL;
}
static int rtled_impl_set_strobe_timeout(struct rt_fled_dev *fled_dev,
int min_ms, int max_ms, int *selector)
{
int sel = 0;
int rc;
for (sel = 0;; sel++) {
rc = fled_dev->hal->rt_hal_fled_strobe_timeout_list(fled_dev,
sel);
if (rc < 0)
return rc;
if (rc >= min_ms && rc <= max_ms) {
*selector = sel;
return fled_dev->hal->rt_hal_fled_set_strobe_timeout_sel
(fled_dev, sel);
}
}
return -EINVAL;
}
static int rtled_impl_get_torch_current(struct rt_fled_dev *fled_dev)
{
int sel = fled_dev->hal->rt_hal_fled_get_torch_current_sel(fled_dev);
if (sel < 0)
return sel;
return fled_dev->hal->rt_hal_fled_torch_current_list(fled_dev, sel);
}
static int rtled_impl_get_strobe_current(struct rt_fled_dev *fled_dev)
{
int sel = fled_dev->hal->rt_hal_fled_get_strobe_current_sel(fled_dev);
if (sel < 0)
return sel;
return fled_dev->hal->rt_hal_fled_strobe_current_list(fled_dev, sel);
}
static int rtled_impl_get_timeout_level(struct rt_fled_dev *fled_dev)
{
int sel = fled_dev->hal->rt_hal_fled_get_timeout_level_sel(fled_dev);
if (sel < 0)
return sel;
return fled_dev->hal->rt_hal_fled_timeout_level_list(fled_dev, sel);
}
static int rtled_impl_get_lv_protection(struct rt_fled_dev *fled_dev)
{
int sel = fled_dev->hal->rt_hal_fled_get_lv_protection_sel(fled_dev);
if (sel < 0)
return sel;
return fled_dev->hal->rt_hal_fled_lv_protection_list(fled_dev, sel);
}
static int rtled_impl_get_strobe_timeout(struct rt_fled_dev *fled_dev)
{
int sel = fled_dev->hal->rt_hal_fled_get_strobe_timeout_sel(fled_dev);
if (sel < 0)
return sel;
return fled_dev->hal->rt_hal_fled_strobe_timeout_list(fled_dev, sel);
}
static int rtled_impl_get_is_ready(struct rt_fled_dev *fled_dev)
{
/* if not implemented, just return ready always */
return 1;
}
#define HAL_NOT_IMPLEMENTED(x) (hal->x == NULL)
static inline int check_hal_implemented(void *x)
{
if (x == NULL)
return -EINVAL;
return 0;
}
static int rtfled_check_hal_implement(struct rt_fled_hal *hal)
{
int rc = 0;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_set_torch_current))
hal->rt_hal_fled_set_torch_current =
rtled_impl_set_torch_current;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_set_strobe_current))
hal->rt_hal_fled_set_strobe_current =
rtled_impl_set_strobe_current;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_set_timeout_level))
hal->rt_hal_fled_set_timeout_level =
rtled_impl_set_timeout_level;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_set_lv_protection))
hal->rt_hal_fled_set_lv_protection =
rtled_impl_set_lv_protection;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_set_strobe_timeout))
hal->rt_hal_fled_set_strobe_timeout =
rtled_impl_set_strobe_timeout;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_get_torch_current))
hal->rt_hal_fled_get_torch_current =
rtled_impl_get_torch_current;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_get_strobe_current))
hal->rt_hal_fled_get_strobe_current =
rtled_impl_get_strobe_current;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_get_timeout_level))
hal->rt_hal_fled_get_timeout_level =
rtled_impl_get_timeout_level;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_get_lv_protection))
hal->rt_hal_fled_get_lv_protection =
rtled_impl_get_lv_protection;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_get_strobe_timeout))
hal->rt_hal_fled_get_strobe_timeout =
rtled_impl_get_strobe_timeout;
if (HAL_NOT_IMPLEMENTED(rt_hal_fled_get_is_ready))
hal->rt_hal_fled_get_is_ready = rtled_impl_get_is_ready;
rc |= check_hal_implemented(hal->rt_hal_fled_set_mode);
rc |= check_hal_implemented(hal->rt_hal_fled_get_mode);
rc |= check_hal_implemented(hal->rt_hal_fled_strobe);
rc |= check_hal_implemented(hal->rt_hal_fled_torch_current_list);
rc |= check_hal_implemented(hal->rt_hal_fled_strobe_current_list);
rc |= check_hal_implemented(hal->rt_hal_fled_timeout_level_list);
rc |= check_hal_implemented(hal->rt_hal_fled_lv_protection_list);
rc |= check_hal_implemented(hal->rt_hal_fled_strobe_timeout_list);
rc |= check_hal_implemented(hal->rt_hal_fled_set_torch_current_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_set_strobe_current_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_set_timeout_level_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_set_lv_protection_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_set_strobe_timeout_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_get_torch_current_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_get_strobe_current_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_get_timeout_level_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_get_lv_protection_sel);
rc |= check_hal_implemented(hal->rt_hal_fled_get_strobe_timeout_sel);
if (rc != 0)
RTFLED_WARN("check_hal_implemented have NULL item.\n");
return 0;
}
static int rtfled_probe(struct platform_device *pdev)
{
struct rt_fled_dev *fled_dev = dev_get_drvdata(pdev->dev.parent);
int rc;
WARN_ON(fled_dev == NULL);
if (!fled_dev)
return -ENODEV;
WARN_ON(fled_dev->hal == NULL);
if (!fled_dev->hal)
return -EPERM;
RTFLED_INFO("Richtek FlashLED Driver is probing\n");
rc = rtfled_check_hal_implement(fled_dev->hal);
if (rc < 0) {
RTFLED_ERR("HAL implemented incompletely\n");
goto err_check_hal;
}
platform_set_drvdata(pdev, fled_dev);
fled_dev->flashlight_dev =
flashlight_device_register
(fled_dev->name ? fled_dev->name : RT_FLED_DEVICE, &pdev->dev,
fled_dev, &rtfled_impl_ops, fled_dev->init_props);
if (fled_dev->hal->rt_hal_fled_init) {
rc = fled_dev->hal->rt_hal_fled_init(fled_dev);
if (rc < 0) {
RTFLED_ERR("Initialization failed\n");
goto err_init;
}
}
RTFLED_INFO("Richtek FlashLED Driver initialized successfully\n");
return 0;
err_init:
flashlight_device_unregister(fled_dev->flashlight_dev);
err_check_hal:
return rc;
}
static int rtfled_remove(struct platform_device *pdev)
{
struct rt_fled_dev *fled_dev = platform_get_drvdata(pdev);
platform_set_drvdata(pdev, NULL);
flashlight_device_unregister(fled_dev->flashlight_dev);
return 0;
}
static struct platform_driver rt_flash_led_driver = {
.driver = {
.name = RT_FLED_DEVICE,
.owner = THIS_MODULE,
},
.shutdown = rfled_shutdown,
.probe = rtfled_probe,
.remove = rtfled_remove,
};
static int __init rtfled_init(void)
{
return platform_driver_register(&rt_flash_led_driver);
}
subsys_initcall(rtfled_init);
static void __exit rtfled_exit(void)
{
platform_driver_unregister(&rt_flash_led_driver);
}
module_exit(rtfled_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Patrick Chang <patrick_chang@richtek.com");
MODULE_VERSION("1.0.2_G");
MODULE_DESCRIPTION("Richtek Flash LED Driver");
@@ -0,0 +1,97 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef LINUX_LEDS_RTFLED_H
#define LINUX_LEDS_RTFLED_H
#include "rt-flashlight.h"
struct rt_fled_dev;
struct rt_fled_hal {
int (*rt_hal_fled_init)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_suspend)
(struct rt_fled_dev *fled_dev, pm_message_t state);
int (*rt_hal_fled_resume)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_set_mode)
(struct rt_fled_dev *fled_dev, enum flashlight_mode mode);
int (*rt_hal_fled_get_mode)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_strobe)(struct rt_fled_dev *fled_dev);
/* Return value : -EINVAL => selector parameter is
* out of range, otherwise current in uA
*/
int (*rt_hal_fled_torch_current_list)
(struct rt_fled_dev *fled_dev, int selector);
int (*rt_hal_fled_strobe_current_list)
(struct rt_fled_dev *fled_dev, int selector);
int (*rt_hal_fled_timeout_level_list)
(struct rt_fled_dev *fled_dev, int selector);
/* Return value : -EINVAL => selector parameter is
* out of range, otherwise voltage in mV
*/
int (*rt_hal_fled_lv_protection_list)
(struct rt_fled_dev *fled_dev, int selector);
/* Return value : -EINVAL => selector parameter is
* out of range, otherwise time in ms
*/
int (*rt_hal_fled_strobe_timeout_list)
(struct rt_fled_dev *fled_dev, int selector);
/* method to set, optional */
int (*rt_hal_fled_set_torch_current)
(struct rt_fled_dev *fled_dev, int min_uA, int max_uA, int *selector);
int (*rt_hal_fled_set_strobe_current)
(struct rt_fled_dev *fled_dev, int min_uA, int max_uA, int *selector);
int (*rt_hal_fled_set_timeout_level)
(struct rt_fled_dev *fled_dev, int min_uA, int max_uA, int *selector);
int (*rt_hal_fled_set_lv_protection)
(struct rt_fled_dev *fled_dev, int min_mV, int max_mV, int *selector);
int (*rt_hal_fled_set_strobe_timeout)
(struct rt_fled_dev *fled_dev, int min_ms, int max_ms, int *selector);
/* method to set */
int (*rt_hal_fled_set_torch_current_sel)
(struct rt_fled_dev *fled_dev, int selector);
int (*rt_hal_fled_set_strobe_current_sel)
(struct rt_fled_dev *fled_dev, int selector);
int (*rt_hal_fled_set_timeout_level_sel)
(struct rt_fled_dev *fled_dev, int selector);
int (*rt_hal_fled_set_lv_protection_sel)
(struct rt_fled_dev *fled_dev, int selector);
int (*rt_hal_fled_set_strobe_timeout_sel)
(struct rt_fled_dev *fled_dev, int selector);
/* method to get */
int (*rt_hal_fled_get_torch_current_sel)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_get_strobe_current_sel)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_get_timeout_level_sel)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_get_lv_protection_sel)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_get_strobe_timeout_sel)(struct rt_fled_dev *fled_dev);
/* method to get, optional*/
int (*rt_hal_fled_get_torch_current)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_get_strobe_current)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_get_timeout_level)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_get_lv_protection)(struct rt_fled_dev *fled_dev);
int (*rt_hal_fled_get_strobe_timeout)(struct rt_fled_dev *fled_dev);
/* Return value : not ready, return 0, ready return 1,
* if failed, return -errno, see definitions in errno.h
*/
int (*rt_hal_fled_get_is_ready)(struct rt_fled_dev *fled_dev);
/* PM shutdown, optional */
void (*rt_hal_fled_shutdown)(struct rt_fled_dev *fled_dev);
};
struct rt_fled_dev {
struct rt_fled_hal *hal;
struct flashlight_device *flashlight_dev;
const struct flashlight_properties *init_props;
char *name;
char *chip_name;
};
/* Public functions
* argument
* @name : Flash LED's name;pass NULL menas "rt-flash-led"
*/
struct rt_fled_dev *rt_fled_get_dev(const char *name);
/* Usage :
* fled_dev = rt_fled_get_dev("FlashLED1");
* fled_dev->hal->fled_set_strobe_current(fled_dev,
* 150, 200);
*/
#endif /*LINUX_LEDS_RTFLED_H*/