qpnp-oledb-regulator.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. /* Copyright (c) 2016-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. #define pr_fmt(fmt) "OLEDB: %s: " fmt, __func__
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/notifier.h>
  19. #include <linux/of.h>
  20. #include <linux/regmap.h>
  21. #include <linux/spmi.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/machine.h>
  25. #include <linux/regulator/of_regulator.h>
  26. #include <linux/regulator/qpnp-labibb-regulator.h>
  27. #include <linux/qpnp/qpnp-pbs.h>
  28. #include <linux/qpnp/qpnp-revid.h>
  29. #define QPNP_OLEDB_REGULATOR_DRIVER_NAME "qcom,qpnp-oledb-regulator"
  30. #define OLEDB_VOUT_STEP_MV 100
  31. #define OLEDB_VOUT_MIN_MV 5000
  32. #define OLEDB_VOUT_MAX_MV 8100
  33. #define OLEDB_VOUT_HW_DEFAULT 6400
  34. #define OLEDB_MODULE_RDY 0x45
  35. #define OLEDB_MODULE_RDY_BIT BIT(7)
  36. #define OLEDB_MODULE_ENABLE 0x46
  37. #define OLEDB_MODULE_ENABLE_BIT BIT(7)
  38. #define OLEDB_EXT_PIN_CTL 0x47
  39. #define OLEDB_EXT_PIN_CTL_BIT BIT(7)
  40. #define OLEDB_SWIRE_CONTROL 0x48
  41. #define OLEDB_EN_SWIRE_VOUT_UPD_BIT BIT(6)
  42. #define OLEDB_EN_SWIRE_PD_UPD_BIT BIT(7)
  43. #define OLEDB_VOUT_PGM 0x49
  44. #define OLEDB_VOUT_PGM_MASK GENMASK(4, 0)
  45. #define OLEDB_VOUT_DEFAULT 0x4A
  46. #define OLEDB_VOUT_DEFAULT_MASK GENMASK(4, 0)
  47. #define OLEDB_PD_CTL 0x4B
  48. #define OLEDB_ILIM_NFET 0x4E
  49. #define OLEDB_ILIMIT_NFET_MASK GENMASK(2, 0)
  50. #define OLEDB_BIAS_GEN_WARMUP_DELAY 0x52
  51. #define OLEDB_BIAS_GEN_WARMUP_DELAY_MASK GENMASK(1, 0)
  52. #define OLEDB_SHORT_PROTECT 0x59
  53. #define OLEDB_ENABLE_SC_DETECTION_BIT BIT(7)
  54. #define OLEDB_DBNC_SHORT_DETECTION_MASK GENMASK(1, 0)
  55. #define OLEDB_FAST_PRECHARGE 0x5A
  56. #define OLEDB_FAST_PRECHG_PPULSE_EN_BIT BIT(7)
  57. #define OLEDB_DBNC_PRECHARGE_MASK GENMASK(5, 4)
  58. #define OLEDB_DBNC_PRECHARGE_SHIFT 4
  59. #define OLEDB_PRECHARGE_PULSE_PERIOD_MASK GENMASK(3, 2)
  60. #define OLEDB_PRECHARGE_PULSE_PERIOD_SHIFT 2
  61. #define OLEDB_PRECHARGE_PULSE_TON_MASK GENMASK(1, 0)
  62. #define OLEDB_EN_PSM 0x5B
  63. #define OLEDB_PSM_ENABLE_BIT BIT(7)
  64. #define OLEDB_PSM_CTL 0x5C
  65. #define OLEDB_PSM_HYSTERYSIS_CTL_BIT BIT(3)
  66. #define OLEDB_PSM_HYSTERYSIS_CTL_BIT_SHIFT 3
  67. #define OLEDB_VREF_PSM_MASK GENMASK(2, 0)
  68. #define OLEDB_PFM_CTL 0x5D
  69. #define OLEDB_PFM_ENABLE_BIT BIT(7)
  70. #define OLEDB_PFM_HYSTERYSIS_CTRL_BIT_MASK BIT(4)
  71. #define OLEDB_PFM_HYSTERYSIS_CTL_BIT_SHIFT 4
  72. #define OLEDB_PFM_CURR_LIMIT_MASK GENMASK(3, 2)
  73. #define OLEDB_PFM_CURR_LIMIT_SHIFT 2
  74. #define OLEDB_PFM_OFF_TIME_NS_MASK GENMASK(1, 0)
  75. #define OLEDB_NLIMIT 0x64
  76. #define OLEDB_ENABLE_NLIMIT_BIT BIT(7)
  77. #define OLEDB_ENABLE_NLIMIT_BIT_SHIFT 7
  78. #define OLEDB_NLIMIT_PGM_MASK GENMASK(1, 0)
  79. #define OLEDB_SPARE_CTL 0xE9
  80. #define OLEDB_FORCE_PD_CTL_SPARE_BIT BIT(7)
  81. #define OLEDB_PD_PBS_TRIGGER_BIT BIT(0)
  82. #define OLEDB_SEC_UNLOCK_CODE 0xA5
  83. #define OLEDB_PSM_HYS_CTRL_MIN 13
  84. #define OLEDB_PSM_HYS_CTRL_MAX 26
  85. #define OLEDB_PFM_HYS_CTRL_MIN 13
  86. #define OLEDB_PFM_HYS_CTRL_MAX 26
  87. #define OLEDB_PFM_OFF_TIME_MIN 110
  88. #define OLEDB_PFM_OFF_TIME_MAX 480
  89. #define OLEDB_PRECHG_TIME_MIN 1
  90. #define OLEDB_PRECHG_TIME_MAX 8
  91. #define OLEDB_PRECHG_PULSE_PERIOD_MIN 3
  92. #define OLEDB_PRECHG_PULSE_PERIOD_MAX 12
  93. #define OLEDB_MIN_SC_DBNC_TIME_FSW 2
  94. #define OLEDB_MAX_SC_DBNC_TIME_FSW 16
  95. #define OLEDB_PRECHG_PULSE_ON_TIME_MIN 1200
  96. #define OLEDB_PRECHG_PULSE_ON_TIME_MAX 3000
  97. #define PSM_HYSTERYSIS_MV_TO_VAL(val_mv) ((val_mv/13) - 1)
  98. #define PFM_HYSTERYSIS_MV_TO_VAL(val_mv) ((val_mv/13) - 1)
  99. #define PFM_OFF_TIME_NS_TO_VAL(val_ns) ((val_ns/110) - 1)
  100. #define PRECHG_DEBOUNCE_TIME_MS_TO_VAL(val_ms) ((val_ms/2) - \
  101. (val_ms/8))
  102. #define PRECHG_PULSE_PERIOD_US_TO_VAL(val_us) ((val_us/3) - 1)
  103. #define PRECHG_PULSE_ON_TIME_NS_TO_VAL(val_ns) (val_ns/600 - 2)
  104. #define SHORT_CIRCUIT_DEBOUNCE_TIME_TO_VAL(val) ((val/4) - (val/16))
  105. struct qpnp_oledb_psm_ctl {
  106. int psm_enable;
  107. int psm_hys_ctl;
  108. int psm_vref;
  109. };
  110. struct qpnp_oledb_pfm_ctl {
  111. int pfm_enable;
  112. int pfm_hys_ctl;
  113. int pfm_curr_limit;
  114. int pfm_off_time;
  115. };
  116. struct qpnp_oledb_fast_precharge_ctl {
  117. int fast_prechg_ppulse_en;
  118. int prechg_debounce_time;
  119. int prechg_pulse_period;
  120. int prechg_pulse_on_time;
  121. };
  122. struct qpnp_oledb {
  123. struct platform_device *pdev;
  124. struct device *dev;
  125. struct regmap *regmap;
  126. struct class oledb_class;
  127. struct regulator_desc rdesc;
  128. struct regulator_dev *rdev;
  129. struct qpnp_oledb_psm_ctl psm_ctl;
  130. struct qpnp_oledb_pfm_ctl pfm_ctl;
  131. struct qpnp_oledb_fast_precharge_ctl fast_prechg_ctl;
  132. struct notifier_block oledb_nb;
  133. struct mutex bus_lock;
  134. struct device_node *pbs_dev_node;
  135. struct pmic_revid_data *pmic_rev_id;
  136. u32 base;
  137. u8 mod_enable;
  138. u8 ext_pinctl_state;
  139. int current_voltage;
  140. int default_voltage;
  141. int vout_mv;
  142. int warmup_delay;
  143. int peak_curr_limit;
  144. int pd_ctl;
  145. int negative_curr_limit;
  146. int nlimit_enable;
  147. int sc_en;
  148. int sc_dbnc_time;
  149. bool swire_control;
  150. bool ext_pin_control;
  151. bool dynamic_ext_pinctl_config;
  152. bool pbs_control;
  153. bool force_pd_control;
  154. bool handle_lab_sc_notification;
  155. bool lab_sc_detected;
  156. bool secure_mode;
  157. };
  158. static const u16 oledb_warmup_dly_ns[] = {6700, 13300, 26700, 53400};
  159. static const u16 oledb_peak_curr_limit_ma[] = {115, 265, 415, 570,
  160. 720, 870, 1020, 1170};
  161. static const u16 oledb_psm_vref_mv[] = {440, 510, 580, 650, 715,
  162. 780, 850, 920};
  163. static const u16 oledb_pfm_curr_limit_ma[] = {130, 200, 270, 340};
  164. static const u16 oledb_nlimit_ma[] = {170, 300, 420, 550};
  165. static int qpnp_oledb_read(struct qpnp_oledb *oledb, u32 address,
  166. u8 *val, int count)
  167. {
  168. int rc = 0;
  169. struct platform_device *pdev = oledb->pdev;
  170. mutex_lock(&oledb->bus_lock);
  171. rc = regmap_bulk_read(oledb->regmap, address, val, count);
  172. if (rc)
  173. pr_err("Failed to read address=0x%02x sid=0x%02x rc=%d\n",
  174. address, to_spmi_device(pdev->dev.parent)->usid, rc);
  175. mutex_unlock(&oledb->bus_lock);
  176. return rc;
  177. }
  178. static int qpnp_oledb_masked_write(struct qpnp_oledb *oledb,
  179. u32 address, u8 mask, u8 val)
  180. {
  181. int rc;
  182. mutex_lock(&oledb->bus_lock);
  183. rc = regmap_update_bits(oledb->regmap, address, mask, val);
  184. if (rc < 0)
  185. pr_err("Failed to write address 0x%04X, rc = %d\n",
  186. address, rc);
  187. else
  188. pr_debug("Wrote 0x%02X to addr 0x%04X\n",
  189. val, address);
  190. mutex_unlock(&oledb->bus_lock);
  191. return rc;
  192. }
  193. #define OLEDB_SEC_ACCESS 0xD0
  194. static int qpnp_oledb_sec_masked_write(struct qpnp_oledb *oledb, u16 address,
  195. u8 mask, u8 val)
  196. {
  197. int rc = 0;
  198. u8 sec_val = OLEDB_SEC_UNLOCK_CODE;
  199. u16 sec_reg_addr = (address & 0xFF00) | OLEDB_SEC_ACCESS;
  200. mutex_lock(&oledb->bus_lock);
  201. rc = regmap_write(oledb->regmap, sec_reg_addr, sec_val);
  202. if (rc < 0) {
  203. pr_err("register %x failed rc = %d\n", sec_reg_addr, rc);
  204. goto error;
  205. }
  206. rc = regmap_update_bits(oledb->regmap, address, mask, val);
  207. if (rc < 0)
  208. pr_err("spmi write failed: addr=%03X, rc=%d\n", address, rc);
  209. error:
  210. mutex_unlock(&oledb->bus_lock);
  211. return rc;
  212. }
  213. static int qpnp_oledb_write(struct qpnp_oledb *oledb, u16 address, u8 *val,
  214. int count)
  215. {
  216. int rc = 0;
  217. struct platform_device *pdev = oledb->pdev;
  218. mutex_lock(&oledb->bus_lock);
  219. rc = regmap_bulk_write(oledb->regmap, address, val, count);
  220. if (rc)
  221. pr_err("Failed to write address=0x%02x sid=0x%02x rc=%d\n",
  222. address, to_spmi_device(pdev->dev.parent)->usid, rc);
  223. else
  224. pr_debug("Wrote 0x%02X to addr 0x%04X\n",
  225. *val, address);
  226. mutex_unlock(&oledb->bus_lock);
  227. return rc;
  228. }
  229. static int qpnp_oledb_regulator_enable(struct regulator_dev *rdev)
  230. {
  231. int rc = 0;
  232. u8 val = 0;
  233. struct qpnp_oledb *oledb = rdev_get_drvdata(rdev);
  234. if (oledb->secure_mode)
  235. return 0;
  236. if (oledb->lab_sc_detected == true) {
  237. pr_info("Short circuit detected: Disabled OLEDB rail\n");
  238. return 0;
  239. }
  240. if (oledb->ext_pin_control) {
  241. rc = qpnp_oledb_read(oledb, oledb->base + OLEDB_EXT_PIN_CTL,
  242. &val, 1);
  243. if (rc < 0) {
  244. pr_err("Failed to read EXT_PIN_CTL rc=%d\n", rc);
  245. return rc;
  246. }
  247. /*
  248. * Enable ext-pin-ctl after display-supply is turned on.
  249. * This is to avoid glitches on the external pin.
  250. */
  251. if (!(val & OLEDB_EXT_PIN_CTL_BIT) &&
  252. oledb->dynamic_ext_pinctl_config) {
  253. val = OLEDB_EXT_PIN_CTL_BIT;
  254. rc = qpnp_oledb_write(oledb, oledb->base +
  255. OLEDB_EXT_PIN_CTL, &val, 1);
  256. if (rc < 0) {
  257. pr_err("Failed to write EXT_PIN_CTL rc=%d\n",
  258. rc);
  259. return rc;
  260. }
  261. }
  262. pr_debug("ext-pin-ctrl mode enabled\n");
  263. } else {
  264. val = OLEDB_MODULE_ENABLE_BIT;
  265. rc = qpnp_oledb_write(oledb, oledb->base + OLEDB_MODULE_ENABLE,
  266. &val, 1);
  267. if (rc < 0) {
  268. pr_err("Failed to write MODULE_ENABLE rc=%d\n", rc);
  269. return rc;
  270. }
  271. ndelay(oledb->warmup_delay);
  272. pr_debug("register-control mode, module enabled\n");
  273. }
  274. oledb->mod_enable = true;
  275. if (oledb->pbs_control) {
  276. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  277. OLEDB_SWIRE_CONTROL, OLEDB_EN_SWIRE_PD_UPD_BIT |
  278. OLEDB_EN_SWIRE_VOUT_UPD_BIT, 0);
  279. if (rc < 0)
  280. pr_err("Failed to write SWIRE_CTL for pbs mode rc=%d\n",
  281. rc);
  282. }
  283. return rc;
  284. }
  285. static int qpnp_oledb_regulator_disable(struct regulator_dev *rdev)
  286. {
  287. int rc = 0;
  288. u8 trigger_bitmap = OLEDB_PD_PBS_TRIGGER_BIT;
  289. u8 val;
  290. struct qpnp_oledb *oledb = rdev_get_drvdata(rdev);
  291. if (oledb->secure_mode)
  292. return 0;
  293. /*
  294. * Disable ext-pin-ctl after display-supply is turned off. This is to
  295. * avoid glitches on the external pin.
  296. */
  297. if (oledb->ext_pin_control) {
  298. if (oledb->dynamic_ext_pinctl_config) {
  299. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  300. OLEDB_EXT_PIN_CTL, OLEDB_EXT_PIN_CTL_BIT, 0);
  301. if (rc < 0) {
  302. pr_err("Failed to write EXT_PIN_CTL rc=%d\n",
  303. rc);
  304. return rc;
  305. }
  306. }
  307. pr_debug("ext-pin-ctrl mode disabled\n");
  308. } else {
  309. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  310. OLEDB_MODULE_ENABLE,
  311. OLEDB_MODULE_ENABLE_BIT, 0);
  312. if (rc < 0) {
  313. pr_err("Failed to write MODULE_ENABLE rc=%d\n", rc);
  314. return rc;
  315. }
  316. pr_debug("Register-control mode, module disabled\n");
  317. }
  318. if (oledb->force_pd_control) {
  319. rc = qpnp_oledb_read(oledb, oledb->base + OLEDB_SPARE_CTL,
  320. &val, 1);
  321. if (rc < 0) {
  322. pr_err("Failed to read OLEDB_SPARE_CTL rc=%d\n", rc);
  323. return rc;
  324. }
  325. if (val & OLEDB_FORCE_PD_CTL_SPARE_BIT) {
  326. rc = qpnp_oledb_sec_masked_write(oledb, oledb->base +
  327. OLEDB_SPARE_CTL,
  328. OLEDB_FORCE_PD_CTL_SPARE_BIT, 0);
  329. if (rc < 0) {
  330. pr_err("Failed to write SPARE_CTL rc=%d\n", rc);
  331. return rc;
  332. }
  333. rc = qpnp_pbs_trigger_event(oledb->pbs_dev_node,
  334. trigger_bitmap);
  335. if (rc < 0)
  336. pr_err("Failed to trigger the PBS sequence\n");
  337. pr_debug("PBS event triggered\n");
  338. } else {
  339. pr_debug("OLEDB_SPARE_CTL register bit not set\n");
  340. }
  341. }
  342. oledb->mod_enable = false;
  343. return rc;
  344. }
  345. static int qpnp_oledb_regulator_is_enabled(struct regulator_dev *rdev)
  346. {
  347. struct qpnp_oledb *oledb = rdev_get_drvdata(rdev);
  348. return oledb->mod_enable;
  349. }
  350. static int qpnp_oledb_regulator_set_voltage(struct regulator_dev *rdev,
  351. int min_uV, int max_uV, unsigned int *selector)
  352. {
  353. u8 val;
  354. int rc = 0;
  355. struct qpnp_oledb *oledb = rdev_get_drvdata(rdev);
  356. if (oledb->swire_control || oledb->secure_mode)
  357. return 0;
  358. val = DIV_ROUND_UP(min_uV - OLEDB_VOUT_MIN_MV, OLEDB_VOUT_STEP_MV);
  359. rc = qpnp_oledb_write(oledb, oledb->base + OLEDB_VOUT_PGM,
  360. &val, 1);
  361. if (rc < 0) {
  362. pr_err("Failed to write VOUT_PGM rc=%d\n", rc);
  363. return rc;
  364. }
  365. oledb->current_voltage = min_uV;
  366. pr_debug("register-control mode, current voltage %d\n",
  367. oledb->current_voltage);
  368. return 0;
  369. }
  370. static int qpnp_oledb_regulator_get_voltage(struct regulator_dev *rdev)
  371. {
  372. struct qpnp_oledb *oledb = rdev_get_drvdata(rdev);
  373. if (oledb->swire_control)
  374. return 0;
  375. return oledb->current_voltage;
  376. }
  377. static struct regulator_ops qpnp_oledb_ops = {
  378. .enable = qpnp_oledb_regulator_enable,
  379. .disable = qpnp_oledb_regulator_disable,
  380. .is_enabled = qpnp_oledb_regulator_is_enabled,
  381. .set_voltage = qpnp_oledb_regulator_set_voltage,
  382. .get_voltage = qpnp_oledb_regulator_get_voltage,
  383. };
  384. static int qpnp_oledb_register_regulator(struct qpnp_oledb *oledb)
  385. {
  386. int rc = 0;
  387. struct platform_device *pdev = oledb->pdev;
  388. struct regulator_init_data *init_data;
  389. struct regulator_desc *rdesc = &oledb->rdesc;
  390. struct regulator_config cfg = {};
  391. init_data = of_get_regulator_init_data(&pdev->dev,
  392. pdev->dev.of_node, rdesc);
  393. if (!init_data) {
  394. pr_err("Unable to get OLEDB regulator init data\n");
  395. return -ENOMEM;
  396. }
  397. if (init_data->constraints.name) {
  398. rdesc->owner = THIS_MODULE;
  399. rdesc->type = REGULATOR_VOLTAGE;
  400. rdesc->ops = &qpnp_oledb_ops;
  401. rdesc->name = init_data->constraints.name;
  402. cfg.dev = &pdev->dev;
  403. cfg.init_data = init_data;
  404. cfg.driver_data = oledb;
  405. cfg.of_node = pdev->dev.of_node;
  406. if (of_get_property(pdev->dev.of_node, "parent-supply",
  407. NULL))
  408. init_data->supply_regulator = "parent";
  409. init_data->constraints.valid_ops_mask
  410. |= REGULATOR_CHANGE_VOLTAGE |
  411. REGULATOR_CHANGE_STATUS;
  412. oledb->rdev = devm_regulator_register(oledb->dev, rdesc, &cfg);
  413. if (IS_ERR(oledb->rdev)) {
  414. rc = PTR_ERR(oledb->rdev);
  415. oledb->rdev = NULL;
  416. pr_err("Unable to register OLEDB regulator, rc = %d\n",
  417. rc);
  418. return rc;
  419. }
  420. } else {
  421. pr_err("OLEDB regulator name missing\n");
  422. return -EINVAL;
  423. }
  424. return 0;
  425. }
  426. static int qpnp_oledb_get_curr_voltage(struct qpnp_oledb *oledb,
  427. u16 *current_voltage)
  428. {
  429. int rc = 0;
  430. u8 val;
  431. if (!(oledb->mod_enable || oledb->ext_pinctl_state)) {
  432. rc = qpnp_oledb_read(oledb, oledb->base + OLEDB_VOUT_DEFAULT,
  433. &val, 1);
  434. if (rc < 0) {
  435. pr_err("Failed to read VOUT_DEFAULT rc=%d\n", rc);
  436. return rc;
  437. }
  438. } else {
  439. rc = qpnp_oledb_read(oledb, oledb->base +
  440. OLEDB_VOUT_PGM, &val, 1);
  441. if (rc < 0) {
  442. pr_err("Failed to read VOUT_PGM rc=%d\n", rc);
  443. return rc;
  444. }
  445. }
  446. *current_voltage = (val * OLEDB_VOUT_STEP_MV) + OLEDB_VOUT_MIN_MV;
  447. return rc;
  448. }
  449. static int qpnp_oledb_init_nlimit(struct qpnp_oledb *oledb)
  450. {
  451. int rc = 0, i = 0;
  452. u32 val, mask = 0;
  453. if (oledb->nlimit_enable != -EINVAL) {
  454. val = oledb->nlimit_enable <<
  455. OLEDB_ENABLE_NLIMIT_BIT_SHIFT;
  456. mask = OLEDB_ENABLE_NLIMIT_BIT;
  457. if (oledb->negative_curr_limit != -EINVAL) {
  458. for (i = 0; i < ARRAY_SIZE(oledb_nlimit_ma); i++) {
  459. if (oledb->negative_curr_limit ==
  460. oledb_nlimit_ma[i])
  461. break;
  462. }
  463. val |= i;
  464. mask |= OLEDB_NLIMIT_PGM_MASK;
  465. }
  466. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  467. OLEDB_NLIMIT, mask, val);
  468. if (rc < 0)
  469. pr_err("Failed to write NLIMT rc=%d\n", rc);
  470. }
  471. return rc;
  472. }
  473. static int qpnp_oledb_init_psm(struct qpnp_oledb *oledb)
  474. {
  475. int rc = 0, i = 0;
  476. u32 val = 0, mask = 0, temp = 0;
  477. struct qpnp_oledb_psm_ctl *psm_ctl = &oledb->psm_ctl;
  478. if (psm_ctl->psm_enable == -EINVAL)
  479. return rc;
  480. if (psm_ctl->psm_enable) {
  481. val = OLEDB_PSM_ENABLE_BIT;
  482. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  483. OLEDB_EN_PSM, OLEDB_PSM_ENABLE_BIT, val);
  484. if (rc < 0) {
  485. pr_err("Failed to write PSM_EN rc=%d\n", rc);
  486. return rc;
  487. }
  488. val = 0;
  489. if (psm_ctl->psm_vref != -EINVAL) {
  490. for (i = 0; i < ARRAY_SIZE(oledb_psm_vref_mv); i++) {
  491. if (psm_ctl->psm_vref ==
  492. oledb_psm_vref_mv[i])
  493. break;
  494. }
  495. val = i;
  496. mask = OLEDB_VREF_PSM_MASK;
  497. }
  498. if (psm_ctl->psm_hys_ctl != -EINVAL) {
  499. temp = PSM_HYSTERYSIS_MV_TO_VAL(psm_ctl->psm_hys_ctl);
  500. val |= (temp << OLEDB_PSM_HYSTERYSIS_CTL_BIT_SHIFT);
  501. mask |= OLEDB_PSM_HYSTERYSIS_CTL_BIT;
  502. }
  503. if (val) {
  504. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  505. OLEDB_PSM_CTL, mask, val);
  506. if (rc < 0)
  507. pr_err("Failed to write PSM_CTL rc=%d\n", rc);
  508. }
  509. } else {
  510. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  511. OLEDB_EN_PSM, OLEDB_PSM_ENABLE_BIT, 0);
  512. if (rc < 0)
  513. pr_err("Failed to write PSM_CTL rc=%d\n", rc);
  514. }
  515. return rc;
  516. }
  517. static int qpnp_oledb_init_pfm(struct qpnp_oledb *oledb)
  518. {
  519. int rc = 0, i = 0;
  520. u32 val = 0, temp = 0, mask = 0;
  521. struct qpnp_oledb_pfm_ctl *pfm_ctl = &oledb->pfm_ctl;
  522. if (pfm_ctl->pfm_enable == -EINVAL)
  523. return rc;
  524. if (pfm_ctl->pfm_enable) {
  525. mask = val = OLEDB_PFM_ENABLE_BIT;
  526. if (pfm_ctl->pfm_hys_ctl != -EINVAL) {
  527. temp = PFM_HYSTERYSIS_MV_TO_VAL(pfm_ctl->pfm_hys_ctl);
  528. val |= temp <<
  529. OLEDB_PFM_HYSTERYSIS_CTL_BIT_SHIFT;
  530. mask |= OLEDB_PFM_HYSTERYSIS_CTRL_BIT_MASK;
  531. }
  532. if (pfm_ctl->pfm_curr_limit != -EINVAL) {
  533. for (i = 0; i < ARRAY_SIZE(oledb_pfm_curr_limit_ma);
  534. i++) {
  535. if (pfm_ctl->pfm_curr_limit ==
  536. oledb_pfm_curr_limit_ma[i])
  537. break;
  538. }
  539. val |= (i << OLEDB_PFM_CURR_LIMIT_SHIFT);
  540. mask |= OLEDB_PFM_CURR_LIMIT_MASK;
  541. }
  542. if (pfm_ctl->pfm_off_time != -EINVAL) {
  543. val |= PFM_OFF_TIME_NS_TO_VAL(pfm_ctl->pfm_off_time);
  544. mask |= OLEDB_PFM_OFF_TIME_NS_MASK;
  545. }
  546. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  547. OLEDB_PFM_CTL, mask, val);
  548. if (rc < 0)
  549. pr_err("Failed to write PFM_CTL rc=%d\n", rc);
  550. } else {
  551. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  552. OLEDB_PFM_CTL, OLEDB_PFM_ENABLE_BIT, 0);
  553. if (rc < 0)
  554. pr_err("Failed to write PFM_CTL rc=%d\n", rc);
  555. }
  556. return rc;
  557. }
  558. static int qpnp_oledb_init_fast_precharge(struct qpnp_oledb *oledb)
  559. {
  560. int rc = 0;
  561. u32 val = 0, temp = 0, mask = 0;
  562. struct qpnp_oledb_fast_precharge_ctl *prechg_ctl =
  563. &oledb->fast_prechg_ctl;
  564. if (prechg_ctl->fast_prechg_ppulse_en == -EINVAL)
  565. return rc;
  566. if (prechg_ctl->fast_prechg_ppulse_en) {
  567. mask = val = OLEDB_FAST_PRECHG_PPULSE_EN_BIT;
  568. if (prechg_ctl->prechg_debounce_time != -EINVAL) {
  569. temp = PRECHG_DEBOUNCE_TIME_MS_TO_VAL(
  570. prechg_ctl->prechg_debounce_time);
  571. val |= temp << OLEDB_DBNC_PRECHARGE_SHIFT;
  572. mask |= OLEDB_DBNC_PRECHARGE_MASK;
  573. }
  574. if (prechg_ctl->prechg_pulse_period != -EINVAL) {
  575. temp = PRECHG_PULSE_PERIOD_US_TO_VAL(
  576. prechg_ctl->prechg_pulse_period);
  577. val |= temp << OLEDB_PRECHARGE_PULSE_PERIOD_SHIFT;
  578. mask |= OLEDB_PRECHARGE_PULSE_PERIOD_MASK;
  579. }
  580. if (prechg_ctl->prechg_pulse_on_time != -EINVAL) {
  581. val |= PRECHG_PULSE_ON_TIME_NS_TO_VAL(
  582. prechg_ctl->prechg_pulse_on_time);
  583. mask |= OLEDB_PRECHARGE_PULSE_TON_MASK;
  584. }
  585. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  586. OLEDB_FAST_PRECHARGE, mask, val);
  587. if (rc < 0)
  588. pr_err("Failed to write FAST_PRECHARGE rc=%d\n", rc);
  589. } else {
  590. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  591. OLEDB_FAST_PRECHARGE,
  592. OLEDB_FAST_PRECHG_PPULSE_EN_BIT, 0);
  593. if (rc < 0)
  594. pr_err("Failed to write FAST_PRECHARGE rc=%d\n", rc);
  595. }
  596. return rc;
  597. }
  598. static int qpnp_oledb_hw_init(struct qpnp_oledb *oledb)
  599. {
  600. int rc, i = 0;
  601. u8 val = 0, mask = 0;
  602. u16 current_voltage;
  603. if (oledb->default_voltage != -EINVAL) {
  604. val = (oledb->default_voltage - OLEDB_VOUT_MIN_MV) /
  605. OLEDB_VOUT_STEP_MV;
  606. rc = qpnp_oledb_write(oledb, oledb->base +
  607. OLEDB_VOUT_DEFAULT, &val, 1);
  608. if (rc < 0) {
  609. pr_err("Failed to write VOUT_DEFAULT rc=%d\n", rc);
  610. return rc;
  611. }
  612. }
  613. rc = qpnp_oledb_read(oledb, oledb->base + OLEDB_MODULE_ENABLE,
  614. &oledb->mod_enable, 1);
  615. if (rc < 0) {
  616. pr_err("Failed to read MODULE_ENABLE rc=%d\n", rc);
  617. return rc;
  618. }
  619. rc = qpnp_oledb_read(oledb, oledb->base + OLEDB_EXT_PIN_CTL,
  620. &oledb->ext_pinctl_state, 1);
  621. if (rc < 0) {
  622. pr_err("Failed to read EXT_PIN_CTL rc=%d\n", rc);
  623. return rc;
  624. }
  625. rc = qpnp_oledb_get_curr_voltage(oledb, &current_voltage);
  626. if (rc < 0)
  627. return rc;
  628. /*
  629. * Go through if the module is not enabled either through
  630. * external pin control or SPMI interface.
  631. */
  632. if (!((oledb->ext_pinctl_state & OLEDB_EXT_PIN_CTL_BIT)
  633. || oledb->mod_enable)) {
  634. if (oledb->warmup_delay != -EINVAL) {
  635. for (i = 0; i < ARRAY_SIZE(oledb_warmup_dly_ns); i++) {
  636. if (oledb->warmup_delay ==
  637. oledb_warmup_dly_ns[i])
  638. break;
  639. }
  640. val = i;
  641. rc = qpnp_oledb_masked_write(oledb,
  642. oledb->base + OLEDB_BIAS_GEN_WARMUP_DELAY,
  643. OLEDB_BIAS_GEN_WARMUP_DELAY_MASK, val);
  644. if (rc < 0) {
  645. pr_err("Failed to write WARMUP_DELAY rc=%d\n",
  646. rc);
  647. return rc;
  648. }
  649. } else {
  650. rc = qpnp_oledb_read(oledb, oledb->base +
  651. OLEDB_BIAS_GEN_WARMUP_DELAY,
  652. &val, 1);
  653. if (rc < 0) {
  654. pr_err("Failed to read WARMUP_DELAY rc=%d\n",
  655. rc);
  656. return rc;
  657. }
  658. oledb->warmup_delay = oledb_warmup_dly_ns[val];
  659. }
  660. if (oledb->peak_curr_limit != -EINVAL) {
  661. for (i = 0; i < ARRAY_SIZE(oledb_peak_curr_limit_ma);
  662. i++) {
  663. if (oledb->peak_curr_limit ==
  664. oledb_peak_curr_limit_ma[i])
  665. break;
  666. }
  667. val = i;
  668. rc = qpnp_oledb_write(oledb,
  669. oledb->base + OLEDB_ILIM_NFET,
  670. &val, 1);
  671. if (rc < 0) {
  672. pr_err("Failed to write ILIM_NEFT rc=%d\n", rc);
  673. return rc;
  674. }
  675. }
  676. if (oledb->pd_ctl != -EINVAL) {
  677. val = oledb->pd_ctl;
  678. rc = qpnp_oledb_write(oledb, oledb->base +
  679. OLEDB_PD_CTL, &val, 1);
  680. if (rc < 0) {
  681. pr_err("Failed to write PD_CTL rc=%d\n", rc);
  682. return rc;
  683. }
  684. }
  685. if (oledb->sc_en != -EINVAL) {
  686. val = oledb->sc_en ? OLEDB_ENABLE_SC_DETECTION_BIT : 0;
  687. mask = OLEDB_ENABLE_SC_DETECTION_BIT;
  688. if (oledb->sc_dbnc_time != -EINVAL) {
  689. val |= SHORT_CIRCUIT_DEBOUNCE_TIME_TO_VAL(
  690. oledb->sc_dbnc_time);
  691. mask |= OLEDB_DBNC_PRECHARGE_MASK;
  692. }
  693. rc = qpnp_oledb_write(oledb, oledb->base +
  694. OLEDB_SHORT_PROTECT, &val, 1);
  695. if (rc < 0) {
  696. pr_err("Failed to write SHORT_PROTECT rc=%d\n",
  697. rc);
  698. return rc;
  699. }
  700. }
  701. rc = qpnp_oledb_init_nlimit(oledb);
  702. if (rc < 0)
  703. return rc;
  704. rc = qpnp_oledb_init_psm(oledb);
  705. if (rc < 0)
  706. return rc;
  707. rc = qpnp_oledb_init_pfm(oledb);
  708. if (rc < 0)
  709. return rc;
  710. rc = qpnp_oledb_init_fast_precharge(oledb);
  711. if (rc < 0)
  712. return rc;
  713. if (oledb->swire_control) {
  714. val = OLEDB_EN_SWIRE_PD_UPD_BIT |
  715. OLEDB_EN_SWIRE_VOUT_UPD_BIT;
  716. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  717. OLEDB_SWIRE_CONTROL, OLEDB_EN_SWIRE_PD_UPD_BIT |
  718. OLEDB_EN_SWIRE_VOUT_UPD_BIT, val);
  719. if (rc < 0)
  720. return rc;
  721. }
  722. rc = qpnp_oledb_read(oledb, oledb->base + OLEDB_MODULE_RDY,
  723. &val, 1);
  724. if (rc < 0) {
  725. pr_err("Failed to read MODULE_RDY rc=%d\n", rc);
  726. return rc;
  727. }
  728. if (!(val & OLEDB_MODULE_RDY_BIT)) {
  729. val = OLEDB_MODULE_RDY_BIT;
  730. rc = qpnp_oledb_write(oledb, oledb->base +
  731. OLEDB_MODULE_RDY, &val, 1);
  732. if (rc < 0) {
  733. pr_err("Failed to write MODULE_RDY rc=%d\n",
  734. rc);
  735. return rc;
  736. }
  737. }
  738. if (!oledb->dynamic_ext_pinctl_config) {
  739. if (oledb->ext_pin_control) {
  740. val = OLEDB_EXT_PIN_CTL_BIT;
  741. rc = qpnp_oledb_write(oledb, oledb->base +
  742. OLEDB_EXT_PIN_CTL, &val, 1);
  743. if (rc < 0) {
  744. pr_err("Failed to write EXT_PIN_CTL rc=%d\n",
  745. rc);
  746. return rc;
  747. }
  748. } else {
  749. val = OLEDB_MODULE_ENABLE_BIT;
  750. rc = qpnp_oledb_write(oledb, oledb->base +
  751. OLEDB_MODULE_ENABLE, &val, 1);
  752. if (rc < 0) {
  753. pr_err("Failed to write MODULE_ENABLE rc=%d\n",
  754. rc);
  755. return rc;
  756. }
  757. ndelay(oledb->warmup_delay);
  758. }
  759. oledb->mod_enable = true;
  760. if (oledb->pbs_control) {
  761. rc = qpnp_oledb_masked_write(oledb,
  762. oledb->base + OLEDB_SWIRE_CONTROL,
  763. OLEDB_EN_SWIRE_PD_UPD_BIT |
  764. OLEDB_EN_SWIRE_VOUT_UPD_BIT, 0);
  765. if (rc < 0) {
  766. pr_err("Failed to write SWIRE_CTL rc=%d\n",
  767. rc);
  768. return rc;
  769. }
  770. }
  771. }
  772. oledb->current_voltage = current_voltage;
  773. } else {
  774. /* module is enabled */
  775. if (oledb->current_voltage == -EINVAL) {
  776. oledb->current_voltage = current_voltage;
  777. } else if (!oledb->swire_control) {
  778. if (oledb->current_voltage < OLEDB_VOUT_MIN_MV) {
  779. pr_err("current_voltage %d is less than min_volt %d\n",
  780. oledb->current_voltage, OLEDB_VOUT_MIN_MV);
  781. return -EINVAL;
  782. }
  783. val = DIV_ROUND_UP(oledb->current_voltage -
  784. OLEDB_VOUT_MIN_MV, OLEDB_VOUT_STEP_MV);
  785. rc = qpnp_oledb_write(oledb, oledb->base +
  786. OLEDB_VOUT_PGM, &val, 1);
  787. if (rc < 0) {
  788. pr_err("Failed to write VOUT_PGM rc=%d\n",
  789. rc);
  790. return rc;
  791. }
  792. }
  793. oledb->mod_enable = true;
  794. }
  795. return rc;
  796. }
  797. static int qpnp_oledb_parse_nlimit(struct qpnp_oledb *oledb)
  798. {
  799. int rc = 0;
  800. struct device_node *of_node = oledb->dev->of_node;
  801. oledb->nlimit_enable = -EINVAL;
  802. rc = of_property_read_u32(of_node, "qcom,negative-curr-limit-enable",
  803. &oledb->nlimit_enable);
  804. if (!rc) {
  805. oledb->negative_curr_limit = -EINVAL;
  806. rc = of_property_read_u32(of_node,
  807. "qcom,negative-curr-limit-ma",
  808. &oledb->negative_curr_limit);
  809. if (!rc) {
  810. u16 min_curr_limit = oledb_nlimit_ma[0];
  811. u16 max_curr_limit = oledb_nlimit_ma[ARRAY_SIZE(
  812. oledb_nlimit_ma) - 1];
  813. if (oledb->negative_curr_limit < min_curr_limit ||
  814. oledb->negative_curr_limit > max_curr_limit) {
  815. pr_err("Invalid value in qcom,negative-curr-limit-ma\n");
  816. return -EINVAL;
  817. }
  818. }
  819. }
  820. return 0;
  821. }
  822. static int qpnp_oledb_parse_psm(struct qpnp_oledb *oledb)
  823. {
  824. int rc = 0;
  825. struct qpnp_oledb_psm_ctl *psm_ctl = &oledb->psm_ctl;
  826. struct device_node *of_node = oledb->dev->of_node;
  827. psm_ctl->psm_enable = -EINVAL;
  828. rc = of_property_read_u32(of_node, "qcom,psm-enable",
  829. &psm_ctl->psm_enable);
  830. if (!rc) {
  831. psm_ctl->psm_hys_ctl = -EINVAL;
  832. rc = of_property_read_u32(of_node, "qcom,psm-hys-mv",
  833. &psm_ctl->psm_hys_ctl);
  834. if (!rc) {
  835. if (psm_ctl->psm_hys_ctl < OLEDB_PSM_HYS_CTRL_MIN ||
  836. psm_ctl->psm_hys_ctl > OLEDB_PSM_HYS_CTRL_MAX) {
  837. pr_err("Invalid value in qcom,psm-hys-mv\n");
  838. return -EINVAL;
  839. }
  840. }
  841. psm_ctl->psm_vref = -EINVAL;
  842. rc = of_property_read_u32(of_node, "qcom,psm-vref-mv",
  843. &psm_ctl->psm_vref);
  844. if (!rc) {
  845. u16 min_vref = oledb_psm_vref_mv[0];
  846. u16 max_vref = oledb_psm_vref_mv[ARRAY_SIZE(
  847. oledb_psm_vref_mv) - 1];
  848. if (psm_ctl->psm_vref < min_vref ||
  849. psm_ctl->psm_vref > max_vref) {
  850. pr_err("Invalid value in qcom,psm-vref-mv\n");
  851. return -EINVAL;
  852. }
  853. }
  854. }
  855. return 0;
  856. }
  857. static int qpnp_oledb_parse_pfm(struct qpnp_oledb *oledb)
  858. {
  859. int rc = 0;
  860. struct qpnp_oledb_pfm_ctl *pfm_ctl = &oledb->pfm_ctl;
  861. struct device_node *of_node = oledb->dev->of_node;
  862. pfm_ctl->pfm_enable = -EINVAL;
  863. rc = of_property_read_u32(of_node, "qcom,pfm-enable",
  864. &pfm_ctl->pfm_enable);
  865. if (!rc) {
  866. pfm_ctl->pfm_hys_ctl = -EINVAL;
  867. rc = of_property_read_u32(of_node, "qcom,pfm-hys-mv",
  868. &pfm_ctl->pfm_hys_ctl);
  869. if (!rc) {
  870. if (pfm_ctl->pfm_hys_ctl < OLEDB_PFM_HYS_CTRL_MIN ||
  871. pfm_ctl->pfm_hys_ctl > OLEDB_PFM_HYS_CTRL_MAX) {
  872. pr_err("Invalid value in qcom,pfm-hys-mv\n");
  873. return -EINVAL;
  874. }
  875. }
  876. pfm_ctl->pfm_curr_limit = -EINVAL;
  877. rc = of_property_read_u32(of_node,
  878. "qcom,pfm-curr-limit-ma", &pfm_ctl->pfm_curr_limit);
  879. if (!rc) {
  880. u16 min_limit = oledb_pfm_curr_limit_ma[0];
  881. u16 max_limit = oledb_pfm_curr_limit_ma[ARRAY_SIZE(
  882. oledb_pfm_curr_limit_ma) - 1];
  883. if (pfm_ctl->pfm_curr_limit < min_limit ||
  884. pfm_ctl->pfm_curr_limit > max_limit) {
  885. pr_err("Invalid value in qcom,pfm-curr-limit-ma\n");
  886. return -EINVAL;
  887. }
  888. }
  889. pfm_ctl->pfm_off_time = -EINVAL;
  890. rc = of_property_read_u32(of_node, "qcom,pfm-off-time-ns",
  891. &pfm_ctl->pfm_off_time);
  892. if (!rc) {
  893. if (pfm_ctl->pfm_off_time < OLEDB_PFM_OFF_TIME_MIN ||
  894. pfm_ctl->pfm_off_time > OLEDB_PFM_OFF_TIME_MAX) {
  895. pr_err("Invalid value in qcom,pfm-off-time-ns\n");
  896. return -EINVAL;
  897. }
  898. }
  899. }
  900. return 0;
  901. }
  902. static int qpnp_oledb_parse_fast_precharge(struct qpnp_oledb *oledb)
  903. {
  904. int rc = 0;
  905. struct device_node *of_node = oledb->dev->of_node;
  906. struct qpnp_oledb_fast_precharge_ctl *fast_prechg =
  907. &oledb->fast_prechg_ctl;
  908. fast_prechg->fast_prechg_ppulse_en = -EINVAL;
  909. rc = of_property_read_u32(of_node, "qcom,fast-precharge-ppulse-enable",
  910. &fast_prechg->fast_prechg_ppulse_en);
  911. if (!rc) {
  912. fast_prechg->prechg_debounce_time = -EINVAL;
  913. rc = of_property_read_u32(of_node,
  914. "qcom,precharge-debounce-time-ms",
  915. &fast_prechg->prechg_debounce_time);
  916. if (!rc) {
  917. int dbnc_time = fast_prechg->prechg_debounce_time;
  918. if (dbnc_time < OLEDB_PRECHG_TIME_MIN || dbnc_time >
  919. OLEDB_PRECHG_TIME_MAX) {
  920. pr_err("Invalid value in qcom,precharge-debounce-time-ms\n");
  921. return -EINVAL;
  922. }
  923. }
  924. fast_prechg->prechg_pulse_period = -EINVAL;
  925. rc = of_property_read_u32(of_node,
  926. "qcom,precharge-pulse-period-us",
  927. &fast_prechg->prechg_pulse_period);
  928. if (!rc) {
  929. int pulse_period = fast_prechg->prechg_pulse_period;
  930. if (pulse_period < OLEDB_PRECHG_PULSE_PERIOD_MIN ||
  931. pulse_period > OLEDB_PRECHG_PULSE_PERIOD_MAX) {
  932. pr_err("Invalid value in qcom,precharge-pulse-period-us\n");
  933. return -EINVAL;
  934. }
  935. }
  936. fast_prechg->prechg_pulse_on_time = -EINVAL;
  937. rc = of_property_read_u32(of_node,
  938. "qcom,precharge-pulse-on-time-ns",
  939. &fast_prechg->prechg_pulse_on_time);
  940. if (!rc) {
  941. int pulse_on_time = fast_prechg->prechg_pulse_on_time;
  942. if (pulse_on_time < OLEDB_PRECHG_PULSE_ON_TIME_MIN ||
  943. pulse_on_time > OLEDB_PRECHG_PULSE_ON_TIME_MAX) {
  944. pr_err("Invalid value in qcom,precharge-pulse-on-time-ns\n");
  945. return -EINVAL;
  946. }
  947. }
  948. }
  949. return 0;
  950. }
  951. static int qpnp_oledb_parse_dt(struct qpnp_oledb *oledb)
  952. {
  953. int rc = 0;
  954. struct device_node *revid_dev_node;
  955. struct device_node *of_node = oledb->dev->of_node;
  956. revid_dev_node = of_parse_phandle(oledb->dev->of_node,
  957. "qcom,pmic-revid", 0);
  958. if (!revid_dev_node) {
  959. pr_err("Missing qcom,pmic-revid property - driver failed\n");
  960. return -EINVAL;
  961. }
  962. oledb->pmic_rev_id = get_revid_data(revid_dev_node);
  963. if (IS_ERR(oledb->pmic_rev_id)) {
  964. pr_debug("Unable to get revid data\n");
  965. return -EPROBE_DEFER;
  966. }
  967. oledb->swire_control =
  968. of_property_read_bool(of_node, "qcom,swire-control");
  969. oledb->ext_pin_control =
  970. of_property_read_bool(of_node, "qcom,ext-pin-control");
  971. if (oledb->ext_pin_control)
  972. oledb->dynamic_ext_pinctl_config =
  973. of_property_read_bool(of_node,
  974. "qcom,dynamic-ext-pinctl-config");
  975. oledb->pbs_control =
  976. of_property_read_bool(of_node, "qcom,pbs-control");
  977. /* Use the force_pd_control only for PM660A versions <= v2.0 */
  978. if (oledb->pmic_rev_id->pmic_subtype == PM660L_SUBTYPE &&
  979. oledb->pmic_rev_id->rev4 <= PM660L_V2P0_REV4) {
  980. if (!(oledb->pmic_rev_id->rev4 == PM660L_V2P0_REV4 &&
  981. oledb->pmic_rev_id->rev2 > PM660L_V2P0_REV2)) {
  982. oledb->force_pd_control = true;
  983. }
  984. }
  985. if (oledb->force_pd_control) {
  986. oledb->pbs_dev_node = of_parse_phandle(of_node,
  987. "qcom,pbs-client", 0);
  988. if (!oledb->pbs_dev_node) {
  989. pr_err("Missing qcom,pbs-client property\n");
  990. return -EINVAL;
  991. }
  992. }
  993. oledb->current_voltage = -EINVAL;
  994. rc = of_property_read_u32(of_node, "qcom,oledb-init-voltage-mv",
  995. &oledb->current_voltage);
  996. if (!rc && (oledb->current_voltage < OLEDB_VOUT_MIN_MV ||
  997. oledb->current_voltage > OLEDB_VOUT_MAX_MV)) {
  998. pr_err("Invalid value in qcom,oledb-init-voltage-mv\n");
  999. return -EINVAL;
  1000. }
  1001. oledb->default_voltage = -EINVAL;
  1002. rc = of_property_read_u32(of_node, "qcom,oledb-default-voltage-mv",
  1003. &oledb->default_voltage);
  1004. if (!rc && (oledb->default_voltage < OLEDB_VOUT_MIN_MV ||
  1005. oledb->default_voltage > OLEDB_VOUT_MAX_MV)) {
  1006. pr_err("Invalid value in qcom,oledb-default-voltage-mv\n");
  1007. return -EINVAL;
  1008. }
  1009. oledb->warmup_delay = -EINVAL;
  1010. rc = of_property_read_u32(of_node, "qcom,bias-gen-warmup-delay-ns",
  1011. &oledb->warmup_delay);
  1012. if (!rc) {
  1013. u16 min_delay = oledb_warmup_dly_ns[0];
  1014. u16 max_delay = oledb_warmup_dly_ns[ARRAY_SIZE(
  1015. oledb_warmup_dly_ns) - 1];
  1016. if (oledb->warmup_delay < min_delay ||
  1017. oledb->warmup_delay > max_delay) {
  1018. pr_err("Invalid value in qcom,bias-gen-warmup-delay-ns\n");
  1019. return -EINVAL;
  1020. }
  1021. }
  1022. oledb->peak_curr_limit = -EINVAL;
  1023. rc = of_property_read_u32(of_node, "qcom,peak-curr-limit-ma",
  1024. &oledb->peak_curr_limit);
  1025. if (!rc) {
  1026. u16 min_limit = oledb_peak_curr_limit_ma[0];
  1027. u16 max_limit = oledb_peak_curr_limit_ma[ARRAY_SIZE(
  1028. oledb_peak_curr_limit_ma) - 1];
  1029. if (oledb->peak_curr_limit < min_limit ||
  1030. oledb->peak_curr_limit > max_limit) {
  1031. pr_err("Invalid value in qcom,peak-curr-limit-ma\n");
  1032. return -EINVAL;
  1033. }
  1034. }
  1035. oledb->pd_ctl = -EINVAL;
  1036. of_property_read_u32(of_node, "qcom,pull-down-enable", &oledb->pd_ctl);
  1037. oledb->sc_en = -EINVAL;
  1038. rc = of_property_read_u32(of_node, "qcom,enable-short-circuit",
  1039. &oledb->sc_en);
  1040. if (!rc) {
  1041. oledb->sc_dbnc_time = -EINVAL;
  1042. rc = of_property_read_u32(of_node,
  1043. "qcom,short-circuit-dbnc-time", &oledb->sc_dbnc_time);
  1044. if (!rc) {
  1045. if (oledb->sc_dbnc_time < OLEDB_MIN_SC_DBNC_TIME_FSW ||
  1046. oledb->sc_dbnc_time > OLEDB_MAX_SC_DBNC_TIME_FSW) {
  1047. pr_err("Invalid value in qcom,short-circuit-dbnc-time\n");
  1048. return -EINVAL;
  1049. }
  1050. }
  1051. }
  1052. rc = qpnp_oledb_parse_nlimit(oledb);
  1053. if (rc < 0)
  1054. return rc;
  1055. rc = qpnp_oledb_parse_psm(oledb);
  1056. if (rc < 0)
  1057. return rc;
  1058. rc = qpnp_oledb_parse_pfm(oledb);
  1059. if (rc < 0)
  1060. return rc;
  1061. rc = qpnp_oledb_parse_fast_precharge(oledb);
  1062. return rc;
  1063. }
  1064. static int qpnp_oledb_force_pulldown_config(struct qpnp_oledb *oledb)
  1065. {
  1066. int rc = 0;
  1067. u8 val;
  1068. val = 1;
  1069. rc = qpnp_oledb_write(oledb, oledb->base + OLEDB_PD_CTL,
  1070. &val, 1);
  1071. if (rc < 0) {
  1072. pr_err("Failed to write PD_CTL rc=%d\n", rc);
  1073. return rc;
  1074. }
  1075. rc = qpnp_oledb_masked_write(oledb, oledb->base +
  1076. OLEDB_SWIRE_CONTROL, OLEDB_EN_SWIRE_PD_UPD_BIT, 0);
  1077. if (rc < 0)
  1078. pr_err("Failed to write SWIRE_CTL for pbs mode rc=%d\n",
  1079. rc);
  1080. return rc;
  1081. }
  1082. static int qpnp_labibb_notifier_cb(struct notifier_block *nb,
  1083. unsigned long action, void *data)
  1084. {
  1085. int rc = 0;
  1086. u8 val;
  1087. struct qpnp_oledb *oledb = container_of(nb, struct qpnp_oledb,
  1088. oledb_nb);
  1089. if (oledb->secure_mode)
  1090. return 0;
  1091. if (action == LAB_VREG_NOT_OK) {
  1092. /* short circuit detected. Disable OLEDB module */
  1093. val = 0;
  1094. rc = qpnp_oledb_write(oledb, oledb->base + OLEDB_MODULE_RDY,
  1095. &val, 1);
  1096. if (rc < 0) {
  1097. pr_err("Failed to write MODULE_RDY rc=%d\n", rc);
  1098. return NOTIFY_STOP;
  1099. }
  1100. oledb->lab_sc_detected = true;
  1101. oledb->mod_enable = false;
  1102. pr_crit("LAB SC detected, disabling OLEDB forever!\n");
  1103. }
  1104. if (action == LAB_VREG_OK) {
  1105. /* Disable SWIRE pull down control and enable via spmi mode */
  1106. rc = qpnp_oledb_force_pulldown_config(oledb);
  1107. if (rc < 0) {
  1108. pr_err("Failed to config force pull down\n");
  1109. return NOTIFY_STOP;
  1110. }
  1111. }
  1112. return NOTIFY_OK;
  1113. }
  1114. static struct class_attribute oledb_attributes[] = {
  1115. [0] = __ATTR(secure_mode, 0664, NULL, NULL),
  1116. __ATTR_NULL,
  1117. };
  1118. static int qpnp_oledb_regulator_probe(struct platform_device *pdev)
  1119. {
  1120. int rc = 0;
  1121. u32 val;
  1122. struct qpnp_oledb *oledb;
  1123. struct device_node *of_node = pdev->dev.of_node;
  1124. oledb = devm_kzalloc(&pdev->dev,
  1125. sizeof(struct qpnp_oledb), GFP_KERNEL);
  1126. if (!oledb)
  1127. return -ENOMEM;
  1128. oledb->pdev = pdev;
  1129. oledb->dev = &pdev->dev;
  1130. oledb->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  1131. dev_set_drvdata(&pdev->dev, oledb);
  1132. if (!oledb->regmap) {
  1133. pr_err("Couldn't get parent's regmap\n");
  1134. return -EINVAL;
  1135. }
  1136. rc = of_property_read_u32(of_node, "reg", &val);
  1137. if (rc < 0) {
  1138. pr_err("Couldn't find reg in node, rc = %d\n", rc);
  1139. return rc;
  1140. }
  1141. mutex_init(&(oledb->bus_lock));
  1142. oledb->base = val;
  1143. rc = qpnp_oledb_parse_dt(oledb);
  1144. if (rc < 0) {
  1145. pr_err("Failed to parse common OLEDB device tree\n");
  1146. return rc;
  1147. }
  1148. rc = qpnp_oledb_hw_init(oledb);
  1149. if (rc < 0) {
  1150. pr_err("Failed to initialize OLEDB, rc=%d\n", rc);
  1151. return rc;
  1152. }
  1153. /* Enable LAB short circuit notification support */
  1154. if (oledb->pmic_rev_id->pmic_subtype == PM660L_SUBTYPE)
  1155. oledb->handle_lab_sc_notification = true;
  1156. if (oledb->force_pd_control || oledb->handle_lab_sc_notification) {
  1157. oledb->oledb_nb.notifier_call = qpnp_labibb_notifier_cb;
  1158. rc = qpnp_labibb_notifier_register(&oledb->oledb_nb);
  1159. if (rc < 0) {
  1160. pr_err("Failed to register qpnp_labibb_notifier_cb\n");
  1161. return rc;
  1162. }
  1163. }
  1164. rc = qpnp_oledb_register_regulator(oledb);
  1165. if (rc < 0) {
  1166. pr_err("Failed to register regulator rc=%d\n", rc);
  1167. goto out;
  1168. }
  1169. oledb->oledb_class.name = "amoled_bias";
  1170. oledb->oledb_class.owner = THIS_MODULE;
  1171. oledb->oledb_class.class_attrs = oledb_attributes;
  1172. rc = class_register(&oledb->oledb_class);
  1173. if (rc < 0) {
  1174. pr_err("Failed to register oledb class rc = %d\n", rc);
  1175. return rc;
  1176. }
  1177. pr_info("OLEDB registered successfully, ext_pin_en=%d mod_en=%d current_voltage=%d mV\n",
  1178. oledb->ext_pin_control, oledb->mod_enable,
  1179. oledb->current_voltage);
  1180. return 0;
  1181. out:
  1182. if (oledb->force_pd_control) {
  1183. rc = qpnp_labibb_notifier_unregister(&oledb->oledb_nb);
  1184. if (rc < 0)
  1185. pr_err("Failed to unregister lab_vreg_ok notifier\n");
  1186. }
  1187. return rc;
  1188. }
  1189. static int qpnp_oledb_regulator_remove(struct platform_device *pdev)
  1190. {
  1191. int rc = 0;
  1192. struct qpnp_oledb *oledb = platform_get_drvdata(pdev);
  1193. if (oledb->force_pd_control) {
  1194. rc = qpnp_labibb_notifier_unregister(&oledb->oledb_nb);
  1195. if (rc < 0)
  1196. pr_err("Failed to unregister lab_vreg_ok notifier\n");
  1197. }
  1198. return rc;
  1199. }
  1200. const struct of_device_id qpnp_oledb_regulator_match_table[] = {
  1201. { .compatible = QPNP_OLEDB_REGULATOR_DRIVER_NAME,},
  1202. { },
  1203. };
  1204. static struct platform_driver qpnp_oledb_regulator_driver = {
  1205. .driver = {
  1206. .name = QPNP_OLEDB_REGULATOR_DRIVER_NAME,
  1207. .of_match_table = qpnp_oledb_regulator_match_table,
  1208. },
  1209. .probe = qpnp_oledb_regulator_probe,
  1210. .remove = qpnp_oledb_regulator_remove,
  1211. };
  1212. static int __init qpnp_oledb_regulator_init(void)
  1213. {
  1214. return platform_driver_register(&qpnp_oledb_regulator_driver);
  1215. }
  1216. arch_initcall(qpnp_oledb_regulator_init);
  1217. static void __exit qpnp_oledb_regulator_exit(void)
  1218. {
  1219. platform_driver_unregister(&qpnp_oledb_regulator_driver);
  1220. }
  1221. module_exit(qpnp_oledb_regulator_exit);
  1222. MODULE_DESCRIPTION("QPNP OLEDB driver");
  1223. MODULE_LICENSE("GPL v2");
  1224. MODULE_ALIAS("qpnp-oledb-regulator");