1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2019 Sven Van Asbroeck
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>
12 struct tps6105x_priv
{
13 struct regmap
*regmap
;
14 struct led_classdev cdev
;
15 struct fwnode_handle
*fwnode
;
18 static void tps6105x_handle_put(void *data
)
20 struct tps6105x_priv
*priv
= data
;
22 fwnode_handle_put(priv
->fwnode
);
25 static int tps6105x_brightness_set(struct led_classdev
*cdev
,
26 enum led_brightness brightness
)
28 struct tps6105x_priv
*priv
= container_of(cdev
, struct tps6105x_priv
,
31 return regmap_update_bits(priv
->regmap
, TPS6105X_REG_0
,
32 TPS6105X_REG0_TORCHC_MASK
,
33 brightness
<< TPS6105X_REG0_TORCHC_SHIFT
);
36 static int tps6105x_led_probe(struct platform_device
*pdev
)
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
;
44 /* This instance is not set for torch mode so bail out */
45 if (pdata
->mode
!= TPS6105X_MODE_TORCH
) {
47 "chip not in torch mode, exit probe");
51 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
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
);
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
;
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
);
74 return devm_led_classdev_register_ext(&pdev
->dev
, &priv
->cdev
,
78 static struct platform_driver led_driver
= {
79 .probe
= tps6105x_led_probe
,
81 .name
= "tps6105x-leds",
85 module_platform_driver(led_driver
);
87 MODULE_DESCRIPTION("TPS6105x LED driver");
88 MODULE_AUTHOR("Sven Van Asbroeck <TheSven73@gmail.com>");
89 MODULE_LICENSE("GPL v2");