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