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