Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 1 | /* |
| 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> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 31 | #include <power/pmic.h> |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 32 | #include <i2c.h> |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 33 | #include <compiler.h> |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 34 | |
| 35 | int pmic_reg_write(struct pmic *p, u32 reg, u32 val) |
| 36 | { |
| 37 | unsigned char buf[4] = { 0 }; |
| 38 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 39 | if (check_reg(p, reg)) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 40 | return -1; |
| 41 | |
| 42 | switch (pmic_i2c_tx_num) { |
| 43 | case 3: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 44 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) { |
| 45 | buf[2] = (cpu_to_le32(val) >> 16) & 0xff; |
| 46 | buf[1] = (cpu_to_le32(val) >> 8) & 0xff; |
| 47 | buf[0] = cpu_to_le32(val) & 0xff; |
| 48 | } else { |
| 49 | buf[0] = (cpu_to_le32(val) >> 16) & 0xff; |
| 50 | buf[1] = (cpu_to_le32(val) >> 8) & 0xff; |
| 51 | buf[2] = cpu_to_le32(val) & 0xff; |
| 52 | } |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 53 | break; |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 54 | case 2: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 55 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) { |
| 56 | buf[1] = (cpu_to_le32(val) >> 8) & 0xff; |
| 57 | buf[0] = cpu_to_le32(val) & 0xff; |
| 58 | } else { |
| 59 | buf[0] = (cpu_to_le32(val) >> 8) & 0xff; |
| 60 | buf[1] = cpu_to_le32(val) & 0xff; |
| 61 | } |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 62 | break; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 63 | case 1: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 64 | buf[0] = cpu_to_le32(val) & 0xff; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 65 | break; |
Fabio Estevam | d38fb7e | 2012-03-16 11:32:09 +0000 | [diff] [blame] | 66 | default: |
| 67 | printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); |
| 68 | return -1; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | if (i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num)) |
| 72 | return -1; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int pmic_reg_read(struct pmic *p, u32 reg, u32 *val) |
| 78 | { |
| 79 | unsigned char buf[4] = { 0 }; |
| 80 | u32 ret_val = 0; |
| 81 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 82 | if (check_reg(p, reg)) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 83 | return -1; |
| 84 | |
| 85 | if (i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num)) |
| 86 | return -1; |
| 87 | |
| 88 | switch (pmic_i2c_tx_num) { |
| 89 | case 3: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 90 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) |
| 91 | ret_val = le32_to_cpu(buf[2] << 16 |
| 92 | | buf[1] << 8 | buf[0]); |
| 93 | else |
| 94 | ret_val = le32_to_cpu(buf[0] << 16 | |
| 95 | buf[1] << 8 | buf[2]); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 96 | break; |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 97 | case 2: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 98 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) |
| 99 | ret_val = le32_to_cpu(buf[1] << 8 | buf[0]); |
| 100 | else |
| 101 | ret_val = le32_to_cpu(buf[0] << 8 | buf[1]); |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 102 | break; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 103 | case 1: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 104 | ret_val = le32_to_cpu(buf[0]); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 105 | break; |
Fabio Estevam | d38fb7e | 2012-03-16 11:32:09 +0000 | [diff] [blame] | 106 | default: |
| 107 | printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); |
| 108 | return -1; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 109 | } |
| 110 | memcpy(val, &ret_val, sizeof(ret_val)); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int pmic_probe(struct pmic *p) |
| 116 | { |
Stefano Babic | ca4f5fb | 2011-10-11 19:18:05 +0200 | [diff] [blame] | 117 | I2C_SET_BUS(p->bus); |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 118 | debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 119 | if (i2c_probe(pmic_i2c_addr)) { |
| 120 | printf("Can't find PMIC:%s\n", p->name); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |