aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/qpnp-misc.c
blob: a38ed1e205d25e3c13a759b5b204ff0cbdce5e1c (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
284
285
286
287
288
289
290
/* Copyright (c) 2013, 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/module.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/spmi.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/qpnp-misc.h>

#define QPNP_MISC_DEV_NAME "qcom,qpnp-misc"

#define REG_DIG_MAJOR_REV	0x01
#define REG_SUBTYPE		0x05

static DEFINE_MUTEX(qpnp_misc_dev_list_mutex);
static LIST_HEAD(qpnp_misc_dev_list);

/**
 * struct qpnp_misc_dev - holds controller device specific information
 * @list:			Doubly-linked list parameter linking to other
 *				qpnp_misc devices.
 * @mutex:			Mutex lock that is used to ensure mutual
 *				exclusion between probing and accessing misc
 *				driver information
 * @dev:			Device pointer to the misc device
 * @resource:			Resource pointer that holds base address
 * @spmi:			Spmi pointer which holds spmi information
 */
struct qpnp_misc_dev {
	struct list_head		list;
	struct mutex			mutex;
	struct device			*dev;
	struct resource			*resource;
	struct spmi_device		*spmi;
};

struct qpnp_misc_version {
	u8				subtype;
	u8				dig_major_rev;
};

static struct of_device_id qpnp_misc_match_table[] = {
	{ .compatible = QPNP_MISC_DEV_NAME },
	{}
};

static u8 qpnp_read_byte(struct spmi_device *spmi, u16 addr)
{
	int rc;
	u8 val;

	rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, addr, &val, 1);
	if (rc) {
		pr_err("SPMI read failed rc=%d\n", rc);
		return 0;
	}
	return val;
}
#if defined(CONFIG_SMB349_CHARGER) || defined(CONFIG_BQ24192_CHARGER)
struct spmi_device		*the_spmi;
#define SMBB_BAT_IF_BAT_PRES_STATUS 0x1208
#endif

#ifdef CONFIG_SMB349_CHARGER
static int
smb349_qpnp_write(struct spmi_device *spmi, u16 addr, u8 val)
{
	int rc;

	rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, addr, &val, 1);
	if (rc) {
		pr_err("spmi write failed: addr=%03X, rc=%d\n", addr, rc);
		return rc;
	}

	return 0;
}
int smb349_pmic_usb_override(bool mode)
{
	int rc;

	if (!the_spmi) {
		pr_err("fail to override spmi not init\n");
		return -ENODEV;
	}
	if (mode) {
		/* charger insert case */
		rc = smb349_qpnp_write(the_spmi, 0x13D0, 0xA5);
		if (rc) {
			pr_err("failed to write pmic 0x13D0 ret:%d\n", rc);
			return rc;
		}
		rc = smb349_qpnp_write(the_spmi, 0x13EA, 0x2F);
		if (rc) {
			pr_err("failed to write pmic 0x13EA ret:%d\n", rc);
			return rc;
		}
	} else {
		/* charser remove case */
		rc = smb349_qpnp_write(the_spmi, 0x13D0, 0xA5);
		if (rc) {
			pr_err("failed to write pmic 0x13D0 ret:%d\n", rc);
			return rc;
		}
		rc = smb349_qpnp_write(the_spmi, 0x13EA, 0x00);
		if (rc) {
			pr_err("failed to write pmic 0x13D0 ret:%d\n", rc);
			return rc;
		}
	}

	return 0;
}
bool smb349_pmic_batt_present(void)
{
	int rc;
	u8  val;

	if (!the_spmi) {
		pr_err("fail to override spmi not init\n");
		return true;
	}
	rc = spmi_ext_register_readl(the_spmi->ctrl,
				the_spmi->sid, SMBB_BAT_IF_BAT_PRES_STATUS, &val, 1);
	if (rc) {
		pr_err("failed to read pmic 0x1208 rc:%d\n", rc);
		return true;
	}

	return val & BIT(7);
}
static u16 smb349_pmic_regs[] = {
	0xDD08, 0xDD10, 0xDD11, 0xDD12,
	0xDD13, 0xDD14, 0xDD15, 0xDD16,
	0xDD18, 0xDD19, 0xDD1A, 0xDD1B,
	0xDD40, 0xDD41, 0xDD42, 0xDD43,
	0xDD45, 0xDD46,
};
void smb349_pmic_reg_dump(void)
{
	int i;
	int rc;
	u8  val;

	if (!the_spmi) {
		pr_err("fail to pmic_reg dump spmi not init\n");
		return;
	}

	for (i = 0 ; i < ARRAY_SIZE(smb349_pmic_regs) ; i++) {
		rc = spmi_ext_register_readl(the_spmi->ctrl,
					the_spmi->sid, smb349_pmic_regs[i], &val, 1);
		pr_err("[DUMP] pmic 0x%04X: 0x%02X\n", smb349_pmic_regs[i], val);
	}

}
#endif
static struct qpnp_misc_version irq_support_version[] = {
	{0x01, 0x02}, /* PM8941 */
	{0x07, 0x00}, /* PM8226 */
	{0x09, 0x00}, /* PMA8084 */
};

static bool __misc_irqs_available(struct qpnp_misc_dev *dev)
{
	int i;
	u8 subtype, dig_major_rev;

	subtype = qpnp_read_byte(dev->spmi, dev->resource->start + REG_SUBTYPE);
	pr_debug("subtype = 0x%02X\n", subtype);

	dig_major_rev = qpnp_read_byte(dev->spmi,
		dev->resource->start + REG_DIG_MAJOR_REV);
	pr_debug("dig_major rev = 0x%02X\n", dig_major_rev);

	for (i = 0; i < ARRAY_SIZE(irq_support_version); i++)
		if (subtype == irq_support_version[i].subtype
		    && dig_major_rev >= irq_support_version[i].dig_major_rev)
			return 1;

	return 0;
}

int qpnp_misc_irqs_available(struct device *consumer_dev)
{
	struct device_node *misc_node = NULL;
	struct qpnp_misc_dev *mdev = NULL;
	struct qpnp_misc_dev *mdev_found = NULL;

	if (IS_ERR_OR_NULL(consumer_dev)) {
		pr_err("Invalid consumer device pointer\n");
		return -EINVAL;
	}

	misc_node = of_parse_phandle(consumer_dev->of_node, "qcom,misc-ref", 0);
	if (!misc_node) {
		pr_debug("Could not find qcom,misc-ref property in %s\n",
			consumer_dev->of_node->full_name);
		return 0;
	}

	mutex_lock(&qpnp_misc_dev_list_mutex);
	list_for_each_entry(mdev, &qpnp_misc_dev_list, list) {
		if (mdev->dev->of_node == misc_node) {
			mdev_found = mdev;
			break;
		}
	}
	mutex_unlock(&qpnp_misc_dev_list_mutex);

	if (!mdev_found) {
		/* No MISC device was found. This API should only
		 * be called by drivers which have specified the
		 * misc phandle in their device tree node */
		pr_err("no probed misc device found\n");
		return -EPROBE_DEFER;
	}

	return __misc_irqs_available(mdev_found);
}

static int __devinit qpnp_misc_probe(struct spmi_device *spmi)
{
	struct resource *resource;
	struct qpnp_misc_dev *mdev = ERR_PTR(-EINVAL);

	resource = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
	if (!resource) {
		pr_err("Unable to get spmi resource for MISC\n");
		return -EINVAL;
	}

	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
	if (!mdev) {
		pr_err("allocation failed\n");
		return -ENOMEM;
	}

	mdev->spmi = spmi;
	mdev->dev = &(spmi->dev);
	mdev->resource = resource;
#if defined(CONFIG_SMB349_CHARGER) || defined(CONFIG_BQ24192_CHARGER)
	the_spmi = spmi;
#endif

	mutex_lock(&qpnp_misc_dev_list_mutex);
	list_add_tail(&mdev->list, &qpnp_misc_dev_list);
	mutex_unlock(&qpnp_misc_dev_list_mutex);

	pr_debug("probed successfully\n");
	return 0;
}

static struct spmi_driver qpnp_misc_driver = {
	.probe	= qpnp_misc_probe,
	.driver	= {
		.name		= QPNP_MISC_DEV_NAME,
		.owner		= THIS_MODULE,
		.of_match_table	= qpnp_misc_match_table,
	},
};

static int __init qpnp_misc_init(void)
{
	return spmi_driver_register(&qpnp_misc_driver);
}

static void __exit qpnp_misc_exit(void)
{
	return spmi_driver_unregister(&qpnp_misc_driver);
}

module_init(qpnp_misc_init);
module_exit(qpnp_misc_exit);

MODULE_DESCRIPTION(QPNP_MISC_DEV_NAME);
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" QPNP_MISC_DEV_NAME);