aboutsummaryrefslogtreecommitdiff
path: root/drivers/power/lp8720.c
blob: ac7fc1167e7bfa270078ba9d88f03101bc72fcc1 (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
/*
 * Copyright (C) 2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <i2c.h>
#include <asm/gpio.h>
#include <lp8720.h>

static struct lp8720_info lp8720_info;

static int lp8720_write(u8 reg, u8 val)
{
	return i2c_write(lp8720_info.chip_idsel, reg, 1, &val, 1);
}

static int lp8720_read(u8 reg, u8 *val)
{
	return i2c_read(lp8720_info.chip_idsel, reg, 1, val, 1);
}

int lp8720_init(int enable_gpio, int chip_idsel)
{
	int ret;

	if (enable_gpio) {
		if (!gpio_is_valid(enable_gpio))
			return -1;

		ret = gpio_request(enable_gpio, "lp8720_en");
		if (ret)
			return ret;

		ret = gpio_direction_output(enable_gpio, 0);
		if (ret)
			return ret;
	}

	lp8720_info.enable_gpio = enable_gpio;
	lp8720_info.chip_idsel = chip_idsel;

	return 0;
}

int lp8720_enable(void)
{
	int ret;

	if (lp8720_info.enable_gpio) {
		ret = gpio_set_value(lp8720_info.enable_gpio, 1);
		if (ret)
			return ret;
	}

	return 0;
}

int lp8720_is_enabled(void)
{
	if (lp8720_info.enable_gpio)
		return gpio_get_value(lp8720_info.enable_gpio);

	/* Assume LP8720 enabled when there is no enable GPIO */
	return 1;
}

int lp8720_ldo_enable(u8 ldo_enable)
{
	u8 val;
	int ret;

	ret = lp8720_read(LP8720_ENABLE_BITS, &val);
	if (ret)
		return ret;

	val |= ldo_enable;

	ret = lp8720_write(LP8720_ENABLE_BITS, val);
	if (ret)
		return ret;

	/* Enable the IC */
	if (!lp8720_is_enabled())
		lp8720_enable();

	return 0;
}

int lp8720_ldo_voltage(u8 ldo_reg, u8 voltage, u8 delay)
{
	u8 val;
	int ret;

	/* Register V field */
	val = voltage & 0x1F;

	/* Register T field */
	val |= (delay & 0x07) << 5;

	ret = lp8720_write(ldo_reg, val);
	if (ret)
		return ret;

	return 0;
}