qpnp-amoled-regulator.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "AMOLED: %s: " fmt, __func__
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/nvmem-consumer.h>
  10. #include <linux/of_address.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/regulator/debug-regulator.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/of_regulator.h>
  16. #include <linux/regulator/machine.h>
  17. /* Register definitions */
  18. #define PERIPH_REVISION4 0x03
  19. #define IBB_PERIPH_TYPE 0x20
  20. #define AB_PERIPH_TYPE 0x24
  21. #define OLEDB_PERIPH_TYPE 0x2C
  22. #define PERIPH_SUBTYPE 0x05
  23. /* AB */
  24. #define AB_LDO_PD_CTL(chip) (chip->ab_base + 0x78)
  25. /* AB_LDO_PD_CTL */
  26. #define PULLDN_EN_BIT BIT(7)
  27. /* IBB */
  28. #define IBB_PD_CTL(chip) (chip->ibb_base + 0x47)
  29. /* IBB_PD_CTL */
  30. #define ENABLE_PD_BIT BIT(7)
  31. #define IBB_DUAL_PHASE_CTL(chip) (chip->ibb_base + 0x70)
  32. /* IBB_DUAL_PHASE_CTL */
  33. #define IBB_DUAL_PHASE_CTL_MASK GENMASK(2, 0)
  34. #define AUTO_DUAL_PHASE_BIT BIT(2)
  35. #define FORCE_DUAL_PHASE_BIT BIT(1)
  36. #define FORCE_SINGLE_PHASE_BIT BIT(0)
  37. /* IBB SPUR FSM/SQM CTL */
  38. #define IBB_SPUR_CTL(chip) (chip->ibb_base + 0xB6)
  39. #define SPUR_FSM_EN BIT(7)
  40. #define SPUR_SQM_EN BIT(6)
  41. #define IBB_SPUR_FREQ_CTL(chip) (chip->ibb_base + 0xB7)
  42. #define FREQ_RES_SEL BIT(0)
  43. #define IBB_SPUR_FREQ_THRESH_HIGH(i) (chip->ibb_base + 0xB8 + i*2)
  44. #define IBB_SPUR_FREQ_THRESH_LOW(i) (chip->ibb_base + 0xB9 + i*2)
  45. #define MAX_SPUR_FREQ_BANDS 3
  46. #define MAX_SPUR_FREQ_KHZ 248
  47. #define AMOLED_SDAM_OFFSET 0xB8
  48. #define SQM_TIMER_LOWER_LIMIT_MS 100
  49. #define SQM_TIMER_UPPER_LIMIT_MS 10000
  50. enum {
  51. SPUR_MITIGATION_DISABLED,
  52. SPUR_MITIGATION_ENABLED_WITHOUT_SQM,
  53. SPUR_MITIGATION_ENABLED_WITH_SQM,
  54. };
  55. struct amoled_regulator {
  56. struct regulator_desc rdesc;
  57. struct regulator_dev *rdev;
  58. struct device_node *node;
  59. unsigned int mode;
  60. bool enabled;
  61. };
  62. struct oledb_regulator {
  63. struct amoled_regulator vreg;
  64. /* DT params */
  65. bool swire_control;
  66. };
  67. struct ab_regulator {
  68. struct amoled_regulator vreg;
  69. /* DT params */
  70. bool swire_control;
  71. bool pd_control;
  72. };
  73. struct ibb_regulator {
  74. struct amoled_regulator vreg;
  75. u8 subtype;
  76. u8 rev4;
  77. /* DT params */
  78. bool swire_control;
  79. bool pd_control;
  80. bool single_phase;
  81. /* ibb_spur_mitigation params */
  82. u32 spur_mitigation_level;
  83. u32 spur_sqm_timer_ms;
  84. u32 spur_freq_thresh_high[MAX_SPUR_FREQ_BANDS];
  85. u32 spur_freq_thresh_low[MAX_SPUR_FREQ_BANDS];
  86. bool spur_freq_res_sel;
  87. };
  88. struct qpnp_amoled {
  89. struct device *dev;
  90. struct regmap *regmap;
  91. struct oledb_regulator oledb;
  92. struct ab_regulator ab;
  93. struct ibb_regulator ibb;
  94. struct nvmem_cell *nvmem_cell;
  95. /* DT params */
  96. u32 oledb_base;
  97. u32 ab_base;
  98. u32 ibb_base;
  99. };
  100. enum reg_type {
  101. OLEDB,
  102. AB,
  103. IBB,
  104. };
  105. enum ibb_subtype {
  106. PM8150A_IBB = 0x03,
  107. PM8350B_IBB = 0x04,
  108. };
  109. enum ibb_rev4 {
  110. IBB_ANA_MAJOR_V1 = 0x01,
  111. IBB_ANA_MAJOR_V2 = 0x02,
  112. };
  113. static inline bool is_spur_mitigation_supported(struct ibb_regulator *ibb)
  114. {
  115. if ((ibb->subtype == PM8350B_IBB) && (ibb->rev4 >= IBB_ANA_MAJOR_V2))
  116. return true;
  117. return false;
  118. }
  119. static inline bool is_phase_ctrl_supported(struct ibb_regulator *ibb)
  120. {
  121. if (ibb->subtype == PM8350B_IBB)
  122. return true;
  123. return false;
  124. }
  125. static int qpnp_amoled_read(struct qpnp_amoled *chip,
  126. u16 addr, u8 *value, u8 count)
  127. {
  128. int rc = 0;
  129. rc = regmap_bulk_read(chip->regmap, addr, value, count);
  130. if (rc < 0)
  131. pr_err("Failed to read from addr=0x%02x rc=%d\n", addr, rc);
  132. return rc;
  133. }
  134. static int qpnp_amoled_write(struct qpnp_amoled *chip,
  135. u16 addr, u8 *value, u8 count)
  136. {
  137. int rc;
  138. rc = regmap_bulk_write(chip->regmap, addr, value, count);
  139. if (rc < 0)
  140. pr_err("Failed to write to addr=0x%02x rc=%d\n", addr, rc);
  141. return rc;
  142. }
  143. static int qpnp_amoled_masked_write(struct qpnp_amoled *chip,
  144. u16 addr, u8 mask, u8 value)
  145. {
  146. int rc = 0;
  147. rc = regmap_update_bits(chip->regmap, addr, mask, value);
  148. if (rc < 0)
  149. pr_err("Failed to write addr=0x%02x value=0x%02x rc=%d\n",
  150. addr, value, rc);
  151. return rc;
  152. }
  153. /* AB regulator */
  154. static int qpnp_ab_regulator_is_enabled(struct regulator_dev *rdev)
  155. {
  156. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  157. return chip->ab.vreg.enabled;
  158. }
  159. static int qpnp_ab_regulator_enable(struct regulator_dev *rdev)
  160. {
  161. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  162. chip->ab.vreg.enabled = true;
  163. return 0;
  164. }
  165. static int qpnp_ab_regulator_disable(struct regulator_dev *rdev)
  166. {
  167. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  168. chip->ab.vreg.enabled = false;
  169. return 0;
  170. }
  171. /* IBB regulator */
  172. static int qpnp_ibb_regulator_is_enabled(struct regulator_dev *rdev)
  173. {
  174. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  175. return chip->ibb.vreg.enabled;
  176. }
  177. static int qpnp_ibb_regulator_enable(struct regulator_dev *rdev)
  178. {
  179. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  180. chip->ibb.vreg.enabled = true;
  181. return 0;
  182. }
  183. static int qpnp_ibb_regulator_disable(struct regulator_dev *rdev)
  184. {
  185. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  186. chip->ibb.vreg.enabled = false;
  187. return 0;
  188. }
  189. /* common to AB and IBB */
  190. static int qpnp_ab_ibb_regulator_set_voltage(struct regulator_dev *rdev,
  191. int min_uV, int max_uV, unsigned int *selector)
  192. {
  193. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  194. /* HW controlled */
  195. if (chip->ab.swire_control || chip->ibb.swire_control)
  196. return 0;
  197. return 0;
  198. }
  199. static int qpnp_ab_ibb_regulator_get_voltage(struct regulator_dev *rdev)
  200. {
  201. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  202. /* HW controlled */
  203. if (chip->ab.swire_control || chip->ibb.swire_control)
  204. return 0;
  205. return 0;
  206. }
  207. static int qpnp_ab_pd_control(struct qpnp_amoled *chip, bool en)
  208. {
  209. u8 val = en ? PULLDN_EN_BIT : 0;
  210. return qpnp_amoled_write(chip, AB_LDO_PD_CTL(chip), &val, 1);
  211. }
  212. static int qpnp_ibb_pd_control(struct qpnp_amoled *chip, bool en)
  213. {
  214. u8 val = en ? ENABLE_PD_BIT : 0;
  215. return qpnp_amoled_masked_write(chip, IBB_PD_CTL(chip), ENABLE_PD_BIT,
  216. val);
  217. }
  218. static int qpnp_ab_ibb_regulator_set_mode(struct regulator_dev *rdev,
  219. unsigned int mode)
  220. {
  221. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  222. int rc = 0;
  223. if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_STANDBY &&
  224. mode != REGULATOR_MODE_IDLE) {
  225. pr_err("Unsupported mode %u\n", mode);
  226. return -EINVAL;
  227. }
  228. if (mode == chip->ab.vreg.mode || mode == chip->ibb.vreg.mode)
  229. return 0;
  230. pr_debug("mode: %d\n", mode);
  231. if (mode == REGULATOR_MODE_NORMAL || mode == REGULATOR_MODE_STANDBY) {
  232. if (chip->ibb.pd_control) {
  233. rc = qpnp_ibb_pd_control(chip, true);
  234. if (rc < 0)
  235. goto error;
  236. }
  237. if (chip->ab.pd_control) {
  238. rc = qpnp_ab_pd_control(chip, true);
  239. if (rc < 0)
  240. goto error;
  241. }
  242. } else if (mode == REGULATOR_MODE_IDLE) {
  243. if (chip->ibb.pd_control) {
  244. rc = qpnp_ibb_pd_control(chip, false);
  245. if (rc < 0)
  246. goto error;
  247. }
  248. if (chip->ab.pd_control) {
  249. rc = qpnp_ab_pd_control(chip, false);
  250. if (rc < 0)
  251. goto error;
  252. }
  253. }
  254. chip->ab.vreg.mode = chip->ibb.vreg.mode = mode;
  255. error:
  256. if (rc < 0)
  257. pr_err("Failed to configure for mode %d\n", mode);
  258. return rc;
  259. }
  260. static unsigned int qpnp_ab_ibb_regulator_get_mode(struct regulator_dev *rdev)
  261. {
  262. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  263. return chip->ibb.vreg.mode;
  264. }
  265. #define SINGLE_PHASE_ILIMIT_UA 30000
  266. static int qpnp_ibb_regulator_set_load(struct regulator_dev *rdev,
  267. int load_uA)
  268. {
  269. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  270. u8 ibb_phase;
  271. if (!is_phase_ctrl_supported(&chip->ibb))
  272. return 0;
  273. /* For IBB single phase, it's configured only once. */
  274. if (chip->ibb.single_phase)
  275. return 0;
  276. if (load_uA < 0)
  277. return -EINVAL;
  278. else if (load_uA <= SINGLE_PHASE_ILIMIT_UA)
  279. ibb_phase = AUTO_DUAL_PHASE_BIT;
  280. else
  281. ibb_phase = FORCE_DUAL_PHASE_BIT;
  282. return qpnp_amoled_masked_write(chip, IBB_DUAL_PHASE_CTL(chip),
  283. IBB_DUAL_PHASE_CTL_MASK, ibb_phase);
  284. }
  285. static const struct regulator_ops qpnp_amoled_ab_ops = {
  286. .enable = qpnp_ab_regulator_enable,
  287. .disable = qpnp_ab_regulator_disable,
  288. .is_enabled = qpnp_ab_regulator_is_enabled,
  289. .set_voltage = qpnp_ab_ibb_regulator_set_voltage,
  290. .get_voltage = qpnp_ab_ibb_regulator_get_voltage,
  291. .set_mode = qpnp_ab_ibb_regulator_set_mode,
  292. .get_mode = qpnp_ab_ibb_regulator_get_mode,
  293. };
  294. static const struct regulator_ops qpnp_amoled_ibb_ops = {
  295. .enable = qpnp_ibb_regulator_enable,
  296. .disable = qpnp_ibb_regulator_disable,
  297. .is_enabled = qpnp_ibb_regulator_is_enabled,
  298. .set_voltage = qpnp_ab_ibb_regulator_set_voltage,
  299. .get_voltage = qpnp_ab_ibb_regulator_get_voltage,
  300. .set_mode = qpnp_ab_ibb_regulator_set_mode,
  301. .get_mode = qpnp_ab_ibb_regulator_get_mode,
  302. .set_load = qpnp_ibb_regulator_set_load,
  303. };
  304. /* OLEDB regulator */
  305. static int qpnp_oledb_regulator_is_enabled(struct regulator_dev *rdev)
  306. {
  307. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  308. return chip->oledb.vreg.enabled;
  309. }
  310. static int qpnp_oledb_regulator_enable(struct regulator_dev *rdev)
  311. {
  312. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  313. chip->oledb.vreg.enabled = true;
  314. return 0;
  315. }
  316. static int qpnp_oledb_regulator_disable(struct regulator_dev *rdev)
  317. {
  318. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  319. chip->oledb.vreg.enabled = false;
  320. return 0;
  321. }
  322. static int qpnp_oledb_regulator_set_voltage(struct regulator_dev *rdev,
  323. int min_uV, int max_uV, unsigned int *selector)
  324. {
  325. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  326. /* HW controlled */
  327. if (chip->oledb.swire_control)
  328. return 0;
  329. return 0;
  330. }
  331. static int qpnp_oledb_regulator_get_voltage(struct regulator_dev *rdev)
  332. {
  333. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  334. /* HW controlled */
  335. if (chip->oledb.swire_control)
  336. return 0;
  337. return 0;
  338. }
  339. static int qpnp_oledb_regulator_set_mode(struct regulator_dev *rdev,
  340. unsigned int mode)
  341. {
  342. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  343. chip->oledb.vreg.mode = mode;
  344. return 0;
  345. }
  346. static unsigned int qpnp_oledb_regulator_get_mode(struct regulator_dev *rdev)
  347. {
  348. struct qpnp_amoled *chip = rdev_get_drvdata(rdev);
  349. return chip->oledb.vreg.mode;
  350. }
  351. static const struct regulator_ops qpnp_amoled_oledb_ops = {
  352. .enable = qpnp_oledb_regulator_enable,
  353. .disable = qpnp_oledb_regulator_disable,
  354. .is_enabled = qpnp_oledb_regulator_is_enabled,
  355. .set_voltage = qpnp_oledb_regulator_set_voltage,
  356. .get_voltage = qpnp_oledb_regulator_get_voltage,
  357. .set_mode = qpnp_oledb_regulator_set_mode,
  358. .get_mode = qpnp_oledb_regulator_get_mode,
  359. };
  360. static int qpnp_amoled_regulator_register(struct qpnp_amoled *chip,
  361. enum reg_type type)
  362. {
  363. int rc = 0;
  364. struct regulator_init_data *init_data;
  365. struct regulator_config cfg = {};
  366. struct regulator_desc *rdesc;
  367. struct regulator_dev *rdev;
  368. struct device_node *node;
  369. if (type == OLEDB) {
  370. node = chip->oledb.vreg.node;
  371. rdesc = &chip->oledb.vreg.rdesc;
  372. rdesc->ops = &qpnp_amoled_oledb_ops;
  373. rdev = chip->oledb.vreg.rdev;
  374. } else if (type == AB) {
  375. node = chip->ab.vreg.node;
  376. rdesc = &chip->ab.vreg.rdesc;
  377. rdesc->ops = &qpnp_amoled_ab_ops;
  378. rdev = chip->ab.vreg.rdev;
  379. } else if (type == IBB) {
  380. node = chip->ibb.vreg.node;
  381. rdesc = &chip->ibb.vreg.rdesc;
  382. rdesc->ops = &qpnp_amoled_ibb_ops;
  383. rdev = chip->ibb.vreg.rdev;
  384. } else {
  385. pr_err("Invalid regulator type %d\n", type);
  386. return -EINVAL;
  387. }
  388. init_data = of_get_regulator_init_data(chip->dev, node, rdesc);
  389. if (!init_data) {
  390. pr_err("Failed to get regulator_init_data for type %d\n", type);
  391. return -ENOMEM;
  392. }
  393. if (init_data->constraints.name) {
  394. rdesc->owner = THIS_MODULE;
  395. rdesc->type = REGULATOR_VOLTAGE;
  396. rdesc->name = init_data->constraints.name;
  397. cfg.dev = chip->dev;
  398. cfg.init_data = init_data;
  399. cfg.driver_data = chip;
  400. cfg.of_node = node;
  401. if (of_get_property(chip->dev->of_node, "parent-supply",
  402. NULL))
  403. init_data->supply_regulator = "parent";
  404. init_data->constraints.valid_ops_mask
  405. |= REGULATOR_CHANGE_VOLTAGE
  406. | REGULATOR_CHANGE_STATUS
  407. | REGULATOR_CHANGE_MODE;
  408. init_data->constraints.valid_modes_mask
  409. |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE
  410. | REGULATOR_MODE_STANDBY;
  411. rdev = devm_regulator_register(chip->dev, rdesc, &cfg);
  412. if (IS_ERR(rdev)) {
  413. rc = PTR_ERR(rdev);
  414. rdev = NULL;
  415. pr_err("Failed to register amoled regulator for type %d rc = %d\n",
  416. type, rc);
  417. return rc;
  418. }
  419. rc = devm_regulator_debug_register(chip->dev, rdev);
  420. if (rc) {
  421. pr_err("failed to register debug regulator rc=%d\n",
  422. rc);
  423. rc = 0;
  424. }
  425. if (type == OLEDB)
  426. chip->oledb.vreg.mode = REGULATOR_MODE_NORMAL;
  427. else if (type == IBB)
  428. chip->ibb.vreg.mode = REGULATOR_MODE_NORMAL;
  429. else
  430. chip->ab.vreg.mode = REGULATOR_MODE_NORMAL;
  431. } else {
  432. pr_err("regulator name missing for type %d\n", type);
  433. return -EINVAL;
  434. }
  435. return rc;
  436. }
  437. static int qpnp_amoled_hw_init(struct qpnp_amoled *chip)
  438. {
  439. int rc;
  440. u8 val;
  441. rc = qpnp_amoled_regulator_register(chip, OLEDB);
  442. if (rc < 0) {
  443. dev_err(chip->dev, "Failed to register OLEDB regulator rc=%d\n",
  444. rc);
  445. return rc;
  446. }
  447. rc = qpnp_amoled_regulator_register(chip, AB);
  448. if (rc < 0) {
  449. dev_err(chip->dev, "Failed to register AB regulator rc=%d\n",
  450. rc);
  451. return rc;
  452. }
  453. rc = qpnp_amoled_regulator_register(chip, IBB);
  454. if (rc < 0) {
  455. dev_err(chip->dev, "Failed to register IBB regulator rc=%d\n",
  456. rc);
  457. return rc;
  458. }
  459. if (is_phase_ctrl_supported(&chip->ibb) && chip->ibb.single_phase) {
  460. val = FORCE_SINGLE_PHASE_BIT;
  461. rc = qpnp_amoled_masked_write(chip, IBB_DUAL_PHASE_CTL(chip),
  462. IBB_DUAL_PHASE_CTL_MASK, val);
  463. if (rc < 0)
  464. return rc;
  465. }
  466. return 0;
  467. }
  468. static int qpnp_amoled_ibb_spur_parse_dt(struct qpnp_amoled *chip, struct device_node *node)
  469. {
  470. int freq_array_len, rc, i;
  471. u32 spur_thres[2*MAX_SPUR_FREQ_BANDS];
  472. rc = of_property_read_u32(node,
  473. "qcom,ibb-spur-mitigation-level",
  474. &chip->ibb.spur_mitigation_level);
  475. if (rc < 0 || (chip->ibb.spur_mitigation_level == SPUR_MITIGATION_DISABLED)) {
  476. dev_dbg(chip->dev, "ibb spur mitigation DISABLED!");
  477. return rc;
  478. }
  479. if (chip->ibb.spur_mitigation_level == SPUR_MITIGATION_ENABLED_WITH_SQM) {
  480. of_property_read_u32(node, "qcom,ibb-spur-sqm-timer-ms",
  481. &chip->ibb.spur_sqm_timer_ms);
  482. chip->nvmem_cell = devm_nvmem_cell_get(chip->dev,
  483. "ibb_spur_sqm_timer");
  484. if (IS_ERR(chip->nvmem_cell)) {
  485. rc = PTR_ERR(chip->nvmem_cell);
  486. if (rc != -EPROBE_DEFER)
  487. dev_err(chip->dev, "Failed to get nvmem-cells, rc=%d\n", rc);
  488. return rc;
  489. }
  490. }
  491. /*
  492. * Read the step size - 1khz or 2khz.
  493. *
  494. * NOTE: Even if this is not defined, step size may still be
  495. * set to 2khz indirectly, if any freq1/2/3 thresh limit
  496. * is in range: 248khz < f < 496khz.
  497. */
  498. chip->ibb.spur_freq_res_sel = of_property_read_bool(node,
  499. "qcom,ibb-spur-2khz-step-size");
  500. freq_array_len = of_property_count_elems_of_size(node,
  501. "qcom,ibb-spur-freq-thresholds", sizeof(u32));
  502. if (freq_array_len != 2*MAX_SPUR_FREQ_BANDS) {
  503. dev_err(chip->dev, "invalid ibb spur freq threshold array size = %d\n",
  504. freq_array_len);
  505. chip->ibb.spur_mitigation_level = SPUR_MITIGATION_DISABLED;
  506. return -EINVAL;
  507. }
  508. rc = of_property_read_u32_array(node,
  509. "qcom,ibb-spur-freq-thresholds", spur_thres, freq_array_len);
  510. if (rc < 0) {
  511. dev_err(chip->dev, "failed to read thresholds = %d\n", rc);
  512. return rc;
  513. }
  514. for (i = 0; i < MAX_SPUR_FREQ_BANDS; i++) {
  515. chip->ibb.spur_freq_thresh_low[i] = spur_thres[2*i];
  516. chip->ibb.spur_freq_thresh_high[i] = spur_thres[(2*i)+1];
  517. }
  518. return rc;
  519. }
  520. static int qpnp_amoled_parse_dt(struct qpnp_amoled *chip)
  521. {
  522. struct device_node *temp, *node = chip->dev->of_node;
  523. const __be32 *prop_addr;
  524. int rc = 0;
  525. u32 base;
  526. u8 val[3];
  527. for_each_available_child_of_node(node, temp) {
  528. prop_addr = of_get_address(temp, 0, NULL, NULL);
  529. if (!prop_addr) {
  530. pr_err("Couldn't get reg address\n");
  531. return -EINVAL;
  532. }
  533. base = be32_to_cpu(*prop_addr);
  534. rc = qpnp_amoled_read(chip, base + PERIPH_REVISION4, val, 3);
  535. if (rc < 0) {
  536. pr_err("Couldn't read PERIPH_REVISION4 for base %x\n", base);
  537. return rc;
  538. }
  539. switch (val[1]) {
  540. case OLEDB_PERIPH_TYPE:
  541. chip->oledb_base = base;
  542. chip->oledb.vreg.node = temp;
  543. chip->oledb.swire_control = of_property_read_bool(temp,
  544. "qcom,swire-control");
  545. break;
  546. case AB_PERIPH_TYPE:
  547. chip->ab_base = base;
  548. chip->ab.vreg.node = temp;
  549. chip->ab.swire_control = of_property_read_bool(temp,
  550. "qcom,swire-control");
  551. chip->ab.pd_control = of_property_read_bool(temp,
  552. "qcom,aod-pd-control");
  553. break;
  554. case IBB_PERIPH_TYPE:
  555. chip->ibb_base = base;
  556. chip->ibb.subtype = val[2];
  557. chip->ibb.rev4 = val[0];
  558. chip->ibb.vreg.node = temp;
  559. chip->ibb.swire_control = of_property_read_bool(temp,
  560. "qcom,swire-control");
  561. chip->ibb.pd_control = of_property_read_bool(temp,
  562. "qcom,aod-pd-control");
  563. chip->ibb.single_phase = of_property_read_bool(temp,
  564. "qcom,ibb-single-phase");
  565. if (is_spur_mitigation_supported(&chip->ibb)) {
  566. rc = qpnp_amoled_ibb_spur_parse_dt(chip, temp);
  567. if (rc < 0)
  568. pr_err("Failed to parse ibb_spur_parse_dt\n");
  569. }
  570. break;
  571. default:
  572. pr_err("Unknown peripheral type 0x%x\n", val[0]);
  573. return -EINVAL;
  574. }
  575. }
  576. return 0;
  577. }
  578. static bool is_2khz_step_needed(struct qpnp_amoled *chip)
  579. {
  580. u8 i;
  581. /*
  582. * If any of the freq1/2/3 band has valid thresh
  583. * (i.e f_high >= f_low)
  584. * and freq values is in range of 248khz < f < 496khz
  585. * then use step_size = 2khz
  586. */
  587. for (i = 0; i < MAX_SPUR_FREQ_BANDS; i++) {
  588. if ((chip->ibb.spur_freq_thresh_high[i] > MAX_SPUR_FREQ_KHZ) &&
  589. (chip->ibb.spur_freq_thresh_high[i] < MAX_SPUR_FREQ_KHZ * 2) &&
  590. (chip->ibb.spur_freq_thresh_high[i] >=
  591. chip->ibb.spur_freq_thresh_low[i])) {
  592. return true;
  593. }
  594. }
  595. return false;
  596. }
  597. static int qpnp_amoled_ibb_spur_set_thresh(struct qpnp_amoled *chip)
  598. {
  599. int i = 0, rc = 0;
  600. u16 low, high, max, temp = 0;
  601. if (!chip->ibb.spur_freq_res_sel)
  602. chip->ibb.spur_freq_res_sel = is_2khz_step_needed(chip);
  603. rc = qpnp_amoled_masked_write(chip,
  604. IBB_SPUR_FREQ_CTL(chip),
  605. FREQ_RES_SEL,
  606. (chip->ibb.spur_freq_res_sel ? FREQ_RES_SEL : 0));
  607. if (rc < 0) {
  608. dev_err(chip->dev, "failed to write IBB_SPUR_CTL register!\n");
  609. return rc;
  610. }
  611. /* Calculate max based on the step size */
  612. max = MAX_SPUR_FREQ_KHZ * (chip->ibb.spur_freq_res_sel ? 2 : 1);
  613. for (i = 0; i < MAX_SPUR_FREQ_BANDS; i++) {
  614. low = chip->ibb.spur_freq_thresh_low[i];
  615. high = chip->ibb.spur_freq_thresh_high[i];
  616. if (high < low || low > max || high > max) {
  617. dev_err(chip->dev, "ibb spur freq band%d threshold invalid!\n",
  618. (i+1));
  619. /* Set both thresholds to max to in effect disable it */
  620. chip->ibb.spur_freq_thresh_high[i] = max;
  621. chip->ibb.spur_freq_thresh_low[i] = max;
  622. low = max;
  623. high = max;
  624. }
  625. /*
  626. *For High threshold, roundoff-to-ceiling for odd frequency
  627. * with 2khz step
  628. */
  629. temp = high / (chip->ibb.spur_freq_res_sel ? 2 : 1);
  630. temp += chip->ibb.spur_freq_res_sel ? (high % 2) : 0;
  631. rc = qpnp_amoled_write(chip, IBB_SPUR_FREQ_THRESH_HIGH(i),
  632. (u8 *)&temp, 1);
  633. if (rc < 0) {
  634. dev_err(chip->dev, "failed to write IBB_SPUR_FREQ_HIGH register!\n");
  635. return rc;
  636. }
  637. /*
  638. * For Low threshold, roundoff-to-floor for odd frequency
  639. * with 2khz step
  640. */
  641. temp = low / (chip->ibb.spur_freq_res_sel ? 2 : 1);
  642. rc = qpnp_amoled_write(chip, IBB_SPUR_FREQ_THRESH_LOW(i),
  643. (u8 *)&temp, 1);
  644. if (rc < 0) {
  645. dev_err(chip->dev, "failed to write IBB_SPUR_FREQ_LOW register!\n");
  646. return rc;
  647. }
  648. }
  649. return 0;
  650. }
  651. static int qpnp_amoled_ibb_spur_set_sqm_timer(struct qpnp_amoled *chip, u16 sqm_timer)
  652. {
  653. return nvmem_cell_write(chip->nvmem_cell,
  654. &sqm_timer,
  655. sizeof(sqm_timer));
  656. }
  657. static int qpnp_amoled_ibb_spur_init(struct qpnp_amoled *chip)
  658. {
  659. int rc = 0;
  660. switch (chip->ibb.spur_mitigation_level) {
  661. case SPUR_MITIGATION_ENABLED_WITH_SQM:
  662. /*set SQM mode */
  663. rc = qpnp_amoled_masked_write(chip, IBB_SPUR_CTL(chip),
  664. SPUR_SQM_EN,
  665. SPUR_SQM_EN);
  666. if (rc < 0) {
  667. dev_err(chip->dev, "failed to enable spur SQM mode!\n");
  668. return rc;
  669. }
  670. /*set SQM timer if defined */
  671. if (chip->ibb.spur_sqm_timer_ms > SQM_TIMER_LOWER_LIMIT_MS &&
  672. chip->ibb.spur_sqm_timer_ms < SQM_TIMER_UPPER_LIMIT_MS) {
  673. rc = qpnp_amoled_ibb_spur_set_sqm_timer(chip,
  674. (u16)chip->ibb.spur_sqm_timer_ms);
  675. if (rc < 0) {
  676. if (rc != -EPROBE_DEFER)
  677. dev_err(chip->dev,
  678. "failed to enable spur SQM timer\n");
  679. return rc;
  680. }
  681. }
  682. fallthrough;
  683. case SPUR_MITIGATION_ENABLED_WITHOUT_SQM:
  684. rc = qpnp_amoled_ibb_spur_set_thresh(chip);
  685. if (rc < 0) {
  686. dev_err(chip->dev, "failed to set spur thresholds!\n");
  687. return rc;
  688. }
  689. rc = qpnp_amoled_masked_write(chip, IBB_SPUR_CTL(chip),
  690. SPUR_FSM_EN,
  691. SPUR_FSM_EN);
  692. if (rc < 0) {
  693. dev_err(chip->dev, "failed to enable spur FSM!\n");
  694. return rc;
  695. }
  696. break;
  697. case SPUR_MITIGATION_DISABLED:
  698. default:
  699. /* disable ibb spur FSM */
  700. rc = qpnp_amoled_masked_write(chip, IBB_SPUR_CTL(chip),
  701. SPUR_FSM_EN,
  702. 0);
  703. if (rc < 0) {
  704. dev_err(chip->dev, "failed to disable spur FSM!\n");
  705. return rc;
  706. }
  707. }
  708. return 0;
  709. }
  710. static int qpnp_amoled_regulator_probe(struct platform_device *pdev)
  711. {
  712. int rc;
  713. struct device_node *node;
  714. struct qpnp_amoled *chip;
  715. node = pdev->dev.of_node;
  716. if (!node) {
  717. pr_err("No nodes defined\n");
  718. return -ENODEV;
  719. }
  720. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  721. if (!chip)
  722. return -ENOMEM;
  723. chip->dev = &pdev->dev;
  724. chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  725. if (!chip->regmap) {
  726. dev_err(&pdev->dev, "Failed to get the regmap handle\n");
  727. rc = -EINVAL;
  728. goto error;
  729. }
  730. dev_set_drvdata(&pdev->dev, chip);
  731. rc = qpnp_amoled_parse_dt(chip);
  732. if (rc < 0) {
  733. dev_err(chip->dev, "Failed to parse DT params rc=%d\n", rc);
  734. goto error;
  735. }
  736. rc = qpnp_amoled_hw_init(chip);
  737. if (rc < 0)
  738. dev_err(chip->dev, "Failed to initialize HW rc=%d\n", rc);
  739. if (is_spur_mitigation_supported(&chip->ibb)) {
  740. rc = qpnp_amoled_ibb_spur_init(chip);
  741. if (rc < 0)
  742. dev_err(chip->dev, "Failed to init ibb spur settings rc=%d\n",
  743. rc);
  744. }
  745. error:
  746. return rc;
  747. }
  748. static int qpnp_amoled_regulator_remove(struct platform_device *pdev)
  749. {
  750. return 0;
  751. }
  752. static const struct of_device_id amoled_match_table[] = {
  753. { .compatible = "qcom,qpnp-amoled-regulator", },
  754. { },
  755. };
  756. static struct platform_driver qpnp_amoled_regulator_driver = {
  757. .driver = {
  758. .name = "qpnp-amoled-regulator",
  759. .of_match_table = amoled_match_table,
  760. },
  761. .probe = qpnp_amoled_regulator_probe,
  762. .remove = qpnp_amoled_regulator_remove,
  763. };
  764. static int __init qpnp_amoled_regulator_init(void)
  765. {
  766. return platform_driver_register(&qpnp_amoled_regulator_driver);
  767. }
  768. arch_initcall(qpnp_amoled_regulator_init);
  769. static void __exit qpnp_amoled_regulator_exit(void)
  770. {
  771. platform_driver_unregister(&qpnp_amoled_regulator_driver);
  772. }
  773. module_exit(qpnp_amoled_regulator_exit);
  774. MODULE_DESCRIPTION("QPNP AMOLED regulator driver");
  775. MODULE_LICENSE("GPL");