blob: 7b50b6b208a5b04bf578804590add7117d21deb0 [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>
27#include "intel_soc_pmic_core.h"
28
Zhu, Lejun51652382014-06-03 13:26:02 +080029static int intel_soc_pmic_find_gpio_irq(struct device *dev)
30{
31 struct gpio_desc *desc;
32 int irq;
33
Jarkko Nikula9dc50212015-02-13 15:01:15 +020034 desc = devm_gpiod_get_index(dev, "intel_soc_pmic", 0, GPIOD_IN);
Zhu, Lejun51652382014-06-03 13:26:02 +080035 if (IS_ERR(desc))
Jarkko Nikulaad7b5a12015-02-13 15:01:14 +020036 return PTR_ERR(desc);
Zhu, Lejun51652382014-06-03 13:26:02 +080037
38 irq = gpiod_to_irq(desc);
39 if (irq < 0)
40 dev_warn(dev, "Can't get irq: %d\n", irq);
41
42 return irq;
43}
44
45static int intel_soc_pmic_i2c_probe(struct i2c_client *i2c,
46 const struct i2c_device_id *i2c_id)
47{
48 struct device *dev = &i2c->dev;
49 const struct acpi_device_id *id;
50 struct intel_soc_pmic_config *config;
51 struct intel_soc_pmic *pmic;
52 int ret;
53 int irq;
54
55 id = acpi_match_device(dev->driver->acpi_match_table, dev);
56 if (!id || !id->driver_data)
57 return -ENODEV;
58
59 config = (struct intel_soc_pmic_config *)id->driver_data;
60
61 pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
Kiran Padwal0a65fbf2015-02-11 15:40:50 +053062 if (!pmic)
63 return -ENOMEM;
64
Zhu, Lejun51652382014-06-03 13:26:02 +080065 dev_set_drvdata(dev, pmic);
66
67 pmic->regmap = devm_regmap_init_i2c(i2c, config->regmap_config);
68
Jarkko Nikula9c98dcb2015-02-13 15:01:13 +020069 /*
70 * On some boards the PMIC interrupt may come from a GPIO line. Try to
71 * lookup the ACPI table for a such connection and setup a GPIO
72 * interrupt if it exists. Otherwise use the IRQ provided by I2C
73 */
Zhu, Lejun51652382014-06-03 13:26:02 +080074 irq = intel_soc_pmic_find_gpio_irq(dev);
75 pmic->irq = (irq < 0) ? i2c->irq : irq;
76
77 ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
78 config->irq_flags | IRQF_ONESHOT,
79 0, config->irq_chip,
80 &pmic->irq_chip_data);
81 if (ret)
82 return ret;
83
84 ret = enable_irq_wake(pmic->irq);
85 if (ret)
86 dev_warn(dev, "Can't enable IRQ as wake source: %d\n", ret);
87
88 ret = mfd_add_devices(dev, -1, config->cell_dev,
89 config->n_cell_devs, NULL, 0,
90 regmap_irq_get_domain(pmic->irq_chip_data));
91 if (ret)
92 goto err_del_irq_chip;
93
94 return 0;
95
96err_del_irq_chip:
97 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
98 return ret;
99}
100
101static int intel_soc_pmic_i2c_remove(struct i2c_client *i2c)
102{
103 struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
104
105 regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
106
107 mfd_remove_devices(&i2c->dev);
108
109 return 0;
110}
111
112static void intel_soc_pmic_shutdown(struct i2c_client *i2c)
113{
114 struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
115
116 disable_irq(pmic->irq);
117
118 return;
119}
120
Jaewon Kimbdaf6702014-09-12 13:35:45 +0900121#if defined(CONFIG_PM_SLEEP)
Zhu, Lejun51652382014-06-03 13:26:02 +0800122static int intel_soc_pmic_suspend(struct device *dev)
123{
124 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
125
126 disable_irq(pmic->irq);
127
128 return 0;
129}
130
131static int intel_soc_pmic_resume(struct device *dev)
132{
133 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
134
135 enable_irq(pmic->irq);
136
137 return 0;
138}
Jaewon Kimbdaf6702014-09-12 13:35:45 +0900139#endif
Zhu, Lejun51652382014-06-03 13:26:02 +0800140
141static SIMPLE_DEV_PM_OPS(intel_soc_pmic_pm_ops, intel_soc_pmic_suspend,
142 intel_soc_pmic_resume);
143
144static const struct i2c_device_id intel_soc_pmic_i2c_id[] = {
145 { }
146};
147MODULE_DEVICE_TABLE(i2c, intel_soc_pmic_i2c_id);
148
Lee Jones52764fd2014-07-02 11:30:12 +0100149#if defined(CONFIG_ACPI)
Zhu, Lejun51652382014-06-03 13:26:02 +0800150static struct acpi_device_id intel_soc_pmic_acpi_match[] = {
151 {"INT33FD", (kernel_ulong_t)&intel_soc_pmic_config_crc},
152 { },
153};
154MODULE_DEVICE_TABLE(acpi, intel_soc_pmic_acpi_match);
Lee Jones52764fd2014-07-02 11:30:12 +0100155#endif
Zhu, Lejun51652382014-06-03 13:26:02 +0800156
157static struct i2c_driver intel_soc_pmic_i2c_driver = {
158 .driver = {
159 .name = "intel_soc_pmic_i2c",
160 .owner = THIS_MODULE,
161 .pm = &intel_soc_pmic_pm_ops,
162 .acpi_match_table = ACPI_PTR(intel_soc_pmic_acpi_match),
163 },
164 .probe = intel_soc_pmic_i2c_probe,
165 .remove = intel_soc_pmic_i2c_remove,
166 .id_table = intel_soc_pmic_i2c_id,
167 .shutdown = intel_soc_pmic_shutdown,
168};
169
170module_i2c_driver(intel_soc_pmic_i2c_driver);
171
172MODULE_DESCRIPTION("I2C driver for Intel SoC PMIC");
173MODULE_LICENSE("GPL v2");
174MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
175MODULE_AUTHOR("Zhu, Lejun <lejun.zhu@linux.intel.com>");