blob: e74c3727cdc1b93c6b7902d5e1ca648a816188c1 [file] [log] [blame]
Łukasz Majewski16db0622011-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 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29#include <common.h>
30#include <linux/types.h>
31#include <pmic.h>
32#include <i2c.h>
33
34int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
35{
36 unsigned char buf[4] = { 0 };
37
38 if (check_reg(reg))
39 return -1;
40
41 switch (pmic_i2c_tx_num) {
42 case 3:
43 buf[0] = (val >> 16) & 0xff;
44 buf[1] = (val >> 8) & 0xff;
45 buf[2] = val & 0xff;
46 break;
Łukasz Majewski8a691872012-11-13 03:21:52 +000047 case 2:
48 buf[0] = (val >> 8) & 0xff;
49 buf[1] = val & 0xff;
50 break;
Łukasz Majewski16db0622011-10-06 02:37:34 +000051 case 1:
52 buf[0] = val & 0xff;
53 break;
Fabio Estevamd38fb7e2012-03-16 11:32:09 +000054 default:
55 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
56 return -1;
Łukasz Majewski16db0622011-10-06 02:37:34 +000057 }
58
59 if (i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
60 return -1;
61
62 return 0;
63}
64
65int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
66{
67 unsigned char buf[4] = { 0 };
68 u32 ret_val = 0;
69
70 if (check_reg(reg))
71 return -1;
72
73 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:
78 ret_val = buf[0] << 16 | buf[1] << 8 | buf[2];
79 break;
Łukasz Majewski8a691872012-11-13 03:21:52 +000080 case 2:
81 ret_val = buf[0] << 8 | buf[1];
82 break;
Łukasz Majewski16db0622011-10-06 02:37:34 +000083 case 1:
84 ret_val = buf[0];
85 break;
Fabio Estevamd38fb7e2012-03-16 11:32:09 +000086 default:
87 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
88 return -1;
Łukasz Majewski16db0622011-10-06 02:37:34 +000089 }
90 memcpy(val, &ret_val, sizeof(ret_val));
91
92 return 0;
93}
94
95int pmic_probe(struct pmic *p)
96{
Stefano Babicca4f5fb2011-10-11 19:18:05 +020097 I2C_SET_BUS(p->bus);
Łukasz Majewski8a691872012-11-13 03:21:52 +000098 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
Łukasz Majewski16db0622011-10-06 02:37:34 +000099 if (i2c_probe(pmic_i2c_addr)) {
100 printf("Can't find PMIC:%s\n", p->name);
101 return -1;
102 }
103
104 return 0;
105}