qcom-coincell.c 3.6 KB

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