1b1eea857SAaron Lu /* 2b1eea857SAaron Lu * intel_pmic.c - Intel PMIC operation region driver 3b1eea857SAaron Lu * 4b1eea857SAaron Lu * Copyright (C) 2014 Intel Corporation. All rights reserved. 5b1eea857SAaron Lu * 6b1eea857SAaron Lu * This program is free software; you can redistribute it and/or 7b1eea857SAaron Lu * modify it under the terms of the GNU General Public License version 8b1eea857SAaron Lu * 2 as published by the Free Software Foundation. 9b1eea857SAaron Lu * 10b1eea857SAaron Lu * This program is distributed in the hope that it will be useful, 11b1eea857SAaron Lu * but WITHOUT ANY WARRANTY; without even the implied warranty of 12b1eea857SAaron Lu * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13b1eea857SAaron Lu * GNU General Public License for more details. 14b1eea857SAaron Lu */ 15b1eea857SAaron Lu 166d3ef8d8SPaul Gortmaker #include <linux/export.h> 17b1eea857SAaron Lu #include <linux/acpi.h> 18b1eea857SAaron Lu #include <linux/regmap.h> 19ac586e2dSSrinivas Pandruvada #include <acpi/acpi_lpat.h> 20b1eea857SAaron Lu #include "intel_pmic.h" 21b1eea857SAaron Lu 22b1eea857SAaron Lu #define PMIC_POWER_OPREGION_ID 0x8d 23b1eea857SAaron Lu #define PMIC_THERMAL_OPREGION_ID 0x8c 240afa877aSFelipe Balbi #define PMIC_REGS_OPREGION_ID 0x8f 250afa877aSFelipe Balbi 260afa877aSFelipe Balbi struct intel_pmic_regs_handler_ctx { 270afa877aSFelipe Balbi unsigned int val; 280afa877aSFelipe Balbi u16 addr; 290afa877aSFelipe Balbi }; 30b1eea857SAaron Lu 31b1eea857SAaron Lu struct intel_pmic_opregion { 32b1eea857SAaron Lu struct mutex lock; 33ac586e2dSSrinivas Pandruvada struct acpi_lpat_conversion_table *lpat_table; 34b1eea857SAaron Lu struct regmap *regmap; 35b1eea857SAaron Lu struct intel_pmic_opregion_data *data; 360afa877aSFelipe Balbi struct intel_pmic_regs_handler_ctx ctx; 37b1eea857SAaron Lu }; 38b1eea857SAaron Lu 39b1eea857SAaron Lu static int pmic_get_reg_bit(int address, struct pmic_table *table, 40b1eea857SAaron Lu int count, int *reg, int *bit) 41b1eea857SAaron Lu { 42b1eea857SAaron Lu int i; 43b1eea857SAaron Lu 44b1eea857SAaron Lu for (i = 0; i < count; i++) { 45b1eea857SAaron Lu if (table[i].address == address) { 46b1eea857SAaron Lu *reg = table[i].reg; 47b1eea857SAaron Lu if (bit) 48b1eea857SAaron Lu *bit = table[i].bit; 49b1eea857SAaron Lu return 0; 50b1eea857SAaron Lu } 51b1eea857SAaron Lu } 52b1eea857SAaron Lu return -ENOENT; 53b1eea857SAaron Lu } 54b1eea857SAaron Lu 55b1eea857SAaron Lu static acpi_status intel_pmic_power_handler(u32 function, 56b1eea857SAaron Lu acpi_physical_address address, u32 bits, u64 *value64, 57b1eea857SAaron Lu void *handler_context, void *region_context) 58b1eea857SAaron Lu { 59b1eea857SAaron Lu struct intel_pmic_opregion *opregion = region_context; 60b1eea857SAaron Lu struct regmap *regmap = opregion->regmap; 61b1eea857SAaron Lu struct intel_pmic_opregion_data *d = opregion->data; 62b1eea857SAaron Lu int reg, bit, result; 63b1eea857SAaron Lu 64b1eea857SAaron Lu if (bits != 32 || !value64) 65b1eea857SAaron Lu return AE_BAD_PARAMETER; 66b1eea857SAaron Lu 67b1eea857SAaron Lu if (function == ACPI_WRITE && !(*value64 == 0 || *value64 == 1)) 68b1eea857SAaron Lu return AE_BAD_PARAMETER; 69b1eea857SAaron Lu 70b1eea857SAaron Lu result = pmic_get_reg_bit(address, d->power_table, 71b1eea857SAaron Lu d->power_table_count, ®, &bit); 72b1eea857SAaron Lu if (result == -ENOENT) 73b1eea857SAaron Lu return AE_BAD_PARAMETER; 74b1eea857SAaron Lu 75b1eea857SAaron Lu mutex_lock(&opregion->lock); 76b1eea857SAaron Lu 77b1eea857SAaron Lu result = function == ACPI_READ ? 78b1eea857SAaron Lu d->get_power(regmap, reg, bit, value64) : 79b1eea857SAaron Lu d->update_power(regmap, reg, bit, *value64 == 1); 80b1eea857SAaron Lu 81b1eea857SAaron Lu mutex_unlock(&opregion->lock); 82b1eea857SAaron Lu 83b1eea857SAaron Lu return result ? AE_ERROR : AE_OK; 84b1eea857SAaron Lu } 85b1eea857SAaron Lu 86b1eea857SAaron Lu static int pmic_read_temp(struct intel_pmic_opregion *opregion, 87b1eea857SAaron Lu int reg, u64 *value) 88b1eea857SAaron Lu { 89b1eea857SAaron Lu int raw_temp, temp; 90b1eea857SAaron Lu 91b1eea857SAaron Lu if (!opregion->data->get_raw_temp) 92b1eea857SAaron Lu return -ENXIO; 93b1eea857SAaron Lu 94b1eea857SAaron Lu raw_temp = opregion->data->get_raw_temp(opregion->regmap, reg); 95b1eea857SAaron Lu if (raw_temp < 0) 96b1eea857SAaron Lu return raw_temp; 97b1eea857SAaron Lu 98ac586e2dSSrinivas Pandruvada if (!opregion->lpat_table) { 99b1eea857SAaron Lu *value = raw_temp; 100b1eea857SAaron Lu return 0; 101b1eea857SAaron Lu } 102b1eea857SAaron Lu 103ac586e2dSSrinivas Pandruvada temp = acpi_lpat_raw_to_temp(opregion->lpat_table, raw_temp); 104b1eea857SAaron Lu if (temp < 0) 105b1eea857SAaron Lu return temp; 106b1eea857SAaron Lu 107b1eea857SAaron Lu *value = temp; 108b1eea857SAaron Lu return 0; 109b1eea857SAaron Lu } 110b1eea857SAaron Lu 111b1eea857SAaron Lu static int pmic_thermal_temp(struct intel_pmic_opregion *opregion, int reg, 112b1eea857SAaron Lu u32 function, u64 *value) 113b1eea857SAaron Lu { 114b1eea857SAaron Lu return function == ACPI_READ ? 115b1eea857SAaron Lu pmic_read_temp(opregion, reg, value) : -EINVAL; 116b1eea857SAaron Lu } 117b1eea857SAaron Lu 118b1eea857SAaron Lu static int pmic_thermal_aux(struct intel_pmic_opregion *opregion, int reg, 119b1eea857SAaron Lu u32 function, u64 *value) 120b1eea857SAaron Lu { 121b1eea857SAaron Lu int raw_temp; 122b1eea857SAaron Lu 123b1eea857SAaron Lu if (function == ACPI_READ) 124b1eea857SAaron Lu return pmic_read_temp(opregion, reg, value); 125b1eea857SAaron Lu 126b1eea857SAaron Lu if (!opregion->data->update_aux) 127b1eea857SAaron Lu return -ENXIO; 128b1eea857SAaron Lu 129ac586e2dSSrinivas Pandruvada if (opregion->lpat_table) { 130ac586e2dSSrinivas Pandruvada raw_temp = acpi_lpat_temp_to_raw(opregion->lpat_table, *value); 131b1eea857SAaron Lu if (raw_temp < 0) 132b1eea857SAaron Lu return raw_temp; 133b1eea857SAaron Lu } else { 134b1eea857SAaron Lu raw_temp = *value; 135b1eea857SAaron Lu } 136b1eea857SAaron Lu 137b1eea857SAaron Lu return opregion->data->update_aux(opregion->regmap, reg, raw_temp); 138b1eea857SAaron Lu } 139b1eea857SAaron Lu 140b1eea857SAaron Lu static int pmic_thermal_pen(struct intel_pmic_opregion *opregion, int reg, 141d8ba8191SBin Gao int bit, u32 function, u64 *value) 142b1eea857SAaron Lu { 143b1eea857SAaron Lu struct intel_pmic_opregion_data *d = opregion->data; 144b1eea857SAaron Lu struct regmap *regmap = opregion->regmap; 145b1eea857SAaron Lu 146b1eea857SAaron Lu if (!d->get_policy || !d->update_policy) 147b1eea857SAaron Lu return -ENXIO; 148b1eea857SAaron Lu 149b1eea857SAaron Lu if (function == ACPI_READ) 150d8ba8191SBin Gao return d->get_policy(regmap, reg, bit, value); 151b1eea857SAaron Lu 152b1eea857SAaron Lu if (*value != 0 && *value != 1) 153b1eea857SAaron Lu return -EINVAL; 154b1eea857SAaron Lu 155d8ba8191SBin Gao return d->update_policy(regmap, reg, bit, *value); 156b1eea857SAaron Lu } 157b1eea857SAaron Lu 158b1eea857SAaron Lu static bool pmic_thermal_is_temp(int address) 159b1eea857SAaron Lu { 160b1eea857SAaron Lu return (address <= 0x3c) && !(address % 12); 161b1eea857SAaron Lu } 162b1eea857SAaron Lu 163b1eea857SAaron Lu static bool pmic_thermal_is_aux(int address) 164b1eea857SAaron Lu { 165b1eea857SAaron Lu return (address >= 4 && address <= 0x40 && !((address - 4) % 12)) || 166b1eea857SAaron Lu (address >= 8 && address <= 0x44 && !((address - 8) % 12)); 167b1eea857SAaron Lu } 168b1eea857SAaron Lu 169b1eea857SAaron Lu static bool pmic_thermal_is_pen(int address) 170b1eea857SAaron Lu { 171b1eea857SAaron Lu return address >= 0x48 && address <= 0x5c; 172b1eea857SAaron Lu } 173b1eea857SAaron Lu 174b1eea857SAaron Lu static acpi_status intel_pmic_thermal_handler(u32 function, 175b1eea857SAaron Lu acpi_physical_address address, u32 bits, u64 *value64, 176b1eea857SAaron Lu void *handler_context, void *region_context) 177b1eea857SAaron Lu { 178b1eea857SAaron Lu struct intel_pmic_opregion *opregion = region_context; 179b1eea857SAaron Lu struct intel_pmic_opregion_data *d = opregion->data; 180d8ba8191SBin Gao int reg, bit, result; 181b1eea857SAaron Lu 182b1eea857SAaron Lu if (bits != 32 || !value64) 183b1eea857SAaron Lu return AE_BAD_PARAMETER; 184b1eea857SAaron Lu 185b1eea857SAaron Lu result = pmic_get_reg_bit(address, d->thermal_table, 186d8ba8191SBin Gao d->thermal_table_count, ®, &bit); 187b1eea857SAaron Lu if (result == -ENOENT) 188b1eea857SAaron Lu return AE_BAD_PARAMETER; 189b1eea857SAaron Lu 190b1eea857SAaron Lu mutex_lock(&opregion->lock); 191b1eea857SAaron Lu 192b1eea857SAaron Lu if (pmic_thermal_is_temp(address)) 193b1eea857SAaron Lu result = pmic_thermal_temp(opregion, reg, function, value64); 194b1eea857SAaron Lu else if (pmic_thermal_is_aux(address)) 195b1eea857SAaron Lu result = pmic_thermal_aux(opregion, reg, function, value64); 196b1eea857SAaron Lu else if (pmic_thermal_is_pen(address)) 197d8ba8191SBin Gao result = pmic_thermal_pen(opregion, reg, bit, 198d8ba8191SBin Gao function, value64); 199b1eea857SAaron Lu else 200b1eea857SAaron Lu result = -EINVAL; 201b1eea857SAaron Lu 202b1eea857SAaron Lu mutex_unlock(&opregion->lock); 203b1eea857SAaron Lu 204b1eea857SAaron Lu if (result < 0) { 205b1eea857SAaron Lu if (result == -EINVAL) 206b1eea857SAaron Lu return AE_BAD_PARAMETER; 207b1eea857SAaron Lu else 208b1eea857SAaron Lu return AE_ERROR; 209b1eea857SAaron Lu } 210b1eea857SAaron Lu 211b1eea857SAaron Lu return AE_OK; 212b1eea857SAaron Lu } 213b1eea857SAaron Lu 2140afa877aSFelipe Balbi static acpi_status intel_pmic_regs_handler(u32 function, 2150afa877aSFelipe Balbi acpi_physical_address address, u32 bits, u64 *value64, 2160afa877aSFelipe Balbi void *handler_context, void *region_context) 2170afa877aSFelipe Balbi { 2180afa877aSFelipe Balbi struct intel_pmic_opregion *opregion = region_context; 219730de199SFelipe Balbi int result = 0; 2200afa877aSFelipe Balbi 2210afa877aSFelipe Balbi switch (address) { 2220afa877aSFelipe Balbi case 0: 2230afa877aSFelipe Balbi return AE_OK; 2240afa877aSFelipe Balbi case 1: 2250afa877aSFelipe Balbi opregion->ctx.addr |= (*value64 & 0xff) << 8; 2260afa877aSFelipe Balbi return AE_OK; 2270afa877aSFelipe Balbi case 2: 2280afa877aSFelipe Balbi opregion->ctx.addr |= *value64 & 0xff; 2290afa877aSFelipe Balbi return AE_OK; 2300afa877aSFelipe Balbi case 3: 2310afa877aSFelipe Balbi opregion->ctx.val = *value64 & 0xff; 2320afa877aSFelipe Balbi return AE_OK; 2330afa877aSFelipe Balbi case 4: 2340afa877aSFelipe Balbi if (*value64) { 2350afa877aSFelipe Balbi result = regmap_write(opregion->regmap, opregion->ctx.addr, 2360afa877aSFelipe Balbi opregion->ctx.val); 2370afa877aSFelipe Balbi } else { 2380afa877aSFelipe Balbi result = regmap_read(opregion->regmap, opregion->ctx.addr, 2390afa877aSFelipe Balbi &opregion->ctx.val); 2400afa877aSFelipe Balbi if (result == 0) 2410afa877aSFelipe Balbi *value64 = opregion->ctx.val; 2420afa877aSFelipe Balbi } 2430afa877aSFelipe Balbi memset(&opregion->ctx, 0x00, sizeof(opregion->ctx)); 2440afa877aSFelipe Balbi } 2450afa877aSFelipe Balbi 2460afa877aSFelipe Balbi if (result < 0) { 2470afa877aSFelipe Balbi if (result == -EINVAL) 2480afa877aSFelipe Balbi return AE_BAD_PARAMETER; 2490afa877aSFelipe Balbi else 2500afa877aSFelipe Balbi return AE_ERROR; 2510afa877aSFelipe Balbi } 2520afa877aSFelipe Balbi 2530afa877aSFelipe Balbi return AE_OK; 2540afa877aSFelipe Balbi } 2550afa877aSFelipe Balbi 256b1eea857SAaron Lu int intel_pmic_install_opregion_handler(struct device *dev, acpi_handle handle, 257b1eea857SAaron Lu struct regmap *regmap, 258b1eea857SAaron Lu struct intel_pmic_opregion_data *d) 259b1eea857SAaron Lu { 260b1eea857SAaron Lu acpi_status status; 261b1eea857SAaron Lu struct intel_pmic_opregion *opregion; 262ac586e2dSSrinivas Pandruvada int ret; 263b1eea857SAaron Lu 264b1eea857SAaron Lu if (!dev || !regmap || !d) 265b1eea857SAaron Lu return -EINVAL; 266b1eea857SAaron Lu 267b1eea857SAaron Lu if (!handle) 268b1eea857SAaron Lu return -ENODEV; 269b1eea857SAaron Lu 270b1eea857SAaron Lu opregion = devm_kzalloc(dev, sizeof(*opregion), GFP_KERNEL); 271b1eea857SAaron Lu if (!opregion) 272b1eea857SAaron Lu return -ENOMEM; 273b1eea857SAaron Lu 274b1eea857SAaron Lu mutex_init(&opregion->lock); 275b1eea857SAaron Lu opregion->regmap = regmap; 276ac586e2dSSrinivas Pandruvada opregion->lpat_table = acpi_lpat_get_conversion_table(handle); 277b1eea857SAaron Lu 278b1eea857SAaron Lu status = acpi_install_address_space_handler(handle, 279b1eea857SAaron Lu PMIC_POWER_OPREGION_ID, 280b1eea857SAaron Lu intel_pmic_power_handler, 281b1eea857SAaron Lu NULL, opregion); 282ac586e2dSSrinivas Pandruvada if (ACPI_FAILURE(status)) { 283ac586e2dSSrinivas Pandruvada ret = -ENODEV; 284ac586e2dSSrinivas Pandruvada goto out_error; 285ac586e2dSSrinivas Pandruvada } 286b1eea857SAaron Lu 287b1eea857SAaron Lu status = acpi_install_address_space_handler(handle, 288b1eea857SAaron Lu PMIC_THERMAL_OPREGION_ID, 289b1eea857SAaron Lu intel_pmic_thermal_handler, 290b1eea857SAaron Lu NULL, opregion); 291b1eea857SAaron Lu if (ACPI_FAILURE(status)) { 292b1eea857SAaron Lu acpi_remove_address_space_handler(handle, PMIC_POWER_OPREGION_ID, 293b1eea857SAaron Lu intel_pmic_power_handler); 294ac586e2dSSrinivas Pandruvada ret = -ENODEV; 2950afa877aSFelipe Balbi goto out_remove_power_handler; 2960afa877aSFelipe Balbi } 2970afa877aSFelipe Balbi 2980afa877aSFelipe Balbi status = acpi_install_address_space_handler(handle, 2990afa877aSFelipe Balbi PMIC_REGS_OPREGION_ID, intel_pmic_regs_handler, NULL, 3000afa877aSFelipe Balbi opregion); 3010afa877aSFelipe Balbi if (ACPI_FAILURE(status)) { 3020afa877aSFelipe Balbi ret = -ENODEV; 3030afa877aSFelipe Balbi goto out_remove_thermal_handler; 304b1eea857SAaron Lu } 305b1eea857SAaron Lu 306b1eea857SAaron Lu opregion->data = d; 307b1eea857SAaron Lu return 0; 308ac586e2dSSrinivas Pandruvada 3090afa877aSFelipe Balbi out_remove_thermal_handler: 3100afa877aSFelipe Balbi acpi_remove_address_space_handler(handle, PMIC_THERMAL_OPREGION_ID, 3110afa877aSFelipe Balbi intel_pmic_thermal_handler); 3120afa877aSFelipe Balbi 3130afa877aSFelipe Balbi out_remove_power_handler: 3140afa877aSFelipe Balbi acpi_remove_address_space_handler(handle, PMIC_POWER_OPREGION_ID, 3150afa877aSFelipe Balbi intel_pmic_power_handler); 3160afa877aSFelipe Balbi 317ac586e2dSSrinivas Pandruvada out_error: 318ac586e2dSSrinivas Pandruvada acpi_lpat_free_conversion_table(opregion->lpat_table); 319ac586e2dSSrinivas Pandruvada return ret; 320b1eea857SAaron Lu } 321b1eea857SAaron Lu EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler); 322