1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2013 Freescale Semiconductor, Inc.
4  */
5 
6 /* Power-One ZM7300 DPM */
7 #include "zm7300.h"
8 #include <log.h>
9 
10 #define DPM_WP 0x96
11 #define WRP_OPCODE 0x01
12 #define WRM_OPCODE 0x02
13 #define RRP_OPCODE 0x11
14 
15 #define DPM_SUCCESS 0x01
16 #define DPM_EXEC_FAIL 0x00
17 
18 static const uint16_t hex_to_1_10mv[] = {
19 	5000,
20 	5125,
21 	5250,
22 	5375,
23 	5500,
24 	5625,
25 	5750,
26 	5875,
27 	6000,
28 	6125,
29 	6250,
30 	6375,
31 	6500,
32 	6625,
33 	6750,
34 	6875,
35 	7000,
36 	7125,
37 	7250,
38 	7375,
39 	7500,
40 	7625,
41 	7750,
42 	7875,
43 	8000,
44 	8125,
45 	8250,
46 	8375,
47 	8500,
48 	8625,
49 	8750,
50 	8875,
51 	9000,
52 	9125,
53 	9250,
54 	9375,
55 	9500,  /* 0.95mV */
56 	9625,
57 	9750,
58 	9875,
59 	10000,  /* 1.0V */
60 	10125,
61 	10250,
62 	10375,
63 	10500,
64 	10625,
65 	10750,
66 	10875,
67 	11000,
68 	11125,
69 	11250,
70 	11375,
71 	11500,
72 	11625,
73 	11750,
74 	11875,
75 	12000,
76 	12125,
77 	12250,
78 	12375,
79 	0,	/* reserved */
80 };
81 
82 
83 /* Read Data d from Register r of POL p */
dpm_rrp(uchar r)84 u8 dpm_rrp(uchar r)
85 {
86 	u8 ret[5];
87 
88 	ret[0] = RRP_OPCODE;
89 	/* POL is 0 */
90 	ret[1] = 0;
91 	ret[2] = r;
92 	i2c_read(I2C_DPM_ADDR, 0, -3, ret, 2);
93 	if (ret[1] == DPM_SUCCESS) { /* the DPM returned success as status */
94 		debug("RRP_OPCODE returned success data is %x\n", ret[0]);
95 		return ret[0];
96 	} else {
97 		return -1;
98 	}
99 }
100 
101 /* Write Data d into DPM register r (RAM) */
dpm_wrm(u8 r,u8 d)102 int dpm_wrm(u8 r, u8 d)
103 {
104 	u8 ret[5];
105 
106 	ret[0] = WRM_OPCODE;
107 	ret[1] = r;
108 	ret[2] = d;
109 	i2c_read(I2C_DPM_ADDR, 0, -3, ret, 1);
110 	if (ret[0] == DPM_SUCCESS) { /* the DPM returned success as status */
111 		debug("WRM_OPCODE returned success data is %x\n", ret[0]);
112 		return ret[0];
113 	} else {
114 		return -1;
115 	}
116 }
117 
118 /* Write Data d into Register r of POL(s) a */
dpm_wrp(u8 r,u8 d)119 int dpm_wrp(u8 r, u8 d)
120 {
121 	u8 ret[7];
122 
123 	ret[0] = WRP_OPCODE;
124 	/* only POL0 is present */
125 	ret[1] = 0x01;
126 	ret[2] = 0x00;
127 	ret[3] = 0x00;
128 	ret[4] = 0x00;
129 	ret[5] = r;
130 	ret[6] = d;
131 	i2c_read(I2C_DPM_ADDR, 0, -7, ret, 1);
132 	if (ret[0] == DPM_SUCCESS) { /* the DPM returned success as status */
133 		debug("WRP_OPCODE returned success data is %x\n", ret[0]);
134 		return 0;
135 	} else {
136 		return -1;
137 	}
138 }
139 
140 /* Uses the DPM command RRP */
zm_read(uchar reg)141 u8 zm_read(uchar reg)
142 {
143 	return dpm_rrp(reg);
144 }
145 
146 /* ZM_write --
147 	Steps:
148 	a. Write data to the register
149 	b. Read data from register and compare to written value
150 	c. Return return_code & voltage_read
151 */
zm_write(u8 reg,u8 data)152 u8 zm_write(u8 reg, u8 data)
153 {
154 	u8 d;
155 
156 	/* write data to register */
157 	dpm_wrp(reg, data);
158 
159 	/* read register and compare to written value */
160 	d = dpm_rrp(reg);
161 	if (d != data) {
162 		printf("zm_write : Comparison register data failed\n");
163 		return -1;
164 	}
165 
166 	return d;
167 }
168 
169 /* zm_write_out_voltage
170  * voltage in 1/10 mV
171  */
zm_write_voltage(int voltage)172 int zm_write_voltage(int voltage)
173 {
174 	u8 reg = 0x7, vid;
175 	uint16_t voltage_read;
176 	u8 ret;
177 
178 	vid =  (voltage - 5000) / ZM_STEP;
179 
180 	ret = zm_write(reg, vid);
181 	if (ret != -1) {
182 		voltage_read = hex_to_1_10mv[ret];
183 		debug("voltage set to %dmV\n", voltage_read/10);
184 		return voltage_read;
185 	}
186 	return -1;
187 }
188 
189 /* zm_read_out_voltage
190  * voltage in 1/10 mV
191  */
zm_read_voltage(void)192 int zm_read_voltage(void)
193 {
194 	u8 reg = 0x7;
195 	u8 ret;
196 	int voltage;
197 
198 	ret = zm_read(reg);
199 	if (ret != -1) {
200 		voltage =  hex_to_1_10mv[ret];
201 		debug("Voltage read is %dmV\n", voltage/10);
202 		return voltage;
203 	} else {
204 		return -1;
205 	}
206 }
207 
zm_disable_wp()208 int zm_disable_wp()
209 {
210 	u8 new_wp_value;
211 
212 	/* Disable using Write-Protect register 0x96 */
213 	new_wp_value = 0x8;
214 	if ((dpm_wrm(DPM_WP, new_wp_value)) < 0) {
215 		printf("Disable Write-Protect register failed\n");
216 		return -1;
217 	}
218 	return 0;
219 }
220 
zm_enable_wp()221 int zm_enable_wp()
222 {
223 	u8 orig_wp_value;
224 	orig_wp_value = 0x0;
225 
226 	/* Enable using Write-Protect register 0x96 */
227 	if ((dpm_wrm(DPM_WP, orig_wp_value)) < 0) {
228 		printf("Enable Write-Protect register failed\n");
229 		return -1;
230 	}
231 	return 0;
232 }
233 
234