2019-05-29 23:57:50 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2010-11-11 22:05:22 +00:00
|
|
|
/*
|
|
|
|
* LP5521 LED chip driver.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Nokia Corporation
|
2013-02-05 10:28:00 +00:00
|
|
|
* Copyright (C) 2012 Texas Instruments
|
2010-11-11 22:05:22 +00:00
|
|
|
*
|
|
|
|
* Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
|
2013-02-05 10:28:00 +00:00
|
|
|
* Milo(Woogyom) Kim <milo.kim@ti.com>
|
2010-11-11 22:05:22 +00:00
|
|
|
*/
|
|
|
|
|
2024-06-26 22:15:13 +00:00
|
|
|
#include <linux/cleanup.h>
|
2010-11-11 22:05:22 +00:00
|
|
|
#include <linux/delay.h>
|
2013-02-05 10:26:14 +00:00
|
|
|
#include <linux/firmware.h>
|
|
|
|
#include <linux/i2c.h>
|
2010-11-11 22:05:22 +00:00
|
|
|
#include <linux/leds.h>
|
2013-02-05 10:26:14 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mutex.h>
|
2013-02-05 09:03:08 +00:00
|
|
|
#include <linux/platform_data/leds-lp55xx.h>
|
2013-02-05 10:26:14 +00:00
|
|
|
#include <linux/slab.h>
|
2013-04-23 11:52:59 +00:00
|
|
|
#include <linux/of.h>
|
2013-02-05 09:03:08 +00:00
|
|
|
|
|
|
|
#include "leds-lp55xx-common.h"
|
2010-11-11 22:05:22 +00:00
|
|
|
|
2013-02-05 10:25:26 +00:00
|
|
|
#define LP5521_MAX_LEDS 3
|
|
|
|
#define LP5521_CMD_DIRECT 0x3F
|
2010-11-11 22:05:22 +00:00
|
|
|
|
|
|
|
/* Registers */
|
|
|
|
#define LP5521_REG_ENABLE 0x00
|
|
|
|
#define LP5521_REG_OP_MODE 0x01
|
|
|
|
#define LP5521_REG_R_PWM 0x02
|
|
|
|
#define LP5521_REG_G_PWM 0x03
|
|
|
|
#define LP5521_REG_B_PWM 0x04
|
|
|
|
#define LP5521_REG_R_CURRENT 0x05
|
|
|
|
#define LP5521_REG_G_CURRENT 0x06
|
|
|
|
#define LP5521_REG_B_CURRENT 0x07
|
|
|
|
#define LP5521_REG_CONFIG 0x08
|
|
|
|
#define LP5521_REG_STATUS 0x0C
|
|
|
|
#define LP5521_REG_RESET 0x0D
|
|
|
|
#define LP5521_REG_R_PROG_MEM 0x10
|
|
|
|
#define LP5521_REG_G_PROG_MEM 0x30
|
|
|
|
#define LP5521_REG_B_PROG_MEM 0x50
|
|
|
|
|
|
|
|
/* Base register to set LED current */
|
|
|
|
#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
|
|
|
|
/* Base register to set the brightness */
|
|
|
|
#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
|
|
|
|
|
|
|
|
/* Bits in ENABLE register */
|
|
|
|
#define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
|
|
|
|
#define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
|
|
|
|
#define LP5521_EXEC_RUN 0x2A
|
2012-03-23 22:02:09 +00:00
|
|
|
#define LP5521_ENABLE_DEFAULT \
|
|
|
|
(LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
|
|
|
|
#define LP5521_ENABLE_RUN_PROGRAM \
|
|
|
|
(LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
|
2010-11-11 22:05:22 +00:00
|
|
|
|
2013-03-21 00:37:04 +00:00
|
|
|
/* CONFIG register */
|
|
|
|
#define LP5521_PWM_HF 0x40 /* PWM: 0 = 256Hz, 1 = 558Hz */
|
|
|
|
#define LP5521_PWRSAVE_EN 0x20 /* 1 = Power save mode */
|
2023-04-21 07:53:05 +00:00
|
|
|
#define LP5521_CP_MODE_MASK 0x18 /* Charge pump mode */
|
|
|
|
#define LP5521_CP_MODE_SHIFT 3
|
2013-03-21 00:37:04 +00:00
|
|
|
#define LP5521_R_TO_BATT 0x04 /* R out: 0 = CP, 1 = Vbat */
|
|
|
|
#define LP5521_CLK_INT 0x01 /* Internal clock */
|
2023-04-21 07:53:05 +00:00
|
|
|
#define LP5521_DEFAULT_CFG (LP5521_PWM_HF | LP5521_PWRSAVE_EN)
|
2013-03-21 00:37:04 +00:00
|
|
|
|
2010-11-11 22:05:22 +00:00
|
|
|
/* Status */
|
|
|
|
#define LP5521_EXT_CLK_USED 0x08
|
|
|
|
|
2011-11-01 00:12:24 +00:00
|
|
|
/* default R channel current register value */
|
|
|
|
#define LP5521_REG_R_CURR_DEFAULT 0xAF
|
|
|
|
|
2013-02-05 09:08:49 +00:00
|
|
|
/* Reset register value */
|
|
|
|
#define LP5521_RESET 0xFF
|
|
|
|
|
2013-02-05 10:18:10 +00:00
|
|
|
static inline void lp5521_wait_opmode_done(void)
|
|
|
|
{
|
|
|
|
/* operation mode change needs to be longer than 153 us */
|
|
|
|
usleep_range(200, 300);
|
|
|
|
}
|
|
|
|
|
2013-02-05 09:03:55 +00:00
|
|
|
static inline void lp5521_wait_enable_done(void)
|
|
|
|
{
|
|
|
|
/* it takes more 488 us to update ENABLE register */
|
|
|
|
usleep_range(500, 600);
|
|
|
|
}
|
|
|
|
|
2013-02-05 10:18:10 +00:00
|
|
|
static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
|
2010-11-11 22:05:22 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-02-05 10:18:10 +00:00
|
|
|
/* stop engine */
|
|
|
|
if (!start) {
|
2024-06-26 16:00:19 +00:00
|
|
|
lp55xx_stop_engine(chip);
|
2013-02-05 10:18:10 +00:00
|
|
|
lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
|
|
|
|
lp5521_wait_opmode_done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-26 16:00:12 +00:00
|
|
|
ret = lp55xx_run_engine_common(chip);
|
|
|
|
if (!ret)
|
|
|
|
lp5521_wait_enable_done();
|
2013-02-05 10:18:10 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 09:57:36 +00:00
|
|
|
static int lp5521_post_init_device(struct lp55xx_chip *chip)
|
2010-11-11 22:05:22 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2013-02-05 09:03:55 +00:00
|
|
|
u8 val;
|
2010-11-11 22:05:22 +00:00
|
|
|
|
2013-02-05 09:03:55 +00:00
|
|
|
/*
|
|
|
|
* Make sure that the chip is reset by reading back the r channel
|
|
|
|
* current reg. This is dummy read is required on some platforms -
|
|
|
|
* otherwise further access to the R G B channels in the
|
|
|
|
* LP5521_REG_ENABLE register will not have any effect - strange!
|
|
|
|
*/
|
2013-02-05 09:57:36 +00:00
|
|
|
ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
|
2013-02-05 09:03:55 +00:00
|
|
|
if (ret) {
|
2013-02-05 09:57:36 +00:00
|
|
|
dev_err(&chip->cl->dev, "error in resetting chip\n");
|
2013-02-05 09:03:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (val != LP5521_REG_R_CURR_DEFAULT) {
|
2013-02-05 09:57:36 +00:00
|
|
|
dev_err(&chip->cl->dev,
|
2013-02-05 09:03:55 +00:00
|
|
|
"unexpected data in register (expected 0x%x got 0x%x)\n",
|
|
|
|
LP5521_REG_R_CURR_DEFAULT, val);
|
|
|
|
ret = -EINVAL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
usleep_range(10000, 20000);
|
2010-11-11 22:05:22 +00:00
|
|
|
|
|
|
|
/* Set all PWMs to direct control mode */
|
2013-02-05 09:57:36 +00:00
|
|
|
ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
|
2023-10-20 09:19:31 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-11-11 22:05:22 +00:00
|
|
|
|
2013-03-21 00:37:04 +00:00
|
|
|
/* Update configuration for the clock setting */
|
|
|
|
val = LP5521_DEFAULT_CFG;
|
|
|
|
if (!lp55xx_is_extclk_used(chip))
|
|
|
|
val |= LP5521_CLK_INT;
|
|
|
|
|
2023-04-21 07:53:05 +00:00
|
|
|
val |= (chip->pdata->charge_pump_mode << LP5521_CP_MODE_SHIFT) & LP5521_CP_MODE_MASK;
|
|
|
|
|
2013-02-05 09:57:36 +00:00
|
|
|
ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
|
2013-02-05 09:03:55 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-11-11 22:05:22 +00:00
|
|
|
|
|
|
|
/* Initialize all channels PWM to zero -> leds off */
|
2013-02-05 09:57:36 +00:00
|
|
|
lp55xx_write(chip, LP5521_REG_R_PWM, 0);
|
|
|
|
lp55xx_write(chip, LP5521_REG_G_PWM, 0);
|
|
|
|
lp55xx_write(chip, LP5521_REG_B_PWM, 0);
|
2010-11-11 22:05:22 +00:00
|
|
|
|
|
|
|
/* Set engines are set to run state when OP_MODE enables engines */
|
2013-02-05 09:57:36 +00:00
|
|
|
ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
|
2013-02-05 09:03:55 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-11-11 22:05:22 +00:00
|
|
|
|
2013-02-05 09:03:55 +00:00
|
|
|
lp5521_wait_enable_done();
|
|
|
|
|
|
|
|
return 0;
|
2010-11-11 22:05:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 10:21:43 +00:00
|
|
|
static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
|
2010-11-11 22:05:22 +00:00
|
|
|
{
|
2013-02-05 10:21:43 +00:00
|
|
|
struct lp55xx_platform_data *pdata = chip->pdata;
|
2010-11-11 22:05:22 +00:00
|
|
|
int ret;
|
|
|
|
u8 status;
|
|
|
|
|
2013-02-05 10:21:43 +00:00
|
|
|
ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
|
2010-11-11 22:05:22 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-02-05 10:21:43 +00:00
|
|
|
if (pdata->clock_mode != LP55XX_CLOCK_EXT)
|
|
|
|
return 0;
|
|
|
|
|
2010-11-11 22:05:22 +00:00
|
|
|
/* Check that ext clock is really in use if requested */
|
2013-02-05 10:21:43 +00:00
|
|
|
if ((status & LP5521_EXT_CLK_USED) == 0)
|
|
|
|
return -EIO;
|
|
|
|
|
2010-11-11 22:05:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t lp5521_selftest(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2013-02-05 10:21:43 +00:00
|
|
|
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
|
|
|
|
struct lp55xx_chip *chip = led->chip;
|
2010-11-11 22:05:22 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-06-26 22:15:13 +00:00
|
|
|
guard(mutex)(&chip->lock);
|
|
|
|
|
2010-11-11 22:05:22 +00:00
|
|
|
ret = lp5521_run_selftest(chip, buf);
|
2013-03-15 00:19:36 +00:00
|
|
|
|
2022-12-01 08:11:24 +00:00
|
|
|
return sysfs_emit(buf, "%s\n", ret ? "FAIL" : "OK");
|
2010-11-11 22:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* device attributes */
|
2024-06-26 16:00:20 +00:00
|
|
|
LP55XX_DEV_ATTR_ENGINE_MODE(1);
|
|
|
|
LP55XX_DEV_ATTR_ENGINE_MODE(2);
|
|
|
|
LP55XX_DEV_ATTR_ENGINE_MODE(3);
|
|
|
|
LP55XX_DEV_ATTR_ENGINE_LOAD(1);
|
|
|
|
LP55XX_DEV_ATTR_ENGINE_LOAD(2);
|
|
|
|
LP55XX_DEV_ATTR_ENGINE_LOAD(3);
|
2013-08-08 04:41:40 +00:00
|
|
|
static LP55XX_DEV_ATTR_RO(selftest, lp5521_selftest);
|
2010-11-11 22:05:22 +00:00
|
|
|
|
|
|
|
static struct attribute *lp5521_attributes[] = {
|
2013-08-08 04:41:40 +00:00
|
|
|
&dev_attr_engine1_mode.attr,
|
|
|
|
&dev_attr_engine2_mode.attr,
|
|
|
|
&dev_attr_engine3_mode.attr,
|
|
|
|
&dev_attr_engine1_load.attr,
|
|
|
|
&dev_attr_engine2_load.attr,
|
|
|
|
&dev_attr_engine3_load.attr,
|
2010-11-11 22:05:22 +00:00
|
|
|
&dev_attr_selftest.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group lp5521_group = {
|
|
|
|
.attrs = lp5521_attributes,
|
|
|
|
};
|
|
|
|
|
2013-02-05 09:08:49 +00:00
|
|
|
/* Chip specific configurations */
|
|
|
|
static struct lp55xx_device_config lp5521_cfg = {
|
2024-06-26 16:00:08 +00:00
|
|
|
.reg_op_mode = {
|
|
|
|
.addr = LP5521_REG_OP_MODE,
|
|
|
|
},
|
2024-06-26 16:00:12 +00:00
|
|
|
.reg_exec = {
|
|
|
|
.addr = LP5521_REG_ENABLE,
|
|
|
|
},
|
2013-02-05 09:08:49 +00:00
|
|
|
.reset = {
|
|
|
|
.addr = LP5521_REG_RESET,
|
|
|
|
.val = LP5521_RESET,
|
|
|
|
},
|
2013-02-05 09:09:56 +00:00
|
|
|
.enable = {
|
|
|
|
.addr = LP5521_REG_ENABLE,
|
|
|
|
.val = LP5521_ENABLE_DEFAULT,
|
|
|
|
},
|
2024-06-26 16:00:13 +00:00
|
|
|
.prog_mem_base = {
|
|
|
|
.addr = LP5521_REG_R_PROG_MEM,
|
|
|
|
},
|
2024-06-26 16:00:15 +00:00
|
|
|
.reg_led_pwm_base = {
|
|
|
|
.addr = LP5521_REG_LED_PWM_BASE,
|
|
|
|
},
|
2024-06-26 16:00:17 +00:00
|
|
|
.reg_led_current_base = {
|
|
|
|
.addr = LP5521_REG_LED_CURRENT_BASE,
|
|
|
|
},
|
2013-02-05 10:07:34 +00:00
|
|
|
.max_channel = LP5521_MAX_LEDS,
|
2013-02-05 09:57:36 +00:00
|
|
|
.post_init_device = lp5521_post_init_device,
|
2024-06-26 16:00:15 +00:00
|
|
|
.brightness_fn = lp55xx_led_brightness,
|
2024-06-26 16:00:16 +00:00
|
|
|
.multicolor_brightness_fn = lp55xx_multicolor_brightness,
|
2024-06-26 16:00:17 +00:00
|
|
|
.set_led_current = lp55xx_set_led_current,
|
2024-06-26 16:00:14 +00:00
|
|
|
.firmware_cb = lp55xx_firmware_loaded_cb,
|
2013-02-05 10:18:10 +00:00
|
|
|
.run_engine = lp5521_run_engine,
|
2013-02-05 10:20:45 +00:00
|
|
|
.dev_attr_group = &lp5521_group,
|
2013-02-05 09:08:49 +00:00
|
|
|
};
|
|
|
|
|
2010-11-11 22:05:22 +00:00
|
|
|
static const struct i2c_device_id lp5521_id[] = {
|
2024-06-26 16:00:09 +00:00
|
|
|
{ "lp5521", .driver_data = (kernel_ulong_t)&lp5521_cfg, }, /* Three channel chip */
|
2010-11-11 22:05:22 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, lp5521_id);
|
|
|
|
|
2013-05-09 04:48:07 +00:00
|
|
|
static const struct of_device_id of_lp5521_leds_match[] = {
|
2024-06-26 16:00:09 +00:00
|
|
|
{ .compatible = "national,lp5521", .data = &lp5521_cfg, },
|
2013-05-09 04:48:07 +00:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(of, of_lp5521_leds_match);
|
2023-08-08 11:11:08 +00:00
|
|
|
|
2010-11-11 22:05:22 +00:00
|
|
|
static struct i2c_driver lp5521_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "lp5521",
|
2023-08-08 11:11:08 +00:00
|
|
|
.of_match_table = of_lp5521_leds_match,
|
2010-11-11 22:05:22 +00:00
|
|
|
},
|
2024-06-26 16:00:09 +00:00
|
|
|
.probe = lp55xx_probe,
|
|
|
|
.remove = lp55xx_remove,
|
2010-11-11 22:05:22 +00:00
|
|
|
.id_table = lp5521_id,
|
|
|
|
};
|
|
|
|
|
2012-01-10 23:09:27 +00:00
|
|
|
module_i2c_driver(lp5521_driver);
|
2010-11-11 22:05:22 +00:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
|
2013-02-05 10:28:00 +00:00
|
|
|
MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
|
2010-11-11 22:05:22 +00:00
|
|
|
MODULE_DESCRIPTION("LP5521 LED engine");
|
|
|
|
MODULE_LICENSE("GPL v2");
|