blob: 0dcf9fe9187307c4240153c6bada20a808ebdf6c [file] [log] [blame]
Łukasz Majewskie542b7f2011-10-06 02:37:34 +00001/*
2 * Copyright (C) 2011 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
4 *
5 * (C) Copyright 2010
6 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
7 *
8 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
9 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Ł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))
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000024 return -1;
25
Aaron Durbin0b259dc2014-05-20 06:01:38 -060026 I2C_SET_BUS(p->bus);
27
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000028 switch (pmic_i2c_tx_num) {
29 case 3:
Łukasz Majewski86879d72012-11-13 03:21:53 +000030 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
31 buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
32 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
33 buf[0] = cpu_to_le32(val) & 0xff;
34 } else {
35 buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
36 buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
37 buf[2] = cpu_to_le32(val) & 0xff;
38 }
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000039 break;
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000040 case 2:
Łukasz Majewski86879d72012-11-13 03:21:53 +000041 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
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) >> 8) & 0xff;
46 buf[1] = cpu_to_le32(val) & 0xff;
47 }
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000048 break;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000049 case 1:
Łukasz Majewski86879d72012-11-13 03:21:53 +000050 buf[0] = cpu_to_le32(val) & 0xff;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000051 break;
Fabio Estevama22429d2012-03-16 11:32:09 +000052 default:
53 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
54 return -1;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000055 }
56
57 if (i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
58 return -1;
59
60 return 0;
61}
62
63int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
64{
65 unsigned char buf[4] = { 0 };
66 u32 ret_val = 0;
67
Łukasz Majewskic7336812012-11-13 03:21:55 +000068 if (check_reg(p, reg))
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000069 return -1;
70
Aaron Durbin0b259dc2014-05-20 06:01:38 -060071 I2C_SET_BUS(p->bus);
72
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000073 if (i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
74 return -1;
75
76 switch (pmic_i2c_tx_num) {
77 case 3:
Łukasz Majewski86879d72012-11-13 03:21:53 +000078 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
79 ret_val = le32_to_cpu(buf[2] << 16
80 | buf[1] << 8 | buf[0]);
81 else
82 ret_val = le32_to_cpu(buf[0] << 16 |
83 buf[1] << 8 | buf[2]);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000084 break;
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000085 case 2:
Łukasz Majewski86879d72012-11-13 03:21:53 +000086 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
87 ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
88 else
89 ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +000090 break;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000091 case 1:
Łukasz Majewski86879d72012-11-13 03:21:53 +000092 ret_val = le32_to_cpu(buf[0]);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000093 break;
Fabio Estevama22429d2012-03-16 11:32:09 +000094 default:
95 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
96 return -1;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000097 }
98 memcpy(val, &ret_val, sizeof(ret_val));
99
100 return 0;
101}
102
103int pmic_probe(struct pmic *p)
104{
Łukasz Majewski3fbb5172013-08-16 15:35:26 +0200105 i2c_set_bus_num(p->bus);
Łukasz Majewskib5bf9ca2012-11-13 03:21:52 +0000106 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000107 if (i2c_probe(pmic_i2c_addr)) {
108 printf("Can't find PMIC:%s\n", p->name);
109 return -1;
110 }
111
112 return 0;
113}