2 * Hardware monitoring driver for LM25066 / LM5064 / LM5066
4 * Copyright (c) 2011 Ericsson AB.
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.
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.
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.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
29 enum chips
{ lm25066
, lm5064
, lm5066
};
31 #define LM25066_READ_VAUX 0xd0
32 #define LM25066_MFR_READ_IIN 0xd1
33 #define LM25066_MFR_READ_PIN 0xd2
34 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
35 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
36 #define LM25066_READ_PIN_PEAK 0xd5
37 #define LM25066_CLEAR_PIN_PEAK 0xd6
38 #define LM25066_DEVICE_SETUP 0xd9
39 #define LM25066_READ_AVG_VIN 0xdc
40 #define LM25066_READ_AVG_VOUT 0xdd
41 #define LM25066_READ_AVG_IIN 0xde
42 #define LM25066_READ_AVG_PIN 0xdf
44 #define LM25066_DEV_SETUP_CL (1 << 4) /* Current limit */
48 struct pmbus_driver_info info
;
51 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
53 static int lm25066_read_word_data(struct i2c_client
*client
, int page
, int reg
)
55 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
56 const struct lm25066_data
*data
= to_lm25066_data(info
);
62 /* Map READ_VAUX into READ_VOUT register on page 1 */
66 ret
= pmbus_read_word_data(client
, 0,
70 /* Adjust returned value to match VOUT coefficients */
73 /* VOUT: 4.54 mV VAUX: 283.2 uV LSB */
74 ret
= DIV_ROUND_CLOSEST(ret
* 2832, 45400);
77 /* VOUT: 4.53 mV VAUX: 700 uV LSB */
78 ret
= DIV_ROUND_CLOSEST(ret
* 70, 453);
81 /* VOUT: 2.18 mV VAUX: 725 uV LSB */
82 ret
= DIV_ROUND_CLOSEST(ret
* 725, 2180);
87 /* No other valid registers on page 1 */
96 ret
= pmbus_read_word_data(client
, 0, LM25066_MFR_READ_IIN
);
99 ret
= pmbus_read_word_data(client
, 0, LM25066_MFR_READ_PIN
);
101 case PMBUS_IIN_OC_WARN_LIMIT
:
102 ret
= pmbus_read_word_data(client
, 0,
103 LM25066_MFR_IIN_OC_WARN_LIMIT
);
105 case PMBUS_PIN_OP_WARN_LIMIT
:
106 ret
= pmbus_read_word_data(client
, 0,
107 LM25066_MFR_PIN_OP_WARN_LIMIT
);
109 case PMBUS_VIRT_READ_VIN_AVG
:
110 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_AVG_VIN
);
112 case PMBUS_VIRT_READ_VOUT_AVG
:
113 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_AVG_VOUT
);
115 case PMBUS_VIRT_READ_IIN_AVG
:
116 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_AVG_IIN
);
118 case PMBUS_VIRT_READ_PIN_AVG
:
119 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_AVG_PIN
);
121 case PMBUS_VIRT_READ_PIN_MAX
:
122 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_PIN_PEAK
);
124 case PMBUS_VIRT_RESET_PIN_HISTORY
:
135 static int lm25066_write_word_data(struct i2c_client
*client
, int page
, int reg
,
144 case PMBUS_IIN_OC_WARN_LIMIT
:
145 ret
= pmbus_write_word_data(client
, 0,
146 LM25066_MFR_IIN_OC_WARN_LIMIT
,
149 case PMBUS_PIN_OP_WARN_LIMIT
:
150 ret
= pmbus_write_word_data(client
, 0,
151 LM25066_MFR_PIN_OP_WARN_LIMIT
,
154 case PMBUS_VIRT_RESET_PIN_HISTORY
:
155 ret
= pmbus_write_byte(client
, 0, LM25066_CLEAR_PIN_PEAK
);
164 static int lm25066_write_byte(struct i2c_client
*client
, int page
, u8 value
)
170 return pmbus_write_byte(client
, page
, value
);
175 static int lm25066_probe(struct i2c_client
*client
,
176 const struct i2c_device_id
*id
)
180 struct lm25066_data
*data
;
181 struct pmbus_driver_info
*info
;
183 if (!i2c_check_functionality(client
->adapter
,
184 I2C_FUNC_SMBUS_READ_BYTE_DATA
))
187 data
= kzalloc(sizeof(struct lm25066_data
), GFP_KERNEL
);
191 config
= i2c_smbus_read_byte_data(client
, LM25066_DEVICE_SETUP
);
197 data
->id
= id
->driver_data
;
201 info
->format
[PSC_VOLTAGE_IN
] = direct
;
202 info
->format
[PSC_VOLTAGE_OUT
] = direct
;
203 info
->format
[PSC_CURRENT_IN
] = direct
;
204 info
->format
[PSC_TEMPERATURE
] = direct
;
205 info
->format
[PSC_POWER
] = direct
;
207 info
->m
[PSC_TEMPERATURE
] = 16;
208 info
->b
[PSC_TEMPERATURE
] = 0;
209 info
->R
[PSC_TEMPERATURE
] = 0;
211 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_VOUT
212 | PMBUS_HAVE_STATUS_VOUT
| PMBUS_HAVE_PIN
| PMBUS_HAVE_IIN
213 | PMBUS_HAVE_STATUS_INPUT
| PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
;
214 info
->func
[1] = PMBUS_HAVE_VOUT
;
216 info
->read_word_data
= lm25066_read_word_data
;
217 info
->write_word_data
= lm25066_write_word_data
;
218 info
->write_byte
= lm25066_write_byte
;
220 switch (id
->driver_data
) {
222 info
->m
[PSC_VOLTAGE_IN
] = 22070;
223 info
->b
[PSC_VOLTAGE_IN
] = 0;
224 info
->R
[PSC_VOLTAGE_IN
] = -2;
225 info
->m
[PSC_VOLTAGE_OUT
] = 22070;
226 info
->b
[PSC_VOLTAGE_OUT
] = 0;
227 info
->R
[PSC_VOLTAGE_OUT
] = -2;
229 if (config
& LM25066_DEV_SETUP_CL
) {
230 info
->m
[PSC_CURRENT_IN
] = 6852;
231 info
->b
[PSC_CURRENT_IN
] = 0;
232 info
->R
[PSC_CURRENT_IN
] = -2;
233 info
->m
[PSC_POWER
] = 369;
234 info
->b
[PSC_POWER
] = 0;
235 info
->R
[PSC_POWER
] = -2;
237 info
->m
[PSC_CURRENT_IN
] = 13661;
238 info
->b
[PSC_CURRENT_IN
] = 0;
239 info
->R
[PSC_CURRENT_IN
] = -2;
240 info
->m
[PSC_POWER
] = 736;
241 info
->b
[PSC_POWER
] = 0;
242 info
->R
[PSC_POWER
] = -2;
246 info
->m
[PSC_VOLTAGE_IN
] = 22075;
247 info
->b
[PSC_VOLTAGE_IN
] = 0;
248 info
->R
[PSC_VOLTAGE_IN
] = -2;
249 info
->m
[PSC_VOLTAGE_OUT
] = 22075;
250 info
->b
[PSC_VOLTAGE_OUT
] = 0;
251 info
->R
[PSC_VOLTAGE_OUT
] = -2;
253 if (config
& LM25066_DEV_SETUP_CL
) {
254 info
->m
[PSC_CURRENT_IN
] = 6713;
255 info
->b
[PSC_CURRENT_IN
] = 0;
256 info
->R
[PSC_CURRENT_IN
] = -2;
257 info
->m
[PSC_POWER
] = 3619;
258 info
->b
[PSC_POWER
] = 0;
259 info
->R
[PSC_POWER
] = -3;
261 info
->m
[PSC_CURRENT_IN
] = 13426;
262 info
->b
[PSC_CURRENT_IN
] = 0;
263 info
->R
[PSC_CURRENT_IN
] = -2;
264 info
->m
[PSC_POWER
] = 7238;
265 info
->b
[PSC_POWER
] = 0;
266 info
->R
[PSC_POWER
] = -3;
270 info
->m
[PSC_VOLTAGE_IN
] = 4587;
271 info
->b
[PSC_VOLTAGE_IN
] = 0;
272 info
->R
[PSC_VOLTAGE_IN
] = -2;
273 info
->m
[PSC_VOLTAGE_OUT
] = 4587;
274 info
->b
[PSC_VOLTAGE_OUT
] = 0;
275 info
->R
[PSC_VOLTAGE_OUT
] = -2;
277 if (config
& LM25066_DEV_SETUP_CL
) {
278 info
->m
[PSC_CURRENT_IN
] = 10753;
279 info
->b
[PSC_CURRENT_IN
] = 0;
280 info
->R
[PSC_CURRENT_IN
] = -2;
281 info
->m
[PSC_POWER
] = 1204;
282 info
->b
[PSC_POWER
] = 0;
283 info
->R
[PSC_POWER
] = -3;
285 info
->m
[PSC_CURRENT_IN
] = 5405;
286 info
->b
[PSC_CURRENT_IN
] = 0;
287 info
->R
[PSC_CURRENT_IN
] = -2;
288 info
->m
[PSC_POWER
] = 605;
289 info
->b
[PSC_POWER
] = 0;
290 info
->R
[PSC_POWER
] = -3;
298 ret
= pmbus_do_probe(client
, id
, info
);
308 static int lm25066_remove(struct i2c_client
*client
)
310 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
311 const struct lm25066_data
*data
= to_lm25066_data(info
);
313 pmbus_do_remove(client
);
318 static const struct i2c_device_id lm25066_id
[] = {
319 {"lm25066", lm25066
},
325 MODULE_DEVICE_TABLE(i2c
, lm25066_id
);
327 /* This is the driver that will be inserted */
328 static struct i2c_driver lm25066_driver
= {
332 .probe
= lm25066_probe
,
333 .remove
= lm25066_remove
,
334 .id_table
= lm25066_id
,
337 static int __init
lm25066_init(void)
339 return i2c_add_driver(&lm25066_driver
);
342 static void __exit
lm25066_exit(void)
344 i2c_del_driver(&lm25066_driver
);
347 MODULE_AUTHOR("Guenter Roeck");
348 MODULE_DESCRIPTION("PMBus driver for LM25066/LM5064/LM5066");
349 MODULE_LICENSE("GPL");
350 module_init(lm25066_init
);
351 module_exit(lm25066_exit
);