blob: ce2b055aec4b672783c22f4da88d2568dab16cb6 [file] [log] [blame]
Kiran Gundabf1e6c02018-01-17 17:50:20 +05301/* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
Kiran Gunda183d58f2017-11-14 15:24:50 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/regmap.h>
17#include <linux/errno.h>
18#include <linux/leds.h>
19#include <linux/slab.h>
20#include <linux/of_device.h>
21#include <linux/spmi.h>
22#include <linux/platform_device.h>
23#include <linux/err.h>
24#include <linux/delay.h>
25#include <linux/of.h>
26#include <linux/regulator/consumer.h>
27#include <linux/workqueue.h>
28#include <linux/power_supply.h>
29#include <linux/leds-qpnp-flash.h>
30#include <linux/qpnp/qpnp-adc.h>
31#include <linux/qpnp/qpnp-revid.h>
32#include <linux/debugfs.h>
33#include <linux/uaccess.h>
34#include "leds.h"
35
36#define FLASH_LED_PERIPHERAL_SUBTYPE(base) (base + 0x05)
37#define FLASH_SAFETY_TIMER(base) (base + 0x40)
38#define FLASH_MAX_CURRENT(base) (base + 0x41)
39#define FLASH_LED0_CURRENT(base) (base + 0x42)
40#define FLASH_LED1_CURRENT(base) (base + 0x43)
41#define FLASH_CLAMP_CURRENT(base) (base + 0x44)
42#define FLASH_MODULE_ENABLE_CTRL(base) (base + 0x46)
43#define FLASH_LED_STROBE_CTRL(base) (base + 0x47)
44#define FLASH_LED_TMR_CTRL(base) (base + 0x48)
45#define FLASH_HEADROOM(base) (base + 0x4A)
46#define FLASH_STARTUP_DELAY(base) (base + 0x4B)
47#define FLASH_MASK_ENABLE(base) (base + 0x4C)
48#define FLASH_VREG_OK_FORCE(base) (base + 0x4F)
49#define FLASH_FAULT_DETECT(base) (base + 0x51)
50#define FLASH_THERMAL_DRATE(base) (base + 0x52)
51#define FLASH_CURRENT_RAMP(base) (base + 0x54)
52#define FLASH_VPH_PWR_DROOP(base) (base + 0x5A)
53#define FLASH_HDRM_SNS_ENABLE_CTRL0(base) (base + 0x5C)
54#define FLASH_HDRM_SNS_ENABLE_CTRL1(base) (base + 0x5D)
55#define FLASH_LED_UNLOCK_SECURE(base) (base + 0xD0)
56#define FLASH_PERPH_RESET_CTRL(base) (base + 0xDA)
57#define FLASH_TORCH(base) (base + 0xE4)
58
59#define FLASH_STATUS_REG_MASK 0xFF
60#define FLASH_LED_FAULT_STATUS(base) (base + 0x08)
61#define INT_LATCHED_STS(base) (base + 0x18)
62#define IN_POLARITY_HIGH(base) (base + 0x12)
63#define INT_SET_TYPE(base) (base + 0x11)
64#define INT_EN_SET(base) (base + 0x15)
65#define INT_LATCHED_CLR(base) (base + 0x14)
66
67#define FLASH_HEADROOM_MASK 0x03
68#define FLASH_STARTUP_DLY_MASK 0x03
69#define FLASH_VREG_OK_FORCE_MASK 0xC0
70#define FLASH_FAULT_DETECT_MASK 0x80
71#define FLASH_THERMAL_DERATE_MASK 0xBF
72#define FLASH_SECURE_MASK 0xFF
73#define FLASH_TORCH_MASK 0x03
74#define FLASH_CURRENT_MASK 0x7F
75#define FLASH_TMR_MASK 0x03
76#define FLASH_TMR_SAFETY 0x00
77#define FLASH_SAFETY_TIMER_MASK 0x7F
78#define FLASH_MODULE_ENABLE_MASK 0xE0
79#define FLASH_STROBE_MASK 0xC0
80#define FLASH_CURRENT_RAMP_MASK 0xBF
81#define FLASH_VPH_PWR_DROOP_MASK 0xF3
82#define FLASH_LED_HDRM_SNS_ENABLE_MASK 0x81
83#define FLASH_MASK_MODULE_CONTRL_MASK 0xE0
84#define FLASH_FOLLOW_OTST2_RB_MASK 0x08
85
86#define FLASH_LED_TRIGGER_DEFAULT "none"
87#define FLASH_LED_HEADROOM_DEFAULT_MV 500
88#define FLASH_LED_STARTUP_DELAY_DEFAULT_US 128
89#define FLASH_LED_CLAMP_CURRENT_DEFAULT_MA 200
90#define FLASH_LED_THERMAL_DERATE_THRESHOLD_DEFAULT_C 80
91#define FLASH_LED_RAMP_UP_STEP_DEFAULT_US 3
92#define FLASH_LED_RAMP_DN_STEP_DEFAULT_US 3
93#define FLASH_LED_VPH_PWR_DROOP_THRESHOLD_DEFAULT_MV 3200
94#define FLASH_LED_VPH_PWR_DROOP_DEBOUNCE_TIME_DEFAULT_US 10
95#define FLASH_LED_THERMAL_DERATE_RATE_DEFAULT_PERCENT 2
96#define FLASH_RAMP_UP_DELAY_US_MIN 1000
97#define FLASH_RAMP_UP_DELAY_US_MAX 1001
98#define FLASH_RAMP_DN_DELAY_US_MIN 2160
99#define FLASH_RAMP_DN_DELAY_US_MAX 2161
100#define FLASH_BOOST_REGULATOR_PROBE_DELAY_MS 2000
101#define FLASH_TORCH_MAX_LEVEL 0x0F
102#define FLASH_MAX_LEVEL 0x4F
103#define FLASH_LED_FLASH_HW_VREG_OK 0x40
104#define FLASH_LED_FLASH_SW_VREG_OK 0x80
105#define FLASH_LED_STROBE_TYPE_HW 0x04
106#define FLASH_DURATION_DIVIDER 10
107#define FLASH_LED_HEADROOM_DIVIDER 100
108#define FLASH_LED_HEADROOM_OFFSET 2
109#define FLASH_LED_MAX_CURRENT_MA 1000
110#define FLASH_LED_THERMAL_THRESHOLD_MIN 95
111#define FLASH_LED_THERMAL_DEVIDER 10
112#define FLASH_LED_VPH_DROOP_THRESHOLD_MIN_MV 2500
113#define FLASH_LED_VPH_DROOP_THRESHOLD_DIVIDER 100
114#define FLASH_LED_HDRM_SNS_ENABLE 0x81
115#define FLASH_LED_HDRM_SNS_DISABLE 0x01
116#define FLASH_LED_UA_PER_MA 1000
117#define FLASH_LED_MASK_MODULE_MASK2_ENABLE 0x20
118#define FLASH_LED_MASK3_ENABLE_SHIFT 7
119#define FLASH_LED_MODULE_CTRL_DEFAULT 0x60
120#define FLASH_LED_CURRENT_READING_DELAY_MIN 5000
121#define FLASH_LED_CURRENT_READING_DELAY_MAX 5001
122#define FLASH_LED_OPEN_FAULT_DETECTED 0xC
123
124#define FLASH_UNLOCK_SECURE 0xA5
125#define FLASH_LED_TORCH_ENABLE 0x00
126#define FLASH_LED_TORCH_DISABLE 0x03
127#define FLASH_MODULE_ENABLE 0x80
128#define FLASH_LED0_TRIGGER 0x80
129#define FLASH_LED1_TRIGGER 0x40
130#define FLASH_LED0_ENABLEMENT 0x40
131#define FLASH_LED1_ENABLEMENT 0x20
132#define FLASH_LED_DISABLE 0x00
133#define FLASH_LED_MIN_CURRENT_MA 13
134#define FLASH_SUBTYPE_DUAL 0x01
135#define FLASH_SUBTYPE_SINGLE 0x02
136
137/*
138 * ID represents physical LEDs for individual control purpose.
139 */
140enum flash_led_id {
141 FLASH_LED_0 = 0,
142 FLASH_LED_1,
143 FLASH_LED_SWITCH,
144};
145
146enum flash_led_type {
147 FLASH = 0,
148 TORCH,
149 SWITCH,
150};
151
152enum thermal_derate_rate {
153 RATE_1_PERCENT = 0,
154 RATE_1P25_PERCENT,
155 RATE_2_PERCENT,
156 RATE_2P5_PERCENT,
157 RATE_5_PERCENT,
158};
159
160enum current_ramp_steps {
161 RAMP_STEP_0P2_US = 0,
162 RAMP_STEP_0P4_US,
163 RAMP_STEP_0P8_US,
164 RAMP_STEP_1P6_US,
165 RAMP_STEP_3P3_US,
166 RAMP_STEP_6P7_US,
167 RAMP_STEP_13P5_US,
168 RAMP_STEP_27US,
169};
170
171struct flash_regulator_data {
172 struct regulator *regs;
173 const char *reg_name;
174 u32 max_volt_uv;
175};
176
177/*
178 * Configurations for each individual LED
179 */
180struct flash_node_data {
181 struct platform_device *pdev;
182 struct regmap *regmap;
183 struct led_classdev cdev;
184 struct work_struct work;
185 struct flash_regulator_data *reg_data;
186 u16 max_current;
187 u16 prgm_current;
188 u16 prgm_current2;
189 u16 duration;
190 u8 id;
191 u8 type;
192 u8 trigger;
193 u8 enable;
194 u8 num_regulators;
195 bool flash_on;
196};
197
198/*
199 * Flash LED configuration read from device tree
200 */
201struct flash_led_platform_data {
202 unsigned int temp_threshold_num;
203 unsigned int temp_derate_curr_num;
204 unsigned int *die_temp_derate_curr_ma;
205 unsigned int *die_temp_threshold_degc;
206 u16 ramp_up_step;
207 u16 ramp_dn_step;
208 u16 vph_pwr_droop_threshold;
209 u16 headroom;
210 u16 clamp_current;
211 u8 thermal_derate_threshold;
212 u8 vph_pwr_droop_debounce_time;
213 u8 startup_dly;
214 u8 thermal_derate_rate;
215 bool pmic_charger_support;
216 bool self_check_en;
217 bool thermal_derate_en;
218 bool current_ramp_en;
219 bool vph_pwr_droop_en;
220 bool hdrm_sns_ch0_en;
221 bool hdrm_sns_ch1_en;
222 bool power_detect_en;
223 bool mask3_en;
224 bool follow_rb_disable;
225 bool die_current_derate_en;
226};
227
228struct qpnp_flash_led_buffer {
229 struct mutex debugfs_lock; /* Prevent thread concurrency */
230 size_t rpos;
231 size_t wpos;
232 size_t len;
233 struct qpnp_flash_led *led;
234 u32 buffer_cnt;
235 char data[0];
236};
237
238/*
239 * Flash LED data structure containing flash LED attributes
240 */
241struct qpnp_flash_led {
242 struct pmic_revid_data *revid_data;
243 struct platform_device *pdev;
244 struct regmap *regmap;
245 struct flash_led_platform_data *pdata;
246 struct pinctrl *pinctrl;
247 struct pinctrl_state *gpio_state_active;
248 struct pinctrl_state *gpio_state_suspend;
249 struct flash_node_data *flash_node;
250 struct power_supply *battery_psy;
251 struct workqueue_struct *ordered_workq;
252 struct qpnp_vadc_chip *vadc_dev;
253 struct mutex flash_led_lock;
254 struct dentry *dbgfs_root;
255 int num_leds;
256 u16 base;
257 u16 current_addr;
258 u16 current2_addr;
259 u8 peripheral_type;
260 u8 fault_reg;
261 bool gpio_enabled;
262 bool charging_enabled;
263 bool strobe_debug;
264 bool dbg_feature_en;
265 bool open_fault;
266};
267
268static u8 qpnp_flash_led_ctrl_dbg_regs[] = {
269 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
270 0x4A, 0x4B, 0x4C, 0x4F, 0x51, 0x52, 0x54, 0x55, 0x5A, 0x5C, 0x5D,
271};
272
273static int flash_led_dbgfs_file_open(struct qpnp_flash_led *led,
274 struct file *file)
275{
276 struct qpnp_flash_led_buffer *log;
277 size_t logbufsize = SZ_4K;
278
279 log = kzalloc(logbufsize, GFP_KERNEL);
280 if (!log)
281 return -ENOMEM;
282
283 log->rpos = 0;
284 log->wpos = 0;
285 log->len = logbufsize - sizeof(*log);
286 mutex_init(&log->debugfs_lock);
287 log->led = led;
288
289 log->buffer_cnt = 1;
290 file->private_data = log;
291
292 return 0;
293}
294
295static int flash_led_dfs_open(struct inode *inode, struct file *file)
296{
297 struct qpnp_flash_led *led = inode->i_private;
298
299 return flash_led_dbgfs_file_open(led, file);
300}
301
302static int flash_led_dfs_close(struct inode *inode, struct file *file)
303{
304 struct qpnp_flash_led_buffer *log = file->private_data;
305
306 if (log) {
307 file->private_data = NULL;
308 mutex_destroy(&log->debugfs_lock);
309 kfree(log);
310 }
311
312 return 0;
313}
314
315#define MIN_BUFFER_WRITE_LEN 20
316static int print_to_log(struct qpnp_flash_led_buffer *log,
317 const char *fmt, ...)
318{
319 va_list args;
320 int cnt;
321 char *log_buf;
322 size_t size = log->len - log->wpos;
323
324 if (size < MIN_BUFFER_WRITE_LEN)
325 return 0; /* not enough buffer left */
326
327 log_buf = &log->data[log->wpos];
328 va_start(args, fmt);
329 cnt = vscnprintf(log_buf, size, fmt, args);
330 va_end(args);
331
332 log->wpos += cnt;
333 return cnt;
334}
335
336static ssize_t flash_led_dfs_latched_reg_read(struct file *fp, char __user *buf,
337 size_t count, loff_t *ppos) {
338 struct qpnp_flash_led_buffer *log = fp->private_data;
339 struct qpnp_flash_led *led;
340 uint val;
341 int rc = 0;
342 size_t len;
343 size_t ret;
344
345 if (!log) {
346 pr_err("error: file private data is NULL\n");
347 return -EFAULT;
348 }
349 led = log->led;
350
351 mutex_lock(&log->debugfs_lock);
352 if ((log->rpos >= log->wpos && log->buffer_cnt == 0) ||
353 ((log->len - log->wpos) < MIN_BUFFER_WRITE_LEN))
354 goto unlock_mutex;
355
356 rc = regmap_read(led->regmap, INT_LATCHED_STS(led->base), &val);
357 if (rc) {
358 dev_err(&led->pdev->dev,
359 "Unable to read from address %x, rc(%d)\n",
360 INT_LATCHED_STS(led->base), rc);
361 goto unlock_mutex;
362 }
363 log->buffer_cnt--;
364
365 rc = print_to_log(log, "0x%05X ", INT_LATCHED_STS(led->base));
366 if (rc == 0)
367 goto unlock_mutex;
368
369 rc = print_to_log(log, "0x%02X ", val);
370 if (rc == 0)
371 goto unlock_mutex;
372
373 if (log->wpos > 0 && log->data[log->wpos - 1] == ' ')
374 log->data[log->wpos - 1] = '\n';
375
376 len = min(count, log->wpos - log->rpos);
377
378 ret = copy_to_user(buf, &log->data[log->rpos], len);
379 if (ret) {
380 pr_err("error copy register value to user\n");
381 rc = -EFAULT;
382 goto unlock_mutex;
383 }
384
385 len -= ret;
386 *ppos += len;
387 log->rpos += len;
388
389 rc = len;
390
391unlock_mutex:
392 mutex_unlock(&log->debugfs_lock);
393 return rc;
394}
395
396static ssize_t flash_led_dfs_fault_reg_read(struct file *fp, char __user *buf,
397 size_t count, loff_t *ppos) {
398 struct qpnp_flash_led_buffer *log = fp->private_data;
399 struct qpnp_flash_led *led;
400 int rc = 0;
401 size_t len;
402 size_t ret;
403
404 if (!log) {
405 pr_err("error: file private data is NULL\n");
406 return -EFAULT;
407 }
408 led = log->led;
409
410 mutex_lock(&log->debugfs_lock);
411 if ((log->rpos >= log->wpos && log->buffer_cnt == 0) ||
412 ((log->len - log->wpos) < MIN_BUFFER_WRITE_LEN))
413 goto unlock_mutex;
414
415 log->buffer_cnt--;
416
417 rc = print_to_log(log, "0x%05X ", FLASH_LED_FAULT_STATUS(led->base));
418 if (rc == 0)
419 goto unlock_mutex;
420
421 rc = print_to_log(log, "0x%02X ", led->fault_reg);
422 if (rc == 0)
423 goto unlock_mutex;
424
425 if (log->wpos > 0 && log->data[log->wpos - 1] == ' ')
426 log->data[log->wpos - 1] = '\n';
427
428 len = min(count, log->wpos - log->rpos);
429
430 ret = copy_to_user(buf, &log->data[log->rpos], len);
431 if (ret) {
432 pr_err("error copy register value to user\n");
433 rc = -EFAULT;
434 goto unlock_mutex;
435 }
436
437 len -= ret;
438 *ppos += len;
439 log->rpos += len;
440
441 rc = len;
442
443unlock_mutex:
444 mutex_unlock(&log->debugfs_lock);
445 return rc;
446}
447
448static ssize_t flash_led_dfs_fault_reg_enable(struct file *file,
449 const char __user *buf, size_t count, loff_t *ppos) {
450
451 u8 *val;
452 int pos = 0;
453 int cnt = 0;
454 int data;
455 size_t ret = 0;
456
457 struct qpnp_flash_led_buffer *log = file->private_data;
458 struct qpnp_flash_led *led;
459 char *kbuf;
460
461 if (!log) {
462 pr_err("error: file private data is NULL\n");
463 return -EFAULT;
464 }
465 led = log->led;
466
467 mutex_lock(&log->debugfs_lock);
468 kbuf = kmalloc(count + 1, GFP_KERNEL);
469 if (!kbuf) {
470 ret = -ENOMEM;
471 goto unlock_mutex;
472 }
473
474 ret = copy_from_user(kbuf, buf, count);
475 if (!ret) {
476 pr_err("failed to copy data from user\n");
477 ret = -EFAULT;
478 goto free_buf;
479 }
480
481 count -= ret;
482 *ppos += count;
483 kbuf[count] = '\0';
484 val = kbuf;
485 while (sscanf(kbuf + pos, "%i", &data) == 1) {
486 pos++;
487 val[cnt++] = data & 0xff;
488 }
489
490 if (!cnt)
491 goto free_buf;
492
493 ret = count;
494 if (*val == 1)
495 led->strobe_debug = true;
496 else
497 led->strobe_debug = false;
498
499free_buf:
500 kfree(kbuf);
501unlock_mutex:
502 mutex_unlock(&log->debugfs_lock);
503 return ret;
504}
505
506static ssize_t flash_led_dfs_dbg_enable(struct file *file,
507 const char __user *buf, size_t count, loff_t *ppos) {
508
509 u8 *val;
510 int pos = 0;
511 int cnt = 0;
512 int data;
513 size_t ret = 0;
514 struct qpnp_flash_led_buffer *log = file->private_data;
515 struct qpnp_flash_led *led;
516 char *kbuf;
517
518 if (!log) {
519 pr_err("error: file private data is NULL\n");
520 return -EFAULT;
521 }
522 led = log->led;
523
524 mutex_lock(&log->debugfs_lock);
525 kbuf = kmalloc(count + 1, GFP_KERNEL);
526 if (!kbuf) {
527 ret = -ENOMEM;
528 goto unlock_mutex;
529 }
530
531 ret = copy_from_user(kbuf, buf, count);
532 if (ret == count) {
533 pr_err("failed to copy data from user\n");
534 ret = -EFAULT;
535 goto free_buf;
536 }
537 count -= ret;
538 *ppos += count;
539 kbuf[count] = '\0';
540 val = kbuf;
541 while (sscanf(kbuf + pos, "%i", &data) == 1) {
542 pos++;
543 val[cnt++] = data & 0xff;
544 }
545
546 if (!cnt)
547 goto free_buf;
548
549 ret = count;
550 if (*val == 1)
551 led->dbg_feature_en = true;
552 else
553 led->dbg_feature_en = false;
554
555free_buf:
556 kfree(kbuf);
557unlock_mutex:
558 mutex_unlock(&log->debugfs_lock);
559 return ret;
560}
561
562static const struct file_operations flash_led_dfs_latched_reg_fops = {
563 .open = flash_led_dfs_open,
564 .release = flash_led_dfs_close,
565 .read = flash_led_dfs_latched_reg_read,
566};
567
568static const struct file_operations flash_led_dfs_strobe_reg_fops = {
569 .open = flash_led_dfs_open,
570 .release = flash_led_dfs_close,
571 .read = flash_led_dfs_fault_reg_read,
572 .write = flash_led_dfs_fault_reg_enable,
573};
574
575static const struct file_operations flash_led_dfs_dbg_feature_fops = {
576 .open = flash_led_dfs_open,
577 .release = flash_led_dfs_close,
578 .write = flash_led_dfs_dbg_enable,
579};
580
581static int
582qpnp_led_masked_write(struct qpnp_flash_led *led, u16 addr, u8 mask, u8 val)
583{
584 int rc;
585
586 rc = regmap_update_bits(led->regmap, addr, mask, val);
587 if (rc)
588 dev_err(&led->pdev->dev,
589 "Unable to update_bits to addr=%x, rc(%d)\n", addr, rc);
590
591 dev_dbg(&led->pdev->dev, "Write 0x%02X to addr 0x%02X\n", val, addr);
592
593 return rc;
594}
595
596static int qpnp_flash_led_get_allowed_die_temp_curr(struct qpnp_flash_led *led,
597 int64_t die_temp_degc)
598{
599 int die_temp_curr_ma;
600
601 if (die_temp_degc >= led->pdata->die_temp_threshold_degc[0])
602 die_temp_curr_ma = 0;
603 else if (die_temp_degc >= led->pdata->die_temp_threshold_degc[1])
604 die_temp_curr_ma = led->pdata->die_temp_derate_curr_ma[0];
605 else if (die_temp_degc >= led->pdata->die_temp_threshold_degc[2])
606 die_temp_curr_ma = led->pdata->die_temp_derate_curr_ma[1];
607 else if (die_temp_degc >= led->pdata->die_temp_threshold_degc[3])
608 die_temp_curr_ma = led->pdata->die_temp_derate_curr_ma[2];
609 else if (die_temp_degc >= led->pdata->die_temp_threshold_degc[4])
610 die_temp_curr_ma = led->pdata->die_temp_derate_curr_ma[3];
611 else
612 die_temp_curr_ma = led->pdata->die_temp_derate_curr_ma[4];
613
614 return die_temp_curr_ma;
615}
616
617static int64_t qpnp_flash_led_get_die_temp(struct qpnp_flash_led *led)
618{
619 struct qpnp_vadc_result die_temp_result;
620 int rc;
621
622 rc = qpnp_vadc_read(led->vadc_dev, SPARE2, &die_temp_result);
623 if (rc) {
624 pr_err("failed to read the die temp\n");
625 return -EINVAL;
626 }
627
628 return die_temp_result.physical;
629}
630
631static int qpnp_get_pmic_revid(struct qpnp_flash_led *led)
632{
633 struct device_node *revid_dev_node;
634
635 revid_dev_node = of_parse_phandle(led->pdev->dev.of_node,
636 "qcom,pmic-revid", 0);
637 if (!revid_dev_node) {
638 dev_err(&led->pdev->dev,
639 "qcom,pmic-revid property missing\n");
640 return -EINVAL;
641 }
642
643 led->revid_data = get_revid_data(revid_dev_node);
644 if (IS_ERR(led->revid_data)) {
645 pr_err("Couldn't get revid data rc = %ld\n",
646 PTR_ERR(led->revid_data));
647 return PTR_ERR(led->revid_data);
648 }
649
650 return 0;
651}
652
653static int
654qpnp_flash_led_get_max_avail_current(struct flash_node_data *flash_node,
655 struct qpnp_flash_led *led)
656{
657 union power_supply_propval prop;
658 int64_t chg_temp_milidegc, die_temp_degc;
659 int max_curr_avail_ma = 2000;
660 int allowed_die_temp_curr_ma = 2000;
661 int rc;
662
663 if (led->pdata->power_detect_en) {
664 if (!led->battery_psy) {
665 dev_err(&led->pdev->dev,
666 "Failed to query power supply\n");
667 return -EINVAL;
668 }
669
670 /*
671 * When charging is enabled, enforce this new enablement
672 * sequence to reduce fuel gauge reading resolution.
673 */
674 if (led->charging_enabled) {
675 rc = qpnp_led_masked_write(led,
676 FLASH_MODULE_ENABLE_CTRL(led->base),
677 FLASH_MODULE_ENABLE, FLASH_MODULE_ENABLE);
678 if (rc) {
679 dev_err(&led->pdev->dev,
680 "Module enable reg write failed\n");
681 return -EINVAL;
682 }
683
684 usleep_range(FLASH_LED_CURRENT_READING_DELAY_MIN,
685 FLASH_LED_CURRENT_READING_DELAY_MAX);
686 }
687
688 power_supply_get_property(led->battery_psy,
689 POWER_SUPPLY_PROP_FLASH_CURRENT_MAX, &prop);
690 if (!prop.intval) {
691 dev_err(&led->pdev->dev,
692 "battery too low for flash\n");
693 return -EINVAL;
694 }
695
696 max_curr_avail_ma = (prop.intval / FLASH_LED_UA_PER_MA);
697 }
698
699 /*
700 * When thermal mitigation is available, this logic will execute to
701 * derate current based upon the PMIC die temperature.
702 */
703 if (led->pdata->die_current_derate_en) {
704 chg_temp_milidegc = qpnp_flash_led_get_die_temp(led);
705 if (chg_temp_milidegc < 0)
706 return -EINVAL;
707
708 die_temp_degc = div_s64(chg_temp_milidegc, 1000);
709 allowed_die_temp_curr_ma =
710 qpnp_flash_led_get_allowed_die_temp_curr(led,
711 die_temp_degc);
712 if (allowed_die_temp_curr_ma < 0)
713 return -EINVAL;
714 }
715
716 max_curr_avail_ma = (max_curr_avail_ma >= allowed_die_temp_curr_ma)
717 ? allowed_die_temp_curr_ma : max_curr_avail_ma;
718
719 return max_curr_avail_ma;
720}
721
722static ssize_t qpnp_flash_led_die_temp_store(struct device *dev,
723 struct device_attribute *attr,
724 const char *buf, size_t count)
725{
726 struct qpnp_flash_led *led;
727 struct flash_node_data *flash_node;
728 unsigned long val;
729 struct led_classdev *led_cdev = dev_get_drvdata(dev);
730 ssize_t ret;
731
732 ret = kstrtoul(buf, 10, &val);
733 if (ret)
734 return ret;
735
736 flash_node = container_of(led_cdev, struct flash_node_data, cdev);
737 led = dev_get_drvdata(&flash_node->pdev->dev);
738
739 /*'0' for disable die_temp feature; non-zero to enable feature*/
740 if (val == 0)
741 led->pdata->die_current_derate_en = false;
742 else
743 led->pdata->die_current_derate_en = true;
744
745 return count;
746}
747
748static ssize_t qpnp_led_strobe_type_store(struct device *dev,
749 struct device_attribute *attr,
750 const char *buf, size_t count)
751{
752 struct flash_node_data *flash_node;
753 unsigned long state;
754 struct led_classdev *led_cdev = dev_get_drvdata(dev);
755 ssize_t ret = -EINVAL;
756
757 ret = kstrtoul(buf, 10, &state);
758 if (ret)
759 return ret;
760
761 flash_node = container_of(led_cdev, struct flash_node_data, cdev);
762
763 /* '0' for sw strobe; '1' for hw strobe */
764 if (state == 1)
765 flash_node->trigger |= FLASH_LED_STROBE_TYPE_HW;
766 else
767 flash_node->trigger &= ~FLASH_LED_STROBE_TYPE_HW;
768
769 return count;
770}
771
772static ssize_t qpnp_flash_led_dump_regs_show(struct device *dev,
773 struct device_attribute *attr, char *buf)
774{
775 struct qpnp_flash_led *led;
776 struct flash_node_data *flash_node;
777 struct led_classdev *led_cdev = dev_get_drvdata(dev);
778 int rc, i, count = 0;
779 u16 addr;
780 uint val;
781
782 flash_node = container_of(led_cdev, struct flash_node_data, cdev);
783 led = dev_get_drvdata(&flash_node->pdev->dev);
784 for (i = 0; i < ARRAY_SIZE(qpnp_flash_led_ctrl_dbg_regs); i++) {
785 addr = led->base + qpnp_flash_led_ctrl_dbg_regs[i];
786 rc = regmap_read(led->regmap, addr, &val);
787 if (rc) {
788 dev_err(&led->pdev->dev,
789 "Unable to read from addr=%x, rc(%d)\n",
790 addr, rc);
791 return -EINVAL;
792 }
793
794 count += snprintf(buf + count, PAGE_SIZE - count,
795 "REG_0x%x = 0x%02x\n", addr, val);
796
797 if (count >= PAGE_SIZE)
798 return PAGE_SIZE - 1;
799 }
800
801 return count;
802}
803
804static ssize_t qpnp_flash_led_current_derate_store(struct device *dev,
805 struct device_attribute *attr,
806 const char *buf, size_t count)
807{
808 struct qpnp_flash_led *led;
809 struct flash_node_data *flash_node;
810 unsigned long val;
811 struct led_classdev *led_cdev = dev_get_drvdata(dev);
812 ssize_t ret;
813
814 ret = kstrtoul(buf, 10, &val);
815 if (ret)
816 return ret;
817
818 flash_node = container_of(led_cdev, struct flash_node_data, cdev);
819 led = dev_get_drvdata(&flash_node->pdev->dev);
820
821 /*'0' for disable derate feature; non-zero to enable derate feature */
822 if (val == 0)
823 led->pdata->power_detect_en = false;
824 else
825 led->pdata->power_detect_en = true;
826
827 return count;
828}
829
830static ssize_t qpnp_flash_led_max_current_show(struct device *dev,
831 struct device_attribute *attr, char *buf)
832{
833 struct qpnp_flash_led *led;
834 struct flash_node_data *flash_node;
835 struct led_classdev *led_cdev = dev_get_drvdata(dev);
836 int max_curr_avail_ma = 0;
837
838 flash_node = container_of(led_cdev, struct flash_node_data, cdev);
839 led = dev_get_drvdata(&flash_node->pdev->dev);
840
841 if (led->flash_node[0].flash_on)
842 max_curr_avail_ma += led->flash_node[0].max_current;
843 if (led->flash_node[1].flash_on)
844 max_curr_avail_ma += led->flash_node[1].max_current;
845
846 if (led->pdata->power_detect_en ||
847 led->pdata->die_current_derate_en) {
848 max_curr_avail_ma =
849 qpnp_flash_led_get_max_avail_current(flash_node, led);
850
851 if (max_curr_avail_ma < 0)
852 return -EINVAL;
853 }
854
855 return snprintf(buf, PAGE_SIZE, "%u\n", max_curr_avail_ma);
856}
857
858static struct device_attribute qpnp_flash_led_attrs[] = {
859 __ATTR(strobe, 0664, NULL, qpnp_led_strobe_type_store),
860 __ATTR(reg_dump, 0664, qpnp_flash_led_dump_regs_show, NULL),
861 __ATTR(enable_current_derate, 0664, NULL,
862 qpnp_flash_led_current_derate_store),
863 __ATTR(max_allowed_current, 0664, qpnp_flash_led_max_current_show,
864 NULL),
865 __ATTR(enable_die_temp_current_derate, 0664, NULL,
866 qpnp_flash_led_die_temp_store),
867};
868
869static int qpnp_flash_led_get_thermal_derate_rate(const char *rate)
870{
871 /*
872 * return 5% derate as default value if user specifies
873 * a value un-supported
874 */
875 if (strcmp(rate, "1_PERCENT") == 0)
876 return RATE_1_PERCENT;
877 else if (strcmp(rate, "1P25_PERCENT") == 0)
878 return RATE_1P25_PERCENT;
879 else if (strcmp(rate, "2_PERCENT") == 0)
880 return RATE_2_PERCENT;
881 else if (strcmp(rate, "2P5_PERCENT") == 0)
882 return RATE_2P5_PERCENT;
883 else if (strcmp(rate, "5_PERCENT") == 0)
884 return RATE_5_PERCENT;
885 else
886 return RATE_5_PERCENT;
887}
888
889static int qpnp_flash_led_get_ramp_step(const char *step)
890{
891 /*
892 * return 27 us as default value if user specifies
893 * a value un-supported
894 */
895 if (strcmp(step, "0P2_US") == 0)
896 return RAMP_STEP_0P2_US;
897 else if (strcmp(step, "0P4_US") == 0)
898 return RAMP_STEP_0P4_US;
899 else if (strcmp(step, "0P8_US") == 0)
900 return RAMP_STEP_0P8_US;
901 else if (strcmp(step, "1P6_US") == 0)
902 return RAMP_STEP_1P6_US;
903 else if (strcmp(step, "3P3_US") == 0)
904 return RAMP_STEP_3P3_US;
905 else if (strcmp(step, "6P7_US") == 0)
906 return RAMP_STEP_6P7_US;
907 else if (strcmp(step, "13P5_US") == 0)
908 return RAMP_STEP_13P5_US;
909 else
910 return RAMP_STEP_27US;
911}
912
913static u8 qpnp_flash_led_get_droop_debounce_time(u8 val)
914{
915 /*
916 * return 10 us as default value if user specifies
917 * a value un-supported
918 */
919 switch (val) {
920 case 0:
921 return 0;
922 case 10:
923 return 1;
924 case 32:
925 return 2;
926 case 64:
927 return 3;
928 default:
929 return 1;
930 }
931}
932
933static u8 qpnp_flash_led_get_startup_dly(u8 val)
934{
935 /*
936 * return 128 us as default value if user specifies
937 * a value un-supported
938 */
939 switch (val) {
940 case 10:
941 return 0;
942 case 32:
943 return 1;
944 case 64:
945 return 2;
946 case 128:
947 return 3;
948 default:
949 return 3;
950 }
951}
952
953static int
954qpnp_flash_led_get_peripheral_type(struct qpnp_flash_led *led)
955{
956 int rc;
957 uint val;
958
959 rc = regmap_read(led->regmap,
960 FLASH_LED_PERIPHERAL_SUBTYPE(led->base), &val);
961 if (rc) {
962 dev_err(&led->pdev->dev,
963 "Unable to read peripheral subtype\n");
964 return -EINVAL;
965 }
966
967 return val;
968}
969
970static int qpnp_flash_led_module_disable(struct qpnp_flash_led *led,
971 struct flash_node_data *flash_node)
972{
973 union power_supply_propval psy_prop;
974 int rc;
975 uint val, tmp;
976
977 rc = regmap_read(led->regmap, FLASH_LED_STROBE_CTRL(led->base), &val);
978 if (rc) {
979 dev_err(&led->pdev->dev, "Unable to read strobe reg\n");
980 return -EINVAL;
981 }
982
983 tmp = (~flash_node->trigger) & val;
984 if (!tmp) {
985 if (flash_node->type == TORCH) {
986 rc = qpnp_led_masked_write(led,
987 FLASH_LED_UNLOCK_SECURE(led->base),
988 FLASH_SECURE_MASK, FLASH_UNLOCK_SECURE);
989 if (rc) {
990 dev_err(&led->pdev->dev,
991 "Secure reg write failed\n");
992 return -EINVAL;
993 }
994
995 rc = qpnp_led_masked_write(led,
996 FLASH_TORCH(led->base),
997 FLASH_TORCH_MASK, FLASH_LED_TORCH_DISABLE);
998 if (rc) {
999 dev_err(&led->pdev->dev,
1000 "Torch reg write failed\n");
1001 return -EINVAL;
1002 }
1003 }
1004
1005 if (led->battery_psy &&
1006 led->revid_data->pmic_subtype == PMI8996_SUBTYPE &&
1007 !led->revid_data->rev3) {
1008 psy_prop.intval = false;
1009 rc = power_supply_set_property(led->battery_psy,
1010 POWER_SUPPLY_PROP_FLASH_TRIGGER,
1011 &psy_prop);
1012 if (rc) {
1013 dev_err(&led->pdev->dev,
1014 "Failed to enble charger i/p current limit\n");
1015 return -EINVAL;
1016 }
1017 }
1018
1019 rc = qpnp_led_masked_write(led,
1020 FLASH_MODULE_ENABLE_CTRL(led->base),
1021 FLASH_MODULE_ENABLE_MASK,
1022 FLASH_LED_MODULE_CTRL_DEFAULT);
1023 if (rc) {
1024 dev_err(&led->pdev->dev, "Module disable failed\n");
1025 return -EINVAL;
1026 }
1027
1028 if (led->pinctrl) {
1029 rc = pinctrl_select_state(led->pinctrl,
1030 led->gpio_state_suspend);
1031 if (rc) {
1032 dev_err(&led->pdev->dev,
1033 "failed to disable GPIO\n");
1034 return -EINVAL;
1035 }
1036 led->gpio_enabled = false;
1037 }
1038
1039 if (led->battery_psy) {
1040 psy_prop.intval = false;
1041 rc = power_supply_set_property(led->battery_psy,
1042 POWER_SUPPLY_PROP_FLASH_ACTIVE,
1043 &psy_prop);
1044 if (rc) {
1045 dev_err(&led->pdev->dev,
1046 "Failed to setup OTG pulse skip enable\n");
1047 return -EINVAL;
1048 }
1049 }
1050 }
1051
1052 if (flash_node->trigger & FLASH_LED0_TRIGGER) {
1053 rc = qpnp_led_masked_write(led,
1054 led->current_addr,
1055 FLASH_CURRENT_MASK, 0x00);
1056 if (rc) {
1057 dev_err(&led->pdev->dev,
1058 "current register write failed\n");
1059 return -EINVAL;
1060 }
1061 }
1062
1063 if (flash_node->trigger & FLASH_LED1_TRIGGER) {
1064 rc = qpnp_led_masked_write(led,
1065 led->current2_addr,
1066 FLASH_CURRENT_MASK, 0x00);
1067 if (rc) {
1068 dev_err(&led->pdev->dev,
1069 "current register write failed\n");
1070 return -EINVAL;
1071 }
1072 }
1073
1074 if (flash_node->id == FLASH_LED_SWITCH)
1075 flash_node->trigger &= FLASH_LED_STROBE_TYPE_HW;
1076
1077 return 0;
1078}
1079
1080static enum
1081led_brightness qpnp_flash_led_brightness_get(struct led_classdev *led_cdev)
1082{
1083 return led_cdev->brightness;
1084}
1085
1086static int flash_regulator_parse_dt(struct qpnp_flash_led *led,
1087 struct flash_node_data *flash_node) {
1088
1089 int i = 0, rc;
1090 struct device_node *node = flash_node->cdev.dev->of_node;
1091 struct device_node *temp = NULL;
1092 const char *temp_string;
1093 u32 val;
1094
1095 flash_node->reg_data = devm_kzalloc(&led->pdev->dev,
1096 sizeof(struct flash_regulator_data *) *
1097 flash_node->num_regulators,
1098 GFP_KERNEL);
1099 if (!flash_node->reg_data) {
1100 dev_err(&led->pdev->dev,
1101 "Unable to allocate memory\n");
1102 return -ENOMEM;
1103 }
1104
1105 for_each_child_of_node(node, temp) {
1106 rc = of_property_read_string(temp, "regulator-name",
1107 &temp_string);
1108 if (!rc)
1109 flash_node->reg_data[i].reg_name = temp_string;
1110 else {
1111 dev_err(&led->pdev->dev,
1112 "Unable to read regulator name\n");
1113 return rc;
1114 }
1115
1116 rc = of_property_read_u32(temp, "max-voltage", &val);
1117 if (!rc) {
1118 flash_node->reg_data[i].max_volt_uv = val;
1119 } else if (rc != -EINVAL) {
1120 dev_err(&led->pdev->dev,
1121 "Unable to read max voltage\n");
1122 return rc;
1123 }
1124
1125 i++;
1126 }
1127
1128 return 0;
1129}
1130
1131static int flash_regulator_setup(struct qpnp_flash_led *led,
1132 struct flash_node_data *flash_node, bool on)
1133{
1134 int i, rc = 0;
1135
1136 if (on == false) {
1137 i = flash_node->num_regulators;
1138 goto error_regulator_setup;
1139 }
1140
1141 for (i = 0; i < flash_node->num_regulators; i++) {
1142 flash_node->reg_data[i].regs =
1143 regulator_get(flash_node->cdev.dev,
1144 flash_node->reg_data[i].reg_name);
1145 if (IS_ERR(flash_node->reg_data[i].regs)) {
1146 rc = PTR_ERR(flash_node->reg_data[i].regs);
1147 dev_err(&led->pdev->dev,
1148 "Failed to get regulator\n");
1149 goto error_regulator_setup;
1150 }
1151
1152 if (regulator_count_voltages(flash_node->reg_data[i].regs)
1153 > 0) {
1154 rc = regulator_set_voltage(flash_node->reg_data[i].regs,
1155 flash_node->reg_data[i].max_volt_uv,
1156 flash_node->reg_data[i].max_volt_uv);
1157 if (rc) {
1158 dev_err(&led->pdev->dev,
1159 "regulator set voltage failed\n");
1160 regulator_put(flash_node->reg_data[i].regs);
1161 goto error_regulator_setup;
1162 }
1163 }
1164 }
1165
1166 return rc;
1167
1168error_regulator_setup:
1169 while (i--) {
1170 if (regulator_count_voltages(flash_node->reg_data[i].regs)
1171 > 0) {
1172 regulator_set_voltage(flash_node->reg_data[i].regs,
1173 0, flash_node->reg_data[i].max_volt_uv);
1174 }
1175
1176 regulator_put(flash_node->reg_data[i].regs);
1177 }
1178
1179 return rc;
1180}
1181
1182static int flash_regulator_enable(struct qpnp_flash_led *led,
1183 struct flash_node_data *flash_node, bool on)
1184{
1185 int i, rc = 0;
1186
1187 if (on == false) {
1188 i = flash_node->num_regulators;
1189 goto error_regulator_enable;
1190 }
1191
1192 for (i = 0; i < flash_node->num_regulators; i++) {
1193 rc = regulator_enable(flash_node->reg_data[i].regs);
1194 if (rc) {
1195 dev_err(&led->pdev->dev,
1196 "regulator enable failed\n");
1197 goto error_regulator_enable;
1198 }
1199 }
1200
1201 return rc;
1202
1203error_regulator_enable:
1204 while (i--)
1205 regulator_disable(flash_node->reg_data[i].regs);
1206
1207 return rc;
1208}
1209
Kiran Gundabf1e6c02018-01-17 17:50:20 +05301210static int qpnp_flash_led_prepare_v1(struct led_trigger *trig, int options,
Kiran Gunda183d58f2017-11-14 15:24:50 +05301211 int *max_current)
1212{
1213 struct led_classdev *led_cdev = trigger_to_lcdev(trig);
1214 struct flash_node_data *flash_node;
1215 struct qpnp_flash_led *led;
1216 int rc;
1217
1218 if (!led_cdev) {
1219 pr_err("Invalid led_trigger provided\n");
1220 return -EINVAL;
1221 }
1222
1223 flash_node = container_of(led_cdev, struct flash_node_data, cdev);
1224 led = dev_get_drvdata(&flash_node->pdev->dev);
1225
1226 if (!(options & FLASH_LED_PREPARE_OPTIONS_MASK)) {
1227 dev_err(&led->pdev->dev, "Invalid options %d\n", options);
1228 return -EINVAL;
1229 }
1230
1231 if (options & ENABLE_REGULATOR) {
1232 rc = flash_regulator_enable(led, flash_node, true);
1233 if (rc < 0) {
1234 dev_err(&led->pdev->dev,
1235 "enable regulator failed, rc=%d\n", rc);
1236 return rc;
1237 }
1238 }
1239
1240 if (options & DISABLE_REGULATOR) {
1241 rc = flash_regulator_enable(led, flash_node, false);
1242 if (rc < 0) {
1243 dev_err(&led->pdev->dev,
1244 "disable regulator failed, rc=%d\n", rc);
1245 return rc;
1246 }
1247 }
1248
1249 if (options & QUERY_MAX_CURRENT) {
1250 rc = qpnp_flash_led_get_max_avail_current(flash_node, led);
1251 if (rc < 0) {
1252 dev_err(&led->pdev->dev,
1253 "query max current failed, rc=%d\n", rc);
1254 return rc;
1255 }
1256 *max_current = rc;
1257 }
1258
1259 return 0;
1260}
1261
1262static void qpnp_flash_led_work(struct work_struct *work)
1263{
1264 struct flash_node_data *flash_node = container_of(work,
1265 struct flash_node_data, work);
1266 struct qpnp_flash_led *led = dev_get_drvdata(&flash_node->pdev->dev);
1267 union power_supply_propval psy_prop;
1268 int rc, brightness = flash_node->cdev.brightness;
1269 int max_curr_avail_ma = 0;
1270 int total_curr_ma = 0;
1271 int i;
1272 u8 val = 0;
1273 uint temp;
1274
1275 mutex_lock(&led->flash_led_lock);
1276
1277 if (!brightness)
1278 goto turn_off;
1279
1280 if (led->open_fault) {
1281 dev_err(&led->pdev->dev, "Open fault detected\n");
1282 mutex_unlock(&led->flash_led_lock);
1283 return;
1284 }
1285
1286 if (!flash_node->flash_on && flash_node->num_regulators > 0) {
1287 rc = flash_regulator_enable(led, flash_node, true);
1288 if (rc) {
1289 mutex_unlock(&led->flash_led_lock);
1290 return;
1291 }
1292 }
1293
1294 if (!led->gpio_enabled && led->pinctrl) {
1295 rc = pinctrl_select_state(led->pinctrl,
1296 led->gpio_state_active);
1297 if (rc) {
1298 dev_err(&led->pdev->dev, "failed to enable GPIO\n");
1299 goto error_enable_gpio;
1300 }
1301 led->gpio_enabled = true;
1302 }
1303
1304 if (led->dbg_feature_en) {
1305 rc = qpnp_led_masked_write(led,
1306 INT_SET_TYPE(led->base),
1307 FLASH_STATUS_REG_MASK, 0x1F);
1308 if (rc) {
1309 dev_err(&led->pdev->dev,
1310 "INT_SET_TYPE write failed\n");
1311 goto exit_flash_led_work;
1312 }
1313
1314 rc = qpnp_led_masked_write(led,
1315 IN_POLARITY_HIGH(led->base),
1316 FLASH_STATUS_REG_MASK, 0x1F);
1317 if (rc) {
1318 dev_err(&led->pdev->dev,
1319 "IN_POLARITY_HIGH write failed\n");
1320 goto exit_flash_led_work;
1321 }
1322
1323 rc = qpnp_led_masked_write(led,
1324 INT_EN_SET(led->base),
1325 FLASH_STATUS_REG_MASK, 0x1F);
1326 if (rc) {
1327 dev_err(&led->pdev->dev, "INT_EN_SET write failed\n");
1328 goto exit_flash_led_work;
1329 }
1330
1331 rc = qpnp_led_masked_write(led,
1332 INT_LATCHED_CLR(led->base),
1333 FLASH_STATUS_REG_MASK, 0x1F);
1334 if (rc) {
1335 dev_err(&led->pdev->dev,
1336 "INT_LATCHED_CLR write failed\n");
1337 goto exit_flash_led_work;
1338 }
1339 }
1340
1341 if (led->flash_node[led->num_leds - 1].id == FLASH_LED_SWITCH &&
1342 flash_node->id != FLASH_LED_SWITCH) {
1343 led->flash_node[led->num_leds - 1].trigger |=
1344 (0x80 >> flash_node->id);
1345 if (flash_node->id == FLASH_LED_0)
1346 led->flash_node[led->num_leds - 1].prgm_current =
1347 flash_node->prgm_current;
1348 else if (flash_node->id == FLASH_LED_1)
1349 led->flash_node[led->num_leds - 1].prgm_current2 =
1350 flash_node->prgm_current;
1351 }
1352
1353 if (flash_node->type == TORCH) {
1354 rc = qpnp_led_masked_write(led,
1355 FLASH_LED_UNLOCK_SECURE(led->base),
1356 FLASH_SECURE_MASK, FLASH_UNLOCK_SECURE);
1357 if (rc) {
1358 dev_err(&led->pdev->dev, "Secure reg write failed\n");
1359 goto exit_flash_led_work;
1360 }
1361
1362 rc = qpnp_led_masked_write(led,
1363 FLASH_TORCH(led->base),
1364 FLASH_TORCH_MASK, FLASH_LED_TORCH_ENABLE);
1365 if (rc) {
1366 dev_err(&led->pdev->dev, "Torch reg write failed\n");
1367 goto exit_flash_led_work;
1368 }
1369
1370 if (flash_node->id == FLASH_LED_SWITCH) {
1371 val = (u8)(flash_node->prgm_current *
1372 FLASH_TORCH_MAX_LEVEL
1373 / flash_node->max_current);
1374 rc = qpnp_led_masked_write(led,
1375 led->current_addr,
1376 FLASH_CURRENT_MASK, val);
1377 if (rc) {
1378 dev_err(&led->pdev->dev,
1379 "Torch reg write failed\n");
1380 goto exit_flash_led_work;
1381 }
1382
1383 val = (u8)(flash_node->prgm_current2 *
1384 FLASH_TORCH_MAX_LEVEL
1385 / flash_node->max_current);
1386 rc = qpnp_led_masked_write(led,
1387 led->current2_addr,
1388 FLASH_CURRENT_MASK, val);
1389 if (rc) {
1390 dev_err(&led->pdev->dev,
1391 "Torch reg write failed\n");
1392 goto exit_flash_led_work;
1393 }
1394 } else {
1395 val = (u8)(flash_node->prgm_current *
1396 FLASH_TORCH_MAX_LEVEL /
1397 flash_node->max_current);
1398 if (flash_node->id == FLASH_LED_0) {
1399 rc = qpnp_led_masked_write(led,
1400 led->current_addr,
1401 FLASH_CURRENT_MASK, val);
1402 if (rc) {
1403 dev_err(&led->pdev->dev,
1404 "current reg write failed\n");
1405 goto exit_flash_led_work;
1406 }
1407 } else {
1408 rc = qpnp_led_masked_write(led,
1409 led->current2_addr,
1410 FLASH_CURRENT_MASK, val);
1411 if (rc) {
1412 dev_err(&led->pdev->dev,
1413 "current reg write failed\n");
1414 goto exit_flash_led_work;
1415 }
1416 }
1417 }
1418
1419 rc = qpnp_led_masked_write(led,
1420 FLASH_MAX_CURRENT(led->base),
1421 FLASH_CURRENT_MASK, FLASH_TORCH_MAX_LEVEL);
1422 if (rc) {
1423 dev_err(&led->pdev->dev,
1424 "Max current reg write failed\n");
1425 goto exit_flash_led_work;
1426 }
1427
1428 rc = qpnp_led_masked_write(led,
1429 FLASH_MODULE_ENABLE_CTRL(led->base),
1430 FLASH_MODULE_ENABLE_MASK, FLASH_MODULE_ENABLE);
1431 if (rc) {
1432 dev_err(&led->pdev->dev,
1433 "Module enable reg write failed\n");
1434 goto exit_flash_led_work;
1435 }
1436
1437 if (led->pdata->hdrm_sns_ch0_en ||
1438 led->pdata->hdrm_sns_ch1_en) {
1439 if (flash_node->id == FLASH_LED_SWITCH) {
1440 rc = qpnp_led_masked_write(led,
1441 FLASH_HDRM_SNS_ENABLE_CTRL0(led->base),
1442 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1443 flash_node->trigger &
1444 FLASH_LED0_TRIGGER ?
1445 FLASH_LED_HDRM_SNS_ENABLE :
1446 FLASH_LED_HDRM_SNS_DISABLE);
1447 if (rc) {
1448 dev_err(&led->pdev->dev,
1449 "Headroom sense enable failed\n");
1450 goto exit_flash_led_work;
1451 }
1452
1453 rc = qpnp_led_masked_write(led,
1454 FLASH_HDRM_SNS_ENABLE_CTRL1(led->base),
1455 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1456 flash_node->trigger &
1457 FLASH_LED1_TRIGGER ?
1458 FLASH_LED_HDRM_SNS_ENABLE :
1459 FLASH_LED_HDRM_SNS_DISABLE);
1460 if (rc) {
1461 dev_err(&led->pdev->dev,
1462 "Headroom sense enable failed\n");
1463 goto exit_flash_led_work;
1464 }
1465 } else if (flash_node->id == FLASH_LED_0) {
1466 rc = qpnp_led_masked_write(led,
1467 FLASH_HDRM_SNS_ENABLE_CTRL0(led->base),
1468 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1469 FLASH_LED_HDRM_SNS_ENABLE);
1470 if (rc) {
1471 dev_err(&led->pdev->dev,
1472 "Headroom sense disable failed\n");
1473 goto exit_flash_led_work;
1474 }
1475 } else if (flash_node->id == FLASH_LED_1) {
1476 rc = qpnp_led_masked_write(led,
1477 FLASH_HDRM_SNS_ENABLE_CTRL1(led->base),
1478 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1479 FLASH_LED_HDRM_SNS_ENABLE);
1480 if (rc) {
1481 dev_err(&led->pdev->dev,
1482 "Headroom sense disable failed\n");
1483 goto exit_flash_led_work;
1484 }
1485 }
1486 }
1487
1488 rc = qpnp_led_masked_write(led,
1489 FLASH_LED_STROBE_CTRL(led->base),
1490 (flash_node->id == FLASH_LED_SWITCH ? FLASH_STROBE_MASK
1491 | FLASH_LED_STROBE_TYPE_HW
1492 : flash_node->trigger |
1493 FLASH_LED_STROBE_TYPE_HW),
1494 flash_node->trigger);
1495 if (rc) {
1496 dev_err(&led->pdev->dev, "Strobe reg write failed\n");
1497 goto exit_flash_led_work;
1498 }
1499 } else if (flash_node->type == FLASH) {
1500 if (flash_node->trigger & FLASH_LED0_TRIGGER)
1501 max_curr_avail_ma += flash_node->max_current;
1502 if (flash_node->trigger & FLASH_LED1_TRIGGER)
1503 max_curr_avail_ma += flash_node->max_current;
1504
1505 psy_prop.intval = true;
1506 rc = power_supply_set_property(led->battery_psy,
1507 POWER_SUPPLY_PROP_FLASH_ACTIVE,
1508 &psy_prop);
1509 if (rc) {
1510 dev_err(&led->pdev->dev,
1511 "Failed to setup OTG pulse skip enable\n");
1512 goto exit_flash_led_work;
1513 }
1514
1515 if (led->pdata->power_detect_en ||
1516 led->pdata->die_current_derate_en) {
1517 if (led->battery_psy) {
1518 power_supply_get_property(led->battery_psy,
1519 POWER_SUPPLY_PROP_STATUS,
1520 &psy_prop);
1521 if (psy_prop.intval < 0) {
1522 dev_err(&led->pdev->dev,
1523 "Invalid battery status\n");
1524 goto exit_flash_led_work;
1525 }
1526
1527 if (psy_prop.intval ==
1528 POWER_SUPPLY_STATUS_CHARGING)
1529 led->charging_enabled = true;
1530 else if (psy_prop.intval ==
1531 POWER_SUPPLY_STATUS_DISCHARGING
1532 || psy_prop.intval ==
1533 POWER_SUPPLY_STATUS_NOT_CHARGING)
1534 led->charging_enabled = false;
1535 }
1536 max_curr_avail_ma =
1537 qpnp_flash_led_get_max_avail_current
1538 (flash_node, led);
1539 if (max_curr_avail_ma < 0) {
1540 dev_err(&led->pdev->dev,
1541 "Failed to get max avail curr\n");
1542 goto exit_flash_led_work;
1543 }
1544 }
1545
1546 if (flash_node->id == FLASH_LED_SWITCH) {
1547 if (flash_node->trigger & FLASH_LED0_TRIGGER)
1548 total_curr_ma += flash_node->prgm_current;
1549 if (flash_node->trigger & FLASH_LED1_TRIGGER)
1550 total_curr_ma += flash_node->prgm_current2;
1551
1552 if (max_curr_avail_ma < total_curr_ma) {
1553 flash_node->prgm_current =
1554 (flash_node->prgm_current *
1555 max_curr_avail_ma) / total_curr_ma;
1556 flash_node->prgm_current2 =
1557 (flash_node->prgm_current2 *
1558 max_curr_avail_ma) / total_curr_ma;
1559 }
1560
1561 val = (u8)(flash_node->prgm_current *
1562 FLASH_MAX_LEVEL / flash_node->max_current);
1563 rc = qpnp_led_masked_write(led,
1564 led->current_addr, FLASH_CURRENT_MASK, val);
1565 if (rc) {
1566 dev_err(&led->pdev->dev,
1567 "Current register write failed\n");
1568 goto exit_flash_led_work;
1569 }
1570
1571 val = (u8)(flash_node->prgm_current2 *
1572 FLASH_MAX_LEVEL / flash_node->max_current);
1573 rc = qpnp_led_masked_write(led,
1574 led->current2_addr, FLASH_CURRENT_MASK, val);
1575 if (rc) {
1576 dev_err(&led->pdev->dev,
1577 "Current register write failed\n");
1578 goto exit_flash_led_work;
1579 }
1580 } else {
1581 if (max_curr_avail_ma < flash_node->prgm_current) {
1582 dev_err(&led->pdev->dev,
1583 "battery only supprots %d mA\n",
1584 max_curr_avail_ma);
1585 flash_node->prgm_current =
1586 (u16)max_curr_avail_ma;
1587 }
1588
1589 val = (u8)(flash_node->prgm_current *
1590 FLASH_MAX_LEVEL
1591 / flash_node->max_current);
1592 if (flash_node->id == FLASH_LED_0) {
1593 rc = qpnp_led_masked_write(
1594 led,
1595 led->current_addr,
1596 FLASH_CURRENT_MASK, val);
1597 if (rc) {
1598 dev_err(&led->pdev->dev,
1599 "current reg write failed\n");
1600 goto exit_flash_led_work;
1601 }
1602 } else if (flash_node->id == FLASH_LED_1) {
1603 rc = qpnp_led_masked_write(
1604 led,
1605 led->current2_addr,
1606 FLASH_CURRENT_MASK, val);
1607 if (rc) {
1608 dev_err(&led->pdev->dev,
1609 "current reg write failed\n");
1610 goto exit_flash_led_work;
1611 }
1612 }
1613 }
1614
1615 val = (u8)((flash_node->duration - FLASH_DURATION_DIVIDER)
1616 / FLASH_DURATION_DIVIDER);
1617 rc = qpnp_led_masked_write(led,
1618 FLASH_SAFETY_TIMER(led->base),
1619 FLASH_SAFETY_TIMER_MASK, val);
1620 if (rc) {
1621 dev_err(&led->pdev->dev,
1622 "Safety timer reg write failed\n");
1623 goto exit_flash_led_work;
1624 }
1625
1626 rc = qpnp_led_masked_write(led,
1627 FLASH_MAX_CURRENT(led->base),
1628 FLASH_CURRENT_MASK, FLASH_MAX_LEVEL);
1629 if (rc) {
1630 dev_err(&led->pdev->dev,
1631 "Max current reg write failed\n");
1632 goto exit_flash_led_work;
1633 }
1634
1635 if (!led->charging_enabled) {
1636 rc = qpnp_led_masked_write(led,
1637 FLASH_MODULE_ENABLE_CTRL(led->base),
1638 FLASH_MODULE_ENABLE, FLASH_MODULE_ENABLE);
1639 if (rc) {
1640 dev_err(&led->pdev->dev,
1641 "Module enable reg write failed\n");
1642 goto exit_flash_led_work;
1643 }
1644
1645 usleep_range(FLASH_RAMP_UP_DELAY_US_MIN,
1646 FLASH_RAMP_UP_DELAY_US_MAX);
1647 }
1648
1649 if (led->revid_data->pmic_subtype == PMI8996_SUBTYPE &&
1650 !led->revid_data->rev3) {
1651 rc = power_supply_set_property(led->battery_psy,
1652 POWER_SUPPLY_PROP_FLASH_TRIGGER,
1653 &psy_prop);
1654 if (rc) {
1655 dev_err(&led->pdev->dev,
1656 "Failed to disable charger i/p curr limit\n");
1657 goto exit_flash_led_work;
1658 }
1659 }
1660
1661 if (led->pdata->hdrm_sns_ch0_en ||
1662 led->pdata->hdrm_sns_ch1_en) {
1663 if (flash_node->id == FLASH_LED_SWITCH) {
1664 rc = qpnp_led_masked_write(led,
1665 FLASH_HDRM_SNS_ENABLE_CTRL0(led->base),
1666 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1667 (flash_node->trigger &
1668 FLASH_LED0_TRIGGER ?
1669 FLASH_LED_HDRM_SNS_ENABLE :
1670 FLASH_LED_HDRM_SNS_DISABLE));
1671 if (rc) {
1672 dev_err(&led->pdev->dev,
1673 "Headroom sense enable failed\n");
1674 goto exit_flash_led_work;
1675 }
1676
1677 rc = qpnp_led_masked_write(led,
1678 FLASH_HDRM_SNS_ENABLE_CTRL1(led->base),
1679 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1680 (flash_node->trigger &
1681 FLASH_LED1_TRIGGER ?
1682 FLASH_LED_HDRM_SNS_ENABLE :
1683 FLASH_LED_HDRM_SNS_DISABLE));
1684 if (rc) {
1685 dev_err(&led->pdev->dev,
1686 "Headroom sense enable failed\n");
1687 goto exit_flash_led_work;
1688 }
1689 } else if (flash_node->id == FLASH_LED_0) {
1690 rc = qpnp_led_masked_write(led,
1691 FLASH_HDRM_SNS_ENABLE_CTRL0(led->base),
1692 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1693 FLASH_LED_HDRM_SNS_ENABLE);
1694 if (rc) {
1695 dev_err(&led->pdev->dev,
1696 "Headroom sense disable failed\n");
1697 goto exit_flash_led_work;
1698 }
1699 } else if (flash_node->id == FLASH_LED_1) {
1700 rc = qpnp_led_masked_write(led,
1701 FLASH_HDRM_SNS_ENABLE_CTRL1(led->base),
1702 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1703 FLASH_LED_HDRM_SNS_ENABLE);
1704 if (rc) {
1705 dev_err(&led->pdev->dev,
1706 "Headroom sense disable failed\n");
1707 goto exit_flash_led_work;
1708 }
1709 }
1710 }
1711
1712 rc = qpnp_led_masked_write(led,
1713 FLASH_LED_STROBE_CTRL(led->base),
1714 (flash_node->id == FLASH_LED_SWITCH ? FLASH_STROBE_MASK
1715 | FLASH_LED_STROBE_TYPE_HW
1716 : flash_node->trigger |
1717 FLASH_LED_STROBE_TYPE_HW),
1718 flash_node->trigger);
1719 if (rc) {
1720 dev_err(&led->pdev->dev, "Strobe reg write failed\n");
1721 goto exit_flash_led_work;
1722 }
1723
1724 if (led->strobe_debug && led->dbg_feature_en) {
1725 udelay(2000);
1726 rc = regmap_read(led->regmap,
1727 FLASH_LED_FAULT_STATUS(led->base),
1728 &temp);
1729 if (rc) {
1730 dev_err(&led->pdev->dev,
1731 "Unable to read from addr= %x, rc(%d)\n",
1732 FLASH_LED_FAULT_STATUS(led->base), rc);
1733 goto exit_flash_led_work;
1734 }
1735 led->fault_reg = temp;
1736 }
1737 } else {
1738 pr_err("Both Torch and Flash cannot be select at same time\n");
1739 for (i = 0; i < led->num_leds; i++)
1740 led->flash_node[i].flash_on = false;
1741 goto turn_off;
1742 }
1743
1744 flash_node->flash_on = true;
1745 mutex_unlock(&led->flash_led_lock);
1746
1747 return;
1748
1749turn_off:
1750 if (led->flash_node[led->num_leds - 1].id == FLASH_LED_SWITCH &&
1751 flash_node->id != FLASH_LED_SWITCH)
1752 led->flash_node[led->num_leds - 1].trigger &=
1753 ~(0x80 >> flash_node->id);
1754 if (flash_node->type == TORCH) {
1755 /*
1756 * Checking LED fault status detects hardware open fault.
1757 * If fault occurs, all subsequent LED enablement requests
1758 * will be rejected to protect hardware.
1759 */
1760 rc = regmap_read(led->regmap,
1761 FLASH_LED_FAULT_STATUS(led->base), &temp);
1762 if (rc) {
1763 dev_err(&led->pdev->dev,
1764 "Failed to read out fault status register\n");
1765 goto exit_flash_led_work;
1766 }
1767
1768 led->open_fault |= (val & FLASH_LED_OPEN_FAULT_DETECTED);
1769 }
1770
1771 rc = qpnp_led_masked_write(led,
1772 FLASH_LED_STROBE_CTRL(led->base),
1773 (flash_node->id == FLASH_LED_SWITCH ? FLASH_STROBE_MASK
1774 | FLASH_LED_STROBE_TYPE_HW
1775 : flash_node->trigger
1776 | FLASH_LED_STROBE_TYPE_HW),
1777 FLASH_LED_DISABLE);
1778 if (rc) {
1779 dev_err(&led->pdev->dev, "Strobe disable failed\n");
1780 goto exit_flash_led_work;
1781 }
1782
1783 usleep_range(FLASH_RAMP_DN_DELAY_US_MIN, FLASH_RAMP_DN_DELAY_US_MAX);
1784exit_flash_hdrm_sns:
1785 if (led->pdata->hdrm_sns_ch0_en) {
1786 if (flash_node->id == FLASH_LED_0 ||
1787 flash_node->id == FLASH_LED_SWITCH) {
1788 rc = qpnp_led_masked_write(led,
1789 FLASH_HDRM_SNS_ENABLE_CTRL0(led->base),
1790 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1791 FLASH_LED_HDRM_SNS_DISABLE);
1792 if (rc) {
1793 dev_err(&led->pdev->dev,
1794 "Headroom sense disable failed\n");
1795 goto exit_flash_hdrm_sns;
1796 }
1797 }
1798 }
1799
1800 if (led->pdata->hdrm_sns_ch1_en) {
1801 if (flash_node->id == FLASH_LED_1 ||
1802 flash_node->id == FLASH_LED_SWITCH) {
1803 rc = qpnp_led_masked_write(led,
1804 FLASH_HDRM_SNS_ENABLE_CTRL1(led->base),
1805 FLASH_LED_HDRM_SNS_ENABLE_MASK,
1806 FLASH_LED_HDRM_SNS_DISABLE);
1807 if (rc) {
1808 dev_err(&led->pdev->dev,
1809 "Headroom sense disable failed\n");
1810 goto exit_flash_hdrm_sns;
1811 }
1812 }
1813 }
1814exit_flash_led_work:
1815 rc = qpnp_flash_led_module_disable(led, flash_node);
1816 if (rc) {
1817 dev_err(&led->pdev->dev, "Module disable failed\n");
1818 goto exit_flash_led_work;
1819 }
1820error_enable_gpio:
1821 if (flash_node->flash_on && flash_node->num_regulators > 0)
1822 flash_regulator_enable(led, flash_node, false);
1823
1824 flash_node->flash_on = false;
1825 mutex_unlock(&led->flash_led_lock);
1826}
1827
1828static void qpnp_flash_led_brightness_set(struct led_classdev *led_cdev,
1829 enum led_brightness value)
1830{
1831 struct flash_node_data *flash_node;
1832 struct qpnp_flash_led *led;
1833
1834 flash_node = container_of(led_cdev, struct flash_node_data, cdev);
1835 led = dev_get_drvdata(&flash_node->pdev->dev);
1836
1837 if (value < LED_OFF) {
1838 pr_err("Invalid brightness value\n");
1839 return;
1840 }
1841
1842 if (value > flash_node->cdev.max_brightness)
1843 value = flash_node->cdev.max_brightness;
1844
1845 flash_node->cdev.brightness = value;
1846 if (led->flash_node[led->num_leds - 1].id ==
1847 FLASH_LED_SWITCH) {
1848 if (flash_node->type == TORCH)
1849 led->flash_node[led->num_leds - 1].type = TORCH;
1850 else if (flash_node->type == FLASH)
1851 led->flash_node[led->num_leds - 1].type = FLASH;
1852
1853 led->flash_node[led->num_leds - 1].max_current
1854 = flash_node->max_current;
1855
1856 if (flash_node->id == FLASH_LED_0 ||
1857 flash_node->id == FLASH_LED_1) {
1858 if (value < FLASH_LED_MIN_CURRENT_MA && value != 0)
1859 value = FLASH_LED_MIN_CURRENT_MA;
1860
1861 flash_node->prgm_current = value;
1862 flash_node->flash_on = value ? true : false;
1863 } else if (flash_node->id == FLASH_LED_SWITCH) {
1864 if (!value) {
1865 flash_node->prgm_current = 0;
1866 flash_node->prgm_current2 = 0;
1867 }
1868 }
1869 } else {
1870 if (value < FLASH_LED_MIN_CURRENT_MA && value != 0)
1871 value = FLASH_LED_MIN_CURRENT_MA;
1872 flash_node->prgm_current = value;
1873 }
1874
1875 queue_work(led->ordered_workq, &flash_node->work);
1876}
1877
1878static int qpnp_flash_led_init_settings(struct qpnp_flash_led *led)
1879{
1880 int rc;
1881 u8 val, temp_val;
1882 uint val_int;
1883
1884 rc = qpnp_led_masked_write(led,
1885 FLASH_MODULE_ENABLE_CTRL(led->base),
1886 FLASH_MODULE_ENABLE_MASK,
1887 FLASH_LED_MODULE_CTRL_DEFAULT);
1888 if (rc) {
1889 dev_err(&led->pdev->dev, "Module disable failed\n");
1890 return rc;
1891 }
1892
1893 rc = qpnp_led_masked_write(led,
1894 FLASH_LED_STROBE_CTRL(led->base),
1895 FLASH_STROBE_MASK, FLASH_LED_DISABLE);
1896 if (rc) {
1897 dev_err(&led->pdev->dev, "Strobe disable failed\n");
1898 return rc;
1899 }
1900
1901 rc = qpnp_led_masked_write(led,
1902 FLASH_LED_TMR_CTRL(led->base),
1903 FLASH_TMR_MASK, FLASH_TMR_SAFETY);
1904 if (rc) {
1905 dev_err(&led->pdev->dev,
1906 "LED timer ctrl reg write failed(%d)\n", rc);
1907 return rc;
1908 }
1909
1910 val = (u8)(led->pdata->headroom / FLASH_LED_HEADROOM_DIVIDER -
1911 FLASH_LED_HEADROOM_OFFSET);
1912 rc = qpnp_led_masked_write(led,
1913 FLASH_HEADROOM(led->base),
1914 FLASH_HEADROOM_MASK, val);
1915 if (rc) {
1916 dev_err(&led->pdev->dev, "Headroom reg write failed\n");
1917 return rc;
1918 }
1919
1920 val = qpnp_flash_led_get_startup_dly(led->pdata->startup_dly);
1921
1922 rc = qpnp_led_masked_write(led,
1923 FLASH_STARTUP_DELAY(led->base),
1924 FLASH_STARTUP_DLY_MASK, val);
1925 if (rc) {
1926 dev_err(&led->pdev->dev, "Startup delay reg write failed\n");
1927 return rc;
1928 }
1929
1930 val = (u8)(led->pdata->clamp_current * FLASH_MAX_LEVEL /
1931 FLASH_LED_MAX_CURRENT_MA);
1932 rc = qpnp_led_masked_write(led,
1933 FLASH_CLAMP_CURRENT(led->base),
1934 FLASH_CURRENT_MASK, val);
1935 if (rc) {
1936 dev_err(&led->pdev->dev, "Clamp current reg write failed\n");
1937 return rc;
1938 }
1939
1940 if (led->pdata->pmic_charger_support)
1941 val = FLASH_LED_FLASH_HW_VREG_OK;
1942 else
1943 val = FLASH_LED_FLASH_SW_VREG_OK;
1944 rc = qpnp_led_masked_write(led,
1945 FLASH_VREG_OK_FORCE(led->base),
1946 FLASH_VREG_OK_FORCE_MASK, val);
1947 if (rc) {
1948 dev_err(&led->pdev->dev, "VREG OK force reg write failed\n");
1949 return rc;
1950 }
1951
1952 if (led->pdata->self_check_en)
1953 val = FLASH_MODULE_ENABLE;
1954 else
1955 val = FLASH_LED_DISABLE;
1956 rc = qpnp_led_masked_write(led,
1957 FLASH_FAULT_DETECT(led->base),
1958 FLASH_FAULT_DETECT_MASK, val);
1959 if (rc) {
1960 dev_err(&led->pdev->dev, "Fault detect reg write failed\n");
1961 return rc;
1962 }
1963
1964 val = 0x0;
1965 val |= led->pdata->mask3_en << FLASH_LED_MASK3_ENABLE_SHIFT;
1966 val |= FLASH_LED_MASK_MODULE_MASK2_ENABLE;
1967 rc = qpnp_led_masked_write(led, FLASH_MASK_ENABLE(led->base),
1968 FLASH_MASK_MODULE_CONTRL_MASK, val);
1969 if (rc) {
1970 dev_err(&led->pdev->dev, "Mask module enable failed\n");
1971 return rc;
1972 }
1973
1974 rc = regmap_read(led->regmap, FLASH_PERPH_RESET_CTRL(led->base),
1975 &val_int);
1976 if (rc) {
1977 dev_err(&led->pdev->dev,
1978 "Unable to read from address %x, rc(%d)\n",
1979 FLASH_PERPH_RESET_CTRL(led->base), rc);
1980 return -EINVAL;
1981 }
1982 val = (u8)val_int;
1983
1984 if (led->pdata->follow_rb_disable) {
1985 rc = qpnp_led_masked_write(led,
1986 FLASH_LED_UNLOCK_SECURE(led->base),
1987 FLASH_SECURE_MASK, FLASH_UNLOCK_SECURE);
1988 if (rc) {
1989 dev_err(&led->pdev->dev, "Secure reg write failed\n");
1990 return -EINVAL;
1991 }
1992
1993 val |= FLASH_FOLLOW_OTST2_RB_MASK;
1994 rc = qpnp_led_masked_write(led,
1995 FLASH_PERPH_RESET_CTRL(led->base),
1996 FLASH_FOLLOW_OTST2_RB_MASK, val);
1997 if (rc) {
1998 dev_err(&led->pdev->dev,
1999 "failed to reset OTST2_RB bit\n");
2000 return rc;
2001 }
2002 } else {
2003 rc = qpnp_led_masked_write(led,
2004 FLASH_LED_UNLOCK_SECURE(led->base),
2005 FLASH_SECURE_MASK, FLASH_UNLOCK_SECURE);
2006 if (rc) {
2007 dev_err(&led->pdev->dev, "Secure reg write failed\n");
2008 return -EINVAL;
2009 }
2010
2011 val &= ~FLASH_FOLLOW_OTST2_RB_MASK;
2012 rc = qpnp_led_masked_write(led,
2013 FLASH_PERPH_RESET_CTRL(led->base),
2014 FLASH_FOLLOW_OTST2_RB_MASK, val);
2015 if (rc) {
2016 dev_err(&led->pdev->dev,
2017 "failed to reset OTST2_RB bit\n");
2018 return rc;
2019 }
2020 }
2021
2022 if (!led->pdata->thermal_derate_en)
2023 val = 0x0;
2024 else {
2025 val = led->pdata->thermal_derate_en << 7;
2026 val |= led->pdata->thermal_derate_rate << 3;
2027 val |= (led->pdata->thermal_derate_threshold -
2028 FLASH_LED_THERMAL_THRESHOLD_MIN) /
2029 FLASH_LED_THERMAL_DEVIDER;
2030 }
2031 rc = qpnp_led_masked_write(led,
2032 FLASH_THERMAL_DRATE(led->base),
2033 FLASH_THERMAL_DERATE_MASK, val);
2034 if (rc) {
2035 dev_err(&led->pdev->dev, "Thermal derate reg write failed\n");
2036 return rc;
2037 }
2038
2039 if (!led->pdata->current_ramp_en)
2040 val = 0x0;
2041 else {
2042 val = led->pdata->current_ramp_en << 7;
2043 val |= led->pdata->ramp_up_step << 3;
2044 val |= led->pdata->ramp_dn_step;
2045 }
2046 rc = qpnp_led_masked_write(led,
2047 FLASH_CURRENT_RAMP(led->base),
2048 FLASH_CURRENT_RAMP_MASK, val);
2049 if (rc) {
2050 dev_err(&led->pdev->dev, "Current ramp reg write failed\n");
2051 return rc;
2052 }
2053
2054 if (!led->pdata->vph_pwr_droop_en)
2055 val = 0x0;
2056 else {
2057 val = led->pdata->vph_pwr_droop_en << 7;
2058 val |= ((led->pdata->vph_pwr_droop_threshold -
2059 FLASH_LED_VPH_DROOP_THRESHOLD_MIN_MV) /
2060 FLASH_LED_VPH_DROOP_THRESHOLD_DIVIDER) << 4;
2061 temp_val =
2062 qpnp_flash_led_get_droop_debounce_time(
2063 led->pdata->vph_pwr_droop_debounce_time);
2064 if (temp_val == 0xFF) {
2065 dev_err(&led->pdev->dev, "Invalid debounce time\n");
2066 return temp_val;
2067 }
2068
2069 val |= temp_val;
2070 }
2071 rc = qpnp_led_masked_write(led,
2072 FLASH_VPH_PWR_DROOP(led->base),
2073 FLASH_VPH_PWR_DROOP_MASK, val);
2074 if (rc) {
2075 dev_err(&led->pdev->dev, "VPH PWR droop reg write failed\n");
2076 return rc;
2077 }
2078
2079 led->battery_psy = power_supply_get_by_name("battery");
2080 if (!led->battery_psy) {
2081 dev_err(&led->pdev->dev,
2082 "Failed to get battery power supply\n");
2083 return -EINVAL;
2084 }
2085
2086 return 0;
2087}
2088
2089static int qpnp_flash_led_parse_each_led_dt(struct qpnp_flash_led *led,
2090 struct flash_node_data *flash_node)
2091{
2092 const char *temp_string;
2093 struct device_node *node = flash_node->cdev.dev->of_node;
2094 struct device_node *temp = NULL;
2095 int rc = 0, num_regs = 0;
2096 u32 val;
2097
2098 rc = of_property_read_string(node, "label", &temp_string);
2099 if (!rc) {
2100 if (strcmp(temp_string, "flash") == 0)
2101 flash_node->type = FLASH;
2102 else if (strcmp(temp_string, "torch") == 0)
2103 flash_node->type = TORCH;
2104 else if (strcmp(temp_string, "switch") == 0)
2105 flash_node->type = SWITCH;
2106 else {
2107 dev_err(&led->pdev->dev, "Wrong flash LED type\n");
2108 return -EINVAL;
2109 }
2110 } else if (rc < 0) {
2111 dev_err(&led->pdev->dev, "Unable to read flash type\n");
2112 return rc;
2113 }
2114
2115 rc = of_property_read_u32(node, "qcom,current", &val);
2116 if (!rc) {
2117 if (val < FLASH_LED_MIN_CURRENT_MA)
2118 val = FLASH_LED_MIN_CURRENT_MA;
2119 flash_node->prgm_current = val;
2120 } else if (rc != -EINVAL) {
2121 dev_err(&led->pdev->dev, "Unable to read current\n");
2122 return rc;
2123 }
2124
2125 rc = of_property_read_u32(node, "qcom,id", &val);
2126 if (!rc)
2127 flash_node->id = (u8)val;
2128 else if (rc != -EINVAL) {
2129 dev_err(&led->pdev->dev, "Unable to read led ID\n");
2130 return rc;
2131 }
2132
2133 if (flash_node->type == SWITCH || flash_node->type == FLASH) {
2134 rc = of_property_read_u32(node, "qcom,duration", &val);
2135 if (!rc)
2136 flash_node->duration = (u16)val;
2137 else if (rc != -EINVAL) {
2138 dev_err(&led->pdev->dev, "Unable to read duration\n");
2139 return rc;
2140 }
2141 }
2142
2143 switch (led->peripheral_type) {
2144 case FLASH_SUBTYPE_SINGLE:
2145 flash_node->trigger = FLASH_LED0_TRIGGER;
2146 break;
2147 case FLASH_SUBTYPE_DUAL:
2148 if (flash_node->id == FLASH_LED_0)
2149 flash_node->trigger = FLASH_LED0_TRIGGER;
2150 else if (flash_node->id == FLASH_LED_1)
2151 flash_node->trigger = FLASH_LED1_TRIGGER;
2152 break;
2153 default:
2154 dev_err(&led->pdev->dev, "Invalid peripheral type\n");
2155 }
2156
2157 while ((temp = of_get_next_child(node, temp))) {
2158 if (of_find_property(temp, "regulator-name", NULL))
2159 num_regs++;
2160 }
2161
2162 if (num_regs)
2163 flash_node->num_regulators = num_regs;
2164
2165 return rc;
2166}
2167
2168static int qpnp_flash_led_parse_common_dt(
2169 struct qpnp_flash_led *led,
2170 struct device_node *node)
2171{
2172 int rc;
2173 u32 val, temp_val;
2174 const char *temp;
2175
2176 led->pdata->headroom = FLASH_LED_HEADROOM_DEFAULT_MV;
2177 rc = of_property_read_u32(node, "qcom,headroom", &val);
2178 if (!rc)
2179 led->pdata->headroom = (u16)val;
2180 else if (rc != -EINVAL) {
2181 dev_err(&led->pdev->dev, "Unable to read headroom\n");
2182 return rc;
2183 }
2184
2185 led->pdata->startup_dly = FLASH_LED_STARTUP_DELAY_DEFAULT_US;
2186 rc = of_property_read_u32(node, "qcom,startup-dly", &val);
2187 if (!rc)
2188 led->pdata->startup_dly = (u8)val;
2189 else if (rc != -EINVAL) {
2190 dev_err(&led->pdev->dev, "Unable to read startup delay\n");
2191 return rc;
2192 }
2193
2194 led->pdata->clamp_current = FLASH_LED_CLAMP_CURRENT_DEFAULT_MA;
2195 rc = of_property_read_u32(node, "qcom,clamp-current", &val);
2196 if (!rc) {
2197 if (val < FLASH_LED_MIN_CURRENT_MA)
2198 val = FLASH_LED_MIN_CURRENT_MA;
2199 led->pdata->clamp_current = (u16)val;
2200 } else if (rc != -EINVAL) {
2201 dev_err(&led->pdev->dev, "Unable to read clamp current\n");
2202 return rc;
2203 }
2204
2205 led->pdata->pmic_charger_support =
2206 of_property_read_bool(node,
2207 "qcom,pmic-charger-support");
2208
2209 led->pdata->self_check_en =
2210 of_property_read_bool(node, "qcom,self-check-enabled");
2211
2212 led->pdata->thermal_derate_en =
2213 of_property_read_bool(node,
2214 "qcom,thermal-derate-enabled");
2215
2216 if (led->pdata->thermal_derate_en) {
2217 led->pdata->thermal_derate_rate =
2218 FLASH_LED_THERMAL_DERATE_RATE_DEFAULT_PERCENT;
2219 rc = of_property_read_string(node, "qcom,thermal-derate-rate",
2220 &temp);
2221 if (!rc) {
2222 temp_val =
2223 qpnp_flash_led_get_thermal_derate_rate(temp);
2224 if (temp_val < 0) {
2225 dev_err(&led->pdev->dev,
2226 "Invalid thermal derate rate\n");
2227 return -EINVAL;
2228 }
2229
2230 led->pdata->thermal_derate_rate = (u8)temp_val;
2231 } else {
2232 dev_err(&led->pdev->dev,
2233 "Unable to read thermal derate rate\n");
2234 return -EINVAL;
2235 }
2236
2237 led->pdata->thermal_derate_threshold =
2238 FLASH_LED_THERMAL_DERATE_THRESHOLD_DEFAULT_C;
2239 rc = of_property_read_u32(node, "qcom,thermal-derate-threshold",
2240 &val);
2241 if (!rc)
2242 led->pdata->thermal_derate_threshold = (u8)val;
2243 else if (rc != -EINVAL) {
2244 dev_err(&led->pdev->dev,
2245 "Unable to read thermal derate threshold\n");
2246 return rc;
2247 }
2248 }
2249
2250 led->pdata->current_ramp_en =
2251 of_property_read_bool(node,
2252 "qcom,current-ramp-enabled");
2253 if (led->pdata->current_ramp_en) {
2254 led->pdata->ramp_up_step = FLASH_LED_RAMP_UP_STEP_DEFAULT_US;
2255 rc = of_property_read_string(node, "qcom,ramp_up_step", &temp);
2256 if (!rc) {
2257 temp_val = qpnp_flash_led_get_ramp_step(temp);
2258 if (temp_val < 0) {
2259 dev_err(&led->pdev->dev,
2260 "Invalid ramp up step values\n");
2261 return -EINVAL;
2262 }
2263 led->pdata->ramp_up_step = (u8)temp_val;
2264 } else if (rc != -EINVAL) {
2265 dev_err(&led->pdev->dev,
2266 "Unable to read ramp up steps\n");
2267 return rc;
2268 }
2269
2270 led->pdata->ramp_dn_step = FLASH_LED_RAMP_DN_STEP_DEFAULT_US;
2271 rc = of_property_read_string(node, "qcom,ramp_dn_step", &temp);
2272 if (!rc) {
2273 temp_val = qpnp_flash_led_get_ramp_step(temp);
2274 if (temp_val < 0) {
2275 dev_err(&led->pdev->dev,
2276 "Invalid ramp down step values\n");
2277 return rc;
2278 }
2279 led->pdata->ramp_dn_step = (u8)temp_val;
2280 } else if (rc != -EINVAL) {
2281 dev_err(&led->pdev->dev,
2282 "Unable to read ramp down steps\n");
2283 return rc;
2284 }
2285 }
2286
2287 led->pdata->vph_pwr_droop_en = of_property_read_bool(node,
2288 "qcom,vph-pwr-droop-enabled");
2289 if (led->pdata->vph_pwr_droop_en) {
2290 led->pdata->vph_pwr_droop_threshold =
2291 FLASH_LED_VPH_PWR_DROOP_THRESHOLD_DEFAULT_MV;
2292 rc = of_property_read_u32(node,
2293 "qcom,vph-pwr-droop-threshold", &val);
2294 if (!rc) {
2295 led->pdata->vph_pwr_droop_threshold = (u16)val;
2296 } else if (rc != -EINVAL) {
2297 dev_err(&led->pdev->dev,
2298 "Unable to read VPH PWR droop threshold\n");
2299 return rc;
2300 }
2301
2302 led->pdata->vph_pwr_droop_debounce_time =
2303 FLASH_LED_VPH_PWR_DROOP_DEBOUNCE_TIME_DEFAULT_US;
2304 rc = of_property_read_u32(node,
2305 "qcom,vph-pwr-droop-debounce-time", &val);
2306 if (!rc)
2307 led->pdata->vph_pwr_droop_debounce_time = (u8)val;
2308 else if (rc != -EINVAL) {
2309 dev_err(&led->pdev->dev,
2310 "Unable to read VPH PWR droop debounce time\n");
2311 return rc;
2312 }
2313 }
2314
2315 led->pdata->hdrm_sns_ch0_en = of_property_read_bool(node,
2316 "qcom,headroom-sense-ch0-enabled");
2317
2318 led->pdata->hdrm_sns_ch1_en = of_property_read_bool(node,
2319 "qcom,headroom-sense-ch1-enabled");
2320
2321 led->pdata->power_detect_en = of_property_read_bool(node,
2322 "qcom,power-detect-enabled");
2323
2324 led->pdata->mask3_en = of_property_read_bool(node,
2325 "qcom,otst2-module-enabled");
2326
2327 led->pdata->follow_rb_disable = of_property_read_bool(node,
2328 "qcom,follow-otst2-rb-disabled");
2329
2330 led->pdata->die_current_derate_en = of_property_read_bool(node,
2331 "qcom,die-current-derate-enabled");
2332
2333 if (led->pdata->die_current_derate_en) {
2334 led->vadc_dev = qpnp_get_vadc(&led->pdev->dev, "die-temp");
2335 if (IS_ERR(led->vadc_dev)) {
2336 pr_err("VADC channel property Missing\n");
2337 return -EINVAL;
2338 }
2339
2340 if (of_find_property(node, "qcom,die-temp-threshold",
2341 &led->pdata->temp_threshold_num)) {
2342 if (led->pdata->temp_threshold_num > 0) {
2343 led->pdata->die_temp_threshold_degc =
2344 devm_kzalloc(&led->pdev->dev,
2345 led->pdata->temp_threshold_num,
2346 GFP_KERNEL);
2347
2348 if (led->pdata->die_temp_threshold_degc
2349 == NULL) {
2350 dev_err(&led->pdev->dev,
2351 "failed to allocate die temp array\n");
2352 return -ENOMEM;
2353 }
2354 led->pdata->temp_threshold_num /=
2355 sizeof(unsigned int);
2356
2357 rc = of_property_read_u32_array(node,
2358 "qcom,die-temp-threshold",
2359 led->pdata->die_temp_threshold_degc,
2360 led->pdata->temp_threshold_num);
2361 if (rc) {
2362 dev_err(&led->pdev->dev,
2363 "couldn't read temp threshold rc=%d\n",
2364 rc);
2365 return rc;
2366 }
2367 }
2368 }
2369
2370 if (of_find_property(node, "qcom,die-temp-derate-current",
2371 &led->pdata->temp_derate_curr_num)) {
2372 if (led->pdata->temp_derate_curr_num > 0) {
2373 led->pdata->die_temp_derate_curr_ma =
2374 devm_kzalloc(&led->pdev->dev,
2375 led->pdata->temp_derate_curr_num,
2376 GFP_KERNEL);
2377 if (led->pdata->die_temp_derate_curr_ma
2378 == NULL) {
2379 dev_err(&led->pdev->dev,
2380 "failed to allocate die derate current array\n");
2381 return -ENOMEM;
2382 }
2383 led->pdata->temp_derate_curr_num /=
2384 sizeof(unsigned int);
2385
2386 rc = of_property_read_u32_array(node,
2387 "qcom,die-temp-derate-current",
2388 led->pdata->die_temp_derate_curr_ma,
2389 led->pdata->temp_derate_curr_num);
2390 if (rc) {
2391 dev_err(&led->pdev->dev,
2392 "couldn't read temp limits rc =%d\n",
2393 rc);
2394 return rc;
2395 }
2396 }
2397 }
2398 if (led->pdata->temp_threshold_num !=
2399 led->pdata->temp_derate_curr_num) {
2400 pr_err("Both array size are not same\n");
2401 return -EINVAL;
2402 }
2403 }
2404
2405 led->pinctrl = devm_pinctrl_get(&led->pdev->dev);
2406 if (IS_ERR_OR_NULL(led->pinctrl)) {
2407 dev_err(&led->pdev->dev, "Unable to acquire pinctrl\n");
2408 led->pinctrl = NULL;
2409 return 0;
2410 }
2411
2412 led->gpio_state_active = pinctrl_lookup_state(led->pinctrl,
2413 "flash_led_enable");
2414 if (IS_ERR_OR_NULL(led->gpio_state_active)) {
2415 dev_err(&led->pdev->dev, "Cannot lookup LED active state\n");
2416 devm_pinctrl_put(led->pinctrl);
2417 led->pinctrl = NULL;
2418 return PTR_ERR(led->gpio_state_active);
2419 }
2420
2421 led->gpio_state_suspend = pinctrl_lookup_state(led->pinctrl,
2422 "flash_led_disable");
2423 if (IS_ERR_OR_NULL(led->gpio_state_suspend)) {
2424 dev_err(&led->pdev->dev, "Cannot lookup LED disable state\n");
2425 devm_pinctrl_put(led->pinctrl);
2426 led->pinctrl = NULL;
2427 return PTR_ERR(led->gpio_state_suspend);
2428 }
2429
2430 return 0;
2431}
2432
2433static int qpnp_flash_led_probe(struct platform_device *pdev)
2434{
2435 struct qpnp_flash_led *led;
2436 unsigned int base;
2437 struct device_node *node, *temp;
2438 struct dentry *root, *file;
2439 int rc, i = 0, j, num_leds = 0;
2440 u32 val;
2441
2442 root = NULL;
2443 node = pdev->dev.of_node;
2444 if (node == NULL) {
2445 dev_info(&pdev->dev, "No flash device defined\n");
2446 return -ENODEV;
2447 }
2448
2449 rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
2450 if (rc < 0) {
2451 dev_err(&pdev->dev,
2452 "Couldn't find reg in node = %s rc = %d\n",
2453 pdev->dev.of_node->full_name, rc);
2454 return rc;
2455 }
2456
2457 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
2458 if (!led)
2459 return -ENOMEM;
2460
2461 led->regmap = dev_get_regmap(pdev->dev.parent, NULL);
2462 if (!led->regmap) {
2463 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
2464 return -EINVAL;
2465 }
2466
2467 led->base = base;
2468 led->pdev = pdev;
2469 led->current_addr = FLASH_LED0_CURRENT(led->base);
2470 led->current2_addr = FLASH_LED1_CURRENT(led->base);
Kiran Gundabf1e6c02018-01-17 17:50:20 +05302471 qpnp_flash_led_prepare = qpnp_flash_led_prepare_v1;
Kiran Gunda183d58f2017-11-14 15:24:50 +05302472
2473 led->pdata = devm_kzalloc(&pdev->dev, sizeof(*led->pdata), GFP_KERNEL);
2474 if (!led->pdata)
2475 return -ENOMEM;
2476
2477 led->peripheral_type = (u8)qpnp_flash_led_get_peripheral_type(led);
2478 if (led->peripheral_type < 0) {
2479 dev_err(&pdev->dev, "Failed to get peripheral type\n");
2480 return rc;
2481 }
2482
2483 rc = qpnp_flash_led_parse_common_dt(led, node);
2484 if (rc) {
2485 dev_err(&pdev->dev,
2486 "Failed to get common config for flash LEDs\n");
2487 return rc;
2488 }
2489
2490 rc = qpnp_flash_led_init_settings(led);
2491 if (rc) {
2492 dev_err(&pdev->dev, "Failed to initialize flash LED\n");
2493 return rc;
2494 }
2495
2496 rc = qpnp_get_pmic_revid(led);
2497 if (rc)
2498 return rc;
2499
2500 temp = NULL;
2501 while ((temp = of_get_next_child(node, temp)))
2502 num_leds++;
2503
2504 if (!num_leds)
2505 return -ECHILD;
2506
2507 led->flash_node = devm_kzalloc(&pdev->dev,
2508 (sizeof(struct flash_node_data) * num_leds),
2509 GFP_KERNEL);
2510 if (!led->flash_node) {
2511 dev_err(&pdev->dev, "Unable to allocate memory\n");
2512 return -ENOMEM;
2513 }
2514
2515 mutex_init(&led->flash_led_lock);
2516
2517 led->ordered_workq = alloc_ordered_workqueue("flash_led_workqueue", 0);
2518 if (!led->ordered_workq) {
2519 dev_err(&pdev->dev, "Failed to allocate ordered workqueue\n");
2520 return -ENOMEM;
2521 }
2522
2523 for_each_child_of_node(node, temp) {
2524 led->flash_node[i].cdev.brightness_set =
2525 qpnp_flash_led_brightness_set;
2526 led->flash_node[i].cdev.brightness_get =
2527 qpnp_flash_led_brightness_get;
2528 led->flash_node[i].pdev = pdev;
2529
2530 INIT_WORK(&led->flash_node[i].work, qpnp_flash_led_work);
2531 rc = of_property_read_string(temp, "qcom,led-name",
2532 &led->flash_node[i].cdev.name);
2533 if (rc < 0) {
2534 dev_err(&led->pdev->dev,
2535 "Unable to read flash name\n");
2536 return rc;
2537 }
2538
2539 rc = of_property_read_string(temp, "qcom,default-led-trigger",
2540 &led->flash_node[i].cdev.default_trigger);
2541 if (rc < 0) {
2542 dev_err(&led->pdev->dev,
2543 "Unable to read trigger name\n");
2544 return rc;
2545 }
2546
2547 rc = of_property_read_u32(temp, "qcom,max-current", &val);
2548 if (!rc) {
2549 if (val < FLASH_LED_MIN_CURRENT_MA)
2550 val = FLASH_LED_MIN_CURRENT_MA;
2551 led->flash_node[i].max_current = (u16)val;
2552 led->flash_node[i].cdev.max_brightness = val;
2553 } else {
2554 dev_err(&led->pdev->dev,
2555 "Unable to read max current\n");
2556 return rc;
2557 }
2558 rc = led_classdev_register(&pdev->dev,
2559 &led->flash_node[i].cdev);
2560 if (rc) {
2561 dev_err(&pdev->dev, "Unable to register led\n");
2562 goto error_led_register;
2563 }
2564
2565 led->flash_node[i].cdev.dev->of_node = temp;
2566
2567 rc = qpnp_flash_led_parse_each_led_dt(led, &led->flash_node[i]);
2568 if (rc) {
2569 dev_err(&pdev->dev,
2570 "Failed to parse config for each LED\n");
2571 goto error_led_register;
2572 }
2573
2574 if (led->flash_node[i].num_regulators) {
2575 rc = flash_regulator_parse_dt(led, &led->flash_node[i]);
2576 if (rc) {
2577 dev_err(&pdev->dev,
2578 "Unable to parse regulator data\n");
2579 goto error_led_register;
2580 }
2581
2582 rc = flash_regulator_setup(led, &led->flash_node[i],
2583 true);
2584 if (rc) {
2585 dev_err(&pdev->dev,
2586 "Unable to set up regulator\n");
2587 goto error_led_register;
2588 }
2589 }
2590
2591 for (j = 0; j < ARRAY_SIZE(qpnp_flash_led_attrs); j++) {
2592 rc =
2593 sysfs_create_file(&led->flash_node[i].cdev.dev->kobj,
2594 &qpnp_flash_led_attrs[j].attr);
2595 if (rc)
2596 goto error_led_register;
2597 }
2598
2599 i++;
2600 }
2601
2602 led->num_leds = i;
2603
2604 root = debugfs_create_dir("flashLED", NULL);
2605 if (IS_ERR_OR_NULL(root)) {
2606 pr_err("Error creating top level directory err%ld",
2607 (long)root);
2608 if (PTR_ERR(root) == -ENODEV)
2609 pr_err("debugfs is not enabled in kernel");
2610 goto error_led_debugfs;
2611 }
2612
2613 led->dbgfs_root = root;
2614 file = debugfs_create_file("enable_debug", 0600, root, led,
2615 &flash_led_dfs_dbg_feature_fops);
2616 if (!file) {
2617 pr_err("error creating 'enable_debug' entry\n");
2618 goto error_led_debugfs;
2619 }
2620
2621 file = debugfs_create_file("latched", 0600, root, led,
2622 &flash_led_dfs_latched_reg_fops);
2623 if (!file) {
2624 pr_err("error creating 'latched' entry\n");
2625 goto error_led_debugfs;
2626 }
2627
2628 file = debugfs_create_file("strobe", 0600, root, led,
2629 &flash_led_dfs_strobe_reg_fops);
2630 if (!file) {
2631 pr_err("error creating 'strobe' entry\n");
2632 goto error_led_debugfs;
2633 }
2634
2635 dev_set_drvdata(&pdev->dev, led);
2636
2637 return 0;
2638
2639error_led_debugfs:
2640 i = led->num_leds - 1;
2641 j = ARRAY_SIZE(qpnp_flash_led_attrs) - 1;
2642error_led_register:
2643 for (; i >= 0; i--) {
2644 for (; j >= 0; j--)
2645 sysfs_remove_file(&led->flash_node[i].cdev.dev->kobj,
2646 &qpnp_flash_led_attrs[j].attr);
2647 j = ARRAY_SIZE(qpnp_flash_led_attrs) - 1;
2648 led_classdev_unregister(&led->flash_node[i].cdev);
2649 }
2650 debugfs_remove_recursive(root);
2651 mutex_destroy(&led->flash_led_lock);
2652 destroy_workqueue(led->ordered_workq);
2653
2654 return rc;
2655}
2656
2657static int qpnp_flash_led_remove(struct platform_device *pdev)
2658{
2659 struct qpnp_flash_led *led = dev_get_drvdata(&pdev->dev);
2660 int i, j;
2661
2662 for (i = led->num_leds - 1; i >= 0; i--) {
2663 if (led->flash_node[i].reg_data) {
2664 if (led->flash_node[i].flash_on)
2665 flash_regulator_enable(led,
2666 &led->flash_node[i], false);
2667 flash_regulator_setup(led, &led->flash_node[i],
2668 false);
2669 }
2670 for (j = 0; j < ARRAY_SIZE(qpnp_flash_led_attrs); j++)
2671 sysfs_remove_file(&led->flash_node[i].cdev.dev->kobj,
2672 &qpnp_flash_led_attrs[j].attr);
2673 led_classdev_unregister(&led->flash_node[i].cdev);
2674 }
2675 debugfs_remove_recursive(led->dbgfs_root);
2676 mutex_destroy(&led->flash_led_lock);
2677 destroy_workqueue(led->ordered_workq);
2678
2679 return 0;
2680}
2681
2682static const struct of_device_id spmi_match_table[] = {
2683 { .compatible = "qcom,qpnp-flash-led",},
2684 { },
2685};
2686
2687static struct platform_driver qpnp_flash_led_driver = {
2688 .driver = {
2689 .name = "qcom,qpnp-flash-led",
2690 .of_match_table = spmi_match_table,
2691 },
2692 .probe = qpnp_flash_led_probe,
2693 .remove = qpnp_flash_led_remove,
2694};
2695
2696static int __init qpnp_flash_led_init(void)
2697{
2698 return platform_driver_register(&qpnp_flash_led_driver);
2699}
2700late_initcall(qpnp_flash_led_init);
2701
2702static void __exit qpnp_flash_led_exit(void)
2703{
2704 platform_driver_unregister(&qpnp_flash_led_driver);
2705}
2706module_exit(qpnp_flash_led_exit);
2707
2708MODULE_DESCRIPTION("QPNP Flash LED driver");
2709MODULE_LICENSE("GPL v2");
2710MODULE_ALIAS("leds:leds-qpnp-flash");