xref: /openbmc/u-boot/drivers/power/power_spi.c (revision 425faf74)
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 void pmic_spi_free(struct spi_slave *slave)
21 {
22 	if (slave)
23 		spi_free_slave(slave);
24 }
25 
26 struct spi_slave *pmic_spi_probe(struct pmic *p)
27 {
28 	return spi_setup_slave(p->bus,
29 		p->hw.spi.cs,
30 		p->hw.spi.clk,
31 		p->hw.spi.mode);
32 }
33 
34 static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
35 {
36 	u32 pmic_tx, pmic_rx;
37 	u32 tmp;
38 
39 	if (!slave) {
40 		slave = pmic_spi_probe(p);
41 
42 		if (!slave)
43 			return -1;
44 	}
45 
46 	if (check_reg(p, reg))
47 		return -1;
48 
49 	if (spi_claim_bus(slave))
50 		return -1;
51 
52 	pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
53 
54 	tmp = cpu_to_be32(pmic_tx);
55 
56 	if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
57 			pmic_spi_flags)) {
58 		spi_release_bus(slave);
59 		return -1;
60 	}
61 
62 	if (write) {
63 		pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
64 		tmp = cpu_to_be32(pmic_tx);
65 		if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
66 			pmic_spi_flags)) {
67 			spi_release_bus(slave);
68 			return -1;
69 		}
70 	}
71 
72 	spi_release_bus(slave);
73 	*val = cpu_to_be32(pmic_rx);
74 
75 	return 0;
76 }
77 
78 int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
79 {
80 	if (pmic_reg(p, reg, &val, 1))
81 		return -1;
82 
83 	return 0;
84 }
85 
86 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
87 {
88 	if (pmic_reg(p, reg, val, 0))
89 		return -1;
90 
91 	return 0;
92 }
93