Lines Matching +full:channel +full:- +full:use
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
6 * Redistribution and use in source and binary forms, with or without
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 pwm_channel_t channel; member
78 { "pwm-backlight", 1 },
86 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) in pwm_backlight_probe()
103 rv = pwm_get_by_ofw_propidx(dev, node, "pwms", 0, &sc->channel); in pwm_backlight_attach()
105 device_printf(dev, "Cannot map pwm channel %d\n", rv); in pwm_backlight_attach()
109 if (regulator_get_by_ofw_property(dev, 0, "power-supply", in pwm_backlight_attach()
110 &sc->power_supply) != 0) { in pwm_backlight_attach()
111 device_printf(dev, "No power-supply property\n"); in pwm_backlight_attach()
115 if (OF_hasprop(node, "brightness-levels")) { in pwm_backlight_attach()
116 sc->nlevels = OF_getencprop_alloc(node, "brightness-levels", in pwm_backlight_attach()
117 (void **)&sc->levels); in pwm_backlight_attach()
118 if (sc->nlevels <= 0) { in pwm_backlight_attach()
122 sc->nlevels /= sizeof(uint32_t); in pwm_backlight_attach()
124 if (OF_getencprop(node, "default-brightness-level", in pwm_backlight_attach()
125 &sc->default_level, sizeof(uint32_t)) <= 0) { in pwm_backlight_attach()
126 device_printf(dev, "No default-brightness-level while brightness-levels is specified\n"); in pwm_backlight_attach()
129 if (sc->default_level > sc->nlevels) { in pwm_backlight_attach()
130 device_printf(dev, "default-brightness-level isn't present in brightness-levels range\n"); in pwm_backlight_attach()
133 sc->channel->duty = sc->channel->period * sc->levels[sc->default_level] / 100; in pwm_backlight_attach()
137 device_printf(dev, "Number of levels: %zd\n", sc->nlevels); in pwm_backlight_attach()
138 device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period); in pwm_backlight_attach()
139 device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty); in pwm_backlight_attach()
143 PWMBUS_CHANNEL_GET_CONFIG(sc->channel->dev, in pwm_backlight_attach()
144 sc->channel->channel, in pwm_backlight_attach()
145 (unsigned int *)&sc->channel->period, in pwm_backlight_attach()
146 (unsigned int *)&sc->channel->duty); in pwm_backlight_attach()
147 if (sc->channel->duty > sc->channel->period) in pwm_backlight_attach()
148 sc->channel->duty = sc->channel->period; in pwm_backlight_attach()
150 device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period); in pwm_backlight_attach()
151 device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty); in pwm_backlight_attach()
155 regulator_enable(sc->power_supply); in pwm_backlight_attach()
156 sc->channel->enabled = true; in pwm_backlight_attach()
157 PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel, in pwm_backlight_attach()
158 sc->channel->period, sc->channel->duty); in pwm_backlight_attach()
159 PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel, in pwm_backlight_attach()
160 sc->channel->enabled); in pwm_backlight_attach()
162 sc->current_level = pwm_backlight_find_level_per_percent(sc, in pwm_backlight_attach()
163 sc->channel->period / sc->channel->duty); in pwm_backlight_attach()
164 sc->cdev = backlight_register("pwm_backlight", dev); in pwm_backlight_attach()
165 if (sc->cdev == NULL) in pwm_backlight_attach()
177 if (sc->nlevels > 0) in pwm_backlight_detach()
178 OF_prop_free(sc->levels); in pwm_backlight_detach()
179 regulator_disable(sc->power_supply); in pwm_backlight_detach()
180 backlight_destroy(sc->cdev); in pwm_backlight_detach()
191 return (-1); in pwm_backlight_find_level_per_percent()
193 for (i = 0, diff = 0; i < sc->nlevels; i++) { in pwm_backlight_find_level_per_percent()
194 if (sc->levels[i] == percent) in pwm_backlight_find_level_per_percent()
196 else if (sc->levels[i] < percent) in pwm_backlight_find_level_per_percent()
197 diff = percent - sc->levels[i]; in pwm_backlight_find_level_per_percent()
199 if (diff < abs((percent - sc->levels[i]))) in pwm_backlight_find_level_per_percent()
200 return (i - 1); in pwm_backlight_find_level_per_percent()
206 return (-1); in pwm_backlight_find_level_per_percent()
218 if (sc->nlevels != 0) { in pwm_backlight_update_status()
220 props->brightness); in pwm_backlight_update_status()
223 sc->current_level = error; in pwm_backlight_update_status()
224 sc->channel->duty = sc->channel->period * in pwm_backlight_update_status()
225 sc->levels[sc->current_level] / 100; in pwm_backlight_update_status()
227 if (props->brightness > 100 || props->brightness < 0) in pwm_backlight_update_status()
229 sc->channel->duty = sc->channel->period * in pwm_backlight_update_status()
230 props->brightness / 100; in pwm_backlight_update_status()
232 sc->channel->enabled = true; in pwm_backlight_update_status()
233 PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel, in pwm_backlight_update_status()
234 sc->channel->period, sc->channel->duty); in pwm_backlight_update_status()
235 PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel, in pwm_backlight_update_status()
236 sc->channel->enabled); in pwm_backlight_update_status()
237 error = regulator_status(sc->power_supply, ®_status); in pwm_backlight_update_status()
240 "Cannot get power-supply status: %d\n", error); in pwm_backlight_update_status()
242 if (props->brightness > 0) { in pwm_backlight_update_status()
244 regulator_enable(sc->power_supply); in pwm_backlight_update_status()
247 regulator_disable(sc->power_supply); in pwm_backlight_update_status()
262 if (sc->nlevels != 0) { in pwm_backlight_get_status()
263 props->brightness = sc->levels[sc->current_level]; in pwm_backlight_get_status()
264 props->nlevels = sc->nlevels; in pwm_backlight_get_status()
265 for (i = 0; i < sc->nlevels; i++) in pwm_backlight_get_status()
266 props->levels[i] = sc->levels[i]; in pwm_backlight_get_status()
268 props->brightness = sc->channel->duty * 100 / sc->channel->period; in pwm_backlight_get_status()
269 props->nlevels = 0; in pwm_backlight_get_status()
278 info->type = BACKLIGHT_TYPE_PANEL; in pwm_backlight_get_info()
279 strlcpy(info->name, "pwm-backlight", BACKLIGHTMAXNAMELENGTH); in pwm_backlight_get_info()