Ł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> |
Masahiro Yamada | e722852 | 2014-11-26 16:00:58 +0900 | [diff] [blame] | 17 | #include <linux/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)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 24 | return -EINVAL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 25 | |
Aaron Durbin | 0f0900b | 2014-05-20 06:01:38 -0600 | [diff] [blame] | 26 | I2C_SET_BUS(p->bus); |
| 27 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 28 | switch (pmic_i2c_tx_num) { |
| 29 | case 3: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 30 | 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 Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 39 | break; |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 40 | case 2: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 41 | 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 Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 48 | break; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 49 | case 1: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 50 | buf[0] = cpu_to_le32(val) & 0xff; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 51 | break; |
Fabio Estevam | d38fb7e | 2012-03-16 11:32:09 +0000 | [diff] [blame] | 52 | default: |
| 53 | printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 54 | return -EINVAL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 57 | return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int pmic_reg_read(struct pmic *p, u32 reg, u32 *val) |
| 61 | { |
| 62 | unsigned char buf[4] = { 0 }; |
| 63 | u32 ret_val = 0; |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 64 | int ret; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 65 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 66 | if (check_reg(p, reg)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 67 | return -EINVAL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 68 | |
Aaron Durbin | 0f0900b | 2014-05-20 06:01:38 -0600 | [diff] [blame] | 69 | I2C_SET_BUS(p->bus); |
| 70 | |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 71 | ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num); |
| 72 | if (ret) |
| 73 | return ret; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 74 | |
| 75 | switch (pmic_i2c_tx_num) { |
| 76 | case 3: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 77 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) |
| 78 | ret_val = le32_to_cpu(buf[2] << 16 |
| 79 | | buf[1] << 8 | buf[0]); |
| 80 | else |
| 81 | ret_val = le32_to_cpu(buf[0] << 16 | |
| 82 | buf[1] << 8 | buf[2]); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 83 | break; |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 84 | case 2: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 85 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) |
| 86 | ret_val = le32_to_cpu(buf[1] << 8 | buf[0]); |
| 87 | else |
| 88 | ret_val = le32_to_cpu(buf[0] << 8 | buf[1]); |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 89 | break; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 90 | case 1: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 91 | ret_val = le32_to_cpu(buf[0]); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 92 | break; |
Fabio Estevam | d38fb7e | 2012-03-16 11:32:09 +0000 | [diff] [blame] | 93 | default: |
| 94 | printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 95 | return -EINVAL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 96 | } |
| 97 | memcpy(val, &ret_val, sizeof(ret_val)); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int pmic_probe(struct pmic *p) |
| 103 | { |
Łukasz Majewski | f4864c3 | 2013-08-16 15:35:26 +0200 | [diff] [blame] | 104 | i2c_set_bus_num(p->bus); |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 105 | debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 106 | if (i2c_probe(pmic_i2c_addr)) { |
| 107 | printf("Can't find PMIC:%s\n", p->name); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 108 | return -ENODEV; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |