qpnp-coincell.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/spmi.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #define QPNP_COINCELL_DRIVER_NAME "qcom,qpnp-coincell"
  21. struct qpnp_coincell {
  22. struct spmi_device *spmi_dev;
  23. u16 base_addr;
  24. };
  25. #define QPNP_COINCELL_REG_TYPE 0x04
  26. #define QPNP_COINCELL_REG_SUBTYPE 0x05
  27. #define QPNP_COINCELL_REG_RSET 0x44
  28. #define QPNP_COINCELL_REG_VSET 0x45
  29. #define QPNP_COINCELL_REG_ENABLE 0x46
  30. #define QPNP_COINCELL_TYPE 0x02
  31. #define QPNP_COINCELL_SUBTYPE 0x20
  32. #define QPNP_COINCELL_ENABLE 0x80
  33. #define QPNP_COINCELL_DISABLE 0x00
  34. static const int qpnp_rset_map[] = {2100, 1700, 1200, 800};
  35. static const int qpnp_vset_map[] = {2500, 3200, 3100, 3000};
  36. static int qpnp_coincell_set_resistance(struct qpnp_coincell *chip, int rset)
  37. {
  38. int i, rc;
  39. u8 reg;
  40. for (i = 0; i < ARRAY_SIZE(qpnp_rset_map); i++)
  41. if (rset == qpnp_rset_map[i])
  42. break;
  43. if (i >= ARRAY_SIZE(qpnp_rset_map)) {
  44. pr_err("invalid rset=%d value\n", rset);
  45. return -EINVAL;
  46. }
  47. reg = i;
  48. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  49. chip->base_addr + QPNP_COINCELL_REG_RSET, &reg, 1);
  50. if (rc)
  51. dev_err(&chip->spmi_dev->dev, "%s: could not write to RSET register, rc=%d\n",
  52. __func__, rc);
  53. return rc;
  54. }
  55. static int qpnp_coincell_set_voltage(struct qpnp_coincell *chip, int vset)
  56. {
  57. int i, rc;
  58. u8 reg;
  59. for (i = 0; i < ARRAY_SIZE(qpnp_vset_map); i++)
  60. if (vset == qpnp_vset_map[i])
  61. break;
  62. if (i >= ARRAY_SIZE(qpnp_vset_map)) {
  63. pr_err("invalid vset=%d value\n", vset);
  64. return -EINVAL;
  65. }
  66. reg = i;
  67. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  68. chip->base_addr + QPNP_COINCELL_REG_VSET, &reg, 1);
  69. if (rc)
  70. dev_err(&chip->spmi_dev->dev, "%s: could not write to VSET register, rc=%d\n",
  71. __func__, rc);
  72. return rc;
  73. }
  74. static int qpnp_coincell_set_charge(struct qpnp_coincell *chip, bool enabled)
  75. {
  76. int rc;
  77. u8 reg;
  78. reg = enabled ? QPNP_COINCELL_ENABLE : QPNP_COINCELL_DISABLE;
  79. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  80. chip->base_addr + QPNP_COINCELL_REG_ENABLE, &reg, 1);
  81. if (rc)
  82. dev_err(&chip->spmi_dev->dev, "%s: could not write to ENABLE register, rc=%d\n",
  83. __func__, rc);
  84. return rc;
  85. }
  86. static void qpnp_coincell_charger_show_state(struct qpnp_coincell *chip)
  87. {
  88. int rc, rset, vset, temp;
  89. bool enabled;
  90. u8 reg[QPNP_COINCELL_REG_ENABLE - QPNP_COINCELL_REG_RSET + 1];
  91. rc = spmi_ext_register_readl(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  92. chip->base_addr + QPNP_COINCELL_REG_RSET, reg, ARRAY_SIZE(reg));
  93. if (rc) {
  94. dev_err(&chip->spmi_dev->dev, "%s: could not read RSET register, rc=%d\n",
  95. __func__, rc);
  96. return;
  97. }
  98. temp = reg[QPNP_COINCELL_REG_RSET - QPNP_COINCELL_REG_RSET];
  99. if (temp >= ARRAY_SIZE(qpnp_rset_map)) {
  100. dev_err(&chip->spmi_dev->dev, "unknown RSET=0x%02X register value\n",
  101. temp);
  102. return;
  103. }
  104. rset = qpnp_rset_map[temp];
  105. temp = reg[QPNP_COINCELL_REG_VSET - QPNP_COINCELL_REG_RSET];
  106. if (temp >= ARRAY_SIZE(qpnp_vset_map)) {
  107. dev_err(&chip->spmi_dev->dev, "unknown VSET=0x%02X register value\n",
  108. temp);
  109. return;
  110. }
  111. vset = qpnp_vset_map[temp];
  112. temp = reg[QPNP_COINCELL_REG_ENABLE - QPNP_COINCELL_REG_RSET];
  113. enabled = temp & QPNP_COINCELL_ENABLE;
  114. pr_info("enabled=%c, voltage=%d mV, resistance=%d ohm\n",
  115. (enabled ? 'Y' : 'N'), vset, rset);
  116. }
  117. static int qpnp_coincell_check_type(struct qpnp_coincell *chip)
  118. {
  119. int rc;
  120. u8 type[2];
  121. rc = spmi_ext_register_readl(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  122. chip->base_addr + QPNP_COINCELL_REG_TYPE, type, 2);
  123. if (rc) {
  124. dev_err(&chip->spmi_dev->dev, "%s: could not read type register, rc=%d\n",
  125. __func__, rc);
  126. return rc;
  127. }
  128. if (type[0] != QPNP_COINCELL_TYPE || type[1] != QPNP_COINCELL_SUBTYPE) {
  129. dev_err(&chip->spmi_dev->dev, "%s: invalid type=0x%02X or subtype=0x%02X register value\n",
  130. __func__, type[0], type[1]);
  131. return -ENODEV;
  132. }
  133. return rc;
  134. }
  135. static int qpnp_coincell_probe(struct spmi_device *spmi)
  136. {
  137. struct device_node *node = spmi->dev.of_node;
  138. struct qpnp_coincell *chip;
  139. struct resource *res;
  140. u32 temp;
  141. int rc = 0;
  142. if (!node) {
  143. dev_err(&spmi->dev, "%s: device node missing\n", __func__);
  144. return -ENODEV;
  145. }
  146. chip = devm_kzalloc(&spmi->dev, sizeof(*chip), GFP_KERNEL);
  147. if (!chip) {
  148. dev_err(&spmi->dev, "%s: cannot allocate qpnp_coincell\n",
  149. __func__);
  150. return -ENOMEM;
  151. }
  152. chip->spmi_dev = spmi;
  153. res = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
  154. if (!res) {
  155. dev_err(&spmi->dev, "%s: node is missing base address\n",
  156. __func__);
  157. return -EINVAL;
  158. }
  159. chip->base_addr = res->start;
  160. rc = qpnp_coincell_check_type(chip);
  161. if (rc)
  162. return rc;
  163. rc = of_property_read_u32(node, "qcom,rset-ohms", &temp);
  164. if (!rc) {
  165. rc = qpnp_coincell_set_resistance(chip, temp);
  166. if (rc)
  167. return rc;
  168. }
  169. rc = of_property_read_u32(node, "qcom,vset-millivolts", &temp);
  170. if (!rc) {
  171. rc = qpnp_coincell_set_voltage(chip, temp);
  172. if (rc)
  173. return rc;
  174. }
  175. rc = of_property_read_u32(node, "qcom,charge-enable", &temp);
  176. if (!rc) {
  177. rc = qpnp_coincell_set_charge(chip, temp);
  178. if (rc)
  179. return rc;
  180. }
  181. qpnp_coincell_charger_show_state(chip);
  182. return 0;
  183. }
  184. static int __devexit qpnp_coincell_remove(struct spmi_device *spmi)
  185. {
  186. return 0;
  187. }
  188. static struct of_device_id qpnp_coincell_match_table[] = {
  189. { .compatible = QPNP_COINCELL_DRIVER_NAME, },
  190. {}
  191. };
  192. static const struct spmi_device_id qpnp_coincell_id[] = {
  193. { QPNP_COINCELL_DRIVER_NAME, 0 },
  194. {}
  195. };
  196. MODULE_DEVICE_TABLE(spmi, qpnp_coincell_id);
  197. static struct spmi_driver qpnp_coincell_driver = {
  198. .driver = {
  199. .name = QPNP_COINCELL_DRIVER_NAME,
  200. .of_match_table = qpnp_coincell_match_table,
  201. .owner = THIS_MODULE,
  202. },
  203. .probe = qpnp_coincell_probe,
  204. .remove = __devexit_p(qpnp_coincell_remove),
  205. .id_table = qpnp_coincell_id,
  206. };
  207. static int __init qpnp_coincell_init(void)
  208. {
  209. return spmi_driver_register(&qpnp_coincell_driver);
  210. }
  211. static void __exit qpnp_coincell_exit(void)
  212. {
  213. spmi_driver_unregister(&qpnp_coincell_driver);
  214. }
  215. MODULE_DESCRIPTION("QPNP PMIC coincell charger driver");
  216. MODULE_LICENSE("GPL v2");
  217. module_init(qpnp_coincell_init);
  218. module_exit(qpnp_coincell_exit);