Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * intel_pmic.c - Intel PMIC operation region driver
0004  *
0005  * Copyright (C) 2014 Intel Corporation. All rights reserved.
0006  */
0007 
0008 #include <linux/export.h>
0009 #include <linux/acpi.h>
0010 #include <linux/mfd/intel_soc_pmic.h>
0011 #include <linux/regmap.h>
0012 #include <acpi/acpi_lpat.h>
0013 #include "intel_pmic.h"
0014 
0015 #define PMIC_POWER_OPREGION_ID      0x8d
0016 #define PMIC_THERMAL_OPREGION_ID    0x8c
0017 #define PMIC_REGS_OPREGION_ID       0x8f
0018 
0019 struct intel_pmic_regs_handler_ctx {
0020     unsigned int val;
0021     u16 addr;
0022 };
0023 
0024 struct intel_pmic_opregion {
0025     struct mutex lock;
0026     struct acpi_lpat_conversion_table *lpat_table;
0027     struct regmap *regmap;
0028     const struct intel_pmic_opregion_data *data;
0029     struct intel_pmic_regs_handler_ctx ctx;
0030 };
0031 
0032 static struct intel_pmic_opregion *intel_pmic_opregion;
0033 
0034 static int pmic_get_reg_bit(int address, struct pmic_table *table,
0035                 int count, int *reg, int *bit)
0036 {
0037     int i;
0038 
0039     for (i = 0; i < count; i++) {
0040         if (table[i].address == address) {
0041             *reg = table[i].reg;
0042             if (bit)
0043                 *bit = table[i].bit;
0044             return 0;
0045         }
0046     }
0047     return -ENOENT;
0048 }
0049 
0050 static acpi_status intel_pmic_power_handler(u32 function,
0051         acpi_physical_address address, u32 bits, u64 *value64,
0052         void *handler_context, void *region_context)
0053 {
0054     struct intel_pmic_opregion *opregion = region_context;
0055     struct regmap *regmap = opregion->regmap;
0056     const struct intel_pmic_opregion_data *d = opregion->data;
0057     int reg, bit, result;
0058 
0059     if (bits != 32 || !value64)
0060         return AE_BAD_PARAMETER;
0061 
0062     if (function == ACPI_WRITE && !(*value64 == 0 || *value64 == 1))
0063         return AE_BAD_PARAMETER;
0064 
0065     result = pmic_get_reg_bit(address, d->power_table,
0066                   d->power_table_count, &reg, &bit);
0067     if (result == -ENOENT)
0068         return AE_BAD_PARAMETER;
0069 
0070     mutex_lock(&opregion->lock);
0071 
0072     result = function == ACPI_READ ?
0073         d->get_power(regmap, reg, bit, value64) :
0074         d->update_power(regmap, reg, bit, *value64 == 1);
0075 
0076     mutex_unlock(&opregion->lock);
0077 
0078     return result ? AE_ERROR : AE_OK;
0079 }
0080 
0081 static int pmic_read_temp(struct intel_pmic_opregion *opregion,
0082               int reg, u64 *value)
0083 {
0084     int raw_temp, temp;
0085 
0086     if (!opregion->data->get_raw_temp)
0087         return -ENXIO;
0088 
0089     raw_temp = opregion->data->get_raw_temp(opregion->regmap, reg);
0090     if (raw_temp < 0)
0091         return raw_temp;
0092 
0093     if (!opregion->lpat_table) {
0094         *value = raw_temp;
0095         return 0;
0096     }
0097 
0098     temp = opregion->data->lpat_raw_to_temp(opregion->lpat_table, raw_temp);
0099     if (temp < 0)
0100         return temp;
0101 
0102     *value = temp;
0103     return 0;
0104 }
0105 
0106 static int pmic_thermal_temp(struct intel_pmic_opregion *opregion, int reg,
0107                  u32 function, u64 *value)
0108 {
0109     return function == ACPI_READ ?
0110         pmic_read_temp(opregion, reg, value) : -EINVAL;
0111 }
0112 
0113 static int pmic_thermal_aux(struct intel_pmic_opregion *opregion, int reg,
0114                 u32 function, u64 *value)
0115 {
0116     int raw_temp;
0117 
0118     if (function == ACPI_READ)
0119         return pmic_read_temp(opregion, reg, value);
0120 
0121     if (!opregion->data->update_aux)
0122         return -ENXIO;
0123 
0124     if (opregion->lpat_table) {
0125         raw_temp = acpi_lpat_temp_to_raw(opregion->lpat_table, *value);
0126         if (raw_temp < 0)
0127             return raw_temp;
0128     } else {
0129         raw_temp = *value;
0130     }
0131 
0132     return opregion->data->update_aux(opregion->regmap, reg, raw_temp);
0133 }
0134 
0135 static int pmic_thermal_pen(struct intel_pmic_opregion *opregion, int reg,
0136                 int bit, u32 function, u64 *value)
0137 {
0138     const struct intel_pmic_opregion_data *d = opregion->data;
0139     struct regmap *regmap = opregion->regmap;
0140 
0141     if (!d->get_policy || !d->update_policy)
0142         return -ENXIO;
0143 
0144     if (function == ACPI_READ)
0145         return d->get_policy(regmap, reg, bit, value);
0146 
0147     if (*value != 0 && *value != 1)
0148         return -EINVAL;
0149 
0150     return d->update_policy(regmap, reg, bit, *value);
0151 }
0152 
0153 static bool pmic_thermal_is_temp(int address)
0154 {
0155     return (address <= 0x3c) && !(address % 12);
0156 }
0157 
0158 static bool pmic_thermal_is_aux(int address)
0159 {
0160     return (address >= 4 && address <= 0x40 && !((address - 4) % 12)) ||
0161            (address >= 8 && address <= 0x44 && !((address - 8) % 12));
0162 }
0163 
0164 static bool pmic_thermal_is_pen(int address)
0165 {
0166     return address >= 0x48 && address <= 0x5c;
0167 }
0168 
0169 static acpi_status intel_pmic_thermal_handler(u32 function,
0170         acpi_physical_address address, u32 bits, u64 *value64,
0171         void *handler_context, void *region_context)
0172 {
0173     struct intel_pmic_opregion *opregion = region_context;
0174     const struct intel_pmic_opregion_data *d = opregion->data;
0175     int reg, bit, result;
0176 
0177     if (bits != 32 || !value64)
0178         return AE_BAD_PARAMETER;
0179 
0180     result = pmic_get_reg_bit(address, d->thermal_table,
0181                   d->thermal_table_count, &reg, &bit);
0182     if (result == -ENOENT)
0183         return AE_BAD_PARAMETER;
0184 
0185     mutex_lock(&opregion->lock);
0186 
0187     if (pmic_thermal_is_temp(address))
0188         result = pmic_thermal_temp(opregion, reg, function, value64);
0189     else if (pmic_thermal_is_aux(address))
0190         result = pmic_thermal_aux(opregion, reg, function, value64);
0191     else if (pmic_thermal_is_pen(address))
0192         result = pmic_thermal_pen(opregion, reg, bit,
0193                         function, value64);
0194     else
0195         result = -EINVAL;
0196 
0197     mutex_unlock(&opregion->lock);
0198 
0199     if (result < 0) {
0200         if (result == -EINVAL)
0201             return AE_BAD_PARAMETER;
0202         else
0203             return AE_ERROR;
0204     }
0205 
0206     return AE_OK;
0207 }
0208 
0209 static acpi_status intel_pmic_regs_handler(u32 function,
0210         acpi_physical_address address, u32 bits, u64 *value64,
0211         void *handler_context, void *region_context)
0212 {
0213     struct intel_pmic_opregion *opregion = region_context;
0214     int result = -EINVAL;
0215 
0216     if (function == ACPI_WRITE) {
0217         switch (address) {
0218         case 0:
0219             return AE_OK;
0220         case 1:
0221             opregion->ctx.addr |= (*value64 & 0xff) << 8;
0222             return AE_OK;
0223         case 2:
0224             opregion->ctx.addr |= *value64 & 0xff;
0225             return AE_OK;
0226         case 3:
0227             opregion->ctx.val = *value64 & 0xff;
0228             return AE_OK;
0229         case 4:
0230             if (*value64) {
0231                 result = regmap_write(opregion->regmap, opregion->ctx.addr,
0232                               opregion->ctx.val);
0233             } else {
0234                 result = regmap_read(opregion->regmap, opregion->ctx.addr,
0235                              &opregion->ctx.val);
0236             }
0237             opregion->ctx.addr = 0;
0238         }
0239     }
0240 
0241     if (function == ACPI_READ && address == 3) {
0242         *value64 = opregion->ctx.val;
0243         return AE_OK;
0244     }
0245 
0246     if (result < 0) {
0247         if (result == -EINVAL)
0248             return AE_BAD_PARAMETER;
0249         else
0250             return AE_ERROR;
0251     }
0252 
0253     return AE_OK;
0254 }
0255 
0256 int intel_pmic_install_opregion_handler(struct device *dev, acpi_handle handle,
0257                     struct regmap *regmap,
0258                     const struct intel_pmic_opregion_data *d)
0259 {
0260     acpi_status status = AE_OK;
0261     struct intel_pmic_opregion *opregion;
0262     int ret;
0263 
0264     if (!dev || !regmap || !d)
0265         return -EINVAL;
0266 
0267     if (!handle)
0268         return -ENODEV;
0269 
0270     opregion = devm_kzalloc(dev, sizeof(*opregion), GFP_KERNEL);
0271     if (!opregion)
0272         return -ENOMEM;
0273 
0274     mutex_init(&opregion->lock);
0275     opregion->regmap = regmap;
0276     opregion->lpat_table = acpi_lpat_get_conversion_table(handle);
0277 
0278     if (d->power_table_count)
0279         status = acpi_install_address_space_handler(handle,
0280                             PMIC_POWER_OPREGION_ID,
0281                             intel_pmic_power_handler,
0282                             NULL, opregion);
0283     if (ACPI_FAILURE(status)) {
0284         ret = -ENODEV;
0285         goto out_error;
0286     }
0287 
0288     if (d->thermal_table_count)
0289         status = acpi_install_address_space_handler(handle,
0290                             PMIC_THERMAL_OPREGION_ID,
0291                             intel_pmic_thermal_handler,
0292                             NULL, opregion);
0293     if (ACPI_FAILURE(status)) {
0294         ret = -ENODEV;
0295         goto out_remove_power_handler;
0296     }
0297 
0298     status = acpi_install_address_space_handler(handle,
0299             PMIC_REGS_OPREGION_ID, intel_pmic_regs_handler, NULL,
0300             opregion);
0301     if (ACPI_FAILURE(status)) {
0302         ret = -ENODEV;
0303         goto out_remove_thermal_handler;
0304     }
0305 
0306     opregion->data = d;
0307     intel_pmic_opregion = opregion;
0308     return 0;
0309 
0310 out_remove_thermal_handler:
0311     if (d->thermal_table_count)
0312         acpi_remove_address_space_handler(handle,
0313                           PMIC_THERMAL_OPREGION_ID,
0314                           intel_pmic_thermal_handler);
0315 
0316 out_remove_power_handler:
0317     if (d->power_table_count)
0318         acpi_remove_address_space_handler(handle,
0319                           PMIC_POWER_OPREGION_ID,
0320                           intel_pmic_power_handler);
0321 
0322 out_error:
0323     acpi_lpat_free_conversion_table(opregion->lpat_table);
0324     return ret;
0325 }
0326 EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler);
0327 
0328 /**
0329  * intel_soc_pmic_exec_mipi_pmic_seq_element - Execute PMIC MIPI sequence
0330  * @i2c_address:  I2C client address for the PMIC
0331  * @reg_address:  PMIC register address
0332  * @value:        New value for the register bits to change
0333  * @mask:         Mask indicating which register bits to change
0334  *
0335  * DSI LCD panels describe an initialization sequence in the i915 VBT (Video
0336  * BIOS Tables) using so called MIPI sequences. One possible element in these
0337  * sequences is a PMIC specific element of 15 bytes.
0338  *
0339  * This function executes these PMIC specific elements sending the embedded
0340  * commands to the PMIC.
0341  *
0342  * Return 0 on success, < 0 on failure.
0343  */
0344 int intel_soc_pmic_exec_mipi_pmic_seq_element(u16 i2c_address, u32 reg_address,
0345                           u32 value, u32 mask)
0346 {
0347     const struct intel_pmic_opregion_data *d;
0348     int ret;
0349 
0350     if (!intel_pmic_opregion) {
0351         pr_warn("%s: No PMIC registered\n", __func__);
0352         return -ENXIO;
0353     }
0354 
0355     d = intel_pmic_opregion->data;
0356 
0357     mutex_lock(&intel_pmic_opregion->lock);
0358 
0359     if (d->exec_mipi_pmic_seq_element) {
0360         ret = d->exec_mipi_pmic_seq_element(intel_pmic_opregion->regmap,
0361                             i2c_address, reg_address,
0362                             value, mask);
0363     } else if (d->pmic_i2c_address) {
0364         if (i2c_address == d->pmic_i2c_address) {
0365             ret = regmap_update_bits(intel_pmic_opregion->regmap,
0366                          reg_address, mask, value);
0367         } else {
0368             pr_err("%s: Unexpected i2c-addr: 0x%02x (reg-addr 0x%x value 0x%x mask 0x%x)\n",
0369                    __func__, i2c_address, reg_address, value, mask);
0370             ret = -ENXIO;
0371         }
0372     } else {
0373         pr_warn("%s: Not implemented\n", __func__);
0374         pr_warn("%s: i2c-addr: 0x%x reg-addr 0x%x value 0x%x mask 0x%x\n",
0375             __func__, i2c_address, reg_address, value, mask);
0376         ret = -EOPNOTSUPP;
0377     }
0378 
0379     mutex_unlock(&intel_pmic_opregion->lock);
0380 
0381     return ret;
0382 }
0383 EXPORT_SYMBOL_GPL(intel_soc_pmic_exec_mipi_pmic_seq_element);