xref: /openbmc/linux/drivers/leds/leds-mt6323.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2216ec6ccSSean Wang /*
3216ec6ccSSean Wang  * LED driver for Mediatek MT6323 PMIC
4216ec6ccSSean Wang  *
5216ec6ccSSean Wang  * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
6216ec6ccSSean Wang  */
7216ec6ccSSean Wang #include <linux/kernel.h>
8216ec6ccSSean Wang #include <linux/leds.h>
9216ec6ccSSean Wang #include <linux/mfd/mt6323/registers.h>
10216ec6ccSSean Wang #include <linux/mfd/mt6397/core.h>
11216ec6ccSSean Wang #include <linux/module.h>
12216ec6ccSSean Wang #include <linux/of.h>
13216ec6ccSSean Wang #include <linux/platform_device.h>
14216ec6ccSSean Wang #include <linux/regmap.h>
15216ec6ccSSean Wang 
16216ec6ccSSean Wang /*
179bef1414SAngeloGioacchino Del Regno  * Register field for TOP_CKPDN0 to enable
18216ec6ccSSean Wang  * 32K clock common for LED device.
19216ec6ccSSean Wang  */
209bef1414SAngeloGioacchino Del Regno #define RG_DRV_32K_CK_PDN		BIT(11)
219bef1414SAngeloGioacchino Del Regno #define RG_DRV_32K_CK_PDN_MASK		BIT(11)
22216ec6ccSSean Wang 
239bb0a9e0SAngeloGioacchino Del Regno /* 32K/1M/6M clock common for WLED device */
249bb0a9e0SAngeloGioacchino Del Regno #define RG_VWLED_1M_CK_PDN		BIT(0)
259bb0a9e0SAngeloGioacchino Del Regno #define RG_VWLED_32K_CK_PDN		BIT(12)
269bb0a9e0SAngeloGioacchino Del Regno #define RG_VWLED_6M_CK_PDN		BIT(13)
279bb0a9e0SAngeloGioacchino Del Regno 
28216ec6ccSSean Wang /*
299bef1414SAngeloGioacchino Del Regno  * Register field for TOP_CKPDN2 to enable
30216ec6ccSSean Wang  * individual clock for LED device.
31216ec6ccSSean Wang  */
329bef1414SAngeloGioacchino Del Regno #define RG_ISINK_CK_PDN(i)	BIT(i)
339bef1414SAngeloGioacchino Del Regno #define RG_ISINK_CK_PDN_MASK(i)	BIT(i)
34216ec6ccSSean Wang 
35216ec6ccSSean Wang /*
369bef1414SAngeloGioacchino Del Regno  * Register field for TOP_CKCON1 to select
37216ec6ccSSean Wang  * clock source.
38216ec6ccSSean Wang  */
399bef1414SAngeloGioacchino Del Regno #define RG_ISINK_CK_SEL_MASK(i)	(BIT(10) << (i))
40216ec6ccSSean Wang 
419bef1414SAngeloGioacchino Del Regno #define ISINK_CON(r, i)		(r + 0x8 * (i))
423ec0b29eSAngeloGioacchino Del Regno 
433ec0b29eSAngeloGioacchino Del Regno /* ISINK_CON0: Register to setup the duty cycle of the blink. */
449bef1414SAngeloGioacchino Del Regno #define ISINK_DIM_DUTY_MASK	(0x1f << 8)
459bef1414SAngeloGioacchino Del Regno #define ISINK_DIM_DUTY(i)	(((i) << 8) & ISINK_DIM_DUTY_MASK)
46216ec6ccSSean Wang 
473ec0b29eSAngeloGioacchino Del Regno /* ISINK_CON1: Register to setup the period of the blink. */
489bef1414SAngeloGioacchino Del Regno #define ISINK_DIM_FSEL_MASK	(0xffff)
499bef1414SAngeloGioacchino Del Regno #define ISINK_DIM_FSEL(i)	((i) & ISINK_DIM_FSEL_MASK)
50216ec6ccSSean Wang 
513ec0b29eSAngeloGioacchino Del Regno /* ISINK_CON2: Register to control the brightness. */
529bef1414SAngeloGioacchino Del Regno #define ISINK_CH_STEP_SHIFT	12
539bef1414SAngeloGioacchino Del Regno #define ISINK_CH_STEP_MASK	(0x7 << 12)
549bef1414SAngeloGioacchino Del Regno #define ISINK_CH_STEP(i)	(((i) << 12) & ISINK_CH_STEP_MASK)
559bef1414SAngeloGioacchino Del Regno #define ISINK_SFSTR0_TC_MASK	(0x3 << 1)
569bef1414SAngeloGioacchino Del Regno #define ISINK_SFSTR0_TC(i)	(((i) << 1) & ISINK_SFSTR0_TC_MASK)
579bef1414SAngeloGioacchino Del Regno #define ISINK_SFSTR0_EN_MASK	BIT(0)
589bef1414SAngeloGioacchino Del Regno #define ISINK_SFSTR0_EN		BIT(0)
59216ec6ccSSean Wang 
60216ec6ccSSean Wang /* Register to LED channel enablement. */
619bef1414SAngeloGioacchino Del Regno #define ISINK_CH_EN_MASK(i)	BIT(i)
629bef1414SAngeloGioacchino Del Regno #define ISINK_CH_EN(i)		BIT(i)
63216ec6ccSSean Wang 
643ec0b29eSAngeloGioacchino Del Regno #define MAX_SUPPORTED_LEDS		8
65216ec6ccSSean Wang 
66216ec6ccSSean Wang struct mt6323_leds;
67216ec6ccSSean Wang 
68216ec6ccSSean Wang /**
69216ec6ccSSean Wang  * struct mt6323_led - state container for the LED device
70216ec6ccSSean Wang  * @id:			the identifier in MT6323 LED device
71216ec6ccSSean Wang  * @parent:		the pointer to MT6323 LED controller
72216ec6ccSSean Wang  * @cdev:		LED class device for this LED device
73216ec6ccSSean Wang  * @current_brightness: current state of the LED device
74216ec6ccSSean Wang  */
75216ec6ccSSean Wang struct mt6323_led {
76216ec6ccSSean Wang 	int			id;
77216ec6ccSSean Wang 	struct mt6323_leds	*parent;
78216ec6ccSSean Wang 	struct led_classdev	cdev;
79*877e9119SNathan Chancellor 	enum led_brightness	current_brightness;
80216ec6ccSSean Wang };
81216ec6ccSSean Wang 
82216ec6ccSSean Wang /**
833ec0b29eSAngeloGioacchino Del Regno  * struct mt6323_regs - register spec for the LED device
843ec0b29eSAngeloGioacchino Del Regno  * @top_ckpdn:		Offset to ISINK_CKPDN[0..x] registers
853ec0b29eSAngeloGioacchino Del Regno  * @num_top_ckpdn:	Number of ISINK_CKPDN registers
863ec0b29eSAngeloGioacchino Del Regno  * @top_ckcon:		Offset to ISINK_CKCON[0..x] registers
873ec0b29eSAngeloGioacchino Del Regno  * @num_top_ckcon:	Number of ISINK_CKCON registers
883ec0b29eSAngeloGioacchino Del Regno  * @isink_con:		Offset to ISINKx_CON[0..x] registers
893ec0b29eSAngeloGioacchino Del Regno  * @num_isink_con:	Number of ISINKx_CON registers
903ec0b29eSAngeloGioacchino Del Regno  * @isink_max_regs:	Number of ISINK[0..x] registers
913ec0b29eSAngeloGioacchino Del Regno  * @isink_en_ctrl:	Offset to ISINK_EN_CTRL register
929bb0a9e0SAngeloGioacchino Del Regno  * @iwled_en_ctrl:	Offset to IWLED_EN_CTRL register
933ec0b29eSAngeloGioacchino Del Regno  */
943ec0b29eSAngeloGioacchino Del Regno struct mt6323_regs {
953ec0b29eSAngeloGioacchino Del Regno 	const u16 *top_ckpdn;
963ec0b29eSAngeloGioacchino Del Regno 	u8 num_top_ckpdn;
973ec0b29eSAngeloGioacchino Del Regno 	const u16 *top_ckcon;
983ec0b29eSAngeloGioacchino Del Regno 	u8 num_top_ckcon;
993ec0b29eSAngeloGioacchino Del Regno 	const u16 *isink_con;
1003ec0b29eSAngeloGioacchino Del Regno 	u8 num_isink_con;
1013ec0b29eSAngeloGioacchino Del Regno 	u8 isink_max_regs;
1023ec0b29eSAngeloGioacchino Del Regno 	u16 isink_en_ctrl;
1039bb0a9e0SAngeloGioacchino Del Regno 	u16 iwled_en_ctrl;
1043ec0b29eSAngeloGioacchino Del Regno };
1053ec0b29eSAngeloGioacchino Del Regno 
1063ec0b29eSAngeloGioacchino Del Regno /**
1073ec0b29eSAngeloGioacchino Del Regno  * struct mt6323_hwspec - hardware specific parameters
1083ec0b29eSAngeloGioacchino Del Regno  * @max_period:		Maximum period for all LEDs
1093ec0b29eSAngeloGioacchino Del Regno  * @max_leds:		Maximum number of supported LEDs
1109bb0a9e0SAngeloGioacchino Del Regno  * @max_wleds:		Maximum number of WLEDs
1113ec0b29eSAngeloGioacchino Del Regno  * @max_brightness:	Maximum brightness for all LEDs
1123ec0b29eSAngeloGioacchino Del Regno  * @unit_duty:		Steps of duty per period
1133ec0b29eSAngeloGioacchino Del Regno  */
1143ec0b29eSAngeloGioacchino Del Regno struct mt6323_hwspec {
1153ec0b29eSAngeloGioacchino Del Regno 	u16 max_period;
1163ec0b29eSAngeloGioacchino Del Regno 	u8 max_leds;
1179bb0a9e0SAngeloGioacchino Del Regno 	u8 max_wleds;
1183ec0b29eSAngeloGioacchino Del Regno 	u16 max_brightness;
1193ec0b29eSAngeloGioacchino Del Regno 	u16 unit_duty;
1203ec0b29eSAngeloGioacchino Del Regno };
1213ec0b29eSAngeloGioacchino Del Regno 
1223ec0b29eSAngeloGioacchino Del Regno /**
1233ec0b29eSAngeloGioacchino Del Regno  * struct mt6323_data - device specific data
1243ec0b29eSAngeloGioacchino Del Regno  * @regs:		Register spec for this device
1253ec0b29eSAngeloGioacchino Del Regno  * @spec:		Hardware specific parameters
1263ec0b29eSAngeloGioacchino Del Regno  */
1273ec0b29eSAngeloGioacchino Del Regno struct mt6323_data {
1283ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_regs *regs;
1293ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_hwspec *spec;
1303ec0b29eSAngeloGioacchino Del Regno };
1313ec0b29eSAngeloGioacchino Del Regno 
1323ec0b29eSAngeloGioacchino Del Regno /**
133216ec6ccSSean Wang  * struct mt6323_leds -	state container for holding LED controller
134216ec6ccSSean Wang  *			of the driver
135216ec6ccSSean Wang  * @dev:		the device pointer
136216ec6ccSSean Wang  * @hw:			the underlying hardware providing shared
137216ec6ccSSean Wang  *			bus for the register operations
1383ec0b29eSAngeloGioacchino Del Regno  * @pdata:		device specific data
139216ec6ccSSean Wang  * @lock:		the lock among process context
140216ec6ccSSean Wang  * @led:		the array that contains the state of individual
141216ec6ccSSean Wang  *			LED device
142216ec6ccSSean Wang  */
143216ec6ccSSean Wang struct mt6323_leds {
144216ec6ccSSean Wang 	struct device		*dev;
145216ec6ccSSean Wang 	struct mt6397_chip	*hw;
1463ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_data *pdata;
147216ec6ccSSean Wang 	/* protect among process context */
148216ec6ccSSean Wang 	struct mutex		lock;
1493ec0b29eSAngeloGioacchino Del Regno 	struct mt6323_led	*led[MAX_SUPPORTED_LEDS];
150216ec6ccSSean Wang };
151216ec6ccSSean Wang 
mt6323_led_hw_brightness(struct led_classdev * cdev,enum led_brightness brightness)152216ec6ccSSean Wang static int mt6323_led_hw_brightness(struct led_classdev *cdev,
153216ec6ccSSean Wang 				    enum led_brightness brightness)
154216ec6ccSSean Wang {
155216ec6ccSSean Wang 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
156216ec6ccSSean Wang 	struct mt6323_leds *leds = led->parent;
1573ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
158216ec6ccSSean Wang 	struct regmap *regmap = leds->hw->regmap;
159216ec6ccSSean Wang 	u32 con2_mask = 0, con2_val = 0;
160216ec6ccSSean Wang 	int ret;
161216ec6ccSSean Wang 
162216ec6ccSSean Wang 	/*
163216ec6ccSSean Wang 	 * Setup current output for the corresponding
164216ec6ccSSean Wang 	 * brightness level.
165216ec6ccSSean Wang 	 */
1669bef1414SAngeloGioacchino Del Regno 	con2_mask |= ISINK_CH_STEP_MASK |
1679bef1414SAngeloGioacchino Del Regno 		     ISINK_SFSTR0_TC_MASK |
1689bef1414SAngeloGioacchino Del Regno 		     ISINK_SFSTR0_EN_MASK;
1699bef1414SAngeloGioacchino Del Regno 	con2_val |=  ISINK_CH_STEP(brightness - 1) |
1709bef1414SAngeloGioacchino Del Regno 		     ISINK_SFSTR0_TC(2) |
1719bef1414SAngeloGioacchino Del Regno 		     ISINK_SFSTR0_EN;
172216ec6ccSSean Wang 
1739bef1414SAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[2], led->id),
174216ec6ccSSean Wang 				 con2_mask, con2_val);
175216ec6ccSSean Wang 	return ret;
176216ec6ccSSean Wang }
177216ec6ccSSean Wang 
mt6323_led_hw_off(struct led_classdev * cdev)178216ec6ccSSean Wang static int mt6323_led_hw_off(struct led_classdev *cdev)
179216ec6ccSSean Wang {
180216ec6ccSSean Wang 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
181216ec6ccSSean Wang 	struct mt6323_leds *leds = led->parent;
1823ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
183216ec6ccSSean Wang 	struct regmap *regmap = leds->hw->regmap;
184216ec6ccSSean Wang 	unsigned int status;
185216ec6ccSSean Wang 	int ret;
186216ec6ccSSean Wang 
1879bef1414SAngeloGioacchino Del Regno 	status = ISINK_CH_EN(led->id);
1883ec0b29eSAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, regs->isink_en_ctrl,
1899bef1414SAngeloGioacchino Del Regno 				 ISINK_CH_EN_MASK(led->id), ~status);
190216ec6ccSSean Wang 	if (ret < 0)
191216ec6ccSSean Wang 		return ret;
192216ec6ccSSean Wang 
193216ec6ccSSean Wang 	usleep_range(100, 300);
1943ec0b29eSAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, regs->top_ckpdn[2],
1959bef1414SAngeloGioacchino Del Regno 				 RG_ISINK_CK_PDN_MASK(led->id),
1969bef1414SAngeloGioacchino Del Regno 				 RG_ISINK_CK_PDN(led->id));
197216ec6ccSSean Wang 	if (ret < 0)
198216ec6ccSSean Wang 		return ret;
199216ec6ccSSean Wang 
200216ec6ccSSean Wang 	return 0;
201216ec6ccSSean Wang }
202216ec6ccSSean Wang 
203216ec6ccSSean Wang static enum led_brightness
mt6323_get_led_hw_brightness(struct led_classdev * cdev)204216ec6ccSSean Wang mt6323_get_led_hw_brightness(struct led_classdev *cdev)
205216ec6ccSSean Wang {
206216ec6ccSSean Wang 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
207216ec6ccSSean Wang 	struct mt6323_leds *leds = led->parent;
2083ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
209216ec6ccSSean Wang 	struct regmap *regmap = leds->hw->regmap;
210216ec6ccSSean Wang 	unsigned int status;
211216ec6ccSSean Wang 	int ret;
212216ec6ccSSean Wang 
2133ec0b29eSAngeloGioacchino Del Regno 	ret = regmap_read(regmap, regs->top_ckpdn[2], &status);
214216ec6ccSSean Wang 	if (ret < 0)
215216ec6ccSSean Wang 		return ret;
216216ec6ccSSean Wang 
2179bef1414SAngeloGioacchino Del Regno 	if (status & RG_ISINK_CK_PDN_MASK(led->id))
218216ec6ccSSean Wang 		return 0;
219216ec6ccSSean Wang 
2203ec0b29eSAngeloGioacchino Del Regno 	ret = regmap_read(regmap, regs->isink_en_ctrl, &status);
221216ec6ccSSean Wang 	if (ret < 0)
222216ec6ccSSean Wang 		return ret;
223216ec6ccSSean Wang 
2249bef1414SAngeloGioacchino Del Regno 	if (!(status & ISINK_CH_EN(led->id)))
225216ec6ccSSean Wang 		return 0;
226216ec6ccSSean Wang 
2279bef1414SAngeloGioacchino Del Regno 	ret = regmap_read(regmap, ISINK_CON(regs->isink_con[2], led->id), &status);
228216ec6ccSSean Wang 	if (ret < 0)
229216ec6ccSSean Wang 		return ret;
230216ec6ccSSean Wang 
2319bef1414SAngeloGioacchino Del Regno 	return  ((status & ISINK_CH_STEP_MASK)
2329bef1414SAngeloGioacchino Del Regno 		  >> ISINK_CH_STEP_SHIFT) + 1;
233216ec6ccSSean Wang }
234216ec6ccSSean Wang 
mt6323_led_hw_on(struct led_classdev * cdev,enum led_brightness brightness)235216ec6ccSSean Wang static int mt6323_led_hw_on(struct led_classdev *cdev,
236216ec6ccSSean Wang 			    enum led_brightness brightness)
237216ec6ccSSean Wang {
238216ec6ccSSean Wang 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
239216ec6ccSSean Wang 	struct mt6323_leds *leds = led->parent;
2403ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
241216ec6ccSSean Wang 	struct regmap *regmap = leds->hw->regmap;
242216ec6ccSSean Wang 	unsigned int status;
243216ec6ccSSean Wang 	int ret;
244216ec6ccSSean Wang 
245216ec6ccSSean Wang 	/*
246216ec6ccSSean Wang 	 * Setup required clock source, enable the corresponding
247216ec6ccSSean Wang 	 * clock and channel and let work with continuous blink as
248216ec6ccSSean Wang 	 * the default.
249216ec6ccSSean Wang 	 */
2503ec0b29eSAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, regs->top_ckcon[1],
2519bef1414SAngeloGioacchino Del Regno 				 RG_ISINK_CK_SEL_MASK(led->id), 0);
252216ec6ccSSean Wang 	if (ret < 0)
253216ec6ccSSean Wang 		return ret;
254216ec6ccSSean Wang 
2559bef1414SAngeloGioacchino Del Regno 	status = RG_ISINK_CK_PDN(led->id);
2563ec0b29eSAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, regs->top_ckpdn[2],
2579bef1414SAngeloGioacchino Del Regno 				 RG_ISINK_CK_PDN_MASK(led->id),
258216ec6ccSSean Wang 				 ~status);
259216ec6ccSSean Wang 	if (ret < 0)
260216ec6ccSSean Wang 		return ret;
261216ec6ccSSean Wang 
262216ec6ccSSean Wang 	usleep_range(100, 300);
263216ec6ccSSean Wang 
2643ec0b29eSAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, regs->isink_en_ctrl,
2659bef1414SAngeloGioacchino Del Regno 				 ISINK_CH_EN_MASK(led->id),
2669bef1414SAngeloGioacchino Del Regno 				 ISINK_CH_EN(led->id));
267216ec6ccSSean Wang 	if (ret < 0)
268216ec6ccSSean Wang 		return ret;
269216ec6ccSSean Wang 
270216ec6ccSSean Wang 	ret = mt6323_led_hw_brightness(cdev, brightness);
271216ec6ccSSean Wang 	if (ret < 0)
272216ec6ccSSean Wang 		return ret;
273216ec6ccSSean Wang 
2749bef1414SAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[0], led->id),
2759bef1414SAngeloGioacchino Del Regno 				 ISINK_DIM_DUTY_MASK,
2769bef1414SAngeloGioacchino Del Regno 				 ISINK_DIM_DUTY(31));
277216ec6ccSSean Wang 	if (ret < 0)
278216ec6ccSSean Wang 		return ret;
279216ec6ccSSean Wang 
2809bef1414SAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[1], led->id),
2819bef1414SAngeloGioacchino Del Regno 				 ISINK_DIM_FSEL_MASK,
2829bef1414SAngeloGioacchino Del Regno 				 ISINK_DIM_FSEL(1000));
283216ec6ccSSean Wang 	if (ret < 0)
284216ec6ccSSean Wang 		return ret;
285216ec6ccSSean Wang 
286216ec6ccSSean Wang 	return 0;
287216ec6ccSSean Wang }
288216ec6ccSSean Wang 
mt6323_led_set_blink(struct led_classdev * cdev,unsigned long * delay_on,unsigned long * delay_off)289216ec6ccSSean Wang static int mt6323_led_set_blink(struct led_classdev *cdev,
290216ec6ccSSean Wang 				unsigned long *delay_on,
291216ec6ccSSean Wang 				unsigned long *delay_off)
292216ec6ccSSean Wang {
293216ec6ccSSean Wang 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
294216ec6ccSSean Wang 	struct mt6323_leds *leds = led->parent;
2953ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
2963ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_hwspec *spec = leds->pdata->spec;
297216ec6ccSSean Wang 	struct regmap *regmap = leds->hw->regmap;
298216ec6ccSSean Wang 	unsigned long period;
299216ec6ccSSean Wang 	u8 duty_hw;
300216ec6ccSSean Wang 	int ret;
301216ec6ccSSean Wang 
302216ec6ccSSean Wang 	/*
303216ec6ccSSean Wang 	 * LED subsystem requires a default user
304216ec6ccSSean Wang 	 * friendly blink pattern for the LED so using
305216ec6ccSSean Wang 	 * 1Hz duty cycle 50% here if without specific
306216ec6ccSSean Wang 	 * value delay_on and delay off being assigned.
307216ec6ccSSean Wang 	 */
308216ec6ccSSean Wang 	if (!*delay_on && !*delay_off) {
309216ec6ccSSean Wang 		*delay_on = 500;
310216ec6ccSSean Wang 		*delay_off = 500;
311216ec6ccSSean Wang 	}
312216ec6ccSSean Wang 
313216ec6ccSSean Wang 	/*
314fa31311cSTom Rix 	 * Units are in ms, if over the hardware able
315fa31311cSTom Rix 	 * to support, fallback into software blink
316fa31311cSTom Rix 	 */
317fa31311cSTom Rix 	period = *delay_on + *delay_off;
318fa31311cSTom Rix 
3193ec0b29eSAngeloGioacchino Del Regno 	if (period > spec->max_period)
320fa31311cSTom Rix 		return -EINVAL;
321fa31311cSTom Rix 
322fa31311cSTom Rix 	/*
323216ec6ccSSean Wang 	 * Calculate duty_hw based on the percentage of period during
324216ec6ccSSean Wang 	 * which the led is ON.
325216ec6ccSSean Wang 	 */
3264c58b6d9SAngeloGioacchino Del Regno 	duty_hw = DIV_ROUND_CLOSEST(*delay_on * 100000ul, period * spec->unit_duty);
327216ec6ccSSean Wang 
328216ec6ccSSean Wang 	/* hardware doesn't support zero duty cycle. */
329216ec6ccSSean Wang 	if (!duty_hw)
330216ec6ccSSean Wang 		return -EINVAL;
331216ec6ccSSean Wang 
332216ec6ccSSean Wang 	mutex_lock(&leds->lock);
333216ec6ccSSean Wang 	/*
334216ec6ccSSean Wang 	 * Set max_brightness as the software blink behavior
335216ec6ccSSean Wang 	 * when no blink brightness.
336216ec6ccSSean Wang 	 */
337216ec6ccSSean Wang 	if (!led->current_brightness) {
338216ec6ccSSean Wang 		ret = mt6323_led_hw_on(cdev, cdev->max_brightness);
339216ec6ccSSean Wang 		if (ret < 0)
340216ec6ccSSean Wang 			goto out;
341216ec6ccSSean Wang 		led->current_brightness = cdev->max_brightness;
342216ec6ccSSean Wang 	}
343216ec6ccSSean Wang 
3449bef1414SAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[0], led->id),
3459bef1414SAngeloGioacchino Del Regno 				 ISINK_DIM_DUTY_MASK,
3469bef1414SAngeloGioacchino Del Regno 				 ISINK_DIM_DUTY(duty_hw - 1));
347216ec6ccSSean Wang 	if (ret < 0)
348216ec6ccSSean Wang 		goto out;
349216ec6ccSSean Wang 
3509bef1414SAngeloGioacchino Del Regno 	ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[1], led->id),
3519bef1414SAngeloGioacchino Del Regno 				 ISINK_DIM_FSEL_MASK,
3529bef1414SAngeloGioacchino Del Regno 				 ISINK_DIM_FSEL(period - 1));
353216ec6ccSSean Wang out:
354216ec6ccSSean Wang 	mutex_unlock(&leds->lock);
355216ec6ccSSean Wang 
356216ec6ccSSean Wang 	return ret;
357216ec6ccSSean Wang }
358216ec6ccSSean Wang 
mt6323_led_set_brightness(struct led_classdev * cdev,enum led_brightness brightness)359216ec6ccSSean Wang static int mt6323_led_set_brightness(struct led_classdev *cdev,
360216ec6ccSSean Wang 				     enum led_brightness brightness)
361216ec6ccSSean Wang {
362216ec6ccSSean Wang 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
363216ec6ccSSean Wang 	struct mt6323_leds *leds = led->parent;
364216ec6ccSSean Wang 	int ret;
365216ec6ccSSean Wang 
366216ec6ccSSean Wang 	mutex_lock(&leds->lock);
367216ec6ccSSean Wang 
368216ec6ccSSean Wang 	if (!led->current_brightness && brightness) {
369216ec6ccSSean Wang 		ret = mt6323_led_hw_on(cdev, brightness);
370216ec6ccSSean Wang 		if (ret < 0)
371216ec6ccSSean Wang 			goto out;
372216ec6ccSSean Wang 	} else if (brightness) {
373216ec6ccSSean Wang 		ret = mt6323_led_hw_brightness(cdev, brightness);
374216ec6ccSSean Wang 		if (ret < 0)
375216ec6ccSSean Wang 			goto out;
376216ec6ccSSean Wang 	} else {
377216ec6ccSSean Wang 		ret = mt6323_led_hw_off(cdev);
378216ec6ccSSean Wang 		if (ret < 0)
379216ec6ccSSean Wang 			goto out;
380216ec6ccSSean Wang 	}
381216ec6ccSSean Wang 
382216ec6ccSSean Wang 	led->current_brightness = brightness;
383216ec6ccSSean Wang out:
384216ec6ccSSean Wang 	mutex_unlock(&leds->lock);
385216ec6ccSSean Wang 
386216ec6ccSSean Wang 	return ret;
387216ec6ccSSean Wang }
388216ec6ccSSean Wang 
mtk_wled_hw_on(struct led_classdev * cdev)3899bb0a9e0SAngeloGioacchino Del Regno static int mtk_wled_hw_on(struct led_classdev *cdev)
3909bb0a9e0SAngeloGioacchino Del Regno {
3919bb0a9e0SAngeloGioacchino Del Regno 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
3929bb0a9e0SAngeloGioacchino Del Regno 	struct mt6323_leds *leds = led->parent;
3939bb0a9e0SAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
3949bb0a9e0SAngeloGioacchino Del Regno 	struct regmap *regmap = leds->hw->regmap;
3959bb0a9e0SAngeloGioacchino Del Regno 	int ret;
3969bb0a9e0SAngeloGioacchino Del Regno 
3979bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_clear_bits(regmap, regs->top_ckpdn[0], RG_VWLED_32K_CK_PDN);
3989bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
3999bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4009bb0a9e0SAngeloGioacchino Del Regno 
4019bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_clear_bits(regmap, regs->top_ckpdn[0], RG_VWLED_6M_CK_PDN);
4029bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4039bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4049bb0a9e0SAngeloGioacchino Del Regno 
4059bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_clear_bits(regmap, regs->top_ckpdn[0], RG_VWLED_1M_CK_PDN);
4069bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4079bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4089bb0a9e0SAngeloGioacchino Del Regno 
4099bb0a9e0SAngeloGioacchino Del Regno 	usleep_range(5000, 6000);
4109bb0a9e0SAngeloGioacchino Del Regno 
4119bb0a9e0SAngeloGioacchino Del Regno 	/* Enable WLED channel pair */
4129bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_set_bits(regmap, regs->iwled_en_ctrl, BIT(led->id));
4139bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4149bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4159bb0a9e0SAngeloGioacchino Del Regno 
4169bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_set_bits(regmap, regs->iwled_en_ctrl, BIT(led->id + 1));
4179bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4189bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4199bb0a9e0SAngeloGioacchino Del Regno 
4209bb0a9e0SAngeloGioacchino Del Regno 	return 0;
4219bb0a9e0SAngeloGioacchino Del Regno }
4229bb0a9e0SAngeloGioacchino Del Regno 
mtk_wled_hw_off(struct led_classdev * cdev)4239bb0a9e0SAngeloGioacchino Del Regno static int mtk_wled_hw_off(struct led_classdev *cdev)
4249bb0a9e0SAngeloGioacchino Del Regno {
4259bb0a9e0SAngeloGioacchino Del Regno 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
4269bb0a9e0SAngeloGioacchino Del Regno 	struct mt6323_leds *leds = led->parent;
4279bb0a9e0SAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
4289bb0a9e0SAngeloGioacchino Del Regno 	struct regmap *regmap = leds->hw->regmap;
4299bb0a9e0SAngeloGioacchino Del Regno 	int ret;
4309bb0a9e0SAngeloGioacchino Del Regno 
4319bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_clear_bits(regmap, regs->iwled_en_ctrl, BIT(led->id + 1));
4329bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4339bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4349bb0a9e0SAngeloGioacchino Del Regno 
4359bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_clear_bits(regmap, regs->iwled_en_ctrl, BIT(led->id));
4369bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4379bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4389bb0a9e0SAngeloGioacchino Del Regno 
4399bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_set_bits(regmap, regs->top_ckpdn[0], RG_VWLED_32K_CK_PDN);
4409bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4419bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4429bb0a9e0SAngeloGioacchino Del Regno 
4439bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_set_bits(regmap, regs->top_ckpdn[0], RG_VWLED_6M_CK_PDN);
4449bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4459bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4469bb0a9e0SAngeloGioacchino Del Regno 
4479bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_set_bits(regmap, regs->top_ckpdn[0], RG_VWLED_1M_CK_PDN);
4489bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4499bb0a9e0SAngeloGioacchino Del Regno 		return ret;
4509bb0a9e0SAngeloGioacchino Del Regno 
4519bb0a9e0SAngeloGioacchino Del Regno 	return 0;
4529bb0a9e0SAngeloGioacchino Del Regno }
4539bb0a9e0SAngeloGioacchino Del Regno 
mt6323_get_wled_brightness(struct led_classdev * cdev)454*877e9119SNathan Chancellor static enum led_brightness mt6323_get_wled_brightness(struct led_classdev *cdev)
4559bb0a9e0SAngeloGioacchino Del Regno {
4569bb0a9e0SAngeloGioacchino Del Regno 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
4579bb0a9e0SAngeloGioacchino Del Regno 	struct mt6323_leds *leds = led->parent;
4589bb0a9e0SAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
4599bb0a9e0SAngeloGioacchino Del Regno 	struct regmap *regmap = leds->hw->regmap;
4609bb0a9e0SAngeloGioacchino Del Regno 	unsigned int status;
4619bb0a9e0SAngeloGioacchino Del Regno 	int ret;
4629bb0a9e0SAngeloGioacchino Del Regno 
4639bb0a9e0SAngeloGioacchino Del Regno 	ret = regmap_read(regmap, regs->iwled_en_ctrl, &status);
4649bb0a9e0SAngeloGioacchino Del Regno 	if (ret)
4659bb0a9e0SAngeloGioacchino Del Regno 		return 0;
4669bb0a9e0SAngeloGioacchino Del Regno 
4679bb0a9e0SAngeloGioacchino Del Regno 	/* Always two channels per WLED */
4689bb0a9e0SAngeloGioacchino Del Regno 	status &= BIT(led->id) | BIT(led->id + 1);
4699bb0a9e0SAngeloGioacchino Del Regno 
4709bb0a9e0SAngeloGioacchino Del Regno 	return status ? led->current_brightness : 0;
4719bb0a9e0SAngeloGioacchino Del Regno }
4729bb0a9e0SAngeloGioacchino Del Regno 
mt6323_wled_set_brightness(struct led_classdev * cdev,enum led_brightness brightness)4739bb0a9e0SAngeloGioacchino Del Regno static int mt6323_wled_set_brightness(struct led_classdev *cdev,
474*877e9119SNathan Chancellor 				      enum led_brightness brightness)
4759bb0a9e0SAngeloGioacchino Del Regno {
4769bb0a9e0SAngeloGioacchino Del Regno 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
4779bb0a9e0SAngeloGioacchino Del Regno 	struct mt6323_leds *leds = led->parent;
4789bb0a9e0SAngeloGioacchino Del Regno 	int ret = 0;
4799bb0a9e0SAngeloGioacchino Del Regno 
4809bb0a9e0SAngeloGioacchino Del Regno 	mutex_lock(&leds->lock);
4819bb0a9e0SAngeloGioacchino Del Regno 
4829bb0a9e0SAngeloGioacchino Del Regno 	if (brightness) {
4839bb0a9e0SAngeloGioacchino Del Regno 		if (!led->current_brightness)
4849bb0a9e0SAngeloGioacchino Del Regno 			ret = mtk_wled_hw_on(cdev);
4859bb0a9e0SAngeloGioacchino Del Regno 		if (ret)
4869bb0a9e0SAngeloGioacchino Del Regno 			goto out;
4879bb0a9e0SAngeloGioacchino Del Regno 	} else {
4889bb0a9e0SAngeloGioacchino Del Regno 		ret = mtk_wled_hw_off(cdev);
4899bb0a9e0SAngeloGioacchino Del Regno 		if (ret)
4909bb0a9e0SAngeloGioacchino Del Regno 			goto out;
4919bb0a9e0SAngeloGioacchino Del Regno 	}
4929bb0a9e0SAngeloGioacchino Del Regno 
4939bb0a9e0SAngeloGioacchino Del Regno 	led->current_brightness = brightness;
4949bb0a9e0SAngeloGioacchino Del Regno out:
4959bb0a9e0SAngeloGioacchino Del Regno 	mutex_unlock(&leds->lock);
4969bb0a9e0SAngeloGioacchino Del Regno 
4979bb0a9e0SAngeloGioacchino Del Regno 	return ret;
4989bb0a9e0SAngeloGioacchino Del Regno }
4999bb0a9e0SAngeloGioacchino Del Regno 
mt6323_led_set_dt_default(struct led_classdev * cdev,struct device_node * np)500216ec6ccSSean Wang static int mt6323_led_set_dt_default(struct led_classdev *cdev,
501216ec6ccSSean Wang 				     struct device_node *np)
502216ec6ccSSean Wang {
503216ec6ccSSean Wang 	struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
50499cade83SAndy Shevchenko 	enum led_default_state state;
505216ec6ccSSean Wang 	int ret = 0;
506216ec6ccSSean Wang 
50799cade83SAndy Shevchenko 	state = led_init_default_state_get(of_fwnode_handle(np));
50899cade83SAndy Shevchenko 	switch (state) {
50999cade83SAndy Shevchenko 	case LEDS_DEFSTATE_ON:
51099cade83SAndy Shevchenko 		ret = mt6323_led_set_brightness(cdev, cdev->max_brightness);
51199cade83SAndy Shevchenko 		break;
51299cade83SAndy Shevchenko 	case LEDS_DEFSTATE_KEEP:
513216ec6ccSSean Wang 		ret = mt6323_get_led_hw_brightness(cdev);
514216ec6ccSSean Wang 		if (ret < 0)
515216ec6ccSSean Wang 			return ret;
516216ec6ccSSean Wang 		led->current_brightness = ret;
517216ec6ccSSean Wang 		ret = 0;
51899cade83SAndy Shevchenko 		break;
51999cade83SAndy Shevchenko 	default:
520216ec6ccSSean Wang 		ret = mt6323_led_set_brightness(cdev, LED_OFF);
521216ec6ccSSean Wang 	}
522216ec6ccSSean Wang 
523216ec6ccSSean Wang 	return ret;
524216ec6ccSSean Wang }
525216ec6ccSSean Wang 
mt6323_led_probe(struct platform_device * pdev)526216ec6ccSSean Wang static int mt6323_led_probe(struct platform_device *pdev)
527216ec6ccSSean Wang {
528216ec6ccSSean Wang 	struct device *dev = &pdev->dev;
529b23ca98aSMarek Behún 	struct device_node *np = dev_of_node(dev);
530216ec6ccSSean Wang 	struct device_node *child;
531b23ca98aSMarek Behún 	struct mt6397_chip *hw = dev_get_drvdata(dev->parent);
532216ec6ccSSean Wang 	struct mt6323_leds *leds;
533216ec6ccSSean Wang 	struct mt6323_led *led;
5343ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_regs *regs;
5353ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_hwspec *spec;
536216ec6ccSSean Wang 	int ret;
537216ec6ccSSean Wang 	unsigned int status;
538216ec6ccSSean Wang 	u32 reg;
5399bb0a9e0SAngeloGioacchino Del Regno 	u8 max_leds;
540216ec6ccSSean Wang 
541216ec6ccSSean Wang 	leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
542216ec6ccSSean Wang 	if (!leds)
543216ec6ccSSean Wang 		return -ENOMEM;
544216ec6ccSSean Wang 
545216ec6ccSSean Wang 	platform_set_drvdata(pdev, leds);
546216ec6ccSSean Wang 	leds->dev = dev;
5473ec0b29eSAngeloGioacchino Del Regno 	leds->pdata = device_get_match_data(dev);
5483ec0b29eSAngeloGioacchino Del Regno 	regs = leds->pdata->regs;
5493ec0b29eSAngeloGioacchino Del Regno 	spec = leds->pdata->spec;
5509bb0a9e0SAngeloGioacchino Del Regno 	max_leds = spec->max_leds + spec->max_wleds;
551216ec6ccSSean Wang 
552216ec6ccSSean Wang 	/*
553216ec6ccSSean Wang 	 * leds->hw points to the underlying bus for the register
554216ec6ccSSean Wang 	 * controlled.
555216ec6ccSSean Wang 	 */
556216ec6ccSSean Wang 	leds->hw = hw;
557216ec6ccSSean Wang 	mutex_init(&leds->lock);
558216ec6ccSSean Wang 
5599bef1414SAngeloGioacchino Del Regno 	status = RG_DRV_32K_CK_PDN;
5603ec0b29eSAngeloGioacchino Del Regno 	ret = regmap_update_bits(leds->hw->regmap, regs->top_ckpdn[0],
5619bef1414SAngeloGioacchino Del Regno 				 RG_DRV_32K_CK_PDN_MASK, ~status);
562216ec6ccSSean Wang 	if (ret < 0) {
563216ec6ccSSean Wang 		dev_err(leds->dev,
5643ec0b29eSAngeloGioacchino Del Regno 			"Failed to update TOP_CKPDN0 Register\n");
565216ec6ccSSean Wang 		return ret;
566216ec6ccSSean Wang 	}
567216ec6ccSSean Wang 
568216ec6ccSSean Wang 	for_each_available_child_of_node(np, child) {
5696b8274deSMarek Behún 		struct led_init_data init_data = {};
5709bb0a9e0SAngeloGioacchino Del Regno 		bool is_wled;
5716b8274deSMarek Behún 
572216ec6ccSSean Wang 		ret = of_property_read_u32(child, "reg", &reg);
573216ec6ccSSean Wang 		if (ret) {
574216ec6ccSSean Wang 			dev_err(dev, "Failed to read led 'reg' property\n");
575216ec6ccSSean Wang 			goto put_child_node;
576216ec6ccSSean Wang 		}
577216ec6ccSSean Wang 
5789bb0a9e0SAngeloGioacchino Del Regno 		if (reg >= max_leds || reg >= MAX_SUPPORTED_LEDS ||
5793ec0b29eSAngeloGioacchino Del Regno 		    leds->led[reg]) {
580216ec6ccSSean Wang 			dev_err(dev, "Invalid led reg %u\n", reg);
581216ec6ccSSean Wang 			ret = -EINVAL;
582216ec6ccSSean Wang 			goto put_child_node;
583216ec6ccSSean Wang 		}
584216ec6ccSSean Wang 
585216ec6ccSSean Wang 		led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
586216ec6ccSSean Wang 		if (!led) {
587216ec6ccSSean Wang 			ret = -ENOMEM;
588216ec6ccSSean Wang 			goto put_child_node;
589216ec6ccSSean Wang 		}
590216ec6ccSSean Wang 
5919bb0a9e0SAngeloGioacchino Del Regno 		is_wled = of_property_read_bool(child, "mediatek,is-wled");
5929bb0a9e0SAngeloGioacchino Del Regno 
593216ec6ccSSean Wang 		leds->led[reg] = led;
594216ec6ccSSean Wang 		leds->led[reg]->id = reg;
5953ec0b29eSAngeloGioacchino Del Regno 		leds->led[reg]->cdev.max_brightness = spec->max_brightness;
5969bb0a9e0SAngeloGioacchino Del Regno 
5979bb0a9e0SAngeloGioacchino Del Regno 		if (is_wled) {
5989bb0a9e0SAngeloGioacchino Del Regno 			leds->led[reg]->cdev.brightness_set_blocking =
5999bb0a9e0SAngeloGioacchino Del Regno 						mt6323_wled_set_brightness;
6009bb0a9e0SAngeloGioacchino Del Regno 			leds->led[reg]->cdev.brightness_get =
6019bb0a9e0SAngeloGioacchino Del Regno 						mt6323_get_wled_brightness;
6029bb0a9e0SAngeloGioacchino Del Regno 		} else {
603216ec6ccSSean Wang 			leds->led[reg]->cdev.brightness_set_blocking =
604216ec6ccSSean Wang 						mt6323_led_set_brightness;
605216ec6ccSSean Wang 			leds->led[reg]->cdev.blink_set = mt6323_led_set_blink;
606216ec6ccSSean Wang 			leds->led[reg]->cdev.brightness_get =
607216ec6ccSSean Wang 						mt6323_get_led_hw_brightness;
6089bb0a9e0SAngeloGioacchino Del Regno 		}
609216ec6ccSSean Wang 		leds->led[reg]->parent = leds;
610216ec6ccSSean Wang 
611216ec6ccSSean Wang 		ret = mt6323_led_set_dt_default(&leds->led[reg]->cdev, child);
612216ec6ccSSean Wang 		if (ret < 0) {
613216ec6ccSSean Wang 			dev_err(leds->dev,
614216ec6ccSSean Wang 				"Failed to LED set default from devicetree\n");
615216ec6ccSSean Wang 			goto put_child_node;
616216ec6ccSSean Wang 		}
617216ec6ccSSean Wang 
6186b8274deSMarek Behún 		init_data.fwnode = of_fwnode_handle(child);
6196b8274deSMarek Behún 
6206b8274deSMarek Behún 		ret = devm_led_classdev_register_ext(dev, &leds->led[reg]->cdev,
6216b8274deSMarek Behún 						     &init_data);
622216ec6ccSSean Wang 		if (ret) {
623b23ca98aSMarek Behún 			dev_err(dev, "Failed to register LED: %d\n", ret);
624216ec6ccSSean Wang 			goto put_child_node;
625216ec6ccSSean Wang 		}
626216ec6ccSSean Wang 	}
627216ec6ccSSean Wang 
628216ec6ccSSean Wang 	return 0;
629216ec6ccSSean Wang 
630216ec6ccSSean Wang put_child_node:
631216ec6ccSSean Wang 	of_node_put(child);
632216ec6ccSSean Wang 	return ret;
633216ec6ccSSean Wang }
634216ec6ccSSean Wang 
mt6323_led_remove(struct platform_device * pdev)635216ec6ccSSean Wang static int mt6323_led_remove(struct platform_device *pdev)
636216ec6ccSSean Wang {
637216ec6ccSSean Wang 	struct mt6323_leds *leds = platform_get_drvdata(pdev);
6383ec0b29eSAngeloGioacchino Del Regno 	const struct mt6323_regs *regs = leds->pdata->regs;
639216ec6ccSSean Wang 	int i;
640216ec6ccSSean Wang 
641216ec6ccSSean Wang 	/* Turn the LEDs off on driver removal. */
642216ec6ccSSean Wang 	for (i = 0 ; leds->led[i] ; i++)
643216ec6ccSSean Wang 		mt6323_led_hw_off(&leds->led[i]->cdev);
644216ec6ccSSean Wang 
6453ec0b29eSAngeloGioacchino Del Regno 	regmap_update_bits(leds->hw->regmap, regs->top_ckpdn[0],
6469bef1414SAngeloGioacchino Del Regno 			   RG_DRV_32K_CK_PDN_MASK,
6479bef1414SAngeloGioacchino Del Regno 			   RG_DRV_32K_CK_PDN);
648216ec6ccSSean Wang 
649216ec6ccSSean Wang 	mutex_destroy(&leds->lock);
650216ec6ccSSean Wang 
651216ec6ccSSean Wang 	return 0;
652216ec6ccSSean Wang }
653216ec6ccSSean Wang 
6543ec0b29eSAngeloGioacchino Del Regno static const struct mt6323_regs mt6323_registers = {
6553ec0b29eSAngeloGioacchino Del Regno 	.top_ckpdn = (const u16[]){ 0x102, 0x106, 0x10e },
6563ec0b29eSAngeloGioacchino Del Regno 	.num_top_ckpdn = 3,
6573ec0b29eSAngeloGioacchino Del Regno 	.top_ckcon = (const u16[]){ 0x120, 0x126 },
6583ec0b29eSAngeloGioacchino Del Regno 	.num_top_ckcon = 2,
6593ec0b29eSAngeloGioacchino Del Regno 	.isink_con = (const u16[]){ 0x330, 0x332, 0x334 },
6603ec0b29eSAngeloGioacchino Del Regno 	.num_isink_con = 3,
6613ec0b29eSAngeloGioacchino Del Regno 	.isink_max_regs = 4, /* ISINK[0..3] */
6623ec0b29eSAngeloGioacchino Del Regno 	.isink_en_ctrl = 0x356,
6633ec0b29eSAngeloGioacchino Del Regno };
6643ec0b29eSAngeloGioacchino Del Regno 
6659540989cSAngeloGioacchino Del Regno static const struct mt6323_regs mt6331_registers = {
6669540989cSAngeloGioacchino Del Regno 	.top_ckpdn = (const u16[]){ 0x138, 0x13e, 0x144 },
6679540989cSAngeloGioacchino Del Regno 	.num_top_ckpdn = 3,
6689540989cSAngeloGioacchino Del Regno 	.top_ckcon = (const u16[]){ 0x14c, 0x14a },
6699540989cSAngeloGioacchino Del Regno 	.num_top_ckcon = 2,
6709540989cSAngeloGioacchino Del Regno 	.isink_con = (const u16[]){ 0x40c, 0x40e, 0x410, 0x412, 0x414 },
6719540989cSAngeloGioacchino Del Regno 	.num_isink_con = 5,
6729540989cSAngeloGioacchino Del Regno 	.isink_max_regs = 4, /* ISINK[0..3] */
6739540989cSAngeloGioacchino Del Regno 	.isink_en_ctrl = 0x43a,
6749540989cSAngeloGioacchino Del Regno };
6759540989cSAngeloGioacchino Del Regno 
6769bb0a9e0SAngeloGioacchino Del Regno static const struct mt6323_regs mt6332_registers = {
6779bb0a9e0SAngeloGioacchino Del Regno 	.top_ckpdn = (const u16[]){ 0x8094, 0x809a, 0x80a0 },
6789bb0a9e0SAngeloGioacchino Del Regno 	.num_top_ckpdn = 3,
6799bb0a9e0SAngeloGioacchino Del Regno 	.top_ckcon = (const u16[]){ 0x80a6, 0x80ac },
6809bb0a9e0SAngeloGioacchino Del Regno 	.num_top_ckcon = 2,
6819bb0a9e0SAngeloGioacchino Del Regno 	.isink_con = (const u16[]){ 0x8cd4 },
6829bb0a9e0SAngeloGioacchino Del Regno 	.num_isink_con = 1,
6839bb0a9e0SAngeloGioacchino Del Regno 	.isink_max_regs = 12, /* IWLED[0..2, 3..9] */
6849bb0a9e0SAngeloGioacchino Del Regno 	.iwled_en_ctrl = 0x8cda,
6859bb0a9e0SAngeloGioacchino Del Regno };
6869bb0a9e0SAngeloGioacchino Del Regno 
6873ec0b29eSAngeloGioacchino Del Regno static const struct mt6323_hwspec mt6323_spec = {
6883ec0b29eSAngeloGioacchino Del Regno 	.max_period = 10000,
6893ec0b29eSAngeloGioacchino Del Regno 	.max_leds = 4,
6903ec0b29eSAngeloGioacchino Del Regno 	.max_brightness = 6,
6913ec0b29eSAngeloGioacchino Del Regno 	.unit_duty = 3125,
6923ec0b29eSAngeloGioacchino Del Regno };
6933ec0b29eSAngeloGioacchino Del Regno 
6949bb0a9e0SAngeloGioacchino Del Regno static const struct mt6323_hwspec mt6332_spec = {
6959bb0a9e0SAngeloGioacchino Del Regno 	/* There are no LEDs in MT6332. Only WLEDs are present. */
6969bb0a9e0SAngeloGioacchino Del Regno 	.max_leds = 0,
6979bb0a9e0SAngeloGioacchino Del Regno 	.max_wleds = 1,
6989bb0a9e0SAngeloGioacchino Del Regno 	.max_brightness = 1024,
6999bb0a9e0SAngeloGioacchino Del Regno };
7009bb0a9e0SAngeloGioacchino Del Regno 
7013ec0b29eSAngeloGioacchino Del Regno static const struct mt6323_data mt6323_pdata = {
7023ec0b29eSAngeloGioacchino Del Regno 	.regs = &mt6323_registers,
7033ec0b29eSAngeloGioacchino Del Regno 	.spec = &mt6323_spec,
7043ec0b29eSAngeloGioacchino Del Regno };
7053ec0b29eSAngeloGioacchino Del Regno 
7069540989cSAngeloGioacchino Del Regno static const struct mt6323_data mt6331_pdata = {
7079540989cSAngeloGioacchino Del Regno 	.regs = &mt6331_registers,
7089540989cSAngeloGioacchino Del Regno 	.spec = &mt6323_spec,
7099540989cSAngeloGioacchino Del Regno };
7109540989cSAngeloGioacchino Del Regno 
7119bb0a9e0SAngeloGioacchino Del Regno static const struct mt6323_data mt6332_pdata = {
7129bb0a9e0SAngeloGioacchino Del Regno 	.regs = &mt6332_registers,
7139bb0a9e0SAngeloGioacchino Del Regno 	.spec = &mt6332_spec,
7149bb0a9e0SAngeloGioacchino Del Regno };
7159bb0a9e0SAngeloGioacchino Del Regno 
716216ec6ccSSean Wang static const struct of_device_id mt6323_led_dt_match[] = {
7173ec0b29eSAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt6323-led", .data = &mt6323_pdata},
7189540989cSAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt6331-led", .data = &mt6331_pdata },
7199bb0a9e0SAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt6332-led", .data = &mt6332_pdata },
720216ec6ccSSean Wang 	{},
721216ec6ccSSean Wang };
722216ec6ccSSean Wang MODULE_DEVICE_TABLE(of, mt6323_led_dt_match);
723216ec6ccSSean Wang 
724216ec6ccSSean Wang static struct platform_driver mt6323_led_driver = {
725216ec6ccSSean Wang 	.probe		= mt6323_led_probe,
726216ec6ccSSean Wang 	.remove		= mt6323_led_remove,
727216ec6ccSSean Wang 	.driver		= {
728216ec6ccSSean Wang 		.name	= "mt6323-led",
729216ec6ccSSean Wang 		.of_match_table = mt6323_led_dt_match,
730216ec6ccSSean Wang 	},
731216ec6ccSSean Wang };
732216ec6ccSSean Wang 
733216ec6ccSSean Wang module_platform_driver(mt6323_led_driver);
734216ec6ccSSean Wang 
735216ec6ccSSean Wang MODULE_DESCRIPTION("LED driver for Mediatek MT6323 PMIC");
736216ec6ccSSean Wang MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
737216ec6ccSSean Wang MODULE_LICENSE("GPL");
738