1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Intel SoC PMIC MFD Driver
4 *
5 * Copyright (C) 2013, 2014 Intel Corporation. All rights reserved.
6 *
7 * Author: Yang, Bin <bin.yang@intel.com>
8 * Author: Zhu, Lejun <lejun.zhu@linux.intel.com>
9 */
10
11#include <linux/acpi.h>
12#include <linux/gpio/consumer.h>
13#include <linux/gpio/machine.h>
14#include <linux/i2c.h>
15#include <linux/interrupt.h>
16#include <linux/module.h>
17#include <linux/mfd/core.h>
18#include <linux/mfd/intel_soc_pmic.h>
19#include <linux/pwm.h>
20#include <linux/regmap.h>
21
22#include "intel_soc_pmic_core.h"
23
24/* Crystal Cove PMIC shares same ACPI ID between different platforms */
25#define BYT_CRC_HRV 2
26#define CHT_CRC_HRV 3
27
28/* Lookup table for the Panel Enable/Disable line as GPIO signals */
29static struct gpiod_lookup_table panel_gpio_table = {
30 /* Intel GFX is consumer */
31 .dev_id = "0000:00:02.0",
32 .table = {
33 /* Panel EN/DISABLE */
34 GPIO_LOOKUP("gpio_crystalcove", 94, "panel", GPIO_ACTIVE_HIGH),
35 { },
36 },
37};
38
39/* PWM consumed by the Intel GFX */
40static struct pwm_lookup crc_pwm_lookup[] = {
41 PWM_LOOKUP("crystal_cove_pwm", 0, "0000:00:02.0", "pwm_backlight", 0, PWM_POLARITY_NORMAL),
42};
43
44static int intel_soc_pmic_i2c_probe(struct i2c_client *i2c,
45 const struct i2c_device_id *i2c_id)
46{
47 struct device *dev = &i2c->dev;
48 struct intel_soc_pmic_config *config;
49 struct intel_soc_pmic *pmic;
50 unsigned long long hrv;
51 acpi_status status;
52 int ret;
53
54 /*
55 * There are 2 different Crystal Cove PMICs a Bay Trail and Cherry
56 * Trail version, use _HRV to differentiate between the 2.
57 */
58 status = acpi_evaluate_integer(ACPI_HANDLE(dev), "_HRV", NULL, &hrv);
59 if (ACPI_FAILURE(status)) {
60 dev_err(dev, "Failed to get PMIC hardware revision\n");
61 return -ENODEV;
62 }
63
64 switch (hrv) {
65 case BYT_CRC_HRV:
66 config = &intel_soc_pmic_config_byt_crc;
67 break;
68 case CHT_CRC_HRV:
69 config = &intel_soc_pmic_config_cht_crc;
70 break;
71 default:
72 dev_warn(dev, "Unknown hardware rev %llu, assuming BYT\n", hrv);
73 config = &intel_soc_pmic_config_byt_crc;
74 }
75
76 pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
77 if (!pmic)
78 return -ENOMEM;
79
80 dev_set_drvdata(dev, pmic);
81
82 pmic->regmap = devm_regmap_init_i2c(i2c, config->regmap_config);
83 if (IS_ERR(pmic->regmap))
84 return PTR_ERR(pmic->regmap);
85
86 pmic->irq = i2c->irq;
87
88 ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
89 config->irq_flags | IRQF_ONESHOT,
90 0, config->irq_chip,
91 &pmic->irq_chip_data);
92 if (ret)
93 return ret;
94
95 ret = enable_irq_wake(pmic->irq);
96 if (ret)
97 dev_warn(dev, "Can't enable IRQ as wake source: %d\n", ret);
98
99 /* Add lookup table binding for Panel Control to the GPIO Chip */
100 gpiod_add_lookup_table(&panel_gpio_table);
101
102 /* Add lookup table for crc-pwm */
103 pwm_add_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
104
105 ret = mfd_add_devices(dev, -1, config->cell_dev,
106 config->n_cell_devs, NULL, 0,
107 regmap_irq_get_domain(pmic->irq_chip_data));
108 if (ret)
109 goto err_del_irq_chip;
110
111 return 0;
112
113err_del_irq_chip:
114 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
115 return ret;
116}
117
118static int intel_soc_pmic_i2c_remove(struct i2c_client *i2c)
119{
120 struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
121
122 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
123
124 /* Remove lookup table for Panel Control from the GPIO Chip */
125 gpiod_remove_lookup_table(&panel_gpio_table);
126
127 /* remove crc-pwm lookup table */
128 pwm_remove_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
129
130 mfd_remove_devices(&i2c->dev);
131
132 return 0;
133}
134
135static void intel_soc_pmic_shutdown(struct i2c_client *i2c)
136{
137 struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
138
139 disable_irq(pmic->irq);
140
141 return;
142}
143
144#if defined(CONFIG_PM_SLEEP)
145static int intel_soc_pmic_suspend(struct device *dev)
146{
147 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
148
149 disable_irq(pmic->irq);
150
151 return 0;
152}
153
154static int intel_soc_pmic_resume(struct device *dev)
155{
156 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
157
158 enable_irq(pmic->irq);
159
160 return 0;
161}
162#endif
163
164static SIMPLE_DEV_PM_OPS(intel_soc_pmic_pm_ops, intel_soc_pmic_suspend,
165 intel_soc_pmic_resume);
166
167static const struct i2c_device_id intel_soc_pmic_i2c_id[] = {
168 { }
169};
170MODULE_DEVICE_TABLE(i2c, intel_soc_pmic_i2c_id);
171
172#if defined(CONFIG_ACPI)
173static const struct acpi_device_id intel_soc_pmic_acpi_match[] = {
174 { "INT33FD" },
175 { },
176};
177MODULE_DEVICE_TABLE(acpi, intel_soc_pmic_acpi_match);
178#endif
179
180static struct i2c_driver intel_soc_pmic_i2c_driver = {
181 .driver = {
182 .name = "intel_soc_pmic_i2c",
183 .pm = &intel_soc_pmic_pm_ops,
184 .acpi_match_table = ACPI_PTR(intel_soc_pmic_acpi_match),
185 },
186 .probe = intel_soc_pmic_i2c_probe,
187 .remove = intel_soc_pmic_i2c_remove,
188 .id_table = intel_soc_pmic_i2c_id,
189 .shutdown = intel_soc_pmic_shutdown,
190};
191
192module_i2c_driver(intel_soc_pmic_i2c_driver);
193
194MODULE_DESCRIPTION("I2C driver for Intel SoC PMIC");
195MODULE_LICENSE("GPL v2");
196MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
197MODULE_AUTHOR("Zhu, Lejun <lejun.zhu@linux.intel.com>");
198