blob: 3b3ccf162369b824c9f31b3ab0233d16e482f214 [file] [log] [blame]
henryc.chen21e4bd42021-03-04 09:44:42 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <console/console.h>
4#include <device/i2c_simple.h>
5#include <soc/mt6691.h>
6
7#define MT6691_MIN_VOLTAGE 300000
8#define MT6691_MAX_VOLTAGE 1300000
9#define MT6691_STEP_UV 5000
10
11enum {
12 MT6691_SLAVE_ADDR = 0x51,
13};
14
15int mt6691_set_voltage(uint8_t i2c_num, unsigned int volt_uv)
16{
17 uint8_t selector;
18
19 if (volt_uv > MT6691_MAX_VOLTAGE || volt_uv < MT6691_MIN_VOLTAGE) {
20 printk(BIOS_ERR, "%s: voltage out of range\n", __func__);
21 return -1;
22 }
23
24 selector = DIV_ROUND_UP(volt_uv - MT6691_MIN_VOLTAGE, MT6691_STEP_UV);
25
26 return i2c_write_field(i2c_num, MT6691_SLAVE_ADDR,
27 MT6691_VSEL0, selector, 0xFF, 0);
28}
29
30int mt6691_get_voltage(uint8_t i2c_num)
31{
32 uint8_t selector = 0;
33 unsigned int volt;
34
35 if (i2c_read_field(i2c_num, MT6691_SLAVE_ADDR, MT6691_VSEL0,
36 &selector, 0xFF, 0) < 0) {
37 printk(BIOS_ERR, "%s: failed to get voltage from i2c\n", __func__);
38 return -1;
39 }
40
41 volt = (selector * MT6691_STEP_UV) + MT6691_MIN_VOLTAGE;
42
43 if (volt > MT6691_MAX_VOLTAGE) {
44 printk(BIOS_ERR, "%s: voltage out of range\n", __func__);
45 return -1;
46 }
47
48 return volt;
49}
50
51static uint8_t get_mt6691_chip_id(uint8_t i2c_num)
52{
53 uint8_t id;
54
55 if (i2c_read_field(i2c_num, MT6691_SLAVE_ADDR, MT6691_MONITOR,
56 &id, 0x1, MT6691_PGOOD_SHIFT) < 0) {
57 printk(BIOS_ERR, "%s: failed to read from i2c", __func__);
58 return 0;
59 }
60
61 return id;
62}
63
64void mt6691_probe(uint8_t i2c_num)
65{
66 /* Check device ID is MT6691 */
67 if (!get_mt6691_chip_id(i2c_num)) {
Julius Wernere9665952022-01-21 17:06:20 -080068 printk(BIOS_ERR, "unknown MT6691 chip_id\n");
henryc.chen21e4bd42021-03-04 09:44:42 +080069 return;
70 }
71 /* Slew rate 12mV */
72 i2c_write_field(i2c_num, MT6691_SLAVE_ADDR, MT6691_CTRL2, 0x1,
73 MT6691_DN_SR_MASK, MT6691_DN_SR_SHIFT);
74}