Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * leds-lm3533.c -- LM3533 LED driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2011-2012 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Johan Hovold <jhovold@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.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/mfd/core.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/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mfd/lm3533.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define LM3533_LVCTRLBANK_MIN		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define LM3533_LVCTRLBANK_MAX		5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define LM3533_LVCTRLBANK_COUNT		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define LM3533_RISEFALLTIME_MAX		7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define LM3533_ALS_CHANNEL_LV_MIN	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define LM3533_ALS_CHANNEL_LV_MAX	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define LM3533_REG_CTRLBANK_BCONF_BASE		0x1b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define LM3533_REG_PATTERN_ENABLE		0x28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define LM3533_REG_PATTERN_LOW_TIME_BASE	0x71
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define LM3533_REG_PATTERN_HIGH_TIME_BASE	0x72
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define LM3533_REG_PATTERN_RISETIME_BASE	0x74
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define LM3533_REG_PATTERN_FALLTIME_BASE	0x75
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define LM3533_REG_PATTERN_STEP			0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define LM3533_LED_FLAG_PATTERN_ENABLE		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) struct lm3533_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct lm3533 *lm3533;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct lm3533_ctrlbank cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct led_classdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static inline struct lm3533_led *to_lm3533_led(struct led_classdev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return container_of(cdev, struct lm3533_led, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static inline int lm3533_led_get_ctrlbank_id(struct lm3533_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return led->id + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static inline u8 lm3533_led_get_lv_reg(struct lm3533_led *led, u8 base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return base + led->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static inline u8 lm3533_led_get_pattern(struct lm3533_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return led->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static inline u8 lm3533_led_get_pattern_reg(struct lm3533_led *led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 								u8 base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return base + lm3533_led_get_pattern(led) * LM3533_REG_PATTERN_STEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static int lm3533_led_pattern_enable(struct lm3533_led *led, int enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u8 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	int pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	dev_dbg(led->cdev.dev, "%s - %d\n", __func__, enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	mutex_lock(&led->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	state = test_bit(LM3533_LED_FLAG_PATTERN_ENABLE, &led->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if ((enable && state) || (!enable && !state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	pattern = lm3533_led_get_pattern(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	mask = 1 << (2 * pattern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		val = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	ret = lm3533_update(led->lm3533, LM3533_REG_PATTERN_ENABLE, val, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		dev_err(led->cdev.dev, "failed to enable pattern %d (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 							pattern, enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	__change_bit(LM3533_LED_FLAG_PATTERN_ENABLE, &led->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	mutex_unlock(&led->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int lm3533_led_set(struct led_classdev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 						enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct lm3533_led *led = to_lm3533_led(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	dev_dbg(led->cdev.dev, "%s - %d\n", __func__, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (value == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		lm3533_led_pattern_enable(led, 0);	/* disable blink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return lm3533_ctrlbank_set_brightness(&led->cb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static enum led_brightness lm3533_led_get(struct led_classdev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct lm3533_led *led = to_lm3533_led(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	ret = lm3533_ctrlbank_get_brightness(&led->cb, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	dev_dbg(led->cdev.dev, "%s - %u\n", __func__, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* Pattern generator defines (delays in us). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define LM3533_LED_DELAY1_VMIN	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #define LM3533_LED_DELAY2_VMIN	0x3d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define LM3533_LED_DELAY3_VMIN	0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define LM3533_LED_DELAY1_VMAX	(LM3533_LED_DELAY2_VMIN - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #define LM3533_LED_DELAY2_VMAX	(LM3533_LED_DELAY3_VMIN - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define LM3533_LED_DELAY3_VMAX	0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define LM3533_LED_DELAY1_TMIN	16384U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #define LM3533_LED_DELAY2_TMIN	1130496U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #define LM3533_LED_DELAY3_TMIN	10305536U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #define LM3533_LED_DELAY1_TMAX	999424U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #define LM3533_LED_DELAY2_TMAX	9781248U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #define LM3533_LED_DELAY3_TMAX	76890112U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* t_step = (t_max - t_min) / (v_max - v_min) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define LM3533_LED_DELAY1_TSTEP	16384
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #define LM3533_LED_DELAY2_TSTEP	131072
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #define LM3533_LED_DELAY3_TSTEP	524288
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* Delay limits for hardware accelerated blinking (in ms). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define LM3533_LED_DELAY_ON_MAX \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	((LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY2_TSTEP / 2) / 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #define LM3533_LED_DELAY_OFF_MAX \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	((LM3533_LED_DELAY3_TMAX + LM3533_LED_DELAY3_TSTEP / 2) / 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  * Returns linear map of *t from [t_min,t_max] to [v_min,v_max] with a step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * size of t_step, where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  *	t_step = (t_max - t_min) / (v_max - v_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * and updates *t to reflect the mapped value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static u8 time_to_val(unsigned *t, unsigned t_min, unsigned t_step,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 							u8 v_min, u8 v_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	unsigned val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	val = (*t + t_step / 2 - t_min) / t_step + v_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	*t = t_step * (val - v_min) + t_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return (u8)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * Returns time code corresponding to *delay (in ms) and updates *delay to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * reflect actual hardware delay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * Hardware supports 256 discrete delay times, divided into three groups with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  * the following ranges and step-sizes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  *	[   16,   999]	[0x00, 0x3e]	step  16 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  *	[ 1130,  9781]	[0x3d, 0x7f]	step 131 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  *	[10306, 76890]	[0x80, 0xff]	step 524 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * Note that delay group 3 is only available for delay_off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static u8 lm3533_led_get_hw_delay(unsigned *delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	unsigned t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	t = *delay * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (t >= (LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY3_TMIN) / 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		t = clamp(t, LM3533_LED_DELAY3_TMIN, LM3533_LED_DELAY3_TMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		val = time_to_val(&t,	LM3533_LED_DELAY3_TMIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 					LM3533_LED_DELAY3_TSTEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 					LM3533_LED_DELAY3_VMIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 					LM3533_LED_DELAY3_VMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	} else if (t >= (LM3533_LED_DELAY1_TMAX + LM3533_LED_DELAY2_TMIN) / 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		t = clamp(t, LM3533_LED_DELAY2_TMIN, LM3533_LED_DELAY2_TMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		val = time_to_val(&t,	LM3533_LED_DELAY2_TMIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 					LM3533_LED_DELAY2_TSTEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 					LM3533_LED_DELAY2_VMIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 					LM3533_LED_DELAY2_VMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		t = clamp(t, LM3533_LED_DELAY1_TMIN, LM3533_LED_DELAY1_TMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		val = time_to_val(&t,	LM3533_LED_DELAY1_TMIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 					LM3533_LED_DELAY1_TSTEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 					LM3533_LED_DELAY1_VMIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 					LM3533_LED_DELAY1_VMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	*delay = (t + 500) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * Set delay register base to *delay (in ms) and update *delay to reflect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * actual hardware delay used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static u8 lm3533_led_delay_set(struct lm3533_led *led, u8 base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 							unsigned long *delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	unsigned t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	t = (unsigned)*delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	/* Delay group 3 is only available for low time (delay off). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (base != LM3533_REG_PATTERN_LOW_TIME_BASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		t = min(t, LM3533_LED_DELAY2_TMAX / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	val = lm3533_led_get_hw_delay(&t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	dev_dbg(led->cdev.dev, "%s - %lu: %u (0x%02x)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 							*delay, t, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	reg = lm3533_led_get_pattern_reg(led, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	ret = lm3533_write(led->lm3533, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		dev_err(led->cdev.dev, "failed to set delay (%02x)\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	*delay = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int lm3533_led_delay_on_set(struct lm3533_led *led, unsigned long *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	return lm3533_led_delay_set(led, LM3533_REG_PATTERN_HIGH_TIME_BASE, t);
^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 lm3533_led_delay_off_set(struct lm3533_led *led, unsigned long *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return lm3533_led_delay_set(led, LM3533_REG_PATTERN_LOW_TIME_BASE, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int lm3533_led_blink_set(struct led_classdev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				unsigned long *delay_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				unsigned long *delay_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct lm3533_led *led = to_lm3533_led(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	dev_dbg(led->cdev.dev, "%s - on = %lu, off = %lu\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 							*delay_on, *delay_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (*delay_on > LM3533_LED_DELAY_ON_MAX ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 					*delay_off > LM3533_LED_DELAY_OFF_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (*delay_on == 0 && *delay_off == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		*delay_on = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		*delay_off = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	ret = lm3533_led_delay_on_set(led, delay_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	ret = lm3533_led_delay_off_set(led, delay_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return lm3533_led_pattern_enable(led, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static ssize_t show_id(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return scnprintf(buf, PAGE_SIZE, "%d\n", led->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  * Pattern generator rise/fall times:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  *   0 - 2048 us (default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  *   1 - 262 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  *   2 - 524 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  *   3 - 1.049 s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  *   4 - 2.097 s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  *   5 - 4.194 s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  *   6 - 8.389 s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)  *   7 - 16.78 s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static ssize_t show_risefalltime(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 					char *buf, u8 base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	reg = lm3533_led_get_pattern_reg(led, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	ret = lm3533_read(led->lm3533, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	return scnprintf(buf, PAGE_SIZE, "%x\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static ssize_t show_risetime(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return show_risefalltime(dev, attr, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 					LM3533_REG_PATTERN_RISETIME_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static ssize_t show_falltime(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	return show_risefalltime(dev, attr, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 					LM3533_REG_PATTERN_FALLTIME_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static ssize_t store_risefalltime(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 					const char *buf, size_t len, u8 base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (kstrtou8(buf, 0, &val) || val > LM3533_RISEFALLTIME_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	reg = lm3533_led_get_pattern_reg(led, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	ret = lm3533_write(led->lm3533, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static ssize_t store_risetime(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 					const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	return store_risefalltime(dev, attr, buf, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 					LM3533_REG_PATTERN_RISETIME_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static ssize_t store_falltime(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 					const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	return store_risefalltime(dev, attr, buf, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 					LM3533_REG_PATTERN_FALLTIME_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static ssize_t show_als_channel(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	unsigned channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	ret = lm3533_read(led->lm3533, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	channel = (val & LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	return scnprintf(buf, PAGE_SIZE, "%u\n", channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static ssize_t store_als_channel(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 					const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	unsigned channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	u8 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (kstrtouint(buf, 0, &channel))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (channel < LM3533_ALS_CHANNEL_LV_MIN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 					channel > LM3533_ALS_CHANNEL_LV_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	mask = LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	val = channel - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	ret = lm3533_update(led->lm3533, reg, val, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static ssize_t show_als_en(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	bool enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	ret = lm3533_read(led->lm3533, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	enable = val & LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	return scnprintf(buf, PAGE_SIZE, "%d\n", enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static ssize_t store_als_en(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 					const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	unsigned enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	u8 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (kstrtouint(buf, 0, &enable))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	mask = LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		val = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	ret = lm3533_update(led->lm3533, reg, val, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static ssize_t show_linear(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	int linear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	ret = lm3533_read(led->lm3533, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (val & LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		linear = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		linear = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	return scnprintf(buf, PAGE_SIZE, "%x\n", linear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static ssize_t store_linear(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 					const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	unsigned long linear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	u8 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (kstrtoul(buf, 0, &linear))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	mask = LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	if (linear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		val = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	ret = lm3533_update(led->lm3533, reg, val, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static ssize_t show_pwm(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 					char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	ret = lm3533_ctrlbank_get_pwm(&led->cb, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	return scnprintf(buf, PAGE_SIZE, "%u\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static ssize_t store_pwm(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 					const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (kstrtou8(buf, 0, &val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	ret = lm3533_ctrlbank_set_pwm(&led->cb, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static LM3533_ATTR_RW(als_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) static LM3533_ATTR_RW(als_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) static LM3533_ATTR_RW(falltime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static LM3533_ATTR_RO(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static LM3533_ATTR_RW(linear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) static LM3533_ATTR_RW(pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) static LM3533_ATTR_RW(risetime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static struct attribute *lm3533_led_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	&dev_attr_als_channel.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	&dev_attr_als_en.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	&dev_attr_falltime.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	&dev_attr_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	&dev_attr_linear.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	&dev_attr_pwm.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	&dev_attr_risetime.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) static umode_t lm3533_led_attr_is_visible(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 					     struct attribute *attr, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	struct device *dev = container_of(kobj, struct device, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	struct lm3533_led *led = to_lm3533_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	umode_t mode = attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	if (attr == &dev_attr_als_channel.attr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 					attr == &dev_attr_als_en.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		if (!led->lm3533->have_als)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	return mode;
^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) static const struct attribute_group lm3533_led_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	.is_visible	= lm3533_led_attr_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	.attrs		= lm3533_led_attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) static const struct attribute_group *lm3533_led_attribute_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	&lm3533_led_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) static int lm3533_led_setup(struct lm3533_led *led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 					struct lm3533_led_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	ret = lm3533_ctrlbank_set_max_current(&led->cb, pdata->max_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	return lm3533_ctrlbank_set_pwm(&led->cb, pdata->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) static int lm3533_led_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	struct lm3533 *lm3533;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	struct lm3533_led_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	struct lm3533_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	dev_dbg(&pdev->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	lm3533 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	if (!lm3533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		dev_err(&pdev->dev, "no platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	if (pdev->id < 0 || pdev->id >= LM3533_LVCTRLBANK_COUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		dev_err(&pdev->dev, "illegal LED id %d\n", pdev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	if (!led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	led->lm3533 = lm3533;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	led->cdev.name = pdata->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	led->cdev.default_trigger = pdata->default_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	led->cdev.brightness_set_blocking = lm3533_led_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	led->cdev.brightness_get = lm3533_led_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	led->cdev.blink_set = lm3533_led_blink_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	led->cdev.brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	led->cdev.groups = lm3533_led_attribute_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	led->id = pdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	mutex_init(&led->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	/* The class framework makes a callback to get brightness during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	 * registration so use parent device (for error reporting) until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	 * registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	led->cb.lm3533 = lm3533;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	led->cb.id = lm3533_led_get_ctrlbank_id(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	led->cb.dev = lm3533->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	platform_set_drvdata(pdev, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	ret = led_classdev_register(pdev->dev.parent, &led->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		dev_err(&pdev->dev, "failed to register LED %d\n", pdev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	led->cb.dev = led->cdev.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	ret = lm3533_led_setup(led, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		goto err_deregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	ret = lm3533_ctrlbank_enable(&led->cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		goto err_deregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) err_deregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	led_classdev_unregister(&led->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static int lm3533_led_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	struct lm3533_led *led = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	dev_dbg(&pdev->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	lm3533_ctrlbank_disable(&led->cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	led_classdev_unregister(&led->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) static void lm3533_led_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	struct lm3533_led *led = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	dev_dbg(&pdev->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	lm3533_ctrlbank_disable(&led->cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	lm3533_led_set(&led->cdev, LED_OFF);		/* disable blink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) static struct platform_driver lm3533_led_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		.name = "lm3533-leds",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	.probe		= lm3533_led_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	.remove		= lm3533_led_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	.shutdown	= lm3533_led_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) module_platform_driver(lm3533_led_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) MODULE_DESCRIPTION("LM3533 LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) MODULE_ALIAS("platform:lm3533-leds");