1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Samsung Electronics
9 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
10 * (C) Copyright 2019 NXP
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))
25 #if CONFIG_IS_ENABLED(DM_I2C)
29 ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
32 printf("%s: Cannot find udev for a bus %d\n", __func__,
38 switch (pmic_i2c_tx_num) {
40 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
41 buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
42 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
43 buf[0] = cpu_to_le32(val) & 0xff;
45 buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
46 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
47 buf[2] = cpu_to_le32(val) & 0xff;
51 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
52 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
53 buf[0] = cpu_to_le32(val) & 0xff;
55 buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
56 buf[1] = cpu_to_le32(val) & 0xff;
60 buf[0] = cpu_to_le32(val) & 0xff;
63 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
67 #if CONFIG_IS_ENABLED(DM_I2C)
68 return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
70 return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
74 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
76 unsigned char buf[4] = { 0 };
80 if (check_reg(p, reg))
83 #if CONFIG_IS_ENABLED(DM_I2C)
86 ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
89 printf("%s: Cannot find udev for a bus %d\n", __func__,
93 ret = dm_i2c_read(dev, reg, buf, pmic_i2c_tx_num);
98 switch (pmic_i2c_tx_num) {
100 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
101 ret_val = le32_to_cpu(buf[2] << 16
102 | buf[1] << 8 | buf[0]);
104 ret_val = le32_to_cpu(buf[0] << 16 |
105 buf[1] << 8 | buf[2]);
108 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
109 ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
111 ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
114 ret_val = le32_to_cpu(buf[0]);
117 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
120 memcpy(val, &ret_val, sizeof(ret_val));
125 int pmic_probe(struct pmic *p)
127 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
128 #if CONFIG_IS_ENABLED(DM_I2C)
132 ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
135 printf("%s: Cannot find udev for a bus %d\n", __func__,
139 #else /* Non DM I2C support - will be removed */
140 i2c_set_bus_num(p->bus);
141 if (i2c_probe(pmic_i2c_addr)) {
142 printf("Can't find PMIC:%s\n", p->name);