Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Samsung Electronics |
| 3 | * Alim Akhtar <alim.akhtar@samsung.com> |
| 4 | * |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 16 | #include <console/console.h> |
David Hendricks | 1d5390e | 2013-01-09 16:26:26 -0800 | [diff] [blame] | 17 | #include <arch/io.h> |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 18 | #include <device/i2c.h> |
David Hendricks | 1d5390e | 2013-01-09 16:26:26 -0800 | [diff] [blame] | 19 | |
| 20 | #include "max77686.h" |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 21 | |
| 22 | /* Chip register numbers (not exported from this module) */ |
| 23 | enum { |
| 24 | REG_BBAT = 0x7e, |
| 25 | |
| 26 | /* Bits for BBAT */ |
| 27 | BBAT_BBCHOSTEN_MASK = 1 << 0, |
| 28 | BBAT_BBCVS_SHIFT = 3, |
| 29 | BBAT_BBCVS_MASK = 3 << BBAT_BBCVS_SHIFT, |
| 30 | }; |
| 31 | |
| 32 | /* |
| 33 | * Max77686 parameters values |
| 34 | * see max77686.h for parameters details |
| 35 | */ |
David Hendricks | 1d5390e | 2013-01-09 16:26:26 -0800 | [diff] [blame] | 36 | struct max77686_para max77686_param[] = {/*{vol_addr, vol_bitpos, |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 37 | vol_bitmask, reg_enaddr, reg_enbitpos, reg_enbitmask, reg_enbiton, |
| 38 | reg_enbitoff, vol_min, vol_div}*/ |
David Hendricks | 1d5390e | 2013-01-09 16:26:26 -0800 | [diff] [blame] | 39 | {/* PMIC_BUCK1 */ 0x11, 0x0, 0x3F, 0x10, 0x0, 0x3, 0x3, 0x0, 750, 50000}, |
| 40 | {/* PMIC_BUCK2 */ 0x14, 0x0, 0xFF, 0x12, 0x4, 0x3, 0x1, 0x0, 600, 12500}, |
| 41 | {/* PMIC_BUCK3 */ 0x1E, 0x0, 0xFF, 0x1C, 0x4, 0x3, 0x1, 0x0, 600, 12500}, |
| 42 | {/* PMIC_BUCK4 */ 0x28, 0x0, 0xFF, 0x26, 0x4, 0x3, 0x1, 0x0, 600, 12500}, |
| 43 | {/* PMIC_BUCK5 */ 0x31, 0x0, 0x3F, 0x30, 0x0, 0x3, 0x3, 0x0, 750, 50000}, |
| 44 | {/* PMIC_BUCK6 */ 0x33, 0x0, 0x3F, 0x32, 0x0, 0x3, 0x3, 0x0, 750, 50000}, |
| 45 | {/* PMIC_BUCK7 */ 0x35, 0x0, 0x3F, 0x34, 0x0, 0x3, 0x3, 0x0, 750, 50000}, |
| 46 | {/* PMIC_BUCK8 */ 0x37, 0x0, 0x3F, 0x36, 0x0, 0x3, 0x3, 0x0, 750, 50000}, |
| 47 | {/* PMIC_BUCK9 */ 0x39, 0x0, 0x3F, 0x38, 0x0, 0x3, 0x3, 0x0, 750, 50000}, |
| 48 | {/* PMIC_LDO1 */ 0x40, 0x0, 0x3F, 0x40, 0x6, 0x3, 0x3, 0x0, 800, 25000}, |
| 49 | {/* PMIC_LDO2 */ 0x41, 0x0, 0x3F, 0x41, 0x6, 0x3, 0x1, 0x0, 800, 25000}, |
| 50 | {/* PMIC_LDO3 */ 0x42, 0x0, 0x3F, 0x42, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 51 | {/* PMIC_LDO4 */ 0x43, 0x0, 0x3F, 0x43, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 52 | {/* PMIC_LDO5 */ 0x44, 0x0, 0x3F, 0x44, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 53 | {/* PMIC_LDO6 */ 0x45, 0x0, 0x3F, 0x45, 0x6, 0x3, 0x1, 0x0, 800, 25000}, |
| 54 | {/* PMIC_LDO7 */ 0x46, 0x0, 0x3F, 0x46, 0x6, 0x3, 0x1, 0x0, 800, 25000}, |
| 55 | {/* PMIC_LDO8 */ 0x47, 0x0, 0x3F, 0x47, 0x6, 0x3, 0x1, 0x0, 800, 25000}, |
| 56 | {/* PMIC_LDO9 */ 0x48, 0x0, 0x3F, 0x48, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 57 | {/* PMIC_LDO10 */ 0x49, 0x0, 0x3F, 0x49, 0x6, 0x3, 0x1, 0x0, 800, 50000}, |
| 58 | {/* PMIC_LDO11 */ 0x4A, 0x0, 0x3F, 0x4A, 0x6, 0x3, 0x1, 0x0, 800, 50000}, |
| 59 | {/* PMIC_LDO12 */ 0x4B, 0x0, 0x3F, 0x4B, 0x6, 0x3, 0x1, 0x0, 800, 50000}, |
| 60 | {/* PMIC_LDO13 */ 0x4C, 0x0, 0x3F, 0x4C, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 61 | {/* PMIC_LDO14 */ 0x4D, 0x0, 0x3F, 0x4D, 0x6, 0x3, 0x1, 0x0, 800, 50000}, |
| 62 | {/* PMIC_LDO15 */ 0x4E, 0x0, 0x3F, 0x4E, 0x6, 0x3, 0x1, 0x0, 800, 25000}, |
| 63 | {/* PMIC_LDO16 */ 0x4F, 0x0, 0x3F, 0x4F, 0x6, 0x3, 0x1, 0x0, 800, 50000}, |
| 64 | {/* PMIC_LDO17 */ 0x50, 0x0, 0x3F, 0x50, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 65 | {/* PMIC_LDO18 */ 0x51, 0x0, 0x3F, 0x51, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 66 | {/* PMIC_LDO19 */ 0x52, 0x0, 0x3F, 0x52, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 67 | {/* PMIC_LDO20 */ 0x53, 0x0, 0x3F, 0x53, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 68 | {/* PMIC_LDO21 */ 0x54, 0x0, 0x3F, 0x54, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 69 | {/* PMIC_LDO22 */ 0x55, 0x0, 0x3F, 0x55, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 70 | {/* PMIC_LDO23 */ 0x56, 0x0, 0x3F, 0x56, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 71 | {/* PMIC_LDO24 */ 0x57, 0x0, 0x3F, 0x57, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 72 | {/* PMIC_LDO25 */ 0x58, 0x0, 0x3F, 0x58, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 73 | {/* PMIC_LDO26 */ 0x59, 0x0, 0x3F, 0x59, 0x6, 0x3, 0x3, 0x0, 800, 50000}, |
| 74 | {/* PMIC_EN32KHZ_CP */ 0x0, 0x0, 0x0, 0x7F, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0}, |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * Write a value to a register |
| 79 | * |
| 80 | * @param chip_addr i2c addr for max77686 |
| 81 | * @param reg reg number to write |
| 82 | * @param val value to be written |
| 83 | * |
| 84 | */ |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 85 | static inline int max77686_i2c_write(unsigned int bus, unsigned char chip_addr, |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 86 | unsigned int reg, unsigned char val) |
| 87 | { |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 88 | return i2c_writeb(bus, chip_addr, reg, val); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Read a value from a register |
| 93 | * |
| 94 | * @param chip_addr i2c addr for max77686 |
| 95 | * @param reg reg number to write |
| 96 | * @param val value to be written |
| 97 | * |
| 98 | */ |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 99 | static inline int max77686_i2c_read(unsigned int bus, unsigned char chip_addr, |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 100 | unsigned int reg, unsigned char *val) |
| 101 | { |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 102 | return i2c_readb(bus, chip_addr, reg, (uint8_t *)val); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Enable the max77686 register |
| 107 | * |
| 108 | * @param reg register number of buck/ldo to be enabled |
| 109 | * @param enable enable or disable bit |
| 110 | * |
| 111 | * REG_DISABLE = 0, |
| 112 | needed to set the buck/ldo enable bit OFF |
| 113 | * @return Return 0 if ok, else -1 |
| 114 | */ |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 115 | static int max77686_enablereg(unsigned int bus, enum max77686_regnum reg, int enable) |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 116 | { |
| 117 | struct max77686_para *pmic; |
| 118 | unsigned char read_data; |
| 119 | int ret; |
| 120 | |
| 121 | pmic = &max77686_param[reg]; |
| 122 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 123 | ret = max77686_i2c_read(bus, MAX77686_I2C_ADDR, pmic->reg_enaddr, |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 124 | &read_data); |
| 125 | if (ret != 0) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 126 | printk(BIOS_DEBUG, "max77686 i2c read failed.\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | if (enable == REG_DISABLE) { |
| 131 | clrbits_8(&read_data, |
| 132 | pmic->reg_enbitmask << pmic->reg_enbitpos); |
| 133 | } else { |
| 134 | clrsetbits_8(&read_data, |
| 135 | pmic->reg_enbitmask << pmic->reg_enbitpos, |
| 136 | pmic->reg_enbiton << pmic->reg_enbitpos); |
| 137 | } |
| 138 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 139 | ret = max77686_i2c_write(bus, MAX77686_I2C_ADDR, |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 140 | pmic->reg_enaddr, read_data); |
| 141 | if (ret != 0) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 142 | printk(BIOS_DEBUG, "max77686 i2c write failed.\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 149 | int max77686_volsetting(unsigned int bus, enum max77686_regnum reg, |
| 150 | unsigned int volt, int enable, int volt_units) |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 151 | { |
| 152 | struct max77686_para *pmic; |
| 153 | unsigned char read_data; |
| 154 | int vol_level = 0; |
| 155 | int ret; |
| 156 | |
| 157 | pmic = &max77686_param[reg]; |
| 158 | |
| 159 | if (pmic->vol_addr == 0) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 160 | printk(BIOS_DEBUG, "not a voltage register.\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 161 | return -1; |
| 162 | } |
| 163 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 164 | ret = max77686_i2c_read(bus, MAX77686_I2C_ADDR, pmic->vol_addr, &read_data); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 165 | if (ret != 0) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 166 | printk(BIOS_DEBUG, "max77686 i2c read failed.\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | if (volt_units == MAX77686_UV) |
David Hendricks | 1d5390e | 2013-01-09 16:26:26 -0800 | [diff] [blame] | 171 | vol_level = volt - (u32)pmic->vol_min * 1000; |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 172 | else |
David Hendricks | 1d5390e | 2013-01-09 16:26:26 -0800 | [diff] [blame] | 173 | vol_level = (volt - (u32)pmic->vol_min) * 1000; |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 174 | |
| 175 | if (vol_level < 0) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 176 | printk(BIOS_DEBUG, "Not a valid voltage level to set\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 177 | return -1; |
| 178 | } |
David Hendricks | 1d5390e | 2013-01-09 16:26:26 -0800 | [diff] [blame] | 179 | vol_level /= (u32)pmic->vol_div; |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 180 | |
| 181 | clrsetbits_8(&read_data, pmic->vol_bitmask << pmic->vol_bitpos, |
| 182 | vol_level << pmic->vol_bitpos); |
| 183 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 184 | ret = max77686_i2c_write(bus, MAX77686_I2C_ADDR, pmic->vol_addr, read_data); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 185 | if (ret != 0) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 186 | printk(BIOS_DEBUG, "max77686 i2c write failed.\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 190 | ret = max77686_enablereg(bus, reg, enable); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 191 | if (ret != 0) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 192 | printk(BIOS_DEBUG, "Failed to enable buck/ldo.\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 193 | return -1; |
| 194 | } |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 198 | int max77686_enable_32khz_cp(unsigned int bus) |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 199 | { |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 200 | return max77686_enablereg(bus, PMIC_EN32KHZ_CP, REG_ENABLE); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 201 | } |
| 202 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 203 | int max77686_disable_backup_batt(unsigned int bus) |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 204 | { |
| 205 | unsigned char val; |
| 206 | int ret; |
| 207 | |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 208 | ret = max77686_i2c_read(bus, MAX77686_I2C_ADDR, REG_BBAT, &val); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 209 | if (ret) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 210 | printk(BIOS_DEBUG, "max77686 i2c read failed\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | /* If we already have the correct values, exit */ |
| 215 | if ((val & (BBAT_BBCVS_MASK | BBAT_BBCHOSTEN_MASK)) == |
| 216 | BBAT_BBCVS_MASK) |
| 217 | return 0; |
| 218 | |
| 219 | /* First disable charging */ |
| 220 | val &= ~BBAT_BBCHOSTEN_MASK; |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 221 | ret = max77686_i2c_write(bus, MAX77686_I2C_ADDR, REG_BBAT, val); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 222 | if (ret) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 223 | printk(BIOS_DEBUG, "max77686 i2c write failed\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | /* Finally select 3.5V to minimize power consumption */ |
| 228 | val |= BBAT_BBCVS_MASK; |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 229 | ret = max77686_i2c_write(bus, MAX77686_I2C_ADDR, REG_BBAT, val); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 230 | if (ret) { |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 231 | printk(BIOS_DEBUG, "max77686 i2c write failed\n"); |
Ronald G. Minnich | f2e13b0 | 2012-11-28 10:44:12 -0800 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |