blob: 12d6ebb4ae5d5bfb5c9e1a753dbc8f81d5194e2d [file] [log] [blame]
Zhu, Lejun51652382014-06-03 13:26:02 +08001/*
2 * intel_soc_pmic_core.c - Intel SoC PMIC MFD Driver
3 *
4 * Copyright (C) 2013, 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 * Author: Yang, Bin <bin.yang@intel.com>
16 * Author: Zhu, Lejun <lejun.zhu@linux.intel.com>
17 */
18
19#include <linux/module.h>
20#include <linux/mfd/core.h>
21#include <linux/i2c.h>
22#include <linux/interrupt.h>
23#include <linux/gpio/consumer.h>
24#include <linux/acpi.h>
25#include <linux/regmap.h>
26#include <linux/mfd/intel_soc_pmic.h>
Shobhit Kumar61dd2ca2015-06-26 14:32:05 +053027#include <linux/gpio/machine.h>
Shobhit Kumara3aa9a92015-06-26 14:32:07 +053028#include <linux/pwm.h>
Zhu, Lejun51652382014-06-03 13:26:02 +080029#include "intel_soc_pmic_core.h"
30
Shobhit Kumar61dd2ca2015-06-26 14:32:05 +053031/* Lookup table for the Panel Enable/Disable line as GPIO signals */
32static struct gpiod_lookup_table panel_gpio_table = {
33 /* Intel GFX is consumer */
34 .dev_id = "0000:00:02.0",
35 .table = {
36 /* Panel EN/DISABLE */
37 GPIO_LOOKUP("gpio_crystalcove", 94, "panel", GPIO_ACTIVE_HIGH),
Ville Syrjälä36e6d082016-04-22 22:38:55 +030038 { },
Shobhit Kumar61dd2ca2015-06-26 14:32:05 +053039 },
40};
41
Shobhit Kumara3aa9a92015-06-26 14:32:07 +053042/* PWM consumed by the Intel GFX */
43static struct pwm_lookup crc_pwm_lookup[] = {
44 PWM_LOOKUP("crystal_cove_pwm", 0, "0000:00:02.0", "pwm_backlight", 0, PWM_POLARITY_NORMAL),
45};
46
Zhu, Lejun51652382014-06-03 13:26:02 +080047static int intel_soc_pmic_find_gpio_irq(struct device *dev)
48{
49 struct gpio_desc *desc;
50 int irq;
51
Jarkko Nikula9dc50212015-02-13 15:01:15 +020052 desc = devm_gpiod_get_index(dev, "intel_soc_pmic", 0, GPIOD_IN);
Zhu, Lejun51652382014-06-03 13:26:02 +080053 if (IS_ERR(desc))
Jarkko Nikulaad7b5a12015-02-13 15:01:14 +020054 return PTR_ERR(desc);
Zhu, Lejun51652382014-06-03 13:26:02 +080055
56 irq = gpiod_to_irq(desc);
57 if (irq < 0)
58 dev_warn(dev, "Can't get irq: %d\n", irq);
59
60 return irq;
61}
62
63static int intel_soc_pmic_i2c_probe(struct i2c_client *i2c,
64 const struct i2c_device_id *i2c_id)
65{
66 struct device *dev = &i2c->dev;
67 const struct acpi_device_id *id;
68 struct intel_soc_pmic_config *config;
69 struct intel_soc_pmic *pmic;
70 int ret;
71 int irq;
72
73 id = acpi_match_device(dev->driver->acpi_match_table, dev);
74 if (!id || !id->driver_data)
75 return -ENODEV;
76
77 config = (struct intel_soc_pmic_config *)id->driver_data;
78
79 pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
Kiran Padwal0a65fbf2015-02-11 15:40:50 +053080 if (!pmic)
81 return -ENOMEM;
82
Zhu, Lejun51652382014-06-03 13:26:02 +080083 dev_set_drvdata(dev, pmic);
84
85 pmic->regmap = devm_regmap_init_i2c(i2c, config->regmap_config);
86
Jarkko Nikula9c98dcb2015-02-13 15:01:13 +020087 /*
88 * On some boards the PMIC interrupt may come from a GPIO line. Try to
89 * lookup the ACPI table for a such connection and setup a GPIO
90 * interrupt if it exists. Otherwise use the IRQ provided by I2C
91 */
Zhu, Lejun51652382014-06-03 13:26:02 +080092 irq = intel_soc_pmic_find_gpio_irq(dev);
93 pmic->irq = (irq < 0) ? i2c->irq : irq;
94
95 ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
96 config->irq_flags | IRQF_ONESHOT,
97 0, config->irq_chip,
98 &pmic->irq_chip_data);
99 if (ret)
100 return ret;
101
102 ret = enable_irq_wake(pmic->irq);
103 if (ret)
104 dev_warn(dev, "Can't enable IRQ as wake source: %d\n", ret);
105
Shobhit Kumar61dd2ca2015-06-26 14:32:05 +0530106 /* Add lookup table binding for Panel Control to the GPIO Chip */
107 gpiod_add_lookup_table(&panel_gpio_table);
108
Shobhit Kumara3aa9a92015-06-26 14:32:07 +0530109 /* Add lookup table for crc-pwm */
110 pwm_add_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
111
Zhu, Lejun51652382014-06-03 13:26:02 +0800112 ret = mfd_add_devices(dev, -1, config->cell_dev,
113 config->n_cell_devs, NULL, 0,
114 regmap_irq_get_domain(pmic->irq_chip_data));
115 if (ret)
116 goto err_del_irq_chip;
117
118 return 0;
119
120err_del_irq_chip:
121 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
122 return ret;
123}
124
125static int intel_soc_pmic_i2c_remove(struct i2c_client *i2c)
126{
127 struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
128
129 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
130
Shobhit Kumar61dd2ca2015-06-26 14:32:05 +0530131 /* Remove lookup table for Panel Control from the GPIO Chip */
132 gpiod_remove_lookup_table(&panel_gpio_table);
133
Shobhit Kumara3aa9a92015-06-26 14:32:07 +0530134 /* remove crc-pwm lookup table */
135 pwm_remove_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
136
Zhu, Lejun51652382014-06-03 13:26:02 +0800137 mfd_remove_devices(&i2c->dev);
138
139 return 0;
140}
141
142static void intel_soc_pmic_shutdown(struct i2c_client *i2c)
143{
144 struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
145
146 disable_irq(pmic->irq);
147
148 return;
149}
150
Jaewon Kimbdaf6702014-09-12 13:35:45 +0900151#if defined(CONFIG_PM_SLEEP)
Zhu, Lejun51652382014-06-03 13:26:02 +0800152static int intel_soc_pmic_suspend(struct device *dev)
153{
154 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
155
156 disable_irq(pmic->irq);
157
158 return 0;
159}
160
161static int intel_soc_pmic_resume(struct device *dev)
162{
163 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
164
165 enable_irq(pmic->irq);
166
167 return 0;
168}
Jaewon Kimbdaf6702014-09-12 13:35:45 +0900169#endif
Zhu, Lejun51652382014-06-03 13:26:02 +0800170
171static SIMPLE_DEV_PM_OPS(intel_soc_pmic_pm_ops, intel_soc_pmic_suspend,
172 intel_soc_pmic_resume);
173
174static const struct i2c_device_id intel_soc_pmic_i2c_id[] = {
175 { }
176};
177MODULE_DEVICE_TABLE(i2c, intel_soc_pmic_i2c_id);
178
Lee Jones52764fd2014-07-02 11:30:12 +0100179#if defined(CONFIG_ACPI)
Mathias Krausec4658752015-06-13 14:20:51 +0200180static const struct acpi_device_id intel_soc_pmic_acpi_match[] = {
Zhu, Lejun51652382014-06-03 13:26:02 +0800181 {"INT33FD", (kernel_ulong_t)&intel_soc_pmic_config_crc},
182 { },
183};
184MODULE_DEVICE_TABLE(acpi, intel_soc_pmic_acpi_match);
Lee Jones52764fd2014-07-02 11:30:12 +0100185#endif
Zhu, Lejun51652382014-06-03 13:26:02 +0800186
187static struct i2c_driver intel_soc_pmic_i2c_driver = {
188 .driver = {
189 .name = "intel_soc_pmic_i2c",
Zhu, Lejun51652382014-06-03 13:26:02 +0800190 .pm = &intel_soc_pmic_pm_ops,
191 .acpi_match_table = ACPI_PTR(intel_soc_pmic_acpi_match),
192 },
193 .probe = intel_soc_pmic_i2c_probe,
194 .remove = intel_soc_pmic_i2c_remove,
195 .id_table = intel_soc_pmic_i2c_id,
196 .shutdown = intel_soc_pmic_shutdown,
197};
198
199module_i2c_driver(intel_soc_pmic_i2c_driver);
200
201MODULE_DESCRIPTION("I2C driver for Intel SoC PMIC");
202MODULE_LICENSE("GPL v2");
203MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
204MODULE_AUTHOR("Zhu, Lejun <lejun.zhu@linux.intel.com>");