blob: fb455a00617835ff6861756d92d416118b66c167 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Łukasz Majewski16db0622011-10-06 02:37:34 +000011 */
12
13#include <common.h>
14#include <linux/types.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000015#include <power/pmic.h>
Łukasz Majewski16db0622011-10-06 02:37:34 +000016#include <spi.h>
17
18static struct spi_slave *slave;
19
20void pmic_spi_free(struct spi_slave *slave)
21{
22 if (slave)
23 spi_free_slave(slave);
24}
25
26struct spi_slave *pmic_spi_probe(struct pmic *p)
27{
28 return spi_setup_slave(p->bus,
29 p->hw.spi.cs,
30 p->hw.spi.clk,
31 p->hw.spi.mode);
32}
33
34static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
35{
36 u32 pmic_tx, pmic_rx;
37 u32 tmp;
38
39 if (!slave) {
40 slave = pmic_spi_probe(p);
41
42 if (!slave)
43 return -1;
44 }
45
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000046 if (check_reg(p, reg))
Łukasz Majewski16db0622011-10-06 02:37:34 +000047 return -1;
48
49 if (spi_claim_bus(slave))
50 return -1;
51
52 pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
53
54 tmp = cpu_to_be32(pmic_tx);
55
56 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
57 pmic_spi_flags)) {
58 spi_release_bus(slave);
59 return -1;
60 }
61
62 if (write) {
Helmut Raigerb6e18412011-10-19 20:34:43 +000063 pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
Łukasz Majewski16db0622011-10-06 02:37:34 +000064 tmp = cpu_to_be32(pmic_tx);
65 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
66 pmic_spi_flags)) {
67 spi_release_bus(slave);
68 return -1;
69 }
70 }
71
72 spi_release_bus(slave);
73 *val = cpu_to_be32(pmic_rx);
74
75 return 0;
76}
77
78int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
79{
80 if (pmic_reg(p, reg, &val, 1))
81 return -1;
82
83 return 0;
84}
85
86int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
87{
88 if (pmic_reg(p, reg, val, 0))
89 return -1;
90
91 return 0;
92}