summaryrefslogtreecommitdiff
path: root/drivers/power/pmic/pm8916.c
blob: ddc30604cff86d6d95e5051fa46fb289c4f0bbe1 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Qualcomm pm8916 pmic driver
 *
 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
 */
#include <common.h>
#include <dm.h>
#include <power/pmic.h>
#include <spmi/spmi.h>

#define PID_SHIFT 8
#define PID_MASK (0xFF << PID_SHIFT)
#define REG_MASK 0xFF

struct pm8916_priv {
	uint32_t usid; /* Slave ID on SPMI bus */
};

static int pm8916_reg_count(struct udevice *dev)
{
	return 0xFFFF;
}

static int pm8916_write(struct udevice *dev, uint reg, const uint8_t *buff,
			int len)
{
	struct pm8916_priv *priv = dev_get_priv(dev);

	if (len != 1)
		return -EINVAL;

	return spmi_reg_write(dev->parent, priv->usid,
			      (reg & PID_MASK) >> PID_SHIFT, reg & REG_MASK,
			      *buff);
}

static int pm8916_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
{
	struct pm8916_priv *priv = dev_get_priv(dev);
	int val;

	if (len != 1)
		return -EINVAL;

	val = spmi_reg_read(dev->parent, priv->usid,
			    (reg & PID_MASK) >> PID_SHIFT, reg & REG_MASK);

	if (val < 0)
		return val;
	*buff = val;
	return 0;
}

static struct dm_pmic_ops pm8916_ops = {
	.reg_count = pm8916_reg_count,
	.read = pm8916_read,
	.write = pm8916_write,
};

static const struct udevice_id pm8916_ids[] = {
	{ .compatible = "qcom,spmi-pmic" },
	{ }
};

static int pm8916_probe(struct udevice *dev)
{
	struct pm8916_priv *priv = dev_get_priv(dev);

	priv->usid = dev_read_addr(dev);

	if (priv->usid == FDT_ADDR_T_NONE)
		return -EINVAL;

	return 0;
}

U_BOOT_DRIVER(pmic_pm8916) = {
	.name = "pmic_pm8916",
	.id = UCLASS_PMIC,
	.of_match = pm8916_ids,
	.bind = dm_scan_fdt_dev,
	.probe = pm8916_probe,
	.ops = &pm8916_ops,
	.priv_auto_alloc_size = sizeof(struct pm8916_priv),
};