summaryrefslogtreecommitdiff
path: root/drivers/platform/msm/qpnp-coincell.c
blob: b427f43e76df128cf817cb0e636028da83f670cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Copyright (c) 2013-2015, 2017, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#define pr_fmt(fmt) "%s: " fmt, __func__

#include <linux/kernel.h>
#include <linux/regmap.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/spmi.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>

#define QPNP_COINCELL_DRIVER_NAME "qcom,qpnp-coincell"

struct qpnp_coincell {
	struct platform_device	*pdev;
	struct regmap		*regmap;
	u16			base_addr;
};

#define QPNP_COINCELL_REG_TYPE		0x04
#define QPNP_COINCELL_REG_SUBTYPE	0x05
#define QPNP_COINCELL_REG_RSET		0x44
#define QPNP_COINCELL_REG_VSET		0x45
#define QPNP_COINCELL_REG_ENABLE	0x46

#define QPNP_COINCELL_TYPE		0x02
#define QPNP_COINCELL_SUBTYPE		0x20
#define QPNP_COINCELL_ENABLE		0x80
#define QPNP_COINCELL_DISABLE		0x00

static const int qpnp_rset_map[] = {2100, 1700, 1200, 800};
static const int qpnp_vset_map[] = {2500, 3200, 3100, 3000};

static int qpnp_coincell_set_resistance(struct qpnp_coincell *chip, int rset)
{
	int i, rc;
	u8 reg;

	for (i = 0; i < ARRAY_SIZE(qpnp_rset_map); i++)
		if (rset == qpnp_rset_map[i])
			break;

	if (i >= ARRAY_SIZE(qpnp_rset_map)) {
		pr_err("invalid rset=%d value\n", rset);
		return -EINVAL;
	}

	reg = i;
	rc = regmap_write(chip->regmap,
			  chip->base_addr + QPNP_COINCELL_REG_RSET, reg);
	if (rc)
		dev_err(&chip->pdev->dev,
			"%s: could not write to RSET register, rc=%d\n",
			__func__, rc);

	return rc;
}

static int qpnp_coincell_set_voltage(struct qpnp_coincell *chip, int vset)
{
	int i, rc;
	u8 reg;

	for (i = 0; i < ARRAY_SIZE(qpnp_vset_map); i++)
		if (vset == qpnp_vset_map[i])
			break;

	if (i >= ARRAY_SIZE(qpnp_vset_map)) {
		pr_err("invalid vset=%d value\n", vset);
		return -EINVAL;
	}

	reg = i;
	rc = regmap_write(chip->regmap,
			  chip->base_addr + QPNP_COINCELL_REG_VSET, reg);
	if (rc)
		dev_err(&chip->pdev->dev,
			"%s: could not write to VSET register, rc=%d\n",
			__func__, rc);

	return rc;
}

static int qpnp_coincell_set_charge(struct qpnp_coincell *chip, bool enabled)
{
	int rc;
	u8 reg;

	reg = enabled ? QPNP_COINCELL_ENABLE : QPNP_COINCELL_DISABLE;
	rc = regmap_write(chip->regmap,
			  chip->base_addr + QPNP_COINCELL_REG_ENABLE, reg);
	if (rc)
		dev_err(&chip->pdev->dev,
			"%s: could not write to ENABLE register, rc=%d\n",
			__func__, rc);

	return rc;
}

static void qpnp_coincell_charger_show_state(struct qpnp_coincell *chip)
{
	int rc, rset, vset, temp;
	bool enabled;
	u8 reg[QPNP_COINCELL_REG_ENABLE - QPNP_COINCELL_REG_RSET + 1];

	rc = regmap_bulk_read(chip->regmap,
			      chip->base_addr + QPNP_COINCELL_REG_RSET, reg,
			      ARRAY_SIZE(reg));
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: could not read RSET register, rc=%d\n",
			__func__, rc);
		return;
	}

	temp = reg[QPNP_COINCELL_REG_RSET - QPNP_COINCELL_REG_RSET];
	if (temp >= ARRAY_SIZE(qpnp_rset_map)) {
		dev_err(&chip->pdev->dev,
			"unknown RSET=0x%02X register value\n",
			temp);
		return;
	}
	rset = qpnp_rset_map[temp];

	temp = reg[QPNP_COINCELL_REG_VSET - QPNP_COINCELL_REG_RSET];
	if (temp >= ARRAY_SIZE(qpnp_vset_map)) {
		dev_err(&chip->pdev->dev,
			"unknown VSET=0x%02X register value\n",
			temp);
		return;
	}
	vset = qpnp_vset_map[temp];

	temp = reg[QPNP_COINCELL_REG_ENABLE - QPNP_COINCELL_REG_RSET];
	enabled = temp & QPNP_COINCELL_ENABLE;

	pr_info("enabled=%c, voltage=%d mV, resistance=%d ohm\n",
		(enabled ? 'Y' : 'N'), vset, rset);
}

static int qpnp_coincell_check_type(struct qpnp_coincell *chip)
{
	int rc;
	u8 type[2];

	rc = regmap_bulk_read(chip->regmap,
			      chip->base_addr + QPNP_COINCELL_REG_TYPE, type,
			      2);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: could not read type register, rc=%d\n",
			__func__, rc);
		return rc;
	}

	if (type[0] != QPNP_COINCELL_TYPE || type[1] != QPNP_COINCELL_SUBTYPE) {
		dev_err(&chip->pdev->dev,
			"%s: invalid type=0x%02X or subtype=0x%02X register value\n",
			__func__, type[0], type[1]);
		return -ENODEV;
	}

	return rc;
}

static int qpnp_coincell_probe(struct platform_device *pdev)
{
	struct device_node *node = pdev->dev.of_node;
	struct qpnp_coincell *chip;
	unsigned int base;
	u32 temp;
	int rc = 0;

	if (!node) {
		dev_err(&pdev->dev, "%s: device node missing\n", __func__);
		return -ENODEV;
	}

	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
	if (!chip)
		return -ENOMEM;

	chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!chip->regmap) {
		dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
		return -EINVAL;
	}
	chip->pdev = pdev;

	rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
	if (rc < 0) {
		dev_err(&pdev->dev,
			"Couldn't find reg in node = %s rc = %d\n",
			pdev->dev.of_node->full_name, rc);
		return rc;
	}
	chip->base_addr = base;

	rc = qpnp_coincell_check_type(chip);
	if (rc)
		return rc;

	rc = of_property_read_u32(node, "qcom,rset-ohms", &temp);
	if (!rc) {
		rc = qpnp_coincell_set_resistance(chip, temp);
		if (rc)
			return rc;
	}

	rc = of_property_read_u32(node, "qcom,vset-millivolts", &temp);
	if (!rc) {
		rc = qpnp_coincell_set_voltage(chip, temp);
		if (rc)
			return rc;
	}

	rc = of_property_read_u32(node, "qcom,charge-enable", &temp);
	if (!rc) {
		rc = qpnp_coincell_set_charge(chip, temp);
		if (rc)
			return rc;
	}

	qpnp_coincell_charger_show_state(chip);

	return 0;
}

static int qpnp_coincell_remove(struct platform_device *pdev)
{
	return 0;
}

static const struct of_device_id qpnp_coincell_match_table[] = {
	{ .compatible = QPNP_COINCELL_DRIVER_NAME, },
	{}
};

static const struct platform_device_id qpnp_coincell_id[] = {
	{ QPNP_COINCELL_DRIVER_NAME, 0 },
	{}
};
MODULE_DEVICE_TABLE(spmi, qpnp_coincell_id);

static struct platform_driver qpnp_coincell_driver = {
	.driver	= {
		.name		= QPNP_COINCELL_DRIVER_NAME,
		.of_match_table	= qpnp_coincell_match_table,
		.owner		= THIS_MODULE,
	},
	.probe		= qpnp_coincell_probe,
	.remove		= qpnp_coincell_remove,
	.id_table	= qpnp_coincell_id,
};

static int __init qpnp_coincell_init(void)
{
	return platform_driver_register(&qpnp_coincell_driver);
}

static void __exit qpnp_coincell_exit(void)
{
	platform_driver_unregister(&qpnp_coincell_driver);
}

MODULE_DESCRIPTION("QPNP PMIC coincell charger driver");
MODULE_LICENSE("GPL v2");

module_init(qpnp_coincell_init);
module_exit(qpnp_coincell_exit);