Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2019 Sven Van Asbroeck
0004  */
0005 
0006 #include <linux/leds.h>
0007 #include <linux/module.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/mfd/tps6105x.h>
0010 #include <linux/regmap.h>
0011 
0012 struct tps6105x_priv {
0013     struct regmap *regmap;
0014     struct led_classdev cdev;
0015     struct fwnode_handle *fwnode;
0016 };
0017 
0018 static void tps6105x_handle_put(void *data)
0019 {
0020     struct tps6105x_priv *priv = data;
0021 
0022     fwnode_handle_put(priv->fwnode);
0023 }
0024 
0025 static int tps6105x_brightness_set(struct led_classdev *cdev,
0026                   enum led_brightness brightness)
0027 {
0028     struct tps6105x_priv *priv = container_of(cdev, struct tps6105x_priv,
0029                             cdev);
0030 
0031     return regmap_update_bits(priv->regmap, TPS6105X_REG_0,
0032                   TPS6105X_REG0_TORCHC_MASK,
0033                   brightness << TPS6105X_REG0_TORCHC_SHIFT);
0034 }
0035 
0036 static int tps6105x_led_probe(struct platform_device *pdev)
0037 {
0038     struct tps6105x *tps6105x = dev_get_platdata(&pdev->dev);
0039     struct tps6105x_platform_data *pdata = tps6105x->pdata;
0040     struct led_init_data init_data = { };
0041     struct tps6105x_priv *priv;
0042     int ret;
0043 
0044     /* This instance is not set for torch mode so bail out */
0045     if (pdata->mode != TPS6105X_MODE_TORCH) {
0046         dev_info(&pdev->dev,
0047             "chip not in torch mode, exit probe");
0048         return -EINVAL;
0049     }
0050 
0051     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0052     if (!priv)
0053         return -ENOMEM;
0054     /* fwnode/devicetree is optional. NULL is allowed for priv->fwnode */
0055     priv->fwnode = device_get_next_child_node(pdev->dev.parent, NULL);
0056     ret = devm_add_action_or_reset(&pdev->dev, tps6105x_handle_put, priv);
0057     if (ret)
0058         return ret;
0059     priv->regmap = tps6105x->regmap;
0060     priv->cdev.brightness_set_blocking = tps6105x_brightness_set;
0061     priv->cdev.max_brightness = 7;
0062     init_data.devicename = "tps6105x";
0063     init_data.default_label = ":torch";
0064     init_data.fwnode = priv->fwnode;
0065 
0066     ret = regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
0067                  TPS6105X_REG0_MODE_MASK |
0068                     TPS6105X_REG0_TORCHC_MASK,
0069                  TPS6105X_REG0_MODE_TORCH <<
0070                     TPS6105X_REG0_MODE_SHIFT);
0071     if (ret)
0072         return ret;
0073 
0074     return devm_led_classdev_register_ext(&pdev->dev, &priv->cdev,
0075                           &init_data);
0076 }
0077 
0078 static struct platform_driver led_driver = {
0079     .probe = tps6105x_led_probe,
0080     .driver = {
0081         .name = "tps6105x-leds",
0082     },
0083 };
0084 
0085 module_platform_driver(led_driver);
0086 
0087 MODULE_DESCRIPTION("TPS6105x LED driver");
0088 MODULE_AUTHOR("Sven Van Asbroeck <TheSven73@gmail.com>");
0089 MODULE_LICENSE("GPL v2");