xref: /openbmc/linux/drivers/hwmon/pmbus/max8688.c (revision c819e2cf)
1 /*
2  * Hardware monitoring driver for Maxim MAX8688
3  *
4  * Copyright (c) 2011 Ericsson AB.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
25 #include <linux/i2c.h>
26 #include "pmbus.h"
27 
28 #define MAX8688_MFR_VOUT_PEAK		0xd4
29 #define MAX8688_MFR_IOUT_PEAK		0xd5
30 #define MAX8688_MFR_TEMPERATURE_PEAK	0xd6
31 #define MAX8688_MFG_STATUS		0xd8
32 
33 #define MAX8688_STATUS_OC_FAULT		(1 << 4)
34 #define MAX8688_STATUS_OV_FAULT		(1 << 5)
35 #define MAX8688_STATUS_OV_WARNING	(1 << 8)
36 #define MAX8688_STATUS_UV_FAULT		(1 << 9)
37 #define MAX8688_STATUS_UV_WARNING	(1 << 10)
38 #define MAX8688_STATUS_UC_FAULT		(1 << 11)
39 #define MAX8688_STATUS_OC_WARNING	(1 << 12)
40 #define MAX8688_STATUS_OT_FAULT		(1 << 13)
41 #define MAX8688_STATUS_OT_WARNING	(1 << 14)
42 
43 static int max8688_read_word_data(struct i2c_client *client, int page, int reg)
44 {
45 	int ret;
46 
47 	if (page)
48 		return -ENXIO;
49 
50 	switch (reg) {
51 	case PMBUS_VIRT_READ_VOUT_MAX:
52 		ret = pmbus_read_word_data(client, 0, MAX8688_MFR_VOUT_PEAK);
53 		break;
54 	case PMBUS_VIRT_READ_IOUT_MAX:
55 		ret = pmbus_read_word_data(client, 0, MAX8688_MFR_IOUT_PEAK);
56 		break;
57 	case PMBUS_VIRT_READ_TEMP_MAX:
58 		ret = pmbus_read_word_data(client, 0,
59 					   MAX8688_MFR_TEMPERATURE_PEAK);
60 		break;
61 	case PMBUS_VIRT_RESET_VOUT_HISTORY:
62 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
63 	case PMBUS_VIRT_RESET_TEMP_HISTORY:
64 		ret = 0;
65 		break;
66 	default:
67 		ret = -ENODATA;
68 		break;
69 	}
70 	return ret;
71 }
72 
73 static int max8688_write_word_data(struct i2c_client *client, int page, int reg,
74 				   u16 word)
75 {
76 	int ret;
77 
78 	switch (reg) {
79 	case PMBUS_VIRT_RESET_VOUT_HISTORY:
80 		ret = pmbus_write_word_data(client, 0, MAX8688_MFR_VOUT_PEAK,
81 					    0);
82 		break;
83 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
84 		ret = pmbus_write_word_data(client, 0, MAX8688_MFR_IOUT_PEAK,
85 					    0);
86 		break;
87 	case PMBUS_VIRT_RESET_TEMP_HISTORY:
88 		ret = pmbus_write_word_data(client, 0,
89 					    MAX8688_MFR_TEMPERATURE_PEAK,
90 					    0xffff);
91 		break;
92 	default:
93 		ret = -ENODATA;
94 		break;
95 	}
96 	return ret;
97 }
98 
99 static int max8688_read_byte_data(struct i2c_client *client, int page, int reg)
100 {
101 	int ret = 0;
102 	int mfg_status;
103 
104 	if (page > 0)
105 		return -ENXIO;
106 
107 	switch (reg) {
108 	case PMBUS_STATUS_VOUT:
109 		mfg_status = pmbus_read_word_data(client, 0,
110 						  MAX8688_MFG_STATUS);
111 		if (mfg_status < 0)
112 			return mfg_status;
113 		if (mfg_status & MAX8688_STATUS_UV_WARNING)
114 			ret |= PB_VOLTAGE_UV_WARNING;
115 		if (mfg_status & MAX8688_STATUS_UV_FAULT)
116 			ret |= PB_VOLTAGE_UV_FAULT;
117 		if (mfg_status & MAX8688_STATUS_OV_WARNING)
118 			ret |= PB_VOLTAGE_OV_WARNING;
119 		if (mfg_status & MAX8688_STATUS_OV_FAULT)
120 			ret |= PB_VOLTAGE_OV_FAULT;
121 		break;
122 	case PMBUS_STATUS_IOUT:
123 		mfg_status = pmbus_read_word_data(client, 0,
124 						  MAX8688_MFG_STATUS);
125 		if (mfg_status < 0)
126 			return mfg_status;
127 		if (mfg_status & MAX8688_STATUS_UC_FAULT)
128 			ret |= PB_IOUT_UC_FAULT;
129 		if (mfg_status & MAX8688_STATUS_OC_WARNING)
130 			ret |= PB_IOUT_OC_WARNING;
131 		if (mfg_status & MAX8688_STATUS_OC_FAULT)
132 			ret |= PB_IOUT_OC_FAULT;
133 		break;
134 	case PMBUS_STATUS_TEMPERATURE:
135 		mfg_status = pmbus_read_word_data(client, 0,
136 						  MAX8688_MFG_STATUS);
137 		if (mfg_status < 0)
138 			return mfg_status;
139 		if (mfg_status & MAX8688_STATUS_OT_WARNING)
140 			ret |= PB_TEMP_OT_WARNING;
141 		if (mfg_status & MAX8688_STATUS_OT_FAULT)
142 			ret |= PB_TEMP_OT_FAULT;
143 		break;
144 	default:
145 		ret = -ENODATA;
146 		break;
147 	}
148 	return ret;
149 }
150 
151 static struct pmbus_driver_info max8688_info = {
152 	.pages = 1,
153 	.format[PSC_VOLTAGE_IN] = direct,
154 	.format[PSC_VOLTAGE_OUT] = direct,
155 	.format[PSC_TEMPERATURE] = direct,
156 	.format[PSC_CURRENT_OUT] = direct,
157 	.m[PSC_VOLTAGE_IN] = 19995,
158 	.b[PSC_VOLTAGE_IN] = 0,
159 	.R[PSC_VOLTAGE_IN] = -1,
160 	.m[PSC_VOLTAGE_OUT] = 19995,
161 	.b[PSC_VOLTAGE_OUT] = 0,
162 	.R[PSC_VOLTAGE_OUT] = -1,
163 	.m[PSC_CURRENT_OUT] = 23109,
164 	.b[PSC_CURRENT_OUT] = 0,
165 	.R[PSC_CURRENT_OUT] = -2,
166 	.m[PSC_TEMPERATURE] = -7612,
167 	.b[PSC_TEMPERATURE] = 335,
168 	.R[PSC_TEMPERATURE] = -3,
169 	.func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP
170 		| PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
171 		| PMBUS_HAVE_STATUS_TEMP,
172 	.read_byte_data = max8688_read_byte_data,
173 	.read_word_data = max8688_read_word_data,
174 	.write_word_data = max8688_write_word_data,
175 };
176 
177 static int max8688_probe(struct i2c_client *client,
178 			 const struct i2c_device_id *id)
179 {
180 	return pmbus_do_probe(client, id, &max8688_info);
181 }
182 
183 static const struct i2c_device_id max8688_id[] = {
184 	{"max8688", 0},
185 	{ }
186 };
187 
188 MODULE_DEVICE_TABLE(i2c, max8688_id);
189 
190 /* This is the driver that will be inserted */
191 static struct i2c_driver max8688_driver = {
192 	.driver = {
193 		   .name = "max8688",
194 		   },
195 	.probe = max8688_probe,
196 	.remove = pmbus_do_remove,
197 	.id_table = max8688_id,
198 };
199 
200 module_i2c_driver(max8688_driver);
201 
202 MODULE_AUTHOR("Guenter Roeck");
203 MODULE_DESCRIPTION("PMBus driver for Maxim MAX8688");
204 MODULE_LICENSE("GPL");
205