Lines Matching +full:led +full:- +full:s
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Silergy SY7802 flash LED driver with an I2C interface
11 #include <linux/led-class-flash.h>
111 struct sy7802_led *led = container_of(lcdev, struct sy7802_led, flash.led_cdev); in sy7802_torch_brightness_set() local
112 struct sy7802 *chip = led->chip; in sy7802_torch_brightness_set()
120 mutex_lock(&chip->mutex); in sy7802_torch_brightness_set()
122 if (chip->fled_strobe_used) { in sy7802_torch_brightness_set()
123 dev_warn(chip->dev, "Cannot set torch brightness whilst strobe is enabled\n"); in sy7802_torch_brightness_set()
124 ret = -EBUSY; in sy7802_torch_brightness_set()
129 fled_torch_used_tmp = chip->fled_torch_used | BIT(led->led_id); in sy7802_torch_brightness_set()
131 fled_torch_used_tmp = chip->fled_torch_used & ~BIT(led->led_id); in sy7802_torch_brightness_set()
133 led_enable_mask = led->led_id == SY7802_LED_JOINT ? in sy7802_torch_brightness_set()
135 SY7802_LEDS_MASK(led->led_id); in sy7802_torch_brightness_set()
142 ret = regmap_update_bits(chip->regmap, SY7802_REG_ENABLE, SY7802_MODE_MASK, in sy7802_torch_brightness_set()
147 torch_mask = led->led_id == SY7802_LED_JOINT ? in sy7802_torch_brightness_set()
149 SY7802_TORCH_CURRENT_MASK(led->led_id); in sy7802_torch_brightness_set()
151 /* Register expects brightness between 0 and MAX_BRIGHTNESS - 1 */ in sy7802_torch_brightness_set()
153 brightness -= 1; in sy7802_torch_brightness_set()
157 ret = regmap_update_bits(chip->regmap, SY7802_REG_TORCH_BRIGHTNESS, torch_mask, brightness); in sy7802_torch_brightness_set()
162 ret = regmap_update_bits(chip->regmap, SY7802_REG_ENABLE, enable_mask, val); in sy7802_torch_brightness_set()
166 chip->fled_torch_used = fled_torch_used_tmp; in sy7802_torch_brightness_set()
169 mutex_unlock(&chip->mutex); in sy7802_torch_brightness_set()
175 struct sy7802_led *led = container_of(fl_cdev, struct sy7802_led, flash); in sy7802_flash_brightness_set() local
176 struct led_flash_setting *s = &fl_cdev->brightness; in sy7802_flash_brightness_set() local
177 u32 val = (brightness - s->min) / s->step; in sy7802_flash_brightness_set()
178 struct sy7802 *chip = led->chip; in sy7802_flash_brightness_set()
183 flash_mask = led->led_id == SY7802_LED_JOINT ? in sy7802_flash_brightness_set()
185 SY7802_FLASH_CURRENT_MASK(led->led_id); in sy7802_flash_brightness_set()
187 mutex_lock(&chip->mutex); in sy7802_flash_brightness_set()
188 ret = regmap_update_bits(chip->regmap, SY7802_REG_FLASH_BRIGHTNESS, flash_mask, val); in sy7802_flash_brightness_set()
189 mutex_unlock(&chip->mutex); in sy7802_flash_brightness_set()
196 struct sy7802_led *led = container_of(fl_cdev, struct sy7802_led, flash); in sy7802_strobe_set() local
197 struct sy7802 *chip = led->chip; in sy7802_strobe_set()
204 mutex_lock(&chip->mutex); in sy7802_strobe_set()
206 if (chip->fled_torch_used) { in sy7802_strobe_set()
207 dev_warn(chip->dev, "Cannot set strobe brightness whilst torch is enabled\n"); in sy7802_strobe_set()
208 ret = -EBUSY; in sy7802_strobe_set()
213 fled_strobe_used_tmp = chip->fled_strobe_used | BIT(led->led_id); in sy7802_strobe_set()
215 fled_strobe_used_tmp = chip->fled_strobe_used & ~BIT(led->led_id); in sy7802_strobe_set()
217 led_enable_mask = led->led_id == SY7802_LED_JOINT ? in sy7802_strobe_set()
219 SY7802_LEDS_MASK(led->led_id); in sy7802_strobe_set()
226 ret = regmap_update_bits(chip->regmap, SY7802_REG_ENABLE, enable_mask, val); in sy7802_strobe_set()
231 chip->fled_strobe_used = fled_strobe_used_tmp; in sy7802_strobe_set()
234 mutex_unlock(&chip->mutex); in sy7802_strobe_set()
240 struct sy7802_led *led = container_of(fl_cdev, struct sy7802_led, flash); in sy7802_strobe_get() local
241 struct sy7802 *chip = led->chip; in sy7802_strobe_get()
243 mutex_lock(&chip->mutex); in sy7802_strobe_get()
244 *state = !!(chip->fled_strobe_used & BIT(led->led_id)); in sy7802_strobe_get()
245 mutex_unlock(&chip->mutex); in sy7802_strobe_get()
252 struct sy7802_led *led = container_of(fl_cdev, struct sy7802_led, flash); in sy7802_timeout_set() local
253 struct led_flash_setting *s = &fl_cdev->timeout; in sy7802_timeout_set() local
254 u32 val = (timeout - s->min) / s->step; in sy7802_timeout_set()
255 struct sy7802 *chip = led->chip; in sy7802_timeout_set()
257 return regmap_write(chip->regmap, SY7802_REG_FLASH_DURATION, val); in sy7802_timeout_set()
262 struct sy7802_led *led = container_of(fl_cdev, struct sy7802_led, flash); in sy7802_fault_get() local
263 struct sy7802 *chip = led->chip; in sy7802_fault_get()
268 ret = regmap_read(chip->regmap, SY7802_REG_FLAGS, &val); in sy7802_fault_get()
295 struct led_flash_setting *s; in sy7802_init_flash_brightness() local
298 s = &fl_cdev->brightness; in sy7802_init_flash_brightness()
299 s->min = SY7802_FLASH_BRIGHTNESS_MIN; in sy7802_init_flash_brightness()
300 s->max = SY7802_FLASH_BRIGHTNESS_MAX; in sy7802_init_flash_brightness()
301 s->step = SY7802_FLASH_BRIGHTNESS_STEP; in sy7802_init_flash_brightness()
302 s->val = SY7802_FLASH_BRIGHTNESS_DEFAULT; in sy7802_init_flash_brightness()
307 struct led_flash_setting *s; in sy7802_init_flash_timeout() local
310 s = &fl_cdev->timeout; in sy7802_init_flash_timeout()
311 s->min = SY7802_TIMEOUT_MIN_US; in sy7802_init_flash_timeout()
312 s->max = SY7802_TIMEOUT_MAX_US; in sy7802_init_flash_timeout()
313 s->step = SY7802_TIMEOUT_STEPSIZE_US; in sy7802_init_flash_timeout()
314 s->val = SY7802_TIMEOUT_DEFAULT_US; in sy7802_init_flash_timeout()
317 static int sy7802_led_register(struct device *dev, struct sy7802_led *led, in sy7802_led_register() argument
325 ret = devm_led_classdev_flash_register_ext(dev, &led->flash, &init_data); in sy7802_led_register()
327 dev_err(dev, "Couldn't register flash %d\n", led->led_id); in sy7802_led_register()
334 static int sy7802_init_flash_properties(struct device *dev, struct sy7802_led *led, in sy7802_init_flash_properties() argument
337 struct led_classdev_flash *flash = &led->flash; in sy7802_init_flash_properties()
338 struct led_classdev *lcdev = &flash->led_cdev; in sy7802_init_flash_properties()
342 num = of_property_count_u32_elems(np, "led-sources"); in sy7802_init_flash_properties()
344 dev_err(dev, "Not specified or wrong number of led-sources\n"); in sy7802_init_flash_properties()
345 return -EINVAL; in sy7802_init_flash_properties()
348 ret = of_property_read_u32_array(np, "led-sources", sources, num); in sy7802_init_flash_properties()
354 return -EINVAL; in sy7802_init_flash_properties()
355 if (led->chip->leds_active & BIT(sources[i])) in sy7802_init_flash_properties()
356 return -EINVAL; in sy7802_init_flash_properties()
357 led->chip->leds_active |= BIT(sources[i]); in sy7802_init_flash_properties()
360 /* If both channels are specified in 'led-sources', joint flash output mode is used */ in sy7802_init_flash_properties()
361 led->led_id = num == 2 ? SY7802_LED_JOINT : sources[0]; in sy7802_init_flash_properties()
363 lcdev->max_brightness = SY7802_TORCH_BRIGHTNESS_MAX; in sy7802_init_flash_properties()
364 lcdev->brightness_set_blocking = sy7802_torch_brightness_set; in sy7802_init_flash_properties()
365 lcdev->flags |= LED_DEV_CAP_FLASH; in sy7802_init_flash_properties()
367 flash->ops = &sy7802_flash_ops; in sy7802_init_flash_properties()
377 struct device *dev = chip->dev; in sy7802_chip_check()
381 ret = regmap_read(chip->regmap, SY7802_REG_DEV_ID, &chipid); in sy7802_chip_check()
386 return dev_err_probe(dev, -ENODEV, "Unsupported chip detected: %x\n", chipid); in sy7802_chip_check()
393 gpiod_set_value_cansleep(chip->enable_gpio, 1); in sy7802_enable()
399 gpiod_set_value_cansleep(chip->enable_gpio, 0); in sy7802_disable()
404 struct device_node *np = dev_of_node(chip->dev); in sy7802_probe_dt()
408 regmap_write(chip->regmap, SY7802_REG_ENABLE, SY7802_MODE_OFF); in sy7802_probe_dt()
409 regmap_write(chip->regmap, SY7802_REG_TORCH_BRIGHTNESS, LED_OFF); in sy7802_probe_dt()
413 struct sy7802_led *led = chip->leds + child_num; in sy7802_probe_dt() local
415 led->chip = chip; in sy7802_probe_dt()
416 led->led_id = child_num; in sy7802_probe_dt()
418 ret = sy7802_init_flash_properties(chip->dev, led, child); in sy7802_probe_dt()
422 ret = sy7802_led_register(chip->dev, led, child); in sy7802_probe_dt()
442 regulator_disable(chip->vin_regulator); in sy7802_regulator_disable_action()
456 struct device *dev = &client->dev; in sy7802_probe()
463 return dev_err_probe(dev, -EINVAL, "Invalid amount of LED nodes %zu\n", count); in sy7802_probe()
467 return -ENOMEM; in sy7802_probe()
469 chip->num_leds = count; in sy7802_probe()
471 chip->dev = dev; in sy7802_probe()
474 chip->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); in sy7802_probe()
475 ret = PTR_ERR_OR_ZERO(chip->enable_gpio); in sy7802_probe()
479 chip->vin_regulator = devm_regulator_get(dev, "vin"); in sy7802_probe()
480 ret = PTR_ERR_OR_ZERO(chip->vin_regulator); in sy7802_probe()
484 ret = regulator_enable(chip->vin_regulator); in sy7802_probe()
492 ret = devm_mutex_init(dev, &chip->mutex); in sy7802_probe()
496 mutex_lock(&chip->mutex); in sy7802_probe()
498 chip->regmap = devm_regmap_init_i2c(client, &sy7802_regmap_config); in sy7802_probe()
499 if (IS_ERR(chip->regmap)) { in sy7802_probe()
500 ret = PTR_ERR(chip->regmap); in sy7802_probe()
518 mutex_unlock(&chip->mutex); in sy7802_probe()
538 MODULE_DESCRIPTION("Silergy SY7802 flash LED driver");