blob: 40acaff2e1c3c836292e05d4bd2e86a13abd607f [file] [log] [blame]
Qipeng Zha39d047c2015-09-15 00:39:19 +08001/*
2 * MFD core driver for Intel Broxton Whiskey Cove PMIC
3 *
4 * Copyright (C) 2015 Intel Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16#include <linux/module.h>
17#include <linux/acpi.h>
18#include <linux/err.h>
19#include <linux/delay.h>
20#include <linux/interrupt.h>
21#include <linux/kernel.h>
22#include <linux/mfd/core.h>
23#include <linux/mfd/intel_bxtwc.h>
24#include <asm/intel_pmc_ipc.h>
25
26/* PMIC device registers */
27#define REG_ADDR_MASK 0xFF00
28#define REG_ADDR_SHIFT 8
29#define REG_OFFSET_MASK 0xFF
30
31/* Interrupt Status Registers */
32#define BXTWC_IRQLVL1 0x4E02
33#define BXTWC_PWRBTNIRQ 0x4E03
34
35#define BXTWC_THRM0IRQ 0x4E04
36#define BXTWC_THRM1IRQ 0x4E05
37#define BXTWC_THRM2IRQ 0x4E06
38#define BXTWC_BCUIRQ 0x4E07
39#define BXTWC_ADCIRQ 0x4E08
40#define BXTWC_CHGR0IRQ 0x4E09
41#define BXTWC_CHGR1IRQ 0x4E0A
42#define BXTWC_GPIOIRQ0 0x4E0B
43#define BXTWC_GPIOIRQ1 0x4E0C
44#define BXTWC_CRITIRQ 0x4E0D
45
46/* Interrupt MASK Registers */
47#define BXTWC_MIRQLVL1 0x4E0E
48#define BXTWC_MPWRTNIRQ 0x4E0F
49
50#define BXTWC_MTHRM0IRQ 0x4E12
51#define BXTWC_MTHRM1IRQ 0x4E13
52#define BXTWC_MTHRM2IRQ 0x4E14
53#define BXTWC_MBCUIRQ 0x4E15
54#define BXTWC_MADCIRQ 0x4E16
55#define BXTWC_MCHGR0IRQ 0x4E17
56#define BXTWC_MCHGR1IRQ 0x4E18
57#define BXTWC_MGPIO0IRQ 0x4E19
58#define BXTWC_MGPIO1IRQ 0x4E1A
59#define BXTWC_MCRITIRQ 0x4E1B
60
61/* Whiskey Cove PMIC share same ACPI ID between different platforms */
62#define BROXTON_PMIC_WC_HRV 4
63
64/* Manage in two IRQ chips since mask registers are not consecutive */
65enum bxtwc_irqs {
66 /* Level 1 */
67 BXTWC_PWRBTN_LVL1_IRQ = 0,
68 BXTWC_TMU_LVL1_IRQ,
69 BXTWC_THRM_LVL1_IRQ,
70 BXTWC_BCU_LVL1_IRQ,
71 BXTWC_ADC_LVL1_IRQ,
72 BXTWC_CHGR_LVL1_IRQ,
73 BXTWC_GPIO_LVL1_IRQ,
74 BXTWC_CRIT_LVL1_IRQ,
75
76 /* Level 2 */
77 BXTWC_PWRBTN_IRQ,
78};
79
80enum bxtwc_irqs_level2 {
81 /* Level 2 */
82 BXTWC_THRM0_IRQ = 0,
83 BXTWC_THRM1_IRQ,
84 BXTWC_THRM2_IRQ,
85 BXTWC_BCU_IRQ,
86 BXTWC_ADC_IRQ,
87 BXTWC_CHGR0_IRQ,
88 BXTWC_CHGR1_IRQ,
89 BXTWC_GPIO0_IRQ,
90 BXTWC_GPIO1_IRQ,
91 BXTWC_CRIT_IRQ,
92};
93
94static const struct regmap_irq bxtwc_regmap_irqs[] = {
95 REGMAP_IRQ_REG(BXTWC_PWRBTN_LVL1_IRQ, 0, BIT(0)),
96 REGMAP_IRQ_REG(BXTWC_TMU_LVL1_IRQ, 0, BIT(1)),
97 REGMAP_IRQ_REG(BXTWC_THRM_LVL1_IRQ, 0, BIT(2)),
98 REGMAP_IRQ_REG(BXTWC_BCU_LVL1_IRQ, 0, BIT(3)),
99 REGMAP_IRQ_REG(BXTWC_ADC_LVL1_IRQ, 0, BIT(4)),
100 REGMAP_IRQ_REG(BXTWC_CHGR_LVL1_IRQ, 0, BIT(5)),
101 REGMAP_IRQ_REG(BXTWC_GPIO_LVL1_IRQ, 0, BIT(6)),
102 REGMAP_IRQ_REG(BXTWC_CRIT_LVL1_IRQ, 0, BIT(7)),
103 REGMAP_IRQ_REG(BXTWC_PWRBTN_IRQ, 1, 0x03),
104};
105
106static const struct regmap_irq bxtwc_regmap_irqs_level2[] = {
107 REGMAP_IRQ_REG(BXTWC_THRM0_IRQ, 0, 0xff),
108 REGMAP_IRQ_REG(BXTWC_THRM1_IRQ, 1, 0xbf),
109 REGMAP_IRQ_REG(BXTWC_THRM2_IRQ, 2, 0xff),
110 REGMAP_IRQ_REG(BXTWC_BCU_IRQ, 3, 0x1f),
111 REGMAP_IRQ_REG(BXTWC_ADC_IRQ, 4, 0xff),
112 REGMAP_IRQ_REG(BXTWC_CHGR0_IRQ, 5, 0x1f),
113 REGMAP_IRQ_REG(BXTWC_CHGR1_IRQ, 6, 0x1f),
114 REGMAP_IRQ_REG(BXTWC_GPIO0_IRQ, 7, 0xff),
115 REGMAP_IRQ_REG(BXTWC_GPIO1_IRQ, 8, 0x3f),
116 REGMAP_IRQ_REG(BXTWC_CRIT_IRQ, 9, 0x03),
117};
118
119static struct regmap_irq_chip bxtwc_regmap_irq_chip = {
120 .name = "bxtwc_irq_chip",
121 .status_base = BXTWC_IRQLVL1,
122 .mask_base = BXTWC_MIRQLVL1,
123 .irqs = bxtwc_regmap_irqs,
124 .num_irqs = ARRAY_SIZE(bxtwc_regmap_irqs),
125 .num_regs = 2,
126};
127
128static struct regmap_irq_chip bxtwc_regmap_irq_chip_level2 = {
129 .name = "bxtwc_irq_chip_level2",
130 .status_base = BXTWC_THRM0IRQ,
131 .mask_base = BXTWC_MTHRM0IRQ,
132 .irqs = bxtwc_regmap_irqs_level2,
133 .num_irqs = ARRAY_SIZE(bxtwc_regmap_irqs_level2),
134 .num_regs = 10,
135};
136
137static struct resource gpio_resources[] = {
138 DEFINE_RES_IRQ_NAMED(BXTWC_GPIO0_IRQ, "GPIO0"),
139 DEFINE_RES_IRQ_NAMED(BXTWC_GPIO1_IRQ, "GPIO1"),
140};
141
142static struct resource adc_resources[] = {
143 DEFINE_RES_IRQ_NAMED(BXTWC_ADC_IRQ, "ADC"),
144};
145
146static struct resource charger_resources[] = {
147 DEFINE_RES_IRQ_NAMED(BXTWC_CHGR0_IRQ, "CHARGER"),
148 DEFINE_RES_IRQ_NAMED(BXTWC_CHGR1_IRQ, "CHARGER1"),
149};
150
151static struct resource thermal_resources[] = {
152 DEFINE_RES_IRQ(BXTWC_THRM0_IRQ),
153 DEFINE_RES_IRQ(BXTWC_THRM1_IRQ),
154 DEFINE_RES_IRQ(BXTWC_THRM2_IRQ),
155};
156
157static struct resource bcu_resources[] = {
158 DEFINE_RES_IRQ_NAMED(BXTWC_BCU_IRQ, "BCU"),
159};
160
161static struct mfd_cell bxt_wc_dev[] = {
162 {
163 .name = "bxt_wcove_gpadc",
164 .num_resources = ARRAY_SIZE(adc_resources),
165 .resources = adc_resources,
166 },
167 {
168 .name = "bxt_wcove_thermal",
169 .num_resources = ARRAY_SIZE(thermal_resources),
170 .resources = thermal_resources,
171 },
172 {
173 .name = "bxt_wcove_ext_charger",
174 .num_resources = ARRAY_SIZE(charger_resources),
175 .resources = charger_resources,
176 },
177 {
178 .name = "bxt_wcove_bcu",
179 .num_resources = ARRAY_SIZE(bcu_resources),
180 .resources = bcu_resources,
181 },
182 {
183 .name = "bxt_wcove_gpio",
184 .num_resources = ARRAY_SIZE(gpio_resources),
185 .resources = gpio_resources,
186 },
187 {
188 .name = "bxt_wcove_region",
189 },
190};
191
192static int regmap_ipc_byte_reg_read(void *context, unsigned int reg,
193 unsigned int *val)
194{
195 int ret;
196 int i2c_addr;
197 u8 ipc_in[2];
198 u8 ipc_out[4];
199 struct intel_soc_pmic *pmic = context;
200
201 if (reg & REG_ADDR_MASK)
202 i2c_addr = (reg & REG_ADDR_MASK) >> REG_ADDR_SHIFT;
203 else {
204 i2c_addr = BXTWC_DEVICE1_ADDR;
205 if (!i2c_addr) {
206 dev_err(pmic->dev, "I2C address not set\n");
207 return -EINVAL;
208 }
209 }
210 reg &= REG_OFFSET_MASK;
211
212 ipc_in[0] = reg;
213 ipc_in[1] = i2c_addr;
214 ret = intel_pmc_ipc_command(PMC_IPC_PMIC_ACCESS,
215 PMC_IPC_PMIC_ACCESS_READ,
216 ipc_in, sizeof(ipc_in), (u32 *)ipc_out, 1);
217 if (ret) {
218 dev_err(pmic->dev, "Failed to read from PMIC\n");
219 return ret;
220 }
221 *val = ipc_out[0];
222
223 return 0;
224}
225
226static int regmap_ipc_byte_reg_write(void *context, unsigned int reg,
227 unsigned int val)
228{
229 int ret;
230 int i2c_addr;
231 u8 ipc_in[3];
232 struct intel_soc_pmic *pmic = context;
233
234 if (reg & REG_ADDR_MASK)
235 i2c_addr = (reg & REG_ADDR_MASK) >> REG_ADDR_SHIFT;
236 else {
237 i2c_addr = BXTWC_DEVICE1_ADDR;
238 if (!i2c_addr) {
239 dev_err(pmic->dev, "I2C address not set\n");
240 return -EINVAL;
241 }
242 }
243 reg &= REG_OFFSET_MASK;
244
245 ipc_in[0] = reg;
246 ipc_in[1] = i2c_addr;
247 ipc_in[2] = val;
248 ret = intel_pmc_ipc_command(PMC_IPC_PMIC_ACCESS,
249 PMC_IPC_PMIC_ACCESS_WRITE,
250 ipc_in, sizeof(ipc_in), NULL, 0);
251 if (ret) {
252 dev_err(pmic->dev, "Failed to write to PMIC\n");
253 return ret;
254 }
255
256 return 0;
257}
258
259/* sysfs interfaces to r/w PMIC registers, required by initial script */
260static unsigned long bxtwc_reg_addr;
261static ssize_t bxtwc_reg_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
263{
264 return sprintf(buf, "0x%lx\n", bxtwc_reg_addr);
265}
266
267static ssize_t bxtwc_reg_store(struct device *dev,
268 struct device_attribute *attr, const char *buf, size_t count)
269{
270 if (kstrtoul(buf, 0, &bxtwc_reg_addr)) {
271 dev_err(dev, "Invalid register address\n");
272 return -EINVAL;
273 }
274 return (ssize_t)count;
275}
276
277static ssize_t bxtwc_val_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
279{
280 int ret;
281 unsigned int val;
282 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
283
284 ret = regmap_read(pmic->regmap, bxtwc_reg_addr, &val);
285 if (ret < 0) {
286 dev_err(dev, "Failed to read 0x%lx\n", bxtwc_reg_addr);
287 return -EIO;
288 }
289
290 return sprintf(buf, "0x%02x\n", val);
291}
292
293static ssize_t bxtwc_val_store(struct device *dev,
294 struct device_attribute *attr, const char *buf, size_t count)
295{
296 int ret;
297 unsigned int val;
298 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
299
300 if (kstrtoul(buf, 0, (unsigned long *)&val)) {
301 dev_err(dev, "Invalid register value\n");
302 return -EINVAL;
303 }
304
305 ret = regmap_write(pmic->regmap, bxtwc_reg_addr, val);
306 if (ret) {
307 dev_err(dev, "Failed to write value 0x%02x to address 0x%lx",
308 val, bxtwc_reg_addr);
309 return -EIO;
310 }
311 return count;
312}
313
314static DEVICE_ATTR(addr, S_IWUSR | S_IRUSR, bxtwc_reg_show, bxtwc_reg_store);
315static DEVICE_ATTR(val, S_IWUSR | S_IRUSR, bxtwc_val_show, bxtwc_val_store);
316static struct attribute *bxtwc_attrs[] = {
317 &dev_attr_addr.attr,
318 &dev_attr_val.attr,
319 NULL
320};
321
322static const struct attribute_group bxtwc_group = {
323 .attrs = bxtwc_attrs,
324};
325
326static const struct regmap_config bxtwc_regmap_config = {
327 .reg_bits = 16,
328 .val_bits = 8,
329 .reg_write = regmap_ipc_byte_reg_write,
330 .reg_read = regmap_ipc_byte_reg_read,
331};
332
333static int bxtwc_probe(struct platform_device *pdev)
334{
335 int ret;
336 acpi_handle handle;
337 acpi_status status;
338 unsigned long long hrv;
339 struct intel_soc_pmic *pmic;
340
341 handle = ACPI_HANDLE(&pdev->dev);
342 status = acpi_evaluate_integer(handle, "_HRV", NULL, &hrv);
343 if (ACPI_FAILURE(status)) {
344 dev_err(&pdev->dev, "Failed to get PMIC hardware revision\n");
345 return -ENODEV;
346 }
347 if (hrv != BROXTON_PMIC_WC_HRV) {
348 dev_err(&pdev->dev, "Invalid PMIC hardware revision: %llu\n",
349 hrv);
350 return -ENODEV;
351 }
352
353 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
354 if (!pmic)
355 return -ENOMEM;
356
357 ret = platform_get_irq(pdev, 0);
358 if (ret < 0) {
359 dev_err(&pdev->dev, "Invalid IRQ\n");
360 return ret;
361 }
362 pmic->irq = ret;
363
364 dev_set_drvdata(&pdev->dev, pmic);
365 pmic->dev = &pdev->dev;
366
367 pmic->regmap = devm_regmap_init(&pdev->dev, NULL, pmic,
368 &bxtwc_regmap_config);
369 if (IS_ERR(pmic->regmap)) {
370 ret = PTR_ERR(pmic->regmap);
371 dev_err(&pdev->dev, "Failed to initialise regmap: %d\n", ret);
372 return ret;
373 }
374
375 ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
376 IRQF_ONESHOT | IRQF_SHARED,
377 0, &bxtwc_regmap_irq_chip,
378 &pmic->irq_chip_data);
379 if (ret) {
380 dev_err(&pdev->dev, "Failed to add IRQ chip\n");
381 return ret;
382 }
383
384 ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
385 IRQF_ONESHOT | IRQF_SHARED,
386 0, &bxtwc_regmap_irq_chip_level2,
387 &pmic->irq_chip_data_level2);
388 if (ret) {
389 dev_err(&pdev->dev, "Failed to add secondary IRQ chip\n");
390 goto err_irq_chip_level2;
391 }
392
393 ret = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE, bxt_wc_dev,
394 ARRAY_SIZE(bxt_wc_dev), NULL, 0,
395 NULL);
396 if (ret) {
397 dev_err(&pdev->dev, "Failed to add devices\n");
398 goto err_mfd;
399 }
400
401 ret = sysfs_create_group(&pdev->dev.kobj, &bxtwc_group);
402 if (ret) {
403 dev_err(&pdev->dev, "Failed to create sysfs group %d\n", ret);
404 goto err_sysfs;
405 }
406
407 return 0;
408
409err_sysfs:
410 mfd_remove_devices(&pdev->dev);
411err_mfd:
412 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data_level2);
413err_irq_chip_level2:
414 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
415
416 return ret;
417}
418
419static int bxtwc_remove(struct platform_device *pdev)
420{
421 struct intel_soc_pmic *pmic = dev_get_drvdata(&pdev->dev);
422
423 sysfs_remove_group(&pdev->dev.kobj, &bxtwc_group);
424 mfd_remove_devices(&pdev->dev);
425 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
426 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data_level2);
427
428 return 0;
429}
430
431static void bxtwc_shutdown(struct platform_device *pdev)
432{
433 struct intel_soc_pmic *pmic = dev_get_drvdata(&pdev->dev);
434
435 disable_irq(pmic->irq);
436}
437
438#ifdef CONFIG_PM_SLEEP
439static int bxtwc_suspend(struct device *dev)
440{
441 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
442
443 disable_irq(pmic->irq);
444
445 return 0;
446}
447
448static int bxtwc_resume(struct device *dev)
449{
450 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
451
452 enable_irq(pmic->irq);
453 return 0;
454}
455#endif
456static SIMPLE_DEV_PM_OPS(bxtwc_pm_ops, bxtwc_suspend, bxtwc_resume);
457
458static const struct acpi_device_id bxtwc_acpi_ids[] = {
459 { "INT34D3", },
460 { }
461};
462MODULE_DEVICE_TABLE(acpi, pmic_acpi_ids);
463
464static struct platform_driver bxtwc_driver = {
465 .probe = bxtwc_probe,
466 .remove = bxtwc_remove,
467 .shutdown = bxtwc_shutdown,
468 .driver = {
469 .name = "BXTWC PMIC",
470 .pm = &bxtwc_pm_ops,
471 .acpi_match_table = ACPI_PTR(bxtwc_acpi_ids),
472 },
473};
474
475module_platform_driver(bxtwc_driver);
476
477MODULE_LICENSE("GPL v2");
478MODULE_AUTHOR("Qipeng Zha<qipeng.zha@intel.com>");