Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Łukasz Majewski | e542b7f | 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 | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 10 | * (C) Copyright 2019 NXP |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 15 | #include <linux/types.h> |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 16 | #include <power/pmic.h> |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 17 | #include <i2c.h> |
Masahiro Yamada | afc366f | 2014-11-26 16:00:58 +0900 | [diff] [blame] | 18 | #include <linux/compiler.h> |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 19 | |
| 20 | int pmic_reg_write(struct pmic *p, u32 reg, u32 val) |
| 21 | { |
| 22 | unsigned char buf[4] = { 0 }; |
| 23 | |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 24 | if (check_reg(p, reg)) |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 25 | return -EINVAL; |
Peng Fan | 6489dac | 2020-12-26 19:35:42 +0800 | [diff] [blame] | 26 | #if CONFIG_IS_ENABLED(DM_I2C) |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 27 | struct udevice *dev; |
| 28 | int ret; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 29 | |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 30 | ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr, |
| 31 | 1, &dev); |
| 32 | if (ret) { |
| 33 | printf("%s: Cannot find udev for a bus %d\n", __func__, |
| 34 | p->bus); |
| 35 | return -ENXIO; |
| 36 | } |
| 37 | #else /* Non DM I2C support - will be removed */ |
Aaron Durbin | 0b259dc | 2014-05-20 06:01:38 -0600 | [diff] [blame] | 38 | I2C_SET_BUS(p->bus); |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 39 | #endif |
Aaron Durbin | 0b259dc | 2014-05-20 06:01:38 -0600 | [diff] [blame] | 40 | |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 41 | switch (pmic_i2c_tx_num) { |
| 42 | case 3: |
Łukasz Majewski | 86879d7 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 43 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) { |
| 44 | buf[2] = (cpu_to_le32(val) >> 16) & 0xff; |
| 45 | buf[1] = (cpu_to_le32(val) >> 8) & 0xff; |
| 46 | buf[0] = cpu_to_le32(val) & 0xff; |
| 47 | } else { |
| 48 | buf[0] = (cpu_to_le32(val) >> 16) & 0xff; |
| 49 | buf[1] = (cpu_to_le32(val) >> 8) & 0xff; |
| 50 | buf[2] = cpu_to_le32(val) & 0xff; |
| 51 | } |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 52 | break; |
Łukasz Majewski | b5bf9ca | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 53 | case 2: |
Łukasz Majewski | 86879d7 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 54 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) { |
| 55 | buf[1] = (cpu_to_le32(val) >> 8) & 0xff; |
| 56 | buf[0] = cpu_to_le32(val) & 0xff; |
| 57 | } else { |
| 58 | buf[0] = (cpu_to_le32(val) >> 8) & 0xff; |
| 59 | buf[1] = cpu_to_le32(val) & 0xff; |
| 60 | } |
Łukasz Majewski | b5bf9ca | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 61 | break; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 62 | case 1: |
Łukasz Majewski | 86879d7 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 63 | buf[0] = cpu_to_le32(val) & 0xff; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 64 | break; |
Fabio Estevam | a22429d | 2012-03-16 11:32:09 +0000 | [diff] [blame] | 65 | default: |
| 66 | printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 67 | return -EINVAL; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Peng Fan | 6489dac | 2020-12-26 19:35:42 +0800 | [diff] [blame] | 70 | #if CONFIG_IS_ENABLED(DM_I2C) |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 71 | return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num); |
| 72 | #else |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 73 | return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num); |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 74 | #endif |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 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; |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 81 | int ret; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 82 | |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 83 | if (check_reg(p, reg)) |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 84 | return -EINVAL; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 85 | |
Peng Fan | 6489dac | 2020-12-26 19:35:42 +0800 | [diff] [blame] | 86 | #if CONFIG_IS_ENABLED(DM_I2C) |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 87 | struct udevice *dev; |
Aaron Durbin | 0b259dc | 2014-05-20 06:01:38 -0600 | [diff] [blame] | 88 | |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 89 | ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr, |
| 90 | 1, &dev); |
| 91 | if (ret) { |
| 92 | printf("%s: Cannot find udev for a bus %d\n", __func__, |
| 93 | p->bus); |
| 94 | return -ENXIO; |
| 95 | } |
| 96 | ret = dm_i2c_read(dev, reg, buf, pmic_i2c_tx_num); |
| 97 | #else /* Non DM I2C support - will be removed */ |
| 98 | I2C_SET_BUS(p->bus); |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 99 | ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num); |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 100 | #endif |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 101 | if (ret) |
| 102 | return ret; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 103 | |
| 104 | switch (pmic_i2c_tx_num) { |
| 105 | case 3: |
Łukasz Majewski | 86879d7 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 106 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) |
| 107 | ret_val = le32_to_cpu(buf[2] << 16 |
| 108 | | buf[1] << 8 | buf[0]); |
| 109 | else |
| 110 | ret_val = le32_to_cpu(buf[0] << 16 | |
| 111 | buf[1] << 8 | buf[2]); |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 112 | break; |
Łukasz Majewski | b5bf9ca | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 113 | case 2: |
Łukasz Majewski | 86879d7 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 114 | if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) |
| 115 | ret_val = le32_to_cpu(buf[1] << 8 | buf[0]); |
| 116 | else |
| 117 | ret_val = le32_to_cpu(buf[0] << 8 | buf[1]); |
Łukasz Majewski | b5bf9ca | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 118 | break; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 119 | case 1: |
Łukasz Majewski | 86879d7 | 2012-11-13 03:21:53 +0000 | [diff] [blame] | 120 | ret_val = le32_to_cpu(buf[0]); |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 121 | break; |
Fabio Estevam | a22429d | 2012-03-16 11:32:09 +0000 | [diff] [blame] | 122 | default: |
| 123 | printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num); |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 124 | return -EINVAL; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 125 | } |
| 126 | memcpy(val, &ret_val, sizeof(ret_val)); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | int pmic_probe(struct pmic *p) |
| 132 | { |
Łukasz Majewski | b5bf9ca | 2012-11-13 03:21:52 +0000 | [diff] [blame] | 133 | debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name); |
Peng Fan | 6489dac | 2020-12-26 19:35:42 +0800 | [diff] [blame] | 134 | #if CONFIG_IS_ENABLED(DM_I2C) |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 135 | struct udevice *dev; |
| 136 | int ret; |
| 137 | |
| 138 | ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr, |
| 139 | 1, &dev); |
| 140 | if (ret) { |
| 141 | printf("%s: Cannot find udev for a bus %d\n", __func__, |
| 142 | p->bus); |
| 143 | return -ENXIO; |
| 144 | } |
| 145 | #else /* Non DM I2C support - will be removed */ |
| 146 | i2c_set_bus_num(p->bus); |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 147 | if (i2c_probe(pmic_i2c_addr)) { |
| 148 | printf("Can't find PMIC:%s\n", p->name); |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 149 | return -ENODEV; |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 150 | } |
Biwen Li | bb1165f | 2020-02-05 22:02:17 +0800 | [diff] [blame] | 151 | #endif |
Łukasz Majewski | e542b7f | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |