Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / platform / x86 / intel / crystal_cove_charger.c
blobe4299cfa22054510c298f5f1af890b0b0e92a6cf
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Driver for the external-charger IRQ pass-through function of the
4 * Intel Bay Trail Crystal Cove PMIC.
6 * Note this is NOT a power_supply class driver, it just deals with IRQ
7 * pass-through, this requires a separate driver because the PMIC's
8 * level 2 interrupt for this must be explicitly acked.
9 */
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/mfd/intel_soc_pmic.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
19 #define CHGRIRQ_REG 0x0a
20 #define MCHGRIRQ_REG 0x17
22 struct crystal_cove_charger_data {
23 struct mutex buslock; /* irq_bus_lock */
24 struct irq_chip irqchip;
25 struct regmap *regmap;
26 struct irq_domain *irq_domain;
27 int irq;
28 int charger_irq;
29 u8 mask;
30 u8 new_mask;
33 static irqreturn_t crystal_cove_charger_irq(int irq, void *data)
35 struct crystal_cove_charger_data *charger = data;
37 /* No need to read CHGRIRQ_REG as there is only 1 IRQ */
38 handle_nested_irq(charger->charger_irq);
40 /* Ack CHGRIRQ 0 */
41 regmap_write(charger->regmap, CHGRIRQ_REG, BIT(0));
43 return IRQ_HANDLED;
46 static void crystal_cove_charger_irq_bus_lock(struct irq_data *data)
48 struct crystal_cove_charger_data *charger = irq_data_get_irq_chip_data(data);
50 mutex_lock(&charger->buslock);
53 static void crystal_cove_charger_irq_bus_sync_unlock(struct irq_data *data)
55 struct crystal_cove_charger_data *charger = irq_data_get_irq_chip_data(data);
57 if (charger->mask != charger->new_mask) {
58 regmap_write(charger->regmap, MCHGRIRQ_REG, charger->new_mask);
59 charger->mask = charger->new_mask;
62 mutex_unlock(&charger->buslock);
65 static void crystal_cove_charger_irq_unmask(struct irq_data *data)
67 struct crystal_cove_charger_data *charger = irq_data_get_irq_chip_data(data);
69 charger->new_mask &= ~BIT(data->hwirq);
72 static void crystal_cove_charger_irq_mask(struct irq_data *data)
74 struct crystal_cove_charger_data *charger = irq_data_get_irq_chip_data(data);
76 charger->new_mask |= BIT(data->hwirq);
79 static void crystal_cove_charger_rm_irq_domain(void *data)
81 struct crystal_cove_charger_data *charger = data;
83 irq_domain_remove(charger->irq_domain);
86 static int crystal_cove_charger_probe(struct platform_device *pdev)
88 struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
89 struct crystal_cove_charger_data *charger;
90 int ret;
92 charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
93 if (!charger)
94 return -ENOMEM;
96 charger->regmap = pmic->regmap;
97 mutex_init(&charger->buslock);
99 charger->irq = platform_get_irq(pdev, 0);
100 if (charger->irq < 0)
101 return charger->irq;
103 charger->irq_domain = irq_domain_create_linear(dev_fwnode(pdev->dev.parent), 1,
104 &irq_domain_simple_ops, NULL);
105 if (!charger->irq_domain)
106 return -ENOMEM;
108 /* Distuingish IRQ domain from others sharing (MFD) the same fwnode */
109 irq_domain_update_bus_token(charger->irq_domain, DOMAIN_BUS_WAKEUP);
111 ret = devm_add_action_or_reset(&pdev->dev, crystal_cove_charger_rm_irq_domain, charger);
112 if (ret)
113 return ret;
115 charger->charger_irq = irq_create_mapping(charger->irq_domain, 0);
116 if (!charger->charger_irq)
117 return -ENOMEM;
119 charger->irqchip.name = KBUILD_MODNAME;
120 charger->irqchip.irq_unmask = crystal_cove_charger_irq_unmask;
121 charger->irqchip.irq_mask = crystal_cove_charger_irq_mask;
122 charger->irqchip.irq_bus_lock = crystal_cove_charger_irq_bus_lock;
123 charger->irqchip.irq_bus_sync_unlock = crystal_cove_charger_irq_bus_sync_unlock;
125 irq_set_chip_data(charger->charger_irq, charger);
126 irq_set_chip_and_handler(charger->charger_irq, &charger->irqchip, handle_simple_irq);
127 irq_set_nested_thread(charger->charger_irq, true);
128 irq_set_noprobe(charger->charger_irq);
130 /* Mask the single 2nd level IRQ before enabling the 1st level IRQ */
131 charger->mask = charger->new_mask = BIT(0);
132 regmap_write(charger->regmap, MCHGRIRQ_REG, charger->mask);
134 ret = devm_request_threaded_irq(&pdev->dev, charger->irq, NULL,
135 crystal_cove_charger_irq,
136 IRQF_ONESHOT, KBUILD_MODNAME, charger);
137 if (ret)
138 return dev_err_probe(&pdev->dev, ret, "requesting irq\n");
140 return 0;
143 static struct platform_driver crystal_cove_charger_driver = {
144 .probe = crystal_cove_charger_probe,
145 .driver = {
146 .name = "crystal_cove_charger",
149 module_platform_driver(crystal_cove_charger_driver);
151 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com");
152 MODULE_DESCRIPTION("Intel Bay Trail Crystal Cove external charger IRQ pass-through");
153 MODULE_LICENSE("GPL");