blob: 306bcc6ccb927fbe36c9068a14a6d34c20a736ce [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Courtney Cavin93c64f12015-03-12 08:47:08 -07002/* Copyright (c) 2015, Sony Mobile Communications, AB.
Courtney Cavin93c64f12015-03-12 08:47:08 -07003 */
4
Kiran Gundafeeab872019-11-01 11:57:03 +05305#include <linux/delay.h>
6#include <linux/interrupt.h>
7#include <linux/ktime.h>
Courtney Cavin93c64f12015-03-12 08:47:08 -07008#include <linux/kernel.h>
Bjorn Andersson7ddbc242015-07-21 17:44:49 -07009#include <linux/backlight.h>
Courtney Cavin93c64f12015-03-12 08:47:08 -070010#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/of_device.h>
Kiran Gunda775d2ff2019-11-01 11:57:01 +053013#include <linux/of_address.h>
Courtney Cavin93c64f12015-03-12 08:47:08 -070014#include <linux/regmap.h>
15
Bjorn Andersson9d6c2432015-10-26 10:45:08 -070016/* From DT binding */
Kiran Gunda775d2ff2019-11-01 11:57:01 +053017#define WLED_MAX_STRINGS 4
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +053018#define MOD_A 0
19#define MOD_B 1
Kiran Gunda775d2ff2019-11-01 11:57:01 +053020
Kiran Gundabb800a32019-11-01 11:57:00 +053021#define WLED_DEFAULT_BRIGHTNESS 2048
Kiran Gunda8663c182019-11-01 11:57:04 +053022#define WLED_SOFT_START_DLY_US 10000
Kiran Gundabb800a32019-11-01 11:57:00 +053023#define WLED3_SINK_REG_BRIGHT_MAX 0xFFF
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +053024#define WLED5_SINK_REG_BRIGHT_MAX_12B 0xFFF
25#define WLED5_SINK_REG_BRIGHT_MAX_15B 0x7FFF
Courtney Cavin93c64f12015-03-12 08:47:08 -070026
Kiran Gunda03b2b5e2019-11-01 11:57:02 +053027/* WLED3/WLED4 control registers */
Kiran Gunda8663c182019-11-01 11:57:04 +053028#define WLED3_CTRL_REG_FAULT_STATUS 0x08
29#define WLED3_CTRL_REG_ILIM_FAULT_BIT BIT(0)
30#define WLED3_CTRL_REG_OVP_FAULT_BIT BIT(1)
31#define WLED4_CTRL_REG_SC_FAULT_BIT BIT(2)
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +053032#define WLED5_CTRL_REG_OVP_PRE_ALARM_BIT BIT(4)
Kiran Gunda8663c182019-11-01 11:57:04 +053033
34#define WLED3_CTRL_REG_INT_RT_STS 0x10
35#define WLED3_CTRL_REG_OVP_FAULT_STATUS BIT(1)
36
Kiran Gundabb800a32019-11-01 11:57:00 +053037#define WLED3_CTRL_REG_MOD_EN 0x46
Kiran Gundabb800a32019-11-01 11:57:00 +053038#define WLED3_CTRL_REG_MOD_EN_MASK BIT(7)
Kiran Gunda775d2ff2019-11-01 11:57:01 +053039#define WLED3_CTRL_REG_MOD_EN_SHIFT 7
Courtney Cavin93c64f12015-03-12 08:47:08 -070040
Kiran Gunda8663c182019-11-01 11:57:04 +053041#define WLED3_CTRL_REG_FEEDBACK_CONTROL 0x48
42
Kiran Gundabb800a32019-11-01 11:57:00 +053043#define WLED3_CTRL_REG_FREQ 0x4c
Kiran Gunda775d2ff2019-11-01 11:57:01 +053044#define WLED3_CTRL_REG_FREQ_MASK GENMASK(3, 0)
Courtney Cavin93c64f12015-03-12 08:47:08 -070045
Kiran Gundabb800a32019-11-01 11:57:00 +053046#define WLED3_CTRL_REG_OVP 0x4d
Kiran Gunda8663c182019-11-01 11:57:04 +053047#define WLED3_CTRL_REG_OVP_MASK GENMASK(1, 0)
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +053048#define WLED5_CTRL_REG_OVP_MASK GENMASK(3, 0)
Courtney Cavin93c64f12015-03-12 08:47:08 -070049
Kiran Gundabb800a32019-11-01 11:57:00 +053050#define WLED3_CTRL_REG_ILIMIT 0x4e
Kiran Gunda775d2ff2019-11-01 11:57:01 +053051#define WLED3_CTRL_REG_ILIMIT_MASK GENMASK(2, 0)
Courtney Cavin93c64f12015-03-12 08:47:08 -070052
Kiran Gunda03b2b5e2019-11-01 11:57:02 +053053/* WLED3/WLED4 sink registers */
Kiran Gundabb800a32019-11-01 11:57:00 +053054#define WLED3_SINK_REG_SYNC 0x47
Kiran Gundabb800a32019-11-01 11:57:00 +053055#define WLED3_SINK_REG_SYNC_CLEAR 0x00
Courtney Cavin93c64f12015-03-12 08:47:08 -070056
Kiran Gundabb800a32019-11-01 11:57:00 +053057#define WLED3_SINK_REG_CURR_SINK 0x4f
Kiran Gunda775d2ff2019-11-01 11:57:01 +053058#define WLED3_SINK_REG_CURR_SINK_MASK GENMASK(7, 5)
59#define WLED3_SINK_REG_CURR_SINK_SHFT 5
Courtney Cavin93c64f12015-03-12 08:47:08 -070060
Kiran Gunda775d2ff2019-11-01 11:57:01 +053061/* WLED3 specific per-'string' registers below */
62#define WLED3_SINK_REG_BRIGHT(n) (0x40 + n)
Courtney Cavin93c64f12015-03-12 08:47:08 -070063
Kiran Gunda775d2ff2019-11-01 11:57:01 +053064#define WLED3_SINK_REG_STR_MOD_EN(n) (0x60 + (n * 0x10))
Kiran Gundabb800a32019-11-01 11:57:00 +053065#define WLED3_SINK_REG_STR_MOD_MASK BIT(7)
Courtney Cavin93c64f12015-03-12 08:47:08 -070066
Kiran Gunda775d2ff2019-11-01 11:57:01 +053067#define WLED3_SINK_REG_STR_FULL_SCALE_CURR(n) (0x62 + (n * 0x10))
68#define WLED3_SINK_REG_STR_FULL_SCALE_CURR_MASK GENMASK(4, 0)
Courtney Cavin93c64f12015-03-12 08:47:08 -070069
Kiran Gunda775d2ff2019-11-01 11:57:01 +053070#define WLED3_SINK_REG_STR_MOD_SRC(n) (0x63 + (n * 0x10))
71#define WLED3_SINK_REG_STR_MOD_SRC_MASK BIT(0)
Kiran Gundabb800a32019-11-01 11:57:00 +053072#define WLED3_SINK_REG_STR_MOD_SRC_INT 0x00
73#define WLED3_SINK_REG_STR_MOD_SRC_EXT 0x01
Courtney Cavin93c64f12015-03-12 08:47:08 -070074
Kiran Gunda775d2ff2019-11-01 11:57:01 +053075#define WLED3_SINK_REG_STR_CABC(n) (0x66 + (n * 0x10))
Kiran Gundabb800a32019-11-01 11:57:00 +053076#define WLED3_SINK_REG_STR_CABC_MASK BIT(7)
Kiran Gunda775d2ff2019-11-01 11:57:01 +053077
Kiran Gundafeeab872019-11-01 11:57:03 +053078/* WLED4 specific control registers */
79#define WLED4_CTRL_REG_SHORT_PROTECT 0x5e
80#define WLED4_CTRL_REG_SHORT_EN_MASK BIT(7)
81
82#define WLED4_CTRL_REG_SEC_ACCESS 0xd0
83#define WLED4_CTRL_REG_SEC_UNLOCK 0xa5
84
85#define WLED4_CTRL_REG_TEST1 0xe2
86#define WLED4_CTRL_REG_TEST1_EXT_FET_DTEST2 0x09
87
Kiran Gunda03b2b5e2019-11-01 11:57:02 +053088/* WLED4 specific sink registers */
89#define WLED4_SINK_REG_CURR_SINK 0x46
90#define WLED4_SINK_REG_CURR_SINK_MASK GENMASK(7, 4)
91#define WLED4_SINK_REG_CURR_SINK_SHFT 4
92
93/* WLED4 specific per-'string' registers below */
94#define WLED4_SINK_REG_STR_MOD_EN(n) (0x50 + (n * 0x10))
95#define WLED4_SINK_REG_STR_MOD_MASK BIT(7)
96
97#define WLED4_SINK_REG_STR_FULL_SCALE_CURR(n) (0x52 + (n * 0x10))
98#define WLED4_SINK_REG_STR_FULL_SCALE_CURR_MASK GENMASK(3, 0)
99
100#define WLED4_SINK_REG_STR_MOD_SRC(n) (0x53 + (n * 0x10))
101#define WLED4_SINK_REG_STR_MOD_SRC_MASK BIT(0)
102#define WLED4_SINK_REG_STR_MOD_SRC_INT 0x00
103#define WLED4_SINK_REG_STR_MOD_SRC_EXT 0x01
104
105#define WLED4_SINK_REG_STR_CABC(n) (0x56 + (n * 0x10))
106#define WLED4_SINK_REG_STR_CABC_MASK BIT(7)
107
108#define WLED4_SINK_REG_BRIGHT(n) (0x57 + (n * 0x10))
109
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530110/* WLED5 specific control registers */
111#define WLED5_CTRL_REG_OVP_INT_CTL 0x5f
112#define WLED5_CTRL_REG_OVP_INT_TIMER_MASK GENMASK(2, 0)
113
114/* WLED5 specific sink registers */
115#define WLED5_SINK_REG_MOD_A_EN 0x50
116#define WLED5_SINK_REG_MOD_B_EN 0x60
117#define WLED5_SINK_REG_MOD_EN_MASK BIT(7)
118
119#define WLED5_SINK_REG_MOD_A_SRC_SEL 0x51
120#define WLED5_SINK_REG_MOD_B_SRC_SEL 0x61
121#define WLED5_SINK_REG_MOD_SRC_SEL_HIGH 0
122#define WLED5_SINK_REG_MOD_SRC_SEL_EXT 0x03
123#define WLED5_SINK_REG_MOD_SRC_SEL_MASK GENMASK(1, 0)
124
125#define WLED5_SINK_REG_MOD_A_BRIGHTNESS_WIDTH_SEL 0x52
126#define WLED5_SINK_REG_MOD_B_BRIGHTNESS_WIDTH_SEL 0x62
127#define WLED5_SINK_REG_BRIGHTNESS_WIDTH_12B 0
128#define WLED5_SINK_REG_BRIGHTNESS_WIDTH_15B 1
129
130#define WLED5_SINK_REG_MOD_A_BRIGHTNESS_LSB 0x53
131#define WLED5_SINK_REG_MOD_A_BRIGHTNESS_MSB 0x54
132#define WLED5_SINK_REG_MOD_B_BRIGHTNESS_LSB 0x63
133#define WLED5_SINK_REG_MOD_B_BRIGHTNESS_MSB 0x64
134
135#define WLED5_SINK_REG_MOD_SYNC_BIT 0x65
136#define WLED5_SINK_REG_SYNC_MOD_A_BIT BIT(0)
137#define WLED5_SINK_REG_SYNC_MOD_B_BIT BIT(1)
138#define WLED5_SINK_REG_SYNC_MASK GENMASK(1, 0)
139
140/* WLED5 specific per-'string' registers below */
141#define WLED5_SINK_REG_STR_FULL_SCALE_CURR(n) (0x72 + (n * 0x10))
142
143#define WLED5_SINK_REG_STR_SRC_SEL(n) (0x73 + (n * 0x10))
144#define WLED5_SINK_REG_SRC_SEL_MOD_A 0
145#define WLED5_SINK_REG_SRC_SEL_MOD_B 1
146#define WLED5_SINK_REG_SRC_SEL_MASK GENMASK(1, 0)
147
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530148struct wled_var_cfg {
149 const u32 *values;
150 u32 (*fn)(u32);
151 int size;
152};
153
154struct wled_u32_opts {
155 const char *name;
156 u32 *val_ptr;
157 const struct wled_var_cfg *cfg;
158};
159
160struct wled_bool_opts {
161 const char *name;
162 bool *val_ptr;
163};
Courtney Cavin93c64f12015-03-12 08:47:08 -0700164
Kiran Gundabb800a32019-11-01 11:57:00 +0530165struct wled_config {
166 u32 boost_i_limit;
Courtney Cavin93c64f12015-03-12 08:47:08 -0700167 u32 ovp;
168 u32 switch_freq;
169 u32 num_strings;
Kiran Gundabb800a32019-11-01 11:57:00 +0530170 u32 string_i_limit;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530171 u32 enabled_strings[WLED_MAX_STRINGS];
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530172 u32 mod_sel;
173 u32 cabc_sel;
Courtney Cavin93c64f12015-03-12 08:47:08 -0700174 bool cs_out_en;
175 bool ext_gen;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530176 bool cabc;
Kiran Gundafeeab872019-11-01 11:57:03 +0530177 bool external_pfet;
Kiran Gunda8663c182019-11-01 11:57:04 +0530178 bool auto_detection_enabled;
Courtney Cavin93c64f12015-03-12 08:47:08 -0700179};
180
Kiran Gundabb800a32019-11-01 11:57:00 +0530181struct wled {
Bjorn Andersson7ddbc242015-07-21 17:44:49 -0700182 const char *name;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530183 struct device *dev;
Courtney Cavin93c64f12015-03-12 08:47:08 -0700184 struct regmap *regmap;
Kiran Gundafeeab872019-11-01 11:57:03 +0530185 struct mutex lock; /* Lock to avoid race from thread irq handler */
186 ktime_t last_short_event;
Kiran Gunda8663c182019-11-01 11:57:04 +0530187 ktime_t start_ovp_fault_time;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530188 u16 ctrl_addr;
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530189 u16 sink_addr;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530190 u16 max_string_count;
Kiran Gunda8663c182019-11-01 11:57:04 +0530191 u16 auto_detection_ovp_count;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530192 u32 brightness;
193 u32 max_brightness;
Kiran Gundafeeab872019-11-01 11:57:03 +0530194 u32 short_count;
Kiran Gunda8663c182019-11-01 11:57:04 +0530195 u32 auto_detect_count;
Kiran Gundaf16899a2020-04-23 21:03:35 +0530196 u32 version;
Kiran Gundafeeab872019-11-01 11:57:03 +0530197 bool disabled_by_short;
198 bool has_short_detect;
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530199 bool cabc_disabled;
Kiran Gundafeeab872019-11-01 11:57:03 +0530200 int short_irq;
Kiran Gunda8663c182019-11-01 11:57:04 +0530201 int ovp_irq;
Courtney Cavin93c64f12015-03-12 08:47:08 -0700202
Kiran Gundabb800a32019-11-01 11:57:00 +0530203 struct wled_config cfg;
Kiran Gunda8663c182019-11-01 11:57:04 +0530204 struct delayed_work ovp_work;
Kiran Gundaf16899a2020-04-23 21:03:35 +0530205
206 /* Configures the brightness. Applicable for wled3, wled4 and wled5 */
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530207 int (*wled_set_brightness)(struct wled *wled, u16 brightness);
Kiran Gundaf16899a2020-04-23 21:03:35 +0530208
209 /* Configures the cabc register. Applicable for wled4 and wled5 */
210 int (*wled_cabc_config)(struct wled *wled, bool enable);
211
212 /*
213 * Toggles the sync bit for the brightness update to take place.
214 * Applicable for WLED3, WLED4 and WLED5.
215 */
216 int (*wled_sync_toggle)(struct wled *wled);
217
218 /*
219 * Time to wait before checking the OVP status after wled module enable.
220 * Applicable for WLED4 and WLED5.
221 */
222 int (*wled_ovp_delay)(struct wled *wled);
223
224 /*
225 * Determines if the auto string detection is required.
226 * Applicable for WLED4 and WLED5
227 */
228 bool (*wled_auto_detection_required)(struct wled *wled);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700229};
230
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530231static int wled3_set_brightness(struct wled *wled, u16 brightness)
232{
233 int rc, i;
Marijn Suijten0a139352021-11-15 21:34:53 +0100234 __le16 v;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530235
Marijn Suijten0a139352021-11-15 21:34:53 +0100236 v = cpu_to_le16(brightness & WLED3_SINK_REG_BRIGHT_MAX);
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530237
Marijn Suijtenb7002cd2021-11-15 21:34:58 +0100238 for (i = 0; i < wled->cfg.num_strings; ++i) {
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530239 rc = regmap_bulk_write(wled->regmap, wled->ctrl_addr +
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100240 WLED3_SINK_REG_BRIGHT(wled->cfg.enabled_strings[i]),
Marijn Suijten0a139352021-11-15 21:34:53 +0100241 &v, sizeof(v));
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530242 if (rc < 0)
243 return rc;
244 }
245
246 return 0;
247}
248
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530249static int wled4_set_brightness(struct wled *wled, u16 brightness)
250{
251 int rc, i;
252 u16 low_limit = wled->max_brightness * 4 / 1000;
Marijn Suijten0a139352021-11-15 21:34:53 +0100253 __le16 v;
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530254
255 /* WLED4's lower limit of operation is 0.4% */
256 if (brightness > 0 && brightness < low_limit)
257 brightness = low_limit;
258
Marijn Suijten0a139352021-11-15 21:34:53 +0100259 v = cpu_to_le16(brightness & WLED3_SINK_REG_BRIGHT_MAX);
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530260
Marijn Suijtenb7002cd2021-11-15 21:34:58 +0100261 for (i = 0; i < wled->cfg.num_strings; ++i) {
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530262 rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100263 WLED4_SINK_REG_BRIGHT(wled->cfg.enabled_strings[i]),
Marijn Suijten0a139352021-11-15 21:34:53 +0100264 &v, sizeof(v));
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530265 if (rc < 0)
266 return rc;
267 }
268
269 return 0;
270}
271
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530272static int wled5_set_brightness(struct wled *wled, u16 brightness)
273{
274 int rc, offset;
275 u16 low_limit = wled->max_brightness * 1 / 1000;
Marijn Suijten0a139352021-11-15 21:34:53 +0100276 __le16 v;
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530277
278 /* WLED5's lower limit is 0.1% */
279 if (brightness < low_limit)
280 brightness = low_limit;
281
Marijn Suijten0a139352021-11-15 21:34:53 +0100282 v = cpu_to_le16(brightness & WLED5_SINK_REG_BRIGHT_MAX_15B);
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530283
284 offset = (wled->cfg.mod_sel == MOD_A) ?
285 WLED5_SINK_REG_MOD_A_BRIGHTNESS_LSB :
286 WLED5_SINK_REG_MOD_B_BRIGHTNESS_LSB;
287
288 rc = regmap_bulk_write(wled->regmap, wled->sink_addr + offset,
Marijn Suijten0a139352021-11-15 21:34:53 +0100289 &v, sizeof(v));
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530290 return rc;
291}
292
Kiran Gunda8663c182019-11-01 11:57:04 +0530293static void wled_ovp_work(struct work_struct *work)
294{
295 struct wled *wled = container_of(work,
296 struct wled, ovp_work.work);
297 enable_irq(wled->ovp_irq);
298}
299
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530300static int wled_module_enable(struct wled *wled, int val)
301{
302 int rc;
303
Kiran Gundafeeab872019-11-01 11:57:03 +0530304 if (wled->disabled_by_short)
305 return -ENXIO;
306
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530307 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
308 WLED3_CTRL_REG_MOD_EN,
309 WLED3_CTRL_REG_MOD_EN_MASK,
310 val << WLED3_CTRL_REG_MOD_EN_SHIFT);
Kiran Gunda8663c182019-11-01 11:57:04 +0530311 if (rc < 0)
312 return rc;
313
314 if (wled->ovp_irq > 0) {
315 if (val) {
316 /*
317 * The hardware generates a storm of spurious OVP
318 * interrupts during soft start operations. So defer
319 * enabling the IRQ for 10ms to ensure that the
320 * soft start is complete.
321 */
322 schedule_delayed_work(&wled->ovp_work, HZ / 100);
323 } else {
324 if (!cancel_delayed_work_sync(&wled->ovp_work))
325 disable_irq(wled->ovp_irq);
326 }
327 }
328
329 return 0;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530330}
331
Kiran Gundaf16899a2020-04-23 21:03:35 +0530332static int wled3_sync_toggle(struct wled *wled)
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530333{
334 int rc;
335 unsigned int mask = GENMASK(wled->max_string_count - 1, 0);
336
337 rc = regmap_update_bits(wled->regmap,
Obeida Shamouncdfd4c62021-03-14 11:11:10 +0100338 wled->sink_addr + WLED3_SINK_REG_SYNC,
Kiran Gunda5eb622e2021-03-18 18:09:40 +0530339 mask, WLED3_SINK_REG_SYNC_CLEAR);
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530340 if (rc < 0)
341 return rc;
342
343 rc = regmap_update_bits(wled->regmap,
Obeida Shamouncdfd4c62021-03-14 11:11:10 +0100344 wled->sink_addr + WLED3_SINK_REG_SYNC,
Kiran Gunda5eb622e2021-03-18 18:09:40 +0530345 mask, mask);
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530346
347 return rc;
348}
349
Kiran Gunda4d6e9cd2021-03-18 18:09:39 +0530350static int wled5_mod_sync_toggle(struct wled *wled)
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530351{
352 int rc;
353 u8 val;
354
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530355 rc = regmap_update_bits(wled->regmap,
356 wled->sink_addr + WLED5_SINK_REG_MOD_SYNC_BIT,
Kiran Gunda5eb622e2021-03-18 18:09:40 +0530357 WLED5_SINK_REG_SYNC_MASK, 0);
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530358 if (rc < 0)
359 return rc;
360
Kiran Gunda5eb622e2021-03-18 18:09:40 +0530361 val = (wled->cfg.mod_sel == MOD_A) ? WLED5_SINK_REG_SYNC_MOD_A_BIT :
362 WLED5_SINK_REG_SYNC_MOD_B_BIT;
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530363 return regmap_update_bits(wled->regmap,
364 wled->sink_addr + WLED5_SINK_REG_MOD_SYNC_BIT,
Kiran Gunda5eb622e2021-03-18 18:09:40 +0530365 WLED5_SINK_REG_SYNC_MASK, val);
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530366}
367
Kiran Gundaf16899a2020-04-23 21:03:35 +0530368static int wled_ovp_fault_status(struct wled *wled, bool *fault_set)
369{
370 int rc;
371 u32 int_rt_sts, fault_sts;
372
373 *fault_set = false;
374 rc = regmap_read(wled->regmap,
375 wled->ctrl_addr + WLED3_CTRL_REG_INT_RT_STS,
376 &int_rt_sts);
377 if (rc < 0) {
378 dev_err(wled->dev, "Failed to read INT_RT_STS rc=%d\n", rc);
379 return rc;
380 }
381
382 rc = regmap_read(wled->regmap,
383 wled->ctrl_addr + WLED3_CTRL_REG_FAULT_STATUS,
384 &fault_sts);
385 if (rc < 0) {
386 dev_err(wled->dev, "Failed to read FAULT_STATUS rc=%d\n", rc);
387 return rc;
388 }
389
390 if (int_rt_sts & WLED3_CTRL_REG_OVP_FAULT_STATUS)
391 *fault_set = true;
392
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530393 if (wled->version == 4 && (fault_sts & WLED3_CTRL_REG_OVP_FAULT_BIT))
394 *fault_set = true;
395
396 if (wled->version == 5 && (fault_sts & (WLED3_CTRL_REG_OVP_FAULT_BIT |
397 WLED5_CTRL_REG_OVP_PRE_ALARM_BIT)))
Kiran Gundaf16899a2020-04-23 21:03:35 +0530398 *fault_set = true;
399
400 if (*fault_set)
401 dev_dbg(wled->dev, "WLED OVP fault detected, int_rt_sts=0x%x fault_sts=0x%x\n",
402 int_rt_sts, fault_sts);
403
404 return rc;
405}
406
407static int wled4_ovp_delay(struct wled *wled)
408{
409 return WLED_SOFT_START_DLY_US;
410}
411
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530412static int wled5_ovp_delay(struct wled *wled)
413{
414 int rc, delay_us;
415 u32 val;
416 u8 ovp_timer_ms[8] = {1, 2, 4, 8, 12, 16, 20, 24};
417
418 /* For WLED5, get the delay based on OVP timer */
419 rc = regmap_read(wled->regmap, wled->ctrl_addr +
420 WLED5_CTRL_REG_OVP_INT_CTL, &val);
421 if (rc < 0)
422 delay_us =
423 ovp_timer_ms[val & WLED5_CTRL_REG_OVP_INT_TIMER_MASK] * 1000;
424 else
425 delay_us = 2 * WLED_SOFT_START_DLY_US;
426
427 dev_dbg(wled->dev, "delay_time_us: %d\n", delay_us);
428
429 return delay_us;
430}
431
Kiran Gundabb800a32019-11-01 11:57:00 +0530432static int wled_update_status(struct backlight_device *bl)
Courtney Cavin93c64f12015-03-12 08:47:08 -0700433{
Kiran Gundabb800a32019-11-01 11:57:00 +0530434 struct wled *wled = bl_get_data(bl);
Sam Ravnborg51d53e52020-07-19 10:07:41 +0200435 u16 brightness = backlight_get_brightness(bl);
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530436 int rc = 0;
Courtney Cavin93c64f12015-03-12 08:47:08 -0700437
Kiran Gundafeeab872019-11-01 11:57:03 +0530438 mutex_lock(&wled->lock);
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530439 if (brightness) {
440 rc = wled->wled_set_brightness(wled, brightness);
441 if (rc < 0) {
442 dev_err(wled->dev, "wled failed to set brightness rc:%d\n",
443 rc);
Kiran Gundafeeab872019-11-01 11:57:03 +0530444 goto unlock_mutex;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530445 }
446
Kiran Gunda4d6e9cd2021-03-18 18:09:39 +0530447 if (wled->version < 5) {
448 rc = wled->wled_sync_toggle(wled);
449 if (rc < 0) {
450 dev_err(wled->dev, "wled sync failed rc:%d\n", rc);
451 goto unlock_mutex;
452 }
453 } else {
454 /*
455 * For WLED5 toggling the MOD_SYNC_BIT updates the
456 * brightness
457 */
458 rc = wled5_mod_sync_toggle(wled);
459 if (rc < 0) {
460 dev_err(wled->dev, "wled mod sync failed rc:%d\n",
461 rc);
462 goto unlock_mutex;
463 }
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530464 }
Courtney Cavin93c64f12015-03-12 08:47:08 -0700465 }
466
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530467 if (!!brightness != !!wled->brightness) {
468 rc = wled_module_enable(wled, !!brightness);
469 if (rc < 0) {
470 dev_err(wled->dev, "wled enable failed rc:%d\n", rc);
Kiran Gundafeeab872019-11-01 11:57:03 +0530471 goto unlock_mutex;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530472 }
473 }
Courtney Cavin93c64f12015-03-12 08:47:08 -0700474
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530475 wled->brightness = brightness;
476
Kiran Gundafeeab872019-11-01 11:57:03 +0530477unlock_mutex:
478 mutex_unlock(&wled->lock);
479
Courtney Cavin93c64f12015-03-12 08:47:08 -0700480 return rc;
481}
482
Kiran Gundaf16899a2020-04-23 21:03:35 +0530483static int wled4_cabc_config(struct wled *wled, bool enable)
484{
485 int i, j, rc;
486 u8 val;
487
488 for (i = 0; i < wled->cfg.num_strings; i++) {
489 j = wled->cfg.enabled_strings[i];
490
491 val = enable ? WLED4_SINK_REG_STR_CABC_MASK : 0;
492 rc = regmap_update_bits(wled->regmap, wled->sink_addr +
493 WLED4_SINK_REG_STR_CABC(j),
494 WLED4_SINK_REG_STR_CABC_MASK, val);
495 if (rc < 0)
496 return rc;
497 }
498
499 return 0;
500}
501
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530502static int wled5_cabc_config(struct wled *wled, bool enable)
503{
504 int rc, offset;
505 u8 reg;
506
507 if (wled->cabc_disabled)
508 return 0;
509
510 reg = enable ? wled->cfg.cabc_sel : 0;
511 offset = (wled->cfg.mod_sel == MOD_A) ? WLED5_SINK_REG_MOD_A_SRC_SEL :
512 WLED5_SINK_REG_MOD_B_SRC_SEL;
513
514 rc = regmap_update_bits(wled->regmap, wled->sink_addr + offset,
515 WLED5_SINK_REG_MOD_SRC_SEL_MASK, reg);
516 if (rc < 0) {
517 pr_err("Error in configuring CABC rc=%d\n", rc);
518 return rc;
519 }
520
521 if (!wled->cfg.cabc_sel)
522 wled->cabc_disabled = true;
523
524 return 0;
525}
526
Kiran Gundafeeab872019-11-01 11:57:03 +0530527#define WLED_SHORT_DLY_MS 20
528#define WLED_SHORT_CNT_MAX 5
529#define WLED_SHORT_RESET_CNT_DLY_US USEC_PER_SEC
530
531static irqreturn_t wled_short_irq_handler(int irq, void *_wled)
532{
533 struct wled *wled = _wled;
534 int rc;
535 s64 elapsed_time;
536
537 wled->short_count++;
538 mutex_lock(&wled->lock);
539 rc = wled_module_enable(wled, false);
540 if (rc < 0) {
541 dev_err(wled->dev, "wled disable failed rc:%d\n", rc);
542 goto unlock_mutex;
543 }
544
545 elapsed_time = ktime_us_delta(ktime_get(),
546 wled->last_short_event);
547 if (elapsed_time > WLED_SHORT_RESET_CNT_DLY_US)
548 wled->short_count = 1;
549
550 if (wled->short_count > WLED_SHORT_CNT_MAX) {
Colin Ian King102a1b32019-11-12 09:30:25 +0000551 dev_err(wled->dev, "Short triggered %d times, disabling WLED forever!\n",
Kiran Gundafeeab872019-11-01 11:57:03 +0530552 wled->short_count);
553 wled->disabled_by_short = true;
554 goto unlock_mutex;
555 }
556
557 wled->last_short_event = ktime_get();
558
559 msleep(WLED_SHORT_DLY_MS);
560 rc = wled_module_enable(wled, true);
561 if (rc < 0)
562 dev_err(wled->dev, "wled enable failed rc:%d\n", rc);
563
564unlock_mutex:
565 mutex_unlock(&wled->lock);
566
567 return IRQ_HANDLED;
568}
569
Kiran Gunda8663c182019-11-01 11:57:04 +0530570#define AUTO_DETECT_BRIGHTNESS 200
571
572static void wled_auto_string_detection(struct wled *wled)
573{
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100574 int rc = 0, i, j, delay_time_us;
Kiran Gundaf16899a2020-04-23 21:03:35 +0530575 u32 sink_config = 0;
Kiran Gunda8663c182019-11-01 11:57:04 +0530576 u8 sink_test = 0, sink_valid = 0, val;
Kiran Gundaf16899a2020-04-23 21:03:35 +0530577 bool fault_set;
Kiran Gunda8663c182019-11-01 11:57:04 +0530578
579 /* Read configured sink configuration */
580 rc = regmap_read(wled->regmap, wled->sink_addr +
581 WLED4_SINK_REG_CURR_SINK, &sink_config);
582 if (rc < 0) {
583 dev_err(wled->dev, "Failed to read SINK configuration rc=%d\n",
584 rc);
585 goto failed_detect;
586 }
587
588 /* Disable the module before starting detection */
589 rc = regmap_update_bits(wled->regmap,
590 wled->ctrl_addr + WLED3_CTRL_REG_MOD_EN,
591 WLED3_CTRL_REG_MOD_EN_MASK, 0);
592 if (rc < 0) {
593 dev_err(wled->dev, "Failed to disable WLED module rc=%d\n", rc);
594 goto failed_detect;
595 }
596
597 /* Set low brightness across all sinks */
598 rc = wled4_set_brightness(wled, AUTO_DETECT_BRIGHTNESS);
599 if (rc < 0) {
600 dev_err(wled->dev, "Failed to set brightness for auto detection rc=%d\n",
601 rc);
602 goto failed_detect;
603 }
604
605 if (wled->cfg.cabc) {
Kiran Gundaf16899a2020-04-23 21:03:35 +0530606 rc = wled->wled_cabc_config(wled, false);
607 if (rc < 0)
608 goto failed_detect;
Kiran Gunda8663c182019-11-01 11:57:04 +0530609 }
610
611 /* Disable all sinks */
612 rc = regmap_write(wled->regmap,
613 wled->sink_addr + WLED4_SINK_REG_CURR_SINK, 0);
614 if (rc < 0) {
615 dev_err(wled->dev, "Failed to disable all sinks rc=%d\n", rc);
616 goto failed_detect;
617 }
618
619 /* Iterate through the strings one by one */
620 for (i = 0; i < wled->cfg.num_strings; i++) {
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100621 j = wled->cfg.enabled_strings[i];
622 sink_test = BIT((WLED4_SINK_REG_CURR_SINK_SHFT + j));
Kiran Gunda8663c182019-11-01 11:57:04 +0530623
624 /* Enable feedback control */
625 rc = regmap_write(wled->regmap, wled->ctrl_addr +
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100626 WLED3_CTRL_REG_FEEDBACK_CONTROL, j + 1);
Kiran Gunda8663c182019-11-01 11:57:04 +0530627 if (rc < 0) {
628 dev_err(wled->dev, "Failed to enable feedback for SINK %d rc = %d\n",
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100629 j + 1, rc);
Kiran Gunda8663c182019-11-01 11:57:04 +0530630 goto failed_detect;
631 }
632
633 /* Enable the sink */
634 rc = regmap_write(wled->regmap, wled->sink_addr +
635 WLED4_SINK_REG_CURR_SINK, sink_test);
636 if (rc < 0) {
637 dev_err(wled->dev, "Failed to configure SINK %d rc=%d\n",
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100638 j + 1, rc);
Kiran Gunda8663c182019-11-01 11:57:04 +0530639 goto failed_detect;
640 }
641
642 /* Enable the module */
643 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
644 WLED3_CTRL_REG_MOD_EN,
645 WLED3_CTRL_REG_MOD_EN_MASK,
646 WLED3_CTRL_REG_MOD_EN_MASK);
647 if (rc < 0) {
648 dev_err(wled->dev, "Failed to enable WLED module rc=%d\n",
649 rc);
650 goto failed_detect;
651 }
652
Kiran Gundaf16899a2020-04-23 21:03:35 +0530653 delay_time_us = wled->wled_ovp_delay(wled);
654 usleep_range(delay_time_us, delay_time_us + 1000);
Kiran Gunda8663c182019-11-01 11:57:04 +0530655
Kiran Gundaf16899a2020-04-23 21:03:35 +0530656 rc = wled_ovp_fault_status(wled, &fault_set);
Kiran Gunda8663c182019-11-01 11:57:04 +0530657 if (rc < 0) {
Kiran Gundaf16899a2020-04-23 21:03:35 +0530658 dev_err(wled->dev, "Error in getting OVP fault_sts, rc=%d\n",
Kiran Gunda8663c182019-11-01 11:57:04 +0530659 rc);
660 goto failed_detect;
661 }
662
Kiran Gundaf16899a2020-04-23 21:03:35 +0530663 if (fault_set)
Kiran Gunda8663c182019-11-01 11:57:04 +0530664 dev_dbg(wled->dev, "WLED OVP fault detected with SINK %d\n",
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100665 j + 1);
Kiran Gunda8663c182019-11-01 11:57:04 +0530666 else
667 sink_valid |= sink_test;
668
669 /* Disable the module */
670 rc = regmap_update_bits(wled->regmap,
671 wled->ctrl_addr + WLED3_CTRL_REG_MOD_EN,
672 WLED3_CTRL_REG_MOD_EN_MASK, 0);
673 if (rc < 0) {
674 dev_err(wled->dev, "Failed to disable WLED module rc=%d\n",
675 rc);
676 goto failed_detect;
677 }
678 }
679
680 if (!sink_valid) {
681 dev_err(wled->dev, "No valid WLED sinks found\n");
682 wled->disabled_by_short = true;
683 goto failed_detect;
684 }
685
686 if (sink_valid != sink_config) {
687 dev_warn(wled->dev, "%x is not a valid sink configuration - using %x instead\n",
688 sink_config, sink_valid);
689 sink_config = sink_valid;
690 }
691
692 /* Write the new sink configuration */
693 rc = regmap_write(wled->regmap,
694 wled->sink_addr + WLED4_SINK_REG_CURR_SINK,
695 sink_config);
696 if (rc < 0) {
697 dev_err(wled->dev, "Failed to reconfigure the default sink rc=%d\n",
698 rc);
699 goto failed_detect;
700 }
701
702 /* Enable valid sinks */
Kiran Gundaf16899a2020-04-23 21:03:35 +0530703 if (wled->version == 4) {
704 for (i = 0; i < wled->cfg.num_strings; i++) {
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100705 j = wled->cfg.enabled_strings[i];
Kiran Gundaf16899a2020-04-23 21:03:35 +0530706 if (sink_config &
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100707 BIT(WLED4_SINK_REG_CURR_SINK_SHFT + j))
Kiran Gundaf16899a2020-04-23 21:03:35 +0530708 val = WLED4_SINK_REG_STR_MOD_MASK;
709 else
710 /* Disable modulator_en for unused sink */
711 val = 0;
712
713 rc = regmap_write(wled->regmap, wled->sink_addr +
Marijn Suijtenec961cf2021-11-15 21:34:59 +0100714 WLED4_SINK_REG_STR_MOD_EN(j), val);
Kiran Gundaf16899a2020-04-23 21:03:35 +0530715 if (rc < 0) {
716 dev_err(wled->dev, "Failed to configure MODULATOR_EN rc=%d\n",
717 rc);
Kiran Gunda8663c182019-11-01 11:57:04 +0530718 goto failed_detect;
Kiran Gundaf16899a2020-04-23 21:03:35 +0530719 }
Kiran Gunda8663c182019-11-01 11:57:04 +0530720 }
721 }
722
Kiran Gundaf16899a2020-04-23 21:03:35 +0530723 /* Enable CABC */
724 rc = wled->wled_cabc_config(wled, true);
725 if (rc < 0)
726 goto failed_detect;
727
Kiran Gunda8663c182019-11-01 11:57:04 +0530728 /* Restore the feedback setting */
729 rc = regmap_write(wled->regmap,
730 wled->ctrl_addr + WLED3_CTRL_REG_FEEDBACK_CONTROL, 0);
731 if (rc < 0) {
732 dev_err(wled->dev, "Failed to restore feedback setting rc=%d\n",
733 rc);
734 goto failed_detect;
735 }
736
737 /* Restore brightness */
738 rc = wled4_set_brightness(wled, wled->brightness);
739 if (rc < 0) {
740 dev_err(wled->dev, "Failed to set brightness after auto detection rc=%d\n",
741 rc);
742 goto failed_detect;
743 }
744
745 rc = regmap_update_bits(wled->regmap,
746 wled->ctrl_addr + WLED3_CTRL_REG_MOD_EN,
747 WLED3_CTRL_REG_MOD_EN_MASK,
748 WLED3_CTRL_REG_MOD_EN_MASK);
749 if (rc < 0) {
750 dev_err(wled->dev, "Failed to enable WLED module rc=%d\n", rc);
751 goto failed_detect;
752 }
753
754failed_detect:
755 return;
756}
757
758#define WLED_AUTO_DETECT_OVP_COUNT 5
759#define WLED_AUTO_DETECT_CNT_DLY_US USEC_PER_SEC
Kiran Gundaf16899a2020-04-23 21:03:35 +0530760
761static bool wled4_auto_detection_required(struct wled *wled)
Kiran Gunda8663c182019-11-01 11:57:04 +0530762{
763 s64 elapsed_time_us;
764
765 if (!wled->cfg.auto_detection_enabled)
766 return false;
767
768 /*
769 * Check if the OVP fault was an occasional one
770 * or if it's firing continuously, the latter qualifies
771 * for an auto-detection check.
772 */
773 if (!wled->auto_detection_ovp_count) {
774 wled->start_ovp_fault_time = ktime_get();
775 wled->auto_detection_ovp_count++;
776 } else {
777 elapsed_time_us = ktime_us_delta(ktime_get(),
778 wled->start_ovp_fault_time);
779 if (elapsed_time_us > WLED_AUTO_DETECT_CNT_DLY_US)
780 wled->auto_detection_ovp_count = 0;
781 else
782 wled->auto_detection_ovp_count++;
783
784 if (wled->auto_detection_ovp_count >=
785 WLED_AUTO_DETECT_OVP_COUNT) {
786 wled->auto_detection_ovp_count = 0;
787 return true;
788 }
789 }
790
791 return false;
792}
793
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +0530794static bool wled5_auto_detection_required(struct wled *wled)
795{
796 if (!wled->cfg.auto_detection_enabled)
797 return false;
798
799 /*
800 * Unlike WLED4, WLED5 has OVP fault density interrupt configuration
801 * i.e. to count the number of OVP alarms for a certain duration before
802 * triggering OVP fault interrupt. By default, number of OVP fault
803 * events counted before an interrupt is fired is 32 and the time
804 * interval is 12 ms. If we see one OVP fault interrupt, then that
805 * should qualify for a real OVP fault condition to run auto detection
806 * algorithm.
807 */
808 return true;
809}
810
Kiran Gunda8663c182019-11-01 11:57:04 +0530811static int wled_auto_detection_at_init(struct wled *wled)
812{
813 int rc;
Kiran Gundaf16899a2020-04-23 21:03:35 +0530814 bool fault_set;
Kiran Gunda8663c182019-11-01 11:57:04 +0530815
816 if (!wled->cfg.auto_detection_enabled)
817 return 0;
818
Kiran Gundaf16899a2020-04-23 21:03:35 +0530819 rc = wled_ovp_fault_status(wled, &fault_set);
Kiran Gunda8663c182019-11-01 11:57:04 +0530820 if (rc < 0) {
Kiran Gundaf16899a2020-04-23 21:03:35 +0530821 dev_err(wled->dev, "Error in getting OVP fault_sts, rc=%d\n",
822 rc);
Kiran Gunda8663c182019-11-01 11:57:04 +0530823 return rc;
824 }
825
Kiran Gundaf16899a2020-04-23 21:03:35 +0530826 if (fault_set) {
Kiran Gunda8663c182019-11-01 11:57:04 +0530827 mutex_lock(&wled->lock);
828 wled_auto_string_detection(wled);
829 mutex_unlock(&wled->lock);
830 }
831
832 return rc;
833}
834
835static irqreturn_t wled_ovp_irq_handler(int irq, void *_wled)
836{
837 struct wled *wled = _wled;
838 int rc;
839 u32 int_sts, fault_sts;
840
841 rc = regmap_read(wled->regmap,
842 wled->ctrl_addr + WLED3_CTRL_REG_INT_RT_STS, &int_sts);
843 if (rc < 0) {
844 dev_err(wled->dev, "Error in reading WLED3_INT_RT_STS rc=%d\n",
845 rc);
846 return IRQ_HANDLED;
847 }
848
849 rc = regmap_read(wled->regmap, wled->ctrl_addr +
850 WLED3_CTRL_REG_FAULT_STATUS, &fault_sts);
851 if (rc < 0) {
852 dev_err(wled->dev, "Error in reading WLED_FAULT_STATUS rc=%d\n",
853 rc);
854 return IRQ_HANDLED;
855 }
856
857 if (fault_sts & (WLED3_CTRL_REG_OVP_FAULT_BIT |
858 WLED3_CTRL_REG_ILIM_FAULT_BIT))
859 dev_dbg(wled->dev, "WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
860 int_sts, fault_sts);
861
862 if (fault_sts & WLED3_CTRL_REG_OVP_FAULT_BIT) {
Kiran Gundaf16899a2020-04-23 21:03:35 +0530863 if (wled->wled_auto_detection_required(wled)) {
Kiran Gunda8663c182019-11-01 11:57:04 +0530864 mutex_lock(&wled->lock);
865 wled_auto_string_detection(wled);
866 mutex_unlock(&wled->lock);
867 }
868 }
869
870 return IRQ_HANDLED;
871}
872
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530873static int wled3_setup(struct wled *wled)
Courtney Cavin93c64f12015-03-12 08:47:08 -0700874{
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530875 u16 addr;
876 u8 sink_en = 0;
877 int rc, i, j;
Courtney Cavin93c64f12015-03-12 08:47:08 -0700878
879 rc = regmap_update_bits(wled->regmap,
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530880 wled->ctrl_addr + WLED3_CTRL_REG_OVP,
881 WLED3_CTRL_REG_OVP_MASK, wled->cfg.ovp);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700882 if (rc)
883 return rc;
884
885 rc = regmap_update_bits(wled->regmap,
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530886 wled->ctrl_addr + WLED3_CTRL_REG_ILIMIT,
887 WLED3_CTRL_REG_ILIMIT_MASK,
888 wled->cfg.boost_i_limit);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700889 if (rc)
890 return rc;
891
892 rc = regmap_update_bits(wled->regmap,
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530893 wled->ctrl_addr + WLED3_CTRL_REG_FREQ,
894 WLED3_CTRL_REG_FREQ_MASK,
895 wled->cfg.switch_freq);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700896 if (rc)
897 return rc;
898
Courtney Cavin93c64f12015-03-12 08:47:08 -0700899 for (i = 0; i < wled->cfg.num_strings; ++i) {
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530900 j = wled->cfg.enabled_strings[i];
901 addr = wled->ctrl_addr + WLED3_SINK_REG_STR_MOD_EN(j);
902 rc = regmap_update_bits(wled->regmap, addr,
903 WLED3_SINK_REG_STR_MOD_MASK,
904 WLED3_SINK_REG_STR_MOD_MASK);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700905 if (rc)
906 return rc;
907
908 if (wled->cfg.ext_gen) {
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530909 addr = wled->ctrl_addr + WLED3_SINK_REG_STR_MOD_SRC(j);
910 rc = regmap_update_bits(wled->regmap, addr,
911 WLED3_SINK_REG_STR_MOD_SRC_MASK,
912 WLED3_SINK_REG_STR_MOD_SRC_EXT);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700913 if (rc)
914 return rc;
915 }
916
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530917 addr = wled->ctrl_addr + WLED3_SINK_REG_STR_FULL_SCALE_CURR(j);
918 rc = regmap_update_bits(wled->regmap, addr,
919 WLED3_SINK_REG_STR_FULL_SCALE_CURR_MASK,
920 wled->cfg.string_i_limit);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700921 if (rc)
922 return rc;
923
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530924 addr = wled->ctrl_addr + WLED3_SINK_REG_STR_CABC(j);
925 rc = regmap_update_bits(wled->regmap, addr,
926 WLED3_SINK_REG_STR_CABC_MASK,
927 wled->cfg.cabc ?
928 WLED3_SINK_REG_STR_CABC_MASK : 0);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700929 if (rc)
930 return rc;
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530931
932 sink_en |= BIT(j + WLED3_SINK_REG_CURR_SINK_SHFT);
Courtney Cavin93c64f12015-03-12 08:47:08 -0700933 }
934
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530935 rc = regmap_update_bits(wled->regmap,
936 wled->ctrl_addr + WLED3_SINK_REG_CURR_SINK,
937 WLED3_SINK_REG_CURR_SINK_MASK, sink_en);
938 if (rc)
939 return rc;
940
Courtney Cavin93c64f12015-03-12 08:47:08 -0700941 return 0;
942}
943
Kiran Gundabb800a32019-11-01 11:57:00 +0530944static const struct wled_config wled3_config_defaults = {
945 .boost_i_limit = 3,
946 .string_i_limit = 20,
Courtney Cavin93c64f12015-03-12 08:47:08 -0700947 .ovp = 2,
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530948 .num_strings = 3,
Courtney Cavin93c64f12015-03-12 08:47:08 -0700949 .switch_freq = 5,
Courtney Cavin93c64f12015-03-12 08:47:08 -0700950 .cs_out_en = false,
951 .ext_gen = false,
Kiran Gunda775d2ff2019-11-01 11:57:01 +0530952 .cabc = false,
Marijn Suijten96571482021-11-15 21:34:56 +0100953 .enabled_strings = {0, 1, 2},
Courtney Cavin93c64f12015-03-12 08:47:08 -0700954};
955
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530956static int wled4_setup(struct wled *wled)
957{
958 int rc, temp, i, j;
959 u16 addr;
960 u8 sink_en = 0;
Kiran Gundafeeab872019-11-01 11:57:03 +0530961 u32 sink_cfg;
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530962
963 rc = regmap_update_bits(wled->regmap,
964 wled->ctrl_addr + WLED3_CTRL_REG_OVP,
965 WLED3_CTRL_REG_OVP_MASK, wled->cfg.ovp);
966 if (rc < 0)
967 return rc;
968
969 rc = regmap_update_bits(wled->regmap,
970 wled->ctrl_addr + WLED3_CTRL_REG_ILIMIT,
971 WLED3_CTRL_REG_ILIMIT_MASK,
972 wled->cfg.boost_i_limit);
973 if (rc < 0)
974 return rc;
975
976 rc = regmap_update_bits(wled->regmap,
977 wled->ctrl_addr + WLED3_CTRL_REG_FREQ,
978 WLED3_CTRL_REG_FREQ_MASK,
979 wled->cfg.switch_freq);
980 if (rc < 0)
981 return rc;
982
Kiran Gundafeeab872019-11-01 11:57:03 +0530983 if (wled->cfg.external_pfet) {
984 /* Unlock the secure register access */
985 rc = regmap_write(wled->regmap, wled->ctrl_addr +
986 WLED4_CTRL_REG_SEC_ACCESS,
987 WLED4_CTRL_REG_SEC_UNLOCK);
988 if (rc < 0)
989 return rc;
990
991 rc = regmap_write(wled->regmap,
992 wled->ctrl_addr + WLED4_CTRL_REG_TEST1,
993 WLED4_CTRL_REG_TEST1_EXT_FET_DTEST2);
994 if (rc < 0)
995 return rc;
996 }
997
Kiran Gunda03b2b5e2019-11-01 11:57:02 +0530998 rc = regmap_read(wled->regmap, wled->sink_addr +
999 WLED4_SINK_REG_CURR_SINK, &sink_cfg);
1000 if (rc < 0)
1001 return rc;
1002
1003 for (i = 0; i < wled->cfg.num_strings; i++) {
1004 j = wled->cfg.enabled_strings[i];
1005 temp = j + WLED4_SINK_REG_CURR_SINK_SHFT;
1006 sink_en |= 1 << temp;
1007 }
1008
Kiran Gunda8663c182019-11-01 11:57:04 +05301009 if (sink_cfg == sink_en) {
1010 rc = wled_auto_detection_at_init(wled);
1011 return rc;
1012 }
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301013
1014 rc = regmap_update_bits(wled->regmap,
1015 wled->sink_addr + WLED4_SINK_REG_CURR_SINK,
1016 WLED4_SINK_REG_CURR_SINK_MASK, 0);
1017 if (rc < 0)
1018 return rc;
1019
1020 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
1021 WLED3_CTRL_REG_MOD_EN,
1022 WLED3_CTRL_REG_MOD_EN_MASK, 0);
1023 if (rc < 0)
1024 return rc;
1025
1026 /* Per sink/string configuration */
1027 for (i = 0; i < wled->cfg.num_strings; i++) {
1028 j = wled->cfg.enabled_strings[i];
1029
1030 addr = wled->sink_addr +
1031 WLED4_SINK_REG_STR_MOD_EN(j);
1032 rc = regmap_update_bits(wled->regmap, addr,
1033 WLED4_SINK_REG_STR_MOD_MASK,
1034 WLED4_SINK_REG_STR_MOD_MASK);
1035 if (rc < 0)
1036 return rc;
1037
1038 addr = wled->sink_addr +
1039 WLED4_SINK_REG_STR_FULL_SCALE_CURR(j);
1040 rc = regmap_update_bits(wled->regmap, addr,
1041 WLED4_SINK_REG_STR_FULL_SCALE_CURR_MASK,
1042 wled->cfg.string_i_limit);
1043 if (rc < 0)
1044 return rc;
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301045 }
1046
Kiran Gundaf16899a2020-04-23 21:03:35 +05301047 rc = wled4_cabc_config(wled, wled->cfg.cabc);
1048 if (rc < 0)
1049 return rc;
1050
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301051 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
1052 WLED3_CTRL_REG_MOD_EN,
1053 WLED3_CTRL_REG_MOD_EN_MASK,
1054 WLED3_CTRL_REG_MOD_EN_MASK);
1055 if (rc < 0)
1056 return rc;
1057
1058 rc = regmap_update_bits(wled->regmap,
1059 wled->sink_addr + WLED4_SINK_REG_CURR_SINK,
1060 WLED4_SINK_REG_CURR_SINK_MASK, sink_en);
1061 if (rc < 0)
1062 return rc;
1063
Kiran Gundaf16899a2020-04-23 21:03:35 +05301064 rc = wled->wled_sync_toggle(wled);
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301065 if (rc < 0) {
1066 dev_err(wled->dev, "Failed to toggle sync reg rc:%d\n", rc);
1067 return rc;
1068 }
1069
Kiran Gunda8663c182019-11-01 11:57:04 +05301070 rc = wled_auto_detection_at_init(wled);
1071
1072 return rc;
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301073}
1074
1075static const struct wled_config wled4_config_defaults = {
1076 .boost_i_limit = 4,
1077 .string_i_limit = 10,
1078 .ovp = 1,
1079 .num_strings = 4,
1080 .switch_freq = 11,
1081 .cabc = false,
Kiran Gundafeeab872019-11-01 11:57:03 +05301082 .external_pfet = false,
Kiran Gunda8663c182019-11-01 11:57:04 +05301083 .auto_detection_enabled = false,
Marijn Suijtenc70aefd2021-11-15 21:34:57 +01001084 .enabled_strings = {0, 1, 2, 3},
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301085};
1086
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301087static int wled5_setup(struct wled *wled)
1088{
1089 int rc, temp, i, j, offset;
1090 u8 sink_en = 0;
1091 u16 addr;
1092 u32 val;
1093
1094 rc = regmap_update_bits(wled->regmap,
1095 wled->ctrl_addr + WLED3_CTRL_REG_OVP,
1096 WLED5_CTRL_REG_OVP_MASK, wled->cfg.ovp);
1097 if (rc < 0)
1098 return rc;
1099
1100 rc = regmap_update_bits(wled->regmap,
1101 wled->ctrl_addr + WLED3_CTRL_REG_ILIMIT,
1102 WLED3_CTRL_REG_ILIMIT_MASK,
1103 wled->cfg.boost_i_limit);
1104 if (rc < 0)
1105 return rc;
1106
1107 rc = regmap_update_bits(wled->regmap,
1108 wled->ctrl_addr + WLED3_CTRL_REG_FREQ,
1109 WLED3_CTRL_REG_FREQ_MASK,
1110 wled->cfg.switch_freq);
1111 if (rc < 0)
1112 return rc;
1113
1114 /* Per sink/string configuration */
1115 for (i = 0; i < wled->cfg.num_strings; ++i) {
1116 j = wled->cfg.enabled_strings[i];
1117 addr = wled->sink_addr +
1118 WLED4_SINK_REG_STR_FULL_SCALE_CURR(j);
1119 rc = regmap_update_bits(wled->regmap, addr,
1120 WLED4_SINK_REG_STR_FULL_SCALE_CURR_MASK,
1121 wled->cfg.string_i_limit);
1122 if (rc < 0)
1123 return rc;
1124
1125 addr = wled->sink_addr + WLED5_SINK_REG_STR_SRC_SEL(j);
1126 rc = regmap_update_bits(wled->regmap, addr,
1127 WLED5_SINK_REG_SRC_SEL_MASK,
1128 wled->cfg.mod_sel == MOD_A ?
1129 WLED5_SINK_REG_SRC_SEL_MOD_A :
1130 WLED5_SINK_REG_SRC_SEL_MOD_B);
1131
1132 temp = j + WLED4_SINK_REG_CURR_SINK_SHFT;
1133 sink_en |= 1 << temp;
1134 }
1135
1136 rc = wled5_cabc_config(wled, wled->cfg.cabc_sel ? true : false);
1137 if (rc < 0)
1138 return rc;
1139
1140 /* Enable one of the modulators A or B based on mod_sel */
1141 addr = wled->sink_addr + WLED5_SINK_REG_MOD_A_EN;
1142 val = (wled->cfg.mod_sel == MOD_A) ? WLED5_SINK_REG_MOD_EN_MASK : 0;
1143 rc = regmap_update_bits(wled->regmap, addr,
1144 WLED5_SINK_REG_MOD_EN_MASK, val);
1145 if (rc < 0)
1146 return rc;
1147
1148 addr = wled->sink_addr + WLED5_SINK_REG_MOD_B_EN;
1149 val = (wled->cfg.mod_sel == MOD_B) ? WLED5_SINK_REG_MOD_EN_MASK : 0;
1150 rc = regmap_update_bits(wled->regmap, addr,
1151 WLED5_SINK_REG_MOD_EN_MASK, val);
1152 if (rc < 0)
1153 return rc;
1154
1155 offset = (wled->cfg.mod_sel == MOD_A) ?
1156 WLED5_SINK_REG_MOD_A_BRIGHTNESS_WIDTH_SEL :
1157 WLED5_SINK_REG_MOD_B_BRIGHTNESS_WIDTH_SEL;
1158
1159 addr = wled->sink_addr + offset;
1160 val = (wled->max_brightness == WLED5_SINK_REG_BRIGHT_MAX_15B) ?
1161 WLED5_SINK_REG_BRIGHTNESS_WIDTH_15B :
1162 WLED5_SINK_REG_BRIGHTNESS_WIDTH_12B;
1163 rc = regmap_write(wled->regmap, addr, val);
1164 if (rc < 0)
1165 return rc;
1166
1167 rc = regmap_update_bits(wled->regmap,
1168 wled->sink_addr + WLED4_SINK_REG_CURR_SINK,
1169 WLED4_SINK_REG_CURR_SINK_MASK, sink_en);
1170 if (rc < 0)
1171 return rc;
1172
1173 /* This updates only FSC configuration in WLED5 */
1174 rc = wled->wled_sync_toggle(wled);
1175 if (rc < 0) {
1176 pr_err("Failed to toggle sync reg rc:%d\n", rc);
1177 return rc;
1178 }
1179
1180 rc = wled_auto_detection_at_init(wled);
1181 if (rc < 0)
1182 return rc;
1183
1184 return 0;
1185}
1186
1187static const struct wled_config wled5_config_defaults = {
1188 .boost_i_limit = 5,
1189 .string_i_limit = 10,
1190 .ovp = 4,
1191 .num_strings = 4,
1192 .switch_freq = 11,
1193 .mod_sel = 0,
1194 .cabc_sel = 0,
1195 .cabc = false,
1196 .external_pfet = false,
1197 .auto_detection_enabled = false,
Marijn Suijtenc70aefd2021-11-15 21:34:57 +01001198 .enabled_strings = {0, 1, 2, 3},
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301199};
1200
Kiran Gundabb800a32019-11-01 11:57:00 +05301201static const u32 wled3_boost_i_limit_values[] = {
Courtney Cavin93c64f12015-03-12 08:47:08 -07001202 105, 385, 525, 805, 980, 1260, 1400, 1680,
1203};
1204
Kiran Gundabb800a32019-11-01 11:57:00 +05301205static const struct wled_var_cfg wled3_boost_i_limit_cfg = {
1206 .values = wled3_boost_i_limit_values,
1207 .size = ARRAY_SIZE(wled3_boost_i_limit_values),
Courtney Cavin93c64f12015-03-12 08:47:08 -07001208};
1209
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301210static const u32 wled4_boost_i_limit_values[] = {
1211 105, 280, 450, 620, 970, 1150, 1300, 1500,
1212};
1213
1214static const struct wled_var_cfg wled4_boost_i_limit_cfg = {
1215 .values = wled4_boost_i_limit_values,
1216 .size = ARRAY_SIZE(wled4_boost_i_limit_values),
1217};
1218
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301219static inline u32 wled5_boost_i_limit_values_fn(u32 idx)
1220{
1221 return 525 + (idx * 175);
1222}
1223
1224static const struct wled_var_cfg wled5_boost_i_limit_cfg = {
1225 .fn = wled5_boost_i_limit_values_fn,
1226 .size = 8,
1227};
1228
Kiran Gundabb800a32019-11-01 11:57:00 +05301229static const u32 wled3_ovp_values[] = {
Courtney Cavin93c64f12015-03-12 08:47:08 -07001230 35, 32, 29, 27,
1231};
1232
Kiran Gundabb800a32019-11-01 11:57:00 +05301233static const struct wled_var_cfg wled3_ovp_cfg = {
1234 .values = wled3_ovp_values,
1235 .size = ARRAY_SIZE(wled3_ovp_values),
Courtney Cavin93c64f12015-03-12 08:47:08 -07001236};
1237
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301238static const u32 wled4_ovp_values[] = {
1239 31100, 29600, 19600, 18100,
1240};
1241
1242static const struct wled_var_cfg wled4_ovp_cfg = {
1243 .values = wled4_ovp_values,
1244 .size = ARRAY_SIZE(wled4_ovp_values),
1245};
1246
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301247static inline u32 wled5_ovp_values_fn(u32 idx)
1248{
1249 /*
1250 * 0000 - 38.5 V
1251 * 0001 - 37 V ..
1252 * 1111 - 16 V
1253 */
1254 return 38500 - (idx * 1500);
1255}
1256
1257static const struct wled_var_cfg wled5_ovp_cfg = {
1258 .fn = wled5_ovp_values_fn,
1259 .size = 16,
1260};
1261
Kiran Gundabb800a32019-11-01 11:57:00 +05301262static u32 wled3_switch_freq_values_fn(u32 idx)
Courtney Cavin93c64f12015-03-12 08:47:08 -07001263{
1264 return 19200 / (2 * (1 + idx));
1265}
1266
Kiran Gundabb800a32019-11-01 11:57:00 +05301267static const struct wled_var_cfg wled3_switch_freq_cfg = {
1268 .fn = wled3_switch_freq_values_fn,
Courtney Cavin93c64f12015-03-12 08:47:08 -07001269 .size = 16,
1270};
1271
Kiran Gundabb800a32019-11-01 11:57:00 +05301272static const struct wled_var_cfg wled3_string_i_limit_cfg = {
Courtney Cavin93c64f12015-03-12 08:47:08 -07001273 .size = 26,
1274};
1275
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301276static const u32 wled4_string_i_limit_values[] = {
1277 0, 2500, 5000, 7500, 10000, 12500, 15000, 17500, 20000,
1278 22500, 25000, 27500, 30000,
1279};
1280
1281static const struct wled_var_cfg wled4_string_i_limit_cfg = {
1282 .values = wled4_string_i_limit_values,
1283 .size = ARRAY_SIZE(wled4_string_i_limit_values),
1284};
1285
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301286static const struct wled_var_cfg wled5_mod_sel_cfg = {
1287 .size = 2,
1288};
1289
1290static const struct wled_var_cfg wled5_cabc_sel_cfg = {
1291 .size = 4,
1292};
1293
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301294static u32 wled_values(const struct wled_var_cfg *cfg, u32 idx)
Courtney Cavin93c64f12015-03-12 08:47:08 -07001295{
1296 if (idx >= cfg->size)
1297 return UINT_MAX;
1298 if (cfg->fn)
1299 return cfg->fn(idx);
1300 if (cfg->values)
1301 return cfg->values[idx];
1302 return idx;
1303}
1304
Kiran Gundaf16899a2020-04-23 21:03:35 +05301305static int wled_configure(struct wled *wled)
Courtney Cavin93c64f12015-03-12 08:47:08 -07001306{
Kiran Gundabb800a32019-11-01 11:57:00 +05301307 struct wled_config *cfg = &wled->cfg;
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301308 struct device *dev = wled->dev;
1309 const __be32 *prop_addr;
Chen Zhou7af43a72020-01-22 09:32:40 +08001310 u32 size, val, c;
1311 int rc, i, j, string_len;
Courtney Cavin93c64f12015-03-12 08:47:08 -07001312
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301313 const struct wled_u32_opts *u32_opts = NULL;
1314 const struct wled_u32_opts wled3_opts[] = {
Courtney Cavin93c64f12015-03-12 08:47:08 -07001315 {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301316 .name = "qcom,current-boost-limit",
1317 .val_ptr = &cfg->boost_i_limit,
Kiran Gundabb800a32019-11-01 11:57:00 +05301318 .cfg = &wled3_boost_i_limit_cfg,
Courtney Cavin93c64f12015-03-12 08:47:08 -07001319 },
1320 {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301321 .name = "qcom,current-limit",
1322 .val_ptr = &cfg->string_i_limit,
Kiran Gundabb800a32019-11-01 11:57:00 +05301323 .cfg = &wled3_string_i_limit_cfg,
Courtney Cavin93c64f12015-03-12 08:47:08 -07001324 },
1325 {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301326 .name = "qcom,ovp",
1327 .val_ptr = &cfg->ovp,
Kiran Gundabb800a32019-11-01 11:57:00 +05301328 .cfg = &wled3_ovp_cfg,
Courtney Cavin93c64f12015-03-12 08:47:08 -07001329 },
1330 {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301331 .name = "qcom,switching-freq",
1332 .val_ptr = &cfg->switch_freq,
Kiran Gundabb800a32019-11-01 11:57:00 +05301333 .cfg = &wled3_switch_freq_cfg,
Courtney Cavin93c64f12015-03-12 08:47:08 -07001334 },
Courtney Cavin93c64f12015-03-12 08:47:08 -07001335 };
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301336
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301337 const struct wled_u32_opts wled4_opts[] = {
1338 {
1339 .name = "qcom,current-boost-limit",
1340 .val_ptr = &cfg->boost_i_limit,
1341 .cfg = &wled4_boost_i_limit_cfg,
1342 },
1343 {
1344 .name = "qcom,current-limit-microamp",
1345 .val_ptr = &cfg->string_i_limit,
1346 .cfg = &wled4_string_i_limit_cfg,
1347 },
1348 {
1349 .name = "qcom,ovp-millivolt",
1350 .val_ptr = &cfg->ovp,
1351 .cfg = &wled4_ovp_cfg,
1352 },
1353 {
1354 .name = "qcom,switching-freq",
1355 .val_ptr = &cfg->switch_freq,
1356 .cfg = &wled3_switch_freq_cfg,
1357 },
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301358 };
1359
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301360 const struct wled_u32_opts wled5_opts[] = {
1361 {
1362 .name = "qcom,current-boost-limit",
1363 .val_ptr = &cfg->boost_i_limit,
1364 .cfg = &wled5_boost_i_limit_cfg,
1365 },
1366 {
1367 .name = "qcom,current-limit-microamp",
1368 .val_ptr = &cfg->string_i_limit,
1369 .cfg = &wled4_string_i_limit_cfg,
1370 },
1371 {
1372 .name = "qcom,ovp-millivolt",
1373 .val_ptr = &cfg->ovp,
1374 .cfg = &wled5_ovp_cfg,
1375 },
1376 {
1377 .name = "qcom,switching-freq",
1378 .val_ptr = &cfg->switch_freq,
1379 .cfg = &wled3_switch_freq_cfg,
1380 },
1381 {
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301382 .name = "qcom,modulator-sel",
1383 .val_ptr = &cfg->mod_sel,
1384 .cfg = &wled5_mod_sel_cfg,
1385 },
1386 {
1387 .name = "qcom,cabc-sel",
1388 .val_ptr = &cfg->cabc_sel,
1389 .cfg = &wled5_cabc_sel_cfg,
1390 },
1391 };
1392
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301393 const struct wled_bool_opts bool_opts[] = {
Courtney Cavin93c64f12015-03-12 08:47:08 -07001394 { "qcom,cs-out", &cfg->cs_out_en, },
1395 { "qcom,ext-gen", &cfg->ext_gen, },
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301396 { "qcom,cabc", &cfg->cabc, },
Kiran Gundafeeab872019-11-01 11:57:03 +05301397 { "qcom,external-pfet", &cfg->external_pfet, },
Kiran Gunda8663c182019-11-01 11:57:04 +05301398 { "qcom,auto-string-detection", &cfg->auto_detection_enabled, },
Courtney Cavin93c64f12015-03-12 08:47:08 -07001399 };
1400
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301401 prop_addr = of_get_address(dev->of_node, 0, NULL, NULL);
1402 if (!prop_addr) {
1403 dev_err(wled->dev, "invalid IO resources\n");
1404 return -EINVAL;
Courtney Cavin93c64f12015-03-12 08:47:08 -07001405 }
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301406 wled->ctrl_addr = be32_to_cpu(*prop_addr);
Courtney Cavin93c64f12015-03-12 08:47:08 -07001407
Bjorn Andersson7ddbc242015-07-21 17:44:49 -07001408 rc = of_property_read_string(dev->of_node, "label", &wled->name);
Courtney Cavin93c64f12015-03-12 08:47:08 -07001409 if (rc)
Rob Herringf86b7752018-08-27 20:03:42 -05001410 wled->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
Courtney Cavin93c64f12015-03-12 08:47:08 -07001411
Kiran Gundaf16899a2020-04-23 21:03:35 +05301412 switch (wled->version) {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301413 case 3:
1414 u32_opts = wled3_opts;
1415 size = ARRAY_SIZE(wled3_opts);
1416 *cfg = wled3_config_defaults;
1417 wled->wled_set_brightness = wled3_set_brightness;
Kiran Gundaf16899a2020-04-23 21:03:35 +05301418 wled->wled_sync_toggle = wled3_sync_toggle;
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301419 wled->max_string_count = 3;
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301420 wled->sink_addr = wled->ctrl_addr;
1421 break;
1422
1423 case 4:
1424 u32_opts = wled4_opts;
1425 size = ARRAY_SIZE(wled4_opts);
1426 *cfg = wled4_config_defaults;
1427 wled->wled_set_brightness = wled4_set_brightness;
Kiran Gundaf16899a2020-04-23 21:03:35 +05301428 wled->wled_sync_toggle = wled3_sync_toggle;
1429 wled->wled_cabc_config = wled4_cabc_config;
1430 wled->wled_ovp_delay = wled4_ovp_delay;
1431 wled->wled_auto_detection_required =
1432 wled4_auto_detection_required;
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301433 wled->max_string_count = 4;
1434
1435 prop_addr = of_get_address(dev->of_node, 1, NULL, NULL);
1436 if (!prop_addr) {
1437 dev_err(wled->dev, "invalid IO resources\n");
1438 return -EINVAL;
1439 }
1440 wled->sink_addr = be32_to_cpu(*prop_addr);
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301441 break;
1442
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301443 case 5:
1444 u32_opts = wled5_opts;
1445 size = ARRAY_SIZE(wled5_opts);
1446 *cfg = wled5_config_defaults;
1447 wled->wled_set_brightness = wled5_set_brightness;
Kiran Gunda4d6e9cd2021-03-18 18:09:39 +05301448 wled->wled_sync_toggle = wled3_sync_toggle;
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301449 wled->wled_cabc_config = wled5_cabc_config;
1450 wled->wled_ovp_delay = wled5_ovp_delay;
1451 wled->wled_auto_detection_required =
1452 wled5_auto_detection_required;
1453 wled->max_string_count = 4;
1454
1455 prop_addr = of_get_address(dev->of_node, 1, NULL, NULL);
1456 if (!prop_addr) {
1457 dev_err(wled->dev, "invalid IO resources\n");
1458 return -EINVAL;
1459 }
1460 wled->sink_addr = be32_to_cpu(*prop_addr);
1461 break;
1462
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301463 default:
1464 dev_err(wled->dev, "Invalid WLED version\n");
1465 return -EINVAL;
1466 }
1467
1468 for (i = 0; i < size; ++i) {
Courtney Cavin93c64f12015-03-12 08:47:08 -07001469 rc = of_property_read_u32(dev->of_node, u32_opts[i].name, &val);
1470 if (rc == -EINVAL) {
1471 continue;
1472 } else if (rc) {
1473 dev_err(dev, "error reading '%s'\n", u32_opts[i].name);
1474 return rc;
1475 }
1476
1477 c = UINT_MAX;
1478 for (j = 0; c != val; j++) {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301479 c = wled_values(u32_opts[i].cfg, j);
Courtney Cavin93c64f12015-03-12 08:47:08 -07001480 if (c == UINT_MAX) {
1481 dev_err(dev, "invalid value for '%s'\n",
1482 u32_opts[i].name);
1483 return -EINVAL;
1484 }
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301485
1486 if (c == val)
1487 break;
Courtney Cavin93c64f12015-03-12 08:47:08 -07001488 }
1489
1490 dev_dbg(dev, "'%s' = %u\n", u32_opts[i].name, c);
1491 *u32_opts[i].val_ptr = j;
1492 }
1493
1494 for (i = 0; i < ARRAY_SIZE(bool_opts); ++i) {
1495 if (of_property_read_bool(dev->of_node, bool_opts[i].name))
1496 *bool_opts[i].val_ptr = true;
1497 }
1498
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301499 string_len = of_property_count_elems_of_size(dev->of_node,
1500 "qcom,enabled-strings",
1501 sizeof(u32));
Marijn Suijtenc05b21e2021-11-15 21:34:51 +01001502 if (string_len > 0) {
1503 if (string_len > wled->max_string_count) {
1504 dev_err(dev, "Cannot have more than %d strings\n",
1505 wled->max_string_count);
1506 return -EINVAL;
1507 }
1508
Marijn Suijtene29e24b2021-11-15 21:34:52 +01001509 rc = of_property_read_u32_array(dev->of_node,
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301510 "qcom,enabled-strings",
1511 wled->cfg.enabled_strings,
Marijn Suijtene29e24b2021-11-15 21:34:52 +01001512 string_len);
1513 if (rc) {
1514 dev_err(dev, "Failed to read %d elements from qcom,enabled-strings: %d\n",
1515 string_len, rc);
1516 return rc;
1517 }
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301518
Marijn Suijtenc05b21e2021-11-15 21:34:51 +01001519 for (i = 0; i < string_len; ++i) {
1520 if (wled->cfg.enabled_strings[i] >= wled->max_string_count) {
1521 dev_err(dev,
1522 "qcom,enabled-strings index %d at %d is out of bounds\n",
1523 wled->cfg.enabled_strings[i], i);
1524 return -EINVAL;
1525 }
1526 }
Marijn Suijten2b4b4962021-11-15 21:34:55 +01001527
1528 cfg->num_strings = string_len;
Marijn Suijtenc05b21e2021-11-15 21:34:51 +01001529 }
1530
Marijn Suijten5ada78b2021-11-15 21:34:54 +01001531 rc = of_property_read_u32(dev->of_node, "qcom,num-strings", &val);
1532 if (!rc) {
1533 if (val < 1 || val > wled->max_string_count) {
1534 dev_err(dev, "qcom,num-strings must be between 1 and %d\n",
1535 wled->max_string_count);
1536 return -EINVAL;
1537 }
1538
Marijn Suijten2b4b4962021-11-15 21:34:55 +01001539 if (string_len > 0) {
1540 dev_warn(dev, "Only one of qcom,num-strings or qcom,enabled-strings"
1541 " should be set\n");
1542 if (val > string_len) {
1543 dev_err(dev, "qcom,num-strings exceeds qcom,enabled-strings\n");
1544 return -EINVAL;
1545 }
Marijn Suijten5ada78b2021-11-15 21:34:54 +01001546 }
1547
1548 cfg->num_strings = val;
1549 }
1550
Courtney Cavin93c64f12015-03-12 08:47:08 -07001551 return 0;
1552}
1553
Kiran Gundafeeab872019-11-01 11:57:03 +05301554static int wled_configure_short_irq(struct wled *wled,
1555 struct platform_device *pdev)
1556{
1557 int rc;
1558
1559 if (!wled->has_short_detect)
1560 return 0;
1561
1562 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
1563 WLED4_CTRL_REG_SHORT_PROTECT,
1564 WLED4_CTRL_REG_SHORT_EN_MASK,
1565 WLED4_CTRL_REG_SHORT_EN_MASK);
1566 if (rc < 0)
1567 return rc;
1568
1569 wled->short_irq = platform_get_irq_byname(pdev, "short");
1570 if (wled->short_irq < 0) {
1571 dev_dbg(&pdev->dev, "short irq is not used\n");
1572 return 0;
1573 }
1574
1575 rc = devm_request_threaded_irq(wled->dev, wled->short_irq,
1576 NULL, wled_short_irq_handler,
1577 IRQF_ONESHOT,
1578 "wled_short_irq", wled);
1579 if (rc < 0)
1580 dev_err(wled->dev, "Unable to request short_irq (err:%d)\n",
1581 rc);
1582
1583 return rc;
1584}
1585
Kiran Gunda8663c182019-11-01 11:57:04 +05301586static int wled_configure_ovp_irq(struct wled *wled,
1587 struct platform_device *pdev)
1588{
1589 int rc;
1590 u32 val;
1591
1592 wled->ovp_irq = platform_get_irq_byname(pdev, "ovp");
1593 if (wled->ovp_irq < 0) {
1594 dev_dbg(&pdev->dev, "OVP IRQ not found - disabling automatic string detection\n");
1595 return 0;
1596 }
1597
1598 rc = devm_request_threaded_irq(wled->dev, wled->ovp_irq, NULL,
1599 wled_ovp_irq_handler, IRQF_ONESHOT,
1600 "wled_ovp_irq", wled);
1601 if (rc < 0) {
1602 dev_err(wled->dev, "Unable to request ovp_irq (err:%d)\n",
1603 rc);
1604 wled->ovp_irq = 0;
1605 return 0;
1606 }
1607
1608 rc = regmap_read(wled->regmap, wled->ctrl_addr +
1609 WLED3_CTRL_REG_MOD_EN, &val);
1610 if (rc < 0)
1611 return rc;
1612
1613 /* Keep OVP irq disabled until module is enabled */
1614 if (!(val & WLED3_CTRL_REG_MOD_EN_MASK))
1615 disable_irq(wled->ovp_irq);
1616
1617 return 0;
1618}
1619
Kiran Gundabb800a32019-11-01 11:57:00 +05301620static const struct backlight_ops wled_ops = {
1621 .update_status = wled_update_status,
Bjorn Andersson7ddbc242015-07-21 17:44:49 -07001622};
1623
Kiran Gundabb800a32019-11-01 11:57:00 +05301624static int wled_probe(struct platform_device *pdev)
Courtney Cavin93c64f12015-03-12 08:47:08 -07001625{
Bjorn Andersson7ddbc242015-07-21 17:44:49 -07001626 struct backlight_properties props;
1627 struct backlight_device *bl;
Kiran Gundabb800a32019-11-01 11:57:00 +05301628 struct wled *wled;
Courtney Cavin93c64f12015-03-12 08:47:08 -07001629 struct regmap *regmap;
Bjorn Andersson9d6c2432015-10-26 10:45:08 -07001630 u32 val;
Courtney Cavin93c64f12015-03-12 08:47:08 -07001631 int rc;
1632
1633 regmap = dev_get_regmap(pdev->dev.parent, NULL);
1634 if (!regmap) {
1635 dev_err(&pdev->dev, "Unable to get regmap\n");
1636 return -EINVAL;
1637 }
1638
1639 wled = devm_kzalloc(&pdev->dev, sizeof(*wled), GFP_KERNEL);
1640 if (!wled)
1641 return -ENOMEM;
1642
1643 wled->regmap = regmap;
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301644 wled->dev = &pdev->dev;
Courtney Cavin93c64f12015-03-12 08:47:08 -07001645
Kiran Gundaf16899a2020-04-23 21:03:35 +05301646 wled->version = (uintptr_t)of_device_get_match_data(&pdev->dev);
1647 if (!wled->version) {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301648 dev_err(&pdev->dev, "Unknown device version\n");
1649 return -ENODEV;
1650 }
1651
Kiran Gundafeeab872019-11-01 11:57:03 +05301652 mutex_init(&wled->lock);
Kiran Gundaf16899a2020-04-23 21:03:35 +05301653 rc = wled_configure(wled);
Courtney Cavin93c64f12015-03-12 08:47:08 -07001654 if (rc)
1655 return rc;
1656
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301657 val = WLED3_SINK_REG_BRIGHT_MAX;
1658 of_property_read_u32(pdev->dev.of_node, "max-brightness", &val);
1659 wled->max_brightness = val;
1660
Kiran Gundaf16899a2020-04-23 21:03:35 +05301661 switch (wled->version) {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301662 case 3:
Kiran Gunda8663c182019-11-01 11:57:04 +05301663 wled->cfg.auto_detection_enabled = false;
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301664 rc = wled3_setup(wled);
1665 if (rc) {
1666 dev_err(&pdev->dev, "wled3_setup failed\n");
1667 return rc;
1668 }
1669 break;
1670
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301671 case 4:
Kiran Gundafeeab872019-11-01 11:57:03 +05301672 wled->has_short_detect = true;
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301673 rc = wled4_setup(wled);
1674 if (rc) {
1675 dev_err(&pdev->dev, "wled4_setup failed\n");
1676 return rc;
1677 }
1678 break;
1679
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301680 case 5:
1681 wled->has_short_detect = true;
1682 if (wled->cfg.cabc_sel)
1683 wled->max_brightness = WLED5_SINK_REG_BRIGHT_MAX_12B;
1684
1685 rc = wled5_setup(wled);
1686 if (rc) {
1687 dev_err(&pdev->dev, "wled5_setup failed\n");
1688 return rc;
1689 }
1690 break;
1691
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301692 default:
1693 dev_err(wled->dev, "Invalid WLED version\n");
1694 break;
1695 }
Courtney Cavin93c64f12015-03-12 08:47:08 -07001696
Kiran Gunda8663c182019-11-01 11:57:04 +05301697 INIT_DELAYED_WORK(&wled->ovp_work, wled_ovp_work);
1698
Kiran Gundafeeab872019-11-01 11:57:03 +05301699 rc = wled_configure_short_irq(wled, pdev);
1700 if (rc < 0)
1701 return rc;
1702
Kiran Gunda8663c182019-11-01 11:57:04 +05301703 rc = wled_configure_ovp_irq(wled, pdev);
1704 if (rc < 0)
1705 return rc;
1706
Kiran Gundabb800a32019-11-01 11:57:00 +05301707 val = WLED_DEFAULT_BRIGHTNESS;
Bjorn Andersson9d6c2432015-10-26 10:45:08 -07001708 of_property_read_u32(pdev->dev.of_node, "default-brightness", &val);
1709
Bjorn Andersson7ddbc242015-07-21 17:44:49 -07001710 memset(&props, 0, sizeof(struct backlight_properties));
1711 props.type = BACKLIGHT_RAW;
Bjorn Andersson9d6c2432015-10-26 10:45:08 -07001712 props.brightness = val;
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301713 props.max_brightness = wled->max_brightness;
Bjorn Andersson7ddbc242015-07-21 17:44:49 -07001714 bl = devm_backlight_device_register(&pdev->dev, wled->name,
1715 &pdev->dev, wled,
Kiran Gundabb800a32019-11-01 11:57:00 +05301716 &wled_ops, &props);
kbuild test robotfc181112015-10-27 02:26:11 +08001717 return PTR_ERR_OR_ZERO(bl);
Courtney Cavin93c64f12015-03-12 08:47:08 -07001718};
1719
Kiran Gundafeeab872019-11-01 11:57:03 +05301720static int wled_remove(struct platform_device *pdev)
1721{
Julia Lawall0b5e0f42021-02-09 22:13:25 +01001722 struct wled *wled = platform_get_drvdata(pdev);
Kiran Gundafeeab872019-11-01 11:57:03 +05301723
1724 mutex_destroy(&wled->lock);
Kiran Gunda8663c182019-11-01 11:57:04 +05301725 cancel_delayed_work_sync(&wled->ovp_work);
Kiran Gundafeeab872019-11-01 11:57:03 +05301726 disable_irq(wled->short_irq);
Kiran Gunda8663c182019-11-01 11:57:04 +05301727 disable_irq(wled->ovp_irq);
Kiran Gundafeeab872019-11-01 11:57:03 +05301728
1729 return 0;
1730}
1731
Kiran Gundabb800a32019-11-01 11:57:00 +05301732static const struct of_device_id wled_match_table[] = {
Kiran Gunda775d2ff2019-11-01 11:57:01 +05301733 { .compatible = "qcom,pm8941-wled", .data = (void *)3 },
Konrad Dybcio6fc632d2021-02-28 13:41:05 +01001734 { .compatible = "qcom,pmi8994-wled", .data = (void *)4 },
Kiran Gunda03b2b5e2019-11-01 11:57:02 +05301735 { .compatible = "qcom,pmi8998-wled", .data = (void *)4 },
1736 { .compatible = "qcom,pm660l-wled", .data = (void *)4 },
Subbaraman Narayanamurthy62a1d3f2020-04-23 21:03:37 +05301737 { .compatible = "qcom,pm8150l-wled", .data = (void *)5 },
Courtney Cavin93c64f12015-03-12 08:47:08 -07001738 {}
1739};
Kiran Gundabb800a32019-11-01 11:57:00 +05301740MODULE_DEVICE_TABLE(of, wled_match_table);
Courtney Cavin93c64f12015-03-12 08:47:08 -07001741
Kiran Gundabb800a32019-11-01 11:57:00 +05301742static struct platform_driver wled_driver = {
1743 .probe = wled_probe,
Kiran Gundafeeab872019-11-01 11:57:03 +05301744 .remove = wled_remove,
Courtney Cavin93c64f12015-03-12 08:47:08 -07001745 .driver = {
Kiran Gundabb800a32019-11-01 11:57:00 +05301746 .name = "qcom,wled",
1747 .of_match_table = wled_match_table,
Courtney Cavin93c64f12015-03-12 08:47:08 -07001748 },
1749};
1750
Kiran Gundabb800a32019-11-01 11:57:00 +05301751module_platform_driver(wled_driver);
Courtney Cavin93c64f12015-03-12 08:47:08 -07001752
Kiran Gundabb800a32019-11-01 11:57:00 +05301753MODULE_DESCRIPTION("Qualcomm WLED driver");
Courtney Cavin93c64f12015-03-12 08:47:08 -07001754MODULE_LICENSE("GPL v2");