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 * SPDX-License-Identifier: GPL-2.0+ 11 */ 12 13 #include <common.h> 14 #include <linux/types.h> 15 #include <power/pmic.h> 16 #include <i2c.h> 17 #include <linux/compiler.h> 18 19 int pmic_reg_write(struct pmic *p, u32 reg, u32 val) 20 { 21 unsigned char buf[4] = { 0 }; 22 int ret; 23 24 if (check_reg(p, reg)) 25 return -EINVAL; 26 27 I2C_SET_BUS(p->bus); 28 29 switch (pmic_i2c_tx_num) { 30 case 3: 31 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) { 32 buf[2] = (cpu_to_le32(val) >> 16) & 0xff; 33 buf[1] = (cpu_to_le32(val) >> 8) & 0xff; 34 buf[0] = cpu_to_le32(val) & 0xff; 35 } else { 36 buf[0] = (cpu_to_le32(val) >> 16) & 0xff; 37 buf[1] = (cpu_to_le32(val) >> 8) & 0xff; 38 buf[2] = cpu_to_le32(val) & 0xff; 39 } 40 break; 41 case 2: 42 if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) { 43 buf[1] = (cpu_to_le32(val) >> 8) & 0xff; 44 buf[0] = cpu_to_le32(val) & 0xff; 45 } else { 46 buf[0] = (cpu_to_le32(val) >> 8) & 0xff; 47 buf[1] = cpu_to_le32(val) & 0xff; 48 } 49 break; 50 case 1: 51 buf[0] = cpu_to_le32(val) & 0xff; 52 break; 53 default: 54 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); 55 return -EINVAL; 56 } 57 58 return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num); 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 int ret; 66 67 if (check_reg(p, reg)) 68 return -EINVAL; 69 70 I2C_SET_BUS(p->bus); 71 72 ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num); 73 if (ret) 74 return ret; 75 76 switch (pmic_i2c_tx_num) { 77 case 3: 78 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]); 84 break; 85 case 2: 86 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]); 90 break; 91 case 1: 92 ret_val = le32_to_cpu(buf[0]); 93 break; 94 default: 95 printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); 96 return -EINVAL; 97 } 98 memcpy(val, &ret_val, sizeof(ret_val)); 99 100 return 0; 101 } 102 103 int pmic_probe(struct pmic *p) 104 { 105 i2c_set_bus_num(p->bus); 106 debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name); 107 if (i2c_probe(pmic_i2c_addr)) { 108 printf("Can't find PMIC:%s\n", p->name); 109 return -ENODEV; 110 } 111 112 return 0; 113 } 114