1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2022 Analog Devices, Inc.
4 * Driver for the MAX77540 and MAX77541
8 #include <linux/interrupt.h>
9 #include <linux/mfd/core.h>
10 #include <linux/mfd/max77541.h>
11 #include <linux/property.h>
12 #include <linux/regmap.h>
14 static const struct regmap_config max77541_regmap_config
= {
19 static const struct regmap_irq max77541_src_irqs
[] = {
20 { .mask
= MAX77541_BIT_INT_SRC_TOPSYS
},
21 { .mask
= MAX77541_BIT_INT_SRC_BUCK
},
24 static const struct regmap_irq_chip max77541_src_irq_chip
= {
25 .name
= "max77541-src",
26 .status_base
= MAX77541_REG_INT_SRC
,
27 .mask_base
= MAX77541_REG_INT_SRC_M
,
29 .irqs
= max77541_src_irqs
,
30 .num_irqs
= ARRAY_SIZE(max77541_src_irqs
),
33 static const struct regmap_irq max77541_topsys_irqs
[] = {
34 { .mask
= MAX77541_BIT_TOPSYS_INT_TJ_120C
},
35 { .mask
= MAX77541_BIT_TOPSYS_INT_TJ_140C
},
36 { .mask
= MAX77541_BIT_TOPSYS_INT_TSHDN
},
37 { .mask
= MAX77541_BIT_TOPSYS_INT_UVLO
},
38 { .mask
= MAX77541_BIT_TOPSYS_INT_ALT_SWO
},
39 { .mask
= MAX77541_BIT_TOPSYS_INT_EXT_FREQ_DET
},
42 static const struct regmap_irq_chip max77541_topsys_irq_chip
= {
43 .name
= "max77541-topsys",
44 .status_base
= MAX77541_REG_TOPSYS_INT
,
45 .mask_base
= MAX77541_REG_TOPSYS_INT_M
,
47 .irqs
= max77541_topsys_irqs
,
48 .num_irqs
= ARRAY_SIZE(max77541_topsys_irqs
),
51 static const struct regmap_irq max77541_buck_irqs
[] = {
52 { .mask
= MAX77541_BIT_BUCK_INT_M1_POK_FLT
},
53 { .mask
= MAX77541_BIT_BUCK_INT_M2_POK_FLT
},
54 { .mask
= MAX77541_BIT_BUCK_INT_M1_SCFLT
},
55 { .mask
= MAX77541_BIT_BUCK_INT_M2_SCFLT
},
58 static const struct regmap_irq_chip max77541_buck_irq_chip
= {
59 .name
= "max77541-buck",
60 .status_base
= MAX77541_REG_BUCK_INT
,
61 .mask_base
= MAX77541_REG_BUCK_INT_M
,
63 .irqs
= max77541_buck_irqs
,
64 .num_irqs
= ARRAY_SIZE(max77541_buck_irqs
),
67 static const struct regmap_irq max77541_adc_irqs
[] = {
68 { .mask
= MAX77541_BIT_ADC_INT_CH1_I
},
69 { .mask
= MAX77541_BIT_ADC_INT_CH2_I
},
70 { .mask
= MAX77541_BIT_ADC_INT_CH3_I
},
71 { .mask
= MAX77541_BIT_ADC_INT_CH6_I
},
74 static const struct regmap_irq_chip max77541_adc_irq_chip
= {
75 .name
= "max77541-adc",
76 .status_base
= MAX77541_REG_ADC_INT
,
77 .mask_base
= MAX77541_REG_ADC_INT_M
,
79 .irqs
= max77541_adc_irqs
,
80 .num_irqs
= ARRAY_SIZE(max77541_adc_irqs
),
83 static const struct mfd_cell max77540_devs
[] = {
84 MFD_CELL_OF("max77540-regulator", NULL
, NULL
, 0, 0, NULL
),
87 static const struct mfd_cell max77541_devs
[] = {
88 MFD_CELL_OF("max77541-regulator", NULL
, NULL
, 0, 0, NULL
),
89 MFD_CELL_OF("max77541-adc", NULL
, NULL
, 0, 0, NULL
),
92 static int max77541_pmic_irq_init(struct device
*dev
)
94 struct max77541
*max77541
= dev_get_drvdata(dev
);
95 int irq
= max77541
->i2c
->irq
;
98 ret
= devm_regmap_add_irq_chip(dev
, max77541
->regmap
, irq
,
99 IRQF_ONESHOT
| IRQF_SHARED
, 0,
100 &max77541_src_irq_chip
,
101 &max77541
->irq_data
);
105 ret
= devm_regmap_add_irq_chip(dev
, max77541
->regmap
, irq
,
106 IRQF_ONESHOT
| IRQF_SHARED
, 0,
107 &max77541_topsys_irq_chip
,
108 &max77541
->irq_topsys
);
112 ret
= devm_regmap_add_irq_chip(dev
, max77541
->regmap
, irq
,
113 IRQF_ONESHOT
| IRQF_SHARED
, 0,
114 &max77541_buck_irq_chip
,
115 &max77541
->irq_buck
);
119 if (max77541
->id
== MAX77541
) {
120 ret
= devm_regmap_add_irq_chip(dev
, max77541
->regmap
, irq
,
121 IRQF_ONESHOT
| IRQF_SHARED
, 0,
122 &max77541_adc_irq_chip
,
131 static int max77541_pmic_setup(struct device
*dev
)
133 struct max77541
*max77541
= dev_get_drvdata(dev
);
134 const struct mfd_cell
*cells
;
138 switch (max77541
->id
) {
140 cells
= max77540_devs
;
141 n_devs
= ARRAY_SIZE(max77540_devs
);
144 cells
= max77541_devs
;
145 n_devs
= ARRAY_SIZE(max77541_devs
);
151 ret
= max77541_pmic_irq_init(dev
);
153 return dev_err_probe(dev
, ret
, "Failed to initialize IRQ\n");
155 ret
= device_init_wakeup(dev
, true);
157 return dev_err_probe(dev
, ret
, "Unable to init wakeup\n");
159 return devm_mfd_add_devices(dev
, PLATFORM_DEVID_NONE
,
160 cells
, n_devs
, NULL
, 0, NULL
);
163 static int max77541_probe(struct i2c_client
*client
)
165 struct device
*dev
= &client
->dev
;
166 struct max77541
*max77541
;
168 max77541
= devm_kzalloc(dev
, sizeof(*max77541
), GFP_KERNEL
);
172 i2c_set_clientdata(client
, max77541
);
173 max77541
->i2c
= client
;
175 max77541
->id
= (uintptr_t)i2c_get_match_data(client
);
179 max77541
->regmap
= devm_regmap_init_i2c(client
,
180 &max77541_regmap_config
);
181 if (IS_ERR(max77541
->regmap
))
182 return dev_err_probe(dev
, PTR_ERR(max77541
->regmap
),
183 "Failed to allocate register map\n");
185 return max77541_pmic_setup(dev
);
188 static const struct of_device_id max77541_of_id
[] = {
190 .compatible
= "adi,max77540",
191 .data
= (void *)MAX77540
,
194 .compatible
= "adi,max77541",
195 .data
= (void *)MAX77541
,
199 MODULE_DEVICE_TABLE(of
, max77541_of_id
);
201 static const struct i2c_device_id max77541_id
[] = {
202 { "max77540", MAX77540
},
203 { "max77541", MAX77541
},
206 MODULE_DEVICE_TABLE(i2c
, max77541_id
);
208 static struct i2c_driver max77541_driver
= {
211 .of_match_table
= max77541_of_id
,
213 .probe
= max77541_probe
,
214 .id_table
= max77541_id
,
216 module_i2c_driver(max77541_driver
);
218 MODULE_DESCRIPTION("MAX7740/MAX7741 Driver");
219 MODULE_AUTHOR("Okan Sahin <okan.sahin@analog.com>");
220 MODULE_LICENSE("GPL");