blob: 1eaf9773ef8b35aa93a472575e46254462a7ba63 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Łukasz Majewskie542b7f2011-10-06 02:37:34 +00002/*
3 * Copyright (C) 2011 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
5 *
6 * (C) Copyright 2010
7 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
8 *
9 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000010 */
11
12#include <common.h>
13#include <linux/types.h>
Łukasz Majewskic7336812012-11-13 03:21:55 +000014#include <power/pmic.h>
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000015#include <spi.h>
16
17static struct spi_slave *slave;
18
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000019static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
20{
21 u32 pmic_tx, pmic_rx;
22 u32 tmp;
23
24 if (!slave) {
Tom Rini5b471de2014-10-25 07:38:31 -040025 slave = spi_setup_slave(p->bus, p->hw.spi.cs, p->hw.spi.clk,
26 p->hw.spi.mode);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000027
28 if (!slave)
Jaehoon Chung505cf472016-12-15 20:49:50 +090029 return -ENODEV;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000030 }
31
Łukasz Majewskic7336812012-11-13 03:21:55 +000032 if (check_reg(p, reg))
Jaehoon Chung505cf472016-12-15 20:49:50 +090033 return -EINVAL;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000034
35 if (spi_claim_bus(slave))
Jaehoon Chung505cf472016-12-15 20:49:50 +090036 return -EBUSY;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000037
38 pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
39
40 tmp = cpu_to_be32(pmic_tx);
41
42 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
Tom Rini5b471de2014-10-25 07:38:31 -040043 pmic_spi_flags))
44 goto err;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000045
46 if (write) {
Helmut Raiger435a7282011-10-19 20:34:43 +000047 pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000048 tmp = cpu_to_be32(pmic_tx);
49 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
Tom Rini5b471de2014-10-25 07:38:31 -040050 pmic_spi_flags))
51 goto err;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000052 }
53
54 spi_release_bus(slave);
55 *val = cpu_to_be32(pmic_rx);
56
57 return 0;
Tom Rini5b471de2014-10-25 07:38:31 -040058
59err:
60 spi_release_bus(slave);
Jaehoon Chung505cf472016-12-15 20:49:50 +090061 return -ENOTSUPP;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000062}
63
64int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
65{
Jaehoon Chung505cf472016-12-15 20:49:50 +090066 return pmic_reg(p, reg, &val, 1);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000067}
68
69int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
70{
Jaehoon Chung505cf472016-12-15 20:49:50 +090071 return pmic_reg(p, reg, val, 0);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000072}