blob: c2fc1c6b42f31ef1db6d51c5663fb4af27b36d6b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Łukasz Majewski16db0622011-10-06 02:37:34 +00002/*
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.
Biwen Lif0018f52020-02-05 22:02:17 +080010 * (C) Copyright 2019 NXP
Łukasz Majewski16db0622011-10-06 02:37:34 +000011 */
12
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Łukasz Majewski16db0622011-10-06 02:37:34 +000014#include <linux/types.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000015#include <power/pmic.h>
Łukasz Majewski16db0622011-10-06 02:37:34 +000016#include <i2c.h>
Masahiro Yamadae7228522014-11-26 16:00:58 +090017#include <linux/compiler.h>
Łukasz Majewski16db0622011-10-06 02:37:34 +000018
19int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
20{
21 unsigned char buf[4] = { 0 };
22
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000023 if (check_reg(p, reg))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090024 return -EINVAL;
Peng Fan0335e522020-12-26 19:35:42 +080025#if CONFIG_IS_ENABLED(DM_I2C)
Biwen Lif0018f52020-02-05 22:02:17 +080026 struct udevice *dev;
27 int ret;
Łukasz Majewski16db0622011-10-06 02:37:34 +000028
Biwen Lif0018f52020-02-05 22:02:17 +080029 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 }
Biwen Lif0018f52020-02-05 22:02:17 +080036#endif
Aaron Durbin0f0900b2014-05-20 06:01:38 -060037
Łukasz Majewski16db0622011-10-06 02:37:34 +000038 switch (pmic_i2c_tx_num) {
39 case 3:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +000040 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 }
Łukasz Majewski16db0622011-10-06 02:37:34 +000049 break;
Łukasz Majewski8a691872012-11-13 03:21:52 +000050 case 2:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +000051 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 }
Łukasz Majewski8a691872012-11-13 03:21:52 +000058 break;
Łukasz Majewski16db0622011-10-06 02:37:34 +000059 case 1:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +000060 buf[0] = cpu_to_le32(val) & 0xff;
Łukasz Majewski16db0622011-10-06 02:37:34 +000061 break;
Fabio Estevamd38fb7e2012-03-16 11:32:09 +000062 default:
63 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
Jaehoon Chung10e80f32016-12-15 20:49:50 +090064 return -EINVAL;
Łukasz Majewski16db0622011-10-06 02:37:34 +000065 }
66
Peng Fan0335e522020-12-26 19:35:42 +080067#if CONFIG_IS_ENABLED(DM_I2C)
Biwen Lif0018f52020-02-05 22:02:17 +080068 return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
69#else
Jaehoon Chung10e80f32016-12-15 20:49:50 +090070 return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
Biwen Lif0018f52020-02-05 22:02:17 +080071#endif
Łukasz Majewski16db0622011-10-06 02:37:34 +000072}
73
74int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
75{
76 unsigned char buf[4] = { 0 };
77 u32 ret_val = 0;
Jaehoon Chung10e80f32016-12-15 20:49:50 +090078 int ret;
Łukasz Majewski16db0622011-10-06 02:37:34 +000079
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000080 if (check_reg(p, reg))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090081 return -EINVAL;
Łukasz Majewski16db0622011-10-06 02:37:34 +000082
Peng Fan0335e522020-12-26 19:35:42 +080083#if CONFIG_IS_ENABLED(DM_I2C)
Biwen Lif0018f52020-02-05 22:02:17 +080084 struct udevice *dev;
Aaron Durbin0f0900b2014-05-20 06:01:38 -060085
Biwen Lif0018f52020-02-05 22:02:17 +080086 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);
Biwen Lif0018f52020-02-05 22:02:17 +080094#endif
Jaehoon Chung10e80f32016-12-15 20:49:50 +090095 if (ret)
96 return ret;
Łukasz Majewski16db0622011-10-06 02:37:34 +000097
98 switch (pmic_i2c_tx_num) {
99 case 3:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +0000100 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]);
Łukasz Majewski16db0622011-10-06 02:37:34 +0000106 break;
Łukasz Majewski8a691872012-11-13 03:21:52 +0000107 case 2:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +0000108 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]);
Łukasz Majewski8a691872012-11-13 03:21:52 +0000112 break;
Łukasz Majewski16db0622011-10-06 02:37:34 +0000113 case 1:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +0000114 ret_val = le32_to_cpu(buf[0]);
Łukasz Majewski16db0622011-10-06 02:37:34 +0000115 break;
Fabio Estevamd38fb7e2012-03-16 11:32:09 +0000116 default:
117 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900118 return -EINVAL;
Łukasz Majewski16db0622011-10-06 02:37:34 +0000119 }
120 memcpy(val, &ret_val, sizeof(ret_val));
121
122 return 0;
123}
124
125int pmic_probe(struct pmic *p)
126{
Łukasz Majewski8a691872012-11-13 03:21:52 +0000127 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
Peng Fan0335e522020-12-26 19:35:42 +0800128#if CONFIG_IS_ENABLED(DM_I2C)
Biwen Lif0018f52020-02-05 22:02:17 +0800129 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);
Łukasz Majewski16db0622011-10-06 02:37:34 +0000141 if (i2c_probe(pmic_i2c_addr)) {
142 printf("Can't find PMIC:%s\n", p->name);
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900143 return -ENODEV;
Łukasz Majewski16db0622011-10-06 02:37:34 +0000144 }
Biwen Lif0018f52020-02-05 22:02:17 +0800145#endif
Łukasz Majewski16db0622011-10-06 02:37:34 +0000146
147 return 0;
148}