2 * Copyright (C) 2011 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
6 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
8 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
10 * SPDX-License-Identifier: GPL-2.0+
14 #include <linux/types.h>
15 #include <power/pmic.h>
17 #include <linux/compiler.h>
19 int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
21 unsigned char buf[4] = { 0 };
23 if (check_reg(p, reg))
28 switch (pmic_i2c_tx_num) {
30 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
31 buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
32 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
33 buf[0] = cpu_to_le32(val) & 0xff;
35 buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
36 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
37 buf[2] = cpu_to_le32(val) & 0xff;
41 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
42 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
43 buf[0] = cpu_to_le32(val) & 0xff;
45 buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
46 buf[1] = cpu_to_le32(val) & 0xff;
50 buf[0] = cpu_to_le32(val) & 0xff;
53 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
57 if (i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
63 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
65 unsigned char buf[4] = { 0 };
68 if (check_reg(p, reg))
73 if (i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
76 switch (pmic_i2c_tx_num) {
78 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
79 ret_val = le32_to_cpu(buf[2] << 16
80 | buf[1] << 8 | buf[0]);
82 ret_val = le32_to_cpu(buf[0] << 16 |
83 buf[1] << 8 | buf[2]);
86 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
87 ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
89 ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
92 ret_val = le32_to_cpu(buf[0]);
95 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
98 memcpy(val, &ret_val, sizeof(ret_val));
103 int pmic_probe(struct pmic *p)
105 i2c_set_bus_num(p->bus);
106 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
107 if (i2c_probe(pmic_i2c_addr)) {
108 printf("Can't find PMIC:%s\n", p->name);