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