Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) // TI LM36274 LED chip family driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/leds-ti-lmu-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mfd/ti-lmu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mfd/ti-lmu-register.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <uapi/linux/uleds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define LM36274_MAX_STRINGS	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define LM36274_BL_EN		BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * struct lm36274
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @pdev: platform device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * @led_dev: led class device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * @lmu_data: Register and setting values for common code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * @regmap: Devices register map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * @dev: Pointer to the devices device struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * @led_sources: The LED strings supported in this array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * @num_leds: Number of LED strings are supported in this array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct lm36274 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct led_classdev led_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct ti_lmu_bank lmu_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	u32 led_sources[LM36274_MAX_STRINGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int lm36274_brightness_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 				  enum led_brightness brt_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct lm36274 *chip = container_of(led_cdev, struct lm36274, led_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return ti_lmu_common_set_brightness(&chip->lmu_data, brt_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static int lm36274_init(struct lm36274 *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int enable_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	for (i = 0; i < chip->num_leds; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		enable_val |= (1 << chip->led_sources[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (!enable_val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		dev_err(chip->dev, "No LEDs were enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	enable_val |= LM36274_BL_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return regmap_write(chip->regmap, LM36274_REG_BL_EN, enable_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int lm36274_parse_dt(struct lm36274 *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			    struct led_init_data *init_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct device *dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct fwnode_handle *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	/* There should only be 1 node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (device_get_child_node_count(dev) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	child = device_get_next_child_node(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	init_data->fwnode = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	init_data->devicename = chip->pdev->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* for backwards compatibility when `label` property is not present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	init_data->default_label = ":";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	chip->num_leds = fwnode_property_count_u32(child, "led-sources");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (chip->num_leds <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	ret = fwnode_property_read_u32_array(child, "led-sources",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 					     chip->led_sources, chip->num_leds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		dev_err(dev, "led-sources property missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int lm36274_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct ti_lmu *lmu = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct led_init_data init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct lm36274 *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	chip->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	chip->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	chip->regmap = lmu->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	platform_set_drvdata(pdev, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	ret = lm36274_parse_dt(chip, &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		dev_err(chip->dev, "Failed to parse DT node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ret = lm36274_init(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		fwnode_handle_put(init_data.fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		dev_err(chip->dev, "Failed to init the device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	chip->lmu_data.regmap = chip->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	chip->lmu_data.max_brightness = MAX_BRIGHTNESS_11BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	chip->lmu_data.msb_brightness_reg = LM36274_REG_BRT_MSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	chip->lmu_data.lsb_brightness_reg = LM36274_REG_BRT_LSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	chip->led_dev.max_brightness = MAX_BRIGHTNESS_11BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	chip->led_dev.brightness_set_blocking = lm36274_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	ret = devm_led_classdev_register_ext(chip->dev, &chip->led_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 					     &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		dev_err(chip->dev, "Failed to register LED for node %pfw\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			init_data.fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	fwnode_handle_put(init_data.fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static const struct of_device_id of_lm36274_leds_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	{ .compatible = "ti,lm36274-backlight", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODULE_DEVICE_TABLE(of, of_lm36274_leds_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static struct platform_driver lm36274_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	.probe  = lm36274_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		.name = "lm36274-leds",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		.of_match_table = of_lm36274_leds_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) module_platform_driver(lm36274_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_DESCRIPTION("Texas Instruments LM36274 LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODULE_LICENSE("GPL v2");