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
15 #include <linux/types.h>
16 #include <power/pmic.h>
18 #include <linux/compiler.h>
20 int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
22 unsigned char buf[4] = { 0 };
24 if (check_reg(p, reg))
26 #if CONFIG_IS_ENABLED(DM_I2C)
30 ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
33 printf("%s: Cannot find udev for a bus %d\n", __func__,
37 #else /* Non DM I2C support - will be removed */
41 switch (pmic_i2c_tx_num) {
43 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
44 buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
45 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
46 buf[0] = cpu_to_le32(val) & 0xff;
48 buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
49 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
50 buf[2] = cpu_to_le32(val) & 0xff;
54 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
55 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
56 buf[0] = cpu_to_le32(val) & 0xff;
58 buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
59 buf[1] = cpu_to_le32(val) & 0xff;
63 buf[0] = cpu_to_le32(val) & 0xff;
66 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
70 #if CONFIG_IS_ENABLED(DM_I2C)
71 return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
73 return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
77 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
79 unsigned char buf[4] = { 0 };
83 if (check_reg(p, reg))
86 #if CONFIG_IS_ENABLED(DM_I2C)
89 ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
92 printf("%s: Cannot find udev for a bus %d\n", __func__,
96 ret = dm_i2c_read(dev, reg, buf, pmic_i2c_tx_num);
97 #else /* Non DM I2C support - will be removed */
99 ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
104 switch (pmic_i2c_tx_num) {
106 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
107 ret_val = le32_to_cpu(buf[2] << 16
108 | buf[1] << 8 | buf[0]);
110 ret_val = le32_to_cpu(buf[0] << 16 |
111 buf[1] << 8 | buf[2]);
114 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
115 ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
117 ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
120 ret_val = le32_to_cpu(buf[0]);
123 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
126 memcpy(val, &ret_val, sizeof(ret_val));
131 int pmic_probe(struct pmic *p)
133 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
134 #if CONFIG_IS_ENABLED(DM_I2C)
138 ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
141 printf("%s: Cannot find udev for a bus %d\n", __func__,
145 #else /* Non DM I2C support - will be removed */
146 i2c_set_bus_num(p->bus);
147 if (i2c_probe(pmic_i2c_addr)) {
148 printf("Can't find PMIC:%s\n", p->name);