]> Git Repo - J-u-boot.git/blob - drivers/power/power_i2c.c
net: mediatek: fix gmac2 usability for mt7629
[J-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 <[email protected]>
5  *
6  * (C) Copyright 2010
7  * Stefano Babic, DENX Software Engineering, [email protected]
8  *
9  * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
10  * (C) Copyright 2019 NXP
11  */
12
13 #include <log.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 CONFIG_IS_ENABLED(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 #endif
37
38         switch (pmic_i2c_tx_num) {
39         case 3:
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;
44                 } else {
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;
48                 }
49                 break;
50         case 2:
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;
54                 } else {
55                         buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
56                         buf[1] = cpu_to_le32(val) & 0xff;
57                 }
58                 break;
59         case 1:
60                 buf[0] = cpu_to_le32(val) & 0xff;
61                 break;
62         default:
63                 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
64                 return -EINVAL;
65         }
66
67 #if CONFIG_IS_ENABLED(DM_I2C)
68         return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
69 #else
70         return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
71 #endif
72 }
73
74 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
75 {
76         unsigned char buf[4] = { 0 };
77         u32 ret_val = 0;
78         int ret;
79
80         if (check_reg(p, reg))
81                 return -EINVAL;
82
83 #if CONFIG_IS_ENABLED(DM_I2C)
84         struct udevice *dev;
85
86         ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
87                                       1, &dev);
88         if (ret) {
89                 printf("%s: Cannot find udev for a bus %d\n", __func__,
90                        p->bus);
91                 return -ENXIO;
92         }
93         ret = dm_i2c_read(dev, reg, buf, pmic_i2c_tx_num);
94 #endif
95         if (ret)
96                 return ret;
97
98         switch (pmic_i2c_tx_num) {
99         case 3:
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]);
103                 else
104                         ret_val = le32_to_cpu(buf[0] << 16 |
105                                               buf[1] << 8 | buf[2]);
106                 break;
107         case 2:
108                 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
109                         ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
110                 else
111                         ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
112                 break;
113         case 1:
114                 ret_val = le32_to_cpu(buf[0]);
115                 break;
116         default:
117                 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
118                 return -EINVAL;
119         }
120         memcpy(val, &ret_val, sizeof(ret_val));
121
122         return 0;
123 }
124
125 int pmic_probe(struct pmic *p)
126 {
127         debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
128 #if CONFIG_IS_ENABLED(DM_I2C)
129         struct udevice *dev;
130         int ret;
131
132         ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
133                                       1, &dev);
134         if (ret) {
135                 printf("%s: Cannot find udev for a bus %d\n", __func__,
136                        p->bus);
137                 return -ENXIO;
138         }
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);
143                 return -ENODEV;
144         }
145 #endif
146
147         return 0;
148 }
This page took 0.030985 seconds and 4 git commands to generate.