Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
max34440.c
Go to the documentation of this file.
1 /*
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 
29 
30 #define MAX34440_MFR_VOUT_PEAK 0xd4
31 #define MAX34440_MFR_IOUT_PEAK 0xd5
32 #define MAX34440_MFR_TEMPERATURE_PEAK 0xd6
33 #define MAX34440_MFR_VOUT_MIN 0xd7
34 
35 #define MAX34446_MFR_POUT_PEAK 0xe0
36 #define MAX34446_MFR_POUT_AVG 0xe1
37 #define MAX34446_MFR_IOUT_AVG 0xe2
38 #define MAX34446_MFR_TEMPERATURE_AVG 0xe3
39 
40 #define MAX34440_STATUS_OC_WARN (1 << 0)
41 #define MAX34440_STATUS_OC_FAULT (1 << 1)
42 #define MAX34440_STATUS_OT_FAULT (1 << 5)
43 #define MAX34440_STATUS_OT_WARN (1 << 6)
44 
45 struct max34440_data {
46  int id;
48 };
49 
50 #define to_max34440_data(x) container_of(x, struct max34440_data, info)
51 
52 static int max34440_read_word_data(struct i2c_client *client, int page, int reg)
53 {
54  int ret;
55  const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
56  const struct max34440_data *data = to_max34440_data(info);
57 
58  switch (reg) {
60  ret = pmbus_read_word_data(client, page,
62  break;
64  ret = pmbus_read_word_data(client, page,
66  break;
68  if (data->id != max34446)
69  return -ENXIO;
70  ret = pmbus_read_word_data(client, page,
72  break;
74  ret = pmbus_read_word_data(client, page,
76  break;
78  if (data->id != max34446)
79  return -ENXIO;
80  ret = pmbus_read_word_data(client, page,
82  break;
84  if (data->id != max34446)
85  return -ENXIO;
86  ret = pmbus_read_word_data(client, page,
88  break;
90  if (data->id != max34446)
91  return -ENXIO;
92  ret = pmbus_read_word_data(client, page,
94  break;
96  ret = pmbus_read_word_data(client, page,
98  break;
100  if (data->id != max34446)
101  return -ENXIO;
102  ret = 0;
103  break;
107  ret = 0;
108  break;
109  default:
110  ret = -ENODATA;
111  break;
112  }
113  return ret;
114 }
115 
116 static int max34440_write_word_data(struct i2c_client *client, int page,
117  int reg, u16 word)
118 {
119  const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
120  const struct max34440_data *data = to_max34440_data(info);
121  int ret;
122 
123  switch (reg) {
125  ret = pmbus_write_word_data(client, page,
127  if (ret)
128  break;
129  ret = pmbus_write_word_data(client, page,
131  break;
133  ret = pmbus_write_word_data(client, page,
134  MAX34440_MFR_VOUT_MIN, 0x7fff);
135  if (ret)
136  break;
137  ret = pmbus_write_word_data(client, page,
139  break;
141  ret = pmbus_write_word_data(client, page,
143  if (!ret && data->id == max34446)
144  ret = pmbus_write_word_data(client, page,
146 
147  break;
149  ret = pmbus_write_word_data(client, page,
151  0x8000);
152  if (!ret && data->id == max34446)
153  ret = pmbus_write_word_data(client, page,
155  break;
156  default:
157  ret = -ENODATA;
158  break;
159  }
160  return ret;
161 }
162 
163 static int max34440_read_byte_data(struct i2c_client *client, int page, int reg)
164 {
165  int ret = 0;
166  int mfg_status;
167 
168  if (page >= 0) {
169  ret = pmbus_set_page(client, page);
170  if (ret < 0)
171  return ret;
172  }
173 
174  switch (reg) {
175  case PMBUS_STATUS_IOUT:
176  mfg_status = pmbus_read_word_data(client, 0,
178  if (mfg_status < 0)
179  return mfg_status;
180  if (mfg_status & MAX34440_STATUS_OC_WARN)
181  ret |= PB_IOUT_OC_WARNING;
182  if (mfg_status & MAX34440_STATUS_OC_FAULT)
183  ret |= PB_IOUT_OC_FAULT;
184  break;
186  mfg_status = pmbus_read_word_data(client, 0,
188  if (mfg_status < 0)
189  return mfg_status;
190  if (mfg_status & MAX34440_STATUS_OT_WARN)
191  ret |= PB_TEMP_OT_WARNING;
192  if (mfg_status & MAX34440_STATUS_OT_FAULT)
193  ret |= PB_TEMP_OT_FAULT;
194  break;
195  default:
196  ret = -ENODATA;
197  break;
198  }
199  return ret;
200 }
201 
202 static struct pmbus_driver_info max34440_info[] = {
203  [max34440] = {
204  .pages = 14,
205  .format[PSC_VOLTAGE_IN] = direct,
206  .format[PSC_VOLTAGE_OUT] = direct,
207  .format[PSC_TEMPERATURE] = direct,
208  .format[PSC_CURRENT_OUT] = direct,
209  .m[PSC_VOLTAGE_IN] = 1,
210  .b[PSC_VOLTAGE_IN] = 0,
211  .R[PSC_VOLTAGE_IN] = 3, /* R = 0 in datasheet reflects mV */
212  .m[PSC_VOLTAGE_OUT] = 1,
213  .b[PSC_VOLTAGE_OUT] = 0,
214  .R[PSC_VOLTAGE_OUT] = 3, /* R = 0 in datasheet reflects mV */
215  .m[PSC_CURRENT_OUT] = 1,
216  .b[PSC_CURRENT_OUT] = 0,
217  .R[PSC_CURRENT_OUT] = 3, /* R = 0 in datasheet reflects mA */
218  .m[PSC_TEMPERATURE] = 1,
219  .b[PSC_TEMPERATURE] = 0,
220  .R[PSC_TEMPERATURE] = 2,
241  .read_byte_data = max34440_read_byte_data,
242  .read_word_data = max34440_read_word_data,
243  .write_word_data = max34440_write_word_data,
244  },
245  [max34441] = {
246  .pages = 12,
247  .format[PSC_VOLTAGE_IN] = direct,
248  .format[PSC_VOLTAGE_OUT] = direct,
249  .format[PSC_TEMPERATURE] = direct,
250  .format[PSC_CURRENT_OUT] = direct,
251  .format[PSC_FAN] = direct,
252  .m[PSC_VOLTAGE_IN] = 1,
253  .b[PSC_VOLTAGE_IN] = 0,
254  .R[PSC_VOLTAGE_IN] = 3,
255  .m[PSC_VOLTAGE_OUT] = 1,
256  .b[PSC_VOLTAGE_OUT] = 0,
257  .R[PSC_VOLTAGE_OUT] = 3,
258  .m[PSC_CURRENT_OUT] = 1,
259  .b[PSC_CURRENT_OUT] = 0,
260  .R[PSC_CURRENT_OUT] = 3,
261  .m[PSC_TEMPERATURE] = 1,
262  .b[PSC_TEMPERATURE] = 0,
263  .R[PSC_TEMPERATURE] = 2,
264  .m[PSC_FAN] = 1,
265  .b[PSC_FAN] = 0,
266  .R[PSC_FAN] = 0,
284  .read_byte_data = max34440_read_byte_data,
285  .read_word_data = max34440_read_word_data,
286  .write_word_data = max34440_write_word_data,
287  },
288  [max34446] = {
289  .pages = 7,
290  .format[PSC_VOLTAGE_IN] = direct,
291  .format[PSC_VOLTAGE_OUT] = direct,
292  .format[PSC_TEMPERATURE] = direct,
293  .format[PSC_CURRENT_OUT] = direct,
294  .format[PSC_POWER] = direct,
295  .m[PSC_VOLTAGE_IN] = 1,
296  .b[PSC_VOLTAGE_IN] = 0,
297  .R[PSC_VOLTAGE_IN] = 3,
298  .m[PSC_VOLTAGE_OUT] = 1,
299  .b[PSC_VOLTAGE_OUT] = 0,
300  .R[PSC_VOLTAGE_OUT] = 3,
301  .m[PSC_CURRENT_OUT] = 1,
302  .b[PSC_CURRENT_OUT] = 0,
303  .R[PSC_CURRENT_OUT] = 3,
304  .m[PSC_POWER] = 1,
305  .b[PSC_POWER] = 0,
306  .R[PSC_POWER] = 3,
307  .m[PSC_TEMPERATURE] = 1,
308  .b[PSC_TEMPERATURE] = 0,
309  .R[PSC_TEMPERATURE] = 2,
321  .read_byte_data = max34440_read_byte_data,
322  .read_word_data = max34440_read_word_data,
323  .write_word_data = max34440_write_word_data,
324  },
325 };
326 
327 static int max34440_probe(struct i2c_client *client,
328  const struct i2c_device_id *id)
329 {
330  struct max34440_data *data;
331 
332  data = devm_kzalloc(&client->dev, sizeof(struct max34440_data),
333  GFP_KERNEL);
334  if (!data)
335  return -ENOMEM;
336  data->id = id->driver_data;
337  data->info = max34440_info[id->driver_data];
338 
339  return pmbus_do_probe(client, id, &data->info);
340 }
341 
342 static const struct i2c_device_id max34440_id[] = {
343  {"max34440", max34440},
344  {"max34441", max34441},
345  {"max34446", max34446},
346  {}
347 };
348 MODULE_DEVICE_TABLE(i2c, max34440_id);
349 
350 /* This is the driver that will be inserted */
351 static struct i2c_driver max34440_driver = {
352  .driver = {
353  .name = "max34440",
354  },
355  .probe = max34440_probe,
356  .remove = pmbus_do_remove,
357  .id_table = max34440_id,
358 };
359 
360 module_i2c_driver(max34440_driver);
361 
362 MODULE_AUTHOR("Guenter Roeck");
363 MODULE_DESCRIPTION("PMBus driver for Maxim MAX34440/MAX34441");
364 MODULE_LICENSE("GPL");