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

+215
View File
@@ -0,0 +1,215 @@
#
# Flashlight device configuration
#
config MTK_FLASHLIGHT
tristate "Mediatek flashlight driver"
help
This is flashlight driver for camera.
Say Y here if you want a flashlight core to handle flashlight devices.
If you have any questions about this, say Y here,
only say N if you know exactly what you are doing.
config MTK_V4L2_FLASHLIGHT
tristate "Mediatek V4L2 flashlight driver"
help
Say Y to enable V4L2 flashlight device
This Framework is good for
controling flashlight Leds.
Say N to disable V4L2 flashlight.
config MTK_FLASHLIGHT_AW3644
tristate "Mediatek flashlight with driver IC (AW3644)"
depends on MTK_FLASHLIGHT
help
This is for the flashlight driver IC (AW3644).
Say Y here if you want a flashlight driver IC AW3644.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_LED191
tristate "Mediatek flashlight led191 gpio driver"
depends on MTK_FLASHLIGHT
help
This is flashlight led191 gpio driver for camera.
Say Y here if you want a driver IC with GPIO interface.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_LM3642
tristate "Mediatek flashlight with driver IC (TI LM3642)"
depends on MTK_FLASHLIGHT
help
This is for the flashlight driver IC (TI LM3642).
Say Y here if you want a flashlight driver IC LM3642.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_LM3643
tristate "Mediatek flashlight with driver IC (TI LM3643)"
depends on MTK_FLASHLIGHT
help
This is for the flashlight driver IC (TI LM3643).
Say Y here if you want a flashlight driver IC LM3643.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_LM3644
tristate "Mediatek flashlight with driver IC (TI LM3644)"
depends on MTK_FLASHLIGHT
help
This is for the flashlight driver IC (TI LM3644).
Say Y here if you want a flashlight driver IC LM3644.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_RT4505
tristate "Mediatek flashlight with driver IC (Richtek RT4505)"
depends on MTK_FLASHLIGHT
help
This is for the flashlight driver IC (Richtek RT4505).
Say Y here if you want a flashlight driver IC RT4505.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_RT5081
tristate "Mediatek flashlight with driver IC (Richtek RT5081)"
depends on MTK_FLASHLIGHT && RT_FLASHLIGHT
help
This is for the flashlight driver IC (Richtek RT5081).
Say Y here if you want a flashlight driver IC RT5081.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_MT6336
tristate "Mediatek flashlight with driver IC (Mediatek MT6336)"
depends on MTK_FLASHLIGHT
help
This is for the flashlight driver IC (Mediatek MT6336).
Say Y here if you want a flashlight driver IC MT6336.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_MT6370
tristate "Mediatek flashlight with driver IC (Mediatek MT6370)"
depends on MTK_FLASHLIGHT && RT_FLASHLIGHT
help
This is for the flashlight driver IC (Mediatek MT6370).
Say Y here if you want a flashlight driver IC MT6370.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_MT6360
tristate "Mediatek flashlight with driver IC (Mediatek MT6360)"
depends on MTK_FLASHLIGHT && RT_FLASHLIGHT
help
This is for the flashlight driver IC (Mediatek MT6360).
Say Y here if you want a flashlight driver IC MT6360.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_SYWT78
tristate "Mediatek flashlight sywt78 gpio driver"
depends on MTK_FLASHLIGHT
help
This is flashlight sywt78 gpio driver for camera.
Say Y here if you want a driver IC with GPIO interface.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_DUMMY
tristate "Mediatek flashlight dummy driver"
depends on MTK_FLASHLIGHT
help
This is flashlight dummy driver for camera.
Say Y here if you want a dummy driver IC with I2C interface.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_DUMMY_GPIO
tristate "Mediatek flashlight dummy gpio driver"
depends on MTK_FLASHLIGHT
help
This is flashlight dummy gpio driver for camera.
Say Y here if you want a dummy driver IC with GPIO interface.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
config MTK_FLASHLIGHT_DEBUG
bool "Mediatek flashlight debugging messages"
depends on MTK_FLASHLIGHT
help
Say Y here if you want the flashlight to produce a bunch of debug
messages to the system log.
Select this if you are having a problem with flashlight support
and want to see more of what is going on.
config MTK_FLASHLIGHT_PT
bool "Mediatek flashlight power throttling"
depends on MTK_FLASHLIGHT
help
This is for flashlight power throttling.
Power throttling will limit flashlight function,
such as disable flashlight, decrease brightness level or
change flash mode to torch mode.
config MTK_FLASHLIGHT_PT_STRICT
bool "Mediatek flashlight power throttling (strict)"
depends on MTK_FLASHLIGHT_PT
help
This is for flashlight strict power throttling.
Strict power throttling will disable flashlight when
over-current, low battary voltage and low battary percentage
situations.
config MTK_FLASHLIGHT_DLPT
bool "Mediatek flashlight dynamic low power throttling"
depends on MTK_FLASHLIGHT
help
This is for flashlight dynamic low power throttling.
Dynamic low power throttling is a mechanism to monitor
the current of battary dynamicly. It will start by request
and notice user by kernel notifier.
config MTK_FLASHLIGHT_OCP81375
tristate "Mediatek flashlight with driver IC (OCS OCP81375)"
depends on MTK_FLASHLIGHT
help
This is for the flashlight driver IC (OCS OCP81375).
Say Y here if you want a flashlight driver IC OCP81375.
Select this only if your board has this IC.
Say M here if you want to compile this driver as a module.
+45
View File
@@ -0,0 +1,45 @@
#
# Makefile for the kernel flashlight device drivers.
#
flashlight-y := flashlight-core.o
flashlight-y += flashlight-device.o
obj-$(CONFIG_MTK_FLASHLIGHT) += flashlight.o
obj-$(CONFIG_MTK_V4L2_FLASHLIGHT) += mtk-composite.o
obj-$(CONFIG_MTK_FLASHLIGHT_LED191) += flashlights-led191.o
obj-$(CONFIG_MTK_FLASHLIGHT_LM3642) += flashlights-lm3642.o
obj-$(CONFIG_MTK_FLASHLIGHT_LM3643) += flashlights-lm3643.o
obj-$(CONFIG_MTK_FLASHLIGHT_LM3644) += flashlights-lm3644.o
obj-$(CONFIG_MTK_FLASHLIGHT_RT4505) += flashlights-rt4505.o
obj-$(CONFIG_MTK_FLASHLIGHT_RT5081) += flashlights-rt5081.o
obj-$(CONFIG_MTK_FLASHLIGHT_MT6336) += flashlights-mt6336.o
obj-$(CONFIG_MTK_FLASHLIGHT_MT6370) += flashlights-mt6370.o
obj-$(CONFIG_MTK_FLASHLIGHT_MT6360) += flashlights-mt6360.o
obj-$(CONFIG_MTK_FLASHLIGHT_AW3644) += flashlights-aw3644.o
obj-$(CONFIG_MTK_FLASHLIGHT_SYWT78) += flashlights-sywt78.o
obj-$(CONFIG_MTK_FLASHLIGHT_OCP81375) += flashlights-ocp81375.o
obj-$(CONFIG_MTK_FLASHLIGHT_DUMMY) += flashlights-dummy.o
obj-$(CONFIG_MTK_FLASHLIGHT_DUMMY_GPIO) += flashlights-dummy-gpio.o
ccflags-$(CONFIG_MTK_FLASHLIGHT_DEBUG) := -DDEBUG
ccflags-$(CONFIG_MTK_FLASHLIGHT_SYWT78) += -DCONFIG_MTK_FLASHLIGHT_SYWT78
ccflags-$(CONFIG_MTK_FLASHLIGHT_PT) += -DCONFIG_MTK_FLASHLIGHT_PT
ccflags-$(CONFIG_MTK_FLASHLIGHT_PT_STRICT) += -DCONFIG_MTK_FLASHLIGHT_PT_STRICT
ccflags-$(CONFIG_MTK_FLASHLIGHT_DLPT) += -DCONFIG_MTK_FLASHLIGHT_DLPT
MTK_PLATFORM := $(subst ",,$(CONFIG_MTK_PLATFORM))
MTK_PROJECT := $(subst ",,$(CONFIG_ARCH_MTK_PROJECT))
MTK_CUSTOM_PATH := $(srctree)/drivers/misc/mediatek
ccflags-y += -D$(MTK_PLATFORM) -D$(MTK_PROJECT)
ifeq ($(CONFIG_MTK_FLASHLIGHT_AW3644),y)
ccflags-y += -DCONFIG_MTK_FLASHLIGHT_AW3644
endif
subdir-ccflags-y += \
-I$(MTK_CUSTOM_PATH)/include/mt-plat \
-I$(MTK_CUSTOM_PATH)/include/mt-plat/$(MTK_PLATFORM)/include/mach \
-I$(MTK_CUSTOM_PATH)/pmic/include \
-I$(MTK_CUSTOM_PATH)/pmic \
-I$(MTK_CUSTOM_PATH)/power_throttling
@@ -0,0 +1,26 @@
This is a introduction for flashlight chip LM3642
[Scope]
- one channel for one LED
- Two silicon revision
* LM3642
* LM3642LT (Half torch current)
[Hardware Interface]
- I2C
* For general control
* Slave address 0x63
[Limit]
- Torch Mode
* Min currnet: 48.4 mA (24 mA for LM3642LT)
* Max current: 375 mA (187.5 mA for LM3642LT)
- Strobe Mode
* Min currnet: 93.75 mA
* Max current: 1500 mA
* Min protected timeout: 100 ms
* Max protected timeout: 800 ms
@@ -0,0 +1,36 @@
This is a introduction for flashlight chip LM3643
[Scope]
- Two channel for two LEDs
[Hardware Interface]
- I2C
* For general control
* Slave address 0x63
- GPIO
* For enable/disable chip
[Limit]
- Channel couple:
* Torch/Strobe
- Channel decouple:
* Enable/Disable
* Brightness level
- Torch Mode
* Step current: 1.4 mA
* Min currnet: 0.977 mA
* Max current: 179 mA
- Strobe Mode
* Step current: 11.725 mA
* Min currnet: 10.9 mA
* Max current: 1500 mA
* Max current for two channel: 1500 mA
* Min protected timeout: 10 ms
* Max protected timeout: 400 ms
@@ -0,0 +1,39 @@
This is a introduction for flashlight chip LM3644
[Scope]
- Two channel for two LEDs
- Two silicon revision
* LM3644
* LM3644TT (Douple torch current and flash timeout)
[Hardware Interface]
- I2C
* For general control
* Slave address 0x63
- GPIO
* For enable/disable chip
[Limit]
- Channel couple:
* Torch/Strobe
- Channel decouple:
* Enable/Disable
* Brightness level
- Torch Mode
* Step current: 1.4 mA (2.8 mA for LM3644TT)
* Min currnet: 0.977 mA (1.954 mA for LM3644TT)
* Max current: 179 mA (360 mA for LM3644TT)
- Strobe Mode
* Step current: 11.725 mA
* Min currnet: 10.9 mA
* Max current: 1500 mA
* Max current for two channel: 1500 mA
* Min protected timeout: 10 ms (40 ms for LM3644TT)
* Max protected timeout: 400 ms (1600 ms for LM3644TT)
@@ -0,0 +1,39 @@
This is a introduction for flashlight chip MT6336
[Scope]
- Two channel for two LEDs
[Hardware Interface]
- I2C
* For general control
* Slave address 0x55, 0x56, 0x57, 0x58
[Limit]
- Channel couple:
* Flash/Torch enable
* Flash/Torch LED current source
* Flash mode watchdog timer
* Torch mode watchdog timer
* Flash/Torch timeout
* Flash mode step up/down timing (default: 10 us)
* Flash mode output regulation voltage (default: 5.0 V)
- Channel decouple:
* Flash/Torch mode enable
* Brightness level
- Torch Mode
* Step current: 12.5 mA
* Min currnet: 12.5 mA
* Max current: 200 mA
- Flash Mode
* Step current: 12.5 mA
* Min currnet: 12.5 mA
* Max current: 1500 mA
* Max current for two channel: 2500 mA
* Min protected timeout: 200 ms
* Max protected timeout: 800 ms
@@ -0,0 +1,34 @@
This is a introduction for flashlight chip MT6370
[Scope]
- Two channel for two LEDs
[Hardware Interface]
- I2C
* For general control
* Slave address 0x34
[Limit]
- Channel couple:
* Torch/Strobe mode
* Strobe mode watchdog timer
- Channel decouple:
* Current source
* Brightness level
- Torch Mode
* Step current: 12.5 mA
* Min currnet: 25 mA
* Max current: 400 mA
- Strobe Mode
* Step current: 12.5 mA
* Min currnet: 25 mA
* Max current: 1500 mA
* Max current for two channel: 2500 mA
* Min protected timeout: 64 ms
* Max protected timeout: 2432 ms
@@ -0,0 +1,31 @@
This is a introduction for flashlight chip OCP81375
[Scope]
- Two channels for two LEDs
[Hardware Interface]
- I2C
* For general control
* Slave address 0x63
[Limit]
- Channel couple:
* Torch/Strobe
- Channel decouple:
* Enable/Disable
* Brightness level
- Torch Mode
* Min currnet: 3.90 mA
* Max current: 499.2 mA
* Current step: 3.90 mA
- Strobe Mode
* Min currnet: 15.63 mA
* Max current: 2000.64 mA
* Current step: 15.63 mA
* Min protected timeout: 40 ms
* Max protected timeout: 1600 ms
@@ -0,0 +1,23 @@
This is a introduction for flashlight chip RT4505
[Scope]
- One channel for one LED
[Hardware Interface]
- I2C
* For general control
* Slave address 0x63
[Limit]
- Torch Mode
* Min currnet: 49.60 mA
* Max current: 375 mA
- Strobe Mode
* Min currnet: 93.74 mA
* Max current: 1500 mA
* Min protected timeout: 100 ms
* Max protected timeout: 800 ms
@@ -0,0 +1,34 @@
This is a introduction for flashlight chip RT5081
[Scope]
- Two channel for two LEDs
[Hardware Interface]
- I2C
* For general control
* Slave address 0x34
[Limit]
- Channel couple:
* Torch/Strobe mode
* Strobe mode watchdog timer
- Channel decouple:
* Current source
* Brightness level
- Torch Mode
* Step current: 12.5 mA
* Min currnet: 25 mA
* Max current: 400 mA
- Strobe Mode
* Step current: 12.5 mA
* Min currnet: 25 mA
* Max current: 1500 mA
* Max current for two channel: 2500 mA
* Min protected timeout: 64 ms
* Max protected timeout: 2432 ms
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,153 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _FLASHLIGHT_CORE_H
#define _FLASHLIGHT_CORE_H
#include <linux/list.h>
#include "flashlight.h"
/* protocol version */
#define FLASHLIGHT_PROTOCOL_VERSION 2
/* sysfs - power throttling */
#define PT_NOTIFY_NUM 4
#define PT_NOTIFY_LOW_VOL 0
#define PT_NOTIFY_LOW_BAT 1
#define PT_NOTIFY_OVER_CUR 2
#define PT_NOTIFY_STRICT 3
/* sysfs - sw disable*/
#define FLASHLIGHT_SW_DISABLE_NUM 2
#define FLASHLIGHT_SW_DISABLE_TYPE 0
#define FLASHLIGHT_SW_DISABLE_STATUS 1
#define FLASHLIGHT_SW_DISABLE_STATUS_TMPBUF_SIZE 9
#define FLASHLIGHT_SW_DISABLE_STATUS_BUF_SIZE \
(FLASHLIGHT_TYPE_MAX * FLASHLIGHT_CT_MAX * FLASHLIGHT_PART_MAX * \
FLASHLIGHT_SW_DISABLE_STATUS_TMPBUF_SIZE + 1)
/* sysfs - charger status */
#define FLASHLIGHT_CHARGER_NUM 4
#define FLASHLIGHT_CHARGER_TYPE 0
#define FLASHLIGHT_CHARGER_CT 1
#define FLASHLIGHT_CHARGER_PART 2
#define FLASHLIGHT_CHARGER_STATUS 3
#define FLASHLIGHT_CHARGER_STATUS_TMPBUF_SIZE 9
#define FLASHLIGHT_CHARGER_STATUS_BUF_SIZE \
(FLASHLIGHT_TYPE_MAX * FLASHLIGHT_CT_MAX * FLASHLIGHT_PART_MAX * \
FLASHLIGHT_CHARGER_STATUS_TMPBUF_SIZE + 1)
/* sysfs - capability */
#define FLASHLIGHT_CAPABILITY_TMPBUF_SIZE 64
#define FLASHLIGHT_CAPABILITY_BUF_SIZE \
(FLASHLIGHT_TYPE_MAX * FLASHLIGHT_CT_MAX * FLASHLIGHT_PART_MAX * \
FLASHLIGHT_CAPABILITY_TMPBUF_SIZE + 1)
/* sysfs - fault */
#define FLASHLIGHT_FAULT_TMPBUF_SIZE 64
#define FLASHLIGHT_FAULT_BUF_SIZE \
(FLASHLIGHT_TYPE_MAX * FLASHLIGHT_CT_MAX * FLASHLIGHT_PART_MAX * \
FLASHLIGHT_FAULT_TMPBUF_SIZE + 1)
/* sysfs - current */
#define FLASHLIGHT_CURRENT_NUM 3
#define FLASHLIGHT_CURRENT_TYPE 0
#define FLASHLIGHT_CURRENT_CT 1
#define FLASHLIGHT_CURRENT_PART 2
#define FLASHLIGHT_DUTY_CURRENT_TMPBUF_SIZE 6
#define FLASHLIGHT_DUTY_CURRENT_BUF_SIZE \
(FLASHLIGHT_MAX_DUTY_NUM * FLASHLIGHT_DUTY_CURRENT_TMPBUF_SIZE + 1)
/* sysfs - strobe */
#define FLASHLIGHT_ARG_NUM 5
#define FLASHLIGHT_ARG_TYPE 0
#define FLASHLIGHT_ARG_CT 1
#define FLASHLIGHT_ARG_PART 2
#define FLASHLIGHT_ARG_LEVEL 3
#define FLASHLIGHT_ARG_DUR 4
#define FLASHLIGHT_ARG_LEVEL_MAX 255
#define FLASHLIGHT_ARG_DUR_MAX 3000 /* ms */
struct flashlight_arg {
int type;
int ct;
int part;
int channel;
int level;
int dur;
int decouple;
};
/* flashlight devices */
#define FLASHLIGHT_NAME_SIZE 32 /* flashlight device name */
struct flashlight_device_id {
int type;
int ct;
int part;
char name[FLASHLIGHT_NAME_SIZE]; /* device name */
int channel; /* device channel */
int decouple; /* device decouple */
};
extern const struct flashlight_device_id flashlight_id[];
extern const int flashlight_device_num;
struct flashlight_dev {
struct list_head node;
struct flashlight_operations *ops;
struct flashlight_device_id dev_id;
/* device status */
int enable;
int level;
int low_pt_level;
int charger_status;
int sw_disable_status;
};
/* device arguments */
struct flashlight_dev_arg {
int channel;
int arg;
};
/* device operations */
struct flashlight_operations {
int (*flashlight_open)(void);
int (*flashlight_release)(void);
int (*flashlight_ioctl)(unsigned int cmd, unsigned long arg);
ssize_t (*flashlight_strobe_store)(struct flashlight_arg arg);
int (*flashlight_set_driver)(int set);
};
/* device resiger */
int flashlight_dev_register(
const char *name, struct flashlight_operations *dev_ops);
int flashlight_dev_unregister(const char *name);
int flashlight_dev_register_by_device_id(
struct flashlight_device_id *dev_id,
struct flashlight_operations *dev_ops);
int flashlight_dev_unregister_by_device_id(struct flashlight_device_id *dev_id);
/* get id and index */
int flashlight_get_type_id(int type_index);
int flashlight_get_ct_id(int ct_index);
int flashlight_get_part_id(int part_index);
int flashlight_get_type_index(int type_id);
int flashlight_get_ct_index(int ct_id);
int flashlight_get_part_index(int part_id);
/* verify id and index */
int flashlight_verify_type_index(int type_index);
int flashlight_verify_ct_index(int ct_index);
int flashlight_verify_part_index(int part_index);
int flashlight_verify_index(int type_index, int ct_index, int part_index);
#ifdef CONFIG_MTK_FLASHLIGHT_PT
int flashlight_pt_is_low(void);
#endif
#ifdef CONFIG_MTK_FLASHLIGHT_DLPT
void flashlight_kicker_pbm(bool status);
#endif
#endif /* _FLASHLIGHT_CORE_H */
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "flashlight-core.h"
#if defined(mt6739)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-rt4505", 0, 0},
};
#elif defined(mt6757)
#if defined(evb6757p_dm_64) || defined(k57pv1_dm_64) || \
defined(k57pv1_64_baymo) || defined(k57pv1_dm_64_bif) || \
defined(k57pv1_dm_64_baymo) || defined(k57pv1_dm_teei_2g) || \
defined(k57pv1_dm_64_zoom)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-rt5081", 0, 0},
{0, 1, 0, "flashlights-rt5081", 1, 0},
};
#elif defined(CONFIG_MTK_FLASHLIGHT_RT5081)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-rt5081", 0, 0},
{0, 1, 0, "flashlights-rt5081", 1, 0},
};
#else
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-lm3643", 0, 0},
{0, 1, 0, "flashlights-lm3643", 1, 0},
};
#endif
#elif defined(mt6758)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-mt6370", 0, 0},
{0, 1, 0, "flashlights-mt6370", 1, 0},
};
#elif defined(mt6759)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-rt5081", 0, 0},
{0, 1, 0, "flashlights-rt5081", 1, 0},
};
#elif defined(mt6761)
#ifdef CONFIG_MTK_FLASHLIGHT_AW3644
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-aw3644", 0, 1},
{1, 0, 0, "flashlights-aw3644", 1, 1},
};
#else
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-none", -1, 0},
{0, 1, 0, "flashlights-none", -1, 0},
{1, 0, 0, "flashlights-none", -1, 0},
{1, 1, 0, "flashlights-none", -1, 0},
{0, 0, 1, "flashlights-none", -1, 0},
{0, 1, 1, "flashlights-none", -1, 0},
{1, 0, 1, "flashlights-none", -1, 0},
{1, 1, 1, "flashlights-none", -1, 0},
};
#endif
#elif defined(mt6763)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-mt6370", 0, 0},
{0, 1, 0, "flashlights-mt6370", 1, 0},
};
#elif defined(mt6768)
#if defined(CONFIG_MTK_FLASHLIGHT_OCP81375)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-ocp81375", 1, 1},
// {1, 0, 0, "flashlights-ocp81375", 0, 1},
};
#else
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-mt6370", 0, 0},
{0, 1, 0, "flashlights-mt6370", 1, 0},
};
#endif
#elif defined(mt6799)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-mt6336", 0, 0},
{0, 1, 0, "flashlights-mt6336", 1, 0},
};
#elif defined(mt8167)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-lm3642", 0, 0},
};
#elif defined(CONFIG_MTK_FLASHLIGHT_SYWT78)
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-sywt78", 0, 0},
};
#else
const struct flashlight_device_id flashlight_id[] = {
/* {TYPE, CT, PART, "NAME", CHANNEL, DECOUPLE} */
{0, 0, 0, "flashlights-none", -1, 0},
{0, 1, 0, "flashlights-none", -1, 0},
{1, 0, 0, "flashlights-none", -1, 0},
{1, 1, 0, "flashlights-none", -1, 0},
{0, 0, 1, "flashlights-none", -1, 0},
{0, 1, 1, "flashlights-none", -1, 0},
{1, 0, 1, "flashlights-none", -1, 0},
{1, 1, 1, "flashlights-none", -1, 0},
};
#endif
const int flashlight_device_num =
sizeof(flashlight_id) / sizeof(struct flashlight_device_id);
@@ -0,0 +1,29 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _FLASHLIGHT_DT_H
#define _FLASHLIGHT_DT_H
#define DUMMY_GPIO_DTNAME "mediatek,flashlights_dummy_gpio"
#define DUMMY_DTNAME "mediatek,flashlights_dummy"
#define DUMMY_DTNAME_I2C "mediatek,flashlights_dummy_i2c"
#define LED191_DTNAME "mediatek,flashlights_led191"
#define LM3642_DTNAME "mediatek,flashlights_lm3642"
#define LM3642_DTNAME_I2C "mediatek,strobe_main"
#define LM3643_DTNAME "mediatek,flashlights_lm3643"
#define LM3643_DTNAME_I2C "mediatek,strobe_main"
#define LM3644_DTNAME "mediatek,flashlights_lm3644"
#define LM3644_DTNAME_I2C "mediatek,strobe_main"
#define MT6336_DTNAME "mediatek,flashlights_mt6336"
#define MT6370_DTNAME "mediatek,flashlights_mt6370"
#define MT6360_DTNAME "mediatek,flashlights_mt6360"
#define RT4505_DTNAME "mediatek,flashlights_rt4505"
#define RT4505_DTNAME_I2C "mediatek,strobe_main"
#define RT5081_DTNAME "mediatek,flashlights_rt5081"
#define SYWT78_DTNAME "mediatek,flashlights_sywt78"
#define AW3644_DTNAME_I2C "mediatek,strobe_main"
#define AW3644_DTNAME "mediatek,flashlights_aw3644"
#endif /* _FLASHLIGHT_DT_H */
@@ -0,0 +1,111 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _FLASHLIGHT_H
#define _FLASHLIGHT_H
#include <linux/types.h>
#include <linux/ioctl.h>
/* device node and sysfs */
#define FLASHLIGHT_CORE "flashlight_core"
#define FLASHLIGHT_DEVNAME "flashlight"
#define FLASHLIGHT_SYSFS_STROBE "flashlight_strobe"
#define FLASHLIGHT_SYSFS_PT "flashlight_pt"
#define FLASHLIGHT_SYSFS_CHARGER "flashlight_charger"
#define FLASHLIGHT_SYSFS_CURRENT "flashlight_current"
#define FLASHLIGHT_SYSFS_CAPABILITY "flashlight_capability"
#define FLASHLIGHT_SYSFS_FAULT "flashlight_fault"
#define FLASHLIGHT_SYSFS_SW_DISABLE "flashlight_sw_disable"
/* scenario */
#define FLASHLIGHT_SCENARIO_CAMERA_MASK 1
#define FLASHLIGHT_SCENARIO_DECOUPLE_MASK 2
#define FLASHLIGHT_SCENARIO_KEEPSTATE_DECOUPLE_MASK 3
#define FLASHLIGHT_SCENARIO_FLASHLIGHT (0 << 0)
#define FLASHLIGHT_SCENARIO_CAMERA (1 << 0)
#define FLASHLIGHT_SCENARIO_COUPLE (0 << 1)
#define FLASHLIGHT_SCENARIO_DECOUPLE (1 << 1)
#define FLASHLIGHT_SCENARIO_KEEPSTATE_COUPLE (0 << 2)
#define FLASHLIGHT_SCENARIO_KEEPSTATE_DECOUPLE (1 << 2)
/* charger status */
#define FLASHLIGHT_CHARGER_NOT_READY 0
#define FLASHLIGHT_CHARGER_READY 1
/* sw disable status*/
#define FLASHLIGHT_SW_DISABLE_ON 1
#define FLASHLIGHT_SW_DISABLE_OFF 0
/* max duty number */
#define FLASHLIGHT_MAX_DUTY_NUM 40
/* flashlight arguments */
#define FLASHLIGHT_TYPE_MAX 2
#define FLASHLIGHT_CT_MAX 3
#define FLASHLIGHT_PART_MAX 2
struct flashlight_user_arg {
int type_id;
int ct_id;
int arg;
};
/* ioctl magic number */
#define FLASHLIGHT_MAGIC 'S'
/* ioctl protocol version 0. */
#define FLASHLIGHTIOC_T_ENABLE _IOW(FLASHLIGHT_MAGIC, 5, int)
#define FLASHLIGHTIOC_T_LEVEL _IOW(FLASHLIGHT_MAGIC, 10, int)
#define FLASHLIGHTIOC_T_FLASHTIME _IOW(FLASHLIGHT_MAGIC, 15, int)
#define FLASHLIGHTIOC_T_STATE _IOW(FLASHLIGHT_MAGIC, 20, int)
#define FLASHLIGHTIOC_G_FLASHTYPE _IOR(FLASHLIGHT_MAGIC, 25, int)
#define FLASHLIGHTIOC_X_SET_DRIVER _IOWR(FLASHLIGHT_MAGIC, 30, int)
#define FLASHLIGHTIOC_T_DELAY _IOW(FLASHLIGHT_MAGIC, 35, int)
/* ioctl protocol version 1. */
#define FLASH_IOC_SET_TIME_OUT_TIME_MS _IOR(FLASHLIGHT_MAGIC, 100, int)
#define FLASH_IOC_SET_STEP _IOR(FLASHLIGHT_MAGIC, 105, int)
#define FLASH_IOC_SET_DUTY _IOR(FLASHLIGHT_MAGIC, 110, int)
#define FLASH_IOC_SET_ONOFF _IOR(FLASHLIGHT_MAGIC, 115, int)
#define FLASH_IOC_UNINIT _IOR(FLASHLIGHT_MAGIC, 120, int)
#define FLASH_IOC_PRE_ON _IOR(FLASHLIGHT_MAGIC, 125, int)
#define FLASH_IOC_GET_PRE_ON_TIME_MS _IOR(FLASHLIGHT_MAGIC, 130, int)
#define FLASH_IOC_GET_PRE_ON_TIME_MS_DUTY _IOR(FLASHLIGHT_MAGIC, 131, int)
#define FLASH_IOC_SET_REG_ADR _IOR(FLASHLIGHT_MAGIC, 135, int)
#define FLASH_IOC_SET_REG_VAL _IOR(FLASHLIGHT_MAGIC, 140, int)
#define FLASH_IOC_SET_REG _IOR(FLASHLIGHT_MAGIC, 145, int)
#define FLASH_IOC_GET_REG _IOR(FLASHLIGHT_MAGIC, 150, int)
#define FLASH_IOC_GET_MAIN_PART_ID _IOR(FLASHLIGHT_MAGIC, 155, int)
#define FLASH_IOC_GET_SUB_PART_ID _IOR(FLASHLIGHT_MAGIC, 160, int)
#define FLASH_IOC_GET_MAIN2_PART_ID _IOR(FLASHLIGHT_MAGIC, 165, int)
#define FLASH_IOC_GET_PART_ID _IOR(FLASHLIGHT_MAGIC, 166, int)
#define FLASH_IOC_HAS_LOW_POWER_DETECT _IOR(FLASHLIGHT_MAGIC, 170, int)
#define FLASH_IOC_LOW_POWER_DETECT_START _IOR(FLASHLIGHT_MAGIC, 175, int)
#define FLASH_IOC_LOW_POWER_DETECT_END _IOR(FLASHLIGHT_MAGIC, 180, int)
#define FLASH_IOC_IS_LOW_POWER _IOR(FLASHLIGHT_MAGIC, 182, int)
#define FLASH_IOC_GET_ERR _IOR(FLASHLIGHT_MAGIC, 185, int)
#define FLASH_IOC_GET_PROTOCOL_VERSION _IOR(FLASHLIGHT_MAGIC, 190, int)
#define FLASH_IOC_IS_CHARGER_IN _IOR(FLASHLIGHT_MAGIC, 195, int)
#define FLASH_IOC_IS_OTG_USE _IOR(FLASHLIGHT_MAGIC, 200, int)
#define FLASH_IOC_GET_FLASH_DRIVER_NAME_ID _IOR(FLASHLIGHT_MAGIC, 205, int)
/* ioctl protocol version 2 */
#define FLASH_IOC_IS_CHARGER_READY _IOR(FLASHLIGHT_MAGIC, 210, int)
#define FLASH_IOC_SET_SCENARIO _IOWR(FLASHLIGHT_MAGIC, 215, int)
#define FLASH_IOC_IS_HARDWARE_READY _IOR(FLASHLIGHT_MAGIC, 220, int)
#define FLASH_IOC_GET_DUTY_NUMBER _IOWR(FLASHLIGHT_MAGIC, 225, int)
#define FLASH_IOC_GET_MAX_TORCH_DUTY _IOWR(FLASHLIGHT_MAGIC, 230, int)
#define FLASH_IOC_GET_DUTY_CURRENT _IOWR(FLASHLIGHT_MAGIC, 235, int)
#define FLASH_IOC_GET_HW_TIMEOUT _IOWR(FLASHLIGHT_MAGIC, 240, int)
#define FLASH_IOC_GET_HW_FAULT _IOR(FLASHLIGHT_MAGIC, 250, int)
#define FLASH_IOC_GET_HW_FAULT2 _IOR(FLASHLIGHT_MAGIC, 251, int)
#endif /* _FLASHLIGHT_H */
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,535 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/pinctrl/consumer.h>
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* define device tree */
/* TODO: modify temp device tree name */
#ifndef DUMMY_GPIO_DTNAME
#define DUMMY_GPIO_DTNAME "mediatek,flashlights_dummy_gpio"
#endif
/* TODO: define driver name */
#define DUMMY_NAME "flashlights-dummy-gpio"
/* define registers */
/* TODO: define register */
/* define mutex and work queue */
static DEFINE_MUTEX(dummy_mutex);
static struct work_struct dummy_work;
/* define pinctrl */
/* TODO: define pinctrl */
#define DUMMY_PINCTRL_PIN_XXX 0
#define DUMMY_PINCTRL_PINSTATE_LOW 0
#define DUMMY_PINCTRL_PINSTATE_HIGH 1
#define DUMMY_PINCTRL_STATE_XXX_HIGH "xxx_high"
#define DUMMY_PINCTRL_STATE_XXX_LOW "xxx_low"
static struct pinctrl *dummy_pinctrl;
static struct pinctrl_state *dummy_xxx_high;
static struct pinctrl_state *dummy_xxx_low;
/* define usage count */
static int use_count;
/* platform data */
struct dummy_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/******************************************************************************
* Pinctrl configuration
*****************************************************************************/
static int dummy_pinctrl_init(struct platform_device *pdev)
{
int ret = 0;
/* get pinctrl */
dummy_pinctrl = devm_pinctrl_get(&pdev->dev);
if (IS_ERR(dummy_pinctrl)) {
pr_info("Failed to get flashlight pinctrl.\n");
ret = PTR_ERR(dummy_pinctrl);
return ret;
}
/* TODO: Flashlight XXX pin initialization */
dummy_xxx_high = pinctrl_lookup_state(
dummy_pinctrl, DUMMY_PINCTRL_STATE_XXX_HIGH);
if (IS_ERR(dummy_xxx_high)) {
pr_info("Failed to init (%s)\n", DUMMY_PINCTRL_STATE_XXX_HIGH);
ret = PTR_ERR(dummy_xxx_high);
}
dummy_xxx_low = pinctrl_lookup_state(
dummy_pinctrl, DUMMY_PINCTRL_STATE_XXX_LOW);
if (IS_ERR(dummy_xxx_low)) {
pr_info("Failed to init (%s)\n", DUMMY_PINCTRL_STATE_XXX_LOW);
ret = PTR_ERR(dummy_xxx_low);
}
return ret;
}
static int dummy_pinctrl_set(int pin, int state)
{
int ret = 0;
if (IS_ERR(dummy_pinctrl)) {
pr_info("pinctrl is not available\n");
return -1;
}
switch (pin) {
case DUMMY_PINCTRL_PIN_XXX:
if (state == DUMMY_PINCTRL_PINSTATE_LOW &&
!IS_ERR(dummy_xxx_low))
pinctrl_select_state(dummy_pinctrl, dummy_xxx_low);
else if (state == DUMMY_PINCTRL_PINSTATE_HIGH &&
!IS_ERR(dummy_xxx_high))
pinctrl_select_state(dummy_pinctrl, dummy_xxx_high);
else
pr_info("set err, pin(%d) state(%d)\n", pin, state);
break;
default:
pr_info("set err, pin(%d) state(%d)\n", pin, state);
break;
}
pr_debug("pin(%d) state(%d)\n", pin, state);
return ret;
}
/******************************************************************************
* dummy operations
*****************************************************************************/
/* flashlight enable function */
static int dummy_enable(void)
{
int pin = 0, state = 0;
/* TODO: wrap enable function */
return dummy_pinctrl_set(pin, state);
}
/* flashlight disable function */
static int dummy_disable(void)
{
int pin = 0, state = 0;
/* TODO: wrap disable function */
return dummy_pinctrl_set(pin, state);
}
/* set flashlight level */
static int dummy_set_level(int level)
{
int pin = 0, state = 0;
/* TODO: wrap set level function */
return dummy_pinctrl_set(pin, state);
}
/* flashlight init */
static int dummy_init(void)
{
int pin = 0, state = 0;
/* TODO: wrap init function */
return dummy_pinctrl_set(pin, state);
}
/* flashlight uninit */
static int dummy_uninit(void)
{
int pin = 0, state = 0;
/* TODO: wrap uninit function */
return dummy_pinctrl_set(pin, state);
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static struct hrtimer dummy_timer;
static unsigned int dummy_timeout_ms;
static void dummy_work_disable(struct work_struct *data)
{
pr_debug("work queue callback\n");
dummy_disable();
}
static enum hrtimer_restart dummy_timer_func(struct hrtimer *timer)
{
schedule_work(&dummy_work);
return HRTIMER_NORESTART;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int dummy_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
ktime_t ktime;
unsigned int s;
unsigned int ns;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
dummy_timeout_ms = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
dummy_set_level(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
if (fl_arg->arg == 1) {
if (dummy_timeout_ms) {
s = dummy_timeout_ms / 1000;
ns = dummy_timeout_ms % 1000 * 1000000;
ktime = ktime_set(s, ns);
hrtimer_start(&dummy_timer, ktime,
HRTIMER_MODE_REL);
}
dummy_enable();
} else {
dummy_disable();
hrtimer_cancel(&dummy_timer);
}
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int dummy_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int dummy_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int dummy_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&dummy_mutex);
if (set) {
if (!use_count)
ret = dummy_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = dummy_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&dummy_mutex);
return ret;
}
static ssize_t dummy_strobe_store(struct flashlight_arg arg)
{
dummy_set_driver(1);
dummy_set_level(arg.level);
dummy_timeout_ms = 0;
dummy_enable();
msleep(arg.dur);
dummy_disable();
dummy_set_driver(0);
return 0;
}
static struct flashlight_operations dummy_ops = {
dummy_open,
dummy_release,
dummy_ioctl,
dummy_strobe_store,
dummy_set_driver
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int dummy_chip_init(void)
{
/* NOTE: Chip initialication move to "set driver" for power saving.
* dummy_init();
*/
return 0;
}
static int dummy_parse_dt(struct device *dev,
struct dummy_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
DUMMY_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int dummy_probe(struct platform_device *pdev)
{
struct dummy_platform_data *pdata = dev_get_platdata(&pdev->dev);
int err;
int i;
pr_debug("Probe start.\n");
/* init pinctrl */
if (dummy_pinctrl_init(pdev)) {
pr_debug("Failed to init pinctrl.\n");
err = -EFAULT;
goto err;
}
/* init platform data */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) {
err = -ENOMEM;
goto err;
}
pdev->dev.platform_data = pdata;
err = dummy_parse_dt(&pdev->dev, pdata);
if (err)
goto err;
}
/* init work queue */
INIT_WORK(&dummy_work, dummy_work_disable);
/* init timer */
hrtimer_init(&dummy_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
dummy_timer.function = dummy_timer_func;
dummy_timeout_ms = 100;
/* init chip hw */
dummy_chip_init();
/* clear usage count */
use_count = 0;
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&dummy_ops)) {
err = -EFAULT;
goto err;
}
} else {
if (flashlight_dev_register(DUMMY_NAME, &dummy_ops)) {
err = -EFAULT;
goto err;
}
}
pr_debug("Probe done.\n");
return 0;
err:
return err;
}
static int dummy_remove(struct platform_device *pdev)
{
struct dummy_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(DUMMY_NAME);
/* flush work queue */
flush_work(&dummy_work);
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id dummy_gpio_of_match[] = {
{.compatible = DUMMY_GPIO_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, dummy_gpio_of_match);
#else
static struct platform_device dummy_gpio_platform_device[] = {
{
.name = DUMMY_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, dummy_gpio_platform_device);
#endif
static struct platform_driver dummy_platform_driver = {
.probe = dummy_probe,
.remove = dummy_remove,
.driver = {
.name = DUMMY_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = dummy_gpio_of_match,
#endif
},
};
static int __init flashlight_dummy_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&dummy_gpio_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&dummy_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_dummy_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&dummy_platform_driver);
pr_debug("Exit done.\n");
}
module_init(flashlight_dummy_init);
module_exit(flashlight_dummy_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight DUMMY GPIO Driver");
@@ -0,0 +1,679 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* define device tree */
/* TODO: modify temp device tree name */
#ifndef DUMMY_DTNAME
#define DUMMY_DTNAME "mediatek,flashlights_dummy"
#endif
#ifndef DUMMY_DTNAME_I2C
#define DUMMY_DTNAME_I2C "mediatek,flashlights_dummy_i2c"
#endif
/* TODO: define driver name */
#define DUMMY_NAME "flashlights-dummy"
/* define registers */
/* TODO: define register */
/* define level */
/* TODO: define brightness level and hardware timeout */
#define DUMMY_LEVEL_NUM 2
#define DUMMY_LEVEL_TORCH 1
#define DUMMY_HW_TIMEOUT 100 /* ms */
/* define mutex and work queue */
static DEFINE_MUTEX(dummy_mutex);
static struct work_struct dummy_work;
/* define usage count */
static int use_count;
/* define i2c */
static struct i2c_client *dummy_i2c_client;
/* platform data */
struct dummy_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/* dummy chip data */
struct dummy_chip_data {
struct i2c_client *client;
struct dummy_platform_data *pdata;
struct mutex lock;
};
/******************************************************************************
* dummy operations
*****************************************************************************/
static const int dummy_current[DUMMY_LEVEL_NUM] = {
/* TODO: define current */
100, 1000
};
static const unsigned char dummy_flash_level[DUMMY_LEVEL_NUM] = {
/* TODO: define register value */
0x00, 0x01
};
static int dummy_level = -1;
static int dummy_is_torch(int level)
{
if (level >= DUMMY_LEVEL_TORCH)
return -1;
return 0;
}
static int dummy_verify_level(int level)
{
if (level < 0)
level = 0;
else if (level >= DUMMY_LEVEL_NUM)
level = DUMMY_LEVEL_NUM - 1;
return level;
}
/* i2c wrapper function */
static int dummy_write_reg(struct i2c_client *client, u8 reg, u8 val)
{
int ret;
struct dummy_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
ret = i2c_smbus_write_byte_data(client, reg, val);
mutex_unlock(&chip->lock);
if (ret < 0)
pr_info("failed writing at 0x%02x\n", reg);
return ret;
}
static int dummy_read_reg(struct i2c_client *client, u8 reg)
{
int val;
struct dummy_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
val = i2c_smbus_read_byte_data(client, reg);
mutex_unlock(&chip->lock);
return val;
}
/* flashlight enable function */
static int dummy_enable(void)
{
unsigned char reg = 0, val = 0;
/* TODO: wrap enable function */
if (!dummy_is_torch(dummy_level)) {
/* torch mode */
;
} else {
/* flash mode */
;
}
return dummy_write_reg(dummy_i2c_client, reg, val);
}
/* flashlight disable function */
static int dummy_disable(void)
{
unsigned char reg = 0, val = 0;
/* TODO: wrap disable function */
return dummy_write_reg(dummy_i2c_client, reg, val);
}
/* set flashlight level */
static int dummy_set_level(int level)
{
unsigned char reg = 0, val = 0;
/* TODO: wrap set level function */
return dummy_write_reg(dummy_i2c_client, reg, val);
}
static int dummy_get_hw_fault(int num)
{
unsigned char reg = 0;
if (num == 1)
return dummy_read_reg(dummy_i2c_client, reg);
else if (num == 2)
return dummy_read_reg(dummy_i2c_client, reg);
pr_info("Error num\n");
return 0;
}
/* flashlight init */
static int dummy_init(void)
{
unsigned char reg = 0, val = 0;
/* TODO: wrap init function */
return dummy_write_reg(dummy_i2c_client, reg, val);
}
/* flashlight uninit */
static int dummy_uninit(void)
{
unsigned char reg = 0, val = 0;
/* TODO: wrap uninit function */
return dummy_write_reg(dummy_i2c_client, reg, val);
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static struct hrtimer dummy_timer;
static unsigned int dummy_timeout_ms;
static void dummy_work_disable(struct work_struct *data)
{
pr_debug("work queue callback\n");
dummy_disable();
}
static enum hrtimer_restart dummy_timer_func(struct hrtimer *timer)
{
schedule_work(&dummy_work);
return HRTIMER_NORESTART;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int dummy_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
ktime_t ktime;
unsigned int s;
unsigned int ns;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
dummy_timeout_ms = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
dummy_set_level(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
if (fl_arg->arg == 1) {
if (dummy_timeout_ms) {
s = dummy_timeout_ms / 1000;
ns = dummy_timeout_ms % 1000 * 1000000;
ktime = ktime_set(s, ns);
hrtimer_start(&dummy_timer, ktime,
HRTIMER_MODE_REL);
}
dummy_enable();
} else {
dummy_disable();
hrtimer_cancel(&dummy_timer);
}
break;
case FLASH_IOC_GET_DUTY_NUMBER:
pr_debug("FLASH_IOC_GET_DUTY_NUMBER(%d)\n", channel);
fl_arg->arg = DUMMY_LEVEL_NUM;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_debug("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n", channel);
fl_arg->arg = DUMMY_LEVEL_TORCH - 1;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = dummy_verify_level(fl_arg->arg);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel, (int)fl_arg->arg);
fl_arg->arg = dummy_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_TIMEOUT:
pr_debug("FLASH_IOC_GET_HW_TIMEOUT(%d)\n", channel);
fl_arg->arg = DUMMY_HW_TIMEOUT;
break;
case FLASH_IOC_GET_HW_FAULT:
pr_debug("FLASH_IOC_GET_HW_FAULT(%d)\n", channel);
fl_arg->arg = dummy_get_hw_fault(1);
break;
case FLASH_IOC_GET_HW_FAULT2:
pr_debug("FLASH_IOC_GET_HW_FAULT2(%d)\n", channel);
fl_arg->arg = dummy_get_hw_fault(2);
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int dummy_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int dummy_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int dummy_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&dummy_mutex);
if (set) {
if (!use_count)
ret = dummy_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = dummy_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&dummy_mutex);
return ret;
}
static ssize_t dummy_strobe_store(struct flashlight_arg arg)
{
dummy_set_driver(1);
dummy_set_level(arg.level);
dummy_timeout_ms = 0;
dummy_enable();
msleep(arg.dur);
dummy_disable();
dummy_set_driver(0);
return 0;
}
static struct flashlight_operations dummy_ops = {
dummy_open,
dummy_release,
dummy_ioctl,
dummy_strobe_store,
dummy_set_driver
};
/******************************************************************************
* I2C device and driver
*****************************************************************************/
static int dummy_chip_init(struct dummy_chip_data *chip)
{
/* NOTE: Chip initialication move to "set driver" for power saving.
* dummy_init();
*/
return 0;
}
static int dummy_parse_dt(struct device *dev,
struct dummy_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
DUMMY_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int dummy_i2c_probe(
struct i2c_client *client, const struct i2c_device_id *id)
{
struct dummy_chip_data *chip;
int err;
pr_debug("Probe start.\n");
/* check i2c */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_info("Failed to check i2c functionality.\n");
err = -ENODEV;
goto err_out;
}
/* init chip private data */
chip = kzalloc(sizeof(struct dummy_chip_data), GFP_KERNEL);
if (!chip) {
err = -ENOMEM;
goto err_out;
}
chip->client = client;
i2c_set_clientdata(client, chip);
dummy_i2c_client = client;
/* init mutex and spinlock */
mutex_init(&chip->lock);
/* init chip hw */
dummy_chip_init(chip);
pr_debug("Probe done.\n");
return 0;
err_out:
return err;
}
static int dummy_i2c_remove(struct i2c_client *client)
{
struct dummy_chip_data *chip = i2c_get_clientdata(client);
pr_debug("Remove start.\n");
client->dev.platform_data = NULL;
/* free resource */
kfree(chip);
pr_debug("Remove done.\n");
return 0;
}
static const struct i2c_device_id dummy_i2c_id[] = {
{DUMMY_NAME, 0},
{}
};
MODULE_DEVICE_TABLE(i2c, dummy_i2c_id);
#ifdef CONFIG_OF
static const struct of_device_id dummy_i2c_of_match[] = {
{.compatible = DUMMY_DTNAME_I2C},
{},
};
MODULE_DEVICE_TABLE(of, dummy_i2c_of_match);
#endif
static struct i2c_driver dummy_i2c_driver = {
.driver = {
.name = DUMMY_NAME,
#ifdef CONFIG_OF
.of_match_table = dummy_i2c_of_match,
#endif
},
.probe = dummy_i2c_probe,
.remove = dummy_i2c_remove,
.id_table = dummy_i2c_id,
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int dummy_probe(struct platform_device *pdev)
{
struct dummy_platform_data *pdata = dev_get_platdata(&pdev->dev);
struct dummy_chip_data *chip = NULL;
int err;
int i;
pr_debug("Probe start.\n");
if (i2c_add_driver(&dummy_i2c_driver)) {
pr_debug("Failed to add i2c driver.\n");
return -1;
}
/* init platform data */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) {
err = -ENOMEM;
goto err_free;
}
pdev->dev.platform_data = pdata;
err = dummy_parse_dt(&pdev->dev, pdata);
if (err)
goto err_free;
}
/* init work queue */
INIT_WORK(&dummy_work, dummy_work_disable);
/* init timer */
hrtimer_init(&dummy_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
dummy_timer.function = dummy_timer_func;
dummy_timeout_ms = 100;
/* clear usage count */
use_count = 0;
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&dummy_ops)) {
err = -EFAULT;
goto err_free;
}
} else {
if (flashlight_dev_register(DUMMY_NAME, &dummy_ops)) {
err = -EFAULT;
goto err_free;
}
}
pr_debug("Probe done.\n");
return 0;
err_free:
chip = i2c_get_clientdata(dummy_i2c_client);
i2c_set_clientdata(dummy_i2c_client, NULL);
kfree(chip);
return err;
}
static int dummy_remove(struct platform_device *pdev)
{
struct dummy_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
i2c_del_driver(&dummy_i2c_driver);
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(DUMMY_NAME);
/* flush work queue */
flush_work(&dummy_work);
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id dummy_of_match[] = {
{.compatible = DUMMY_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, dummy_of_match);
#else
static struct platform_device dummy_platform_device[] = {
{
.name = DUMMY_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, dummy_platform_device);
#endif
static struct platform_driver dummy_platform_driver = {
.probe = dummy_probe,
.remove = dummy_remove,
.driver = {
.name = DUMMY_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = dummy_of_match,
#endif
},
};
static int __init flashlight_dummy_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&dummy_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&dummy_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_dummy_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&dummy_platform_driver);
pr_debug("Exit done.\n");
}
module_init(flashlight_dummy_init);
module_exit(flashlight_dummy_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight DUMMY Driver");
@@ -0,0 +1,535 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/pinctrl/consumer.h>
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* define device tree */
#ifndef LED191_DTNAME
#define LED191_DTNAME "mediatek,flashlights_led191"
#endif
#define LED191_NAME "flashlights_led191"
/* define registers */
/* define mutex and work queue */
static DEFINE_MUTEX(led191_mutex);
static struct work_struct led191_work;
/* define pinctrl */
#define LED191_PINCTRL_PIN_HWEN 0
#define LED191_PINCTRL_PINSTATE_LOW 0
#define LED191_PINCTRL_PINSTATE_HIGH 1
#define LED191_PINCTRL_STATE_HWEN_HIGH "hwen_high"
#define LED191_PINCTRL_STATE_HWEN_LOW "hwen_low"
static struct pinctrl *led191_pinctrl;
static struct pinctrl_state *led191_hwen_high;
static struct pinctrl_state *led191_hwen_low;
/* define usage count */
static int use_count;
static int g_flash_duty = -1;
/* platform data */
struct led191_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/******************************************************************************
* Pinctrl configuration
*****************************************************************************/
static int led191_pinctrl_init(struct platform_device *pdev)
{
int ret = 0;
/* get pinctrl */
led191_pinctrl = devm_pinctrl_get(&pdev->dev);
if (IS_ERR(led191_pinctrl)) {
pr_info("Failed to get flashlight pinctrl.\n");
ret = PTR_ERR(led191_pinctrl);
}
/* Flashlight pin initialization */
led191_hwen_high = pinctrl_lookup_state(
led191_pinctrl, LED191_PINCTRL_STATE_HWEN_HIGH);
if (IS_ERR(led191_hwen_high)) {
pr_info("Failed to init (%s)\n",
LED191_PINCTRL_STATE_HWEN_HIGH);
ret = PTR_ERR(led191_hwen_high);
}
led191_hwen_low = pinctrl_lookup_state(
led191_pinctrl, LED191_PINCTRL_STATE_HWEN_LOW);
if (IS_ERR(led191_hwen_low)) {
pr_info("Failed to init (%s)\n", LED191_PINCTRL_STATE_HWEN_LOW);
ret = PTR_ERR(led191_hwen_low);
}
return ret;
}
static int led191_pinctrl_set(int pin, int state)
{
int ret = 0;
if (IS_ERR(led191_pinctrl)) {
pr_info("pinctrl is not available\n");
return -1;
}
switch (pin) {
case LED191_PINCTRL_PIN_HWEN:
if (state == LED191_PINCTRL_PINSTATE_LOW &&
!IS_ERR(led191_hwen_low))
ret = pinctrl_select_state(
led191_pinctrl, led191_hwen_low);
else if (state == LED191_PINCTRL_PINSTATE_HIGH &&
!IS_ERR(led191_hwen_high))
ret = pinctrl_select_state(
led191_pinctrl, led191_hwen_high);
else
pr_info("set err, pin(%d) state(%d)\n", pin, state);
break;
default:
pr_info("set err, pin(%d) state(%d)\n", pin, state);
break;
}
pr_debug("pin(%d) state(%d), ret:%d\n", pin, state, ret);
return ret;
}
/******************************************************************************
* led191 operations
*****************************************************************************/
/* flashlight enable function */
static int led191_enable(void)
{
int pin = LED191_PINCTRL_PIN_HWEN;
if (g_flash_duty == 1) {
led191_pinctrl_set(pin, 1);
} else {
led191_pinctrl_set(pin, 1);
led191_pinctrl_set(pin, 0);
}
led191_pinctrl_set(pin, 1);
return 0;
}
/* flashlight disable function */
static int led191_disable(void)
{
int pin = 0;
int state = 0;
return led191_pinctrl_set(pin, state);
}
/* set flashlight level */
static int led191_set_level(int level)
{
g_flash_duty = level;
return 0;
}
/* flashlight init */
static int led191_init(void)
{
int pin = 0;
int state = 0;
return led191_pinctrl_set(pin, state);
}
/* flashlight uninit */
static int led191_uninit(void)
{
int pin = 0;
int state = 0;
return led191_pinctrl_set(pin, state);
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static struct hrtimer led191_timer;
static unsigned int led191_timeout_ms;
static void led191_work_disable(struct work_struct *data)
{
pr_debug("work queue callback\n");
led191_disable();
}
static enum hrtimer_restart led191_timer_func(struct hrtimer *timer)
{
schedule_work(&led191_work);
return HRTIMER_NORESTART;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int led191_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
ktime_t ktime;
unsigned int s;
unsigned int ns;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
led191_timeout_ms = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
led191_set_level(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
if (fl_arg->arg == 1) {
if (led191_timeout_ms) {
s = led191_timeout_ms / 1000;
ns = led191_timeout_ms % 1000 * 1000000;
ktime = ktime_set(s, ns);
hrtimer_start(&led191_timer, ktime,
HRTIMER_MODE_REL);
}
led191_enable();
} else {
led191_disable();
hrtimer_cancel(&led191_timer);
}
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int led191_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int led191_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int led191_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&led191_mutex);
if (set) {
if (!use_count)
ret = led191_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = led191_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&led191_mutex);
return ret;
}
static ssize_t led191_strobe_store(struct flashlight_arg arg)
{
led191_set_driver(1);
led191_set_level(arg.level);
led191_timeout_ms = 0;
led191_enable();
msleep(arg.dur);
led191_disable();
led191_set_driver(0);
return 0;
}
static struct flashlight_operations led191_ops = {
led191_open,
led191_release,
led191_ioctl,
led191_strobe_store,
led191_set_driver
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int led191_chip_init(void)
{
/* NOTE: Chip initialication move to "set driver" for power saving.
* led191_init();
*/
return 0;
}
static int led191_parse_dt(struct device *dev,
struct led191_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
LED191_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int led191_probe(struct platform_device *pdev)
{
struct led191_platform_data *pdata = dev_get_platdata(&pdev->dev);
int err;
int i;
pr_debug("Probe start.\n");
/* init pinctrl */
if (led191_pinctrl_init(pdev)) {
pr_debug("Failed to init pinctrl.\n");
err = -EFAULT;
goto err;
}
/* init platform data */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) {
err = -ENOMEM;
goto err;
}
pdev->dev.platform_data = pdata;
err = led191_parse_dt(&pdev->dev, pdata);
if (err)
goto err;
}
/* init work queue */
INIT_WORK(&led191_work, led191_work_disable);
/* init timer */
hrtimer_init(&led191_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
led191_timer.function = led191_timer_func;
led191_timeout_ms = 100;
/* init chip hw */
led191_chip_init();
/* clear usage count */
use_count = 0;
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&led191_ops)) {
err = -EFAULT;
goto err;
}
} else {
if (flashlight_dev_register(LED191_NAME, &led191_ops)) {
err = -EFAULT;
goto err;
}
}
pr_debug("Probe done.\n");
return 0;
err:
return err;
}
static int led191_remove(struct platform_device *pdev)
{
struct led191_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(LED191_NAME);
/* flush work queue */
flush_work(&led191_work);
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id led191_gpio_of_match[] = {
{.compatible = LED191_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, led191_gpio_of_match);
#else
static struct platform_device led191_gpio_platform_device[] = {
{
.name = LED191_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, led191_gpio_platform_device);
#endif
static struct platform_driver led191_platform_driver = {
.probe = led191_probe,
.remove = led191_remove,
.driver = {
.name = LED191_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = led191_gpio_of_match,
#endif
},
};
static int __init flashlight_led191_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&led191_gpio_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&led191_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_led191_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&led191_platform_driver);
pr_debug("Exit done.\n");
}
module_init(flashlight_led191_init);
module_exit(flashlight_led191_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight LED191 GPIO Driver");
@@ -0,0 +1,704 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* device tree should be defined in flashlight-dt.h */
#ifndef LM3642_DTNAME
#define LM3642_DTNAME "mediatek,flashlights_lm3642"
#endif
#ifndef LM3642_DTNAME_I2C
#define LM3642_DTNAME_I2C "mediatek,flashlights_lm3642_i2c"
#endif
#define LM3642_NAME "flashlights-lm3642"
/* define registers */
#define LM3642_REG_SILICON_REVISION (0x00)
#define LM3642_REG_FLASH_FEATURE (0x08)
#define LM3642_INDUCTOR_CURRENT_LIMIT (0x40)
#define LM3642_FLASH_RAMP_TIME (0x00)
#define LM3642_FLASH_TIMEOUT (0x07)
#define LM3642_REG_CURRENT_CONTROL (0x09)
#define LM3642_REG_ENABLE (0x0A)
#define LM3642_ENABLE_STANDBY (0x00)
#define LM3642_ENABLE_TORCH (0x02)
#define LM3642_ENABLE_FLASH (0x03)
#define LM3642_REG_FLAG (0x0B)
/* define level */
#define LM3642_LEVEL_NUM 18
#define LM3642_LEVEL_TORCH 4
#define LM3642_HW_TIMEOUT 800 /* ms */
/* define mutex and work queue */
static DEFINE_MUTEX(lm3642_mutex);
static struct work_struct lm3642_work;
/* lm3642 revision */
static int is_lm3642lt;
/* define usage count */
static int use_count;
/* define i2c */
static struct i2c_client *lm3642_i2c_client;
/* platform data */
struct lm3642_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/* lm3642 chip data */
struct lm3642_chip_data {
struct i2c_client *client;
struct lm3642_platform_data *pdata;
struct mutex lock;
};
/******************************************************************************
* lm3642 operations
*****************************************************************************/
static const int lm3642_current[LM3642_LEVEL_NUM] = {
48, 93, 141, 188, 281, 375, 469, 563, 656, 750,
844, 938, 1031, 1125, 1219, 1313, 1406, 1500
};
static const unsigned char lm3642_flash_level[LM3642_LEVEL_NUM] = {
0x00, 0x10, 0x20, 0x30, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};
static const unsigned char lm3642lt_flash_level[LM3642_LEVEL_NUM] = {
0x10, 0x30, 0x50, 0x70, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};
static int lm3642_level = -1;
static int lm3642_is_torch(int level)
{
if (level >= LM3642_LEVEL_TORCH)
return -1;
return 0;
}
static int lm3642_verify_level(int level)
{
if (level < 0)
level = 0;
else if (level >= LM3642_LEVEL_NUM)
level = LM3642_LEVEL_NUM - 1;
return level;
}
/* i2c wrapper function */
static int lm3642_write_reg(struct i2c_client *client, u8 reg, u8 val)
{
int ret;
struct lm3642_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
ret = i2c_smbus_write_byte_data(client, reg, val);
mutex_unlock(&chip->lock);
if (ret < 0)
pr_info("failed writing at 0x%02x\n", reg);
return ret;
}
static int lm3642_read_reg(struct i2c_client *client, u8 reg)
{
int val = 0;
struct lm3642_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
val = i2c_smbus_read_byte_data(client, reg);
mutex_unlock(&chip->lock);
return val;
}
/* flashlight enable function */
static int lm3642_enable(void)
{
unsigned char reg, val;
reg = LM3642_REG_ENABLE;
if (!lm3642_is_torch(lm3642_level)) {
/* torch mode */
val = LM3642_ENABLE_TORCH;
} else {
/* flash mode */
val = LM3642_ENABLE_FLASH;
}
return lm3642_write_reg(lm3642_i2c_client, reg, val);
}
/* flashlight disable function */
static int lm3642_disable(void)
{
unsigned char reg, val;
reg = LM3642_REG_ENABLE;
val = LM3642_ENABLE_STANDBY;
return lm3642_write_reg(lm3642_i2c_client, reg, val);
}
/* set flashlight level */
static int lm3642_set_level(int level)
{
unsigned char reg, val;
level = lm3642_verify_level(level);
lm3642_level = level;
reg = LM3642_REG_CURRENT_CONTROL;
if (is_lm3642lt)
val = lm3642lt_flash_level[level];
else
val = lm3642_flash_level[level];
return lm3642_write_reg(lm3642_i2c_client, reg, val);
}
static int lm3642_get_flag(void)
{
return lm3642_read_reg(lm3642_i2c_client, LM3642_REG_FLAG);
}
/* flashlight init */
int lm3642_init(void)
{
int ret;
/* get silicon revision */
is_lm3642lt = lm3642_read_reg(
lm3642_i2c_client, LM3642_REG_SILICON_REVISION);
pr_info("LM3642(LT) revision(%d).\n", is_lm3642lt);
/* disable */
ret = lm3642_write_reg(lm3642_i2c_client, LM3642_REG_ENABLE,
LM3642_ENABLE_STANDBY);
/* set flash ramp time and timeout */
ret = lm3642_write_reg(lm3642_i2c_client, LM3642_REG_FLASH_FEATURE,
LM3642_INDUCTOR_CURRENT_LIMIT |
LM3642_FLASH_RAMP_TIME |
LM3642_FLASH_TIMEOUT);
return ret;
}
/* flashlight uninit */
int lm3642_uninit(void)
{
lm3642_disable();
return 0;
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static struct hrtimer lm3642_timer;
static unsigned int lm3642_timeout_ms;
static void lm3642_work_disable(struct work_struct *data)
{
pr_debug("work queue callback\n");
lm3642_disable();
}
static enum hrtimer_restart lm3642_timer_func(struct hrtimer *timer)
{
schedule_work(&lm3642_work);
return HRTIMER_NORESTART;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int lm3642_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
ktime_t ktime;
unsigned int s;
unsigned int ns;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
lm3642_timeout_ms = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
lm3642_set_level(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
if (fl_arg->arg == 1) {
if (lm3642_timeout_ms) {
s = lm3642_timeout_ms / 1000;
ns = lm3642_timeout_ms % 1000 * 1000000;
ktime = ktime_set(s, ns);
hrtimer_start(&lm3642_timer, ktime,
HRTIMER_MODE_REL);
}
lm3642_enable();
} else {
lm3642_disable();
hrtimer_cancel(&lm3642_timer);
}
break;
case FLASH_IOC_GET_DUTY_NUMBER:
pr_debug("FLASH_IOC_GET_DUTY_NUMBER(%d)\n", channel);
fl_arg->arg = LM3642_LEVEL_NUM;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_debug("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n", channel);
fl_arg->arg = LM3642_LEVEL_TORCH - 1;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = lm3642_verify_level(fl_arg->arg);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel, (int)fl_arg->arg);
fl_arg->arg = lm3642_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_TIMEOUT:
pr_debug("FLASH_IOC_GET_HW_TIMEOUT(%d)\n", channel);
fl_arg->arg = LM3642_HW_TIMEOUT;
break;
case FLASH_IOC_GET_HW_FAULT:
pr_debug("FLASH_IOC_GET_HW_FAULT(%d)\n", channel);
fl_arg->arg = lm3642_get_flag();
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int lm3642_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int lm3642_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int lm3642_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&lm3642_mutex);
if (set) {
if (!use_count)
ret = lm3642_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = lm3642_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&lm3642_mutex);
return ret;
}
static ssize_t lm3642_strobe_store(struct flashlight_arg arg)
{
lm3642_set_driver(1);
lm3642_set_level(arg.level);
lm3642_timeout_ms = 0;
lm3642_enable();
msleep(arg.dur);
lm3642_disable();
lm3642_set_driver(0);
return 0;
}
static struct flashlight_operations lm3642_ops = {
lm3642_open,
lm3642_release,
lm3642_ioctl,
lm3642_strobe_store,
lm3642_set_driver
};
/******************************************************************************
* I2C device and driver
*****************************************************************************/
static int lm3642_chip_init(struct lm3642_chip_data *chip)
{
/* NOTE: Chip initialication move to "set driver" for power saving.
* lm3642_init();
*/
return 0;
}
static int lm3642_parse_dt(struct device *dev,
struct lm3642_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
LM3642_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int lm3642_i2c_probe(
struct i2c_client *client, const struct i2c_device_id *id)
{
struct lm3642_chip_data *chip;
int err;
pr_debug("i2c probe start.\n");
/* check i2c */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_info("Failed to check i2c functionality.\n");
err = -ENODEV;
goto err_out;
}
/* init chip private data */
chip = kzalloc(sizeof(struct lm3642_chip_data), GFP_KERNEL);
if (!chip) {
err = -ENOMEM;
goto err_out;
}
chip->client = client;
i2c_set_clientdata(client, chip);
lm3642_i2c_client = client;
/* init mutex and spinlock */
mutex_init(&chip->lock);
/* init chip hw */
lm3642_chip_init(chip);
pr_debug("i2c probe done.\n");
return 0;
err_out:
return err;
}
static int lm3642_i2c_remove(struct i2c_client *client)
{
struct lm3642_chip_data *chip = i2c_get_clientdata(client);
pr_debug("Remove start.\n");
client->dev.platform_data = NULL;
/* free resource */
kfree(chip);
pr_debug("Remove done.\n");
return 0;
}
static const struct i2c_device_id lm3642_i2c_id[] = {
{LM3642_NAME, 0},
{}
};
MODULE_DEVICE_TABLE(i2c, lm3642_i2c_id);
#ifdef CONFIG_OF
static const struct of_device_id lm3642_i2c_of_match[] = {
{.compatible = LM3642_DTNAME_I2C},
{},
};
MODULE_DEVICE_TABLE(of, lm3642_i2c_of_match);
#endif
static struct i2c_driver lm3642_i2c_driver = {
.driver = {
.name = LM3642_NAME,
#ifdef CONFIG_OF
.of_match_table = lm3642_i2c_of_match,
#endif
},
.probe = lm3642_i2c_probe,
.remove = lm3642_i2c_remove,
.id_table = lm3642_i2c_id,
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int lm3642_probe(struct platform_device *pdev)
{
struct lm3642_platform_data *pdata = dev_get_platdata(&pdev->dev);
struct lm3642_chip_data *chip = NULL;
int err;
int i;
pr_debug("Probe start.\n");
if (i2c_add_driver(&lm3642_i2c_driver)) {
pr_debug("Failed to add i2c driver.\n");
return -1;
}
/* init platform data */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) {
err = -ENOMEM;
goto err_free;
}
pdev->dev.platform_data = pdata;
err = lm3642_parse_dt(&pdev->dev, pdata);
if (err)
goto err_free;
}
/* init work queue */
INIT_WORK(&lm3642_work, lm3642_work_disable);
/* init timer */
hrtimer_init(&lm3642_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
lm3642_timer.function = lm3642_timer_func;
lm3642_timeout_ms = 800;
/* clear usage count */
use_count = 0;
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&lm3642_ops)) {
err = -EFAULT;
goto err_free;
}
} else {
if (flashlight_dev_register(LM3642_NAME, &lm3642_ops)) {
err = -EFAULT;
goto err_free;
}
}
pr_debug("Probe done.\n");
return 0;
err_free:
chip = i2c_get_clientdata(lm3642_i2c_client);
i2c_set_clientdata(lm3642_i2c_client, NULL);
kfree(chip);
return err;
}
static int lm3642_remove(struct platform_device *pdev)
{
struct lm3642_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
i2c_del_driver(&lm3642_i2c_driver);
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(LM3642_NAME);
/* flush work queue */
flush_work(&lm3642_work);
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id lm3642_of_match[] = {
{.compatible = LM3642_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, lm3642_of_match);
#else
static struct platform_device lm3642_platform_device[] = {
{
.name = LM3642_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, lm3642_platform_device);
#endif
static struct platform_driver lm3642_platform_driver = {
.probe = lm3642_probe,
.remove = lm3642_remove,
.driver = {
.name = LM3642_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = lm3642_of_match,
#endif
},
};
static int __init flashlight_lm3642_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&lm3642_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&lm3642_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_lm3642_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&lm3642_platform_driver);
pr_debug("Exit done.\n");
}
module_init(flashlight_lm3642_init);
module_exit(flashlight_lm3642_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Xi Chen <xixi.chen@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight LM3642 Driver");
@@ -0,0 +1,976 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/pinctrl/consumer.h>
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* device tree should be defined in flashlight-dt.h */
#ifndef LM3643_DTNAME
#define LM3643_DTNAME "mediatek,flashlights_lm3643"
#endif
#ifndef LM3643_DTNAME_I2C
#define LM3643_DTNAME_I2C "mediatek,flashlights_lm3643_i2c"
#endif
#define LM3643_NAME "flashlights-lm3643"
/* define registers */
#define LM3643_REG_ENABLE (0x01)
#define LM3643_MASK_ENABLE_LED1 (0x01)
#define LM3643_MASK_ENABLE_LED2 (0x02)
#define LM3643_DISABLE (0x00)
#define LM3643_ENABLE_LED1 (0x01)
#define LM3643_ENABLE_LED1_TORCH (0x09)
#define LM3643_ENABLE_LED1_FLASH (0x0D)
#define LM3643_ENABLE_LED2 (0x02)
#define LM3643_ENABLE_LED2_TORCH (0x0A)
#define LM3643_ENABLE_LED2_FLASH (0x0E)
#define LM3643_REG_TORCH_LEVEL_LED1 (0x05)
#define LM3643_REG_FLASH_LEVEL_LED1 (0x03)
#define LM3643_REG_TORCH_LEVEL_LED2 (0x06)
#define LM3643_REG_FLASH_LEVEL_LED2 (0x04)
#define LM3643_REG_TIMING_CONF (0x08)
#define LM3643_TORCH_RAMP_TIME (0x00)
#define LM3643_FLASH_TIMEOUT (0x0F)
#define LM3643_REG_FLAG1 (0x0A)
#define LM3643_REG_FLAG2 (0x0B)
/* define channel, level */
#define LM3643_CHANNEL_NUM 2
#define LM3643_CHANNEL_CH1 0
#define LM3643_CHANNEL_CH2 1
#define LM3643_LEVEL_NUM 26
#define LM3643_LEVEL_TORCH 7
#define LM3643_HW_TIMEOUT 400 /* ms */
/* define mutex and work queue */
static DEFINE_MUTEX(lm3643_mutex);
static struct work_struct lm3643_work_ch1;
static struct work_struct lm3643_work_ch2;
/* define pinctrl */
#define LM3643_PINCTRL_PIN_HWEN 0
#define LM3643_PINCTRL_PINSTATE_LOW 0
#define LM3643_PINCTRL_PINSTATE_HIGH 1
#define LM3643_PINCTRL_STATE_HWEN_HIGH "hwen_high"
#define LM3643_PINCTRL_STATE_HWEN_LOW "hwen_low"
static struct pinctrl *lm3643_pinctrl;
static struct pinctrl_state *lm3643_hwen_high;
static struct pinctrl_state *lm3643_hwen_low;
/* define usage count */
static int use_count;
/* define i2c */
static struct i2c_client *lm3643_i2c_client;
/* platform data */
struct lm3643_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/* lm3643 chip data */
struct lm3643_chip_data {
struct i2c_client *client;
struct lm3643_platform_data *pdata;
struct mutex lock;
};
/******************************************************************************
* Pinctrl configuration
*****************************************************************************/
static int lm3643_pinctrl_init(struct platform_device *pdev)
{
int ret = 0;
/* get pinctrl */
lm3643_pinctrl = devm_pinctrl_get(&pdev->dev);
if (IS_ERR(lm3643_pinctrl)) {
pr_info("Failed to get flashlight pinctrl.\n");
ret = PTR_ERR(lm3643_pinctrl);
}
/* Flashlight HWEN pin initialization */
lm3643_hwen_high = pinctrl_lookup_state(
lm3643_pinctrl, LM3643_PINCTRL_STATE_HWEN_HIGH);
if (IS_ERR(lm3643_hwen_high)) {
pr_info("Failed to init (%s)\n",
LM3643_PINCTRL_STATE_HWEN_HIGH);
ret = PTR_ERR(lm3643_hwen_high);
}
lm3643_hwen_low = pinctrl_lookup_state(
lm3643_pinctrl, LM3643_PINCTRL_STATE_HWEN_LOW);
if (IS_ERR(lm3643_hwen_low)) {
pr_info("Failed to init (%s)\n", LM3643_PINCTRL_STATE_HWEN_LOW);
ret = PTR_ERR(lm3643_hwen_low);
}
return ret;
}
static int lm3643_pinctrl_set(int pin, int state)
{
int ret = 0;
if (IS_ERR(lm3643_pinctrl)) {
pr_info("pinctrl is not available\n");
return -1;
}
switch (pin) {
case LM3643_PINCTRL_PIN_HWEN:
if (state == LM3643_PINCTRL_PINSTATE_LOW &&
!IS_ERR(lm3643_hwen_low))
pinctrl_select_state(lm3643_pinctrl, lm3643_hwen_low);
else if (state == LM3643_PINCTRL_PINSTATE_HIGH &&
!IS_ERR(lm3643_hwen_high))
pinctrl_select_state(lm3643_pinctrl, lm3643_hwen_high);
else
pr_info("set err, pin(%d) state(%d)\n", pin, state);
break;
default:
pr_info("set err, pin(%d) state(%d)\n", pin, state);
break;
}
pr_debug("pin(%d) state(%d)\n", pin, state);
return ret;
}
/******************************************************************************
* lm3643 operations
*****************************************************************************/
static const int lm3643_current[LM3643_LEVEL_NUM] = {
22, 46, 70, 93, 116, 140, 163, 198, 245, 304,
351, 398, 445, 503, 550, 597, 656, 703, 750, 796,
855, 902, 949, 996, 1054, 1101
};
static const unsigned char lm3643_torch_level[LM3643_LEVEL_NUM] = {
0x0F, 0x20, 0x31, 0x42, 0x52, 0x63, 0x74, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const unsigned char lm3643_flash_level[LM3643_LEVEL_NUM] = {
0x01, 0x03, 0x05, 0x07, 0x09, 0x0B, 0x0D, 0x10, 0x14, 0x19,
0x1D, 0x21, 0x25, 0x2A, 0x2E, 0x32, 0x37, 0x3B, 0x3F, 0x43,
0x48, 0x4C, 0x50, 0x54, 0x59, 0x5D
};
static unsigned char lm3643_reg_enable;
static int lm3643_level_ch1 = -1;
static int lm3643_level_ch2 = -1;
static int lm3643_is_torch(int level)
{
if (level >= LM3643_LEVEL_TORCH)
return -1;
return 0;
}
static int lm3643_verify_level(int level)
{
if (level < 0)
level = 0;
else if (level >= LM3643_LEVEL_NUM)
level = LM3643_LEVEL_NUM - 1;
return level;
}
/* i2c wrapper function */
static int lm3643_write_reg(struct i2c_client *client, u8 reg, u8 val)
{
int ret;
struct lm3643_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
ret = i2c_smbus_write_byte_data(client, reg, val);
mutex_unlock(&chip->lock);
if (ret < 0)
pr_info("failed writing at 0x%02x\n", reg);
return ret;
}
static int lm3643_read_reg(struct i2c_client *client, u8 reg)
{
int val;
struct lm3643_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
val = i2c_smbus_read_byte_data(client, reg);
mutex_unlock(&chip->lock);
return val;
}
/* flashlight enable function */
static int lm3643_enable_ch1(void)
{
unsigned char reg, val;
reg = LM3643_REG_ENABLE;
if (!lm3643_is_torch(lm3643_level_ch1)) {
/* torch mode */
lm3643_reg_enable |= LM3643_ENABLE_LED1_TORCH;
} else {
/* flash mode */
lm3643_reg_enable |= LM3643_ENABLE_LED1_FLASH;
}
val = lm3643_reg_enable;
return lm3643_write_reg(lm3643_i2c_client, reg, val);
}
static int lm3643_enable_ch2(void)
{
unsigned char reg, val;
reg = LM3643_REG_ENABLE;
if (!lm3643_is_torch(lm3643_level_ch2)) {
/* torch mode */
lm3643_reg_enable |= LM3643_ENABLE_LED2_TORCH;
} else {
/* flash mode */
lm3643_reg_enable |= LM3643_ENABLE_LED2_FLASH;
}
val = lm3643_reg_enable;
return lm3643_write_reg(lm3643_i2c_client, reg, val);
}
static int lm3643_enable(int channel)
{
if (channel == LM3643_CHANNEL_CH1)
lm3643_enable_ch1();
else if (channel == LM3643_CHANNEL_CH2)
lm3643_enable_ch2();
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
/* flashlight disable function */
static int lm3643_disable_ch1(void)
{
unsigned char reg, val;
reg = LM3643_REG_ENABLE;
if (lm3643_reg_enable & LM3643_MASK_ENABLE_LED2) {
/* if LED 2 is enable, disable LED 1 */
lm3643_reg_enable &= (~LM3643_ENABLE_LED1);
} else {
/* if LED 2 is disable, disable LED 1 and clear mode */
lm3643_reg_enable &= (~LM3643_ENABLE_LED1_FLASH);
}
val = lm3643_reg_enable;
return lm3643_write_reg(lm3643_i2c_client, reg, val);
}
static int lm3643_disable_ch2(void)
{
unsigned char reg, val;
reg = LM3643_REG_ENABLE;
if (lm3643_reg_enable & LM3643_MASK_ENABLE_LED1) {
/* if LED 1 is enable, disable LED 2 */
lm3643_reg_enable &= (~LM3643_ENABLE_LED2);
} else {
/* if LED 1 is disable, disable LED 2 and clear mode */
lm3643_reg_enable &= (~LM3643_ENABLE_LED2_FLASH);
}
val = lm3643_reg_enable;
return lm3643_write_reg(lm3643_i2c_client, reg, val);
}
static int lm3643_disable(int channel)
{
if (channel == LM3643_CHANNEL_CH1)
lm3643_disable_ch1();
else if (channel == LM3643_CHANNEL_CH2)
lm3643_disable_ch2();
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
/* set flashlight level */
static int lm3643_set_level_ch1(int level)
{
int ret;
unsigned char reg, val;
level = lm3643_verify_level(level);
/* set torch brightness level */
reg = LM3643_REG_TORCH_LEVEL_LED1;
val = lm3643_torch_level[level];
ret = lm3643_write_reg(lm3643_i2c_client, reg, val);
lm3643_level_ch1 = level;
/* set flash brightness level */
reg = LM3643_REG_FLASH_LEVEL_LED1;
val = lm3643_flash_level[level];
ret = lm3643_write_reg(lm3643_i2c_client, reg, val);
return ret;
}
static int lm3643_set_level_ch2(int level)
{
int ret;
unsigned char reg, val;
level = lm3643_verify_level(level);
/* set torch brightness level */
reg = LM3643_REG_TORCH_LEVEL_LED2;
val = lm3643_torch_level[level];
ret = lm3643_write_reg(lm3643_i2c_client, reg, val);
lm3643_level_ch2 = level;
/* set flash brightness level */
reg = LM3643_REG_FLASH_LEVEL_LED2;
val = lm3643_flash_level[level];
ret = lm3643_write_reg(lm3643_i2c_client, reg, val);
return ret;
}
static int lm3643_set_level(int channel, int level)
{
if (channel == LM3643_CHANNEL_CH1)
lm3643_set_level_ch1(level);
else if (channel == LM3643_CHANNEL_CH2)
lm3643_set_level_ch2(level);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int lm3643_get_flag(int num)
{
if (num == 1)
return lm3643_read_reg(lm3643_i2c_client, LM3643_REG_FLAG1);
else if (num == 2)
return lm3643_read_reg(lm3643_i2c_client, LM3643_REG_FLAG2);
pr_info("Error num\n");
return 0;
}
/* flashlight init */
int lm3643_init(void)
{
int ret;
unsigned char reg, val;
lm3643_pinctrl_set(
LM3643_PINCTRL_PIN_HWEN, LM3643_PINCTRL_PINSTATE_HIGH);
msleep(20);
/* clear enable register */
reg = LM3643_REG_ENABLE;
val = LM3643_DISABLE;
ret = lm3643_write_reg(lm3643_i2c_client, reg, val);
lm3643_reg_enable = val;
/* set torch current ramp time and flash timeout */
reg = LM3643_REG_TIMING_CONF;
val = LM3643_TORCH_RAMP_TIME | LM3643_FLASH_TIMEOUT;
ret = lm3643_write_reg(lm3643_i2c_client, reg, val);
return ret;
}
/* flashlight uninit */
int lm3643_uninit(void)
{
lm3643_disable(LM3643_CHANNEL_CH1);
lm3643_disable(LM3643_CHANNEL_CH2);
lm3643_pinctrl_set(
LM3643_PINCTRL_PIN_HWEN, LM3643_PINCTRL_PINSTATE_LOW);
return 0;
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static struct hrtimer lm3643_timer_ch1;
static struct hrtimer lm3643_timer_ch2;
static unsigned int lm3643_timeout_ms[LM3643_CHANNEL_NUM];
static void lm3643_work_disable_ch1(struct work_struct *data)
{
pr_debug("ht work queue callback\n");
lm3643_disable_ch1();
}
static void lm3643_work_disable_ch2(struct work_struct *data)
{
pr_debug("lt work queue callback\n");
lm3643_disable_ch2();
}
static enum hrtimer_restart lm3643_timer_func_ch1(struct hrtimer *timer)
{
schedule_work(&lm3643_work_ch1);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart lm3643_timer_func_ch2(struct hrtimer *timer)
{
schedule_work(&lm3643_work_ch2);
return HRTIMER_NORESTART;
}
static int lm3643_timer_start(int channel, ktime_t ktime)
{
if (channel == LM3643_CHANNEL_CH1)
hrtimer_start(&lm3643_timer_ch1, ktime, HRTIMER_MODE_REL);
else if (channel == LM3643_CHANNEL_CH2)
hrtimer_start(&lm3643_timer_ch2, ktime, HRTIMER_MODE_REL);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int lm3643_timer_cancel(int channel)
{
if (channel == LM3643_CHANNEL_CH1)
hrtimer_cancel(&lm3643_timer_ch1);
else if (channel == LM3643_CHANNEL_CH2)
hrtimer_cancel(&lm3643_timer_ch2);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int lm3643_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
ktime_t ktime;
unsigned int s;
unsigned int ns;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
/* verify channel */
if (channel < 0 || channel >= LM3643_CHANNEL_NUM) {
pr_info("Failed with error channel\n");
return -EINVAL;
}
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
lm3643_timeout_ms[channel] = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
lm3643_set_level(channel, fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
if (fl_arg->arg == 1) {
if (lm3643_timeout_ms[channel]) {
s = lm3643_timeout_ms[channel] / 1000;
ns = lm3643_timeout_ms[channel] % 1000
* 1000000;
ktime = ktime_set(s, ns);
lm3643_timer_start(channel, ktime);
}
lm3643_enable(channel);
} else {
lm3643_disable(channel);
lm3643_timer_cancel(channel);
}
break;
case FLASH_IOC_GET_DUTY_NUMBER:
pr_debug("FLASH_IOC_GET_DUTY_NUMBER(%d)\n", channel);
fl_arg->arg = LM3643_LEVEL_NUM;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_debug("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n", channel);
fl_arg->arg = LM3643_LEVEL_TORCH - 1;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = lm3643_verify_level(fl_arg->arg);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel, (int)fl_arg->arg);
fl_arg->arg = lm3643_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_TIMEOUT:
pr_debug("FLASH_IOC_GET_HW_TIMEOUT(%d)\n", channel);
fl_arg->arg = LM3643_HW_TIMEOUT;
break;
case FLASH_IOC_GET_HW_FAULT:
pr_debug("FLASH_IOC_GET_HW_FAULT(%d)\n", channel);
fl_arg->arg = lm3643_get_flag(1);
break;
case FLASH_IOC_GET_HW_FAULT2:
pr_debug("FLASH_IOC_GET_HW_FAULT2(%d)\n", channel);
fl_arg->arg = lm3643_get_flag(2);
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int lm3643_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int lm3643_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int lm3643_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&lm3643_mutex);
if (set) {
if (!use_count)
ret = lm3643_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = lm3643_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&lm3643_mutex);
return ret;
}
static ssize_t lm3643_strobe_store(struct flashlight_arg arg)
{
lm3643_set_driver(1);
lm3643_set_level(arg.channel, arg.level);
lm3643_timeout_ms[arg.channel] = 0;
lm3643_enable(arg.channel);
msleep(arg.dur);
lm3643_disable(arg.channel);
lm3643_set_driver(0);
return 0;
}
static struct flashlight_operations lm3643_ops = {
lm3643_open,
lm3643_release,
lm3643_ioctl,
lm3643_strobe_store,
lm3643_set_driver
};
/******************************************************************************
* I2C device and driver
*****************************************************************************/
static int lm3643_chip_init(struct lm3643_chip_data *chip)
{
/* NOTE: Chip initialication move to "set driver" for power saving.
* lm3643_init();
*/
return 0;
}
static int lm3643_parse_dt(struct device *dev,
struct lm3643_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
LM3643_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int lm3643_i2c_probe(
struct i2c_client *client, const struct i2c_device_id *id)
{
struct lm3643_chip_data *chip;
int err;
pr_debug("i2c probe start.\n");
/* check i2c */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_info("Failed to check i2c functionality.\n");
err = -ENODEV;
goto err_out;
}
/* init chip private data */
chip = kzalloc(sizeof(struct lm3643_chip_data), GFP_KERNEL);
if (!chip) {
err = -ENOMEM;
goto err_out;
}
chip->client = client;
i2c_set_clientdata(client, chip);
lm3643_i2c_client = client;
/* init mutex and spinlock */
mutex_init(&chip->lock);
/* init chip hw */
lm3643_chip_init(chip);
pr_debug("i2c probe done.\n");
return 0;
err_out:
return err;
}
static int lm3643_i2c_remove(struct i2c_client *client)
{
struct lm3643_chip_data *chip = i2c_get_clientdata(client);
pr_debug("Remove start.\n");
client->dev.platform_data = NULL;
/* free resource */
kfree(chip);
pr_debug("Remove done.\n");
return 0;
}
static const struct i2c_device_id lm3643_i2c_id[] = {
{LM3643_NAME, 0},
{}
};
#ifdef CONFIG_OF
static const struct of_device_id lm3643_i2c_of_match[] = {
{.compatible = LM3643_DTNAME_I2C},
{},
};
#endif
static struct i2c_driver lm3643_i2c_driver = {
.driver = {
.name = LM3643_NAME,
#ifdef CONFIG_OF
.of_match_table = lm3643_i2c_of_match,
#endif
},
.probe = lm3643_i2c_probe,
.remove = lm3643_i2c_remove,
.id_table = lm3643_i2c_id,
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int lm3643_probe(struct platform_device *pdev)
{
struct lm3643_platform_data *pdata = dev_get_platdata(&pdev->dev);
struct lm3643_chip_data *chip = NULL;
int err;
int i;
pr_debug("Probe start.\n");
/* init pinctrl */
if (lm3643_pinctrl_init(pdev)) {
pr_debug("Failed to init pinctrl.\n");
return -1;
}
if (i2c_add_driver(&lm3643_i2c_driver)) {
pr_debug("Failed to add i2c driver.\n");
return -1;
}
/* init platform data */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) {
err = -ENOMEM;
goto err_free;
}
pdev->dev.platform_data = pdata;
err = lm3643_parse_dt(&pdev->dev, pdata);
if (err)
goto err_free;
}
/* init work queue */
INIT_WORK(&lm3643_work_ch1, lm3643_work_disable_ch1);
INIT_WORK(&lm3643_work_ch2, lm3643_work_disable_ch2);
/* init timer */
hrtimer_init(&lm3643_timer_ch1, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
lm3643_timer_ch1.function = lm3643_timer_func_ch1;
hrtimer_init(&lm3643_timer_ch2, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
lm3643_timer_ch2.function = lm3643_timer_func_ch2;
lm3643_timeout_ms[LM3643_CHANNEL_CH1] = 100;
lm3643_timeout_ms[LM3643_CHANNEL_CH2] = 100;
/* clear usage count */
use_count = 0;
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&lm3643_ops)) {
err = -EFAULT;
goto err_free;
}
} else {
if (flashlight_dev_register(LM3643_NAME, &lm3643_ops)) {
err = -EFAULT;
goto err_free;
}
}
pr_debug("Probe done.\n");
return 0;
err_free:
chip = i2c_get_clientdata(lm3643_i2c_client);
i2c_set_clientdata(lm3643_i2c_client, NULL);
kfree(chip);
return err;
}
static int lm3643_remove(struct platform_device *pdev)
{
struct lm3643_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
i2c_del_driver(&lm3643_i2c_driver);
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(LM3643_NAME);
/* flush work queue */
flush_work(&lm3643_work_ch1);
flush_work(&lm3643_work_ch2);
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id lm3643_of_match[] = {
{.compatible = LM3643_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, lm3643_of_match);
#else
static struct platform_device lm3643_platform_device[] = {
{
.name = LM3643_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, lm3643_platform_device);
#endif
static struct platform_driver lm3643_platform_driver = {
.probe = lm3643_probe,
.remove = lm3643_remove,
.driver = {
.name = LM3643_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = lm3643_of_match,
#endif
},
};
static int __init flashlight_lm3643_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&lm3643_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&lm3643_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_lm3643_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&lm3643_platform_driver);
pr_debug("Exit done.\n");
}
module_init(flashlight_lm3643_init);
module_exit(flashlight_lm3643_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight LM3643 Driver");
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,991 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include "mt6336/mt6336.h"
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* device tree should be defined in flashlight-dt.h */
#ifndef MT6336_DTNAME
#define MT6336_DTNAME "mediatek,flashlights_mt6336"
#endif
#define MT6336_NAME "flashlights-mt6336"
/* define channel, level */
#define MT6336_CHANNEL_NUM 2
#define MT6336_CHANNEL_CH1 0
#define MT6336_CHANNEL_CH2 1
#define MT6336_NONE (-1)
#define MT6336_DISABLE 0
#define MT6336_ENABLE 1
#define MT6336_ENABLE_TORCH 1
#define MT6336_ENABLE_FLASH 2
#define MT6336_LEVEL_NUM 34
#define MT6336_LEVEL_TORCH 8
#define MT6336_WDT_TIMEOUT 600 /* ms */
#define MT6336_HW_TIMEOUT 600 /* ms */
/* define mutex, work queue and timer */
static DEFINE_MUTEX(mt6336_mutex);
static struct work_struct mt6336_work_ch1;
static struct work_struct mt6336_work_ch2;
static struct hrtimer mt6336_timer_ch1;
static struct hrtimer mt6336_timer_ch2;
static unsigned int mt6336_timeout_ms[MT6336_CHANNEL_NUM];
/* define usage count */
static int use_count;
/* mt6336 pmic control handler */
static struct mt6336_ctrl *flashlight_ctrl;
/* platform data */
struct mt6336_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/******************************************************************************
* mt6336 operations
*****************************************************************************/
static const int mt6336_current[MT6336_LEVEL_NUM] = {
25, 50, 75, 100, 125, 150, 175, 200, 250, 300,
350, 400, 450, 500, 550, 600, 650, 700, 750, 800,
850, 900, 950, 1000, 1050, 1100, 1150, 1200, 1250, 1300,
1350, 1400, 1450, 1500
};
static const unsigned char mt6336_level[MT6336_LEVEL_NUM] = {
0x01, 0x03, 0x05, 0x07, 0x09, 0x0B, 0x0D, 0x0F, 0x13, 0x17,
0x1B, 0x1F, 0x23, 0x27, 0x2B, 0x2F, 0x33, 0x37, 0x3B, 0x3F,
0x43, 0x47, 0x4B, 0x4F, 0x53, 0x57, 0x5B, 0x5F, 0x63, 0x67,
0x6B, 0x6F, 0x73, 0x77
};
static int is_preenable;
static int mt6336_decouple_mode;
static int mt6336_en_ch1;
static int mt6336_en_ch2;
static int mt6336_level_ch1;
static int mt6336_level_ch2;
static int mt6336_is_torch(int level)
{
if (level >= MT6336_LEVEL_TORCH)
return -1;
return 0;
}
static int mt6336_verify_level(int level)
{
if (level < 0)
level = 0;
else if (level >= MT6336_LEVEL_NUM)
level = MT6336_LEVEL_NUM - 1;
return level;
}
/* pre-enable steps before enable flashlight */
static int mt6336_preenable(void)
{
int ret;
ret = mt6336_set_register_value(0x0519, 0x0A);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_GM_EN, 0x06);
* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_CLAMP_EN, 0x01);
*/
ret = mt6336_set_register_value(0x0520, 0x00);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_GM_TUNE_SYS_MSB, 0x00);
*/
ret = mt6336_set_register_value(0x055A, 0x01);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_SWCHR_RSV_TRIM_MSB, 0x01);
*/
ret = mt6336_set_register_value(0x0455, 0x00);
/* mt6336_set_flag_register_value(
* MT6336_RG_EN_HW_GAIN_SET, 0x00);
*/
ret = mt6336_set_register_value(0x03C9, 0x00);
/* mt6336_set_flag_register_value(
* MT6336_AUXADC_HWGAIN_EN, 0x00);
*/
ret = mt6336_set_register_value(0x0553, 0x54);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_PWR_LG_DTC, 0x01);
* mt6336_set_flag_register_value(
* MT6336_RG_A_PWR_LG_SRCEH, 0x01);
* mt6336_set_flag_register_value(
* MT6336_RG_A_PWR_LG_SRC, 0x01);
* mt6336_set_flag_register_value(
* MT6336_RG_A_PWR_LG_VTHSEL, 0x00);
*/
ret = mt6336_set_register_value(0x055F, 0xE0);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_FTR_RC, 0x03);
* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_FTR_DROP, 0x04);
* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_FTR_SHOOT_EN, 0x00);
* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_FTR_DROP_EN, 0x00);
* mt6336_set_flag_register_value(
* MT6336_RG_A_SWCHR_ZCD_TRIM_EN, 0x00);
*/
ret = mt6336_set_register_value(0x053D, 0x45);
/* mt6336_set_flag_register_value(
* MT6336_RG_VRAMP_SLP, 0x04);
* mt6336_set_flag_register_value(
* MT6336_RG_A_VRAMP_DCOS, 0x05);
*/
/* only way to verify register is to get again */
ret = 0;
return ret;
}
/* post-enable steps after enable flashlight */
static int mt6336_postenable(void)
{
int ret;
ret = mt6336_set_register_value(0x052A, 0x88);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_GM_RSV_LSB, 0x88);
*/
ret = mt6336_set_register_value(0x0553, 0x14);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_PWR_LG_DTC, 0x00);
* mt6336_set_flag_register_value(
* MT6336_RG_A_PWR_LG_SRCEH, 0x01);
* mt6336_set_flag_register_value(
* MT6336_RG_A_PWR_LG_SRC, 0x01);
* mt6336_set_flag_register_value(
* MT6336_RG_A_PWR_LG_VTHSEL, 0x00);
*/
ret = mt6336_set_register_value(0x0519, 0x3E);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_GM_EN, 0x1F);
* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_CLAMP_EN, 0x01);
*/
ret = mt6336_set_register_value(0x051E, 0x02);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_GM_TUNE_ICHIN_MSB, 0x02);
*/
ret = mt6336_set_register_value(0x0520, 0x04);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_GM_TUNE_SYS_MSB, 0x04);
*/
ret = mt6336_set_register_value(0x055A, 0x00);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_SWCHR_RSV_TRIM_MSB, 0x00);
*/
ret = mt6336_set_register_value(0x0455, 0x01);
/* mt6336_set_flag_register_value(
* MT6336_RG_EN_HW_GAIN_SET, 0x01);
*/
ret = mt6336_set_register_value(0x03C9, 0x10);
/* mt6336_set_flag_register_value(
* MT6336_AUXADC_HWGAIN_EN, 0x01);
*/
ret = mt6336_set_register_value(0x03CF, 0x03);
/* mt6336_set_flag_register_value(
* MT6336_AUXADC_HWGAIN_DET_PRD_M, 0x03);
*/
ret = mt6336_set_register_value(0x0402, 0x03);
/* mt6336_set_flag_register_value(
* MT6336_RG_DIS_REVFET, 0x00);
* mt6336_set_flag_register_value(
* MT6336_RG_T_TERM_EXT, 0x00);
* mt6336_set_flag_register_value(
* MT6336_RG_EN_TERM, 0x01);
* mt6336_set_flag_register_value(
* MT6336_RG_EN_RECHARGE, 0x01);
*/
ret = mt6336_set_register_value(0x0529, 0x80);
/* mt6336_set_flag_register_value(
* MT6336_RG_A_LOOP_GM_RSV_MSB, 0x88);
*/
ret = mt6336_set_register_value(0x051F, 0x84);
ret = mt6336_set_register_value(0x053D, 0x47);
/* only way to verify register is to get again */
ret = 0;
return ret;
}
/* flashlight enable function */
static int mt6336_enable(void)
{
u8 buck_enable_status;
mt6336_ctrl_enable(flashlight_ctrl);
if ((mt6336_en_ch1 == MT6336_ENABLE_FLASH)
|| (mt6336_en_ch2 == MT6336_ENABLE_FLASH)) {
/* flash mode */
if (!mt6336_get_flag_register_value(
MT6336_DA_QI_OTG_MODE_MUX)) {
/* disable charging */
/* mt6336_set_register_value(0x0400, 0x00); */
buck_enable_status = mt6336_get_flag_register_value(
MT6336_RG_EN_BUCK);
mt6336_set_flag_register_value(MT6336_RG_EN_BUCK, 0x00);
/*
* mt6336_set_flag_register_value(
* MT6336_RG_EN_CHARGE, 0x00);
*/
/* pre-enable steps */
mt6336_preenable();
is_preenable = 1;
/* enable flash mode, source current */
if (mt6336_en_ch1)
mt6336_set_flag_register_value(
MT6336_RG_EN_IFLA1, 0x01);
if (mt6336_en_ch2)
mt6336_set_flag_register_value(
MT6336_RG_EN_IFLA2, 0x01);
mt6336_set_flag_register_value(
MT6336_RG_EN_LEDCS, 0x01);
/* enable flash and apply previous buck setting */
mt6336_set_flag_register_value(MT6336_RG_EN_BUCK,
buck_enable_status);
mt6336_set_flag_register_value(MT6336_RG_EN_FLASH,
0x01);
} else {
/* failed in OTG mode */
pr_info("Failed to turn on flash mode since in OTG mode.\n");
}
} else {
/* torch mode */
/* pre-enable steps */
if (!mt6336_get_flag_register_value(
MT6336_DA_QI_OTG_MODE_MUX) &&
!mt6336_get_flag_register_value(
MT6336_DA_QI_VBUS_PLUGIN_MUX)) {
mt6336_preenable();
is_preenable = 1;
}
/* enable torch mode, source current and enable torch */
if (mt6336_en_ch1)
mt6336_set_flag_register_value(
MT6336_RG_EN_ITOR1, 0x01);
if (mt6336_en_ch2)
mt6336_set_flag_register_value(
MT6336_RG_EN_ITOR2, 0x01);
mt6336_set_flag_register_value(MT6336_RG_EN_LEDCS, 0x01);
mt6336_set_flag_register_value(MT6336_RG_EN_TORCH, 0x01);
}
mt6336_ctrl_disable(flashlight_ctrl);
mt6336_en_ch1 = MT6336_NONE;
mt6336_en_ch2 = MT6336_NONE;
return 0;
}
/* flashlight disable function */
static int mt6336_disable(void)
{
mt6336_ctrl_enable(flashlight_ctrl);
/* disable torch/flash mode */
mt6336_set_flag_register_value(MT6336_RG_EN_ITOR1, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_ITOR2, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_IFLA1, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_IFLA2, 0x00);
/* pre-enable steps */
if (is_preenable) {
mt6336_postenable();
is_preenable = 0;
}
/* disable source current and disable torch/flash */
mt6336_set_flag_register_value(MT6336_RG_EN_LEDCS, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_TORCH, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_FLASH, 0x00);
mt6336_ctrl_disable(flashlight_ctrl);
mt6336_en_ch1 = MT6336_NONE;
mt6336_en_ch2 = MT6336_NONE;
return 0;
}
/* set flashlight level */
static int mt6336_set_level_ch1(int level)
{
level = mt6336_verify_level(level);
mt6336_level_ch1 = level;
/* set brightness level */
mt6336_ctrl_enable(flashlight_ctrl);
mt6336_set_flag_register_value(MT6336_RG_ITOR1, mt6336_level[level]);
mt6336_set_flag_register_value(MT6336_RG_IFLA1, mt6336_level[level]);
mt6336_ctrl_disable(flashlight_ctrl);
return 0;
}
static int mt6336_set_level_ch2(int level)
{
level = mt6336_verify_level(level);
mt6336_level_ch2 = level;
/* set brightness level */
mt6336_ctrl_enable(flashlight_ctrl);
mt6336_set_flag_register_value(MT6336_RG_ITOR2, mt6336_level[level]);
mt6336_set_flag_register_value(MT6336_RG_IFLA2, mt6336_level[level]);
mt6336_ctrl_disable(flashlight_ctrl);
return 0;
}
static int mt6336_set_level(int channel, int level)
{
if (channel == MT6336_CHANNEL_CH1)
mt6336_set_level_ch1(level);
else if (channel == MT6336_CHANNEL_CH2)
mt6336_set_level_ch2(level);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int mt6336_set_scenario(int scenario)
{
/* set decouple mode */
mt6336_decouple_mode = scenario & FLASHLIGHT_SCENARIO_DECOUPLE_MASK;
return 0;
}
/* flashlight init */
int mt6336_init(void)
{
int ret = 0;
mt6336_ctrl_enable(flashlight_ctrl);
/* clear flash/torch mode enable register */
mt6336_set_flag_register_value(MT6336_RG_EN_IFLA1, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_IFLA2, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_ITOR1, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_ITOR2, 0x00);
/* clear current source register */
mt6336_set_flag_register_value(MT6336_RG_EN_LEDCS, 0x00);
/* clear flash/torch enable register */
mt6336_set_flag_register_value(MT6336_RG_EN_FLASH, 0x00);
mt6336_set_flag_register_value(MT6336_RG_EN_TORCH, 0x00);
/* setup flash mode output regulation voltage (default: 5.0 V) */
mt6336_set_flag_register_value(MT6336_RG_VFLA, 0x05);
/* setup flash mode current step up/down timing (default: 10 us) */
mt6336_set_flag_register_value(MT6336_RG_TSTEP_ILED, 0x01);
/* setup flash/torch watchdog timeout (default: 600 ms) */
mt6336_set_flag_register_value(MT6336_RG_FLA_WDT, 0x10);
/* enable flash watchdog (default) */
mt6336_set_flag_register_value(MT6336_RG_EN_FLA_WDT, 0x01);
/* disable torch watchdog (default) */
mt6336_set_flag_register_value(MT6336_RG_EN_TOR_WDT, 0x00);
mt6336_ctrl_disable(flashlight_ctrl);
/* clear flashlight state */
mt6336_en_ch1 = MT6336_NONE;
mt6336_en_ch2 = MT6336_NONE;
/* clear decouple mode */
mt6336_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
is_preenable = 0;
return ret;
}
/* flashlight uninit */
int mt6336_uninit(void)
{
/* clear flashlight state */
mt6336_en_ch1 = MT6336_NONE;
mt6336_en_ch2 = MT6336_NONE;
/* clear decouple mode */
mt6336_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
return mt6336_disable();
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
void mt6336_isr_short_ch1(void)
{
schedule_work(&mt6336_work_ch1);
}
void mt6336_isr_short_ch2(void)
{
schedule_work(&mt6336_work_ch2);
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static void mt6336_work_disable_ch1(struct work_struct *data)
{
pr_debug("ht work queue callback\n");
mt6336_disable();
}
static void mt6336_work_disable_ch2(struct work_struct *data)
{
pr_debug("lt work queue callback\n");
mt6336_disable();
}
static enum hrtimer_restart mt6336_timer_func_ch1(struct hrtimer *timer)
{
schedule_work(&mt6336_work_ch1);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart mt6336_timer_func_ch2(struct hrtimer *timer)
{
schedule_work(&mt6336_work_ch2);
return HRTIMER_NORESTART;
}
static int mt6336_timer_start(int channel, ktime_t ktime)
{
if (channel == MT6336_CHANNEL_CH1)
hrtimer_start(&mt6336_timer_ch1, ktime, HRTIMER_MODE_REL);
else if (channel == MT6336_CHANNEL_CH2)
hrtimer_start(&mt6336_timer_ch2, ktime, HRTIMER_MODE_REL);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int mt6336_timer_cancel(int channel)
{
if (channel == MT6336_CHANNEL_CH1)
hrtimer_cancel(&mt6336_timer_ch1);
else if (channel == MT6336_CHANNEL_CH2)
hrtimer_cancel(&mt6336_timer_ch2);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
/******************************************************************************
* Flashlight operation wrapper function
*****************************************************************************/
static int mt6336_operate(int channel, int enable)
{
ktime_t ktime;
unsigned int s;
unsigned int ns;
/* setup enable/disable */
if (channel == MT6336_CHANNEL_CH1) {
mt6336_en_ch1 = enable;
if (mt6336_en_ch1)
if (mt6336_is_torch(mt6336_level_ch1))
mt6336_en_ch1 = MT6336_ENABLE_FLASH;
} else if (channel == MT6336_CHANNEL_CH2) {
mt6336_en_ch2 = enable;
if (mt6336_en_ch2)
if (mt6336_is_torch(mt6336_level_ch2))
mt6336_en_ch2 = MT6336_ENABLE_FLASH;
} else {
pr_info("Error channel\n");
return -1;
}
/* decouple mode */
if (mt6336_decouple_mode) {
if (channel == MT6336_CHANNEL_CH1)
mt6336_en_ch2 = MT6336_DISABLE;
else if (channel == MT6336_CHANNEL_CH2)
mt6336_en_ch1 = MT6336_DISABLE;
}
/* operate flashlight and setup timer */
if ((mt6336_en_ch1 != MT6336_NONE) && (mt6336_en_ch2 != MT6336_NONE)) {
if ((mt6336_en_ch1 == MT6336_DISABLE) &&
(mt6336_en_ch2 == MT6336_DISABLE)) {
mt6336_disable();
mt6336_timer_cancel(MT6336_CHANNEL_CH1);
mt6336_timer_cancel(MT6336_CHANNEL_CH2);
} else {
if (mt6336_timeout_ms[MT6336_CHANNEL_CH1]) {
s = mt6336_timeout_ms[MT6336_CHANNEL_CH1] /
1000;
ns = mt6336_timeout_ms[MT6336_CHANNEL_CH1] %
1000 * 1000000;
ktime = ktime_set(s, ns);
mt6336_timer_start(MT6336_CHANNEL_CH1, ktime);
}
if (mt6336_timeout_ms[MT6336_CHANNEL_CH2]) {
s = mt6336_timeout_ms[MT6336_CHANNEL_CH2] /
1000;
ns = mt6336_timeout_ms[MT6336_CHANNEL_CH2] %
1000 * 1000000;
ktime = ktime_set(s, ns);
mt6336_timer_start(MT6336_CHANNEL_CH2, ktime);
}
mt6336_enable();
}
/* clear flashlight state */
mt6336_en_ch1 = MT6336_NONE;
mt6336_en_ch2 = MT6336_NONE;
}
return 0;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int mt6336_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
/* verify channel */
if (channel < 0 || channel >= MT6336_CHANNEL_NUM) {
pr_info("Failed with error channel\n");
return -EINVAL;
}
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
mt6336_timeout_ms[channel] = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
mt6336_set_level(channel, fl_arg->arg);
break;
case FLASH_IOC_SET_SCENARIO:
pr_debug("FLASH_IOC_SET_SCENARIO(%d): %d\n",
channel, (int)fl_arg->arg);
mt6336_set_scenario(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
mt6336_operate(channel, fl_arg->arg);
break;
case FLASH_IOC_GET_DUTY_NUMBER:
pr_debug("FLASH_IOC_GET_DUTY_NUMBER(%d)\n", channel);
fl_arg->arg = MT6336_LEVEL_NUM;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_debug("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n", channel);
fl_arg->arg = MT6336_LEVEL_TORCH - 1;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = mt6336_verify_level(fl_arg->arg);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel, (int)fl_arg->arg);
fl_arg->arg = mt6336_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_TIMEOUT:
pr_debug("FLASH_IOC_GET_HW_TIMEOUT(%d)\n", channel);
fl_arg->arg = MT6336_HW_TIMEOUT;
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int mt6336_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int mt6336_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int mt6336_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&mt6336_mutex);
if (set) {
if (!use_count)
ret = mt6336_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = mt6336_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&mt6336_mutex);
return ret;
}
static ssize_t mt6336_strobe_store(struct flashlight_arg arg)
{
mt6336_set_driver(1);
mt6336_set_scenario(FLASHLIGHT_SCENARIO_COUPLE);
mt6336_set_level(arg.channel, arg.level);
mt6336_timeout_ms[arg.channel] = 0;
if (arg.level < 0)
mt6336_operate(arg.channel, MT6336_DISABLE);
else
mt6336_operate(arg.channel, MT6336_ENABLE);
msleep(arg.dur);
mt6336_operate(arg.channel, MT6336_DISABLE);
mt6336_set_driver(0);
return 0;
}
static struct flashlight_operations mt6336_ops = {
mt6336_open,
mt6336_release,
mt6336_ioctl,
mt6336_strobe_store,
mt6336_set_driver
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int mt6336_parse_dt(struct device *dev,
struct mt6336_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
MT6336_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int mt6336_probe(struct platform_device *pdev)
{
struct mt6336_platform_data *pdata = dev_get_platdata(&pdev->dev);
int ret;
int i;
pr_debug("Probe start.\n");
/* parse dt */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return -ENOMEM;
pdev->dev.platform_data = pdata;
ret = mt6336_parse_dt(&pdev->dev, pdata);
if (ret)
return ret;
}
/* init work queue */
INIT_WORK(&mt6336_work_ch1, mt6336_work_disable_ch1);
INIT_WORK(&mt6336_work_ch2, mt6336_work_disable_ch2);
/* init timer */
hrtimer_init(&mt6336_timer_ch1, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mt6336_timer_ch1.function = mt6336_timer_func_ch1;
hrtimer_init(&mt6336_timer_ch2, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mt6336_timer_ch2.function = mt6336_timer_func_ch2;
mt6336_timeout_ms[MT6336_CHANNEL_CH1] = 600;
mt6336_timeout_ms[MT6336_CHANNEL_CH2] = 600;
/* get mt6336 pmic control handler */
flashlight_ctrl = mt6336_ctrl_get("mt6336_flashlight");
/* register and enable mt6336 pmic ISR */
mt6336_ctrl_enable(flashlight_ctrl);
mt6336_register_interrupt_callback(
MT6336_INT_LED1_SHORT, mt6336_isr_short_ch1);
mt6336_register_interrupt_callback(
MT6336_INT_LED2_SHORT, mt6336_isr_short_ch2);
/* mt6336_register_interrupt_callback(MT6336_INT_LED1_OPEN, NULL); */
/* mt6336_register_interrupt_callback(MT6336_INT_LED2_OPEN, NULL); */
mt6336_enable_interrupt(MT6336_INT_LED1_SHORT, "flashlight");
mt6336_enable_interrupt(MT6336_INT_LED2_SHORT, "flashlight");
/* mt6336_enable_interrupt(MT6336_INT_LED1_OPEN, "flashlight"); */
/* mt6336_enable_interrupt(MT6336_INT_LED2_OPEN, "flashlight"); */
mt6336_ctrl_disable(flashlight_ctrl);
/* clear usage count */
use_count = 0;
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&mt6336_ops))
return -EFAULT;
} else {
if (flashlight_dev_register(MT6336_NAME, &mt6336_ops))
return -EFAULT;
}
pr_debug("Probe done.\n");
return 0;
}
static int mt6336_remove(struct platform_device *pdev)
{
struct mt6336_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(MT6336_NAME);
/* flush work queue */
flush_work(&mt6336_work_ch1);
flush_work(&mt6336_work_ch2);
/* disable mt6336 pmic ISR */
mt6336_ctrl_enable(flashlight_ctrl);
mt6336_disable_interrupt(MT6336_INT_LED1_SHORT, "flashlight");
mt6336_disable_interrupt(MT6336_INT_LED2_SHORT, "flashlight");
/* mt6336_disable_interrupt(MT6336_INT_LED1_OPEN, "flashlight"); */
/* mt6336_disable_interrupt(MT6336_INT_LED2_OPEN, "flashlight"); */
mt6336_ctrl_disable(flashlight_ctrl);
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id mt6336_of_match[] = {
{.compatible = MT6336_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, mt6336_of_match);
#else
static struct platform_device mt6336_platform_device[] = {
{
.name = MT6336_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, mt6336_platform_device);
#endif
static struct platform_driver mt6336_platform_driver = {
.probe = mt6336_probe,
.remove = mt6336_remove,
.driver = {
.name = MT6336_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = mt6336_of_match,
#endif
},
};
static int __init flashlight_mt6336_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&mt6336_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&mt6336_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_mt6336_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&mt6336_platform_driver);
pr_debug("Exit done.\n");
}
/* replace module_init() since conflict in kernel init process */
late_initcall(flashlight_mt6336_init);
module_exit(flashlight_mt6336_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight MT6336 Driver");
@@ -0,0 +1,987 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/power_supply.h>
#include "richtek/rt-flashlight.h"
#include "v1/mtk_charger.h"
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* device tree should be defined in flashlight-dt.h */
#ifndef MT6360_DTNAME
#define MT6360_DTNAME "mediatek,flashlights_mt6360"
#endif
#define MT6360_NAME "flashlights-mt6360"
/* define channel, level */
#define MT6360_CHANNEL_NUM 2
#define MT6360_CHANNEL_CH1 0
#define MT6360_CHANNEL_CH2 1
#define MT6360_CHANNEL_ALL 2
#define MT6360_NONE (-1)
#define MT6360_DISABLE 0
#define MT6360_ENABLE 1
#define MT6360_ENABLE_TORCH 1
#define MT6360_ENABLE_FLASH 2
#define MT6360_LEVEL_NUM 32
#define MT6360_LEVEL_TORCH 16
#define MT6360_LEVEL_FLASH MT6360_LEVEL_NUM
#define MT6360_WDT_TIMEOUT 1248 /* ms */
#define MT6360_HW_TIMEOUT 400 /* ms */
/* define mutex, work queue and timer */
static DEFINE_MUTEX(mt6360_mutex);
static struct work_struct mt6360_work_ch1;
static struct work_struct mt6360_work_ch2;
static struct hrtimer mt6360_timer_ch1;
static struct hrtimer mt6360_timer_ch2;
static unsigned int mt6360_timeout_ms[MT6360_CHANNEL_NUM];
/* define usage count */
static int use_count;
static int fd_use_count;
/* define RTK flashlight device */
static struct flashlight_device *flashlight_dev_ch1;
static struct flashlight_device *flashlight_dev_ch2;
#define RT_FLED_DEVICE_CH1 "mt-flash-led1"
#define RT_FLED_DEVICE_CH2 "mt-flash-led2"
/* is decrease voltage */
static int is_decrease_voltage;
/* platform data */
struct mt6360_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
#if defined(CONFIG_MACH_MT6877) || defined(CONFIG_MACH_MT6833) \
|| defined(CONFIG_MACH_MT6893) || defined(CONFIG_MACH_MT6785) \
|| defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873)
/* define charger consumer */
static struct charger_consumer *flashlight_charger_consumer;
#define CHARGER_SUPPLY_NAME "charger_port1"
#else
/******************************************************************************
* Charger power supply class
*****************************************************************************/
static int mt6360_high_voltage_supply(int enable)
{
union power_supply_propval prop;
static struct power_supply *chg_psy;
int ret;
if (chg_psy == NULL)
chg_psy = power_supply_get_by_name("mtk-master-charger");
if (chg_psy == NULL || IS_ERR(chg_psy)) {
pr_notice("%s Couldn't get chg_psy\n", __func__);
ret = -1;
} else {
prop.intval = enable;
ret = power_supply_set_property(chg_psy,
POWER_SUPPLY_PROP_VOLTAGE_MAX, &prop);
pr_notice("%s enable_hv:%d\n", __func__, prop.intval);
power_supply_changed(chg_psy);
}
return ret;
}
#endif
/******************************************************************************
* mt6360 operations
*****************************************************************************/
static const int mt6360_current[MT6360_LEVEL_NUM] = {
25, 50, 75, 100, 125, 150, 175, 200, 225, 250,
275, 300, 325, 350, 375, 400, 450, 500, 550, 600,
650, 700, 750, 800, 850, 900, 950, 1000, 1050, 1100,
1150, 1200
};
static const unsigned char mt6360_torch_level[MT6360_LEVEL_TORCH] = {
0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x12,
0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1E
};
/* 0x00~0x74 6.25mA/step 0x75~0xB1 12.5mA/step */
static const unsigned char mt6360_strobe_level[MT6360_LEVEL_FLASH] = {
0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C, 0x20, 0x24,
0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C, 0x44, 0x4C, 0x54, 0x5C,
0x64, 0x6C, 0x74, 0x78, 0x7C, 0x80, 0x84, 0x88, 0x8C, 0x90,
0x94, 0x98
};
static int mt6360_decouple_mode;
static int mt6360_en_ch1;
static int mt6360_en_ch2;
static int mt6360_level_ch1;
static int mt6360_level_ch2;
static int mt6360_is_charger_ready(void)
{
if (flashlight_is_ready(flashlight_dev_ch1) &&
flashlight_is_ready(flashlight_dev_ch2))
return FLASHLIGHT_CHARGER_READY;
else
return FLASHLIGHT_CHARGER_NOT_READY;
}
static int mt6360_is_torch(int level)
{
if (level >= MT6360_LEVEL_TORCH)
return -1;
return 0;
}
static int mt6360_verify_level(int level)
{
if (level < 0)
level = 0;
else if (level >= MT6360_LEVEL_NUM)
level = MT6360_LEVEL_NUM - 1;
return level;
}
/* flashlight enable function */
static int mt6360_enable(void)
{
int ret = 0;
enum flashlight_mode mode = FLASHLIGHT_MODE_TORCH;
if (!flashlight_dev_ch1 || !flashlight_dev_ch2) {
pr_info("Failed to enable since no flashlight device.\n");
return -1;
}
/* set flash mode if any channel is flash mode */
if ((mt6360_en_ch1 == MT6360_ENABLE_FLASH)
|| (mt6360_en_ch2 == MT6360_ENABLE_FLASH))
mode = FLASHLIGHT_MODE_FLASH;
pr_debug("enable(%d,%d), mode:%d.\n",
mt6360_en_ch1, mt6360_en_ch2, mode);
/* enable channel 1 and channel 2 */
if (mt6360_decouple_mode == FLASHLIGHT_SCENARIO_COUPLE &&
mt6360_en_ch1 != MT6360_DISABLE &&
mt6360_en_ch2 != MT6360_DISABLE) {
pr_info("dual flash mode\n");
if (mode == FLASHLIGHT_MODE_TORCH)
ret |= flashlight_set_mode(
flashlight_dev_ch1, FLASHLIGHT_MODE_DUAL_TORCH);
else
ret |= flashlight_set_mode(
flashlight_dev_ch1, FLASHLIGHT_MODE_DUAL_FLASH);
} else {
if (mt6360_en_ch1)
ret |= flashlight_set_mode(
flashlight_dev_ch1, mode);
else if (mt6360_decouple_mode == FLASHLIGHT_SCENARIO_COUPLE)
ret |= flashlight_set_mode(
flashlight_dev_ch1, FLASHLIGHT_MODE_OFF);
if (mt6360_en_ch2)
ret |= flashlight_set_mode(
flashlight_dev_ch2, mode);
else if (mt6360_decouple_mode == FLASHLIGHT_SCENARIO_COUPLE)
ret |= flashlight_set_mode(
flashlight_dev_ch2, FLASHLIGHT_MODE_OFF);
}
if (ret < 0)
pr_info("Failed to enable.\n");
return ret;
}
/* flashlight disable function */
static int mt6360_disable_ch1(void)
{
int ret = 0;
pr_debug("disable_ch1.\n");
if (!flashlight_dev_ch1) {
pr_info("Failed to disable since no flashlight device.\n");
return -1;
}
ret |= flashlight_set_mode(flashlight_dev_ch1, FLASHLIGHT_MODE_OFF);
if (ret < 0)
pr_info("Failed to disable.\n");
return ret;
}
static int mt6360_disable_ch2(void)
{
int ret = 0;
pr_debug("disable_ch2.\n");
if (!flashlight_dev_ch2) {
pr_info("Failed to disable since no flashlight device.\n");
return -1;
}
ret |= flashlight_set_mode(flashlight_dev_ch2, FLASHLIGHT_MODE_OFF);
if (ret < 0)
pr_info("Failed to disable.\n");
return ret;
}
static int mt6360_disable_all(void)
{
int ret = 0;
pr_debug("disable_ch1.\n");
if (!flashlight_dev_ch1) {
pr_info("Failed to disable since no flashlight device.\n");
return -1;
}
ret |= flashlight_set_mode(flashlight_dev_ch1,
FLASHLIGHT_MODE_DUAL_OFF);
if (ret < 0)
pr_info("Failed to disable.\n");
return ret;
}
static int mt6360_disable(int channel)
{
int ret = 0;
if (channel == MT6360_CHANNEL_CH1)
ret = mt6360_disable_ch1();
else if (channel == MT6360_CHANNEL_CH2)
ret = mt6360_disable_ch2();
else if (channel == MT6360_CHANNEL_ALL)
ret = mt6360_disable_all();
else {
pr_info("Error channel\n");
return -1;
}
return ret;
}
/* set flashlight level */
static int mt6360_set_level_ch1(int level)
{
level = mt6360_verify_level(level);
mt6360_level_ch1 = level;
if (!flashlight_dev_ch1) {
pr_info("Failed to set ht level since no flashlight device.\n");
return -1;
}
/* set brightness level */
if (!mt6360_is_torch(level))
flashlight_set_torch_brightness(
flashlight_dev_ch1, mt6360_torch_level[level]);
flashlight_set_strobe_brightness(
flashlight_dev_ch1, mt6360_strobe_level[level]);
return 0;
}
static int mt6360_set_level_ch2(int level)
{
level = mt6360_verify_level(level);
mt6360_level_ch2 = level;
if (!flashlight_dev_ch2) {
pr_info("Failed to set lt level since no flashlight device.\n");
return -1;
}
/* set brightness level */
if (!mt6360_is_torch(level))
flashlight_set_torch_brightness(
flashlight_dev_ch2, mt6360_torch_level[level]);
flashlight_set_strobe_brightness(
flashlight_dev_ch2, mt6360_strobe_level[level]);
return 0;
}
static int mt6360_set_level(int channel, int level)
{
if (channel == MT6360_CHANNEL_CH1)
mt6360_set_level_ch1(level);
else if (channel == MT6360_CHANNEL_CH2)
mt6360_set_level_ch2(level);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int mt6360_set_scenario(int scenario)
{
/* set decouple mode */
mt6360_decouple_mode = scenario & FLASHLIGHT_SCENARIO_DECOUPLE_MASK;
/* notify charger to increase or decrease voltage */
mutex_lock(&mt6360_mutex);
if (scenario & FLASHLIGHT_SCENARIO_CAMERA_MASK) {
if (!is_decrease_voltage) {
pr_info("Decrease voltage level.\n");
#if defined(CONFIG_MACH_MT6877) || defined(CONFIG_MACH_MT6833) \
|| defined(CONFIG_MACH_MT6893) || defined(CONFIG_MACH_MT6785) \
|| defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873)
charger_manager_enable_high_voltage_charging(
flashlight_charger_consumer, false);
#else
mt6360_high_voltage_supply(0);
#endif
is_decrease_voltage = 1;
}
} else {
if (is_decrease_voltage) {
pr_info("Increase voltage level.\n");
#if defined(CONFIG_MACH_MT6877) || defined(CONFIG_MACH_MT6833) \
|| defined(CONFIG_MACH_MT6893) || defined(CONFIG_MACH_MT6785) \
|| defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873)
charger_manager_enable_high_voltage_charging(
flashlight_charger_consumer, true);
#else
mt6360_high_voltage_supply(1);
#endif
is_decrease_voltage = 0;
}
}
mutex_unlock(&mt6360_mutex);
return 0;
}
/* flashlight init */
static int mt6360_init(void)
{
/* clear flashlight state */
mt6360_en_ch1 = MT6360_NONE;
mt6360_en_ch2 = MT6360_NONE;
/* clear decouple mode */
mt6360_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
/* clear charger status */
is_decrease_voltage = 0;
return 0;
}
/* flashlight uninit */
static int mt6360_uninit(void)
{
int ret;
/* clear flashlight state */
mt6360_en_ch1 = MT6360_NONE;
mt6360_en_ch2 = MT6360_NONE;
/* clear decouple mode */
mt6360_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
/* clear charger status */
is_decrease_voltage = 0;
ret = mt6360_disable(MT6360_CHANNEL_ALL);
return ret;
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static void mt6360_work_disable_ch1(struct work_struct *data)
{
pr_debug("ht work queue callback\n");
mt6360_disable(MT6360_CHANNEL_CH1);
}
static void mt6360_work_disable_ch2(struct work_struct *data)
{
pr_debug("lt work queue callback\n");
mt6360_disable(MT6360_CHANNEL_CH2);
}
static enum hrtimer_restart mt6360_timer_func_ch1(struct hrtimer *timer)
{
schedule_work(&mt6360_work_ch1);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart mt6360_timer_func_ch2(struct hrtimer *timer)
{
schedule_work(&mt6360_work_ch2);
return HRTIMER_NORESTART;
}
static int mt6360_timer_start(int channel, ktime_t ktime)
{
if (channel == MT6360_CHANNEL_CH1)
hrtimer_start(&mt6360_timer_ch1, ktime, HRTIMER_MODE_REL);
else if (channel == MT6360_CHANNEL_CH2)
hrtimer_start(&mt6360_timer_ch2, ktime, HRTIMER_MODE_REL);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int mt6360_timer_cancel(int channel)
{
if (channel == MT6360_CHANNEL_CH1)
hrtimer_cancel(&mt6360_timer_ch1);
else if (channel == MT6360_CHANNEL_CH2)
hrtimer_cancel(&mt6360_timer_ch2);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
/******************************************************************************
* Flashlight operation wrapper function
*****************************************************************************/
static int mt6360_operate(int channel, int enable)
{
ktime_t ktime;
unsigned int s;
unsigned int ns;
/* setup enable/disable */
if (channel == MT6360_CHANNEL_CH1) {
mt6360_en_ch1 = enable;
if (mt6360_en_ch1)
if (mt6360_is_torch(mt6360_level_ch1))
mt6360_en_ch1 = MT6360_ENABLE_FLASH;
} else if (channel == MT6360_CHANNEL_CH2) {
mt6360_en_ch2 = enable;
if (mt6360_en_ch2)
if (mt6360_is_torch(mt6360_level_ch2))
mt6360_en_ch2 = MT6360_ENABLE_FLASH;
} else {
pr_info("Error channel\n");
return -1;
}
/* decouple mode */
if (mt6360_decouple_mode) {
if (channel == MT6360_CHANNEL_CH1) {
mt6360_en_ch2 = MT6360_DISABLE;
mt6360_timeout_ms[MT6360_CHANNEL_CH2] = 0;
} else if (channel == MT6360_CHANNEL_CH2) {
mt6360_en_ch1 = MT6360_DISABLE;
mt6360_timeout_ms[MT6360_CHANNEL_CH1] = 0;
}
}
pr_debug("en_ch(%d,%d), decouple:%d\n",
mt6360_en_ch1, mt6360_en_ch2, mt6360_decouple_mode);
/* operate flashlight and setup timer */
if ((mt6360_en_ch1 != MT6360_NONE) && (mt6360_en_ch2 != MT6360_NONE)) {
if ((mt6360_en_ch1 == MT6360_DISABLE) &&
(mt6360_en_ch2 == MT6360_DISABLE)) {
if (mt6360_decouple_mode) {
if (channel == MT6360_CHANNEL_CH1) {
mt6360_disable(MT6360_CHANNEL_CH1);
mt6360_timer_cancel(MT6360_CHANNEL_CH1);
} else if (channel == MT6360_CHANNEL_CH2) {
mt6360_disable(MT6360_CHANNEL_CH2);
mt6360_timer_cancel(MT6360_CHANNEL_CH2);
}
} else {
mt6360_disable(MT6360_CHANNEL_ALL);
mt6360_timer_cancel(MT6360_CHANNEL_CH1);
mt6360_timer_cancel(MT6360_CHANNEL_CH2);
}
} else {
if (mt6360_timeout_ms[MT6360_CHANNEL_CH1] &&
mt6360_en_ch1 != MT6360_DISABLE) {
s = mt6360_timeout_ms[MT6360_CHANNEL_CH1] /
1000;
ns = mt6360_timeout_ms[MT6360_CHANNEL_CH1] %
1000 * 1000000;
ktime = ktime_set(s, ns);
mt6360_timer_start(MT6360_CHANNEL_CH1, ktime);
}
if (mt6360_timeout_ms[MT6360_CHANNEL_CH2] &&
mt6360_en_ch2 != MT6360_DISABLE) {
s = mt6360_timeout_ms[MT6360_CHANNEL_CH2] /
1000;
ns = mt6360_timeout_ms[MT6360_CHANNEL_CH2] %
1000 * 1000000;
ktime = ktime_set(s, ns);
mt6360_timer_start(MT6360_CHANNEL_CH2, ktime);
}
mt6360_enable();
}
/* clear flashlight state */
mt6360_en_ch1 = MT6360_NONE;
mt6360_en_ch2 = MT6360_NONE;
}
return 0;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int mt6360_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
/* verify channel */
if (channel < 0 || channel >= MT6360_CHANNEL_NUM) {
pr_info("Failed with error channel\n");
return -EINVAL;
}
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
mt6360_timeout_ms[channel] = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
mt6360_set_level(channel, fl_arg->arg);
break;
case FLASH_IOC_SET_SCENARIO:
pr_debug("FLASH_IOC_SET_SCENARIO(%d): %d\n",
channel, (int)fl_arg->arg);
mt6360_set_scenario(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
mt6360_operate(channel, fl_arg->arg);
break;
case FLASH_IOC_IS_CHARGER_READY:
pr_debug("FLASH_IOC_IS_CHARGER_READY(%d)\n", channel);
fl_arg->arg = mt6360_is_charger_ready();
pr_debug("FLASH_IOC_IS_CHARGER_READY(%d)\n", fl_arg->arg);
break;
case FLASH_IOC_GET_DUTY_NUMBER:
pr_debug("FLASH_IOC_GET_DUTY_NUMBER(%d)\n", channel);
fl_arg->arg = MT6360_LEVEL_NUM;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_debug("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n", channel);
fl_arg->arg = MT6360_LEVEL_TORCH - 1;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = mt6360_verify_level(fl_arg->arg);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel, (int)fl_arg->arg);
fl_arg->arg = mt6360_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_TIMEOUT:
pr_debug("FLASH_IOC_GET_HW_TIMEOUT(%d)\n", channel);
fl_arg->arg = MT6360_HW_TIMEOUT;
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int mt6360_open(void)
{
/* Move to set driver for saving power */
mutex_lock(&mt6360_mutex);
fd_use_count++;
pr_debug("open driver: %d\n", fd_use_count);
mutex_unlock(&mt6360_mutex);
return 0;
}
static int mt6360_release(void)
{
/* Move to set driver for saving power */
mutex_lock(&mt6360_mutex);
fd_use_count--;
pr_debug("close driver: %d\n", fd_use_count);
/* If camera NE, we need to enable pe by ourselves*/
if (fd_use_count == 0 && is_decrease_voltage) {
pr_info("Increase voltage level.\n");
#if defined(CONFIG_MACH_MT6877) || defined(CONFIG_MACH_MT6833) \
|| defined(CONFIG_MACH_MT6893) || defined(CONFIG_MACH_MT6785) \
|| defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873)
charger_manager_enable_high_voltage_charging(
flashlight_charger_consumer, true);
#else
mt6360_high_voltage_supply(1);
#endif
is_decrease_voltage = 0;
}
mutex_unlock(&mt6360_mutex);
return 0;
}
static int mt6360_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&mt6360_mutex);
if (set) {
if (!use_count)
ret = mt6360_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = mt6360_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&mt6360_mutex);
return ret;
}
static ssize_t mt6360_strobe_store(struct flashlight_arg arg)
{
mt6360_set_driver(1);
if (arg.decouple)
mt6360_set_scenario(
FLASHLIGHT_SCENARIO_CAMERA |
FLASHLIGHT_SCENARIO_DECOUPLE);
else
mt6360_set_scenario(
FLASHLIGHT_SCENARIO_CAMERA |
FLASHLIGHT_SCENARIO_COUPLE);
mt6360_set_level(arg.channel, arg.level);
mt6360_timeout_ms[arg.channel] = 0;
if (arg.level < 0)
mt6360_operate(arg.channel, MT6360_DISABLE);
else
mt6360_operate(arg.channel, MT6360_ENABLE);
msleep(arg.dur);
if (arg.decouple)
mt6360_set_scenario(
FLASHLIGHT_SCENARIO_FLASHLIGHT |
FLASHLIGHT_SCENARIO_DECOUPLE);
else
mt6360_set_scenario(
FLASHLIGHT_SCENARIO_FLASHLIGHT |
FLASHLIGHT_SCENARIO_COUPLE);
mt6360_operate(arg.channel, MT6360_DISABLE);
mt6360_set_driver(0);
return 0;
}
static struct flashlight_operations mt6360_ops = {
mt6360_open,
mt6360_release,
mt6360_ioctl,
mt6360_strobe_store,
mt6360_set_driver
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int mt6360_parse_dt(struct device *dev,
struct mt6360_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
MT6360_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int mt6360_probe(struct platform_device *pdev)
{
struct mt6360_platform_data *pdata = dev_get_platdata(&pdev->dev);
int ret;
int i;
pr_debug("Probe start.\n");
/* parse dt */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return -ENOMEM;
pdev->dev.platform_data = pdata;
ret = mt6360_parse_dt(&pdev->dev, pdata);
if (ret)
return ret;
}
/* init work queue */
INIT_WORK(&mt6360_work_ch1, mt6360_work_disable_ch1);
INIT_WORK(&mt6360_work_ch2, mt6360_work_disable_ch2);
/* init timer */
hrtimer_init(&mt6360_timer_ch1, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mt6360_timer_ch1.function = mt6360_timer_func_ch1;
hrtimer_init(&mt6360_timer_ch2, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mt6360_timer_ch2.function = mt6360_timer_func_ch2;
mt6360_timeout_ms[MT6360_CHANNEL_CH1] = 600;
mt6360_timeout_ms[MT6360_CHANNEL_CH2] = 600;
/* clear attributes */
use_count = 0;
fd_use_count = 0;
is_decrease_voltage = 0;
/* get RTK flashlight handler */
flashlight_dev_ch1 = find_flashlight_by_name(RT_FLED_DEVICE_CH1);
if (!flashlight_dev_ch1) {
pr_info("Failed to get ht flashlight device.\n");
return -EFAULT;
}
flashlight_dev_ch2 = find_flashlight_by_name(RT_FLED_DEVICE_CH2);
if (!flashlight_dev_ch2) {
pr_info("Failed to get lt flashlight device.\n");
return -EFAULT;
}
/* setup strobe mode timeout */
if (flashlight_set_strobe_timeout(flashlight_dev_ch1,
MT6360_HW_TIMEOUT, MT6360_HW_TIMEOUT + 200) < 0)
pr_info("Failed to set strobe timeout.\n");
#if defined(CONFIG_MACH_MT6877) || defined(CONFIG_MACH_MT6833) \
|| defined(CONFIG_MACH_MT6893) || defined(CONFIG_MACH_MT6785) \
|| defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873)
/* get charger consumer manager */
flashlight_charger_consumer = charger_manager_get_by_name(
&flashlight_dev_ch1->dev, CHARGER_SUPPLY_NAME);
if (!flashlight_charger_consumer) {
pr_info("Failed to get charger manager.\n");
return -EFAULT;
}
#endif
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&mt6360_ops))
return -EFAULT;
} else {
if (flashlight_dev_register(MT6360_NAME, &mt6360_ops))
return -EFAULT;
}
pr_debug("Probe done.\n");
return 0;
}
static int mt6360_remove(struct platform_device *pdev)
{
struct mt6360_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(MT6360_NAME);
/* flush work queue */
flush_work(&mt6360_work_ch1);
flush_work(&mt6360_work_ch2);
/* clear RTK flashlight device */
flashlight_dev_ch1 = NULL;
flashlight_dev_ch2 = NULL;
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id mt6360_of_match[] = {
{.compatible = MT6360_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, mt6360_of_match);
#else
static struct platform_device mt6360_platform_device[] = {
{
.name = MT6360_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, mt6360_platform_device);
#endif
static struct platform_driver mt6360_platform_driver = {
.probe = mt6360_probe,
.remove = mt6360_remove,
.driver = {
.name = MT6360_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = mt6360_of_match,
#endif
},
};
static int __init flashlight_mt6360_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&mt6360_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&mt6360_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_mt6360_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&mt6360_platform_driver);
pr_debug("Exit done.\n");
}
/* replace module_init() since conflict in kernel init process */
late_initcall(flashlight_mt6360_init);
module_exit(flashlight_mt6360_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Roger Wang <Roger-HY.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight MT6360 Driver");
@@ -0,0 +1,984 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/power_supply.h>
#include "richtek/rt-flashlight.h"
#include "v1/mtk_charger.h"
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* device tree should be defined in flashlight-dt.h */
#ifndef MT6370_DTNAME
#define MT6370_DTNAME "mediatek,flashlights_mt6370"
#endif
#define MT6370_NAME "flashlights-mt6370"
/* define channel, level */
#define MT6370_CHANNEL_NUM 2
#define MT6370_CHANNEL_CH1 0
#define MT6370_CHANNEL_CH2 1
#define MT6370_CHANNEL_ALL 2
#define MT6370_NONE (-1)
#define MT6370_DISABLE 0
#define MT6370_ENABLE 1
#define MT6370_ENABLE_TORCH 1
#define MT6370_ENABLE_FLASH 2
#define MT6370_LEVEL_NUM 32
#define MT6370_LEVEL_TORCH 16
#define MT6370_LEVEL_FLASH MT6370_LEVEL_NUM
#define MT6370_WDT_TIMEOUT 1248 /* ms */
#define MT6370_HW_TIMEOUT 400 /* ms */
/* define mutex, work queue and timer */
static DEFINE_MUTEX(mt6370_mutex);
static struct work_struct mt6370_work_ch1;
static struct work_struct mt6370_work_ch2;
static struct hrtimer mt6370_timer_ch1;
static struct hrtimer mt6370_timer_ch2;
static unsigned int mt6370_timeout_ms[MT6370_CHANNEL_NUM];
/* define usage count */
static int use_count;
static int fd_use_count;
/* define RTK flashlight device */
static struct flashlight_device *flashlight_dev_ch1;
static struct flashlight_device *flashlight_dev_ch2;
#define RT_FLED_DEVICE_CH1 "mt-flash-led1"
#define RT_FLED_DEVICE_CH2 "mt-flash-led2"
#if defined(CONFIG_MACH_MT6768) || defined(CONFIG_MACH_MT6781)
/* define charger consumer */
static struct charger_consumer *flashlight_charger_consumer;
#define CHARGER_SUPPLY_NAME "charger_port1"
#endif
/* is decrease voltage */
static int is_decrease_voltage;
/* platform data */
struct mt6370_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
#if !defined(CONFIG_MACH_MT6768) && !defined(CONFIG_MACH_MT6781)
/******************************************************************************
* Charger power supply class
*****************************************************************************/
static int mt6370_high_voltage_supply(int enable)
{
union power_supply_propval prop;
static struct power_supply *chg_psy;
int ret;
if (chg_psy == NULL)
chg_psy = power_supply_get_by_name("mtk-master-charger");
if (chg_psy == NULL || IS_ERR(chg_psy)) {
pr_notice("%s Couldn't get chg_psy\n", __func__);
ret = -1;
} else {
prop.intval = enable;
ret = power_supply_set_property(chg_psy,
POWER_SUPPLY_PROP_VOLTAGE_MAX, &prop);
pr_notice("%s enable_hv:%d\n", __func__, prop.intval);
power_supply_changed(chg_psy);
}
return ret;
}
#endif
/******************************************************************************
* mt6370 operations
*****************************************************************************/
static const int mt6370_current[MT6370_LEVEL_NUM] = {
25, 50, 75, 100, 125, 150, 175, 200, 225, 250,
275, 300, 325, 350, 375, 400, 450, 500, 550, 600,
650, 700, 750, 800, 850, 900, 950, 1000, 1050, 1100,
1150, 1200
};
static const unsigned char mt6370_torch_level[MT6370_LEVEL_TORCH] = {
0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x12,
0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1E
};
static const unsigned char mt6370_strobe_level[MT6370_LEVEL_FLASH] = {
0x80, 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10,
0x12, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x20, 0x24, 0x28, 0x2C,
0x30, 0x34, 0x38, 0x3C, 0x40, 0x44, 0x48, 0x4C, 0x50, 0x54,
0x58, 0x5C
};
static int mt6370_decouple_mode;
static int mt6370_en_ch1;
static int mt6370_en_ch2;
static int mt6370_level_ch1;
static int mt6370_level_ch2;
static int mt6370_is_charger_ready(void)
{
if (flashlight_is_ready(flashlight_dev_ch1) &&
flashlight_is_ready(flashlight_dev_ch2))
return FLASHLIGHT_CHARGER_READY;
else
return FLASHLIGHT_CHARGER_NOT_READY;
}
static int mt6370_is_torch(int level)
{
if (level >= MT6370_LEVEL_TORCH)
return -1;
return 0;
}
static int mt6370_verify_level(int level)
{
if (level < 0)
level = 0;
else if (level >= MT6370_LEVEL_NUM)
level = MT6370_LEVEL_NUM - 1;
return level;
}
/* flashlight enable function */
static int mt6370_enable(void)
{
int ret = 0;
enum flashlight_mode mode = FLASHLIGHT_MODE_TORCH;
if (!flashlight_dev_ch1 || !flashlight_dev_ch2) {
pr_info("Failed to enable since no flashlight device.\n");
return -1;
}
/* set flash mode if any channel is flash mode */
if ((mt6370_en_ch1 == MT6370_ENABLE_FLASH)
|| (mt6370_en_ch2 == MT6370_ENABLE_FLASH))
mode = FLASHLIGHT_MODE_FLASH;
pr_debug("enable(%d,%d), mode:%d.\n",
mt6370_en_ch1, mt6370_en_ch2, mode);
/* enable channel 1 and channel 2 */
if (mt6370_decouple_mode == FLASHLIGHT_SCENARIO_COUPLE &&
mt6370_en_ch1 != MT6370_DISABLE &&
mt6370_en_ch2 != MT6370_DISABLE) {
pr_info("dual flash mode\n");
if (mode == FLASHLIGHT_MODE_TORCH)
ret |= flashlight_set_mode(
flashlight_dev_ch1, FLASHLIGHT_MODE_DUAL_TORCH);
else
ret |= flashlight_set_mode(
flashlight_dev_ch1, FLASHLIGHT_MODE_DUAL_FLASH);
} else {
if (mt6370_en_ch1)
ret |= flashlight_set_mode(
flashlight_dev_ch1, mode);
else if (mt6370_decouple_mode == FLASHLIGHT_SCENARIO_COUPLE)
ret |= flashlight_set_mode(
flashlight_dev_ch1, FLASHLIGHT_MODE_OFF);
if (mt6370_en_ch2)
ret |= flashlight_set_mode(
flashlight_dev_ch2, mode);
else if (mt6370_decouple_mode == FLASHLIGHT_SCENARIO_COUPLE)
ret |= flashlight_set_mode(
flashlight_dev_ch2, FLASHLIGHT_MODE_OFF);
}
if (ret < 0)
pr_info("Failed to enable.\n");
return ret;
}
/* flashlight disable function */
static int mt6370_disable_ch1(void)
{
int ret = 0;
pr_debug("disable_ch1.\n");
if (!flashlight_dev_ch1) {
pr_info("Failed to disable since no flashlight device.\n");
return -1;
}
ret |= flashlight_set_mode(flashlight_dev_ch1, FLASHLIGHT_MODE_OFF);
if (ret < 0)
pr_info("Failed to disable.\n");
return ret;
}
static int mt6370_disable_ch2(void)
{
int ret = 0;
pr_debug("disable_ch2.\n");
if (!flashlight_dev_ch2) {
pr_info("Failed to disable since no flashlight device.\n");
return -1;
}
ret |= flashlight_set_mode(flashlight_dev_ch2, FLASHLIGHT_MODE_OFF);
if (ret < 0)
pr_info("Failed to disable.\n");
return ret;
}
static int mt6370_disable_all(void)
{
int ret = 0;
pr_debug("disable_ch1.\n");
if (!flashlight_dev_ch1) {
pr_info("Failed to disable since no flashlight device.\n");
return -1;
}
ret |= flashlight_set_mode(flashlight_dev_ch1,
FLASHLIGHT_MODE_DUAL_OFF);
if (ret < 0)
pr_info("Failed to disable.\n");
return ret;
}
static int mt6370_disable(int channel)
{
int ret = 0;
if (channel == MT6370_CHANNEL_CH1)
ret = mt6370_disable_ch1();
else if (channel == MT6370_CHANNEL_CH2)
ret = mt6370_disable_ch2();
else if (channel == MT6370_CHANNEL_ALL)
ret = mt6370_disable_all();
else {
pr_info("Error channel\n");
return -1;
}
return ret;
}
/* set flashlight level */
static int mt6370_set_level_ch1(int level)
{
level = mt6370_verify_level(level);
mt6370_level_ch1 = level;
if (!flashlight_dev_ch1) {
pr_info("Failed to set ht level since no flashlight device.\n");
return -1;
}
if (level < 0)
return -1;
/* set brightness level */
if (!mt6370_is_torch(level))
flashlight_set_torch_brightness(
flashlight_dev_ch1, mt6370_torch_level[level]);
flashlight_set_strobe_brightness(
flashlight_dev_ch1, mt6370_strobe_level[level]);
return 0;
}
static int mt6370_set_level_ch2(int level)
{
level = mt6370_verify_level(level);
mt6370_level_ch2 = level;
if (!flashlight_dev_ch2) {
pr_info("Failed to set lt level since no flashlight device.\n");
return -1;
}
if (level < 0)
return -1;
/* set brightness level */
if (!mt6370_is_torch(level))
flashlight_set_torch_brightness(
flashlight_dev_ch2, mt6370_torch_level[level]);
flashlight_set_strobe_brightness(
flashlight_dev_ch2, mt6370_strobe_level[level]);
return 0;
}
static int mt6370_set_level(int channel, int level)
{
if (channel == MT6370_CHANNEL_CH1)
mt6370_set_level_ch1(level);
else if (channel == MT6370_CHANNEL_CH2)
mt6370_set_level_ch2(level);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int mt6370_set_scenario(int scenario)
{
/* set decouple mode */
mt6370_decouple_mode = scenario & FLASHLIGHT_SCENARIO_DECOUPLE_MASK;
/* notify charger to increase or decrease voltage */
mutex_lock(&mt6370_mutex);
if (scenario & FLASHLIGHT_SCENARIO_CAMERA_MASK) {
if (!is_decrease_voltage) {
pr_info("Decrease voltage level.\n");
#if defined(CONFIG_MACH_MT6768) || defined(CONFIG_MACH_MT6781)
charger_manager_enable_high_voltage_charging(
flashlight_charger_consumer, false);
#else
mt6370_high_voltage_supply(0);
#endif
is_decrease_voltage = 1;
}
} else {
if (is_decrease_voltage) {
pr_info("Increase voltage level.\n");
#if defined(CONFIG_MACH_MT6768) || defined(CONFIG_MACH_MT6781)
charger_manager_enable_high_voltage_charging(
flashlight_charger_consumer, true);
#else
mt6370_high_voltage_supply(1);
#endif
is_decrease_voltage = 0;
}
}
mutex_unlock(&mt6370_mutex);
return 0;
}
/* flashlight init */
static int mt6370_init(void)
{
/* clear flashlight state */
mt6370_en_ch1 = MT6370_NONE;
mt6370_en_ch2 = MT6370_NONE;
/* clear decouple mode */
mt6370_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
/* clear charger status */
is_decrease_voltage = 0;
return 0;
}
/* flashlight uninit */
static int mt6370_uninit(void)
{
int ret;
/* clear flashlight state */
mt6370_en_ch1 = MT6370_NONE;
mt6370_en_ch2 = MT6370_NONE;
/* clear decouple mode */
mt6370_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
/* clear charger status */
is_decrease_voltage = 0;
ret = mt6370_disable(MT6370_CHANNEL_ALL);
return ret;
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static void mt6370_work_disable_ch1(struct work_struct *data)
{
pr_debug("ht work queue callback\n");
mt6370_disable(MT6370_CHANNEL_CH1);
}
static void mt6370_work_disable_ch2(struct work_struct *data)
{
pr_debug("lt work queue callback\n");
mt6370_disable(MT6370_CHANNEL_CH2);
}
static enum hrtimer_restart mt6370_timer_func_ch1(struct hrtimer *timer)
{
schedule_work(&mt6370_work_ch1);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart mt6370_timer_func_ch2(struct hrtimer *timer)
{
schedule_work(&mt6370_work_ch2);
return HRTIMER_NORESTART;
}
static int mt6370_timer_start(int channel, ktime_t ktime)
{
if (channel == MT6370_CHANNEL_CH1)
hrtimer_start(&mt6370_timer_ch1, ktime, HRTIMER_MODE_REL);
else if (channel == MT6370_CHANNEL_CH2)
hrtimer_start(&mt6370_timer_ch2, ktime, HRTIMER_MODE_REL);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int mt6370_timer_cancel(int channel)
{
if (channel == MT6370_CHANNEL_CH1)
hrtimer_cancel(&mt6370_timer_ch1);
else if (channel == MT6370_CHANNEL_CH2)
hrtimer_cancel(&mt6370_timer_ch2);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
/******************************************************************************
* Flashlight operation wrapper function
*****************************************************************************/
static int mt6370_operate(int channel, int enable)
{
ktime_t ktime;
unsigned int s;
unsigned int ns;
/* setup enable/disable */
if (channel == MT6370_CHANNEL_CH1) {
mt6370_en_ch1 = enable;
if (mt6370_en_ch1)
if (mt6370_is_torch(mt6370_level_ch1))
mt6370_en_ch1 = MT6370_ENABLE_FLASH;
} else if (channel == MT6370_CHANNEL_CH2) {
mt6370_en_ch2 = enable;
if (mt6370_en_ch2)
if (mt6370_is_torch(mt6370_level_ch2))
mt6370_en_ch2 = MT6370_ENABLE_FLASH;
} else {
pr_info("Error channel\n");
return -1;
}
/* decouple mode */
if (mt6370_decouple_mode) {
if (channel == MT6370_CHANNEL_CH1) {
mt6370_en_ch2 = MT6370_DISABLE;
mt6370_timeout_ms[MT6370_CHANNEL_CH2] = 0;
} else if (channel == MT6370_CHANNEL_CH2) {
mt6370_en_ch1 = MT6370_DISABLE;
mt6370_timeout_ms[MT6370_CHANNEL_CH1] = 0;
}
}
pr_debug("en_ch(%d,%d), decouple:%d\n",
mt6370_en_ch1, mt6370_en_ch2, mt6370_decouple_mode);
/* operate flashlight and setup timer */
if ((mt6370_en_ch1 != MT6370_NONE) && (mt6370_en_ch2 != MT6370_NONE)) {
if ((mt6370_en_ch1 == MT6370_DISABLE) &&
(mt6370_en_ch2 == MT6370_DISABLE)) {
if (mt6370_decouple_mode) {
if (channel == MT6370_CHANNEL_CH1) {
mt6370_disable(MT6370_CHANNEL_CH1);
mt6370_timer_cancel(MT6370_CHANNEL_CH1);
} else if (channel == MT6370_CHANNEL_CH2) {
mt6370_disable(MT6370_CHANNEL_CH2);
mt6370_timer_cancel(MT6370_CHANNEL_CH2);
}
} else {
mt6370_disable(MT6370_CHANNEL_ALL);
mt6370_timer_cancel(MT6370_CHANNEL_CH1);
mt6370_timer_cancel(MT6370_CHANNEL_CH2);
}
} else {
if (mt6370_timeout_ms[MT6370_CHANNEL_CH1] &&
mt6370_en_ch1 != MT6370_DISABLE) {
s = mt6370_timeout_ms[MT6370_CHANNEL_CH1] /
1000;
ns = mt6370_timeout_ms[MT6370_CHANNEL_CH1] %
1000 * 1000000;
ktime = ktime_set(s, ns);
mt6370_timer_start(MT6370_CHANNEL_CH1, ktime);
}
if (mt6370_timeout_ms[MT6370_CHANNEL_CH2] &&
mt6370_en_ch2 != MT6370_DISABLE) {
s = mt6370_timeout_ms[MT6370_CHANNEL_CH2] /
1000;
ns = mt6370_timeout_ms[MT6370_CHANNEL_CH2] %
1000 * 1000000;
ktime = ktime_set(s, ns);
mt6370_timer_start(MT6370_CHANNEL_CH2, ktime);
}
mt6370_enable();
}
/* clear flashlight state */
mt6370_en_ch1 = MT6370_NONE;
mt6370_en_ch2 = MT6370_NONE;
}
return 0;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int mt6370_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
/* verify channel */
if (channel < 0 || channel >= MT6370_CHANNEL_NUM) {
pr_info("Failed with error channel\n");
return -EINVAL;
}
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
mt6370_timeout_ms[channel] = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
mt6370_set_level(channel, fl_arg->arg);
break;
case FLASH_IOC_SET_SCENARIO:
pr_debug("FLASH_IOC_SET_SCENARIO(%d): %d\n",
channel, (int)fl_arg->arg);
mt6370_set_scenario(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
mt6370_operate(channel, fl_arg->arg);
break;
case FLASH_IOC_IS_CHARGER_READY:
pr_debug("FLASH_IOC_IS_CHARGER_READY(%d)\n", channel);
fl_arg->arg = mt6370_is_charger_ready();
break;
case FLASH_IOC_GET_DUTY_NUMBER:
pr_debug("FLASH_IOC_GET_DUTY_NUMBER(%d)\n", channel);
fl_arg->arg = MT6370_LEVEL_NUM;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_debug("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n", channel);
fl_arg->arg = MT6370_LEVEL_TORCH - 1;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = mt6370_verify_level(fl_arg->arg);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel, (int)fl_arg->arg);
fl_arg->arg = mt6370_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_TIMEOUT:
pr_debug("FLASH_IOC_GET_HW_TIMEOUT(%d)\n", channel);
fl_arg->arg = MT6370_HW_TIMEOUT;
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int mt6370_open(void)
{
/* Move to set driver for saving power */
mutex_lock(&mt6370_mutex);
fd_use_count++;
pr_debug("open driver: %d\n", fd_use_count);
mutex_unlock(&mt6370_mutex);
return 0;
}
static int mt6370_release(void)
{
/* Move to set driver for saving power */
mutex_lock(&mt6370_mutex);
fd_use_count--;
pr_debug("close driver: %d\n", fd_use_count);
/* If camera NE, we need to enable pe by ourselves*/
if (fd_use_count == 0 && is_decrease_voltage) {
pr_info("Increase voltage level.\n");
#if defined(CONFIG_MACH_MT6768) || defined(CONFIG_MACH_MT6781)
charger_manager_enable_high_voltage_charging(
flashlight_charger_consumer, true);
#else
mt6370_high_voltage_supply(1);
#endif
is_decrease_voltage = 0;
}
mutex_unlock(&mt6370_mutex);
return 0;
}
static int mt6370_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&mt6370_mutex);
if (set) {
if (!use_count)
ret = mt6370_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = mt6370_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&mt6370_mutex);
return ret;
}
static ssize_t mt6370_strobe_store(struct flashlight_arg arg)
{
mt6370_set_driver(1);
if (arg.decouple)
mt6370_set_scenario(
FLASHLIGHT_SCENARIO_CAMERA |
FLASHLIGHT_SCENARIO_DECOUPLE);
else
mt6370_set_scenario(
FLASHLIGHT_SCENARIO_CAMERA |
FLASHLIGHT_SCENARIO_COUPLE);
mt6370_set_level(arg.channel, arg.level);
mt6370_timeout_ms[arg.channel] = 0;
if (arg.level < 0)
mt6370_operate(arg.channel, MT6370_DISABLE);
else
mt6370_operate(arg.channel, MT6370_ENABLE);
msleep(arg.dur);
if (arg.decouple)
mt6370_set_scenario(
FLASHLIGHT_SCENARIO_FLASHLIGHT |
FLASHLIGHT_SCENARIO_DECOUPLE);
else
mt6370_set_scenario(
FLASHLIGHT_SCENARIO_FLASHLIGHT |
FLASHLIGHT_SCENARIO_COUPLE);
mt6370_operate(arg.channel, MT6370_DISABLE);
mt6370_set_driver(0);
return 0;
}
static struct flashlight_operations mt6370_ops = {
mt6370_open,
mt6370_release,
mt6370_ioctl,
mt6370_strobe_store,
mt6370_set_driver
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int mt6370_parse_dt(struct device *dev,
struct mt6370_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
MT6370_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int mt6370_probe(struct platform_device *pdev)
{
struct mt6370_platform_data *pdata = dev_get_platdata(&pdev->dev);
int ret;
int i;
pr_debug("Probe start.\n");
/* parse dt */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return -ENOMEM;
pdev->dev.platform_data = pdata;
ret = mt6370_parse_dt(&pdev->dev, pdata);
if (ret)
return ret;
}
/* init work queue */
INIT_WORK(&mt6370_work_ch1, mt6370_work_disable_ch1);
INIT_WORK(&mt6370_work_ch2, mt6370_work_disable_ch2);
/* init timer */
hrtimer_init(&mt6370_timer_ch1, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mt6370_timer_ch1.function = mt6370_timer_func_ch1;
hrtimer_init(&mt6370_timer_ch2, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mt6370_timer_ch2.function = mt6370_timer_func_ch2;
mt6370_timeout_ms[MT6370_CHANNEL_CH1] = 600;
mt6370_timeout_ms[MT6370_CHANNEL_CH2] = 600;
/* clear attributes */
use_count = 0;
fd_use_count = 0;
is_decrease_voltage = 0;
/* get RTK flashlight handler */
flashlight_dev_ch1 = find_flashlight_by_name(RT_FLED_DEVICE_CH1);
if (!flashlight_dev_ch1) {
pr_info("Failed to get ht flashlight device.\n");
return -EFAULT;
}
flashlight_dev_ch2 = find_flashlight_by_name(RT_FLED_DEVICE_CH2);
if (!flashlight_dev_ch2) {
pr_info("Failed to get lt flashlight device.\n");
return -EFAULT;
}
/* setup strobe mode timeout */
if (flashlight_set_strobe_timeout(flashlight_dev_ch1,
MT6370_HW_TIMEOUT, MT6370_HW_TIMEOUT + 200) < 0)
pr_info("Failed to set strobe timeout.\n");
/* get charger consumer manager */
#if defined(CONFIG_MACH_MT6768) || defined(CONFIG_MACH_MT6781)
flashlight_charger_consumer = charger_manager_get_by_name(
&flashlight_dev_ch1->dev, CHARGER_SUPPLY_NAME);
if (!flashlight_charger_consumer) {
pr_info("Failed to get charger manager.\n");
return -EFAULT;
}
#endif
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&mt6370_ops))
return -EFAULT;
} else {
if (flashlight_dev_register(MT6370_NAME, &mt6370_ops))
return -EFAULT;
}
pr_debug("Probe done.\n");
return 0;
}
static int mt6370_remove(struct platform_device *pdev)
{
struct mt6370_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(MT6370_NAME);
/* flush work queue */
flush_work(&mt6370_work_ch1);
flush_work(&mt6370_work_ch2);
/* clear RTK flashlight device */
flashlight_dev_ch1 = NULL;
flashlight_dev_ch2 = NULL;
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id mt6370_of_match[] = {
{.compatible = MT6370_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, mt6370_of_match);
#else
static struct platform_device mt6370_platform_device[] = {
{
.name = MT6370_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, mt6370_platform_device);
#endif
static struct platform_driver mt6370_platform_driver = {
.probe = mt6370_probe,
.remove = mt6370_remove,
.driver = {
.name = MT6370_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = mt6370_of_match,
#endif
},
};
static int __init flashlight_mt6370_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&mt6370_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&mt6370_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_mt6370_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&mt6370_platform_driver);
pr_debug("Exit done.\n");
}
/* replace module_init() since conflict in kernel init process */
late_initcall(flashlight_mt6370_init);
module_exit(flashlight_mt6370_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight MT6370 Driver");
@@ -0,0 +1,994 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2022 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/time.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/version.h>
#include <linux/mutex.h>
#include <linux/i2c.h>
#include <linux/leds.h>
#include <linux/pinctrl/consumer.h>
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* define device tree */
/* TODO: modify temp device tree name */
#ifndef OCP81375_DTNAME_I2C
#define OCP81375_DTNAME_I2C "mediatek,strobe_main"
#endif
/* define device tree */
/* TODO: modify temp device tree name */
#ifndef OCP81375_DTNAME
#define OCP81375_DTNAME "mediatek,flashlights_ocp81375"
#endif
#define OCP81375_NAME "flashlights-ocp81375"
/* define registers */
#define OCP81375_REG_ENABLE 0x01
#define OCP81375_REG_IVFM 0x02
#define OCP81375_REG_FLASH_LEVEL_LED1 0x03
#define OCP81375_REG_FLASH_LEVEL_LED2 0x04
#define OCP81375_REG_TORCH_LEVEL_LED1 0x05
#define OCP81375_REG_TORCH_LEVEL_LED2 0x06
#define OCP81375_REG_BOOST_CONFIG 0x07
#define OCP81375_REG_TIMING_CONFIG 0x08
#define OCP81375_REG_TEMP 0x09
#define OCP81375_REG_FLAG1 0x0A
#define OCP81375_REG_FLAG2 0x0B
#define OCP81375_REG_DEVICE_ID 0x0C
#define OCP81375_PINCTRL_PIN_HWEN 0
#define OCP81375_PINCTRL_PINSTATE_LOW 0
#define OCP81375_PINCTRL_PINSTATE_HIGH 1
#define OCP81375_PINCTRL_STATE_HWEN_HIGH "ocp81375hwen_high"
#define OCP81375_PINCTRL_STATE_HWEN_LOW "ocp81375hwen_low"
static struct pinctrl *ocp81375_pinctrl;
static struct pinctrl_state *ocp81375_hwen_high;
static struct pinctrl_state *ocp81375_hwen_low;
/* define channel, level */
#define OCP81375_CHANNEL_NUM 2
#define OCP81375_CHANNEL_CH1 0
#define OCP81375_CHANNEL_CH2 1
#define OCP81375_NONE (-1)
#define OCP81375_DISABLE 0
#define OCP81375_ENABLE 1
#define OCP81375_ENABLE_TORCH 1
#define OCP81375_ENABLE_FLASH 2
#define OCP81375_WAIT_TIME 3
#define OCP81375_RETRY_TIMES 3
//IC para struct: some static, some auto generate
struct flashlight_dev_para_struct {
int min_flash_duty;//min duty supported by IC flash mode(auto generate)
int flashmode;//if this time flashlight operate use flash mode(auto generate)
int max_torch_current;//max duty supported by IC torch mode(static, config by developer)
int max_flash_current;//max duty supported by IC flash mode(static, config by developer)
int duty_num;//save duty num(auto generate)
int duty_reg_code[30];//save IC reg code by duty(auto generate)
};
//project para struct: static, config by developer
struct project_current_config_struct {
int hw_limit[2];//hardware limit, [0]torch current limit, [1]flash current limit
int sysui_torch[2];//sysui torch, [0]low1 level current, [1]low2 level current
int fac_flash;//factory mode current
int faceid_torch;//face id current
int torch_360;//torch_360 current
int phonecall;//phonecall reminder current
int torch_duty_range[2];//slide control torch current, [0]min, [1]max
int duty_num;//app duty num
int app_duty_current[30];//app duty current
};
/* define mutex, work queue and timer */
static DEFINE_MUTEX(ocp81375_mutex);
static struct work_struct ocp81375_work_ch1;
static struct work_struct ocp81375_work_ch2;
static struct hrtimer ocp81375_timer_ch1;
static struct hrtimer ocp81375_timer_ch2;
static unsigned int ocp81375_timeout_ms[OCP81375_CHANNEL_NUM];
static struct flashlight_dev_para_struct ocp81375_para[OCP81375_CHANNEL_NUM];
static struct project_current_config_struct
g_project_current_config[OCP81375_CHANNEL_NUM];
/* define usage count */
static int use_count;
static int hwen_count;
/* define i2c */
static struct i2c_client *OCP81375_i2c_client;
/* platform data */
struct ocp81375_platform_data {
u8 torch_pin_enable;
u8 pam_sync_pin_enable;
u8 thermal_comp_mode_enable;
u8 strobe_pin_disable;
u8 vout_mode_enable;
};
/* ocp81375 chip data */
struct ocp81375_chip_data {
struct i2c_client *client;
struct ocp81375_platform_data *pdata;
struct mutex lock;
u8 last_flag;
u8 no_pdata;
};
static int ocp81375_flash_read(struct i2c_client *client, u8 reg)
{
int ret;
//char data = 0;
struct ocp81375_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
ret = i2c_smbus_read_byte_data(client, reg);
mutex_unlock(&chip->lock);
pr_info("%s reg:0x%x val:0x%x\n", __func__, reg, ret);
if (ret < 0)
pr_info("failed reading at 0x%02x\n", reg);
return ret;
}
/******************************************************************************
* ocp81375 operations
*****************************************************************************/
/* i2c wrapper function */
static int ocp81375_flash_write(struct i2c_client *client, u8 reg, u8 val)
{
int ret;
struct ocp81375_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
ret = i2c_smbus_write_byte_data(client, reg, val);
mutex_unlock(&chip->lock);
if (ret < 0)
pr_info("failed writing at 0x%02x\n", reg);
return ret;
}
/******************************************************************************
* Pinctrl configuration
*****************************************************************************/
static int ocp81375_pinctrl_init(struct platform_device *pdev)
{
int ret = 0;
pr_info("%s in\n", __func__);
/* get pinctrl */
ocp81375_pinctrl = devm_pinctrl_get(&pdev->dev);
if (IS_ERR(ocp81375_pinctrl)) {
pr_info("Failed to get flashlight pinctrl.\n");
ret = PTR_ERR(ocp81375_pinctrl);
return ret;
}
/* Flashlight HWEN pin initialization */
ocp81375_hwen_high = pinctrl_lookup_state(ocp81375_pinctrl,
OCP81375_PINCTRL_STATE_HWEN_HIGH);
if (IS_ERR(ocp81375_hwen_high)) {
pr_info("Failed to init (%s)\n",
OCP81375_PINCTRL_STATE_HWEN_HIGH);
ret = PTR_ERR(ocp81375_hwen_high);
}
ocp81375_hwen_low = pinctrl_lookup_state(ocp81375_pinctrl,
OCP81375_PINCTRL_STATE_HWEN_LOW);
if (IS_ERR(ocp81375_hwen_low)) {
pr_info("Failed to init (%s)\n", OCP81375_PINCTRL_STATE_HWEN_LOW);
ret = PTR_ERR(ocp81375_hwen_low);
}
pr_info("%s out\n", __func__);
return ret;
}
static int ocp81375_pinctrl_set(int pin, int state)
{
int ret = 0;
pr_info("%s in\n", __func__);
if (IS_ERR(ocp81375_pinctrl)) {
pr_info("pinctrl is not available\n");
return -1;
}
switch (pin) {
case OCP81375_PINCTRL_PIN_HWEN:
if (state == OCP81375_PINCTRL_PINSTATE_LOW &&
!IS_ERR(ocp81375_hwen_low)) {
pinctrl_select_state(ocp81375_pinctrl, ocp81375_hwen_low);
hwen_count = 1;
} else if (state == OCP81375_PINCTRL_PINSTATE_HIGH &&
!IS_ERR(ocp81375_hwen_high)) {
pinctrl_select_state(ocp81375_pinctrl, ocp81375_hwen_high);
hwen_count = 0;
} else
pr_info("set err, pin(%d) state(%d)\n", pin, state);
break;
default:
pr_info("set err, pin(%d) state(%d)\n", pin, state);
break;
}
pr_info("pin(%d) state(%d)\n", pin, state);
pr_info("%s out\n", __func__);
return ret;
}
static int ocp81375_decouple_mode;
static int ocp81375_keepstate_decouple_mode;
static int ocp81375_en_ch1;
static int ocp81375_en_ch2;
static int ocp81375_set_torch_brightness(int channel,int regval)
{
int led1regval = 0;
if (channel == OCP81375_CHANNEL_CH1){
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_TORCH_LEVEL_LED1,regval&0x7f);
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_TORCH_LEVEL_LED2,0x00);
} else if (channel == OCP81375_CHANNEL_CH2){
/* OCP81375_REG_TORCH_LEVEL_LED1:
* bit7 default=1,LED2 Torch Current is set to LED1 Torch Current,
* bit7 =0,Torch Current is not set to LED1 Torch Current
* before set led2 torch,clear OCP81375_REG_TORCH_LEVEL_LED1 bit6 first
*/
if (ocp81375_keepstate_decouple_mode == 1){//360 torch
led1regval = ocp81375_flash_read(OCP81375_i2c_client,
OCP81375_REG_TORCH_LEVEL_LED1);
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_TORCH_LEVEL_LED2, 0x00);
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_TORCH_LEVEL_LED1, (0x7f & led1regval));
} else {
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_TORCH_LEVEL_LED1,0);
}
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_TORCH_LEVEL_LED2,regval);
} else {
pr_info("Error channel\n");
return -1;
}
mdelay(OCP81375_WAIT_TIME);
return 0;
}
static int ocp81375_set_strobe_brightness(int channel,int regval)
{
int led1regval = 0;
if (channel == OCP81375_CHANNEL_CH1){
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_FLASH_LEVEL_LED1,regval&0x7f);
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_FLASH_LEVEL_LED2,0x00);
} else if (channel == OCP81375_CHANNEL_CH2) {
/* OCP81375_REG_FLASH_LEVEL_LED1:
* bit7 default=1,LED2 Flash Current is set to LED1 Flash Current,
* bit7 =0,Flash Current is not set to LED1 Flash Current
* before set led2 flash,clear OCP81375_REG_FLASH_LEVEL_LED1 bit6 first
*/
if (ocp81375_keepstate_decouple_mode == 1){//360 torch
led1regval = ocp81375_flash_read(OCP81375_i2c_client,
OCP81375_REG_FLASH_LEVEL_LED1);
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_FLASH_LEVEL_LED1,(0x7f&led1regval));
} else {
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_FLASH_LEVEL_LED1,0);
}
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_FLASH_LEVEL_LED2,regval);
} else {
pr_info("Error channel\n");
return -1;
}
mdelay(OCP81375_WAIT_TIME);
return 0;
}
static int ocp81375_is_torch(int channel,int level)
{
if (level >= ocp81375_para[channel].min_flash_duty)
return -1;
return 0;
}
static int ocp81375_verify_level(int channel, int level)
{
if (level < 0)
level = 0;
else if (level >= ocp81375_para[channel].duty_num)
level = ocp81375_para[channel].duty_num - 1;
return level;
}
/* flashlight enable function */
static int ocp81375_enable(int channel)
{
int enableregval = 0;
if (channel == OCP81375_CHANNEL_CH1) {
if (ocp81375_en_ch1 == OCP81375_ENABLE_FLASH) {
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_ENABLE, 0x0D);
} else{
if (ocp81375_keepstate_decouple_mode == 1) {//360 torch
enableregval =
ocp81375_flash_read(OCP81375_i2c_client,
OCP81375_REG_ENABLE);
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_ENABLE, (0x09|enableregval));
} else{
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_ENABLE, 0x09);
}
}
} else{
if (ocp81375_en_ch2 == OCP81375_ENABLE_FLASH) {
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_ENABLE, 0x0E);
} else{
if (ocp81375_keepstate_decouple_mode == 1) {//360 torch
enableregval =
ocp81375_flash_read(OCP81375_i2c_client,
OCP81375_REG_ENABLE);
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_ENABLE, (0x0A|enableregval));
} else{
ocp81375_flash_write(OCP81375_i2c_client,
OCP81375_REG_ENABLE, 0x0A);
}
}
}
return 0;
}
/* flashlight disable function */
static int ocp81375_disable(void)
{
pr_info("%s\n", __func__);
if (hwen_count != 1)
ocp81375_flash_write(OCP81375_i2c_client, OCP81375_REG_ENABLE, 0x00);
return 0;
}
/* set flashlight level */
static int ocp81375_set_level(int channel,int lel)
{
int level = 0;
level = ocp81375_verify_level(channel,lel);
ocp81375_para[channel].flashmode = 0;
if (!ocp81375_is_torch(channel,level)){
ocp81375_set_torch_brightness(
channel, ocp81375_para[channel].duty_reg_code[level]);
} else {
ocp81375_para[channel].flashmode = 1;
ocp81375_set_strobe_brightness(
channel, ocp81375_para[channel].duty_reg_code[level]);
}
return 0;
}
static int ocp81375_set_scenario(int scenario)
{
/* set decouple mode */
ocp81375_decouple_mode = scenario & FLASHLIGHT_SCENARIO_DECOUPLE_MASK;
ocp81375_keepstate_decouple_mode =
scenario & FLASHLIGHT_SCENARIO_KEEPSTATE_DECOUPLE_MASK;
return 0;
}
/* flashlight init */
static int ocp81375_init(void)
{
pr_info("%s\n", __func__);
/* clear flashlight state */
ocp81375_en_ch1 = OCP81375_DISABLE;
ocp81375_en_ch2 = OCP81375_DISABLE;
/* clear decouple mode */
ocp81375_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
ocp81375_keepstate_decouple_mode = FLASHLIGHT_SCENARIO_KEEPSTATE_COUPLE;
ocp81375_pinctrl_set(OCP81375_PINCTRL_PIN_HWEN,
OCP81375_PINCTRL_PINSTATE_HIGH);
mdelay(OCP81375_WAIT_TIME);
ocp81375_flash_write(OCP81375_i2c_client, OCP81375_REG_ENABLE, 0x00);
ocp81375_flash_write(OCP81375_i2c_client, OCP81375_REG_BOOST_CONFIG, 0x09);
ocp81375_flash_write(OCP81375_i2c_client, OCP81375_REG_TIMING_CONFIG, 0x1f);
return 0;
}
/* flashlight uninit */
static int ocp81375_uninit(void)
{
/* clear flashlight state */
ocp81375_en_ch1 = OCP81375_NONE;
ocp81375_en_ch2 = OCP81375_NONE;
ocp81375_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
ocp81375_keepstate_decouple_mode = FLASHLIGHT_SCENARIO_KEEPSTATE_COUPLE;
ocp81375_disable();
ocp81375_pinctrl_set(OCP81375_PINCTRL_PIN_HWEN,
OCP81375_PINCTRL_PINSTATE_LOW);
return 0;
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static void ocp81375_work_disable_ch1(struct work_struct *data)
{
pr_debug("ht work queue callback\n");
ocp81375_disable();
}
static void ocp81375_work_disable_ch2(struct work_struct *data)
{
pr_debug("lt work queue callback\n");
ocp81375_disable();
}
static enum hrtimer_restart ocp81375_timer_func_ch1(struct hrtimer *timer)
{
schedule_work(&ocp81375_work_ch1);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart ocp81375_timer_func_ch2(struct hrtimer *timer)
{
schedule_work(&ocp81375_work_ch2);
return HRTIMER_NORESTART;
}
static int ocp81375_timer_start(int channel, ktime_t ktime)
{
if (channel == OCP81375_CHANNEL_CH1)
hrtimer_start(&ocp81375_timer_ch1, ktime, HRTIMER_MODE_REL);
else if (channel == OCP81375_CHANNEL_CH2)
hrtimer_start(&ocp81375_timer_ch2, ktime, HRTIMER_MODE_REL);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int ocp81375_timer_cancel(int channel)
{
if (channel == OCP81375_CHANNEL_CH1)
hrtimer_cancel(&ocp81375_timer_ch1);
else if (channel == OCP81375_CHANNEL_CH2)
hrtimer_cancel(&ocp81375_timer_ch2);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int ocp81375_get_hw_fault(int num)
{
if (num == 1)
return ocp81375_flash_read(OCP81375_i2c_client,
OCP81375_REG_FLAG1);
else if (num == 2)
return ocp81375_flash_read(OCP81375_i2c_client,
OCP81375_REG_FLAG2);
pr_info("Error num\n");
return 0;
}
static int ocp81375_operate(int channel, int enable)
{
ktime_t ktime;
unsigned int s;
unsigned int ns;
/* setup enable/disable */
if (channel == OCP81375_CHANNEL_CH1) {
ocp81375_en_ch1 = enable;
if (ocp81375_en_ch1 && ocp81375_para[channel].flashmode)
ocp81375_en_ch1 = OCP81375_ENABLE_FLASH;
} else if (channel == OCP81375_CHANNEL_CH2) {
ocp81375_en_ch2 = enable;
if (ocp81375_en_ch2 && ocp81375_para[channel].flashmode)
ocp81375_en_ch2 = OCP81375_ENABLE_FLASH;
} else {
pr_info("Error channel\n");
return -1;
}
/* decouple mode */
if (ocp81375_decouple_mode) {
if (channel == OCP81375_CHANNEL_CH1) {
ocp81375_en_ch2 = OCP81375_DISABLE;
ocp81375_timeout_ms[OCP81375_CHANNEL_CH2] = 0;
} else if (channel == OCP81375_CHANNEL_CH2) {
ocp81375_en_ch1 = OCP81375_DISABLE;
ocp81375_timeout_ms[OCP81375_CHANNEL_CH1] = 0;
}
}
/* operate flashlight and setup timer */
if ((ocp81375_en_ch1 != OCP81375_NONE) && (ocp81375_en_ch2 != OCP81375_NONE)) {
if ((ocp81375_en_ch1 == OCP81375_DISABLE) &&
(ocp81375_en_ch2 == OCP81375_DISABLE)) {
ocp81375_disable();
ocp81375_timer_cancel(OCP81375_CHANNEL_CH1);
ocp81375_timer_cancel(OCP81375_CHANNEL_CH2);
} else {
if (ocp81375_timeout_ms[OCP81375_CHANNEL_CH1] &&
ocp81375_en_ch1 != OCP81375_DISABLE) {
s = ocp81375_timeout_ms[OCP81375_CHANNEL_CH1] /
1000;
ns = ocp81375_timeout_ms[OCP81375_CHANNEL_CH1] %
1000 * 1000000;
ktime = ktime_set(s, ns);
ocp81375_timer_start(OCP81375_CHANNEL_CH1, ktime);
}
if (ocp81375_timeout_ms[OCP81375_CHANNEL_CH2] &&
ocp81375_en_ch2 != OCP81375_DISABLE) {
s = ocp81375_timeout_ms[OCP81375_CHANNEL_CH2] /
1000;
ns = ocp81375_timeout_ms[OCP81375_CHANNEL_CH2] %
1000 * 1000000;
ktime = ktime_set(s, ns);
ocp81375_timer_start(OCP81375_CHANNEL_CH2, ktime);
}
ocp81375_enable(channel);
}
/* clear flashlight state */
if ((ocp81375_keepstate_decouple_mode == 0)
|| (channel != OCP81375_CHANNEL_CH1))
ocp81375_en_ch1 = OCP81375_NONE;
if ((ocp81375_keepstate_decouple_mode == 0)
|| (channel != OCP81375_CHANNEL_CH2))
ocp81375_en_ch2 = OCP81375_NONE;
}
return 0;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int ocp81375_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
ocp81375_timeout_ms[channel] = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
ocp81375_set_level(channel,fl_arg->arg);
break;
case FLASH_IOC_SET_SCENARIO:
pr_debug("FLASH_IOC_SET_SCENARIO(%d): %d\n",
channel, (int)fl_arg->arg);
ocp81375_set_scenario(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
ocp81375_operate(channel, fl_arg->arg);
break;
case FLASH_IOC_GET_DUTY_NUMBER:
channel = (channel==0?1:0);
pr_info("FLASH_IOC_GET_DUTY_NUMBER(%d)\n",
g_project_current_config[0].duty_num);
fl_arg->arg = g_project_current_config[channel].duty_num;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_info("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n",
ocp81375_para[channel].min_flash_duty);
fl_arg->arg = ocp81375_para[channel].min_flash_duty;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = ocp81375_verify_level(channel,fl_arg->arg);
channel = (channel==0?1:0);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel,
g_project_current_config[channel].app_duty_current[fl_arg->arg]);
fl_arg->arg =
g_project_current_config[channel].app_duty_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_FAULT:
pr_debug("FLASH_IOC_GET_HW_FAULT(%d)\n", channel);
fl_arg->arg = ocp81375_get_hw_fault(1);
break;
case FLASH_IOC_GET_HW_FAULT2:
pr_debug("FLASH_IOC_GET_HW_FAULT2(%d)\n", channel);
fl_arg->arg = ocp81375_get_hw_fault(2);
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int ocp81375_open(void)
{
return 0;
}
static int ocp81375_release(void)
{
/* uninit chip and clear usage count */
mutex_lock(&ocp81375_mutex);
use_count--;
if (!use_count)
ocp81375_uninit();
if (use_count < 0)
use_count = 0;
mutex_unlock(&ocp81375_mutex);
pr_info("Release: %d\n", use_count);
return 0;
}
static int ocp81375_set_driver(int set)
{
int ret = 0;
/* init chip and set usage count */
mutex_lock(&ocp81375_mutex);
if (set) {
if (!use_count)
ret = ocp81375_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = ocp81375_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&ocp81375_mutex);
return 0;
}
static ssize_t ocp81375_strobe_store(struct flashlight_arg arg)
{
ocp81375_set_driver(1);
if (arg.decouple)
ocp81375_set_scenario(
FLASHLIGHT_SCENARIO_CAMERA |
FLASHLIGHT_SCENARIO_DECOUPLE);
else
ocp81375_set_scenario(
FLASHLIGHT_SCENARIO_CAMERA |
FLASHLIGHT_SCENARIO_COUPLE);
ocp81375_set_level(arg.channel, arg.level);
ocp81375_timeout_ms[arg.channel] = 0;
if (arg.level < 0)
ocp81375_operate(arg.channel, OCP81375_DISABLE);
else
ocp81375_operate(arg.channel, OCP81375_ENABLE);
msleep(arg.dur);
if (arg.decouple)
ocp81375_set_scenario(
FLASHLIGHT_SCENARIO_FLASHLIGHT |
FLASHLIGHT_SCENARIO_DECOUPLE);
else
ocp81375_set_scenario(
FLASHLIGHT_SCENARIO_FLASHLIGHT |
FLASHLIGHT_SCENARIO_COUPLE);
ocp81375_operate(arg.channel, OCP81375_DISABLE);
ocp81375_set_driver(0);
return 0;
}
static struct flashlight_operations ocp81375_ops = {
ocp81375_open,
ocp81375_release,
ocp81375_ioctl,
ocp81375_strobe_store,
ocp81375_set_driver
};
/******************************************************************************
* I2C device and driver
*****************************************************************************/
static int ocp81375_chip_init(struct ocp81375_chip_data *chip)
{
/* NOTE: Chip initialication move to "set driver" operation
* ocp81375_init();
*/
return 0;
}
static int ocp81375_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct ocp81375_chip_data *chip;
struct ocp81375_platform_data *pdata = client->dev.platform_data;
int err;
pr_info("%s start.\n", __func__);
/* check i2c */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_info("Failed to check i2c functionality.\n");
err = -ENODEV;
goto err_out;
}
/* init chip private data */
chip = kzalloc(sizeof(struct ocp81375_chip_data), GFP_KERNEL);
if (!chip) {
err = -ENOMEM;
goto err_out;
}
chip->client = client;
/* init platform data */
if (!pdata) {
pdata = kzalloc(sizeof(struct ocp81375_platform_data),
GFP_KERNEL);
chip->no_pdata = 1;
}
chip->pdata = pdata;
i2c_set_clientdata(client, chip);
OCP81375_i2c_client = client;
/* init mutex and spinlock */
mutex_init(&chip->lock);
/* init work queue */
INIT_WORK(&ocp81375_work_ch1, ocp81375_work_disable_ch1);
INIT_WORK(&ocp81375_work_ch2, ocp81375_work_disable_ch2);
/* init timer */
hrtimer_init(&ocp81375_timer_ch1, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
ocp81375_timer_ch1.function = ocp81375_timer_func_ch1;
hrtimer_init(&ocp81375_timer_ch2, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
ocp81375_timer_ch2.function = ocp81375_timer_func_ch2;
ocp81375_timeout_ms[OCP81375_CHANNEL_CH1] = 100;
ocp81375_timeout_ms[OCP81375_CHANNEL_CH2] = 100;
/* init chip hw */
ocp81375_chip_init(chip);
/* register flashlight operations */
if (flashlight_dev_register(OCP81375_NAME, &ocp81375_ops)) {
err = -EFAULT;
goto err_free;
}
/* clear usage count */
use_count = 0;
pr_info("Probe done.\n");
return 0;
err_free:
kfree(chip->pdata);
i2c_set_clientdata(client, NULL);
kfree(chip);
err_out:
return err;
}
static int ocp81375_i2c_remove(struct i2c_client *client)
{
struct ocp81375_chip_data *chip = i2c_get_clientdata(client);
pr_info("Remove start.\n");
/* flush work queue */
flush_work(&ocp81375_work_ch1);
flush_work(&ocp81375_work_ch2);
/* unregister flashlight operations */
flashlight_dev_unregister(OCP81375_NAME);
/* free resource */
if (chip->no_pdata)
kfree(chip->pdata);
kfree(chip);
pr_info("Remove done.\n");
return 0;
}
static const struct i2c_device_id ocp81375_i2c_id[] = {
{OCP81375_NAME, 0},
{}
};
MODULE_DEVICE_TABLE(i2c, ocp81375_i2c_id);
#ifdef CONFIG_OF
static const struct of_device_id ocp81375_i2c_of_match[] = {
{.compatible = OCP81375_DTNAME_I2C},
{},
};
MODULE_DEVICE_TABLE(of, ocp81375_i2c_of_match);
#endif
static struct i2c_driver ocp81375_i2c_driver = {
.driver = {
.name = OCP81375_NAME,
#ifdef CONFIG_OF
.of_match_table = ocp81375_i2c_of_match,
#endif
},
.probe = ocp81375_i2c_probe,
.remove = ocp81375_i2c_remove,
.id_table = ocp81375_i2c_id,
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int ocp81375_probe(struct platform_device *dev)
{
int i = 0;
pr_info("ocp81375_platform_probe start.\n");
/* init pinctrl */
if (ocp81375_pinctrl_init(dev)) {
pr_info("Failed to init pinctrl.\n");
return -1;
}
if (i2c_add_driver(&ocp81375_i2c_driver)) {
pr_info("Failed to add i2c driver.\n");
return -1;
}
for(;i<OCP81375_CHANNEL_NUM;i++){
ocp81375_para[i].min_flash_duty = 30;//must
ocp81375_para[i].flashmode = 0;//must
ocp81375_para[i].max_torch_current = 500;//config according to datasheet
ocp81375_para[i].max_flash_current = 2000;//config according to datasheet
ocp81375_para[i].duty_num = 0;//must
memset(&ocp81375_para[i].duty_reg_code[0], 0,
sizeof(ocp81375_para[i].duty_reg_code));
}
pr_info("%s done.\n", __func__);
return 0;
}
static int ocp81375_remove(struct platform_device *dev)
{
pr_debug("Remove start.\n");
i2c_del_driver(&ocp81375_i2c_driver);
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id ocp81375_of_match[] = {
{.compatible = OCP81375_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, ocp81375_of_match);
#else
static struct platform_device ocp81375_platform_device = {
.name = OCP81375_NAME,
.id = 0,
.dev = {}
};
MODULE_DEVICE_TABLE(platform, ocp81375_platform_device);
#endif
static struct platform_driver ocp81375_platform_driver = {
.probe = ocp81375_probe,
.remove = ocp81375_remove,
.driver = {
.name = OCP81375_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = ocp81375_of_match,
#endif
},
};
static int __init flashlight_ocp81375_init(void)
{
int ret;
pr_info("flashlight_ocp81375_initInit start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&ocp81375_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&ocp81375_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_ocp81375_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&ocp81375_platform_driver);
pr_debug("Exit done.\n");
}
module_init(flashlight_ocp81375_init);
module_exit(flashlight_ocp81375_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight OCP81375 Driver");
@@ -0,0 +1,560 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* define device tree */
#ifndef RT4505_DTNAME_I2C
#define RT4505_DTNAME_I2C "mediatek,flashlights_rt4505_i2c"
#endif
#define RT4505_NAME "flashlights-rt4505"
/* define registers */
#define RT4505_REG_ENABLE (0x0A)
#define RT4505_ENABLE_TORCH (0x71)
#define RT4505_ENABLE_FLASH (0x77)
#define RT4505_DISABLE (0x70)
#define RT4505_REG_LEVEL (0x09)
#define RT4505_REG_RESET (0x00)
#define RT4505_FLASH_RESET (0x80)
#define RT4505_REG_FLASH_FEATURE (0x08)
#define RT4505_FLASH_TIMEOUT (0x07)
/* define level */
#define RT4505_LEVEL_NUM 18
#define RT4505_LEVEL_TORCH 4
#define RT4505_HW_TIMEOUT 800 /* ms */
/* define mutex and work queue */
static DEFINE_MUTEX(rt4505_mutex);
static struct work_struct rt4505_work;
/* define usage count */
static int use_count;
/* define i2c */
static struct i2c_client *rt4505_i2c_client;
/* platform data */
struct rt4505_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/* rt4505 chip data */
struct rt4505_chip_data {
struct i2c_client *client;
struct rt4505_platform_data *pdata;
struct mutex lock;
};
/******************************************************************************
* rt4505 operations
*****************************************************************************/
static const int rt4505_current[RT4505_LEVEL_NUM] = {
49, 93, 140, 187, 281, 375, 468, 562, 656, 750,
843, 937, 1031, 1125, 1218, 1312, 1406, 1500
};
static const unsigned char rt4505_flash_level[RT4505_LEVEL_NUM] = {
0x00, 0x20, 0x40, 0x60, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};
static int rt4505_level = -1;
static int rt4505_is_torch(int level)
{
if (level >= RT4505_LEVEL_TORCH)
return -1;
return 0;
}
static int rt4505_verify_level(int level)
{
if (level < 0)
level = 0;
else if (level >= RT4505_LEVEL_NUM)
level = RT4505_LEVEL_NUM - 1;
return level;
}
/* i2c wrapper function */
static int rt4505_write_reg(struct i2c_client *client, u8 reg, u8 val)
{
int ret;
struct rt4505_chip_data *chip = i2c_get_clientdata(client);
mutex_lock(&chip->lock);
ret = i2c_smbus_write_byte_data(client, reg, val);
mutex_unlock(&chip->lock);
if (ret < 0)
pr_info("failed writing at 0x%02x\n", reg);
return ret;
}
/* flashlight enable function */
static int rt4505_enable(void)
{
unsigned char reg, val;
reg = RT4505_REG_ENABLE;
if (!rt4505_is_torch(rt4505_level)) {
/* torch mode */
val = RT4505_ENABLE_TORCH;
} else {
/* flash mode */
val = RT4505_ENABLE_FLASH;
}
return rt4505_write_reg(rt4505_i2c_client, reg, val);
}
/* flashlight disable function */
static int rt4505_disable(void)
{
unsigned char reg, val;
reg = RT4505_REG_ENABLE;
val = RT4505_DISABLE;
return rt4505_write_reg(rt4505_i2c_client, reg, val);
}
/* set flashlight level */
static int rt4505_set_level(int level)
{
unsigned char reg, val;
level = rt4505_verify_level(level);
rt4505_level = level;
reg = RT4505_REG_LEVEL;
val = rt4505_flash_level[level];
return rt4505_write_reg(rt4505_i2c_client, reg, val);
}
/* flashlight init */
int rt4505_init(void)
{
int ret;
unsigned char reg, val;
/* reset chip */
reg = RT4505_REG_RESET;
val = RT4505_FLASH_RESET;
ret = rt4505_write_reg(rt4505_i2c_client, reg, val);
/* set flash timeout */
reg = RT4505_REG_FLASH_FEATURE;
val = RT4505_FLASH_TIMEOUT;
ret = rt4505_write_reg(rt4505_i2c_client, reg, val);
return ret;
}
/* flashlight uninit */
int rt4505_uninit(void)
{
rt4505_disable();
return 0;
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static struct hrtimer rt4505_timer;
static unsigned int rt4505_timeout_ms;
static void rt4505_work_disable(struct work_struct *data)
{
pr_debug("work queue callback\n");
rt4505_disable();
}
static enum hrtimer_restart rt4505_timer_func(struct hrtimer *timer)
{
schedule_work(&rt4505_work);
return HRTIMER_NORESTART;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int rt4505_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
ktime_t ktime;
unsigned int s;
unsigned int ns;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
rt4505_timeout_ms = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
rt4505_set_level(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
if (fl_arg->arg == 1) {
if (rt4505_timeout_ms) {
s = rt4505_timeout_ms / 1000;
ns = rt4505_timeout_ms % 1000 * 1000000;
ktime = ktime_set(s, ns);
hrtimer_start(&rt4505_timer, ktime,
HRTIMER_MODE_REL);
}
rt4505_enable();
} else {
rt4505_disable();
hrtimer_cancel(&rt4505_timer);
}
break;
case FLASH_IOC_GET_DUTY_NUMBER:
pr_debug("FLASH_IOC_GET_DUTY_NUMBER(%d)\n", channel);
fl_arg->arg = RT4505_LEVEL_NUM;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_debug("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n", channel);
fl_arg->arg = RT4505_LEVEL_TORCH - 1;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = rt4505_verify_level(fl_arg->arg);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel, (int)fl_arg->arg);
fl_arg->arg = rt4505_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_TIMEOUT:
pr_debug("FLASH_IOC_GET_HW_TIMEOUT(%d)\n", channel);
fl_arg->arg = RT4505_HW_TIMEOUT;
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int rt4505_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int rt4505_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int rt4505_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&rt4505_mutex);
if (set) {
if (!use_count)
ret = rt4505_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = rt4505_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&rt4505_mutex);
return ret;
}
static ssize_t rt4505_strobe_store(struct flashlight_arg arg)
{
rt4505_set_driver(1);
rt4505_set_level(arg.level);
rt4505_timeout_ms = 0;
rt4505_enable();
msleep(arg.dur);
rt4505_disable();
rt4505_set_driver(0);
return 0;
}
static struct flashlight_operations rt4505_ops = {
rt4505_open,
rt4505_release,
rt4505_ioctl,
rt4505_strobe_store,
rt4505_set_driver
};
/******************************************************************************
* I2C device and driver
*****************************************************************************/
static int rt4505_chip_init(struct rt4505_chip_data *chip)
{
/* NOTE: Chip initialication move to "set driver" for power saving.
* rt4505_init();
*/
return 0;
}
static int rt4505_parse_dt(struct device *dev,
struct rt4505_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
RT4505_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int rt4505_i2c_probe(
struct i2c_client *client, const struct i2c_device_id *id)
{
struct rt4505_platform_data *pdata = dev_get_platdata(&client->dev);
struct rt4505_chip_data *chip;
int err;
int i;
pr_debug("Probe start.\n");
/* check i2c */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_info("Failed to check i2c functionality.\n");
err = -ENODEV;
goto err_out;
}
/* init chip private data */
chip = kzalloc(sizeof(struct rt4505_chip_data), GFP_KERNEL);
if (!chip) {
err = -ENOMEM;
goto err_out;
}
chip->client = client;
/* init platform data */
if (!pdata) {
pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) {
err = -ENOMEM;
goto err_free;
}
client->dev.platform_data = pdata;
err = rt4505_parse_dt(&client->dev, pdata);
if (err)
goto err_free;
}
chip->pdata = pdata;
i2c_set_clientdata(client, chip);
rt4505_i2c_client = client;
/* init mutex and spinlock */
mutex_init(&chip->lock);
/* init work queue */
INIT_WORK(&rt4505_work, rt4505_work_disable);
/* init timer */
hrtimer_init(&rt4505_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
rt4505_timer.function = rt4505_timer_func;
rt4505_timeout_ms = 400;
/* init chip hw */
rt4505_chip_init(chip);
/* clear usage count */
use_count = 0;
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&rt4505_ops)) {
err = -EFAULT;
goto err_free;
}
} else {
if (flashlight_dev_register(RT4505_NAME, &rt4505_ops)) {
err = -EFAULT;
goto err_free;
}
}
pr_debug("Probe done.\n");
return 0;
err_free:
i2c_set_clientdata(client, NULL);
kfree(chip);
err_out:
return err;
}
static int rt4505_i2c_remove(struct i2c_client *client)
{
struct rt4505_platform_data *pdata = dev_get_platdata(&client->dev);
struct rt4505_chip_data *chip = i2c_get_clientdata(client);
int i;
pr_debug("Remove start.\n");
client->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(RT4505_NAME);
/* flush work queue */
flush_work(&rt4505_work);
/* free resource */
kfree(chip);
pr_debug("Remove done.\n");
return 0;
}
static const struct i2c_device_id rt4505_i2c_id[] = {
{RT4505_NAME, 0},
{}
};
MODULE_DEVICE_TABLE(i2c, rt4505_i2c_id);
#ifdef CONFIG_OF
static const struct of_device_id rt4505_i2c_of_match[] = {
{.compatible = RT4505_DTNAME_I2C},
{},
};
MODULE_DEVICE_TABLE(of, rt4505_i2c_of_match);
#endif
static struct i2c_driver rt4505_i2c_driver = {
.driver = {
.name = RT4505_NAME,
#ifdef CONFIG_OF
.of_match_table = rt4505_i2c_of_match,
#endif
},
.probe = rt4505_i2c_probe,
.remove = rt4505_i2c_remove,
.id_table = rt4505_i2c_id,
};
module_i2c_driver(rt4505_i2c_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight RT4505 Driver");
@@ -0,0 +1,822 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include "richtek/rt-flashlight.h"
#include "mtk_charger.h"
#include "flashlight-core.h"
#include "flashlight-dt.h"
/* device tree should be defined in flashlight-dt.h */
#ifndef RT5081_DTNAME
#define RT5081_DTNAME "mediatek,flashlights_rt5081"
#endif
#define RT5081_NAME "flashlights-rt5081"
/* define channel, level */
#define RT5081_CHANNEL_NUM 2
#define RT5081_CHANNEL_CH1 0
#define RT5081_CHANNEL_CH2 1
#define RT5081_NONE (-1)
#define RT5081_DISABLE 0
#define RT5081_ENABLE 1
#define RT5081_ENABLE_TORCH 1
#define RT5081_ENABLE_FLASH 2
#define RT5081_LEVEL_NUM 32
#define RT5081_LEVEL_TORCH 16
#define RT5081_LEVEL_FLASH RT5081_LEVEL_NUM
#define RT5081_WDT_TIMEOUT 1248 /* ms */
#define RT5081_HW_TIMEOUT 400 /* ms */
/* define mutex, work queue and timer */
static DEFINE_MUTEX(rt5081_mutex);
static struct work_struct rt5081_work_ch1;
static struct work_struct rt5081_work_ch2;
static struct hrtimer rt5081_timer_ch1;
static struct hrtimer rt5081_timer_ch2;
static unsigned int rt5081_timeout_ms[RT5081_CHANNEL_NUM];
/* define usage count */
static int use_count;
/* define RTK flashlight device */
static struct flashlight_device *flashlight_dev_ch1;
static struct flashlight_device *flashlight_dev_ch2;
#define RT_FLED_DEVICE_CH1 "rt-flash-led1"
#define RT_FLED_DEVICE_CH2 "rt-flash-led2"
/* define charger consumer */
static struct charger_consumer *flashlight_charger_consumer;
#define CHARGER_SUPPLY_NAME "charger_port1"
/* is decrease voltage */
static int is_decrease_voltage;
/* platform data */
struct rt5081_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/******************************************************************************
* rt5081 operations
*****************************************************************************/
static const int rt5081_current[RT5081_LEVEL_NUM] = {
25, 50, 75, 100, 125, 150, 175, 200, 225, 250,
275, 300, 325, 350, 375, 400, 450, 500, 550, 600,
650, 700, 750, 800, 850, 900, 950, 1000, 1050, 1100,
1150, 1200
};
static const unsigned char rt5081_torch_level[RT5081_LEVEL_TORCH] = {
0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x12,
0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1E
};
static const unsigned char rt5081_strobe_level[RT5081_LEVEL_FLASH] = {
0x80, 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10,
0x12, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x20, 0x24, 0x28, 0x2C,
0x30, 0x34, 0x38, 0x3C, 0x40, 0x44, 0x48, 0x4C, 0x50, 0x54,
0x58, 0x5C
};
static int rt5081_decouple_mode;
static int rt5081_en_ch1;
static int rt5081_en_ch2;
static int rt5081_level_ch1;
static int rt5081_level_ch2;
static int rt5081_is_charger_ready(void)
{
if (flashlight_is_ready(flashlight_dev_ch1) &&
flashlight_is_ready(flashlight_dev_ch2))
return FLASHLIGHT_CHARGER_READY;
else
return FLASHLIGHT_CHARGER_NOT_READY;
}
static int rt5081_is_torch(int level)
{
if (level >= RT5081_LEVEL_TORCH)
return -1;
return 0;
}
static int rt5081_verify_level(int level)
{
if (level < 0)
level = 0;
else if (level >= RT5081_LEVEL_NUM)
level = RT5081_LEVEL_NUM - 1;
return level;
}
/* flashlight enable function */
static int rt5081_enable(void)
{
int ret = 0;
enum flashlight_mode mode = FLASHLIGHT_MODE_TORCH;
if (!flashlight_dev_ch1 || !flashlight_dev_ch2) {
pr_info("Failed to enable since no flashlight device.\n");
return -1;
}
/* set flash mode if any channel is flash mode */
if ((rt5081_en_ch1 == RT5081_ENABLE_FLASH)
|| (rt5081_en_ch2 == RT5081_ENABLE_FLASH))
mode = FLASHLIGHT_MODE_FLASH;
/* enable channel 1 and channel 2 */
if (rt5081_en_ch1)
ret |= flashlight_set_mode(
flashlight_dev_ch1, mode);
else
ret |= flashlight_set_mode(
flashlight_dev_ch1, FLASHLIGHT_MODE_OFF);
if (rt5081_en_ch2)
ret |= flashlight_set_mode(
flashlight_dev_ch2, mode);
else
ret |= flashlight_set_mode(
flashlight_dev_ch2, FLASHLIGHT_MODE_OFF);
if (ret < 0)
pr_info("Failed to enable.\n");
return ret;
}
/* flashlight disable function */
static int rt5081_disable(void)
{
int ret = 0;
if (!flashlight_dev_ch1 || !flashlight_dev_ch2) {
pr_info("Failed to disable since no flashlight device.\n");
return -1;
}
/* disable channel 1 and channel 2 */
ret |= flashlight_set_mode(flashlight_dev_ch1, FLASHLIGHT_MODE_OFF);
ret |= flashlight_set_mode(flashlight_dev_ch2, FLASHLIGHT_MODE_OFF);
if (ret < 0)
pr_info("Failed to disable.\n");
return ret;
}
/* set flashlight level */
static int rt5081_set_level_ch1(int level)
{
level = rt5081_verify_level(level);
rt5081_level_ch1 = level;
if (!flashlight_dev_ch1) {
pr_info("Failed to set ht level since no flashlight device.\n");
return -1;
}
/* set brightness level */
if (!rt5081_is_torch(level))
flashlight_set_torch_brightness(
flashlight_dev_ch1, rt5081_torch_level[level]);
flashlight_set_strobe_brightness(
flashlight_dev_ch1, rt5081_strobe_level[level]);
return 0;
}
static int rt5081_set_level_ch2(int level)
{
level = rt5081_verify_level(level);
rt5081_level_ch2 = level;
if (!flashlight_dev_ch2) {
pr_info("Failed to set lt level since no flashlight device.\n");
return -1;
}
/* set brightness level */
if (!rt5081_is_torch(level))
flashlight_set_torch_brightness(
flashlight_dev_ch2, rt5081_torch_level[level]);
flashlight_set_strobe_brightness(
flashlight_dev_ch2, rt5081_strobe_level[level]);
return 0;
}
static int rt5081_set_level(int channel, int level)
{
if (channel == RT5081_CHANNEL_CH1)
rt5081_set_level_ch1(level);
else if (channel == RT5081_CHANNEL_CH2)
rt5081_set_level_ch2(level);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int rt5081_set_scenario(int scenario)
{
/* set decouple mode */
rt5081_decouple_mode = scenario & FLASHLIGHT_SCENARIO_DECOUPLE_MASK;
/* notify charger to increase or decrease voltage */
if (!flashlight_charger_consumer) {
pr_info("Failed with no charger consumer handler.\n");
return -1;
}
mutex_lock(&rt5081_mutex);
if (scenario & FLASHLIGHT_SCENARIO_CAMERA_MASK) {
if (!is_decrease_voltage) {
pr_info("Decrease voltage level.\n");
charger_manager_enable_high_voltage_charging(
flashlight_charger_consumer, false);
is_decrease_voltage = 1;
}
} else {
if (is_decrease_voltage) {
pr_info("Increase voltage level.\n");
charger_manager_enable_high_voltage_charging(
flashlight_charger_consumer, true);
is_decrease_voltage = 0;
}
}
mutex_unlock(&rt5081_mutex);
return 0;
}
/* flashlight init */
static int rt5081_init(void)
{
/* clear flashlight state */
rt5081_en_ch1 = RT5081_NONE;
rt5081_en_ch2 = RT5081_NONE;
/* clear decouple mode */
rt5081_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
/* clear charger status */
is_decrease_voltage = 0;
return 0;
}
/* flashlight uninit */
static int rt5081_uninit(void)
{
/* clear flashlight state */
rt5081_en_ch1 = RT5081_NONE;
rt5081_en_ch2 = RT5081_NONE;
/* clear decouple mode */
rt5081_decouple_mode = FLASHLIGHT_SCENARIO_COUPLE;
/* clear charger status */
is_decrease_voltage = 0;
return rt5081_disable();
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static void rt5081_work_disable_ch1(struct work_struct *data)
{
pr_debug("ht work queue callback\n");
rt5081_disable();
}
static void rt5081_work_disable_ch2(struct work_struct *data)
{
pr_debug("lt work queue callback\n");
rt5081_disable();
}
static enum hrtimer_restart rt5081_timer_func_ch1(struct hrtimer *timer)
{
schedule_work(&rt5081_work_ch1);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart rt5081_timer_func_ch2(struct hrtimer *timer)
{
schedule_work(&rt5081_work_ch2);
return HRTIMER_NORESTART;
}
static int rt5081_timer_start(int channel, ktime_t ktime)
{
if (channel == RT5081_CHANNEL_CH1)
hrtimer_start(&rt5081_timer_ch1, ktime, HRTIMER_MODE_REL);
else if (channel == RT5081_CHANNEL_CH2)
hrtimer_start(&rt5081_timer_ch2, ktime, HRTIMER_MODE_REL);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
static int rt5081_timer_cancel(int channel)
{
if (channel == RT5081_CHANNEL_CH1)
hrtimer_cancel(&rt5081_timer_ch1);
else if (channel == RT5081_CHANNEL_CH2)
hrtimer_cancel(&rt5081_timer_ch2);
else {
pr_info("Error channel\n");
return -1;
}
return 0;
}
/******************************************************************************
* Flashlight operation wrapper function
*****************************************************************************/
static int rt5081_operate(int channel, int enable)
{
ktime_t ktime;
unsigned int s;
unsigned int ns;
/* setup enable/disable */
if (channel == RT5081_CHANNEL_CH1) {
rt5081_en_ch1 = enable;
if (rt5081_en_ch1)
if (rt5081_is_torch(rt5081_level_ch1))
rt5081_en_ch1 = RT5081_ENABLE_FLASH;
} else if (channel == RT5081_CHANNEL_CH2) {
rt5081_en_ch2 = enable;
if (rt5081_en_ch2)
if (rt5081_is_torch(rt5081_level_ch2))
rt5081_en_ch2 = RT5081_ENABLE_FLASH;
} else {
pr_info("Error channel\n");
return -1;
}
/* decouple mode */
if (rt5081_decouple_mode) {
if (channel == RT5081_CHANNEL_CH1) {
rt5081_en_ch2 = RT5081_DISABLE;
rt5081_timeout_ms[RT5081_CHANNEL_CH2] = 0;
} else if (channel == RT5081_CHANNEL_CH2) {
rt5081_en_ch1 = RT5081_DISABLE;
rt5081_timeout_ms[RT5081_CHANNEL_CH1] = 0;
}
}
/* operate flashlight and setup timer */
if ((rt5081_en_ch1 != RT5081_NONE) && (rt5081_en_ch2 != RT5081_NONE)) {
if ((rt5081_en_ch1 == RT5081_DISABLE) &&
(rt5081_en_ch2 == RT5081_DISABLE)) {
rt5081_disable();
rt5081_timer_cancel(RT5081_CHANNEL_CH1);
rt5081_timer_cancel(RT5081_CHANNEL_CH2);
} else {
if (rt5081_timeout_ms[RT5081_CHANNEL_CH1]) {
s = rt5081_timeout_ms[RT5081_CHANNEL_CH1]
/ 1000;
ns = rt5081_timeout_ms[RT5081_CHANNEL_CH1]
% 1000 * 1000000;
ktime = ktime_set(s, ns);
rt5081_timer_start(RT5081_CHANNEL_CH1, ktime);
}
if (rt5081_timeout_ms[RT5081_CHANNEL_CH2]) {
s = rt5081_timeout_ms[RT5081_CHANNEL_CH2]
/ 1000;
ns = rt5081_timeout_ms[RT5081_CHANNEL_CH2]
% 1000 * 1000000;
ktime = ktime_set(s, ns);
rt5081_timer_start(RT5081_CHANNEL_CH2, ktime);
}
rt5081_enable();
}
/* clear flashlight state */
rt5081_en_ch1 = RT5081_NONE;
rt5081_en_ch2 = RT5081_NONE;
}
return 0;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int rt5081_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
/* verify channel */
if (channel < 0 || channel >= RT5081_CHANNEL_NUM) {
pr_info("Failed with error channel\n");
return -EINVAL;
}
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
pr_debug("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
rt5081_timeout_ms[channel] = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
pr_debug("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
rt5081_set_level(channel, fl_arg->arg);
break;
case FLASH_IOC_SET_SCENARIO:
pr_debug("FLASH_IOC_SET_SCENARIO(%d): %d\n",
channel, (int)fl_arg->arg);
rt5081_set_scenario(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
pr_debug("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
rt5081_operate(channel, fl_arg->arg);
break;
case FLASH_IOC_IS_CHARGER_READY:
pr_debug("FLASH_IOC_IS_CHARGER_READY(%d)\n", channel);
fl_arg->arg = rt5081_is_charger_ready();
break;
case FLASH_IOC_GET_DUTY_NUMBER:
pr_debug("FLASH_IOC_GET_DUTY_NUMBER(%d)\n", channel);
fl_arg->arg = RT5081_LEVEL_NUM;
break;
case FLASH_IOC_GET_MAX_TORCH_DUTY:
pr_debug("FLASH_IOC_GET_MAX_TORCH_DUTY(%d)\n", channel);
fl_arg->arg = RT5081_LEVEL_TORCH - 1;
break;
case FLASH_IOC_GET_DUTY_CURRENT:
fl_arg->arg = rt5081_verify_level(fl_arg->arg);
pr_debug("FLASH_IOC_GET_DUTY_CURRENT(%d): %d\n",
channel, (int)fl_arg->arg);
fl_arg->arg = rt5081_current[fl_arg->arg];
break;
case FLASH_IOC_GET_HW_TIMEOUT:
pr_debug("FLASH_IOC_GET_HW_TIMEOUT(%d)\n", channel);
fl_arg->arg = RT5081_HW_TIMEOUT;
break;
default:
pr_info("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int rt5081_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int rt5081_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int rt5081_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&rt5081_mutex);
if (set) {
if (!use_count)
ret = rt5081_init();
use_count++;
pr_debug("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = rt5081_uninit();
if (use_count < 0)
use_count = 0;
pr_debug("Unset driver: %d\n", use_count);
}
mutex_unlock(&rt5081_mutex);
return ret;
}
static ssize_t rt5081_strobe_store(struct flashlight_arg arg)
{
rt5081_set_driver(1);
rt5081_set_scenario(
FLASHLIGHT_SCENARIO_CAMERA |
FLASHLIGHT_SCENARIO_COUPLE);
rt5081_set_level(arg.channel, arg.level);
rt5081_timeout_ms[arg.channel] = 0;
if (arg.level < 0)
rt5081_operate(arg.channel, RT5081_DISABLE);
else
rt5081_operate(arg.channel, RT5081_ENABLE);
msleep(arg.dur);
rt5081_set_scenario(
FLASHLIGHT_SCENARIO_FLASHLIGHT |
FLASHLIGHT_SCENARIO_COUPLE);
rt5081_operate(arg.channel, RT5081_DISABLE);
rt5081_set_driver(0);
return 0;
}
static struct flashlight_operations rt5081_ops = {
rt5081_open,
rt5081_release,
rt5081_ioctl,
rt5081_strobe_store,
rt5081_set_driver
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int rt5081_parse_dt(struct device *dev,
struct rt5081_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
pr_info("Parse no dt, node.\n");
return 0;
}
pr_info("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
pr_info("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
RT5081_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int rt5081_probe(struct platform_device *pdev)
{
struct rt5081_platform_data *pdata = dev_get_platdata(&pdev->dev);
int ret;
int i;
pr_debug("Probe start.\n");
/* parse dt */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return -ENOMEM;
pdev->dev.platform_data = pdata;
ret = rt5081_parse_dt(&pdev->dev, pdata);
if (ret)
return ret;
}
/* init work queue */
INIT_WORK(&rt5081_work_ch1, rt5081_work_disable_ch1);
INIT_WORK(&rt5081_work_ch2, rt5081_work_disable_ch2);
/* init timer */
hrtimer_init(&rt5081_timer_ch1, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
rt5081_timer_ch1.function = rt5081_timer_func_ch1;
hrtimer_init(&rt5081_timer_ch2, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
rt5081_timer_ch2.function = rt5081_timer_func_ch2;
rt5081_timeout_ms[RT5081_CHANNEL_CH1] = 600;
rt5081_timeout_ms[RT5081_CHANNEL_CH2] = 600;
/* clear attributes */
use_count = 0;
is_decrease_voltage = 0;
/* get RTK flashlight handler */
flashlight_dev_ch1 = find_flashlight_by_name(RT_FLED_DEVICE_CH1);
if (!flashlight_dev_ch1) {
pr_info("Failed to get ht flashlight device.\n");
return -EFAULT;
}
flashlight_dev_ch2 = find_flashlight_by_name(RT_FLED_DEVICE_CH2);
if (!flashlight_dev_ch2) {
pr_info("Failed to get lt flashlight device.\n");
return -EFAULT;
}
/* setup strobe mode timeout */
if (flashlight_set_strobe_timeout(flashlight_dev_ch1,
RT5081_HW_TIMEOUT, RT5081_HW_TIMEOUT + 200) < 0)
pr_info("Failed to set strobe timeout.\n");
/* get charger consumer manager */
flashlight_charger_consumer = charger_manager_get_by_name(
&flashlight_dev_ch1->dev, CHARGER_SUPPLY_NAME);
if (!flashlight_charger_consumer) {
pr_info("Failed to get charger manager.\n");
return -EFAULT;
}
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i], &rt5081_ops))
return -EFAULT;
} else {
if (flashlight_dev_register(RT5081_NAME, &rt5081_ops))
return -EFAULT;
}
pr_debug("Probe done.\n");
return 0;
}
static int rt5081_remove(struct platform_device *pdev)
{
struct rt5081_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
pr_debug("Remove start.\n");
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(RT5081_NAME);
/* flush work queue */
flush_work(&rt5081_work_ch1);
flush_work(&rt5081_work_ch2);
/* clear RTK flashlight device */
flashlight_dev_ch1 = NULL;
flashlight_dev_ch2 = NULL;
pr_debug("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id rt5081_of_match[] = {
{.compatible = RT5081_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, rt5081_of_match);
#else
static struct platform_device rt5081_platform_device[] = {
{
.name = RT5081_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, rt5081_platform_device);
#endif
static struct platform_driver rt5081_platform_driver = {
.probe = rt5081_probe,
.remove = rt5081_remove,
.driver = {
.name = RT5081_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = rt5081_of_match,
#endif
},
};
static int __init flashlight_rt5081_init(void)
{
int ret;
pr_debug("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&rt5081_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&rt5081_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done.\n");
return 0;
}
static void __exit flashlight_rt5081_exit(void)
{
pr_debug("Exit start.\n");
platform_driver_unregister(&rt5081_platform_driver);
pr_debug("Exit done.\n");
}
/* replace module_init() since conflict in kernel init process */
late_initcall(flashlight_rt5081_init);
module_exit(flashlight_rt5081_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Wang <Simon-TCH.Wang@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight RT5081 Driver");
@@ -0,0 +1,589 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/pinctrl/consumer.h>
#include "flashlight-core.h"
#include "flashlight-dt.h"
#define TAG_NAME "[flashligh_sywt78_drv]"
#define PK_DBG_NONE(fmt, arg...) do {} while (0)
#define PK_DBG_FUNC(fmt, arg...) pr_debug(TAG_NAME "%s: " fmt, __func__, ##arg)
#define PK_ERR(fmt, arg...) pr_info(TAG_NAME "%s: " fmt, __func__, ##arg)
#define DEBUG_LEDS_STROBE
#ifdef DEBUG_LEDS_STROBE
#define PK_INF(fmt, arg...) pr_info(TAG_NAME "%s is called.\n", __func__)
#define PK_DBG PK_DBG_FUNC
#else
#define PK_INF(fmt, arg...) do {} while (0)
#define PK_DBG(a, ...)
#endif
/* define device tree */
#ifndef SYWT78_DTNAME
#define SYWT78_DTNAME "mediatek,flashlights_sywt78"
#endif
#define SYWT78_NAME "flashlights-sywt78"
/* define registers */
/* define mutex and work queue */
static DEFINE_MUTEX(sywt78_mutex);
static struct work_struct sywt78_work;
/* define pinctrl */
#define SYWT78_PINCTRL_PIN_TORCH 0
#define SYWT78_PINCTRL_PIN_FLASH 1
#define SYWT78_PINCTRL_PINSTATE_LOW 0
#define SYWT78_PINCTRL_PINSTATE_HIGH 1
#define SYWT78_PINCTRL_STATE_FLASH_HIGH "flash_high"
#define SYWT78_PINCTRL_STATE_FLASH_LOW "flash_low"
#define SYWT78_PINCTRL_STATE_TORCH_HIGH "torch_high"
#define SYWT78_PINCTRL_STATE_TORCH_LOW "torch_low"
static struct pinctrl *sywt78_pinctrl;
static struct pinctrl_state *sywt78_flash_high;
static struct pinctrl_state *sywt78_flash_low;
static struct pinctrl_state *sywt78_torch_high;
static struct pinctrl_state *sywt78_torch_low;
/* define usage count */
static int use_count;
static int g_flash_duty = -1;
/* platform data */
struct sywt78_platform_data {
int channel_num;
struct flashlight_device_id *dev_id;
};
/******************************************************************************
* Pinctrl configuration
*****************************************************************************/
static int sywt78_pinctrl_init(struct platform_device *pdev)
{
int ret = 0;
/* get pinctrl */
sywt78_pinctrl = devm_pinctrl_get(&pdev->dev);
if (IS_ERR(sywt78_pinctrl)) {
PK_ERR("Failed to get flashlight pinctrl.\n");
ret = PTR_ERR(sywt78_pinctrl);
}
/* Flashlight pin initialization */
sywt78_flash_high = pinctrl_lookup_state(
sywt78_pinctrl, SYWT78_PINCTRL_STATE_FLASH_HIGH);
if (IS_ERR(sywt78_flash_high)) {
PK_ERR("Failed to init (%s)\n",
SYWT78_PINCTRL_STATE_FLASH_HIGH);
ret = PTR_ERR(sywt78_flash_high);
}
sywt78_flash_low = pinctrl_lookup_state(
sywt78_pinctrl, SYWT78_PINCTRL_STATE_FLASH_LOW);
if (IS_ERR(sywt78_flash_low)) {
PK_ERR("Failed to init (%s)\n", SYWT78_PINCTRL_STATE_FLASH_LOW);
ret = PTR_ERR(sywt78_flash_low);
}
sywt78_torch_high = pinctrl_lookup_state(
sywt78_pinctrl, SYWT78_PINCTRL_STATE_TORCH_HIGH);
if (IS_ERR(sywt78_torch_high)) {
PK_ERR("Failed to init (%s)\n",
SYWT78_PINCTRL_STATE_TORCH_HIGH);
ret = PTR_ERR(sywt78_torch_high);
}
sywt78_torch_low = pinctrl_lookup_state(
sywt78_pinctrl, SYWT78_PINCTRL_STATE_TORCH_LOW);
if (IS_ERR(sywt78_torch_low)) {
PK_ERR("Failed to init (%s)\n", SYWT78_PINCTRL_STATE_TORCH_LOW);
ret = PTR_ERR(sywt78_torch_low);
}
return ret;
}
static int sywt78_pinctrl_set(int pin, int state)
{
int ret = 0;
if (IS_ERR(sywt78_pinctrl)) {
PK_ERR("pinctrl is not available\n");
return -1;
}
switch (pin) {
case SYWT78_PINCTRL_PIN_FLASH:
if (state == SYWT78_PINCTRL_PINSTATE_LOW &&
!IS_ERR(sywt78_flash_low))
ret = pinctrl_select_state(
sywt78_pinctrl, sywt78_flash_low);
else if (state == SYWT78_PINCTRL_PINSTATE_HIGH &&
!IS_ERR(sywt78_flash_high))
ret = pinctrl_select_state(
sywt78_pinctrl, sywt78_flash_high);
else
PK_ERR("set err, pin(%d) state(%d)\n", pin, state);
break;
case SYWT78_PINCTRL_PIN_TORCH:
if (state == SYWT78_PINCTRL_PINSTATE_LOW &&
!IS_ERR(sywt78_torch_low))
ret = pinctrl_select_state(
sywt78_pinctrl, sywt78_torch_low);
else if (state == SYWT78_PINCTRL_PINSTATE_HIGH &&
!IS_ERR(sywt78_torch_high))
ret = pinctrl_select_state(
sywt78_pinctrl, sywt78_torch_high);
else
PK_ERR("set err, pin(%d) state(%d)\n", pin, state);
break;
default:
PK_ERR("set err, pin(%d) state(%d)\n", pin, state);
break;
}
PK_DBG("pin(%d) state(%d), ret:%d\n", pin, state, ret);
return ret;
}
/******************************************************************************
* sywt78 operations
*****************************************************************************/
/* flashlight enable function */
static int sywt78_enable(void)
{
int pin_flash = SYWT78_PINCTRL_PIN_FLASH;
int pin_torch = SYWT78_PINCTRL_PIN_TORCH;
PK_DBG("g_flash_duty %d\n", g_flash_duty);
if (g_flash_duty == 1) /* flash mode */
sywt78_pinctrl_set(pin_flash, SYWT78_PINCTRL_PINSTATE_HIGH);
else /* torch mode */
sywt78_pinctrl_set(pin_torch, SYWT78_PINCTRL_PINSTATE_HIGH);
return 0;
}
/* flashlight disable function */
static int sywt78_disable(void)
{
int pin_flash = SYWT78_PINCTRL_PIN_FLASH;
int pin_torch = SYWT78_PINCTRL_PIN_TORCH;
int state = SYWT78_PINCTRL_PINSTATE_LOW;
sywt78_pinctrl_set(pin_torch, state);
sywt78_pinctrl_set(pin_flash, state);
return 0;
}
/* set flashlight level */
static int sywt78_set_level(int level)
{
g_flash_duty = level;
return 0;
}
/* flashlight init */
static int sywt78_init(void)
{
int pin_flash = SYWT78_PINCTRL_PIN_FLASH;
int pin_torch = SYWT78_PINCTRL_PIN_TORCH;
int state = SYWT78_PINCTRL_PINSTATE_LOW;
sywt78_pinctrl_set(pin_torch, state);
sywt78_pinctrl_set(pin_flash, state);
return 0;
}
/* flashlight uninit */
static int sywt78_uninit(void)
{
sywt78_disable();
return 0;
}
/******************************************************************************
* Timer and work queue
*****************************************************************************/
static struct hrtimer sywt78_timer;
static unsigned int sywt78_timeout_ms;
static void sywt78_work_disable(struct work_struct *data)
{
PK_DBG("work queue callback\n");
sywt78_disable();
}
static enum hrtimer_restart sywt78_timer_func(struct hrtimer *timer)
{
schedule_work(&sywt78_work);
return HRTIMER_NORESTART;
}
/******************************************************************************
* Flashlight operations
*****************************************************************************/
static int sywt78_ioctl(unsigned int cmd, unsigned long arg)
{
struct flashlight_dev_arg *fl_arg;
int channel;
ktime_t ktime;
unsigned int s;
unsigned int ns;
fl_arg = (struct flashlight_dev_arg *)arg;
channel = fl_arg->channel;
switch (cmd) {
case FLASH_IOC_SET_TIME_OUT_TIME_MS:
PK_DBG("FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n",
channel, (int)fl_arg->arg);
sywt78_timeout_ms = fl_arg->arg;
break;
case FLASH_IOC_SET_DUTY:
PK_DBG("FLASH_IOC_SET_DUTY(%d): %d\n",
channel, (int)fl_arg->arg);
sywt78_set_level(fl_arg->arg);
break;
case FLASH_IOC_SET_ONOFF:
PK_DBG("FLASH_IOC_SET_ONOFF(%d): %d\n",
channel, (int)fl_arg->arg);
if (fl_arg->arg == 1) {
if (sywt78_timeout_ms) {
s = sywt78_timeout_ms / 1000;
ns = sywt78_timeout_ms % 1000 * 1000000;
ktime = ktime_set(s, ns);
hrtimer_start(&sywt78_timer, ktime,
HRTIMER_MODE_REL);
}
sywt78_enable();
} else {
sywt78_disable();
hrtimer_cancel(&sywt78_timer);
}
break;
default:
PK_INF("No such command and arg(%d): (%d, %d)\n",
channel, _IOC_NR(cmd), (int)fl_arg->arg);
return -ENOTTY;
}
return 0;
}
static int sywt78_open(void)
{
/* Move to set driver for saving power */
return 0;
}
static int sywt78_release(void)
{
/* Move to set driver for saving power */
return 0;
}
static int sywt78_set_driver(int set)
{
int ret = 0;
/* set chip and usage count */
mutex_lock(&sywt78_mutex);
if (set) {
if (!use_count)
ret = sywt78_init();
use_count++;
PK_DBG("Set driver: %d\n", use_count);
} else {
use_count--;
if (!use_count)
ret = sywt78_uninit();
if (use_count < 0)
use_count = 0;
PK_DBG("Unset driver: %d\n", use_count);
}
mutex_unlock(&sywt78_mutex);
return ret;
}
static ssize_t sywt78_strobe_store(struct flashlight_arg arg)
{
sywt78_set_driver(1);
sywt78_set_level(arg.level);
sywt78_timeout_ms = 0;
sywt78_enable();
msleep(arg.dur);
sywt78_disable();
sywt78_set_driver(0);
return 0;
}
static struct flashlight_operations sywt78_ops = {
sywt78_open,
sywt78_release,
sywt78_ioctl,
sywt78_strobe_store,
sywt78_set_driver
};
/******************************************************************************
* Platform device and driver
*****************************************************************************/
static int sywt78_chip_init(void)
{
/* NOTE: Chip initialication move to "set driver" for power saving.
* sywt78_init();
*/
return 0;
}
static int sywt78_parse_dt(struct device *dev,
struct sywt78_platform_data *pdata)
{
struct device_node *np, *cnp;
u32 decouple = 0;
int i = 0;
if (!dev || !dev->of_node || !pdata)
return -ENODEV;
np = dev->of_node;
pdata->channel_num = of_get_child_count(np);
if (!pdata->channel_num) {
PK_INF("Parse no dt, node.\n");
return 0;
}
PK_INF("Channel number(%d).\n", pdata->channel_num);
if (of_property_read_u32(np, "decouple", &decouple))
PK_INF("Parse no dt, decouple.\n");
pdata->dev_id = devm_kzalloc(dev,
pdata->channel_num *
sizeof(struct flashlight_device_id),
GFP_KERNEL);
if (!pdata->dev_id)
return -ENOMEM;
for_each_child_of_node(np, cnp) {
if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type))
goto err_node_put;
if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct))
goto err_node_put;
if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part))
goto err_node_put;
snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE,
SYWT78_NAME);
pdata->dev_id[i].channel = i;
pdata->dev_id[i].decouple = decouple;
pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n",
pdata->dev_id[i].type, pdata->dev_id[i].ct,
pdata->dev_id[i].part, pdata->dev_id[i].name,
pdata->dev_id[i].channel,
pdata->dev_id[i].decouple);
i++;
}
return 0;
err_node_put:
of_node_put(cnp);
return -EINVAL;
}
static int sywt78_probe(struct platform_device *pdev)
{
struct sywt78_platform_data *pdata = dev_get_platdata(&pdev->dev);
int err;
int i;
PK_DBG("Probe start.\n");
/* init pinctrl */
if (sywt78_pinctrl_init(pdev)) {
PK_DBG("Failed to init pinctrl.\n");
err = -EFAULT;
goto err;
}
/* init platform data */
if (!pdata) {
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) {
err = -ENOMEM;
goto err;
}
pdev->dev.platform_data = pdata;
err = sywt78_parse_dt(&pdev->dev, pdata);
if (err)
goto err;
}
/* init work queue */
INIT_WORK(&sywt78_work, sywt78_work_disable);
/* init timer */
hrtimer_init(&sywt78_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
sywt78_timer.function = sywt78_timer_func;
sywt78_timeout_ms = 100;
/* init chip hw */
sywt78_chip_init();
/* clear usage count */
use_count = 0;
/* register flashlight device */
if (pdata->channel_num) {
for (i = 0; i < pdata->channel_num; i++)
if (flashlight_dev_register_by_device_id(
&pdata->dev_id[i],
&sywt78_ops)) {
err = -EFAULT;
goto err;
}
} else {
if (flashlight_dev_register(SYWT78_NAME, &sywt78_ops)) {
err = -EFAULT;
goto err;
}
}
PK_DBG("Probe done.\n");
return 0;
err:
return err;
}
static int sywt78_remove(struct platform_device *pdev)
{
struct sywt78_platform_data *pdata = dev_get_platdata(&pdev->dev);
int i;
PK_DBG("Remove start.\n");
pdev->dev.platform_data = NULL;
/* unregister flashlight device */
if (pdata && pdata->channel_num)
for (i = 0; i < pdata->channel_num; i++)
flashlight_dev_unregister_by_device_id(
&pdata->dev_id[i]);
else
flashlight_dev_unregister(SYWT78_NAME);
/* flush work queue */
flush_work(&sywt78_work);
PK_DBG("Remove done.\n");
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id sywt78_gpio_of_match[] = {
{.compatible = SYWT78_DTNAME},
{},
};
MODULE_DEVICE_TABLE(of, sywt78_gpio_of_match);
#else
static struct platform_device sywt78_gpio_platform_device[] = {
{
.name = SYWT78_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, sywt78_gpio_platform_device);
#endif
static struct platform_driver sywt78_platform_driver = {
.probe = sywt78_probe,
.remove = sywt78_remove,
.driver = {
.name = SYWT78_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = sywt78_gpio_of_match,
#endif
},
};
static int __init flashlight_sywt78_init(void)
{
int ret;
PK_DBG("Init start.\n");
#ifndef CONFIG_OF
ret = platform_device_register(&sywt78_gpio_platform_device);
if (ret) {
PK_ERR("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&sywt78_platform_driver);
if (ret) {
PK_ERR("Failed to register platform driver\n");
return ret;
}
PK_DBG("Init done.\n");
return 0;
}
static void __exit flashlight_sywt78_exit(void)
{
PK_DBG("Exit start.\n");
platform_driver_unregister(&sywt78_platform_driver);
PK_DBG("Exit done.\n");
}
module_init(flashlight_sywt78_init);
module_exit(flashlight_sywt78_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Dongchun Zhu <dongchun.zhu@mediatek.com>");
MODULE_DESCRIPTION("MTK Flashlight SYWT78 GPIO Driver");
@@ -0,0 +1,354 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/of_graph.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-subdev.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-common.h>
#ifdef CONFIG_COMPAT
#include <linux/compat.h>
#endif
#include "mtk-composite.h"
/******************************************************************************
* Definition
*****************************************************************************/
#define MTK_COMPOSITE_NAME "mtk-composite-v4l2-1"
static int
fl_async_bound(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *subdev,
struct v4l2_async_subdev *asd)
{
struct mtk_composite_v4l2_device *pfdev =
container_of(notifier->v4l2_dev,
struct mtk_composite_v4l2_device, v4l2_dev);
bool found = false;
int i;
for (i = 0; i < ARRAY_SIZE(pfdev->asd); i++) {
if (pfdev->asd[i]->match.fwnode ==
asd[0].match.fwnode) {
pfdev->sd[i] = subdev;
found = true;
break;
}
}
if (!found) {
pr_info("sub device (%s) not matched\n", subdev->name);
return -EINVAL;
}
return 0;
}
static int fl_probe_complete(struct mtk_composite_v4l2_device *vpfe)
{
int err;
struct v4l2_subdev *sd;
/* set first sub device as current one */
vpfe->v4l2_dev.ctrl_handler = vpfe->sd[0]->ctrl_handler;
err = v4l2_device_register_subdev_nodes(&vpfe->v4l2_dev);
if (err) {
pr_info("Unable to v4l2_device_register_subdev_nodes\n");
goto probe_out;
}
list_for_each_entry(sd, &vpfe->v4l2_dev.subdevs, list) {
if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
continue;
#if defined(CONFIG_MEDIA_CONTROLLER)
pr_info("%s v4l2:%s\n", __func__, sd->entity.name);
#endif
}
pr_debug("%s -\n", __func__);
return 0;
probe_out:
v4l2_device_unregister(&vpfe->v4l2_dev);
return err;
}
static int fl_async_complete(struct v4l2_async_notifier *notifier)
{
struct mtk_composite_v4l2_device *pfdev =
container_of(notifier->v4l2_dev,
struct mtk_composite_v4l2_device, v4l2_dev);
return fl_probe_complete(pfdev);
}
static struct v4l2_async_subdev *
mtk_get_pdata(struct platform_device *pdev,
struct mtk_composite_v4l2_device *pfdev)
{
struct device_node *endpoint = NULL;
struct v4l2_async_subdev *pdata[MISC_MAX_SUBDEVS] = {0};
struct v4l2_async_notifier *notifier;
unsigned int i;
if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node) {
pr_info("pdev->dev.of_node %p\n", pdev->dev.of_node);
return pdev->dev.platform_data;
}
notifier = &pfdev->notifier;
for (i = 0; ; i++) {
struct device_node *rem;
endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
endpoint);
if (!endpoint)
break;
rem = of_graph_get_remote_port_parent(endpoint);
if (!rem) {
pr_info("Remote device at %s not found\n",
endpoint->full_name);
goto done;
}
pdata[i] = devm_kzalloc(&pdev->dev,
sizeof(struct v4l2_async_subdev),
GFP_KERNEL);
if (!pdata[i]) {
of_node_put(rem);
pr_info("i %d, pdata %p\n", i, pdata[i]);
goto done;
}
pr_debug("rem %p, pdata[i] %p, name %s, full_name %s\n",
rem, pdata[i], rem->name, rem->full_name);
pdata[i]->match_type = V4L2_ASYNC_MATCH_FWNODE;
pdata[i]->match.fwnode = of_fwnode_handle(rem);
of_node_put(rem);
notifier->num_subdevs++;
pfdev->asd[i] = pdata[i];
}
of_node_put(endpoint);
return pdata[0];
done:
of_node_put(endpoint);
return NULL;
}
static void mtk_composite_unregister_entities(
struct mtk_composite_v4l2_device *isp)
{
v4l2_device_unregister(&isp->v4l2_dev);
media_device_unregister(&isp->media_dev);
}
static const struct v4l2_async_notifier_operations fl_async_notify_ops = {
.bound = fl_async_bound,
.complete = fl_async_complete,
};
static int mtk_composite_probe(struct platform_device *dev)
{
struct mtk_composite_v4l2_device *pfdev;
int rc = 0;
pr_info("flash v4l2 probe\n");
pfdev = devm_kzalloc(&dev->dev, sizeof(*pfdev), GFP_KERNEL);
if (!pfdev) {
pr_info("could not allocate memory\n");
return -ENOMEM;
}
pfdev->vdev = video_device_alloc();
if (WARN_ON(!pfdev->vdev)) {
rc = -ENOMEM;
pr_info("failed to allocate video_device\n");
goto vdec_end;
}
pfdev->asd[0] = mtk_get_pdata(dev, pfdev);
pr_debug("asd %p %p %p\n", pfdev->asd[0], pfdev->asd[1],
pfdev->asd[2]);
#if defined(CONFIG_MEDIA_CONTROLLER)
pfdev->v4l2_dev.mdev = kzalloc(sizeof(struct media_device),
GFP_KERNEL);
if (!pfdev->v4l2_dev.mdev) {
rc = -ENOMEM;
pr_info("failed to allocate media_device\n");
goto mdev_end;
}
strlcpy(pfdev->v4l2_dev.mdev->model, "mtk_V4L2_misc_core",
sizeof(pfdev->v4l2_dev.mdev->model));
pfdev->v4l2_dev.mdev->dev = &(dev->dev);
media_device_init(pfdev->v4l2_dev.mdev);
rc = media_device_register(pfdev->v4l2_dev.mdev);
if (WARN_ON(rc < 0)) {
pr_info("failed to register media_device");
goto mdev_end;
}
/* Initialize mutex */
mutex_init(&pfdev->v4l2_dev.mdev->graph_mutex);
rc = media_entity_pads_init(&pfdev->vdev->entity, 0, NULL);
if (WARN_ON(rc < 0)) {
pr_info("media_entity_pads init failed\n");
goto mdev_end;
}
pfdev->vdev->entity.function = MEDIA_ENT_F_IO_V4L;
#endif
rc = v4l2_device_register(&dev->dev, &pfdev->v4l2_dev);
if (rc) {
pr_info("Unable to register v4l2 device.\n");
goto mdev_end;
}
platform_set_drvdata(dev, pfdev);
pr_debug("platform_set_drvdata num_subdevs %d\n",
pfdev->notifier.num_subdevs);
pfdev->sd = devm_kzalloc(&dev->dev, sizeof(struct v4l2_subdev *) *
ARRAY_SIZE(pfdev->asd), GFP_KERNEL);
if (!pfdev->sd) {
rc = -ENOMEM;
pr_info("Unable to devm_kzalloc.\n");
goto mdev_end;
}
pfdev->notifier.subdevs = pfdev->asd;
pfdev->notifier.ops = &fl_async_notify_ops;
rc = v4l2_async_notifier_register(&pfdev->v4l2_dev, &pfdev->notifier);
if (rc) {
pr_info("Error registering async notifier\n");
rc = -EINVAL;
goto mdev_end;
}
return 0;
mdev_end:
kzfree(pfdev->vdev);
vdec_end:
kfree(pfdev);
return rc;
}
static int mtk_composite_remove(struct platform_device *dev)
{
struct mtk_composite_v4l2_device *isp = platform_get_drvdata(dev);
v4l2_async_notifier_unregister(&isp->notifier);
mtk_composite_unregister_entities(isp);
return 0;
}
static void mtk_composite_shutdown(struct platform_device *dev)
{
}
#ifdef CONFIG_OF
static const struct of_device_id mtk_composite_of_match[] = {
{.compatible = "mediatek,mtk_composite_v4l2_1"},
{},
};
MODULE_DEVICE_TABLE(of, mtk_composite_of_match);
#else
static struct platform_device mtk_composite_platform_device[] = {
{
.name = MTK_COMPOSITE_NAME,
.id = 0,
.dev = {}
},
{}
};
MODULE_DEVICE_TABLE(platform, mtk_composite_platform_device);
#endif
static struct platform_driver mtk_composite_platform_driver = {
.probe = mtk_composite_probe,
.remove = mtk_composite_remove,
.shutdown = mtk_composite_shutdown,
.driver = {
.name = MTK_COMPOSITE_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = mtk_composite_of_match,
#endif
},
};
static int __init mtk_composite_init(void)
{
int ret;
pr_debug("Init start\n");
#ifndef CONFIG_OF
ret = platform_device_register(&mtk_composite_platform_device);
if (ret) {
pr_info("Failed to register platform device\n");
return ret;
}
#endif
ret = platform_driver_register(&mtk_composite_platform_driver);
if (ret) {
pr_info("Failed to register platform driver\n");
return ret;
}
pr_debug("Init done\n");
return 0;
}
static void __exit mtk_composite_exit(void)
{
platform_driver_unregister(&mtk_composite_platform_driver);
}
late_initcall(mtk_composite_init);
module_exit(mtk_composite_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("MTK V4L2 Composite Driver");
@@ -0,0 +1,41 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MTK_COMPOSITE_H
#define _MTK_COMPOSITE_H
#include <linux/list.h>
#include <linux/leds.h>
#include <linux/led-class-flash.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/of.h>
#define MISC_MAX_SUBDEVS 8
struct mtk_composite_async_subdev {
struct v4l2_subdev *sd;
struct v4l2_async_subdev asd;
};
struct mtk_composite_v4l2_device {
struct v4l2_device v4l2_dev;
struct v4l2_async_notifier notifier;
struct media_device media_dev;
struct video_device *vdev;
struct device *dev;
u32 revision;
struct v4l2_subdev **sd;
struct v4l2_async_subdev *asd[MISC_MAX_SUBDEVS];
struct v4l2_subdev *subdevs[MISC_MAX_SUBDEVS];
};
#endif /* _MTK_COMPOSITE_H */
@@ -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*/