qcom-coincell.c 4.0 KB

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