blob: 1e554461f30706f091d788a1032f56808154c54d [file] [log] [blame]
Łukasz Majewskie542b7f2011-10-06 02:37:34 +00001/*
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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000011 */
12
13#include <common.h>
14#include <linux/types.h>
Łukasz Majewskic7336812012-11-13 03:21:55 +000015#include <power/pmic.h>
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000016#include <spi.h>
17
18static struct spi_slave *slave;
19
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000020static 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) {
Tom Rini5b471de2014-10-25 07:38:31 -040026 slave = spi_setup_slave(p->bus, p->hw.spi.cs, p->hw.spi.clk,
27 p->hw.spi.mode);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000028
29 if (!slave)
30 return -1;
31 }
32
Łukasz Majewskic7336812012-11-13 03:21:55 +000033 if (check_reg(p, reg))
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000034 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,
Tom Rini5b471de2014-10-25 07:38:31 -040044 pmic_spi_flags))
45 goto err;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000046
47 if (write) {
Helmut Raiger435a7282011-10-19 20:34:43 +000048 pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000049 tmp = cpu_to_be32(pmic_tx);
50 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
Tom Rini5b471de2014-10-25 07:38:31 -040051 pmic_spi_flags))
52 goto err;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000053 }
54
55 spi_release_bus(slave);
56 *val = cpu_to_be32(pmic_rx);
57
58 return 0;
Tom Rini5b471de2014-10-25 07:38:31 -040059
60err:
61 spi_release_bus(slave);
62 return -1;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000063}
64
65int 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
73int 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}