blob: 54427316ce42f2163767e12b475ac5a3602a837f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Łukasz Majewski16db0622011-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 Majewski16db0622011-10-06 02:37:34 +000010 */
11
Łukasz Majewski16db0622011-10-06 02:37:34 +000012#include <linux/types.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000013#include <power/pmic.h>
Łukasz Majewski16db0622011-10-06 02:37:34 +000014#include <spi.h>
15
16static struct spi_slave *slave;
17
Łukasz Majewski16db0622011-10-06 02:37:34 +000018static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
19{
20 u32 pmic_tx, pmic_rx;
21 u32 tmp;
22
23 if (!slave) {
Tom Rini9ea18542014-10-25 07:38:31 -040024 slave = spi_setup_slave(p->bus, p->hw.spi.cs, p->hw.spi.clk,
25 p->hw.spi.mode);
Łukasz Majewski16db0622011-10-06 02:37:34 +000026
27 if (!slave)
Jaehoon Chung10e80f32016-12-15 20:49:50 +090028 return -ENODEV;
Łukasz Majewski16db0622011-10-06 02:37:34 +000029 }
30
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000031 if (check_reg(p, reg))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090032 return -EINVAL;
Łukasz Majewski16db0622011-10-06 02:37:34 +000033
34 if (spi_claim_bus(slave))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090035 return -EBUSY;
Łukasz Majewski16db0622011-10-06 02:37:34 +000036
37 pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
38
39 tmp = cpu_to_be32(pmic_tx);
40
41 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
Tom Rini9ea18542014-10-25 07:38:31 -040042 pmic_spi_flags))
43 goto err;
Łukasz Majewski16db0622011-10-06 02:37:34 +000044
45 if (write) {
Helmut Raigerb6e18412011-10-19 20:34:43 +000046 pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
Łukasz Majewski16db0622011-10-06 02:37:34 +000047 tmp = cpu_to_be32(pmic_tx);
48 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
Tom Rini9ea18542014-10-25 07:38:31 -040049 pmic_spi_flags))
50 goto err;
Łukasz Majewski16db0622011-10-06 02:37:34 +000051 }
52
53 spi_release_bus(slave);
54 *val = cpu_to_be32(pmic_rx);
55
56 return 0;
Tom Rini9ea18542014-10-25 07:38:31 -040057
58err:
59 spi_release_bus(slave);
Jaehoon Chung10e80f32016-12-15 20:49:50 +090060 return -ENOTSUPP;
Łukasz Majewski16db0622011-10-06 02:37:34 +000061}
62
63int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
64{
Jaehoon Chung10e80f32016-12-15 20:49:50 +090065 return pmic_reg(p, reg, &val, 1);
Łukasz Majewski16db0622011-10-06 02:37:34 +000066}
67
68int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
69{
Jaehoon Chung10e80f32016-12-15 20:49:50 +090070 return pmic_reg(p, reg, val, 0);
Łukasz Majewski16db0622011-10-06 02:37:34 +000071}