intel_pmic_xpower.c 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * intel_pmic_xpower.c - XPower AXP288 PMIC operation region driver
 *
 * Copyright (C) 2014 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

16
#include <linux/init.h>
17 18 19 20 21 22 23 24 25 26 27 28 29
#include <linux/acpi.h>
#include <linux/mfd/axp20x.h>
#include <linux/regmap.h>
#include <linux/platform_device.h>
#include "intel_pmic.h"

#define XPOWER_GPADC_LOW	0x5b

static struct pmic_table power_table[] = {
	{
		.address = 0x00,
		.reg = 0x13,
		.bit = 0x05,
30
	}, /* ALD1 */
31 32 33 34
	{
		.address = 0x04,
		.reg = 0x13,
		.bit = 0x06,
35
	}, /* ALD2 */
36 37 38 39
	{
		.address = 0x08,
		.reg = 0x13,
		.bit = 0x07,
40
	}, /* ALD3 */
41 42 43 44
	{
		.address = 0x0c,
		.reg = 0x12,
		.bit = 0x03,
45
	}, /* DLD1 */
46 47 48 49
	{
		.address = 0x10,
		.reg = 0x12,
		.bit = 0x04,
50
	}, /* DLD2 */
51 52 53 54
	{
		.address = 0x14,
		.reg = 0x12,
		.bit = 0x05,
55
	}, /* DLD3 */
56 57 58 59
	{
		.address = 0x18,
		.reg = 0x12,
		.bit = 0x06,
60
	}, /* DLD4 */
61 62 63 64
	{
		.address = 0x1c,
		.reg = 0x12,
		.bit = 0x00,
65
	}, /* ELD1 */
66 67 68 69
	{
		.address = 0x20,
		.reg = 0x12,
		.bit = 0x01,
70
	}, /* ELD2 */
71 72 73 74
	{
		.address = 0x24,
		.reg = 0x12,
		.bit = 0x02,
75
	}, /* ELD3 */
76 77 78 79
	{
		.address = 0x28,
		.reg = 0x13,
		.bit = 0x02,
80
	}, /* FLD1 */
81 82 83 84
	{
		.address = 0x2c,
		.reg = 0x13,
		.bit = 0x03,
85
	}, /* FLD2 */
86 87 88 89
	{
		.address = 0x30,
		.reg = 0x13,
		.bit = 0x04,
90
	}, /* FLD3 */
91
	{
92
		.address = 0x34,
93 94
		.reg = 0x10,
		.bit = 0x03,
95
	}, /* BUC1 */
96
	{
97
		.address = 0x38,
98 99
		.reg = 0x10,
		.bit = 0x06,
100
	}, /* BUC2 */
101
	{
102
		.address = 0x3c,
103 104
		.reg = 0x10,
		.bit = 0x05,
105
	}, /* BUC3 */
106
	{
107
		.address = 0x40,
108 109
		.reg = 0x10,
		.bit = 0x04,
110
	}, /* BUC4 */
111
	{
112
		.address = 0x44,
113 114
		.reg = 0x10,
		.bit = 0x01,
115
	}, /* BUC5 */
116
	{
117
		.address = 0x48,
118 119
		.reg = 0x10,
		.bit = 0x00
120
	}, /* BUC6 */
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
};

/* TMP0 - TMP5 are the same, all from GPADC */
static struct pmic_table thermal_table[] = {
	{
		.address = 0x00,
		.reg = XPOWER_GPADC_LOW
	},
	{
		.address = 0x0c,
		.reg = XPOWER_GPADC_LOW
	},
	{
		.address = 0x18,
		.reg = XPOWER_GPADC_LOW
	},
	{
		.address = 0x24,
		.reg = XPOWER_GPADC_LOW
	},
	{
		.address = 0x30,
		.reg = XPOWER_GPADC_LOW
	},
	{
		.address = 0x3c,
		.reg = XPOWER_GPADC_LOW
	},
};

static int intel_xpower_pmic_get_power(struct regmap *regmap, int reg,
				       int bit, u64 *value)
{
	int data;

	if (regmap_read(regmap, reg, &data))
		return -EIO;

	*value = (data & BIT(bit)) ? 1 : 0;
	return 0;
}

static int intel_xpower_pmic_update_power(struct regmap *regmap, int reg,
					  int bit, bool on)
{
	int data;

	if (regmap_read(regmap, reg, &data))
		return -EIO;

	if (on)
		data |= BIT(bit);
	else
		data &= ~BIT(bit);

	if (regmap_write(regmap, reg, data))
		return -EIO;

	return 0;
}

/**
 * intel_xpower_pmic_get_raw_temp(): Get raw temperature reading from the PMIC
 *
 * @regmap: regmap of the PMIC device
 * @reg: register to get the reading
 *
 * Return a positive value on success, errno on failure.
 */
static int intel_xpower_pmic_get_raw_temp(struct regmap *regmap, int reg)
{
192
	u8 buf[2];
193

194 195
	if (regmap_bulk_read(regmap, AXP288_GP_ADC_H, buf, 2))
		return -EIO;
196

197
	return (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
198 199 200 201 202 203 204 205 206 207 208 209
}

static struct intel_pmic_opregion_data intel_xpower_pmic_opregion_data = {
	.get_power = intel_xpower_pmic_get_power,
	.update_power = intel_xpower_pmic_update_power,
	.get_raw_temp = intel_xpower_pmic_get_raw_temp,
	.power_table = power_table,
	.power_table_count = ARRAY_SIZE(power_table),
	.thermal_table = thermal_table,
	.thermal_table_count = ARRAY_SIZE(thermal_table),
};

210 211 212 213 214 215
static acpi_status intel_xpower_pmic_gpio_handler(u32 function,
		acpi_physical_address address, u32 bit_width, u64 *value,
		void *handler_context, void *region_context)
{
	return AE_OK;
}
216 217 218

static int intel_xpower_pmic_opregion_probe(struct platform_device *pdev)
{
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	struct device *parent = pdev->dev.parent;
	struct axp20x_dev *axp20x = dev_get_drvdata(parent);
	acpi_status status;
	int result;

	status = acpi_install_address_space_handler(ACPI_HANDLE(parent),
			ACPI_ADR_SPACE_GPIO, intel_xpower_pmic_gpio_handler,
			NULL, NULL);
	if (ACPI_FAILURE(status))
		return -ENODEV;

	result = intel_pmic_install_opregion_handler(&pdev->dev,
					ACPI_HANDLE(parent), axp20x->regmap,
					&intel_xpower_pmic_opregion_data);
	if (result)
		acpi_remove_address_space_handler(ACPI_HANDLE(parent),
						  ACPI_ADR_SPACE_GPIO,
						  intel_xpower_pmic_gpio_handler);

	return result;
239 240 241 242 243 244 245 246 247 248 249 250 251
}

static struct platform_driver intel_xpower_pmic_opregion_driver = {
	.probe = intel_xpower_pmic_opregion_probe,
	.driver = {
		.name = "axp288_pmic_acpi",
	},
};

static int __init intel_xpower_pmic_opregion_driver_init(void)
{
	return platform_driver_register(&intel_xpower_pmic_opregion_driver);
}
252
device_initcall(intel_xpower_pmic_opregion_driver_init);