1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * An hwmon driver for the NXP MC34VR500 PMIC
5 * Author: Mario Kicherer <dev@kicherer.org>
8 #include <linux/bits.h>
9 #include <linux/dev_printk.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/hwmon.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqreturn.h>
17 #include <linux/module.h>
19 #include <linux/regmap.h>
21 #define MC34VR500_I2C_ADDR 0x08
22 #define MC34VR500_DEVICEID_VALUE 0x14
25 #define ENS_BIT BIT(0)
26 #define LOWVINS_BIT BIT(1)
27 #define THERM110S_BIT BIT(2)
28 #define THERM120S_BIT BIT(3)
29 #define THERM125S_BIT BIT(4)
30 #define THERM130S_BIT BIT(5)
32 #define MC34VR500_DEVICEID 0x00
34 #define MC34VR500_SILICONREVID 0x03
35 #define MC34VR500_FABID 0x04
36 #define MC34VR500_INTSTAT0 0x05
37 #define MC34VR500_INTMASK0 0x06
38 #define MC34VR500_INTSENSE0 0x07
40 struct mc34vr500_data {
41 struct device *hwmon_dev;
42 struct regmap *regmap;
45 static irqreturn_t mc34vr500_process_interrupt(int irq, void *userdata)
47 struct mc34vr500_data *data = (struct mc34vr500_data *)userdata;
51 ret = regmap_read(data->regmap, MC34VR500_INTSTAT0, ®);
56 if (reg & LOWVINS_BIT)
57 hwmon_notify_event(data->hwmon_dev, hwmon_in,
58 hwmon_in_min_alarm, 0);
60 if (reg & THERM110S_BIT)
61 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
62 hwmon_temp_max_alarm, 0);
64 if (reg & THERM120S_BIT)
65 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
66 hwmon_temp_crit_alarm, 0);
68 if (reg & THERM130S_BIT)
69 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
70 hwmon_temp_emergency_alarm, 0);
72 /* write 1 to clear */
73 regmap_write(data->regmap, MC34VR500_INTSTAT0, LOWVINS_BIT |
74 THERM110S_BIT | THERM120S_BIT | THERM130S_BIT);
80 static umode_t mc34vr500_is_visible(const void *data,
81 enum hwmon_sensor_types type,
82 u32 attr, int channel)
85 case hwmon_in_min_alarm:
86 case hwmon_temp_max_alarm:
87 case hwmon_temp_crit_alarm:
88 case hwmon_temp_emergency_alarm:
97 static int mc34vr500_alarm_read(struct mc34vr500_data *data, int index,
103 ret = regmap_read(data->regmap, MC34VR500_INTSENSE0, ®);
107 *val = !!(reg & index);
112 static int mc34vr500_read(struct device *dev, enum hwmon_sensor_types type,
113 u32 attr, int channel, long *val)
115 struct mc34vr500_data *data = dev_get_drvdata(dev);
120 case hwmon_in_min_alarm:
121 return mc34vr500_alarm_read(data, LOWVINS_BIT, val);
127 case hwmon_temp_max_alarm:
128 return mc34vr500_alarm_read(data, THERM110S_BIT, val);
129 case hwmon_temp_crit_alarm:
130 return mc34vr500_alarm_read(data, THERM120S_BIT, val);
131 case hwmon_temp_emergency_alarm:
132 return mc34vr500_alarm_read(data, THERM130S_BIT, val);
141 static const struct hwmon_channel_info * const mc34vr500_info[] = {
142 HWMON_CHANNEL_INFO(in, HWMON_I_MIN_ALARM),
143 HWMON_CHANNEL_INFO(temp, HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM
144 | HWMON_T_EMERGENCY_ALARM),
148 static const struct hwmon_ops mc34vr500_hwmon_ops = {
149 .is_visible = mc34vr500_is_visible,
150 .read = mc34vr500_read,
153 static const struct hwmon_chip_info mc34vr500_chip_info = {
154 .ops = &mc34vr500_hwmon_ops,
155 .info = mc34vr500_info,
158 static const struct regmap_config mc34vr500_regmap_config = {
161 .max_register = MC34VR500_INTSENSE0,
164 static int mc34vr500_probe(struct i2c_client *client)
166 struct device *dev = &client->dev;
167 struct mc34vr500_data *data;
168 struct device *hwmon_dev;
170 unsigned int reg, revid, fabid;
171 struct regmap *regmap;
173 regmap = devm_regmap_init_i2c(client, &mc34vr500_regmap_config);
175 return PTR_ERR(regmap);
177 data = devm_kzalloc(dev, sizeof(struct mc34vr500_data), GFP_KERNEL);
181 data->regmap = regmap;
183 ret = regmap_read(regmap, MC34VR500_DEVICEID, ®);
187 if (reg != MC34VR500_DEVICEID_VALUE)
190 ret = regmap_read(regmap, MC34VR500_SILICONREVID, &revid);
194 ret = regmap_read(regmap, MC34VR500_FABID, &fabid);
198 dev_dbg(dev, "mc34vr500: revid 0x%x fabid 0x%x\n", revid, fabid);
200 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
202 &mc34vr500_chip_info,
204 if (IS_ERR(hwmon_dev))
205 return PTR_ERR(hwmon_dev);
207 data->hwmon_dev = hwmon_dev;
210 ret = devm_request_threaded_irq(dev, client->irq, NULL,
211 mc34vr500_process_interrupt,
212 IRQF_TRIGGER_RISING |
215 dev_name(dev), data);
219 /* write 1 to clear interrupts */
220 ret = regmap_write(regmap, MC34VR500_INTSTAT0, LOWVINS_BIT |
221 THERM110S_BIT | THERM120S_BIT |
226 /* unmask interrupts */
227 ret = regmap_write(regmap, MC34VR500_INTMASK0,
228 (unsigned int) ~(LOWVINS_BIT | THERM110S_BIT |
229 THERM120S_BIT | THERM130S_BIT));
237 static const struct i2c_device_id mc34vr500_id[] = {
241 MODULE_DEVICE_TABLE(i2c, mc34vr500_id);
243 static const struct of_device_id __maybe_unused mc34vr500_of_match[] = {
244 { .compatible = "nxp,mc34vr500" },
247 MODULE_DEVICE_TABLE(of, mc34vr500_of_match);
249 static struct i2c_driver mc34vr500_driver = {
252 .of_match_table = of_match_ptr(mc34vr500_of_match),
254 .probe = mc34vr500_probe,
255 .id_table = mc34vr500_id,
258 module_i2c_driver(mc34vr500_driver);
260 MODULE_AUTHOR("Mario Kicherer <dev@kicherer.org>");
262 MODULE_DESCRIPTION("MC34VR500 driver");
263 MODULE_LICENSE("GPL");