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.
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__,
36 #else /* Non DM I2C support - will be removed */
40 switch (pmic_i2c_tx_num) {
42 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
43 buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
44 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
45 buf[0] = cpu_to_le32(val) & 0xff;
47 buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
48 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
49 buf[2] = cpu_to_le32(val) & 0xff;
53 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
54 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
55 buf[0] = cpu_to_le32(val) & 0xff;
57 buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
58 buf[1] = cpu_to_le32(val) & 0xff;
62 buf[0] = cpu_to_le32(val) & 0xff;
65 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
69 #if CONFIG_IS_ENABLED(DM_I2C)
70 return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
72 return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
76 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
78 unsigned char buf[4] = { 0 };
82 if (check_reg(p, reg))
85 #if CONFIG_IS_ENABLED(DM_I2C)
88 ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
91 printf("%s: Cannot find udev for a bus %d\n", __func__,
95 ret = dm_i2c_read(dev, reg, buf, pmic_i2c_tx_num);
96 #else /* Non DM I2C support - will be removed */
98 ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
103 switch (pmic_i2c_tx_num) {
105 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
106 ret_val = le32_to_cpu(buf[2] << 16
107 | buf[1] << 8 | buf[0]);
109 ret_val = le32_to_cpu(buf[0] << 16 |
110 buf[1] << 8 | buf[2]);
113 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
114 ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
116 ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
119 ret_val = le32_to_cpu(buf[0]);
122 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
125 memcpy(val, &ret_val, sizeof(ret_val));
130 int pmic_probe(struct pmic *p)
132 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
133 #if CONFIG_IS_ENABLED(DM_I2C)
137 ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
140 printf("%s: Cannot find udev for a bus %d\n", __func__,
144 #else /* Non DM I2C support - will be removed */
145 i2c_set_bus_num(p->bus);
146 if (i2c_probe(pmic_i2c_addr)) {
147 printf("Can't find PMIC:%s\n", p->name);