blob: d9026f9ff13e16046fa0006b528352148efc78cc [file] [log] [blame]
Guenter Roecka3eeb452011-01-26 20:15:27 -08001/*
2 * Hardware monitoring driver for Maxim MAX34440/MAX34441
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
28enum chips { max34440, max34441 };
29
Guenter Roeck98591dbe2011-07-09 13:17:43 -070030#define MAX34440_MFR_VOUT_PEAK 0xd4
31#define MAX34440_MFR_IOUT_PEAK 0xd5
32#define MAX34440_MFR_TEMPERATURE_PEAK 0xd6
33
Guenter Roecka3eeb452011-01-26 20:15:27 -080034#define MAX34440_STATUS_OC_WARN (1 << 0)
35#define MAX34440_STATUS_OC_FAULT (1 << 1)
36#define MAX34440_STATUS_OT_FAULT (1 << 5)
37#define MAX34440_STATUS_OT_WARN (1 << 6)
38
Guenter Roeck98591dbe2011-07-09 13:17:43 -070039static int max34440_read_word_data(struct i2c_client *client, int page, int reg)
40{
41 int ret;
42
43 switch (reg) {
44 case PMBUS_VIRT_READ_VOUT_MAX:
45 ret = pmbus_read_word_data(client, page,
46 MAX34440_MFR_VOUT_PEAK);
47 break;
48 case PMBUS_VIRT_READ_IOUT_MAX:
49 ret = pmbus_read_word_data(client, page,
50 MAX34440_MFR_IOUT_PEAK);
51 break;
52 case PMBUS_VIRT_READ_TEMP_MAX:
53 ret = pmbus_read_word_data(client, page,
54 MAX34440_MFR_TEMPERATURE_PEAK);
55 break;
56 case PMBUS_VIRT_RESET_VOUT_HISTORY:
57 case PMBUS_VIRT_RESET_IOUT_HISTORY:
58 case PMBUS_VIRT_RESET_TEMP_HISTORY:
59 ret = 0;
60 break;
61 default:
62 ret = -ENODATA;
63 break;
64 }
65 return ret;
66}
67
68static int max34440_write_word_data(struct i2c_client *client, int page,
69 int reg, u16 word)
70{
71 int ret;
72
73 switch (reg) {
74 case PMBUS_VIRT_RESET_VOUT_HISTORY:
75 ret = pmbus_write_word_data(client, page,
76 MAX34440_MFR_VOUT_PEAK, 0);
77 break;
78 case PMBUS_VIRT_RESET_IOUT_HISTORY:
79 ret = pmbus_write_word_data(client, page,
80 MAX34440_MFR_IOUT_PEAK, 0);
81 break;
82 case PMBUS_VIRT_RESET_TEMP_HISTORY:
83 ret = pmbus_write_word_data(client, page,
84 MAX34440_MFR_TEMPERATURE_PEAK,
Guenter Roeckdc91ad82012-02-24 03:44:34 -080085 0x8000);
Guenter Roeck98591dbe2011-07-09 13:17:43 -070086 break;
87 default:
88 ret = -ENODATA;
89 break;
90 }
91 return ret;
92}
93
Guenter Roeck2cfa6ae2011-03-08 23:00:10 -080094static int max34440_read_byte_data(struct i2c_client *client, int page, int reg)
Guenter Roecka3eeb452011-01-26 20:15:27 -080095{
Guenter Roeckda8e48a2011-07-29 22:19:39 -070096 int ret = 0;
Guenter Roecka3eeb452011-01-26 20:15:27 -080097 int mfg_status;
98
Guenter Roeckda8e48a2011-07-29 22:19:39 -070099 if (page >= 0) {
100 ret = pmbus_set_page(client, page);
101 if (ret < 0)
102 return ret;
103 }
Guenter Roecka3eeb452011-01-26 20:15:27 -0800104
105 switch (reg) {
106 case PMBUS_STATUS_IOUT:
107 mfg_status = pmbus_read_word_data(client, 0,
108 PMBUS_STATUS_MFR_SPECIFIC);
109 if (mfg_status < 0)
110 return mfg_status;
111 if (mfg_status & MAX34440_STATUS_OC_WARN)
112 ret |= PB_IOUT_OC_WARNING;
113 if (mfg_status & MAX34440_STATUS_OC_FAULT)
114 ret |= PB_IOUT_OC_FAULT;
115 break;
116 case PMBUS_STATUS_TEMPERATURE:
117 mfg_status = pmbus_read_word_data(client, 0,
118 PMBUS_STATUS_MFR_SPECIFIC);
119 if (mfg_status < 0)
120 return mfg_status;
121 if (mfg_status & MAX34440_STATUS_OT_WARN)
122 ret |= PB_TEMP_OT_WARNING;
123 if (mfg_status & MAX34440_STATUS_OT_FAULT)
124 ret |= PB_TEMP_OT_FAULT;
125 break;
126 default:
127 ret = -ENODATA;
128 break;
129 }
130 return ret;
131}
132
133static struct pmbus_driver_info max34440_info[] = {
134 [max34440] = {
135 .pages = 14,
Guenter Roeck1061d852011-06-25 11:21:49 -0700136 .format[PSC_VOLTAGE_IN] = direct,
137 .format[PSC_VOLTAGE_OUT] = direct,
138 .format[PSC_TEMPERATURE] = direct,
139 .format[PSC_CURRENT_OUT] = direct,
Guenter Roecka3eeb452011-01-26 20:15:27 -0800140 .m[PSC_VOLTAGE_IN] = 1,
141 .b[PSC_VOLTAGE_IN] = 0,
142 .R[PSC_VOLTAGE_IN] = 3, /* R = 0 in datasheet reflects mV */
143 .m[PSC_VOLTAGE_OUT] = 1,
144 .b[PSC_VOLTAGE_OUT] = 0,
145 .R[PSC_VOLTAGE_OUT] = 3, /* R = 0 in datasheet reflects mV */
146 .m[PSC_CURRENT_OUT] = 1,
147 .b[PSC_CURRENT_OUT] = 0,
148 .R[PSC_CURRENT_OUT] = 3, /* R = 0 in datasheet reflects mA */
149 .m[PSC_TEMPERATURE] = 1,
150 .b[PSC_TEMPERATURE] = 0,
151 .R[PSC_TEMPERATURE] = 2,
152 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
153 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
154 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
155 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
156 .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
157 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
158 .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
159 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
160 .func[4] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
161 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
162 .func[5] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
163 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
164 .func[6] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
165 .func[7] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
166 .func[8] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
167 .func[9] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
168 .func[10] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
169 .func[11] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
170 .func[12] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
171 .func[13] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
Guenter Roeck2cfa6ae2011-03-08 23:00:10 -0800172 .read_byte_data = max34440_read_byte_data,
Guenter Roeck98591dbe2011-07-09 13:17:43 -0700173 .read_word_data = max34440_read_word_data,
174 .write_word_data = max34440_write_word_data,
Guenter Roecka3eeb452011-01-26 20:15:27 -0800175 },
176 [max34441] = {
177 .pages = 12,
Guenter Roeck1061d852011-06-25 11:21:49 -0700178 .format[PSC_VOLTAGE_IN] = direct,
179 .format[PSC_VOLTAGE_OUT] = direct,
180 .format[PSC_TEMPERATURE] = direct,
181 .format[PSC_CURRENT_OUT] = direct,
182 .format[PSC_FAN] = direct,
Guenter Roecka3eeb452011-01-26 20:15:27 -0800183 .m[PSC_VOLTAGE_IN] = 1,
184 .b[PSC_VOLTAGE_IN] = 0,
185 .R[PSC_VOLTAGE_IN] = 3,
186 .m[PSC_VOLTAGE_OUT] = 1,
187 .b[PSC_VOLTAGE_OUT] = 0,
188 .R[PSC_VOLTAGE_OUT] = 3,
189 .m[PSC_CURRENT_OUT] = 1,
190 .b[PSC_CURRENT_OUT] = 0,
191 .R[PSC_CURRENT_OUT] = 3,
192 .m[PSC_TEMPERATURE] = 1,
193 .b[PSC_TEMPERATURE] = 0,
194 .R[PSC_TEMPERATURE] = 2,
195 .m[PSC_FAN] = 1,
196 .b[PSC_FAN] = 0,
197 .R[PSC_FAN] = 0,
198 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
199 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
200 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
201 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
202 .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
203 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
204 .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
205 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
206 .func[4] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
207 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
208 .func[5] = PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12,
209 .func[6] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
210 .func[7] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
211 .func[8] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
212 .func[9] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
213 .func[10] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
214 .func[11] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
Guenter Roeck2cfa6ae2011-03-08 23:00:10 -0800215 .read_byte_data = max34440_read_byte_data,
Guenter Roeck98591dbe2011-07-09 13:17:43 -0700216 .read_word_data = max34440_read_word_data,
217 .write_word_data = max34440_write_word_data,
Guenter Roecka3eeb452011-01-26 20:15:27 -0800218 },
219};
220
221static int max34440_probe(struct i2c_client *client,
222 const struct i2c_device_id *id)
223{
224 return pmbus_do_probe(client, id, &max34440_info[id->driver_data]);
225}
226
227static int max34440_remove(struct i2c_client *client)
228{
Guenter Roeck866cf122011-08-26 08:12:38 -0700229 pmbus_do_remove(client);
230 return 0;
Guenter Roecka3eeb452011-01-26 20:15:27 -0800231}
232
233static const struct i2c_device_id max34440_id[] = {
234 {"max34440", max34440},
235 {"max34441", max34441},
236 {}
237};
238
239MODULE_DEVICE_TABLE(i2c, max34440_id);
240
241/* This is the driver that will be inserted */
242static struct i2c_driver max34440_driver = {
243 .driver = {
244 .name = "max34440",
245 },
246 .probe = max34440_probe,
247 .remove = max34440_remove,
248 .id_table = max34440_id,
249};
250
Axel Linf0967ee2012-01-20 15:38:18 +0800251module_i2c_driver(max34440_driver);
Guenter Roecka3eeb452011-01-26 20:15:27 -0800252
253MODULE_AUTHOR("Guenter Roeck");
254MODULE_DESCRIPTION("PMBus driver for Maxim MAX34440/MAX34441");
255MODULE_LICENSE("GPL");