Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 1 | /* |
| 2 | * intel_pmic_xpower.c - XPower AXP288 PMIC operation region driver |
| 3 | * |
| 4 | * Copyright (C) 2014 Intel Corporation. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License version |
| 8 | * 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
Paul Gortmaker | 6d3ef8d | 2016-07-11 18:05:16 -0400 | [diff] [blame] | 16 | #include <linux/init.h> |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 17 | #include <linux/acpi.h> |
| 18 | #include <linux/mfd/axp20x.h> |
| 19 | #include <linux/regmap.h> |
| 20 | #include <linux/platform_device.h> |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 21 | #include "intel_pmic.h" |
| 22 | |
| 23 | #define XPOWER_GPADC_LOW 0x5b |
| 24 | |
| 25 | static struct pmic_table power_table[] = { |
| 26 | { |
| 27 | .address = 0x00, |
| 28 | .reg = 0x13, |
| 29 | .bit = 0x05, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 30 | }, /* ALD1 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 31 | { |
| 32 | .address = 0x04, |
| 33 | .reg = 0x13, |
| 34 | .bit = 0x06, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 35 | }, /* ALD2 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 36 | { |
| 37 | .address = 0x08, |
| 38 | .reg = 0x13, |
| 39 | .bit = 0x07, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 40 | }, /* ALD3 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 41 | { |
| 42 | .address = 0x0c, |
| 43 | .reg = 0x12, |
| 44 | .bit = 0x03, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 45 | }, /* DLD1 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 46 | { |
| 47 | .address = 0x10, |
| 48 | .reg = 0x12, |
| 49 | .bit = 0x04, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 50 | }, /* DLD2 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 51 | { |
| 52 | .address = 0x14, |
| 53 | .reg = 0x12, |
| 54 | .bit = 0x05, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 55 | }, /* DLD3 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 56 | { |
| 57 | .address = 0x18, |
| 58 | .reg = 0x12, |
| 59 | .bit = 0x06, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 60 | }, /* DLD4 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 61 | { |
| 62 | .address = 0x1c, |
| 63 | .reg = 0x12, |
| 64 | .bit = 0x00, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 65 | }, /* ELD1 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 66 | { |
| 67 | .address = 0x20, |
| 68 | .reg = 0x12, |
| 69 | .bit = 0x01, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 70 | }, /* ELD2 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 71 | { |
| 72 | .address = 0x24, |
| 73 | .reg = 0x12, |
| 74 | .bit = 0x02, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 75 | }, /* ELD3 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 76 | { |
| 77 | .address = 0x28, |
| 78 | .reg = 0x13, |
| 79 | .bit = 0x02, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 80 | }, /* FLD1 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 81 | { |
| 82 | .address = 0x2c, |
| 83 | .reg = 0x13, |
| 84 | .bit = 0x03, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 85 | }, /* FLD2 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 86 | { |
| 87 | .address = 0x30, |
| 88 | .reg = 0x13, |
| 89 | .bit = 0x04, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 90 | }, /* FLD3 */ |
| 91 | { |
| 92 | .address = 0x34, |
| 93 | .reg = 0x10, |
| 94 | .bit = 0x03, |
| 95 | }, /* BUC1 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 96 | { |
| 97 | .address = 0x38, |
| 98 | .reg = 0x10, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 99 | .bit = 0x06, |
| 100 | }, /* BUC2 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 101 | { |
| 102 | .address = 0x3c, |
| 103 | .reg = 0x10, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 104 | .bit = 0x05, |
| 105 | }, /* BUC3 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 106 | { |
| 107 | .address = 0x40, |
| 108 | .reg = 0x10, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 109 | .bit = 0x04, |
| 110 | }, /* BUC4 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 111 | { |
| 112 | .address = 0x44, |
| 113 | .reg = 0x10, |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 114 | .bit = 0x01, |
| 115 | }, /* BUC5 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 116 | { |
| 117 | .address = 0x48, |
| 118 | .reg = 0x10, |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 119 | .bit = 0x00 |
Hans de Goede | 2bde7c3 | 2017-04-21 13:48:08 +0200 | [diff] [blame^] | 120 | }, /* BUC6 */ |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /* TMP0 - TMP5 are the same, all from GPADC */ |
| 124 | static struct pmic_table thermal_table[] = { |
| 125 | { |
| 126 | .address = 0x00, |
| 127 | .reg = XPOWER_GPADC_LOW |
| 128 | }, |
| 129 | { |
| 130 | .address = 0x0c, |
| 131 | .reg = XPOWER_GPADC_LOW |
| 132 | }, |
| 133 | { |
| 134 | .address = 0x18, |
| 135 | .reg = XPOWER_GPADC_LOW |
| 136 | }, |
| 137 | { |
| 138 | .address = 0x24, |
| 139 | .reg = XPOWER_GPADC_LOW |
| 140 | }, |
| 141 | { |
| 142 | .address = 0x30, |
| 143 | .reg = XPOWER_GPADC_LOW |
| 144 | }, |
| 145 | { |
| 146 | .address = 0x3c, |
| 147 | .reg = XPOWER_GPADC_LOW |
| 148 | }, |
| 149 | }; |
| 150 | |
| 151 | static int intel_xpower_pmic_get_power(struct regmap *regmap, int reg, |
| 152 | int bit, u64 *value) |
| 153 | { |
| 154 | int data; |
| 155 | |
| 156 | if (regmap_read(regmap, reg, &data)) |
| 157 | return -EIO; |
| 158 | |
| 159 | *value = (data & BIT(bit)) ? 1 : 0; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int intel_xpower_pmic_update_power(struct regmap *regmap, int reg, |
| 164 | int bit, bool on) |
| 165 | { |
| 166 | int data; |
| 167 | |
| 168 | if (regmap_read(regmap, reg, &data)) |
| 169 | return -EIO; |
| 170 | |
| 171 | if (on) |
| 172 | data |= BIT(bit); |
| 173 | else |
| 174 | data &= ~BIT(bit); |
| 175 | |
| 176 | if (regmap_write(regmap, reg, data)) |
| 177 | return -EIO; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * intel_xpower_pmic_get_raw_temp(): Get raw temperature reading from the PMIC |
| 184 | * |
| 185 | * @regmap: regmap of the PMIC device |
| 186 | * @reg: register to get the reading |
| 187 | * |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 188 | * Return a positive value on success, errno on failure. |
| 189 | */ |
| 190 | static int intel_xpower_pmic_get_raw_temp(struct regmap *regmap, int reg) |
| 191 | { |
Hans de Goede | 2e5a7f7 | 2017-04-19 15:07:00 +0200 | [diff] [blame] | 192 | u8 buf[2]; |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 193 | |
Hans de Goede | 2e5a7f7 | 2017-04-19 15:07:00 +0200 | [diff] [blame] | 194 | if (regmap_bulk_read(regmap, AXP288_GP_ADC_H, buf, 2)) |
| 195 | return -EIO; |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 196 | |
Hans de Goede | 2e5a7f7 | 2017-04-19 15:07:00 +0200 | [diff] [blame] | 197 | return (buf[0] << 4) + ((buf[1] >> 4) & 0x0F); |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static struct intel_pmic_opregion_data intel_xpower_pmic_opregion_data = { |
| 201 | .get_power = intel_xpower_pmic_get_power, |
| 202 | .update_power = intel_xpower_pmic_update_power, |
| 203 | .get_raw_temp = intel_xpower_pmic_get_raw_temp, |
| 204 | .power_table = power_table, |
| 205 | .power_table_count = ARRAY_SIZE(power_table), |
| 206 | .thermal_table = thermal_table, |
| 207 | .thermal_table_count = ARRAY_SIZE(thermal_table), |
| 208 | }; |
| 209 | |
Aaron Lu | 491cb35 | 2014-11-24 17:32:33 +0800 | [diff] [blame] | 210 | static acpi_status intel_xpower_pmic_gpio_handler(u32 function, |
| 211 | acpi_physical_address address, u32 bit_width, u64 *value, |
| 212 | void *handler_context, void *region_context) |
| 213 | { |
| 214 | return AE_OK; |
| 215 | } |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 216 | |
| 217 | static int intel_xpower_pmic_opregion_probe(struct platform_device *pdev) |
| 218 | { |
Aaron Lu | 491cb35 | 2014-11-24 17:32:33 +0800 | [diff] [blame] | 219 | struct device *parent = pdev->dev.parent; |
| 220 | struct axp20x_dev *axp20x = dev_get_drvdata(parent); |
| 221 | acpi_status status; |
| 222 | int result; |
| 223 | |
| 224 | status = acpi_install_address_space_handler(ACPI_HANDLE(parent), |
| 225 | ACPI_ADR_SPACE_GPIO, intel_xpower_pmic_gpio_handler, |
| 226 | NULL, NULL); |
| 227 | if (ACPI_FAILURE(status)) |
| 228 | return -ENODEV; |
| 229 | |
| 230 | result = intel_pmic_install_opregion_handler(&pdev->dev, |
| 231 | ACPI_HANDLE(parent), axp20x->regmap, |
| 232 | &intel_xpower_pmic_opregion_data); |
| 233 | if (result) |
| 234 | acpi_remove_address_space_handler(ACPI_HANDLE(parent), |
| 235 | ACPI_ADR_SPACE_GPIO, |
| 236 | intel_xpower_pmic_gpio_handler); |
| 237 | |
| 238 | return result; |
Aaron Lu | d8139f6 | 2014-11-24 17:24:47 +0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static struct platform_driver intel_xpower_pmic_opregion_driver = { |
| 242 | .probe = intel_xpower_pmic_opregion_probe, |
| 243 | .driver = { |
| 244 | .name = "axp288_pmic_acpi", |
| 245 | }, |
| 246 | }; |
| 247 | |
| 248 | static int __init intel_xpower_pmic_opregion_driver_init(void) |
| 249 | { |
| 250 | return platform_driver_register(&intel_xpower_pmic_opregion_driver); |
| 251 | } |
Paul Gortmaker | 6d3ef8d | 2016-07-11 18:05:16 -0400 | [diff] [blame] | 252 | device_initcall(intel_xpower_pmic_opregion_driver_init); |