qcom-coincell.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2015, Sony Mobile Communications Inc.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/of.h>
  9. #include <linux/regmap.h>
  10. #include <linux/platform_device.h>
  11. struct qcom_coincell {
  12. struct device *dev;
  13. struct regmap *regmap;
  14. u32 base_addr;
  15. };
  16. #define QCOM_COINCELL_REG_RSET 0x44
  17. #define QCOM_COINCELL_REG_VSET 0x45
  18. #define QCOM_COINCELL_REG_ENABLE 0x46
  19. #define QCOM_COINCELL_ENABLE BIT(7)
  20. static const int qcom_rset_map[] = { 2100, 1700, 1200, 800 };
  21. static const int qcom_vset_map[] = { 2500, 3200, 3100, 3000 };
  22. /* NOTE: for pm8921 and others, voltage of 2500 is 16 (10000b), not 0 */
  23. /* if enable==0, rset and vset are ignored */
  24. static int qcom_coincell_chgr_config(struct qcom_coincell *chgr, int rset,
  25. int vset, bool enable)
  26. {
  27. int i, j, rc;
  28. /* if disabling, just do that and skip other operations */
  29. if (!enable)
  30. return regmap_write(chgr->regmap,
  31. chgr->base_addr + QCOM_COINCELL_REG_ENABLE, 0);
  32. /* find index for current-limiting resistor */
  33. for (i = 0; i < ARRAY_SIZE(qcom_rset_map); i++)
  34. if (rset == qcom_rset_map[i])
  35. break;
  36. if (i >= ARRAY_SIZE(qcom_rset_map)) {
  37. dev_err(chgr->dev, "invalid rset-ohms value %d\n", rset);
  38. return -EINVAL;
  39. }
  40. /* find index for charge voltage */
  41. for (j = 0; j < ARRAY_SIZE(qcom_vset_map); j++)
  42. if (vset == qcom_vset_map[j])
  43. break;
  44. if (j >= ARRAY_SIZE(qcom_vset_map)) {
  45. dev_err(chgr->dev, "invalid vset-millivolts value %d\n", vset);
  46. return -EINVAL;
  47. }
  48. rc = regmap_write(chgr->regmap,
  49. chgr->base_addr + QCOM_COINCELL_REG_RSET, i);
  50. if (rc) {
  51. /*
  52. * This is mainly to flag a bad base_addr (reg) from dts.
  53. * Other failures writing to the registers should be
  54. * extremely rare, or indicative of problems that
  55. * should be reported elsewhere (eg. spmi failure).
  56. */
  57. dev_err(chgr->dev, "could not write to RSET register\n");
  58. return rc;
  59. }
  60. rc = regmap_write(chgr->regmap,
  61. chgr->base_addr + QCOM_COINCELL_REG_VSET, j);
  62. if (rc)
  63. return rc;
  64. /* set 'enable' register */
  65. return regmap_write(chgr->regmap,
  66. chgr->base_addr + QCOM_COINCELL_REG_ENABLE,
  67. QCOM_COINCELL_ENABLE);
  68. }
  69. static int qcom_coincell_probe(struct platform_device *pdev)
  70. {
  71. struct device_node *node = pdev->dev.of_node;
  72. struct qcom_coincell chgr;
  73. u32 rset = 0;
  74. u32 vset = 0;
  75. bool enable;
  76. int rc;
  77. chgr.dev = &pdev->dev;
  78. chgr.regmap = dev_get_regmap(pdev->dev.parent, NULL);
  79. if (!chgr.regmap) {
  80. dev_err(chgr.dev, "Unable to get regmap\n");
  81. return -EINVAL;
  82. }
  83. rc = of_property_read_u32(node, "reg", &chgr.base_addr);
  84. if (rc)
  85. return rc;
  86. enable = !of_property_read_bool(node, "qcom,charger-disable");
  87. if (enable) {
  88. rc = of_property_read_u32(node, "qcom,rset-ohms", &rset);
  89. if (rc) {
  90. dev_err(chgr.dev,
  91. "can't find 'qcom,rset-ohms' in DT block");
  92. return rc;
  93. }
  94. rc = of_property_read_u32(node, "qcom,vset-millivolts", &vset);
  95. if (rc) {
  96. dev_err(chgr.dev,
  97. "can't find 'qcom,vset-millivolts' in DT block");
  98. return rc;
  99. }
  100. }
  101. return qcom_coincell_chgr_config(&chgr, rset, vset, enable);
  102. }
  103. static const struct of_device_id qcom_coincell_match_table[] = {
  104. { .compatible = "qcom,pm8941-coincell", },
  105. {}
  106. };
  107. MODULE_DEVICE_TABLE(of, qcom_coincell_match_table);
  108. static struct platform_driver qcom_coincell_driver = {
  109. .driver = {
  110. .name = "qcom-spmi-coincell",
  111. .of_match_table = qcom_coincell_match_table,
  112. },
  113. .probe = qcom_coincell_probe,
  114. };
  115. module_platform_driver(qcom_coincell_driver);
  116. MODULE_DESCRIPTION("Qualcomm PMIC coincell charger driver");
  117. MODULE_LICENSE("GPL v2");