xref: /linux/drivers/leds/leds-lp3952.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *	LED driver for TI lp3952 controller
4  *
5  *	Copyright (C) 2016, DAQRI, LLC.
6  *	Author: Tony Makkiel <tony.makkiel@daqri.com>
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/leds.h>
15 #include <linux/leds-lp3952.h>
16 #include <linux/module.h>
17 #include <linux/notifier.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm.h>
20 #include <linux/reboot.h>
21 #include <linux/regmap.h>
22 
lp3952_register_write(struct i2c_client * client,u8 reg,u8 val)23 static int lp3952_register_write(struct i2c_client *client, u8 reg, u8 val)
24 {
25 	int ret;
26 	struct lp3952_led_array *priv = i2c_get_clientdata(client);
27 
28 	ret = regmap_write(priv->regmap, reg, val);
29 
30 	if (ret)
31 		dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
32 			__func__, reg, val, ret);
33 	return ret;
34 }
35 
lp3952_on_off(struct lp3952_led_array * priv,enum lp3952_leds led_id,bool on)36 static void lp3952_on_off(struct lp3952_led_array *priv,
37 			  enum lp3952_leds led_id, bool on)
38 {
39 	int ret, val;
40 
41 	dev_dbg(&priv->client->dev, "%s LED %d to %d\n", __func__, led_id, on);
42 
43 	val = 1 << led_id;
44 	if (led_id == LP3952_LED_ALL)
45 		val = LP3952_LED_MASK_ALL;
46 
47 	ret = regmap_update_bits(priv->regmap, LP3952_REG_LED_CTRL, val,
48 				 on ? val : 0);
49 	if (ret)
50 		dev_err(&priv->client->dev, "%s, Error %d\n", __func__, ret);
51 }
52 
53 /*
54  * Using Imax to control brightness. There are 4 possible
55  * setting 25, 50, 75 and 100 % of Imax. Possible values are
56  * values 0-4. 0 meaning turn off.
57  */
lp3952_set_brightness(struct led_classdev * cdev,enum led_brightness value)58 static int lp3952_set_brightness(struct led_classdev *cdev,
59 				 enum led_brightness value)
60 {
61 	unsigned int reg, shift_val;
62 	struct lp3952_ctrl_hdl *led = container_of(cdev,
63 						   struct lp3952_ctrl_hdl,
64 						   cdev);
65 	struct lp3952_led_array *priv = (struct lp3952_led_array *)led->priv;
66 
67 	dev_dbg(cdev->dev, "Brightness request: %d on %d\n", value,
68 		led->channel);
69 
70 	if (value == LED_OFF) {
71 		lp3952_on_off(priv, led->channel, false);
72 		return 0;
73 	}
74 
75 	if (led->channel > LP3952_RED_1) {
76 		dev_err(cdev->dev, " %s Invalid LED requested", __func__);
77 		return -EINVAL;
78 	}
79 
80 	if (led->channel >= LP3952_BLUE_1) {
81 		reg = LP3952_REG_RGB1_MAX_I_CTRL;
82 		shift_val = (led->channel - LP3952_BLUE_1) * 2;
83 	} else {
84 		reg = LP3952_REG_RGB2_MAX_I_CTRL;
85 		shift_val = led->channel * 2;
86 	}
87 
88 	/* Enable the LED in case it is not enabled already */
89 	lp3952_on_off(priv, led->channel, true);
90 
91 	return regmap_update_bits(priv->regmap, reg, 3 << shift_val,
92 				  --value << shift_val);
93 }
94 
lp3952_get_label(struct device * dev,const char * label,char * dest)95 static int lp3952_get_label(struct device *dev, const char *label, char *dest)
96 {
97 	int ret;
98 	const char *str;
99 
100 	ret = device_property_read_string(dev, label, &str);
101 	if (ret)
102 		return ret;
103 
104 	strscpy(dest, str, LP3952_LABEL_MAX_LEN);
105 	return 0;
106 }
107 
lp3952_register_led_classdev(struct lp3952_led_array * priv)108 static int lp3952_register_led_classdev(struct lp3952_led_array *priv)
109 {
110 	int i, acpi_ret, ret = -ENODEV;
111 	static const char *led_name_hdl[LP3952_LED_ALL] = {
112 		"blue2",
113 		"green2",
114 		"red2",
115 		"blue1",
116 		"green1",
117 		"red1"
118 	};
119 
120 	for (i = 0; i < LP3952_LED_ALL; i++) {
121 		acpi_ret = lp3952_get_label(&priv->client->dev, led_name_hdl[i],
122 					    priv->leds[i].name);
123 		if (acpi_ret)
124 			continue;
125 
126 		priv->leds[i].cdev.name = priv->leds[i].name;
127 		priv->leds[i].cdev.brightness = LED_OFF;
128 		priv->leds[i].cdev.max_brightness = LP3952_BRIGHT_MAX;
129 		priv->leds[i].cdev.brightness_set_blocking =
130 				lp3952_set_brightness;
131 		priv->leds[i].channel = i;
132 		priv->leds[i].priv = priv;
133 
134 		ret = devm_led_classdev_register(&priv->client->dev,
135 						 &priv->leds[i].cdev);
136 		if (ret < 0) {
137 			dev_err(&priv->client->dev,
138 				"couldn't register LED %s\n",
139 				priv->leds[i].cdev.name);
140 			break;
141 		}
142 	}
143 	return ret;
144 }
145 
lp3952_set_pattern_gen_cmd(struct lp3952_led_array * priv,u8 cmd_index,u8 r,u8 g,u8 b,enum lp3952_tt tt,enum lp3952_cet cet)146 static int lp3952_set_pattern_gen_cmd(struct lp3952_led_array *priv,
147 				      u8 cmd_index, u8 r, u8 g, u8 b,
148 				      enum lp3952_tt tt, enum lp3952_cet cet)
149 {
150 	int ret;
151 	struct ptrn_gen_cmd line = {
152 		{
153 			{
154 				.r = r,
155 				.g = g,
156 				.b = b,
157 				.cet = cet,
158 				.tt = tt
159 			}
160 		}
161 	};
162 
163 	if (cmd_index >= LP3952_CMD_REG_COUNT)
164 		return -EINVAL;
165 
166 	ret = lp3952_register_write(priv->client,
167 				    LP3952_REG_CMD_0 + cmd_index * 2,
168 				    line.bytes.msb);
169 	if (ret)
170 		return ret;
171 
172 	return lp3952_register_write(priv->client,
173 				      LP3952_REG_CMD_0 + cmd_index * 2 + 1,
174 				      line.bytes.lsb);
175 }
176 
lp3952_configure(struct lp3952_led_array * priv)177 static int lp3952_configure(struct lp3952_led_array *priv)
178 {
179 	int ret;
180 
181 	/* Disable any LEDs on from any previous conf. */
182 	ret = lp3952_register_write(priv->client, LP3952_REG_LED_CTRL, 0);
183 	if (ret)
184 		return ret;
185 
186 	/* enable rgb patter, loop */
187 	ret = lp3952_register_write(priv->client, LP3952_REG_PAT_GEN_CTRL,
188 				    LP3952_PATRN_LOOP | LP3952_PATRN_GEN_EN);
189 	if (ret)
190 		return ret;
191 
192 	/* Update Bit 6 (Active mode), Select both Led sets, Bit [1:0] */
193 	ret = lp3952_register_write(priv->client, LP3952_REG_ENABLES,
194 				    LP3952_ACTIVE_MODE | LP3952_INT_B00ST_LDR);
195 	if (ret)
196 		return ret;
197 
198 	/* Set Cmd1 for RGB intensity,cmd and transition time */
199 	return lp3952_set_pattern_gen_cmd(priv, 0, I46, I71, I100, TT0,
200 					   CET197);
201 }
202 
203 static const struct regmap_config lp3952_regmap = {
204 	.reg_bits = 8,
205 	.val_bits = 8,
206 	.max_register = REG_MAX,
207 	.cache_type = REGCACHE_MAPLE,
208 };
209 
gpio_set_low_action(void * data)210 static void gpio_set_low_action(void *data)
211 {
212 	struct lp3952_led_array *priv = data;
213 
214 	gpiod_set_value(priv->enable_gpio, 0);
215 }
216 
lp3952_probe(struct i2c_client * client)217 static int lp3952_probe(struct i2c_client *client)
218 {
219 	int status;
220 	struct lp3952_led_array *priv;
221 
222 	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
223 	if (!priv)
224 		return -ENOMEM;
225 
226 	priv->client = client;
227 
228 	priv->enable_gpio = devm_gpiod_get(&client->dev, "nrst",
229 					   GPIOD_OUT_HIGH);
230 	if (IS_ERR(priv->enable_gpio)) {
231 		status = PTR_ERR(priv->enable_gpio);
232 		dev_err(&client->dev, "Failed to enable gpio: %d\n", status);
233 		return status;
234 	}
235 
236 	status = devm_add_action(&client->dev, gpio_set_low_action, priv);
237 	if (status)
238 		return status;
239 
240 	priv->regmap = devm_regmap_init_i2c(client, &lp3952_regmap);
241 	if (IS_ERR(priv->regmap)) {
242 		int err = PTR_ERR(priv->regmap);
243 
244 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
245 			err);
246 		return err;
247 	}
248 
249 	i2c_set_clientdata(client, priv);
250 
251 	status = lp3952_configure(priv);
252 	if (status) {
253 		dev_err(&client->dev, "Probe failed. Device not found (%d)\n",
254 			status);
255 		return status;
256 	}
257 
258 	status = lp3952_register_led_classdev(priv);
259 	if (status) {
260 		dev_err(&client->dev, "Unable to register led_classdev: %d\n",
261 			status);
262 		return status;
263 	}
264 
265 	return 0;
266 }
267 
268 static const struct i2c_device_id lp3952_id[] = {
269 	{ LP3952_NAME },
270 	{}
271 };
272 MODULE_DEVICE_TABLE(i2c, lp3952_id);
273 
274 static struct i2c_driver lp3952_i2c_driver = {
275 	.driver = {
276 			.name = LP3952_NAME,
277 	},
278 	.probe = lp3952_probe,
279 	.id_table = lp3952_id,
280 };
281 
282 module_i2c_driver(lp3952_i2c_driver);
283 
284 MODULE_AUTHOR("Tony Makkiel <tony.makkiel@daqri.com>");
285 MODULE_DESCRIPTION("lp3952 I2C LED controller driver");
286 MODULE_LICENSE("GPL v2");
287