1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
7 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
9 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
13 #include <linux/types.h>
14 #include <power/pmic.h>
16 #include <linux/compiler.h>
18 int pmic_reg_write(struct pmic
*p
, u32 reg
, u32 val
)
20 unsigned char buf
[4] = { 0 };
22 if (check_reg(p
, reg
))
27 switch (pmic_i2c_tx_num
) {
29 if (p
->sensor_byte_order
== PMIC_SENSOR_BYTE_ORDER_BIG
) {
30 buf
[2] = (cpu_to_le32(val
) >> 16) & 0xff;
31 buf
[1] = (cpu_to_le32(val
) >> 8) & 0xff;
32 buf
[0] = cpu_to_le32(val
) & 0xff;
34 buf
[0] = (cpu_to_le32(val
) >> 16) & 0xff;
35 buf
[1] = (cpu_to_le32(val
) >> 8) & 0xff;
36 buf
[2] = cpu_to_le32(val
) & 0xff;
40 if (p
->sensor_byte_order
== PMIC_SENSOR_BYTE_ORDER_BIG
) {
41 buf
[1] = (cpu_to_le32(val
) >> 8) & 0xff;
42 buf
[0] = cpu_to_le32(val
) & 0xff;
44 buf
[0] = (cpu_to_le32(val
) >> 8) & 0xff;
45 buf
[1] = cpu_to_le32(val
) & 0xff;
49 buf
[0] = cpu_to_le32(val
) & 0xff;
52 printf("%s: invalid tx_num: %d", __func__
, pmic_i2c_tx_num
);
56 return i2c_write(pmic_i2c_addr
, reg
, 1, buf
, pmic_i2c_tx_num
);
59 int pmic_reg_read(struct pmic
*p
, u32 reg
, u32
*val
)
61 unsigned char buf
[4] = { 0 };
65 if (check_reg(p
, reg
))
70 ret
= i2c_read(pmic_i2c_addr
, reg
, 1, buf
, pmic_i2c_tx_num
);
74 switch (pmic_i2c_tx_num
) {
76 if (p
->sensor_byte_order
== PMIC_SENSOR_BYTE_ORDER_BIG
)
77 ret_val
= le32_to_cpu(buf
[2] << 16
78 | buf
[1] << 8 | buf
[0]);
80 ret_val
= le32_to_cpu(buf
[0] << 16 |
81 buf
[1] << 8 | buf
[2]);
84 if (p
->sensor_byte_order
== PMIC_SENSOR_BYTE_ORDER_BIG
)
85 ret_val
= le32_to_cpu(buf
[1] << 8 | buf
[0]);
87 ret_val
= le32_to_cpu(buf
[0] << 8 | buf
[1]);
90 ret_val
= le32_to_cpu(buf
[0]);
93 printf("%s: invalid tx_num: %d", __func__
, pmic_i2c_tx_num
);
96 memcpy(val
, &ret_val
, sizeof(ret_val
));
101 int pmic_probe(struct pmic
*p
)
103 i2c_set_bus_num(p
->bus
);
104 debug("Bus: %d PMIC:%s probed!\n", p
->bus
, p
->name
);
105 if (i2c_probe(pmic_i2c_addr
)) {
106 printf("Can't find PMIC:%s\n", p
->name
);