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.
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/slab.h>
10 #include <linux/regmap.h>
11 #include <linux/platform_device.h>
13 struct qcom_coincell {
15 struct regmap *regmap;
19 #define QCOM_COINCELL_REG_RSET 0x44
20 #define QCOM_COINCELL_REG_VSET 0x45
21 #define QCOM_COINCELL_REG_ENABLE 0x46
23 #define QCOM_COINCELL_ENABLE BIT(7)
25 static const int qcom_rset_map[] = { 2100, 1700, 1200, 800 };
26 static const int qcom_vset_map[] = { 2500, 3200, 3100, 3000 };
27 /* NOTE: for pm8921 and others, voltage of 2500 is 16 (10000b), not 0 */
29 /* if enable==0, rset and vset are ignored */
30 static int qcom_coincell_chgr_config(struct qcom_coincell *chgr, int rset,
31 int vset, bool enable)
35 /* if disabling, just do that and skip other operations */
37 return regmap_write(chgr->regmap,
38 chgr->base_addr + QCOM_COINCELL_REG_ENABLE, 0);
40 /* find index for current-limiting resistor */
41 for (i = 0; i < ARRAY_SIZE(qcom_rset_map); i++)
42 if (rset == qcom_rset_map[i])
45 if (i >= ARRAY_SIZE(qcom_rset_map)) {
46 dev_err(chgr->dev, "invalid rset-ohms value %d\n", rset);
50 /* find index for charge voltage */
51 for (j = 0; j < ARRAY_SIZE(qcom_vset_map); j++)
52 if (vset == qcom_vset_map[j])
55 if (j >= ARRAY_SIZE(qcom_vset_map)) {
56 dev_err(chgr->dev, "invalid vset-millivolts value %d\n", vset);
60 rc = regmap_write(chgr->regmap,
61 chgr->base_addr + QCOM_COINCELL_REG_RSET, i);
64 * This is mainly to flag a bad base_addr (reg) from dts.
65 * Other failures writing to the registers should be
66 * extremely rare, or indicative of problems that
67 * should be reported elsewhere (eg. spmi failure).
69 dev_err(chgr->dev, "could not write to RSET register\n");
73 rc = regmap_write(chgr->regmap,
74 chgr->base_addr + QCOM_COINCELL_REG_VSET, j);
78 /* set 'enable' register */
79 return regmap_write(chgr->regmap,
80 chgr->base_addr + QCOM_COINCELL_REG_ENABLE,
81 QCOM_COINCELL_ENABLE);
84 static int qcom_coincell_probe(struct platform_device *pdev)
86 struct device_node *node = pdev->dev.of_node;
87 struct qcom_coincell chgr;
93 chgr.dev = &pdev->dev;
95 chgr.regmap = dev_get_regmap(pdev->dev.parent, NULL);
97 dev_err(chgr.dev, "Unable to get regmap\n");
101 rc = of_property_read_u32(node, "reg", &chgr.base_addr);
105 enable = !of_property_read_bool(node, "qcom,charger-disable");
108 rc = of_property_read_u32(node, "qcom,rset-ohms", &rset);
111 "can't find 'qcom,rset-ohms' in DT block");
115 rc = of_property_read_u32(node, "qcom,vset-millivolts", &vset);
118 "can't find 'qcom,vset-millivolts' in DT block");
123 return qcom_coincell_chgr_config(&chgr, rset, vset, enable);
126 static const struct of_device_id qcom_coincell_match_table[] = {
127 { .compatible = "qcom,pm8941-coincell", },
131 MODULE_DEVICE_TABLE(of, qcom_coincell_match_table);
133 static struct platform_driver qcom_coincell_driver = {
135 .name = "qcom-spmi-coincell",
136 .of_match_table = qcom_coincell_match_table,
138 .probe = qcom_coincell_probe,
141 module_platform_driver(qcom_coincell_driver);
143 MODULE_DESCRIPTION("Qualcomm PMIC coincell charger driver");
144 MODULE_LICENSE("GPL v2");