xref: /openbmc/u-boot/drivers/power/power_spi.c (revision 713cb680)
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  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28 
29 #include <common.h>
30 #include <linux/types.h>
31 #include <power/pmic.h>
32 #include <spi.h>
33 
34 static struct spi_slave *slave;
35 
36 void pmic_spi_free(struct spi_slave *slave)
37 {
38 	if (slave)
39 		spi_free_slave(slave);
40 }
41 
42 struct spi_slave *pmic_spi_probe(struct pmic *p)
43 {
44 	return spi_setup_slave(p->bus,
45 		p->hw.spi.cs,
46 		p->hw.spi.clk,
47 		p->hw.spi.mode);
48 }
49 
50 static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
51 {
52 	u32 pmic_tx, pmic_rx;
53 	u32 tmp;
54 
55 	if (!slave) {
56 		slave = pmic_spi_probe(p);
57 
58 		if (!slave)
59 			return -1;
60 	}
61 
62 	if (check_reg(p, reg))
63 		return -1;
64 
65 	if (spi_claim_bus(slave))
66 		return -1;
67 
68 	pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
69 
70 	tmp = cpu_to_be32(pmic_tx);
71 
72 	if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
73 			pmic_spi_flags)) {
74 		spi_release_bus(slave);
75 		return -1;
76 	}
77 
78 	if (write) {
79 		pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
80 		tmp = cpu_to_be32(pmic_tx);
81 		if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
82 			pmic_spi_flags)) {
83 			spi_release_bus(slave);
84 			return -1;
85 		}
86 	}
87 
88 	spi_release_bus(slave);
89 	*val = cpu_to_be32(pmic_rx);
90 
91 	return 0;
92 }
93 
94 int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
95 {
96 	if (pmic_reg(p, reg, &val, 1))
97 		return -1;
98 
99 	return 0;
100 }
101 
102 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
103 {
104 	if (pmic_reg(p, reg, val, 0))
105 		return -1;
106 
107 	return 0;
108 }
109