xref: /openbmc/linux/drivers/leds/leds-lp5521.c (revision 9ca3bd8022d76a0d1b386cedcecaf49004a58644)
1500fe141SSamu Onkalo /*
2500fe141SSamu Onkalo  * LP5521 LED chip driver.
3500fe141SSamu Onkalo  *
4500fe141SSamu Onkalo  * Copyright (C) 2010 Nokia Corporation
5500fe141SSamu Onkalo  *
6500fe141SSamu Onkalo  * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
7500fe141SSamu Onkalo  *
8500fe141SSamu Onkalo  * This program is free software; you can redistribute it and/or
9500fe141SSamu Onkalo  * modify it under the terms of the GNU General Public License
10500fe141SSamu Onkalo  * version 2 as published by the Free Software Foundation.
11500fe141SSamu Onkalo  *
12500fe141SSamu Onkalo  * This program is distributed in the hope that it will be useful, but
13500fe141SSamu Onkalo  * WITHOUT ANY WARRANTY; without even the implied warranty of
14500fe141SSamu Onkalo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
15500fe141SSamu Onkalo  * General Public License for more details.
16500fe141SSamu Onkalo  *
17500fe141SSamu Onkalo  * You should have received a copy of the GNU General Public License
18500fe141SSamu Onkalo  * along with this program; if not, write to the Free Software
19500fe141SSamu Onkalo  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20500fe141SSamu Onkalo  * 02110-1301 USA
21500fe141SSamu Onkalo  */
22500fe141SSamu Onkalo 
23500fe141SSamu Onkalo #include <linux/module.h>
24500fe141SSamu Onkalo #include <linux/init.h>
25500fe141SSamu Onkalo #include <linux/i2c.h>
26500fe141SSamu Onkalo #include <linux/mutex.h>
27500fe141SSamu Onkalo #include <linux/gpio.h>
28500fe141SSamu Onkalo #include <linux/interrupt.h>
29500fe141SSamu Onkalo #include <linux/delay.h>
30500fe141SSamu Onkalo #include <linux/ctype.h>
31500fe141SSamu Onkalo #include <linux/spinlock.h>
32500fe141SSamu Onkalo #include <linux/wait.h>
33500fe141SSamu Onkalo #include <linux/leds.h>
34500fe141SSamu Onkalo #include <linux/leds-lp5521.h>
35500fe141SSamu Onkalo #include <linux/workqueue.h>
36500fe141SSamu Onkalo #include <linux/slab.h>
376a0c9a47SMilo(Woogyom) Kim #include <linux/platform_data/leds-lp55xx.h>
389ce7cb17SMilo(Woogyom) Kim #include <linux/firmware.h>
396a0c9a47SMilo(Woogyom) Kim 
406a0c9a47SMilo(Woogyom) Kim #include "leds-lp55xx-common.h"
41500fe141SSamu Onkalo 
42500fe141SSamu Onkalo #define LP5521_PROGRAM_LENGTH		32	/* in bytes */
43500fe141SSamu Onkalo 
44500fe141SSamu Onkalo #define LP5521_MAX_LEDS			3	/* Maximum number of LEDs */
45500fe141SSamu Onkalo #define LP5521_MAX_ENGINES		3	/* Maximum number of engines */
46500fe141SSamu Onkalo 
47500fe141SSamu Onkalo #define LP5521_ENG_MASK_BASE		0x30	/* 00110000 */
48500fe141SSamu Onkalo #define LP5521_ENG_STATUS_MASK		0x07	/* 00000111 */
49500fe141SSamu Onkalo 
50500fe141SSamu Onkalo #define LP5521_CMD_LOAD			0x15	/* 00010101 */
51500fe141SSamu Onkalo #define LP5521_CMD_RUN			0x2a	/* 00101010 */
52500fe141SSamu Onkalo #define LP5521_CMD_DIRECT		0x3f	/* 00111111 */
53500fe141SSamu Onkalo #define LP5521_CMD_DISABLED		0x00	/* 00000000 */
54500fe141SSamu Onkalo 
55500fe141SSamu Onkalo /* Registers */
56500fe141SSamu Onkalo #define LP5521_REG_ENABLE		0x00
57500fe141SSamu Onkalo #define LP5521_REG_OP_MODE		0x01
58500fe141SSamu Onkalo #define LP5521_REG_R_PWM		0x02
59500fe141SSamu Onkalo #define LP5521_REG_G_PWM		0x03
60500fe141SSamu Onkalo #define LP5521_REG_B_PWM		0x04
61500fe141SSamu Onkalo #define LP5521_REG_R_CURRENT		0x05
62500fe141SSamu Onkalo #define LP5521_REG_G_CURRENT		0x06
63500fe141SSamu Onkalo #define LP5521_REG_B_CURRENT		0x07
64500fe141SSamu Onkalo #define LP5521_REG_CONFIG		0x08
65500fe141SSamu Onkalo #define LP5521_REG_R_CHANNEL_PC		0x09
66500fe141SSamu Onkalo #define LP5521_REG_G_CHANNEL_PC		0x0A
67500fe141SSamu Onkalo #define LP5521_REG_B_CHANNEL_PC		0x0B
68500fe141SSamu Onkalo #define LP5521_REG_STATUS		0x0C
69500fe141SSamu Onkalo #define LP5521_REG_RESET		0x0D
70500fe141SSamu Onkalo #define LP5521_REG_GPO			0x0E
71500fe141SSamu Onkalo #define LP5521_REG_R_PROG_MEM		0x10
72500fe141SSamu Onkalo #define LP5521_REG_G_PROG_MEM		0x30
73500fe141SSamu Onkalo #define LP5521_REG_B_PROG_MEM		0x50
74500fe141SSamu Onkalo 
75500fe141SSamu Onkalo #define LP5521_PROG_MEM_BASE		LP5521_REG_R_PROG_MEM
76500fe141SSamu Onkalo #define LP5521_PROG_MEM_SIZE		0x20
77500fe141SSamu Onkalo 
78500fe141SSamu Onkalo /* Base register to set LED current */
79500fe141SSamu Onkalo #define LP5521_REG_LED_CURRENT_BASE	LP5521_REG_R_CURRENT
80500fe141SSamu Onkalo 
81500fe141SSamu Onkalo /* Base register to set the brightness */
82500fe141SSamu Onkalo #define LP5521_REG_LED_PWM_BASE		LP5521_REG_R_PWM
83500fe141SSamu Onkalo 
84500fe141SSamu Onkalo /* Bits in ENABLE register */
85500fe141SSamu Onkalo #define LP5521_MASTER_ENABLE		0x40	/* Chip master enable */
86500fe141SSamu Onkalo #define LP5521_LOGARITHMIC_PWM		0x80	/* Logarithmic PWM adjustment */
87500fe141SSamu Onkalo #define LP5521_EXEC_RUN			0x2A
8832a2f747SKim, Milo #define LP5521_ENABLE_DEFAULT	\
8932a2f747SKim, Milo 	(LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
9032a2f747SKim, Milo #define LP5521_ENABLE_RUN_PROGRAM	\
9132a2f747SKim, Milo 	(LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
92500fe141SSamu Onkalo 
93500fe141SSamu Onkalo /* Status */
94500fe141SSamu Onkalo #define LP5521_EXT_CLK_USED		0x08
95500fe141SSamu Onkalo 
96b3c49c05SSrinidhi KASAGAR /* default R channel current register value */
97b3c49c05SSrinidhi KASAGAR #define LP5521_REG_R_CURR_DEFAULT	0xAF
98b3c49c05SSrinidhi KASAGAR 
99011af7bcSKim, Milo /* Pattern Mode */
100011af7bcSKim, Milo #define PATTERN_OFF	0
101011af7bcSKim, Milo 
10248068d5dSMilo(Woogyom) Kim /* Reset register value */
10348068d5dSMilo(Woogyom) Kim #define LP5521_RESET			0xFF
10448068d5dSMilo(Woogyom) Kim 
1059ce7cb17SMilo(Woogyom) Kim /* Program Memory Operations */
1069ce7cb17SMilo(Woogyom) Kim #define LP5521_MODE_R_M			0x30	/* Operation Mode Register */
1079ce7cb17SMilo(Woogyom) Kim #define LP5521_MODE_G_M			0x0C
1089ce7cb17SMilo(Woogyom) Kim #define LP5521_MODE_B_M			0x03
1099ce7cb17SMilo(Woogyom) Kim #define LP5521_LOAD_R			0x10
1109ce7cb17SMilo(Woogyom) Kim #define LP5521_LOAD_G			0x04
1119ce7cb17SMilo(Woogyom) Kim #define LP5521_LOAD_B			0x01
1129ce7cb17SMilo(Woogyom) Kim 
1139ce7cb17SMilo(Woogyom) Kim #define LP5521_R_IS_LOADING(mode)	\
1149ce7cb17SMilo(Woogyom) Kim 	((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
1159ce7cb17SMilo(Woogyom) Kim #define LP5521_G_IS_LOADING(mode)	\
1169ce7cb17SMilo(Woogyom) Kim 	((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
1179ce7cb17SMilo(Woogyom) Kim #define LP5521_B_IS_LOADING(mode)	\
1189ce7cb17SMilo(Woogyom) Kim 	((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
1199ce7cb17SMilo(Woogyom) Kim 
1209ce7cb17SMilo(Woogyom) Kim #define LP5521_EXEC_R_M			0x30	/* Enable Register */
1219ce7cb17SMilo(Woogyom) Kim #define LP5521_EXEC_G_M			0x0C
1229ce7cb17SMilo(Woogyom) Kim #define LP5521_EXEC_B_M			0x03
1239ce7cb17SMilo(Woogyom) Kim #define LP5521_EXEC_M			0x3F
1249ce7cb17SMilo(Woogyom) Kim #define LP5521_RUN_R			0x20
1259ce7cb17SMilo(Woogyom) Kim #define LP5521_RUN_G			0x08
1269ce7cb17SMilo(Woogyom) Kim #define LP5521_RUN_B			0x02
127500fe141SSamu Onkalo 
128500fe141SSamu Onkalo struct lp5521_led {
129500fe141SSamu Onkalo 	int			id;
130500fe141SSamu Onkalo 	u8			chan_nr;
131500fe141SSamu Onkalo 	u8			led_current;
132500fe141SSamu Onkalo 	u8			max_current;
133500fe141SSamu Onkalo 	struct led_classdev	cdev;
134500fe141SSamu Onkalo 	struct work_struct	brightness_work;
135500fe141SSamu Onkalo 	u8			brightness;
136500fe141SSamu Onkalo };
137500fe141SSamu Onkalo 
138500fe141SSamu Onkalo struct lp5521_chip {
139500fe141SSamu Onkalo 	struct lp5521_platform_data *pdata;
140500fe141SSamu Onkalo 	struct mutex		lock; /* Serialize control */
141500fe141SSamu Onkalo 	struct i2c_client	*client;
142500fe141SSamu Onkalo 	struct lp5521_led	leds[LP5521_MAX_LEDS];
143500fe141SSamu Onkalo 	u8			num_channels;
144500fe141SSamu Onkalo 	u8			num_leds;
145500fe141SSamu Onkalo };
146500fe141SSamu Onkalo 
1479ce7cb17SMilo(Woogyom) Kim static inline void lp5521_wait_opmode_done(void)
1489ce7cb17SMilo(Woogyom) Kim {
1499ce7cb17SMilo(Woogyom) Kim 	/* operation mode change needs to be longer than 153 us */
1509ce7cb17SMilo(Woogyom) Kim 	usleep_range(200, 300);
1519ce7cb17SMilo(Woogyom) Kim }
1529ce7cb17SMilo(Woogyom) Kim 
15394482174SMilo(Woogyom) Kim static inline void lp5521_wait_enable_done(void)
15494482174SMilo(Woogyom) Kim {
15594482174SMilo(Woogyom) Kim 	/* it takes more 488 us to update ENABLE register */
15694482174SMilo(Woogyom) Kim 	usleep_range(500, 600);
15794482174SMilo(Woogyom) Kim }
15894482174SMilo(Woogyom) Kim 
159a96bfa13SMilo(Woogyom) Kim static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
160a96bfa13SMilo(Woogyom) Kim {
161a96bfa13SMilo(Woogyom) Kim 	led->led_current = led_current;
162a96bfa13SMilo(Woogyom) Kim 	lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
163a96bfa13SMilo(Woogyom) Kim 		led_current);
164a96bfa13SMilo(Woogyom) Kim }
165a96bfa13SMilo(Woogyom) Kim 
166500fe141SSamu Onkalo static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
167500fe141SSamu Onkalo {
168500fe141SSamu Onkalo 	return i2c_smbus_write_byte_data(client, reg, value);
169500fe141SSamu Onkalo }
170500fe141SSamu Onkalo 
1719ce7cb17SMilo(Woogyom) Kim static void lp5521_load_engine(struct lp55xx_chip *chip)
172500fe141SSamu Onkalo {
1739ce7cb17SMilo(Woogyom) Kim 	enum lp55xx_engine_index idx = chip->engine_idx;
1749ce7cb17SMilo(Woogyom) Kim 	u8 mask[] = {
1759ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_1] = LP5521_MODE_R_M,
1769ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_2] = LP5521_MODE_G_M,
1779ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_3] = LP5521_MODE_B_M,
1789ce7cb17SMilo(Woogyom) Kim 	};
179500fe141SSamu Onkalo 
1809ce7cb17SMilo(Woogyom) Kim 	u8 val[] = {
1819ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_1] = LP5521_LOAD_R,
1829ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_2] = LP5521_LOAD_G,
1839ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_3] = LP5521_LOAD_B,
1849ce7cb17SMilo(Woogyom) Kim 	};
185500fe141SSamu Onkalo 
1869ce7cb17SMilo(Woogyom) Kim 	lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
187500fe141SSamu Onkalo 
1889ce7cb17SMilo(Woogyom) Kim 	lp5521_wait_opmode_done();
189500fe141SSamu Onkalo }
190500fe141SSamu Onkalo 
1919ce7cb17SMilo(Woogyom) Kim static void lp5521_stop_engine(struct lp55xx_chip *chip)
192500fe141SSamu Onkalo {
1939ce7cb17SMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
1949ce7cb17SMilo(Woogyom) Kim 	lp5521_wait_opmode_done();
1959ce7cb17SMilo(Woogyom) Kim }
1969ce7cb17SMilo(Woogyom) Kim 
1979ce7cb17SMilo(Woogyom) Kim static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
1989ce7cb17SMilo(Woogyom) Kim {
199500fe141SSamu Onkalo 	int ret;
200500fe141SSamu Onkalo 	u8 mode;
2019ce7cb17SMilo(Woogyom) Kim 	u8 exec;
202500fe141SSamu Onkalo 
2039ce7cb17SMilo(Woogyom) Kim 	/* stop engine */
2049ce7cb17SMilo(Woogyom) Kim 	if (!start) {
2059ce7cb17SMilo(Woogyom) Kim 		lp5521_stop_engine(chip);
2069ce7cb17SMilo(Woogyom) Kim 		lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
2079ce7cb17SMilo(Woogyom) Kim 		lp5521_wait_opmode_done();
2089ce7cb17SMilo(Woogyom) Kim 		return;
2099ce7cb17SMilo(Woogyom) Kim 	}
2109ce7cb17SMilo(Woogyom) Kim 
2119ce7cb17SMilo(Woogyom) Kim 	/*
2129ce7cb17SMilo(Woogyom) Kim 	 * To run the engine,
2139ce7cb17SMilo(Woogyom) Kim 	 * operation mode and enable register should updated at the same time
2149ce7cb17SMilo(Woogyom) Kim 	 */
2159ce7cb17SMilo(Woogyom) Kim 
2169ce7cb17SMilo(Woogyom) Kim 	ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
2175bc9ad77SDan Carpenter 	if (ret)
2189ce7cb17SMilo(Woogyom) Kim 		return;
2195bc9ad77SDan Carpenter 
2209ce7cb17SMilo(Woogyom) Kim 	ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
2215bc9ad77SDan Carpenter 	if (ret)
2229ce7cb17SMilo(Woogyom) Kim 		return;
223500fe141SSamu Onkalo 
2249ce7cb17SMilo(Woogyom) Kim 	/* change operation mode to RUN only when each engine is loading */
2259ce7cb17SMilo(Woogyom) Kim 	if (LP5521_R_IS_LOADING(mode)) {
2269ce7cb17SMilo(Woogyom) Kim 		mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
2279ce7cb17SMilo(Woogyom) Kim 		exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
2289ce7cb17SMilo(Woogyom) Kim 	}
229500fe141SSamu Onkalo 
2309ce7cb17SMilo(Woogyom) Kim 	if (LP5521_G_IS_LOADING(mode)) {
2319ce7cb17SMilo(Woogyom) Kim 		mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
2329ce7cb17SMilo(Woogyom) Kim 		exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
2339ce7cb17SMilo(Woogyom) Kim 	}
234500fe141SSamu Onkalo 
2359ce7cb17SMilo(Woogyom) Kim 	if (LP5521_B_IS_LOADING(mode)) {
2369ce7cb17SMilo(Woogyom) Kim 		mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
2379ce7cb17SMilo(Woogyom) Kim 		exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
2389ce7cb17SMilo(Woogyom) Kim 	}
2399ce7cb17SMilo(Woogyom) Kim 
2409ce7cb17SMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
2419ce7cb17SMilo(Woogyom) Kim 	lp5521_wait_opmode_done();
2429ce7cb17SMilo(Woogyom) Kim 
2439ce7cb17SMilo(Woogyom) Kim 	lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
2449ce7cb17SMilo(Woogyom) Kim 	lp5521_wait_enable_done();
2459ce7cb17SMilo(Woogyom) Kim }
2469ce7cb17SMilo(Woogyom) Kim 
2479ce7cb17SMilo(Woogyom) Kim static int lp5521_update_program_memory(struct lp55xx_chip *chip,
2489ce7cb17SMilo(Woogyom) Kim 					const u8 *data, size_t size)
2499ce7cb17SMilo(Woogyom) Kim {
2509ce7cb17SMilo(Woogyom) Kim 	enum lp55xx_engine_index idx = chip->engine_idx;
2519ce7cb17SMilo(Woogyom) Kim 	u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
2529ce7cb17SMilo(Woogyom) Kim 	u8 addr[] = {
2539ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
2549ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
2559ce7cb17SMilo(Woogyom) Kim 		[LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
2569ce7cb17SMilo(Woogyom) Kim 	};
2579ce7cb17SMilo(Woogyom) Kim 	unsigned cmd;
2589ce7cb17SMilo(Woogyom) Kim 	char c[3];
2599ce7cb17SMilo(Woogyom) Kim 	int program_size;
2609ce7cb17SMilo(Woogyom) Kim 	int nrchars;
2619ce7cb17SMilo(Woogyom) Kim 	int offset = 0;
2629ce7cb17SMilo(Woogyom) Kim 	int ret;
2639ce7cb17SMilo(Woogyom) Kim 	int i;
2649ce7cb17SMilo(Woogyom) Kim 
2659ce7cb17SMilo(Woogyom) Kim 	/* clear program memory before updating */
2669ce7cb17SMilo(Woogyom) Kim 	for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
2679ce7cb17SMilo(Woogyom) Kim 		lp55xx_write(chip, addr[idx] + i, 0);
2689ce7cb17SMilo(Woogyom) Kim 
2699ce7cb17SMilo(Woogyom) Kim 	i = 0;
2709ce7cb17SMilo(Woogyom) Kim 	while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
2719ce7cb17SMilo(Woogyom) Kim 		/* separate sscanfs because length is working only for %s */
2729ce7cb17SMilo(Woogyom) Kim 		ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
2739ce7cb17SMilo(Woogyom) Kim 		if (ret != 1)
2749ce7cb17SMilo(Woogyom) Kim 			goto err;
2759ce7cb17SMilo(Woogyom) Kim 
2769ce7cb17SMilo(Woogyom) Kim 		ret = sscanf(c, "%2x", &cmd);
2779ce7cb17SMilo(Woogyom) Kim 		if (ret != 1)
2789ce7cb17SMilo(Woogyom) Kim 			goto err;
2799ce7cb17SMilo(Woogyom) Kim 
2809ce7cb17SMilo(Woogyom) Kim 		pattern[i] = (u8)cmd;
2819ce7cb17SMilo(Woogyom) Kim 		offset += nrchars;
2829ce7cb17SMilo(Woogyom) Kim 		i++;
2839ce7cb17SMilo(Woogyom) Kim 	}
2849ce7cb17SMilo(Woogyom) Kim 
2859ce7cb17SMilo(Woogyom) Kim 	/* Each instruction is 16bit long. Check that length is even */
2869ce7cb17SMilo(Woogyom) Kim 	if (i % 2)
2879ce7cb17SMilo(Woogyom) Kim 		goto err;
2889ce7cb17SMilo(Woogyom) Kim 
2899ce7cb17SMilo(Woogyom) Kim 	program_size = i;
2909ce7cb17SMilo(Woogyom) Kim 	for (i = 0; i < program_size; i++)
2919ce7cb17SMilo(Woogyom) Kim 		lp55xx_write(chip, addr[idx] + i, pattern[i]);
2929ce7cb17SMilo(Woogyom) Kim 
2939ce7cb17SMilo(Woogyom) Kim 	return 0;
2949ce7cb17SMilo(Woogyom) Kim 
2959ce7cb17SMilo(Woogyom) Kim err:
2969ce7cb17SMilo(Woogyom) Kim 	dev_err(&chip->cl->dev, "wrong pattern format\n");
2979ce7cb17SMilo(Woogyom) Kim 	return -EINVAL;
2989ce7cb17SMilo(Woogyom) Kim }
2999ce7cb17SMilo(Woogyom) Kim 
3009ce7cb17SMilo(Woogyom) Kim static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
3019ce7cb17SMilo(Woogyom) Kim {
3029ce7cb17SMilo(Woogyom) Kim 	const struct firmware *fw = chip->fw;
3039ce7cb17SMilo(Woogyom) Kim 
3049ce7cb17SMilo(Woogyom) Kim 	if (fw->size > LP5521_PROGRAM_LENGTH) {
3059ce7cb17SMilo(Woogyom) Kim 		dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
3069ce7cb17SMilo(Woogyom) Kim 			fw->size);
3079ce7cb17SMilo(Woogyom) Kim 		return;
3089ce7cb17SMilo(Woogyom) Kim 	}
3099ce7cb17SMilo(Woogyom) Kim 
3109ce7cb17SMilo(Woogyom) Kim 	/*
3119ce7cb17SMilo(Woogyom) Kim 	 * Program momery sequence
3129ce7cb17SMilo(Woogyom) Kim 	 *  1) set engine mode to "LOAD"
3139ce7cb17SMilo(Woogyom) Kim 	 *  2) write firmware data into program memory
3149ce7cb17SMilo(Woogyom) Kim 	 */
3159ce7cb17SMilo(Woogyom) Kim 
3169ce7cb17SMilo(Woogyom) Kim 	lp5521_load_engine(chip);
3179ce7cb17SMilo(Woogyom) Kim 	lp5521_update_program_memory(chip, fw->data, fw->size);
318500fe141SSamu Onkalo }
319500fe141SSamu Onkalo 
320ffbdccdbSMilo(Woogyom) Kim static int lp5521_post_init_device(struct lp55xx_chip *chip)
321500fe141SSamu Onkalo {
322500fe141SSamu Onkalo 	int ret;
32394482174SMilo(Woogyom) Kim 	u8 val;
324500fe141SSamu Onkalo 
32594482174SMilo(Woogyom) Kim 	/*
32694482174SMilo(Woogyom) Kim 	 * Make sure that the chip is reset by reading back the r channel
32794482174SMilo(Woogyom) Kim 	 * current reg. This is dummy read is required on some platforms -
32894482174SMilo(Woogyom) Kim 	 * otherwise further access to the R G B channels in the
32994482174SMilo(Woogyom) Kim 	 * LP5521_REG_ENABLE register will not have any effect - strange!
33094482174SMilo(Woogyom) Kim 	 */
331ffbdccdbSMilo(Woogyom) Kim 	ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
33294482174SMilo(Woogyom) Kim 	if (ret) {
333ffbdccdbSMilo(Woogyom) Kim 		dev_err(&chip->cl->dev, "error in resetting chip\n");
33494482174SMilo(Woogyom) Kim 		return ret;
33594482174SMilo(Woogyom) Kim 	}
33694482174SMilo(Woogyom) Kim 	if (val != LP5521_REG_R_CURR_DEFAULT) {
337ffbdccdbSMilo(Woogyom) Kim 		dev_err(&chip->cl->dev,
33894482174SMilo(Woogyom) Kim 			"unexpected data in register (expected 0x%x got 0x%x)\n",
33994482174SMilo(Woogyom) Kim 			LP5521_REG_R_CURR_DEFAULT, val);
34094482174SMilo(Woogyom) Kim 		ret = -EINVAL;
34194482174SMilo(Woogyom) Kim 		return ret;
34294482174SMilo(Woogyom) Kim 	}
34394482174SMilo(Woogyom) Kim 	usleep_range(10000, 20000);
344500fe141SSamu Onkalo 
345500fe141SSamu Onkalo 	/* Set all PWMs to direct control mode */
346ffbdccdbSMilo(Woogyom) Kim 	ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
347500fe141SSamu Onkalo 
348ffbdccdbSMilo(Woogyom) Kim 	val = chip->pdata->update_config ?
3493b49aacdSKim, Milo 		: (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
350ffbdccdbSMilo(Woogyom) Kim 	ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
35194482174SMilo(Woogyom) Kim 	if (ret)
35294482174SMilo(Woogyom) Kim 		return ret;
353500fe141SSamu Onkalo 
354500fe141SSamu Onkalo 	/* Initialize all channels PWM to zero -> leds off */
355ffbdccdbSMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_R_PWM, 0);
356ffbdccdbSMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_G_PWM, 0);
357ffbdccdbSMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_B_PWM, 0);
358500fe141SSamu Onkalo 
359500fe141SSamu Onkalo 	/* Set engines are set to run state when OP_MODE enables engines */
360ffbdccdbSMilo(Woogyom) Kim 	ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
36194482174SMilo(Woogyom) Kim 	if (ret)
362500fe141SSamu Onkalo 		return ret;
36394482174SMilo(Woogyom) Kim 
36494482174SMilo(Woogyom) Kim 	lp5521_wait_enable_done();
36594482174SMilo(Woogyom) Kim 
36694482174SMilo(Woogyom) Kim 	return 0;
367500fe141SSamu Onkalo }
368500fe141SSamu Onkalo 
369*9ca3bd80SMilo(Woogyom) Kim static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
370500fe141SSamu Onkalo {
371*9ca3bd80SMilo(Woogyom) Kim 	struct lp55xx_platform_data *pdata = chip->pdata;
372500fe141SSamu Onkalo 	int ret;
373500fe141SSamu Onkalo 	u8 status;
374500fe141SSamu Onkalo 
375*9ca3bd80SMilo(Woogyom) Kim 	ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
376500fe141SSamu Onkalo 	if (ret < 0)
377500fe141SSamu Onkalo 		return ret;
378500fe141SSamu Onkalo 
379*9ca3bd80SMilo(Woogyom) Kim 	if (pdata->clock_mode != LP55XX_CLOCK_EXT)
380*9ca3bd80SMilo(Woogyom) Kim 		return 0;
381*9ca3bd80SMilo(Woogyom) Kim 
382500fe141SSamu Onkalo 	/* Check that ext clock is really in use if requested */
383500fe141SSamu Onkalo 	if  ((status & LP5521_EXT_CLK_USED) == 0)
384500fe141SSamu Onkalo 		return -EIO;
385*9ca3bd80SMilo(Woogyom) Kim 
386500fe141SSamu Onkalo 	return 0;
387500fe141SSamu Onkalo }
388500fe141SSamu Onkalo 
389500fe141SSamu Onkalo static void lp5521_led_brightness_work(struct work_struct *work)
390500fe141SSamu Onkalo {
391a6e4679aSMilo(Woogyom) Kim 	struct lp55xx_led *led = container_of(work, struct lp55xx_led,
392500fe141SSamu Onkalo 					      brightness_work);
393a6e4679aSMilo(Woogyom) Kim 	struct lp55xx_chip *chip = led->chip;
394500fe141SSamu Onkalo 
395500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
396a6e4679aSMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
397500fe141SSamu Onkalo 		led->brightness);
398500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
399500fe141SSamu Onkalo }
400500fe141SSamu Onkalo 
401500fe141SSamu Onkalo static ssize_t lp5521_selftest(struct device *dev,
402500fe141SSamu Onkalo 			       struct device_attribute *attr,
403500fe141SSamu Onkalo 			       char *buf)
404500fe141SSamu Onkalo {
405*9ca3bd80SMilo(Woogyom) Kim 	struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
406*9ca3bd80SMilo(Woogyom) Kim 	struct lp55xx_chip *chip = led->chip;
407500fe141SSamu Onkalo 	int ret;
408500fe141SSamu Onkalo 
409500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
410500fe141SSamu Onkalo 	ret = lp5521_run_selftest(chip, buf);
411500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
412500fe141SSamu Onkalo 	return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
413500fe141SSamu Onkalo }
414500fe141SSamu Onkalo 
415011af7bcSKim, Milo static void lp5521_clear_program_memory(struct i2c_client *cl)
416011af7bcSKim, Milo {
417011af7bcSKim, Milo 	int i;
418011af7bcSKim, Milo 	u8 rgb_mem[] = {
419011af7bcSKim, Milo 		LP5521_REG_R_PROG_MEM,
420011af7bcSKim, Milo 		LP5521_REG_G_PROG_MEM,
421011af7bcSKim, Milo 		LP5521_REG_B_PROG_MEM,
422011af7bcSKim, Milo 	};
423011af7bcSKim, Milo 
424011af7bcSKim, Milo 	for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
425011af7bcSKim, Milo 		lp5521_write(cl, rgb_mem[i], 0);
426011af7bcSKim, Milo 		lp5521_write(cl, rgb_mem[i] + 1, 0);
427011af7bcSKim, Milo 	}
428011af7bcSKim, Milo }
429011af7bcSKim, Milo 
430011af7bcSKim, Milo static void lp5521_write_program_memory(struct i2c_client *cl,
431011af7bcSKim, Milo 				u8 base, u8 *rgb, int size)
432011af7bcSKim, Milo {
433011af7bcSKim, Milo 	int i;
434011af7bcSKim, Milo 
435011af7bcSKim, Milo 	if (!rgb || size <= 0)
436011af7bcSKim, Milo 		return;
437011af7bcSKim, Milo 
438011af7bcSKim, Milo 	for (i = 0; i < size; i++)
439011af7bcSKim, Milo 		lp5521_write(cl, base + i, *(rgb + i));
440011af7bcSKim, Milo 
441011af7bcSKim, Milo 	lp5521_write(cl, base + i, 0);
442011af7bcSKim, Milo 	lp5521_write(cl, base + i + 1, 0);
443011af7bcSKim, Milo }
444011af7bcSKim, Milo 
445011af7bcSKim, Milo static inline struct lp5521_led_pattern *lp5521_get_pattern
446011af7bcSKim, Milo 					(struct lp5521_chip *chip, u8 offset)
447011af7bcSKim, Milo {
448011af7bcSKim, Milo 	struct lp5521_led_pattern *ptn;
449011af7bcSKim, Milo 	ptn = chip->pdata->patterns + (offset - 1);
450011af7bcSKim, Milo 	return ptn;
451011af7bcSKim, Milo }
452011af7bcSKim, Milo 
453011af7bcSKim, Milo static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
454011af7bcSKim, Milo {
455011af7bcSKim, Milo 	struct lp5521_led_pattern *ptn;
456011af7bcSKim, Milo 	struct i2c_client *cl = chip->client;
457011af7bcSKim, Milo 	int num_patterns = chip->pdata->num_patterns;
458011af7bcSKim, Milo 
459011af7bcSKim, Milo 	if (mode > num_patterns || !(chip->pdata->patterns))
460011af7bcSKim, Milo 		return;
461011af7bcSKim, Milo 
462011af7bcSKim, Milo 	if (mode == PATTERN_OFF) {
46332a2f747SKim, Milo 		lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
464011af7bcSKim, Milo 		usleep_range(1000, 2000);
465011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
466011af7bcSKim, Milo 	} else {
467011af7bcSKim, Milo 		ptn = lp5521_get_pattern(chip, mode);
468011af7bcSKim, Milo 		if (!ptn)
469011af7bcSKim, Milo 			return;
470011af7bcSKim, Milo 
471011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
472011af7bcSKim, Milo 		usleep_range(1000, 2000);
473011af7bcSKim, Milo 
474011af7bcSKim, Milo 		lp5521_clear_program_memory(cl);
475011af7bcSKim, Milo 
476011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
477011af7bcSKim, Milo 					ptn->r, ptn->size_r);
478011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
479011af7bcSKim, Milo 					ptn->g, ptn->size_g);
480011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
481011af7bcSKim, Milo 					ptn->b, ptn->size_b);
482011af7bcSKim, Milo 
483011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
484011af7bcSKim, Milo 		usleep_range(1000, 2000);
48532a2f747SKim, Milo 		lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
486011af7bcSKim, Milo 	}
487011af7bcSKim, Milo }
488011af7bcSKim, Milo 
489500fe141SSamu Onkalo /* device attributes */
490500fe141SSamu Onkalo static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
491500fe141SSamu Onkalo 
492500fe141SSamu Onkalo static struct attribute *lp5521_attributes[] = {
493500fe141SSamu Onkalo 	&dev_attr_selftest.attr,
494500fe141SSamu Onkalo 	NULL
495500fe141SSamu Onkalo };
496500fe141SSamu Onkalo 
497500fe141SSamu Onkalo static const struct attribute_group lp5521_group = {
498500fe141SSamu Onkalo 	.attrs = lp5521_attributes,
499500fe141SSamu Onkalo };
500500fe141SSamu Onkalo 
501500fe141SSamu Onkalo static void lp5521_unregister_sysfs(struct i2c_client *client)
502500fe141SSamu Onkalo {
503500fe141SSamu Onkalo 	struct device *dev = &client->dev;
504500fe141SSamu Onkalo 
505500fe141SSamu Onkalo 	sysfs_remove_group(&dev->kobj, &lp5521_group);
5061904f83dSMilo(Woogyom) Kim }
5071904f83dSMilo(Woogyom) Kim 
50848068d5dSMilo(Woogyom) Kim /* Chip specific configurations */
50948068d5dSMilo(Woogyom) Kim static struct lp55xx_device_config lp5521_cfg = {
51048068d5dSMilo(Woogyom) Kim 	.reset = {
51148068d5dSMilo(Woogyom) Kim 		.addr = LP5521_REG_RESET,
51248068d5dSMilo(Woogyom) Kim 		.val  = LP5521_RESET,
51348068d5dSMilo(Woogyom) Kim 	},
514e3a700d8SMilo(Woogyom) Kim 	.enable = {
515e3a700d8SMilo(Woogyom) Kim 		.addr = LP5521_REG_ENABLE,
516e3a700d8SMilo(Woogyom) Kim 		.val  = LP5521_ENABLE_DEFAULT,
517e3a700d8SMilo(Woogyom) Kim 	},
5180e202346SMilo(Woogyom) Kim 	.max_channel  = LP5521_MAX_LEDS,
519ffbdccdbSMilo(Woogyom) Kim 	.post_init_device   = lp5521_post_init_device,
520a6e4679aSMilo(Woogyom) Kim 	.brightness_work_fn = lp5521_led_brightness_work,
521a96bfa13SMilo(Woogyom) Kim 	.set_led_current    = lp5521_set_led_current,
5229ce7cb17SMilo(Woogyom) Kim 	.firmware_cb        = lp5521_firmware_loaded,
5239ce7cb17SMilo(Woogyom) Kim 	.run_engine         = lp5521_run_engine,
524e73c0ce6SMilo(Woogyom) Kim 	.dev_attr_group     = &lp5521_group,
52548068d5dSMilo(Woogyom) Kim };
52648068d5dSMilo(Woogyom) Kim 
52798ea1ea2SBill Pemberton static int lp5521_probe(struct i2c_client *client,
528500fe141SSamu Onkalo 			const struct i2c_device_id *id)
529500fe141SSamu Onkalo {
5301904f83dSMilo(Woogyom) Kim 	int ret;
5316a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_chip *chip;
5326a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_led *led;
5336a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_platform_data *pdata = client->dev.platform_data;
534500fe141SSamu Onkalo 
5356a0c9a47SMilo(Woogyom) Kim 	if (!pdata) {
536500fe141SSamu Onkalo 		dev_err(&client->dev, "no platform data\n");
537e430dc00SBryan Wu 		return -EINVAL;
538500fe141SSamu Onkalo 	}
539500fe141SSamu Onkalo 
5406a0c9a47SMilo(Woogyom) Kim 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
5416a0c9a47SMilo(Woogyom) Kim 	if (!chip)
5426a0c9a47SMilo(Woogyom) Kim 		return -ENOMEM;
543500fe141SSamu Onkalo 
5446a0c9a47SMilo(Woogyom) Kim 	led = devm_kzalloc(&client->dev,
5456a0c9a47SMilo(Woogyom) Kim 			sizeof(*led) * pdata->num_channels, GFP_KERNEL);
5466a0c9a47SMilo(Woogyom) Kim 	if (!led)
5476a0c9a47SMilo(Woogyom) Kim 		return -ENOMEM;
5486a0c9a47SMilo(Woogyom) Kim 
5496a0c9a47SMilo(Woogyom) Kim 	chip->cl = client;
5506a0c9a47SMilo(Woogyom) Kim 	chip->pdata = pdata;
55148068d5dSMilo(Woogyom) Kim 	chip->cfg = &lp5521_cfg;
5526a0c9a47SMilo(Woogyom) Kim 
5536a0c9a47SMilo(Woogyom) Kim 	mutex_init(&chip->lock);
5546a0c9a47SMilo(Woogyom) Kim 
5556a0c9a47SMilo(Woogyom) Kim 	i2c_set_clientdata(client, led);
556500fe141SSamu Onkalo 
55722ebeb48SMilo(Woogyom) Kim 	ret = lp55xx_init_device(chip);
558944f7b1dSMilo(Woogyom) Kim 	if (ret)
559f6c64c6fSMilo(Woogyom) Kim 		goto err_init;
560500fe141SSamu Onkalo 
561500fe141SSamu Onkalo 	dev_info(&client->dev, "%s programmable led chip found\n", id->name);
562500fe141SSamu Onkalo 
5639e9b3db1SMilo(Woogyom) Kim 	ret = lp55xx_register_leds(led, chip);
564f6524808SMilo(Woogyom) Kim 	if (ret)
5659e9b3db1SMilo(Woogyom) Kim 		goto err_register_leds;
566500fe141SSamu Onkalo 
567e73c0ce6SMilo(Woogyom) Kim 	ret = lp55xx_register_sysfs(chip);
568500fe141SSamu Onkalo 	if (ret) {
569500fe141SSamu Onkalo 		dev_err(&client->dev, "registering sysfs failed\n");
570e73c0ce6SMilo(Woogyom) Kim 		goto err_register_sysfs;
571500fe141SSamu Onkalo 	}
572e73c0ce6SMilo(Woogyom) Kim 
573e73c0ce6SMilo(Woogyom) Kim 	return 0;
574e73c0ce6SMilo(Woogyom) Kim 
575e73c0ce6SMilo(Woogyom) Kim err_register_sysfs:
576c3a68ebfSMilo(Woogyom) Kim 	lp55xx_unregister_leds(led, chip);
5779e9b3db1SMilo(Woogyom) Kim err_register_leds:
5786ce61762SMilo(Woogyom) Kim 	lp55xx_deinit_device(chip);
579f6c64c6fSMilo(Woogyom) Kim err_init:
580500fe141SSamu Onkalo 	return ret;
581500fe141SSamu Onkalo }
582500fe141SSamu Onkalo 
583678e8a6bSBill Pemberton static int lp5521_remove(struct i2c_client *client)
584500fe141SSamu Onkalo {
585945c7007SMilo(Woogyom) Kim 	struct lp5521_chip *old_chip = i2c_get_clientdata(client);
5866ce61762SMilo(Woogyom) Kim 	struct lp55xx_led *led = i2c_get_clientdata(client);
5876ce61762SMilo(Woogyom) Kim 	struct lp55xx_chip *chip = led->chip;
588500fe141SSamu Onkalo 
589945c7007SMilo(Woogyom) Kim 	lp5521_run_led_pattern(PATTERN_OFF, old_chip);
590500fe141SSamu Onkalo 	lp5521_unregister_sysfs(client);
591500fe141SSamu Onkalo 
592c3a68ebfSMilo(Woogyom) Kim 	lp55xx_unregister_leds(led, chip);
5936ce61762SMilo(Woogyom) Kim 	lp55xx_deinit_device(chip);
594500fe141SSamu Onkalo 
595500fe141SSamu Onkalo 	return 0;
596500fe141SSamu Onkalo }
597500fe141SSamu Onkalo 
598500fe141SSamu Onkalo static const struct i2c_device_id lp5521_id[] = {
599500fe141SSamu Onkalo 	{ "lp5521", 0 }, /* Three channel chip */
600500fe141SSamu Onkalo 	{ }
601500fe141SSamu Onkalo };
602500fe141SSamu Onkalo MODULE_DEVICE_TABLE(i2c, lp5521_id);
603500fe141SSamu Onkalo 
604500fe141SSamu Onkalo static struct i2c_driver lp5521_driver = {
605500fe141SSamu Onkalo 	.driver = {
606500fe141SSamu Onkalo 		.name	= "lp5521",
607500fe141SSamu Onkalo 	},
608500fe141SSamu Onkalo 	.probe		= lp5521_probe,
609df07cf81SBill Pemberton 	.remove		= lp5521_remove,
610500fe141SSamu Onkalo 	.id_table	= lp5521_id,
611500fe141SSamu Onkalo };
612500fe141SSamu Onkalo 
61309a0d183SAxel Lin module_i2c_driver(lp5521_driver);
614500fe141SSamu Onkalo 
615500fe141SSamu Onkalo MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
616500fe141SSamu Onkalo MODULE_DESCRIPTION("LP5521 LED engine");
617500fe141SSamu Onkalo MODULE_LICENSE("GPL v2");
618