xref: /linux/drivers/mfd/88pm886.c (revision 80739fd00c7ea1315d362ce889bef499452913ef)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/i2c.h>
3 #include <linux/mfd/core.h>
4 #include <linux/module.h>
5 #include <linux/notifier.h>
6 #include <linux/of.h>
7 #include <linux/platform_device.h>
8 #include <linux/reboot.h>
9 #include <linux/regmap.h>
10 
11 #include <linux/mfd/88pm886.h>
12 
13 static const struct regmap_config pm886_regmap_config = {
14 	.reg_bits = 8,
15 	.val_bits = 8,
16 	.max_register = PM886_REG_RTC_SPARE6,
17 };
18 
19 static struct regmap_irq pm886_regmap_irqs[] = {
20 	REGMAP_IRQ_REG(PM886_IRQ_ONKEY, 0, PM886_INT_ENA1_ONKEY),
21 };
22 
23 static struct regmap_irq_chip pm886_regmap_irq_chip = {
24 	.name = "88pm886",
25 	.irqs = pm886_regmap_irqs,
26 	.num_irqs = ARRAY_SIZE(pm886_regmap_irqs),
27 	.num_regs = 4,
28 	.status_base = PM886_REG_INT_STATUS1,
29 	.ack_base = PM886_REG_INT_STATUS1,
30 	.unmask_base = PM886_REG_INT_ENA_1,
31 };
32 
33 static struct resource pm886_onkey_resources[] = {
34 	DEFINE_RES_IRQ_NAMED(PM886_IRQ_ONKEY, "88pm886-onkey"),
35 };
36 
37 static struct mfd_cell pm886_devs[] = {
38 	MFD_CELL_RES("88pm886-onkey", pm886_onkey_resources),
39 	MFD_CELL_NAME("88pm886-regulator"),
40 	MFD_CELL_NAME("88pm886-rtc"),
41 };
42 
pm886_power_off_handler(struct sys_off_data * sys_off_data)43 static int pm886_power_off_handler(struct sys_off_data *sys_off_data)
44 {
45 	struct pm886_chip *chip = sys_off_data->cb_data;
46 	struct regmap *regmap = chip->regmap;
47 	struct device *dev = &chip->client->dev;
48 	int err;
49 
50 	err = regmap_update_bits(regmap, PM886_REG_MISC_CONFIG1, PM886_SW_PDOWN, PM886_SW_PDOWN);
51 	if (err) {
52 		dev_err(dev, "Failed to power off the device: %d\n", err);
53 		return NOTIFY_BAD;
54 	}
55 	return NOTIFY_DONE;
56 }
57 
pm886_setup_irq(struct pm886_chip * chip,struct regmap_irq_chip_data ** irq_data)58 static int pm886_setup_irq(struct pm886_chip *chip,
59 		struct regmap_irq_chip_data **irq_data)
60 {
61 	struct regmap *regmap = chip->regmap;
62 	struct device *dev = &chip->client->dev;
63 	int err;
64 
65 	/* Set interrupt clearing mode to clear on write. */
66 	err = regmap_update_bits(regmap, PM886_REG_MISC_CONFIG2,
67 			PM886_INT_INV | PM886_INT_CLEAR | PM886_INT_MASK_MODE,
68 			PM886_INT_WC);
69 	if (err) {
70 		dev_err(dev, "Failed to set interrupt clearing mode: %d\n", err);
71 		return err;
72 	}
73 
74 	err = devm_regmap_add_irq_chip(dev, regmap, chip->client->irq,
75 					IRQF_ONESHOT, 0, &pm886_regmap_irq_chip,
76 					irq_data);
77 	if (err) {
78 		dev_err(dev, "Failed to request IRQ: %d\n", err);
79 		return err;
80 	}
81 
82 	return 0;
83 }
84 
pm886_probe(struct i2c_client * client)85 static int pm886_probe(struct i2c_client *client)
86 {
87 	struct regmap_irq_chip_data *irq_data;
88 	struct device *dev = &client->dev;
89 	struct pm886_chip *chip;
90 	struct regmap *regmap;
91 	unsigned int chip_id;
92 	int err;
93 
94 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
95 	if (!chip)
96 		return -ENOMEM;
97 
98 	chip->client = client;
99 	chip->chip_id = (uintptr_t)device_get_match_data(dev);
100 	i2c_set_clientdata(client, chip);
101 
102 	regmap = devm_regmap_init_i2c(client, &pm886_regmap_config);
103 	if (IS_ERR(regmap))
104 		return dev_err_probe(dev, PTR_ERR(regmap), "Failed to initialize regmap\n");
105 	chip->regmap = regmap;
106 
107 	err = regmap_read(regmap, PM886_REG_ID, &chip_id);
108 	if (err)
109 		return dev_err_probe(dev, err, "Failed to read chip ID\n");
110 
111 	if (chip->chip_id != chip_id)
112 		return dev_err_probe(dev, -EINVAL, "Unsupported chip: 0x%x\n", chip_id);
113 
114 	err = pm886_setup_irq(chip, &irq_data);
115 	if (err)
116 		return err;
117 
118 	err = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE, pm886_devs, ARRAY_SIZE(pm886_devs),
119 				NULL, 0, regmap_irq_get_domain(irq_data));
120 	if (err)
121 		return dev_err_probe(dev, err, "Failed to add devices\n");
122 
123 	err = devm_register_power_off_handler(dev, pm886_power_off_handler, chip);
124 	if (err)
125 		return dev_err_probe(dev, err, "Failed to register power off handler\n");
126 
127 	device_init_wakeup(dev, device_property_read_bool(dev, "wakeup-source"));
128 
129 	return 0;
130 }
131 
132 static const struct of_device_id pm886_of_match[] = {
133 	{ .compatible = "marvell,88pm886-a1", .data = (void *)PM886_A1_CHIP_ID },
134 	{ }
135 };
136 MODULE_DEVICE_TABLE(of, pm886_of_match);
137 
138 static struct i2c_driver pm886_i2c_driver = {
139 	.driver = {
140 		.name = "88pm886",
141 		.of_match_table = pm886_of_match,
142 	},
143 	.probe = pm886_probe,
144 };
145 module_i2c_driver(pm886_i2c_driver);
146 
147 MODULE_DESCRIPTION("Marvell 88PM886 PMIC driver");
148 MODULE_AUTHOR("Karel Balej <balejk@matfyz.cz>");
149 MODULE_LICENSE("GPL");
150