blob: d3e72da0d35a8b0a0b4272136d3b54c45f56bcdb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd102cf62015-08-30 16:55:30 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 *
6 * Based on Rockchip's drivers/power/pmic/pmic_act8846.c:
7 * Copyright (C) 2012 rockchips
8 * zyw <zyw@rock-chips.com>
Simon Glassd102cf62015-08-30 16:55:30 -06009 */
10
Simon Glassd102cf62015-08-30 16:55:30 -060011#include <dm.h>
12#include <errno.h>
13#include <power/act8846_pmic.h>
14#include <power/pmic.h>
15#include <power/regulator.h>
16
17static const u16 voltage_map[] = {
18 600, 625, 650, 675, 700, 725, 750, 775,
19 800, 825, 850, 875, 900, 925, 950, 975,
20 1000, 1025, 1050, 1075, 1100, 1125, 1150, 1175,
21 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550,
22 1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950,
23 2000, 2050, 2100, 2150, 2200, 2250, 2300, 2350,
24 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100,
25 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900,
26};
27
28enum {
29 REG_SYS0,
30 REG_SYS1,
31 REG1_VOL = 0x10,
32 REG1_CTL = 0X11,
33 REG2_VOL0 = 0x20,
34 REG2_VOL1,
35 REG2_CTL,
36 REG3_VOL0 = 0x30,
37 REG3_VOL1,
38 REG3_CTL,
39 REG4_VOL0 = 0x40,
40 REG4_VOL1,
41 REG4_CTL,
42 REG5_VOL = 0x50,
43 REG5_CTL,
44 REG6_VOL = 0X58,
45 REG6_CTL,
46 REG7_VOL = 0x60,
47 REG7_CTL,
48 REG8_VOL = 0x68,
49 REG8_CTL,
50 REG9_VOL = 0x70,
51 REG9_CTL,
52 REG10_VOL = 0x80,
53 REG10_CTL,
54 REG11_VOL = 0x90,
55 REG11_CTL,
56 REG12_VOL = 0xa0,
57 REG12_CTL,
58 REG13 = 0xb1,
59};
60
61static const u8 addr_vol[] = {
62 0, REG1_VOL, REG2_VOL0, REG3_VOL0, REG4_VOL0,
63 REG5_VOL, REG6_VOL, REG7_VOL, REG8_VOL, REG9_VOL,
64 REG10_VOL, REG11_VOL, REG12_VOL,
65};
66
67static const u8 addr_ctl[] = {
68 0, REG1_CTL, REG2_CTL, REG3_CTL, REG4_CTL,
69 REG5_CTL, REG6_CTL, REG7_CTL, REG8_CTL, REG9_CTL,
70 REG10_CTL, REG11_CTL, REG12_CTL,
71};
72
73static int check_volt_table(const u16 *volt_table, int uvolt)
74{
75 int i;
76
77 for (i = VOL_MIN_IDX; i < VOL_MAX_IDX; i++) {
78 if (uvolt <= (volt_table[i] * 1000))
79 return i;
80 }
81 return -EINVAL;
82}
83
84static int reg_get_value(struct udevice *dev)
85{
86 int reg = dev->driver_data;
87 int ret;
88
John Keepingbfea23a2016-08-07 12:55:39 +010089 ret = pmic_reg_read(dev->parent, addr_vol[reg]);
Simon Glassd102cf62015-08-30 16:55:30 -060090 if (ret < 0)
91 return ret;
92
93 return voltage_map[ret & LDO_VOL_MASK] * 1000;
94}
95
96static int reg_set_value(struct udevice *dev, int uvolt)
97{
98 int reg = dev->driver_data;
99 int val;
100
101 val = check_volt_table(voltage_map, uvolt);
102 if (val < 0)
103 return val;
104
105 return pmic_clrsetbits(dev->parent, addr_vol[reg], LDO_VOL_MASK, val);
106}
107
108static int reg_set_enable(struct udevice *dev, bool enable)
109{
110 int reg = dev->driver_data;
111
112 return pmic_clrsetbits(dev->parent, addr_ctl[reg], LDO_EN_MASK,
113 enable ? LDO_EN_MASK : 0);
114}
115
Keerthy9bb8fd02017-06-13 09:53:47 +0530116static int reg_get_enable(struct udevice *dev)
Simon Glassd102cf62015-08-30 16:55:30 -0600117{
118 int reg = dev->driver_data;
119 int ret;
120
John Keepingbfea23a2016-08-07 12:55:39 +0100121 ret = pmic_reg_read(dev->parent, addr_ctl[reg]);
Simon Glassd102cf62015-08-30 16:55:30 -0600122 if (ret < 0)
123 return ret;
124
125 return ret & LDO_EN_MASK ? true : false;
126}
127
128static int act8846_reg_probe(struct udevice *dev)
129{
Simon Glass71fa5b42020-12-03 16:55:18 -0700130 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glassd102cf62015-08-30 16:55:30 -0600131 int reg = dev->driver_data;
132
Simon Glass71fa5b42020-12-03 16:55:18 -0700133 uc_pdata = dev_get_uclass_plat(dev);
Simon Glassd102cf62015-08-30 16:55:30 -0600134
135 uc_pdata->type = reg <= 4 ? REGULATOR_TYPE_BUCK : REGULATOR_TYPE_LDO;
136 uc_pdata->mode_count = 0;
137
138 return 0;
139}
140
141static const struct dm_regulator_ops act8846_reg_ops = {
142 .get_value = reg_get_value,
143 .set_value = reg_set_value,
144 .get_enable = reg_get_enable,
145 .set_enable = reg_set_enable,
146};
147
148U_BOOT_DRIVER(act8846_buck) = {
149 .name = "act8846_reg",
150 .id = UCLASS_REGULATOR,
151 .ops = &act8846_reg_ops,
152 .probe = act8846_reg_probe,
153};