blob: bcddff2d7a05aa0653d192749abdf480f512a8a1 [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.
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000010 */
11
12#include <common.h>
13#include <linux/types.h>
Łukasz Majewskic7336812012-11-13 03:21:55 +000014#include <power/pmic.h>
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000015#include <i2c.h>
Masahiro Yamadaafc366f2014-11-26 16:00:58 +090016#include <linux/compiler.h>
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000017
18int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
19{
20 unsigned char buf[4] = { 0 };
21
Łukasz Majewskic7336812012-11-13 03:21:55 +000022 if (check_reg(p, reg))
Jaehoon Chung505cf472016-12-15 20:49:50 +090023 return -EINVAL;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000024
Aaron Durbin0b259dc2014-05-20 06:01:38 -060025 I2C_SET_BUS(p->bus);
26
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000027 switch (pmic_i2c_tx_num) {
28 case 3:
Łukasz Majewski86879d72012-11-13 03:21:53 +000029 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
30 buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
31 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
32 buf[0] = cpu_to_le32(val) & 0xff;
33 } else {
34 buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
35 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
36 buf[2] = cpu_to_le32(val) & 0xff;
37 }
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000038 break;
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000039 case 2:
Łukasz Majewski86879d72012-11-13 03:21:53 +000040 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
41 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
42 buf[0] = cpu_to_le32(val) & 0xff;
43 } else {
44 buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
45 buf[1] = cpu_to_le32(val) & 0xff;
46 }
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000047 break;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000048 case 1:
Łukasz Majewski86879d72012-11-13 03:21:53 +000049 buf[0] = cpu_to_le32(val) & 0xff;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000050 break;
Fabio Estevama22429d2012-03-16 11:32:09 +000051 default:
52 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
Jaehoon Chung505cf472016-12-15 20:49:50 +090053 return -EINVAL;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000054 }
55
Jaehoon Chung505cf472016-12-15 20:49:50 +090056 return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000057}
58
59int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
60{
61 unsigned char buf[4] = { 0 };
62 u32 ret_val = 0;
Jaehoon Chung505cf472016-12-15 20:49:50 +090063 int ret;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000064
Łukasz Majewskic7336812012-11-13 03:21:55 +000065 if (check_reg(p, reg))
Jaehoon Chung505cf472016-12-15 20:49:50 +090066 return -EINVAL;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000067
Aaron Durbin0b259dc2014-05-20 06:01:38 -060068 I2C_SET_BUS(p->bus);
69
Jaehoon Chung505cf472016-12-15 20:49:50 +090070 ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
71 if (ret)
72 return ret;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000073
74 switch (pmic_i2c_tx_num) {
75 case 3:
Łukasz Majewski86879d72012-11-13 03:21:53 +000076 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
77 ret_val = le32_to_cpu(buf[2] << 16
78 | buf[1] << 8 | buf[0]);
79 else
80 ret_val = le32_to_cpu(buf[0] << 16 |
81 buf[1] << 8 | buf[2]);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000082 break;
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000083 case 2:
Łukasz Majewski86879d72012-11-13 03:21:53 +000084 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
85 ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
86 else
87 ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000088 break;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000089 case 1:
Łukasz Majewski86879d72012-11-13 03:21:53 +000090 ret_val = le32_to_cpu(buf[0]);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000091 break;
Fabio Estevama22429d2012-03-16 11:32:09 +000092 default:
93 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
Jaehoon Chung505cf472016-12-15 20:49:50 +090094 return -EINVAL;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000095 }
96 memcpy(val, &ret_val, sizeof(ret_val));
97
98 return 0;
99}
100
101int pmic_probe(struct pmic *p)
102{
Łukasz Majewski3fbb5172013-08-16 15:35:26 +0200103 i2c_set_bus_num(p->bus);
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +0000104 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000105 if (i2c_probe(pmic_i2c_addr)) {
106 printf("Can't find PMIC:%s\n", p->name);
Jaehoon Chung505cf472016-12-15 20:49:50 +0900107 return -ENODEV;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000108 }
109
110 return 0;
111}