leds-qpnp-flash.c 69 KB

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