]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/power/power_spi.c
net: sh_eth: Fix DT base address fetching
[people/ms/u-boot.git] / drivers / power / power_spi.c
CommitLineData
e542b7f0
ŁM
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 *
1a459660 10 * SPDX-License-Identifier: GPL-2.0+
e542b7f0
ŁM
11 */
12
13#include <common.h>
14#include <linux/types.h>
c7336815 15#include <power/pmic.h>
e542b7f0
ŁM
16#include <spi.h>
17
18static struct spi_slave *slave;
19
e542b7f0
ŁM
20static 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) {
5b471dee
TR
26 slave = spi_setup_slave(p->bus, p->hw.spi.cs, p->hw.spi.clk,
27 p->hw.spi.mode);
e542b7f0
ŁM
28
29 if (!slave)
505cf475 30 return -ENODEV;
e542b7f0
ŁM
31 }
32
c7336815 33 if (check_reg(p, reg))
505cf475 34 return -EINVAL;
e542b7f0
ŁM
35
36 if (spi_claim_bus(slave))
505cf475 37 return -EBUSY;
e542b7f0
ŁM
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,
5b471dee
TR
44 pmic_spi_flags))
45 goto err;
e542b7f0
ŁM
46
47 if (write) {
435a7285 48 pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
e542b7f0
ŁM
49 tmp = cpu_to_be32(pmic_tx);
50 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
5b471dee
TR
51 pmic_spi_flags))
52 goto err;
e542b7f0
ŁM
53 }
54
55 spi_release_bus(slave);
56 *val = cpu_to_be32(pmic_rx);
57
58 return 0;
5b471dee
TR
59
60err:
61 spi_release_bus(slave);
505cf475 62 return -ENOTSUPP;
e542b7f0
ŁM
63}
64
65int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
66{
505cf475 67 return pmic_reg(p, reg, &val, 1);
e542b7f0
ŁM
68}
69
70int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
71{
505cf475 72 return pmic_reg(p, reg, val, 0);
e542b7f0 73}