xref: /openbmc/linux/drivers/hwmon/pmbus/ir35221.c (revision ae213c44)
1 /*
2  * Hardware monitoring driver for IR35221
3  *
4  * Copyright (C) IBM Corporation 2017.
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
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include "pmbus.h"
18 
19 #define IR35221_MFR_VIN_PEAK		0xc5
20 #define IR35221_MFR_VOUT_PEAK		0xc6
21 #define IR35221_MFR_IOUT_PEAK		0xc7
22 #define IR35221_MFR_TEMP_PEAK		0xc8
23 #define IR35221_MFR_VIN_VALLEY		0xc9
24 #define IR35221_MFR_VOUT_VALLEY		0xca
25 #define IR35221_MFR_IOUT_VALLEY		0xcb
26 #define IR35221_MFR_TEMP_VALLEY		0xcc
27 
28 static int ir35221_read_word_data(struct i2c_client *client, int page, int reg)
29 {
30 	int ret;
31 
32 	switch (reg) {
33 	case PMBUS_VIRT_READ_VIN_MAX:
34 		ret = pmbus_read_word_data(client, page, IR35221_MFR_VIN_PEAK);
35 		break;
36 	case PMBUS_VIRT_READ_VOUT_MAX:
37 		ret = pmbus_read_word_data(client, page, IR35221_MFR_VOUT_PEAK);
38 		break;
39 	case PMBUS_VIRT_READ_IOUT_MAX:
40 		ret = pmbus_read_word_data(client, page, IR35221_MFR_IOUT_PEAK);
41 		break;
42 	case PMBUS_VIRT_READ_TEMP_MAX:
43 		ret = pmbus_read_word_data(client, page, IR35221_MFR_TEMP_PEAK);
44 		break;
45 	case PMBUS_VIRT_READ_VIN_MIN:
46 		ret = pmbus_read_word_data(client, page,
47 					   IR35221_MFR_VIN_VALLEY);
48 		break;
49 	case PMBUS_VIRT_READ_VOUT_MIN:
50 		ret = pmbus_read_word_data(client, page,
51 					   IR35221_MFR_VOUT_VALLEY);
52 		break;
53 	case PMBUS_VIRT_READ_IOUT_MIN:
54 		ret = pmbus_read_word_data(client, page,
55 					   IR35221_MFR_IOUT_VALLEY);
56 		break;
57 	case PMBUS_VIRT_READ_TEMP_MIN:
58 		ret = pmbus_read_word_data(client, page,
59 					   IR35221_MFR_TEMP_VALLEY);
60 		break;
61 	default:
62 		ret = -ENODATA;
63 		break;
64 	}
65 
66 	return ret;
67 }
68 
69 static int ir35221_probe(struct i2c_client *client,
70 			 const struct i2c_device_id *id)
71 {
72 	struct pmbus_driver_info *info;
73 	u8 buf[I2C_SMBUS_BLOCK_MAX];
74 	int ret;
75 
76 	if (!i2c_check_functionality(client->adapter,
77 				     I2C_FUNC_SMBUS_READ_BYTE_DATA
78 				| I2C_FUNC_SMBUS_READ_WORD_DATA
79 				| I2C_FUNC_SMBUS_READ_BLOCK_DATA))
80 		return -ENODEV;
81 
82 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
83 	if (ret < 0) {
84 		dev_err(&client->dev, "Failed to read PMBUS_MFR_ID\n");
85 		return ret;
86 	}
87 	if (ret != 2 || strncmp(buf, "RI", strlen("RI"))) {
88 		dev_err(&client->dev, "MFR_ID unrecognised\n");
89 		return -ENODEV;
90 	}
91 
92 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
93 	if (ret < 0) {
94 		dev_err(&client->dev, "Failed to read PMBUS_MFR_MODEL\n");
95 		return ret;
96 	}
97 	if (ret != 2 || !(buf[0] == 0x6c && buf[1] == 0x00)) {
98 		dev_err(&client->dev, "MFR_MODEL unrecognised\n");
99 		return -ENODEV;
100 	}
101 
102 	info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info),
103 			    GFP_KERNEL);
104 	if (!info)
105 		return -ENOMEM;
106 
107 	info->read_word_data = ir35221_read_word_data;
108 
109 	info->pages = 2;
110 	info->format[PSC_VOLTAGE_IN] = linear;
111 	info->format[PSC_VOLTAGE_OUT] = linear;
112 	info->format[PSC_CURRENT_IN] = linear;
113 	info->format[PSC_CURRENT_OUT] = linear;
114 	info->format[PSC_POWER] = linear;
115 	info->format[PSC_TEMPERATURE] = linear;
116 
117 	info->func[0] = PMBUS_HAVE_VIN
118 		| PMBUS_HAVE_VOUT | PMBUS_HAVE_IIN
119 		| PMBUS_HAVE_IOUT | PMBUS_HAVE_PIN
120 		| PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
121 		| PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
122 		| PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP;
123 	info->func[1] = info->func[0];
124 
125 	return pmbus_do_probe(client, id, info);
126 }
127 
128 static const struct i2c_device_id ir35221_id[] = {
129 	{"ir35221", 0},
130 	{}
131 };
132 
133 MODULE_DEVICE_TABLE(i2c, ir35221_id);
134 
135 static struct i2c_driver ir35221_driver = {
136 	.driver = {
137 		.name	= "ir35221",
138 	},
139 	.probe		= ir35221_probe,
140 	.remove		= pmbus_do_remove,
141 	.id_table	= ir35221_id,
142 };
143 
144 module_i2c_driver(ir35221_driver);
145 
146 MODULE_AUTHOR("Samuel Mendoza-Jonas <sam@mendozajonas.com");
147 MODULE_DESCRIPTION("PMBus driver for IR35221");
148 MODULE_LICENSE("GPL");
149