treewide: mem: Enable MEMTEST via defconfig
[oweals/u-boot.git] / drivers / power / power_i2c.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2011 Samsung Electronics
4  * Lukasz Majewski <l.majewski@samsung.com>
5  *
6  * (C) Copyright 2010
7  * Stefano Babic, DENX Software Engineering, sbabic@denx.de
8  *
9  * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
10  * (C) Copyright 2019 NXP
11  */
12
13 #include <common.h>
14 #include <linux/types.h>
15 #include <power/pmic.h>
16 #include <i2c.h>
17 #include <linux/compiler.h>
18
19 int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
20 {
21         unsigned char buf[4] = { 0 };
22
23         if (check_reg(p, reg))
24                 return -EINVAL;
25 #if defined(CONFIG_DM_I2C)
26         struct udevice *dev;
27         int ret;
28
29         ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
30                                       1, &dev);
31         if (ret) {
32                 printf("%s: Cannot find udev for a bus %d\n", __func__,
33                        p->bus);
34                 return -ENXIO;
35         }
36 #else /* Non DM I2C support - will be removed */
37         I2C_SET_BUS(p->bus);
38 #endif
39
40         switch (pmic_i2c_tx_num) {
41         case 3:
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;
46                 } else {
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;
50                 }
51                 break;
52         case 2:
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;
56                 } else {
57                         buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
58                         buf[1] = cpu_to_le32(val) & 0xff;
59                 }
60                 break;
61         case 1:
62                 buf[0] = cpu_to_le32(val) & 0xff;
63                 break;
64         default:
65                 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
66                 return -EINVAL;
67         }
68
69 #if defined(CONFIG_DM_I2C)
70         return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
71 #else
72         return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
73 #endif
74 }
75
76 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
77 {
78         unsigned char buf[4] = { 0 };
79         u32 ret_val = 0;
80         int ret;
81
82         if (check_reg(p, reg))
83                 return -EINVAL;
84
85 #if defined(CONFIG_DM_I2C)
86         struct udevice *dev;
87
88         ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
89                                       1, &dev);
90         if (ret) {
91                 printf("%s: Cannot find udev for a bus %d\n", __func__,
92                        p->bus);
93                 return -ENXIO;
94         }
95         ret = dm_i2c_read(dev, reg, buf, pmic_i2c_tx_num);
96 #else /* Non DM I2C support - will be removed */
97         I2C_SET_BUS(p->bus);
98         ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
99 #endif
100         if (ret)
101                 return ret;
102
103         switch (pmic_i2c_tx_num) {
104         case 3:
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]);
108                 else
109                         ret_val = le32_to_cpu(buf[0] << 16 |
110                                               buf[1] << 8 | buf[2]);
111                 break;
112         case 2:
113                 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
114                         ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
115                 else
116                         ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
117                 break;
118         case 1:
119                 ret_val = le32_to_cpu(buf[0]);
120                 break;
121         default:
122                 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
123                 return -EINVAL;
124         }
125         memcpy(val, &ret_val, sizeof(ret_val));
126
127         return 0;
128 }
129
130 int pmic_probe(struct pmic *p)
131 {
132         debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
133 #if defined(CONFIG_DM_I2C)
134         struct udevice *dev;
135         int ret;
136
137         ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
138                                       1, &dev);
139         if (ret) {
140                 printf("%s: Cannot find udev for a bus %d\n", __func__,
141                        p->bus);
142                 return -ENXIO;
143         }
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);
148                 return -ENODEV;
149         }
150 #endif
151
152         return 0;
153 }