1 /*
2  * Copyright (c) 2021 Sky Hero SA
3  * Copyright (c) 2018 Linaro Limited
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #define DT_DRV_COMPAT ti_tlc59108
9 
10 /**
11  * @file
12  * @brief LED driver for the TLC59108 I2C LED driver
13  */
14 
15 #include <zephyr/drivers/i2c.h>
16 #include <zephyr/drivers/led.h>
17 #include <zephyr/sys/util.h>
18 #include <zephyr/kernel.h>
19 #include <zephyr/logging/log.h>
20 
21 LOG_MODULE_REGISTER(tlc59108, CONFIG_LED_LOG_LEVEL);
22 
23 #include "led_context.h"
24 
25 /* TLC59108 max supported LED id */
26 #define TLC59108_MAX_LED 7
27 
28 /* TLC59108 select registers determine the source that drives LED outputs */
29 #define TLC59108_LED_OFF         0x0     /* LED driver off */
30 #define TLC59108_LED_ON          0x1     /* LED driver on */
31 #define TLC59108_LED_PWM         0x2     /* Controlled through PWM */
32 #define TLC59108_LED_GRP_PWM     0x3     /* Controlled through PWM/GRPPWM */
33 
34 /* TLC59108 control register */
35 #define TLC59108_MODE1           0x00
36 #define TLC59108_MODE2           0x01
37 #define TLC59108_PWM_BASE        0x02
38 #define TLC59108_GRPPWM          0x0A
39 #define TLC59108_GRPFREQ         0x0B
40 #define TLC59108_LEDOUT0         0x0C
41 #define TLC59108_LEDOUT1         0x0D
42 
43 /* TLC59108 mode register 1 */
44 #define TLC59108_MODE1_OSC       0x10
45 
46 /* TLC59108 mode register 2 */
47 #define TLC59108_MODE2_DMBLNK    0x20    /* Enable blinking */
48 
49 #define TLC59108_MASK            0x03
50 
51 
52 struct tlc59108_cfg {
53 	struct i2c_dt_spec i2c;
54 };
55 
56 struct tlc59108_data {
57 	struct led_data dev_data;
58 };
59 
tlc59108_set_ledout(const struct device * dev,uint32_t led,uint8_t val)60 static int tlc59108_set_ledout(const struct device *dev, uint32_t led,
61 		uint8_t val)
62 {
63 	const struct tlc59108_cfg *config = dev->config;
64 
65 	if (led < 4) {
66 		if (i2c_reg_update_byte_dt(&config->i2c, TLC59108_LEDOUT0,
67 					   TLC59108_MASK << (led << 1), val << (led << 1))) {
68 			LOG_ERR("LED reg 0x%x update failed", TLC59108_LEDOUT0);
69 			return -EIO;
70 		}
71 	} else {
72 		if (i2c_reg_update_byte_dt(&config->i2c, TLC59108_LEDOUT1,
73 					   TLC59108_MASK << ((led - 4) << 1),
74 					   val << ((led - 4) << 1))) {
75 			LOG_ERR("LED reg 0x%x update failed", TLC59108_LEDOUT1);
76 			return -EIO;
77 		}
78 	}
79 
80 	return 0;
81 }
82 
tlc59108_led_blink(const struct device * dev,uint32_t led,uint32_t delay_on,uint32_t delay_off)83 static int tlc59108_led_blink(const struct device *dev, uint32_t led,
84 		uint32_t delay_on, uint32_t delay_off)
85 {
86 	const struct tlc59108_cfg *config = dev->config;
87 	struct tlc59108_data *data = dev->data;
88 	struct led_data *dev_data = &data->dev_data;
89 	uint8_t gdc, gfrq;
90 	uint32_t period;
91 
92 	period = delay_on + delay_off;
93 
94 	if (led > TLC59108_MAX_LED) {
95 		return -EINVAL;
96 	}
97 
98 	if (period < dev_data->min_period || period > dev_data->max_period) {
99 		return -EINVAL;
100 	}
101 
102 	/*
103 	 * From manual:
104 	 * duty cycle = (GDC / 256) ->
105 	 *	(time_on / period) = (GDC / 256) ->
106 	 *		GDC = ((time_on * 256) / period)
107 	 */
108 	gdc = delay_on * 256U / period;
109 	if (i2c_reg_write_byte_dt(&config->i2c, TLC59108_GRPPWM, gdc)) {
110 		LOG_ERR("LED reg 0x%x write failed", TLC59108_GRPPWM);
111 		return -EIO;
112 	}
113 
114 	/*
115 	 * From manual:
116 	 * period = ((GFRQ + 1) / 24) in seconds.
117 	 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
118 	 *		GFRQ = ((period * 24 / 1000) - 1)
119 	 */
120 	gfrq = (period * 24U / 1000) - 1;
121 	if (i2c_reg_write_byte_dt(&config->i2c, TLC59108_GRPFREQ, gfrq)) {
122 		LOG_ERR("LED reg 0x%x write failed", TLC59108_GRPFREQ);
123 		return -EIO;
124 	}
125 
126 	/* Enable blinking mode */
127 	if (i2c_reg_update_byte_dt(&config->i2c, TLC59108_MODE2, TLC59108_MODE2_DMBLNK,
128 				   TLC59108_MODE2_DMBLNK)) {
129 		LOG_ERR("LED reg 0x%x update failed", TLC59108_MODE2);
130 		return -EIO;
131 	}
132 
133 	/* Select the GRPPWM source to drive the LED output */
134 	return tlc59108_set_ledout(dev, led, TLC59108_LED_GRP_PWM);
135 }
136 
tlc59108_led_set_brightness(const struct device * dev,uint32_t led,uint8_t value)137 static int tlc59108_led_set_brightness(const struct device *dev, uint32_t led,
138 		uint8_t value)
139 {
140 	const struct tlc59108_cfg *config = dev->config;
141 	struct tlc59108_data *data = dev->data;
142 	struct led_data *dev_data = &data->dev_data;
143 	uint8_t val;
144 
145 	if (led > TLC59108_MAX_LED) {
146 		return -EINVAL;
147 	}
148 
149 	if (value < dev_data->min_brightness ||
150 	    value > dev_data->max_brightness) {
151 		return -EINVAL;
152 	}
153 
154 	/* Set the LED brightness value */
155 	val = (value * 255U) / dev_data->max_brightness;
156 	if (i2c_reg_write_byte_dt(&config->i2c, TLC59108_PWM_BASE + led, val)) {
157 		LOG_ERR("LED 0x%x reg write failed", TLC59108_PWM_BASE + led);
158 		return -EIO;
159 	}
160 
161 	/* Set the LED driver to be controlled through its PWMx register. */
162 	return tlc59108_set_ledout(dev, led, TLC59108_LED_PWM);
163 }
164 
tlc59108_led_on(const struct device * dev,uint32_t led)165 static inline int tlc59108_led_on(const struct device *dev, uint32_t led)
166 {
167 	if (led > TLC59108_MAX_LED) {
168 		return -EINVAL;
169 	}
170 
171 	/* Set LED state to ON */
172 	return tlc59108_set_ledout(dev, led, TLC59108_LED_ON);
173 }
174 
tlc59108_led_off(const struct device * dev,uint32_t led)175 static inline int tlc59108_led_off(const struct device *dev, uint32_t led)
176 {
177 	if (led > TLC59108_MAX_LED) {
178 		return -EINVAL;
179 	}
180 
181 	/* Set LED state to OFF */
182 	return tlc59108_set_ledout(dev, led, TLC59108_LED_OFF);
183 }
184 
tlc59108_led_init(const struct device * dev)185 static int tlc59108_led_init(const struct device *dev)
186 {
187 	const struct tlc59108_cfg *config = dev->config;
188 	struct tlc59108_data *data = dev->data;
189 	struct led_data *dev_data = &data->dev_data;
190 
191 	if (!device_is_ready(config->i2c.bus)) {
192 		LOG_ERR("I2C bus device %s is not ready", config->i2c.bus->name);
193 		return -ENODEV;
194 	}
195 
196 	/* Wake up from sleep mode */
197 	if (i2c_reg_update_byte_dt(&config->i2c, TLC59108_MODE1, TLC59108_MODE1_OSC, 0)) {
198 		LOG_ERR("LED reg 0x%x update failed", TLC59108_MODE1);
199 		return -EIO;
200 	}
201 
202 	/* Hardware specific limits */
203 	dev_data->min_period = 41U;
204 	dev_data->max_period = 10730U;
205 	dev_data->min_brightness = 0U;
206 	dev_data->max_brightness = 100U;
207 
208 	return 0;
209 }
210 
211 static DEVICE_API(led, tlc59108_led_api) = {
212 	.blink = tlc59108_led_blink,
213 	.set_brightness = tlc59108_led_set_brightness,
214 	.on = tlc59108_led_on,
215 	.off = tlc59108_led_off,
216 };
217 
218 #define TLC59108_DEVICE(id) \
219 	static const struct tlc59108_cfg tlc59108_##id##_cfg = {	\
220 		.i2c = I2C_DT_SPEC_INST_GET(id),			\
221 	};								\
222 	static struct tlc59108_data tlc59108_##id##_data;		\
223 									\
224 	DEVICE_DT_INST_DEFINE(id, &tlc59108_led_init, NULL,		\
225 			&tlc59108_##id##_data,				\
226 			&tlc59108_##id##_cfg, POST_KERNEL,		\
227 			CONFIG_LED_INIT_PRIORITY,			\
228 			&tlc59108_led_api);
229 
230 DT_INST_FOREACH_STATUS_OKAY(TLC59108_DEVICE)
231