^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * v0.1.1 Fix the bug that when pwm is disabled, the light cannot be turned off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <media/v4l2-ctrls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <media/v4l2-subdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/led-class-flash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pinctrl/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/rk-camera-module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/rk-led-flash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define FLASH_TIMEOUT_MIN 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define FLASH_TIMEOUT_STEP 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct rgb13h_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct led_classdev_flash fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct v4l2_ctrl_handler ctrls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct v4l2_subdev sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* secures access to the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct gpio_desc *gpio_en;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* maximum LED current in torch mode*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u32 max_mm_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* maximum LED current in flash mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u32 max_flash_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* maximum flash timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u32 max_flash_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u32 intensity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u32 intensity_torch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) bool strobe_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* brightness cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u32 torch_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* assures led-triggers compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct work_struct work_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct __kernel_old_timeval timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u32 timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) bool waiting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) wait_queue_head_t done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct work_struct work_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) enum v4l2_flash_led_mode led_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) u32 module_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const char *module_facing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct pwm_device *pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct pwm_state pwm_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct rgb13h_led *fled_cdev_to_led(struct led_classdev_flash *fled_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return container_of(fled_cdev, struct rgb13h_led, fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static struct rgb13h_led *sd_to_led(struct v4l2_subdev *subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return container_of(subdev, struct rgb13h_led, sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int rgb13h_set_output(struct rgb13h_led *led, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) mutex_lock(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!IS_ERR(led->gpio_en))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) gpiod_direction_output(led->gpio_en, on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!IS_ERR(led->pwm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (led->led_mode == V4L2_FLASH_LED_MODE_TORCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) led->pwm_state.duty_cycle =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) div_u64(led->intensity_torch * led->pwm_state.period, led->max_mm_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) led->pwm_state.duty_cycle =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) div_u64(led->intensity * led->pwm_state.period, led->max_flash_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) led->pwm_state.enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pwm_apply_state(led->pwm, &led->pwm_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) dev_dbg(&led->pdev->dev, "led pwm duty=%llu, period=%llu, polarity=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) led->pwm_state.duty_cycle, led->pwm_state.period, led->pwm_state.polarity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) led->pwm_state.enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pwm_apply_state(led->pwm, &led->pwm_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (!on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) led->strobe_state = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (led->waiting) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) led->waiting = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) wake_up(&led->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) led->timestamp = ns_to_kernel_old_timeval(ktime_get_ns());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) mutex_unlock(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void rgb13h_timeout_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct rgb13h_led *led =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) container_of(work, struct rgb13h_led, work_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) wait_event_timeout(led->done, !led->waiting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) usecs_to_jiffies(led->timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (led->waiting) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) led->waiting = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) led->strobe_state = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) rgb13h_set_output(led, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static void rgb13h_brightness_set_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct rgb13h_led *led =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) container_of(work, struct rgb13h_led, work_brightness_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) rgb13h_set_output(led, !!led->torch_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static void rgb13h_led_brightness_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct rgb13h_led *led = fled_cdev_to_led(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) led->torch_brightness = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) schedule_work(&led->work_brightness_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int rgb13h_led_brightness_set_sync(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct rgb13h_led *led = fled_cdev_to_led(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) rgb13h_set_output(led, !!brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int rgb13h_led_flash_strobe_set(struct led_classdev_flash *fled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) bool state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct rgb13h_led *led = fled_cdev_to_led(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) mutex_lock(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) led->strobe_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) led->waiting = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) schedule_work(&led->work_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) mutex_unlock(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return rgb13h_set_output(led, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int rgb13h_led_flash_strobe_get(struct led_classdev_flash *fled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) bool *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct rgb13h_led *led = fled_cdev_to_led(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) mutex_lock(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *state = led->strobe_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) mutex_unlock(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int rgb13h_led_flash_timeout_set(struct led_classdev_flash *fled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u32 timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct rgb13h_led *led = fled_cdev_to_led(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mutex_lock(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) led->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) mutex_unlock(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int rgb13h_led_parse_dt(struct rgb13h_led *led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct device_node **sub_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct led_classdev *led_cdev = &led->fled_cdev.led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct device *dev = &led->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct device_node *child_node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ret = of_property_read_u32(child_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) RKMODULE_CAMERA_MODULE_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) &led->module_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) ret |= of_property_read_string(child_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) RKMODULE_CAMERA_MODULE_FACING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) &led->module_facing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dev_err(dev, "could not get module information!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) led->gpio_en = devm_gpiod_get(dev, "enable", GPIOD_ASIS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (IS_ERR(led->gpio_en)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ret = PTR_ERR(led->gpio_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) dev_info(dev, "Unable to claim enable-gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) led->pwm = devm_pwm_get(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (IS_ERR(led->pwm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ret = PTR_ERR(led->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) dev_info(dev, "Unable to get pwm device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) led->pwm_state.period = led->pwm->args.period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) led->pwm_state.polarity = led->pwm->args.polarity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) dev_dbg(dev, "period %llu, polarity %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) led->pwm_state.period, led->pwm_state.polarity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (IS_ERR(led->gpio_en) && IS_ERR(led->pwm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) dev_err(dev, "Neither enable-gpio nor pwm can be get,return error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) led_cdev->name = of_get_property(child_node, "label", NULL) ? :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) child_node->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ret = of_property_read_u32(child_node, "led-max-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) &led->max_mm_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) "led-max-microamp DT property missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (led->max_mm_current <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) led->max_mm_current = 20000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) "get led-max-microamp error value, used default value 20000\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) ret = of_property_read_u32(child_node, "flash-max-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) &led->max_flash_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) "flash-max-microamp DT property missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (led->max_flash_current <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) led->max_flash_current = 20000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) "get flash-max-microamp error value, used default value 20000\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) ret = of_property_read_u32(child_node, "flash-max-timeout-us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) &led->max_flash_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) "flash-max-timeout-us DT property missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (led->max_flash_tm <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) led->max_flash_tm = 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) "get flash-max-timeout-us error value, used default value 1s\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *sub_node = child_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static int rgb13h_led_get_configuration(struct rgb13h_led *led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct device_node **sub_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ret = rgb13h_led_parse_dt(led, sub_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static void rgb13h_init_flash_timeout(struct rgb13h_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct led_classdev_flash *fled_cdev = &led->fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct led_flash_setting *setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /* Init flash timeout setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) setting = &fled_cdev->timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) setting->min = FLASH_TIMEOUT_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) setting->max = led->max_flash_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) setting->step = FLASH_TIMEOUT_STEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) setting->val = setting->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int rgb13h_get_ctrl(struct v4l2_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct rgb13h_led *led =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) container_of(ctrl->handler, struct rgb13h_led, ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) switch (ctrl->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) case V4L2_CID_FLASH_FAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) ctrl->val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) case V4L2_CID_FLASH_STROBE_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (led->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ctrl->val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) ctrl->val = led->strobe_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) case V4L2_CID_FLASH_INTENSITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ctrl->val = led->intensity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) case V4L2_CID_FLASH_TORCH_INTENSITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) ctrl->val = led->intensity_torch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) case V4L2_CID_FLASH_LED_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ctrl->val = led->led_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) dev_err(&led->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) "ctrl 0x%x not supported\n", ctrl->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static int rgb13h_set_ctrl(struct v4l2_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct rgb13h_led *led =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) container_of(ctrl->handler, struct rgb13h_led, ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) switch (ctrl->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) case V4L2_CID_FLASH_LED_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) led->led_mode = ctrl->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) rgb13h_set_output(led, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (led->led_mode == V4L2_FLASH_LED_MODE_TORCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return rgb13h_set_output(led, LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) case V4L2_CID_FLASH_STROBE_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (ctrl->val == V4L2_FLASH_STROBE_SOURCE_EXTERNAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) case V4L2_CID_FLASH_STROBE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (led->led_mode != V4L2_FLASH_LED_MODE_FLASH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return rgb13h_led_flash_strobe_set(&led->fled_cdev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) case V4L2_CID_FLASH_STROBE_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (led->led_mode != V4L2_FLASH_LED_MODE_FLASH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return rgb13h_led_flash_strobe_set(&led->fled_cdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) case V4L2_CID_FLASH_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return rgb13h_led_flash_timeout_set(&led->fled_cdev, ctrl->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) case V4L2_CID_FLASH_INTENSITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) led->intensity = ctrl->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) case V4L2_CID_FLASH_TORCH_INTENSITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) led->intensity_torch = ctrl->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (led->led_mode != V4L2_FLASH_LED_MODE_TORCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return rgb13h_set_output(led, LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) dev_err(&led->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) "ctrl 0x%x not supported\n", ctrl->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static const struct v4l2_ctrl_ops rgb13h_ctrl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) .g_volatile_ctrl = rgb13h_get_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) .s_ctrl = rgb13h_set_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static int rgb13h_init_controls(struct rgb13h_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct v4l2_ctrl *ctrl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) v4l2_ctrl_handler_init(&led->ctrls, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /* V4L2_CID_FLASH_LED_MODE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) v4l2_ctrl_new_std_menu(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) V4L2_CID_FLASH_LED_MODE, 2, ~7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) V4L2_FLASH_LED_MODE_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) led->led_mode = V4L2_FLASH_LED_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* V4L2_CID_FLASH_STROBE_SOURCE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) v4l2_ctrl_new_std_menu(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) V4L2_CID_FLASH_STROBE_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 0, ~1, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /* V4L2_CID_FLASH_STROBE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) v4l2_ctrl_new_std(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /* V4L2_CID_FLASH_STROBE_STOP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) v4l2_ctrl_new_std(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* V4L2_CID_FLASH_STROBE_STATUS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ctrl = v4l2_ctrl_new_std(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) V4L2_CID_FLASH_STROBE_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 0, 1, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) led->strobe_state = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* V4L2_CID_FLASH_TIMEOUT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) v4l2_ctrl_new_std(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) V4L2_CID_FLASH_TIMEOUT, FLASH_TIMEOUT_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) led->max_flash_tm, FLASH_TIMEOUT_STEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) led->max_flash_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) led->timeout = led->max_flash_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* V4L2_CID_FLASH_INTENSITY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ctrl = v4l2_ctrl_new_std(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) V4L2_CID_FLASH_INTENSITY, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) led->max_flash_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) led->max_flash_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (ctrl && IS_ERR(led->pwm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) led->intensity = led->max_flash_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* V4L2_CID_FLASH_TORCH_INTENSITY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) ctrl = v4l2_ctrl_new_std(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) V4L2_CID_FLASH_TORCH_INTENSITY, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) led->max_mm_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) led->max_mm_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (ctrl && IS_ERR(led->pwm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) led->intensity_torch = led->max_mm_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* V4L2_CID_FLASH_FAULT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) ctrl = v4l2_ctrl_new_std(&led->ctrls, &rgb13h_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) V4L2_CID_FLASH_FAULT, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) V4L2_FLASH_FAULT_OVER_VOLTAGE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) V4L2_FLASH_FAULT_TIMEOUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) V4L2_FLASH_FAULT_OVER_TEMPERATURE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) led->sd.ctrl_handler = &led->ctrls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return led->ctrls.error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) static long rgb13h_ioctl(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) unsigned int cmd, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct rgb13h_led *led = sd_to_led(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct __kernel_old_timeval *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (cmd == RK_VIDIOC_FLASH_TIMEINFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) t = (struct __kernel_old_timeval *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) t->tv_sec = led->timestamp.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) t->tv_usec = led->timestamp.tv_usec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) #define RK_VIDIOC_COMPAT_FLASH_TIMEINFO \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) _IOR('V', BASE_VIDIOC_PRIVATE + 0, struct old_timeval32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static long rgb13h_compat_ioctl32(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct __kernel_old_timeval t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct old_timeval32 compat_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct old_timeval32 __user *p32 = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (cmd == RK_VIDIOC_COMPAT_FLASH_TIMEINFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) rgb13h_ioctl(sd, RK_VIDIOC_FLASH_TIMEINFO, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) compat_t.tv_sec = t.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) compat_t.tv_usec = t.tv_usec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) put_user(compat_t.tv_sec, &p32->tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) put_user(compat_t.tv_usec, &p32->tv_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) static const struct v4l2_subdev_core_ops v4l2_flash_core_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) .ioctl = rgb13h_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) .compat_ioctl32 = rgb13h_compat_ioctl32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static const struct v4l2_subdev_ops v4l2_flash_subdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) .core = &v4l2_flash_core_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static const struct led_flash_ops flash_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) .strobe_set = rgb13h_led_flash_strobe_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) .strobe_get = rgb13h_led_flash_strobe_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) .timeout_set = rgb13h_led_flash_timeout_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static int rgb13h_led_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct device_node *sub_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct rgb13h_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct led_classdev *led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) struct led_classdev_flash *fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) struct v4l2_subdev *sd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) char facing[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) dev_info(dev, "driver version: %02x.%02x.%02x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) DRIVER_VERSION >> 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) (DRIVER_VERSION & 0xff00) >> 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) DRIVER_VERSION & 0x00ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (!led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) led->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) platform_set_drvdata(pdev, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) fled_cdev = &led->fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) fled_cdev->ops = &flash_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) led_cdev = &fled_cdev->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) ret = rgb13h_led_get_configuration(led, &sub_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) mutex_init(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /* Initialize LED Flash class device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) led_cdev->brightness_set = rgb13h_led_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) led_cdev->brightness_set_blocking = rgb13h_led_brightness_set_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) led_cdev->max_brightness = LED_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) led_cdev->flags |= LED_DEV_CAP_FLASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) INIT_WORK(&led->work_brightness_set, rgb13h_brightness_set_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /* Init strobe timeout handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) led->waiting = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) init_waitqueue_head(&led->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) INIT_WORK(&led->work_timeout, rgb13h_timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) rgb13h_init_flash_timeout(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /* Register LED Flash class device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) ret = led_classdev_flash_register(&pdev->dev, fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) goto err_flash_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) sd = &led->sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) sd->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) v4l2_subdev_init(sd, &v4l2_flash_subdev_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) memset(facing, 0, sizeof(facing));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (strcmp(led->module_facing, "back") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) facing[0] = 'b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) facing[0] = 'f';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) led->module_index, facing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) led_cdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) ret = media_entity_pads_init(&sd->entity, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) goto error_v4l2_flash_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) sd->entity.function = MEDIA_ENT_F_FLASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) ret = rgb13h_init_controls(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) goto err_init_controls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) ret = v4l2_async_register_subdev(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) goto err_async_register_sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) err_async_register_sd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) v4l2_ctrl_handler_free(sd->ctrl_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) err_init_controls:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) media_entity_cleanup(&sd->entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) error_v4l2_flash_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) led_classdev_flash_unregister(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) err_flash_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) mutex_destroy(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static int rgb13h_led_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct rgb13h_led *led = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) v4l2_async_unregister_subdev(&led->sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) v4l2_ctrl_handler_free(led->sd.ctrl_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) media_entity_cleanup(&led->sd.entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) led_classdev_flash_unregister(&led->fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) mutex_destroy(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static const struct of_device_id rgb13h_led_dt_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) { .compatible = "led,rgb13h" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) MODULE_DEVICE_TABLE(of, rgb13h_led_dt_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) static struct platform_driver rgb13h_led_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) .probe = rgb13h_led_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) .remove = rgb13h_led_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) .name = "rgb13h-flash",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) .of_match_table = rgb13h_led_dt_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) module_platform_driver(rgb13h_led_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) MODULE_DESCRIPTION("GPIO LEDS Flash driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) MODULE_LICENSE("GPL v2");