blob: ef8531df0ff286c943e469ce560a727df1acbae9 [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
Łukasz Majewski16db0622011-10-06 02:37:34 +000020static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
21{
22 u32 pmic_tx, pmic_rx;
23 u32 tmp;
24
25 if (!slave) {
Tom Rini9ea18542014-10-25 07:38:31 -040026 slave = spi_setup_slave(p->bus, p->hw.spi.cs, p->hw.spi.clk,
27 p->hw.spi.mode);
Łukasz Majewski16db0622011-10-06 02:37:34 +000028
29 if (!slave)
Jaehoon Chung10e80f32016-12-15 20:49:50 +090030 return -ENODEV;
Łukasz Majewski16db0622011-10-06 02:37:34 +000031 }
32
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000033 if (check_reg(p, reg))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090034 return -EINVAL;
Łukasz Majewski16db0622011-10-06 02:37:34 +000035
36 if (spi_claim_bus(slave))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090037 return -EBUSY;
Łukasz Majewski16db0622011-10-06 02:37:34 +000038
39 pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
40
41 tmp = cpu_to_be32(pmic_tx);
42
43 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
Tom Rini9ea18542014-10-25 07:38:31 -040044 pmic_spi_flags))
45 goto err;
Łukasz Majewski16db0622011-10-06 02:37:34 +000046
47 if (write) {
Helmut Raigerb6e18412011-10-19 20:34:43 +000048 pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
Łukasz Majewski16db0622011-10-06 02:37:34 +000049 tmp = cpu_to_be32(pmic_tx);
50 if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
Tom Rini9ea18542014-10-25 07:38:31 -040051 pmic_spi_flags))
52 goto err;
Łukasz Majewski16db0622011-10-06 02:37:34 +000053 }
54
55 spi_release_bus(slave);
56 *val = cpu_to_be32(pmic_rx);
57
58 return 0;
Tom Rini9ea18542014-10-25 07:38:31 -040059
60err:
61 spi_release_bus(slave);
Jaehoon Chung10e80f32016-12-15 20:49:50 +090062 return -ENOTSUPP;
Łukasz Majewski16db0622011-10-06 02:37:34 +000063}
64
65int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
66{
Jaehoon Chung10e80f32016-12-15 20:49:50 +090067 return pmic_reg(p, reg, &val, 1);
Łukasz Majewski16db0622011-10-06 02:37:34 +000068}
69
70int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
71{
Jaehoon Chung10e80f32016-12-15 20:49:50 +090072 return pmic_reg(p, reg, val, 0);
Łukasz Majewski16db0622011-10-06 02:37:34 +000073}