Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // TI LM36274 LED chip family driver
0003 // Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/
0004 
0005 #include <linux/bitops.h>
0006 #include <linux/device.h>
0007 #include <linux/err.h>
0008 #include <linux/leds.h>
0009 #include <linux/leds-ti-lmu-common.h>
0010 #include <linux/mod_devicetable.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/property.h>
0014 
0015 #include <linux/mfd/ti-lmu.h>
0016 #include <linux/mfd/ti-lmu-register.h>
0017 
0018 #include <uapi/linux/uleds.h>
0019 
0020 #define LM36274_MAX_STRINGS 4
0021 #define LM36274_BL_EN       BIT(4)
0022 
0023 /**
0024  * struct lm36274
0025  * @pdev: platform device
0026  * @led_dev: led class device
0027  * @lmu_data: Register and setting values for common code
0028  * @regmap: Devices register map
0029  * @dev: Pointer to the devices device struct
0030  * @led_sources: The LED strings supported in this array
0031  * @num_leds: Number of LED strings are supported in this array
0032  */
0033 struct lm36274 {
0034     struct platform_device *pdev;
0035     struct led_classdev led_dev;
0036     struct ti_lmu_bank lmu_data;
0037     struct regmap *regmap;
0038     struct device *dev;
0039 
0040     u32 led_sources[LM36274_MAX_STRINGS];
0041     int num_leds;
0042 };
0043 
0044 static int lm36274_brightness_set(struct led_classdev *led_cdev,
0045                   enum led_brightness brt_val)
0046 {
0047     struct lm36274 *chip = container_of(led_cdev, struct lm36274, led_dev);
0048 
0049     return ti_lmu_common_set_brightness(&chip->lmu_data, brt_val);
0050 }
0051 
0052 static int lm36274_init(struct lm36274 *chip)
0053 {
0054     int enable_val = 0;
0055     int i;
0056 
0057     for (i = 0; i < chip->num_leds; i++)
0058         enable_val |= (1 << chip->led_sources[i]);
0059 
0060     if (!enable_val) {
0061         dev_err(chip->dev, "No LEDs were enabled\n");
0062         return -EINVAL;
0063     }
0064 
0065     enable_val |= LM36274_BL_EN;
0066 
0067     return regmap_write(chip->regmap, LM36274_REG_BL_EN, enable_val);
0068 }
0069 
0070 static int lm36274_parse_dt(struct lm36274 *chip,
0071                 struct led_init_data *init_data)
0072 {
0073     struct device *dev = chip->dev;
0074     struct fwnode_handle *child;
0075     int ret;
0076 
0077     /* There should only be 1 node */
0078     if (device_get_child_node_count(dev) != 1)
0079         return -EINVAL;
0080 
0081     child = device_get_next_child_node(dev, NULL);
0082 
0083     init_data->fwnode = child;
0084     init_data->devicename = chip->pdev->name;
0085     /* for backwards compatibility when `label` property is not present */
0086     init_data->default_label = ":";
0087 
0088     chip->num_leds = fwnode_property_count_u32(child, "led-sources");
0089     if (chip->num_leds <= 0) {
0090         ret = -ENODEV;
0091         goto err;
0092     }
0093 
0094     ret = fwnode_property_read_u32_array(child, "led-sources",
0095                          chip->led_sources, chip->num_leds);
0096     if (ret) {
0097         dev_err(dev, "led-sources property missing\n");
0098         goto err;
0099     }
0100 
0101     return 0;
0102 err:
0103     fwnode_handle_put(child);
0104     return ret;
0105 }
0106 
0107 static int lm36274_probe(struct platform_device *pdev)
0108 {
0109     struct ti_lmu *lmu = dev_get_drvdata(pdev->dev.parent);
0110     struct led_init_data init_data = {};
0111     struct lm36274 *chip;
0112     int ret;
0113 
0114     chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
0115     if (!chip)
0116         return -ENOMEM;
0117 
0118     chip->pdev = pdev;
0119     chip->dev = &pdev->dev;
0120     chip->regmap = lmu->regmap;
0121     platform_set_drvdata(pdev, chip);
0122 
0123     ret = lm36274_parse_dt(chip, &init_data);
0124     if (ret) {
0125         dev_err(chip->dev, "Failed to parse DT node\n");
0126         return ret;
0127     }
0128 
0129     ret = lm36274_init(chip);
0130     if (ret) {
0131         fwnode_handle_put(init_data.fwnode);
0132         dev_err(chip->dev, "Failed to init the device\n");
0133         return ret;
0134     }
0135 
0136     chip->lmu_data.regmap = chip->regmap;
0137     chip->lmu_data.max_brightness = MAX_BRIGHTNESS_11BIT;
0138     chip->lmu_data.msb_brightness_reg = LM36274_REG_BRT_MSB;
0139     chip->lmu_data.lsb_brightness_reg = LM36274_REG_BRT_LSB;
0140 
0141     chip->led_dev.max_brightness = MAX_BRIGHTNESS_11BIT;
0142     chip->led_dev.brightness_set_blocking = lm36274_brightness_set;
0143 
0144     ret = devm_led_classdev_register_ext(chip->dev, &chip->led_dev,
0145                          &init_data);
0146     if (ret)
0147         dev_err(chip->dev, "Failed to register LED for node %pfw\n",
0148             init_data.fwnode);
0149 
0150     fwnode_handle_put(init_data.fwnode);
0151 
0152     return ret;
0153 }
0154 
0155 static const struct of_device_id of_lm36274_leds_match[] = {
0156     { .compatible = "ti,lm36274-backlight", },
0157     {},
0158 };
0159 MODULE_DEVICE_TABLE(of, of_lm36274_leds_match);
0160 
0161 static struct platform_driver lm36274_driver = {
0162     .probe  = lm36274_probe,
0163     .driver = {
0164         .name = "lm36274-leds",
0165         .of_match_table = of_lm36274_leds_match,
0166     },
0167 };
0168 module_platform_driver(lm36274_driver)
0169 
0170 MODULE_DESCRIPTION("Texas Instruments LM36274 LED driver");
0171 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
0172 MODULE_LICENSE("GPL v2");