1 // SPDX-License-Identifier: GPL-2.0
3 * AMD Platform Management Framework Driver - Smart PC Capabilities
5 * Copyright (c) 2023, Advanced Micro Devices, Inc.
12 #include <acpi/button.h>
13 #include <linux/amd-pmf-io.h>
14 #include <linux/power_supply.h>
15 #include <linux/units.h>
18 #ifdef CONFIG_AMD_PMF_DEBUG
19 static const char *ta_slider_as_str(unsigned int state)
22 case TA_BEST_PERFORMANCE:
24 case TA_BETTER_PERFORMANCE:
29 return "Unknown TA Slider State";
33 void amd_pmf_dump_ta_inputs(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
35 dev_dbg(dev->dev, "==== TA inputs START ====\n");
36 dev_dbg(dev->dev, "Slider State: %s\n", ta_slider_as_str(in->ev_info.power_slider));
37 dev_dbg(dev->dev, "Power Source: %s\n", amd_pmf_source_as_str(in->ev_info.power_source));
38 dev_dbg(dev->dev, "Battery Percentage: %u\n", in->ev_info.bat_percentage);
39 dev_dbg(dev->dev, "Designed Battery Capacity: %u\n", in->ev_info.bat_design);
40 dev_dbg(dev->dev, "Fully Charged Capacity: %u\n", in->ev_info.full_charge_capacity);
41 dev_dbg(dev->dev, "Drain Rate: %d\n", in->ev_info.drain_rate);
42 dev_dbg(dev->dev, "Socket Power: %u\n", in->ev_info.socket_power);
43 dev_dbg(dev->dev, "Skin Temperature: %u\n", in->ev_info.skin_temperature);
44 dev_dbg(dev->dev, "Avg C0 Residency: %u\n", in->ev_info.avg_c0residency);
45 dev_dbg(dev->dev, "Max C0 Residency: %u\n", in->ev_info.max_c0residency);
46 dev_dbg(dev->dev, "GFX Busy: %u\n", in->ev_info.gfx_busy);
47 dev_dbg(dev->dev, "LID State: %s\n", in->ev_info.lid_state ? "close" : "open");
48 dev_dbg(dev->dev, "User Presence: %s\n", in->ev_info.user_present ? "Present" : "Away");
49 dev_dbg(dev->dev, "Ambient Light: %d\n", in->ev_info.ambient_light);
50 dev_dbg(dev->dev, "==== TA inputs END ====\n");
53 void amd_pmf_dump_ta_inputs(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in) {}
56 static void amd_pmf_get_smu_info(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
61 memset(dev->buf, 0, sizeof(dev->m_table));
62 amd_pmf_send_cmd(dev, SET_TRANSFER_TABLE, 0, 7, NULL);
63 memcpy(&dev->m_table, dev->buf, sizeof(dev->m_table));
65 in->ev_info.socket_power = dev->m_table.apu_power + dev->m_table.dgpu_power;
66 in->ev_info.skin_temperature = dev->m_table.skin_temp;
68 /* Get the avg and max C0 residency of all the cores */
69 max = dev->m_table.avg_core_c0residency[0];
70 for (i = 0; i < ARRAY_SIZE(dev->m_table.avg_core_c0residency); i++) {
71 avg += dev->m_table.avg_core_c0residency[i];
72 if (dev->m_table.avg_core_c0residency[i] > max)
73 max = dev->m_table.avg_core_c0residency[i];
76 avg = DIV_ROUND_CLOSEST(avg, ARRAY_SIZE(dev->m_table.avg_core_c0residency));
77 in->ev_info.avg_c0residency = avg;
78 in->ev_info.max_c0residency = max;
79 in->ev_info.gfx_busy = dev->m_table.avg_gfx_activity;
82 static const char * const pmf_battery_supply_name[] = {
87 static int amd_pmf_get_battery_prop(enum power_supply_property prop)
89 union power_supply_propval value;
90 struct power_supply *psy;
93 for (i = 0; i < ARRAY_SIZE(pmf_battery_supply_name); i++) {
94 psy = power_supply_get_by_name(pmf_battery_supply_name[i]);
98 ret = power_supply_get_property(psy, prop, &value);
100 power_supply_put(psy);
108 static int amd_pmf_get_battery_info(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
112 val = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_PRESENT);
118 in->ev_info.bat_percentage = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_CAPACITY);
119 /* all values in mWh metrics */
120 in->ev_info.bat_design = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN) /
122 in->ev_info.full_charge_capacity = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_ENERGY_FULL) /
124 in->ev_info.drain_rate = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_POWER_NOW) /
130 static int amd_pmf_get_slider_info(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
134 switch (dev->current_profile) {
135 case PLATFORM_PROFILE_PERFORMANCE:
136 val = TA_BEST_PERFORMANCE;
138 case PLATFORM_PROFILE_BALANCED:
139 val = TA_BETTER_PERFORMANCE;
141 case PLATFORM_PROFILE_LOW_POWER:
142 val = TA_BEST_BATTERY;
145 dev_err(dev->dev, "Unknown Platform Profile.\n");
148 in->ev_info.power_slider = val;
153 static void amd_pmf_get_sensor_info(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
155 struct amd_sfh_info sfh_info;
157 /* Get the latest information from SFH */
158 in->ev_info.user_present = false;
161 if (!amd_get_sfh_info(&sfh_info, MT_ALS))
162 in->ev_info.ambient_light = sfh_info.ambient_light;
164 dev_dbg(dev->dev, "ALS is not enabled/detected\n");
167 if (!amd_get_sfh_info(&sfh_info, MT_HPD)) {
168 if (sfh_info.user_present == SFH_USER_PRESENT)
169 in->ev_info.user_present = true;
171 dev_dbg(dev->dev, "HPD is not enabled/detected\n");
175 void amd_pmf_populate_ta_inputs(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
177 /* TA side lid open is 1 and close is 0, hence the ! here */
178 in->ev_info.lid_state = !acpi_lid_open();
179 in->ev_info.power_source = amd_pmf_get_power_source();
180 amd_pmf_get_smu_info(dev, in);
181 amd_pmf_get_battery_info(dev, in);
182 amd_pmf_get_slider_info(dev, in);
183 amd_pmf_get_sensor_info(dev, in);