blob: ff35377afc3cee8a09bf6c272b51f86e80ff9440 [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 *
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) {
79 pmic_tx = p->hw.spi.prepare_tx(0, NULL, write);
80 pmic_tx &= ~(1 << 31);
81 tmp = cpu_to_be32(pmic_tx);
82 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
83 pmic_spi_flags)) {
84 spi_release_bus(slave);
85 return -1;
86 }
87 }
88
89 spi_release_bus(slave);
90 *val = cpu_to_be32(pmic_rx);
91
92 return 0;
93}
94
95int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
96{
97 if (pmic_reg(p, reg, &val, 1))
98 return -1;
99
100 return 0;
101}
102
103int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
104{
105 if (pmic_reg(p, reg, val, 0))
106 return -1;
107
108 return 0;
109}