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 
16 #include <linux/module.h>
17 #include <linux/acpi.h>
18 #include <linux/mfd/axp20x.h>
19 #include <linux/regmap.h>
20 #include <linux/platform_device.h>
21 #include <linux/iio/consumer.h>
22 #include "intel_pmic.h"
23 
24 #define XPOWER_GPADC_LOW	0x5b
25 
26 static struct pmic_table power_table[] = {
27 	{
28 		.address = 0x00,
29 		.reg = 0x13,
30 		.bit = 0x05,
31 	},
32 	{
33 		.address = 0x04,
34 		.reg = 0x13,
35 		.bit = 0x06,
36 	},
37 	{
38 		.address = 0x08,
39 		.reg = 0x13,
40 		.bit = 0x07,
41 	},
42 	{
43 		.address = 0x0c,
44 		.reg = 0x12,
45 		.bit = 0x03,
46 	},
47 	{
48 		.address = 0x10,
49 		.reg = 0x12,
50 		.bit = 0x04,
51 	},
52 	{
53 		.address = 0x14,
54 		.reg = 0x12,
55 		.bit = 0x05,
56 	},
57 	{
58 		.address = 0x18,
59 		.reg = 0x12,
60 		.bit = 0x06,
61 	},
62 	{
63 		.address = 0x1c,
64 		.reg = 0x12,
65 		.bit = 0x00,
66 	},
67 	{
68 		.address = 0x20,
69 		.reg = 0x12,
70 		.bit = 0x01,
71 	},
72 	{
73 		.address = 0x24,
74 		.reg = 0x12,
75 		.bit = 0x02,
76 	},
77 	{
78 		.address = 0x28,
79 		.reg = 0x13,
80 		.bit = 0x02,
81 	},
82 	{
83 		.address = 0x2c,
84 		.reg = 0x13,
85 		.bit = 0x03,
86 	},
87 	{
88 		.address = 0x30,
89 		.reg = 0x13,
90 		.bit = 0x04,
91 	},
92 	{
93 		.address = 0x38,
94 		.reg = 0x10,
95 		.bit = 0x03,
96 	},
97 	{
98 		.address = 0x3c,
99 		.reg = 0x10,
100 		.bit = 0x06,
101 	},
102 	{
103 		.address = 0x40,
104 		.reg = 0x10,
105 		.bit = 0x05,
106 	},
107 	{
108 		.address = 0x44,
109 		.reg = 0x10,
110 		.bit = 0x04,
111 	},
112 	{
113 		.address = 0x48,
114 		.reg = 0x10,
115 		.bit = 0x01,
116 	},
117 	{
118 		.address = 0x4c,
119 		.reg = 0x10,
120 		.bit = 0x00
121 	},
122 };
123 
124 /* TMP0 - TMP5 are the same, all from GPADC */
125 static struct pmic_table thermal_table[] = {
126 	{
127 		.address = 0x00,
128 		.reg = XPOWER_GPADC_LOW
129 	},
130 	{
131 		.address = 0x0c,
132 		.reg = XPOWER_GPADC_LOW
133 	},
134 	{
135 		.address = 0x18,
136 		.reg = XPOWER_GPADC_LOW
137 	},
138 	{
139 		.address = 0x24,
140 		.reg = XPOWER_GPADC_LOW
141 	},
142 	{
143 		.address = 0x30,
144 		.reg = XPOWER_GPADC_LOW
145 	},
146 	{
147 		.address = 0x3c,
148 		.reg = XPOWER_GPADC_LOW
149 	},
150 };
151 
152 static int intel_xpower_pmic_get_power(struct regmap *regmap, int reg,
153 				       int bit, u64 *value)
154 {
155 	int data;
156 
157 	if (regmap_read(regmap, reg, &data))
158 		return -EIO;
159 
160 	*value = (data & BIT(bit)) ? 1 : 0;
161 	return 0;
162 }
163 
164 static int intel_xpower_pmic_update_power(struct regmap *regmap, int reg,
165 					  int bit, bool on)
166 {
167 	int data;
168 
169 	if (regmap_read(regmap, reg, &data))
170 		return -EIO;
171 
172 	if (on)
173 		data |= BIT(bit);
174 	else
175 		data &= ~BIT(bit);
176 
177 	if (regmap_write(regmap, reg, data))
178 		return -EIO;
179 
180 	return 0;
181 }
182 
183 /**
184  * intel_xpower_pmic_get_raw_temp(): Get raw temperature reading from the PMIC
185  *
186  * @regmap: regmap of the PMIC device
187  * @reg: register to get the reading
188  *
189  * We could get the sensor value by manipulating the HW regs here, but since
190  * the axp288 IIO driver may also access the same regs at the same time, the
191  * APIs provided by IIO subsystem are used here instead to avoid problems. As
192  * a result, the two passed in params are of no actual use.
193  *
194  * Return a positive value on success, errno on failure.
195  */
196 static int intel_xpower_pmic_get_raw_temp(struct regmap *regmap, int reg)
197 {
198 	struct iio_channel *gpadc_chan;
199 	int ret, val;
200 
201 	gpadc_chan = iio_channel_get(NULL, "axp288-system-temp");
202 	if (IS_ERR_OR_NULL(gpadc_chan))
203 		return -EACCES;
204 
205 	ret = iio_read_channel_raw(gpadc_chan, &val);
206 	if (ret < 0)
207 		val = ret;
208 
209 	iio_channel_release(gpadc_chan);
210 	return val;
211 }
212 
213 static struct intel_pmic_opregion_data intel_xpower_pmic_opregion_data = {
214 	.get_power = intel_xpower_pmic_get_power,
215 	.update_power = intel_xpower_pmic_update_power,
216 	.get_raw_temp = intel_xpower_pmic_get_raw_temp,
217 	.power_table = power_table,
218 	.power_table_count = ARRAY_SIZE(power_table),
219 	.thermal_table = thermal_table,
220 	.thermal_table_count = ARRAY_SIZE(thermal_table),
221 };
222 
223 static acpi_status intel_xpower_pmic_gpio_handler(u32 function,
224 		acpi_physical_address address, u32 bit_width, u64 *value,
225 		void *handler_context, void *region_context)
226 {
227 	return AE_OK;
228 }
229 
230 static int intel_xpower_pmic_opregion_probe(struct platform_device *pdev)
231 {
232 	struct device *parent = pdev->dev.parent;
233 	struct axp20x_dev *axp20x = dev_get_drvdata(parent);
234 	acpi_status status;
235 	int result;
236 
237 	status = acpi_install_address_space_handler(ACPI_HANDLE(parent),
238 			ACPI_ADR_SPACE_GPIO, intel_xpower_pmic_gpio_handler,
239 			NULL, NULL);
240 	if (ACPI_FAILURE(status))
241 		return -ENODEV;
242 
243 	result = intel_pmic_install_opregion_handler(&pdev->dev,
244 					ACPI_HANDLE(parent), axp20x->regmap,
245 					&intel_xpower_pmic_opregion_data);
246 	if (result)
247 		acpi_remove_address_space_handler(ACPI_HANDLE(parent),
248 						  ACPI_ADR_SPACE_GPIO,
249 						  intel_xpower_pmic_gpio_handler);
250 
251 	return result;
252 }
253 
254 static struct platform_driver intel_xpower_pmic_opregion_driver = {
255 	.probe = intel_xpower_pmic_opregion_probe,
256 	.driver = {
257 		.name = "axp288_pmic_acpi",
258 	},
259 };
260 
261 static int __init intel_xpower_pmic_opregion_driver_init(void)
262 {
263 	return platform_driver_register(&intel_xpower_pmic_opregion_driver);
264 }
265 module_init(intel_xpower_pmic_opregion_driver_init);
266 
267 MODULE_DESCRIPTION("XPower AXP288 ACPI operation region driver");
268 MODULE_LICENSE("GPL");
269