qcom-labibb-regulator.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (c) 2020, The Linux Foundation. All rights reserved.
  3. #include <linux/module.h>
  4. #include <linux/of_irq.h>
  5. #include <linux/of.h>
  6. #include <linux/of_device.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/regmap.h>
  9. #include <linux/regulator/driver.h>
  10. #include <linux/regulator/of_regulator.h>
  11. #define REG_PERPH_TYPE 0x04
  12. #define QCOM_LAB_TYPE 0x24
  13. #define QCOM_IBB_TYPE 0x20
  14. #define PMI8998_LAB_REG_BASE 0xde00
  15. #define PMI8998_IBB_REG_BASE 0xdc00
  16. #define PMI8998_IBB_LAB_REG_OFFSET 0x200
  17. #define REG_LABIBB_STATUS1 0x08
  18. #define LABIBB_STATUS1_SC_BIT BIT(6)
  19. #define LABIBB_STATUS1_VREG_OK_BIT BIT(7)
  20. #define REG_LABIBB_INT_SET_TYPE 0x11
  21. #define REG_LABIBB_INT_POLARITY_HIGH 0x12
  22. #define REG_LABIBB_INT_POLARITY_LOW 0x13
  23. #define REG_LABIBB_INT_LATCHED_CLR 0x14
  24. #define REG_LABIBB_INT_EN_SET 0x15
  25. #define REG_LABIBB_INT_EN_CLR 0x16
  26. #define LABIBB_INT_VREG_OK BIT(0)
  27. #define LABIBB_INT_VREG_TYPE_LEVEL 0
  28. #define REG_LABIBB_VOLTAGE 0x41
  29. #define LABIBB_VOLTAGE_OVERRIDE_EN BIT(7)
  30. #define LAB_VOLTAGE_SET_MASK GENMASK(3, 0)
  31. #define IBB_VOLTAGE_SET_MASK GENMASK(5, 0)
  32. #define REG_LABIBB_ENABLE_CTL 0x46
  33. #define LABIBB_CONTROL_ENABLE BIT(7)
  34. #define REG_LABIBB_PD_CTL 0x47
  35. #define LAB_PD_CTL_MASK GENMASK(1, 0)
  36. #define IBB_PD_CTL_MASK (BIT(0) | BIT(7))
  37. #define LAB_PD_CTL_STRONG_PULL BIT(0)
  38. #define IBB_PD_CTL_HALF_STRENGTH BIT(0)
  39. #define IBB_PD_CTL_EN BIT(7)
  40. #define REG_LABIBB_CURRENT_LIMIT 0x4b
  41. #define LAB_CURRENT_LIMIT_MASK GENMASK(2, 0)
  42. #define IBB_CURRENT_LIMIT_MASK GENMASK(4, 0)
  43. #define LAB_CURRENT_LIMIT_OVERRIDE_EN BIT(3)
  44. #define LABIBB_CURRENT_LIMIT_EN BIT(7)
  45. #define REG_IBB_PWRUP_PWRDN_CTL_1 0x58
  46. #define IBB_CTL_1_DISCHARGE_EN BIT(2)
  47. #define REG_LABIBB_SOFT_START_CTL 0x5f
  48. #define REG_LABIBB_SEC_ACCESS 0xd0
  49. #define LABIBB_SEC_UNLOCK_CODE 0xa5
  50. #define LAB_ENABLE_CTL_MASK BIT(7)
  51. #define IBB_ENABLE_CTL_MASK (BIT(7) | BIT(6))
  52. #define LABIBB_OFF_ON_DELAY 1000
  53. #define LAB_ENABLE_TIME (LABIBB_OFF_ON_DELAY * 2)
  54. #define IBB_ENABLE_TIME (LABIBB_OFF_ON_DELAY * 10)
  55. #define LABIBB_POLL_ENABLED_TIME 1000
  56. #define OCP_RECOVERY_INTERVAL_MS 500
  57. #define SC_RECOVERY_INTERVAL_MS 250
  58. #define LABIBB_MAX_OCP_COUNT 4
  59. #define LABIBB_MAX_SC_COUNT 3
  60. #define LABIBB_MAX_FATAL_COUNT 2
  61. struct labibb_current_limits {
  62. u32 uA_min;
  63. u32 uA_step;
  64. u8 ovr_val;
  65. };
  66. struct labibb_regulator {
  67. struct regulator_desc desc;
  68. struct device *dev;
  69. struct regmap *regmap;
  70. struct regulator_dev *rdev;
  71. struct labibb_current_limits uA_limits;
  72. struct delayed_work ocp_recovery_work;
  73. struct delayed_work sc_recovery_work;
  74. u16 base;
  75. u8 type;
  76. u8 dischg_sel;
  77. u8 soft_start_sel;
  78. int sc_irq;
  79. int sc_count;
  80. int ocp_irq;
  81. int ocp_irq_count;
  82. int fatal_count;
  83. };
  84. struct labibb_regulator_data {
  85. const char *name;
  86. u8 type;
  87. u16 base;
  88. const struct regulator_desc *desc;
  89. };
  90. static int qcom_labibb_ocp_hw_enable(struct regulator_dev *rdev)
  91. {
  92. struct labibb_regulator *vreg = rdev_get_drvdata(rdev);
  93. int ret;
  94. /* Clear irq latch status to avoid spurious event */
  95. ret = regmap_update_bits(rdev->regmap,
  96. vreg->base + REG_LABIBB_INT_LATCHED_CLR,
  97. LABIBB_INT_VREG_OK, 1);
  98. if (ret)
  99. return ret;
  100. /* Enable OCP HW interrupt */
  101. return regmap_update_bits(rdev->regmap,
  102. vreg->base + REG_LABIBB_INT_EN_SET,
  103. LABIBB_INT_VREG_OK, 1);
  104. }
  105. static int qcom_labibb_ocp_hw_disable(struct regulator_dev *rdev)
  106. {
  107. struct labibb_regulator *vreg = rdev_get_drvdata(rdev);
  108. return regmap_update_bits(rdev->regmap,
  109. vreg->base + REG_LABIBB_INT_EN_CLR,
  110. LABIBB_INT_VREG_OK, 1);
  111. }
  112. /**
  113. * qcom_labibb_check_ocp_status - Check the Over-Current Protection status
  114. * @vreg: Main driver structure
  115. *
  116. * This function checks the STATUS1 register for the VREG_OK bit: if it is
  117. * set, then there is no Over-Current event.
  118. *
  119. * Returns: Zero if there is no over-current, 1 if in over-current or
  120. * negative number for error
  121. */
  122. static int qcom_labibb_check_ocp_status(struct labibb_regulator *vreg)
  123. {
  124. u32 cur_status;
  125. int ret;
  126. ret = regmap_read(vreg->rdev->regmap, vreg->base + REG_LABIBB_STATUS1,
  127. &cur_status);
  128. if (ret)
  129. return ret;
  130. return !(cur_status & LABIBB_STATUS1_VREG_OK_BIT);
  131. }
  132. /**
  133. * qcom_labibb_ocp_recovery_worker - Handle OCP event
  134. * @work: OCP work structure
  135. *
  136. * This is the worker function to handle the Over Current Protection
  137. * hardware event; This will check if the hardware is still
  138. * signaling an over-current condition and will eventually stop
  139. * the regulator if such condition is still signaled after
  140. * LABIBB_MAX_OCP_COUNT times.
  141. *
  142. * If the driver that is consuming the regulator did not take action
  143. * for the OCP condition, or the hardware did not stabilize, a cut
  144. * of the LAB and IBB regulators will be forced (regulators will be
  145. * disabled).
  146. *
  147. * As last, if the writes to shut down the LAB/IBB regulators fail
  148. * for more than LABIBB_MAX_FATAL_COUNT, then a kernel panic will be
  149. * triggered, as a last resort to protect the hardware from burning;
  150. * this, however, is expected to never happen, but this is kept to
  151. * try to further ensure that we protect the hardware at all costs.
  152. */
  153. static void qcom_labibb_ocp_recovery_worker(struct work_struct *work)
  154. {
  155. struct labibb_regulator *vreg;
  156. const struct regulator_ops *ops;
  157. int ret;
  158. vreg = container_of(work, struct labibb_regulator,
  159. ocp_recovery_work.work);
  160. ops = vreg->rdev->desc->ops;
  161. if (vreg->ocp_irq_count >= LABIBB_MAX_OCP_COUNT) {
  162. /*
  163. * If we tried to disable the regulator multiple times but
  164. * we kept failing, there's only one last hope to save our
  165. * hardware from the death: raise a kernel bug, reboot and
  166. * hope that the bootloader kindly saves us. This, though
  167. * is done only as paranoid checking, because failing the
  168. * regmap write to disable the vreg is almost impossible,
  169. * since we got here after multiple regmap R/W.
  170. */
  171. BUG_ON(vreg->fatal_count > LABIBB_MAX_FATAL_COUNT);
  172. dev_err(&vreg->rdev->dev, "LABIBB: CRITICAL: Disabling regulator\n");
  173. /* Disable the regulator immediately to avoid damage */
  174. ret = ops->disable(vreg->rdev);
  175. if (ret) {
  176. vreg->fatal_count++;
  177. goto reschedule;
  178. }
  179. enable_irq(vreg->ocp_irq);
  180. vreg->fatal_count = 0;
  181. return;
  182. }
  183. ret = qcom_labibb_check_ocp_status(vreg);
  184. if (ret != 0) {
  185. vreg->ocp_irq_count++;
  186. goto reschedule;
  187. }
  188. ret = qcom_labibb_ocp_hw_enable(vreg->rdev);
  189. if (ret) {
  190. /* We cannot trust it without OCP enabled. */
  191. dev_err(vreg->dev, "Cannot enable OCP IRQ\n");
  192. vreg->ocp_irq_count++;
  193. goto reschedule;
  194. }
  195. enable_irq(vreg->ocp_irq);
  196. /* Everything went fine: reset the OCP count! */
  197. vreg->ocp_irq_count = 0;
  198. return;
  199. reschedule:
  200. mod_delayed_work(system_wq, &vreg->ocp_recovery_work,
  201. msecs_to_jiffies(OCP_RECOVERY_INTERVAL_MS));
  202. }
  203. /**
  204. * qcom_labibb_ocp_isr - Interrupt routine for OverCurrent Protection
  205. * @irq: Interrupt number
  206. * @chip: Main driver structure
  207. *
  208. * Over Current Protection (OCP) will signal to the client driver
  209. * that an over-current event has happened and then will schedule
  210. * a recovery worker.
  211. *
  212. * Disabling and eventually re-enabling the regulator is expected
  213. * to be done by the driver, as some hardware may be triggering an
  214. * over-current condition only at first initialization or it may
  215. * be expected only for a very brief amount of time, after which
  216. * the attached hardware may be expected to stabilize its current
  217. * draw.
  218. *
  219. * Returns: IRQ_HANDLED for success or IRQ_NONE for failure.
  220. */
  221. static irqreturn_t qcom_labibb_ocp_isr(int irq, void *chip)
  222. {
  223. struct labibb_regulator *vreg = chip;
  224. const struct regulator_ops *ops = vreg->rdev->desc->ops;
  225. int ret;
  226. /* If the regulator is not enabled, this is a fake event */
  227. if (!ops->is_enabled(vreg->rdev))
  228. return IRQ_HANDLED;
  229. /* If we tried to recover for too many times it's not getting better */
  230. if (vreg->ocp_irq_count > LABIBB_MAX_OCP_COUNT)
  231. return IRQ_NONE;
  232. /*
  233. * If we (unlikely) can't read this register, to prevent hardware
  234. * damage at all costs, we assume that the overcurrent event was
  235. * real; Moreover, if the status register is not signaling OCP,
  236. * it was a spurious event, so it's all ok.
  237. */
  238. ret = qcom_labibb_check_ocp_status(vreg);
  239. if (ret == 0) {
  240. vreg->ocp_irq_count = 0;
  241. goto end;
  242. }
  243. vreg->ocp_irq_count++;
  244. /*
  245. * Disable the interrupt temporarily, or it will fire continuously;
  246. * we will re-enable it in the recovery worker function.
  247. */
  248. disable_irq_nosync(irq);
  249. /* Warn the user for overcurrent */
  250. dev_warn(vreg->dev, "Over-Current interrupt fired!\n");
  251. /* Disable the interrupt to avoid hogging */
  252. ret = qcom_labibb_ocp_hw_disable(vreg->rdev);
  253. if (ret)
  254. goto end;
  255. /* Signal overcurrent event to drivers */
  256. regulator_notifier_call_chain(vreg->rdev,
  257. REGULATOR_EVENT_OVER_CURRENT, NULL);
  258. end:
  259. /* Schedule the recovery work */
  260. schedule_delayed_work(&vreg->ocp_recovery_work,
  261. msecs_to_jiffies(OCP_RECOVERY_INTERVAL_MS));
  262. if (ret)
  263. return IRQ_NONE;
  264. return IRQ_HANDLED;
  265. }
  266. static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
  267. int severity, bool enable)
  268. {
  269. struct labibb_regulator *vreg = rdev_get_drvdata(rdev);
  270. char *ocp_irq_name;
  271. u32 irq_flags = IRQF_ONESHOT;
  272. int irq_trig_low, ret;
  273. /*
  274. * labibb supports only protection - and does not support setting
  275. * limit. Furthermore, we don't support disabling protection.
  276. */
  277. if (lim || severity != REGULATOR_SEVERITY_PROT || !enable)
  278. return -EINVAL;
  279. /* If there is no OCP interrupt, there's nothing to set */
  280. if (vreg->ocp_irq <= 0)
  281. return -EINVAL;
  282. ocp_irq_name = devm_kasprintf(vreg->dev, GFP_KERNEL, "%s-over-current",
  283. vreg->desc.name);
  284. if (!ocp_irq_name)
  285. return -ENOMEM;
  286. /* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
  287. switch (vreg->type) {
  288. case QCOM_LAB_TYPE:
  289. irq_flags |= IRQF_TRIGGER_LOW;
  290. irq_trig_low = 1;
  291. break;
  292. case QCOM_IBB_TYPE:
  293. irq_flags |= IRQF_TRIGGER_HIGH;
  294. irq_trig_low = 0;
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. /* Activate OCP HW level interrupt */
  300. ret = regmap_update_bits(rdev->regmap,
  301. vreg->base + REG_LABIBB_INT_SET_TYPE,
  302. LABIBB_INT_VREG_OK,
  303. LABIBB_INT_VREG_TYPE_LEVEL);
  304. if (ret)
  305. return ret;
  306. /* Set OCP interrupt polarity */
  307. ret = regmap_update_bits(rdev->regmap,
  308. vreg->base + REG_LABIBB_INT_POLARITY_HIGH,
  309. LABIBB_INT_VREG_OK, !irq_trig_low);
  310. if (ret)
  311. return ret;
  312. ret = regmap_update_bits(rdev->regmap,
  313. vreg->base + REG_LABIBB_INT_POLARITY_LOW,
  314. LABIBB_INT_VREG_OK, irq_trig_low);
  315. if (ret)
  316. return ret;
  317. ret = qcom_labibb_ocp_hw_enable(rdev);
  318. if (ret)
  319. return ret;
  320. return devm_request_threaded_irq(vreg->dev, vreg->ocp_irq, NULL,
  321. qcom_labibb_ocp_isr, irq_flags,
  322. ocp_irq_name, vreg);
  323. }
  324. /**
  325. * qcom_labibb_check_sc_status - Check the Short Circuit Protection status
  326. * @vreg: Main driver structure
  327. *
  328. * This function checks the STATUS1 register on both LAB and IBB regulators
  329. * for the ShortCircuit bit: if it is set on *any* of them, then we have
  330. * experienced a short-circuit event.
  331. *
  332. * Returns: Zero if there is no short-circuit, 1 if in short-circuit or
  333. * negative number for error
  334. */
  335. static int qcom_labibb_check_sc_status(struct labibb_regulator *vreg)
  336. {
  337. u32 ibb_status, ibb_reg, lab_status, lab_reg;
  338. int ret;
  339. /* We have to work on both regulators due to PBS... */
  340. lab_reg = ibb_reg = vreg->base + REG_LABIBB_STATUS1;
  341. if (vreg->type == QCOM_LAB_TYPE)
  342. ibb_reg -= PMI8998_IBB_LAB_REG_OFFSET;
  343. else
  344. lab_reg += PMI8998_IBB_LAB_REG_OFFSET;
  345. ret = regmap_read(vreg->rdev->regmap, lab_reg, &lab_status);
  346. if (ret)
  347. return ret;
  348. ret = regmap_read(vreg->rdev->regmap, ibb_reg, &ibb_status);
  349. if (ret)
  350. return ret;
  351. return !!(lab_status & LABIBB_STATUS1_SC_BIT) ||
  352. !!(ibb_status & LABIBB_STATUS1_SC_BIT);
  353. }
  354. /**
  355. * qcom_labibb_sc_recovery_worker - Handle Short Circuit event
  356. * @work: SC work structure
  357. *
  358. * This is the worker function to handle the Short Circuit Protection
  359. * hardware event; This will check if the hardware is still
  360. * signaling a short-circuit condition and will eventually never
  361. * re-enable the regulator if such condition is still signaled after
  362. * LABIBB_MAX_SC_COUNT times.
  363. *
  364. * If the driver that is consuming the regulator did not take action
  365. * for the SC condition, or the hardware did not stabilize, this
  366. * worker will stop rescheduling, leaving the regulators disabled
  367. * as already done by the Portable Batch System (PBS).
  368. *
  369. * Returns: IRQ_HANDLED for success or IRQ_NONE for failure.
  370. */
  371. static void qcom_labibb_sc_recovery_worker(struct work_struct *work)
  372. {
  373. struct labibb_regulator *vreg;
  374. const struct regulator_ops *ops;
  375. u32 lab_reg, ibb_reg, lab_val, ibb_val, val;
  376. bool pbs_cut = false;
  377. int i, sc, ret;
  378. vreg = container_of(work, struct labibb_regulator,
  379. sc_recovery_work.work);
  380. ops = vreg->rdev->desc->ops;
  381. /*
  382. * If we tried to check the regulator status multiple times but we
  383. * kept failing, then just bail out, as the Portable Batch System
  384. * (PBS) will disable the vregs for us, preventing hardware damage.
  385. */
  386. if (vreg->fatal_count > LABIBB_MAX_FATAL_COUNT)
  387. return;
  388. /* Too many short-circuit events. Throw in the towel. */
  389. if (vreg->sc_count > LABIBB_MAX_SC_COUNT)
  390. return;
  391. /*
  392. * The Portable Batch System (PBS) automatically disables LAB
  393. * and IBB when a short-circuit event is detected, so we have to
  394. * check and work on both of them at the same time.
  395. */
  396. lab_reg = ibb_reg = vreg->base + REG_LABIBB_ENABLE_CTL;
  397. if (vreg->type == QCOM_LAB_TYPE)
  398. ibb_reg -= PMI8998_IBB_LAB_REG_OFFSET;
  399. else
  400. lab_reg += PMI8998_IBB_LAB_REG_OFFSET;
  401. sc = qcom_labibb_check_sc_status(vreg);
  402. if (sc)
  403. goto reschedule;
  404. for (i = 0; i < LABIBB_MAX_SC_COUNT; i++) {
  405. ret = regmap_read(vreg->regmap, lab_reg, &lab_val);
  406. if (ret) {
  407. vreg->fatal_count++;
  408. goto reschedule;
  409. }
  410. ret = regmap_read(vreg->regmap, ibb_reg, &ibb_val);
  411. if (ret) {
  412. vreg->fatal_count++;
  413. goto reschedule;
  414. }
  415. val = lab_val & ibb_val;
  416. if (!(val & LABIBB_CONTROL_ENABLE)) {
  417. pbs_cut = true;
  418. break;
  419. }
  420. usleep_range(5000, 6000);
  421. }
  422. if (pbs_cut)
  423. goto reschedule;
  424. /*
  425. * If we have reached this point, we either have successfully
  426. * recovered from the SC condition or we had a spurious SC IRQ,
  427. * which means that we can re-enable the regulators, if they
  428. * have ever been disabled by the PBS.
  429. */
  430. ret = ops->enable(vreg->rdev);
  431. if (ret)
  432. goto reschedule;
  433. /* Everything went fine: reset the OCP count! */
  434. vreg->sc_count = 0;
  435. enable_irq(vreg->sc_irq);
  436. return;
  437. reschedule:
  438. /*
  439. * Now that we have done basic handling of the short-circuit,
  440. * reschedule this worker in the regular system workqueue, as
  441. * taking action is not truly urgent anymore.
  442. */
  443. vreg->sc_count++;
  444. mod_delayed_work(system_wq, &vreg->sc_recovery_work,
  445. msecs_to_jiffies(SC_RECOVERY_INTERVAL_MS));
  446. }
  447. /**
  448. * qcom_labibb_sc_isr - Interrupt routine for Short Circuit Protection
  449. * @irq: Interrupt number
  450. * @chip: Main driver structure
  451. *
  452. * Short Circuit Protection (SCP) will signal to the client driver
  453. * that a regulation-out event has happened and then will schedule
  454. * a recovery worker.
  455. *
  456. * The LAB and IBB regulators will be automatically disabled by the
  457. * Portable Batch System (PBS) and they will be enabled again by
  458. * the worker function if the hardware stops signaling the short
  459. * circuit event.
  460. *
  461. * Returns: IRQ_HANDLED for success or IRQ_NONE for failure.
  462. */
  463. static irqreturn_t qcom_labibb_sc_isr(int irq, void *chip)
  464. {
  465. struct labibb_regulator *vreg = chip;
  466. if (vreg->sc_count > LABIBB_MAX_SC_COUNT)
  467. return IRQ_NONE;
  468. /* Warn the user for short circuit */
  469. dev_warn(vreg->dev, "Short-Circuit interrupt fired!\n");
  470. /*
  471. * Disable the interrupt temporarily, or it will fire continuously;
  472. * we will re-enable it in the recovery worker function.
  473. */
  474. disable_irq_nosync(irq);
  475. /* Signal out of regulation event to drivers */
  476. regulator_notifier_call_chain(vreg->rdev,
  477. REGULATOR_EVENT_REGULATION_OUT, NULL);
  478. /* Schedule the short-circuit handling as high-priority work */
  479. mod_delayed_work(system_highpri_wq, &vreg->sc_recovery_work,
  480. msecs_to_jiffies(SC_RECOVERY_INTERVAL_MS));
  481. return IRQ_HANDLED;
  482. }
  483. static int qcom_labibb_set_current_limit(struct regulator_dev *rdev,
  484. int min_uA, int max_uA)
  485. {
  486. struct labibb_regulator *vreg = rdev_get_drvdata(rdev);
  487. struct regulator_desc *desc = &vreg->desc;
  488. struct labibb_current_limits *lim = &vreg->uA_limits;
  489. u32 mask, val;
  490. int i, ret, sel = -1;
  491. if (min_uA < lim->uA_min || max_uA < lim->uA_min)
  492. return -EINVAL;
  493. for (i = 0; i < desc->n_current_limits; i++) {
  494. int uA_limit = (lim->uA_step * i) + lim->uA_min;
  495. if (max_uA >= uA_limit && min_uA <= uA_limit)
  496. sel = i;
  497. }
  498. if (sel < 0)
  499. return -EINVAL;
  500. /* Current limit setting needs secure access */
  501. ret = regmap_write(vreg->regmap, vreg->base + REG_LABIBB_SEC_ACCESS,
  502. LABIBB_SEC_UNLOCK_CODE);
  503. if (ret)
  504. return ret;
  505. mask = desc->csel_mask | lim->ovr_val;
  506. mask |= LABIBB_CURRENT_LIMIT_EN;
  507. val = (u32)sel | lim->ovr_val;
  508. val |= LABIBB_CURRENT_LIMIT_EN;
  509. return regmap_update_bits(vreg->regmap, desc->csel_reg, mask, val);
  510. }
  511. static int qcom_labibb_get_current_limit(struct regulator_dev *rdev)
  512. {
  513. struct labibb_regulator *vreg = rdev_get_drvdata(rdev);
  514. struct regulator_desc *desc = &vreg->desc;
  515. struct labibb_current_limits *lim = &vreg->uA_limits;
  516. unsigned int cur_step;
  517. int ret;
  518. ret = regmap_read(vreg->regmap, desc->csel_reg, &cur_step);
  519. if (ret)
  520. return ret;
  521. cur_step &= desc->csel_mask;
  522. return (cur_step * lim->uA_step) + lim->uA_min;
  523. }
  524. static int qcom_labibb_set_soft_start(struct regulator_dev *rdev)
  525. {
  526. struct labibb_regulator *vreg = rdev_get_drvdata(rdev);
  527. u32 val = 0;
  528. if (vreg->type == QCOM_IBB_TYPE)
  529. val = vreg->dischg_sel;
  530. else
  531. val = vreg->soft_start_sel;
  532. return regmap_write(rdev->regmap, rdev->desc->soft_start_reg, val);
  533. }
  534. static int qcom_labibb_get_table_sel(const int *table, int sz, u32 value)
  535. {
  536. int i;
  537. for (i = 0; i < sz; i++)
  538. if (table[i] == value)
  539. return i;
  540. return -EINVAL;
  541. }
  542. /* IBB discharge resistor values in KOhms */
  543. static const int dischg_resistor_values[] = { 300, 64, 32, 16 };
  544. /* Soft start time in microseconds */
  545. static const int soft_start_values[] = { 200, 400, 600, 800 };
  546. static int qcom_labibb_of_parse_cb(struct device_node *np,
  547. const struct regulator_desc *desc,
  548. struct regulator_config *config)
  549. {
  550. struct labibb_regulator *vreg = config->driver_data;
  551. u32 dischg_kohms, soft_start_time;
  552. int ret;
  553. ret = of_property_read_u32(np, "qcom,discharge-resistor-kohms",
  554. &dischg_kohms);
  555. if (ret)
  556. dischg_kohms = 300;
  557. ret = qcom_labibb_get_table_sel(dischg_resistor_values,
  558. ARRAY_SIZE(dischg_resistor_values),
  559. dischg_kohms);
  560. if (ret < 0)
  561. return ret;
  562. vreg->dischg_sel = (u8)ret;
  563. ret = of_property_read_u32(np, "qcom,soft-start-us",
  564. &soft_start_time);
  565. if (ret)
  566. soft_start_time = 200;
  567. ret = qcom_labibb_get_table_sel(soft_start_values,
  568. ARRAY_SIZE(soft_start_values),
  569. soft_start_time);
  570. if (ret < 0)
  571. return ret;
  572. vreg->soft_start_sel = (u8)ret;
  573. return 0;
  574. }
  575. static const struct regulator_ops qcom_labibb_ops = {
  576. .enable = regulator_enable_regmap,
  577. .disable = regulator_disable_regmap,
  578. .is_enabled = regulator_is_enabled_regmap,
  579. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  580. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  581. .list_voltage = regulator_list_voltage_linear,
  582. .map_voltage = regulator_map_voltage_linear,
  583. .set_active_discharge = regulator_set_active_discharge_regmap,
  584. .set_pull_down = regulator_set_pull_down_regmap,
  585. .set_current_limit = qcom_labibb_set_current_limit,
  586. .get_current_limit = qcom_labibb_get_current_limit,
  587. .set_soft_start = qcom_labibb_set_soft_start,
  588. .set_over_current_protection = qcom_labibb_set_ocp,
  589. };
  590. static const struct regulator_desc pmi8998_lab_desc = {
  591. .enable_mask = LAB_ENABLE_CTL_MASK,
  592. .enable_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_ENABLE_CTL),
  593. .enable_val = LABIBB_CONTROL_ENABLE,
  594. .enable_time = LAB_ENABLE_TIME,
  595. .poll_enabled_time = LABIBB_POLL_ENABLED_TIME,
  596. .soft_start_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_SOFT_START_CTL),
  597. .pull_down_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_PD_CTL),
  598. .pull_down_mask = LAB_PD_CTL_MASK,
  599. .pull_down_val_on = LAB_PD_CTL_STRONG_PULL,
  600. .vsel_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_VOLTAGE),
  601. .vsel_mask = LAB_VOLTAGE_SET_MASK,
  602. .apply_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_VOLTAGE),
  603. .apply_bit = LABIBB_VOLTAGE_OVERRIDE_EN,
  604. .csel_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_CURRENT_LIMIT),
  605. .csel_mask = LAB_CURRENT_LIMIT_MASK,
  606. .n_current_limits = 8,
  607. .off_on_delay = LABIBB_OFF_ON_DELAY,
  608. .owner = THIS_MODULE,
  609. .type = REGULATOR_VOLTAGE,
  610. .min_uV = 4600000,
  611. .uV_step = 100000,
  612. .n_voltages = 16,
  613. .ops = &qcom_labibb_ops,
  614. .of_parse_cb = qcom_labibb_of_parse_cb,
  615. };
  616. static const struct regulator_desc pmi8998_ibb_desc = {
  617. .enable_mask = IBB_ENABLE_CTL_MASK,
  618. .enable_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_ENABLE_CTL),
  619. .enable_val = LABIBB_CONTROL_ENABLE,
  620. .enable_time = IBB_ENABLE_TIME,
  621. .poll_enabled_time = LABIBB_POLL_ENABLED_TIME,
  622. .soft_start_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_SOFT_START_CTL),
  623. .active_discharge_off = 0,
  624. .active_discharge_on = IBB_CTL_1_DISCHARGE_EN,
  625. .active_discharge_mask = IBB_CTL_1_DISCHARGE_EN,
  626. .active_discharge_reg = (PMI8998_IBB_REG_BASE + REG_IBB_PWRUP_PWRDN_CTL_1),
  627. .pull_down_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_PD_CTL),
  628. .pull_down_mask = IBB_PD_CTL_MASK,
  629. .pull_down_val_on = IBB_PD_CTL_HALF_STRENGTH | IBB_PD_CTL_EN,
  630. .vsel_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_VOLTAGE),
  631. .vsel_mask = IBB_VOLTAGE_SET_MASK,
  632. .apply_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_VOLTAGE),
  633. .apply_bit = LABIBB_VOLTAGE_OVERRIDE_EN,
  634. .csel_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_CURRENT_LIMIT),
  635. .csel_mask = IBB_CURRENT_LIMIT_MASK,
  636. .n_current_limits = 32,
  637. .off_on_delay = LABIBB_OFF_ON_DELAY,
  638. .owner = THIS_MODULE,
  639. .type = REGULATOR_VOLTAGE,
  640. .min_uV = 1400000,
  641. .uV_step = 100000,
  642. .n_voltages = 64,
  643. .ops = &qcom_labibb_ops,
  644. .of_parse_cb = qcom_labibb_of_parse_cb,
  645. };
  646. static const struct labibb_regulator_data pmi8998_labibb_data[] = {
  647. {"lab", QCOM_LAB_TYPE, PMI8998_LAB_REG_BASE, &pmi8998_lab_desc},
  648. {"ibb", QCOM_IBB_TYPE, PMI8998_IBB_REG_BASE, &pmi8998_ibb_desc},
  649. { },
  650. };
  651. static const struct of_device_id qcom_labibb_match[] = {
  652. { .compatible = "qcom,pmi8998-lab-ibb", .data = &pmi8998_labibb_data},
  653. { },
  654. };
  655. MODULE_DEVICE_TABLE(of, qcom_labibb_match);
  656. static int qcom_labibb_regulator_probe(struct platform_device *pdev)
  657. {
  658. struct labibb_regulator *vreg;
  659. struct device *dev = &pdev->dev;
  660. struct regulator_config cfg = {};
  661. struct device_node *reg_node;
  662. const struct of_device_id *match;
  663. const struct labibb_regulator_data *reg_data;
  664. struct regmap *reg_regmap;
  665. unsigned int type;
  666. int ret;
  667. reg_regmap = dev_get_regmap(pdev->dev.parent, NULL);
  668. if (!reg_regmap) {
  669. dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
  670. return -ENODEV;
  671. }
  672. match = of_match_device(qcom_labibb_match, &pdev->dev);
  673. if (!match)
  674. return -ENODEV;
  675. for (reg_data = match->data; reg_data->name; reg_data++) {
  676. char *sc_irq_name;
  677. int irq = 0;
  678. /* Validate if the type of regulator is indeed
  679. * what's mentioned in DT.
  680. */
  681. ret = regmap_read(reg_regmap, reg_data->base + REG_PERPH_TYPE,
  682. &type);
  683. if (ret < 0) {
  684. dev_err(dev,
  685. "Peripheral type read failed ret=%d\n",
  686. ret);
  687. return -EINVAL;
  688. }
  689. if (WARN_ON((type != QCOM_LAB_TYPE) && (type != QCOM_IBB_TYPE)) ||
  690. WARN_ON(type != reg_data->type))
  691. return -EINVAL;
  692. vreg = devm_kzalloc(&pdev->dev, sizeof(*vreg),
  693. GFP_KERNEL);
  694. if (!vreg)
  695. return -ENOMEM;
  696. sc_irq_name = devm_kasprintf(dev, GFP_KERNEL,
  697. "%s-short-circuit",
  698. reg_data->name);
  699. if (!sc_irq_name)
  700. return -ENOMEM;
  701. reg_node = of_get_child_by_name(pdev->dev.of_node,
  702. reg_data->name);
  703. if (!reg_node)
  704. return -EINVAL;
  705. /* The Short Circuit interrupt is critical */
  706. irq = of_irq_get_byname(reg_node, "sc-err");
  707. if (irq <= 0) {
  708. if (irq == 0)
  709. irq = -EINVAL;
  710. of_node_put(reg_node);
  711. return dev_err_probe(vreg->dev, irq,
  712. "Short-circuit irq not found.\n");
  713. }
  714. vreg->sc_irq = irq;
  715. /* OverCurrent Protection IRQ is optional */
  716. irq = of_irq_get_byname(reg_node, "ocp");
  717. vreg->ocp_irq = irq;
  718. vreg->ocp_irq_count = 0;
  719. of_node_put(reg_node);
  720. vreg->regmap = reg_regmap;
  721. vreg->dev = dev;
  722. vreg->base = reg_data->base;
  723. vreg->type = reg_data->type;
  724. INIT_DELAYED_WORK(&vreg->sc_recovery_work,
  725. qcom_labibb_sc_recovery_worker);
  726. if (vreg->ocp_irq > 0)
  727. INIT_DELAYED_WORK(&vreg->ocp_recovery_work,
  728. qcom_labibb_ocp_recovery_worker);
  729. switch (vreg->type) {
  730. case QCOM_LAB_TYPE:
  731. /* LAB Limits: 200-1600mA */
  732. vreg->uA_limits.uA_min = 200000;
  733. vreg->uA_limits.uA_step = 200000;
  734. vreg->uA_limits.ovr_val = LAB_CURRENT_LIMIT_OVERRIDE_EN;
  735. break;
  736. case QCOM_IBB_TYPE:
  737. /* IBB Limits: 0-1550mA */
  738. vreg->uA_limits.uA_min = 0;
  739. vreg->uA_limits.uA_step = 50000;
  740. vreg->uA_limits.ovr_val = 0; /* No override bit */
  741. break;
  742. default:
  743. return -EINVAL;
  744. }
  745. memcpy(&vreg->desc, reg_data->desc, sizeof(vreg->desc));
  746. vreg->desc.of_match = reg_data->name;
  747. vreg->desc.name = reg_data->name;
  748. cfg.dev = vreg->dev;
  749. cfg.driver_data = vreg;
  750. cfg.regmap = vreg->regmap;
  751. vreg->rdev = devm_regulator_register(vreg->dev, &vreg->desc,
  752. &cfg);
  753. if (IS_ERR(vreg->rdev)) {
  754. dev_err(dev, "qcom_labibb: error registering %s : %d\n",
  755. reg_data->name, ret);
  756. return PTR_ERR(vreg->rdev);
  757. }
  758. ret = devm_request_threaded_irq(vreg->dev, vreg->sc_irq, NULL,
  759. qcom_labibb_sc_isr,
  760. IRQF_ONESHOT |
  761. IRQF_TRIGGER_RISING,
  762. sc_irq_name, vreg);
  763. if (ret)
  764. return ret;
  765. }
  766. return 0;
  767. }
  768. static struct platform_driver qcom_labibb_regulator_driver = {
  769. .driver = {
  770. .name = "qcom-lab-ibb-regulator",
  771. .of_match_table = qcom_labibb_match,
  772. },
  773. .probe = qcom_labibb_regulator_probe,
  774. };
  775. module_platform_driver(qcom_labibb_regulator_driver);
  776. MODULE_DESCRIPTION("Qualcomm labibb driver");
  777. MODULE_AUTHOR("Nisha Kumari <[email protected]>");
  778. MODULE_AUTHOR("Sumit Semwal <[email protected]>");
  779. MODULE_LICENSE("GPL v2");