2019-06-05 20:56:34 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
// TI LM36274 LED chip family driver
|
2020-07-13 22:51:15 +08:00
|
|
|
// Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/
|
2019-06-05 20:56:34 +08:00
|
|
|
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/leds.h>
|
|
|
|
#include <linux/leds-ti-lmu-common.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of_device.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
|
|
|
|
#include <linux/mfd/ti-lmu.h>
|
|
|
|
#include <linux/mfd/ti-lmu-register.h>
|
|
|
|
|
|
|
|
#include <uapi/linux/uleds.h>
|
|
|
|
|
|
|
|
#define LM36274_MAX_STRINGS 4
|
|
|
|
#define LM36274_BL_EN BIT(4)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct lm36274
|
|
|
|
* @pdev: platform device
|
|
|
|
* @led_dev: led class device
|
|
|
|
* @lmu_data: Register and setting values for common code
|
|
|
|
* @regmap: Devices register map
|
|
|
|
* @dev: Pointer to the devices device struct
|
|
|
|
* @led_sources - The LED strings supported in this array
|
|
|
|
* @num_leds - Number of LED strings are supported in this array
|
|
|
|
*/
|
|
|
|
struct lm36274 {
|
|
|
|
struct platform_device *pdev;
|
|
|
|
struct led_classdev led_dev;
|
|
|
|
struct ti_lmu_bank lmu_data;
|
|
|
|
struct regmap *regmap;
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
u32 led_sources[LM36274_MAX_STRINGS];
|
|
|
|
int num_leds;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int lm36274_brightness_set(struct led_classdev *led_cdev,
|
2020-09-20 02:02:56 +08:00
|
|
|
enum led_brightness brt_val)
|
2019-06-05 20:56:34 +08:00
|
|
|
{
|
2020-09-20 02:02:56 +08:00
|
|
|
struct lm36274 *chip = container_of(led_cdev, struct lm36274, led_dev);
|
2019-06-05 20:56:34 +08:00
|
|
|
|
2020-09-20 02:02:56 +08:00
|
|
|
return ti_lmu_common_set_brightness(&chip->lmu_data, brt_val);
|
2019-06-05 20:56:34 +08:00
|
|
|
}
|
|
|
|
|
2020-09-20 02:02:56 +08:00
|
|
|
static int lm36274_init(struct lm36274 *chip)
|
2019-06-05 20:56:34 +08:00
|
|
|
{
|
|
|
|
int enable_val = 0;
|
|
|
|
int i;
|
|
|
|
|
2020-09-20 02:02:56 +08:00
|
|
|
for (i = 0; i < chip->num_leds; i++)
|
|
|
|
enable_val |= (1 << chip->led_sources[i]);
|
2019-06-05 20:56:34 +08:00
|
|
|
|
|
|
|
if (!enable_val) {
|
2020-09-20 02:02:56 +08:00
|
|
|
dev_err(chip->dev, "No LEDs were enabled\n");
|
2019-06-05 20:56:34 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_val |= LM36274_BL_EN;
|
|
|
|
|
2020-09-20 02:02:56 +08:00
|
|
|
return regmap_write(chip->regmap, LM36274_REG_BL_EN, enable_val);
|
2019-06-05 20:56:34 +08:00
|
|
|
}
|
|
|
|
|
2020-09-20 02:02:58 +08:00
|
|
|
static int lm36274_parse_dt(struct lm36274 *chip,
|
|
|
|
struct led_init_data *init_data)
|
2019-06-05 20:56:34 +08:00
|
|
|
{
|
2020-09-20 02:02:56 +08:00
|
|
|
struct device *dev = &chip->pdev->dev;
|
2020-09-20 02:02:58 +08:00
|
|
|
struct fwnode_handle *child;
|
2020-09-20 02:02:57 +08:00
|
|
|
int ret;
|
2019-06-05 20:56:34 +08:00
|
|
|
|
|
|
|
/* There should only be 1 node */
|
2020-09-20 02:02:57 +08:00
|
|
|
if (device_get_child_node_count(dev) != 1)
|
2019-06-05 20:56:34 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2020-09-20 02:02:57 +08:00
|
|
|
child = device_get_next_child_node(dev, NULL);
|
|
|
|
|
2020-09-20 02:02:58 +08:00
|
|
|
init_data->fwnode = child;
|
|
|
|
init_data->devicename = chip->pdev->name;
|
|
|
|
/* for backwards compatibility when `label` property is not present */
|
|
|
|
init_data->default_label = ":";
|
2020-09-20 02:02:57 +08:00
|
|
|
|
|
|
|
chip->num_leds = fwnode_property_count_u32(child, "led-sources");
|
2020-09-20 02:02:58 +08:00
|
|
|
if (chip->num_leds <= 0) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto err;
|
|
|
|
}
|
2019-06-05 20:56:34 +08:00
|
|
|
|
2020-09-20 02:02:57 +08:00
|
|
|
ret = fwnode_property_read_u32_array(child, "led-sources",
|
|
|
|
chip->led_sources, chip->num_leds);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "led-sources property missing\n");
|
2020-09-20 02:02:58 +08:00
|
|
|
goto err;
|
2019-06-05 20:56:34 +08:00
|
|
|
}
|
|
|
|
|
2020-09-20 02:02:57 +08:00
|
|
|
fwnode_property_read_string(child, "linux,default-trigger",
|
|
|
|
&chip->led_dev.default_trigger);
|
|
|
|
|
2019-06-05 20:56:34 +08:00
|
|
|
return 0;
|
2020-09-20 02:02:58 +08:00
|
|
|
err:
|
|
|
|
fwnode_handle_put(child);
|
|
|
|
return ret;
|
2019-06-05 20:56:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lm36274_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct ti_lmu *lmu = dev_get_drvdata(pdev->dev.parent);
|
2020-09-20 02:02:58 +08:00
|
|
|
struct led_init_data init_data = {};
|
2020-09-20 02:02:56 +08:00
|
|
|
struct lm36274 *chip;
|
2019-06-05 20:56:34 +08:00
|
|
|
int ret;
|
|
|
|
|
2020-09-20 02:02:56 +08:00
|
|
|
chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
|
|
|
|
if (!chip)
|
2019-06-05 20:56:34 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-09-20 02:02:56 +08:00
|
|
|
chip->pdev = pdev;
|
|
|
|
chip->dev = lmu->dev;
|
|
|
|
chip->regmap = lmu->regmap;
|
|
|
|
platform_set_drvdata(pdev, chip);
|
2019-06-05 20:56:34 +08:00
|
|
|
|
2020-09-20 02:02:58 +08:00
|
|
|
ret = lm36274_parse_dt(chip, &init_data);
|
2019-06-05 20:56:34 +08:00
|
|
|
if (ret) {
|
2020-09-20 02:02:56 +08:00
|
|
|
dev_err(chip->dev, "Failed to parse DT node\n");
|
2019-06-05 20:56:34 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-20 02:02:56 +08:00
|
|
|
ret = lm36274_init(chip);
|
2019-06-05 20:56:34 +08:00
|
|
|
if (ret) {
|
2020-09-20 02:02:56 +08:00
|
|
|
dev_err(chip->dev, "Failed to init the device\n");
|
2019-06-05 20:56:34 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-20 02:02:59 +08:00
|
|
|
chip->lmu_data.regmap = chip->regmap;
|
|
|
|
chip->lmu_data.max_brightness = MAX_BRIGHTNESS_11BIT;
|
|
|
|
chip->lmu_data.msb_brightness_reg = LM36274_REG_BRT_MSB;
|
|
|
|
chip->lmu_data.lsb_brightness_reg = LM36274_REG_BRT_LSB;
|
|
|
|
|
|
|
|
chip->led_dev.max_brightness = MAX_BRIGHTNESS_11BIT;
|
|
|
|
chip->led_dev.brightness_set_blocking = lm36274_brightness_set;
|
|
|
|
|
2020-09-20 02:02:58 +08:00
|
|
|
ret = led_classdev_register_ext(chip->dev, &chip->led_dev, &init_data);
|
|
|
|
if (ret)
|
|
|
|
dev_err(chip->dev, "Failed to register LED for node %pfw\n",
|
|
|
|
init_data.fwnode);
|
|
|
|
|
|
|
|
fwnode_handle_put(init_data.fwnode);
|
|
|
|
|
|
|
|
return ret;
|
2020-06-01 21:39:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lm36274_remove(struct platform_device *pdev)
|
|
|
|
{
|
2020-09-20 02:02:56 +08:00
|
|
|
struct lm36274 *chip = platform_get_drvdata(pdev);
|
2020-06-01 21:39:48 +08:00
|
|
|
|
2020-09-20 02:02:56 +08:00
|
|
|
led_classdev_unregister(&chip->led_dev);
|
2020-06-01 21:39:48 +08:00
|
|
|
|
|
|
|
return 0;
|
2019-06-05 20:56:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id of_lm36274_leds_match[] = {
|
|
|
|
{ .compatible = "ti,lm36274-backlight", },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, of_lm36274_leds_match);
|
|
|
|
|
|
|
|
static struct platform_driver lm36274_driver = {
|
|
|
|
.probe = lm36274_probe,
|
2020-06-01 21:39:48 +08:00
|
|
|
.remove = lm36274_remove,
|
2019-06-05 20:56:34 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "lm36274-leds",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
module_platform_driver(lm36274_driver)
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Texas Instruments LM36274 LED driver");
|
|
|
|
MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
|
|
|
|
MODULE_LICENSE("GPL v2");
|