1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright (C) 2011 Samsung Electronics
4  *  Lukasz Majewski <l.majewski@samsung.com>
5  */
6 
7 #include <common.h>
8 #include <spi.h>
9 #include <power/pmic.h>
10 #include <fsl_pmic.h>
11 #include <errno.h>
12 
13 #if defined(CONFIG_POWER_FSL_MC13892)
14 #define FSL_PMIC_I2C_LENGTH	3
15 #elif defined(CONFIG_POWER_FSL_MC34704)
16 #define FSL_PMIC_I2C_LENGTH	1
17 #endif
18 
19 #if defined(CONFIG_POWER_SPI)
pmic_spi_prepare_tx(u32 reg,u32 * val,u32 write)20 static u32 pmic_spi_prepare_tx(u32 reg, u32 *val, u32 write)
21 {
22 	return (write << 31) | (reg << 25) | (*val & 0x00FFFFFF);
23 }
24 #endif
25 
pmic_init(unsigned char bus)26 int pmic_init(unsigned char bus)
27 {
28 	static const char name[] = "FSL_PMIC";
29 	struct pmic *p = pmic_alloc();
30 
31 	if (!p) {
32 		printf("%s: POWER allocation error!\n", __func__);
33 		return -ENOMEM;
34 	}
35 
36 	p->name = name;
37 	p->number_of_regs = PMIC_NUM_OF_REGS;
38 	p->bus = bus;
39 
40 #if defined(CONFIG_POWER_SPI)
41 	p->interface = PMIC_SPI;
42 	p->hw.spi.cs = CONFIG_FSL_PMIC_CS;
43 	p->hw.spi.clk = CONFIG_FSL_PMIC_CLK;
44 	p->hw.spi.mode = CONFIG_FSL_PMIC_MODE;
45 	p->hw.spi.bitlen = CONFIG_FSL_PMIC_BITLEN;
46 	p->hw.spi.flags = SPI_XFER_BEGIN | SPI_XFER_END;
47 	p->hw.spi.prepare_tx = pmic_spi_prepare_tx;
48 #elif defined(CONFIG_POWER_I2C)
49 	p->interface = PMIC_I2C;
50 	p->hw.i2c.addr = CONFIG_SYS_FSL_PMIC_I2C_ADDR;
51 	p->hw.i2c.tx_num = FSL_PMIC_I2C_LENGTH;
52 #else
53 #error "You must select CONFIG_POWER_SPI or CONFIG_POWER_I2C"
54 #endif
55 
56 	return 0;
57 }
58