blob: b30f223a5c208e467382a2d2c695870f65c613a1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Łukasz Majewskie542b7f2011-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 Libb1165f2020-02-05 22:02:17 +080010 * (C) Copyright 2019 NXP
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000011 */
12
13#include <common.h>
14#include <linux/types.h>
Łukasz Majewskic7336812012-11-13 03:21:55 +000015#include <power/pmic.h>
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000016#include <i2c.h>
Masahiro Yamadaafc366f2014-11-26 16:00:58 +090017#include <linux/compiler.h>
Łukasz Majewskie542b7f2011-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 Majewskic7336812012-11-13 03:21:55 +000023 if (check_reg(p, reg))
Jaehoon Chung505cf472016-12-15 20:49:50 +090024 return -EINVAL;
Biwen Libb1165f2020-02-05 22:02:17 +080025#if defined(CONFIG_DM_I2C)
26 struct udevice *dev;
27 int ret;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000028
Biwen Libb1165f2020-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 Durbin0b259dc2014-05-20 06:01:38 -060037 I2C_SET_BUS(p->bus);
Biwen Libb1165f2020-02-05 22:02:17 +080038#endif
Aaron Durbin0b259dc2014-05-20 06:01:38 -060039
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000040 switch (pmic_i2c_tx_num) {
41 case 3:
Łukasz Majewski86879d72012-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 Majewskie542b7f2011-10-06 02:37:34 +000051 break;
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000052 case 2:
Łukasz Majewski86879d72012-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 Majewskib5bf9ca2012-11-13 03:21:52 +000060 break;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000061 case 1:
Łukasz Majewski86879d72012-11-13 03:21:53 +000062 buf[0] = cpu_to_le32(val) & 0xff;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000063 break;
Fabio Estevama22429d2012-03-16 11:32:09 +000064 default:
65 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
Jaehoon Chung505cf472016-12-15 20:49:50 +090066 return -EINVAL;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000067 }
68
Biwen Libb1165f2020-02-05 22:02:17 +080069#if defined(CONFIG_DM_I2C)
70 return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
71#else
Jaehoon Chung505cf472016-12-15 20:49:50 +090072 return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
Biwen Libb1165f2020-02-05 22:02:17 +080073#endif
Łukasz Majewskie542b7f2011-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 Chung505cf472016-12-15 20:49:50 +090080 int ret;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000081
Łukasz Majewskic7336812012-11-13 03:21:55 +000082 if (check_reg(p, reg))
Jaehoon Chung505cf472016-12-15 20:49:50 +090083 return -EINVAL;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000084
Biwen Libb1165f2020-02-05 22:02:17 +080085#if defined(CONFIG_DM_I2C)
86 struct udevice *dev;
Aaron Durbin0b259dc2014-05-20 06:01:38 -060087
Biwen Libb1165f2020-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 Chung505cf472016-12-15 20:49:50 +090098 ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
Biwen Libb1165f2020-02-05 22:02:17 +080099#endif
Jaehoon Chung505cf472016-12-15 20:49:50 +0900100 if (ret)
101 return ret;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000102
103 switch (pmic_i2c_tx_num) {
104 case 3:
Łukasz Majewski86879d72012-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 Majewskie542b7f2011-10-06 02:37:34 +0000111 break;
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +0000112 case 2:
Łukasz Majewski86879d72012-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 Majewskib5bf9ca2012-11-13 03:21:52 +0000117 break;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000118 case 1:
Łukasz Majewski86879d72012-11-13 03:21:53 +0000119 ret_val = le32_to_cpu(buf[0]);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000120 break;
Fabio Estevama22429d2012-03-16 11:32:09 +0000121 default:
122 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
Jaehoon Chung505cf472016-12-15 20:49:50 +0900123 return -EINVAL;
Łukasz Majewskie542b7f2011-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 Majewskib5bf9ca2012-11-13 03:21:52 +0000132 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
Biwen Libb1165f2020-02-05 22:02:17 +0800133#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);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000146 if (i2c_probe(pmic_i2c_addr)) {
147 printf("Can't find PMIC:%s\n", p->name);
Jaehoon Chung505cf472016-12-15 20:49:50 +0900148 return -ENODEV;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000149 }
Biwen Libb1165f2020-02-05 22:02:17 +0800150#endif
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000151
152 return 0;
153}