blob: a871fc4198722c0b60cc02a07934f03108b7e908 [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 }
36#else /* Non DM I2C support - will be removed */
Aaron Durbin0f0900b2014-05-20 06:01:38 -060037 I2C_SET_BUS(p->bus);
Biwen Lif0018f52020-02-05 22:02:17 +080038#endif
Aaron Durbin0f0900b2014-05-20 06:01:38 -060039
Łukasz Majewski16db0622011-10-06 02:37:34 +000040 switch (pmic_i2c_tx_num) {
41 case 3:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +000042 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 }
Łukasz Majewski16db0622011-10-06 02:37:34 +000051 break;
Łukasz Majewski8a691872012-11-13 03:21:52 +000052 case 2:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +000053 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 }
Łukasz Majewski8a691872012-11-13 03:21:52 +000060 break;
Łukasz Majewski16db0622011-10-06 02:37:34 +000061 case 1:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +000062 buf[0] = cpu_to_le32(val) & 0xff;
Łukasz Majewski16db0622011-10-06 02:37:34 +000063 break;
Fabio Estevamd38fb7e2012-03-16 11:32:09 +000064 default:
65 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
Jaehoon Chung10e80f32016-12-15 20:49:50 +090066 return -EINVAL;
Łukasz Majewski16db0622011-10-06 02:37:34 +000067 }
68
Peng Fan0335e522020-12-26 19:35:42 +080069#if CONFIG_IS_ENABLED(DM_I2C)
Biwen Lif0018f52020-02-05 22:02:17 +080070 return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
71#else
Jaehoon Chung10e80f32016-12-15 20:49:50 +090072 return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
Biwen Lif0018f52020-02-05 22:02:17 +080073#endif
Łukasz Majewski16db0622011-10-06 02:37:34 +000074}
75
76int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
77{
78 unsigned char buf[4] = { 0 };
79 u32 ret_val = 0;
Jaehoon Chung10e80f32016-12-15 20:49:50 +090080 int ret;
Łukasz Majewski16db0622011-10-06 02:37:34 +000081
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000082 if (check_reg(p, reg))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090083 return -EINVAL;
Łukasz Majewski16db0622011-10-06 02:37:34 +000084
Peng Fan0335e522020-12-26 19:35:42 +080085#if CONFIG_IS_ENABLED(DM_I2C)
Biwen Lif0018f52020-02-05 22:02:17 +080086 struct udevice *dev;
Aaron Durbin0f0900b2014-05-20 06:01:38 -060087
Biwen Lif0018f52020-02-05 22:02:17 +080088 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);
Jaehoon Chung10e80f32016-12-15 20:49:50 +090098 ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
Biwen Lif0018f52020-02-05 22:02:17 +080099#endif
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900100 if (ret)
101 return ret;
Łukasz Majewski16db0622011-10-06 02:37:34 +0000102
103 switch (pmic_i2c_tx_num) {
104 case 3:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +0000105 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]);
Łukasz Majewski16db0622011-10-06 02:37:34 +0000111 break;
Łukasz Majewski8a691872012-11-13 03:21:52 +0000112 case 2:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +0000113 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]);
Łukasz Majewski8a691872012-11-13 03:21:52 +0000117 break;
Łukasz Majewski16db0622011-10-06 02:37:34 +0000118 case 1:
Łukasz Majewski40f5ca02012-11-13 03:21:53 +0000119 ret_val = le32_to_cpu(buf[0]);
Łukasz Majewski16db0622011-10-06 02:37:34 +0000120 break;
Fabio Estevamd38fb7e2012-03-16 11:32:09 +0000121 default:
122 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900123 return -EINVAL;
Łukasz Majewski16db0622011-10-06 02:37:34 +0000124 }
125 memcpy(val, &ret_val, sizeof(ret_val));
126
127 return 0;
128}
129
130int pmic_probe(struct pmic *p)
131{
Łukasz Majewski8a691872012-11-13 03:21:52 +0000132 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
Peng Fan0335e522020-12-26 19:35:42 +0800133#if CONFIG_IS_ENABLED(DM_I2C)
Biwen Lif0018f52020-02-05 22:02:17 +0800134 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);
Łukasz Majewski16db0622011-10-06 02:37:34 +0000146 if (i2c_probe(pmic_i2c_addr)) {
147 printf("Can't find PMIC:%s\n", p->name);
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900148 return -ENODEV;
Łukasz Majewski16db0622011-10-06 02:37:34 +0000149 }
Biwen Lif0018f52020-02-05 22:02:17 +0800150#endif
Łukasz Majewski16db0622011-10-06 02:37:34 +0000151
152 return 0;
153}