qcom-spmi-pmic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  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/spmi.h>
  16. #include <linux/regmap.h>
  17. #include <linux/of_platform.h>
  18. #define PMIC_REV2 0x101
  19. #define PMIC_REV3 0x102
  20. #define PMIC_REV4 0x103
  21. #define PMIC_TYPE 0x104
  22. #define PMIC_SUBTYPE 0x105
  23. #define PMIC_TYPE_VALUE 0x51
  24. #define COMMON_SUBTYPE 0x00
  25. #define PM8941_SUBTYPE 0x01
  26. #define PM8841_SUBTYPE 0x02
  27. #define PM8019_SUBTYPE 0x03
  28. #define PM8226_SUBTYPE 0x04
  29. #define PM8110_SUBTYPE 0x05
  30. #define PMA8084_SUBTYPE 0x06
  31. #define PMI8962_SUBTYPE 0x07
  32. #define PMD9635_SUBTYPE 0x08
  33. #define PM8994_SUBTYPE 0x09
  34. #define PMI8994_SUBTYPE 0x0a
  35. #define PM8916_SUBTYPE 0x0b
  36. #define PM8004_SUBTYPE 0x0c
  37. #define PM8909_SUBTYPE 0x0d
  38. static const struct of_device_id pmic_spmi_id_table[] = {
  39. { .compatible = "qcom,spmi-pmic", .data = (void *)COMMON_SUBTYPE },
  40. { .compatible = "qcom,pm8941", .data = (void *)PM8941_SUBTYPE },
  41. { .compatible = "qcom,pm8841", .data = (void *)PM8841_SUBTYPE },
  42. { .compatible = "qcom,pm8019", .data = (void *)PM8019_SUBTYPE },
  43. { .compatible = "qcom,pm8226", .data = (void *)PM8226_SUBTYPE },
  44. { .compatible = "qcom,pm8110", .data = (void *)PM8110_SUBTYPE },
  45. { .compatible = "qcom,pma8084", .data = (void *)PMA8084_SUBTYPE },
  46. { .compatible = "qcom,pmi8962", .data = (void *)PMI8962_SUBTYPE },
  47. { .compatible = "qcom,pmd9635", .data = (void *)PMD9635_SUBTYPE },
  48. { .compatible = "qcom,pm8994", .data = (void *)PM8994_SUBTYPE },
  49. { .compatible = "qcom,pmi8994", .data = (void *)PMI8994_SUBTYPE },
  50. { .compatible = "qcom,pm8916", .data = (void *)PM8916_SUBTYPE },
  51. { .compatible = "qcom,pm8004", .data = (void *)PM8004_SUBTYPE },
  52. { .compatible = "qcom,pm8909", .data = (void *)PM8909_SUBTYPE },
  53. { }
  54. };
  55. static void pmic_spmi_show_revid(struct regmap *map, struct device *dev)
  56. {
  57. unsigned int rev2, minor, major, type, subtype;
  58. const char *name = "unknown";
  59. int ret, i;
  60. ret = regmap_read(map, PMIC_TYPE, &type);
  61. if (ret < 0)
  62. return;
  63. if (type != PMIC_TYPE_VALUE)
  64. return;
  65. ret = regmap_read(map, PMIC_SUBTYPE, &subtype);
  66. if (ret < 0)
  67. return;
  68. for (i = 0; i < ARRAY_SIZE(pmic_spmi_id_table); i++) {
  69. if (subtype == (unsigned long)pmic_spmi_id_table[i].data)
  70. break;
  71. }
  72. if (i != ARRAY_SIZE(pmic_spmi_id_table))
  73. name = pmic_spmi_id_table[i].compatible;
  74. ret = regmap_read(map, PMIC_REV2, &rev2);
  75. if (ret < 0)
  76. return;
  77. ret = regmap_read(map, PMIC_REV3, &minor);
  78. if (ret < 0)
  79. return;
  80. ret = regmap_read(map, PMIC_REV4, &major);
  81. if (ret < 0)
  82. return;
  83. /*
  84. * In early versions of PM8941 and PM8226, the major revision number
  85. * started incrementing from 0 (eg 0 = v1.0, 1 = v2.0).
  86. * Increment the major revision number here if the chip is an early
  87. * version of PM8941 or PM8226.
  88. */
  89. if ((subtype == PM8941_SUBTYPE || subtype == PM8226_SUBTYPE) &&
  90. major < 0x02)
  91. major++;
  92. if (subtype == PM8110_SUBTYPE)
  93. minor = rev2;
  94. dev_dbg(dev, "%x: %s v%d.%d\n", subtype, name, major, minor);
  95. }
  96. static const struct regmap_config spmi_regmap_config = {
  97. .reg_bits = 16,
  98. .val_bits = 8,
  99. .max_register = 0xffff,
  100. .fast_io = true,
  101. };
  102. static int pmic_spmi_probe(struct spmi_device *sdev)
  103. {
  104. struct device_node *root = sdev->dev.of_node;
  105. struct regmap *regmap;
  106. regmap = devm_regmap_init_spmi_ext(sdev, &spmi_regmap_config);
  107. if (IS_ERR(regmap))
  108. return PTR_ERR(regmap);
  109. pmic_spmi_show_revid(regmap, &sdev->dev);
  110. return of_platform_populate(root, NULL, NULL, &sdev->dev);
  111. }
  112. static void pmic_spmi_remove(struct spmi_device *sdev)
  113. {
  114. of_platform_depopulate(&sdev->dev);
  115. }
  116. MODULE_DEVICE_TABLE(of, pmic_spmi_id_table);
  117. static struct spmi_driver pmic_spmi_driver = {
  118. .probe = pmic_spmi_probe,
  119. .remove = pmic_spmi_remove,
  120. .driver = {
  121. .name = "pmic-spmi",
  122. .of_match_table = pmic_spmi_id_table,
  123. },
  124. };
  125. module_spmi_driver(pmic_spmi_driver);
  126. MODULE_DESCRIPTION("Qualcomm SPMI PMIC driver");
  127. MODULE_ALIAS("spmi:spmi-pmic");
  128. MODULE_LICENSE("GPL v2");
  129. MODULE_AUTHOR("Josh Cartwright <joshc@codeaurora.org>");
  130. MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");