1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pmic.c - Intel PMIC operation region driver
5 * Copyright (C) 2014 Intel Corporation. All rights reserved.
8 #include <linux/export.h>
9 #include <linux/acpi.h>
10 #include <linux/mfd/intel_soc_pmic.h>
11 #include <linux/regmap.h>
12 #include <acpi/acpi_lpat.h>
13 #include "intel_pmic.h"
15 #define PMIC_POWER_OPREGION_ID 0x8d
16 #define PMIC_THERMAL_OPREGION_ID 0x8c
17 #define PMIC_REGS_OPREGION_ID 0x8f
19 struct intel_pmic_regs_handler_ctx {
24 struct intel_pmic_opregion {
26 struct acpi_lpat_conversion_table *lpat_table;
27 struct regmap *regmap;
28 struct intel_pmic_opregion_data *data;
29 struct intel_pmic_regs_handler_ctx ctx;
32 static struct intel_pmic_opregion *intel_pmic_opregion;
34 static int pmic_get_reg_bit(int address, struct pmic_table *table,
35 int count, int *reg, int *bit)
39 for (i = 0; i < count; i++) {
40 if (table[i].address == address) {
50 static acpi_status intel_pmic_power_handler(u32 function,
51 acpi_physical_address address, u32 bits, u64 *value64,
52 void *handler_context, void *region_context)
54 struct intel_pmic_opregion *opregion = region_context;
55 struct regmap *regmap = opregion->regmap;
56 struct intel_pmic_opregion_data *d = opregion->data;
59 if (bits != 32 || !value64)
60 return AE_BAD_PARAMETER;
62 if (function == ACPI_WRITE && !(*value64 == 0 || *value64 == 1))
63 return AE_BAD_PARAMETER;
65 result = pmic_get_reg_bit(address, d->power_table,
66 d->power_table_count, ®, &bit);
67 if (result == -ENOENT)
68 return AE_BAD_PARAMETER;
70 mutex_lock(&opregion->lock);
72 result = function == ACPI_READ ?
73 d->get_power(regmap, reg, bit, value64) :
74 d->update_power(regmap, reg, bit, *value64 == 1);
76 mutex_unlock(&opregion->lock);
78 return result ? AE_ERROR : AE_OK;
81 static int pmic_read_temp(struct intel_pmic_opregion *opregion,
86 if (!opregion->data->get_raw_temp)
89 raw_temp = opregion->data->get_raw_temp(opregion->regmap, reg);
93 if (!opregion->lpat_table) {
98 temp = acpi_lpat_raw_to_temp(opregion->lpat_table, raw_temp);
106 static int pmic_thermal_temp(struct intel_pmic_opregion *opregion, int reg,
107 u32 function, u64 *value)
109 return function == ACPI_READ ?
110 pmic_read_temp(opregion, reg, value) : -EINVAL;
113 static int pmic_thermal_aux(struct intel_pmic_opregion *opregion, int reg,
114 u32 function, u64 *value)
118 if (function == ACPI_READ)
119 return pmic_read_temp(opregion, reg, value);
121 if (!opregion->data->update_aux)
124 if (opregion->lpat_table) {
125 raw_temp = acpi_lpat_temp_to_raw(opregion->lpat_table, *value);
132 return opregion->data->update_aux(opregion->regmap, reg, raw_temp);
135 static int pmic_thermal_pen(struct intel_pmic_opregion *opregion, int reg,
136 int bit, u32 function, u64 *value)
138 struct intel_pmic_opregion_data *d = opregion->data;
139 struct regmap *regmap = opregion->regmap;
141 if (!d->get_policy || !d->update_policy)
144 if (function == ACPI_READ)
145 return d->get_policy(regmap, reg, bit, value);
147 if (*value != 0 && *value != 1)
150 return d->update_policy(regmap, reg, bit, *value);
153 static bool pmic_thermal_is_temp(int address)
155 return (address <= 0x3c) && !(address % 12);
158 static bool pmic_thermal_is_aux(int address)
160 return (address >= 4 && address <= 0x40 && !((address - 4) % 12)) ||
161 (address >= 8 && address <= 0x44 && !((address - 8) % 12));
164 static bool pmic_thermal_is_pen(int address)
166 return address >= 0x48 && address <= 0x5c;
169 static acpi_status intel_pmic_thermal_handler(u32 function,
170 acpi_physical_address address, u32 bits, u64 *value64,
171 void *handler_context, void *region_context)
173 struct intel_pmic_opregion *opregion = region_context;
174 struct intel_pmic_opregion_data *d = opregion->data;
175 int reg, bit, result;
177 if (bits != 32 || !value64)
178 return AE_BAD_PARAMETER;
180 result = pmic_get_reg_bit(address, d->thermal_table,
181 d->thermal_table_count, ®, &bit);
182 if (result == -ENOENT)
183 return AE_BAD_PARAMETER;
185 mutex_lock(&opregion->lock);
187 if (pmic_thermal_is_temp(address))
188 result = pmic_thermal_temp(opregion, reg, function, value64);
189 else if (pmic_thermal_is_aux(address))
190 result = pmic_thermal_aux(opregion, reg, function, value64);
191 else if (pmic_thermal_is_pen(address))
192 result = pmic_thermal_pen(opregion, reg, bit,
197 mutex_unlock(&opregion->lock);
200 if (result == -EINVAL)
201 return AE_BAD_PARAMETER;
209 static acpi_status intel_pmic_regs_handler(u32 function,
210 acpi_physical_address address, u32 bits, u64 *value64,
211 void *handler_context, void *region_context)
213 struct intel_pmic_opregion *opregion = region_context;
214 int result = -EINVAL;
216 if (function == ACPI_WRITE) {
221 opregion->ctx.addr |= (*value64 & 0xff) << 8;
224 opregion->ctx.addr |= *value64 & 0xff;
227 opregion->ctx.val = *value64 & 0xff;
231 result = regmap_write(opregion->regmap, opregion->ctx.addr,
234 result = regmap_read(opregion->regmap, opregion->ctx.addr,
237 opregion->ctx.addr = 0;
241 if (function == ACPI_READ && address == 3) {
242 *value64 = opregion->ctx.val;
247 if (result == -EINVAL)
248 return AE_BAD_PARAMETER;
256 int intel_pmic_install_opregion_handler(struct device *dev, acpi_handle handle,
257 struct regmap *regmap,
258 struct intel_pmic_opregion_data *d)
260 acpi_status status = AE_OK;
261 struct intel_pmic_opregion *opregion;
264 if (!dev || !regmap || !d)
270 opregion = devm_kzalloc(dev, sizeof(*opregion), GFP_KERNEL);
274 mutex_init(&opregion->lock);
275 opregion->regmap = regmap;
276 opregion->lpat_table = acpi_lpat_get_conversion_table(handle);
278 if (d->power_table_count)
279 status = acpi_install_address_space_handler(handle,
280 PMIC_POWER_OPREGION_ID,
281 intel_pmic_power_handler,
283 if (ACPI_FAILURE(status)) {
288 if (d->thermal_table_count)
289 status = acpi_install_address_space_handler(handle,
290 PMIC_THERMAL_OPREGION_ID,
291 intel_pmic_thermal_handler,
293 if (ACPI_FAILURE(status)) {
295 goto out_remove_power_handler;
298 status = acpi_install_address_space_handler(handle,
299 PMIC_REGS_OPREGION_ID, intel_pmic_regs_handler, NULL,
301 if (ACPI_FAILURE(status)) {
303 goto out_remove_thermal_handler;
307 intel_pmic_opregion = opregion;
310 out_remove_thermal_handler:
311 if (d->thermal_table_count)
312 acpi_remove_address_space_handler(handle,
313 PMIC_THERMAL_OPREGION_ID,
314 intel_pmic_thermal_handler);
316 out_remove_power_handler:
317 if (d->power_table_count)
318 acpi_remove_address_space_handler(handle,
319 PMIC_POWER_OPREGION_ID,
320 intel_pmic_power_handler);
323 acpi_lpat_free_conversion_table(opregion->lpat_table);
326 EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler);
329 * intel_soc_pmic_exec_mipi_pmic_seq_element - Execute PMIC MIPI sequence
330 * @i2c_address: I2C client address for the PMIC
331 * @reg_address: PMIC register address
332 * @value: New value for the register bits to change
333 * @mask: Mask indicating which register bits to change
335 * DSI LCD panels describe an initialization sequence in the i915 VBT (Video
336 * BIOS Tables) using so called MIPI sequences. One possible element in these
337 * sequences is a PMIC specific element of 15 bytes.
339 * This function executes these PMIC specific elements sending the embedded
340 * commands to the PMIC.
342 * Return 0 on success, < 0 on failure.
344 int intel_soc_pmic_exec_mipi_pmic_seq_element(u16 i2c_address, u32 reg_address,
347 struct intel_pmic_opregion_data *d;
350 if (!intel_pmic_opregion) {
351 pr_warn("%s: No PMIC registered\n", __func__);
355 d = intel_pmic_opregion->data;
357 mutex_lock(&intel_pmic_opregion->lock);
359 if (d->exec_mipi_pmic_seq_element) {
360 ret = d->exec_mipi_pmic_seq_element(intel_pmic_opregion->regmap,
361 i2c_address, reg_address,
363 } else if (d->pmic_i2c_address) {
364 if (i2c_address == d->pmic_i2c_address) {
365 ret = regmap_update_bits(intel_pmic_opregion->regmap,
366 reg_address, mask, value);
368 pr_err("%s: Unexpected i2c-addr: 0x%02x (reg-addr 0x%x value 0x%x mask 0x%x)\n",
369 __func__, i2c_address, reg_address, value, mask);
373 pr_warn("%s: Not implemented\n", __func__);
374 pr_warn("%s: i2c-addr: 0x%x reg-addr 0x%x value 0x%x mask 0x%x\n",
375 __func__, i2c_address, reg_address, value, mask);
379 mutex_unlock(&intel_pmic_opregion->lock);
383 EXPORT_SYMBOL_GPL(intel_soc_pmic_exec_mipi_pmic_seq_element);