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