summaryrefslogtreecommitdiff
path: root/drivers/leds/leds-bd2606mvv.c
blob: 3fda712d2f80955fb8fe1a6d03c864634e683f87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2023 Andreas Kemnade
 *
 * Datasheet:
 * https://fscdn.rohm.com/en/products/databook/datasheet/ic/power/led_driver/bd2606mvv_1-e.pdf
 *
 * If LED brightness cannot be controlled independently due to shared
 * brightness registers, max_brightness is set to 1 and only on/off
 * is possible for the affected LED pair.
 */

#include <linux/i2c.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/slab.h>

#define BD2606_MAX_LEDS 6
#define BD2606_MAX_BRIGHTNESS 63
#define BD2606_REG_PWRCNT 3
#define ldev_to_led(c)	container_of(c, struct bd2606mvv_led, ldev)

struct bd2606mvv_led {
	unsigned int led_no;
	struct led_classdev ldev;
	struct bd2606mvv_priv *priv;
};

struct bd2606mvv_priv {
	struct bd2606mvv_led leds[BD2606_MAX_LEDS];
	struct regmap *regmap;
};

static int
bd2606mvv_brightness_set(struct led_classdev *led_cdev,
		      enum led_brightness brightness)
{
	struct bd2606mvv_led *led = ldev_to_led(led_cdev);
	struct bd2606mvv_priv *priv = led->priv;
	int err;

	if (brightness == 0)
		return regmap_update_bits(priv->regmap,
					  BD2606_REG_PWRCNT,
					  1 << led->led_no,
					  0);

	/* shared brightness register */
	err = regmap_write(priv->regmap, led->led_no / 2,
			   led_cdev->max_brightness == 1 ?
			   BD2606_MAX_BRIGHTNESS : brightness);
	if (err)
		return err;

	return regmap_update_bits(priv->regmap,
				  BD2606_REG_PWRCNT,
				  1 << led->led_no,
				  1 << led->led_no);
}

static const struct regmap_config bd2606mvv_regmap = {
	.reg_bits = 8,
	.val_bits = 8,
	.max_register = 0x3,
};

static int bd2606mvv_probe(struct i2c_client *client)
{
	struct fwnode_handle *np, *child;
	struct device *dev = &client->dev;
	struct bd2606mvv_priv *priv;
	struct fwnode_handle *led_fwnodes[BD2606_MAX_LEDS] = { 0 };
	int active_pairs[BD2606_MAX_LEDS / 2] = { 0 };
	int err, reg;
	int i;

	np = dev_fwnode(dev);
	if (!np)
		return -ENODEV;

	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->regmap = devm_regmap_init_i2c(client, &bd2606mvv_regmap);
	if (IS_ERR(priv->regmap)) {
		err = PTR_ERR(priv->regmap);
		dev_err(dev, "Failed to allocate register map: %d\n", err);
		return err;
	}

	i2c_set_clientdata(client, priv);

	fwnode_for_each_available_child_node(np, child) {
		struct bd2606mvv_led *led;

		err = fwnode_property_read_u32(child, "reg", &reg);
		if (err) {
			fwnode_handle_put(child);
			return err;
		}
		if (reg < 0 || reg >= BD2606_MAX_LEDS || led_fwnodes[reg]) {
			fwnode_handle_put(child);
			return -EINVAL;
		}
		led = &priv->leds[reg];
		led_fwnodes[reg] = child;
		active_pairs[reg / 2]++;
		led->priv = priv;
		led->led_no = reg;
		led->ldev.brightness_set_blocking = bd2606mvv_brightness_set;
		led->ldev.max_brightness = BD2606_MAX_BRIGHTNESS;
	}

	for (i = 0; i < BD2606_MAX_LEDS; i++) {
		struct led_init_data init_data = {};

		if (!led_fwnodes[i])
			continue;

		init_data.fwnode = led_fwnodes[i];
		/* Check whether brightness can be independently adjusted. */
		if (active_pairs[i / 2] == 2)
			priv->leds[i].ldev.max_brightness = 1;

		err = devm_led_classdev_register_ext(dev,
						     &priv->leds[i].ldev,
						     &init_data);
		if (err < 0) {
			fwnode_handle_put(child);
			return dev_err_probe(dev, err,
					     "couldn't register LED %s\n",
					     priv->leds[i].ldev.name);
		}
	}
	return 0;
}

static const struct of_device_id __maybe_unused of_bd2606mvv_leds_match[] = {
	{ .compatible = "rohm,bd2606mvv", },
	{},
};
MODULE_DEVICE_TABLE(of, of_bd2606mvv_leds_match);

static struct i2c_driver bd2606mvv_driver = {
	.driver   = {
		.name    = "leds-bd2606mvv",
		.of_match_table = of_match_ptr(of_bd2606mvv_leds_match),
	},
	.probe = bd2606mvv_probe,
};

module_i2c_driver(bd2606mvv_driver);

MODULE_AUTHOR("Andreas Kemnade <andreas@kemnade.info>");
MODULE_DESCRIPTION("BD2606 LED driver");
MODULE_LICENSE("GPL");