Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Samsung Electronics |
| 4 | * Lukasz Majewski <l.majewski@samsung.com> |
| 5 | * |
| 6 | * (C) Copyright 2010 |
| 7 | * Stefano Babic, DENX Software Engineering, sbabic@denx.de |
| 8 | * |
| 9 | * (C) Copyright 2008-2009 Freescale Semiconductor, Inc. |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 10 | * (C) Copyright 2019 NXP |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 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; |
Peng Fan | 0335e52 | 2020-12-26 19:35:42 +0800 | [diff] [blame] | 25 | #if CONFIG_IS_ENABLED(DM_I2C) |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 26 | struct udevice *dev; |
| 27 | int ret; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 28 | |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 29 | ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr, |
| 30 | 1, &dev); |
| 31 | if (ret) { |
| 32 | printf("%s: Cannot find udev for a bus %d\n", __func__, |
| 33 | p->bus); |
| 34 | return -ENXIO; |
| 35 | } |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 36 | #endif |
Aaron Durbin | 0f0900b | 2014-05-20 06:01:38 -0600 | [diff] [blame] | 37 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 38 | switch (pmic_i2c_tx_num) { |
| 39 | case 3: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 40 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) { |
| 41 | buf[2] = (cpu_to_le32(val) >> 16) & 0xff; |
| 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) >> 16) & 0xff; |
| 46 | buf[1] = (cpu_to_le32(val) >> 8) & 0xff; |
| 47 | buf[2] = cpu_to_le32(val) & 0xff; |
| 48 | } |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 49 | break; |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 50 | case 2: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 51 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) { |
| 52 | buf[1] = (cpu_to_le32(val) >> 8) & 0xff; |
| 53 | buf[0] = cpu_to_le32(val) & 0xff; |
| 54 | } else { |
| 55 | buf[0] = (cpu_to_le32(val) >> 8) & 0xff; |
| 56 | buf[1] = cpu_to_le32(val) & 0xff; |
| 57 | } |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 58 | break; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 59 | case 1: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 60 | buf[0] = cpu_to_le32(val) & 0xff; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 61 | break; |
Fabio Estevam | d38fb7e | 2012-03-16 11:32:09 +0000 | [diff] [blame] | 62 | default: |
| 63 | printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 64 | return -EINVAL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Peng Fan | 0335e52 | 2020-12-26 19:35:42 +0800 | [diff] [blame] | 67 | #if CONFIG_IS_ENABLED(DM_I2C) |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 68 | return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num); |
| 69 | #else |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 70 | return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num); |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 71 | #endif |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | int pmic_reg_read(struct pmic *p, u32 reg, u32 *val) |
| 75 | { |
| 76 | unsigned char buf[4] = { 0 }; |
| 77 | u32 ret_val = 0; |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 78 | int ret; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 79 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 80 | if (check_reg(p, reg)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 81 | return -EINVAL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 82 | |
Peng Fan | 0335e52 | 2020-12-26 19:35:42 +0800 | [diff] [blame] | 83 | #if CONFIG_IS_ENABLED(DM_I2C) |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 84 | struct udevice *dev; |
Aaron Durbin | 0f0900b | 2014-05-20 06:01:38 -0600 | [diff] [blame] | 85 | |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 86 | ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr, |
| 87 | 1, &dev); |
| 88 | if (ret) { |
| 89 | printf("%s: Cannot find udev for a bus %d\n", __func__, |
| 90 | p->bus); |
| 91 | return -ENXIO; |
| 92 | } |
| 93 | ret = dm_i2c_read(dev, reg, buf, pmic_i2c_tx_num); |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 94 | #endif |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 95 | if (ret) |
| 96 | return ret; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 97 | |
| 98 | switch (pmic_i2c_tx_num) { |
| 99 | case 3: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 100 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) |
| 101 | ret_val = le32_to_cpu(buf[2] << 16 |
| 102 | | buf[1] << 8 | buf[0]); |
| 103 | else |
| 104 | ret_val = le32_to_cpu(buf[0] << 16 | |
| 105 | buf[1] << 8 | buf[2]); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 106 | break; |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 107 | case 2: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 108 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) |
| 109 | ret_val = le32_to_cpu(buf[1] << 8 | buf[0]); |
| 110 | else |
| 111 | ret_val = le32_to_cpu(buf[0] << 8 | buf[1]); |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 112 | break; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 113 | case 1: |
Łukasz Majewski | 40f5ca0 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 114 | ret_val = le32_to_cpu(buf[0]); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 115 | break; |
Fabio Estevam | d38fb7e | 2012-03-16 11:32:09 +0000 | [diff] [blame] | 116 | default: |
| 117 | printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 118 | return -EINVAL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 119 | } |
| 120 | memcpy(val, &ret_val, sizeof(ret_val)); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int pmic_probe(struct pmic *p) |
| 126 | { |
Łukasz Majewski | 8a69187 | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 127 | debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name); |
Peng Fan | 0335e52 | 2020-12-26 19:35:42 +0800 | [diff] [blame] | 128 | #if CONFIG_IS_ENABLED(DM_I2C) |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 129 | struct udevice *dev; |
| 130 | int ret; |
| 131 | |
| 132 | ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr, |
| 133 | 1, &dev); |
| 134 | if (ret) { |
| 135 | printf("%s: Cannot find udev for a bus %d\n", __func__, |
| 136 | p->bus); |
| 137 | return -ENXIO; |
| 138 | } |
| 139 | #else /* Non DM I2C support - will be removed */ |
| 140 | i2c_set_bus_num(p->bus); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 141 | if (i2c_probe(pmic_i2c_addr)) { |
| 142 | printf("Can't find PMIC:%s\n", p->name); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 143 | return -ENODEV; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 144 | } |
Biwen Li | f0018f5 | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 145 | #endif |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |