xref: /openbmc/u-boot/drivers/power/power_spi.c (revision 7c1058fa)
1 /*
2  * Copyright (C) 2011 Samsung Electronics
3  * Lukasz Majewski <l.majewski@samsung.com>
4  *
5  * (C) Copyright 2010
6  * Stefano Babic, DENX Software Engineering, sbabic@denx.de
7  *
8  * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
9  *
10  * SPDX-License-Identifier:	GPL-2.0+
11  */
12 
13 #include <common.h>
14 #include <linux/types.h>
15 #include <power/pmic.h>
16 #include <spi.h>
17 
18 static struct spi_slave *slave;
19 
20 static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
21 {
22 	u32 pmic_tx, pmic_rx;
23 	u32 tmp;
24 
25 	if (!slave) {
26 		slave = spi_setup_slave(p->bus, p->hw.spi.cs, p->hw.spi.clk,
27 					p->hw.spi.mode);
28 
29 		if (!slave)
30 			return -1;
31 	}
32 
33 	if (check_reg(p, reg))
34 		return -1;
35 
36 	if (spi_claim_bus(slave))
37 		return -1;
38 
39 	pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
40 
41 	tmp = cpu_to_be32(pmic_tx);
42 
43 	if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
44 			pmic_spi_flags))
45 		goto err;
46 
47 	if (write) {
48 		pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
49 		tmp = cpu_to_be32(pmic_tx);
50 		if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
51 			pmic_spi_flags))
52 			goto err;
53 	}
54 
55 	spi_release_bus(slave);
56 	*val = cpu_to_be32(pmic_rx);
57 
58 	return 0;
59 
60 err:
61 	spi_release_bus(slave);
62 	return -1;
63 }
64 
65 int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
66 {
67 	if (pmic_reg(p, reg, &val, 1))
68 		return -1;
69 
70 	return 0;
71 }
72 
73 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
74 {
75 	if (pmic_reg(p, reg, val, 0))
76 		return -1;
77 
78 	return 0;
79 }
80