blob: 5a0dd22e29078c6a4b0b8e9fc7d2ad2867ece25e [file] [log] [blame]
Łukasz Majewski16db0622011-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 *
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 <pmic.h>
32#include <spi.h>
33
34static struct spi_slave *slave;
35
36void pmic_spi_free(struct spi_slave *slave)
37{
38 if (slave)
39 spi_free_slave(slave);
40}
41
42struct 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
50static 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(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) {
Helmut Raigerb6e18412011-10-19 20:34:43 +000079 pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
Łukasz Majewski16db0622011-10-06 02:37:34 +000080 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
94int 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
102int 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}