blob: dea42de833f12bc48c2c48739c3fba8ac7d1224e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hans de Goedebb930c32015-04-25 14:07:37 +02002/*
3 * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
4 *
5 * Sunxi PMIC bus access helpers
6 *
7 * The axp152 & axp209 use an i2c bus, the axp221 uses the p2wi bus and the
8 * axp223 uses the rsb bus, these functions abstract this.
Hans de Goedebb930c32015-04-25 14:07:37 +02009 */
10
11#include <common.h>
12#include <asm/arch/p2wi.h>
13#include <asm/arch/rsb.h>
Hans de Goede858b6db2015-04-25 22:18:09 +020014#include <i2c.h>
Hans de Goedebb930c32015-04-25 14:07:37 +020015#include <asm/arch/pmic_bus.h>
16
Hans de Goede858b6db2015-04-25 22:18:09 +020017#define AXP152_I2C_ADDR 0x30
18
19#define AXP209_I2C_ADDR 0x34
20
Hans de Goedebb930c32015-04-25 14:07:37 +020021#define AXP221_CHIP_ADDR 0x68
22#define AXP221_CTRL_ADDR 0x3e
23#define AXP221_INIT_DATA 0x3e
24
vishnupatekar1895dfd2015-11-29 01:07:22 +080025/* AXP818 device and runtime addresses are same as AXP223 */
Hans de Goedebb930c32015-04-25 14:07:37 +020026#define AXP223_DEVICE_ADDR 0x3a3
27#define AXP223_RUNTIME_ADDR 0x2d
28
29int pmic_bus_init(void)
30{
31 /* This cannot be 0 because it is used in SPL before BSS is ready */
32 static int needs_init = 1;
Hans de Goede858b6db2015-04-25 22:18:09 +020033 __maybe_unused int ret;
Hans de Goedebb930c32015-04-25 14:07:37 +020034
35 if (!needs_init)
36 return 0;
37
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +080038#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede858b6db2015-04-25 22:18:09 +020039# ifdef CONFIG_MACH_SUN6I
Hans de Goedebb930c32015-04-25 14:07:37 +020040 p2wi_init();
41 ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
42 AXP221_INIT_DATA);
Chen-Yu Tsai478a3c52016-11-30 15:30:30 +080043# elif defined CONFIG_MACH_SUN8I_R40
44 /* Nothing. R40 uses the AXP221s in I2C mode */
45 ret = 0;
Hans de Goede858b6db2015-04-25 22:18:09 +020046# else
Hans de Goedebb930c32015-04-25 14:07:37 +020047 ret = rsb_init();
48 if (ret)
49 return ret;
50
51 ret = rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
Hans de Goede858b6db2015-04-25 22:18:09 +020052# endif
Hans de Goedebb930c32015-04-25 14:07:37 +020053 if (ret)
54 return ret;
Hans de Goede858b6db2015-04-25 22:18:09 +020055#endif
Hans de Goedebb930c32015-04-25 14:07:37 +020056
57 needs_init = 0;
58 return 0;
59}
60
61int pmic_bus_read(u8 reg, u8 *data)
62{
Hans de Goede858b6db2015-04-25 22:18:09 +020063#ifdef CONFIG_AXP152_POWER
64 return i2c_read(AXP152_I2C_ADDR, reg, 1, data, 1);
65#elif defined CONFIG_AXP209_POWER
66 return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +080067#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede858b6db2015-04-25 22:18:09 +020068# ifdef CONFIG_MACH_SUN6I
Hans de Goedebb930c32015-04-25 14:07:37 +020069 return p2wi_read(reg, data);
Chen-Yu Tsai478a3c52016-11-30 15:30:30 +080070# elif defined CONFIG_MACH_SUN8I_R40
71 return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
Hans de Goede858b6db2015-04-25 22:18:09 +020072# else
Hans de Goedebb930c32015-04-25 14:07:37 +020073 return rsb_read(AXP223_RUNTIME_ADDR, reg, data);
Hans de Goede858b6db2015-04-25 22:18:09 +020074# endif
Hans de Goedebb930c32015-04-25 14:07:37 +020075#endif
76}
77
78int pmic_bus_write(u8 reg, u8 data)
79{
Hans de Goede858b6db2015-04-25 22:18:09 +020080#ifdef CONFIG_AXP152_POWER
81 return i2c_write(AXP152_I2C_ADDR, reg, 1, &data, 1);
82#elif defined CONFIG_AXP209_POWER
83 return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +080084#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede858b6db2015-04-25 22:18:09 +020085# ifdef CONFIG_MACH_SUN6I
Hans de Goedebb930c32015-04-25 14:07:37 +020086 return p2wi_write(reg, data);
Chen-Yu Tsai478a3c52016-11-30 15:30:30 +080087# elif defined CONFIG_MACH_SUN8I_R40
88 return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
Hans de Goede858b6db2015-04-25 22:18:09 +020089# else
Hans de Goedebb930c32015-04-25 14:07:37 +020090 return rsb_write(AXP223_RUNTIME_ADDR, reg, data);
Hans de Goede858b6db2015-04-25 22:18:09 +020091# endif
Hans de Goedebb930c32015-04-25 14:07:37 +020092#endif
93}
94
95int pmic_bus_setbits(u8 reg, u8 bits)
96{
97 int ret;
98 u8 val;
99
100 ret = pmic_bus_read(reg, &val);
101 if (ret)
102 return ret;
103
Olliver Schinaglfa2ca7c2018-11-21 20:05:26 +0200104 if ((val & bits) == bits)
105 return 0;
106
Hans de Goedebb930c32015-04-25 14:07:37 +0200107 val |= bits;
108 return pmic_bus_write(reg, val);
109}
110
111int pmic_bus_clrbits(u8 reg, u8 bits)
112{
113 int ret;
114 u8 val;
115
116 ret = pmic_bus_read(reg, &val);
117 if (ret)
118 return ret;
119
Olliver Schinaglfa2ca7c2018-11-21 20:05:26 +0200120 if (!(val & bits))
121 return 0;
122
Hans de Goedebb930c32015-04-25 14:07:37 +0200123 val &= ~bits;
124 return pmic_bus_write(reg, val);
125}