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