Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 1 | /* |
| 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 Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <linux/types.h> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 15 | #include <power/pmic.h> |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 16 | #include <spi.h> |
| 17 | |
| 18 | static struct spi_slave *slave; |
| 19 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 20 | static 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 Rini | 9ea1854 | 2014-10-25 07:38:31 -0400 | [diff] [blame] | 26 | slave = spi_setup_slave(p->bus, p->hw.spi.cs, p->hw.spi.clk, |
| 27 | p->hw.spi.mode); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 28 | |
| 29 | if (!slave) |
| 30 | return -1; |
| 31 | } |
| 32 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 33 | if (check_reg(p, reg)) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 34 | return -1; |
| 35 | |
| 36 | if (spi_claim_bus(slave)) |
| 37 | return -1; |
| 38 | |
| 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 Rini | 9ea1854 | 2014-10-25 07:38:31 -0400 | [diff] [blame] | 44 | pmic_spi_flags)) |
| 45 | goto err; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 46 | |
| 47 | if (write) { |
Helmut Raiger | b6e1841 | 2011-10-19 20:34:43 +0000 | [diff] [blame] | 48 | pmic_tx = p->hw.spi.prepare_tx(reg, val, 0); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 49 | tmp = cpu_to_be32(pmic_tx); |
| 50 | if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx, |
Tom Rini | 9ea1854 | 2014-10-25 07:38:31 -0400 | [diff] [blame] | 51 | pmic_spi_flags)) |
| 52 | goto err; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | spi_release_bus(slave); |
| 56 | *val = cpu_to_be32(pmic_rx); |
| 57 | |
| 58 | return 0; |
Tom Rini | 9ea1854 | 2014-10-25 07:38:31 -0400 | [diff] [blame] | 59 | |
| 60 | err: |
| 61 | spi_release_bus(slave); |
| 62 | return -1; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int pmic_reg_write(struct pmic *p, u32 reg, u32 val) |
| 66 | { |
| 67 | if (pmic_reg(p, reg, &val, 1)) |
| 68 | return -1; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int pmic_reg_read(struct pmic *p, u32 reg, u32 *val) |
| 74 | { |
| 75 | if (pmic_reg(p, reg, val, 0)) |
| 76 | return -1; |
| 77 | |
| 78 | return 0; |
| 79 | } |