xref: /openbmc/linux/drivers/leds/leds-tps6105x.c (revision b4e18b29)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2019 Sven Van Asbroeck
4  */
5 
6 #include <linux/leds.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/mfd/tps6105x.h>
10 #include <linux/regmap.h>
11 
12 struct tps6105x_priv {
13 	struct regmap *regmap;
14 	struct led_classdev cdev;
15 	struct fwnode_handle *fwnode;
16 };
17 
18 static void tps6105x_handle_put(void *data)
19 {
20 	struct tps6105x_priv *priv = data;
21 
22 	fwnode_handle_put(priv->fwnode);
23 }
24 
25 static int tps6105x_brightness_set(struct led_classdev *cdev,
26 				  enum led_brightness brightness)
27 {
28 	struct tps6105x_priv *priv = container_of(cdev, struct tps6105x_priv,
29 							cdev);
30 
31 	return regmap_update_bits(priv->regmap, TPS6105X_REG_0,
32 				  TPS6105X_REG0_TORCHC_MASK,
33 				  brightness << TPS6105X_REG0_TORCHC_SHIFT);
34 }
35 
36 static int tps6105x_led_probe(struct platform_device *pdev)
37 {
38 	struct tps6105x *tps6105x = dev_get_platdata(&pdev->dev);
39 	struct tps6105x_platform_data *pdata = tps6105x->pdata;
40 	struct led_init_data init_data = { };
41 	struct tps6105x_priv *priv;
42 	int ret;
43 
44 	/* This instance is not set for torch mode so bail out */
45 	if (pdata->mode != TPS6105X_MODE_TORCH) {
46 		dev_info(&pdev->dev,
47 			"chip not in torch mode, exit probe");
48 		return -EINVAL;
49 	}
50 
51 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
52 	if (!priv)
53 		return -ENOMEM;
54 	/* fwnode/devicetree is optional. NULL is allowed for priv->fwnode */
55 	priv->fwnode = device_get_next_child_node(pdev->dev.parent, NULL);
56 	ret = devm_add_action_or_reset(&pdev->dev, tps6105x_handle_put, priv);
57 	if (ret)
58 		return ret;
59 	priv->regmap = tps6105x->regmap;
60 	priv->cdev.brightness_set_blocking = tps6105x_brightness_set;
61 	priv->cdev.max_brightness = 7;
62 	init_data.devicename = "tps6105x";
63 	init_data.default_label = ":torch";
64 	init_data.fwnode = priv->fwnode;
65 
66 	ret = regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
67 				 TPS6105X_REG0_MODE_MASK |
68 					TPS6105X_REG0_TORCHC_MASK,
69 				 TPS6105X_REG0_MODE_TORCH <<
70 					TPS6105X_REG0_MODE_SHIFT);
71 	if (ret)
72 		return ret;
73 
74 	return devm_led_classdev_register_ext(&pdev->dev, &priv->cdev,
75 					      &init_data);
76 }
77 
78 static struct platform_driver led_driver = {
79 	.probe = tps6105x_led_probe,
80 	.driver = {
81 		.name = "tps6105x-leds",
82 	},
83 };
84 
85 module_platform_driver(led_driver);
86 
87 MODULE_DESCRIPTION("TPS6105x LED driver");
88 MODULE_AUTHOR("Sven Van Asbroeck <TheSven73@gmail.com>");
89 MODULE_LICENSE("GPL v2");
90